• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/mpeg12enc.c

Go to the documentation of this file.
00001 /*
00002  * MPEG1/2 encoder
00003  * Copyright (c) 2000,2001 Fabrice Bellard
00004  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00028 #include "avcodec.h"
00029 #include "dsputil.h"
00030 #include "mpegvideo.h"
00031 
00032 #include "mpeg12.h"
00033 #include "mpeg12data.h"
00034 #include "bytestream.h"
00035 
00036 
00037 static const uint8_t inv_non_linear_qscale[13] = {
00038     0, 2, 4, 6, 8,
00039     9,10,11,12,13,14,15,16,
00040 };
00041 
00042 static const uint8_t svcd_scan_offset_placeholder[14] = {
00043     0x10, 0x0E,
00044     0x00, 0x80, 0x81,
00045     0x00, 0x80, 0x81,
00046     0xff, 0xff, 0xff,
00047     0xff, 0xff, 0xff,
00048 };
00049 
00050 static void mpeg1_encode_block(MpegEncContext *s,
00051                          DCTELEM *block,
00052                          int component);
00053 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code);    // RAL: f_code parameter added
00054 
00055 static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
00056 static uint8_t fcode_tab[MAX_MV*2+1];
00057 
00058 static uint8_t  uni_mpeg1_ac_vlc_len [64*64*2];
00059 static uint8_t  uni_mpeg2_ac_vlc_len [64*64*2];
00060 
00061 /* simple include everything table for dc, first byte is bits number next 3 are code*/
00062 static uint32_t mpeg1_lum_dc_uni[512];
00063 static uint32_t mpeg1_chr_dc_uni[512];
00064 
00065 static uint8_t mpeg1_index_run[2][64];
00066 static int8_t mpeg1_max_level[2][64];
00067 
00068 static void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){
00069     int i;
00070 
00071     for(i=0; i<128; i++){
00072         int level= i-64;
00073         int run;
00074         for(run=0; run<64; run++){
00075             int len, bits, code;
00076 
00077             int alevel= FFABS(level);
00078             int sign= (level>>31)&1;
00079 
00080             if (alevel > rl->max_level[0][run])
00081                 code= 111; /*rl->n*/
00082             else
00083                 code= rl->index_run[0][run] + alevel - 1;
00084 
00085             if (code < 111 /* rl->n */) {
00086                 /* store the vlc & sign at once */
00087                 len=   rl->table_vlc[code][1]+1;
00088                 bits= (rl->table_vlc[code][0]<<1) + sign;
00089             } else {
00090                 len=  rl->table_vlc[111/*rl->n*/][1]+6;
00091                 bits= rl->table_vlc[111/*rl->n*/][0]<<6;
00092 
00093                 bits|= run;
00094                 if (alevel < 128) {
00095                     bits<<=8; len+=8;
00096                     bits|= level & 0xff;
00097                 } else {
00098                     bits<<=16; len+=16;
00099                     bits|= level & 0xff;
00100                     if (level < 0) {
00101                         bits|= 0x8001 + level + 255;
00102                     } else {
00103                         bits|= level & 0xffff;
00104                     }
00105                 }
00106             }
00107 
00108             uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
00109         }
00110     }
00111 }
00112 
00113 
00114 static int find_frame_rate_index(MpegEncContext *s){
00115     int i;
00116     int64_t dmin= INT64_MAX;
00117     int64_t d;
00118 
00119     for(i=1;i<14;i++) {
00120         int64_t n0= 1001LL/ff_frame_rate_tab[i].den*ff_frame_rate_tab[i].num*s->avctx->time_base.num;
00121         int64_t n1= 1001LL*s->avctx->time_base.den;
00122         if(s->avctx->strict_std_compliance > FF_COMPLIANCE_INOFFICIAL && i>=9) break;
00123 
00124         d = FFABS(n0 - n1);
00125         if(d < dmin){
00126             dmin=d;
00127             s->frame_rate_index= i;
00128         }
00129     }
00130     if(dmin)
00131         return -1;
00132     else
00133         return 0;
00134 }
00135 
00136 static av_cold int encode_init(AVCodecContext *avctx)
00137 {
00138     MpegEncContext *s = avctx->priv_data;
00139 
00140     if(MPV_encode_init(avctx) < 0)
00141         return -1;
00142 
00143     if(find_frame_rate_index(s) < 0){
00144         if(s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
00145             av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num);
00146             return -1;
00147         }else{
00148             av_log(avctx, AV_LOG_INFO, "MPEG1/2 does not support %d/%d fps, there may be AV sync issues\n", avctx->time_base.den, avctx->time_base.num);
00149         }
00150     }
00151 
00152     if(avctx->profile == FF_PROFILE_UNKNOWN){
00153         if(avctx->level != FF_LEVEL_UNKNOWN){
00154             av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
00155             return -1;
00156         }
00157         avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0; /* Main or 4:2:2 */
00158     }
00159 
00160     if(avctx->level == FF_LEVEL_UNKNOWN){
00161         if(avctx->profile == 0){ /* 4:2:2 */
00162             if(avctx->width <= 720 && avctx->height <= 608) avctx->level = 5; /* Main */
00163             else                                            avctx->level = 2; /* High */
00164         }else{
00165             if(avctx->profile != 1 && s->chroma_format != CHROMA_420){
00166                 av_log(avctx, AV_LOG_ERROR, "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
00167                 return -1;
00168             }
00169             if(avctx->width <= 720 && avctx->height <= 576) avctx->level = 8; /* Main */
00170             else if(avctx->width <= 1440)                   avctx->level = 6; /* High 1440 */
00171             else                                            avctx->level = 4; /* High */
00172         }
00173     }
00174 
00175     if((avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE) && s->frame_rate_index != 4){
00176         av_log(avctx, AV_LOG_ERROR, "Drop frame time code only allowed with 1001/30000 fps\n");
00177         return -1;
00178     }
00179 
00180     return 0;
00181 }
00182 
00183 static void put_header(MpegEncContext *s, int header)
00184 {
00185     align_put_bits(&s->pb);
00186     put_bits(&s->pb, 16, header>>16);
00187     put_sbits(&s->pb, 16, header);
00188 }
00189 
00190 /* put sequence header if needed */
00191 static void mpeg1_encode_sequence_header(MpegEncContext *s)
00192 {
00193         unsigned int vbv_buffer_size;
00194         unsigned int fps, v;
00195         int i;
00196         uint64_t time_code;
00197         float best_aspect_error= 1E10;
00198         float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
00199         int constraint_parameter_flag;
00200 
00201         if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA)
00202 
00203         if (s->current_picture.key_frame) {
00204             AVRational framerate= ff_frame_rate_tab[s->frame_rate_index];
00205 
00206             /* mpeg1 header repeated every gop */
00207             put_header(s, SEQ_START_CODE);
00208 
00209             put_sbits(&s->pb, 12, s->width );
00210             put_sbits(&s->pb, 12, s->height);
00211 
00212             for(i=1; i<15; i++){
00213                 float error= aspect_ratio;
00214                 if(s->codec_id == CODEC_ID_MPEG1VIDEO || i <=1)
00215                     error-= 1.0/ff_mpeg1_aspect[i];
00216                 else
00217                     error-= av_q2d(ff_mpeg2_aspect[i])*s->height/s->width;
00218 
00219                 error= FFABS(error);
00220 
00221                 if(error < best_aspect_error){
00222                     best_aspect_error= error;
00223                     s->aspect_ratio_info= i;
00224                 }
00225             }
00226 
00227             put_bits(&s->pb, 4, s->aspect_ratio_info);
00228             put_bits(&s->pb, 4, s->frame_rate_index);
00229 
00230             if(s->avctx->rc_max_rate){
00231                 v = (s->avctx->rc_max_rate + 399) / 400;
00232                 if (v > 0x3ffff && s->codec_id == CODEC_ID_MPEG1VIDEO)
00233                     v = 0x3ffff;
00234             }else{
00235                 v= 0x3FFFF;
00236             }
00237 
00238             if(s->avctx->rc_buffer_size)
00239                 vbv_buffer_size = s->avctx->rc_buffer_size;
00240             else
00241                 /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
00242                 vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
00243             vbv_buffer_size= (vbv_buffer_size + 16383) / 16384;
00244 
00245             put_sbits(&s->pb, 18, v);
00246             put_bits(&s->pb, 1, 1); /* marker */
00247             put_sbits(&s->pb, 10, vbv_buffer_size);
00248 
00249             constraint_parameter_flag=
00250                 s->width <= 768 && s->height <= 576 &&
00251                 s->mb_width * s->mb_height <= 396 &&
00252                 s->mb_width * s->mb_height * framerate.num <= framerate.den*396*25 &&
00253                 framerate.num <= framerate.den*30 &&
00254                 s->avctx->me_range && s->avctx->me_range < 128 &&
00255                 vbv_buffer_size <= 20 &&
00256                 v <= 1856000/400 &&
00257                 s->codec_id == CODEC_ID_MPEG1VIDEO;
00258 
00259             put_bits(&s->pb, 1, constraint_parameter_flag);
00260 
00261             ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
00262             ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
00263 
00264             if(s->codec_id == CODEC_ID_MPEG2VIDEO){
00265                 put_header(s, EXT_START_CODE);
00266                 put_bits(&s->pb, 4, 1); //seq ext
00267 
00268                 put_bits(&s->pb, 1, s->avctx->profile == 0); //escx 1 for 4:2:2 profile */
00269 
00270                 put_bits(&s->pb, 3, s->avctx->profile); //profile
00271                 put_bits(&s->pb, 4, s->avctx->level); //level
00272 
00273                 put_bits(&s->pb, 1, s->progressive_sequence);
00274                 put_bits(&s->pb, 2, s->chroma_format);
00275                 put_bits(&s->pb, 2, s->width >>12);
00276                 put_bits(&s->pb, 2, s->height>>12);
00277                 put_bits(&s->pb, 12, v>>18); //bitrate ext
00278                 put_bits(&s->pb, 1, 1); //marker
00279                 put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext
00280                 put_bits(&s->pb, 1, s->low_delay);
00281                 put_bits(&s->pb, 2, 0); // frame_rate_ext_n
00282                 put_bits(&s->pb, 5, 0); // frame_rate_ext_d
00283             }
00284 
00285             put_header(s, GOP_START_CODE);
00286             put_bits(&s->pb, 1, !!(s->avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE)); /* drop frame flag */
00287             /* time code : we must convert from the real frame rate to a
00288                fake mpeg frame rate in case of low frame rate */
00289             fps = (framerate.num + framerate.den/2)/ framerate.den;
00290             time_code = s->current_picture_ptr->coded_picture_number + s->avctx->timecode_frame_start;
00291 
00292             s->gop_picture_number = s->current_picture_ptr->coded_picture_number;
00293             if (s->avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE) {
00294                 /* only works for NTSC 29.97 */
00295                 int d = time_code / 17982;
00296                 int m = time_code % 17982;
00297                 //if (m < 2) m += 2; /* not needed since -2,-1 / 1798 in C returns 0 */
00298                 time_code += 18 * d + 2 * ((m - 2) / 1798);
00299             }
00300             put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
00301             put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
00302             put_bits(&s->pb, 1, 1);
00303             put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
00304             put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
00305             put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
00306             put_bits(&s->pb, 1, 0); /* broken link */
00307         }
00308 }
00309 
00310 static inline void encode_mb_skip_run(MpegEncContext *s, int run){
00311     while (run >= 33) {
00312         put_bits(&s->pb, 11, 0x008);
00313         run -= 33;
00314     }
00315     put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1],
00316              ff_mpeg12_mbAddrIncrTable[run][0]);
00317 }
00318 
00319 static av_always_inline void put_qscale(MpegEncContext *s)
00320 {
00321     if(s->q_scale_type){
00322         assert(s->qscale>=1 && s->qscale <=12);
00323         put_bits(&s->pb, 5, inv_non_linear_qscale[s->qscale]);
00324     }else{
00325         put_bits(&s->pb, 5, s->qscale);
00326     }
00327 }
00328 
00329 void ff_mpeg1_encode_slice_header(MpegEncContext *s){
00330     put_header(s, SLICE_MIN_START_CODE + s->mb_y);
00331     put_qscale(s);
00332     put_bits(&s->pb, 1, 0); /* slice extra information */
00333 }
00334 
00335 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
00336 {
00337     mpeg1_encode_sequence_header(s);
00338 
00339     /* mpeg1 picture header */
00340     put_header(s, PICTURE_START_CODE);
00341     /* temporal reference */
00342 
00343     // RAL: s->picture_number instead of s->fake_picture_number
00344     put_bits(&s->pb, 10, (s->picture_number -
00345                           s->gop_picture_number) & 0x3ff);
00346     put_bits(&s->pb, 3, s->pict_type);
00347 
00348     s->vbv_delay_ptr= s->pb.buf + put_bits_count(&s->pb)/8;
00349     put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
00350 
00351     // RAL: Forward f_code also needed for B frames
00352     if (s->pict_type == FF_P_TYPE || s->pict_type == FF_B_TYPE) {
00353         put_bits(&s->pb, 1, 0); /* half pel coordinates */
00354         if(s->codec_id == CODEC_ID_MPEG1VIDEO)
00355             put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
00356         else
00357             put_bits(&s->pb, 3, 7); /* forward_f_code */
00358     }
00359 
00360     // RAL: Backward f_code necessary for B frames
00361     if (s->pict_type == FF_B_TYPE) {
00362         put_bits(&s->pb, 1, 0); /* half pel coordinates */
00363         if(s->codec_id == CODEC_ID_MPEG1VIDEO)
00364             put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
00365         else
00366             put_bits(&s->pb, 3, 7); /* backward_f_code */
00367     }
00368 
00369     put_bits(&s->pb, 1, 0); /* extra bit picture */
00370 
00371     s->frame_pred_frame_dct = 1;
00372     if(s->codec_id == CODEC_ID_MPEG2VIDEO){
00373         put_header(s, EXT_START_CODE);
00374         put_bits(&s->pb, 4, 8); //pic ext
00375         if (s->pict_type == FF_P_TYPE || s->pict_type == FF_B_TYPE) {
00376             put_bits(&s->pb, 4, s->f_code);
00377             put_bits(&s->pb, 4, s->f_code);
00378         }else{
00379             put_bits(&s->pb, 8, 255);
00380         }
00381         if (s->pict_type == FF_B_TYPE) {
00382             put_bits(&s->pb, 4, s->b_code);
00383             put_bits(&s->pb, 4, s->b_code);
00384         }else{
00385             put_bits(&s->pb, 8, 255);
00386         }
00387         put_bits(&s->pb, 2, s->intra_dc_precision);
00388 
00389         assert(s->picture_structure == PICT_FRAME);
00390         put_bits(&s->pb, 2, s->picture_structure);
00391         if (s->progressive_sequence) {
00392             put_bits(&s->pb, 1, 0); /* no repeat */
00393         } else {
00394             put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
00395         }
00396         /* XXX: optimize the generation of this flag with entropy
00397            measures */
00398         s->frame_pred_frame_dct = s->progressive_sequence;
00399 
00400         put_bits(&s->pb, 1, s->frame_pred_frame_dct);
00401         put_bits(&s->pb, 1, s->concealment_motion_vectors);
00402         put_bits(&s->pb, 1, s->q_scale_type);
00403         put_bits(&s->pb, 1, s->intra_vlc_format);
00404         put_bits(&s->pb, 1, s->alternate_scan);
00405         put_bits(&s->pb, 1, s->repeat_first_field);
00406         s->progressive_frame = s->progressive_sequence;
00407         put_bits(&s->pb, 1, s->chroma_format == CHROMA_420 ? s->progressive_frame : 0); /* chroma_420_type */
00408         put_bits(&s->pb, 1, s->progressive_frame);
00409         put_bits(&s->pb, 1, 0); //composite_display_flag
00410     }
00411     if(s->flags & CODEC_FLAG_SVCD_SCAN_OFFSET){
00412         int i;
00413 
00414         put_header(s, USER_START_CODE);
00415         for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){
00416             put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
00417         }
00418     }
00419 
00420     s->mb_y=0;
00421     ff_mpeg1_encode_slice_header(s);
00422 }
00423 
00424 static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
00425                                 int has_mv, int field_motion)
00426 {
00427     put_bits(&s->pb, n, bits);
00428     if (!s->frame_pred_frame_dct) {
00429         if (has_mv)
00430             put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */
00431         put_bits(&s->pb, 1, s->interlaced_dct);
00432     }
00433 }
00434 
00435 static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
00436                                                    DCTELEM block[6][64],
00437                                                    int motion_x, int motion_y,
00438                                                    int mb_block_count)
00439 {
00440     int i, cbp;
00441     const int mb_x = s->mb_x;
00442     const int mb_y = s->mb_y;
00443     const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
00444 
00445     /* compute cbp */
00446     cbp = 0;
00447     for(i=0;i<mb_block_count;i++) {
00448         if (s->block_last_index[i] >= 0)
00449             cbp |= 1 << (mb_block_count - 1 - i);
00450     }
00451 
00452     if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
00453         (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) &&
00454         ((s->pict_type == FF_P_TYPE && (motion_x | motion_y) == 0) ||
00455         (s->pict_type == FF_B_TYPE && s->mv_dir == s->last_mv_dir && (((s->mv_dir & MV_DIR_FORWARD) ? ((s->mv[0][0][0] - s->last_mv[0][0][0])|(s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
00456         ((s->mv_dir & MV_DIR_BACKWARD) ? ((s->mv[1][0][0] - s->last_mv[1][0][0])|(s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
00457         s->mb_skip_run++;
00458         s->qscale -= s->dquant;
00459         s->skip_count++;
00460         s->misc_bits++;
00461         s->last_bits++;
00462         if(s->pict_type == FF_P_TYPE){
00463             s->last_mv[0][1][0]= s->last_mv[0][0][0]=
00464             s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0;
00465         }
00466     } else {
00467         if(first_mb){
00468             assert(s->mb_skip_run == 0);
00469             encode_mb_skip_run(s, s->mb_x);
00470         }else{
00471             encode_mb_skip_run(s, s->mb_skip_run);
00472         }
00473 
00474         if (s->pict_type == FF_I_TYPE) {
00475             if(s->dquant && cbp){
00476                 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_type : macroblock_quant = 1 */
00477                 put_qscale(s);
00478             }else{
00479                 put_mb_modes(s, 1, 1, 0, 0); /* macroblock_type : macroblock_quant = 0 */
00480                 s->qscale -= s->dquant;
00481             }
00482             s->misc_bits+= get_bits_diff(s);
00483             s->i_count++;
00484         } else if (s->mb_intra) {
00485             if(s->dquant && cbp){
00486                 put_mb_modes(s, 6, 0x01, 0, 0);
00487                 put_qscale(s);
00488             }else{
00489                 put_mb_modes(s, 5, 0x03, 0, 0);
00490                 s->qscale -= s->dquant;
00491             }
00492             s->misc_bits+= get_bits_diff(s);
00493             s->i_count++;
00494             memset(s->last_mv, 0, sizeof(s->last_mv));
00495         } else if (s->pict_type == FF_P_TYPE) {
00496             if(s->mv_type == MV_TYPE_16X16){
00497                 if (cbp != 0) {
00498                     if ((motion_x|motion_y) == 0) {
00499                         if(s->dquant){
00500                             put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */
00501                             put_qscale(s);
00502                         }else{
00503                             put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */
00504                         }
00505                         s->misc_bits+= get_bits_diff(s);
00506                     } else {
00507                         if(s->dquant){
00508                             put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
00509                             put_qscale(s);
00510                         }else{
00511                             put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
00512                         }
00513                         s->misc_bits+= get_bits_diff(s);
00514                         mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);    // RAL: f_code parameter added
00515                         mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);    // RAL: f_code parameter added
00516                         s->mv_bits+= get_bits_diff(s);
00517                     }
00518                 } else {
00519                     put_bits(&s->pb, 3, 1); /* motion only */
00520                     if (!s->frame_pred_frame_dct)
00521                         put_bits(&s->pb, 2, 2); /* motion_type: frame */
00522                     s->misc_bits+= get_bits_diff(s);
00523                     mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);    // RAL: f_code parameter added
00524                     mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);    // RAL: f_code parameter added
00525                     s->qscale -= s->dquant;
00526                     s->mv_bits+= get_bits_diff(s);
00527                 }
00528                 s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x;
00529                 s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y;
00530             }else{
00531                 assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
00532 
00533                 if (cbp) {
00534                     if(s->dquant){
00535                         put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
00536                         put_qscale(s);
00537                     }else{
00538                         put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
00539                     }
00540                 } else {
00541                     put_bits(&s->pb, 3, 1); /* motion only */
00542                     put_bits(&s->pb, 2, 1); /* motion_type: field */
00543                     s->qscale -= s->dquant;
00544                 }
00545                 s->misc_bits+= get_bits_diff(s);
00546                 for(i=0; i<2; i++){
00547                     put_bits(&s->pb, 1, s->field_select[0][i]);
00548                     mpeg1_encode_motion(s, s->mv[0][i][0] -  s->last_mv[0][i][0]    , s->f_code);
00549                     mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
00550                     s->last_mv[0][i][0]=   s->mv[0][i][0];
00551                     s->last_mv[0][i][1]= 2*s->mv[0][i][1];
00552                 }
00553                 s->mv_bits+= get_bits_diff(s);
00554             }
00555             if(cbp) {
00556                 if (s->chroma_y_shift) {
00557                     put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
00558                 } else {
00559                     put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
00560                     put_sbits(&s->pb, 2, cbp);
00561                 }
00562             }
00563             s->f_count++;
00564         } else{
00565             if(s->mv_type == MV_TYPE_16X16){
00566                 if (cbp){    // With coded bloc pattern
00567                     if (s->dquant) {
00568                         if(s->mv_dir == MV_DIR_FORWARD)
00569                             put_mb_modes(s, 6, 3, 1, 0);
00570                         else
00571                             put_mb_modes(s, 8-s->mv_dir, 2, 1, 0);
00572                         put_qscale(s);
00573                     } else {
00574                         put_mb_modes(s, 5-s->mv_dir, 3, 1, 0);
00575                     }
00576                 }else{    // No coded bloc pattern
00577                     put_bits(&s->pb, 5-s->mv_dir, 2);
00578                     if (!s->frame_pred_frame_dct)
00579                         put_bits(&s->pb, 2, 2); /* motion_type: frame */
00580                     s->qscale -= s->dquant;
00581                 }
00582                 s->misc_bits += get_bits_diff(s);
00583                 if (s->mv_dir&MV_DIR_FORWARD){
00584                     mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
00585                     mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
00586                     s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0];
00587                     s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1];
00588                     s->f_count++;
00589                 }
00590                 if (s->mv_dir&MV_DIR_BACKWARD){
00591                     mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
00592                     mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
00593                     s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0];
00594                     s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1];
00595                     s->b_count++;
00596                 }
00597             }else{
00598                 assert(s->mv_type == MV_TYPE_FIELD);
00599                 assert(!s->frame_pred_frame_dct);
00600                 if (cbp){    // With coded bloc pattern
00601                     if (s->dquant) {
00602                         if(s->mv_dir == MV_DIR_FORWARD)
00603                             put_mb_modes(s, 6, 3, 1, 1);
00604                         else
00605                             put_mb_modes(s, 8-s->mv_dir, 2, 1, 1);
00606                         put_qscale(s);
00607                     } else {
00608                         put_mb_modes(s, 5-s->mv_dir, 3, 1, 1);
00609                     }
00610                 }else{    // No coded bloc pattern
00611                     put_bits(&s->pb, 5-s->mv_dir, 2);
00612                     put_bits(&s->pb, 2, 1); /* motion_type: field */
00613                     s->qscale -= s->dquant;
00614                 }
00615                 s->misc_bits += get_bits_diff(s);
00616                 if (s->mv_dir&MV_DIR_FORWARD){
00617                     for(i=0; i<2; i++){
00618                         put_bits(&s->pb, 1, s->field_select[0][i]);
00619                         mpeg1_encode_motion(s, s->mv[0][i][0] -  s->last_mv[0][i][0]    , s->f_code);
00620                         mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
00621                         s->last_mv[0][i][0]=   s->mv[0][i][0];
00622                         s->last_mv[0][i][1]= 2*s->mv[0][i][1];
00623                     }
00624                     s->f_count++;
00625                 }
00626                 if (s->mv_dir&MV_DIR_BACKWARD){
00627                     for(i=0; i<2; i++){
00628                         put_bits(&s->pb, 1, s->field_select[1][i]);
00629                         mpeg1_encode_motion(s, s->mv[1][i][0] -  s->last_mv[1][i][0]    , s->b_code);
00630                         mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code);
00631                         s->last_mv[1][i][0]=   s->mv[1][i][0];
00632                         s->last_mv[1][i][1]= 2*s->mv[1][i][1];
00633                     }
00634                     s->b_count++;
00635                 }
00636             }
00637             s->mv_bits += get_bits_diff(s);
00638             if(cbp) {
00639                 if (s->chroma_y_shift) {
00640                     put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
00641                 } else {
00642                     put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
00643                     put_sbits(&s->pb, 2, cbp);
00644                 }
00645             }
00646         }
00647         for(i=0;i<mb_block_count;i++) {
00648             if (cbp & (1 << (mb_block_count - 1 - i))) {
00649                 mpeg1_encode_block(s, block[i], i);
00650             }
00651         }
00652         s->mb_skip_run = 0;
00653         if(s->mb_intra)
00654             s->i_tex_bits+= get_bits_diff(s);
00655         else
00656             s->p_tex_bits+= get_bits_diff(s);
00657     }
00658 }
00659 
00660 void mpeg1_encode_mb(MpegEncContext *s, DCTELEM block[6][64], int motion_x, int motion_y)
00661 {
00662     if (s->chroma_format == CHROMA_420) mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
00663     else                                mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
00664 }
00665 
00666 // RAL: Parameter added: f_or_b_code
00667 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
00668 {
00669     int code, bit_size, l, bits, range, sign;
00670 
00671     if (val == 0) {
00672         /* zero vector */
00673         code = 0;
00674         put_bits(&s->pb,
00675                  ff_mpeg12_mbMotionVectorTable[0][1],
00676                  ff_mpeg12_mbMotionVectorTable[0][0]);
00677     } else {
00678         bit_size = f_or_b_code - 1;
00679         range = 1 << bit_size;
00680         /* modulo encoding */
00681         l= INT_BIT - 5 - bit_size;
00682         val= (val<<l)>>l;
00683 
00684         if (val >= 0) {
00685             val--;
00686             code = (val >> bit_size) + 1;
00687             bits = val & (range - 1);
00688             sign = 0;
00689         } else {
00690             val = -val;
00691             val--;
00692             code = (val >> bit_size) + 1;
00693             bits = val & (range - 1);
00694             sign = 1;
00695         }
00696 
00697         assert(code > 0 && code <= 16);
00698 
00699         put_bits(&s->pb,
00700                  ff_mpeg12_mbMotionVectorTable[code][1],
00701                  ff_mpeg12_mbMotionVectorTable[code][0]);
00702 
00703         put_bits(&s->pb, 1, sign);
00704         if (bit_size > 0) {
00705             put_bits(&s->pb, bit_size, bits);
00706         }
00707     }
00708 }
00709 
00710 void ff_mpeg1_encode_init(MpegEncContext *s)
00711 {
00712     static int done=0;
00713 
00714     ff_mpeg12_common_init(s);
00715 
00716     if(!done){
00717         int f_code;
00718         int mv;
00719         int i;
00720 
00721         done=1;
00722         init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
00723         init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
00724 
00725         for(i=0; i<64; i++)
00726         {
00727                 mpeg1_max_level[0][i]= ff_rl_mpeg1.max_level[0][i];
00728                 mpeg1_index_run[0][i]= ff_rl_mpeg1.index_run[0][i];
00729         }
00730 
00731         init_uni_ac_vlc(&ff_rl_mpeg1, uni_mpeg1_ac_vlc_len);
00732         if(s->intra_vlc_format)
00733             init_uni_ac_vlc(&ff_rl_mpeg2, uni_mpeg2_ac_vlc_len);
00734 
00735         /* build unified dc encoding tables */
00736         for(i=-255; i<256; i++)
00737         {
00738                 int adiff, index;
00739                 int bits, code;
00740                 int diff=i;
00741 
00742                 adiff = FFABS(diff);
00743                 if(diff<0) diff--;
00744                 index = av_log2(2*adiff);
00745 
00746                 bits= ff_mpeg12_vlc_dc_lum_bits[index] + index;
00747                 code= (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
00748                 mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
00749 
00750                 bits= ff_mpeg12_vlc_dc_chroma_bits[index] + index;
00751                 code= (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
00752                 mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
00753         }
00754 
00755         for(f_code=1; f_code<=MAX_FCODE; f_code++){
00756             for(mv=-MAX_MV; mv<=MAX_MV; mv++){
00757                 int len;
00758 
00759                 if(mv==0) len= ff_mpeg12_mbMotionVectorTable[0][1];
00760                 else{
00761                     int val, bit_size, range, code;
00762 
00763                     bit_size = f_code - 1;
00764                     range = 1 << bit_size;
00765 
00766                     val=mv;
00767                     if (val < 0)
00768                         val = -val;
00769                     val--;
00770                     code = (val >> bit_size) + 1;
00771                     if(code<17){
00772                         len= ff_mpeg12_mbMotionVectorTable[code][1] + 1 + bit_size;
00773                     }else{
00774                         len= ff_mpeg12_mbMotionVectorTable[16][1] + 2 + bit_size;
00775                     }
00776                 }
00777 
00778                 mv_penalty[f_code][mv+MAX_MV]= len;
00779             }
00780         }
00781 
00782 
00783         for(f_code=MAX_FCODE; f_code>0; f_code--){
00784             for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
00785                 fcode_tab[mv+MAX_MV]= f_code;
00786             }
00787         }
00788     }
00789     s->me.mv_penalty= mv_penalty;
00790     s->fcode_tab= fcode_tab;
00791     if(s->codec_id == CODEC_ID_MPEG1VIDEO){
00792         s->min_qcoeff=-255;
00793         s->max_qcoeff= 255;
00794     }else{
00795         s->min_qcoeff=-2047;
00796         s->max_qcoeff= 2047;
00797     }
00798     if (s->intra_vlc_format) {
00799         s->intra_ac_vlc_length=
00800         s->intra_ac_vlc_last_length= uni_mpeg2_ac_vlc_len;
00801     } else {
00802         s->intra_ac_vlc_length=
00803         s->intra_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
00804     }
00805     s->inter_ac_vlc_length=
00806     s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
00807 }
00808 
00809 static inline void encode_dc(MpegEncContext *s, int diff, int component)
00810 {
00811   if(((unsigned) (diff+255)) >= 511){
00812         int index;
00813 
00814         if(diff<0){
00815             index= av_log2_16bit(-2*diff);
00816             diff--;
00817         }else{
00818             index= av_log2_16bit(2*diff);
00819         }
00820         if (component == 0) {
00821             put_bits(
00822                 &s->pb,
00823                 ff_mpeg12_vlc_dc_lum_bits[index] + index,
00824                 (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)));
00825         }else{
00826             put_bits(
00827                 &s->pb,
00828                 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
00829                 (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)));
00830         }
00831   }else{
00832     if (component == 0) {
00833         put_bits(
00834             &s->pb,
00835             mpeg1_lum_dc_uni[diff+255]&0xFF,
00836             mpeg1_lum_dc_uni[diff+255]>>8);
00837     } else {
00838         put_bits(
00839             &s->pb,
00840             mpeg1_chr_dc_uni[diff+255]&0xFF,
00841             mpeg1_chr_dc_uni[diff+255]>>8);
00842     }
00843   }
00844 }
00845 
00846 static void mpeg1_encode_block(MpegEncContext *s,
00847                                DCTELEM *block,
00848                                int n)
00849 {
00850     int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
00851     int code, component;
00852     const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
00853 
00854     last_index = s->block_last_index[n];
00855 
00856     /* DC coef */
00857     if (s->mb_intra) {
00858         component = (n <= 3 ? 0 : (n&1) + 1);
00859         dc = block[0]; /* overflow is impossible */
00860         diff = dc - s->last_dc[component];
00861         encode_dc(s, diff, component);
00862         s->last_dc[component] = dc;
00863         i = 1;
00864         if (s->intra_vlc_format)
00865             table_vlc = ff_rl_mpeg2.table_vlc;
00866     } else {
00867         /* encode the first coefficient : needs to be done here because
00868            it is handled slightly differently */
00869         level = block[0];
00870         if (abs(level) == 1) {
00871                 code = ((uint32_t)level >> 31); /* the sign bit */
00872                 put_bits(&s->pb, 2, code | 0x02);
00873                 i = 1;
00874         } else {
00875             i = 0;
00876             last_non_zero = -1;
00877             goto next_coef;
00878         }
00879     }
00880 
00881     /* now quantify & encode AC coefs */
00882     last_non_zero = i - 1;
00883 
00884     for(;i<=last_index;i++) {
00885         j = s->intra_scantable.permutated[i];
00886         level = block[j];
00887     next_coef:
00888 #if 0
00889         if (level != 0)
00890             dprintf(s->avctx, "level[%d]=%d\n", i, level);
00891 #endif
00892         /* encode using VLC */
00893         if (level != 0) {
00894             run = i - last_non_zero - 1;
00895 
00896             alevel= level;
00897             MASK_ABS(sign, alevel)
00898             sign&=1;
00899 
00900             if (alevel <= mpeg1_max_level[0][run]){
00901                 code= mpeg1_index_run[0][run] + alevel - 1;
00902                 /* store the vlc & sign at once */
00903                 put_bits(&s->pb, table_vlc[code][1]+1, (table_vlc[code][0]<<1) + sign);
00904             } else {
00905                 /* escape seems to be pretty rare <5% so I do not optimize it */
00906                 put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
00907                 /* escape: only clip in this case */
00908                 put_bits(&s->pb, 6, run);
00909                 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
00910                     if (alevel < 128) {
00911                         put_sbits(&s->pb, 8, level);
00912                     } else {
00913                         if (level < 0) {
00914                             put_bits(&s->pb, 16, 0x8001 + level + 255);
00915                         } else {
00916                             put_sbits(&s->pb, 16, level);
00917                         }
00918                     }
00919                 }else{
00920                     put_sbits(&s->pb, 12, level);
00921                 }
00922             }
00923             last_non_zero = i;
00924         }
00925     }
00926     /* end of block */
00927     put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
00928 }
00929 
00930 AVCodec mpeg1video_encoder = {
00931     "mpeg1video",
00932     CODEC_TYPE_VIDEO,
00933     CODEC_ID_MPEG1VIDEO,
00934     sizeof(MpegEncContext),
00935     encode_init,
00936     MPV_encode_picture,
00937     MPV_encode_end,
00938     .supported_framerates= ff_frame_rate_tab+1,
00939     .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
00940     .capabilities= CODEC_CAP_DELAY,
00941     .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
00942 };
00943 
00944 AVCodec mpeg2video_encoder = {
00945     "mpeg2video",
00946     CODEC_TYPE_VIDEO,
00947     CODEC_ID_MPEG2VIDEO,
00948     sizeof(MpegEncContext),
00949     encode_init,
00950     MPV_encode_picture,
00951     MPV_encode_end,
00952     .supported_framerates= ff_frame_rate_tab+1,
00953     .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE},
00954     .capabilities= CODEC_CAP_DELAY,
00955     .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
00956 };

Generated on Tue Nov 4 2014 12:59:22 for ffmpeg by  doxygen 1.7.1