FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mpeg4videoenc.c
Go to the documentation of this file.
1 /*
2  * MPEG-4 encoder
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * Copyright (c) 2002-2010 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 #include "libavutil/attributes.h"
24 #include "libavutil/log.h"
25 #include "libavutil/opt.h"
26 #include "mpegutils.h"
27 #include "mpegvideo.h"
28 #include "h263.h"
29 #include "mpeg4video.h"
30 
31 /* The uni_DCtab_* tables below contain unified bits+length tables to encode DC
32  * differences in MPEG-4. Unified in the sense that the specification specifies
33  * this encoding in several steps. */
36 static uint16_t uni_DCtab_lum_bits[512];
37 static uint16_t uni_DCtab_chrom_bits[512];
38 
39 /* Unified encoding tables for run length encoding of coefficients.
40  * Unified in the sense that the specification specifies the encoding in several steps. */
41 static uint32_t uni_mpeg4_intra_rl_bits[64 * 64 * 2 * 2];
42 static uint8_t uni_mpeg4_intra_rl_len[64 * 64 * 2 * 2];
43 static uint32_t uni_mpeg4_inter_rl_bits[64 * 64 * 2 * 2];
44 static uint8_t uni_mpeg4_inter_rl_len[64 * 64 * 2 * 2];
45 
46 //#define UNI_MPEG4_ENC_INDEX(last, run, level) ((last) * 128 + (run) * 256 + (level))
47 //#define UNI_MPEG4_ENC_INDEX(last, run, level) ((last) * 128 * 64 + (run) + (level) * 64)
48 #define UNI_MPEG4_ENC_INDEX(last, run, level) ((last) * 128 * 64 + (run) * 128 + (level))
49 
50 /* MPEG-4
51  * inter
52  * max level: 24/6
53  * max run: 53/63
54  *
55  * intra
56  * max level: 53/16
57  * max run: 29/41
58  */
59 
60 /**
61  * Return the number of bits that encoding the 8x8 block in block would need.
62  * @param[in] block_last_index last index in scantable order that refers to a non zero element in block.
63  */
64 static inline int get_block_rate(MpegEncContext *s, int16_t block[64],
65  int block_last_index, uint8_t scantable[64])
66 {
67  int last = 0;
68  int j;
69  int rate = 0;
70 
71  for (j = 1; j <= block_last_index; j++) {
72  const int index = scantable[j];
73  int level = block[index];
74  if (level) {
75  level += 64;
76  if ((level & (~127)) == 0) {
77  if (j < block_last_index)
78  rate += s->intra_ac_vlc_length[UNI_AC_ENC_INDEX(j - last - 1, level)];
79  else
80  rate += s->intra_ac_vlc_last_length[UNI_AC_ENC_INDEX(j - last - 1, level)];
81  } else
82  rate += s->ac_esc_length;
83 
84  last = j;
85  }
86  }
87 
88  return rate;
89 }
90 
91 /**
92  * Restore the ac coefficients in block that have been changed by decide_ac_pred().
93  * This function also restores s->block_last_index.
94  * @param[in,out] block MB coefficients, these will be restored
95  * @param[in] dir ac prediction direction for each 8x8 block
96  * @param[out] st scantable for each 8x8 block
97  * @param[in] zigzag_last_index index referring to the last non zero coefficient in zigzag order
98  */
99 static inline void restore_ac_coeffs(MpegEncContext *s, int16_t block[6][64],
100  const int dir[6], uint8_t *st[6],
101  const int zigzag_last_index[6])
102 {
103  int i, n;
104  memcpy(s->block_last_index, zigzag_last_index, sizeof(int) * 6);
105 
106  for (n = 0; n < 6; n++) {
107  int16_t *ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
108 
109  st[n] = s->intra_scantable.permutated;
110  if (dir[n]) {
111  /* top prediction */
112  for (i = 1; i < 8; i++)
113  block[n][s->idsp.idct_permutation[i]] = ac_val[i + 8];
114  } else {
115  /* left prediction */
116  for (i = 1; i < 8; i++)
117  block[n][s->idsp.idct_permutation[i << 3]] = ac_val[i];
118  }
119  }
120 }
121 
122 /**
123  * Return the optimal value (0 or 1) for the ac_pred element for the given MB in MPEG-4.
124  * This function will also update s->block_last_index and s->ac_val.
125  * @param[in,out] block MB coefficients, these will be updated if 1 is returned
126  * @param[in] dir ac prediction direction for each 8x8 block
127  * @param[out] st scantable for each 8x8 block
128  * @param[out] zigzag_last_index index referring to the last non zero coefficient in zigzag order
129  */
130 static inline int decide_ac_pred(MpegEncContext *s, int16_t block[6][64],
131  const int dir[6], uint8_t *st[6],
132  int zigzag_last_index[6])
133 {
134  int score = 0;
135  int i, n;
136  int8_t *const qscale_table = s->current_picture.qscale_table;
137 
138  memcpy(zigzag_last_index, s->block_last_index, sizeof(int) * 6);
139 
140  for (n = 0; n < 6; n++) {
141  int16_t *ac_val, *ac_val1;
142 
143  score -= get_block_rate(s, block[n], s->block_last_index[n],
145 
146  ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
147  ac_val1 = ac_val;
148  if (dir[n]) {
149  const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
150  /* top prediction */
151  ac_val -= s->block_wrap[n] * 16;
152  if (s->mb_y == 0 || s->qscale == qscale_table[xy] || n == 2 || n == 3) {
153  /* same qscale */
154  for (i = 1; i < 8; i++) {
155  const int level = block[n][s->idsp.idct_permutation[i]];
156  block[n][s->idsp.idct_permutation[i]] = level - ac_val[i + 8];
157  ac_val1[i] = block[n][s->idsp.idct_permutation[i << 3]];
158  ac_val1[i + 8] = level;
159  }
160  } else {
161  /* different qscale, we must rescale */
162  for (i = 1; i < 8; i++) {
163  const int level = block[n][s->idsp.idct_permutation[i]];
164  block[n][s->idsp.idct_permutation[i]] = level - ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
165  ac_val1[i] = block[n][s->idsp.idct_permutation[i << 3]];
166  ac_val1[i + 8] = level;
167  }
168  }
169  st[n] = s->intra_h_scantable.permutated;
170  } else {
171  const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
172  /* left prediction */
173  ac_val -= 16;
174  if (s->mb_x == 0 || s->qscale == qscale_table[xy] || n == 1 || n == 3) {
175  /* same qscale */
176  for (i = 1; i < 8; i++) {
177  const int level = block[n][s->idsp.idct_permutation[i << 3]];
178  block[n][s->idsp.idct_permutation[i << 3]] = level - ac_val[i];
179  ac_val1[i] = level;
180  ac_val1[i + 8] = block[n][s->idsp.idct_permutation[i]];
181  }
182  } else {
183  /* different qscale, we must rescale */
184  for (i = 1; i < 8; i++) {
185  const int level = block[n][s->idsp.idct_permutation[i << 3]];
186  block[n][s->idsp.idct_permutation[i << 3]] = level - ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
187  ac_val1[i] = level;
188  ac_val1[i + 8] = block[n][s->idsp.idct_permutation[i]];
189  }
190  }
191  st[n] = s->intra_v_scantable.permutated;
192  }
193 
194  for (i = 63; i > 0; i--) // FIXME optimize
195  if (block[n][st[n][i]])
196  break;
197  s->block_last_index[n] = i;
198 
199  score += get_block_rate(s, block[n], s->block_last_index[n], st[n]);
200  }
201 
202  if (score < 0) {
203  return 1;
204  } else {
205  restore_ac_coeffs(s, block, dir, st, zigzag_last_index);
206  return 0;
207  }
208 }
209 
210 /**
211  * modify mb_type & qscale so that encoding is actually possible in MPEG-4
212  */
214 {
215  int i;
216  int8_t *const qscale_table = s->current_picture.qscale_table;
217 
219 
220  if (s->pict_type == AV_PICTURE_TYPE_B) {
221  int odd = 0;
222  /* ok, come on, this isn't funny anymore, there's more code for
223  * handling this MPEG-4 mess than for the actual adaptive quantization */
224 
225  for (i = 0; i < s->mb_num; i++) {
226  int mb_xy = s->mb_index2xy[i];
227  odd += qscale_table[mb_xy] & 1;
228  }
229 
230  if (2 * odd > s->mb_num)
231  odd = 1;
232  else
233  odd = 0;
234 
235  for (i = 0; i < s->mb_num; i++) {
236  int mb_xy = s->mb_index2xy[i];
237  if ((qscale_table[mb_xy] & 1) != odd)
238  qscale_table[mb_xy]++;
239  if (qscale_table[mb_xy] > 31)
240  qscale_table[mb_xy] = 31;
241  }
242 
243  for (i = 1; i < s->mb_num; i++) {
244  int mb_xy = s->mb_index2xy[i];
245  if (qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i - 1]] &&
246  (s->mb_type[mb_xy] & CANDIDATE_MB_TYPE_DIRECT)) {
247  s->mb_type[mb_xy] |= CANDIDATE_MB_TYPE_BIDIR;
248  }
249  }
250  }
251 }
252 
253 /**
254  * Encode the dc value.
255  * @param n block index (0-3 are luma, 4-5 are chroma)
256  */
257 static inline void mpeg4_encode_dc(PutBitContext *s, int level, int n)
258 {
259 #if 1
260  /* DC will overflow if level is outside the [-255,255] range. */
261  level += 256;
262  if (n < 4) {
263  /* luminance */
264  put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]);
265  } else {
266  /* chrominance */
268  }
269 #else
270  int size, v;
271  /* find number of bits */
272  size = 0;
273  v = abs(level);
274  while (v) {
275  v >>= 1;
276  size++;
277  }
278 
279  if (n < 4) {
280  /* luminance */
281  put_bits(s, ff_mpeg4_DCtab_lum[size][1], ff_mpeg4_DCtab_lum[size][0]);
282  } else {
283  /* chrominance */
284  put_bits(s, ff_mpeg4_DCtab_chrom[size][1], ff_mpeg4_DCtab_chrom[size][0]);
285  }
286 
287  /* encode remaining bits */
288  if (size > 0) {
289  if (level < 0)
290  level = (-level) ^ ((1 << size) - 1);
291  put_bits(s, size, level);
292  if (size > 8)
293  put_bits(s, 1, 1);
294  }
295 #endif
296 }
297 
298 static inline int mpeg4_get_dc_length(int level, int n)
299 {
300  if (n < 4)
301  return uni_DCtab_lum_len[level + 256];
302  else
303  return uni_DCtab_chrom_len[level + 256];
304 }
305 
306 /**
307  * Encode an 8x8 block.
308  * @param n block index (0-3 are luma, 4-5 are chroma)
309  */
310 static inline void mpeg4_encode_block(MpegEncContext *s,
311  int16_t *block, int n, int intra_dc,
312  uint8_t *scan_table, PutBitContext *dc_pb,
313  PutBitContext *ac_pb)
314 {
315  int i, last_non_zero;
316  uint32_t *bits_tab;
317  uint8_t *len_tab;
318  const int last_index = s->block_last_index[n];
319 
320  if (s->mb_intra) { // Note gcc (3.2.1 at least) will optimize this away
321  /* MPEG-4 based DC predictor */
322  mpeg4_encode_dc(dc_pb, intra_dc, n);
323  if (last_index < 1)
324  return;
325  i = 1;
326  bits_tab = uni_mpeg4_intra_rl_bits;
327  len_tab = uni_mpeg4_intra_rl_len;
328  } else {
329  if (last_index < 0)
330  return;
331  i = 0;
332  bits_tab = uni_mpeg4_inter_rl_bits;
333  len_tab = uni_mpeg4_inter_rl_len;
334  }
335 
336  /* AC coefs */
337  last_non_zero = i - 1;
338  for (; i < last_index; i++) {
339  int level = block[scan_table[i]];
340  if (level) {
341  int run = i - last_non_zero - 1;
342  level += 64;
343  if ((level & (~127)) == 0) {
344  const int index = UNI_MPEG4_ENC_INDEX(0, run, level);
345  put_bits(ac_pb, len_tab[index], bits_tab[index]);
346  } else { // ESC3
347  put_bits(ac_pb,
348  7 + 2 + 1 + 6 + 1 + 12 + 1,
349  (3 << 23) + (3 << 21) + (0 << 20) + (run << 14) +
350  (1 << 13) + (((level - 64) & 0xfff) << 1) + 1);
351  }
352  last_non_zero = i;
353  }
354  }
355  /* if (i <= last_index) */ {
356  int level = block[scan_table[i]];
357  int run = i - last_non_zero - 1;
358  level += 64;
359  if ((level & (~127)) == 0) {
360  const int index = UNI_MPEG4_ENC_INDEX(1, run, level);
361  put_bits(ac_pb, len_tab[index], bits_tab[index]);
362  } else { // ESC3
363  put_bits(ac_pb,
364  7 + 2 + 1 + 6 + 1 + 12 + 1,
365  (3 << 23) + (3 << 21) + (1 << 20) + (run << 14) +
366  (1 << 13) + (((level - 64) & 0xfff) << 1) + 1);
367  }
368  }
369 }
370 
372  int16_t *block, int n,
373  int intra_dc, uint8_t *scan_table)
374 {
375  int i, last_non_zero;
376  uint8_t *len_tab;
377  const int last_index = s->block_last_index[n];
378  int len = 0;
379 
380  if (s->mb_intra) { // Note gcc (3.2.1 at least) will optimize this away
381  /* MPEG-4 based DC predictor */
382  len += mpeg4_get_dc_length(intra_dc, n);
383  if (last_index < 1)
384  return len;
385  i = 1;
386  len_tab = uni_mpeg4_intra_rl_len;
387  } else {
388  if (last_index < 0)
389  return 0;
390  i = 0;
391  len_tab = uni_mpeg4_inter_rl_len;
392  }
393 
394  /* AC coefs */
395  last_non_zero = i - 1;
396  for (; i < last_index; i++) {
397  int level = block[scan_table[i]];
398  if (level) {
399  int run = i - last_non_zero - 1;
400  level += 64;
401  if ((level & (~127)) == 0) {
402  const int index = UNI_MPEG4_ENC_INDEX(0, run, level);
403  len += len_tab[index];
404  } else { // ESC3
405  len += 7 + 2 + 1 + 6 + 1 + 12 + 1;
406  }
407  last_non_zero = i;
408  }
409  }
410  /* if (i <= last_index) */ {
411  int level = block[scan_table[i]];
412  int run = i - last_non_zero - 1;
413  level += 64;
414  if ((level & (~127)) == 0) {
415  const int index = UNI_MPEG4_ENC_INDEX(1, run, level);
416  len += len_tab[index];
417  } else { // ESC3
418  len += 7 + 2 + 1 + 6 + 1 + 12 + 1;
419  }
420  }
421 
422  return len;
423 }
424 
425 static inline void mpeg4_encode_blocks(MpegEncContext *s, int16_t block[6][64],
426  int intra_dc[6], uint8_t **scan_table,
427  PutBitContext *dc_pb,
428  PutBitContext *ac_pb)
429 {
430  int i;
431 
432  if (scan_table) {
434  for (i = 0; i < 6; i++)
435  skip_put_bits(&s->pb,
436  mpeg4_get_block_length(s, block[i], i,
437  intra_dc[i], scan_table[i]));
438  } else {
439  /* encode each block */
440  for (i = 0; i < 6; i++)
441  mpeg4_encode_block(s, block[i], i,
442  intra_dc[i], scan_table[i], dc_pb, ac_pb);
443  }
444  } else {
446  for (i = 0; i < 6; i++)
447  skip_put_bits(&s->pb,
448  mpeg4_get_block_length(s, block[i], i, 0,
450  } else {
451  /* encode each block */
452  for (i = 0; i < 6; i++)
453  mpeg4_encode_block(s, block[i], i, 0,
454  s->intra_scantable.permutated, dc_pb, ac_pb);
455  }
456  }
457 }
458 
459 static inline int get_b_cbp(MpegEncContext *s, int16_t block[6][64],
460  int motion_x, int motion_y, int mb_type)
461 {
462  int cbp = 0, i;
463 
464  if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
465  int score = 0;
466  const int lambda = s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
467 
468  for (i = 0; i < 6; i++) {
469  if (s->coded_score[i] < 0) {
470  score += s->coded_score[i];
471  cbp |= 1 << (5 - i);
472  }
473  }
474 
475  if (cbp) {
476  int zero_score = -6;
477  if ((motion_x | motion_y | s->dquant | mb_type) == 0)
478  zero_score -= 4; // 2 * MV + mb_type + cbp bit
479 
480  zero_score *= lambda;
481  if (zero_score <= score)
482  cbp = 0;
483  }
484 
485  for (i = 0; i < 6; i++) {
486  if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i)) & 1) == 0) {
487  s->block_last_index[i] = -1;
488  s->bdsp.clear_block(s->block[i]);
489  }
490  }
491  } else {
492  for (i = 0; i < 6; i++) {
493  if (s->block_last_index[i] >= 0)
494  cbp |= 1 << (5 - i);
495  }
496  }
497  return cbp;
498 }
499 
500 // FIXME this is duplicated to h263.c
501 static const int dquant_code[5] = { 1, 0, 9, 2, 3 };
502 
503 void ff_mpeg4_encode_mb(MpegEncContext *s, int16_t block[6][64],
504  int motion_x, int motion_y)
505 {
506  int cbpc, cbpy, pred_x, pred_y;
507  PutBitContext *const pb2 = s->data_partitioning ? &s->pb2 : &s->pb;
508  PutBitContext *const tex_pb = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B ? &s->tex_pb : &s->pb;
509  PutBitContext *const dc_pb = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_I ? &s->pb2 : &s->pb;
510  const int interleaved_stats = (s->avctx->flags & AV_CODEC_FLAG_PASS1) && !s->data_partitioning ? 1 : 0;
511 
512  if (!s->mb_intra) {
513  int i, cbp;
514 
515  if (s->pict_type == AV_PICTURE_TYPE_B) {
516  /* convert from mv_dir to type */
517  static const int mb_type_table[8] = { -1, 3, 2, 1, -1, -1, -1, 0 };
518  int mb_type = mb_type_table[s->mv_dir];
519 
520  if (s->mb_x == 0) {
521  for (i = 0; i < 2; i++)
522  s->last_mv[i][0][0] =
523  s->last_mv[i][0][1] =
524  s->last_mv[i][1][0] =
525  s->last_mv[i][1][1] = 0;
526  }
527 
528  av_assert2(s->dquant >= -2 && s->dquant <= 2);
529  av_assert2((s->dquant & 1) == 0);
530  av_assert2(mb_type >= 0);
531 
532  /* nothing to do if this MB was skipped in the next P-frame */
533  if (s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]) { // FIXME avoid DCT & ...
534  s->skip_count++;
535  s->mv[0][0][0] =
536  s->mv[0][0][1] =
537  s->mv[1][0][0] =
538  s->mv[1][0][1] = 0;
539  s->mv_dir = MV_DIR_FORWARD; // doesn't matter
540  s->qscale -= s->dquant;
541 // s->mb_skipped = 1;
542 
543  return;
544  }
545 
546  cbp = get_b_cbp(s, block, motion_x, motion_y, mb_type);
547 
548  if ((cbp | motion_x | motion_y | mb_type) == 0) {
549  /* direct MB with MV={0,0} */
550  av_assert2(s->dquant == 0);
551 
552  put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */
553 
554  if (interleaved_stats) {
555  s->misc_bits++;
556  s->last_bits++;
557  }
558  s->skip_count++;
559  return;
560  }
561 
562  put_bits(&s->pb, 1, 0); /* mb coded modb1=0 */
563  put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ // FIXME merge
564  put_bits(&s->pb, mb_type + 1, 1); // this table is so simple that we don't need it :)
565  if (cbp)
566  put_bits(&s->pb, 6, cbp);
567 
568  if (cbp && mb_type) {
569  if (s->dquant)
570  put_bits(&s->pb, 2, (s->dquant >> 2) + 3);
571  else
572  put_bits(&s->pb, 1, 0);
573  } else
574  s->qscale -= s->dquant;
575 
576  if (!s->progressive_sequence) {
577  if (cbp)
578  put_bits(&s->pb, 1, s->interlaced_dct);
579  if (mb_type) // not direct mode
580  put_bits(&s->pb, 1, s->mv_type == MV_TYPE_FIELD);
581  }
582 
583  if (interleaved_stats)
584  s->misc_bits += get_bits_diff(s);
585 
586  if (!mb_type) {
588  ff_h263_encode_motion_vector(s, motion_x, motion_y, 1);
589  s->b_count++;
590  s->f_count++;
591  } else {
592  av_assert2(mb_type > 0 && mb_type < 4);
593  if (s->mv_type != MV_TYPE_FIELD) {
594  if (s->mv_dir & MV_DIR_FORWARD) {
596  s->mv[0][0][0] - s->last_mv[0][0][0],
597  s->mv[0][0][1] - s->last_mv[0][0][1],
598  s->f_code);
599  s->last_mv[0][0][0] =
600  s->last_mv[0][1][0] = s->mv[0][0][0];
601  s->last_mv[0][0][1] =
602  s->last_mv[0][1][1] = s->mv[0][0][1];
603  s->f_count++;
604  }
605  if (s->mv_dir & MV_DIR_BACKWARD) {
607  s->mv[1][0][0] - s->last_mv[1][0][0],
608  s->mv[1][0][1] - s->last_mv[1][0][1],
609  s->b_code);
610  s->last_mv[1][0][0] =
611  s->last_mv[1][1][0] = s->mv[1][0][0];
612  s->last_mv[1][0][1] =
613  s->last_mv[1][1][1] = s->mv[1][0][1];
614  s->b_count++;
615  }
616  } else {
617  if (s->mv_dir & MV_DIR_FORWARD) {
618  put_bits(&s->pb, 1, s->field_select[0][0]);
619  put_bits(&s->pb, 1, s->field_select[0][1]);
620  }
621  if (s->mv_dir & MV_DIR_BACKWARD) {
622  put_bits(&s->pb, 1, s->field_select[1][0]);
623  put_bits(&s->pb, 1, s->field_select[1][1]);
624  }
625  if (s->mv_dir & MV_DIR_FORWARD) {
626  for (i = 0; i < 2; i++) {
628  s->mv[0][i][0] - s->last_mv[0][i][0],
629  s->mv[0][i][1] - s->last_mv[0][i][1] / 2,
630  s->f_code);
631  s->last_mv[0][i][0] = s->mv[0][i][0];
632  s->last_mv[0][i][1] = s->mv[0][i][1] * 2;
633  }
634  s->f_count++;
635  }
636  if (s->mv_dir & MV_DIR_BACKWARD) {
637  for (i = 0; i < 2; i++) {
639  s->mv[1][i][0] - s->last_mv[1][i][0],
640  s->mv[1][i][1] - s->last_mv[1][i][1] / 2,
641  s->b_code);
642  s->last_mv[1][i][0] = s->mv[1][i][0];
643  s->last_mv[1][i][1] = s->mv[1][i][1] * 2;
644  }
645  s->b_count++;
646  }
647  }
648  }
649 
650  if (interleaved_stats)
651  s->mv_bits += get_bits_diff(s);
652 
653  mpeg4_encode_blocks(s, block, NULL, NULL, NULL, &s->pb);
654 
655  if (interleaved_stats)
656  s->p_tex_bits += get_bits_diff(s);
657  } else { /* s->pict_type==AV_PICTURE_TYPE_B */
658  cbp = get_p_cbp(s, block, motion_x, motion_y);
659 
660  if ((cbp | motion_x | motion_y | s->dquant) == 0 &&
661  s->mv_type == MV_TYPE_16X16) {
662  /* Check if the B-frames can skip it too, as we must skip it
663  * if we skip here why didn't they just compress
664  * the skip-mb bits instead of reusing them ?! */
665  if (s->max_b_frames > 0) {
666  int i;
667  int x, y, offset;
668  uint8_t *p_pic;
669 
670  x = s->mb_x * 16;
671  y = s->mb_y * 16;
672 
673  offset = x + y * s->linesize;
674  p_pic = s->new_picture.f->data[0] + offset;
675 
676  s->mb_skipped = 1;
677  for (i = 0; i < s->max_b_frames; i++) {
678  uint8_t *b_pic;
679  int diff;
680  Picture *pic = s->reordered_input_picture[i + 1];
681 
682  if (!pic || pic->f->pict_type != AV_PICTURE_TYPE_B)
683  break;
684 
685  b_pic = pic->f->data[0] + offset;
686  if (!pic->shared)
687  b_pic += INPLACE_OFFSET;
688 
689  if (x + 16 > s->width || y + 16 > s->height) {
690  int x1, y1;
691  int xe = FFMIN(16, s->width - x);
692  int ye = FFMIN(16, s->height - y);
693  diff = 0;
694  for (y1 = 0; y1 < ye; y1++) {
695  for (x1 = 0; x1 < xe; x1++) {
696  diff += FFABS(p_pic[x1 + y1 * s->linesize] - b_pic[x1 + y1 * s->linesize]);
697  }
698  }
699  diff = diff * 256 / (xe * ye);
700  } else {
701  diff = s->mecc.sad[0](NULL, p_pic, b_pic, s->linesize, 16);
702  }
703  if (diff > s->qscale * 70) { // FIXME check that 70 is optimal
704  s->mb_skipped = 0;
705  break;
706  }
707  }
708  } else
709  s->mb_skipped = 1;
710 
711  if (s->mb_skipped == 1) {
712  /* skip macroblock */
713  put_bits(&s->pb, 1, 1);
714 
715  if (interleaved_stats) {
716  s->misc_bits++;
717  s->last_bits++;
718  }
719  s->skip_count++;
720 
721  return;
722  }
723  }
724 
725  put_bits(&s->pb, 1, 0); /* mb coded */
726  cbpc = cbp & 3;
727  cbpy = cbp >> 2;
728  cbpy ^= 0xf;
729  if (s->mv_type == MV_TYPE_16X16) {
730  if (s->dquant)
731  cbpc += 8;
732  put_bits(&s->pb,
735 
736  put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
737  if (s->dquant)
738  put_bits(pb2, 2, dquant_code[s->dquant + 2]);
739 
740  if (!s->progressive_sequence) {
741  if (cbp)
742  put_bits(pb2, 1, s->interlaced_dct);
743  put_bits(pb2, 1, 0);
744  }
745 
746  if (interleaved_stats)
747  s->misc_bits += get_bits_diff(s);
748 
749  /* motion vectors: 16x16 mode */
750  ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
751 
753  motion_x - pred_x,
754  motion_y - pred_y,
755  s->f_code);
756  } else if (s->mv_type == MV_TYPE_FIELD) {
757  if (s->dquant)
758  cbpc += 8;
759  put_bits(&s->pb,
762 
763  put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
764  if (s->dquant)
765  put_bits(pb2, 2, dquant_code[s->dquant + 2]);
766 
768  if (cbp)
769  put_bits(pb2, 1, s->interlaced_dct);
770  put_bits(pb2, 1, 1);
771 
772  if (interleaved_stats)
773  s->misc_bits += get_bits_diff(s);
774 
775  /* motion vectors: 16x8 interlaced mode */
776  ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
777  pred_y /= 2;
778 
779  put_bits(&s->pb, 1, s->field_select[0][0]);
780  put_bits(&s->pb, 1, s->field_select[0][1]);
781 
783  s->mv[0][0][0] - pred_x,
784  s->mv[0][0][1] - pred_y,
785  s->f_code);
787  s->mv[0][1][0] - pred_x,
788  s->mv[0][1][1] - pred_y,
789  s->f_code);
790  } else {
792  put_bits(&s->pb,
793  ff_h263_inter_MCBPC_bits[cbpc + 16],
794  ff_h263_inter_MCBPC_code[cbpc + 16]);
795  put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
796 
797  if (!s->progressive_sequence && cbp)
798  put_bits(pb2, 1, s->interlaced_dct);
799 
800  if (interleaved_stats)
801  s->misc_bits += get_bits_diff(s);
802 
803  for (i = 0; i < 4; i++) {
804  /* motion vectors: 8x8 mode*/
805  ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
806 
808  s->current_picture.motion_val[0][s->block_index[i]][0] - pred_x,
809  s->current_picture.motion_val[0][s->block_index[i]][1] - pred_y,
810  s->f_code);
811  }
812  }
813 
814  if (interleaved_stats)
815  s->mv_bits += get_bits_diff(s);
816 
817  mpeg4_encode_blocks(s, block, NULL, NULL, NULL, tex_pb);
818 
819  if (interleaved_stats)
820  s->p_tex_bits += get_bits_diff(s);
821 
822  s->f_count++;
823  }
824  } else {
825  int cbp;
826  int dc_diff[6]; // dc values with the dc prediction subtracted
827  int dir[6]; // prediction direction
828  int zigzag_last_index[6];
829  uint8_t *scan_table[6];
830  int i;
831 
832  for (i = 0; i < 6; i++)
833  dc_diff[i] = ff_mpeg4_pred_dc(s, i, block[i][0], &dir[i], 1);
834 
835  if (s->avctx->flags & AV_CODEC_FLAG_AC_PRED) {
836  s->ac_pred = decide_ac_pred(s, block, dir, scan_table, zigzag_last_index);
837  } else {
838  for (i = 0; i < 6; i++)
839  scan_table[i] = s->intra_scantable.permutated;
840  }
841 
842  /* compute cbp */
843  cbp = 0;
844  for (i = 0; i < 6; i++)
845  if (s->block_last_index[i] >= 1)
846  cbp |= 1 << (5 - i);
847 
848  cbpc = cbp & 3;
849  if (s->pict_type == AV_PICTURE_TYPE_I) {
850  if (s->dquant)
851  cbpc += 4;
852  put_bits(&s->pb,
855  } else {
856  if (s->dquant)
857  cbpc += 8;
858  put_bits(&s->pb, 1, 0); /* mb coded */
859  put_bits(&s->pb,
860  ff_h263_inter_MCBPC_bits[cbpc + 4],
861  ff_h263_inter_MCBPC_code[cbpc + 4]);
862  }
863  put_bits(pb2, 1, s->ac_pred);
864  cbpy = cbp >> 2;
865  put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
866  if (s->dquant)
867  put_bits(dc_pb, 2, dquant_code[s->dquant + 2]);
868 
869  if (!s->progressive_sequence)
870  put_bits(dc_pb, 1, s->interlaced_dct);
871 
872  if (interleaved_stats)
873  s->misc_bits += get_bits_diff(s);
874 
875  mpeg4_encode_blocks(s, block, dc_diff, scan_table, dc_pb, tex_pb);
876 
877  if (interleaved_stats)
878  s->i_tex_bits += get_bits_diff(s);
879  s->i_count++;
880 
881  /* restore ac coeffs & last_index stuff
882  * if we messed them up with the prediction */
883  if (s->ac_pred)
884  restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index);
885  }
886 }
887 
888 /**
889  * add MPEG-4 stuffing bits (01...1)
890  */
892 {
893  int length;
894  put_bits(pbc, 1, 0);
895  length = (-put_bits_count(pbc)) & 7;
896  if (length)
897  put_bits(pbc, length, (1 << length) - 1);
898 }
899 
900 /* must be called before writing the header */
902 {
903  if (s->pict_type == AV_PICTURE_TYPE_B) {
905  } else {
906  s->last_time_base = s->time_base;
907  s->time_base = FFUDIV(s->time, s->avctx->time_base.den);
908  }
909 }
910 
912 {
913  int hours, minutes, seconds;
914  int64_t time;
915 
916  put_bits(&s->pb, 16, 0);
917  put_bits(&s->pb, 16, GOP_STARTCODE);
918 
919  time = s->current_picture_ptr->f->pts;
920  if (s->reordered_input_picture[1])
921  time = FFMIN(time, s->reordered_input_picture[1]->f->pts);
922  time = time * s->avctx->time_base.num;
923  s->last_time_base = FFUDIV(time, s->avctx->time_base.den);
924 
925  seconds = FFUDIV(time, s->avctx->time_base.den);
926  minutes = FFUDIV(seconds, 60); seconds = FFUMOD(seconds, 60);
927  hours = FFUDIV(minutes, 60); minutes = FFUMOD(minutes, 60);
928  hours = FFUMOD(hours , 24);
929 
930  put_bits(&s->pb, 5, hours);
931  put_bits(&s->pb, 6, minutes);
932  put_bits(&s->pb, 1, 1);
933  put_bits(&s->pb, 6, seconds);
934 
935  put_bits(&s->pb, 1, !!(s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP));
936  put_bits(&s->pb, 1, 0); // broken link == NO
937 
938  ff_mpeg4_stuffing(&s->pb);
939 }
940 
942 {
943  int profile_and_level_indication;
944  int vo_ver_id;
945 
946  if (s->avctx->profile != FF_PROFILE_UNKNOWN) {
947  profile_and_level_indication = s->avctx->profile << 4;
948  } else if (s->max_b_frames || s->quarter_sample) {
949  profile_and_level_indication = 0xF0; // adv simple
950  } else {
951  profile_and_level_indication = 0x00; // simple
952  }
953 
954  if (s->avctx->level != FF_LEVEL_UNKNOWN)
955  profile_and_level_indication |= s->avctx->level;
956  else
957  profile_and_level_indication |= 1; // level 1
958 
959  if (profile_and_level_indication >> 4 == 0xF)
960  vo_ver_id = 5;
961  else
962  vo_ver_id = 1;
963 
964  // FIXME levels
965 
966  put_bits(&s->pb, 16, 0);
967  put_bits(&s->pb, 16, VOS_STARTCODE);
968 
969  put_bits(&s->pb, 8, profile_and_level_indication);
970 
971  put_bits(&s->pb, 16, 0);
972  put_bits(&s->pb, 16, VISUAL_OBJ_STARTCODE);
973 
974  put_bits(&s->pb, 1, 1);
975  put_bits(&s->pb, 4, vo_ver_id);
976  put_bits(&s->pb, 3, 1); // priority
977 
978  put_bits(&s->pb, 4, 1); // visual obj type== video obj
979 
980  put_bits(&s->pb, 1, 0); // video signal type == no clue // FIXME
981 
982  ff_mpeg4_stuffing(&s->pb);
983 }
984 
986  int vo_number,
987  int vol_number)
988 {
989  int vo_ver_id;
990 
991  if (!CONFIG_MPEG4_ENCODER)
992  return;
993 
994  if (s->max_b_frames || s->quarter_sample) {
995  vo_ver_id = 5;
997  } else {
998  vo_ver_id = 1;
999  s->vo_type = SIMPLE_VO_TYPE;
1000  }
1001 
1002  put_bits(&s->pb, 16, 0);
1003  put_bits(&s->pb, 16, 0x100 + vo_number); /* video obj */
1004  put_bits(&s->pb, 16, 0);
1005  put_bits(&s->pb, 16, 0x120 + vol_number); /* video obj layer */
1006 
1007  put_bits(&s->pb, 1, 0); /* random access vol */
1008  put_bits(&s->pb, 8, s->vo_type); /* video obj type indication */
1009  if (s->workaround_bugs & FF_BUG_MS) {
1010  put_bits(&s->pb, 1, 0); /* is obj layer id= no */
1011  } else {
1012  put_bits(&s->pb, 1, 1); /* is obj layer id= yes */
1013  put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
1014  put_bits(&s->pb, 3, 1); /* is obj layer priority */
1015  }
1016 
1018 
1019  put_bits(&s->pb, 4, s->aspect_ratio_info); /* aspect ratio info */
1023  put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
1024  put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
1025  }
1026 
1027  if (s->workaround_bugs & FF_BUG_MS) {
1028  put_bits(&s->pb, 1, 0); /* vol control parameters= no @@@ */
1029  } else {
1030  put_bits(&s->pb, 1, 1); /* vol control parameters= yes */
1031  put_bits(&s->pb, 2, 1); /* chroma format YUV 420/YV12 */
1032  put_bits(&s->pb, 1, s->low_delay);
1033  put_bits(&s->pb, 1, 0); /* vbv parameters= no */
1034  }
1035 
1036  put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */
1037  put_bits(&s->pb, 1, 1); /* marker bit */
1038 
1039  put_bits(&s->pb, 16, s->avctx->time_base.den);
1040  if (s->time_increment_bits < 1)
1041  s->time_increment_bits = 1;
1042  put_bits(&s->pb, 1, 1); /* marker bit */
1043  put_bits(&s->pb, 1, 0); /* fixed vop rate=no */
1044  put_bits(&s->pb, 1, 1); /* marker bit */
1045  put_bits(&s->pb, 13, s->width); /* vol width */
1046  put_bits(&s->pb, 1, 1); /* marker bit */
1047  put_bits(&s->pb, 13, s->height); /* vol height */
1048  put_bits(&s->pb, 1, 1); /* marker bit */
1049  put_bits(&s->pb, 1, s->progressive_sequence ? 0 : 1);
1050  put_bits(&s->pb, 1, 1); /* obmc disable */
1051  if (vo_ver_id == 1)
1052  put_bits(&s->pb, 1, 0); /* sprite enable */
1053  else
1054  put_bits(&s->pb, 2, 0); /* sprite enable */
1055 
1056  put_bits(&s->pb, 1, 0); /* not 8 bit == false */
1057  put_bits(&s->pb, 1, s->mpeg_quant); /* quant type = (0 = H.263 style) */
1058 
1059  if (s->mpeg_quant) {
1062  }
1063 
1064  if (vo_ver_id != 1)
1065  put_bits(&s->pb, 1, s->quarter_sample);
1066  put_bits(&s->pb, 1, 1); /* complexity estimation disable */
1067  put_bits(&s->pb, 1, s->rtp_mode ? 0 : 1); /* resync marker disable */
1068  put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
1069  if (s->data_partitioning)
1070  put_bits(&s->pb, 1, 0); /* no rvlc */
1071 
1072  if (vo_ver_id != 1) {
1073  put_bits(&s->pb, 1, 0); /* newpred */
1074  put_bits(&s->pb, 1, 0); /* reduced res vop */
1075  }
1076  put_bits(&s->pb, 1, 0); /* scalability */
1077 
1078  ff_mpeg4_stuffing(&s->pb);
1079 
1080  /* user data */
1081  if (!(s->avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
1082  put_bits(&s->pb, 16, 0);
1083  put_bits(&s->pb, 16, 0x1B2); /* user_data */
1085  }
1086 }
1087 
1088 /* write MPEG-4 VOP header */
1090 {
1091  uint64_t time_incr;
1092  int64_t time_div, time_mod;
1093 
1094  if (s->pict_type == AV_PICTURE_TYPE_I) {
1095  if (!(s->avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) {
1096  if (s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT) // HACK, the reference sw is buggy
1098  if (s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT || picture_number == 0) // HACK, the reference sw is buggy
1099  mpeg4_encode_vol_header(s, 0, 0);
1100  }
1101  if (!(s->workaround_bugs & FF_BUG_MS))
1103  }
1104 
1106 
1107  put_bits(&s->pb, 16, 0); /* vop header */
1108  put_bits(&s->pb, 16, VOP_STARTCODE); /* vop header */
1109  put_bits(&s->pb, 2, s->pict_type - 1); /* pict type: I = 0 , P = 1 */
1110 
1111  time_div = FFUDIV(s->time, s->avctx->time_base.den);
1112  time_mod = FFUMOD(s->time, s->avctx->time_base.den);
1113  time_incr = time_div - s->last_time_base;
1114 
1115  // This limits the frame duration to max 1 hour
1116  if (time_incr > 3600) {
1117  av_log(s->avctx, AV_LOG_ERROR, "time_incr %"PRIu64" too large\n", time_incr);
1118  return AVERROR(EINVAL);
1119  }
1120  while (time_incr--)
1121  put_bits(&s->pb, 1, 1);
1122 
1123  put_bits(&s->pb, 1, 0);
1124 
1125  put_bits(&s->pb, 1, 1); /* marker */
1126  put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
1127  put_bits(&s->pb, 1, 1); /* marker */
1128  put_bits(&s->pb, 1, 1); /* vop coded */
1129  if (s->pict_type == AV_PICTURE_TYPE_P) {
1130  put_bits(&s->pb, 1, s->no_rounding); /* rounding type */
1131  }
1132  put_bits(&s->pb, 3, 0); /* intra dc VLC threshold */
1133  if (!s->progressive_sequence) {
1135  put_bits(&s->pb, 1, s->alternate_scan);
1136  }
1137  // FIXME sprite stuff
1138 
1139  put_bits(&s->pb, 5, s->qscale);
1140 
1141  if (s->pict_type != AV_PICTURE_TYPE_I)
1142  put_bits(&s->pb, 3, s->f_code); /* fcode_for */
1143  if (s->pict_type == AV_PICTURE_TYPE_B)
1144  put_bits(&s->pb, 3, s->b_code); /* fcode_back */
1145 
1146  return 0;
1147 }
1148 
1149 static av_cold void init_uni_dc_tab(void)
1150 {
1151  int level, uni_code, uni_len;
1152 
1153  for (level = -256; level < 256; level++) {
1154  int size, v, l;
1155  /* find number of bits */
1156  size = 0;
1157  v = abs(level);
1158  while (v) {
1159  v >>= 1;
1160  size++;
1161  }
1162 
1163  if (level < 0)
1164  l = (-level) ^ ((1 << size) - 1);
1165  else
1166  l = level;
1167 
1168  /* luminance */
1169  uni_code = ff_mpeg4_DCtab_lum[size][0];
1170  uni_len = ff_mpeg4_DCtab_lum[size][1];
1171 
1172  if (size > 0) {
1173  uni_code <<= size;
1174  uni_code |= l;
1175  uni_len += size;
1176  if (size > 8) {
1177  uni_code <<= 1;
1178  uni_code |= 1;
1179  uni_len++;
1180  }
1181  }
1182  uni_DCtab_lum_bits[level + 256] = uni_code;
1183  uni_DCtab_lum_len[level + 256] = uni_len;
1184 
1185  /* chrominance */
1186  uni_code = ff_mpeg4_DCtab_chrom[size][0];
1187  uni_len = ff_mpeg4_DCtab_chrom[size][1];
1188 
1189  if (size > 0) {
1190  uni_code <<= size;
1191  uni_code |= l;
1192  uni_len += size;
1193  if (size > 8) {
1194  uni_code <<= 1;
1195  uni_code |= 1;
1196  uni_len++;
1197  }
1198  }
1199  uni_DCtab_chrom_bits[level + 256] = uni_code;
1200  uni_DCtab_chrom_len[level + 256] = uni_len;
1201  }
1202 }
1203 
1204 static av_cold void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab,
1205  uint8_t *len_tab)
1206 {
1207  int slevel, run, last;
1208 
1209  av_assert0(MAX_LEVEL >= 64);
1210  av_assert0(MAX_RUN >= 63);
1211 
1212  for (slevel = -64; slevel < 64; slevel++) {
1213  if (slevel == 0)
1214  continue;
1215  for (run = 0; run < 64; run++) {
1216  for (last = 0; last <= 1; last++) {
1217  const int index = UNI_MPEG4_ENC_INDEX(last, run, slevel + 64);
1218  int level = slevel < 0 ? -slevel : slevel;
1219  int sign = slevel < 0 ? 1 : 0;
1220  int bits, len, code;
1221  int level1, run1;
1222 
1223  len_tab[index] = 100;
1224 
1225  /* ESC0 */
1226  code = get_rl_index(rl, last, run, level);
1227  bits = rl->table_vlc[code][0];
1228  len = rl->table_vlc[code][1];
1229  bits = bits * 2 + sign;
1230  len++;
1231 
1232  if (code != rl->n && len < len_tab[index]) {
1233  bits_tab[index] = bits;
1234  len_tab[index] = len;
1235  }
1236  /* ESC1 */
1237  bits = rl->table_vlc[rl->n][0];
1238  len = rl->table_vlc[rl->n][1];
1239  bits = bits * 2;
1240  len++; // esc1
1241  level1 = level - rl->max_level[last][run];
1242  if (level1 > 0) {
1243  code = get_rl_index(rl, last, run, level1);
1244  bits <<= rl->table_vlc[code][1];
1245  len += rl->table_vlc[code][1];
1246  bits += rl->table_vlc[code][0];
1247  bits = bits * 2 + sign;
1248  len++;
1249 
1250  if (code != rl->n && len < len_tab[index]) {
1251  bits_tab[index] = bits;
1252  len_tab[index] = len;
1253  }
1254  }
1255  /* ESC2 */
1256  bits = rl->table_vlc[rl->n][0];
1257  len = rl->table_vlc[rl->n][1];
1258  bits = bits * 4 + 2;
1259  len += 2; // esc2
1260  run1 = run - rl->max_run[last][level] - 1;
1261  if (run1 >= 0) {
1262  code = get_rl_index(rl, last, run1, level);
1263  bits <<= rl->table_vlc[code][1];
1264  len += rl->table_vlc[code][1];
1265  bits += rl->table_vlc[code][0];
1266  bits = bits * 2 + sign;
1267  len++;
1268 
1269  if (code != rl->n && len < len_tab[index]) {
1270  bits_tab[index] = bits;
1271  len_tab[index] = len;
1272  }
1273  }
1274  /* ESC3 */
1275  bits = rl->table_vlc[rl->n][0];
1276  len = rl->table_vlc[rl->n][1];
1277  bits = bits * 4 + 3;
1278  len += 2; // esc3
1279  bits = bits * 2 + last;
1280  len++;
1281  bits = bits * 64 + run;
1282  len += 6;
1283  bits = bits * 2 + 1;
1284  len++; // marker
1285  bits = bits * 4096 + (slevel & 0xfff);
1286  len += 12;
1287  bits = bits * 2 + 1;
1288  len++; // marker
1289 
1290  if (len < len_tab[index]) {
1291  bits_tab[index] = bits;
1292  len_tab[index] = len;
1293  }
1294  }
1295  }
1296  }
1297 }
1298 
1300 {
1301  MpegEncContext *s = avctx->priv_data;
1302  int ret;
1303  static int done = 0;
1304 
1305  if (avctx->width >= (1<<13) || avctx->height >= (1<<13)) {
1306  av_log(avctx, AV_LOG_ERROR, "dimensions too large for MPEG-4\n");
1307  return AVERROR(EINVAL);
1308  }
1309 
1310  if ((ret = ff_mpv_encode_init(avctx)) < 0)
1311  return ret;
1312 
1313  if (!done) {
1314  done = 1;
1315 
1316  init_uni_dc_tab();
1317 
1319 
1322  }
1323 
1324  s->min_qcoeff = -2048;
1325  s->max_qcoeff = 2047;
1331  s->ac_esc_length = 7 + 2 + 1 + 6 + 1 + 12 + 1;
1334 
1336  s->avctx->extradata = av_malloc(1024);
1337  init_put_bits(&s->pb, s->avctx->extradata, 1024);
1338 
1339  if (!(s->workaround_bugs & FF_BUG_MS))
1341  mpeg4_encode_vol_header(s, 0, 0);
1342 
1343 // ff_mpeg4_stuffing(&s->pb); ?
1344  flush_put_bits(&s->pb);
1345  s->avctx->extradata_size = (put_bits_count(&s->pb) + 7) >> 3;
1346  }
1347  return 0;
1348 }
1349 
1351 {
1352  uint8_t *start = put_bits_ptr(&s->pb);
1353  uint8_t *end = s->pb.buf_end;
1354  int size = end - start;
1355  int pb_size = (((intptr_t)start + size / 3) & (~3)) - (intptr_t)start;
1356  int tex_size = (size - 2 * pb_size) & (~3);
1357 
1358  set_put_bits_buffer_size(&s->pb, pb_size);
1359  init_put_bits(&s->tex_pb, start + pb_size, tex_size);
1360  init_put_bits(&s->pb2, start + pb_size + tex_size, pb_size);
1361 }
1362 
1364 {
1365  const int pb2_len = put_bits_count(&s->pb2);
1366  const int tex_pb_len = put_bits_count(&s->tex_pb);
1367  const int bits = put_bits_count(&s->pb);
1368 
1369  if (s->pict_type == AV_PICTURE_TYPE_I) {
1370  put_bits(&s->pb, 19, DC_MARKER);
1371  s->misc_bits += 19 + pb2_len + bits - s->last_bits;
1372  s->i_tex_bits += tex_pb_len;
1373  } else {
1374  put_bits(&s->pb, 17, MOTION_MARKER);
1375  s->misc_bits += 17 + pb2_len;
1376  s->mv_bits += bits - s->last_bits;
1377  s->p_tex_bits += tex_pb_len;
1378  }
1379 
1380  flush_put_bits(&s->pb2);
1381  flush_put_bits(&s->tex_pb);
1382 
1383  set_put_bits_buffer_size(&s->pb, s->pb2.buf_end - s->pb.buf);
1384  avpriv_copy_bits(&s->pb, s->pb2.buf, pb2_len);
1385  avpriv_copy_bits(&s->pb, s->tex_pb.buf, tex_pb_len);
1386  s->last_bits = put_bits_count(&s->pb);
1387 }
1388 
1390 {
1391  int mb_num_bits = av_log2(s->mb_num - 1) + 1;
1392 
1394  put_bits(&s->pb, 1, 1);
1395 
1396  put_bits(&s->pb, mb_num_bits, s->mb_x + s->mb_y * s->mb_width);
1397  put_bits(&s->pb, s->quant_precision, s->qscale);
1398  put_bits(&s->pb, 1, 0); /* no HEC */
1399 }
1400 
1401 #define OFFSET(x) offsetof(MpegEncContext, x)
1402 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1403 static const AVOption options[] = {
1404  { "data_partitioning", "Use data partitioning.", OFFSET(data_partitioning), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1405  { "alternate_scan", "Enable alternate scantable.", OFFSET(alternate_scan), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1407  { NULL },
1408 };
1409 
1410 static const AVClass mpeg4enc_class = {
1411  .class_name = "MPEG4 encoder",
1412  .item_name = av_default_item_name,
1413  .option = options,
1414  .version = LIBAVUTIL_VERSION_INT,
1415 };
1416 
1418  .name = "mpeg4",
1419  .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
1420  .type = AVMEDIA_TYPE_VIDEO,
1421  .id = AV_CODEC_ID_MPEG4,
1422  .priv_data_size = sizeof(MpegEncContext),
1423  .init = encode_init,
1424  .encode2 = ff_mpv_encode_picture,
1425  .close = ff_mpv_encode_end,
1428  .priv_class = &mpeg4enc_class,
1429 };
int last_time_base
Definition: mpegvideo.h:386
static void mpeg4_encode_visual_object_header(MpegEncContext *s)
#define INPLACE_OFFSET
Definition: mpegutils.h:123
IDCTDSPContext idsp
Definition: mpegvideo.h:227
#define NULL
Definition: coverity.c:32
int aspect_ratio_info
Definition: mpegvideo.h:400
const char * s
Definition: avisynth_c.h:768
ScanTable intra_v_scantable
Definition: mpegvideo.h:90
RLTable ff_mpeg4_rl_intra
Definition: mpeg4data.h:109
const uint8_t ff_mpeg4_c_dc_scale_table[32]
Definition: mpeg4data.h:363
int time_increment_bits
< number of bits to represent the fractional part of time (encoder only)
Definition: mpegvideo.h:385
AVOption.
Definition: opt.h:245
#define MV_TYPE_FIELD
2 vectors, one per field
Definition: mpegvideo.h:269
static uint8_t uni_mpeg4_intra_rl_len[64 *64 *2 *2]
Definition: mpeg4videoenc.c:42
const uint8_t * y_dc_scale_table
qscale -> y_dc_scale table
Definition: mpegvideo.h:185
static void mpeg4_encode_block(MpegEncContext *s, int16_t *block, int n, int intra_dc, uint8_t *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
Encode an 8x8 block.
void(* clear_block)(int16_t *block)
Definition: blockdsp.h:35
int last_mv[2][2][2]
last MV, used for MV prediction in MPEG-1 & B-frame MPEG-4
Definition: mpegvideo.h:278
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:206
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
void ff_clean_mpeg4_qscales(MpegEncContext *s)
modify mb_type & qscale so that encoding is actually possible in MPEG-4
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int16_t(*[3] ac_val)[16]
used for MPEG-4 AC prediction, all 3 arrays must be continuous
Definition: mpegvideo.h:191
static int get_block_rate(MpegEncContext *s, int16_t block[64], int block_last_index, uint8_t scantable[64])
Return the number of bits that encoding the 8x8 block in block would need.
Definition: mpeg4videoenc.c:64
#define FF_COMPLIANCE_VERY_STRICT
Strictly conform to an older more strict version of the spec or reference software.
Definition: avcodec.h:2895
#define FF_MPV_COMMON_OPTS
Definition: mpegvideo.h:604
int num
Numerator.
Definition: rational.h:59
#define CANDIDATE_MB_TYPE_BIDIR
Definition: mpegutils.h:114
static void skip_put_bits(PutBitContext *s, int n)
Skip the given number of bits.
Definition: put_bits.h:249
void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
Definition: bitstream.c:65
int av_log2(unsigned v)
Definition: intmath.c:26
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:2087
int min_qcoeff
minimum encodable coefficient
Definition: mpegvideo.h:308
int coded_score[12]
Definition: mpegvideo.h:320
mpegvideo header.
#define FF_ASPECT_EXTENDED
Definition: avcodec.h:1881
static uint32_t uni_mpeg4_intra_rl_bits[64 *64 *2 *2]
Definition: mpeg4videoenc.c:41
#define DC_MARKER
Definition: mpeg4video.h:54
int mpv_flags
flags set by private options
Definition: mpegvideo.h:525
uint8_t permutated[64]
Definition: idctdsp.h:31
uint8_t run
Definition: svq3.c:206
uint8_t * intra_ac_vlc_length
Definition: mpegvideo.h:311
#define UNI_AC_ENC_INDEX(run, level)
Definition: mpegvideo.h:318
int mb_num
number of MBs of a picture
Definition: mpegvideo.h:130
int profile
profile
Definition: avcodec.h:3181
#define FF_LAMBDA_SHIFT
Definition: avutil.h:219
AVCodec.
Definition: avcodec.h:3600
int time_base
time in seconds of last I,P,S Frame
Definition: mpegvideo.h:387
RLTable.
Definition: rl.h:39
int qscale
QP.
Definition: mpegvideo.h:201
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
Definition: h263.c:309
static av_cold void init_uni_dc_tab(void)
int field_select[2][2]
Definition: mpegvideo.h:277
int block_wrap[6]
Definition: mpegvideo.h:294
Macro definitions for various function/variable attributes.
int quant_precision
Definition: mpegvideo.h:398
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1813
#define FF_BUG_MS
Work around various bugs in Microsoft's broken decoders.
Definition: avcodec.h:2879
#define FF_MPV_FLAG_CBP_RD
Definition: mpegvideo.h:573
static int16_t block[64]
Definition: dct.c:113
#define FF_LEVEL_UNKNOWN
Definition: avcodec.h:3280
void ff_mpeg4_stuffing(PutBitContext *pbc)
add MPEG-4 stuffing bits (01...1)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define AV_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:984
static uint8_t uni_DCtab_chrom_len[512]
Definition: mpeg4videoenc.c:35
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int8_t * max_run[2]
encoding & decoding
Definition: rl.h:47
int64_t time
time of current frame
Definition: mpegvideo.h:388
#define MV_DIRECT
bidirectional mode where the difference equals the MV of the last P/S/I-Frame (MPEG-4) ...
Definition: mpegvideo.h:264
uint8_t bits
Definition: crc.c:296
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
AVOptions.
PutBitContext pb2
used for data partitioned VOPs
Definition: mpegvideo.h:407
#define VOP_STARTCODE
Definition: mpeg4video.h:60
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
static int decide_ac_pred(MpegEncContext *s, int16_t block[6][64], const int dir[6], uint8_t *st[6], int zigzag_last_index[6])
Return the optimal value (0 or 1) for the ac_pred element for the given MB in MPEG-4.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:268
int misc_bits
cbp, mb_type
Definition: mpegvideo.h:352
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1791
int no_rounding
apply no rounding to motion compensation (MPEG-4, msmpeg4, ...) for B-frames rounding mode is always ...
Definition: mpegvideo.h:284
int interlaced_dct
Definition: mpegvideo.h:480
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:177
const uint8_t ff_mpeg4_DCtab_chrom[13][2]
Definition: mpeg4data.h:41
#define RECT_SHAPE
Definition: mpeg4video.h:33
int8_t * max_level[2]
encoding & decoding
Definition: rl.h:46
const uint8_t ff_h263_intra_MCBPC_bits[9]
Definition: h263data.c:35
ptrdiff_t size
Definition: opengl_enc.c:101
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
int max_qcoeff
maximum encodable coefficient
Definition: mpegvideo.h:309
static const int dquant_code[5]
#define MAX_LEVEL
Definition: rl.h:36
static uint32_t uni_mpeg4_inter_rl_bits[64 *64 *2 *2]
Definition: mpeg4videoenc.c:43
static int ff_mpeg4_pred_dc(MpegEncContext *s, int n, int level, int *dir_ptr, int encoding)
Predict the dc.
Definition: mpeg4video.h:180
int dquant
qscale difference to prev qscale
Definition: mpegvideo.h:207
#define av_log(a,...)
#define MOTION_MARKER
Definition: mpeg4video.h:53
#define UNI_MPEG4_ENC_INDEX(last, run, level)
Definition: mpeg4videoenc.c:48
#define ROUNDED_DIV(a, b)
Definition: common.h:56
static void mpeg4_encode_gop_header(MpegEncContext *s)
static int get_bits_diff(MpegEncContext *s)
Definition: mpegvideo.h:744
void ff_mpeg4_init_partitions(MpegEncContext *s)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
Definition: put_bits.h:227
uint8_t * mbskip_table
Definition: mpegpicture.h:59
const uint8_t ff_h263_inter_MCBPC_code[28]
Definition: h263data.c:40
static void restore_ac_coeffs(MpegEncContext *s, int16_t block[6][64], const int dir[6], uint8_t *st[6], const int zigzag_last_index[6])
Restore the ac coefficients in block that have been changed by decide_ac_pred().
Definition: mpeg4videoenc.c:99
uint8_t * inter_ac_vlc_last_length
Definition: mpegvideo.h:316
void ff_mpeg4_merge_partitions(MpegEncContext *s)
int mb_skipped
MUST BE SET only during DECODING.
Definition: mpegvideo.h:192
int strict_std_compliance
strictly follow the std (MPEG-4, ...)
Definition: mpegvideo.h:115
av_default_item_name
int partitioned_frame
is current frame partitioned
Definition: mpegvideo.h:403
#define AVERROR(e)
Definition: error.h:43
int ff_mpeg4_encode_picture_header(MpegEncContext *s, int picture_number)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
void ff_mpeg4_encode_mb(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1771
#define SIMPLE_VO_TYPE
Definition: mpeg4video.h:38
uint8_t * buf
Definition: put_bits.h:38
GLsizei GLsizei * length
Definition: opengl_enc.c:115
void ff_mpeg4_init_direct_mv(MpegEncContext *s)
Definition: mpeg4video.c:71
const char * name
Name of the codec implementation.
Definition: avcodec.h:3607
int quarter_sample
1->qpel, 0->half pel ME/MC
Definition: mpegvideo.h:399
uint16_t * mb_type
Table for candidate MB types for encoding (defines in mpegutils.h)
Definition: mpegvideo.h:291
av_const int ff_h263_aspect_to_info(AVRational aspect)
Return the 4 bit value that specifies the given aspect ratio.
Definition: ituh263enc.c:89
int low_delay
no reordering needed / has no B-frames
Definition: mpegvideo.h:404
static av_cold void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab)
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
void ff_clean_h263_qscales(MpegEncContext *s)
modify qscale so that encoding is actually possible in H.263 (limit difference to -2...
Definition: ituh263enc.c:266
uint8_t * intra_ac_vlc_last_length
Definition: mpegvideo.h:312
#define ADV_SIMPLE_VO_TYPE
Definition: mpeg4video.h:44
const uint8_t ff_mpeg4_DCtab_lum[13][2]
Definition: mpeg4data.h:35
uint8_t ff_mpeg4_static_rl_table_store[3][2][2 *MAX_RUN+MAX_LEVEL+3]
Definition: mpeg4video.c:28
int n
number of entries of table_vlc minus 1
Definition: rl.h:40
const uint8_t ff_h263_inter_MCBPC_bits[28]
Definition: h263data.c:49
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:258
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:886
#define FFMIN(a, b)
Definition: common.h:96
const uint8_t ff_mpeg4_y_dc_scale_table[32]
Definition: mpeg4data.h:359
const uint16_t(* table_vlc)[2]
Definition: rl.h:42
#define AV_CODEC_FLAG_AC_PRED
H.263 advanced intra coding / MPEG-4 AC prediction.
Definition: avcodec.h:891
Picture new_picture
copy of the source picture structure for encoding.
Definition: mpegvideo.h:171
static uint8_t uni_mpeg4_inter_rl_len[64 *64 *2 *2]
Definition: mpeg4videoenc.c:44
int width
picture width / height.
Definition: avcodec.h:1863
int16_t(*[2] motion_val)[2]
Definition: mpegpicture.h:53
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:181
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:3182
Picture.
Definition: mpegpicture.h:45
static const AVClass mpeg4enc_class
int alternate_scan
Definition: mpegvideo.h:467
static uint16_t uni_DCtab_lum_bits[512]
Definition: mpeg4videoenc.c:36
static uint16_t uni_DCtab_chrom_bits[512]
Definition: mpeg4videoenc.c:37
static int get_b_cbp(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y, int mb_type)
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:849
static uint8_t uni_DCtab_lum_len[512]
Definition: mpeg4videoenc.c:34
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
int level
level
Definition: avcodec.h:3279
int block_last_index[12]
last non zero coefficient in block
Definition: mpegvideo.h:83
int n
Definition: avisynth_c.h:684
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:94
int ac_esc_length
num of bits needed to encode the longest esc
Definition: mpegvideo.h:310
static void set_put_bits_buffer_size(PutBitContext *s, int size)
Change the end of the buffer.
Definition: put_bits.h:261
void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
int block_index[6]
index to current MB in block based arrays with edges
Definition: mpegvideo.h:293
int * mb_index2xy
mb_index -> mb_x + mb_y*mb_stride
Definition: mpegvideo.h:297
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:1026
#define VE
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:266
#define MV_DIR_BACKWARD
Definition: mpegvideo.h:263
#define FFUDIV(a, b)
Definition: common.h:63
uint8_t * luma_dc_vlc_length
Definition: mpegvideo.h:317
unsigned int lambda2
(lambda*lambda) >> FF_LAMBDA_SHIFT
Definition: mpegvideo.h:204
ptrdiff_t linesize
line size, in bytes, may be different from width
Definition: mpegvideo.h:131
BlockDSPContext bdsp
Definition: mpegvideo.h:223
const uint8_t ff_h263_intra_MCBPC_code[9]
Definition: h263data.c:34
main external API structure.
Definition: avcodec.h:1676
ScanTable intra_scantable
Definition: mpegvideo.h:88
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:97
uint8_t * buf_end
Definition: put_bits.h:38
av_cold int ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Definition: rl.c:39
int ff_mpv_encode_init(AVCodecContext *avctx)
int data_partitioning
data partitioning flag from header
Definition: mpegvideo.h:402
int extradata_size
Definition: avcodec.h:1792
uint8_t * inter_ac_vlc_length
Definition: mpegvideo.h:315
int progressive_sequence
Definition: mpegvideo.h:453
uint16_t * intra_matrix
custom intra quantization matrix
Definition: avcodec.h:2249
Describe the class of an AVClass context structure.
Definition: log.h:67
ScanTable intra_h_scantable
Definition: mpegvideo.h:89
int index
Definition: gxfenc.c:89
#define CANDIDATE_MB_TYPE_DIRECT
Definition: mpegutils.h:111
static av_cold int encode_init(AVCodecContext *avctx)
struct AVFrame * f
Definition: mpegpicture.h:46
static int mpeg4_get_block_length(MpegEncContext *s, int16_t *block, int n, int intra_dc, uint8_t *scan_table)
static void ff_h263_encode_motion_vector(MpegEncContext *s, int x, int y, int f_code)
Definition: h263.h:118
RLTable ff_h263_rl_inter
Definition: h263data.c:161
int f_code
forward MV resolution
Definition: mpegvideo.h:235
const uint8_t ff_h263_cbpy_tab[16][2]
Definition: h263data.c:84
uint16_t * inter_matrix
custom inter quantization matrix
Definition: avcodec.h:2256
#define MV_DIR_FORWARD
Definition: mpegvideo.h:262
int max_b_frames
max number of B-frames for encoding
Definition: mpegvideo.h:112
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:209
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s)
Definition: mpeg4video.c:30
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
const uint8_t * c_dc_scale_table
qscale -> c_dc_scale table
Definition: mpegvideo.h:186
uint8_t level
Definition: svq3.c:207
me_cmp_func sad[6]
Definition: me_cmp.h:56
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:882
int mv[2][4][2]
motion vectors for a macroblock first coordinate : 0 = forward 1 = backward second " : depend...
Definition: mpegvideo.h:276
MpegEncContext.
Definition: mpegvideo.h:78
int8_t * qscale_table
Definition: mpegpicture.h:50
#define MAX_RUN
Definition: rl.h:35
struct AVCodecContext * avctx
Definition: mpegvideo.h:95
PutBitContext pb
bit output
Definition: mpegvideo.h:148
#define VISUAL_OBJ_STARTCODE
Definition: mpeg4video.h:59
MECmpContext mecc
Definition: mpegvideo.h:228
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 ...
Definition: mpegvideo.h:127
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
if(ret< 0)
Definition: vf_mcdeint.c:282
int shared
Definition: mpegpicture.h:88
#define AV_CODEC_FLAG2_NO_OUTPUT
Skip bitstream encoding.
Definition: avcodec.h:905
Bi-dir predicted.
Definition: avutil.h:270
static const AVOption options[]
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
int den
Denominator.
Definition: rational.h:60
int ff_mpv_encode_end(AVCodecContext *avctx)
static int get_rl_index(const RLTable *rl, int last, int run, int level)
Definition: rl.h:76
void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
void * priv_data
Definition: avcodec.h:1718
int last_bits
temp var used for calculating the above vars
Definition: mpegvideo.h:353
static av_always_inline int diff(const uint32_t a, const uint32_t b)
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:327
int len
#define FFUMOD(a, b)
Definition: common.h:64
int16_t(* block)[64]
points to one of the following blocks
Definition: mpegvideo.h:497
PutBitContext tex_pb
used for data partitioned VOPs
Definition: mpegvideo.h:406
Picture next_picture
copy of the next picture structure.
Definition: mpegvideo.h:165
static int get_p_cbp(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
Definition: h263.h:129
Picture ** reordered_input_picture
pointer to the next pictures in coded order for encoding
Definition: mpegvideo.h:135
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1778
#define LIBAVCODEC_IDENT
Definition: version.h:42
void avpriv_put_string(PutBitContext *pb, const char *string, int terminate_string)
Put the string string in the bitstream.
Definition: bitstream.c:54
void INT64 start
Definition: avisynth_c.h:690
int workaround_bugs
workaround bugs in encoders which cannot be detected automatically
Definition: mpegvideo.h:116
static int mpeg4_get_dc_length(int level, int n)
void ff_set_mpeg4_time(MpegEncContext *s)
#define AV_CODEC_FLAG_CLOSED_GOP
Definition: avcodec.h:896
static void mpeg4_encode_blocks(MpegEncContext *s, int16_t block[6][64], int intra_dc[6], uint8_t **scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
#define MV_TYPE_8X8
4 vectors (H.263, MPEG-4 4MV)
Definition: mpegvideo.h:267
int b_code
backward MV resolution for B-frames (MPEG-4)
Definition: mpegvideo.h:236
int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
static void mpeg4_encode_dc(PutBitContext *s, int level, int n)
Encode the dc value.
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
static void mpeg4_encode_vol_header(MpegEncContext *s, int vo_number, int vol_number)
#define OFFSET(x)
AVCodec ff_mpeg4_encoder
Predicted.
Definition: avutil.h:269
#define GOP_STARTCODE
Definition: mpeg4video.h:58
#define VOS_STARTCODE
Definition: mpeg4video.h:56