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