3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
8 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/log.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/timecode.h"
35 #include "libavutil/stereo3d.h"
38 #include "bytestream.h"
41 #include "mpeg12data.h"
42 #include "mpegvideo.h"
45 static const uint8_t inv_non_linear_qscale[] = {
46 0, 2, 4, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16,
49 static const uint8_t svcd_scan_offset_placeholder[] = {
50 0x10, 0x0E, 0x00, 0x80, 0x81, 0x00, 0x80,
51 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
54 static uint8_t mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
55 static uint8_t fcode_tab[MAX_MV * 2 + 1];
57 static uint8_t uni_mpeg1_ac_vlc_len[64 * 64 * 2];
58 static uint8_t uni_mpeg2_ac_vlc_len[64 * 64 * 2];
60 /* simple include everything table for dc, first byte is bits
61 * number next 3 are code */
62 static uint32_t mpeg1_lum_dc_uni[512];
63 static uint32_t mpeg1_chr_dc_uni[512];
65 static uint8_t mpeg1_index_run[2][64];
66 static int8_t mpeg1_max_level[2][64];
68 static av_cold void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len)
72 for (i = 0; i < 128; i++) {
77 for (run = 0; run < 64; run++) {
79 int alevel = FFABS(level);
81 if (alevel > rl->max_level[0][run])
82 code = 111; /* rl->n */
84 code = rl->index_run[0][run] + alevel - 1;
86 if (code < 111) { /* rl->n */
87 /* length of VLC and sign */
88 len = rl->table_vlc[code][1] + 1;
90 len = rl->table_vlc[111 /* rl->n */][1] + 6;
98 uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
103 static int find_frame_rate_index(MpegEncContext *s)
106 AVRational bestq = (AVRational) {0, 0};
108 AVRational target = av_inv_q(s->avctx->time_base);
110 for (i = 1; i < 14; i++) {
111 if (s->avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL &&
115 for (ext.num=1; ext.num <= 4; ext.num++) {
116 for (ext.den=1; ext.den <= 32; ext.den++) {
117 AVRational q = av_mul_q(ext, ff_mpeg12_frame_rate_tab[i]);
119 if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO && (ext.den!=1 || ext.num!=1))
121 if (av_gcd(ext.den, ext.num) != 1)
125 || av_nearer_q(target, bestq, q) < 0
126 || ext.num==1 && ext.den==1 && av_nearer_q(target, bestq, q) == 0) {
128 s->frame_rate_index = i;
129 s->mpeg2_frame_rate_ext.num = ext.num;
130 s->mpeg2_frame_rate_ext.den = ext.den;
136 if (av_cmp_q(target, bestq))
142 static av_cold int encode_init(AVCodecContext *avctx)
144 MpegEncContext *s = avctx->priv_data;
146 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && avctx->height > 2800)
147 avctx->thread_count = 1;
149 if (ff_MPV_encode_init(avctx) < 0)
152 if (find_frame_rate_index(s) < 0) {
153 if (s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
154 av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n",
155 avctx->time_base.den, avctx->time_base.num);
158 av_log(avctx, AV_LOG_INFO,
159 "MPEG1/2 does not support %d/%d fps, there may be AV sync issues\n",
160 avctx->time_base.den, avctx->time_base.num);
164 if (avctx->profile == FF_PROFILE_UNKNOWN) {
165 if (avctx->level != FF_LEVEL_UNKNOWN) {
166 av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
170 avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0;
173 if (avctx->level == FF_LEVEL_UNKNOWN) {
174 if (avctx->profile == 0) { /* 4:2:2 */
175 if (avctx->width <= 720 && avctx->height <= 608)
176 avctx->level = 5; /* Main */
178 avctx->level = 2; /* High */
180 if (avctx->profile != 1 && s->chroma_format != CHROMA_420) {
181 av_log(avctx, AV_LOG_ERROR,
182 "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
185 if (avctx->width <= 720 && avctx->height <= 576)
186 avctx->level = 8; /* Main */
187 else if (avctx->width <= 1440)
188 avctx->level = 6; /* High 1440 */
190 avctx->level = 4; /* High */
194 if ((avctx->width & 0xFFF) == 0 && (avctx->height & 0xFFF) == 1) {
195 av_log(avctx, AV_LOG_ERROR, "Width / Height is invalid for MPEG2\n");
196 return AVERROR(EINVAL);
199 if (s->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
200 if ((avctx->width & 0xFFF) == 0 || (avctx->height & 0xFFF) == 0) {
201 av_log(avctx, AV_LOG_ERROR, "Width or Height are not allowed to be multiplies of 4096\n"
202 "add '-strict %d' if you want to use them anyway.\n", FF_COMPLIANCE_UNOFFICIAL);
203 return AVERROR(EINVAL);
207 s->drop_frame_timecode = s->drop_frame_timecode || !!(avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE);
208 if (s->drop_frame_timecode)
209 s->tc.flags |= AV_TIMECODE_FLAG_DROPFRAME;
210 if (s->drop_frame_timecode && s->frame_rate_index != 4) {
211 av_log(avctx, AV_LOG_ERROR,
212 "Drop frame time code only allowed with 1001/30000 fps\n");
217 AVRational rate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
218 int ret = av_timecode_init_from_string(&s->tc, rate, s->tc_opt_str, s);
221 s->drop_frame_timecode = !!(s->tc.flags & AV_TIMECODE_FLAG_DROPFRAME);
222 s->avctx->timecode_frame_start = s->tc.start;
224 s->avctx->timecode_frame_start = 0; // default is -1
229 static void put_header(MpegEncContext *s, int header)
231 avpriv_align_put_bits(&s->pb);
232 put_bits(&s->pb, 16, header >> 16);
233 put_sbits(&s->pb, 16, header);
236 /* put sequence header if needed */
237 static void mpeg1_encode_sequence_header(MpegEncContext *s)
239 unsigned int vbv_buffer_size, fps, v;
240 int i, constraint_parameter_flag;
242 float best_aspect_error = 1E10;
243 float aspect_ratio = av_q2d(s->avctx->sample_aspect_ratio);
245 if (aspect_ratio == 0.0)
246 aspect_ratio = 1.0; // pixel aspect 1.1 (VGA)
248 if (s->current_picture.f.key_frame) {
249 AVRational framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
251 /* mpeg1 header repeated every gop */
252 put_header(s, SEQ_START_CODE);
254 put_sbits(&s->pb, 12, s->width & 0xFFF);
255 put_sbits(&s->pb, 12, s->height & 0xFFF);
257 for (i = 1; i < 15; i++) {
258 float error = aspect_ratio;
259 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || i <= 1)
260 error -= 1.0 / ff_mpeg1_aspect[i];
262 error -= av_q2d(ff_mpeg2_aspect[i]) * s->height / s->width;
264 error = FFABS(error);
266 if (error < best_aspect_error) {
267 best_aspect_error = error;
268 s->aspect_ratio_info = i;
272 put_bits(&s->pb, 4, s->aspect_ratio_info);
273 put_bits(&s->pb, 4, s->frame_rate_index);
275 if (s->avctx->rc_max_rate) {
276 v = (s->avctx->rc_max_rate + 399) / 400;
277 if (v > 0x3ffff && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
283 if (s->avctx->rc_buffer_size)
284 vbv_buffer_size = s->avctx->rc_buffer_size;
286 /* VBV calculation: Scaled so that a VCD has the proper
287 * VBV size of 40 kilobytes */
288 vbv_buffer_size = ((20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
289 vbv_buffer_size = (vbv_buffer_size + 16383) / 16384;
291 put_sbits(&s->pb, 18, v);
292 put_bits(&s->pb, 1, 1); // marker
293 put_sbits(&s->pb, 10, vbv_buffer_size);
295 constraint_parameter_flag =
298 s->mb_width * s->mb_height <= 396 &&
299 s->mb_width * s->mb_height * framerate.num <= 396 * 25 * framerate.den &&
300 framerate.num <= framerate.den * 30 &&
301 s->avctx->me_range &&
302 s->avctx->me_range < 128 &&
303 vbv_buffer_size <= 20 &&
304 v <= 1856000 / 400 &&
305 s->codec_id == AV_CODEC_ID_MPEG1VIDEO;
307 put_bits(&s->pb, 1, constraint_parameter_flag);
309 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
310 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
312 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
313 put_header(s, EXT_START_CODE);
314 put_bits(&s->pb, 4, 1); // seq ext
316 put_bits(&s->pb, 1, s->avctx->profile == 0); // escx 1 for 4:2:2 profile
318 put_bits(&s->pb, 3, s->avctx->profile); // profile
319 put_bits(&s->pb, 4, s->avctx->level); // level
321 put_bits(&s->pb, 1, s->progressive_sequence);
322 put_bits(&s->pb, 2, s->chroma_format);
323 put_bits(&s->pb, 2, s->width >> 12);
324 put_bits(&s->pb, 2, s->height >> 12);
325 put_bits(&s->pb, 12, v >> 18); // bitrate ext
326 put_bits(&s->pb, 1, 1); // marker
327 put_bits(&s->pb, 8, vbv_buffer_size >> 10); // vbv buffer ext
328 put_bits(&s->pb, 1, s->low_delay);
329 put_bits(&s->pb, 2, s->mpeg2_frame_rate_ext.num-1); // frame_rate_ext_n
330 put_bits(&s->pb, 5, s->mpeg2_frame_rate_ext.den-1); // frame_rate_ext_d
333 put_header(s, GOP_START_CODE);
334 put_bits(&s->pb, 1, s->drop_frame_timecode); // drop frame flag
335 /* time code: we must convert from the real frame rate to a
336 * fake MPEG frame rate in case of low frame rate */
337 fps = (framerate.num + framerate.den / 2) / framerate.den;
338 time_code = s->current_picture_ptr->f.coded_picture_number +
339 s->avctx->timecode_frame_start;
341 s->gop_picture_number = s->current_picture_ptr->f.coded_picture_number;
343 av_assert0(s->drop_frame_timecode == !!(s->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
344 if (s->drop_frame_timecode)
345 time_code = av_timecode_adjust_ntsc_framenum2(time_code, fps);
347 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
348 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
349 put_bits(&s->pb, 1, 1);
350 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
351 put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
352 put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
353 put_bits(&s->pb, 1, 0); // broken link
357 static inline void encode_mb_skip_run(MpegEncContext *s, int run)
360 put_bits(&s->pb, 11, 0x008);
363 put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1],
364 ff_mpeg12_mbAddrIncrTable[run][0]);
367 static av_always_inline void put_qscale(MpegEncContext *s)
369 if (s->q_scale_type) {
370 av_assert2(s->qscale >= 1 && s->qscale <= 12);
371 put_bits(&s->pb, 5, inv_non_linear_qscale[s->qscale]);
373 put_bits(&s->pb, 5, s->qscale);
377 void ff_mpeg1_encode_slice_header(MpegEncContext *s)
379 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->height > 2800) {
380 put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127));
381 /* slice_vertical_position_extension */
382 put_bits(&s->pb, 3, s->mb_y >> 7);
384 put_header(s, SLICE_MIN_START_CODE + s->mb_y);
387 /* slice extra information */
388 put_bits(&s->pb, 1, 0);
391 void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
393 AVFrameSideData *side_data;
394 mpeg1_encode_sequence_header(s);
396 /* mpeg1 picture header */
397 put_header(s, PICTURE_START_CODE);
398 /* temporal reference */
400 // RAL: s->picture_number instead of s->fake_picture_number
402 (s->picture_number - s->gop_picture_number) & 0x3ff);
403 put_bits(&s->pb, 3, s->pict_type);
405 s->vbv_delay_ptr = s->pb.buf + put_bits_count(&s->pb) / 8;
406 put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
408 // RAL: Forward f_code also needed for B-frames
409 if (s->pict_type == AV_PICTURE_TYPE_P ||
410 s->pict_type == AV_PICTURE_TYPE_B) {
411 put_bits(&s->pb, 1, 0); /* half pel coordinates */
412 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
413 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
415 put_bits(&s->pb, 3, 7); /* forward_f_code */
418 // RAL: Backward f_code necessary for B-frames
419 if (s->pict_type == AV_PICTURE_TYPE_B) {
420 put_bits(&s->pb, 1, 0); /* half pel coordinates */
421 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
422 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
424 put_bits(&s->pb, 3, 7); /* backward_f_code */
427 put_bits(&s->pb, 1, 0); /* extra bit picture */
429 s->frame_pred_frame_dct = 1;
430 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
431 put_header(s, EXT_START_CODE);
432 put_bits(&s->pb, 4, 8); /* pic ext */
433 if (s->pict_type == AV_PICTURE_TYPE_P ||
434 s->pict_type == AV_PICTURE_TYPE_B) {
435 put_bits(&s->pb, 4, s->f_code);
436 put_bits(&s->pb, 4, s->f_code);
438 put_bits(&s->pb, 8, 255);
440 if (s->pict_type == AV_PICTURE_TYPE_B) {
441 put_bits(&s->pb, 4, s->b_code);
442 put_bits(&s->pb, 4, s->b_code);
444 put_bits(&s->pb, 8, 255);
446 put_bits(&s->pb, 2, s->intra_dc_precision);
448 av_assert0(s->picture_structure == PICT_FRAME);
449 put_bits(&s->pb, 2, s->picture_structure);
450 if (s->progressive_sequence)
451 put_bits(&s->pb, 1, 0); /* no repeat */
453 put_bits(&s->pb, 1, s->current_picture_ptr->f.top_field_first);
454 /* XXX: optimize the generation of this flag with entropy measures */
455 s->frame_pred_frame_dct = s->progressive_sequence;
457 put_bits(&s->pb, 1, s->frame_pred_frame_dct);
458 put_bits(&s->pb, 1, s->concealment_motion_vectors);
459 put_bits(&s->pb, 1, s->q_scale_type);
460 put_bits(&s->pb, 1, s->intra_vlc_format);
461 put_bits(&s->pb, 1, s->alternate_scan);
462 put_bits(&s->pb, 1, s->repeat_first_field);
463 s->progressive_frame = s->progressive_sequence;
464 /* chroma_420_type */
465 put_bits(&s->pb, 1, s->chroma_format ==
466 CHROMA_420 ? s->progressive_frame : 0);
467 put_bits(&s->pb, 1, s->progressive_frame);
468 put_bits(&s->pb, 1, 0); /* composite_display_flag */
470 if (s->scan_offset) {
473 put_header(s, USER_START_CODE);
474 for (i = 0; i < sizeof(svcd_scan_offset_placeholder); i++)
475 put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
477 side_data = av_frame_get_side_data(&s->current_picture_ptr->f,
478 AV_FRAME_DATA_STEREO3D);
480 AVStereo3D *stereo = (AVStereo3D *)side_data->data;
483 switch (stereo->type) {
484 case AV_STEREO3D_SIDEBYSIDE:
487 case AV_STEREO3D_TOPBOTTOM:
493 case AV_STEREO3D_SIDEBYSIDE_QUINCUNX:
502 put_header(s, USER_START_CODE);
503 put_bits(&s->pb, 8, 'J'); // S3D_video_format_signaling_identifier
504 put_bits(&s->pb, 8, 'P');
505 put_bits(&s->pb, 8, '3');
506 put_bits(&s->pb, 8, 'D');
507 put_bits(&s->pb, 8, 0x03); // S3D_video_format_length
509 put_bits(&s->pb, 1, 1); // reserved_bit
510 put_bits(&s->pb, 7, fpa_type); // S3D_video_format_type
511 put_bits(&s->pb, 8, 0x04); // reserved_data[0]
512 put_bits(&s->pb, 8, 0xFF); // reserved_data[1]
517 ff_mpeg1_encode_slice_header(s);
520 static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
521 int has_mv, int field_motion)
523 put_bits(&s->pb, n, bits);
524 if (!s->frame_pred_frame_dct) {
526 /* motion_type: frame/field */
527 put_bits(&s->pb, 2, 2 - field_motion);
528 put_bits(&s->pb, 1, s->interlaced_dct);
532 // RAL: Parameter added: f_or_b_code
533 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
538 ff_mpeg12_mbMotionVectorTable[0][1],
539 ff_mpeg12_mbMotionVectorTable[0][0]);
541 int code, sign, bits;
542 int bit_size = f_or_b_code - 1;
543 int range = 1 << bit_size;
544 /* modulo encoding */
545 val = sign_extend(val, 5 + bit_size);
549 code = (val >> bit_size) + 1;
550 bits = val & (range - 1);
555 code = (val >> bit_size) + 1;
556 bits = val & (range - 1);
560 av_assert2(code > 0 && code <= 16);
563 ff_mpeg12_mbMotionVectorTable[code][1],
564 ff_mpeg12_mbMotionVectorTable[code][0]);
566 put_bits(&s->pb, 1, sign);
568 put_bits(&s->pb, bit_size, bits);
572 static inline void encode_dc(MpegEncContext *s, int diff, int component)
574 if (((unsigned) (diff + 255)) >= 511) {
578 index = av_log2_16bit(-2 * diff);
581 index = av_log2_16bit(2 * diff);
585 ff_mpeg12_vlc_dc_lum_bits[index] + index,
586 (ff_mpeg12_vlc_dc_lum_code[index] << index) +
587 (diff & ((1 << index) - 1)));
590 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
591 (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
592 (diff & ((1 << index) - 1)));
596 mpeg1_lum_dc_uni[diff + 255] & 0xFF,
597 mpeg1_lum_dc_uni[diff + 255] >> 8);
600 mpeg1_chr_dc_uni[diff + 255] & 0xFF,
601 mpeg1_chr_dc_uni[diff + 255] >> 8);
605 static void mpeg1_encode_block(MpegEncContext *s, int16_t *block, int n)
607 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
609 const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
611 last_index = s->block_last_index[n];
615 component = (n <= 3 ? 0 : (n & 1) + 1);
616 dc = block[0]; /* overflow is impossible */
617 diff = dc - s->last_dc[component];
618 encode_dc(s, diff, component);
619 s->last_dc[component] = dc;
621 if (s->intra_vlc_format)
622 table_vlc = ff_rl_mpeg2.table_vlc;
624 /* encode the first coefficient: needs to be done here because
625 * it is handled slightly differently */
627 if (abs(level) == 1) {
628 code = ((uint32_t)level >> 31); /* the sign bit */
629 put_bits(&s->pb, 2, code | 0x02);
638 /* now quantify & encode AC coefs */
639 last_non_zero = i - 1;
641 for (; i <= last_index; i++) {
642 j = s->intra_scantable.permutated[i];
646 /* encode using VLC */
648 run = i - last_non_zero - 1;
651 MASK_ABS(sign, alevel);
654 if (alevel <= mpeg1_max_level[0][run]) {
655 code = mpeg1_index_run[0][run] + alevel - 1;
656 /* store the VLC & sign at once */
657 put_bits(&s->pb, table_vlc[code][1] + 1,
658 (table_vlc[code][0] << 1) + sign);
660 /* escape seems to be pretty rare <5% so I do not optimize it */
661 put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
662 /* escape: only clip in this case */
663 put_bits(&s->pb, 6, run);
664 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
666 put_sbits(&s->pb, 8, level);
669 put_bits(&s->pb, 16, 0x8001 + level + 255);
671 put_sbits(&s->pb, 16, level);
674 put_sbits(&s->pb, 12, level);
681 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
684 static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
685 int16_t block[6][64],
686 int motion_x, int motion_y,
690 const int mb_x = s->mb_x;
691 const int mb_y = s->mb_y;
692 const int first_mb = mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
696 for (i = 0; i < mb_block_count; i++)
697 if (s->block_last_index[i] >= 0)
698 cbp |= 1 << (mb_block_count - 1 - i);
700 if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
701 (mb_x != s->mb_width - 1 ||
702 (mb_y != s->end_mb_y - 1 && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)) &&
703 ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) ||
704 (s->pict_type == AV_PICTURE_TYPE_B && s->mv_dir == s->last_mv_dir &&
705 (((s->mv_dir & MV_DIR_FORWARD)
706 ? ((s->mv[0][0][0] - s->last_mv[0][0][0]) |
707 (s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
708 ((s->mv_dir & MV_DIR_BACKWARD)
709 ? ((s->mv[1][0][0] - s->last_mv[1][0][0]) |
710 (s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
712 s->qscale -= s->dquant;
716 if (s->pict_type == AV_PICTURE_TYPE_P) {
717 s->last_mv[0][0][0] =
718 s->last_mv[0][0][1] =
719 s->last_mv[0][1][0] =
720 s->last_mv[0][1][1] = 0;
724 av_assert0(s->mb_skip_run == 0);
725 encode_mb_skip_run(s, s->mb_x);
727 encode_mb_skip_run(s, s->mb_skip_run);
730 if (s->pict_type == AV_PICTURE_TYPE_I) {
731 if (s->dquant && cbp) {
732 /* macroblock_type: macroblock_quant = 1 */
733 put_mb_modes(s, 2, 1, 0, 0);
736 /* macroblock_type: macroblock_quant = 0 */
737 put_mb_modes(s, 1, 1, 0, 0);
738 s->qscale -= s->dquant;
740 s->misc_bits += get_bits_diff(s);
742 } else if (s->mb_intra) {
743 if (s->dquant && cbp) {
744 put_mb_modes(s, 6, 0x01, 0, 0);
747 put_mb_modes(s, 5, 0x03, 0, 0);
748 s->qscale -= s->dquant;
750 s->misc_bits += get_bits_diff(s);
752 memset(s->last_mv, 0, sizeof(s->last_mv));
753 } else if (s->pict_type == AV_PICTURE_TYPE_P) {
754 if (s->mv_type == MV_TYPE_16X16) {
756 if ((motion_x | motion_y) == 0) {
758 /* macroblock_pattern & quant */
759 put_mb_modes(s, 5, 1, 0, 0);
762 /* macroblock_pattern only */
763 put_mb_modes(s, 2, 1, 0, 0);
765 s->misc_bits += get_bits_diff(s);
768 put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
771 put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
773 s->misc_bits += get_bits_diff(s);
774 // RAL: f_code parameter added
775 mpeg1_encode_motion(s,
776 motion_x - s->last_mv[0][0][0],
778 // RAL: f_code parameter added
779 mpeg1_encode_motion(s,
780 motion_y - s->last_mv[0][0][1],
782 s->mv_bits += get_bits_diff(s);
785 put_bits(&s->pb, 3, 1); /* motion only */
786 if (!s->frame_pred_frame_dct)
787 put_bits(&s->pb, 2, 2); /* motion_type: frame */
788 s->misc_bits += get_bits_diff(s);
789 // RAL: f_code parameter added
790 mpeg1_encode_motion(s,
791 motion_x - s->last_mv[0][0][0],
793 // RAL: f_code parameter added
794 mpeg1_encode_motion(s,
795 motion_y - s->last_mv[0][0][1],
797 s->qscale -= s->dquant;
798 s->mv_bits += get_bits_diff(s);
800 s->last_mv[0][1][0] = s->last_mv[0][0][0] = motion_x;
801 s->last_mv[0][1][1] = s->last_mv[0][0][1] = motion_y;
803 av_assert2(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
807 put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
810 put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
813 put_bits(&s->pb, 3, 1); /* motion only */
814 put_bits(&s->pb, 2, 1); /* motion_type: field */
815 s->qscale -= s->dquant;
817 s->misc_bits += get_bits_diff(s);
818 for (i = 0; i < 2; i++) {
819 put_bits(&s->pb, 1, s->field_select[0][i]);
820 mpeg1_encode_motion(s,
821 s->mv[0][i][0] - s->last_mv[0][i][0],
823 mpeg1_encode_motion(s,
824 s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
826 s->last_mv[0][i][0] = s->mv[0][i][0];
827 s->last_mv[0][i][1] = 2 * s->mv[0][i][1];
829 s->mv_bits += get_bits_diff(s);
832 if (s->chroma_y_shift) {
834 ff_mpeg12_mbPatTable[cbp][1],
835 ff_mpeg12_mbPatTable[cbp][0]);
838 ff_mpeg12_mbPatTable[cbp >> 2][1],
839 ff_mpeg12_mbPatTable[cbp >> 2][0]);
840 put_sbits(&s->pb, 2, cbp);
845 if (s->mv_type == MV_TYPE_16X16) {
846 if (cbp) { // With coded bloc pattern
848 if (s->mv_dir == MV_DIR_FORWARD)
849 put_mb_modes(s, 6, 3, 1, 0);
851 put_mb_modes(s, 8 - s->mv_dir, 2, 1, 0);
854 put_mb_modes(s, 5 - s->mv_dir, 3, 1, 0);
856 } else { // No coded bloc pattern
857 put_bits(&s->pb, 5 - s->mv_dir, 2);
858 if (!s->frame_pred_frame_dct)
859 put_bits(&s->pb, 2, 2); /* motion_type: frame */
860 s->qscale -= s->dquant;
862 s->misc_bits += get_bits_diff(s);
863 if (s->mv_dir & MV_DIR_FORWARD) {
864 mpeg1_encode_motion(s,
865 s->mv[0][0][0] - s->last_mv[0][0][0],
867 mpeg1_encode_motion(s,
868 s->mv[0][0][1] - s->last_mv[0][0][1],
870 s->last_mv[0][0][0] =
871 s->last_mv[0][1][0] = s->mv[0][0][0];
872 s->last_mv[0][0][1] =
873 s->last_mv[0][1][1] = s->mv[0][0][1];
876 if (s->mv_dir & MV_DIR_BACKWARD) {
877 mpeg1_encode_motion(s,
878 s->mv[1][0][0] - s->last_mv[1][0][0],
880 mpeg1_encode_motion(s,
881 s->mv[1][0][1] - s->last_mv[1][0][1],
883 s->last_mv[1][0][0] =
884 s->last_mv[1][1][0] = s->mv[1][0][0];
885 s->last_mv[1][0][1] =
886 s->last_mv[1][1][1] = s->mv[1][0][1];
890 av_assert2(s->mv_type == MV_TYPE_FIELD);
891 av_assert2(!s->frame_pred_frame_dct);
892 if (cbp) { // With coded bloc pattern
894 if (s->mv_dir == MV_DIR_FORWARD)
895 put_mb_modes(s, 6, 3, 1, 1);
897 put_mb_modes(s, 8 - s->mv_dir, 2, 1, 1);
900 put_mb_modes(s, 5 - s->mv_dir, 3, 1, 1);
902 } else { // No coded bloc pattern
903 put_bits(&s->pb, 5 - s->mv_dir, 2);
904 put_bits(&s->pb, 2, 1); /* motion_type: field */
905 s->qscale -= s->dquant;
907 s->misc_bits += get_bits_diff(s);
908 if (s->mv_dir & MV_DIR_FORWARD) {
909 for (i = 0; i < 2; i++) {
910 put_bits(&s->pb, 1, s->field_select[0][i]);
911 mpeg1_encode_motion(s,
912 s->mv[0][i][0] - s->last_mv[0][i][0],
914 mpeg1_encode_motion(s,
915 s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
917 s->last_mv[0][i][0] = s->mv[0][i][0];
918 s->last_mv[0][i][1] = s->mv[0][i][1] * 2;
922 if (s->mv_dir & MV_DIR_BACKWARD) {
923 for (i = 0; i < 2; i++) {
924 put_bits(&s->pb, 1, s->field_select[1][i]);
925 mpeg1_encode_motion(s,
926 s->mv[1][i][0] - s->last_mv[1][i][0],
928 mpeg1_encode_motion(s,
929 s->mv[1][i][1] - (s->last_mv[1][i][1] >> 1),
931 s->last_mv[1][i][0] = s->mv[1][i][0];
932 s->last_mv[1][i][1] = s->mv[1][i][1] * 2;
937 s->mv_bits += get_bits_diff(s);
939 if (s->chroma_y_shift) {
941 ff_mpeg12_mbPatTable[cbp][1],
942 ff_mpeg12_mbPatTable[cbp][0]);
945 ff_mpeg12_mbPatTable[cbp >> 2][1],
946 ff_mpeg12_mbPatTable[cbp >> 2][0]);
947 put_sbits(&s->pb, 2, cbp);
951 for (i = 0; i < mb_block_count; i++)
952 if (cbp & (1 << (mb_block_count - 1 - i)))
953 mpeg1_encode_block(s, block[i], i);
956 s->i_tex_bits += get_bits_diff(s);
958 s->p_tex_bits += get_bits_diff(s);
962 void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[6][64],
963 int motion_x, int motion_y)
965 if (s->chroma_format == CHROMA_420)
966 mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
968 mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
971 av_cold void ff_mpeg1_encode_init(MpegEncContext *s)
975 ff_mpeg12_common_init(s);
983 ff_init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
984 ff_init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
986 for (i = 0; i < 64; i++) {
987 mpeg1_max_level[0][i] = ff_rl_mpeg1.max_level[0][i];
988 mpeg1_index_run[0][i] = ff_rl_mpeg1.index_run[0][i];
991 init_uni_ac_vlc(&ff_rl_mpeg1, uni_mpeg1_ac_vlc_len);
992 if (s->intra_vlc_format)
993 init_uni_ac_vlc(&ff_rl_mpeg2, uni_mpeg2_ac_vlc_len);
995 /* build unified dc encoding tables */
996 for (i = -255; i < 256; i++) {
1001 adiff = FFABS(diff);
1004 index = av_log2(2 * adiff);
1006 bits = ff_mpeg12_vlc_dc_lum_bits[index] + index;
1007 code = (ff_mpeg12_vlc_dc_lum_code[index] << index) +
1008 (diff & ((1 << index) - 1));
1009 mpeg1_lum_dc_uni[i + 255] = bits + (code << 8);
1011 bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index;
1012 code = (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
1013 (diff & ((1 << index) - 1));
1014 mpeg1_chr_dc_uni[i + 255] = bits + (code << 8);
1017 for (f_code = 1; f_code <= MAX_FCODE; f_code++)
1018 for (mv = -MAX_MV; mv <= MAX_MV; mv++) {
1022 len = ff_mpeg12_mbMotionVectorTable[0][1];
1024 int val, bit_size, code;
1026 bit_size = f_code - 1;
1032 code = (val >> bit_size) + 1;
1034 len = ff_mpeg12_mbMotionVectorTable[code][1] +
1037 len = ff_mpeg12_mbMotionVectorTable[16][1] +
1041 mv_penalty[f_code][mv + MAX_MV] = len;
1045 for (f_code = MAX_FCODE; f_code > 0; f_code--)
1046 for (mv = -(8 << f_code); mv < (8 << f_code); mv++)
1047 fcode_tab[mv + MAX_MV] = f_code;
1049 s->me.mv_penalty = mv_penalty;
1050 s->fcode_tab = fcode_tab;
1051 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1052 s->min_qcoeff = -255;
1053 s->max_qcoeff = 255;
1055 s->min_qcoeff = -2047;
1056 s->max_qcoeff = 2047;
1058 if (s->intra_vlc_format) {
1059 s->intra_ac_vlc_length =
1060 s->intra_ac_vlc_last_length = uni_mpeg2_ac_vlc_len;
1062 s->intra_ac_vlc_length =
1063 s->intra_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
1065 s->inter_ac_vlc_length =
1066 s->inter_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
1069 #define OFFSET(x) offsetof(MpegEncContext, x)
1070 #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
1071 #define COMMON_OPTS \
1072 { "gop_timecode", "MPEG GOP Timecode in hh:mm:ss[:;.]ff format", \
1073 OFFSET(tc_opt_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, VE },\
1074 { "intra_vlc", "Use MPEG-2 intra VLC table.", \
1075 OFFSET(intra_vlc_format), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \
1076 { "drop_frame_timecode", "Timecode is in drop frame format.", \
1077 OFFSET(drop_frame_timecode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \
1078 { "scan_offset", "Reserve space for SVCD scan offset user data.", \
1079 OFFSET(scan_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1081 static const AVOption mpeg1_options[] = {
1087 static const AVOption mpeg2_options[] = {
1089 { "non_linear_quant", "Use nonlinear quantizer.", OFFSET(q_scale_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1090 { "alternate_scan", "Enable alternate scantable.", OFFSET(alternate_scan), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1095 #define mpeg12_class(x) \
1096 static const AVClass mpeg ## x ## _class = { \
1097 .class_name = "mpeg" # x "video encoder", \
1098 .item_name = av_default_item_name, \
1099 .option = mpeg ## x ## _options, \
1100 .version = LIBAVUTIL_VERSION_INT, \
1106 AVCodec ff_mpeg1video_encoder = {
1107 .name = "mpeg1video",
1108 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
1109 .type = AVMEDIA_TYPE_VIDEO,
1110 .id = AV_CODEC_ID_MPEG1VIDEO,
1111 .priv_data_size = sizeof(MpegEncContext),
1112 .init = encode_init,
1113 .encode2 = ff_MPV_encode_picture,
1114 .close = ff_MPV_encode_end,
1115 .supported_framerates = ff_mpeg12_frame_rate_tab + 1,
1116 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
1118 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
1119 .priv_class = &mpeg1_class,
1122 AVCodec ff_mpeg2video_encoder = {
1123 .name = "mpeg2video",
1124 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
1125 .type = AVMEDIA_TYPE_VIDEO,
1126 .id = AV_CODEC_ID_MPEG2VIDEO,
1127 .priv_data_size = sizeof(MpegEncContext),
1128 .init = encode_init,
1129 .encode2 = ff_MPV_encode_picture,
1130 .close = ff_MPV_encode_end,
1131 .supported_framerates = ff_mpeg2_frame_rate_tab,
1132 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
1135 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
1136 .priv_class = &mpeg2_class,