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