FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg4videoenc.c
Date: 2022-01-28 07:56:06
Exec Total Coverage
Lines: 620 788 78.7%
Branches: 301 438 68.7%

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