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
31 #include "mpegvideo.h"
34 #include "mpeg12data.h"
35 #include "bytestream.h"
36 #include "libavutil/log.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/avassert.h"
39 #include "libavutil/timecode.h"
41 static const uint8_t inv_non_linear_qscale[13] = {
43 9,10,11,12,13,14,15,16,
46 static const uint8_t svcd_scan_offset_placeholder[14] = {
54 static void mpeg1_encode_block(MpegEncContext *s,
57 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code); // RAL: f_code parameter added
59 static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
60 static uint8_t fcode_tab[MAX_MV*2+1];
62 static uint8_t uni_mpeg1_ac_vlc_len [64*64*2];
63 static uint8_t uni_mpeg2_ac_vlc_len [64*64*2];
65 /* simple include everything table for dc, first byte is bits number next 3 are code*/
66 static uint32_t mpeg1_lum_dc_uni[512];
67 static uint32_t mpeg1_chr_dc_uni[512];
69 static uint8_t mpeg1_index_run[2][64];
70 static int8_t mpeg1_max_level[2][64];
72 static void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){
80 for(run=0; run<64; run++){
83 int alevel= FFABS(level);
85 if (alevel > rl->max_level[0][run])
88 code= rl->index_run[0][run] + alevel - 1;
90 if (code < 111 /* rl->n */) {
91 /* length of vlc and sign */
92 len= rl->table_vlc[code][1]+1;
94 len= rl->table_vlc[111/*rl->n*/][1]+6;
103 uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
109 static int find_frame_rate_index(MpegEncContext *s){
111 AVRational bestq= (AVRational){0, 0};
113 AVRational target = av_inv_q(s->avctx->time_base);
116 if(s->avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL && i>=9) break;
118 for (ext.num=1; ext.num <= 4; ext.num++) {
119 for (ext.den=1; ext.den <= 32; ext.den++) {
120 AVRational q = av_mul_q(ext, ff_mpeg12_frame_rate_tab[i]);
122 if(s->codec_id != AV_CODEC_ID_MPEG2VIDEO && (ext.den!=1 || ext.num!=1))
124 if(av_gcd(ext.den, ext.num) != 1)
128 || av_nearer_q(target, bestq, q) < 0
129 || ext.num==1 && ext.den==1 && av_nearer_q(target, bestq, q) == 0){
131 s->frame_rate_index= i;
132 s->mpeg2_frame_rate_ext.num = ext.num;
133 s->mpeg2_frame_rate_ext.den = ext.den;
138 if(av_cmp_q(target, bestq))
144 static av_cold int encode_init(AVCodecContext *avctx)
146 MpegEncContext *s = avctx->priv_data;
148 if(ff_MPV_encode_init(avctx) < 0)
151 if(find_frame_rate_index(s) < 0){
152 if(s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
153 av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num);
156 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);
160 if(avctx->profile == FF_PROFILE_UNKNOWN){
161 if(avctx->level != FF_LEVEL_UNKNOWN){
162 av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
165 avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0; /* Main or 4:2:2 */
168 if(avctx->level == FF_LEVEL_UNKNOWN){
169 if(avctx->profile == 0){ /* 4:2:2 */
170 if(avctx->width <= 720 && avctx->height <= 608) avctx->level = 5; /* Main */
171 else avctx->level = 2; /* High */
173 if(avctx->profile != 1 && s->chroma_format != CHROMA_420){
174 av_log(avctx, AV_LOG_ERROR, "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
177 if(avctx->width <= 720 && avctx->height <= 576) avctx->level = 8; /* Main */
178 else if(avctx->width <= 1440) avctx->level = 6; /* High 1440 */
179 else avctx->level = 4; /* High */
183 if ((avctx->width & 0xFFF) == 0 && (avctx->height & 0xFFF) == 1) {
184 av_log(avctx, AV_LOG_ERROR, "Width / Height is invalid for MPEG2\n");
185 return AVERROR(EINVAL);
188 if (s->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
189 if ((avctx->width & 0xFFF) == 0 || (avctx->height & 0xFFF) == 0) {
190 av_log(avctx, AV_LOG_ERROR, "Width or Height are not allowed to be multiplies of 4096\n"
191 "add '-strict %d' if you want to use them anyway.\n", FF_COMPLIANCE_UNOFFICIAL);
192 return AVERROR(EINVAL);
196 s->drop_frame_timecode = s->drop_frame_timecode || !!(avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE);
197 if (s->drop_frame_timecode)
198 s->tc.flags |= AV_TIMECODE_FLAG_DROPFRAME;
199 if (s->drop_frame_timecode && s->frame_rate_index != 4) {
200 av_log(avctx, AV_LOG_ERROR, "Drop frame time code only allowed with 1001/30000 fps\n");
205 AVRational rate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
206 int ret = av_timecode_init_from_string(&s->tc, rate, s->tc_opt_str, s);
209 s->drop_frame_timecode = !!(s->tc.flags & AV_TIMECODE_FLAG_DROPFRAME);
210 s->avctx->timecode_frame_start = s->tc.start;
212 s->avctx->timecode_frame_start = 0; // default is -1
217 static void put_header(MpegEncContext *s, int header)
219 avpriv_align_put_bits(&s->pb);
220 put_bits(&s->pb, 16, header>>16);
221 put_sbits(&s->pb, 16, header);
224 /* put sequence header if needed */
225 static void mpeg1_encode_sequence_header(MpegEncContext *s)
227 unsigned int vbv_buffer_size;
231 float best_aspect_error= 1E10;
232 float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
233 int constraint_parameter_flag;
235 if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA)
237 if (s->current_picture.f.key_frame) {
238 AVRational framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
240 /* mpeg1 header repeated every gop */
241 put_header(s, SEQ_START_CODE);
243 put_sbits(&s->pb, 12, s->width & 0xFFF);
244 put_sbits(&s->pb, 12, s->height & 0xFFF);
247 float error= aspect_ratio;
248 if(s->codec_id == AV_CODEC_ID_MPEG1VIDEO || i <=1)
249 error-= 1.0/ff_mpeg1_aspect[i];
251 error-= av_q2d(ff_mpeg2_aspect[i])*s->height/s->width;
255 if(error < best_aspect_error){
256 best_aspect_error= error;
257 s->aspect_ratio_info= i;
261 put_bits(&s->pb, 4, s->aspect_ratio_info);
262 put_bits(&s->pb, 4, s->frame_rate_index);
264 if(s->avctx->rc_max_rate){
265 v = (s->avctx->rc_max_rate + 399) / 400;
266 if (v > 0x3ffff && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
272 if(s->avctx->rc_buffer_size)
273 vbv_buffer_size = s->avctx->rc_buffer_size;
275 /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
276 vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
277 vbv_buffer_size= (vbv_buffer_size + 16383) / 16384;
279 put_sbits(&s->pb, 18, v);
280 put_bits(&s->pb, 1, 1); /* marker */
281 put_sbits(&s->pb, 10, vbv_buffer_size);
283 constraint_parameter_flag=
284 s->width <= 768 && s->height <= 576 &&
285 s->mb_width * s->mb_height <= 396 &&
286 s->mb_width * s->mb_height * framerate.num <= framerate.den*396*25 &&
287 framerate.num <= framerate.den*30 &&
288 s->avctx->me_range && s->avctx->me_range < 128 &&
289 vbv_buffer_size <= 20 &&
291 s->codec_id == AV_CODEC_ID_MPEG1VIDEO;
293 put_bits(&s->pb, 1, constraint_parameter_flag);
295 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
296 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
298 if(s->codec_id == AV_CODEC_ID_MPEG2VIDEO){
299 put_header(s, EXT_START_CODE);
300 put_bits(&s->pb, 4, 1); //seq ext
302 put_bits(&s->pb, 1, s->avctx->profile == 0); //escx 1 for 4:2:2 profile */
304 put_bits(&s->pb, 3, s->avctx->profile); //profile
305 put_bits(&s->pb, 4, s->avctx->level); //level
307 put_bits(&s->pb, 1, s->progressive_sequence);
308 put_bits(&s->pb, 2, s->chroma_format);
309 put_bits(&s->pb, 2, s->width >>12);
310 put_bits(&s->pb, 2, s->height>>12);
311 put_bits(&s->pb, 12, v>>18); //bitrate ext
312 put_bits(&s->pb, 1, 1); //marker
313 put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext
314 put_bits(&s->pb, 1, s->low_delay);
315 put_bits(&s->pb, 2, s->mpeg2_frame_rate_ext.num-1); // frame_rate_ext_n
316 put_bits(&s->pb, 5, s->mpeg2_frame_rate_ext.den-1); // frame_rate_ext_d
319 put_header(s, GOP_START_CODE);
320 put_bits(&s->pb, 1, s->drop_frame_timecode); /* drop frame flag */
321 /* time code : we must convert from the real frame rate to a
322 fake mpeg frame rate in case of low frame rate */
323 fps = (framerate.num + framerate.den/2)/ framerate.den;
324 time_code = s->current_picture_ptr->f.coded_picture_number + s->avctx->timecode_frame_start;
326 s->gop_picture_number = s->current_picture_ptr->f.coded_picture_number;
327 av_assert0(s->drop_frame_timecode == !!(s->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
328 if (s->drop_frame_timecode)
329 time_code = av_timecode_adjust_ntsc_framenum2(time_code, fps);
330 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
331 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
332 put_bits(&s->pb, 1, 1);
333 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
334 put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
335 put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
336 put_bits(&s->pb, 1, 0); /* broken link */
340 static inline void encode_mb_skip_run(MpegEncContext *s, int run){
342 put_bits(&s->pb, 11, 0x008);
345 put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1],
346 ff_mpeg12_mbAddrIncrTable[run][0]);
349 static av_always_inline void put_qscale(MpegEncContext *s)
352 av_assert2(s->qscale>=1 && s->qscale <=12);
353 put_bits(&s->pb, 5, inv_non_linear_qscale[s->qscale]);
355 put_bits(&s->pb, 5, s->qscale);
359 void ff_mpeg1_encode_slice_header(MpegEncContext *s){
360 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->height > 2800) {
361 put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127));
362 put_bits(&s->pb, 3, s->mb_y >> 7); /* slice_vertical_position_extension */
364 put_header(s, SLICE_MIN_START_CODE + s->mb_y);
367 put_bits(&s->pb, 1, 0); /* slice extra information */
370 void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
372 mpeg1_encode_sequence_header(s);
374 /* mpeg1 picture header */
375 put_header(s, PICTURE_START_CODE);
376 /* temporal reference */
378 // RAL: s->picture_number instead of s->fake_picture_number
379 put_bits(&s->pb, 10, (s->picture_number -
380 s->gop_picture_number) & 0x3ff);
381 put_bits(&s->pb, 3, s->pict_type);
383 s->vbv_delay_ptr= s->pb.buf + put_bits_count(&s->pb)/8;
384 put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
386 // RAL: Forward f_code also needed for B frames
387 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
388 put_bits(&s->pb, 1, 0); /* half pel coordinates */
389 if(s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
390 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
392 put_bits(&s->pb, 3, 7); /* forward_f_code */
395 // RAL: Backward f_code necessary for B frames
396 if (s->pict_type == AV_PICTURE_TYPE_B) {
397 put_bits(&s->pb, 1, 0); /* half pel coordinates */
398 if(s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
399 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
401 put_bits(&s->pb, 3, 7); /* backward_f_code */
404 put_bits(&s->pb, 1, 0); /* extra bit picture */
406 s->frame_pred_frame_dct = 1;
407 if(s->codec_id == AV_CODEC_ID_MPEG2VIDEO){
408 put_header(s, EXT_START_CODE);
409 put_bits(&s->pb, 4, 8); //pic ext
410 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
411 put_bits(&s->pb, 4, s->f_code);
412 put_bits(&s->pb, 4, s->f_code);
414 put_bits(&s->pb, 8, 255);
416 if (s->pict_type == AV_PICTURE_TYPE_B) {
417 put_bits(&s->pb, 4, s->b_code);
418 put_bits(&s->pb, 4, s->b_code);
420 put_bits(&s->pb, 8, 255);
422 put_bits(&s->pb, 2, s->intra_dc_precision);
424 av_assert0(s->picture_structure == PICT_FRAME);
425 put_bits(&s->pb, 2, s->picture_structure);
426 if (s->progressive_sequence) {
427 put_bits(&s->pb, 1, 0); /* no repeat */
429 put_bits(&s->pb, 1, s->current_picture_ptr->f.top_field_first);
431 /* XXX: optimize the generation of this flag with entropy
433 s->frame_pred_frame_dct = s->progressive_sequence;
435 put_bits(&s->pb, 1, s->frame_pred_frame_dct);
436 put_bits(&s->pb, 1, s->concealment_motion_vectors);
437 put_bits(&s->pb, 1, s->q_scale_type);
438 put_bits(&s->pb, 1, s->intra_vlc_format);
439 put_bits(&s->pb, 1, s->alternate_scan);
440 put_bits(&s->pb, 1, s->repeat_first_field);
441 s->progressive_frame = s->progressive_sequence;
442 put_bits(&s->pb, 1, s->chroma_format == CHROMA_420 ? s->progressive_frame : 0); /* chroma_420_type */
443 put_bits(&s->pb, 1, s->progressive_frame);
444 put_bits(&s->pb, 1, 0); //composite_display_flag
446 if (s->scan_offset) {
449 put_header(s, USER_START_CODE);
450 for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){
451 put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
456 ff_mpeg1_encode_slice_header(s);
459 static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
460 int has_mv, int field_motion)
462 put_bits(&s->pb, n, bits);
463 if (!s->frame_pred_frame_dct) {
465 put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */
466 put_bits(&s->pb, 1, s->interlaced_dct);
470 static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
471 DCTELEM block[6][64],
472 int motion_x, int motion_y,
476 const int mb_x = s->mb_x;
477 const int mb_y = s->mb_y;
478 const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
482 for(i=0;i<mb_block_count;i++) {
483 if (s->block_last_index[i] >= 0)
484 cbp |= 1 << (mb_block_count - 1 - i);
487 if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
488 (mb_x != s->mb_width - 1 || (mb_y != s->end_mb_y - 1 && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)) &&
489 ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) ||
490 (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) |
491 ((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))) {
493 s->qscale -= s->dquant;
497 if(s->pict_type == AV_PICTURE_TYPE_P){
498 s->last_mv[0][1][0]= s->last_mv[0][0][0]=
499 s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0;
503 av_assert0(s->mb_skip_run == 0);
504 encode_mb_skip_run(s, s->mb_x);
506 encode_mb_skip_run(s, s->mb_skip_run);
509 if (s->pict_type == AV_PICTURE_TYPE_I) {
510 if(s->dquant && cbp){
511 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_type : macroblock_quant = 1 */
514 put_mb_modes(s, 1, 1, 0, 0); /* macroblock_type : macroblock_quant = 0 */
515 s->qscale -= s->dquant;
517 s->misc_bits+= get_bits_diff(s);
519 } else if (s->mb_intra) {
520 if(s->dquant && cbp){
521 put_mb_modes(s, 6, 0x01, 0, 0);
524 put_mb_modes(s, 5, 0x03, 0, 0);
525 s->qscale -= s->dquant;
527 s->misc_bits+= get_bits_diff(s);
529 memset(s->last_mv, 0, sizeof(s->last_mv));
530 } else if (s->pict_type == AV_PICTURE_TYPE_P) {
531 if(s->mv_type == MV_TYPE_16X16){
533 if ((motion_x|motion_y) == 0) {
535 put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */
538 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */
540 s->misc_bits+= get_bits_diff(s);
543 put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
546 put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
548 s->misc_bits+= get_bits_diff(s);
549 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
550 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
551 s->mv_bits+= get_bits_diff(s);
554 put_bits(&s->pb, 3, 1); /* motion only */
555 if (!s->frame_pred_frame_dct)
556 put_bits(&s->pb, 2, 2); /* motion_type: frame */
557 s->misc_bits+= get_bits_diff(s);
558 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
559 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
560 s->qscale -= s->dquant;
561 s->mv_bits+= get_bits_diff(s);
563 s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x;
564 s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y;
566 av_assert2(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
570 put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
573 put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
576 put_bits(&s->pb, 3, 1); /* motion only */
577 put_bits(&s->pb, 2, 1); /* motion_type: field */
578 s->qscale -= s->dquant;
580 s->misc_bits+= get_bits_diff(s);
582 put_bits(&s->pb, 1, s->field_select[0][i]);
583 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
584 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
585 s->last_mv[0][i][0]= s->mv[0][i][0];
586 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
588 s->mv_bits+= get_bits_diff(s);
591 if (s->chroma_y_shift) {
592 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
594 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
595 put_sbits(&s->pb, 2, cbp);
600 if(s->mv_type == MV_TYPE_16X16){
601 if (cbp){ // With coded bloc pattern
603 if(s->mv_dir == MV_DIR_FORWARD)
604 put_mb_modes(s, 6, 3, 1, 0);
606 put_mb_modes(s, 8-s->mv_dir, 2, 1, 0);
609 put_mb_modes(s, 5-s->mv_dir, 3, 1, 0);
611 }else{ // No coded bloc pattern
612 put_bits(&s->pb, 5-s->mv_dir, 2);
613 if (!s->frame_pred_frame_dct)
614 put_bits(&s->pb, 2, 2); /* motion_type: frame */
615 s->qscale -= s->dquant;
617 s->misc_bits += get_bits_diff(s);
618 if (s->mv_dir&MV_DIR_FORWARD){
619 mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
620 mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
621 s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0];
622 s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1];
625 if (s->mv_dir&MV_DIR_BACKWARD){
626 mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
627 mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
628 s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0];
629 s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1];
633 av_assert2(s->mv_type == MV_TYPE_FIELD);
634 av_assert2(!s->frame_pred_frame_dct);
635 if (cbp){ // With coded bloc pattern
637 if(s->mv_dir == MV_DIR_FORWARD)
638 put_mb_modes(s, 6, 3, 1, 1);
640 put_mb_modes(s, 8-s->mv_dir, 2, 1, 1);
643 put_mb_modes(s, 5-s->mv_dir, 3, 1, 1);
645 }else{ // No coded bloc pattern
646 put_bits(&s->pb, 5-s->mv_dir, 2);
647 put_bits(&s->pb, 2, 1); /* motion_type: field */
648 s->qscale -= s->dquant;
650 s->misc_bits += get_bits_diff(s);
651 if (s->mv_dir&MV_DIR_FORWARD){
653 put_bits(&s->pb, 1, s->field_select[0][i]);
654 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
655 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
656 s->last_mv[0][i][0]= s->mv[0][i][0];
657 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
661 if (s->mv_dir&MV_DIR_BACKWARD){
663 put_bits(&s->pb, 1, s->field_select[1][i]);
664 mpeg1_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code);
665 mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code);
666 s->last_mv[1][i][0]= s->mv[1][i][0];
667 s->last_mv[1][i][1]= 2*s->mv[1][i][1];
672 s->mv_bits += get_bits_diff(s);
674 if (s->chroma_y_shift) {
675 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
677 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
678 put_sbits(&s->pb, 2, cbp);
682 for(i=0;i<mb_block_count;i++) {
683 if (cbp & (1 << (mb_block_count - 1 - i))) {
684 mpeg1_encode_block(s, block[i], i);
689 s->i_tex_bits+= get_bits_diff(s);
691 s->p_tex_bits+= get_bits_diff(s);
695 void ff_mpeg1_encode_mb(MpegEncContext *s, DCTELEM block[6][64], int motion_x, int motion_y)
697 if (s->chroma_format == CHROMA_420) mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
698 else mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
701 // RAL: Parameter added: f_or_b_code
702 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
707 ff_mpeg12_mbMotionVectorTable[0][1],
708 ff_mpeg12_mbMotionVectorTable[0][0]);
710 int code, sign, bits;
711 int bit_size = f_or_b_code - 1;
712 int range = 1 << bit_size;
713 /* modulo encoding */
714 val = sign_extend(val, 5 + bit_size);
718 code = (val >> bit_size) + 1;
719 bits = val & (range - 1);
724 code = (val >> bit_size) + 1;
725 bits = val & (range - 1);
729 av_assert2(code > 0 && code <= 16);
732 ff_mpeg12_mbMotionVectorTable[code][1],
733 ff_mpeg12_mbMotionVectorTable[code][0]);
735 put_bits(&s->pb, 1, sign);
737 put_bits(&s->pb, bit_size, bits);
742 void ff_mpeg1_encode_init(MpegEncContext *s)
746 ff_mpeg12_common_init(s);
754 ff_init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
755 ff_init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
759 mpeg1_max_level[0][i]= ff_rl_mpeg1.max_level[0][i];
760 mpeg1_index_run[0][i]= ff_rl_mpeg1.index_run[0][i];
763 init_uni_ac_vlc(&ff_rl_mpeg1, uni_mpeg1_ac_vlc_len);
764 if(s->intra_vlc_format)
765 init_uni_ac_vlc(&ff_rl_mpeg2, uni_mpeg2_ac_vlc_len);
767 /* build unified dc encoding tables */
768 for(i=-255; i<256; i++)
776 index = av_log2(2*adiff);
778 bits= ff_mpeg12_vlc_dc_lum_bits[index] + index;
779 code= (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
780 mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
782 bits= ff_mpeg12_vlc_dc_chroma_bits[index] + index;
783 code= (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
784 mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
787 for(f_code=1; f_code<=MAX_FCODE; f_code++){
788 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
791 if(mv==0) len= ff_mpeg12_mbMotionVectorTable[0][1];
793 int val, bit_size, code;
795 bit_size = f_code - 1;
801 code = (val >> bit_size) + 1;
803 len= ff_mpeg12_mbMotionVectorTable[code][1] + 1 + bit_size;
805 len= ff_mpeg12_mbMotionVectorTable[16][1] + 2 + bit_size;
809 mv_penalty[f_code][mv+MAX_MV]= len;
814 for(f_code=MAX_FCODE; f_code>0; f_code--){
815 for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
816 fcode_tab[mv+MAX_MV]= f_code;
820 s->me.mv_penalty= mv_penalty;
821 s->fcode_tab= fcode_tab;
822 if(s->codec_id == AV_CODEC_ID_MPEG1VIDEO){
829 if (s->intra_vlc_format) {
830 s->intra_ac_vlc_length=
831 s->intra_ac_vlc_last_length= uni_mpeg2_ac_vlc_len;
833 s->intra_ac_vlc_length=
834 s->intra_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
836 s->inter_ac_vlc_length=
837 s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
840 static inline void encode_dc(MpegEncContext *s, int diff, int component)
842 if(((unsigned) (diff+255)) >= 511){
846 index= av_log2_16bit(-2*diff);
849 index= av_log2_16bit(2*diff);
851 if (component == 0) {
854 ff_mpeg12_vlc_dc_lum_bits[index] + index,
855 (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)));
859 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
860 (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)));
863 if (component == 0) {
866 mpeg1_lum_dc_uni[diff+255]&0xFF,
867 mpeg1_lum_dc_uni[diff+255]>>8);
871 mpeg1_chr_dc_uni[diff+255]&0xFF,
872 mpeg1_chr_dc_uni[diff+255]>>8);
877 static void mpeg1_encode_block(MpegEncContext *s,
881 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
883 const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
885 last_index = s->block_last_index[n];
889 component = (n <= 3 ? 0 : (n&1) + 1);
890 dc = block[0]; /* overflow is impossible */
891 diff = dc - s->last_dc[component];
892 encode_dc(s, diff, component);
893 s->last_dc[component] = dc;
895 if (s->intra_vlc_format)
896 table_vlc = ff_rl_mpeg2.table_vlc;
898 /* encode the first coefficient : needs to be done here because
899 it is handled slightly differently */
901 if (abs(level) == 1) {
902 code = ((uint32_t)level >> 31); /* the sign bit */
903 put_bits(&s->pb, 2, code | 0x02);
912 /* now quantify & encode AC coefs */
913 last_non_zero = i - 1;
915 for(;i<=last_index;i++) {
916 j = s->intra_scantable.permutated[i];
919 /* encode using VLC */
921 run = i - last_non_zero - 1;
924 MASK_ABS(sign, alevel);
927 if (alevel <= mpeg1_max_level[0][run]){
928 code= mpeg1_index_run[0][run] + alevel - 1;
929 /* store the vlc & sign at once */
930 put_bits(&s->pb, table_vlc[code][1]+1, (table_vlc[code][0]<<1) + sign);
932 /* escape seems to be pretty rare <5% so I do not optimize it */
933 put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
934 /* escape: only clip in this case */
935 put_bits(&s->pb, 6, run);
936 if(s->codec_id == AV_CODEC_ID_MPEG1VIDEO){
938 put_sbits(&s->pb, 8, level);
941 put_bits(&s->pb, 16, 0x8001 + level + 255);
943 put_sbits(&s->pb, 16, level);
947 put_sbits(&s->pb, 12, level);
954 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
957 #define OFFSET(x) offsetof(MpegEncContext, x)
958 #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
960 { "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 },\
961 { "intra_vlc", "Use MPEG-2 intra VLC table.", OFFSET(intra_vlc_format), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },\
962 { "drop_frame_timecode", "Timecode is in drop frame format.", OFFSET(drop_frame_timecode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE}, \
963 { "scan_offset", "Reserve space for SVCD scan offset user data.", OFFSET(scan_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
965 static const AVOption mpeg1_options[] = {
971 static const AVOption mpeg2_options[] = {
973 { "non_linear_quant", "Use nonlinear quantizer.", OFFSET(q_scale_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
974 { "alternate_scan", "Enable alternate scantable.", OFFSET(alternate_scan), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
979 #define mpeg12_class(x)\
980 static const AVClass mpeg## x ##_class = {\
981 .class_name = "mpeg" #x "video encoder",\
982 .item_name = av_default_item_name,\
983 .option = mpeg## x ##_options,\
984 .version = LIBAVUTIL_VERSION_INT,\
990 AVCodec ff_mpeg1video_encoder = {
991 .name = "mpeg1video",
992 .type = AVMEDIA_TYPE_VIDEO,
993 .id = AV_CODEC_ID_MPEG1VIDEO,
994 .priv_data_size = sizeof(MpegEncContext),
996 .encode2 = ff_MPV_encode_picture,
997 .close = ff_MPV_encode_end,
998 .supported_framerates = ff_mpeg12_frame_rate_tab + 1,
999 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P,
1001 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
1002 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
1003 .priv_class = &mpeg1_class,
1006 AVCodec ff_mpeg2video_encoder = {
1007 .name = "mpeg2video",
1008 .type = AVMEDIA_TYPE_VIDEO,
1009 .id = AV_CODEC_ID_MPEG2VIDEO,
1010 .priv_data_size = sizeof(MpegEncContext),
1011 .init = encode_init,
1012 .encode2 = ff_MPV_encode_picture,
1013 .close = ff_MPV_encode_end,
1014 .supported_framerates = ff_mpeg2_frame_rate_tab,
1015 .pix_fmts = (const enum AVPixelFormat[]){
1016 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE
1018 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
1019 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
1020 .priv_class = &mpeg2_class,