00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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 #include "timecode.h"
00037 #include "libavutil/log.h"
00038 #include "libavutil/opt.h"
00039 #include "libavutil/avassert.h"
00040
00041 static const uint8_t inv_non_linear_qscale[13] = {
00042 0, 2, 4, 6, 8,
00043 9,10,11,12,13,14,15,16,
00044 };
00045
00046 static const uint8_t svcd_scan_offset_placeholder[14] = {
00047 0x10, 0x0E,
00048 0x00, 0x80, 0x81,
00049 0x00, 0x80, 0x81,
00050 0xff, 0xff, 0xff,
00051 0xff, 0xff, 0xff,
00052 };
00053
00054 static void mpeg1_encode_block(MpegEncContext *s,
00055 DCTELEM *block,
00056 int component);
00057 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code);
00058
00059 static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
00060 static uint8_t fcode_tab[MAX_MV*2+1];
00061
00062 static uint8_t uni_mpeg1_ac_vlc_len [64*64*2];
00063 static uint8_t uni_mpeg2_ac_vlc_len [64*64*2];
00064
00065
00066 static uint32_t mpeg1_lum_dc_uni[512];
00067 static uint32_t mpeg1_chr_dc_uni[512];
00068
00069 static uint8_t mpeg1_index_run[2][64];
00070 static int8_t mpeg1_max_level[2][64];
00071
00072 static void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){
00073 int i;
00074
00075 for(i=0; i<128; i++){
00076 int level= i-64;
00077 int run;
00078 if (!level)
00079 continue;
00080 for(run=0; run<64; run++){
00081 int len, code;
00082
00083 int alevel= FFABS(level);
00084
00085 if (alevel > rl->max_level[0][run])
00086 code= 111;
00087 else
00088 code= rl->index_run[0][run] + alevel - 1;
00089
00090 if (code < 111 /* rl->n */) {
00091
00092 len= rl->table_vlc[code][1]+1;
00093 } else {
00094 len= rl->table_vlc[111][1]+6;
00095
00096 if (alevel < 128) {
00097 len += 8;
00098 } else {
00099 len += 16;
00100 }
00101 }
00102
00103 uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
00104 }
00105 }
00106 }
00107
00108
00109 static int find_frame_rate_index(MpegEncContext *s){
00110 int i;
00111 int64_t dmin= INT64_MAX;
00112 int64_t d;
00113
00114 for(i=1;i<14;i++) {
00115 int64_t n0= 1001LL/avpriv_frame_rate_tab[i].den*avpriv_frame_rate_tab[i].num*s->avctx->time_base.num;
00116 int64_t n1= 1001LL*s->avctx->time_base.den;
00117 if(s->avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL && i>=9) break;
00118
00119 d = FFABS(n0 - n1);
00120 if(d < dmin){
00121 dmin=d;
00122 s->frame_rate_index= i;
00123 }
00124 }
00125 if(dmin)
00126 return -1;
00127 else
00128 return 0;
00129 }
00130
00131 static av_cold int encode_init(AVCodecContext *avctx)
00132 {
00133 MpegEncContext *s = avctx->priv_data;
00134
00135 if(MPV_encode_init(avctx) < 0)
00136 return -1;
00137
00138 #if FF_API_MPEGVIDEO_GLOBAL_OPTS
00139 if (avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE)
00140 s->drop_frame_timecode = 1;
00141 if (avctx->flags & CODEC_FLAG_SVCD_SCAN_OFFSET)
00142 s->scan_offset = 1;
00143 #endif
00144
00145 if(find_frame_rate_index(s) < 0){
00146 if(s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
00147 av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num);
00148 return -1;
00149 }else{
00150 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);
00151 }
00152 }
00153
00154 if(avctx->profile == FF_PROFILE_UNKNOWN){
00155 if(avctx->level != FF_LEVEL_UNKNOWN){
00156 av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
00157 return -1;
00158 }
00159 avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0;
00160 }
00161
00162 if(avctx->level == FF_LEVEL_UNKNOWN){
00163 if(avctx->profile == 0){
00164 if(avctx->width <= 720 && avctx->height <= 608) avctx->level = 5;
00165 else avctx->level = 2;
00166 }else{
00167 if(avctx->profile != 1 && s->chroma_format != CHROMA_420){
00168 av_log(avctx, AV_LOG_ERROR, "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
00169 return -1;
00170 }
00171 if(avctx->width <= 720 && avctx->height <= 576) avctx->level = 8;
00172 else if(avctx->width <= 1440) avctx->level = 6;
00173 else avctx->level = 4;
00174 }
00175 }
00176
00177 s->drop_frame_timecode = s->tc.drop = s->drop_frame_timecode || !!(avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE);
00178 if (s->drop_frame_timecode && s->frame_rate_index != 4) {
00179 av_log(avctx, AV_LOG_ERROR, "Drop frame time code only allowed with 1001/30000 fps\n");
00180 return -1;
00181 }
00182
00183 if (s->tc.str) {
00184 s->tc.rate = avpriv_frame_rate_tab[s->frame_rate_index];
00185 if (avpriv_init_smpte_timecode(s, &s->tc) < 0)
00186 return -1;
00187 s->drop_frame_timecode = s->tc.drop;
00188 s->avctx->timecode_frame_start = s->tc.start;
00189 } else {
00190 s->avctx->timecode_frame_start = 0;
00191 }
00192 return 0;
00193 }
00194
00195 static void put_header(MpegEncContext *s, int header)
00196 {
00197 avpriv_align_put_bits(&s->pb);
00198 put_bits(&s->pb, 16, header>>16);
00199 put_sbits(&s->pb, 16, header);
00200 }
00201
00202
00203 static void mpeg1_encode_sequence_header(MpegEncContext *s)
00204 {
00205 unsigned int vbv_buffer_size;
00206 unsigned int fps, v;
00207 int i;
00208 uint64_t time_code;
00209 float best_aspect_error= 1E10;
00210 float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
00211 int constraint_parameter_flag;
00212
00213 if(aspect_ratio==0.0) aspect_ratio= 1.0;
00214
00215 if (s->current_picture.f.key_frame) {
00216 AVRational framerate= avpriv_frame_rate_tab[s->frame_rate_index];
00217
00218
00219 put_header(s, SEQ_START_CODE);
00220
00221 put_sbits(&s->pb, 12, s->width );
00222 put_sbits(&s->pb, 12, s->height);
00223
00224 for(i=1; i<15; i++){
00225 float error= aspect_ratio;
00226 if(s->codec_id == CODEC_ID_MPEG1VIDEO || i <=1)
00227 error-= 1.0/ff_mpeg1_aspect[i];
00228 else
00229 error-= av_q2d(ff_mpeg2_aspect[i])*s->height/s->width;
00230
00231 error= FFABS(error);
00232
00233 if(error < best_aspect_error){
00234 best_aspect_error= error;
00235 s->aspect_ratio_info= i;
00236 }
00237 }
00238
00239 put_bits(&s->pb, 4, s->aspect_ratio_info);
00240 put_bits(&s->pb, 4, s->frame_rate_index);
00241
00242 if(s->avctx->rc_max_rate){
00243 v = (s->avctx->rc_max_rate + 399) / 400;
00244 if (v > 0x3ffff && s->codec_id == CODEC_ID_MPEG1VIDEO)
00245 v = 0x3ffff;
00246 }else{
00247 v= 0x3FFFF;
00248 }
00249
00250 if(s->avctx->rc_buffer_size)
00251 vbv_buffer_size = s->avctx->rc_buffer_size;
00252 else
00253
00254 vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
00255 vbv_buffer_size= (vbv_buffer_size + 16383) / 16384;
00256
00257 put_sbits(&s->pb, 18, v);
00258 put_bits(&s->pb, 1, 1);
00259 put_sbits(&s->pb, 10, vbv_buffer_size);
00260
00261 constraint_parameter_flag=
00262 s->width <= 768 && s->height <= 576 &&
00263 s->mb_width * s->mb_height <= 396 &&
00264 s->mb_width * s->mb_height * framerate.num <= framerate.den*396*25 &&
00265 framerate.num <= framerate.den*30 &&
00266 s->avctx->me_range && s->avctx->me_range < 128 &&
00267 vbv_buffer_size <= 20 &&
00268 v <= 1856000/400 &&
00269 s->codec_id == CODEC_ID_MPEG1VIDEO;
00270
00271 put_bits(&s->pb, 1, constraint_parameter_flag);
00272
00273 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
00274 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
00275
00276 if(s->codec_id == CODEC_ID_MPEG2VIDEO){
00277 put_header(s, EXT_START_CODE);
00278 put_bits(&s->pb, 4, 1);
00279
00280 put_bits(&s->pb, 1, s->avctx->profile == 0);
00281
00282 put_bits(&s->pb, 3, s->avctx->profile);
00283 put_bits(&s->pb, 4, s->avctx->level);
00284
00285 put_bits(&s->pb, 1, s->progressive_sequence);
00286 put_bits(&s->pb, 2, s->chroma_format);
00287 put_bits(&s->pb, 2, s->width >>12);
00288 put_bits(&s->pb, 2, s->height>>12);
00289 put_bits(&s->pb, 12, v>>18);
00290 put_bits(&s->pb, 1, 1);
00291 put_bits(&s->pb, 8, vbv_buffer_size >>10);
00292 put_bits(&s->pb, 1, s->low_delay);
00293 put_bits(&s->pb, 2, 0);
00294 put_bits(&s->pb, 5, 0);
00295 }
00296
00297 put_header(s, GOP_START_CODE);
00298 put_bits(&s->pb, 1, s->drop_frame_timecode);
00299
00300
00301 fps = (framerate.num + framerate.den/2)/ framerate.den;
00302 time_code = s->current_picture_ptr->f.coded_picture_number + s->avctx->timecode_frame_start;
00303
00304 s->gop_picture_number = s->current_picture_ptr->f.coded_picture_number;
00305 av_assert0(s->drop_frame_timecode == s->tc.drop);
00306 if (s->tc.drop)
00307 time_code = avpriv_framenum_to_drop_timecode(time_code);
00308 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
00309 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
00310 put_bits(&s->pb, 1, 1);
00311 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
00312 put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
00313 put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
00314 put_bits(&s->pb, 1, 0);
00315 }
00316 }
00317
00318 static inline void encode_mb_skip_run(MpegEncContext *s, int run){
00319 while (run >= 33) {
00320 put_bits(&s->pb, 11, 0x008);
00321 run -= 33;
00322 }
00323 put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1],
00324 ff_mpeg12_mbAddrIncrTable[run][0]);
00325 }
00326
00327 static av_always_inline void put_qscale(MpegEncContext *s)
00328 {
00329 if(s->q_scale_type){
00330 assert(s->qscale>=1 && s->qscale <=12);
00331 put_bits(&s->pb, 5, inv_non_linear_qscale[s->qscale]);
00332 }else{
00333 put_bits(&s->pb, 5, s->qscale);
00334 }
00335 }
00336
00337 void ff_mpeg1_encode_slice_header(MpegEncContext *s){
00338 if (s->height > 2800) {
00339 put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127));
00340 put_bits(&s->pb, 3, s->mb_y >> 7);
00341 } else {
00342 put_header(s, SLICE_MIN_START_CODE + s->mb_y);
00343 }
00344 put_qscale(s);
00345 put_bits(&s->pb, 1, 0);
00346 }
00347
00348 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
00349 {
00350 mpeg1_encode_sequence_header(s);
00351
00352
00353 put_header(s, PICTURE_START_CODE);
00354
00355
00356
00357 put_bits(&s->pb, 10, (s->picture_number -
00358 s->gop_picture_number) & 0x3ff);
00359 put_bits(&s->pb, 3, s->pict_type);
00360
00361 s->vbv_delay_ptr= s->pb.buf + put_bits_count(&s->pb)/8;
00362 put_bits(&s->pb, 16, 0xFFFF);
00363
00364
00365 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
00366 put_bits(&s->pb, 1, 0);
00367 if(s->codec_id == CODEC_ID_MPEG1VIDEO)
00368 put_bits(&s->pb, 3, s->f_code);
00369 else
00370 put_bits(&s->pb, 3, 7);
00371 }
00372
00373
00374 if (s->pict_type == AV_PICTURE_TYPE_B) {
00375 put_bits(&s->pb, 1, 0);
00376 if(s->codec_id == CODEC_ID_MPEG1VIDEO)
00377 put_bits(&s->pb, 3, s->b_code);
00378 else
00379 put_bits(&s->pb, 3, 7);
00380 }
00381
00382 put_bits(&s->pb, 1, 0);
00383
00384 s->frame_pred_frame_dct = 1;
00385 if(s->codec_id == CODEC_ID_MPEG2VIDEO){
00386 put_header(s, EXT_START_CODE);
00387 put_bits(&s->pb, 4, 8);
00388 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
00389 put_bits(&s->pb, 4, s->f_code);
00390 put_bits(&s->pb, 4, s->f_code);
00391 }else{
00392 put_bits(&s->pb, 8, 255);
00393 }
00394 if (s->pict_type == AV_PICTURE_TYPE_B) {
00395 put_bits(&s->pb, 4, s->b_code);
00396 put_bits(&s->pb, 4, s->b_code);
00397 }else{
00398 put_bits(&s->pb, 8, 255);
00399 }
00400 put_bits(&s->pb, 2, s->intra_dc_precision);
00401
00402 assert(s->picture_structure == PICT_FRAME);
00403 put_bits(&s->pb, 2, s->picture_structure);
00404 if (s->progressive_sequence) {
00405 put_bits(&s->pb, 1, 0);
00406 } else {
00407 put_bits(&s->pb, 1, s->current_picture_ptr->f.top_field_first);
00408 }
00409
00410
00411 s->frame_pred_frame_dct = s->progressive_sequence;
00412
00413 put_bits(&s->pb, 1, s->frame_pred_frame_dct);
00414 put_bits(&s->pb, 1, s->concealment_motion_vectors);
00415 put_bits(&s->pb, 1, s->q_scale_type);
00416 put_bits(&s->pb, 1, s->intra_vlc_format);
00417 put_bits(&s->pb, 1, s->alternate_scan);
00418 put_bits(&s->pb, 1, s->repeat_first_field);
00419 s->progressive_frame = s->progressive_sequence;
00420 put_bits(&s->pb, 1, s->chroma_format == CHROMA_420 ? s->progressive_frame : 0);
00421 put_bits(&s->pb, 1, s->progressive_frame);
00422 put_bits(&s->pb, 1, 0);
00423 }
00424 if (s->scan_offset) {
00425 int i;
00426
00427 put_header(s, USER_START_CODE);
00428 for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){
00429 put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
00430 }
00431 }
00432
00433 s->mb_y=0;
00434 ff_mpeg1_encode_slice_header(s);
00435 }
00436
00437 static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
00438 int has_mv, int field_motion)
00439 {
00440 put_bits(&s->pb, n, bits);
00441 if (!s->frame_pred_frame_dct) {
00442 if (has_mv)
00443 put_bits(&s->pb, 2, 2 - field_motion);
00444 put_bits(&s->pb, 1, s->interlaced_dct);
00445 }
00446 }
00447
00448 static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
00449 DCTELEM block[6][64],
00450 int motion_x, int motion_y,
00451 int mb_block_count)
00452 {
00453 int i, cbp;
00454 const int mb_x = s->mb_x;
00455 const int mb_y = s->mb_y;
00456 const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
00457
00458
00459 cbp = 0;
00460 for(i=0;i<mb_block_count;i++) {
00461 if (s->block_last_index[i] >= 0)
00462 cbp |= 1 << (mb_block_count - 1 - i);
00463 }
00464
00465 if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
00466 (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) &&
00467 ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) ||
00468 (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) |
00469 ((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))) {
00470 s->mb_skip_run++;
00471 s->qscale -= s->dquant;
00472 s->skip_count++;
00473 s->misc_bits++;
00474 s->last_bits++;
00475 if(s->pict_type == AV_PICTURE_TYPE_P){
00476 s->last_mv[0][1][0]= s->last_mv[0][0][0]=
00477 s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0;
00478 }
00479 } else {
00480 if(first_mb){
00481 assert(s->mb_skip_run == 0);
00482 encode_mb_skip_run(s, s->mb_x);
00483 }else{
00484 encode_mb_skip_run(s, s->mb_skip_run);
00485 }
00486
00487 if (s->pict_type == AV_PICTURE_TYPE_I) {
00488 if(s->dquant && cbp){
00489 put_mb_modes(s, 2, 1, 0, 0);
00490 put_qscale(s);
00491 }else{
00492 put_mb_modes(s, 1, 1, 0, 0);
00493 s->qscale -= s->dquant;
00494 }
00495 s->misc_bits+= get_bits_diff(s);
00496 s->i_count++;
00497 } else if (s->mb_intra) {
00498 if(s->dquant && cbp){
00499 put_mb_modes(s, 6, 0x01, 0, 0);
00500 put_qscale(s);
00501 }else{
00502 put_mb_modes(s, 5, 0x03, 0, 0);
00503 s->qscale -= s->dquant;
00504 }
00505 s->misc_bits+= get_bits_diff(s);
00506 s->i_count++;
00507 memset(s->last_mv, 0, sizeof(s->last_mv));
00508 } else if (s->pict_type == AV_PICTURE_TYPE_P) {
00509 if(s->mv_type == MV_TYPE_16X16){
00510 if (cbp != 0) {
00511 if ((motion_x|motion_y) == 0) {
00512 if(s->dquant){
00513 put_mb_modes(s, 5, 1, 0, 0);
00514 put_qscale(s);
00515 }else{
00516 put_mb_modes(s, 2, 1, 0, 0);
00517 }
00518 s->misc_bits+= get_bits_diff(s);
00519 } else {
00520 if(s->dquant){
00521 put_mb_modes(s, 5, 2, 1, 0);
00522 put_qscale(s);
00523 }else{
00524 put_mb_modes(s, 1, 1, 1, 0);
00525 }
00526 s->misc_bits+= get_bits_diff(s);
00527 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);
00528 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);
00529 s->mv_bits+= get_bits_diff(s);
00530 }
00531 } else {
00532 put_bits(&s->pb, 3, 1);
00533 if (!s->frame_pred_frame_dct)
00534 put_bits(&s->pb, 2, 2);
00535 s->misc_bits+= get_bits_diff(s);
00536 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);
00537 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);
00538 s->qscale -= s->dquant;
00539 s->mv_bits+= get_bits_diff(s);
00540 }
00541 s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x;
00542 s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y;
00543 }else{
00544 assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
00545
00546 if (cbp) {
00547 if(s->dquant){
00548 put_mb_modes(s, 5, 2, 1, 1);
00549 put_qscale(s);
00550 }else{
00551 put_mb_modes(s, 1, 1, 1, 1);
00552 }
00553 } else {
00554 put_bits(&s->pb, 3, 1);
00555 put_bits(&s->pb, 2, 1);
00556 s->qscale -= s->dquant;
00557 }
00558 s->misc_bits+= get_bits_diff(s);
00559 for(i=0; i<2; i++){
00560 put_bits(&s->pb, 1, s->field_select[0][i]);
00561 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
00562 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
00563 s->last_mv[0][i][0]= s->mv[0][i][0];
00564 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
00565 }
00566 s->mv_bits+= get_bits_diff(s);
00567 }
00568 if(cbp) {
00569 if (s->chroma_y_shift) {
00570 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
00571 } else {
00572 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
00573 put_sbits(&s->pb, 2, cbp);
00574 }
00575 }
00576 s->f_count++;
00577 } else{
00578 if(s->mv_type == MV_TYPE_16X16){
00579 if (cbp){
00580 if (s->dquant) {
00581 if(s->mv_dir == MV_DIR_FORWARD)
00582 put_mb_modes(s, 6, 3, 1, 0);
00583 else
00584 put_mb_modes(s, 8-s->mv_dir, 2, 1, 0);
00585 put_qscale(s);
00586 } else {
00587 put_mb_modes(s, 5-s->mv_dir, 3, 1, 0);
00588 }
00589 }else{
00590 put_bits(&s->pb, 5-s->mv_dir, 2);
00591 if (!s->frame_pred_frame_dct)
00592 put_bits(&s->pb, 2, 2);
00593 s->qscale -= s->dquant;
00594 }
00595 s->misc_bits += get_bits_diff(s);
00596 if (s->mv_dir&MV_DIR_FORWARD){
00597 mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
00598 mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
00599 s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0];
00600 s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1];
00601 s->f_count++;
00602 }
00603 if (s->mv_dir&MV_DIR_BACKWARD){
00604 mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
00605 mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
00606 s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0];
00607 s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1];
00608 s->b_count++;
00609 }
00610 }else{
00611 assert(s->mv_type == MV_TYPE_FIELD);
00612 assert(!s->frame_pred_frame_dct);
00613 if (cbp){
00614 if (s->dquant) {
00615 if(s->mv_dir == MV_DIR_FORWARD)
00616 put_mb_modes(s, 6, 3, 1, 1);
00617 else
00618 put_mb_modes(s, 8-s->mv_dir, 2, 1, 1);
00619 put_qscale(s);
00620 } else {
00621 put_mb_modes(s, 5-s->mv_dir, 3, 1, 1);
00622 }
00623 }else{
00624 put_bits(&s->pb, 5-s->mv_dir, 2);
00625 put_bits(&s->pb, 2, 1);
00626 s->qscale -= s->dquant;
00627 }
00628 s->misc_bits += get_bits_diff(s);
00629 if (s->mv_dir&MV_DIR_FORWARD){
00630 for(i=0; i<2; i++){
00631 put_bits(&s->pb, 1, s->field_select[0][i]);
00632 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
00633 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
00634 s->last_mv[0][i][0]= s->mv[0][i][0];
00635 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
00636 }
00637 s->f_count++;
00638 }
00639 if (s->mv_dir&MV_DIR_BACKWARD){
00640 for(i=0; i<2; i++){
00641 put_bits(&s->pb, 1, s->field_select[1][i]);
00642 mpeg1_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code);
00643 mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code);
00644 s->last_mv[1][i][0]= s->mv[1][i][0];
00645 s->last_mv[1][i][1]= 2*s->mv[1][i][1];
00646 }
00647 s->b_count++;
00648 }
00649 }
00650 s->mv_bits += get_bits_diff(s);
00651 if(cbp) {
00652 if (s->chroma_y_shift) {
00653 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
00654 } else {
00655 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
00656 put_sbits(&s->pb, 2, cbp);
00657 }
00658 }
00659 }
00660 for(i=0;i<mb_block_count;i++) {
00661 if (cbp & (1 << (mb_block_count - 1 - i))) {
00662 mpeg1_encode_block(s, block[i], i);
00663 }
00664 }
00665 s->mb_skip_run = 0;
00666 if(s->mb_intra)
00667 s->i_tex_bits+= get_bits_diff(s);
00668 else
00669 s->p_tex_bits+= get_bits_diff(s);
00670 }
00671 }
00672
00673 void mpeg1_encode_mb(MpegEncContext *s, DCTELEM block[6][64], int motion_x, int motion_y)
00674 {
00675 if (s->chroma_format == CHROMA_420) mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
00676 else mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
00677 }
00678
00679
00680 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
00681 {
00682 if (val == 0) {
00683
00684 put_bits(&s->pb,
00685 ff_mpeg12_mbMotionVectorTable[0][1],
00686 ff_mpeg12_mbMotionVectorTable[0][0]);
00687 } else {
00688 int code, sign, bits;
00689 int bit_size = f_or_b_code - 1;
00690 int range = 1 << bit_size;
00691
00692 val = sign_extend(val, 5 + bit_size);
00693
00694 if (val >= 0) {
00695 val--;
00696 code = (val >> bit_size) + 1;
00697 bits = val & (range - 1);
00698 sign = 0;
00699 } else {
00700 val = -val;
00701 val--;
00702 code = (val >> bit_size) + 1;
00703 bits = val & (range - 1);
00704 sign = 1;
00705 }
00706
00707 assert(code > 0 && code <= 16);
00708
00709 put_bits(&s->pb,
00710 ff_mpeg12_mbMotionVectorTable[code][1],
00711 ff_mpeg12_mbMotionVectorTable[code][0]);
00712
00713 put_bits(&s->pb, 1, sign);
00714 if (bit_size > 0) {
00715 put_bits(&s->pb, bit_size, bits);
00716 }
00717 }
00718 }
00719
00720 void ff_mpeg1_encode_init(MpegEncContext *s)
00721 {
00722 static int done=0;
00723
00724 ff_mpeg12_common_init(s);
00725
00726 if(!done){
00727 int f_code;
00728 int mv;
00729 int i;
00730
00731 done=1;
00732 ff_init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
00733 ff_init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
00734
00735 for(i=0; i<64; i++)
00736 {
00737 mpeg1_max_level[0][i]= ff_rl_mpeg1.max_level[0][i];
00738 mpeg1_index_run[0][i]= ff_rl_mpeg1.index_run[0][i];
00739 }
00740
00741 init_uni_ac_vlc(&ff_rl_mpeg1, uni_mpeg1_ac_vlc_len);
00742 if(s->intra_vlc_format)
00743 init_uni_ac_vlc(&ff_rl_mpeg2, uni_mpeg2_ac_vlc_len);
00744
00745
00746 for(i=-255; i<256; i++)
00747 {
00748 int adiff, index;
00749 int bits, code;
00750 int diff=i;
00751
00752 adiff = FFABS(diff);
00753 if(diff<0) diff--;
00754 index = av_log2(2*adiff);
00755
00756 bits= ff_mpeg12_vlc_dc_lum_bits[index] + index;
00757 code= (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
00758 mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
00759
00760 bits= ff_mpeg12_vlc_dc_chroma_bits[index] + index;
00761 code= (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
00762 mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
00763 }
00764
00765 for(f_code=1; f_code<=MAX_FCODE; f_code++){
00766 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
00767 int len;
00768
00769 if(mv==0) len= ff_mpeg12_mbMotionVectorTable[0][1];
00770 else{
00771 int val, bit_size, code;
00772
00773 bit_size = f_code - 1;
00774
00775 val=mv;
00776 if (val < 0)
00777 val = -val;
00778 val--;
00779 code = (val >> bit_size) + 1;
00780 if(code<17){
00781 len= ff_mpeg12_mbMotionVectorTable[code][1] + 1 + bit_size;
00782 }else{
00783 len= ff_mpeg12_mbMotionVectorTable[16][1] + 2 + bit_size;
00784 }
00785 }
00786
00787 mv_penalty[f_code][mv+MAX_MV]= len;
00788 }
00789 }
00790
00791
00792 for(f_code=MAX_FCODE; f_code>0; f_code--){
00793 for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
00794 fcode_tab[mv+MAX_MV]= f_code;
00795 }
00796 }
00797 }
00798 s->me.mv_penalty= mv_penalty;
00799 s->fcode_tab= fcode_tab;
00800 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
00801 s->min_qcoeff=-255;
00802 s->max_qcoeff= 255;
00803 }else{
00804 s->min_qcoeff=-2047;
00805 s->max_qcoeff= 2047;
00806 }
00807 if (s->intra_vlc_format) {
00808 s->intra_ac_vlc_length=
00809 s->intra_ac_vlc_last_length= uni_mpeg2_ac_vlc_len;
00810 } else {
00811 s->intra_ac_vlc_length=
00812 s->intra_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
00813 }
00814 s->inter_ac_vlc_length=
00815 s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
00816 }
00817
00818 static inline void encode_dc(MpegEncContext *s, int diff, int component)
00819 {
00820 if(((unsigned) (diff+255)) >= 511){
00821 int index;
00822
00823 if(diff<0){
00824 index= av_log2_16bit(-2*diff);
00825 diff--;
00826 }else{
00827 index= av_log2_16bit(2*diff);
00828 }
00829 if (component == 0) {
00830 put_bits(
00831 &s->pb,
00832 ff_mpeg12_vlc_dc_lum_bits[index] + index,
00833 (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)));
00834 }else{
00835 put_bits(
00836 &s->pb,
00837 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
00838 (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)));
00839 }
00840 }else{
00841 if (component == 0) {
00842 put_bits(
00843 &s->pb,
00844 mpeg1_lum_dc_uni[diff+255]&0xFF,
00845 mpeg1_lum_dc_uni[diff+255]>>8);
00846 } else {
00847 put_bits(
00848 &s->pb,
00849 mpeg1_chr_dc_uni[diff+255]&0xFF,
00850 mpeg1_chr_dc_uni[diff+255]>>8);
00851 }
00852 }
00853 }
00854
00855 static void mpeg1_encode_block(MpegEncContext *s,
00856 DCTELEM *block,
00857 int n)
00858 {
00859 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
00860 int code, component;
00861 const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
00862
00863 last_index = s->block_last_index[n];
00864
00865
00866 if (s->mb_intra) {
00867 component = (n <= 3 ? 0 : (n&1) + 1);
00868 dc = block[0];
00869 diff = dc - s->last_dc[component];
00870 encode_dc(s, diff, component);
00871 s->last_dc[component] = dc;
00872 i = 1;
00873 if (s->intra_vlc_format)
00874 table_vlc = ff_rl_mpeg2.table_vlc;
00875 } else {
00876
00877
00878 level = block[0];
00879 if (abs(level) == 1) {
00880 code = ((uint32_t)level >> 31);
00881 put_bits(&s->pb, 2, code | 0x02);
00882 i = 1;
00883 } else {
00884 i = 0;
00885 last_non_zero = -1;
00886 goto next_coef;
00887 }
00888 }
00889
00890
00891 last_non_zero = i - 1;
00892
00893 for(;i<=last_index;i++) {
00894 j = s->intra_scantable.permutated[i];
00895 level = block[j];
00896 next_coef:
00897
00898 if (level != 0) {
00899 run = i - last_non_zero - 1;
00900
00901 alevel= level;
00902 MASK_ABS(sign, alevel)
00903 sign&=1;
00904
00905 if (alevel <= mpeg1_max_level[0][run]){
00906 code= mpeg1_index_run[0][run] + alevel - 1;
00907
00908 put_bits(&s->pb, table_vlc[code][1]+1, (table_vlc[code][0]<<1) + sign);
00909 } else {
00910
00911 put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
00912
00913 put_bits(&s->pb, 6, run);
00914 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
00915 if (alevel < 128) {
00916 put_sbits(&s->pb, 8, level);
00917 } else {
00918 if (level < 0) {
00919 put_bits(&s->pb, 16, 0x8001 + level + 255);
00920 } else {
00921 put_sbits(&s->pb, 16, level);
00922 }
00923 }
00924 }else{
00925 put_sbits(&s->pb, 12, level);
00926 }
00927 }
00928 last_non_zero = i;
00929 }
00930 }
00931
00932 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
00933 }
00934
00935 #define OFFSET(x) offsetof(MpegEncContext, x)
00936 #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
00937 #define COMMON_OPTS\
00938 {TIMECODE_OPT(MpegEncContext,\
00939 AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)},\
00940 { "intra_vlc", "Use MPEG-2 intra VLC table.", OFFSET(intra_vlc_format), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },\
00941 { "drop_frame_timecode", "Timecode is in drop frame format.", OFFSET(drop_frame_timecode), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE}, \
00942 { "scan_offset", "Reserve space for SVCD scan offset user data.", OFFSET(scan_offset), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
00943
00944 static const AVOption mpeg1_options[] = {
00945 COMMON_OPTS
00946 { NULL },
00947 };
00948
00949 static const AVOption mpeg2_options[] = {
00950 COMMON_OPTS
00951 { "non_linear_quant", "Use nonlinear quantizer.", OFFSET(q_scale_type), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
00952 { "alternate_scan", "Enable alternate scantable.", OFFSET(alternate_scan), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
00953 { NULL },
00954 };
00955
00956 #define mpeg12_class(x)\
00957 static const AVClass mpeg## x ##_class = {\
00958 .class_name = "mpeg" #x "video encoder",\
00959 .item_name = av_default_item_name,\
00960 .option = mpeg## x ##_options,\
00961 .version = LIBAVUTIL_VERSION_INT,\
00962 };
00963
00964 mpeg12_class(1)
00965 mpeg12_class(2)
00966
00967 AVCodec ff_mpeg1video_encoder = {
00968 .name = "mpeg1video",
00969 .type = AVMEDIA_TYPE_VIDEO,
00970 .id = CODEC_ID_MPEG1VIDEO,
00971 .priv_data_size = sizeof(MpegEncContext),
00972 .init = encode_init,
00973 .encode = MPV_encode_picture,
00974 .close = MPV_encode_end,
00975 .supported_framerates= avpriv_frame_rate_tab+1,
00976 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
00977 .capabilities= CODEC_CAP_DELAY,
00978 .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
00979 .priv_class = &mpeg1_class,
00980 };
00981
00982 AVCodec ff_mpeg2video_encoder = {
00983 .name = "mpeg2video",
00984 .type = AVMEDIA_TYPE_VIDEO,
00985 .id = CODEC_ID_MPEG2VIDEO,
00986 .priv_data_size = sizeof(MpegEncContext),
00987 .init = encode_init,
00988 .encode = MPV_encode_picture,
00989 .close = MPV_encode_end,
00990 .supported_framerates= avpriv_frame_rate_tab+1,
00991 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE},
00992 .capabilities= CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
00993 .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
00994 .priv_class = &mpeg2_class,
00995 };