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 "mpegvideo.h"
33 #include "mpeg12data.h"
34 #include "bytestream.h"
35 #include "libavutil/log.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/avassert.h"
38 #include "libavutil/timecode.h"
40 static const uint8_t inv_non_linear_qscale[13] = {
42 9,10,11,12,13,14,15,16,
45 static const uint8_t svcd_scan_offset_placeholder[14] = {
53 static void mpeg1_encode_block(MpegEncContext *s,
56 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code); // RAL: f_code parameter added
58 static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
59 static uint8_t fcode_tab[MAX_MV*2+1];
61 static uint8_t uni_mpeg1_ac_vlc_len [64*64*2];
62 static uint8_t uni_mpeg2_ac_vlc_len [64*64*2];
64 /* simple include everything table for dc, first byte is bits number next 3 are code*/
65 static uint32_t mpeg1_lum_dc_uni[512];
66 static uint32_t mpeg1_chr_dc_uni[512];
68 static uint8_t mpeg1_index_run[2][64];
69 static int8_t mpeg1_max_level[2][64];
71 static void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){
79 for(run=0; run<64; run++){
82 int alevel= FFABS(level);
84 if (alevel > rl->max_level[0][run])
87 code= rl->index_run[0][run] + alevel - 1;
89 if (code < 111 /* rl->n */) {
90 /* length of vlc and sign */
91 len= rl->table_vlc[code][1]+1;
93 len= rl->table_vlc[111/*rl->n*/][1]+6;
102 uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
108 static int find_frame_rate_index(MpegEncContext *s){
110 AVRational bestq= (AVRational){0, 0};
112 AVRational target = av_inv_q(s->avctx->time_base);
115 if(s->avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL && i>=9) break;
117 for (ext.num=1; ext.num <= 4; ext.num++) {
118 for (ext.den=1; ext.den <= 32; ext.den++) {
119 AVRational q = av_mul_q(ext, ff_mpeg12_frame_rate_tab[i]);
121 if(s->codec_id != AV_CODEC_ID_MPEG2VIDEO && (ext.den!=1 || ext.num!=1))
123 if(av_gcd(ext.den, ext.num) != 1)
127 || av_nearer_q(target, bestq, q) < 0
128 || ext.num==1 && ext.den==1 && av_nearer_q(target, bestq, q) == 0){
130 s->frame_rate_index= i;
131 s->mpeg2_frame_rate_ext.num = ext.num;
132 s->mpeg2_frame_rate_ext.den = ext.den;
137 if(av_cmp_q(target, bestq))
143 static av_cold int encode_init(AVCodecContext *avctx)
145 MpegEncContext *s = avctx->priv_data;
147 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && avctx->height > 2800)
148 avctx->thread_count = 1;
150 if(ff_MPV_encode_init(avctx) < 0)
153 if(find_frame_rate_index(s) < 0){
154 if(s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
155 av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num);
158 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);
162 if(avctx->profile == FF_PROFILE_UNKNOWN){
163 if(avctx->level != FF_LEVEL_UNKNOWN){
164 av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
167 avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0; /* Main or 4:2:2 */
170 if(avctx->level == FF_LEVEL_UNKNOWN){
171 if(avctx->profile == 0){ /* 4:2:2 */
172 if(avctx->width <= 720 && avctx->height <= 608) avctx->level = 5; /* Main */
173 else avctx->level = 2; /* High */
175 if(avctx->profile != 1 && s->chroma_format != CHROMA_420){
176 av_log(avctx, AV_LOG_ERROR, "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
179 if(avctx->width <= 720 && avctx->height <= 576) avctx->level = 8; /* Main */
180 else if(avctx->width <= 1440) avctx->level = 6; /* High 1440 */
181 else avctx->level = 4; /* High */
185 if ((avctx->width & 0xFFF) == 0 && (avctx->height & 0xFFF) == 1) {
186 av_log(avctx, AV_LOG_ERROR, "Width / Height is invalid for MPEG2\n");
187 return AVERROR(EINVAL);
190 if (s->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
191 if ((avctx->width & 0xFFF) == 0 || (avctx->height & 0xFFF) == 0) {
192 av_log(avctx, AV_LOG_ERROR, "Width or Height are not allowed to be multiplies of 4096\n"
193 "add '-strict %d' if you want to use them anyway.\n", FF_COMPLIANCE_UNOFFICIAL);
194 return AVERROR(EINVAL);
198 s->drop_frame_timecode = s->drop_frame_timecode || !!(avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE);
199 if (s->drop_frame_timecode)
200 s->tc.flags |= AV_TIMECODE_FLAG_DROPFRAME;
201 if (s->drop_frame_timecode && s->frame_rate_index != 4) {
202 av_log(avctx, AV_LOG_ERROR, "Drop frame time code only allowed with 1001/30000 fps\n");
207 AVRational rate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
208 int ret = av_timecode_init_from_string(&s->tc, rate, s->tc_opt_str, s);
211 s->drop_frame_timecode = !!(s->tc.flags & AV_TIMECODE_FLAG_DROPFRAME);
212 s->avctx->timecode_frame_start = s->tc.start;
214 s->avctx->timecode_frame_start = 0; // default is -1
219 static void put_header(MpegEncContext *s, int header)
221 avpriv_align_put_bits(&s->pb);
222 put_bits(&s->pb, 16, header>>16);
223 put_sbits(&s->pb, 16, header);
226 /* put sequence header if needed */
227 static void mpeg1_encode_sequence_header(MpegEncContext *s)
229 unsigned int vbv_buffer_size;
233 float best_aspect_error= 1E10;
234 float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
235 int constraint_parameter_flag;
237 if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA)
239 if (s->current_picture.f.key_frame) {
240 AVRational framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
242 /* mpeg1 header repeated every gop */
243 put_header(s, SEQ_START_CODE);
245 put_sbits(&s->pb, 12, s->width & 0xFFF);
246 put_sbits(&s->pb, 12, s->height & 0xFFF);
249 float error= aspect_ratio;
250 if(s->codec_id == AV_CODEC_ID_MPEG1VIDEO || i <=1)
251 error-= 1.0/ff_mpeg1_aspect[i];
253 error-= av_q2d(ff_mpeg2_aspect[i])*s->height/s->width;
257 if(error < best_aspect_error){
258 best_aspect_error= error;
259 s->aspect_ratio_info= i;
263 put_bits(&s->pb, 4, s->aspect_ratio_info);
264 put_bits(&s->pb, 4, s->frame_rate_index);
266 if(s->avctx->rc_max_rate){
267 v = (s->avctx->rc_max_rate + 399) / 400;
268 if (v > 0x3ffff && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
274 if(s->avctx->rc_buffer_size)
275 vbv_buffer_size = s->avctx->rc_buffer_size;
277 /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
278 vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
279 vbv_buffer_size= (vbv_buffer_size + 16383) / 16384;
281 put_sbits(&s->pb, 18, v);
282 put_bits(&s->pb, 1, 1); /* marker */
283 put_sbits(&s->pb, 10, vbv_buffer_size);
285 constraint_parameter_flag=
286 s->width <= 768 && s->height <= 576 &&
287 s->mb_width * s->mb_height <= 396 &&
288 s->mb_width * s->mb_height * framerate.num <= framerate.den*396*25 &&
289 framerate.num <= framerate.den*30 &&
290 s->avctx->me_range && s->avctx->me_range < 128 &&
291 vbv_buffer_size <= 20 &&
293 s->codec_id == AV_CODEC_ID_MPEG1VIDEO;
295 put_bits(&s->pb, 1, constraint_parameter_flag);
297 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
298 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
300 if(s->codec_id == AV_CODEC_ID_MPEG2VIDEO){
301 put_header(s, EXT_START_CODE);
302 put_bits(&s->pb, 4, 1); //seq ext
304 put_bits(&s->pb, 1, s->avctx->profile == 0); //escx 1 for 4:2:2 profile */
306 put_bits(&s->pb, 3, s->avctx->profile); //profile
307 put_bits(&s->pb, 4, s->avctx->level); //level
309 put_bits(&s->pb, 1, s->progressive_sequence);
310 put_bits(&s->pb, 2, s->chroma_format);
311 put_bits(&s->pb, 2, s->width >>12);
312 put_bits(&s->pb, 2, s->height>>12);
313 put_bits(&s->pb, 12, v>>18); //bitrate ext
314 put_bits(&s->pb, 1, 1); //marker
315 put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext
316 put_bits(&s->pb, 1, s->low_delay);
317 put_bits(&s->pb, 2, s->mpeg2_frame_rate_ext.num-1); // frame_rate_ext_n
318 put_bits(&s->pb, 5, s->mpeg2_frame_rate_ext.den-1); // frame_rate_ext_d
321 put_header(s, GOP_START_CODE);
322 put_bits(&s->pb, 1, s->drop_frame_timecode); /* drop frame flag */
323 /* time code : we must convert from the real frame rate to a
324 fake mpeg frame rate in case of low frame rate */
325 fps = (framerate.num + framerate.den/2)/ framerate.den;
326 time_code = s->current_picture_ptr->f.coded_picture_number + s->avctx->timecode_frame_start;
328 s->gop_picture_number = s->current_picture_ptr->f.coded_picture_number;
329 av_assert0(s->drop_frame_timecode == !!(s->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
330 if (s->drop_frame_timecode)
331 time_code = av_timecode_adjust_ntsc_framenum2(time_code, fps);
332 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
333 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
334 put_bits(&s->pb, 1, 1);
335 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
336 put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
337 put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
338 put_bits(&s->pb, 1, 0); /* broken link */
342 static inline void encode_mb_skip_run(MpegEncContext *s, int run){
344 put_bits(&s->pb, 11, 0x008);
347 put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1],
348 ff_mpeg12_mbAddrIncrTable[run][0]);
351 static av_always_inline void put_qscale(MpegEncContext *s)
354 av_assert2(s->qscale>=1 && s->qscale <=12);
355 put_bits(&s->pb, 5, inv_non_linear_qscale[s->qscale]);
357 put_bits(&s->pb, 5, s->qscale);
361 void ff_mpeg1_encode_slice_header(MpegEncContext *s){
362 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->height > 2800) {
363 put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127));
364 put_bits(&s->pb, 3, s->mb_y >> 7); /* slice_vertical_position_extension */
366 put_header(s, SLICE_MIN_START_CODE + s->mb_y);
369 put_bits(&s->pb, 1, 0); /* slice extra information */
372 void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
374 mpeg1_encode_sequence_header(s);
376 /* mpeg1 picture header */
377 put_header(s, PICTURE_START_CODE);
378 /* temporal reference */
380 // RAL: s->picture_number instead of s->fake_picture_number
381 put_bits(&s->pb, 10, (s->picture_number -
382 s->gop_picture_number) & 0x3ff);
383 put_bits(&s->pb, 3, s->pict_type);
385 s->vbv_delay_ptr= s->pb.buf + put_bits_count(&s->pb)/8;
386 put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
388 // RAL: Forward f_code also needed for B frames
389 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
390 put_bits(&s->pb, 1, 0); /* half pel coordinates */
391 if(s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
392 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
394 put_bits(&s->pb, 3, 7); /* forward_f_code */
397 // RAL: Backward f_code necessary for B frames
398 if (s->pict_type == AV_PICTURE_TYPE_B) {
399 put_bits(&s->pb, 1, 0); /* half pel coordinates */
400 if(s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
401 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
403 put_bits(&s->pb, 3, 7); /* backward_f_code */
406 put_bits(&s->pb, 1, 0); /* extra bit picture */
408 s->frame_pred_frame_dct = 1;
409 if(s->codec_id == AV_CODEC_ID_MPEG2VIDEO){
410 put_header(s, EXT_START_CODE);
411 put_bits(&s->pb, 4, 8); //pic ext
412 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
413 put_bits(&s->pb, 4, s->f_code);
414 put_bits(&s->pb, 4, s->f_code);
416 put_bits(&s->pb, 8, 255);
418 if (s->pict_type == AV_PICTURE_TYPE_B) {
419 put_bits(&s->pb, 4, s->b_code);
420 put_bits(&s->pb, 4, s->b_code);
422 put_bits(&s->pb, 8, 255);
424 put_bits(&s->pb, 2, s->intra_dc_precision);
426 av_assert0(s->picture_structure == PICT_FRAME);
427 put_bits(&s->pb, 2, s->picture_structure);
428 if (s->progressive_sequence) {
429 put_bits(&s->pb, 1, 0); /* no repeat */
431 put_bits(&s->pb, 1, s->current_picture_ptr->f.top_field_first);
433 /* XXX: optimize the generation of this flag with entropy
435 s->frame_pred_frame_dct = s->progressive_sequence;
437 put_bits(&s->pb, 1, s->frame_pred_frame_dct);
438 put_bits(&s->pb, 1, s->concealment_motion_vectors);
439 put_bits(&s->pb, 1, s->q_scale_type);
440 put_bits(&s->pb, 1, s->intra_vlc_format);
441 put_bits(&s->pb, 1, s->alternate_scan);
442 put_bits(&s->pb, 1, s->repeat_first_field);
443 s->progressive_frame = s->progressive_sequence;
444 put_bits(&s->pb, 1, s->chroma_format == CHROMA_420 ? s->progressive_frame : 0); /* chroma_420_type */
445 put_bits(&s->pb, 1, s->progressive_frame);
446 put_bits(&s->pb, 1, 0); //composite_display_flag
448 if (s->scan_offset) {
451 put_header(s, USER_START_CODE);
452 for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){
453 put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
458 ff_mpeg1_encode_slice_header(s);
461 static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
462 int has_mv, int field_motion)
464 put_bits(&s->pb, n, bits);
465 if (!s->frame_pred_frame_dct) {
467 put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */
468 put_bits(&s->pb, 1, s->interlaced_dct);
472 static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
473 int16_t block[6][64],
474 int motion_x, int motion_y,
478 const int mb_x = s->mb_x;
479 const int mb_y = s->mb_y;
480 const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
484 for(i=0;i<mb_block_count;i++) {
485 if (s->block_last_index[i] >= 0)
486 cbp |= 1 << (mb_block_count - 1 - i);
489 if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
490 (mb_x != s->mb_width - 1 || (mb_y != s->end_mb_y - 1 && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)) &&
491 ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) ||
492 (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) |
493 ((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))) {
495 s->qscale -= s->dquant;
499 if(s->pict_type == AV_PICTURE_TYPE_P){
500 s->last_mv[0][1][0]= s->last_mv[0][0][0]=
501 s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0;
505 av_assert0(s->mb_skip_run == 0);
506 encode_mb_skip_run(s, s->mb_x);
508 encode_mb_skip_run(s, s->mb_skip_run);
511 if (s->pict_type == AV_PICTURE_TYPE_I) {
512 if(s->dquant && cbp){
513 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_type : macroblock_quant = 1 */
516 put_mb_modes(s, 1, 1, 0, 0); /* macroblock_type : macroblock_quant = 0 */
517 s->qscale -= s->dquant;
519 s->misc_bits+= get_bits_diff(s);
521 } else if (s->mb_intra) {
522 if(s->dquant && cbp){
523 put_mb_modes(s, 6, 0x01, 0, 0);
526 put_mb_modes(s, 5, 0x03, 0, 0);
527 s->qscale -= s->dquant;
529 s->misc_bits+= get_bits_diff(s);
531 memset(s->last_mv, 0, sizeof(s->last_mv));
532 } else if (s->pict_type == AV_PICTURE_TYPE_P) {
533 if(s->mv_type == MV_TYPE_16X16){
535 if ((motion_x|motion_y) == 0) {
537 put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */
540 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */
542 s->misc_bits+= get_bits_diff(s);
545 put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
548 put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
550 s->misc_bits+= get_bits_diff(s);
551 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
552 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
553 s->mv_bits+= get_bits_diff(s);
556 put_bits(&s->pb, 3, 1); /* motion only */
557 if (!s->frame_pred_frame_dct)
558 put_bits(&s->pb, 2, 2); /* motion_type: frame */
559 s->misc_bits+= get_bits_diff(s);
560 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
561 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
562 s->qscale -= s->dquant;
563 s->mv_bits+= get_bits_diff(s);
565 s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x;
566 s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y;
568 av_assert2(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
572 put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
575 put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
578 put_bits(&s->pb, 3, 1); /* motion only */
579 put_bits(&s->pb, 2, 1); /* motion_type: field */
580 s->qscale -= s->dquant;
582 s->misc_bits+= get_bits_diff(s);
584 put_bits(&s->pb, 1, s->field_select[0][i]);
585 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
586 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
587 s->last_mv[0][i][0]= s->mv[0][i][0];
588 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
590 s->mv_bits+= get_bits_diff(s);
593 if (s->chroma_y_shift) {
594 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
596 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
597 put_sbits(&s->pb, 2, cbp);
602 if(s->mv_type == MV_TYPE_16X16){
603 if (cbp){ // With coded bloc pattern
605 if(s->mv_dir == MV_DIR_FORWARD)
606 put_mb_modes(s, 6, 3, 1, 0);
608 put_mb_modes(s, 8-s->mv_dir, 2, 1, 0);
611 put_mb_modes(s, 5-s->mv_dir, 3, 1, 0);
613 }else{ // No coded bloc pattern
614 put_bits(&s->pb, 5-s->mv_dir, 2);
615 if (!s->frame_pred_frame_dct)
616 put_bits(&s->pb, 2, 2); /* motion_type: frame */
617 s->qscale -= s->dquant;
619 s->misc_bits += get_bits_diff(s);
620 if (s->mv_dir&MV_DIR_FORWARD){
621 mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
622 mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
623 s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0];
624 s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1];
627 if (s->mv_dir&MV_DIR_BACKWARD){
628 mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
629 mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
630 s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0];
631 s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1];
635 av_assert2(s->mv_type == MV_TYPE_FIELD);
636 av_assert2(!s->frame_pred_frame_dct);
637 if (cbp){ // With coded bloc pattern
639 if(s->mv_dir == MV_DIR_FORWARD)
640 put_mb_modes(s, 6, 3, 1, 1);
642 put_mb_modes(s, 8-s->mv_dir, 2, 1, 1);
645 put_mb_modes(s, 5-s->mv_dir, 3, 1, 1);
647 }else{ // No coded bloc pattern
648 put_bits(&s->pb, 5-s->mv_dir, 2);
649 put_bits(&s->pb, 2, 1); /* motion_type: field */
650 s->qscale -= s->dquant;
652 s->misc_bits += get_bits_diff(s);
653 if (s->mv_dir&MV_DIR_FORWARD){
655 put_bits(&s->pb, 1, s->field_select[0][i]);
656 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
657 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
658 s->last_mv[0][i][0]= s->mv[0][i][0];
659 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
663 if (s->mv_dir&MV_DIR_BACKWARD){
665 put_bits(&s->pb, 1, s->field_select[1][i]);
666 mpeg1_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code);
667 mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code);
668 s->last_mv[1][i][0]= s->mv[1][i][0];
669 s->last_mv[1][i][1]= 2*s->mv[1][i][1];
674 s->mv_bits += get_bits_diff(s);
676 if (s->chroma_y_shift) {
677 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
679 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
680 put_sbits(&s->pb, 2, cbp);
684 for(i=0;i<mb_block_count;i++) {
685 if (cbp & (1 << (mb_block_count - 1 - i))) {
686 mpeg1_encode_block(s, block[i], i);
691 s->i_tex_bits+= get_bits_diff(s);
693 s->p_tex_bits+= get_bits_diff(s);
697 void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
699 if (s->chroma_format == CHROMA_420) mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
700 else mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
703 // RAL: Parameter added: f_or_b_code
704 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
709 ff_mpeg12_mbMotionVectorTable[0][1],
710 ff_mpeg12_mbMotionVectorTable[0][0]);
712 int code, sign, bits;
713 int bit_size = f_or_b_code - 1;
714 int range = 1 << bit_size;
715 /* modulo encoding */
716 val = sign_extend(val, 5 + bit_size);
720 code = (val >> bit_size) + 1;
721 bits = val & (range - 1);
726 code = (val >> bit_size) + 1;
727 bits = val & (range - 1);
731 av_assert2(code > 0 && code <= 16);
734 ff_mpeg12_mbMotionVectorTable[code][1],
735 ff_mpeg12_mbMotionVectorTable[code][0]);
737 put_bits(&s->pb, 1, sign);
739 put_bits(&s->pb, bit_size, bits);
744 void ff_mpeg1_encode_init(MpegEncContext *s)
748 ff_mpeg12_common_init(s);
756 ff_init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
757 ff_init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
761 mpeg1_max_level[0][i]= ff_rl_mpeg1.max_level[0][i];
762 mpeg1_index_run[0][i]= ff_rl_mpeg1.index_run[0][i];
765 init_uni_ac_vlc(&ff_rl_mpeg1, uni_mpeg1_ac_vlc_len);
766 if(s->intra_vlc_format)
767 init_uni_ac_vlc(&ff_rl_mpeg2, uni_mpeg2_ac_vlc_len);
769 /* build unified dc encoding tables */
770 for(i=-255; i<256; i++)
778 index = av_log2(2*adiff);
780 bits= ff_mpeg12_vlc_dc_lum_bits[index] + index;
781 code= (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
782 mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
784 bits= ff_mpeg12_vlc_dc_chroma_bits[index] + index;
785 code= (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
786 mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
789 for(f_code=1; f_code<=MAX_FCODE; f_code++){
790 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
793 if(mv==0) len= ff_mpeg12_mbMotionVectorTable[0][1];
795 int val, bit_size, code;
797 bit_size = f_code - 1;
803 code = (val >> bit_size) + 1;
805 len= ff_mpeg12_mbMotionVectorTable[code][1] + 1 + bit_size;
807 len= ff_mpeg12_mbMotionVectorTable[16][1] + 2 + bit_size;
811 mv_penalty[f_code][mv+MAX_MV]= len;
816 for(f_code=MAX_FCODE; f_code>0; f_code--){
817 for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
818 fcode_tab[mv+MAX_MV]= f_code;
822 s->me.mv_penalty= mv_penalty;
823 s->fcode_tab= fcode_tab;
824 if(s->codec_id == AV_CODEC_ID_MPEG1VIDEO){
831 if (s->intra_vlc_format) {
832 s->intra_ac_vlc_length=
833 s->intra_ac_vlc_last_length= uni_mpeg2_ac_vlc_len;
835 s->intra_ac_vlc_length=
836 s->intra_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
838 s->inter_ac_vlc_length=
839 s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
842 static inline void encode_dc(MpegEncContext *s, int diff, int component)
844 if(((unsigned) (diff+255)) >= 511){
848 index= av_log2_16bit(-2*diff);
851 index= av_log2_16bit(2*diff);
853 if (component == 0) {
856 ff_mpeg12_vlc_dc_lum_bits[index] + index,
857 (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)));
861 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
862 (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)));
865 if (component == 0) {
868 mpeg1_lum_dc_uni[diff+255]&0xFF,
869 mpeg1_lum_dc_uni[diff+255]>>8);
873 mpeg1_chr_dc_uni[diff+255]&0xFF,
874 mpeg1_chr_dc_uni[diff+255]>>8);
879 static void mpeg1_encode_block(MpegEncContext *s,
883 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
885 const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
887 last_index = s->block_last_index[n];
891 component = (n <= 3 ? 0 : (n&1) + 1);
892 dc = block[0]; /* overflow is impossible */
893 diff = dc - s->last_dc[component];
894 encode_dc(s, diff, component);
895 s->last_dc[component] = dc;
897 if (s->intra_vlc_format)
898 table_vlc = ff_rl_mpeg2.table_vlc;
900 /* encode the first coefficient : needs to be done here because
901 it is handled slightly differently */
903 if (abs(level) == 1) {
904 code = ((uint32_t)level >> 31); /* the sign bit */
905 put_bits(&s->pb, 2, code | 0x02);
914 /* now quantify & encode AC coefs */
915 last_non_zero = i - 1;
917 for(;i<=last_index;i++) {
918 j = s->intra_scantable.permutated[i];
921 /* encode using VLC */
923 run = i - last_non_zero - 1;
926 MASK_ABS(sign, alevel);
929 if (alevel <= mpeg1_max_level[0][run]){
930 code= mpeg1_index_run[0][run] + alevel - 1;
931 /* store the vlc & sign at once */
932 put_bits(&s->pb, table_vlc[code][1]+1, (table_vlc[code][0]<<1) + sign);
934 /* escape seems to be pretty rare <5% so I do not optimize it */
935 put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
936 /* escape: only clip in this case */
937 put_bits(&s->pb, 6, run);
938 if(s->codec_id == AV_CODEC_ID_MPEG1VIDEO){
940 put_sbits(&s->pb, 8, level);
943 put_bits(&s->pb, 16, 0x8001 + level + 255);
945 put_sbits(&s->pb, 16, level);
949 put_sbits(&s->pb, 12, level);
956 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
959 #define OFFSET(x) offsetof(MpegEncContext, x)
960 #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
962 { "gop_timecode", "MPEG GOP Timecode in hh:mm:ss[:;.]ff format", OFFSET(tc_opt_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, VE },\
963 { "intra_vlc", "Use MPEG-2 intra VLC table.", OFFSET(intra_vlc_format), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },\
964 { "drop_frame_timecode", "Timecode is in drop frame format.", OFFSET(drop_frame_timecode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE}, \
965 { "scan_offset", "Reserve space for SVCD scan offset user data.", OFFSET(scan_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
967 static const AVOption mpeg1_options[] = {
973 static const AVOption mpeg2_options[] = {
975 { "non_linear_quant", "Use nonlinear quantizer.", OFFSET(q_scale_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
976 { "alternate_scan", "Enable alternate scantable.", OFFSET(alternate_scan), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
981 #define mpeg12_class(x)\
982 static const AVClass mpeg## x ##_class = {\
983 .class_name = "mpeg" #x "video encoder",\
984 .item_name = av_default_item_name,\
985 .option = mpeg## x ##_options,\
986 .version = LIBAVUTIL_VERSION_INT,\
992 AVCodec ff_mpeg1video_encoder = {
993 .name = "mpeg1video",
994 .type = AVMEDIA_TYPE_VIDEO,
995 .id = AV_CODEC_ID_MPEG1VIDEO,
996 .priv_data_size = sizeof(MpegEncContext),
998 .encode2 = ff_MPV_encode_picture,
999 .close = ff_MPV_encode_end,
1000 .supported_framerates = ff_mpeg12_frame_rate_tab + 1,
1001 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P,
1003 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
1004 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
1005 .priv_class = &mpeg1_class,
1008 AVCodec ff_mpeg2video_encoder = {
1009 .name = "mpeg2video",
1010 .type = AVMEDIA_TYPE_VIDEO,
1011 .id = AV_CODEC_ID_MPEG2VIDEO,
1012 .priv_data_size = sizeof(MpegEncContext),
1013 .init = encode_init,
1014 .encode2 = ff_MPV_encode_picture,
1015 .close = ff_MPV_encode_end,
1016 .supported_framerates = ff_mpeg2_frame_rate_tab,
1017 .pix_fmts = (const enum AVPixelFormat[]){
1018 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE
1020 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
1021 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
1022 .priv_class = &mpeg2_class,