FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mpeg12enc.c
Go to the documentation of this file.
1 /*
2  * MPEG1/2 encoder
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22 
23 /**
24  * @file
25  * MPEG1/2 encoder
26  */
27 
28 #include "avcodec.h"
29 #include "mathops.h"
30 #include "mpegvideo.h"
31 
32 #include "mpeg12.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"
39 
40 static const uint8_t inv_non_linear_qscale[13] = {
41  0, 2, 4, 6, 8,
42  9,10,11,12,13,14,15,16,
43 };
44 
46  0x10, 0x0E,
47  0x00, 0x80, 0x81,
48  0x00, 0x80, 0x81,
49  0xff, 0xff, 0xff,
50  0xff, 0xff, 0xff,
51 };
52 
53 static void mpeg1_encode_block(MpegEncContext *s,
54  int16_t *block,
55  int component);
56 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code); // RAL: f_code parameter added
57 
59 static uint8_t fcode_tab[MAX_MV*2+1];
60 
61 static uint8_t uni_mpeg1_ac_vlc_len [64*64*2];
62 static uint8_t uni_mpeg2_ac_vlc_len [64*64*2];
63 
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];
67 
68 static uint8_t mpeg1_index_run[2][64];
69 static int8_t mpeg1_max_level[2][64];
70 
71 static void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){
72  int i;
73 
74  for(i=0; i<128; i++){
75  int level= i-64;
76  int run;
77  if (!level)
78  continue;
79  for(run=0; run<64; run++){
80  int len, code;
81 
82  int alevel= FFABS(level);
83 
84  if (alevel > rl->max_level[0][run])
85  code= 111; /*rl->n*/
86  else
87  code= rl->index_run[0][run] + alevel - 1;
88 
89  if (code < 111 /* rl->n */) {
90  /* length of vlc and sign */
91  len= rl->table_vlc[code][1]+1;
92  } else {
93  len= rl->table_vlc[111/*rl->n*/][1]+6;
94 
95  if (alevel < 128) {
96  len += 8;
97  } else {
98  len += 16;
99  }
100  }
101 
102  uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
103  }
104  }
105 }
106 
107 
109  int i;
110  AVRational bestq= (AVRational){0, 0};
111  AVRational ext;
112  AVRational target = av_inv_q(s->avctx->time_base);
113 
114  for(i=1;i<14;i++) {
115  if(s->avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL && i>=9) break;
116 
117  for (ext.num=1; ext.num <= 4; ext.num++) {
118  for (ext.den=1; ext.den <= 32; ext.den++) {
120 
121  if(s->codec_id != AV_CODEC_ID_MPEG2VIDEO && (ext.den!=1 || ext.num!=1))
122  continue;
123  if(av_gcd(ext.den, ext.num) != 1)
124  continue;
125 
126  if( bestq.num==0
127  || av_nearer_q(target, bestq, q) < 0
128  || ext.num==1 && ext.den==1 && av_nearer_q(target, bestq, q) == 0){
129  bestq = q;
130  s->frame_rate_index= i;
131  s->mpeg2_frame_rate_ext.num = ext.num;
132  s->mpeg2_frame_rate_ext.den = ext.den;
133  }
134  }
135  }
136  }
137  if(av_cmp_q(target, bestq))
138  return -1;
139  else
140  return 0;
141 }
142 
144 {
145  MpegEncContext *s = avctx->priv_data;
146 
147  if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && avctx->height > 2800)
148  avctx->thread_count = 1;
149 
150  if(ff_MPV_encode_init(avctx) < 0)
151  return -1;
152 
153  if(find_frame_rate_index(s) < 0){
155  av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num);
156  return -1;
157  }else{
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);
159  }
160  }
161 
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");
165  return -1;
166  }
167  avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0; /* Main or 4:2:2 */
168  }
169 
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 */
174  }else{
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");
177  return -1;
178  }
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 */
182  }
183  }
184 
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);
188  }
189 
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);
195  }
196  }
197 
199  if (s->drop_frame_timecode)
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");
203  return -1;
204  }
205 
206  if (s->tc_opt_str) {
208  int ret = av_timecode_init_from_string(&s->tc, rate, s->tc_opt_str, s);
209  if (ret < 0)
210  return ret;
213  } else {
214  s->avctx->timecode_frame_start = 0; // default is -1
215  }
216  return 0;
217 }
218 
219 static void put_header(MpegEncContext *s, int header)
220 {
222  put_bits(&s->pb, 16, header>>16);
223  put_sbits(&s->pb, 16, header);
224 }
225 
226 /* put sequence header if needed */
228 {
229  unsigned int vbv_buffer_size;
230  unsigned int fps, v;
231  int i;
232  uint64_t time_code;
233  float best_aspect_error= 1E10;
234  float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
235  int constraint_parameter_flag;
236 
237  if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA)
238 
239  if (s->current_picture.f.key_frame) {
241 
242  /* mpeg1 header repeated every gop */
244 
245  put_sbits(&s->pb, 12, s->width & 0xFFF);
246  put_sbits(&s->pb, 12, s->height & 0xFFF);
247 
248  for(i=1; i<15; i++){
249  float error= aspect_ratio;
250  if(s->codec_id == AV_CODEC_ID_MPEG1VIDEO || i <=1)
251  error-= 1.0/ff_mpeg1_aspect[i];
252  else
253  error-= av_q2d(ff_mpeg2_aspect[i])*s->height/s->width;
254 
255  error= FFABS(error);
256 
257  if(error < best_aspect_error){
258  best_aspect_error= error;
259  s->aspect_ratio_info= i;
260  }
261  }
262 
263  put_bits(&s->pb, 4, s->aspect_ratio_info);
264  put_bits(&s->pb, 4, s->frame_rate_index);
265 
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)
269  v = 0x3ffff;
270  }else{
271  v= 0x3FFFF;
272  }
273 
274  if(s->avctx->rc_buffer_size)
275  vbv_buffer_size = s->avctx->rc_buffer_size;
276  else
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;
280 
281  put_sbits(&s->pb, 18, v);
282  put_bits(&s->pb, 1, 1); /* marker */
283  put_sbits(&s->pb, 10, vbv_buffer_size);
284 
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 &&
292  v <= 1856000/400 &&
294 
295  put_bits(&s->pb, 1, constraint_parameter_flag);
296 
299 
302  put_bits(&s->pb, 4, 1); //seq ext
303 
304  put_bits(&s->pb, 1, s->avctx->profile == 0); //escx 1 for 4:2:2 profile */
305 
306  put_bits(&s->pb, 3, s->avctx->profile); //profile
307  put_bits(&s->pb, 4, s->avctx->level); //level
308 
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
319  }
320 
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;
327 
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 */
339  }
340 }
341 
342 static inline void encode_mb_skip_run(MpegEncContext *s, int run){
343  while (run >= 33) {
344  put_bits(&s->pb, 11, 0x008);
345  run -= 33;
346  }
348  ff_mpeg12_mbAddrIncrTable[run][0]);
349 }
350 
352 {
353  if(s->q_scale_type){
354  av_assert2(s->qscale>=1 && s->qscale <=12);
356  }else{
357  put_bits(&s->pb, 5, s->qscale);
358  }
359 }
360 
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 */
365  } else {
367  }
368  put_qscale(s);
369  put_bits(&s->pb, 1, 0); /* slice extra information */
370 }
371 
372 void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
373 {
375 
376  /* mpeg1 picture header */
378  /* temporal reference */
379 
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);
384 
385  s->vbv_delay_ptr= s->pb.buf + put_bits_count(&s->pb)/8;
386  put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
387 
388  // RAL: Forward f_code also needed for B frames
390  put_bits(&s->pb, 1, 0); /* half pel coordinates */
392  put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
393  else
394  put_bits(&s->pb, 3, 7); /* forward_f_code */
395  }
396 
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 */
401  put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
402  else
403  put_bits(&s->pb, 3, 7); /* backward_f_code */
404  }
405 
406  put_bits(&s->pb, 1, 0); /* extra bit picture */
407 
408  s->frame_pred_frame_dct = 1;
411  put_bits(&s->pb, 4, 8); //pic ext
413  put_bits(&s->pb, 4, s->f_code);
414  put_bits(&s->pb, 4, s->f_code);
415  }else{
416  put_bits(&s->pb, 8, 255);
417  }
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);
421  }else{
422  put_bits(&s->pb, 8, 255);
423  }
424  put_bits(&s->pb, 2, s->intra_dc_precision);
425 
427  put_bits(&s->pb, 2, s->picture_structure);
428  if (s->progressive_sequence) {
429  put_bits(&s->pb, 1, 0); /* no repeat */
430  } else {
432  }
433  /* XXX: optimize the generation of this flag with entropy
434  measures */
436 
437  put_bits(&s->pb, 1, s->frame_pred_frame_dct);
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);
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
447  }
448  if (s->scan_offset) {
449  int i;
450 
452  for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){
454  }
455  }
456 
457  s->mb_y=0;
459 }
460 
461 static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
462  int has_mv, int field_motion)
463 {
464  put_bits(&s->pb, n, bits);
465  if (!s->frame_pred_frame_dct) {
466  if (has_mv)
467  put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */
468  put_bits(&s->pb, 1, s->interlaced_dct);
469  }
470 }
471 
473  int16_t block[6][64],
474  int motion_x, int motion_y,
475  int mb_block_count)
476 {
477  int i, cbp;
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;
481 
482  /* compute cbp */
483  cbp = 0;
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);
487  }
488 
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))) {
494  s->mb_skip_run++;
495  s->qscale -= s->dquant;
496  s->skip_count++;
497  s->misc_bits++;
498  s->last_bits++;
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;
502  }
503  } else {
504  if(first_mb){
505  av_assert0(s->mb_skip_run == 0);
506  encode_mb_skip_run(s, s->mb_x);
507  }else{
509  }
510 
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 */
514  put_qscale(s);
515  }else{
516  put_mb_modes(s, 1, 1, 0, 0); /* macroblock_type : macroblock_quant = 0 */
517  s->qscale -= s->dquant;
518  }
519  s->misc_bits+= get_bits_diff(s);
520  s->i_count++;
521  } else if (s->mb_intra) {
522  if(s->dquant && cbp){
523  put_mb_modes(s, 6, 0x01, 0, 0);
524  put_qscale(s);
525  }else{
526  put_mb_modes(s, 5, 0x03, 0, 0);
527  s->qscale -= s->dquant;
528  }
529  s->misc_bits+= get_bits_diff(s);
530  s->i_count++;
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){
534  if (cbp != 0) {
535  if ((motion_x|motion_y) == 0) {
536  if(s->dquant){
537  put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */
538  put_qscale(s);
539  }else{
540  put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */
541  }
542  s->misc_bits+= get_bits_diff(s);
543  } else {
544  if(s->dquant){
545  put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
546  put_qscale(s);
547  }else{
548  put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
549  }
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);
554  }
555  } else {
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);
564  }
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;
567  }else{
569 
570  if (cbp) {
571  if(s->dquant){
572  put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
573  put_qscale(s);
574  }else{
575  put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
576  }
577  } else {
578  put_bits(&s->pb, 3, 1); /* motion only */
579  put_bits(&s->pb, 2, 1); /* motion_type: field */
580  s->qscale -= s->dquant;
581  }
582  s->misc_bits+= get_bits_diff(s);
583  for(i=0; i<2; i++){
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];
589  }
590  s->mv_bits+= get_bits_diff(s);
591  }
592  if(cbp) {
593  if (s->chroma_y_shift) {
594  put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
595  } else {
596  put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
597  put_sbits(&s->pb, 2, cbp);
598  }
599  }
600  s->f_count++;
601  } else{
602  if(s->mv_type == MV_TYPE_16X16){
603  if (cbp){ // With coded bloc pattern
604  if (s->dquant) {
605  if(s->mv_dir == MV_DIR_FORWARD)
606  put_mb_modes(s, 6, 3, 1, 0);
607  else
608  put_mb_modes(s, 8-s->mv_dir, 2, 1, 0);
609  put_qscale(s);
610  } else {
611  put_mb_modes(s, 5-s->mv_dir, 3, 1, 0);
612  }
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;
618  }
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];
625  s->f_count++;
626  }
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];
632  s->b_count++;
633  }
634  }else{
637  if (cbp){ // With coded bloc pattern
638  if (s->dquant) {
639  if(s->mv_dir == MV_DIR_FORWARD)
640  put_mb_modes(s, 6, 3, 1, 1);
641  else
642  put_mb_modes(s, 8-s->mv_dir, 2, 1, 1);
643  put_qscale(s);
644  } else {
645  put_mb_modes(s, 5-s->mv_dir, 3, 1, 1);
646  }
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;
651  }
652  s->misc_bits += get_bits_diff(s);
653  if (s->mv_dir&MV_DIR_FORWARD){
654  for(i=0; i<2; i++){
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];
660  }
661  s->f_count++;
662  }
663  if (s->mv_dir&MV_DIR_BACKWARD){
664  for(i=0; i<2; i++){
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];
670  }
671  s->b_count++;
672  }
673  }
674  s->mv_bits += get_bits_diff(s);
675  if(cbp) {
676  if (s->chroma_y_shift) {
677  put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
678  } else {
679  put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
680  put_sbits(&s->pb, 2, cbp);
681  }
682  }
683  }
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);
687  }
688  }
689  s->mb_skip_run = 0;
690  if(s->mb_intra)
691  s->i_tex_bits+= get_bits_diff(s);
692  else
693  s->p_tex_bits+= get_bits_diff(s);
694  }
695 }
696 
697 void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
698 {
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);
701 }
702 
703 // RAL: Parameter added: f_or_b_code
704 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
705 {
706  if (val == 0) {
707  /* zero vector */
708  put_bits(&s->pb,
711  } else {
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);
717 
718  if (val >= 0) {
719  val--;
720  code = (val >> bit_size) + 1;
721  bits = val & (range - 1);
722  sign = 0;
723  } else {
724  val = -val;
725  val--;
726  code = (val >> bit_size) + 1;
727  bits = val & (range - 1);
728  sign = 1;
729  }
730 
731  av_assert2(code > 0 && code <= 16);
732 
733  put_bits(&s->pb,
736 
737  put_bits(&s->pb, 1, sign);
738  if (bit_size > 0) {
739  put_bits(&s->pb, bit_size, bits);
740  }
741  }
742 }
743 
745 {
746  static int done=0;
747 
749 
750  if(!done){
751  int f_code;
752  int mv;
753  int i;
754 
755  done=1;
758 
759  for(i=0; i<64; i++)
760  {
763  }
764 
766  if(s->intra_vlc_format)
768 
769  /* build unified dc encoding tables */
770  for(i=-255; i<256; i++)
771  {
772  int adiff, index;
773  int bits, code;
774  int diff=i;
775 
776  adiff = FFABS(diff);
777  if(diff<0) diff--;
778  index = av_log2(2*adiff);
779 
781  code= (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
782  mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
783 
785  code= (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
786  mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
787  }
788 
789  for(f_code=1; f_code<=MAX_FCODE; f_code++){
790  for(mv=-MAX_MV; mv<=MAX_MV; mv++){
791  int len;
792 
793  if(mv==0) len= ff_mpeg12_mbMotionVectorTable[0][1];
794  else{
795  int val, bit_size, code;
796 
797  bit_size = f_code - 1;
798 
799  val=mv;
800  if (val < 0)
801  val = -val;
802  val--;
803  code = (val >> bit_size) + 1;
804  if(code<17){
805  len= ff_mpeg12_mbMotionVectorTable[code][1] + 1 + bit_size;
806  }else{
807  len= ff_mpeg12_mbMotionVectorTable[16][1] + 2 + bit_size;
808  }
809  }
810 
811  mv_penalty[f_code][mv+MAX_MV]= len;
812  }
813  }
814 
815 
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;
819  }
820  }
821  }
823  s->fcode_tab= fcode_tab;
825  s->min_qcoeff=-255;
826  s->max_qcoeff= 255;
827  }else{
828  s->min_qcoeff=-2047;
829  s->max_qcoeff= 2047;
830  }
831  if (s->intra_vlc_format) {
834  } else {
837  }
840 }
841 
842 static inline void encode_dc(MpegEncContext *s, int diff, int component)
843 {
844  if(((unsigned) (diff+255)) >= 511){
845  int index;
846 
847  if(diff<0){
848  index= av_log2_16bit(-2*diff);
849  diff--;
850  }else{
851  index= av_log2_16bit(2*diff);
852  }
853  if (component == 0) {
854  put_bits(
855  &s->pb,
856  ff_mpeg12_vlc_dc_lum_bits[index] + index,
857  (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)));
858  }else{
859  put_bits(
860  &s->pb,
861  ff_mpeg12_vlc_dc_chroma_bits[index] + index,
862  (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)));
863  }
864  }else{
865  if (component == 0) {
866  put_bits(
867  &s->pb,
868  mpeg1_lum_dc_uni[diff+255]&0xFF,
869  mpeg1_lum_dc_uni[diff+255]>>8);
870  } else {
871  put_bits(
872  &s->pb,
873  mpeg1_chr_dc_uni[diff+255]&0xFF,
874  mpeg1_chr_dc_uni[diff+255]>>8);
875  }
876  }
877 }
878 
880  int16_t *block,
881  int n)
882 {
883  int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
884  int code, component;
885  const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
886 
887  last_index = s->block_last_index[n];
888 
889  /* DC coef */
890  if (s->mb_intra) {
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;
896  i = 1;
897  if (s->intra_vlc_format)
898  table_vlc = ff_rl_mpeg2.table_vlc;
899  } else {
900  /* encode the first coefficient : needs to be done here because
901  it is handled slightly differently */
902  level = block[0];
903  if (abs(level) == 1) {
904  code = ((uint32_t)level >> 31); /* the sign bit */
905  put_bits(&s->pb, 2, code | 0x02);
906  i = 1;
907  } else {
908  i = 0;
909  last_non_zero = -1;
910  goto next_coef;
911  }
912  }
913 
914  /* now quantify & encode AC coefs */
915  last_non_zero = i - 1;
916 
917  for(;i<=last_index;i++) {
918  j = s->intra_scantable.permutated[i];
919  level = block[j];
920  next_coef:
921  /* encode using VLC */
922  if (level != 0) {
923  run = i - last_non_zero - 1;
924 
925  alevel= level;
926  MASK_ABS(sign, alevel);
927  sign&=1;
928 
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);
933  } else {
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);
939  if (alevel < 128) {
940  put_sbits(&s->pb, 8, level);
941  } else {
942  if (level < 0) {
943  put_bits(&s->pb, 16, 0x8001 + level + 255);
944  } else {
945  put_sbits(&s->pb, 16, level);
946  }
947  }
948  }else{
949  put_sbits(&s->pb, 12, level);
950  }
951  }
952  last_non_zero = i;
953  }
954  }
955  /* end of block */
956  put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
957 }
958 
959 #define OFFSET(x) offsetof(MpegEncContext, x)
960 #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
961 #define COMMON_OPTS\
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 },
966 
967 static const AVOption mpeg1_options[] = {
970  { NULL },
971 };
972 
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 },
978  { NULL },
979 };
980 
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,\
987 };
988 
990 mpeg12_class(2)
991 
992 AVCodec ff_mpeg1video_encoder = {
993  .name = "mpeg1video",
994  .type = AVMEDIA_TYPE_VIDEO,
996  .priv_data_size = sizeof(MpegEncContext),
997  .init = encode_init,
998  .encode2 = ff_MPV_encode_picture,
1000  .supported_framerates = ff_mpeg12_frame_rate_tab + 1,
1001  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P,
1002  AV_PIX_FMT_NONE },
1003  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
1004  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
1005  .priv_class = &mpeg1_class,
1006 };
1007 
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,
1016  .supported_framerates = ff_mpeg2_frame_rate_tab,
1017  .pix_fmts = (const enum AVPixelFormat[]){
1019  },
1020  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
1021  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
1022  .priv_class = &mpeg2_class,
1023 };