Libav
|
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 if (s->height > 2800) { 00331 put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127)); 00332 put_bits(&s->pb, 3, s->mb_y >> 7); /* slice_vertical_position_extension */ 00333 } else { 00334 put_header(s, SLICE_MIN_START_CODE + s->mb_y); 00335 } 00336 put_qscale(s); 00337 put_bits(&s->pb, 1, 0); /* slice extra information */ 00338 } 00339 00340 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number) 00341 { 00342 mpeg1_encode_sequence_header(s); 00343 00344 /* mpeg1 picture header */ 00345 put_header(s, PICTURE_START_CODE); 00346 /* temporal reference */ 00347 00348 // RAL: s->picture_number instead of s->fake_picture_number 00349 put_bits(&s->pb, 10, (s->picture_number - 00350 s->gop_picture_number) & 0x3ff); 00351 put_bits(&s->pb, 3, s->pict_type); 00352 00353 s->vbv_delay_ptr= s->pb.buf + put_bits_count(&s->pb)/8; 00354 put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */ 00355 00356 // RAL: Forward f_code also needed for B frames 00357 if (s->pict_type == FF_P_TYPE || s->pict_type == FF_B_TYPE) { 00358 put_bits(&s->pb, 1, 0); /* half pel coordinates */ 00359 if(s->codec_id == CODEC_ID_MPEG1VIDEO) 00360 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */ 00361 else 00362 put_bits(&s->pb, 3, 7); /* forward_f_code */ 00363 } 00364 00365 // RAL: Backward f_code necessary for B frames 00366 if (s->pict_type == FF_B_TYPE) { 00367 put_bits(&s->pb, 1, 0); /* half pel coordinates */ 00368 if(s->codec_id == CODEC_ID_MPEG1VIDEO) 00369 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */ 00370 else 00371 put_bits(&s->pb, 3, 7); /* backward_f_code */ 00372 } 00373 00374 put_bits(&s->pb, 1, 0); /* extra bit picture */ 00375 00376 s->frame_pred_frame_dct = 1; 00377 if(s->codec_id == CODEC_ID_MPEG2VIDEO){ 00378 put_header(s, EXT_START_CODE); 00379 put_bits(&s->pb, 4, 8); //pic ext 00380 if (s->pict_type == FF_P_TYPE || s->pict_type == FF_B_TYPE) { 00381 put_bits(&s->pb, 4, s->f_code); 00382 put_bits(&s->pb, 4, s->f_code); 00383 }else{ 00384 put_bits(&s->pb, 8, 255); 00385 } 00386 if (s->pict_type == FF_B_TYPE) { 00387 put_bits(&s->pb, 4, s->b_code); 00388 put_bits(&s->pb, 4, s->b_code); 00389 }else{ 00390 put_bits(&s->pb, 8, 255); 00391 } 00392 put_bits(&s->pb, 2, s->intra_dc_precision); 00393 00394 assert(s->picture_structure == PICT_FRAME); 00395 put_bits(&s->pb, 2, s->picture_structure); 00396 if (s->progressive_sequence) { 00397 put_bits(&s->pb, 1, 0); /* no repeat */ 00398 } else { 00399 put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first); 00400 } 00401 /* XXX: optimize the generation of this flag with entropy 00402 measures */ 00403 s->frame_pred_frame_dct = s->progressive_sequence; 00404 00405 put_bits(&s->pb, 1, s->frame_pred_frame_dct); 00406 put_bits(&s->pb, 1, s->concealment_motion_vectors); 00407 put_bits(&s->pb, 1, s->q_scale_type); 00408 put_bits(&s->pb, 1, s->intra_vlc_format); 00409 put_bits(&s->pb, 1, s->alternate_scan); 00410 put_bits(&s->pb, 1, s->repeat_first_field); 00411 s->progressive_frame = s->progressive_sequence; 00412 put_bits(&s->pb, 1, s->chroma_format == CHROMA_420 ? s->progressive_frame : 0); /* chroma_420_type */ 00413 put_bits(&s->pb, 1, s->progressive_frame); 00414 put_bits(&s->pb, 1, 0); //composite_display_flag 00415 } 00416 if(s->flags & CODEC_FLAG_SVCD_SCAN_OFFSET){ 00417 int i; 00418 00419 put_header(s, USER_START_CODE); 00420 for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){ 00421 put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]); 00422 } 00423 } 00424 00425 s->mb_y=0; 00426 ff_mpeg1_encode_slice_header(s); 00427 } 00428 00429 static inline void put_mb_modes(MpegEncContext *s, int n, int bits, 00430 int has_mv, int field_motion) 00431 { 00432 put_bits(&s->pb, n, bits); 00433 if (!s->frame_pred_frame_dct) { 00434 if (has_mv) 00435 put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */ 00436 put_bits(&s->pb, 1, s->interlaced_dct); 00437 } 00438 } 00439 00440 static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s, 00441 DCTELEM block[6][64], 00442 int motion_x, int motion_y, 00443 int mb_block_count) 00444 { 00445 int i, cbp; 00446 const int mb_x = s->mb_x; 00447 const int mb_y = s->mb_y; 00448 const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y; 00449 00450 /* compute cbp */ 00451 cbp = 0; 00452 for(i=0;i<mb_block_count;i++) { 00453 if (s->block_last_index[i] >= 0) 00454 cbp |= 1 << (mb_block_count - 1 - i); 00455 } 00456 00457 if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 && 00458 (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) && 00459 ((s->pict_type == FF_P_TYPE && (motion_x | motion_y) == 0) || 00460 (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) | 00461 ((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))) { 00462 s->mb_skip_run++; 00463 s->qscale -= s->dquant; 00464 s->skip_count++; 00465 s->misc_bits++; 00466 s->last_bits++; 00467 if(s->pict_type == FF_P_TYPE){ 00468 s->last_mv[0][1][0]= s->last_mv[0][0][0]= 00469 s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0; 00470 } 00471 } else { 00472 if(first_mb){ 00473 assert(s->mb_skip_run == 0); 00474 encode_mb_skip_run(s, s->mb_x); 00475 }else{ 00476 encode_mb_skip_run(s, s->mb_skip_run); 00477 } 00478 00479 if (s->pict_type == FF_I_TYPE) { 00480 if(s->dquant && cbp){ 00481 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_type : macroblock_quant = 1 */ 00482 put_qscale(s); 00483 }else{ 00484 put_mb_modes(s, 1, 1, 0, 0); /* macroblock_type : macroblock_quant = 0 */ 00485 s->qscale -= s->dquant; 00486 } 00487 s->misc_bits+= get_bits_diff(s); 00488 s->i_count++; 00489 } else if (s->mb_intra) { 00490 if(s->dquant && cbp){ 00491 put_mb_modes(s, 6, 0x01, 0, 0); 00492 put_qscale(s); 00493 }else{ 00494 put_mb_modes(s, 5, 0x03, 0, 0); 00495 s->qscale -= s->dquant; 00496 } 00497 s->misc_bits+= get_bits_diff(s); 00498 s->i_count++; 00499 memset(s->last_mv, 0, sizeof(s->last_mv)); 00500 } else if (s->pict_type == FF_P_TYPE) { 00501 if(s->mv_type == MV_TYPE_16X16){ 00502 if (cbp != 0) { 00503 if ((motion_x|motion_y) == 0) { 00504 if(s->dquant){ 00505 put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */ 00506 put_qscale(s); 00507 }else{ 00508 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */ 00509 } 00510 s->misc_bits+= get_bits_diff(s); 00511 } else { 00512 if(s->dquant){ 00513 put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */ 00514 put_qscale(s); 00515 }else{ 00516 put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */ 00517 } 00518 s->misc_bits+= get_bits_diff(s); 00519 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added 00520 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added 00521 s->mv_bits+= get_bits_diff(s); 00522 } 00523 } else { 00524 put_bits(&s->pb, 3, 1); /* motion only */ 00525 if (!s->frame_pred_frame_dct) 00526 put_bits(&s->pb, 2, 2); /* motion_type: frame */ 00527 s->misc_bits+= get_bits_diff(s); 00528 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added 00529 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added 00530 s->qscale -= s->dquant; 00531 s->mv_bits+= get_bits_diff(s); 00532 } 00533 s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x; 00534 s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y; 00535 }else{ 00536 assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD); 00537 00538 if (cbp) { 00539 if(s->dquant){ 00540 put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */ 00541 put_qscale(s); 00542 }else{ 00543 put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */ 00544 } 00545 } else { 00546 put_bits(&s->pb, 3, 1); /* motion only */ 00547 put_bits(&s->pb, 2, 1); /* motion_type: field */ 00548 s->qscale -= s->dquant; 00549 } 00550 s->misc_bits+= get_bits_diff(s); 00551 for(i=0; i<2; i++){ 00552 put_bits(&s->pb, 1, s->field_select[0][i]); 00553 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code); 00554 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code); 00555 s->last_mv[0][i][0]= s->mv[0][i][0]; 00556 s->last_mv[0][i][1]= 2*s->mv[0][i][1]; 00557 } 00558 s->mv_bits+= get_bits_diff(s); 00559 } 00560 if(cbp) { 00561 if (s->chroma_y_shift) { 00562 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]); 00563 } else { 00564 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]); 00565 put_sbits(&s->pb, 2, cbp); 00566 } 00567 } 00568 s->f_count++; 00569 } else{ 00570 if(s->mv_type == MV_TYPE_16X16){ 00571 if (cbp){ // With coded bloc pattern 00572 if (s->dquant) { 00573 if(s->mv_dir == MV_DIR_FORWARD) 00574 put_mb_modes(s, 6, 3, 1, 0); 00575 else 00576 put_mb_modes(s, 8-s->mv_dir, 2, 1, 0); 00577 put_qscale(s); 00578 } else { 00579 put_mb_modes(s, 5-s->mv_dir, 3, 1, 0); 00580 } 00581 }else{ // No coded bloc pattern 00582 put_bits(&s->pb, 5-s->mv_dir, 2); 00583 if (!s->frame_pred_frame_dct) 00584 put_bits(&s->pb, 2, 2); /* motion_type: frame */ 00585 s->qscale -= s->dquant; 00586 } 00587 s->misc_bits += get_bits_diff(s); 00588 if (s->mv_dir&MV_DIR_FORWARD){ 00589 mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code); 00590 mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code); 00591 s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0]; 00592 s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1]; 00593 s->f_count++; 00594 } 00595 if (s->mv_dir&MV_DIR_BACKWARD){ 00596 mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code); 00597 mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code); 00598 s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0]; 00599 s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1]; 00600 s->b_count++; 00601 } 00602 }else{ 00603 assert(s->mv_type == MV_TYPE_FIELD); 00604 assert(!s->frame_pred_frame_dct); 00605 if (cbp){ // With coded bloc pattern 00606 if (s->dquant) { 00607 if(s->mv_dir == MV_DIR_FORWARD) 00608 put_mb_modes(s, 6, 3, 1, 1); 00609 else 00610 put_mb_modes(s, 8-s->mv_dir, 2, 1, 1); 00611 put_qscale(s); 00612 } else { 00613 put_mb_modes(s, 5-s->mv_dir, 3, 1, 1); 00614 } 00615 }else{ // No coded bloc pattern 00616 put_bits(&s->pb, 5-s->mv_dir, 2); 00617 put_bits(&s->pb, 2, 1); /* motion_type: field */ 00618 s->qscale -= s->dquant; 00619 } 00620 s->misc_bits += get_bits_diff(s); 00621 if (s->mv_dir&MV_DIR_FORWARD){ 00622 for(i=0; i<2; i++){ 00623 put_bits(&s->pb, 1, s->field_select[0][i]); 00624 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code); 00625 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code); 00626 s->last_mv[0][i][0]= s->mv[0][i][0]; 00627 s->last_mv[0][i][1]= 2*s->mv[0][i][1]; 00628 } 00629 s->f_count++; 00630 } 00631 if (s->mv_dir&MV_DIR_BACKWARD){ 00632 for(i=0; i<2; i++){ 00633 put_bits(&s->pb, 1, s->field_select[1][i]); 00634 mpeg1_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code); 00635 mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code); 00636 s->last_mv[1][i][0]= s->mv[1][i][0]; 00637 s->last_mv[1][i][1]= 2*s->mv[1][i][1]; 00638 } 00639 s->b_count++; 00640 } 00641 } 00642 s->mv_bits += get_bits_diff(s); 00643 if(cbp) { 00644 if (s->chroma_y_shift) { 00645 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]); 00646 } else { 00647 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]); 00648 put_sbits(&s->pb, 2, cbp); 00649 } 00650 } 00651 } 00652 for(i=0;i<mb_block_count;i++) { 00653 if (cbp & (1 << (mb_block_count - 1 - i))) { 00654 mpeg1_encode_block(s, block[i], i); 00655 } 00656 } 00657 s->mb_skip_run = 0; 00658 if(s->mb_intra) 00659 s->i_tex_bits+= get_bits_diff(s); 00660 else 00661 s->p_tex_bits+= get_bits_diff(s); 00662 } 00663 } 00664 00665 void mpeg1_encode_mb(MpegEncContext *s, DCTELEM block[6][64], int motion_x, int motion_y) 00666 { 00667 if (s->chroma_format == CHROMA_420) mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6); 00668 else mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8); 00669 } 00670 00671 // RAL: Parameter added: f_or_b_code 00672 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code) 00673 { 00674 if (val == 0) { 00675 /* zero vector */ 00676 put_bits(&s->pb, 00677 ff_mpeg12_mbMotionVectorTable[0][1], 00678 ff_mpeg12_mbMotionVectorTable[0][0]); 00679 } else { 00680 int code, sign, bits; 00681 int bit_size = f_or_b_code - 1; 00682 int range = 1 << bit_size; 00683 /* modulo encoding */ 00684 int l= INT_BIT - 5 - bit_size; 00685 val= (val<<l)>>l; 00686 00687 if (val >= 0) { 00688 val--; 00689 code = (val >> bit_size) + 1; 00690 bits = val & (range - 1); 00691 sign = 0; 00692 } else { 00693 val = -val; 00694 val--; 00695 code = (val >> bit_size) + 1; 00696 bits = val & (range - 1); 00697 sign = 1; 00698 } 00699 00700 assert(code > 0 && code <= 16); 00701 00702 put_bits(&s->pb, 00703 ff_mpeg12_mbMotionVectorTable[code][1], 00704 ff_mpeg12_mbMotionVectorTable[code][0]); 00705 00706 put_bits(&s->pb, 1, sign); 00707 if (bit_size > 0) { 00708 put_bits(&s->pb, bit_size, bits); 00709 } 00710 } 00711 } 00712 00713 void ff_mpeg1_encode_init(MpegEncContext *s) 00714 { 00715 static int done=0; 00716 00717 ff_mpeg12_common_init(s); 00718 00719 if(!done){ 00720 int f_code; 00721 int mv; 00722 int i; 00723 00724 done=1; 00725 init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]); 00726 init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]); 00727 00728 for(i=0; i<64; i++) 00729 { 00730 mpeg1_max_level[0][i]= ff_rl_mpeg1.max_level[0][i]; 00731 mpeg1_index_run[0][i]= ff_rl_mpeg1.index_run[0][i]; 00732 } 00733 00734 init_uni_ac_vlc(&ff_rl_mpeg1, uni_mpeg1_ac_vlc_len); 00735 if(s->intra_vlc_format) 00736 init_uni_ac_vlc(&ff_rl_mpeg2, uni_mpeg2_ac_vlc_len); 00737 00738 /* build unified dc encoding tables */ 00739 for(i=-255; i<256; i++) 00740 { 00741 int adiff, index; 00742 int bits, code; 00743 int diff=i; 00744 00745 adiff = FFABS(diff); 00746 if(diff<0) diff--; 00747 index = av_log2(2*adiff); 00748 00749 bits= ff_mpeg12_vlc_dc_lum_bits[index] + index; 00750 code= (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)); 00751 mpeg1_lum_dc_uni[i+255]= bits + (code<<8); 00752 00753 bits= ff_mpeg12_vlc_dc_chroma_bits[index] + index; 00754 code= (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)); 00755 mpeg1_chr_dc_uni[i+255]= bits + (code<<8); 00756 } 00757 00758 for(f_code=1; f_code<=MAX_FCODE; f_code++){ 00759 for(mv=-MAX_MV; mv<=MAX_MV; mv++){ 00760 int len; 00761 00762 if(mv==0) len= ff_mpeg12_mbMotionVectorTable[0][1]; 00763 else{ 00764 int val, bit_size, range, code; 00765 00766 bit_size = f_code - 1; 00767 range = 1 << bit_size; 00768 00769 val=mv; 00770 if (val < 0) 00771 val = -val; 00772 val--; 00773 code = (val >> bit_size) + 1; 00774 if(code<17){ 00775 len= ff_mpeg12_mbMotionVectorTable[code][1] + 1 + bit_size; 00776 }else{ 00777 len= ff_mpeg12_mbMotionVectorTable[16][1] + 2 + bit_size; 00778 } 00779 } 00780 00781 mv_penalty[f_code][mv+MAX_MV]= len; 00782 } 00783 } 00784 00785 00786 for(f_code=MAX_FCODE; f_code>0; f_code--){ 00787 for(mv=-(8<<f_code); mv<(8<<f_code); mv++){ 00788 fcode_tab[mv+MAX_MV]= f_code; 00789 } 00790 } 00791 } 00792 s->me.mv_penalty= mv_penalty; 00793 s->fcode_tab= fcode_tab; 00794 if(s->codec_id == CODEC_ID_MPEG1VIDEO){ 00795 s->min_qcoeff=-255; 00796 s->max_qcoeff= 255; 00797 }else{ 00798 s->min_qcoeff=-2047; 00799 s->max_qcoeff= 2047; 00800 } 00801 if (s->intra_vlc_format) { 00802 s->intra_ac_vlc_length= 00803 s->intra_ac_vlc_last_length= uni_mpeg2_ac_vlc_len; 00804 } else { 00805 s->intra_ac_vlc_length= 00806 s->intra_ac_vlc_last_length= uni_mpeg1_ac_vlc_len; 00807 } 00808 s->inter_ac_vlc_length= 00809 s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len; 00810 } 00811 00812 static inline void encode_dc(MpegEncContext *s, int diff, int component) 00813 { 00814 if(((unsigned) (diff+255)) >= 511){ 00815 int index; 00816 00817 if(diff<0){ 00818 index= av_log2_16bit(-2*diff); 00819 diff--; 00820 }else{ 00821 index= av_log2_16bit(2*diff); 00822 } 00823 if (component == 0) { 00824 put_bits( 00825 &s->pb, 00826 ff_mpeg12_vlc_dc_lum_bits[index] + index, 00827 (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1))); 00828 }else{ 00829 put_bits( 00830 &s->pb, 00831 ff_mpeg12_vlc_dc_chroma_bits[index] + index, 00832 (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1))); 00833 } 00834 }else{ 00835 if (component == 0) { 00836 put_bits( 00837 &s->pb, 00838 mpeg1_lum_dc_uni[diff+255]&0xFF, 00839 mpeg1_lum_dc_uni[diff+255]>>8); 00840 } else { 00841 put_bits( 00842 &s->pb, 00843 mpeg1_chr_dc_uni[diff+255]&0xFF, 00844 mpeg1_chr_dc_uni[diff+255]>>8); 00845 } 00846 } 00847 } 00848 00849 static void mpeg1_encode_block(MpegEncContext *s, 00850 DCTELEM *block, 00851 int n) 00852 { 00853 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign; 00854 int code, component; 00855 const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc; 00856 00857 last_index = s->block_last_index[n]; 00858 00859 /* DC coef */ 00860 if (s->mb_intra) { 00861 component = (n <= 3 ? 0 : (n&1) + 1); 00862 dc = block[0]; /* overflow is impossible */ 00863 diff = dc - s->last_dc[component]; 00864 encode_dc(s, diff, component); 00865 s->last_dc[component] = dc; 00866 i = 1; 00867 if (s->intra_vlc_format) 00868 table_vlc = ff_rl_mpeg2.table_vlc; 00869 } else { 00870 /* encode the first coefficient : needs to be done here because 00871 it is handled slightly differently */ 00872 level = block[0]; 00873 if (abs(level) == 1) { 00874 code = ((uint32_t)level >> 31); /* the sign bit */ 00875 put_bits(&s->pb, 2, code | 0x02); 00876 i = 1; 00877 } else { 00878 i = 0; 00879 last_non_zero = -1; 00880 goto next_coef; 00881 } 00882 } 00883 00884 /* now quantify & encode AC coefs */ 00885 last_non_zero = i - 1; 00886 00887 for(;i<=last_index;i++) { 00888 j = s->intra_scantable.permutated[i]; 00889 level = block[j]; 00890 next_coef: 00891 #if 0 00892 if (level != 0) 00893 dprintf(s->avctx, "level[%d]=%d\n", i, level); 00894 #endif 00895 /* encode using VLC */ 00896 if (level != 0) { 00897 run = i - last_non_zero - 1; 00898 00899 alevel= level; 00900 MASK_ABS(sign, alevel) 00901 sign&=1; 00902 00903 if (alevel <= mpeg1_max_level[0][run]){ 00904 code= mpeg1_index_run[0][run] + alevel - 1; 00905 /* store the vlc & sign at once */ 00906 put_bits(&s->pb, table_vlc[code][1]+1, (table_vlc[code][0]<<1) + sign); 00907 } else { 00908 /* escape seems to be pretty rare <5% so I do not optimize it */ 00909 put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]); 00910 /* escape: only clip in this case */ 00911 put_bits(&s->pb, 6, run); 00912 if(s->codec_id == CODEC_ID_MPEG1VIDEO){ 00913 if (alevel < 128) { 00914 put_sbits(&s->pb, 8, level); 00915 } else { 00916 if (level < 0) { 00917 put_bits(&s->pb, 16, 0x8001 + level + 255); 00918 } else { 00919 put_sbits(&s->pb, 16, level); 00920 } 00921 } 00922 }else{ 00923 put_sbits(&s->pb, 12, level); 00924 } 00925 } 00926 last_non_zero = i; 00927 } 00928 } 00929 /* end of block */ 00930 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]); 00931 } 00932 00933 AVCodec mpeg1video_encoder = { 00934 "mpeg1video", 00935 AVMEDIA_TYPE_VIDEO, 00936 CODEC_ID_MPEG1VIDEO, 00937 sizeof(MpegEncContext), 00938 encode_init, 00939 MPV_encode_picture, 00940 MPV_encode_end, 00941 .supported_framerates= ff_frame_rate_tab+1, 00942 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE}, 00943 .capabilities= CODEC_CAP_DELAY, 00944 .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"), 00945 }; 00946 00947 AVCodec mpeg2video_encoder = { 00948 "mpeg2video", 00949 AVMEDIA_TYPE_VIDEO, 00950 CODEC_ID_MPEG2VIDEO, 00951 sizeof(MpegEncContext), 00952 encode_init, 00953 MPV_encode_picture, 00954 MPV_encode_end, 00955 .supported_framerates= ff_frame_rate_tab+1, 00956 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE}, 00957 .capabilities= CODEC_CAP_DELAY, 00958 .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"), 00959 };