3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/attributes.h"
24 #include "libavutil/log.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/thread.h"
27 #include "mpegutils.h"
28 #include "mpegvideo.h"
30 #include "mpeg4video.h"
33 /* The uni_DCtab_* tables below contain unified bits+length tables to encode DC
34 * differences in MPEG-4. Unified in the sense that the specification specifies
35 * this encoding in several steps. */
36 static uint8_t uni_DCtab_lum_len[512];
37 static uint8_t uni_DCtab_chrom_len[512];
38 static uint16_t uni_DCtab_lum_bits[512];
39 static uint16_t uni_DCtab_chrom_bits[512];
41 /* Unified encoding tables for run length encoding of coefficients.
42 * Unified in the sense that the specification specifies the encoding in several steps. */
43 static uint32_t uni_mpeg4_intra_rl_bits[64 * 64 * 2 * 2];
44 static uint8_t uni_mpeg4_intra_rl_len[64 * 64 * 2 * 2];
45 static uint32_t uni_mpeg4_inter_rl_bits[64 * 64 * 2 * 2];
46 static uint8_t uni_mpeg4_inter_rl_len[64 * 64 * 2 * 2];
48 //#define UNI_MPEG4_ENC_INDEX(last, run, level) ((last) * 128 + (run) * 256 + (level))
49 //#define UNI_MPEG4_ENC_INDEX(last, run, level) ((last) * 128 * 64 + (run) + (level) * 64)
50 #define UNI_MPEG4_ENC_INDEX(last, run, level) ((last) * 128 * 64 + (run) * 128 + (level))
63 * Return the number of bits that encoding the 8x8 block in block would need.
64 * @param[in] block_last_index last index in scantable order that refers to a non zero element in block.
66 static inline int get_block_rate(MpegEncContext *s, int16_t block[64],
67 int block_last_index, uint8_t scantable[64])
73 for (j = 1; j <= block_last_index; j++) {
74 const int index = scantable[j];
75 int level = block[index];
78 if ((level & (~127)) == 0) {
79 if (j < block_last_index)
80 rate += s->intra_ac_vlc_length[UNI_AC_ENC_INDEX(j - last - 1, level)];
82 rate += s->intra_ac_vlc_last_length[UNI_AC_ENC_INDEX(j - last - 1, level)];
84 rate += s->ac_esc_length;
94 * Restore the ac coefficients in block that have been changed by decide_ac_pred().
95 * This function also restores s->block_last_index.
96 * @param[in,out] block MB coefficients, these will be restored
97 * @param[in] dir ac prediction direction for each 8x8 block
98 * @param[out] st scantable for each 8x8 block
99 * @param[in] zigzag_last_index index referring to the last non zero coefficient in zigzag order
101 static inline void restore_ac_coeffs(MpegEncContext *s, int16_t block[6][64],
102 const int dir[6], uint8_t *st[6],
103 const int zigzag_last_index[6])
106 memcpy(s->block_last_index, zigzag_last_index, sizeof(int) * 6);
108 for (n = 0; n < 6; n++) {
109 int16_t *ac_val = &s->ac_val[0][0][0] + s->block_index[n] * 16;
111 st[n] = s->intra_scantable.permutated;
114 for (i = 1; i < 8; i++)
115 block[n][s->idsp.idct_permutation[i]] = ac_val[i + 8];
117 /* left prediction */
118 for (i = 1; i < 8; i++)
119 block[n][s->idsp.idct_permutation[i << 3]] = ac_val[i];
125 * Return the optimal value (0 or 1) for the ac_pred element for the given MB in MPEG-4.
126 * This function will also update s->block_last_index and s->ac_val.
127 * @param[in,out] block MB coefficients, these will be updated if 1 is returned
128 * @param[in] dir ac prediction direction for each 8x8 block
129 * @param[out] st scantable for each 8x8 block
130 * @param[out] zigzag_last_index index referring to the last non zero coefficient in zigzag order
132 static inline int decide_ac_pred(MpegEncContext *s, int16_t block[6][64],
133 const int dir[6], uint8_t *st[6],
134 int zigzag_last_index[6])
138 int8_t *const qscale_table = s->current_picture.qscale_table;
140 memcpy(zigzag_last_index, s->block_last_index, sizeof(int) * 6);
142 for (n = 0; n < 6; n++) {
143 int16_t *ac_val, *ac_val1;
145 score -= get_block_rate(s, block[n], s->block_last_index[n],
146 s->intra_scantable.permutated);
148 ac_val = &s->ac_val[0][0][0] + s->block_index[n] * 16;
151 const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
153 ac_val -= s->block_wrap[n] * 16;
154 if (s->mb_y == 0 || s->qscale == qscale_table[xy] || n == 2 || n == 3) {
156 for (i = 1; i < 8; i++) {
157 const int level = block[n][s->idsp.idct_permutation[i]];
158 block[n][s->idsp.idct_permutation[i]] = level - ac_val[i + 8];
159 ac_val1[i] = block[n][s->idsp.idct_permutation[i << 3]];
160 ac_val1[i + 8] = level;
163 /* different qscale, we must rescale */
164 for (i = 1; i < 8; i++) {
165 const int level = block[n][s->idsp.idct_permutation[i]];
166 block[n][s->idsp.idct_permutation[i]] = level - ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
167 ac_val1[i] = block[n][s->idsp.idct_permutation[i << 3]];
168 ac_val1[i + 8] = level;
171 st[n] = s->intra_h_scantable.permutated;
173 const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
174 /* left prediction */
176 if (s->mb_x == 0 || s->qscale == qscale_table[xy] || n == 1 || n == 3) {
178 for (i = 1; i < 8; i++) {
179 const int level = block[n][s->idsp.idct_permutation[i << 3]];
180 block[n][s->idsp.idct_permutation[i << 3]] = level - ac_val[i];
182 ac_val1[i + 8] = block[n][s->idsp.idct_permutation[i]];
185 /* different qscale, we must rescale */
186 for (i = 1; i < 8; i++) {
187 const int level = block[n][s->idsp.idct_permutation[i << 3]];
188 block[n][s->idsp.idct_permutation[i << 3]] = level - ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
190 ac_val1[i + 8] = block[n][s->idsp.idct_permutation[i]];
193 st[n] = s->intra_v_scantable.permutated;
196 for (i = 63; i > 0; i--) // FIXME optimize
197 if (block[n][st[n][i]])
199 s->block_last_index[n] = i;
201 score += get_block_rate(s, block[n], s->block_last_index[n], st[n]);
207 restore_ac_coeffs(s, block, dir, st, zigzag_last_index);
213 * modify mb_type & qscale so that encoding is actually possible in MPEG-4
215 void ff_clean_mpeg4_qscales(MpegEncContext *s)
218 int8_t *const qscale_table = s->current_picture.qscale_table;
220 ff_clean_h263_qscales(s);
222 if (s->pict_type == AV_PICTURE_TYPE_B) {
224 /* ok, come on, this isn't funny anymore, there's more code for
225 * handling this MPEG-4 mess than for the actual adaptive quantization */
227 for (i = 0; i < s->mb_num; i++) {
228 int mb_xy = s->mb_index2xy[i];
229 odd += qscale_table[mb_xy] & 1;
232 if (2 * odd > s->mb_num)
237 for (i = 0; i < s->mb_num; i++) {
238 int mb_xy = s->mb_index2xy[i];
239 if ((qscale_table[mb_xy] & 1) != odd)
240 qscale_table[mb_xy]++;
241 if (qscale_table[mb_xy] > 31)
242 qscale_table[mb_xy] = 31;
245 for (i = 1; i < s->mb_num; i++) {
246 int mb_xy = s->mb_index2xy[i];
247 if (qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i - 1]] &&
248 (s->mb_type[mb_xy] & CANDIDATE_MB_TYPE_DIRECT)) {
249 s->mb_type[mb_xy] |= CANDIDATE_MB_TYPE_BIDIR;
256 * Encode the dc value.
257 * @param n block index (0-3 are luma, 4-5 are chroma)
259 static inline void mpeg4_encode_dc(PutBitContext *s, int level, int n)
261 /* DC will overflow if level is outside the [-255,255] range. */
265 put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]);
268 put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]);
272 static inline int mpeg4_get_dc_length(int level, int n)
275 return uni_DCtab_lum_len[level + 256];
277 return uni_DCtab_chrom_len[level + 256];
281 * Encode an 8x8 block.
282 * @param n block index (0-3 are luma, 4-5 are chroma)
284 static inline void mpeg4_encode_block(MpegEncContext *s,
285 int16_t *block, int n, int intra_dc,
286 uint8_t *scan_table, PutBitContext *dc_pb,
287 PutBitContext *ac_pb)
289 int i, last_non_zero;
292 const int last_index = s->block_last_index[n];
294 if (s->mb_intra) { // Note gcc (3.2.1 at least) will optimize this away
295 /* MPEG-4 based DC predictor */
296 mpeg4_encode_dc(dc_pb, intra_dc, n);
300 bits_tab = uni_mpeg4_intra_rl_bits;
301 len_tab = uni_mpeg4_intra_rl_len;
306 bits_tab = uni_mpeg4_inter_rl_bits;
307 len_tab = uni_mpeg4_inter_rl_len;
311 last_non_zero = i - 1;
312 for (; i < last_index; i++) {
313 int level = block[scan_table[i]];
315 int run = i - last_non_zero - 1;
317 if ((level & (~127)) == 0) {
318 const int index = UNI_MPEG4_ENC_INDEX(0, run, level);
319 put_bits(ac_pb, len_tab[index], bits_tab[index]);
322 7 + 2 + 1 + 6 + 1 + 12 + 1,
323 (3 << 23) + (3 << 21) + (0 << 20) + (run << 14) +
324 (1 << 13) + (((level - 64) & 0xfff) << 1) + 1);
329 /* if (i <= last_index) */ {
330 int level = block[scan_table[i]];
331 int run = i - last_non_zero - 1;
333 if ((level & (~127)) == 0) {
334 const int index = UNI_MPEG4_ENC_INDEX(1, run, level);
335 put_bits(ac_pb, len_tab[index], bits_tab[index]);
338 7 + 2 + 1 + 6 + 1 + 12 + 1,
339 (3 << 23) + (3 << 21) + (1 << 20) + (run << 14) +
340 (1 << 13) + (((level - 64) & 0xfff) << 1) + 1);
345 static int mpeg4_get_block_length(MpegEncContext *s,
346 int16_t *block, int n,
347 int intra_dc, uint8_t *scan_table)
349 int i, last_non_zero;
351 const int last_index = s->block_last_index[n];
354 if (s->mb_intra) { // Note gcc (3.2.1 at least) will optimize this away
355 /* MPEG-4 based DC predictor */
356 len += mpeg4_get_dc_length(intra_dc, n);
360 len_tab = uni_mpeg4_intra_rl_len;
365 len_tab = uni_mpeg4_inter_rl_len;
369 last_non_zero = i - 1;
370 for (; i < last_index; i++) {
371 int level = block[scan_table[i]];
373 int run = i - last_non_zero - 1;
375 if ((level & (~127)) == 0) {
376 const int index = UNI_MPEG4_ENC_INDEX(0, run, level);
377 len += len_tab[index];
379 len += 7 + 2 + 1 + 6 + 1 + 12 + 1;
384 /* if (i <= last_index) */ {
385 int level = block[scan_table[i]];
386 int run = i - last_non_zero - 1;
388 if ((level & (~127)) == 0) {
389 const int index = UNI_MPEG4_ENC_INDEX(1, run, level);
390 len += len_tab[index];
392 len += 7 + 2 + 1 + 6 + 1 + 12 + 1;
399 static inline void mpeg4_encode_blocks(MpegEncContext *s, int16_t block[6][64],
400 int intra_dc[6], uint8_t **scan_table,
401 PutBitContext *dc_pb,
402 PutBitContext *ac_pb)
407 if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT) {
408 for (i = 0; i < 6; i++)
409 skip_put_bits(&s->pb,
410 mpeg4_get_block_length(s, block[i], i,
411 intra_dc[i], scan_table[i]));
413 /* encode each block */
414 for (i = 0; i < 6; i++)
415 mpeg4_encode_block(s, block[i], i,
416 intra_dc[i], scan_table[i], dc_pb, ac_pb);
419 if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT) {
420 for (i = 0; i < 6; i++)
421 skip_put_bits(&s->pb,
422 mpeg4_get_block_length(s, block[i], i, 0,
423 s->intra_scantable.permutated));
425 /* encode each block */
426 for (i = 0; i < 6; i++)
427 mpeg4_encode_block(s, block[i], i, 0,
428 s->intra_scantable.permutated, dc_pb, ac_pb);
433 static inline int get_b_cbp(MpegEncContext *s, int16_t block[6][64],
434 int motion_x, int motion_y, int mb_type)
438 if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
440 const int lambda = s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
442 for (i = 0; i < 6; i++) {
443 if (s->coded_score[i] < 0) {
444 score += s->coded_score[i];
451 if ((motion_x | motion_y | s->dquant | mb_type) == 0)
452 zero_score -= 4; // 2 * MV + mb_type + cbp bit
454 zero_score *= lambda;
455 if (zero_score <= score)
459 for (i = 0; i < 6; i++) {
460 if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i)) & 1) == 0) {
461 s->block_last_index[i] = -1;
462 s->bdsp.clear_block(s->block[i]);
466 for (i = 0; i < 6; i++) {
467 if (s->block_last_index[i] >= 0)
474 // FIXME this is duplicated to h263.c
475 static const int dquant_code[5] = { 1, 0, 9, 2, 3 };
477 void ff_mpeg4_encode_mb(MpegEncContext *s, int16_t block[6][64],
478 int motion_x, int motion_y)
480 int cbpc, cbpy, pred_x, pred_y;
481 PutBitContext *const pb2 = s->data_partitioning ? &s->pb2 : &s->pb;
482 PutBitContext *const tex_pb = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B ? &s->tex_pb : &s->pb;
483 PutBitContext *const dc_pb = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_I ? &s->pb2 : &s->pb;
484 const int interleaved_stats = (s->avctx->flags & AV_CODEC_FLAG_PASS1) && !s->data_partitioning ? 1 : 0;
489 if (s->pict_type == AV_PICTURE_TYPE_B) {
490 /* convert from mv_dir to type */
491 static const int mb_type_table[8] = { -1, 3, 2, 1, -1, -1, -1, 0 };
492 int mb_type = mb_type_table[s->mv_dir];
495 for (i = 0; i < 2; i++)
496 s->last_mv[i][0][0] =
497 s->last_mv[i][0][1] =
498 s->last_mv[i][1][0] =
499 s->last_mv[i][1][1] = 0;
502 av_assert2(s->dquant >= -2 && s->dquant <= 2);
503 av_assert2((s->dquant & 1) == 0);
504 av_assert2(mb_type >= 0);
506 /* nothing to do if this MB was skipped in the next P-frame */
507 if (s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]) { // FIXME avoid DCT & ...
513 s->mv_dir = MV_DIR_FORWARD; // doesn't matter
514 s->qscale -= s->dquant;
515 // s->mb_skipped = 1;
520 cbp = get_b_cbp(s, block, motion_x, motion_y, mb_type);
522 if ((cbp | motion_x | motion_y | mb_type) == 0) {
523 /* direct MB with MV={0,0} */
524 av_assert2(s->dquant == 0);
526 put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */
528 if (interleaved_stats) {
536 put_bits(&s->pb, 1, 0); /* mb coded modb1=0 */
537 put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ // FIXME merge
538 put_bits(&s->pb, mb_type + 1, 1); // this table is so simple that we don't need it :)
540 put_bits(&s->pb, 6, cbp);
542 if (cbp && mb_type) {
544 put_bits(&s->pb, 2, (s->dquant >> 2) + 3);
546 put_bits(&s->pb, 1, 0);
548 s->qscale -= s->dquant;
550 if (!s->progressive_sequence) {
552 put_bits(&s->pb, 1, s->interlaced_dct);
553 if (mb_type) // not direct mode
554 put_bits(&s->pb, 1, s->mv_type == MV_TYPE_FIELD);
557 if (interleaved_stats)
558 s->misc_bits += get_bits_diff(s);
561 av_assert2(s->mv_dir & MV_DIRECT);
562 ff_h263_encode_motion_vector(s, motion_x, motion_y, 1);
566 av_assert2(mb_type > 0 && mb_type < 4);
567 if (s->mv_type != MV_TYPE_FIELD) {
568 if (s->mv_dir & MV_DIR_FORWARD) {
569 ff_h263_encode_motion_vector(s,
570 s->mv[0][0][0] - s->last_mv[0][0][0],
571 s->mv[0][0][1] - s->last_mv[0][0][1],
573 s->last_mv[0][0][0] =
574 s->last_mv[0][1][0] = s->mv[0][0][0];
575 s->last_mv[0][0][1] =
576 s->last_mv[0][1][1] = s->mv[0][0][1];
579 if (s->mv_dir & MV_DIR_BACKWARD) {
580 ff_h263_encode_motion_vector(s,
581 s->mv[1][0][0] - s->last_mv[1][0][0],
582 s->mv[1][0][1] - s->last_mv[1][0][1],
584 s->last_mv[1][0][0] =
585 s->last_mv[1][1][0] = s->mv[1][0][0];
586 s->last_mv[1][0][1] =
587 s->last_mv[1][1][1] = s->mv[1][0][1];
591 if (s->mv_dir & MV_DIR_FORWARD) {
592 put_bits(&s->pb, 1, s->field_select[0][0]);
593 put_bits(&s->pb, 1, s->field_select[0][1]);
595 if (s->mv_dir & MV_DIR_BACKWARD) {
596 put_bits(&s->pb, 1, s->field_select[1][0]);
597 put_bits(&s->pb, 1, s->field_select[1][1]);
599 if (s->mv_dir & MV_DIR_FORWARD) {
600 for (i = 0; i < 2; i++) {
601 ff_h263_encode_motion_vector(s,
602 s->mv[0][i][0] - s->last_mv[0][i][0],
603 s->mv[0][i][1] - s->last_mv[0][i][1] / 2,
605 s->last_mv[0][i][0] = s->mv[0][i][0];
606 s->last_mv[0][i][1] = s->mv[0][i][1] * 2;
610 if (s->mv_dir & MV_DIR_BACKWARD) {
611 for (i = 0; i < 2; i++) {
612 ff_h263_encode_motion_vector(s,
613 s->mv[1][i][0] - s->last_mv[1][i][0],
614 s->mv[1][i][1] - s->last_mv[1][i][1] / 2,
616 s->last_mv[1][i][0] = s->mv[1][i][0];
617 s->last_mv[1][i][1] = s->mv[1][i][1] * 2;
624 if (interleaved_stats)
625 s->mv_bits += get_bits_diff(s);
627 mpeg4_encode_blocks(s, block, NULL, NULL, NULL, &s->pb);
629 if (interleaved_stats)
630 s->p_tex_bits += get_bits_diff(s);
631 } else { /* s->pict_type==AV_PICTURE_TYPE_B */
632 cbp = get_p_cbp(s, block, motion_x, motion_y);
634 if ((cbp | motion_x | motion_y | s->dquant) == 0 &&
635 s->mv_type == MV_TYPE_16X16) {
636 /* Check if the B-frames can skip it too, as we must skip it
637 * if we skip here why didn't they just compress
638 * the skip-mb bits instead of reusing them ?! */
639 if (s->max_b_frames > 0) {
647 offset = x + y * s->linesize;
648 p_pic = s->new_picture.f->data[0] + offset;
651 for (i = 0; i < s->max_b_frames; i++) {
654 Picture *pic = s->reordered_input_picture[i + 1];
656 if (!pic || pic->f->pict_type != AV_PICTURE_TYPE_B)
659 b_pic = pic->f->data[0] + offset;
661 b_pic += INPLACE_OFFSET;
663 if (x + 16 > s->width || y + 16 > s->height) {
665 int xe = FFMIN(16, s->width - x);
666 int ye = FFMIN(16, s->height - y);
668 for (y1 = 0; y1 < ye; y1++) {
669 for (x1 = 0; x1 < xe; x1++) {
670 diff += FFABS(p_pic[x1 + y1 * s->linesize] - b_pic[x1 + y1 * s->linesize]);
673 diff = diff * 256 / (xe * ye);
675 diff = s->mecc.sad[0](NULL, p_pic, b_pic, s->linesize, 16);
677 if (diff > s->qscale * 70) { // FIXME check that 70 is optimal
685 if (s->mb_skipped == 1) {
686 /* skip macroblock */
687 put_bits(&s->pb, 1, 1);
689 if (interleaved_stats) {
699 put_bits(&s->pb, 1, 0); /* mb coded */
703 if (s->mv_type == MV_TYPE_16X16) {
707 ff_h263_inter_MCBPC_bits[cbpc],
708 ff_h263_inter_MCBPC_code[cbpc]);
710 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
712 put_bits(pb2, 2, dquant_code[s->dquant + 2]);
714 if (!s->progressive_sequence) {
716 put_bits(pb2, 1, s->interlaced_dct);
720 if (interleaved_stats)
721 s->misc_bits += get_bits_diff(s);
723 /* motion vectors: 16x16 mode */
724 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
726 ff_h263_encode_motion_vector(s,
730 } else if (s->mv_type == MV_TYPE_FIELD) {
734 ff_h263_inter_MCBPC_bits[cbpc],
735 ff_h263_inter_MCBPC_code[cbpc]);
737 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
739 put_bits(pb2, 2, dquant_code[s->dquant + 2]);
741 av_assert2(!s->progressive_sequence);
743 put_bits(pb2, 1, s->interlaced_dct);
746 if (interleaved_stats)
747 s->misc_bits += get_bits_diff(s);
749 /* motion vectors: 16x8 interlaced mode */
750 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
753 put_bits(&s->pb, 1, s->field_select[0][0]);
754 put_bits(&s->pb, 1, s->field_select[0][1]);
756 ff_h263_encode_motion_vector(s,
757 s->mv[0][0][0] - pred_x,
758 s->mv[0][0][1] - pred_y,
760 ff_h263_encode_motion_vector(s,
761 s->mv[0][1][0] - pred_x,
762 s->mv[0][1][1] - pred_y,
765 av_assert2(s->mv_type == MV_TYPE_8X8);
767 ff_h263_inter_MCBPC_bits[cbpc + 16],
768 ff_h263_inter_MCBPC_code[cbpc + 16]);
769 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
771 if (!s->progressive_sequence && cbp)
772 put_bits(pb2, 1, s->interlaced_dct);
774 if (interleaved_stats)
775 s->misc_bits += get_bits_diff(s);
777 for (i = 0; i < 4; i++) {
778 /* motion vectors: 8x8 mode*/
779 ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
781 ff_h263_encode_motion_vector(s,
782 s->current_picture.motion_val[0][s->block_index[i]][0] - pred_x,
783 s->current_picture.motion_val[0][s->block_index[i]][1] - pred_y,
788 if (interleaved_stats)
789 s->mv_bits += get_bits_diff(s);
791 mpeg4_encode_blocks(s, block, NULL, NULL, NULL, tex_pb);
793 if (interleaved_stats)
794 s->p_tex_bits += get_bits_diff(s);
800 int dc_diff[6]; // dc values with the dc prediction subtracted
801 int dir[6]; // prediction direction
802 int zigzag_last_index[6];
803 uint8_t *scan_table[6];
806 for (i = 0; i < 6; i++)
807 dc_diff[i] = ff_mpeg4_pred_dc(s, i, block[i][0], &dir[i], 1);
809 if (s->avctx->flags & AV_CODEC_FLAG_AC_PRED) {
810 s->ac_pred = decide_ac_pred(s, block, dir, scan_table, zigzag_last_index);
812 for (i = 0; i < 6; i++)
813 scan_table[i] = s->intra_scantable.permutated;
818 for (i = 0; i < 6; i++)
819 if (s->block_last_index[i] >= 1)
823 if (s->pict_type == AV_PICTURE_TYPE_I) {
827 ff_h263_intra_MCBPC_bits[cbpc],
828 ff_h263_intra_MCBPC_code[cbpc]);
832 put_bits(&s->pb, 1, 0); /* mb coded */
834 ff_h263_inter_MCBPC_bits[cbpc + 4],
835 ff_h263_inter_MCBPC_code[cbpc + 4]);
837 put_bits(pb2, 1, s->ac_pred);
839 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
841 put_bits(dc_pb, 2, dquant_code[s->dquant + 2]);
843 if (!s->progressive_sequence)
844 put_bits(dc_pb, 1, s->interlaced_dct);
846 if (interleaved_stats)
847 s->misc_bits += get_bits_diff(s);
849 mpeg4_encode_blocks(s, block, dc_diff, scan_table, dc_pb, tex_pb);
851 if (interleaved_stats)
852 s->i_tex_bits += get_bits_diff(s);
855 /* restore ac coeffs & last_index stuff
856 * if we messed them up with the prediction */
858 restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index);
863 * add MPEG-4 stuffing bits (01...1)
865 void ff_mpeg4_stuffing(PutBitContext *pbc)
869 length = (-put_bits_count(pbc)) & 7;
871 put_bits(pbc, length, (1 << length) - 1);
874 /* must be called before writing the header */
875 void ff_set_mpeg4_time(MpegEncContext *s)
877 if (s->pict_type == AV_PICTURE_TYPE_B) {
878 ff_mpeg4_init_direct_mv(s);
880 s->last_time_base = s->time_base;
881 s->time_base = FFUDIV(s->time, s->avctx->time_base.den);
885 static void mpeg4_encode_gop_header(MpegEncContext *s)
887 int64_t hours, minutes, seconds;
890 put_bits(&s->pb, 16, 0);
891 put_bits(&s->pb, 16, GOP_STARTCODE);
893 time = s->current_picture_ptr->f->pts;
894 if (s->reordered_input_picture[1])
895 time = FFMIN(time, s->reordered_input_picture[1]->f->pts);
896 time = time * s->avctx->time_base.num;
897 s->last_time_base = FFUDIV(time, s->avctx->time_base.den);
899 seconds = FFUDIV(time, s->avctx->time_base.den);
900 minutes = FFUDIV(seconds, 60); seconds = FFUMOD(seconds, 60);
901 hours = FFUDIV(minutes, 60); minutes = FFUMOD(minutes, 60);
902 hours = FFUMOD(hours , 24);
904 put_bits(&s->pb, 5, hours);
905 put_bits(&s->pb, 6, minutes);
906 put_bits(&s->pb, 1, 1);
907 put_bits(&s->pb, 6, seconds);
909 put_bits(&s->pb, 1, !!(s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP));
910 put_bits(&s->pb, 1, 0); // broken link == NO
912 ff_mpeg4_stuffing(&s->pb);
915 static void mpeg4_encode_visual_object_header(MpegEncContext *s)
917 int profile_and_level_indication;
920 if (s->avctx->profile != FF_PROFILE_UNKNOWN) {
921 profile_and_level_indication = s->avctx->profile << 4;
922 } else if (s->max_b_frames || s->quarter_sample) {
923 profile_and_level_indication = 0xF0; // adv simple
925 profile_and_level_indication = 0x00; // simple
928 if (s->avctx->level != FF_LEVEL_UNKNOWN)
929 profile_and_level_indication |= s->avctx->level;
931 profile_and_level_indication |= 1; // level 1
933 if (profile_and_level_indication >> 4 == 0xF)
940 put_bits(&s->pb, 16, 0);
941 put_bits(&s->pb, 16, VOS_STARTCODE);
943 put_bits(&s->pb, 8, profile_and_level_indication);
945 put_bits(&s->pb, 16, 0);
946 put_bits(&s->pb, 16, VISUAL_OBJ_STARTCODE);
948 put_bits(&s->pb, 1, 1);
949 put_bits(&s->pb, 4, vo_ver_id);
950 put_bits(&s->pb, 3, 1); // priority
952 put_bits(&s->pb, 4, 1); // visual obj type== video obj
954 put_bits(&s->pb, 1, 0); // video signal type == no clue // FIXME
956 ff_mpeg4_stuffing(&s->pb);
959 static void mpeg4_encode_vol_header(MpegEncContext *s,
965 if (s->max_b_frames || s->quarter_sample) {
967 s->vo_type = ADV_SIMPLE_VO_TYPE;
970 s->vo_type = SIMPLE_VO_TYPE;
973 put_bits(&s->pb, 16, 0);
974 put_bits(&s->pb, 16, 0x100 + vo_number); /* video obj */
975 put_bits(&s->pb, 16, 0);
976 put_bits(&s->pb, 16, 0x120 + vol_number); /* video obj layer */
978 put_bits(&s->pb, 1, 0); /* random access vol */
979 put_bits(&s->pb, 8, s->vo_type); /* video obj type indication */
980 if (s->workaround_bugs & FF_BUG_MS) {
981 put_bits(&s->pb, 1, 0); /* is obj layer id= no */
983 put_bits(&s->pb, 1, 1); /* is obj layer id= yes */
984 put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
985 put_bits(&s->pb, 3, 1); /* is obj layer priority */
988 s->aspect_ratio_info = ff_h263_aspect_to_info(s->avctx->sample_aspect_ratio);
990 put_bits(&s->pb, 4, s->aspect_ratio_info); /* aspect ratio info */
991 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
992 av_reduce(&s->avctx->sample_aspect_ratio.num, &s->avctx->sample_aspect_ratio.den,
993 s->avctx->sample_aspect_ratio.num, s->avctx->sample_aspect_ratio.den, 255);
994 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
995 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
998 if (s->workaround_bugs & FF_BUG_MS) {
999 put_bits(&s->pb, 1, 0); /* vol control parameters= no @@@ */
1001 put_bits(&s->pb, 1, 1); /* vol control parameters= yes */
1002 put_bits(&s->pb, 2, 1); /* chroma format YUV 420/YV12 */
1003 put_bits(&s->pb, 1, s->low_delay);
1004 put_bits(&s->pb, 1, 0); /* vbv parameters= no */
1007 put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */
1008 put_bits(&s->pb, 1, 1); /* marker bit */
1010 put_bits(&s->pb, 16, s->avctx->time_base.den);
1011 if (s->time_increment_bits < 1)
1012 s->time_increment_bits = 1;
1013 put_bits(&s->pb, 1, 1); /* marker bit */
1014 put_bits(&s->pb, 1, 0); /* fixed vop rate=no */
1015 put_bits(&s->pb, 1, 1); /* marker bit */
1016 put_bits(&s->pb, 13, s->width); /* vol width */
1017 put_bits(&s->pb, 1, 1); /* marker bit */
1018 put_bits(&s->pb, 13, s->height); /* vol height */
1019 put_bits(&s->pb, 1, 1); /* marker bit */
1020 put_bits(&s->pb, 1, s->progressive_sequence ? 0 : 1);
1021 put_bits(&s->pb, 1, 1); /* obmc disable */
1023 put_bits(&s->pb, 1, 0); /* sprite enable */
1025 put_bits(&s->pb, 2, 0); /* sprite enable */
1027 put_bits(&s->pb, 1, 0); /* not 8 bit == false */
1028 put_bits(&s->pb, 1, s->mpeg_quant); /* quant type = (0 = H.263 style) */
1030 if (s->mpeg_quant) {
1031 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
1032 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
1036 put_bits(&s->pb, 1, s->quarter_sample);
1037 put_bits(&s->pb, 1, 1); /* complexity estimation disable */
1038 put_bits(&s->pb, 1, s->rtp_mode ? 0 : 1); /* resync marker disable */
1039 put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
1040 if (s->data_partitioning)
1041 put_bits(&s->pb, 1, 0); /* no rvlc */
1043 if (vo_ver_id != 1) {
1044 put_bits(&s->pb, 1, 0); /* newpred */
1045 put_bits(&s->pb, 1, 0); /* reduced res vop */
1047 put_bits(&s->pb, 1, 0); /* scalability */
1049 ff_mpeg4_stuffing(&s->pb);
1052 if (!(s->avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
1053 put_bits(&s->pb, 16, 0);
1054 put_bits(&s->pb, 16, 0x1B2); /* user_data */
1055 ff_put_string(&s->pb, LIBAVCODEC_IDENT, 0);
1059 /* write MPEG-4 VOP header */
1060 int ff_mpeg4_encode_picture_header(MpegEncContext *s, int picture_number)
1063 int64_t time_div, time_mod;
1065 if (s->pict_type == AV_PICTURE_TYPE_I) {
1066 if (!(s->avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) {
1067 if (s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT) // HACK, the reference sw is buggy
1068 mpeg4_encode_visual_object_header(s);
1069 if (s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT || picture_number == 0) // HACK, the reference sw is buggy
1070 mpeg4_encode_vol_header(s, 0, 0);
1072 if (!(s->workaround_bugs & FF_BUG_MS))
1073 mpeg4_encode_gop_header(s);
1076 s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
1078 put_bits(&s->pb, 16, 0); /* vop header */
1079 put_bits(&s->pb, 16, VOP_STARTCODE); /* vop header */
1080 put_bits(&s->pb, 2, s->pict_type - 1); /* pict type: I = 0 , P = 1 */
1082 time_div = FFUDIV(s->time, s->avctx->time_base.den);
1083 time_mod = FFUMOD(s->time, s->avctx->time_base.den);
1084 time_incr = time_div - s->last_time_base;
1086 // This limits the frame duration to max 1 hour
1087 if (time_incr > 3600) {
1088 av_log(s->avctx, AV_LOG_ERROR, "time_incr %"PRIu64" too large\n", time_incr);
1089 return AVERROR(EINVAL);
1092 put_bits(&s->pb, 1, 1);
1094 put_bits(&s->pb, 1, 0);
1096 put_bits(&s->pb, 1, 1); /* marker */
1097 put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
1098 put_bits(&s->pb, 1, 1); /* marker */
1099 put_bits(&s->pb, 1, 1); /* vop coded */
1100 if (s->pict_type == AV_PICTURE_TYPE_P) {
1101 put_bits(&s->pb, 1, s->no_rounding); /* rounding type */
1103 put_bits(&s->pb, 3, 0); /* intra dc VLC threshold */
1104 if (!s->progressive_sequence) {
1105 put_bits(&s->pb, 1, s->current_picture_ptr->f->top_field_first);
1106 put_bits(&s->pb, 1, s->alternate_scan);
1108 // FIXME sprite stuff
1110 put_bits(&s->pb, 5, s->qscale);
1112 if (s->pict_type != AV_PICTURE_TYPE_I)
1113 put_bits(&s->pb, 3, s->f_code); /* fcode_for */
1114 if (s->pict_type == AV_PICTURE_TYPE_B)
1115 put_bits(&s->pb, 3, s->b_code); /* fcode_back */
1120 static av_cold void init_uni_dc_tab(void)
1122 int level, uni_code, uni_len;
1124 for (level = -256; level < 256; level++) {
1126 /* find number of bits */
1135 l = (-level) ^ ((1 << size) - 1);
1140 uni_code = ff_mpeg4_DCtab_lum[size][0];
1141 uni_len = ff_mpeg4_DCtab_lum[size][1];
1153 uni_DCtab_lum_bits[level + 256] = uni_code;
1154 uni_DCtab_lum_len[level + 256] = uni_len;
1157 uni_code = ff_mpeg4_DCtab_chrom[size][0];
1158 uni_len = ff_mpeg4_DCtab_chrom[size][1];
1170 uni_DCtab_chrom_bits[level + 256] = uni_code;
1171 uni_DCtab_chrom_len[level + 256] = uni_len;
1175 static av_cold void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab,
1178 int slevel, run, last;
1180 av_assert0(MAX_LEVEL >= 64);
1181 av_assert0(MAX_RUN >= 63);
1183 for (slevel = -64; slevel < 64; slevel++) {
1186 for (run = 0; run < 64; run++) {
1187 for (last = 0; last <= 1; last++) {
1188 const int index = UNI_MPEG4_ENC_INDEX(last, run, slevel + 64);
1189 int level = slevel < 0 ? -slevel : slevel;
1190 int sign = slevel < 0 ? 1 : 0;
1191 int bits, len, code;
1194 len_tab[index] = 100;
1197 code = get_rl_index(rl, last, run, level);
1198 bits = rl->table_vlc[code][0];
1199 len = rl->table_vlc[code][1];
1200 bits = bits * 2 + sign;
1203 if (code != rl->n && len < len_tab[index]) {
1204 bits_tab[index] = bits;
1205 len_tab[index] = len;
1208 bits = rl->table_vlc[rl->n][0];
1209 len = rl->table_vlc[rl->n][1];
1212 level1 = level - rl->max_level[last][run];
1214 code = get_rl_index(rl, last, run, level1);
1215 bits <<= rl->table_vlc[code][1];
1216 len += rl->table_vlc[code][1];
1217 bits += rl->table_vlc[code][0];
1218 bits = bits * 2 + sign;
1221 if (code != rl->n && len < len_tab[index]) {
1222 bits_tab[index] = bits;
1223 len_tab[index] = len;
1227 bits = rl->table_vlc[rl->n][0];
1228 len = rl->table_vlc[rl->n][1];
1229 bits = bits * 4 + 2;
1231 run1 = run - rl->max_run[last][level] - 1;
1233 code = get_rl_index(rl, last, run1, level);
1234 bits <<= rl->table_vlc[code][1];
1235 len += rl->table_vlc[code][1];
1236 bits += rl->table_vlc[code][0];
1237 bits = bits * 2 + sign;
1240 if (code != rl->n && len < len_tab[index]) {
1241 bits_tab[index] = bits;
1242 len_tab[index] = len;
1246 bits = rl->table_vlc[rl->n][0];
1247 len = rl->table_vlc[rl->n][1];
1248 bits = bits * 4 + 3;
1250 bits = bits * 2 + last;
1252 bits = bits * 64 + run;
1254 bits = bits * 2 + 1;
1256 bits = bits * 4096 + (slevel & 0xfff);
1258 bits = bits * 2 + 1;
1261 if (len < len_tab[index]) {
1262 bits_tab[index] = bits;
1263 len_tab[index] = len;
1270 static av_cold void mpeg4_encode_init_static(void)
1274 ff_mpeg4_init_rl_intra();
1276 init_uni_mpeg4_rl_tab(&ff_mpeg4_rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
1277 init_uni_mpeg4_rl_tab(&ff_h263_rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
1280 static av_cold int encode_init(AVCodecContext *avctx)
1282 static AVOnce init_static_once = AV_ONCE_INIT;
1283 MpegEncContext *s = avctx->priv_data;
1286 if (avctx->width >= (1<<13) || avctx->height >= (1<<13)) {
1287 av_log(avctx, AV_LOG_ERROR, "dimensions too large for MPEG-4\n");
1288 return AVERROR(EINVAL);
1291 if ((ret = ff_mpv_encode_init(avctx)) < 0)
1294 ff_thread_once(&init_static_once, mpeg4_encode_init_static);
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;
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);
1313 if (!(s->workaround_bugs & FF_BUG_MS))
1314 mpeg4_encode_visual_object_header(s);
1315 mpeg4_encode_vol_header(s, 0, 0);
1317 // ff_mpeg4_stuffing(&s->pb); ?
1318 flush_put_bits(&s->pb);
1319 s->avctx->extradata_size = put_bytes_output(&s->pb);
1324 void ff_mpeg4_init_partitions(MpegEncContext *s)
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);
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);
1337 void ff_mpeg4_merge_partitions(MpegEncContext *s)
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);
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;
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;
1354 flush_put_bits(&s->pb2);
1355 flush_put_bits(&s->tex_pb);
1357 set_put_bits_buffer_size(&s->pb, s->pb2.buf_end - s->pb.buf);
1358 ff_copy_bits(&s->pb, s->pb2.buf, pb2_len);
1359 ff_copy_bits(&s->pb, s->tex_pb.buf, tex_pb_len);
1360 s->last_bits = put_bits_count(&s->pb);
1363 void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
1365 int mb_num_bits = av_log2(s->mb_num - 1) + 1;
1367 put_bits(&s->pb, ff_mpeg4_get_video_packet_prefix_length(s), 0);
1368 put_bits(&s->pb, 1, 1);
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 */
1375 #define OFFSET(x) offsetof(MpegEncContext, x)
1376 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1377 static const AVOption options[] = {
1378 { "data_partitioning", "Use data partitioning.", OFFSET(data_partitioning), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1379 { "alternate_scan", "Enable alternate scantable.", OFFSET(alternate_scan), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1380 { "mpeg_quant", "Use MPEG quantizers instead of H.263",
1381 OFFSET(mpeg_quant), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, VE },
1382 FF_MPV_COMMON_BFRAME_OPTS
1384 #if FF_API_MPEGVIDEO_OPTS
1385 FF_MPV_DEPRECATED_A53_CC_OPT
1386 FF_MPV_DEPRECATED_MATRIX_OPT
1388 FF_MPEG4_PROFILE_OPTS
1392 static const AVClass mpeg4enc_class = {
1393 .class_name = "MPEG4 encoder",
1394 .item_name = av_default_item_name,
1396 .version = LIBAVUTIL_VERSION_INT,
1399 const AVCodec ff_mpeg4_encoder = {
1401 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
1402 .type = AVMEDIA_TYPE_VIDEO,
1403 .id = AV_CODEC_ID_MPEG4,
1404 .priv_data_size = sizeof(MpegEncContext),
1405 .init = encode_init,
1406 .encode2 = ff_mpv_encode_picture,
1407 .close = ff_mpv_encode_end,
1408 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
1409 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
1410 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1411 .priv_class = &mpeg4enc_class,