3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "mpegvideo.h"
33 #include "mpeg12data.h"
34 #include "bytestream.h"
36 #include "libavutil/log.h"
37 #include "libavutil/opt.h"
39 static const uint8_t inv_non_linear_qscale[13] = {
41 9,10,11,12,13,14,15,16,
44 static const uint8_t svcd_scan_offset_placeholder[14] = {
52 static void mpeg1_encode_block(MpegEncContext *s,
55 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code); // RAL: f_code parameter added
57 static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
58 static uint8_t fcode_tab[MAX_MV*2+1];
60 static uint8_t uni_mpeg1_ac_vlc_len [64*64*2];
61 static uint8_t uni_mpeg2_ac_vlc_len [64*64*2];
63 /* simple include everything table for dc, first byte is bits number next 3 are code*/
64 static uint32_t mpeg1_lum_dc_uni[512];
65 static uint32_t mpeg1_chr_dc_uni[512];
67 static uint8_t mpeg1_index_run[2][64];
68 static int8_t mpeg1_max_level[2][64];
70 static void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){
76 for(run=0; run<64; run++){
79 int alevel= FFABS(level);
80 int sign= (level>>31)&1;
82 if (alevel > rl->max_level[0][run])
85 code= rl->index_run[0][run] + alevel - 1;
87 if (code < 111 /* rl->n */) {
88 /* store the vlc & sign at once */
89 len= rl->table_vlc[code][1]+1;
90 bits= (rl->table_vlc[code][0]<<1) + sign;
92 len= rl->table_vlc[111/*rl->n*/][1]+6;
93 bits= rl->table_vlc[111/*rl->n*/][0]<<6;
103 bits|= 0x8001 + level + 255;
105 bits|= level & 0xffff;
110 uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
116 static int find_frame_rate_index(MpegEncContext *s){
118 int64_t dmin= INT64_MAX;
122 int64_t n0= 1001LL/ff_frame_rate_tab[i].den*ff_frame_rate_tab[i].num*s->avctx->time_base.num;
123 int64_t n1= 1001LL*s->avctx->time_base.den;
124 if(s->avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL && i>=9) break;
129 s->frame_rate_index= i;
138 static av_cold int encode_init(AVCodecContext *avctx)
140 MpegEncContext *s = avctx->priv_data;
142 if(MPV_encode_init(avctx) < 0)
145 #if FF_API_MPEGVIDEO_GLOBAL_OPTS
146 if (avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE)
147 s->drop_frame_timecode = 1;
150 if(find_frame_rate_index(s) < 0){
151 if(s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
152 av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num);
155 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);
159 if(avctx->profile == FF_PROFILE_UNKNOWN){
160 if(avctx->level != FF_LEVEL_UNKNOWN){
161 av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
164 avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0; /* Main or 4:2:2 */
167 if(avctx->level == FF_LEVEL_UNKNOWN){
168 if(avctx->profile == 0){ /* 4:2:2 */
169 if(avctx->width <= 720 && avctx->height <= 608) avctx->level = 5; /* Main */
170 else avctx->level = 2; /* High */
172 if(avctx->profile != 1 && s->chroma_format != CHROMA_420){
173 av_log(avctx, AV_LOG_ERROR, "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
176 if(avctx->width <= 720 && avctx->height <= 576) avctx->level = 8; /* Main */
177 else if(avctx->width <= 1440) avctx->level = 6; /* High 1440 */
178 else avctx->level = 4; /* High */
182 if (s->drop_frame_timecode && s->frame_rate_index != 4) {
183 av_log(avctx, AV_LOG_ERROR, "Drop frame time code only allowed with 1001/30000 fps\n");
190 static void put_header(MpegEncContext *s, int header)
192 align_put_bits(&s->pb);
193 put_bits(&s->pb, 16, header>>16);
194 put_sbits(&s->pb, 16, header);
197 /* put sequence header if needed */
198 static void mpeg1_encode_sequence_header(MpegEncContext *s)
200 unsigned int vbv_buffer_size;
204 float best_aspect_error= 1E10;
205 float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
206 int constraint_parameter_flag;
208 if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA)
210 if (s->current_picture.f.key_frame) {
211 AVRational framerate= ff_frame_rate_tab[s->frame_rate_index];
213 /* mpeg1 header repeated every gop */
214 put_header(s, SEQ_START_CODE);
216 put_sbits(&s->pb, 12, s->width );
217 put_sbits(&s->pb, 12, s->height);
220 float error= aspect_ratio;
221 if(s->codec_id == CODEC_ID_MPEG1VIDEO || i <=1)
222 error-= 1.0/ff_mpeg1_aspect[i];
224 error-= av_q2d(ff_mpeg2_aspect[i])*s->height/s->width;
228 if(error < best_aspect_error){
229 best_aspect_error= error;
230 s->aspect_ratio_info= i;
234 put_bits(&s->pb, 4, s->aspect_ratio_info);
235 put_bits(&s->pb, 4, s->frame_rate_index);
237 if(s->avctx->rc_max_rate){
238 v = (s->avctx->rc_max_rate + 399) / 400;
239 if (v > 0x3ffff && s->codec_id == CODEC_ID_MPEG1VIDEO)
245 if(s->avctx->rc_buffer_size)
246 vbv_buffer_size = s->avctx->rc_buffer_size;
248 /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
249 vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
250 vbv_buffer_size= (vbv_buffer_size + 16383) / 16384;
252 put_sbits(&s->pb, 18, v);
253 put_bits(&s->pb, 1, 1); /* marker */
254 put_sbits(&s->pb, 10, vbv_buffer_size);
256 constraint_parameter_flag=
257 s->width <= 768 && s->height <= 576 &&
258 s->mb_width * s->mb_height <= 396 &&
259 s->mb_width * s->mb_height * framerate.num <= framerate.den*396*25 &&
260 framerate.num <= framerate.den*30 &&
261 s->avctx->me_range && s->avctx->me_range < 128 &&
262 vbv_buffer_size <= 20 &&
264 s->codec_id == CODEC_ID_MPEG1VIDEO;
266 put_bits(&s->pb, 1, constraint_parameter_flag);
268 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
269 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
271 if(s->codec_id == CODEC_ID_MPEG2VIDEO){
272 put_header(s, EXT_START_CODE);
273 put_bits(&s->pb, 4, 1); //seq ext
275 put_bits(&s->pb, 1, s->avctx->profile == 0); //escx 1 for 4:2:2 profile */
277 put_bits(&s->pb, 3, s->avctx->profile); //profile
278 put_bits(&s->pb, 4, s->avctx->level); //level
280 put_bits(&s->pb, 1, s->progressive_sequence);
281 put_bits(&s->pb, 2, s->chroma_format);
282 put_bits(&s->pb, 2, s->width >>12);
283 put_bits(&s->pb, 2, s->height>>12);
284 put_bits(&s->pb, 12, v>>18); //bitrate ext
285 put_bits(&s->pb, 1, 1); //marker
286 put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext
287 put_bits(&s->pb, 1, s->low_delay);
288 put_bits(&s->pb, 2, 0); // frame_rate_ext_n
289 put_bits(&s->pb, 5, 0); // frame_rate_ext_d
292 put_header(s, GOP_START_CODE);
293 put_bits(&s->pb, 1, s->drop_frame_timecode); /* drop frame flag */
294 /* time code : we must convert from the real frame rate to a
295 fake mpeg frame rate in case of low frame rate */
296 fps = (framerate.num + framerate.den/2)/ framerate.den;
297 time_code = s->current_picture_ptr->f.coded_picture_number + s->avctx->timecode_frame_start;
299 s->gop_picture_number = s->current_picture_ptr->f.coded_picture_number;
300 if (s->drop_frame_timecode) {
301 /* only works for NTSC 29.97 */
302 int d = time_code / 17982;
303 int m = time_code % 17982;
304 //if (m < 2) m += 2; /* not needed since -2,-1 / 1798 in C returns 0 */
305 time_code += 18 * d + 2 * ((m - 2) / 1798);
307 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
308 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
309 put_bits(&s->pb, 1, 1);
310 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
311 put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
312 put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
313 put_bits(&s->pb, 1, 0); /* broken link */
317 static inline void encode_mb_skip_run(MpegEncContext *s, int run){
319 put_bits(&s->pb, 11, 0x008);
322 put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1],
323 ff_mpeg12_mbAddrIncrTable[run][0]);
326 static av_always_inline void put_qscale(MpegEncContext *s)
329 assert(s->qscale>=1 && s->qscale <=12);
330 put_bits(&s->pb, 5, inv_non_linear_qscale[s->qscale]);
332 put_bits(&s->pb, 5, s->qscale);
336 void ff_mpeg1_encode_slice_header(MpegEncContext *s){
337 if (s->height > 2800) {
338 put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127));
339 put_bits(&s->pb, 3, s->mb_y >> 7); /* slice_vertical_position_extension */
341 put_header(s, SLICE_MIN_START_CODE + s->mb_y);
344 put_bits(&s->pb, 1, 0); /* slice extra information */
347 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
349 mpeg1_encode_sequence_header(s);
351 /* mpeg1 picture header */
352 put_header(s, PICTURE_START_CODE);
353 /* temporal reference */
355 // RAL: s->picture_number instead of s->fake_picture_number
356 put_bits(&s->pb, 10, (s->picture_number -
357 s->gop_picture_number) & 0x3ff);
358 put_bits(&s->pb, 3, s->pict_type);
360 s->vbv_delay_ptr= s->pb.buf + put_bits_count(&s->pb)/8;
361 put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
363 // RAL: Forward f_code also needed for B frames
364 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
365 put_bits(&s->pb, 1, 0); /* half pel coordinates */
366 if(s->codec_id == CODEC_ID_MPEG1VIDEO)
367 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
369 put_bits(&s->pb, 3, 7); /* forward_f_code */
372 // RAL: Backward f_code necessary for B frames
373 if (s->pict_type == AV_PICTURE_TYPE_B) {
374 put_bits(&s->pb, 1, 0); /* half pel coordinates */
375 if(s->codec_id == CODEC_ID_MPEG1VIDEO)
376 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
378 put_bits(&s->pb, 3, 7); /* backward_f_code */
381 put_bits(&s->pb, 1, 0); /* extra bit picture */
383 s->frame_pred_frame_dct = 1;
384 if(s->codec_id == CODEC_ID_MPEG2VIDEO){
385 put_header(s, EXT_START_CODE);
386 put_bits(&s->pb, 4, 8); //pic ext
387 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
388 put_bits(&s->pb, 4, s->f_code);
389 put_bits(&s->pb, 4, s->f_code);
391 put_bits(&s->pb, 8, 255);
393 if (s->pict_type == AV_PICTURE_TYPE_B) {
394 put_bits(&s->pb, 4, s->b_code);
395 put_bits(&s->pb, 4, s->b_code);
397 put_bits(&s->pb, 8, 255);
399 put_bits(&s->pb, 2, s->intra_dc_precision);
401 assert(s->picture_structure == PICT_FRAME);
402 put_bits(&s->pb, 2, s->picture_structure);
403 if (s->progressive_sequence) {
404 put_bits(&s->pb, 1, 0); /* no repeat */
406 put_bits(&s->pb, 1, s->current_picture_ptr->f.top_field_first);
408 /* XXX: optimize the generation of this flag with entropy
410 s->frame_pred_frame_dct = s->progressive_sequence;
412 put_bits(&s->pb, 1, s->frame_pred_frame_dct);
413 put_bits(&s->pb, 1, s->concealment_motion_vectors);
414 put_bits(&s->pb, 1, s->q_scale_type);
415 put_bits(&s->pb, 1, s->intra_vlc_format);
416 put_bits(&s->pb, 1, s->alternate_scan);
417 put_bits(&s->pb, 1, s->repeat_first_field);
418 s->progressive_frame = s->progressive_sequence;
419 put_bits(&s->pb, 1, s->chroma_format == CHROMA_420 ? s->progressive_frame : 0); /* chroma_420_type */
420 put_bits(&s->pb, 1, s->progressive_frame);
421 put_bits(&s->pb, 1, 0); //composite_display_flag
423 if(s->flags & CODEC_FLAG_SVCD_SCAN_OFFSET){
426 put_header(s, USER_START_CODE);
427 for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){
428 put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
433 ff_mpeg1_encode_slice_header(s);
436 static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
437 int has_mv, int field_motion)
439 put_bits(&s->pb, n, bits);
440 if (!s->frame_pred_frame_dct) {
442 put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */
443 put_bits(&s->pb, 1, s->interlaced_dct);
447 static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
448 DCTELEM block[6][64],
449 int motion_x, int motion_y,
453 const int mb_x = s->mb_x;
454 const int mb_y = s->mb_y;
455 const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
459 for(i=0;i<mb_block_count;i++) {
460 if (s->block_last_index[i] >= 0)
461 cbp |= 1 << (mb_block_count - 1 - i);
464 if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
465 (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) &&
466 ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) ||
467 (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) |
468 ((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))) {
470 s->qscale -= s->dquant;
474 if(s->pict_type == AV_PICTURE_TYPE_P){
475 s->last_mv[0][1][0]= s->last_mv[0][0][0]=
476 s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0;
480 assert(s->mb_skip_run == 0);
481 encode_mb_skip_run(s, s->mb_x);
483 encode_mb_skip_run(s, s->mb_skip_run);
486 if (s->pict_type == AV_PICTURE_TYPE_I) {
487 if(s->dquant && cbp){
488 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_type : macroblock_quant = 1 */
491 put_mb_modes(s, 1, 1, 0, 0); /* macroblock_type : macroblock_quant = 0 */
492 s->qscale -= s->dquant;
494 s->misc_bits+= get_bits_diff(s);
496 } else if (s->mb_intra) {
497 if(s->dquant && cbp){
498 put_mb_modes(s, 6, 0x01, 0, 0);
501 put_mb_modes(s, 5, 0x03, 0, 0);
502 s->qscale -= s->dquant;
504 s->misc_bits+= get_bits_diff(s);
506 memset(s->last_mv, 0, sizeof(s->last_mv));
507 } else if (s->pict_type == AV_PICTURE_TYPE_P) {
508 if(s->mv_type == MV_TYPE_16X16){
510 if ((motion_x|motion_y) == 0) {
512 put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */
515 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */
517 s->misc_bits+= get_bits_diff(s);
520 put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
523 put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
525 s->misc_bits+= get_bits_diff(s);
526 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
527 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
528 s->mv_bits+= get_bits_diff(s);
531 put_bits(&s->pb, 3, 1); /* motion only */
532 if (!s->frame_pred_frame_dct)
533 put_bits(&s->pb, 2, 2); /* motion_type: frame */
534 s->misc_bits+= get_bits_diff(s);
535 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
536 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
537 s->qscale -= s->dquant;
538 s->mv_bits+= get_bits_diff(s);
540 s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x;
541 s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y;
543 assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
547 put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
550 put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
553 put_bits(&s->pb, 3, 1); /* motion only */
554 put_bits(&s->pb, 2, 1); /* motion_type: field */
555 s->qscale -= s->dquant;
557 s->misc_bits+= get_bits_diff(s);
559 put_bits(&s->pb, 1, s->field_select[0][i]);
560 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
561 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
562 s->last_mv[0][i][0]= s->mv[0][i][0];
563 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
565 s->mv_bits+= get_bits_diff(s);
568 if (s->chroma_y_shift) {
569 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
571 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
572 put_sbits(&s->pb, 2, cbp);
577 if(s->mv_type == MV_TYPE_16X16){
578 if (cbp){ // With coded bloc pattern
580 if(s->mv_dir == MV_DIR_FORWARD)
581 put_mb_modes(s, 6, 3, 1, 0);
583 put_mb_modes(s, 8-s->mv_dir, 2, 1, 0);
586 put_mb_modes(s, 5-s->mv_dir, 3, 1, 0);
588 }else{ // No coded bloc pattern
589 put_bits(&s->pb, 5-s->mv_dir, 2);
590 if (!s->frame_pred_frame_dct)
591 put_bits(&s->pb, 2, 2); /* motion_type: frame */
592 s->qscale -= s->dquant;
594 s->misc_bits += get_bits_diff(s);
595 if (s->mv_dir&MV_DIR_FORWARD){
596 mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
597 mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
598 s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0];
599 s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1];
602 if (s->mv_dir&MV_DIR_BACKWARD){
603 mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
604 mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
605 s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0];
606 s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1];
610 assert(s->mv_type == MV_TYPE_FIELD);
611 assert(!s->frame_pred_frame_dct);
612 if (cbp){ // With coded bloc pattern
614 if(s->mv_dir == MV_DIR_FORWARD)
615 put_mb_modes(s, 6, 3, 1, 1);
617 put_mb_modes(s, 8-s->mv_dir, 2, 1, 1);
620 put_mb_modes(s, 5-s->mv_dir, 3, 1, 1);
622 }else{ // No coded bloc pattern
623 put_bits(&s->pb, 5-s->mv_dir, 2);
624 put_bits(&s->pb, 2, 1); /* motion_type: field */
625 s->qscale -= s->dquant;
627 s->misc_bits += get_bits_diff(s);
628 if (s->mv_dir&MV_DIR_FORWARD){
630 put_bits(&s->pb, 1, s->field_select[0][i]);
631 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
632 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
633 s->last_mv[0][i][0]= s->mv[0][i][0];
634 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
638 if (s->mv_dir&MV_DIR_BACKWARD){
640 put_bits(&s->pb, 1, s->field_select[1][i]);
641 mpeg1_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code);
642 mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code);
643 s->last_mv[1][i][0]= s->mv[1][i][0];
644 s->last_mv[1][i][1]= 2*s->mv[1][i][1];
649 s->mv_bits += get_bits_diff(s);
651 if (s->chroma_y_shift) {
652 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
654 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
655 put_sbits(&s->pb, 2, cbp);
659 for(i=0;i<mb_block_count;i++) {
660 if (cbp & (1 << (mb_block_count - 1 - i))) {
661 mpeg1_encode_block(s, block[i], i);
666 s->i_tex_bits+= get_bits_diff(s);
668 s->p_tex_bits+= get_bits_diff(s);
672 void mpeg1_encode_mb(MpegEncContext *s, DCTELEM block[6][64], int motion_x, int motion_y)
674 if (s->chroma_format == CHROMA_420) mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
675 else mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
678 // RAL: Parameter added: f_or_b_code
679 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
684 ff_mpeg12_mbMotionVectorTable[0][1],
685 ff_mpeg12_mbMotionVectorTable[0][0]);
687 int code, sign, bits;
688 int bit_size = f_or_b_code - 1;
689 int range = 1 << bit_size;
690 /* modulo encoding */
691 int l= INT_BIT - 5 - bit_size;
696 code = (val >> bit_size) + 1;
697 bits = val & (range - 1);
702 code = (val >> bit_size) + 1;
703 bits = val & (range - 1);
707 assert(code > 0 && code <= 16);
710 ff_mpeg12_mbMotionVectorTable[code][1],
711 ff_mpeg12_mbMotionVectorTable[code][0]);
713 put_bits(&s->pb, 1, sign);
715 put_bits(&s->pb, bit_size, bits);
720 void ff_mpeg1_encode_init(MpegEncContext *s)
724 ff_mpeg12_common_init(s);
732 init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
733 init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
737 mpeg1_max_level[0][i]= ff_rl_mpeg1.max_level[0][i];
738 mpeg1_index_run[0][i]= ff_rl_mpeg1.index_run[0][i];
741 init_uni_ac_vlc(&ff_rl_mpeg1, uni_mpeg1_ac_vlc_len);
742 if(s->intra_vlc_format)
743 init_uni_ac_vlc(&ff_rl_mpeg2, uni_mpeg2_ac_vlc_len);
745 /* build unified dc encoding tables */
746 for(i=-255; i<256; i++)
754 index = av_log2(2*adiff);
756 bits= ff_mpeg12_vlc_dc_lum_bits[index] + index;
757 code= (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
758 mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
760 bits= ff_mpeg12_vlc_dc_chroma_bits[index] + index;
761 code= (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
762 mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
765 for(f_code=1; f_code<=MAX_FCODE; f_code++){
766 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
769 if(mv==0) len= ff_mpeg12_mbMotionVectorTable[0][1];
771 int val, bit_size, code;
773 bit_size = f_code - 1;
779 code = (val >> bit_size) + 1;
781 len= ff_mpeg12_mbMotionVectorTable[code][1] + 1 + bit_size;
783 len= ff_mpeg12_mbMotionVectorTable[16][1] + 2 + bit_size;
787 mv_penalty[f_code][mv+MAX_MV]= len;
792 for(f_code=MAX_FCODE; f_code>0; f_code--){
793 for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
794 fcode_tab[mv+MAX_MV]= f_code;
798 s->me.mv_penalty= mv_penalty;
799 s->fcode_tab= fcode_tab;
800 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
807 if (s->intra_vlc_format) {
808 s->intra_ac_vlc_length=
809 s->intra_ac_vlc_last_length= uni_mpeg2_ac_vlc_len;
811 s->intra_ac_vlc_length=
812 s->intra_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
814 s->inter_ac_vlc_length=
815 s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
818 static inline void encode_dc(MpegEncContext *s, int diff, int component)
820 if(((unsigned) (diff+255)) >= 511){
824 index= av_log2_16bit(-2*diff);
827 index= av_log2_16bit(2*diff);
829 if (component == 0) {
832 ff_mpeg12_vlc_dc_lum_bits[index] + index,
833 (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)));
837 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
838 (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)));
841 if (component == 0) {
844 mpeg1_lum_dc_uni[diff+255]&0xFF,
845 mpeg1_lum_dc_uni[diff+255]>>8);
849 mpeg1_chr_dc_uni[diff+255]&0xFF,
850 mpeg1_chr_dc_uni[diff+255]>>8);
855 static void mpeg1_encode_block(MpegEncContext *s,
859 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
861 const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
863 last_index = s->block_last_index[n];
867 component = (n <= 3 ? 0 : (n&1) + 1);
868 dc = block[0]; /* overflow is impossible */
869 diff = dc - s->last_dc[component];
870 encode_dc(s, diff, component);
871 s->last_dc[component] = dc;
873 if (s->intra_vlc_format)
874 table_vlc = ff_rl_mpeg2.table_vlc;
876 /* encode the first coefficient : needs to be done here because
877 it is handled slightly differently */
879 if (abs(level) == 1) {
880 code = ((uint32_t)level >> 31); /* the sign bit */
881 put_bits(&s->pb, 2, code | 0x02);
890 /* now quantify & encode AC coefs */
891 last_non_zero = i - 1;
893 for(;i<=last_index;i++) {
894 j = s->intra_scantable.permutated[i];
897 /* encode using VLC */
899 run = i - last_non_zero - 1;
902 MASK_ABS(sign, alevel)
905 if (alevel <= mpeg1_max_level[0][run]){
906 code= mpeg1_index_run[0][run] + alevel - 1;
907 /* store the vlc & sign at once */
908 put_bits(&s->pb, table_vlc[code][1]+1, (table_vlc[code][0]<<1) + sign);
910 /* escape seems to be pretty rare <5% so I do not optimize it */
911 put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
912 /* escape: only clip in this case */
913 put_bits(&s->pb, 6, run);
914 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
916 put_sbits(&s->pb, 8, level);
919 put_bits(&s->pb, 16, 0x8001 + level + 255);
921 put_sbits(&s->pb, 16, level);
925 put_sbits(&s->pb, 12, level);
932 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
935 #define OFFSET(x) offsetof(MpegEncContext, x)
936 #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
938 { "intra_vlc", "Use MPEG-2 intra VLC table.", OFFSET(intra_vlc_format), FF_OPT_TYPE_INT, { 0 }, 0, 1, VE },\
939 { "drop_frame_timecode", "Timecode is in drop frame format.", OFFSET(drop_frame_timecode), FF_OPT_TYPE_INT, { 0 }, 0, 1, VE},
941 static const AVOption mpeg1_options[] = {
946 static const AVOption mpeg2_options[] = {
948 { "non_linear_quant", "Use nonlinear quantizer.", OFFSET(q_scale_type), FF_OPT_TYPE_INT, { 0 }, 0, 1, VE },
952 #define mpeg12_class(x)\
953 static const AVClass mpeg## x ##_class = {\
954 .class_name = "mpeg" #x "video encoder",\
955 .item_name = av_default_item_name,\
956 .option = mpeg## x ##_options,\
957 .version = LIBAVUTIL_VERSION_INT,\
963 AVCodec ff_mpeg1video_encoder = {
964 .name = "mpeg1video",
965 .type = AVMEDIA_TYPE_VIDEO,
966 .id = CODEC_ID_MPEG1VIDEO,
967 .priv_data_size = sizeof(MpegEncContext),
969 .encode = MPV_encode_picture,
970 .close = MPV_encode_end,
971 .supported_framerates= ff_frame_rate_tab+1,
972 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
973 .capabilities= CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
974 .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
975 .priv_class = &mpeg1_class,
978 AVCodec ff_mpeg2video_encoder = {
979 .name = "mpeg2video",
980 .type = AVMEDIA_TYPE_VIDEO,
981 .id = CODEC_ID_MPEG2VIDEO,
982 .priv_data_size = sizeof(MpegEncContext),
984 .encode = MPV_encode_picture,
985 .close = MPV_encode_end,
986 .supported_framerates= ff_frame_rate_tab+1,
987 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE},
988 .capabilities= CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
989 .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
990 .priv_class = &mpeg2_class,