aarch64: Add assembly support for -fsanitize=hwaddress tagged globals.
[libav.git] / libavcodec / mpeg4videoenc.c
blob8815ba85c4503cf22456f84e090c257b79d610f8
1 /*
2 * MPEG-4 encoder
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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"
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. */
34 static uint8_t uni_DCtab_lum_len[512];
35 static uint8_t uni_DCtab_chrom_len[512];
36 static uint16_t uni_DCtab_lum_bits[512];
37 static uint16_t uni_DCtab_chrom_bits[512];
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];
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))
50 /* MPEG-4
51 * inter
52 * max level: 24/6
53 * max run: 53/63
55 * intra
56 * max level: 53/16
57 * max run: 29/41
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.
64 static inline int get_block_rate(MpegEncContext *s, int16_t block[64],
65 int block_last_index, uint8_t scantable[64])
67 int last = 0;
68 int j;
69 int rate = 0;
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;
84 last = j;
88 return rate;
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
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])
103 int i, n;
104 memcpy(s->block_last_index, zigzag_last_index, sizeof(int) * 6);
106 for (n = 0; n < 6; n++) {
107 int16_t *ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
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];
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
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])
134 int score = 0;
135 int i, n;
136 int8_t *const qscale_table = s->current_picture.qscale_table;
138 memcpy(zigzag_last_index, s->block_last_index, sizeof(int) * 6);
140 for (n = 0; n < 6; n++) {
141 int16_t *ac_val, *ac_val1;
143 score -= get_block_rate(s, block[n], s->block_last_index[n],
144 s->intra_scantable.permutated);
146 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
147 ac_val1 = ac_val;
148 if (dir[n]) {
149 const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
150 /* top prediction */
151 ac_val -= s->block_wrap[n] * 16;
152 if (s->mb_y == 0 || s->qscale == qscale_table[xy] || n == 2 || n == 3) {
153 /* same qscale */
154 for (i = 1; i < 8; i++) {
155 const int level = block[n][s->idsp.idct_permutation[i]];
156 block[n][s->idsp.idct_permutation[i]] = level - ac_val[i + 8];
157 ac_val1[i] = block[n][s->idsp.idct_permutation[i << 3]];
158 ac_val1[i + 8] = level;
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;
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]];
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]];
191 st[n] = s->intra_v_scantable.permutated;
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;
199 score += get_block_rate(s, block[n], s->block_last_index[n], st[n]);
202 if (score < 0) {
203 return 1;
204 } else {
205 restore_ac_coeffs(s, block, dir, st, zigzag_last_index);
206 return 0;
211 * modify mb_type & qscale so that encoding is actually possible in MPEG-4
213 void ff_clean_mpeg4_qscales(MpegEncContext *s)
215 int i;
216 int8_t *const qscale_table = s->current_picture.qscale_table;
218 ff_clean_h263_qscales(s);
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 */
225 for (i = 0; i < s->mb_num; i++) {
226 int mb_xy = s->mb_index2xy[i];
227 odd += qscale_table[mb_xy] & 1;
230 if (2 * odd > s->mb_num)
231 odd = 1;
232 else
233 odd = 0;
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;
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;
254 * Encode the dc value.
255 * @param n block index (0-3 are luma, 4-5 are chroma)
257 static inline void mpeg4_encode_dc(PutBitContext *s, int level, int n)
259 /* DC will overflow if level is outside the [-255,255] range. */
260 level += 256;
261 if (n < 4) {
262 /* luminance */
263 put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]);
264 } else {
265 /* chrominance */
266 put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]);
270 static inline int mpeg4_get_dc_length(int level, int n)
272 if (n < 4)
273 return uni_DCtab_lum_len[level + 256];
274 else
275 return uni_DCtab_chrom_len[level + 256];
279 * Encode an 8x8 block.
280 * @param n block index (0-3 are luma, 4-5 are chroma)
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)
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];
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;
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);
324 last_non_zero = i;
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);
343 static int mpeg4_get_block_length(MpegEncContext *s,
344 int16_t *block, int n,
345 int intra_dc, uint8_t *scan_table)
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;
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;
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;
379 last_non_zero = i;
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;
394 return len;
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)
402 int i;
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,
408 mpeg4_get_block_length(s, block[i], i,
409 intra_dc[i], scan_table[i]));
410 } else {
411 /* encode each block */
412 for (i = 0; i < 6; i++)
413 mpeg4_encode_block(s, block[i], i,
414 intra_dc[i], scan_table[i], dc_pb, ac_pb);
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,
420 mpeg4_get_block_length(s, block[i], i, 0,
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);
431 static inline int get_b_cbp(MpegEncContext *s, int16_t block[6][64],
432 int motion_x, int motion_y, int mb_type)
434 int cbp = 0, i;
436 if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
437 int score = 0;
438 const int lambda = s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
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);
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
452 zero_score *= lambda;
453 if (zero_score <= score)
454 cbp = 0;
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]);
463 } else {
464 for (i = 0; i < 6; i++) {
465 if (s->block_last_index[i] >= 0)
466 cbp |= 1 << (5 - i);
469 return cbp;
472 // FIXME this is duplicated to h263.c
473 static const int dquant_code[5] = { 1, 0, 9, 2, 3 };
475 void ff_mpeg4_encode_mb(MpegEncContext *s, int16_t block[6][64],
476 int motion_x, int motion_y)
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;
484 if (!s->mb_intra) {
485 int i, cbp;
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];
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;
500 assert(s->dquant >= -2 && s->dquant <= 2);
501 assert((s->dquant & 1) == 0);
502 assert(mb_type >= 0);
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;
515 return;
518 cbp = get_b_cbp(s, block, motion_x, motion_y, mb_type);
520 if ((cbp | motion_x | motion_y | mb_type) == 0) {
521 /* direct MB with MV={0,0} */
522 assert(s->dquant == 0);
524 put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */
526 if (interleaved_stats) {
527 s->misc_bits++;
528 s->last_bits++;
530 s->skip_count++;
531 return;
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);
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;
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);
555 if (interleaved_stats)
556 s->misc_bits += get_bits_diff(s);
558 if (!mb_type) {
559 assert(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 assert(mb_type > 0 && mb_type < 4);
565 if (s->mv_type != MV_TYPE_FIELD) {
566 if (s->mv_dir & MV_DIR_FORWARD) {
567 ff_h263_encode_motion_vector(s,
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++;
577 if (s->mv_dir & MV_DIR_BACKWARD) {
578 ff_h263_encode_motion_vector(s,
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++;
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]);
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]);
597 if (s->mv_dir & MV_DIR_FORWARD) {
598 for (i = 0; i < 2; i++) {
599 ff_h263_encode_motion_vector(s,
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;
606 s->f_count++;
608 if (s->mv_dir & MV_DIR_BACKWARD) {
609 for (i = 0; i < 2; i++) {
610 ff_h263_encode_motion_vector(s,
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;
617 s->b_count++;
622 if (interleaved_stats)
623 s->mv_bits += get_bits_diff(s);
625 mpeg4_encode_blocks(s, block, NULL, NULL, NULL, &s->pb);
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);
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;
642 x = s->mb_x * 16;
643 y = s->mb_y * 16;
644 if (x + 16 > s->width)
645 x = s->width - 16;
646 if (y + 16 > s->height)
647 y = s->height - 16;
649 offset = x + y * s->linesize;
650 p_pic = s->new_picture.f->data[0] + offset;
652 s->mb_skipped = 1;
653 for (i = 0; i < s->max_b_frames; i++) {
654 uint8_t *b_pic;
655 int diff;
656 Picture *pic = s->reordered_input_picture[i + 1];
658 if (!pic || pic->f->pict_type != AV_PICTURE_TYPE_B)
659 break;
661 b_pic = pic->f->data[0] + offset;
662 if (!pic->shared)
663 b_pic += INPLACE_OFFSET;
664 diff = s->mecc.sad[0](NULL, p_pic, b_pic, s->linesize, 16);
665 if (diff > s->qscale * 70) { // FIXME check that 70 is optimal
666 s->mb_skipped = 0;
667 break;
670 } else
671 s->mb_skipped = 1;
673 if (s->mb_skipped == 1) {
674 /* skip macroblock */
675 put_bits(&s->pb, 1, 1);
677 if (interleaved_stats) {
678 s->misc_bits++;
679 s->last_bits++;
681 s->skip_count++;
683 return;
687 put_bits(&s->pb, 1, 0); /* mb coded */
688 cbpc = cbp & 3;
689 cbpy = cbp >> 2;
690 cbpy ^= 0xf;
691 if (s->mv_type == MV_TYPE_16X16) {
692 if (s->dquant)
693 cbpc += 8;
694 put_bits(&s->pb,
695 ff_h263_inter_MCBPC_bits[cbpc],
696 ff_h263_inter_MCBPC_code[cbpc]);
698 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
699 if (s->dquant)
700 put_bits(pb2, 2, dquant_code[s->dquant + 2]);
702 if (!s->progressive_sequence) {
703 if (cbp)
704 put_bits(pb2, 1, s->interlaced_dct);
705 put_bits(pb2, 1, 0);
708 if (interleaved_stats)
709 s->misc_bits += get_bits_diff(s);
711 /* motion vectors: 16x16 mode */
712 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
714 ff_h263_encode_motion_vector(s,
715 motion_x - pred_x,
716 motion_y - pred_y,
717 s->f_code);
718 } else if (s->mv_type == MV_TYPE_FIELD) {
719 if (s->dquant)
720 cbpc += 8;
721 put_bits(&s->pb,
722 ff_h263_inter_MCBPC_bits[cbpc],
723 ff_h263_inter_MCBPC_code[cbpc]);
725 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
726 if (s->dquant)
727 put_bits(pb2, 2, dquant_code[s->dquant + 2]);
729 assert(!s->progressive_sequence);
730 if (cbp)
731 put_bits(pb2, 1, s->interlaced_dct);
732 put_bits(pb2, 1, 1);
734 if (interleaved_stats)
735 s->misc_bits += get_bits_diff(s);
737 /* motion vectors: 16x8 interlaced mode */
738 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
739 pred_y /= 2;
741 put_bits(&s->pb, 1, s->field_select[0][0]);
742 put_bits(&s->pb, 1, s->field_select[0][1]);
744 ff_h263_encode_motion_vector(s,
745 s->mv[0][0][0] - pred_x,
746 s->mv[0][0][1] - pred_y,
747 s->f_code);
748 ff_h263_encode_motion_vector(s,
749 s->mv[0][1][0] - pred_x,
750 s->mv[0][1][1] - pred_y,
751 s->f_code);
752 } else {
753 assert(s->mv_type == MV_TYPE_8X8);
754 put_bits(&s->pb,
755 ff_h263_inter_MCBPC_bits[cbpc + 16],
756 ff_h263_inter_MCBPC_code[cbpc + 16]);
757 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
759 if (!s->progressive_sequence && cbp)
760 put_bits(pb2, 1, s->interlaced_dct);
762 if (interleaved_stats)
763 s->misc_bits += get_bits_diff(s);
765 for (i = 0; i < 4; i++) {
766 /* motion vectors: 8x8 mode*/
767 ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
769 ff_h263_encode_motion_vector(s,
770 s->current_picture.motion_val[0][s->block_index[i]][0] - pred_x,
771 s->current_picture.motion_val[0][s->block_index[i]][1] - pred_y,
772 s->f_code);
776 if (interleaved_stats)
777 s->mv_bits += get_bits_diff(s);
779 mpeg4_encode_blocks(s, block, NULL, NULL, NULL, tex_pb);
781 if (interleaved_stats)
782 s->p_tex_bits += get_bits_diff(s);
784 s->f_count++;
786 } else {
787 int cbp;
788 int dc_diff[6]; // dc values with the dc prediction subtracted
789 int dir[6]; // prediction direction
790 int zigzag_last_index[6];
791 uint8_t *scan_table[6];
792 int i;
794 for (i = 0; i < 6; i++)
795 dc_diff[i] = ff_mpeg4_pred_dc(s, i, block[i][0], &dir[i], 1);
797 if (s->avctx->flags & AV_CODEC_FLAG_AC_PRED) {
798 s->ac_pred = decide_ac_pred(s, block, dir, scan_table, zigzag_last_index);
799 } else {
800 for (i = 0; i < 6; i++)
801 scan_table[i] = s->intra_scantable.permutated;
804 /* compute cbp */
805 cbp = 0;
806 for (i = 0; i < 6; i++)
807 if (s->block_last_index[i] >= 1)
808 cbp |= 1 << (5 - i);
810 cbpc = cbp & 3;
811 if (s->pict_type == AV_PICTURE_TYPE_I) {
812 if (s->dquant)
813 cbpc += 4;
814 put_bits(&s->pb,
815 ff_h263_intra_MCBPC_bits[cbpc],
816 ff_h263_intra_MCBPC_code[cbpc]);
817 } else {
818 if (s->dquant)
819 cbpc += 8;
820 put_bits(&s->pb, 1, 0); /* mb coded */
821 put_bits(&s->pb,
822 ff_h263_inter_MCBPC_bits[cbpc + 4],
823 ff_h263_inter_MCBPC_code[cbpc + 4]);
825 put_bits(pb2, 1, s->ac_pred);
826 cbpy = cbp >> 2;
827 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
828 if (s->dquant)
829 put_bits(dc_pb, 2, dquant_code[s->dquant + 2]);
831 if (!s->progressive_sequence)
832 put_bits(dc_pb, 1, s->interlaced_dct);
834 if (interleaved_stats)
835 s->misc_bits += get_bits_diff(s);
837 mpeg4_encode_blocks(s, block, dc_diff, scan_table, dc_pb, tex_pb);
839 if (interleaved_stats)
840 s->i_tex_bits += get_bits_diff(s);
841 s->i_count++;
843 /* restore ac coeffs & last_index stuff
844 * if we messed them up with the prediction */
845 if (s->ac_pred)
846 restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index);
851 * add MPEG-4 stuffing bits (01...1)
853 void ff_mpeg4_stuffing(PutBitContext *pbc)
855 int length;
856 put_bits(pbc, 1, 0);
857 length = (-put_bits_count(pbc)) & 7;
858 if (length)
859 put_bits(pbc, length, (1 << length) - 1);
862 /* must be called before writing the header */
863 void ff_set_mpeg4_time(MpegEncContext *s)
865 if (s->pict_type == AV_PICTURE_TYPE_B) {
866 ff_mpeg4_init_direct_mv(s);
867 } else {
868 s->last_time_base = s->time_base;
869 s->time_base = s->time / s->avctx->time_base.den;
873 static void mpeg4_encode_gop_header(MpegEncContext *s)
875 int hours, minutes, seconds;
876 int64_t time;
878 put_bits(&s->pb, 16, 0);
879 put_bits(&s->pb, 16, GOP_STARTCODE);
881 time = s->current_picture_ptr->f->pts;
882 if (s->reordered_input_picture[1])
883 time = FFMIN(time, s->reordered_input_picture[1]->f->pts);
884 time = time * s->avctx->time_base.num;
886 seconds = time / s->avctx->time_base.den;
887 minutes = seconds / 60;
888 seconds %= 60;
889 hours = minutes / 60;
890 minutes %= 60;
891 hours %= 24;
893 put_bits(&s->pb, 5, hours);
894 put_bits(&s->pb, 6, minutes);
895 put_bits(&s->pb, 1, 1);
896 put_bits(&s->pb, 6, seconds);
898 put_bits(&s->pb, 1, !!(s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP));
899 put_bits(&s->pb, 1, 0); // broken link == NO
901 s->last_time_base = time / s->avctx->time_base.den;
903 ff_mpeg4_stuffing(&s->pb);
906 static void mpeg4_encode_visual_object_header(MpegEncContext *s)
908 int profile_and_level_indication;
909 int vo_ver_id;
911 if (s->avctx->profile != FF_PROFILE_UNKNOWN) {
912 profile_and_level_indication = s->avctx->profile << 4;
913 } else if (s->max_b_frames || s->quarter_sample) {
914 profile_and_level_indication = 0xF0; // adv simple
915 } else {
916 profile_and_level_indication = 0x00; // simple
919 if (s->avctx->level != FF_LEVEL_UNKNOWN)
920 profile_and_level_indication |= s->avctx->level;
921 else
922 profile_and_level_indication |= 1; // level 1
924 if (profile_and_level_indication >> 4 == 0xF)
925 vo_ver_id = 5;
926 else
927 vo_ver_id = 1;
929 // FIXME levels
931 put_bits(&s->pb, 16, 0);
932 put_bits(&s->pb, 16, VOS_STARTCODE);
934 put_bits(&s->pb, 8, profile_and_level_indication);
936 put_bits(&s->pb, 16, 0);
937 put_bits(&s->pb, 16, VISUAL_OBJ_STARTCODE);
939 put_bits(&s->pb, 1, 1);
940 put_bits(&s->pb, 4, vo_ver_id);
941 put_bits(&s->pb, 3, 1); // priority
943 put_bits(&s->pb, 4, 1); // visual obj type== video obj
945 put_bits(&s->pb, 1, 0); // video signal type == no clue // FIXME
947 ff_mpeg4_stuffing(&s->pb);
950 static void mpeg4_encode_vol_header(MpegEncContext *s,
951 int vo_number,
952 int vol_number)
954 int vo_ver_id;
956 if (!CONFIG_MPEG4_ENCODER)
957 return;
959 if (s->max_b_frames || s->quarter_sample) {
960 vo_ver_id = 5;
961 s->vo_type = ADV_SIMPLE_VO_TYPE;
962 } else {
963 vo_ver_id = 1;
964 s->vo_type = SIMPLE_VO_TYPE;
967 put_bits(&s->pb, 16, 0);
968 put_bits(&s->pb, 16, 0x100 + vo_number); /* video obj */
969 put_bits(&s->pb, 16, 0);
970 put_bits(&s->pb, 16, 0x120 + vol_number); /* video obj layer */
972 put_bits(&s->pb, 1, 0); /* random access vol */
973 put_bits(&s->pb, 8, s->vo_type); /* video obj type indication */
974 if (s->workaround_bugs & FF_BUG_MS) {
975 put_bits(&s->pb, 1, 0); /* is obj layer id= no */
976 } else {
977 put_bits(&s->pb, 1, 1); /* is obj layer id= yes */
978 put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
979 put_bits(&s->pb, 3, 1); /* is obj layer priority */
982 s->aspect_ratio_info = ff_h263_aspect_to_info(s->avctx->sample_aspect_ratio);
984 put_bits(&s->pb, 4, s->aspect_ratio_info); /* aspect ratio info */
985 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
986 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
987 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
990 if (s->workaround_bugs & FF_BUG_MS) {
991 put_bits(&s->pb, 1, 0); /* vol control parameters= no @@@ */
992 } else {
993 put_bits(&s->pb, 1, 1); /* vol control parameters= yes */
994 put_bits(&s->pb, 2, 1); /* chroma format YUV 420/YV12 */
995 put_bits(&s->pb, 1, s->low_delay);
996 put_bits(&s->pb, 1, 0); /* vbv parameters= no */
999 put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */
1000 put_bits(&s->pb, 1, 1); /* marker bit */
1002 put_bits(&s->pb, 16, s->avctx->time_base.den);
1003 if (s->time_increment_bits < 1)
1004 s->time_increment_bits = 1;
1005 put_bits(&s->pb, 1, 1); /* marker bit */
1006 put_bits(&s->pb, 1, 0); /* fixed vop rate=no */
1007 put_bits(&s->pb, 1, 1); /* marker bit */
1008 put_bits(&s->pb, 13, s->width); /* vol width */
1009 put_bits(&s->pb, 1, 1); /* marker bit */
1010 put_bits(&s->pb, 13, s->height); /* vol height */
1011 put_bits(&s->pb, 1, 1); /* marker bit */
1012 put_bits(&s->pb, 1, s->progressive_sequence ? 0 : 1);
1013 put_bits(&s->pb, 1, 1); /* obmc disable */
1014 if (vo_ver_id == 1)
1015 put_bits(&s->pb, 1, 0); /* sprite enable */
1016 else
1017 put_bits(&s->pb, 2, 0); /* sprite enable */
1019 put_bits(&s->pb, 1, 0); /* not 8 bit == false */
1020 put_bits(&s->pb, 1, s->mpeg_quant); /* quant type = (0 = H.263 style) */
1022 if (s->mpeg_quant) {
1023 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
1024 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
1027 if (vo_ver_id != 1)
1028 put_bits(&s->pb, 1, s->quarter_sample);
1029 put_bits(&s->pb, 1, 1); /* complexity estimation disable */
1030 put_bits(&s->pb, 1, s->rtp_mode ? 0 : 1); /* resync marker disable */
1031 put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
1032 if (s->data_partitioning)
1033 put_bits(&s->pb, 1, 0); /* no rvlc */
1035 if (vo_ver_id != 1) {
1036 put_bits(&s->pb, 1, 0); /* newpred */
1037 put_bits(&s->pb, 1, 0); /* reduced res vop */
1039 put_bits(&s->pb, 1, 0); /* scalability */
1041 ff_mpeg4_stuffing(&s->pb);
1043 /* user data */
1044 if (!(s->avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
1045 put_bits(&s->pb, 16, 0);
1046 put_bits(&s->pb, 16, 0x1B2); /* user_data */
1047 avpriv_put_string(&s->pb, LIBAVCODEC_IDENT, 0);
1051 /* write MPEG-4 VOP header */
1052 void ff_mpeg4_encode_picture_header(MpegEncContext *s, int picture_number)
1054 int time_incr;
1055 int time_div, time_mod;
1057 if (s->pict_type == AV_PICTURE_TYPE_I) {
1058 if (!(s->avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) {
1059 if (s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT) // HACK, the reference sw is buggy
1060 mpeg4_encode_visual_object_header(s);
1061 if (s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT || picture_number == 0) // HACK, the reference sw is buggy
1062 mpeg4_encode_vol_header(s, 0, 0);
1064 if (!(s->workaround_bugs & FF_BUG_MS))
1065 mpeg4_encode_gop_header(s);
1068 s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
1070 put_bits(&s->pb, 16, 0); /* vop header */
1071 put_bits(&s->pb, 16, VOP_STARTCODE); /* vop header */
1072 put_bits(&s->pb, 2, s->pict_type - 1); /* pict type: I = 0 , P = 1 */
1074 assert(s->time >= 0);
1075 time_div = s->time / s->avctx->time_base.den;
1076 time_mod = s->time % s->avctx->time_base.den;
1077 time_incr = time_div - s->last_time_base;
1078 assert(time_incr >= 0);
1079 while (time_incr--)
1080 put_bits(&s->pb, 1, 1);
1082 put_bits(&s->pb, 1, 0);
1084 put_bits(&s->pb, 1, 1); /* marker */
1085 put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
1086 put_bits(&s->pb, 1, 1); /* marker */
1087 put_bits(&s->pb, 1, 1); /* vop coded */
1088 if (s->pict_type == AV_PICTURE_TYPE_P) {
1089 put_bits(&s->pb, 1, s->no_rounding); /* rounding type */
1091 put_bits(&s->pb, 3, 0); /* intra dc VLC threshold */
1092 if (!s->progressive_sequence) {
1093 put_bits(&s->pb, 1, s->current_picture_ptr->f->top_field_first);
1094 put_bits(&s->pb, 1, s->alternate_scan);
1096 // FIXME sprite stuff
1098 put_bits(&s->pb, 5, s->qscale);
1100 if (s->pict_type != AV_PICTURE_TYPE_I)
1101 put_bits(&s->pb, 3, s->f_code); /* fcode_for */
1102 if (s->pict_type == AV_PICTURE_TYPE_B)
1103 put_bits(&s->pb, 3, s->b_code); /* fcode_back */
1106 static av_cold void init_uni_dc_tab(void)
1108 int level, uni_code, uni_len;
1110 for (level = -256; level < 256; level++) {
1111 int size, v, l;
1112 /* find number of bits */
1113 size = 0;
1114 v = abs(level);
1115 while (v) {
1116 v >>= 1;
1117 size++;
1120 if (level < 0)
1121 l = (-level) ^ ((1 << size) - 1);
1122 else
1123 l = level;
1125 /* luminance */
1126 uni_code = ff_mpeg4_DCtab_lum[size][0];
1127 uni_len = ff_mpeg4_DCtab_lum[size][1];
1129 if (size > 0) {
1130 uni_code <<= size;
1131 uni_code |= l;
1132 uni_len += size;
1133 if (size > 8) {
1134 uni_code <<= 1;
1135 uni_code |= 1;
1136 uni_len++;
1139 uni_DCtab_lum_bits[level + 256] = uni_code;
1140 uni_DCtab_lum_len[level + 256] = uni_len;
1142 /* chrominance */
1143 uni_code = ff_mpeg4_DCtab_chrom[size][0];
1144 uni_len = ff_mpeg4_DCtab_chrom[size][1];
1146 if (size > 0) {
1147 uni_code <<= size;
1148 uni_code |= l;
1149 uni_len += size;
1150 if (size > 8) {
1151 uni_code <<= 1;
1152 uni_code |= 1;
1153 uni_len++;
1156 uni_DCtab_chrom_bits[level + 256] = uni_code;
1157 uni_DCtab_chrom_len[level + 256] = uni_len;
1161 static av_cold void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab,
1162 uint8_t *len_tab)
1164 int slevel, run, last;
1166 assert(MAX_LEVEL >= 64);
1167 assert(MAX_RUN >= 63);
1169 for (slevel = -64; slevel < 64; slevel++) {
1170 if (slevel == 0)
1171 continue;
1172 for (run = 0; run < 64; run++) {
1173 for (last = 0; last <= 1; last++) {
1174 const int index = UNI_MPEG4_ENC_INDEX(last, run, slevel + 64);
1175 int level = slevel < 0 ? -slevel : slevel;
1176 int sign = slevel < 0 ? 1 : 0;
1177 int bits, len, code;
1178 int level1, run1;
1180 len_tab[index] = 100;
1182 /* ESC0 */
1183 code = get_rl_index(rl, last, run, level);
1184 bits = rl->table_vlc[code][0];
1185 len = rl->table_vlc[code][1];
1186 bits = bits * 2 + sign;
1187 len++;
1189 if (code != rl->n && len < len_tab[index]) {
1190 bits_tab[index] = bits;
1191 len_tab[index] = len;
1193 /* ESC1 */
1194 bits = rl->table_vlc[rl->n][0];
1195 len = rl->table_vlc[rl->n][1];
1196 bits = bits * 2;
1197 len++; // esc1
1198 level1 = level - rl->max_level[last][run];
1199 if (level1 > 0) {
1200 code = get_rl_index(rl, last, run, level1);
1201 bits <<= rl->table_vlc[code][1];
1202 len += rl->table_vlc[code][1];
1203 bits += rl->table_vlc[code][0];
1204 bits = bits * 2 + sign;
1205 len++;
1207 if (code != rl->n && len < len_tab[index]) {
1208 bits_tab[index] = bits;
1209 len_tab[index] = len;
1212 /* ESC2 */
1213 bits = rl->table_vlc[rl->n][0];
1214 len = rl->table_vlc[rl->n][1];
1215 bits = bits * 4 + 2;
1216 len += 2; // esc2
1217 run1 = run - rl->max_run[last][level] - 1;
1218 if (run1 >= 0) {
1219 code = get_rl_index(rl, last, run1, level);
1220 bits <<= rl->table_vlc[code][1];
1221 len += rl->table_vlc[code][1];
1222 bits += rl->table_vlc[code][0];
1223 bits = bits * 2 + sign;
1224 len++;
1226 if (code != rl->n && len < len_tab[index]) {
1227 bits_tab[index] = bits;
1228 len_tab[index] = len;
1231 /* ESC3 */
1232 bits = rl->table_vlc[rl->n][0];
1233 len = rl->table_vlc[rl->n][1];
1234 bits = bits * 4 + 3;
1235 len += 2; // esc3
1236 bits = bits * 2 + last;
1237 len++;
1238 bits = bits * 64 + run;
1239 len += 6;
1240 bits = bits * 2 + 1;
1241 len++; // marker
1242 bits = bits * 4096 + (slevel & 0xfff);
1243 len += 12;
1244 bits = bits * 2 + 1;
1245 len++; // marker
1247 if (len < len_tab[index]) {
1248 bits_tab[index] = bits;
1249 len_tab[index] = len;
1256 static av_cold int encode_init(AVCodecContext *avctx)
1258 MpegEncContext *s = avctx->priv_data;
1259 int ret;
1260 static int done = 0;
1262 if ((ret = ff_mpv_encode_init(avctx)) < 0)
1263 return ret;
1265 if (!done) {
1266 done = 1;
1268 init_uni_dc_tab();
1270 ff_rl_init(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
1272 init_uni_mpeg4_rl_tab(&ff_mpeg4_rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
1273 init_uni_mpeg4_rl_tab(&ff_h263_rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
1276 s->min_qcoeff = -2048;
1277 s->max_qcoeff = 2047;
1278 s->intra_ac_vlc_length = uni_mpeg4_intra_rl_len;
1279 s->intra_ac_vlc_last_length = uni_mpeg4_intra_rl_len + 128 * 64;
1280 s->inter_ac_vlc_length = uni_mpeg4_inter_rl_len;
1281 s->inter_ac_vlc_last_length = uni_mpeg4_inter_rl_len + 128 * 64;
1282 s->luma_dc_vlc_length = uni_DCtab_lum_len;
1283 s->ac_esc_length = 7 + 2 + 1 + 6 + 1 + 12 + 1;
1284 s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
1285 s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
1287 if (s->avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
1288 s->avctx->extradata = av_malloc(1024);
1289 init_put_bits(&s->pb, s->avctx->extradata, 1024);
1291 if (!(s->workaround_bugs & FF_BUG_MS))
1292 mpeg4_encode_visual_object_header(s);
1293 mpeg4_encode_vol_header(s, 0, 0);
1295 // ff_mpeg4_stuffing(&s->pb); ?
1296 flush_put_bits(&s->pb);
1297 s->avctx->extradata_size = (put_bits_count(&s->pb) + 7) >> 3;
1299 return 0;
1302 void ff_mpeg4_init_partitions(MpegEncContext *s)
1304 uint8_t *start = put_bits_ptr(&s->pb);
1305 uint8_t *end = s->pb.buf_end;
1306 int size = end - start;
1307 int pb_size = (((intptr_t)start + size / 3) & (~3)) - (intptr_t)start;
1308 int tex_size = (size - 2 * pb_size) & (~3);
1310 set_put_bits_buffer_size(&s->pb, pb_size);
1311 init_put_bits(&s->tex_pb, start + pb_size, tex_size);
1312 init_put_bits(&s->pb2, start + pb_size + tex_size, pb_size);
1315 void ff_mpeg4_merge_partitions(MpegEncContext *s)
1317 const int pb2_len = put_bits_count(&s->pb2);
1318 const int tex_pb_len = put_bits_count(&s->tex_pb);
1319 const int bits = put_bits_count(&s->pb);
1321 if (s->pict_type == AV_PICTURE_TYPE_I) {
1322 put_bits(&s->pb, 19, DC_MARKER);
1323 s->misc_bits += 19 + pb2_len + bits - s->last_bits;
1324 s->i_tex_bits += tex_pb_len;
1325 } else {
1326 put_bits(&s->pb, 17, MOTION_MARKER);
1327 s->misc_bits += 17 + pb2_len;
1328 s->mv_bits += bits - s->last_bits;
1329 s->p_tex_bits += tex_pb_len;
1332 flush_put_bits(&s->pb2);
1333 flush_put_bits(&s->tex_pb);
1335 set_put_bits_buffer_size(&s->pb, s->pb2.buf_end - s->pb.buf);
1336 avpriv_copy_bits(&s->pb, s->pb2.buf, pb2_len);
1337 avpriv_copy_bits(&s->pb, s->tex_pb.buf, tex_pb_len);
1338 s->last_bits = put_bits_count(&s->pb);
1341 void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
1343 int mb_num_bits = av_log2(s->mb_num - 1) + 1;
1345 put_bits(&s->pb, ff_mpeg4_get_video_packet_prefix_length(s), 0);
1346 put_bits(&s->pb, 1, 1);
1348 put_bits(&s->pb, mb_num_bits, s->mb_x + s->mb_y * s->mb_width);
1349 put_bits(&s->pb, s->quant_precision, s->qscale);
1350 put_bits(&s->pb, 1, 0); /* no HEC */
1353 #define OFFSET(x) offsetof(MpegEncContext, x)
1354 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1355 static const AVOption options[] = {
1356 { "data_partitioning", "Use data partitioning.", OFFSET(data_partitioning), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1357 { "alternate_scan", "Enable alternate scantable.", OFFSET(alternate_scan), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1358 FF_MPV_COMMON_OPTS
1359 { NULL },
1362 static const AVClass mpeg4enc_class = {
1363 .class_name = "MPEG4 encoder",
1364 .item_name = av_default_item_name,
1365 .option = options,
1366 .version = LIBAVUTIL_VERSION_INT,
1369 AVCodec ff_mpeg4_encoder = {
1370 .name = "mpeg4",
1371 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
1372 .type = AVMEDIA_TYPE_VIDEO,
1373 .id = AV_CODEC_ID_MPEG4,
1374 .priv_data_size = sizeof(MpegEncContext),
1375 .init = encode_init,
1376 .encode2 = ff_mpv_encode_picture,
1377 .close = ff_mpv_encode_end,
1378 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
1379 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
1380 .priv_class = &mpeg4enc_class,