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