FFmpeg coverage


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