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