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