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