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
24 * @file libavcodec/mpeg12enc.c
30 #include "mpegvideo.h"
33 #include "mpeg12data.h"
34 #include "bytestream.h"
37 static const uint8_t inv_non_linear_qscale[13] = {
39 9,10,11,12,13,14,15,16,
42 static const uint8_t svcd_scan_offset_placeholder[14] = {
50 static void mpeg1_encode_block(MpegEncContext *s,
53 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code); // RAL: f_code parameter added
55 static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
56 static uint8_t fcode_tab[MAX_MV*2+1];
58 static uint8_t uni_mpeg1_ac_vlc_len [64*64*2];
59 static uint8_t uni_mpeg2_ac_vlc_len [64*64*2];
61 /* simple include everything table for dc, first byte is bits 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 void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){
74 for(run=0; run<64; run++){
77 int alevel= FFABS(level);
78 int sign= (level>>31)&1;
80 if (alevel > rl->max_level[0][run])
83 code= rl->index_run[0][run] + alevel - 1;
85 if (code < 111 /* rl->n */) {
86 /* store the vlc & sign at once */
87 len= rl->table_vlc[code][1]+1;
88 bits= (rl->table_vlc[code][0]<<1) + sign;
90 len= rl->table_vlc[111/*rl->n*/][1]+6;
91 bits= rl->table_vlc[111/*rl->n*/][0]<<6;
101 bits|= 0x8001 + level + 255;
103 bits|= level & 0xffff;
108 uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
114 static int find_frame_rate_index(MpegEncContext *s){
116 int64_t dmin= INT64_MAX;
120 int64_t n0= 1001LL/ff_frame_rate_tab[i].den*ff_frame_rate_tab[i].num*s->avctx->time_base.num;
121 int64_t n1= 1001LL*s->avctx->time_base.den;
122 if(s->avctx->strict_std_compliance > FF_COMPLIANCE_INOFFICIAL && i>=9) break;
127 s->frame_rate_index= i;
136 static av_cold int encode_init(AVCodecContext *avctx)
138 MpegEncContext *s = avctx->priv_data;
140 if(MPV_encode_init(avctx) < 0)
143 if(find_frame_rate_index(s) < 0){
144 if(s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
145 av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num);
148 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);
152 if(avctx->profile == FF_PROFILE_UNKNOWN){
153 if(avctx->level != FF_LEVEL_UNKNOWN){
154 av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
157 avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0; /* Main or 4:2:2 */
160 if(avctx->level == FF_LEVEL_UNKNOWN){
161 if(avctx->profile == 0){ /* 4:2:2 */
162 if(avctx->width <= 720 && avctx->height <= 608) avctx->level = 5; /* Main */
163 else avctx->level = 2; /* High */
165 if(avctx->profile != 1 && s->chroma_format != CHROMA_420){
166 av_log(avctx, AV_LOG_ERROR, "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
169 if(avctx->width <= 720 && avctx->height <= 576) avctx->level = 8; /* Main */
170 else if(avctx->width <= 1440) avctx->level = 6; /* High 1440 */
171 else avctx->level = 4; /* High */
175 if((avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE) && s->frame_rate_index != 4){
176 av_log(avctx, AV_LOG_ERROR, "Drop frame time code only allowed with 1001/30000 fps\n");
183 static void put_header(MpegEncContext *s, int header)
185 align_put_bits(&s->pb);
186 put_bits(&s->pb, 16, header>>16);
187 put_sbits(&s->pb, 16, header);
190 /* put sequence header if needed */
191 static void mpeg1_encode_sequence_header(MpegEncContext *s)
193 unsigned int vbv_buffer_size;
197 float best_aspect_error= 1E10;
198 float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
199 int constraint_parameter_flag;
201 if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA)
203 if (s->current_picture.key_frame) {
204 AVRational framerate= ff_frame_rate_tab[s->frame_rate_index];
206 /* mpeg1 header repeated every gop */
207 put_header(s, SEQ_START_CODE);
209 put_sbits(&s->pb, 12, s->width );
210 put_sbits(&s->pb, 12, s->height);
213 float error= aspect_ratio;
214 if(s->codec_id == CODEC_ID_MPEG1VIDEO || i <=1)
215 error-= 1.0/ff_mpeg1_aspect[i];
217 error-= av_q2d(ff_mpeg2_aspect[i])*s->height/s->width;
221 if(error < best_aspect_error){
222 best_aspect_error= error;
223 s->aspect_ratio_info= i;
227 put_bits(&s->pb, 4, s->aspect_ratio_info);
228 put_bits(&s->pb, 4, s->frame_rate_index);
230 if(s->avctx->rc_max_rate){
231 v = (s->avctx->rc_max_rate + 399) / 400;
232 if (v > 0x3ffff && s->codec_id == CODEC_ID_MPEG1VIDEO)
238 if(s->avctx->rc_buffer_size)
239 vbv_buffer_size = s->avctx->rc_buffer_size;
241 /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
242 vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
243 vbv_buffer_size= (vbv_buffer_size + 16383) / 16384;
245 put_sbits(&s->pb, 18, v);
246 put_bits(&s->pb, 1, 1); /* marker */
247 put_sbits(&s->pb, 10, vbv_buffer_size);
249 constraint_parameter_flag=
250 s->width <= 768 && s->height <= 576 &&
251 s->mb_width * s->mb_height <= 396 &&
252 s->mb_width * s->mb_height * framerate.num <= framerate.den*396*25 &&
253 framerate.num <= framerate.den*30 &&
254 s->avctx->me_range && s->avctx->me_range < 128 &&
255 vbv_buffer_size <= 20 &&
257 s->codec_id == CODEC_ID_MPEG1VIDEO;
259 put_bits(&s->pb, 1, constraint_parameter_flag);
261 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
262 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
264 if(s->codec_id == CODEC_ID_MPEG2VIDEO){
265 put_header(s, EXT_START_CODE);
266 put_bits(&s->pb, 4, 1); //seq ext
268 put_bits(&s->pb, 1, s->avctx->profile == 0); //escx 1 for 4:2:2 profile */
270 put_bits(&s->pb, 3, s->avctx->profile); //profile
271 put_bits(&s->pb, 4, s->avctx->level); //level
273 put_bits(&s->pb, 1, s->progressive_sequence);
274 put_bits(&s->pb, 2, s->chroma_format);
275 put_bits(&s->pb, 2, s->width >>12);
276 put_bits(&s->pb, 2, s->height>>12);
277 put_bits(&s->pb, 12, v>>18); //bitrate ext
278 put_bits(&s->pb, 1, 1); //marker
279 put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext
280 put_bits(&s->pb, 1, s->low_delay);
281 put_bits(&s->pb, 2, 0); // frame_rate_ext_n
282 put_bits(&s->pb, 5, 0); // frame_rate_ext_d
285 put_header(s, GOP_START_CODE);
286 put_bits(&s->pb, 1, !!(s->avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE)); /* drop frame flag */
287 /* time code : we must convert from the real frame rate to a
288 fake mpeg frame rate in case of low frame rate */
289 fps = (framerate.num + framerate.den/2)/ framerate.den;
290 time_code = s->current_picture_ptr->coded_picture_number + s->avctx->timecode_frame_start;
292 s->gop_picture_number = s->current_picture_ptr->coded_picture_number;
293 if (s->avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE) {
294 /* only works for NTSC 29.97 */
295 int d = time_code / 17982;
296 int m = time_code % 17982;
297 //if (m < 2) m += 2; /* not needed since -2,-1 / 1798 in C returns 0 */
298 time_code += 18 * d + 2 * ((m - 2) / 1798);
300 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
301 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
302 put_bits(&s->pb, 1, 1);
303 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
304 put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
305 put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
306 put_bits(&s->pb, 1, 0); /* broken link */
310 static inline void encode_mb_skip_run(MpegEncContext *s, int run){
312 put_bits(&s->pb, 11, 0x008);
315 put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1],
316 ff_mpeg12_mbAddrIncrTable[run][0]);
319 static av_always_inline void put_qscale(MpegEncContext *s)
322 assert(s->qscale>=1 && s->qscale <=12);
323 put_bits(&s->pb, 5, inv_non_linear_qscale[s->qscale]);
325 put_bits(&s->pb, 5, s->qscale);
329 void ff_mpeg1_encode_slice_header(MpegEncContext *s){
330 if (s->height > 2800) {
331 put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127));
332 put_bits(&s->pb, 3, s->mb_y >> 7); /* slice_vertical_position_extension */
334 put_header(s, SLICE_MIN_START_CODE + s->mb_y);
337 put_bits(&s->pb, 1, 0); /* slice extra information */
340 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
342 mpeg1_encode_sequence_header(s);
344 /* mpeg1 picture header */
345 put_header(s, PICTURE_START_CODE);
346 /* temporal reference */
348 // RAL: s->picture_number instead of s->fake_picture_number
349 put_bits(&s->pb, 10, (s->picture_number -
350 s->gop_picture_number) & 0x3ff);
351 put_bits(&s->pb, 3, s->pict_type);
353 s->vbv_delay_ptr= s->pb.buf + put_bits_count(&s->pb)/8;
354 put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
356 // RAL: Forward f_code also needed for B frames
357 if (s->pict_type == FF_P_TYPE || s->pict_type == FF_B_TYPE) {
358 put_bits(&s->pb, 1, 0); /* half pel coordinates */
359 if(s->codec_id == CODEC_ID_MPEG1VIDEO)
360 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
362 put_bits(&s->pb, 3, 7); /* forward_f_code */
365 // RAL: Backward f_code necessary for B frames
366 if (s->pict_type == FF_B_TYPE) {
367 put_bits(&s->pb, 1, 0); /* half pel coordinates */
368 if(s->codec_id == CODEC_ID_MPEG1VIDEO)
369 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
371 put_bits(&s->pb, 3, 7); /* backward_f_code */
374 put_bits(&s->pb, 1, 0); /* extra bit picture */
376 s->frame_pred_frame_dct = 1;
377 if(s->codec_id == CODEC_ID_MPEG2VIDEO){
378 put_header(s, EXT_START_CODE);
379 put_bits(&s->pb, 4, 8); //pic ext
380 if (s->pict_type == FF_P_TYPE || s->pict_type == FF_B_TYPE) {
381 put_bits(&s->pb, 4, s->f_code);
382 put_bits(&s->pb, 4, s->f_code);
384 put_bits(&s->pb, 8, 255);
386 if (s->pict_type == FF_B_TYPE) {
387 put_bits(&s->pb, 4, s->b_code);
388 put_bits(&s->pb, 4, s->b_code);
390 put_bits(&s->pb, 8, 255);
392 put_bits(&s->pb, 2, s->intra_dc_precision);
394 assert(s->picture_structure == PICT_FRAME);
395 put_bits(&s->pb, 2, s->picture_structure);
396 if (s->progressive_sequence) {
397 put_bits(&s->pb, 1, 0); /* no repeat */
399 put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
401 /* XXX: optimize the generation of this flag with entropy
403 s->frame_pred_frame_dct = s->progressive_sequence;
405 put_bits(&s->pb, 1, s->frame_pred_frame_dct);
406 put_bits(&s->pb, 1, s->concealment_motion_vectors);
407 put_bits(&s->pb, 1, s->q_scale_type);
408 put_bits(&s->pb, 1, s->intra_vlc_format);
409 put_bits(&s->pb, 1, s->alternate_scan);
410 put_bits(&s->pb, 1, s->repeat_first_field);
411 s->progressive_frame = s->progressive_sequence;
412 put_bits(&s->pb, 1, s->chroma_format == CHROMA_420 ? s->progressive_frame : 0); /* chroma_420_type */
413 put_bits(&s->pb, 1, s->progressive_frame);
414 put_bits(&s->pb, 1, 0); //composite_display_flag
416 if(s->flags & CODEC_FLAG_SVCD_SCAN_OFFSET){
419 put_header(s, USER_START_CODE);
420 for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){
421 put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
426 ff_mpeg1_encode_slice_header(s);
429 static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
430 int has_mv, int field_motion)
432 put_bits(&s->pb, n, bits);
433 if (!s->frame_pred_frame_dct) {
435 put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */
436 put_bits(&s->pb, 1, s->interlaced_dct);
440 static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
441 DCTELEM block[6][64],
442 int motion_x, int motion_y,
446 const int mb_x = s->mb_x;
447 const int mb_y = s->mb_y;
448 const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
452 for(i=0;i<mb_block_count;i++) {
453 if (s->block_last_index[i] >= 0)
454 cbp |= 1 << (mb_block_count - 1 - i);
457 if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
458 (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) &&
459 ((s->pict_type == FF_P_TYPE && (motion_x | motion_y) == 0) ||
460 (s->pict_type == FF_B_TYPE && 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) |
461 ((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))) {
463 s->qscale -= s->dquant;
467 if(s->pict_type == FF_P_TYPE){
468 s->last_mv[0][1][0]= s->last_mv[0][0][0]=
469 s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0;
473 assert(s->mb_skip_run == 0);
474 encode_mb_skip_run(s, s->mb_x);
476 encode_mb_skip_run(s, s->mb_skip_run);
479 if (s->pict_type == FF_I_TYPE) {
480 if(s->dquant && cbp){
481 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_type : macroblock_quant = 1 */
484 put_mb_modes(s, 1, 1, 0, 0); /* macroblock_type : macroblock_quant = 0 */
485 s->qscale -= s->dquant;
487 s->misc_bits+= get_bits_diff(s);
489 } else if (s->mb_intra) {
490 if(s->dquant && cbp){
491 put_mb_modes(s, 6, 0x01, 0, 0);
494 put_mb_modes(s, 5, 0x03, 0, 0);
495 s->qscale -= s->dquant;
497 s->misc_bits+= get_bits_diff(s);
499 memset(s->last_mv, 0, sizeof(s->last_mv));
500 } else if (s->pict_type == FF_P_TYPE) {
501 if(s->mv_type == MV_TYPE_16X16){
503 if ((motion_x|motion_y) == 0) {
505 put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */
508 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */
510 s->misc_bits+= get_bits_diff(s);
513 put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
516 put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
518 s->misc_bits+= get_bits_diff(s);
519 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
520 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
521 s->mv_bits+= get_bits_diff(s);
524 put_bits(&s->pb, 3, 1); /* motion only */
525 if (!s->frame_pred_frame_dct)
526 put_bits(&s->pb, 2, 2); /* motion_type: frame */
527 s->misc_bits+= get_bits_diff(s);
528 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
529 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
530 s->qscale -= s->dquant;
531 s->mv_bits+= get_bits_diff(s);
533 s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x;
534 s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y;
536 assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
540 put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
543 put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
546 put_bits(&s->pb, 3, 1); /* motion only */
547 put_bits(&s->pb, 2, 1); /* motion_type: field */
548 s->qscale -= s->dquant;
550 s->misc_bits+= get_bits_diff(s);
552 put_bits(&s->pb, 1, s->field_select[0][i]);
553 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
554 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
555 s->last_mv[0][i][0]= s->mv[0][i][0];
556 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
558 s->mv_bits+= get_bits_diff(s);
561 if (s->chroma_y_shift) {
562 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
564 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
565 put_sbits(&s->pb, 2, cbp);
570 if(s->mv_type == MV_TYPE_16X16){
571 if (cbp){ // With coded bloc pattern
573 if(s->mv_dir == MV_DIR_FORWARD)
574 put_mb_modes(s, 6, 3, 1, 0);
576 put_mb_modes(s, 8-s->mv_dir, 2, 1, 0);
579 put_mb_modes(s, 5-s->mv_dir, 3, 1, 0);
581 }else{ // No coded bloc pattern
582 put_bits(&s->pb, 5-s->mv_dir, 2);
583 if (!s->frame_pred_frame_dct)
584 put_bits(&s->pb, 2, 2); /* motion_type: frame */
585 s->qscale -= s->dquant;
587 s->misc_bits += get_bits_diff(s);
588 if (s->mv_dir&MV_DIR_FORWARD){
589 mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
590 mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
591 s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0];
592 s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1];
595 if (s->mv_dir&MV_DIR_BACKWARD){
596 mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
597 mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
598 s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0];
599 s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1];
603 assert(s->mv_type == MV_TYPE_FIELD);
604 assert(!s->frame_pred_frame_dct);
605 if (cbp){ // With coded bloc pattern
607 if(s->mv_dir == MV_DIR_FORWARD)
608 put_mb_modes(s, 6, 3, 1, 1);
610 put_mb_modes(s, 8-s->mv_dir, 2, 1, 1);
613 put_mb_modes(s, 5-s->mv_dir, 3, 1, 1);
615 }else{ // No coded bloc pattern
616 put_bits(&s->pb, 5-s->mv_dir, 2);
617 put_bits(&s->pb, 2, 1); /* motion_type: field */
618 s->qscale -= s->dquant;
620 s->misc_bits += get_bits_diff(s);
621 if (s->mv_dir&MV_DIR_FORWARD){
623 put_bits(&s->pb, 1, s->field_select[0][i]);
624 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
625 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
626 s->last_mv[0][i][0]= s->mv[0][i][0];
627 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
631 if (s->mv_dir&MV_DIR_BACKWARD){
633 put_bits(&s->pb, 1, s->field_select[1][i]);
634 mpeg1_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code);
635 mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code);
636 s->last_mv[1][i][0]= s->mv[1][i][0];
637 s->last_mv[1][i][1]= 2*s->mv[1][i][1];
642 s->mv_bits += get_bits_diff(s);
644 if (s->chroma_y_shift) {
645 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
647 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
648 put_sbits(&s->pb, 2, cbp);
652 for(i=0;i<mb_block_count;i++) {
653 if (cbp & (1 << (mb_block_count - 1 - i))) {
654 mpeg1_encode_block(s, block[i], i);
659 s->i_tex_bits+= get_bits_diff(s);
661 s->p_tex_bits+= get_bits_diff(s);
665 void mpeg1_encode_mb(MpegEncContext *s, DCTELEM block[6][64], int motion_x, int motion_y)
667 if (s->chroma_format == CHROMA_420) mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
668 else mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
671 // RAL: Parameter added: f_or_b_code
672 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
677 ff_mpeg12_mbMotionVectorTable[0][1],
678 ff_mpeg12_mbMotionVectorTable[0][0]);
680 int code, sign, bits;
681 int bit_size = f_or_b_code - 1;
682 int range = 1 << bit_size;
683 /* modulo encoding */
684 int l= INT_BIT - 5 - bit_size;
689 code = (val >> bit_size) + 1;
690 bits = val & (range - 1);
695 code = (val >> bit_size) + 1;
696 bits = val & (range - 1);
700 assert(code > 0 && code <= 16);
703 ff_mpeg12_mbMotionVectorTable[code][1],
704 ff_mpeg12_mbMotionVectorTable[code][0]);
706 put_bits(&s->pb, 1, sign);
708 put_bits(&s->pb, bit_size, bits);
713 void ff_mpeg1_encode_init(MpegEncContext *s)
717 ff_mpeg12_common_init(s);
725 init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
726 init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
730 mpeg1_max_level[0][i]= ff_rl_mpeg1.max_level[0][i];
731 mpeg1_index_run[0][i]= ff_rl_mpeg1.index_run[0][i];
734 init_uni_ac_vlc(&ff_rl_mpeg1, uni_mpeg1_ac_vlc_len);
735 if(s->intra_vlc_format)
736 init_uni_ac_vlc(&ff_rl_mpeg2, uni_mpeg2_ac_vlc_len);
738 /* build unified dc encoding tables */
739 for(i=-255; i<256; i++)
747 index = av_log2(2*adiff);
749 bits= ff_mpeg12_vlc_dc_lum_bits[index] + index;
750 code= (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
751 mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
753 bits= ff_mpeg12_vlc_dc_chroma_bits[index] + index;
754 code= (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
755 mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
758 for(f_code=1; f_code<=MAX_FCODE; f_code++){
759 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
762 if(mv==0) len= ff_mpeg12_mbMotionVectorTable[0][1];
764 int val, bit_size, range, code;
766 bit_size = f_code - 1;
767 range = 1 << bit_size;
773 code = (val >> bit_size) + 1;
775 len= ff_mpeg12_mbMotionVectorTable[code][1] + 1 + bit_size;
777 len= ff_mpeg12_mbMotionVectorTable[16][1] + 2 + bit_size;
781 mv_penalty[f_code][mv+MAX_MV]= len;
786 for(f_code=MAX_FCODE; f_code>0; f_code--){
787 for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
788 fcode_tab[mv+MAX_MV]= f_code;
792 s->me.mv_penalty= mv_penalty;
793 s->fcode_tab= fcode_tab;
794 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
801 if (s->intra_vlc_format) {
802 s->intra_ac_vlc_length=
803 s->intra_ac_vlc_last_length= uni_mpeg2_ac_vlc_len;
805 s->intra_ac_vlc_length=
806 s->intra_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
808 s->inter_ac_vlc_length=
809 s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
812 static inline void encode_dc(MpegEncContext *s, int diff, int component)
814 if(((unsigned) (diff+255)) >= 511){
818 index= av_log2_16bit(-2*diff);
821 index= av_log2_16bit(2*diff);
823 if (component == 0) {
826 ff_mpeg12_vlc_dc_lum_bits[index] + index,
827 (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)));
831 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
832 (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)));
835 if (component == 0) {
838 mpeg1_lum_dc_uni[diff+255]&0xFF,
839 mpeg1_lum_dc_uni[diff+255]>>8);
843 mpeg1_chr_dc_uni[diff+255]&0xFF,
844 mpeg1_chr_dc_uni[diff+255]>>8);
849 static void mpeg1_encode_block(MpegEncContext *s,
853 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
855 const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
857 last_index = s->block_last_index[n];
861 component = (n <= 3 ? 0 : (n&1) + 1);
862 dc = block[0]; /* overflow is impossible */
863 diff = dc - s->last_dc[component];
864 encode_dc(s, diff, component);
865 s->last_dc[component] = dc;
867 if (s->intra_vlc_format)
868 table_vlc = ff_rl_mpeg2.table_vlc;
870 /* encode the first coefficient : needs to be done here because
871 it is handled slightly differently */
873 if (abs(level) == 1) {
874 code = ((uint32_t)level >> 31); /* the sign bit */
875 put_bits(&s->pb, 2, code | 0x02);
884 /* now quantify & encode AC coefs */
885 last_non_zero = i - 1;
887 for(;i<=last_index;i++) {
888 j = s->intra_scantable.permutated[i];
893 dprintf(s->avctx, "level[%d]=%d\n", i, level);
895 /* encode using VLC */
897 run = i - last_non_zero - 1;
900 MASK_ABS(sign, alevel)
903 if (alevel <= mpeg1_max_level[0][run]){
904 code= mpeg1_index_run[0][run] + alevel - 1;
905 /* store the vlc & sign at once */
906 put_bits(&s->pb, table_vlc[code][1]+1, (table_vlc[code][0]<<1) + sign);
908 /* escape seems to be pretty rare <5% so I do not optimize it */
909 put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
910 /* escape: only clip in this case */
911 put_bits(&s->pb, 6, run);
912 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
914 put_sbits(&s->pb, 8, level);
917 put_bits(&s->pb, 16, 0x8001 + level + 255);
919 put_sbits(&s->pb, 16, level);
923 put_sbits(&s->pb, 12, level);
930 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
933 AVCodec mpeg1video_encoder = {
937 sizeof(MpegEncContext),
941 .supported_framerates= ff_frame_rate_tab+1,
942 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
943 .capabilities= CODEC_CAP_DELAY,
944 .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
947 AVCodec mpeg2video_encoder = {
951 sizeof(MpegEncContext),
955 .supported_framerates= ff_frame_rate_tab+1,
956 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE},
957 .capabilities= CODEC_CAP_DELAY,
958 .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),