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