FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg4videoenc.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 611 772 79.1%
Functions: 22 24 91.7%
Branches: 302 434 69.6%

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