FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg4videoenc.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 625 788 79.3%
Functions: 23 25 92.0%
Branches: 302 436 69.3%

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