FFmpeg
 All Data Structures Namespaces 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 <stdint.h>
29 
30 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/log.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/timecode.h"
35 #include "libavutil/stereo3d.h"
36 
37 #include "avcodec.h"
38 #include "bytestream.h"
39 #include "mathops.h"
40 #include "mpeg12.h"
41 #include "mpeg12data.h"
42 #include "mpegvideo.h"
43 
44 
45 static const uint8_t inv_non_linear_qscale[] = {
46  0, 2, 4, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16,
47 };
48 
50  0x10, 0x0E, 0x00, 0x80, 0x81, 0x00, 0x80,
51  0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
52 };
53 
54 static uint8_t mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
55 static uint8_t fcode_tab[MAX_MV * 2 + 1];
56 
57 static uint8_t uni_mpeg1_ac_vlc_len[64 * 64 * 2];
58 static uint8_t uni_mpeg2_ac_vlc_len[64 * 64 * 2];
59 
60 /* simple include everything table for dc, first byte is bits
61  * number next 3 are code */
62 static uint32_t mpeg1_lum_dc_uni[512];
63 static uint32_t mpeg1_chr_dc_uni[512];
64 
65 static uint8_t mpeg1_index_run[2][64];
66 static int8_t mpeg1_max_level[2][64];
67 
68 static av_cold void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len)
69 {
70  int i;
71 
72  for (i = 0; i < 128; i++) {
73  int level = i - 64;
74  int run;
75  if (!level)
76  continue;
77  for (run = 0; run < 64; run++) {
78  int len, code;
79  int alevel = FFABS(level);
80 
81  if (alevel > rl->max_level[0][run])
82  code = 111; /* rl->n */
83  else
84  code = rl->index_run[0][run] + alevel - 1;
85 
86  if (code < 111) { /* rl->n */
87  /* length of VLC and sign */
88  len = rl->table_vlc[code][1] + 1;
89  } else {
90  len = rl->table_vlc[111 /* rl->n */][1] + 6;
91 
92  if (alevel < 128)
93  len += 8;
94  else
95  len += 16;
96  }
97 
98  uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
99  }
100  }
101 }
102 
104 {
105  int i;
106  AVRational bestq = (AVRational) {0, 0};
107  AVRational ext;
108  AVRational target = av_inv_q(s->avctx->time_base);
109 
110  for (i = 1; i < 14; i++) {
112  i >= 9)
113  break;
114 
115  for (ext.num=1; ext.num <= 4; ext.num++) {
116  for (ext.den=1; ext.den <= 32; ext.den++) {
118 
119  if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO && (ext.den!=1 || ext.num!=1))
120  continue;
121  if (av_gcd(ext.den, ext.num) != 1)
122  continue;
123 
124  if ( bestq.num==0
125  || av_nearer_q(target, bestq, q) < 0
126  || ext.num==1 && ext.den==1 && av_nearer_q(target, bestq, q) == 0) {
127  bestq = q;
128  s->frame_rate_index = i;
129  s->mpeg2_frame_rate_ext.num = ext.num;
130  s->mpeg2_frame_rate_ext.den = ext.den;
131  }
132  }
133  }
134  }
135 
136  if (av_cmp_q(target, bestq))
137  return -1;
138  else
139  return 0;
140 }
141 
143 {
144  MpegEncContext *s = avctx->priv_data;
145 
146  if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && avctx->height > 2800)
147  avctx->thread_count = 1;
148 
149  if (ff_MPV_encode_init(avctx) < 0)
150  return -1;
151 
152  if (find_frame_rate_index(s) < 0) {
154  av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n",
155  avctx->time_base.den, avctx->time_base.num);
156  return -1;
157  } else {
158  av_log(avctx, AV_LOG_INFO,
159  "MPEG1/2 does not support %d/%d fps, there may be AV sync issues\n",
160  avctx->time_base.den, avctx->time_base.num);
161  }
162  }
163 
164  if (avctx->profile == FF_PROFILE_UNKNOWN) {
165  if (avctx->level != FF_LEVEL_UNKNOWN) {
166  av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
167  return -1;
168  }
169  /* Main or 4:2:2 */
170  avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0;
171  }
172 
173  if (avctx->level == FF_LEVEL_UNKNOWN) {
174  if (avctx->profile == 0) { /* 4:2:2 */
175  if (avctx->width <= 720 && avctx->height <= 608)
176  avctx->level = 5; /* Main */
177  else
178  avctx->level = 2; /* High */
179  } else {
180  if (avctx->profile != 1 && s->chroma_format != CHROMA_420) {
181  av_log(avctx, AV_LOG_ERROR,
182  "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
183  return -1;
184  }
185  if (avctx->width <= 720 && avctx->height <= 576)
186  avctx->level = 8; /* Main */
187  else if (avctx->width <= 1440)
188  avctx->level = 6; /* High 1440 */
189  else
190  avctx->level = 4; /* High */
191  }
192  }
193 
194  if ((avctx->width & 0xFFF) == 0 && (avctx->height & 0xFFF) == 1) {
195  av_log(avctx, AV_LOG_ERROR, "Width / Height is invalid for MPEG2\n");
196  return AVERROR(EINVAL);
197  }
198 
200  if ((avctx->width & 0xFFF) == 0 || (avctx->height & 0xFFF) == 0) {
201  av_log(avctx, AV_LOG_ERROR, "Width or Height are not allowed to be multiplies of 4096\n"
202  "add '-strict %d' if you want to use them anyway.\n", FF_COMPLIANCE_UNOFFICIAL);
203  return AVERROR(EINVAL);
204  }
205  }
206 
208  if (s->drop_frame_timecode)
210  if (s->drop_frame_timecode && s->frame_rate_index != 4) {
211  av_log(avctx, AV_LOG_ERROR,
212  "Drop frame time code only allowed with 1001/30000 fps\n");
213  return -1;
214  }
215 
216  if (s->tc_opt_str) {
218  int ret = av_timecode_init_from_string(&s->tc, rate, s->tc_opt_str, s);
219  if (ret < 0)
220  return ret;
223  } else {
224  s->avctx->timecode_frame_start = 0; // default is -1
225  }
226  return 0;
227 }
228 
229 static void put_header(MpegEncContext *s, int header)
230 {
232  put_bits(&s->pb, 16, header >> 16);
233  put_sbits(&s->pb, 16, header);
234 }
235 
236 /* put sequence header if needed */
238 {
239  unsigned int vbv_buffer_size, fps, v;
240  int i, constraint_parameter_flag;
241  uint64_t time_code;
242  float best_aspect_error = 1E10;
243  float aspect_ratio = av_q2d(s->avctx->sample_aspect_ratio);
244 
245  if (aspect_ratio == 0.0)
246  aspect_ratio = 1.0; // pixel aspect 1.1 (VGA)
247 
248  if (s->current_picture.f.key_frame) {
250 
251  /* mpeg1 header repeated every gop */
253 
254  put_sbits(&s->pb, 12, s->width & 0xFFF);
255  put_sbits(&s->pb, 12, s->height & 0xFFF);
256 
257  for (i = 1; i < 15; i++) {
258  float error = aspect_ratio;
259  if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || i <= 1)
260  error -= 1.0 / ff_mpeg1_aspect[i];
261  else
262  error -= av_q2d(ff_mpeg2_aspect[i]) * s->height / s->width;
263 
264  error = FFABS(error);
265 
266  if (error < best_aspect_error) {
267  best_aspect_error = error;
268  s->aspect_ratio_info = i;
269  }
270  }
271 
272  put_bits(&s->pb, 4, s->aspect_ratio_info);
273  put_bits(&s->pb, 4, s->frame_rate_index);
274 
275  if (s->avctx->rc_max_rate) {
276  v = (s->avctx->rc_max_rate + 399) / 400;
277  if (v > 0x3ffff && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
278  v = 0x3ffff;
279  } else {
280  v = 0x3FFFF;
281  }
282 
283  if (s->avctx->rc_buffer_size)
284  vbv_buffer_size = s->avctx->rc_buffer_size;
285  else
286  /* VBV calculation: Scaled so that a VCD has the proper
287  * VBV size of 40 kilobytes */
288  vbv_buffer_size = ((20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
289  vbv_buffer_size = (vbv_buffer_size + 16383) / 16384;
290 
291  put_sbits(&s->pb, 18, v);
292  put_bits(&s->pb, 1, 1); // marker
293  put_sbits(&s->pb, 10, vbv_buffer_size);
294 
295  constraint_parameter_flag =
296  s->width <= 768 &&
297  s->height <= 576 &&
298  s->mb_width * s->mb_height <= 396 &&
299  s->mb_width * s->mb_height * framerate.num <= 396 * 25 * framerate.den &&
300  framerate.num <= framerate.den * 30 &&
301  s->avctx->me_range &&
302  s->avctx->me_range < 128 &&
303  vbv_buffer_size <= 20 &&
304  v <= 1856000 / 400 &&
306 
307  put_bits(&s->pb, 1, constraint_parameter_flag);
308 
311 
312  if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
314  put_bits(&s->pb, 4, 1); // seq ext
315 
316  put_bits(&s->pb, 1, s->avctx->profile == 0); // escx 1 for 4:2:2 profile
317 
318  put_bits(&s->pb, 3, s->avctx->profile); // profile
319  put_bits(&s->pb, 4, s->avctx->level); // level
320 
321  put_bits(&s->pb, 1, s->progressive_sequence);
322  put_bits(&s->pb, 2, s->chroma_format);
323  put_bits(&s->pb, 2, s->width >> 12);
324  put_bits(&s->pb, 2, s->height >> 12);
325  put_bits(&s->pb, 12, v >> 18); // bitrate ext
326  put_bits(&s->pb, 1, 1); // marker
327  put_bits(&s->pb, 8, vbv_buffer_size >> 10); // vbv buffer ext
328  put_bits(&s->pb, 1, s->low_delay);
329  put_bits(&s->pb, 2, s->mpeg2_frame_rate_ext.num-1); // frame_rate_ext_n
330  put_bits(&s->pb, 5, s->mpeg2_frame_rate_ext.den-1); // frame_rate_ext_d
331  }
332 
334  put_bits(&s->pb, 1, s->drop_frame_timecode); // drop frame flag
335  /* time code: we must convert from the real frame rate to a
336  * fake MPEG frame rate in case of low frame rate */
337  fps = (framerate.num + framerate.den / 2) / framerate.den;
338  time_code = s->current_picture_ptr->f.coded_picture_number +
340 
342 
344  if (s->drop_frame_timecode)
345  time_code = av_timecode_adjust_ntsc_framenum2(time_code, fps);
346 
347  put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
348  put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
349  put_bits(&s->pb, 1, 1);
350  put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
351  put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
352  put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
353  put_bits(&s->pb, 1, 0); // broken link
354  }
355 }
356 
357 static inline void encode_mb_skip_run(MpegEncContext *s, int run)
358 {
359  while (run >= 33) {
360  put_bits(&s->pb, 11, 0x008);
361  run -= 33;
362  }
364  ff_mpeg12_mbAddrIncrTable[run][0]);
365 }
366 
368 {
369  if (s->q_scale_type) {
370  av_assert2(s->qscale >= 1 && s->qscale <= 12);
372  } else {
373  put_bits(&s->pb, 5, s->qscale);
374  }
375 }
376 
378 {
379  if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->height > 2800) {
380  put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127));
381  /* slice_vertical_position_extension */
382  put_bits(&s->pb, 3, s->mb_y >> 7);
383  } else {
385  }
386  put_qscale(s);
387  /* slice extra information */
388  put_bits(&s->pb, 1, 0);
389 }
390 
392 {
393  AVFrameSideData *side_data;
395 
396  /* mpeg1 picture header */
398  /* temporal reference */
399 
400  // RAL: s->picture_number instead of s->fake_picture_number
401  put_bits(&s->pb, 10,
402  (s->picture_number - s->gop_picture_number) & 0x3ff);
403  put_bits(&s->pb, 3, s->pict_type);
404 
405  s->vbv_delay_ptr = s->pb.buf + put_bits_count(&s->pb) / 8;
406  put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
407 
408  // RAL: Forward f_code also needed for B-frames
409  if (s->pict_type == AV_PICTURE_TYPE_P ||
411  put_bits(&s->pb, 1, 0); /* half pel coordinates */
413  put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
414  else
415  put_bits(&s->pb, 3, 7); /* forward_f_code */
416  }
417 
418  // RAL: Backward f_code necessary for B-frames
419  if (s->pict_type == AV_PICTURE_TYPE_B) {
420  put_bits(&s->pb, 1, 0); /* half pel coordinates */
422  put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
423  else
424  put_bits(&s->pb, 3, 7); /* backward_f_code */
425  }
426 
427  put_bits(&s->pb, 1, 0); /* extra bit picture */
428 
429  s->frame_pred_frame_dct = 1;
430  if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
432  put_bits(&s->pb, 4, 8); /* pic ext */
433  if (s->pict_type == AV_PICTURE_TYPE_P ||
435  put_bits(&s->pb, 4, s->f_code);
436  put_bits(&s->pb, 4, s->f_code);
437  } else {
438  put_bits(&s->pb, 8, 255);
439  }
440  if (s->pict_type == AV_PICTURE_TYPE_B) {
441  put_bits(&s->pb, 4, s->b_code);
442  put_bits(&s->pb, 4, s->b_code);
443  } else {
444  put_bits(&s->pb, 8, 255);
445  }
446  put_bits(&s->pb, 2, s->intra_dc_precision);
447 
449  put_bits(&s->pb, 2, s->picture_structure);
450  if (s->progressive_sequence)
451  put_bits(&s->pb, 1, 0); /* no repeat */
452  else
454  /* XXX: optimize the generation of this flag with entropy measures */
456 
457  put_bits(&s->pb, 1, s->frame_pred_frame_dct);
459  put_bits(&s->pb, 1, s->q_scale_type);
460  put_bits(&s->pb, 1, s->intra_vlc_format);
461  put_bits(&s->pb, 1, s->alternate_scan);
462  put_bits(&s->pb, 1, s->repeat_first_field);
464  /* chroma_420_type */
465  put_bits(&s->pb, 1, s->chroma_format ==
466  CHROMA_420 ? s->progressive_frame : 0);
467  put_bits(&s->pb, 1, s->progressive_frame);
468  put_bits(&s->pb, 1, 0); /* composite_display_flag */
469  }
470  if (s->scan_offset) {
471  int i;
472 
474  for (i = 0; i < sizeof(svcd_scan_offset_placeholder); i++)
476  }
479  if (side_data) {
480  AVStereo3D *stereo = (AVStereo3D *)side_data->data;
481  uint8_t fpa_type;
482 
483  switch (stereo->type) {
485  fpa_type = 0x03;
486  break;
488  fpa_type = 0x04;
489  break;
490  case AV_STEREO3D_2D:
491  fpa_type = 0x08;
492  break;
494  fpa_type = 0x23;
495  break;
496  default:
497  fpa_type = 0;
498  break;
499  }
500 
501  if (fpa_type != 0) {
503  put_bits(&s->pb, 8, 'J'); // S3D_video_format_signaling_identifier
504  put_bits(&s->pb, 8, 'P');
505  put_bits(&s->pb, 8, '3');
506  put_bits(&s->pb, 8, 'D');
507  put_bits(&s->pb, 8, 0x03); // S3D_video_format_length
508 
509  put_bits(&s->pb, 1, 1); // reserved_bit
510  put_bits(&s->pb, 7, fpa_type); // S3D_video_format_type
511  put_bits(&s->pb, 8, 0x04); // reserved_data[0]
512  put_bits(&s->pb, 8, 0xFF); // reserved_data[1]
513  }
514  }
515 
516  s->mb_y = 0;
518 }
519 
520 static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
521  int has_mv, int field_motion)
522 {
523  put_bits(&s->pb, n, bits);
524  if (!s->frame_pred_frame_dct) {
525  if (has_mv)
526  /* motion_type: frame/field */
527  put_bits(&s->pb, 2, 2 - field_motion);
528  put_bits(&s->pb, 1, s->interlaced_dct);
529  }
530 }
531 
532 // RAL: Parameter added: f_or_b_code
533 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
534 {
535  if (val == 0) {
536  /* zero vector */
537  put_bits(&s->pb,
540  } else {
541  int code, sign, bits;
542  int bit_size = f_or_b_code - 1;
543  int range = 1 << bit_size;
544  /* modulo encoding */
545  val = sign_extend(val, 5 + bit_size);
546 
547  if (val >= 0) {
548  val--;
549  code = (val >> bit_size) + 1;
550  bits = val & (range - 1);
551  sign = 0;
552  } else {
553  val = -val;
554  val--;
555  code = (val >> bit_size) + 1;
556  bits = val & (range - 1);
557  sign = 1;
558  }
559 
560  av_assert2(code > 0 && code <= 16);
561 
562  put_bits(&s->pb,
565 
566  put_bits(&s->pb, 1, sign);
567  if (bit_size > 0)
568  put_bits(&s->pb, bit_size, bits);
569  }
570 }
571 
572 static inline void encode_dc(MpegEncContext *s, int diff, int component)
573 {
574  if (((unsigned) (diff + 255)) >= 511) {
575  int index;
576 
577  if (diff < 0) {
578  index = av_log2_16bit(-2 * diff);
579  diff--;
580  } else {
581  index = av_log2_16bit(2 * diff);
582  }
583  if (component == 0)
584  put_bits(&s->pb,
585  ff_mpeg12_vlc_dc_lum_bits[index] + index,
586  (ff_mpeg12_vlc_dc_lum_code[index] << index) +
587  (diff & ((1 << index) - 1)));
588  else
589  put_bits(&s->pb,
590  ff_mpeg12_vlc_dc_chroma_bits[index] + index,
591  (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
592  (diff & ((1 << index) - 1)));
593  } else {
594  if (component == 0)
595  put_bits(&s->pb,
596  mpeg1_lum_dc_uni[diff + 255] & 0xFF,
597  mpeg1_lum_dc_uni[diff + 255] >> 8);
598  else
599  put_bits(&s->pb,
600  mpeg1_chr_dc_uni[diff + 255] & 0xFF,
601  mpeg1_chr_dc_uni[diff + 255] >> 8);
602  }
603 }
604 
605 static void mpeg1_encode_block(MpegEncContext *s, int16_t *block, int n)
606 {
607  int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
608  int code, component;
609  const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
610 
611  last_index = s->block_last_index[n];
612 
613  /* DC coef */
614  if (s->mb_intra) {
615  component = (n <= 3 ? 0 : (n & 1) + 1);
616  dc = block[0]; /* overflow is impossible */
617  diff = dc - s->last_dc[component];
618  encode_dc(s, diff, component);
619  s->last_dc[component] = dc;
620  i = 1;
621  if (s->intra_vlc_format)
622  table_vlc = ff_rl_mpeg2.table_vlc;
623  } else {
624  /* encode the first coefficient: needs to be done here because
625  * it is handled slightly differently */
626  level = block[0];
627  if (abs(level) == 1) {
628  code = ((uint32_t)level >> 31); /* the sign bit */
629  put_bits(&s->pb, 2, code | 0x02);
630  i = 1;
631  } else {
632  i = 0;
633  last_non_zero = -1;
634  goto next_coef;
635  }
636  }
637 
638  /* now quantify & encode AC coefs */
639  last_non_zero = i - 1;
640 
641  for (; i <= last_index; i++) {
642  j = s->intra_scantable.permutated[i];
643  level = block[j];
644 
645 next_coef:
646  /* encode using VLC */
647  if (level != 0) {
648  run = i - last_non_zero - 1;
649 
650  alevel = level;
651  MASK_ABS(sign, alevel);
652  sign &= 1;
653 
654  if (alevel <= mpeg1_max_level[0][run]) {
655  code = mpeg1_index_run[0][run] + alevel - 1;
656  /* store the VLC & sign at once */
657  put_bits(&s->pb, table_vlc[code][1] + 1,
658  (table_vlc[code][0] << 1) + sign);
659  } else {
660  /* escape seems to be pretty rare <5% so I do not optimize it */
661  put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
662  /* escape: only clip in this case */
663  put_bits(&s->pb, 6, run);
664  if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
665  if (alevel < 128) {
666  put_sbits(&s->pb, 8, level);
667  } else {
668  if (level < 0)
669  put_bits(&s->pb, 16, 0x8001 + level + 255);
670  else
671  put_sbits(&s->pb, 16, level);
672  }
673  } else {
674  put_sbits(&s->pb, 12, level);
675  }
676  }
677  last_non_zero = i;
678  }
679  }
680  /* end of block */
681  put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
682 }
683 
685  int16_t block[6][64],
686  int motion_x, int motion_y,
687  int mb_block_count)
688 {
689  int i, cbp;
690  const int mb_x = s->mb_x;
691  const int mb_y = s->mb_y;
692  const int first_mb = mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
693 
694  /* compute cbp */
695  cbp = 0;
696  for (i = 0; i < mb_block_count; i++)
697  if (s->block_last_index[i] >= 0)
698  cbp |= 1 << (mb_block_count - 1 - i);
699 
700  if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
701  (mb_x != s->mb_width - 1 ||
702  (mb_y != s->end_mb_y - 1 && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)) &&
703  ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) ||
704  (s->pict_type == AV_PICTURE_TYPE_B && s->mv_dir == s->last_mv_dir &&
705  (((s->mv_dir & MV_DIR_FORWARD)
706  ? ((s->mv[0][0][0] - s->last_mv[0][0][0]) |
707  (s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
708  ((s->mv_dir & MV_DIR_BACKWARD)
709  ? ((s->mv[1][0][0] - s->last_mv[1][0][0]) |
710  (s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
711  s->mb_skip_run++;
712  s->qscale -= s->dquant;
713  s->skip_count++;
714  s->misc_bits++;
715  s->last_bits++;
716  if (s->pict_type == AV_PICTURE_TYPE_P) {
717  s->last_mv[0][0][0] =
718  s->last_mv[0][0][1] =
719  s->last_mv[0][1][0] =
720  s->last_mv[0][1][1] = 0;
721  }
722  } else {
723  if (first_mb) {
724  av_assert0(s->mb_skip_run == 0);
725  encode_mb_skip_run(s, s->mb_x);
726  } else {
728  }
729 
730  if (s->pict_type == AV_PICTURE_TYPE_I) {
731  if (s->dquant && cbp) {
732  /* macroblock_type: macroblock_quant = 1 */
733  put_mb_modes(s, 2, 1, 0, 0);
734  put_qscale(s);
735  } else {
736  /* macroblock_type: macroblock_quant = 0 */
737  put_mb_modes(s, 1, 1, 0, 0);
738  s->qscale -= s->dquant;
739  }
740  s->misc_bits += get_bits_diff(s);
741  s->i_count++;
742  } else if (s->mb_intra) {
743  if (s->dquant && cbp) {
744  put_mb_modes(s, 6, 0x01, 0, 0);
745  put_qscale(s);
746  } else {
747  put_mb_modes(s, 5, 0x03, 0, 0);
748  s->qscale -= s->dquant;
749  }
750  s->misc_bits += get_bits_diff(s);
751  s->i_count++;
752  memset(s->last_mv, 0, sizeof(s->last_mv));
753  } else if (s->pict_type == AV_PICTURE_TYPE_P) {
754  if (s->mv_type == MV_TYPE_16X16) {
755  if (cbp != 0) {
756  if ((motion_x | motion_y) == 0) {
757  if (s->dquant) {
758  /* macroblock_pattern & quant */
759  put_mb_modes(s, 5, 1, 0, 0);
760  put_qscale(s);
761  } else {
762  /* macroblock_pattern only */
763  put_mb_modes(s, 2, 1, 0, 0);
764  }
765  s->misc_bits += get_bits_diff(s);
766  } else {
767  if (s->dquant) {
768  put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
769  put_qscale(s);
770  } else {
771  put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
772  }
773  s->misc_bits += get_bits_diff(s);
774  // RAL: f_code parameter added
776  motion_x - s->last_mv[0][0][0],
777  s->f_code);
778  // RAL: f_code parameter added
780  motion_y - s->last_mv[0][0][1],
781  s->f_code);
782  s->mv_bits += get_bits_diff(s);
783  }
784  } else {
785  put_bits(&s->pb, 3, 1); /* motion only */
786  if (!s->frame_pred_frame_dct)
787  put_bits(&s->pb, 2, 2); /* motion_type: frame */
788  s->misc_bits += get_bits_diff(s);
789  // RAL: f_code parameter added
791  motion_x - s->last_mv[0][0][0],
792  s->f_code);
793  // RAL: f_code parameter added
795  motion_y - s->last_mv[0][0][1],
796  s->f_code);
797  s->qscale -= s->dquant;
798  s->mv_bits += get_bits_diff(s);
799  }
800  s->last_mv[0][1][0] = s->last_mv[0][0][0] = motion_x;
801  s->last_mv[0][1][1] = s->last_mv[0][0][1] = motion_y;
802  } else {
804 
805  if (cbp) {
806  if (s->dquant) {
807  put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
808  put_qscale(s);
809  } else {
810  put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
811  }
812  } else {
813  put_bits(&s->pb, 3, 1); /* motion only */
814  put_bits(&s->pb, 2, 1); /* motion_type: field */
815  s->qscale -= s->dquant;
816  }
817  s->misc_bits += get_bits_diff(s);
818  for (i = 0; i < 2; i++) {
819  put_bits(&s->pb, 1, s->field_select[0][i]);
821  s->mv[0][i][0] - s->last_mv[0][i][0],
822  s->f_code);
824  s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
825  s->f_code);
826  s->last_mv[0][i][0] = s->mv[0][i][0];
827  s->last_mv[0][i][1] = 2 * s->mv[0][i][1];
828  }
829  s->mv_bits += get_bits_diff(s);
830  }
831  if (cbp) {
832  if (s->chroma_y_shift) {
833  put_bits(&s->pb,
834  ff_mpeg12_mbPatTable[cbp][1],
835  ff_mpeg12_mbPatTable[cbp][0]);
836  } else {
837  put_bits(&s->pb,
838  ff_mpeg12_mbPatTable[cbp >> 2][1],
839  ff_mpeg12_mbPatTable[cbp >> 2][0]);
840  put_sbits(&s->pb, 2, cbp);
841  }
842  }
843  s->f_count++;
844  } else {
845  if (s->mv_type == MV_TYPE_16X16) {
846  if (cbp) { // With coded bloc pattern
847  if (s->dquant) {
848  if (s->mv_dir == MV_DIR_FORWARD)
849  put_mb_modes(s, 6, 3, 1, 0);
850  else
851  put_mb_modes(s, 8 - s->mv_dir, 2, 1, 0);
852  put_qscale(s);
853  } else {
854  put_mb_modes(s, 5 - s->mv_dir, 3, 1, 0);
855  }
856  } else { // No coded bloc pattern
857  put_bits(&s->pb, 5 - s->mv_dir, 2);
858  if (!s->frame_pred_frame_dct)
859  put_bits(&s->pb, 2, 2); /* motion_type: frame */
860  s->qscale -= s->dquant;
861  }
862  s->misc_bits += get_bits_diff(s);
863  if (s->mv_dir & MV_DIR_FORWARD) {
865  s->mv[0][0][0] - s->last_mv[0][0][0],
866  s->f_code);
868  s->mv[0][0][1] - s->last_mv[0][0][1],
869  s->f_code);
870  s->last_mv[0][0][0] =
871  s->last_mv[0][1][0] = s->mv[0][0][0];
872  s->last_mv[0][0][1] =
873  s->last_mv[0][1][1] = s->mv[0][0][1];
874  s->f_count++;
875  }
876  if (s->mv_dir & MV_DIR_BACKWARD) {
878  s->mv[1][0][0] - s->last_mv[1][0][0],
879  s->b_code);
881  s->mv[1][0][1] - s->last_mv[1][0][1],
882  s->b_code);
883  s->last_mv[1][0][0] =
884  s->last_mv[1][1][0] = s->mv[1][0][0];
885  s->last_mv[1][0][1] =
886  s->last_mv[1][1][1] = s->mv[1][0][1];
887  s->b_count++;
888  }
889  } else {
892  if (cbp) { // With coded bloc pattern
893  if (s->dquant) {
894  if (s->mv_dir == MV_DIR_FORWARD)
895  put_mb_modes(s, 6, 3, 1, 1);
896  else
897  put_mb_modes(s, 8 - s->mv_dir, 2, 1, 1);
898  put_qscale(s);
899  } else {
900  put_mb_modes(s, 5 - s->mv_dir, 3, 1, 1);
901  }
902  } else { // No coded bloc pattern
903  put_bits(&s->pb, 5 - s->mv_dir, 2);
904  put_bits(&s->pb, 2, 1); /* motion_type: field */
905  s->qscale -= s->dquant;
906  }
907  s->misc_bits += get_bits_diff(s);
908  if (s->mv_dir & MV_DIR_FORWARD) {
909  for (i = 0; i < 2; i++) {
910  put_bits(&s->pb, 1, s->field_select[0][i]);
912  s->mv[0][i][0] - s->last_mv[0][i][0],
913  s->f_code);
915  s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
916  s->f_code);
917  s->last_mv[0][i][0] = s->mv[0][i][0];
918  s->last_mv[0][i][1] = s->mv[0][i][1] * 2;
919  }
920  s->f_count++;
921  }
922  if (s->mv_dir & MV_DIR_BACKWARD) {
923  for (i = 0; i < 2; i++) {
924  put_bits(&s->pb, 1, s->field_select[1][i]);
926  s->mv[1][i][0] - s->last_mv[1][i][0],
927  s->b_code);
929  s->mv[1][i][1] - (s->last_mv[1][i][1] >> 1),
930  s->b_code);
931  s->last_mv[1][i][0] = s->mv[1][i][0];
932  s->last_mv[1][i][1] = s->mv[1][i][1] * 2;
933  }
934  s->b_count++;
935  }
936  }
937  s->mv_bits += get_bits_diff(s);
938  if (cbp) {
939  if (s->chroma_y_shift) {
940  put_bits(&s->pb,
941  ff_mpeg12_mbPatTable[cbp][1],
942  ff_mpeg12_mbPatTable[cbp][0]);
943  } else {
944  put_bits(&s->pb,
945  ff_mpeg12_mbPatTable[cbp >> 2][1],
946  ff_mpeg12_mbPatTable[cbp >> 2][0]);
947  put_sbits(&s->pb, 2, cbp);
948  }
949  }
950  }
951  for (i = 0; i < mb_block_count; i++)
952  if (cbp & (1 << (mb_block_count - 1 - i)))
953  mpeg1_encode_block(s, block[i], i);
954  s->mb_skip_run = 0;
955  if (s->mb_intra)
956  s->i_tex_bits += get_bits_diff(s);
957  else
958  s->p_tex_bits += get_bits_diff(s);
959  }
960 }
961 
962 void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[6][64],
963  int motion_x, int motion_y)
964 {
965  if (s->chroma_format == CHROMA_420)
966  mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
967  else
968  mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
969 }
970 
972 {
973  static int done = 0;
974 
976 
977  if (!done) {
978  int f_code;
979  int mv;
980  int i;
981 
982  done = 1;
985 
986  for (i = 0; i < 64; i++) {
987  mpeg1_max_level[0][i] = ff_rl_mpeg1.max_level[0][i];
988  mpeg1_index_run[0][i] = ff_rl_mpeg1.index_run[0][i];
989  }
990 
992  if (s->intra_vlc_format)
994 
995  /* build unified dc encoding tables */
996  for (i = -255; i < 256; i++) {
997  int adiff, index;
998  int bits, code;
999  int diff = i;
1000 
1001  adiff = FFABS(diff);
1002  if (diff < 0)
1003  diff--;
1004  index = av_log2(2 * adiff);
1005 
1007  code = (ff_mpeg12_vlc_dc_lum_code[index] << index) +
1008  (diff & ((1 << index) - 1));
1009  mpeg1_lum_dc_uni[i + 255] = bits + (code << 8);
1010 
1013  (diff & ((1 << index) - 1));
1014  mpeg1_chr_dc_uni[i + 255] = bits + (code << 8);
1015  }
1016 
1017  for (f_code = 1; f_code <= MAX_FCODE; f_code++)
1018  for (mv = -MAX_MV; mv <= MAX_MV; mv++) {
1019  int len;
1020 
1021  if (mv == 0) {
1022  len = ff_mpeg12_mbMotionVectorTable[0][1];
1023  } else {
1024  int val, bit_size, code;
1025 
1026  bit_size = f_code - 1;
1027 
1028  val = mv;
1029  if (val < 0)
1030  val = -val;
1031  val--;
1032  code = (val >> bit_size) + 1;
1033  if (code < 17)
1034  len = ff_mpeg12_mbMotionVectorTable[code][1] +
1035  1 + bit_size;
1036  else
1037  len = ff_mpeg12_mbMotionVectorTable[16][1] +
1038  2 + bit_size;
1039  }
1040 
1041  mv_penalty[f_code][mv + MAX_MV] = len;
1042  }
1043 
1044 
1045  for (f_code = MAX_FCODE; f_code > 0; f_code--)
1046  for (mv = -(8 << f_code); mv < (8 << f_code); mv++)
1047  fcode_tab[mv + MAX_MV] = f_code;
1048  }
1049  s->me.mv_penalty = mv_penalty;
1050  s->fcode_tab = fcode_tab;
1051  if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1052  s->min_qcoeff = -255;
1053  s->max_qcoeff = 255;
1054  } else {
1055  s->min_qcoeff = -2047;
1056  s->max_qcoeff = 2047;
1057  }
1058  if (s->intra_vlc_format) {
1059  s->intra_ac_vlc_length =
1061  } else {
1062  s->intra_ac_vlc_length =
1064  }
1065  s->inter_ac_vlc_length =
1067 }
1068 
1069 #define OFFSET(x) offsetof(MpegEncContext, x)
1070 #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
1071 #define COMMON_OPTS \
1072  { "gop_timecode", "MPEG GOP Timecode in hh:mm:ss[:;.]ff format", \
1073  OFFSET(tc_opt_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, VE },\
1074  { "intra_vlc", "Use MPEG-2 intra VLC table.", \
1075  OFFSET(intra_vlc_format), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \
1076  { "drop_frame_timecode", "Timecode is in drop frame format.", \
1077  OFFSET(drop_frame_timecode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \
1078  { "scan_offset", "Reserve space for SVCD scan offset user data.", \
1079  OFFSET(scan_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1080 
1081 static const AVOption mpeg1_options[] = {
1082  COMMON_OPTS
1084  { NULL },
1085 };
1086 
1087 static const AVOption mpeg2_options[] = {
1088  COMMON_OPTS
1089  { "non_linear_quant", "Use nonlinear quantizer.", OFFSET(q_scale_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1090  { "alternate_scan", "Enable alternate scantable.", OFFSET(alternate_scan), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1092  { NULL },
1093 };
1094 
1095 #define mpeg12_class(x) \
1096 static const AVClass mpeg ## x ## _class = { \
1097  .class_name = "mpeg" # x "video encoder", \
1098  .item_name = av_default_item_name, \
1099  .option = mpeg ## x ## _options, \
1100  .version = LIBAVUTIL_VERSION_INT, \
1101 };
1102 
1104 mpeg12_class(2)
1105 
1106 AVCodec ff_mpeg1video_encoder = {
1107  .name = "mpeg1video",
1108  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
1109  .type = AVMEDIA_TYPE_VIDEO,
1110  .id = AV_CODEC_ID_MPEG1VIDEO,
1111  .priv_data_size = sizeof(MpegEncContext),
1112  .init = encode_init,
1113  .encode2 = ff_MPV_encode_picture,
1115  .supported_framerates = ff_mpeg12_frame_rate_tab + 1,
1116  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
1117  AV_PIX_FMT_NONE },
1118  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
1119  .priv_class = &mpeg1_class,
1120 };
1121 
1123  .name = "mpeg2video",
1124  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
1125  .type = AVMEDIA_TYPE_VIDEO,
1126  .id = AV_CODEC_ID_MPEG2VIDEO,
1127  .priv_data_size = sizeof(MpegEncContext),
1128  .init = encode_init,
1129  .encode2 = ff_MPV_encode_picture,
1131  .supported_framerates = ff_mpeg2_frame_rate_tab,
1132  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
1134  AV_PIX_FMT_NONE },
1135  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
1136  .priv_class = &mpeg2_class,
1137 };