Libav
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
28 #include <stdint.h>
29 
30 #include "libavutil/attributes.h"
31 #include "libavutil/log.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/stereo3d.h"
34 
35 #include "avcodec.h"
36 #include "bytestream.h"
37 #include "mathops.h"
38 #include "mpeg12.h"
39 #include "mpeg12data.h"
40 #include "mpegvideo.h"
41 
42 
43 static const uint8_t inv_non_linear_qscale[] = {
44  0, 2, 4, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16,
45 };
46 
48  0x10, 0x0E, 0x00, 0x80, 0x81, 0x00, 0x80,
49  0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
50 };
51 
52 static uint8_t mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
53 static uint8_t fcode_tab[MAX_MV * 2 + 1];
54 
55 static uint8_t uni_mpeg1_ac_vlc_len[64 * 64 * 2];
56 static uint8_t uni_mpeg2_ac_vlc_len[64 * 64 * 2];
57 
58 /* simple include everything table for dc, first byte is bits
59  * number next 3 are code */
60 static uint32_t mpeg1_lum_dc_uni[512];
61 static uint32_t mpeg1_chr_dc_uni[512];
62 
63 static uint8_t mpeg1_index_run[2][64];
64 static int8_t mpeg1_max_level[2][64];
65 
66 static av_cold void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len)
67 {
68  int i;
69 
70  for (i = 0; i < 128; i++) {
71  int level = i - 64;
72  int run;
73  if (!level)
74  continue;
75  for (run = 0; run < 64; run++) {
76  int len, code;
77  int alevel = FFABS(level);
78 
79  if (alevel > rl->max_level[0][run])
80  code = 111; /* rl->n */
81  else
82  code = rl->index_run[0][run] + alevel - 1;
83 
84  if (code < 111) { /* rl->n */
85  /* length of VLC and sign */
86  len = rl->table_vlc[code][1] + 1;
87  } else {
88  len = rl->table_vlc[111][1] + 6; /* rl->n */
89 
90  if (alevel < 128)
91  len += 8;
92  else
93  len += 16;
94  }
95 
96  uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
97  }
98  }
99 }
100 
102 {
103  int i;
104  int64_t dmin = INT64_MAX;
105  int64_t d;
106 
107  for (i = 1; i < 14; i++) {
108  int64_t n0 = 1001LL / ff_mpeg12_frame_rate_tab[i].den *
110  int64_t n1 = 1001LL * s->avctx->time_base.den;
111 
113  i >= 9)
114  break;
115 
116  d = FFABS(n0 - n1);
117  if (d < dmin) {
118  dmin = d;
119  s->frame_rate_index = i;
120  }
121  }
122 
123  if (dmin)
124  return -1;
125  else
126  return 0;
127 }
128 
130 {
131  MpegEncContext *s = avctx->priv_data;
132 
133  if (ff_MPV_encode_init(avctx) < 0)
134  return -1;
135 
136  if (find_frame_rate_index(s) < 0) {
138  av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n",
139  avctx->time_base.den, avctx->time_base.num);
140  return -1;
141  } else {
142  av_log(avctx, AV_LOG_INFO,
143  "MPEG1/2 does not support %d/%d fps, there may be AV sync issues\n",
144  avctx->time_base.den, avctx->time_base.num);
145  }
146  }
147 
148  if (avctx->profile == FF_PROFILE_UNKNOWN) {
149  if (avctx->level != FF_LEVEL_UNKNOWN) {
150  av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
151  return -1;
152  }
153  /* Main or 4:2:2 */
154  avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0;
155  }
156 
157  if (avctx->level == FF_LEVEL_UNKNOWN) {
158  if (avctx->profile == 0) { /* 4:2:2 */
159  if (avctx->width <= 720 && avctx->height <= 608)
160  avctx->level = 5; /* Main */
161  else
162  avctx->level = 2; /* High */
163  } else {
164  if (avctx->profile != 1 && s->chroma_format != CHROMA_420) {
165  av_log(avctx, AV_LOG_ERROR,
166  "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
167  return -1;
168  }
169  if (avctx->width <= 720 && avctx->height <= 576)
170  avctx->level = 8; /* Main */
171  else if (avctx->width <= 1440)
172  avctx->level = 6; /* High 1440 */
173  else
174  avctx->level = 4; /* High */
175  }
176  }
177 
178  if (s->drop_frame_timecode && s->frame_rate_index != 4) {
179  av_log(avctx, AV_LOG_ERROR,
180  "Drop frame time code only allowed with 1001/30000 fps\n");
181  return -1;
182  }
183 
184  return 0;
185 }
186 
187 static void put_header(MpegEncContext *s, int header)
188 {
190  put_bits(&s->pb, 16, header >> 16);
191  put_sbits(&s->pb, 16, header);
192 }
193 
194 /* put sequence header if needed */
196 {
197  unsigned int vbv_buffer_size, fps, v;
198  int i, constraint_parameter_flag;
199  uint64_t time_code;
200  float best_aspect_error = 1E10;
201  float aspect_ratio = av_q2d(s->avctx->sample_aspect_ratio);
202 
203  if (aspect_ratio == 0.0)
204  aspect_ratio = 1.0; // pixel aspect 1.1 (VGA)
205 
206  if (s->current_picture.f.key_frame) {
208 
209  /* mpeg1 header repeated every gop */
211 
212  put_sbits(&s->pb, 12, s->width);
213  put_sbits(&s->pb, 12, s->height);
214 
215  for (i = 1; i < 15; i++) {
216  float error = aspect_ratio;
217  if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || i <= 1)
218  error -= 1.0 / ff_mpeg1_aspect[i];
219  else
220  error -= av_q2d(ff_mpeg2_aspect[i]) * s->height / s->width;
221 
222  error = FFABS(error);
223 
224  if (error < best_aspect_error) {
225  best_aspect_error = error;
226  s->aspect_ratio_info = i;
227  }
228  }
229 
230  put_bits(&s->pb, 4, s->aspect_ratio_info);
231  put_bits(&s->pb, 4, s->frame_rate_index);
232 
233  if (s->avctx->rc_max_rate) {
234  v = (s->avctx->rc_max_rate + 399) / 400;
235  if (v > 0x3ffff && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
236  v = 0x3ffff;
237  } else {
238  v = 0x3FFFF;
239  }
240 
241  if (s->avctx->rc_buffer_size)
242  vbv_buffer_size = s->avctx->rc_buffer_size;
243  else
244  /* VBV calculation: Scaled so that a VCD has the proper
245  * VBV size of 40 kilobytes */
246  vbv_buffer_size = ((20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
247  vbv_buffer_size = (vbv_buffer_size + 16383) / 16384;
248 
249  put_sbits(&s->pb, 18, v);
250  put_bits(&s->pb, 1, 1); // marker
251  put_sbits(&s->pb, 10, vbv_buffer_size);
252 
253  constraint_parameter_flag =
254  s->width <= 768 &&
255  s->height <= 576 &&
256  s->mb_width * s->mb_height <= 396 &&
257  s->mb_width * s->mb_height * framerate.num <= 396 * 25 * framerate.den &&
258  framerate.num <= framerate.den * 30 &&
259  s->avctx->me_range &&
260  s->avctx->me_range < 128 &&
261  vbv_buffer_size <= 20 &&
262  v <= 1856000 / 400 &&
264 
265  put_bits(&s->pb, 1, constraint_parameter_flag);
266 
269 
270  if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
272  put_bits(&s->pb, 4, 1); // seq ext
273 
274  put_bits(&s->pb, 1, s->avctx->profile == 0); // escx 1 for 4:2:2 profile
275 
276  put_bits(&s->pb, 3, s->avctx->profile); // profile
277  put_bits(&s->pb, 4, s->avctx->level); // level
278 
279  put_bits(&s->pb, 1, s->progressive_sequence);
280  put_bits(&s->pb, 2, s->chroma_format);
281  put_bits(&s->pb, 2, s->width >> 12);
282  put_bits(&s->pb, 2, s->height >> 12);
283  put_bits(&s->pb, 12, v >> 18); // bitrate ext
284  put_bits(&s->pb, 1, 1); // marker
285  put_bits(&s->pb, 8, vbv_buffer_size >> 10); // vbv buffer ext
286  put_bits(&s->pb, 1, s->low_delay);
287  put_bits(&s->pb, 2, 0); // frame_rate_ext_n
288  put_bits(&s->pb, 5, 0); // frame_rate_ext_d
289  }
290 
292  put_bits(&s->pb, 1, s->drop_frame_timecode); // drop frame flag
293  /* time code: we must convert from the real frame rate to a
294  * fake MPEG frame rate in case of low frame rate */
295  fps = (framerate.num + framerate.den / 2) / framerate.den;
296  time_code = s->current_picture_ptr->f.coded_picture_number +
298 
300  if (s->drop_frame_timecode) {
301  /* only works for NTSC 29.97 */
302  int d = time_code / 17982;
303  int m = time_code % 17982;
304  /* not needed since -2,-1 / 1798 in C returns 0 */
305  // if (m < 2)
306  // m += 2;
307  time_code += 18 * d + 2 * ((m - 2) / 1798);
308  }
309  put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
310  put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
311  put_bits(&s->pb, 1, 1);
312  put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
313  put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
314  put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
315  put_bits(&s->pb, 1, 0); // broken link
316  }
317 }
318 
319 static inline void encode_mb_skip_run(MpegEncContext *s, int run)
320 {
321  while (run >= 33) {
322  put_bits(&s->pb, 11, 0x008);
323  run -= 33;
324  }
326  ff_mpeg12_mbAddrIncrTable[run][0]);
327 }
328 
330 {
331  if (s->q_scale_type) {
332  assert(s->qscale >= 1 && s->qscale <= 12);
334  } else {
335  put_bits(&s->pb, 5, s->qscale);
336  }
337 }
338 
340 {
341  if (s->height > 2800) {
342  put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127));
343  /* slice_vertical_position_extension */
344  put_bits(&s->pb, 3, s->mb_y >> 7);
345  } else {
347  }
348  put_qscale(s);
349  /* slice extra information */
350  put_bits(&s->pb, 1, 0);
351 }
352 
353 void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
354 {
355  AVFrameSideData *side_data;
357 
358  /* mpeg1 picture header */
360  /* temporal reference */
361 
362  // RAL: s->picture_number instead of s->fake_picture_number
363  put_bits(&s->pb, 10,
364  (s->picture_number - s->gop_picture_number) & 0x3ff);
365  put_bits(&s->pb, 3, s->pict_type);
366 
367  s->vbv_delay_ptr = s->pb.buf + put_bits_count(&s->pb) / 8;
368  put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
369 
370  // RAL: Forward f_code also needed for B-frames
371  if (s->pict_type == AV_PICTURE_TYPE_P ||
373  put_bits(&s->pb, 1, 0); /* half pel coordinates */
375  put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
376  else
377  put_bits(&s->pb, 3, 7); /* forward_f_code */
378  }
379 
380  // RAL: Backward f_code necessary for B-frames
381  if (s->pict_type == AV_PICTURE_TYPE_B) {
382  put_bits(&s->pb, 1, 0); /* half pel coordinates */
384  put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
385  else
386  put_bits(&s->pb, 3, 7); /* backward_f_code */
387  }
388 
389  put_bits(&s->pb, 1, 0); /* extra bit picture */
390 
391  s->frame_pred_frame_dct = 1;
392  if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
394  put_bits(&s->pb, 4, 8); /* pic ext */
395  if (s->pict_type == AV_PICTURE_TYPE_P ||
397  put_bits(&s->pb, 4, s->f_code);
398  put_bits(&s->pb, 4, s->f_code);
399  } else {
400  put_bits(&s->pb, 8, 255);
401  }
402  if (s->pict_type == AV_PICTURE_TYPE_B) {
403  put_bits(&s->pb, 4, s->b_code);
404  put_bits(&s->pb, 4, s->b_code);
405  } else {
406  put_bits(&s->pb, 8, 255);
407  }
408  put_bits(&s->pb, 2, s->intra_dc_precision);
409 
410  assert(s->picture_structure == PICT_FRAME);
411  put_bits(&s->pb, 2, s->picture_structure);
412  if (s->progressive_sequence)
413  put_bits(&s->pb, 1, 0); /* no repeat */
414  else
416  /* XXX: optimize the generation of this flag with entropy measures */
418 
419  put_bits(&s->pb, 1, s->frame_pred_frame_dct);
421  put_bits(&s->pb, 1, s->q_scale_type);
422  put_bits(&s->pb, 1, s->intra_vlc_format);
423  put_bits(&s->pb, 1, s->alternate_scan);
424  put_bits(&s->pb, 1, s->repeat_first_field);
426  /* chroma_420_type */
427  put_bits(&s->pb, 1, s->chroma_format ==
428  CHROMA_420 ? s->progressive_frame : 0);
429  put_bits(&s->pb, 1, s->progressive_frame);
430  put_bits(&s->pb, 1, 0); /* composite_display_flag */
431  }
432  if (s->scan_offset) {
433  int i;
434 
436  for (i = 0; i < sizeof(svcd_scan_offset_placeholder); i++)
438  }
441  if (side_data) {
442  AVStereo3D *stereo = (AVStereo3D *)side_data->data;
443  uint8_t fpa_type;
444 
445  switch (stereo->type) {
447  fpa_type = 0x03;
448  break;
450  fpa_type = 0x04;
451  break;
452  case AV_STEREO3D_2D:
453  fpa_type = 0x08;
454  break;
456  fpa_type = 0x23;
457  break;
458  default:
459  fpa_type = 0;
460  break;
461  }
462 
463  if (fpa_type != 0) {
465  put_bits(&s->pb, 8, 'J'); // S3D_video_format_signaling_identifier
466  put_bits(&s->pb, 8, 'P');
467  put_bits(&s->pb, 8, '3');
468  put_bits(&s->pb, 8, 'D');
469  put_bits(&s->pb, 8, 0x03); // S3D_video_format_length
470 
471  put_bits(&s->pb, 1, 1); // reserved_bit
472  put_bits(&s->pb, 7, fpa_type); // S3D_video_format_type
473  put_bits(&s->pb, 8, 0x04); // reserved_data[0]
474  put_bits(&s->pb, 8, 0xFF); // reserved_data[1]
475  }
476  }
477 
478  s->mb_y = 0;
480 }
481 
482 static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
483  int has_mv, int field_motion)
484 {
485  put_bits(&s->pb, n, bits);
486  if (!s->frame_pred_frame_dct) {
487  if (has_mv)
488  /* motion_type: frame/field */
489  put_bits(&s->pb, 2, 2 - field_motion);
490  put_bits(&s->pb, 1, s->interlaced_dct);
491  }
492 }
493 
494 // RAL: Parameter added: f_or_b_code
495 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
496 {
497  if (val == 0) {
498  /* zero vector */
499  put_bits(&s->pb,
502  } else {
503  int code, sign, bits;
504  int bit_size = f_or_b_code - 1;
505  int range = 1 << bit_size;
506  /* modulo encoding */
507  val = sign_extend(val, 5 + bit_size);
508 
509  if (val >= 0) {
510  val--;
511  code = (val >> bit_size) + 1;
512  bits = val & (range - 1);
513  sign = 0;
514  } else {
515  val = -val;
516  val--;
517  code = (val >> bit_size) + 1;
518  bits = val & (range - 1);
519  sign = 1;
520  }
521 
522  assert(code > 0 && code <= 16);
523 
524  put_bits(&s->pb,
527 
528  put_bits(&s->pb, 1, sign);
529  if (bit_size > 0)
530  put_bits(&s->pb, bit_size, bits);
531  }
532 }
533 
534 static inline void encode_dc(MpegEncContext *s, int diff, int component)
535 {
536  if (((unsigned) (diff + 255)) >= 511) {
537  int index;
538 
539  if (diff < 0) {
540  index = av_log2_16bit(-2 * diff);
541  diff--;
542  } else {
543  index = av_log2_16bit(2 * diff);
544  }
545  if (component == 0)
546  put_bits(&s->pb,
547  ff_mpeg12_vlc_dc_lum_bits[index] + index,
548  (ff_mpeg12_vlc_dc_lum_code[index] << index) +
549  (diff & ((1 << index) - 1)));
550  else
551  put_bits(&s->pb,
552  ff_mpeg12_vlc_dc_chroma_bits[index] + index,
553  (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
554  (diff & ((1 << index) - 1)));
555  } else {
556  if (component == 0)
557  put_bits(&s->pb,
558  mpeg1_lum_dc_uni[diff + 255] & 0xFF,
559  mpeg1_lum_dc_uni[diff + 255] >> 8);
560  else
561  put_bits(&s->pb,
562  mpeg1_chr_dc_uni[diff + 255] & 0xFF,
563  mpeg1_chr_dc_uni[diff + 255] >> 8);
564  }
565 }
566 
567 static void mpeg1_encode_block(MpegEncContext *s, int16_t *block, int n)
568 {
569  int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
570  int code, component;
571  const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
572 
573  last_index = s->block_last_index[n];
574 
575  /* DC coef */
576  if (s->mb_intra) {
577  component = (n <= 3 ? 0 : (n & 1) + 1);
578  dc = block[0]; /* overflow is impossible */
579  diff = dc - s->last_dc[component];
580  encode_dc(s, diff, component);
581  s->last_dc[component] = dc;
582  i = 1;
583  if (s->intra_vlc_format)
584  table_vlc = ff_rl_mpeg2.table_vlc;
585  } else {
586  /* encode the first coefficient: needs to be done here because
587  * it is handled slightly differently */
588  level = block[0];
589  if (abs(level) == 1) {
590  code = ((uint32_t)level >> 31); /* the sign bit */
591  put_bits(&s->pb, 2, code | 0x02);
592  i = 1;
593  } else {
594  i = 0;
595  last_non_zero = -1;
596  goto next_coef;
597  }
598  }
599 
600  /* now quantify & encode AC coefs */
601  last_non_zero = i - 1;
602 
603  for (; i <= last_index; i++) {
604  j = s->intra_scantable.permutated[i];
605  level = block[j];
606 
607 next_coef:
608  /* encode using VLC */
609  if (level != 0) {
610  run = i - last_non_zero - 1;
611 
612  alevel = level;
613  MASK_ABS(sign, alevel);
614  sign &= 1;
615 
616  if (alevel <= mpeg1_max_level[0][run]) {
617  code = mpeg1_index_run[0][run] + alevel - 1;
618  /* store the VLC & sign at once */
619  put_bits(&s->pb, table_vlc[code][1] + 1,
620  (table_vlc[code][0] << 1) + sign);
621  } else {
622  /* escape seems to be pretty rare <5% so I do not optimize it */
623  put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
624  /* escape: only clip in this case */
625  put_bits(&s->pb, 6, run);
626  if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
627  if (alevel < 128) {
628  put_sbits(&s->pb, 8, level);
629  } else {
630  if (level < 0)
631  put_bits(&s->pb, 16, 0x8001 + level + 255);
632  else
633  put_sbits(&s->pb, 16, level);
634  }
635  } else {
636  put_sbits(&s->pb, 12, level);
637  }
638  }
639  last_non_zero = i;
640  }
641  }
642  /* end of block */
643  put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
644 }
645 
647  int16_t block[6][64],
648  int motion_x, int motion_y,
649  int mb_block_count)
650 {
651  int i, cbp;
652  const int mb_x = s->mb_x;
653  const int mb_y = s->mb_y;
654  const int first_mb = mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
655 
656  /* compute cbp */
657  cbp = 0;
658  for (i = 0; i < mb_block_count; i++)
659  if (s->block_last_index[i] >= 0)
660  cbp |= 1 << (mb_block_count - 1 - i);
661 
662  if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
663  (mb_x != s->mb_width - 1 ||
664  (mb_y != s->mb_height - 1 && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)) &&
665  ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) ||
666  (s->pict_type == AV_PICTURE_TYPE_B && s->mv_dir == s->last_mv_dir &&
667  (((s->mv_dir & MV_DIR_FORWARD)
668  ? ((s->mv[0][0][0] - s->last_mv[0][0][0]) |
669  (s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
670  ((s->mv_dir & MV_DIR_BACKWARD)
671  ? ((s->mv[1][0][0] - s->last_mv[1][0][0]) |
672  (s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
673  s->mb_skip_run++;
674  s->qscale -= s->dquant;
675  s->skip_count++;
676  s->misc_bits++;
677  s->last_bits++;
678  if (s->pict_type == AV_PICTURE_TYPE_P) {
679  s->last_mv[0][0][0] =
680  s->last_mv[0][0][1] =
681  s->last_mv[0][1][0] =
682  s->last_mv[0][1][1] = 0;
683  }
684  } else {
685  if (first_mb) {
686  assert(s->mb_skip_run == 0);
687  encode_mb_skip_run(s, s->mb_x);
688  } else {
690  }
691 
692  if (s->pict_type == AV_PICTURE_TYPE_I) {
693  if (s->dquant && cbp) {
694  /* macroblock_type: macroblock_quant = 1 */
695  put_mb_modes(s, 2, 1, 0, 0);
696  put_qscale(s);
697  } else {
698  /* macroblock_type: macroblock_quant = 0 */
699  put_mb_modes(s, 1, 1, 0, 0);
700  s->qscale -= s->dquant;
701  }
702  s->misc_bits += get_bits_diff(s);
703  s->i_count++;
704  } else if (s->mb_intra) {
705  if (s->dquant && cbp) {
706  put_mb_modes(s, 6, 0x01, 0, 0);
707  put_qscale(s);
708  } else {
709  put_mb_modes(s, 5, 0x03, 0, 0);
710  s->qscale -= s->dquant;
711  }
712  s->misc_bits += get_bits_diff(s);
713  s->i_count++;
714  memset(s->last_mv, 0, sizeof(s->last_mv));
715  } else if (s->pict_type == AV_PICTURE_TYPE_P) {
716  if (s->mv_type == MV_TYPE_16X16) {
717  if (cbp != 0) {
718  if ((motion_x | motion_y) == 0) {
719  if (s->dquant) {
720  /* macroblock_pattern & quant */
721  put_mb_modes(s, 5, 1, 0, 0);
722  put_qscale(s);
723  } else {
724  /* macroblock_pattern only */
725  put_mb_modes(s, 2, 1, 0, 0);
726  }
727  s->misc_bits += get_bits_diff(s);
728  } else {
729  if (s->dquant) {
730  put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
731  put_qscale(s);
732  } else {
733  put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
734  }
735  s->misc_bits += get_bits_diff(s);
736  // RAL: f_code parameter added
738  motion_x - s->last_mv[0][0][0],
739  s->f_code);
740  // RAL: f_code parameter added
742  motion_y - s->last_mv[0][0][1],
743  s->f_code);
744  s->mv_bits += get_bits_diff(s);
745  }
746  } else {
747  put_bits(&s->pb, 3, 1); /* motion only */
748  if (!s->frame_pred_frame_dct)
749  put_bits(&s->pb, 2, 2); /* motion_type: frame */
750  s->misc_bits += get_bits_diff(s);
751  // RAL: f_code parameter added
753  motion_x - s->last_mv[0][0][0],
754  s->f_code);
755  // RAL: f_code parameter added
757  motion_y - s->last_mv[0][0][1],
758  s->f_code);
759  s->qscale -= s->dquant;
760  s->mv_bits += get_bits_diff(s);
761  }
762  s->last_mv[0][1][0] = s->last_mv[0][0][0] = motion_x;
763  s->last_mv[0][1][1] = s->last_mv[0][0][1] = motion_y;
764  } else {
765  assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
766 
767  if (cbp) {
768  if (s->dquant) {
769  put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
770  put_qscale(s);
771  } else {
772  put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
773  }
774  } else {
775  put_bits(&s->pb, 3, 1); /* motion only */
776  put_bits(&s->pb, 2, 1); /* motion_type: field */
777  s->qscale -= s->dquant;
778  }
779  s->misc_bits += get_bits_diff(s);
780  for (i = 0; i < 2; i++) {
781  put_bits(&s->pb, 1, s->field_select[0][i]);
783  s->mv[0][i][0] - s->last_mv[0][i][0],
784  s->f_code);
786  s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
787  s->f_code);
788  s->last_mv[0][i][0] = s->mv[0][i][0];
789  s->last_mv[0][i][1] = 2 * s->mv[0][i][1];
790  }
791  s->mv_bits += get_bits_diff(s);
792  }
793  if (cbp) {
794  if (s->chroma_y_shift) {
795  put_bits(&s->pb,
796  ff_mpeg12_mbPatTable[cbp][1],
797  ff_mpeg12_mbPatTable[cbp][0]);
798  } else {
799  put_bits(&s->pb,
800  ff_mpeg12_mbPatTable[cbp >> 2][1],
801  ff_mpeg12_mbPatTable[cbp >> 2][0]);
802  put_sbits(&s->pb, 2, cbp);
803  }
804  }
805  s->f_count++;
806  } else {
807  if (s->mv_type == MV_TYPE_16X16) {
808  if (cbp) { // With coded bloc pattern
809  if (s->dquant) {
810  if (s->mv_dir == MV_DIR_FORWARD)
811  put_mb_modes(s, 6, 3, 1, 0);
812  else
813  put_mb_modes(s, 8 - s->mv_dir, 2, 1, 0);
814  put_qscale(s);
815  } else {
816  put_mb_modes(s, 5 - s->mv_dir, 3, 1, 0);
817  }
818  } else { // No coded bloc pattern
819  put_bits(&s->pb, 5 - s->mv_dir, 2);
820  if (!s->frame_pred_frame_dct)
821  put_bits(&s->pb, 2, 2); /* motion_type: frame */
822  s->qscale -= s->dquant;
823  }
824  s->misc_bits += get_bits_diff(s);
825  if (s->mv_dir & MV_DIR_FORWARD) {
827  s->mv[0][0][0] - s->last_mv[0][0][0],
828  s->f_code);
830  s->mv[0][0][1] - s->last_mv[0][0][1],
831  s->f_code);
832  s->last_mv[0][0][0] =
833  s->last_mv[0][1][0] = s->mv[0][0][0];
834  s->last_mv[0][0][1] =
835  s->last_mv[0][1][1] = s->mv[0][0][1];
836  s->f_count++;
837  }
838  if (s->mv_dir & MV_DIR_BACKWARD) {
840  s->mv[1][0][0] - s->last_mv[1][0][0],
841  s->b_code);
843  s->mv[1][0][1] - s->last_mv[1][0][1],
844  s->b_code);
845  s->last_mv[1][0][0] =
846  s->last_mv[1][1][0] = s->mv[1][0][0];
847  s->last_mv[1][0][1] =
848  s->last_mv[1][1][1] = s->mv[1][0][1];
849  s->b_count++;
850  }
851  } else {
852  assert(s->mv_type == MV_TYPE_FIELD);
853  assert(!s->frame_pred_frame_dct);
854  if (cbp) { // With coded bloc pattern
855  if (s->dquant) {
856  if (s->mv_dir == MV_DIR_FORWARD)
857  put_mb_modes(s, 6, 3, 1, 1);
858  else
859  put_mb_modes(s, 8 - s->mv_dir, 2, 1, 1);
860  put_qscale(s);
861  } else {
862  put_mb_modes(s, 5 - s->mv_dir, 3, 1, 1);
863  }
864  } else { // No coded bloc pattern
865  put_bits(&s->pb, 5 - s->mv_dir, 2);
866  put_bits(&s->pb, 2, 1); /* motion_type: field */
867  s->qscale -= s->dquant;
868  }
869  s->misc_bits += get_bits_diff(s);
870  if (s->mv_dir & MV_DIR_FORWARD) {
871  for (i = 0; i < 2; i++) {
872  put_bits(&s->pb, 1, s->field_select[0][i]);
874  s->mv[0][i][0] - s->last_mv[0][i][0],
875  s->f_code);
877  s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
878  s->f_code);
879  s->last_mv[0][i][0] = s->mv[0][i][0];
880  s->last_mv[0][i][1] = s->mv[0][i][1] * 2;
881  }
882  s->f_count++;
883  }
884  if (s->mv_dir & MV_DIR_BACKWARD) {
885  for (i = 0; i < 2; i++) {
886  put_bits(&s->pb, 1, s->field_select[1][i]);
888  s->mv[1][i][0] - s->last_mv[1][i][0],
889  s->b_code);
891  s->mv[1][i][1] - (s->last_mv[1][i][1] >> 1),
892  s->b_code);
893  s->last_mv[1][i][0] = s->mv[1][i][0];
894  s->last_mv[1][i][1] = s->mv[1][i][1] * 2;
895  }
896  s->b_count++;
897  }
898  }
899  s->mv_bits += get_bits_diff(s);
900  if (cbp) {
901  if (s->chroma_y_shift) {
902  put_bits(&s->pb,
903  ff_mpeg12_mbPatTable[cbp][1],
904  ff_mpeg12_mbPatTable[cbp][0]);
905  } else {
906  put_bits(&s->pb,
907  ff_mpeg12_mbPatTable[cbp >> 2][1],
908  ff_mpeg12_mbPatTable[cbp >> 2][0]);
909  put_sbits(&s->pb, 2, cbp);
910  }
911  }
912  }
913  for (i = 0; i < mb_block_count; i++)
914  if (cbp & (1 << (mb_block_count - 1 - i)))
915  mpeg1_encode_block(s, block[i], i);
916  s->mb_skip_run = 0;
917  if (s->mb_intra)
918  s->i_tex_bits += get_bits_diff(s);
919  else
920  s->p_tex_bits += get_bits_diff(s);
921  }
922 }
923 
924 void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[6][64],
925  int motion_x, int motion_y)
926 {
927  if (s->chroma_format == CHROMA_420)
928  mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
929  else
930  mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
931 }
932 
934 {
935  static int done = 0;
936 
938 
939  if (!done) {
940  int f_code;
941  int mv;
942  int i;
943 
944  done = 1;
947 
948  for (i = 0; i < 64; i++) {
949  mpeg1_max_level[0][i] = ff_rl_mpeg1.max_level[0][i];
950  mpeg1_index_run[0][i] = ff_rl_mpeg1.index_run[0][i];
951  }
952 
954  if (s->intra_vlc_format)
956 
957  /* build unified dc encoding tables */
958  for (i = -255; i < 256; i++) {
959  int adiff, index;
960  int bits, code;
961  int diff = i;
962 
963  adiff = FFABS(diff);
964  if (diff < 0)
965  diff--;
966  index = av_log2(2 * adiff);
967 
969  code = (ff_mpeg12_vlc_dc_lum_code[index] << index) +
970  (diff & ((1 << index) - 1));
971  mpeg1_lum_dc_uni[i + 255] = bits + (code << 8);
972 
975  (diff & ((1 << index) - 1));
976  mpeg1_chr_dc_uni[i + 255] = bits + (code << 8);
977  }
978 
979  for (f_code = 1; f_code <= MAX_FCODE; f_code++)
980  for (mv = -MAX_MV; mv <= MAX_MV; mv++) {
981  int len;
982 
983  if (mv == 0) {
984  len = ff_mpeg12_mbMotionVectorTable[0][1];
985  } else {
986  int val, bit_size, code;
987 
988  bit_size = f_code - 1;
989 
990  val = mv;
991  if (val < 0)
992  val = -val;
993  val--;
994  code = (val >> bit_size) + 1;
995  if (code < 17)
996  len = ff_mpeg12_mbMotionVectorTable[code][1] +
997  1 + bit_size;
998  else
999  len = ff_mpeg12_mbMotionVectorTable[16][1] +
1000  2 + bit_size;
1001  }
1002 
1003  mv_penalty[f_code][mv + MAX_MV] = len;
1004  }
1005 
1006 
1007  for (f_code = MAX_FCODE; f_code > 0; f_code--)
1008  for (mv = -(8 << f_code); mv < (8 << f_code); mv++)
1009  fcode_tab[mv + MAX_MV] = f_code;
1010  }
1011  s->me.mv_penalty = mv_penalty;
1012  s->fcode_tab = fcode_tab;
1013  if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1014  s->min_qcoeff = -255;
1015  s->max_qcoeff = 255;
1016  } else {
1017  s->min_qcoeff = -2047;
1018  s->max_qcoeff = 2047;
1019  }
1020  if (s->intra_vlc_format) {
1021  s->intra_ac_vlc_length =
1023  } else {
1024  s->intra_ac_vlc_length =
1026  }
1027  s->inter_ac_vlc_length =
1029 }
1030 
1031 #define OFFSET(x) offsetof(MpegEncContext, x)
1032 #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
1033 #define COMMON_OPTS \
1034  { "intra_vlc", "Use MPEG-2 intra VLC table.", \
1035  OFFSET(intra_vlc_format), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \
1036  { "drop_frame_timecode", "Timecode is in drop frame format.", \
1037  OFFSET(drop_frame_timecode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \
1038  { "scan_offset", "Reserve space for SVCD scan offset user data.", \
1039  OFFSET(scan_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1040 
1041 static const AVOption mpeg1_options[] = {
1042  COMMON_OPTS
1044  { NULL },
1045 };
1046 
1047 static const AVOption mpeg2_options[] = {
1048  COMMON_OPTS
1049  { "non_linear_quant", "Use nonlinear quantizer.", OFFSET(q_scale_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1050  { "alternate_scan", "Enable alternate scantable.", OFFSET(alternate_scan), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1052  { NULL },
1053 };
1054 
1055 #define mpeg12_class(x) \
1056 static const AVClass mpeg ## x ## _class = { \
1057  .class_name = "mpeg" # x "video encoder", \
1058  .item_name = av_default_item_name, \
1059  .option = mpeg ## x ## _options, \
1060  .version = LIBAVUTIL_VERSION_INT, \
1061 };
1062 
1064 mpeg12_class(2)
1065 
1066 AVCodec ff_mpeg1video_encoder = {
1067  .name = "mpeg1video",
1068  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
1069  .type = AVMEDIA_TYPE_VIDEO,
1070  .id = AV_CODEC_ID_MPEG1VIDEO,
1071  .priv_data_size = sizeof(MpegEncContext),
1072  .init = encode_init,
1073  .encode2 = ff_MPV_encode_picture,
1075  .supported_framerates = ff_mpeg12_frame_rate_tab + 1,
1076  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
1077  AV_PIX_FMT_NONE },
1078  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
1079  .priv_class = &mpeg1_class,
1080 };
1081 
1083  .name = "mpeg2video",
1084  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
1085  .type = AVMEDIA_TYPE_VIDEO,
1086  .id = AV_CODEC_ID_MPEG2VIDEO,
1087  .priv_data_size = sizeof(MpegEncContext),
1088  .init = encode_init,
1089  .encode2 = ff_MPV_encode_picture,
1091  .supported_framerates = ff_mpeg12_frame_rate_tab + 1,
1092  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
1094  AV_PIX_FMT_NONE },
1095  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
1096  .priv_class = &mpeg2_class,
1097 };
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:2332
av_cold void ff_mpeg1_encode_init(MpegEncContext *s)
Definition: mpeg12enc.c:933
static uint8_t uni_mpeg1_ac_vlc_len[64 *64 *2]
Definition: mpeg12enc.c:55
#define MASK_ABS(mask, level)
Definition: mathops.h:148
AVCodec ff_mpeg2video_encoder
Definition: mpeg12enc.c:1082
int aspect_ratio_info
Definition: mpegvideo.h:587
int picture_number
Definition: mpegvideo.h:298
AVOption.
Definition: opt.h:233
uint8_t * fcode_tab
smallest fcode needed for each MV
Definition: mpegvideo.h:454
#define MV_TYPE_FIELD
2 vectors, one per field
Definition: mpegvideo.h:444
int last_mv[2][2][2]
last MV, used for MV prediction in MPEG1 & B-frame MPEG4
Definition: mpegvideo.h:453
static int find_frame_rate_index(MpegEncContext *s)
Definition: mpeg12enc.c:101
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:177
Views are next to each other, but when upscaling apply a checkerboard pattern.
Definition: stereo3d.h:84
int ff_MPV_encode_end(AVCodecContext *avctx)
#define FF_MPV_COMMON_OPTS
Definition: mpegvideo.h:740
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:280
void avpriv_align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
Definition: bitstream.c:45
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:1422
int scan_offset
reserve space for SVCD scan offset user data.
Definition: mpegvideo.h:669
int min_qcoeff
minimum encodable coefficient
Definition: mpegvideo.h:499
mpegvideo header.
uint8_t permutated[64]
Definition: dsputil.h:113
const uint16_t ff_mpeg12_vlc_dc_lum_code[12]
Definition: mpeg12data.c:52
uint8_t run
Definition: svq3.c:142
uint8_t * intra_ac_vlc_length
Definition: mpegvideo.h:502
#define UNI_AC_ENC_INDEX(run, level)
Definition: mpegvideo.h:507
#define SLICE_MIN_START_CODE
Definition: mpegvideo.h:83
int profile
profile
Definition: avcodec.h:2596
AVCodec.
Definition: avcodec.h:2755
int qscale
QP.
Definition: mpegvideo.h:392
RLTable.
Definition: rl.h:38
int field_select[2][2]
Definition: mpegvideo.h:452
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:1173
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:469
#define FF_LEVEL_UNKNOWN
Definition: avcodec.h:2680
uint8_t bits
Definition: crc.c:216
uint8_t
#define av_cold
Definition: attributes.h:66
static uint32_t mpeg1_lum_dc_uni[512]
Definition: mpeg12enc.c:60
#define PICT_FRAME
Definition: mpegvideo.h:646
AVOptions.
static void encode_mb_skip_run(MpegEncContext *s, int run)
Definition: mpeg12enc.c:319
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:120
int me_range
maximum motion estimation search range in subpel units If 0 then no limit.
Definition: avcodec.h:1529
const float ff_mpeg1_aspect[16]
Definition: mpeg12data.c:328
int misc_bits
cbp, mb_type
Definition: mpegvideo.h:540
static const AVOption mpeg1_options[]
Definition: mpeg12enc.c:1041
static void mpeg1_encode_block(MpegEncContext *s, int16_t *block, int n)
Definition: mpeg12enc.c:567
int interlaced_dct
Definition: mpegvideo.h:666
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:366
#define CHROMA_420
Definition: mpegvideo.h:658
int intra_dc_precision
Definition: mpegvideo.h:648
int repeat_first_field
Definition: mpegvideo.h:655
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
uint8_t(* mv_penalty)[MAX_MV *2+1]
amount of bits needed to encode a MV
Definition: mpegvideo.h:253
static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV *2+1]
Definition: mpeg12enc.c:52
static int8_t mpeg1_max_level[2][64]
Definition: mpeg12enc.c:64
int8_t * max_level[2]
encoding & decoding
Definition: rl.h:45
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:300
int max_qcoeff
maximum encodable coefficient
Definition: mpegvideo.h:500
int dquant
qscale difference to prev qscale
Definition: mpegvideo.h:398
int gop_picture_number
index of the first picture of a GOP based on fake_pic_num & mpeg1 specific
Definition: mpegvideo.h:635
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition: avcodec.h:2331
int ff_MPV_encode_init(AVCodecContext *avctx)
static int get_bits_diff(MpegEncContext *s)
Definition: mpegvideo.h:846
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
int last_dc[3]
last DC values for MPEG1
Definition: mpegvideo.h:371
static void put_mb_modes(MpegEncContext *s, int n, int bits, int has_mv, int field_motion)
Definition: mpeg12enc.c:482
uint8_t * inter_ac_vlc_last_length
Definition: mpegvideo.h:505
#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:740
int chroma_y_shift
Definition: mpegvideo.h:662
int strict_std_compliance
strictly follow the std (MPEG4, ...)
Definition: mpegvideo.h:288
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
static const uint8_t inv_non_linear_qscale[]
Definition: mpeg12enc.c:43
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:534
uint8_t * buf
Definition: put_bits.h:43
int rc_max_rate
maximum bitrate
Definition: avcodec.h:2115
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:2762
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:139
int low_delay
no reordering needed / has no b-frames
Definition: mpegvideo.h:591
Video is not stereoscopic (and metadata has to be there).
Definition: stereo3d.h:32
static void mpeg1_encode_sequence_header(MpegEncContext *s)
Definition: mpeg12enc.c:195
static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
Definition: mpeg12enc.c:495
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:72
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
int resync_mb_x
x position of last resync marker
Definition: mpegvideo.h:544
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2093
uint8_t * intra_ac_vlc_last_length
Definition: mpegvideo.h:503
#define mpeg12_class(x)
Definition: mpeg12enc.c:1055
int intra_vlc_format
Definition: mpegvideo.h:653
int64_t timecode_frame_start
GOP timecode frame start number, in non drop frame format.
Definition: avcodec.h:2239
int progressive_frame
Definition: mpegvideo.h:664
uint8_t * vbv_delay_ptr
pointer to vbv_delay in the bitstream
Definition: mpegvideo.h:637
const uint16_t(* table_vlc)[2]
Definition: rl.h:41
static uint8_t fcode_tab[MAX_MV *2+1]
Definition: mpeg12enc.c:53
int width
picture width / height.
Definition: avcodec.h:1217
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:370
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2597
RLTable ff_rl_mpeg1
Definition: mpeg12data.c:166
int alternate_scan
Definition: mpegvideo.h:654
#define GOP_START_CODE
Definition: mpegvideo.h:81
static uint8_t uni_mpeg2_ac_vlc_len[64 *64 *2]
Definition: mpeg12enc.c:56
#define FFABS(a)
Definition: common.h:52
int level
level
Definition: avcodec.h:2679
#define VE
Definition: mpeg12enc.c:1032
int block_last_index[12]
last non zero coefficient in block
Definition: mpegvideo.h:314
MotionEstContext me
Definition: mpegvideo.h:457
#define EXT_START_CODE
Definition: cavs.h:32
static av_cold void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len)
Definition: mpeg12enc.c:66
static void put_header(MpegEncContext *s, int header)
Definition: mpeg12enc.c:187
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:110
void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
if(ac->has_optimized_func)
static const uint8_t svcd_scan_offset_placeholder[]
Definition: mpeg12enc.c:47
static const int8_t mv[256][2]
Definition: 4xm.c:72
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:441
int frame_pred_frame_dct
Definition: mpegvideo.h:649
NULL
Definition: eval.c:55
#define MV_DIR_BACKWARD
Definition: mpegvideo.h:438
int coded_picture_number
picture number in bitstream order
Definition: frame.h:198
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:124
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
int concealment_motion_vectors
Definition: mpegvideo.h:651
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1054
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:489
ScanTable intra_scantable
Definition: mpegvideo.h:319
#define USER_START_CODE
Definition: cavs.h:33
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:268
MPEG1/2 tables.
uint8_t * data
Definition: frame.h:76
uint8_t * inter_ac_vlc_length
Definition: mpegvideo.h:504
static uint8_t mpeg1_index_run[2][64]
Definition: mpeg12enc.c:63
int progressive_sequence
Definition: mpegvideo.h:640
uint16_t * intra_matrix
custom intra quantization matrix
Definition: avcodec.h:1581
int index
Definition: gxfenc.c:72
rational number numerator/denominator
Definition: rational.h:43
#define COMMON_OPTS
Definition: mpeg12enc.c:1033
uint8_t * index_run[2]
encoding only
Definition: rl.h:44
#define OFFSET(x)
Definition: mpeg12enc.c:1031
static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y, int mb_block_count)
Definition: mpeg12enc.c:646
#define MAX_MV
Definition: mpegvideo.h:62
void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
Definition: mpeg12enc.c:353
int f_code
forward MV resolution
Definition: mpegvideo.h:415
#define MAX_FCODE
Definition: mpegvideo.h:61
int ff_MPV_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
#define CODEC_FLAG_CLOSED_GOP
Definition: avcodec.h:690
#define MV_DIR_FORWARD
Definition: mpegvideo.h:437
uint16_t * inter_matrix
custom inter quantization matrix
Definition: avcodec.h:1588
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:399
int bit_rate
wanted bit rate
Definition: mpegvideo.h:271
av_cold void ff_mpeg12_common_init(MpegEncContext *s)
Definition: mpeg12.c:109
Views are on top of each other.
Definition: stereo3d.h:52
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:123
int last_mv_dir
last mv_dir, used for b frame encoding
Definition: mpegvideo.h:636
uint8_t level
Definition: svq3.c:143
int mv[2][4][2]
motion vectors for a macroblock first coordinate : 0 = forward 1 = backward second " : depend...
Definition: mpegvideo.h:451
static av_cold int encode_init(AVCodecContext *avctx)
Definition: mpeg12enc.c:129
MpegEncContext.
Definition: mpegvideo.h:264
Views are next to each other.
Definition: stereo3d.h:42
struct AVCodecContext * avctx
Definition: mpegvideo.h:266
PutBitContext pb
bit output
Definition: mpegvideo.h:337
const AVRational ff_mpeg2_aspect[16]
Definition: mpeg12data.c:349
void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
Definition: mpeg12enc.c:924
static av_always_inline void put_qscale(MpegEncContext *s)
Definition: mpeg12enc.c:329
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
#define CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:786
static uint32_t mpeg1_chr_dc_uni[512]
Definition: mpeg12enc.c:61
static const AVOption mpeg2_options[]
Definition: mpeg12enc.c:1047
uint8_t ff_mpeg12_static_rl_table_store[2][2][2 *MAX_RUN+MAX_LEVEL+3]
Definition: mpeg12.c:39
Bi-dir predicted.
Definition: avutil.h:255
const uint8_t ff_mpeg12_mbPatTable[64][2]
Definition: mpeg12data.c:221
int den
denominator
Definition: rational.h:45
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:498
void * priv_data
Definition: avcodec.h:1090
void ff_mpeg1_encode_slice_header(MpegEncContext *s)
Definition: mpeg12enc.c:339
int last_bits
temp var used for calculating the above vars
Definition: mpegvideo.h:541
int frame_rate_index
Definition: mpegvideo.h:403
int picture_structure
Definition: mpegvideo.h:642
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:297
int len
#define av_log2_16bit
Definition: intmath.h:86
#define SEQ_START_CODE
Definition: mpegvideo.h:80
int drop_frame_timecode
timecode is in drop frame format.
Definition: mpegvideo.h:668
#define av_log2
Definition: intmath.h:85
int resync_mb_y
y position of last resync marker
Definition: mpegvideo.h:545
av_cold void ff_init_rl(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Definition: mpegvideo.c:1479
const uint8_t ff_mpeg12_mbAddrIncrTable[36][2]
Definition: mpeg12data.c:182
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:163
struct AVFrame f
Definition: mpegvideo.h:100
int flags
AVCodecContext.flags (HQ, MV4, ...)
Definition: mpegvideo.h:283
const uint8_t ff_mpeg12_mbMotionVectorTable[17][2]
Definition: mpeg12data.c:288
#define PICTURE_START_CODE
Definition: mpegvideo.h:82
#define av_always_inline
Definition: attributes.h:40
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=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);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_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
int b_code
backward MV resolution for B Frames (mpeg4)
Definition: mpegvideo.h:416
Stereoscopic 3d metadata.
Definition: frame.h:62
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
const AVRational ff_mpeg12_frame_rate_tab[16]
Definition: mpeg12data.c:308
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2327
Predicted.
Definition: avutil.h:254
static int16_t block[64]
Definition: dct-test.c:170