FFmpeg  2.6.9
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules 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 "mpegutils.h"
43 #include "mpegvideo.h"
44 
45 
46 static const uint8_t inv_non_linear_qscale[] = {
47  0, 2, 4, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16,
48 };
49 
51  0x10, 0x0E, 0x00, 0x80, 0x81, 0x00, 0x80,
52  0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
53 };
54 
55 static uint8_t mv_penalty[MAX_FCODE + 1][MAX_DMV * 2 + 1];
56 static uint8_t fcode_tab[MAX_MV * 2 + 1];
57 
58 static uint8_t uni_mpeg1_ac_vlc_len[64 * 64 * 2];
59 static uint8_t uni_mpeg2_ac_vlc_len[64 * 64 * 2];
60 
61 /* simple include everything table for dc, first byte is bits
62  * number next 3 are code */
63 static uint32_t mpeg1_lum_dc_uni[512];
64 static uint32_t mpeg1_chr_dc_uni[512];
65 
66 static uint8_t mpeg1_index_run[2][64];
67 static int8_t mpeg1_max_level[2][64];
68 
70 {
71  int i;
72 
73  for (i = 0; i < 128; i++) {
74  int level = i - 64;
75  int run;
76  if (!level)
77  continue;
78  for (run = 0; run < 64; run++) {
79  int len, code;
80  int alevel = FFABS(level);
81 
82  if (alevel > rl->max_level[0][run])
83  code = 111; /* rl->n */
84  else
85  code = rl->index_run[0][run] + alevel - 1;
86 
87  if (code < 111) { /* rl->n */
88  /* length of VLC and sign */
89  len = rl->table_vlc[code][1] + 1;
90  } else {
91  len = rl->table_vlc[111 /* rl->n */][1] + 6;
92 
93  if (alevel < 128)
94  len += 8;
95  else
96  len += 16;
97  }
98 
99  uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
100  }
101  }
102 }
103 
105 {
106  int i;
107  AVRational bestq = (AVRational) {0, 0};
108  AVRational ext;
109  AVRational target = av_inv_q(s->avctx->time_base);
110 
111  for (i = 1; i < 14; i++) {
113  i >= 9)
114  break;
115 
116  for (ext.num=1; ext.num <= 4; ext.num++) {
117  for (ext.den=1; ext.den <= 32; ext.den++) {
119 
120  if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO && (ext.den!=1 || ext.num!=1))
121  continue;
122  if (av_gcd(ext.den, ext.num) != 1)
123  continue;
124 
125  if ( bestq.num==0
126  || av_nearer_q(target, bestq, q) < 0
127  || ext.num==1 && ext.den==1 && av_nearer_q(target, bestq, q) == 0) {
128  bestq = q;
129  s->frame_rate_index = i;
130  s->mpeg2_frame_rate_ext.num = ext.num;
131  s->mpeg2_frame_rate_ext.den = ext.den;
132  }
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 (ff_mpv_encode_init(avctx) < 0)
148  return -1;
149 
150  if (find_frame_rate_index(s) < 0) {
152  av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n",
153  avctx->time_base.den, avctx->time_base.num);
154  return -1;
155  } else {
156  av_log(avctx, AV_LOG_INFO,
157  "MPEG1/2 does not support %d/%d fps, there may be AV sync issues\n",
158  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  /* Main or 4:2:2 */
168  avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0;
169  }
170 
171  if (avctx->level == FF_LEVEL_UNKNOWN) {
172  if (avctx->profile == 0) { /* 4:2:2 */
173  if (avctx->width <= 720 && avctx->height <= 608)
174  avctx->level = 5; /* Main */
175  else
176  avctx->level = 2; /* High */
177  } else {
178  if (avctx->profile != 1 && s->chroma_format != CHROMA_420) {
179  av_log(avctx, AV_LOG_ERROR,
180  "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
181  return -1;
182  }
183  if (avctx->width <= 720 && avctx->height <= 576)
184  avctx->level = 8; /* Main */
185  else if (avctx->width <= 1440)
186  avctx->level = 6; /* High 1440 */
187  else
188  avctx->level = 4; /* High */
189  }
190  }
191 
192  if ((avctx->width & 0xFFF) == 0 && (avctx->height & 0xFFF) == 1) {
193  av_log(avctx, AV_LOG_ERROR, "Width / Height is invalid for MPEG2\n");
194  return AVERROR(EINVAL);
195  }
196 
198  if ((avctx->width & 0xFFF) == 0 || (avctx->height & 0xFFF) == 0) {
199  av_log(avctx, AV_LOG_ERROR, "Width or Height are not allowed to be multiples of 4096\n"
200  "add '-strict %d' if you want to use them anyway.\n", FF_COMPLIANCE_UNOFFICIAL);
201  return AVERROR(EINVAL);
202  }
203  }
204 
206  if (s->drop_frame_timecode)
208  if (s->drop_frame_timecode && s->frame_rate_index != 4) {
209  av_log(avctx, AV_LOG_ERROR,
210  "Drop frame time code only allowed with 1001/30000 fps\n");
211  return -1;
212  }
213 
214  if (s->tc_opt_str) {
216  int ret = av_timecode_init_from_string(&s->tc, rate, s->tc_opt_str, s);
217  if (ret < 0)
218  return ret;
221  } else {
222  s->avctx->timecode_frame_start = 0; // default is -1
223  }
224  return 0;
225 }
226 
227 static void put_header(MpegEncContext *s, int header)
228 {
230  put_bits(&s->pb, 16, header >> 16);
231  put_sbits(&s->pb, 16, header);
232 }
233 
234 /* put sequence header if needed */
236 {
237  unsigned int vbv_buffer_size, fps, v;
238  int i, constraint_parameter_flag;
239  uint64_t time_code;
240  int64_t best_aspect_error = INT64_MAX;
241  AVRational aspect_ratio = s->avctx->sample_aspect_ratio;
242 
243  if (aspect_ratio.num == 0 || aspect_ratio.den == 0)
244  aspect_ratio = (AVRational){1,1}; // pixel aspect 1.1 (VGA)
245 
246  if (s->current_picture.f->key_frame) {
248 
249  /* mpeg1 header repeated every gop */
251 
252  put_sbits(&s->pb, 12, s->width & 0xFFF);
253  put_sbits(&s->pb, 12, s->height & 0xFFF);
254 
255  for (i = 1; i < 15; i++) {
256  int64_t error = aspect_ratio.num * (1LL<<32) / aspect_ratio.den;
257  if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || i <= 1)
258  error -= (1LL<<32) / ff_mpeg1_aspect[i];
259  else
260  error -= (1LL<<32)*ff_mpeg2_aspect[i].num * s->height / s->width / ff_mpeg2_aspect[i].den;
261 
262  error = FFABS(error);
263 
264  if (error - 2 <= best_aspect_error) {
265  best_aspect_error = error;
266  s->aspect_ratio_info = i;
267  }
268  }
269 
270  put_bits(&s->pb, 4, s->aspect_ratio_info);
271  put_bits(&s->pb, 4, s->frame_rate_index);
272 
273  if (s->avctx->rc_max_rate) {
274  v = (s->avctx->rc_max_rate + 399) / 400;
275  if (v > 0x3ffff && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
276  v = 0x3ffff;
277  } else {
278  v = 0x3FFFF;
279  }
280 
281  if (s->avctx->rc_buffer_size)
282  vbv_buffer_size = s->avctx->rc_buffer_size;
283  else
284  /* VBV calculation: Scaled so that a VCD has the proper
285  * VBV size of 40 kilobytes */
286  vbv_buffer_size = ((20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
287  vbv_buffer_size = (vbv_buffer_size + 16383) / 16384;
288 
289  put_sbits(&s->pb, 18, v);
290  put_bits(&s->pb, 1, 1); // marker
291  put_sbits(&s->pb, 10, vbv_buffer_size);
292 
293  constraint_parameter_flag =
294  s->width <= 768 &&
295  s->height <= 576 &&
296  s->mb_width * s->mb_height <= 396 &&
297  s->mb_width * s->mb_height * framerate.num <= 396 * 25 * framerate.den &&
298  framerate.num <= framerate.den * 30 &&
299  s->avctx->me_range &&
300  s->avctx->me_range < 128 &&
301  vbv_buffer_size <= 20 &&
302  v <= 1856000 / 400 &&
304 
305  put_bits(&s->pb, 1, constraint_parameter_flag);
306 
309 
310  if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
311  AVFrameSideData *side_data;
312  int width = s->width;
313  int height = s->height;
314  int use_seq_disp_ext;
315 
317  put_bits(&s->pb, 4, 1); // seq ext
318 
319  put_bits(&s->pb, 1, s->avctx->profile == 0); // escx 1 for 4:2:2 profile
320 
321  put_bits(&s->pb, 3, s->avctx->profile); // profile
322  put_bits(&s->pb, 4, s->avctx->level); // level
323 
324  put_bits(&s->pb, 1, s->progressive_sequence);
325  put_bits(&s->pb, 2, s->chroma_format);
326  put_bits(&s->pb, 2, s->width >> 12);
327  put_bits(&s->pb, 2, s->height >> 12);
328  put_bits(&s->pb, 12, v >> 18); // bitrate ext
329  put_bits(&s->pb, 1, 1); // marker
330  put_bits(&s->pb, 8, vbv_buffer_size >> 10); // vbv buffer ext
331  put_bits(&s->pb, 1, s->low_delay);
332  put_bits(&s->pb, 2, s->mpeg2_frame_rate_ext.num-1); // frame_rate_ext_n
333  put_bits(&s->pb, 5, s->mpeg2_frame_rate_ext.den-1); // frame_rate_ext_d
334 
336  if (side_data) {
337  AVPanScan *pan_scan = (AVPanScan *)side_data->data;
338  if (pan_scan->width && pan_scan->height) {
339  width = pan_scan->width >> 4;
340  height = pan_scan->height >> 4;
341  }
342  }
343 
344  use_seq_disp_ext = (width != s->width ||
345  height != s->height ||
349 
350  if (s->seq_disp_ext == 1 || (s->seq_disp_ext == -1 && use_seq_disp_ext)) {
352  put_bits(&s->pb, 4, 2); // sequence display extension
353  put_bits(&s->pb, 3, 0); // video_format: 0 is components
354  put_bits(&s->pb, 1, 1); // colour_description
355  put_bits(&s->pb, 8, s->avctx->color_primaries); // colour_primaries
356  put_bits(&s->pb, 8, s->avctx->color_trc); // transfer_characteristics
357  put_bits(&s->pb, 8, s->avctx->colorspace); // matrix_coefficients
358  put_bits(&s->pb, 14, width); // display_horizontal_size
359  put_bits(&s->pb, 1, 1); // marker_bit
360  put_bits(&s->pb, 14, height); // display_vertical_size
361  put_bits(&s->pb, 3, 0); // remaining 3 bits are zero padding
362  }
363  }
364 
366  put_bits(&s->pb, 1, s->drop_frame_timecode); // drop frame flag
367  /* time code: we must convert from the real frame rate to a
368  * fake MPEG frame rate in case of low frame rate */
369  fps = (framerate.num + framerate.den / 2) / framerate.den;
370  time_code = s->current_picture_ptr->f->coded_picture_number +
372 
374 
376  if (s->drop_frame_timecode)
377  time_code = av_timecode_adjust_ntsc_framenum2(time_code, fps);
378 
379  put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
380  put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
381  put_bits(&s->pb, 1, 1);
382  put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
383  put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
384  put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP) || s->intra_only || !s->gop_picture_number);
385  put_bits(&s->pb, 1, 0); // broken link
386  }
387 }
388 
389 static inline void encode_mb_skip_run(MpegEncContext *s, int run)
390 {
391  while (run >= 33) {
392  put_bits(&s->pb, 11, 0x008);
393  run -= 33;
394  }
396  ff_mpeg12_mbAddrIncrTable[run][0]);
397 }
398 
400 {
401  if (s->q_scale_type) {
402  av_assert2(s->qscale >= 1 && s->qscale <= 12);
404  } else {
405  put_bits(&s->pb, 5, s->qscale);
406  }
407 }
408 
410 {
411  if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->height > 2800) {
412  put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127));
413  /* slice_vertical_position_extension */
414  put_bits(&s->pb, 3, s->mb_y >> 7);
415  } else {
417  }
418  put_qscale(s);
419  /* slice extra information */
420  put_bits(&s->pb, 1, 0);
421 }
422 
424 {
425  AVFrameSideData *side_data;
427 
428  /* mpeg1 picture header */
430  /* temporal reference */
431 
432  // RAL: s->picture_number instead of s->fake_picture_number
433  put_bits(&s->pb, 10,
434  (s->picture_number - s->gop_picture_number) & 0x3ff);
435  put_bits(&s->pb, 3, s->pict_type);
436 
437  s->vbv_delay_ptr = s->pb.buf + put_bits_count(&s->pb) / 8;
438  put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
439 
440  // RAL: Forward f_code also needed for B-frames
441  if (s->pict_type == AV_PICTURE_TYPE_P ||
443  put_bits(&s->pb, 1, 0); /* half pel coordinates */
445  put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
446  else
447  put_bits(&s->pb, 3, 7); /* forward_f_code */
448  }
449 
450  // RAL: Backward f_code necessary for B-frames
451  if (s->pict_type == AV_PICTURE_TYPE_B) {
452  put_bits(&s->pb, 1, 0); /* half pel coordinates */
454  put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
455  else
456  put_bits(&s->pb, 3, 7); /* backward_f_code */
457  }
458 
459  put_bits(&s->pb, 1, 0); /* extra bit picture */
460 
461  s->frame_pred_frame_dct = 1;
462  if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
464  put_bits(&s->pb, 4, 8); /* pic ext */
465  if (s->pict_type == AV_PICTURE_TYPE_P ||
467  put_bits(&s->pb, 4, s->f_code);
468  put_bits(&s->pb, 4, s->f_code);
469  } else {
470  put_bits(&s->pb, 8, 255);
471  }
472  if (s->pict_type == AV_PICTURE_TYPE_B) {
473  put_bits(&s->pb, 4, s->b_code);
474  put_bits(&s->pb, 4, s->b_code);
475  } else {
476  put_bits(&s->pb, 8, 255);
477  }
478  put_bits(&s->pb, 2, s->intra_dc_precision);
479 
481  put_bits(&s->pb, 2, s->picture_structure);
482  if (s->progressive_sequence)
483  put_bits(&s->pb, 1, 0); /* no repeat */
484  else
486  /* XXX: optimize the generation of this flag with entropy measures */
488 
489  put_bits(&s->pb, 1, s->frame_pred_frame_dct);
491  put_bits(&s->pb, 1, s->q_scale_type);
492  put_bits(&s->pb, 1, s->intra_vlc_format);
493  put_bits(&s->pb, 1, s->alternate_scan);
494  put_bits(&s->pb, 1, s->repeat_first_field);
496  /* chroma_420_type */
497  put_bits(&s->pb, 1, s->chroma_format ==
498  CHROMA_420 ? s->progressive_frame : 0);
499  put_bits(&s->pb, 1, s->progressive_frame);
500  put_bits(&s->pb, 1, 0); /* composite_display_flag */
501  }
502  if (s->scan_offset) {
503  int i;
504 
506  for (i = 0; i < sizeof(svcd_scan_offset_placeholder); i++)
508  }
511  if (side_data) {
512  AVStereo3D *stereo = (AVStereo3D *)side_data->data;
513  uint8_t fpa_type;
514 
515  switch (stereo->type) {
517  fpa_type = 0x03;
518  break;
520  fpa_type = 0x04;
521  break;
522  case AV_STEREO3D_2D:
523  fpa_type = 0x08;
524  break;
526  fpa_type = 0x23;
527  break;
528  default:
529  fpa_type = 0;
530  break;
531  }
532 
533  if (fpa_type != 0) {
535  put_bits(&s->pb, 8, 'J'); // S3D_video_format_signaling_identifier
536  put_bits(&s->pb, 8, 'P');
537  put_bits(&s->pb, 8, '3');
538  put_bits(&s->pb, 8, 'D');
539  put_bits(&s->pb, 8, 0x03); // S3D_video_format_length
540 
541  put_bits(&s->pb, 1, 1); // reserved_bit
542  put_bits(&s->pb, 7, fpa_type); // S3D_video_format_type
543  put_bits(&s->pb, 8, 0x04); // reserved_data[0]
544  put_bits(&s->pb, 8, 0xFF); // reserved_data[1]
545  }
546  }
547 
548  s->mb_y = 0;
550 }
551 
552 static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
553  int has_mv, int field_motion)
554 {
555  put_bits(&s->pb, n, bits);
556  if (!s->frame_pred_frame_dct) {
557  if (has_mv)
558  /* motion_type: frame/field */
559  put_bits(&s->pb, 2, 2 - field_motion);
560  put_bits(&s->pb, 1, s->interlaced_dct);
561  }
562 }
563 
564 // RAL: Parameter added: f_or_b_code
565 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
566 {
567  if (val == 0) {
568  /* zero vector */
569  put_bits(&s->pb,
572  } else {
573  int code, sign, bits;
574  int bit_size = f_or_b_code - 1;
575  int range = 1 << bit_size;
576  /* modulo encoding */
577  val = sign_extend(val, 5 + bit_size);
578 
579  if (val >= 0) {
580  val--;
581  code = (val >> bit_size) + 1;
582  bits = val & (range - 1);
583  sign = 0;
584  } else {
585  val = -val;
586  val--;
587  code = (val >> bit_size) + 1;
588  bits = val & (range - 1);
589  sign = 1;
590  }
591 
592  av_assert2(code > 0 && code <= 16);
593 
594  put_bits(&s->pb,
597 
598  put_bits(&s->pb, 1, sign);
599  if (bit_size > 0)
600  put_bits(&s->pb, bit_size, bits);
601  }
602 }
603 
604 static inline void encode_dc(MpegEncContext *s, int diff, int component)
605 {
606  unsigned int diff_u = diff + 255;
607  if (diff_u >= 511) {
608  int index;
609 
610  if (diff < 0) {
611  index = av_log2_16bit(-2 * diff);
612  diff--;
613  } else {
614  index = av_log2_16bit(2 * diff);
615  }
616  if (component == 0)
617  put_bits(&s->pb,
618  ff_mpeg12_vlc_dc_lum_bits[index] + index,
619  (ff_mpeg12_vlc_dc_lum_code[index] << index) +
620  (diff & ((1 << index) - 1)));
621  else
622  put_bits(&s->pb,
623  ff_mpeg12_vlc_dc_chroma_bits[index] + index,
624  (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
625  (diff & ((1 << index) - 1)));
626  } else {
627  if (component == 0)
628  put_bits(&s->pb,
629  mpeg1_lum_dc_uni[diff + 255] & 0xFF,
630  mpeg1_lum_dc_uni[diff + 255] >> 8);
631  else
632  put_bits(&s->pb,
633  mpeg1_chr_dc_uni[diff + 255] & 0xFF,
634  mpeg1_chr_dc_uni[diff + 255] >> 8);
635  }
636 }
637 
638 static void mpeg1_encode_block(MpegEncContext *s, int16_t *block, int n)
639 {
640  int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
641  int code, component;
642  const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
643 
644  last_index = s->block_last_index[n];
645 
646  /* DC coef */
647  if (s->mb_intra) {
648  component = (n <= 3 ? 0 : (n & 1) + 1);
649  dc = block[0]; /* overflow is impossible */
650  diff = dc - s->last_dc[component];
651  encode_dc(s, diff, component);
652  s->last_dc[component] = dc;
653  i = 1;
654  if (s->intra_vlc_format)
655  table_vlc = ff_rl_mpeg2.table_vlc;
656  } else {
657  /* encode the first coefficient: needs to be done here because
658  * it is handled slightly differently */
659  level = block[0];
660  if (abs(level) == 1) {
661  code = ((uint32_t)level >> 31); /* the sign bit */
662  put_bits(&s->pb, 2, code | 0x02);
663  i = 1;
664  } else {
665  i = 0;
666  last_non_zero = -1;
667  goto next_coef;
668  }
669  }
670 
671  /* now quantify & encode AC coefs */
672  last_non_zero = i - 1;
673 
674  for (; i <= last_index; i++) {
675  j = s->intra_scantable.permutated[i];
676  level = block[j];
677 
678 next_coef:
679  /* encode using VLC */
680  if (level != 0) {
681  run = i - last_non_zero - 1;
682 
683  alevel = level;
684  MASK_ABS(sign, alevel);
685  sign &= 1;
686 
687  if (alevel <= mpeg1_max_level[0][run]) {
688  code = mpeg1_index_run[0][run] + alevel - 1;
689  /* store the VLC & sign at once */
690  put_bits(&s->pb, table_vlc[code][1] + 1,
691  (table_vlc[code][0] << 1) + sign);
692  } else {
693  /* escape seems to be pretty rare <5% so I do not optimize it */
694  put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
695  /* escape: only clip in this case */
696  put_bits(&s->pb, 6, run);
697  if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
698  if (alevel < 128) {
699  put_sbits(&s->pb, 8, level);
700  } else {
701  if (level < 0)
702  put_bits(&s->pb, 16, 0x8001 + level + 255);
703  else
704  put_sbits(&s->pb, 16, level);
705  }
706  } else {
707  put_sbits(&s->pb, 12, level);
708  }
709  }
710  last_non_zero = i;
711  }
712  }
713  /* end of block */
714  put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
715 }
716 
718  int16_t block[8][64],
719  int motion_x, int motion_y,
720  int mb_block_count)
721 {
722  int i, cbp;
723  const int mb_x = s->mb_x;
724  const int mb_y = s->mb_y;
725  const int first_mb = mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
726 
727  /* compute cbp */
728  cbp = 0;
729  for (i = 0; i < mb_block_count; i++)
730  if (s->block_last_index[i] >= 0)
731  cbp |= 1 << (mb_block_count - 1 - i);
732 
733  if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
734  (mb_x != s->mb_width - 1 ||
735  (mb_y != s->end_mb_y - 1 && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)) &&
736  ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) ||
737  (s->pict_type == AV_PICTURE_TYPE_B && s->mv_dir == s->last_mv_dir &&
738  (((s->mv_dir & MV_DIR_FORWARD)
739  ? ((s->mv[0][0][0] - s->last_mv[0][0][0]) |
740  (s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
741  ((s->mv_dir & MV_DIR_BACKWARD)
742  ? ((s->mv[1][0][0] - s->last_mv[1][0][0]) |
743  (s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
744  s->mb_skip_run++;
745  s->qscale -= s->dquant;
746  s->skip_count++;
747  s->misc_bits++;
748  s->last_bits++;
749  if (s->pict_type == AV_PICTURE_TYPE_P) {
750  s->last_mv[0][0][0] =
751  s->last_mv[0][0][1] =
752  s->last_mv[0][1][0] =
753  s->last_mv[0][1][1] = 0;
754  }
755  } else {
756  if (first_mb) {
757  av_assert0(s->mb_skip_run == 0);
758  encode_mb_skip_run(s, s->mb_x);
759  } else {
761  }
762 
763  if (s->pict_type == AV_PICTURE_TYPE_I) {
764  if (s->dquant && cbp) {
765  /* macroblock_type: macroblock_quant = 1 */
766  put_mb_modes(s, 2, 1, 0, 0);
767  put_qscale(s);
768  } else {
769  /* macroblock_type: macroblock_quant = 0 */
770  put_mb_modes(s, 1, 1, 0, 0);
771  s->qscale -= s->dquant;
772  }
773  s->misc_bits += get_bits_diff(s);
774  s->i_count++;
775  } else if (s->mb_intra) {
776  if (s->dquant && cbp) {
777  put_mb_modes(s, 6, 0x01, 0, 0);
778  put_qscale(s);
779  } else {
780  put_mb_modes(s, 5, 0x03, 0, 0);
781  s->qscale -= s->dquant;
782  }
783  s->misc_bits += get_bits_diff(s);
784  s->i_count++;
785  memset(s->last_mv, 0, sizeof(s->last_mv));
786  } else if (s->pict_type == AV_PICTURE_TYPE_P) {
787  if (s->mv_type == MV_TYPE_16X16) {
788  if (cbp != 0) {
789  if ((motion_x | motion_y) == 0) {
790  if (s->dquant) {
791  /* macroblock_pattern & quant */
792  put_mb_modes(s, 5, 1, 0, 0);
793  put_qscale(s);
794  } else {
795  /* macroblock_pattern only */
796  put_mb_modes(s, 2, 1, 0, 0);
797  }
798  s->misc_bits += get_bits_diff(s);
799  } else {
800  if (s->dquant) {
801  put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
802  put_qscale(s);
803  } else {
804  put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
805  }
806  s->misc_bits += get_bits_diff(s);
807  // RAL: f_code parameter added
809  motion_x - s->last_mv[0][0][0],
810  s->f_code);
811  // RAL: f_code parameter added
813  motion_y - s->last_mv[0][0][1],
814  s->f_code);
815  s->mv_bits += get_bits_diff(s);
816  }
817  } else {
818  put_bits(&s->pb, 3, 1); /* motion only */
819  if (!s->frame_pred_frame_dct)
820  put_bits(&s->pb, 2, 2); /* motion_type: frame */
821  s->misc_bits += get_bits_diff(s);
822  // RAL: f_code parameter added
824  motion_x - s->last_mv[0][0][0],
825  s->f_code);
826  // RAL: f_code parameter added
828  motion_y - s->last_mv[0][0][1],
829  s->f_code);
830  s->qscale -= s->dquant;
831  s->mv_bits += get_bits_diff(s);
832  }
833  s->last_mv[0][1][0] = s->last_mv[0][0][0] = motion_x;
834  s->last_mv[0][1][1] = s->last_mv[0][0][1] = motion_y;
835  } else {
837 
838  if (cbp) {
839  if (s->dquant) {
840  put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
841  put_qscale(s);
842  } else {
843  put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
844  }
845  } else {
846  put_bits(&s->pb, 3, 1); /* motion only */
847  put_bits(&s->pb, 2, 1); /* motion_type: field */
848  s->qscale -= s->dquant;
849  }
850  s->misc_bits += get_bits_diff(s);
851  for (i = 0; i < 2; i++) {
852  put_bits(&s->pb, 1, s->field_select[0][i]);
854  s->mv[0][i][0] - s->last_mv[0][i][0],
855  s->f_code);
857  s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
858  s->f_code);
859  s->last_mv[0][i][0] = s->mv[0][i][0];
860  s->last_mv[0][i][1] = 2 * s->mv[0][i][1];
861  }
862  s->mv_bits += get_bits_diff(s);
863  }
864  if (cbp) {
865  if (s->chroma_y_shift) {
866  put_bits(&s->pb,
867  ff_mpeg12_mbPatTable[cbp][1],
868  ff_mpeg12_mbPatTable[cbp][0]);
869  } else {
870  put_bits(&s->pb,
871  ff_mpeg12_mbPatTable[cbp >> 2][1],
872  ff_mpeg12_mbPatTable[cbp >> 2][0]);
873  put_sbits(&s->pb, 2, cbp);
874  }
875  }
876  s->f_count++;
877  } else {
878  if (s->mv_type == MV_TYPE_16X16) {
879  if (cbp) { // With coded bloc pattern
880  if (s->dquant) {
881  if (s->mv_dir == MV_DIR_FORWARD)
882  put_mb_modes(s, 6, 3, 1, 0);
883  else
884  put_mb_modes(s, 8 - s->mv_dir, 2, 1, 0);
885  put_qscale(s);
886  } else {
887  put_mb_modes(s, 5 - s->mv_dir, 3, 1, 0);
888  }
889  } else { // No coded bloc pattern
890  put_bits(&s->pb, 5 - s->mv_dir, 2);
891  if (!s->frame_pred_frame_dct)
892  put_bits(&s->pb, 2, 2); /* motion_type: frame */
893  s->qscale -= s->dquant;
894  }
895  s->misc_bits += get_bits_diff(s);
896  if (s->mv_dir & MV_DIR_FORWARD) {
898  s->mv[0][0][0] - s->last_mv[0][0][0],
899  s->f_code);
901  s->mv[0][0][1] - s->last_mv[0][0][1],
902  s->f_code);
903  s->last_mv[0][0][0] =
904  s->last_mv[0][1][0] = s->mv[0][0][0];
905  s->last_mv[0][0][1] =
906  s->last_mv[0][1][1] = s->mv[0][0][1];
907  s->f_count++;
908  }
909  if (s->mv_dir & MV_DIR_BACKWARD) {
911  s->mv[1][0][0] - s->last_mv[1][0][0],
912  s->b_code);
914  s->mv[1][0][1] - s->last_mv[1][0][1],
915  s->b_code);
916  s->last_mv[1][0][0] =
917  s->last_mv[1][1][0] = s->mv[1][0][0];
918  s->last_mv[1][0][1] =
919  s->last_mv[1][1][1] = s->mv[1][0][1];
920  s->b_count++;
921  }
922  } else {
925  if (cbp) { // With coded bloc pattern
926  if (s->dquant) {
927  if (s->mv_dir == MV_DIR_FORWARD)
928  put_mb_modes(s, 6, 3, 1, 1);
929  else
930  put_mb_modes(s, 8 - s->mv_dir, 2, 1, 1);
931  put_qscale(s);
932  } else {
933  put_mb_modes(s, 5 - s->mv_dir, 3, 1, 1);
934  }
935  } else { // No coded bloc pattern
936  put_bits(&s->pb, 5 - s->mv_dir, 2);
937  put_bits(&s->pb, 2, 1); /* motion_type: field */
938  s->qscale -= s->dquant;
939  }
940  s->misc_bits += get_bits_diff(s);
941  if (s->mv_dir & MV_DIR_FORWARD) {
942  for (i = 0; i < 2; i++) {
943  put_bits(&s->pb, 1, s->field_select[0][i]);
945  s->mv[0][i][0] - s->last_mv[0][i][0],
946  s->f_code);
948  s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
949  s->f_code);
950  s->last_mv[0][i][0] = s->mv[0][i][0];
951  s->last_mv[0][i][1] = s->mv[0][i][1] * 2;
952  }
953  s->f_count++;
954  }
955  if (s->mv_dir & MV_DIR_BACKWARD) {
956  for (i = 0; i < 2; i++) {
957  put_bits(&s->pb, 1, s->field_select[1][i]);
959  s->mv[1][i][0] - s->last_mv[1][i][0],
960  s->b_code);
962  s->mv[1][i][1] - (s->last_mv[1][i][1] >> 1),
963  s->b_code);
964  s->last_mv[1][i][0] = s->mv[1][i][0];
965  s->last_mv[1][i][1] = s->mv[1][i][1] * 2;
966  }
967  s->b_count++;
968  }
969  }
970  s->mv_bits += get_bits_diff(s);
971  if (cbp) {
972  if (s->chroma_y_shift) {
973  put_bits(&s->pb,
974  ff_mpeg12_mbPatTable[cbp][1],
975  ff_mpeg12_mbPatTable[cbp][0]);
976  } else {
977  put_bits(&s->pb,
978  ff_mpeg12_mbPatTable[cbp >> 2][1],
979  ff_mpeg12_mbPatTable[cbp >> 2][0]);
980  put_sbits(&s->pb, 2, cbp);
981  }
982  }
983  }
984  for (i = 0; i < mb_block_count; i++)
985  if (cbp & (1 << (mb_block_count - 1 - i)))
986  mpeg1_encode_block(s, block[i], i);
987  s->mb_skip_run = 0;
988  if (s->mb_intra)
989  s->i_tex_bits += get_bits_diff(s);
990  else
991  s->p_tex_bits += get_bits_diff(s);
992  }
993 }
994 
995 void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[8][64],
996  int motion_x, int motion_y)
997 {
998  if (s->chroma_format == CHROMA_420)
999  mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
1000  else
1001  mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
1002 }
1003 
1005 {
1006  static int done = 0;
1007 
1009 
1010  if (!done) {
1011  int f_code;
1012  int mv;
1013  int i;
1014 
1015  done = 1;
1018 
1019  for (i = 0; i < 64; i++) {
1020  mpeg1_max_level[0][i] = ff_rl_mpeg1.max_level[0][i];
1021  mpeg1_index_run[0][i] = ff_rl_mpeg1.index_run[0][i];
1022  }
1023 
1025  if (s->intra_vlc_format)
1027 
1028  /* build unified dc encoding tables */
1029  for (i = -255; i < 256; i++) {
1030  int adiff, index;
1031  int bits, code;
1032  int diff = i;
1033 
1034  adiff = FFABS(diff);
1035  if (diff < 0)
1036  diff--;
1037  index = av_log2(2 * adiff);
1038 
1040  code = (ff_mpeg12_vlc_dc_lum_code[index] << index) +
1041  (diff & ((1 << index) - 1));
1042  mpeg1_lum_dc_uni[i + 255] = bits + (code << 8);
1043 
1046  (diff & ((1 << index) - 1));
1047  mpeg1_chr_dc_uni[i + 255] = bits + (code << 8);
1048  }
1049 
1050  for (f_code = 1; f_code <= MAX_FCODE; f_code++)
1051  for (mv = -MAX_DMV; mv <= MAX_DMV; mv++) {
1052  int len;
1053 
1054  if (mv == 0) {
1055  len = ff_mpeg12_mbMotionVectorTable[0][1];
1056  } else {
1057  int val, bit_size, code;
1058 
1059  bit_size = f_code - 1;
1060 
1061  val = mv;
1062  if (val < 0)
1063  val = -val;
1064  val--;
1065  code = (val >> bit_size) + 1;
1066  if (code < 17)
1067  len = ff_mpeg12_mbMotionVectorTable[code][1] +
1068  1 + bit_size;
1069  else
1070  len = ff_mpeg12_mbMotionVectorTable[16][1] +
1071  2 + bit_size;
1072  }
1073 
1074  mv_penalty[f_code][mv + MAX_DMV] = len;
1075  }
1076 
1077 
1078  for (f_code = MAX_FCODE; f_code > 0; f_code--)
1079  for (mv = -(8 << f_code); mv < (8 << f_code); mv++)
1080  fcode_tab[mv + MAX_MV] = f_code;
1081  }
1082  s->me.mv_penalty = mv_penalty;
1083  s->fcode_tab = fcode_tab;
1084  if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1085  s->min_qcoeff = -255;
1086  s->max_qcoeff = 255;
1087  } else {
1088  s->min_qcoeff = -2047;
1089  s->max_qcoeff = 2047;
1090  }
1091  if (s->intra_vlc_format) {
1092  s->intra_ac_vlc_length =
1094  } else {
1095  s->intra_ac_vlc_length =
1097  }
1098  s->inter_ac_vlc_length =
1100 }
1101 
1102 #define OFFSET(x) offsetof(MpegEncContext, x)
1103 #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
1104 #define COMMON_OPTS \
1105  { "gop_timecode", "MPEG GOP Timecode in hh:mm:ss[:;.]ff format", \
1106  OFFSET(tc_opt_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, VE },\
1107  { "intra_vlc", "Use MPEG-2 intra VLC table.", \
1108  OFFSET(intra_vlc_format), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \
1109  { "drop_frame_timecode", "Timecode is in drop frame format.", \
1110  OFFSET(drop_frame_timecode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \
1111  { "scan_offset", "Reserve space for SVCD scan offset user data.", \
1112  OFFSET(scan_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1113 
1114 static const AVOption mpeg1_options[] = {
1115  COMMON_OPTS
1117  { NULL },
1118 };
1119 
1120 static const AVOption mpeg2_options[] = {
1121  COMMON_OPTS
1122  { "non_linear_quant", "Use nonlinear quantizer.", OFFSET(q_scale_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1123  { "alternate_scan", "Enable alternate scantable.", OFFSET(alternate_scan), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1124  { "seq_disp_ext", "Write sequence_display_extension blocks.", OFFSET(seq_disp_ext), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE, "seq_disp_ext" },
1125  { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, VE, "seq_disp_ext" },
1126  { "never", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, VE, "seq_disp_ext" },
1127  { "always", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, VE, "seq_disp_ext" },
1129  { NULL },
1130 };
1131 
1132 #define mpeg12_class(x) \
1133 static const AVClass mpeg ## x ## _class = { \
1134  .class_name = "mpeg" # x "video encoder", \
1135  .item_name = av_default_item_name, \
1136  .option = mpeg ## x ## _options, \
1137  .version = LIBAVUTIL_VERSION_INT, \
1138 };
1139 
1141 mpeg12_class(2)
1142 
1143 AVCodec ff_mpeg1video_encoder = {
1144  .name = "mpeg1video",
1145  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
1146  .type = AVMEDIA_TYPE_VIDEO,
1147  .id = AV_CODEC_ID_MPEG1VIDEO,
1148  .priv_data_size = sizeof(MpegEncContext),
1149  .init = encode_init,
1150  .encode2 = ff_mpv_encode_picture,
1151  .close = ff_mpv_encode_end,
1152  .supported_framerates = ff_mpeg12_frame_rate_tab + 1,
1153  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
1154  AV_PIX_FMT_NONE },
1155  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
1156  .priv_class = &mpeg1_class,
1157 };
1158 
1160  .name = "mpeg2video",
1161  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
1162  .type = AVMEDIA_TYPE_VIDEO,
1163  .id = AV_CODEC_ID_MPEG2VIDEO,
1164  .priv_data_size = sizeof(MpegEncContext),
1165  .init = encode_init,
1166  .encode2 = ff_mpv_encode_picture,
1167  .close = ff_mpv_encode_end,
1168  .supported_framerates = ff_mpeg2_frame_rate_tab,
1169  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
1171  AV_PIX_FMT_NONE },
1172  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
1173  .priv_class = &mpeg2_class,
1174 };
av_cold void ff_mpeg1_encode_init(MpegEncContext *s)
Definition: mpeg12enc.c:1004
static uint8_t uni_mpeg1_ac_vlc_len[64 *64 *2]
Definition: mpeg12enc.c:58
#define MASK_ABS(mask, level)
Definition: mathops.h:164
#define NULL
Definition: coverity.c:32
AVCodec ff_mpeg2video_encoder
Definition: mpeg12enc.c:1159
const char const char void * val
Definition: avisynth_c.h:672
float v
int aspect_ratio_info
Definition: mpegvideo.h:534
int picture_number
Definition: mpegvideo.h:262
const char * s
Definition: avisynth_c.h:669
const AVRational ff_mpeg2_frame_rate_tab[]
Definition: mpeg12data.c:328
AVOption.
Definition: opt.h:255
uint8_t * fcode_tab
smallest fcode needed for each MV
Definition: mpegvideo.h:414
#define MV_TYPE_FIELD
2 vectors, one per field
Definition: mpegvideo.h:404
int last_mv[2][2][2]
last MV, used for MV prediction in MPEG1 & B-frame MPEG4
Definition: mpegvideo.h:413
static int find_frame_rate_index(MpegEncContext *s)
Definition: mpeg12enc.c:104
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:200
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:198
Views are next to each other, but when upscaling apply a checkerboard pattern.
Definition: stereo3d.h:87
int end_mb_y
end mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y) ...
Definition: mpegvideo.h:289
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int height
Definition: avcodec.h:935
#define FF_MPV_COMMON_OPTS
Definition: mpegvideo.h:699
int num
numerator
Definition: rational.h:44
const unsigned char ff_mpeg12_vlc_dc_lum_bits[12]
Definition: mpeg12data.c:55
enum AVCodecID codec_id
Definition: mpegvideo.h:244
void avpriv_align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
Definition: bitstream.c:47
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1621
int scan_offset
reserve space for SVCD scan offset user data.
Definition: mpegvideo.h:614
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:66
int min_qcoeff
minimum encodable coefficient
Definition: mpegvideo.h:442
mpegvideo header.
#define CODEC_FLAG2_DROP_FRAME_TIMECODE
timecode is in drop frame format. DEPRECATED!!!!
Definition: avcodec.h:766
uint8_t permutated[64]
Definition: idctdsp.h:31
const uint16_t ff_mpeg12_vlc_dc_lum_code[12]
Definition: mpeg12data.c:52
uint8_t run
Definition: svq3.c:149
uint8_t * intra_ac_vlc_length
Definition: mpegvideo.h:445
#define UNI_AC_ENC_INDEX(run, level)
Definition: mpegvideo.h:452
#define SLICE_MIN_START_CODE
Definition: mpegvideo.h:89
int profile
profile
Definition: avcodec.h:2833
AVCodec.
Definition: avcodec.h:3173
uint8_t(* mv_penalty)[MAX_DMV *2+1]
amount of bits needed to encode a MV
Definition: mpegvideo.h:202
int qscale
QP.
Definition: mpegvideo.h:341
RLTable.
Definition: rl.h:38
int field_select[2][2]
Definition: mpegvideo.h:412
Macro definitions for various function/variable attributes.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1367
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:597
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
Parse timecode representation (hh:mm:ss[:;.
Definition: timecode.c:192
AVRational mpeg2_frame_rate_ext
Definition: mpegvideo.h:355
if()
Definition: avfilter.c:975
uint8_t bits
Definition: crc.c:295
uint8_t
#define av_cold
Definition: attributes.h:74
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
static uint32_t mpeg1_lum_dc_uni[512]
Definition: mpeg12enc.c:63
static uint8_t uni_ac_vlc_len[64 *64 *2]
Definition: mjpegenc.c:41
AVOptions.
static void encode_mb_skip_run(MpegEncContext *s, int run)
Definition: mpeg12enc.c:389
RLTable ff_rl_mpeg2
Definition: mpeg12data.c:174
Stereo 3D type: this structure describes how two videos are packed within a single video surface...
Definition: stereo3d.h:123
int me_range
maximum motion estimation search range in subpel units If 0 then no limit.
Definition: avcodec.h:1733
timecode is drop frame
Definition: timecode.h:36
const float ff_mpeg1_aspect[16]
Definition: mpeg12data.c:394
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2834
int misc_bits
cbp, mb_type
Definition: mpegvideo.h:486
static const AVOption mpeg1_options[]
Definition: mpeg12enc.c:1114
static void mpeg1_encode_block(MpegEncContext *s, int16_t *block, int n)
Definition: mpeg12enc.c:638
int interlaced_dct
Definition: mpegvideo.h:611
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:315
#define CHROMA_420
Definition: mpegvideo.h:603
The data is the AVPanScan struct defined in libavcodec.
Definition: frame.h:52
int intra_dc_precision
Definition: mpegvideo.h:592
int repeat_first_field
Definition: mpegvideo.h:600
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
static int8_t mpeg1_max_level[2][64]
Definition: mpeg12enc.c:67
int start
timecode frame start (first base frame number)
Definition: timecode.h:42
int8_t * max_level[2]
encoding & decoding
Definition: rl.h:45
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:264
static const uint8_t header[24]
Definition: sdr2.c:67
int max_qcoeff
maximum encodable coefficient
Definition: mpegvideo.h:443
int dquant
qscale difference to prev qscale
Definition: mpegvideo.h:347
int gop_picture_number
index of the first picture of a GOP based on fake_pic_num & mpeg1 specific
Definition: mpegvideo.h:581
#define av_log(a,...)
static int get_bits_diff(MpegEncContext *s)
Definition: mpegvideo.h:835
int intra_only
if true, only intra pictures are generated
Definition: mpegvideo.h:234
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:175
int last_dc[3]
last DC values for MPEG1
Definition: mpegvideo.h:320
static void put_mb_modes(MpegEncContext *s, int n, int bits, int has_mv, int field_motion)
Definition: mpeg12enc.c:552
uint8_t * inter_ac_vlc_last_length
Definition: mpegvideo.h:450
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:822
int chroma_y_shift
Definition: mpegvideo.h:607
int strict_std_compliance
strictly follow the std (MPEG4, ...)
Definition: mpegvideo.h:252
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:180
static const uint8_t inv_non_linear_qscale[]
Definition: mpeg12enc.c:46
int av_nearer_q(AVRational q, AVRational q1, AVRational q2)
Definition: rational.c:126
const uint16_t ff_mpeg12_vlc_dc_chroma_code[12]
Definition: mpeg12data.c:59
static void encode_dc(MpegEncContext *s, int diff, int component)
Definition: mpeg12enc.c:604
uint8_t * buf
Definition: put_bits.h:38
int rc_max_rate
maximum bitrate
Definition: avcodec.h:2325
simple assert() macros that are a bit more flexible than ISO C assert().
int64_t av_gcd(int64_t a, int64_t b)
Return the greatest common divisor of a and b.
Definition: mathematics.c:55
const char * name
Name of the codec implementation.
Definition: avcodec.h:3180
int width
width and height in 1/16 pel
Definition: avcodec.h:934
int low_delay
no reordering needed / has no b-frames
Definition: mpegvideo.h:538
Video is not stereoscopic (and metadata has to be there).
Definition: stereo3d.h:35
Libavcodec external API header.
static void mpeg1_encode_sequence_header(MpegEncContext *s)
Definition: mpeg12enc.c:235
static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
Definition: mpeg12enc.c:565
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:72
int resync_mb_x
x position of last resync marker
Definition: mpegvideo.h:490
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2302
uint8_t * intra_ac_vlc_last_length
Definition: mpegvideo.h:446
#define mpeg12_class(x)
Definition: mpeg12enc.c:1132
int intra_vlc_format
Definition: mpegvideo.h:597
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:2546
int64_t timecode_frame_start
GOP timecode frame start number.
Definition: avcodec.h:2453
int progressive_frame
Definition: mpegvideo.h:609
uint8_t * vbv_delay_ptr
pointer to vbv_delay in the bitstream
Definition: mpegvideo.h:583
const uint16_t(* table_vlc)[2]
Definition: rl.h:41
static uint8_t fcode_tab[MAX_MV *2+1]
Definition: mpeg12enc.c:56
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1412
const unsigned char ff_mpeg12_vlc_dc_chroma_bits[12]
Definition: mpeg12data.c:62
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:319
RLTable ff_rl_mpeg1
Definition: mpeg12data.c:166
int alternate_scan
Definition: mpegvideo.h:598
#define GOP_START_CODE
Definition: mpegvideo.h:87
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:1937
static uint8_t uni_mpeg2_ac_vlc_len[64 *64 *2]
Definition: mpeg12enc.c:59
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:68
int level
level
Definition: avcodec.h:2917
#define VE
Definition: mpeg12enc.c:1103
int block_last_index[12]
last non zero coefficient in block
Definition: mpegvideo.h:218
MotionEstContext me
Definition: mpegvideo.h:417
int n
Definition: avisynth_c.h:589
#define EXT_START_CODE
Definition: cavs.h:33
static av_cold void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len)
Definition: mpeg12enc.c:69
static void put_header(MpegEncContext *s, int header)
Definition: mpeg12enc.c:227
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:107
void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
static const uint8_t svcd_scan_offset_placeholder[]
Definition: mpeg12enc.c:50
static const int8_t mv[256][2]
Definition: 4xm.c:77
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:401
int frame_pred_frame_dct
Definition: mpegvideo.h:593
int av_timecode_adjust_ntsc_framenum2(int framenum, int fps)
Adjust frame number for NTSC drop frame time code.
Definition: timecode.c:34
#define MV_DIR_BACKWARD
Definition: mpegvideo.h:398
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition: avcodec.h:2545
int coded_picture_number
picture number in bitstream order
Definition: frame.h:266
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:127
#define AV_LOG_INFO
Standard information.
Definition: log.h:186
int concealment_motion_vectors
Definition: mpegvideo.h:595
char * tc_opt_str
timecode option string
Definition: mpegvideo.h:619
Timecode helpers header.
main external API structure.
Definition: avcodec.h:1239
ScanTable intra_scantable
Definition: mpegvideo.h:223
#define USER_START_CODE
Definition: cavs.h:34
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:232
MPEG1/2 tables.
static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s, int16_t block[8][64], int motion_x, int motion_y, int mb_block_count)
Definition: mpeg12enc.c:717
uint8_t * data
Definition: frame.h:129
int ff_mpv_encode_init(AVCodecContext *avctx)
uint8_t * inter_ac_vlc_length
Definition: mpegvideo.h:449
static uint8_t mpeg1_index_run[2][64]
Definition: mpeg12enc.c:66
int progressive_sequence
Definition: mpegvideo.h:586
uint16_t * intra_matrix
custom intra quantization matrix
Definition: avcodec.h:1785
BYTE int const BYTE int int int height
Definition: avisynth_c.h:714
static uint8_t mv_penalty[MAX_FCODE+1][MAX_DMV *2+1]
Definition: mpeg12enc.c:55
int index
Definition: gxfenc.c:89
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1951
rational number numerator/denominator
Definition: rational.h:43
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1944
struct AVFrame * f
Definition: mpegvideo.h:104
#define COMMON_OPTS
Definition: mpeg12enc.c:1104
uint8_t * index_run[2]
encoding only
Definition: rl.h:44
#define OFFSET(x)
Definition: mpeg12enc.c:1102
#define MAX_MV
Definition: mpegvideo.h:68
void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
Definition: mpeg12enc.c:423
int f_code
forward MV resolution
Definition: mpegvideo.h:375
#define MAX_FCODE
Definition: mpegvideo.h:67
#define MAX_DMV
Definition: mpegvideo.h:69
#define CODEC_FLAG_CLOSED_GOP
Definition: avcodec.h:762
#define MV_DIR_FORWARD
Definition: mpegvideo.h:397
uint16_t * inter_matrix
custom inter quantization matrix
Definition: avcodec.h:1792
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:349
int bit_rate
wanted bit rate
Definition: mpegvideo.h:235
av_cold void ff_mpeg12_common_init(MpegEncContext *s)
Definition: mpeg12.c:114
Views are on top of each other.
Definition: stereo3d.h:55
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:139
int last_mv_dir
last mv_dir, used for b frame encoding
Definition: mpegvideo.h:582
void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[8][64], int motion_x, int motion_y)
Definition: mpeg12enc.c:995
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:133
Pan Scan area.
Definition: avcodec.h:921
uint8_t level
Definition: svq3.c:150
int mv[2][4][2]
motion vectors for a macroblock first coordinate : 0 = forward 1 = backward second " : depend...
Definition: mpegvideo.h:411
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> dc
static av_cold int encode_init(AVCodecContext *avctx)
Definition: mpeg12enc.c:143
MpegEncContext.
Definition: mpegvideo.h:213
Views are next to each other.
Definition: stereo3d.h:45
struct AVCodecContext * avctx
Definition: mpegvideo.h:230
PutBitContext pb
bit output
Definition: mpegvideo.h:286
const AVRational ff_mpeg2_aspect[16]
Definition: mpeg12data.c:415
static av_always_inline void put_qscale(MpegEncContext *s)
Definition: mpeg12enc.c:399
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
#define CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:868
static uint32_t mpeg1_chr_dc_uni[512]
Definition: mpeg12enc.c:64
static const AVOption mpeg2_options[]
Definition: mpeg12enc.c:1120
uint8_t ff_mpeg12_static_rl_table_store[2][2][2 *MAX_RUN+MAX_LEVEL+3]
Definition: mpeg12.c:44
Bi-dir predicted.
Definition: avutil.h:269
const uint8_t ff_mpeg12_mbPatTable[64][2]
Definition: mpeg12data.c:221
int den
denominator
Definition: rational.h:45
int ff_mpv_encode_end(AVCodecContext *avctx)
void * priv_data
Definition: avcodec.h:1281
void ff_mpeg1_encode_slice_header(MpegEncContext *s)
Definition: mpeg12enc.c:409
#define PICT_FRAME
Definition: mpegutils.h:35
int last_bits
temp var used for calculating the above vars
Definition: mpegvideo.h:487
int frame_rate_index
Definition: mpegvideo.h:354
static av_always_inline int diff(const uint32_t a, const uint32_t b)
int picture_structure
Definition: mpegvideo.h:590
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:364
int len
#define av_log2_16bit
Definition: intmath.h:106
#define SEQ_START_CODE
Definition: mpegvideo.h:86
int drop_frame_timecode
timecode is in drop frame format.
Definition: mpegvideo.h:613
#define av_log2
Definition: intmath.h:105
int resync_mb_y
y position of last resync marker
Definition: mpegvideo.h:491
av_cold void ff_init_rl(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Definition: mpegvideo.c:1659
const uint8_t ff_mpeg12_mbAddrIncrTable[36][2]
Definition: mpeg12data.c:182
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:229
int flags2
CODEC_FLAG2_*.
Definition: avcodec.h:1340
int flags
AVCodecContext.flags (HQ, MV4, ...)
Definition: mpegvideo.h:247
const uint8_t ff_mpeg12_mbMotionVectorTable[17][2]
Definition: mpeg12data.c:288
#define PICTURE_START_CODE
Definition: mpegvideo.h:88
#define av_always_inline
Definition: attributes.h:37
int b_code
backward MV resolution for B Frames (mpeg4)
Definition: mpegvideo.h:376
int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
AVTimecode tc
timecode context
Definition: mpegvideo.h:620
uint32_t flags
flags such as drop frame, +24 hours support, ...
Definition: timecode.h:43
Stereoscopic 3d metadata.
Definition: frame.h:63
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
const AVRational ff_mpeg12_frame_rate_tab[16]
Definition: mpeg12data.c:308
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2541
Predicted.
Definition: avutil.h:268
#define FF_LEVEL_UNKNOWN
Definition: avcodec.h:2918
static int width
static int16_t block[64]
Definition: dct-test.c:110