FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg4videoenc.c
Date: 2026-10-19 01:23:10
Exec Total Coverage
Lines: 620 729 85.0%
Functions: 25 25 100.0%
Branches: 286 392 73.0%

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 "mathops.h"
33 #include "mpeg4video.h"
34 #include "mpeg4videodata.h"
35 #include "mpeg4videodefs.h"
36 #include "mpeg4videoenc.h"
37 #include "mpegvideoenc.h"
38 #include "profiles.h"
39 #include "put_bits.h"
40 #include "version.h"
41
42 /**
43 * Minimal fcode that a motion vector component would need.
44 */
45 static uint8_t fcode_tab[MAX_MV*2+1];
46
47 /* The uni_DCtab_* tables below contain unified bits+length tables to encode DC
48 * differences in MPEG-4. Unified in the sense that the specification specifies
49 * this encoding in several steps. */
50 static uint8_t uni_DCtab_lum_len[512];
51 static uint8_t uni_DCtab_chrom_len[512];
52 static uint16_t uni_DCtab_lum_bits[512];
53 static uint16_t uni_DCtab_chrom_bits[512];
54
55 /* Unified encoding tables for run length encoding of coefficients.
56 * Unified in the sense that the specification specifies the encoding in several steps. */
57 static uint32_t uni_mpeg4_intra_rl_bits[64 * 64 * 2 * 2];
58 static uint8_t uni_mpeg4_intra_rl_len[64 * 64 * 2 * 2];
59 static uint32_t uni_mpeg4_inter_rl_bits[64 * 64 * 2 * 2];
60 static uint8_t uni_mpeg4_inter_rl_len[64 * 64 * 2 * 2];
61
62 //#define UNI_MPEG4_ENC_INDEX(last, run, level) ((last) * 128 + (run) * 256 + (level))
63 //#define UNI_MPEG4_ENC_INDEX(last, run, level) ((last) * 128 * 64 + (run) + (level) * 64)
64 #define UNI_MPEG4_ENC_INDEX(last, run, level) ((last) * 128 * 64 + (run) * 128 + (level))
65
66 /* MPEG-4
67 * inter
68 * max level: 24/6
69 * max run: 53/63
70 *
71 * intra
72 * max level: 53/16
73 * max run: 29/41
74 */
75
76 typedef struct Mpeg4EncContext {
77 MPVMainEncContext m;
78 /// number of bits to represent the fractional part of time
79 int time_increment_bits;
80 } Mpeg4EncContext;
81
82 2749 static inline Mpeg4EncContext *mainctx_to_mpeg4(MPVMainEncContext *m)
83 {
84 2749 return (Mpeg4EncContext*)m;
85 }
86
87 /**
88 * Return the number of bits that encoding the 8x8 block in block would need.
89 * @param[in] block_last_index last index in scantable order that refers to a non zero element in block.
90 */
91 585996 static inline int get_block_rate(MPVEncContext *const s, int16_t block[64],
92 int block_last_index, const uint8_t scantable[64])
93 {
94 585996 int last = 0;
95 int j;
96 585996 int rate = 0;
97
98
2/2
✓ Branch 0 taken 11782546 times.
✓ Branch 1 taken 585996 times.
12368542 for (j = 1; j <= block_last_index; j++) {
99 11782546 const int index = scantable[j];
100 11782546 int level = block[index];
101
2/2
✓ Branch 0 taken 5582079 times.
✓ Branch 1 taken 6200467 times.
11782546 if (level) {
102 5582079 level += 64;
103
2/2
✓ Branch 0 taken 5580489 times.
✓ Branch 1 taken 1590 times.
5582079 if ((level & (~127)) == 0) {
104
2/2
✓ Branch 0 taken 5142149 times.
✓ Branch 1 taken 438340 times.
5580489 if (j < block_last_index)
105 5142149 rate += s->intra_ac_vlc_length[UNI_AC_ENC_INDEX(j - last - 1, level)];
106 else
107 438340 rate += s->intra_ac_vlc_last_length[UNI_AC_ENC_INDEX(j - last - 1, level)];
108 } else
109 1590 rate += s->ac_esc_length;
110
111 5582079 last = j;
112 }
113 }
114
115 585996 return rate;
116 }
117
118 /**
119 * Restore the ac coefficients in block that have been changed by decide_ac_pred().
120 * This function also restores s->c.block_last_index.
121 * @param[in,out] block MB coefficients, these will be restored
122 * @param[in] dir ac prediction direction for each 8x8 block
123 * @param[out] st scantable for each 8x8 block
124 * @param[in] zigzag_last_index index referring to the last non zero coefficient in zigzag order
125 */
126 48833 static inline void restore_ac_coeffs(MPVEncContext *const s, int16_t block[6][64],
127 const int dir[6], const uint8_t *st[6],
128 const int zigzag_last_index[6])
129 {
130 int i, n;
131 48833 memcpy(s->c.block_last_index, zigzag_last_index, sizeof(int) * 6);
132
133
2/2
✓ Branch 0 taken 292998 times.
✓ Branch 1 taken 48833 times.
341831 for (n = 0; n < 6; n++) {
134 292998 int16_t *ac_val = &s->c.ac_val[0][0] + s->c.block_index[n] * 16;
135
136 292998 st[n] = s->c.intra_scantable.permutated;
137
2/2
✓ Branch 0 taken 80423 times.
✓ Branch 1 taken 212575 times.
292998 if (dir[n]) {
138 /* top prediction */
139
2/2
✓ Branch 0 taken 562961 times.
✓ Branch 1 taken 80423 times.
643384 for (i = 1; i < 8; i++)
140 562961 block[n][s->c.idsp.idct_permutation[i]] = ac_val[i + 8];
141 } else {
142 /* left prediction */
143
2/2
✓ Branch 0 taken 1488025 times.
✓ Branch 1 taken 212575 times.
1700600 for (i = 1; i < 8; i++)
144 1488025 block[n][s->c.idsp.idct_permutation[i << 3]] = ac_val[i];
145 }
146 }
147 48833 }
148
149 /**
150 * Predict the dc.
151 * @param n block index (0-3 are luma, 4-5 are chroma)
152 * @param dir_ptr pointer to an integer where the prediction direction will be stored
153 */
154 1339434 static int mpeg4_pred_dc(MpegEncContext *s, int n, int *dir_ptr)
155 {
156 1339434 const int16_t *const dc_val = s->dc_val + s->block_index[n];
157 1339434 const int wrap = s->block_wrap[n];
158
159 /* B C
160 * A X
161 */
162 1339434 const int a = dc_val[-1];
163 1339434 const int b = dc_val[-1 - wrap];
164 1339434 const int c = dc_val[-wrap];
165 int pred;
166
167 // There is no need for out-of-slice handling here, as all values are set
168 // appropriately when a new slice is opened.
169
2/2
✓ Branch 0 taken 468734 times.
✓ Branch 1 taken 870700 times.
1339434 if (abs(a - b) < abs(b - c)) {
170 468734 pred = c;
171 468734 *dir_ptr = 1; /* top */
172 } else {
173 870700 pred = a;
174 870700 *dir_ptr = 0; /* left */
175 }
176 1339434 return pred;
177 }
178
179 /**
180 * Return the optimal value (0 or 1) for the ac_pred element for the given MB in MPEG-4.
181 * This function will also update s->c.block_last_index and s->c.ac_val.
182 * @param[in,out] block MB coefficients, these will be updated if 1 is returned
183 * @param[in] dir ac prediction direction for each 8x8 block
184 * @param[out] st scantable for each 8x8 block
185 * @param[out] zigzag_last_index index referring to the last non zero coefficient in zigzag order
186 */
187 48833 static inline int decide_ac_pred(MPVEncContext *const s, int16_t block[6][64],
188 const int dir[6], const uint8_t *st[6],
189 int zigzag_last_index[6])
190 {
191 48833 int score = 0;
192 int i, n;
193 48833 const int8_t *const qscale_table = s->c.cur_pic.qscale_table;
194
195 48833 memcpy(zigzag_last_index, s->c.block_last_index, sizeof(int) * 6);
196
197
2/2
✓ Branch 0 taken 292998 times.
✓ Branch 1 taken 48833 times.
341831 for (n = 0; n < 6; n++) {
198 int16_t *ac_val, *ac_val1;
199
200 585996 score -= get_block_rate(s, block[n], s->c.block_last_index[n],
201 292998 s->c.intra_scantable.permutated);
202
203 292998 ac_val = &s->c.ac_val[0][0] + s->c.block_index[n] * 16;
204 292998 ac_val1 = ac_val;
205
2/2
✓ Branch 0 taken 80423 times.
✓ Branch 1 taken 212575 times.
292998 if (dir[n]) {
206 80423 const int xy = s->c.mb_x + s->c.mb_y * s->c.mb_stride - s->c.mb_stride;
207 /* top prediction */
208 80423 ac_val -= s->c.block_wrap[n] * 16;
209
3/8
✓ Branch 0 taken 62487 times.
✓ Branch 1 taken 17936 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 62487 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
80423 if (s->c.first_slice_line || s->c.qscale == qscale_table[xy] || n == 2 || n == 3) {
210 /* same qscale */
211
2/2
✓ Branch 0 taken 562961 times.
✓ Branch 1 taken 80423 times.
643384 for (i = 1; i < 8; i++) {
212 562961 const int level = block[n][s->c.idsp.idct_permutation[i]];
213 562961 block[n][s->c.idsp.idct_permutation[i]] = level - ac_val[i + 8];
214 562961 ac_val1[i] = block[n][s->c.idsp.idct_permutation[i << 3]];
215 562961 ac_val1[i + 8] = level;
216 }
217 } else {
218 /* different qscale, we must rescale */
219 ✗ for (i = 1; i < 8; i++) {
220 ✗ const int level = block[n][s->c.idsp.idct_permutation[i]];
221 ✗ block[n][s->c.idsp.idct_permutation[i]] = level - ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->c.qscale);
222 ✗ ac_val1[i] = block[n][s->c.idsp.idct_permutation[i << 3]];
223 ✗ ac_val1[i + 8] = level;
224 }
225 }
226 80423 st[n] = s->permutated_intra_h_scantable;
227 } else {
228 212575 const int xy = s->c.mb_x - 1 + s->c.mb_y * s->c.mb_stride;
229 /* left prediction */
230 212575 ac_val -= 16;
231
3/8
✓ Branch 0 taken 204228 times.
✓ Branch 1 taken 8347 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 204228 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
212575 if (s->c.mb_x == 0 || s->c.qscale == qscale_table[xy] || n == 1 || n == 3) {
232 /* same qscale */
233
2/2
✓ Branch 0 taken 1488025 times.
✓ Branch 1 taken 212575 times.
1700600 for (i = 1; i < 8; i++) {
234 1488025 const int level = block[n][s->c.idsp.idct_permutation[i << 3]];
235 1488025 block[n][s->c.idsp.idct_permutation[i << 3]] = level - ac_val[i];
236 1488025 ac_val1[i] = level;
237 1488025 ac_val1[i + 8] = block[n][s->c.idsp.idct_permutation[i]];
238 }
239 } else {
240 /* different qscale, we must rescale */
241 ✗ for (i = 1; i < 8; i++) {
242 ✗ const int level = block[n][s->c.idsp.idct_permutation[i << 3]];
243 ✗ block[n][s->c.idsp.idct_permutation[i << 3]] = level - ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->c.qscale);
244 ✗ ac_val1[i] = level;
245 ✗ ac_val1[i + 8] = block[n][s->c.idsp.idct_permutation[i]];
246 }
247 }
248 212575 st[n] = s->permutated_intra_v_scantable;
249 }
250
251
2/2
✓ Branch 0 taken 12582510 times.
✓ Branch 1 taken 68557 times.
12651067 for (i = 63; i > 0; i--) // FIXME optimize
252
2/2
✓ Branch 0 taken 224441 times.
✓ Branch 1 taken 12358069 times.
12582510 if (block[n][st[n][i]])
253 224441 break;
254 292998 s->c.block_last_index[n] = i;
255
256 292998 score += get_block_rate(s, block[n], s->c.block_last_index[n], st[n]);
257 }
258
259
2/2
✓ Branch 0 taken 7324 times.
✓ Branch 1 taken 41509 times.
48833 if (score < 0) {
260 7324 return 1;
261 } else {
262 41509 restore_ac_coeffs(s, block, dir, st, zigzag_last_index);
263 41509 return 0;
264 }
265 }
266
267 /**
268 * modify mb_type & qscale so that encoding is actually possible in MPEG-4
269 */
270 450 void ff_clean_mpeg4_qscales(MPVEncContext *const s)
271 {
272 450 ff_clean_h263_qscales(s);
273
274
2/2
✓ Branch 0 taken 217 times.
✓ Branch 1 taken 233 times.
450 if (s->c.pict_type == AV_PICTURE_TYPE_B) {
275 217 int8_t *const qscale_table = s->c.cur_pic.qscale_table;
276 217 int odd = 0;
277 /* ok, come on, this isn't funny anymore, there's more code for
278 * handling this MPEG-4 mess than for the actual adaptive quantization */
279
280
2/2
✓ Branch 0 taken 69291 times.
✓ Branch 1 taken 217 times.
69508 for (int i = 0; i < s->c.mb_num; i++) {
281 69291 int mb_xy = s->c.mb_index2xy[i];
282 69291 odd += qscale_table[mb_xy] & 1;
283 }
284
285
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 99 times.
217 if (2 * odd > s->c.mb_num)
286 118 odd = 1;
287 else
288 99 odd = 0;
289
290
2/2
✓ Branch 0 taken 69291 times.
✓ Branch 1 taken 217 times.
69508 for (int i = 0; i < s->c.mb_num; i++) {
291 69291 int mb_xy = s->c.mb_index2xy[i];
292
2/2
✓ Branch 0 taken 13207 times.
✓ Branch 1 taken 56084 times.
69291 if ((qscale_table[mb_xy] & 1) != odd)
293 13207 qscale_table[mb_xy]++;
294
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 69287 times.
69291 if (qscale_table[mb_xy] > 31)
295 4 qscale_table[mb_xy] = 31;
296 }
297
298
2/2
✓ Branch 0 taken 69074 times.
✓ Branch 1 taken 217 times.
69291 for (int i = 1; i < s->c.mb_num; i++) {
299 69074 int mb_xy = s->c.mb_index2xy[i];
300
2/2
✓ Branch 0 taken 16156 times.
✓ Branch 1 taken 52918 times.
69074 if (qscale_table[mb_xy] != qscale_table[s->c.mb_index2xy[i - 1]] &&
301
1/2
✓ Branch 0 taken 16156 times.
✗ Branch 1 not taken.
16156 (s->mb_type[mb_xy] & CANDIDATE_MB_TYPE_DIRECT)) {
302 16156 s->mb_type[mb_xy] |= CANDIDATE_MB_TYPE_BIDIR;
303 }
304 }
305 }
306 450 }
307
308 /**
309 * Encode the dc value.
310 * @param n block index (0-3 are luma, 4-5 are chroma)
311 */
312 1339434 static inline void mpeg4_encode_dc(PutBitContext *s, int level, int n)
313 {
314 /* DC will overflow if level is outside the [-255,255] range. */
315 1339434 level += 256;
316
2/2
✓ Branch 0 taken 892956 times.
✓ Branch 1 taken 446478 times.
1339434 if (n < 4) {
317 /* luminance */
318 892956 put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]);
319 } else {
320 /* chrominance */
321 446478 put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]);
322 }
323 1339434 }
324
325 /**
326 * Encode the AC coefficients of an 8x8 block.
327 */
328 5037146 static inline void mpeg4_encode_ac_coeffs(const int16_t block[64],
329 const int last_index, int i,
330 const uint8_t *const scan_table,
331 PutBitContext *const ac_pb,
332 const uint32_t *const bits_tab,
333 const uint8_t *const len_tab)
334 {
335 5037146 int last_non_zero = i - 1;
336
337 /* AC coefs */
338
2/2
✓ Branch 0 taken 133437573 times.
✓ Branch 1 taken 5037146 times.
138474719 for (; i < last_index; i++) {
339 133437573 int level = block[scan_table[i]];
340
2/2
✓ Branch 0 taken 43739415 times.
✓ Branch 1 taken 89698158 times.
133437573 if (level) {
341 43739415 int run = i - last_non_zero - 1;
342 43739415 level += 64;
343
2/2
✓ Branch 0 taken 43721040 times.
✓ Branch 1 taken 18375 times.
43739415 if ((level & (~127)) == 0) {
344 43721040 const int index = UNI_MPEG4_ENC_INDEX(0, run, level);
345 43721040 put_bits(ac_pb, len_tab[index], bits_tab[index]);
346 } else { // ESC3
347 18375 put_bits(ac_pb,
348 7 + 2 + 1 + 6 + 1 + 12 + 1,
349 18375 (3 << 23) + (3 << 21) + (0 << 20) + (run << 14) +
350 18375 (1 << 13) + (((level - 64) & 0xfff) << 1) + 1);
351 }
352 43739415 last_non_zero = i;
353 }
354 }
355 /* if (i <= last_index) */ {
356 5037146 int level = block[scan_table[i]];
357 5037146 int run = i - last_non_zero - 1;
358 5037146 level += 64;
359
2/2
✓ Branch 0 taken 5036798 times.
✓ Branch 1 taken 348 times.
5037146 if ((level & (~127)) == 0) {
360 5036798 const int index = UNI_MPEG4_ENC_INDEX(1, run, level);
361 5036798 put_bits(ac_pb, len_tab[index], bits_tab[index]);
362 } else { // ESC3
363 348 put_bits(ac_pb,
364 7 + 2 + 1 + 6 + 1 + 12 + 1,
365 348 (3 << 23) + (3 << 21) + (1 << 20) + (run << 14) +
366 348 (1 << 13) + (((level - 64) & 0xfff) << 1) + 1);
367 }
368 }
369 5037146 }
370
371 1579172 static void mpeg4_encode_blocks_inter(MPVEncContext *const s,
372 const int16_t block[6][64],
373 PutBitContext *ac_pb)
374 {
375 /* encode each block */
376
2/2
✓ Branch 0 taken 9475032 times.
✓ Branch 1 taken 1579172 times.
11054204 for (int n = 0; n < 6; ++n) {
377 9475032 const int last_index = s->c.block_last_index[n];
378
2/2
✓ Branch 0 taken 5476378 times.
✓ Branch 1 taken 3998654 times.
9475032 if (last_index < 0)
379 5476378 continue;
380
381 3998654 mpeg4_encode_ac_coeffs(block[n], last_index, 0,
382 3998654 s->c.intra_scantable.permutated, ac_pb,
383 uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
384 }
385 1579172 }
386
387 223239 static void mpeg4_encode_blocks_intra(MPVEncContext *const s,
388 const int16_t block[6][64],
389 const int intra_dc[6],
390 const uint8_t * const *scan_table,
391 PutBitContext *dc_pb,
392 PutBitContext *ac_pb)
393 {
394 /* encode each block */
395
2/2
✓ Branch 0 taken 1339434 times.
✓ Branch 1 taken 223239 times.
1562673 for (int n = 0; n < 6; ++n) {
396 1339434 mpeg4_encode_dc(dc_pb, intra_dc[n], n);
397
398 1339434 const int last_index = s->c.block_last_index[n];
399
2/2
✓ Branch 0 taken 300942 times.
✓ Branch 1 taken 1038492 times.
1339434 if (last_index <= 0)
400 300942 continue;
401
402 1038492 mpeg4_encode_ac_coeffs(block[n], last_index, 1,
403 1038492 scan_table[n], ac_pb,
404 uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
405 }
406 223239 }
407
408 729370 static inline int get_b_cbp(MPVEncContext *const s, int16_t block[6][64],
409 int motion_x, int motion_y, int mb_type)
410 {
411 729370 int cbp = 0, i;
412
413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 729370 times.
729370 if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
414 ✗ int score = 0;
415 ✗ const int lambda = s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
416
417 ✗ for (i = 0; i < 6; i++) {
418 ✗ if (s->coded_score[i] < 0) {
419 ✗ score += s->coded_score[i];
420 ✗ cbp |= 1 << (5 - i);
421 }
422 }
423
424 ✗ if (cbp) {
425 ✗ int zero_score = -6;
426 ✗ if ((motion_x | motion_y | s->dquant | mb_type) == 0)
427 ✗ zero_score -= 4; // 2 * MV + mb_type + cbp bit
428
429 ✗ zero_score *= lambda;
430 ✗ if (zero_score <= score)
431 ✗ cbp = 0;
432 }
433
434 ✗ for (i = 0; i < 6; i++) {
435 ✗ if (s->c.block_last_index[i] >= 0 && ((cbp >> (5 - i)) & 1) == 0) {
436 ✗ s->c.block_last_index[i] = -1;
437 ✗ s->c.bdsp.clear_block(s->block[i]);
438 }
439 }
440 } else {
441
2/2
✓ Branch 0 taken 4376220 times.
✓ Branch 1 taken 729370 times.
5105590 for (i = 0; i < 6; i++) {
442
2/2
✓ Branch 0 taken 1453577 times.
✓ Branch 1 taken 2922643 times.
4376220 if (s->c.block_last_index[i] >= 0)
443 1453577 cbp |= 1 << (5 - i);
444 }
445 }
446 729370 return cbp;
447 }
448
449 // FIXME this is duplicated to h263.c
450 static const int dquant_code[5] = { 1, 0, 9, 2, 3 };
451
452 1869190 static void mpeg4_encode_mb(MPVEncContext *const s, int16_t block[][64],
453 int motion_x, int motion_y)
454 {
455 int cbpc, cbpy, pred_x, pred_y;
456
2/2
✓ Branch 0 taken 414860 times.
✓ Branch 1 taken 1454330 times.
1869190 PutBitContext *const pb2 = s->data_partitioning ? &s->pb2 : &s->pb;
457
4/4
✓ Branch 0 taken 414860 times.
✓ Branch 1 taken 1454330 times.
✓ Branch 2 taken 261745 times.
✓ Branch 3 taken 153115 times.
1869190 PutBitContext *const tex_pb = s->data_partitioning && s->c.pict_type != AV_PICTURE_TYPE_B ? &s->tex_pb : &s->pb;
458
4/4
✓ Branch 0 taken 414860 times.
✓ Branch 1 taken 1454330 times.
✓ Branch 2 taken 396905 times.
✓ Branch 3 taken 17955 times.
1869190 PutBitContext *const dc_pb = s->data_partitioning && s->c.pict_type != AV_PICTURE_TYPE_I ? &s->pb2 : &s->pb;
459
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1869190 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1869190 const int interleaved_stats = (s->c.avctx->flags & AV_CODEC_FLAG_PASS1) && !s->data_partitioning;
460
461
2/2
✓ Branch 0 taken 1645951 times.
✓ Branch 1 taken 223239 times.
1869190 if (!s->c.mb_intra) {
462 int i, cbp;
463
464
2/2
✓ Branch 0 taken 729477 times.
✓ Branch 1 taken 916474 times.
1645951 if (s->c.pict_type == AV_PICTURE_TYPE_B) {
465 /* convert from mv_dir to type */
466 static const int mb_type_table[8] = { -1, 3, 2, 1, -1, -1, -1, 0 };
467 729477 int mb_type = mb_type_table[s->c.mv_dir];
468
469
2/2
✓ Branch 0 taken 34851 times.
✓ Branch 1 taken 694626 times.
729477 if (s->c.mb_x == 0) {
470
2/2
✓ Branch 0 taken 69702 times.
✓ Branch 1 taken 34851 times.
104553 for (i = 0; i < 2; i++)
471 69702 s->c.last_mv[i][0][0] =
472 69702 s->c.last_mv[i][0][1] =
473 69702 s->c.last_mv[i][1][0] =
474 69702 s->c.last_mv[i][1][1] = 0;
475 }
476
477 av_assert2(s->dquant >= -2 && s->dquant <= 2);
478 av_assert2((s->dquant & 1) == 0);
479 av_assert2(mb_type >= 0);
480
481 /* nothing to do if this MB was skipped in the next P-frame */
482
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 729370 times.
729477 if (s->c.next_pic.mbskip_table[s->c.mb_y * s->c.mb_stride + s->c.mb_x]) { // FIXME avoid DCT & ...
483 107 s->c.mv[0][0][0] =
484 107 s->c.mv[0][0][1] =
485 107 s->c.mv[1][0][0] =
486 107 s->c.mv[1][0][1] = 0;
487 107 s->c.mv_dir = MV_DIR_FORWARD; // doesn't matter
488 107 s->c.qscale -= s->dquant;
489 // s->c.mb_skipped = 1;
490
491 66779 return;
492 }
493
494 729370 cbp = get_b_cbp(s, block, motion_x, motion_y, mb_type);
495
496
2/2
✓ Branch 0 taken 43811 times.
✓ Branch 1 taken 685559 times.
729370 if ((cbp | motion_x | motion_y | mb_type) == 0) {
497 /* direct MB with MV={0,0} */
498 av_assert2(s->dquant == 0);
499
500 43811 put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */
501
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43811 times.
43811 if (interleaved_stats) {
503 ✗ s->misc_bits++;
504 ✗ s->last_bits++;
505 }
506 43811 return;
507 }
508
509 685559 put_bits(&s->pb, 1, 0); /* mb coded modb1=0 */
510 685559 put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ // FIXME merge
511 685559 put_bits(&s->pb, mb_type + 1, 1); // this table is so simple that we don't need it :)
512
2/2
✓ Branch 0 taken 448891 times.
✓ Branch 1 taken 236668 times.
685559 if (cbp)
513 448891 put_bits(&s->pb, 6, cbp);
514
515
4/4
✓ Branch 0 taken 448891 times.
✓ Branch 1 taken 236668 times.
✓ Branch 2 taken 314353 times.
✓ Branch 3 taken 134538 times.
685559 if (cbp && mb_type) {
516
2/2
✓ Branch 0 taken 44148 times.
✓ Branch 1 taken 270205 times.
314353 if (s->dquant)
517 44148 put_bits(&s->pb, 2, (s->dquant >> 2) + 3);
518 else
519 270205 put_bits(&s->pb, 1, 0);
520 } else
521 371206 s->c.qscale -= s->dquant;
522
523
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 685559 times.
685559 if (!s->c.progressive_sequence) {
524 ✗ if (cbp)
525 ✗ put_bits(&s->pb, 1, s->c.interlaced_dct);
526 ✗ if (mb_type) // not direct mode
527 ✗ put_bits(&s->pb, 1, s->c.mv_type == MV_TYPE_FIELD);
528 }
529
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 685559 times.
685559 if (interleaved_stats)
531 ✗ s->misc_bits += get_bits_diff(s);
532
533
2/2
✓ Branch 0 taken 158411 times.
✓ Branch 1 taken 527148 times.
685559 if (!mb_type) {
534 av_assert2(s->c.mv_dir & MV_DIRECT);
535 158411 ff_h263_encode_motion_vector(s, motion_x, motion_y, 1);
536 } else {
537 av_assert2(mb_type > 0 && mb_type < 4);
538
1/2
✓ Branch 0 taken 527148 times.
✗ Branch 1 not taken.
527148 if (s->c.mv_type != MV_TYPE_FIELD) {
539
2/2
✓ Branch 0 taken 357960 times.
✓ Branch 1 taken 169188 times.
527148 if (s->c.mv_dir & MV_DIR_FORWARD) {
540 357960 ff_h263_encode_motion_vector(s,
541 357960 s->c.mv[0][0][0] - s->c.last_mv[0][0][0],
542 357960 s->c.mv[0][0][1] - s->c.last_mv[0][0][1],
543 s->f_code);
544 357960 s->c.last_mv[0][0][0] =
545 357960 s->c.last_mv[0][1][0] = s->c.mv[0][0][0];
546 357960 s->c.last_mv[0][0][1] =
547 357960 s->c.last_mv[0][1][1] = s->c.mv[0][0][1];
548 }
549
2/2
✓ Branch 0 taken 360596 times.
✓ Branch 1 taken 166552 times.
527148 if (s->c.mv_dir & MV_DIR_BACKWARD) {
550 360596 ff_h263_encode_motion_vector(s,
551 360596 s->c.mv[1][0][0] - s->c.last_mv[1][0][0],
552 360596 s->c.mv[1][0][1] - s->c.last_mv[1][0][1],
553 s->b_code);
554 360596 s->c.last_mv[1][0][0] =
555 360596 s->c.last_mv[1][1][0] = s->c.mv[1][0][0];
556 360596 s->c.last_mv[1][0][1] =
557 360596 s->c.last_mv[1][1][1] = s->c.mv[1][0][1];
558 }
559 } else {
560 ✗ if (s->c.mv_dir & MV_DIR_FORWARD) {
561 ✗ put_bits(&s->pb, 1, s->c.field_select[0][0]);
562 ✗ put_bits(&s->pb, 1, s->c.field_select[0][1]);
563 }
564 ✗ if (s->c.mv_dir & MV_DIR_BACKWARD) {
565 ✗ put_bits(&s->pb, 1, s->c.field_select[1][0]);
566 ✗ put_bits(&s->pb, 1, s->c.field_select[1][1]);
567 }
568 ✗ if (s->c.mv_dir & MV_DIR_FORWARD) {
569 ✗ for (i = 0; i < 2; i++) {
570 ✗ ff_h263_encode_motion_vector(s,
571 ✗ s->c.mv[0][i][0] - s->c.last_mv[0][i][0],
572 ✗ s->c.mv[0][i][1] - s->c.last_mv[0][i][1] / 2,
573 s->f_code);
574 ✗ s->c.last_mv[0][i][0] = s->c.mv[0][i][0];
575 ✗ s->c.last_mv[0][i][1] = s->c.mv[0][i][1] * 2;
576 }
577 }
578 ✗ if (s->c.mv_dir & MV_DIR_BACKWARD) {
579 ✗ for (i = 0; i < 2; i++) {
580 ✗ ff_h263_encode_motion_vector(s,
581 ✗ s->c.mv[1][i][0] - s->c.last_mv[1][i][0],
582 ✗ s->c.mv[1][i][1] - s->c.last_mv[1][i][1] / 2,
583 s->b_code);
584 ✗ s->c.last_mv[1][i][0] = s->c.mv[1][i][0];
585 ✗ s->c.last_mv[1][i][1] = s->c.mv[1][i][1] * 2;
586 }
587 }
588 }
589 }
590
591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 685559 times.
685559 if (interleaved_stats)
592 ✗ s->mv_bits += get_bits_diff(s);
593
594 685559 mpeg4_encode_blocks_inter(s, block, &s->pb);
595
596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 685559 times.
685559 if (interleaved_stats)
597 ✗ s->p_tex_bits += get_bits_diff(s);
598 } else { /* s->c.pict_type == AV_PICTURE_TYPE_B */
599 916474 cbp = get_p_cbp(s, block, motion_x, motion_y);
600
601
2/2
✓ Branch 0 taken 61703 times.
✓ Branch 1 taken 854771 times.
916474 if ((cbp | motion_x | motion_y | s->dquant) == 0 &&
602
2/2
✓ Branch 0 taken 23110 times.
✓ Branch 1 taken 38593 times.
61703 s->c.mv_type == MV_TYPE_16X16) {
603 23110 const MPVMainEncContext *const m = slice_to_mainenc(s);
604 /* Check if the B-frames can skip it too, as we must skip it
605 * if we skip here why didn't they just compress
606 * the skip-mb bits instead of reusing them ?! */
607
2/2
✓ Branch 0 taken 1007 times.
✓ Branch 1 taken 22103 times.
23110 if (m->max_b_frames > 0) {
608 int x, y, offset;
609 const uint8_t *p_pic;
610
611 1007 x = s->c.mb_x * 16;
612 1007 y = s->c.mb_y * 16;
613
614 1007 offset = x + y * s->c.linesize;
615 1007 p_pic = s->new_pic->data[0] + offset;
616
617 1007 s->c.mb_skipped = 1;
618
2/2
✓ Branch 0 taken 1106 times.
✓ Branch 1 taken 64 times.
1170 for (int i = 0; i < m->max_b_frames; i++) {
619 const uint8_t *b_pic;
620 int diff;
621 1106 const MPVPicture *pic = m->reordered_input_picture[i + 1];
622
623
3/4
✓ Branch 0 taken 412 times.
✓ Branch 1 taken 694 times.
✓ Branch 2 taken 412 times.
✗ Branch 3 not taken.
1106 if (!pic || pic->f->pict_type != AV_PICTURE_TYPE_B)
624 break;
625
626 412 b_pic = pic->f->data[0] + offset;
627
628
3/4
✓ Branch 0 taken 411 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 411 times.
412 if (x + 16 > s->c.width || y + 16 > s->c.height) {
629 int x1, y1;
630 1 int xe = FFMIN(16, s->c.width - x);
631 1 int ye = FFMIN(16, s->c.height - y);
632 1 diff = 0;
633
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (y1 = 0; y1 < ye; y1++) {
634
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (x1 = 0; x1 < xe; x1++) {
635 4 diff += FFABS(p_pic[x1 + y1 * s->c.linesize] - b_pic[x1 + y1 * s->c.linesize]);
636 }
637 }
638 1 diff = diff * 256 / (xe * ye);
639 } else {
640 411 diff = s->sad_cmp[0](NULL, p_pic, b_pic, s->c.linesize, 16);
641 }
642
2/2
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 163 times.
412 if (diff > s->c.qscale * 70) { // FIXME check that 70 is optimal
643 249 s->c.mb_skipped = 0;
644 249 break;
645 }
646 }
647 } else
648 22103 s->c.mb_skipped = 1;
649
650
2/2
✓ Branch 0 taken 22861 times.
✓ Branch 1 taken 249 times.
23110 if (s->c.mb_skipped == 1) {
651 /* skip macroblock */
652 22861 put_bits(&s->pb, 1, 1);
653
654
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22861 times.
22861 if (interleaved_stats) {
655 ✗ s->misc_bits++;
656 ✗ s->last_bits++;
657 }
658
659 22861 return;
660 }
661 }
662
663 893613 put_bits(&s->pb, 1, 0); /* mb coded */
664 893613 cbpc = cbp & 3;
665 893613 cbpy = cbp >> 2;
666 893613 cbpy ^= 0xf;
667
2/2
✓ Branch 0 taken 660145 times.
✓ Branch 1 taken 233468 times.
893613 if (s->c.mv_type == MV_TYPE_16X16) {
668
2/2
✓ Branch 0 taken 83250 times.
✓ Branch 1 taken 576895 times.
660145 if (s->dquant)
669 83250 cbpc += 8;
670 660145 put_bits(&s->pb,
671 660145 ff_h263_inter_MCBPC_bits[cbpc],
672 660145 ff_h263_inter_MCBPC_code[cbpc]);
673
674 660145 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
675
2/2
✓ Branch 0 taken 83250 times.
✓ Branch 1 taken 576895 times.
660145 if (s->dquant)
676 83250 put_bits(pb2, 2, dquant_code[s->dquant + 2]);
677
678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 660145 times.
660145 if (!s->c.progressive_sequence) {
679 ✗ if (cbp)
680 ✗ put_bits(pb2, 1, s->c.interlaced_dct);
681 ✗ put_bits(pb2, 1, 0);
682 }
683
684
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 660145 times.
660145 if (interleaved_stats)
685 ✗ s->misc_bits += get_bits_diff(s);
686
687 /* motion vectors: 16x16 mode */
688 660145 ff_h263_pred_motion(&s->c, 0, 0, &pred_x, &pred_y);
689
690 660145 ff_h263_encode_motion_vector(s,
691 motion_x - pred_x,
692 motion_y - pred_y,
693 s->f_code);
694
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 233468 times.
233468 } else if (s->c.mv_type == MV_TYPE_FIELD) {
695 ✗ if (s->dquant)
696 ✗ cbpc += 8;
697 ✗ put_bits(&s->pb,
698 ✗ ff_h263_inter_MCBPC_bits[cbpc],
699 ✗ ff_h263_inter_MCBPC_code[cbpc]);
700
701 ✗ put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
702 ✗ if (s->dquant)
703 ✗ put_bits(pb2, 2, dquant_code[s->dquant + 2]);
704
705 av_assert2(!s->c.progressive_sequence);
706 ✗ if (cbp)
707 ✗ put_bits(pb2, 1, s->c.interlaced_dct);
708 ✗ put_bits(pb2, 1, 1);
709
710 ✗ if (interleaved_stats)
711 ✗ s->misc_bits += get_bits_diff(s);
712
713 /* motion vectors: 16x8 interlaced mode */
714 ✗ ff_h263_pred_motion(&s->c, 0, 0, &pred_x, &pred_y);
715 ✗ pred_y /= 2;
716
717 ✗ put_bits(&s->pb, 1, s->c.field_select[0][0]);
718 ✗ put_bits(&s->pb, 1, s->c.field_select[0][1]);
719
720 ✗ ff_h263_encode_motion_vector(s,
721 ✗ s->c.mv[0][0][0] - pred_x,
722 ✗ s->c.mv[0][0][1] - pred_y,
723 s->f_code);
724 ✗ ff_h263_encode_motion_vector(s,
725 ✗ s->c.mv[0][1][0] - pred_x,
726 ✗ s->c.mv[0][1][1] - pred_y,
727 s->f_code);
728 } else {
729 av_assert2(s->c.mv_type == MV_TYPE_8X8);
730 233468 put_bits(&s->pb,
731 233468 ff_h263_inter_MCBPC_bits[cbpc + 16],
732 233468 ff_h263_inter_MCBPC_code[cbpc + 16]);
733 233468 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
734
735
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 233468 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
233468 if (!s->c.progressive_sequence && cbp)
736 ✗ put_bits(pb2, 1, s->c.interlaced_dct);
737
738
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 233468 times.
233468 if (interleaved_stats)
739 ✗ s->misc_bits += get_bits_diff(s);
740
741
2/2
✓ Branch 0 taken 933872 times.
✓ Branch 1 taken 233468 times.
1167340 for (i = 0; i < 4; i++) {
742 /* motion vectors: 8x8 mode*/
743 933872 ff_h263_pred_motion(&s->c, i, 0, &pred_x, &pred_y);
744
745 933872 ff_h263_encode_motion_vector(s,
746 933872 s->c.cur_pic.motion_val[0][s->c.block_index[i]][0] - pred_x,
747 933872 s->c.cur_pic.motion_val[0][s->c.block_index[i]][1] - pred_y,
748 s->f_code);
749 }
750 }
751
752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 893613 times.
893613 if (interleaved_stats)
753 ✗ s->mv_bits += get_bits_diff(s);
754
755 893613 mpeg4_encode_blocks_inter(s, block, tex_pb);
756
757
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 893613 times.
893613 if (interleaved_stats)
758 ✗ s->p_tex_bits += get_bits_diff(s);
759 }
760 } else {
761 int cbp;
762 int dc_diff[6]; // dc values with the dc prediction subtracted
763 int dir[6]; // prediction direction
764 int zigzag_last_index[6];
765 const uint8_t *scan_table[6];
766 int i;
767
768
2/2
✓ Branch 0 taken 1339434 times.
✓ Branch 1 taken 223239 times.
1562673 for (int i = 0; i < 6; i++) {
769 1339434 int pred = mpeg4_pred_dc(&s->c, i, &dir[i]);
770
2/2
✓ Branch 0 taken 892956 times.
✓ Branch 1 taken 446478 times.
1339434 int scale = i < 4 ? s->c.y_dc_scale : s->c.c_dc_scale;
771
772 1339434 pred = FASTDIV((pred + (scale >> 1)), scale);
773 1339434 dc_diff[i] = block[i][0] - pred;
774 1339434 s->c.dc_val[s->c.block_index[i]] = av_clip_uintp2(block[i][0] * scale, 11);
775 }
776
777
2/2
✓ Branch 0 taken 48833 times.
✓ Branch 1 taken 174406 times.
223239 if (s->c.avctx->flags & AV_CODEC_FLAG_AC_PRED) {
778 48833 s->c.ac_pred = decide_ac_pred(s, block, dir, scan_table, zigzag_last_index);
779 } else {
780
2/2
✓ Branch 0 taken 1046436 times.
✓ Branch 1 taken 174406 times.
1220842 for (i = 0; i < 6; i++)
781 1046436 scan_table[i] = s->c.intra_scantable.permutated;
782 }
783
784 /* compute cbp */
785 223239 cbp = 0;
786
2/2
✓ Branch 0 taken 1339434 times.
✓ Branch 1 taken 223239 times.
1562673 for (i = 0; i < 6; i++)
787
2/2
✓ Branch 0 taken 1038492 times.
✓ Branch 1 taken 300942 times.
1339434 if (s->c.block_last_index[i] >= 1)
788 1038492 cbp |= 1 << (5 - i);
789
790 223239 cbpc = cbp & 3;
791
2/2
✓ Branch 0 taken 117920 times.
✓ Branch 1 taken 105319 times.
223239 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
792
2/2
✓ Branch 0 taken 28249 times.
✓ Branch 1 taken 89671 times.
117920 if (s->dquant)
793 28249 cbpc += 4;
794 117920 put_bits(&s->pb,
795 117920 ff_h263_intra_MCBPC_bits[cbpc],
796 117920 ff_h263_intra_MCBPC_code[cbpc]);
797 } else {
798
2/2
✓ Branch 0 taken 11984 times.
✓ Branch 1 taken 93335 times.
105319 if (s->dquant)
799 11984 cbpc += 8;
800 105319 put_bits(&s->pb, 1, 0); /* mb coded */
801 105319 put_bits(&s->pb,
802 105319 ff_h263_inter_MCBPC_bits[cbpc + 4],
803 105319 ff_h263_inter_MCBPC_code[cbpc + 4]);
804 }
805 223239 put_bits(pb2, 1, s->c.ac_pred);
806 223239 cbpy = cbp >> 2;
807 223239 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
808
2/2
✓ Branch 0 taken 40233 times.
✓ Branch 1 taken 183006 times.
223239 if (s->dquant)
809 40233 put_bits(dc_pb, 2, dquant_code[s->dquant + 2]);
810
811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 223239 times.
223239 if (!s->c.progressive_sequence)
812 ✗ put_bits(dc_pb, 1, s->c.interlaced_dct);
813
814
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 223239 times.
223239 if (interleaved_stats)
815 ✗ s->misc_bits += get_bits_diff(s);
816
817 223239 mpeg4_encode_blocks_intra(s, block, dc_diff, scan_table, dc_pb, tex_pb);
818
819
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 223239 times.
223239 if (interleaved_stats)
820 ✗ s->i_tex_bits += get_bits_diff(s);
821 223239 s->i_count++;
822
823 /* restore ac coeffs & last_index stuff
824 * if we messed them up with the prediction */
825
2/2
✓ Branch 0 taken 7324 times.
✓ Branch 1 taken 215915 times.
223239 if (s->c.ac_pred)
826 7324 restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index);
827 }
828 }
829
830 /**
831 * add MPEG-4 stuffing bits (01...1)
832 */
833 6833 void ff_mpeg4_stuffing(PutBitContext *pbc)
834 {
835 6833 int length = 8 - (put_bits_count(pbc) & 7);
836
837 6833 put_bits(pbc, length, (1 << (length - 1)) - 1);
838 6833 }
839
840 /* must be called before writing the header */
841 2749 void ff_set_mpeg4_time(MPVEncContext *const s)
842 {
843
2/2
✓ Branch 0 taken 602 times.
✓ Branch 1 taken 2147 times.
2749 if (s->c.pict_type == AV_PICTURE_TYPE_B) {
844 602 ff_mpeg4_init_direct_mv(&s->c);
845 } else {
846 2147 s->c.last_time_base = s->c.time_base;
847
2/2
✓ Branch 0 taken 2074 times.
✓ Branch 1 taken 73 times.
2147 s->c.time_base = FFUDIV(s->c.time, s->c.avctx->time_base.den);
848 }
849 2749 }
850
851 290 static void mpeg4_encode_gop_header(MPVMainEncContext *const m)
852 {
853 290 MPVEncContext *const s = &m->s;
854 int64_t hours, minutes, seconds;
855 int64_t time;
856
857 290 put_bits32(&s->pb, GOP_STARTCODE);
858
859 290 time = s->c.cur_pic.ptr->f->pts;
860
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 211 times.
290 if (m->reordered_input_picture[1])
861 79 time = FFMIN(time, m->reordered_input_picture[1]->f->pts);
862 290 seconds = av_rescale_rnd(time, s->c.avctx->time_base.num, s->c.avctx->time_base.den, AV_ROUND_DOWN);
863 290 s->c.last_time_base = seconds;
864
865
4/4
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 89 times.
✓ Branch 2 taken 201 times.
✓ Branch 3 taken 89 times.
290 minutes = FFUDIV(seconds, 60); seconds = FFUMOD(seconds, 60);
866
2/4
✓ Branch 0 taken 290 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 290 times.
✗ Branch 3 not taken.
290 hours = FFUDIV(minutes, 60); minutes = FFUMOD(minutes, 60);
867
1/2
✓ Branch 0 taken 290 times.
✗ Branch 1 not taken.
290 hours = FFUMOD(hours , 24);
868
869 290 put_bits(&s->pb, 5, hours);
870 290 put_bits(&s->pb, 6, minutes);
871 290 put_bits(&s->pb, 1, 1);
872 290 put_bits(&s->pb, 6, seconds);
873
874 290 put_bits(&s->pb, 1, !!(s->c.avctx->flags & AV_CODEC_FLAG_CLOSED_GOP));
875 290 put_bits(&s->pb, 1, 0); // broken link == NO
876
877 290 ff_mpeg4_stuffing(&s->pb);
878 290 }
879
880 236 static void mpeg4_encode_visual_object_header(MPVMainEncContext *const m)
881 {
882 236 MPVEncContext *const s = &m->s;
883 int profile_and_level_indication;
884 int vo_ver_id;
885
886
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 if (s->c.avctx->profile != AV_PROFILE_UNKNOWN) {
887 ✗ profile_and_level_indication = s->c.avctx->profile << 4;
888
3/4
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 107 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 129 times.
236 } else if (m->max_b_frames || s->c.quarter_sample) {
889 107 profile_and_level_indication = 0xF0; // adv simple
890 } else {
891 129 profile_and_level_indication = 0x00; // simple
892 }
893
894
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 if (s->c.avctx->level != AV_LEVEL_UNKNOWN)
895 ✗ profile_and_level_indication |= s->c.avctx->level;
896 else
897 236 profile_and_level_indication |= 1; // level 1
898
899
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 129 times.
236 if (profile_and_level_indication >> 4 == 0xF)
900 107 vo_ver_id = 5;
901 else
902 129 vo_ver_id = 1;
903
904 // FIXME levels
905
906 236 put_bits32(&s->pb, VOS_STARTCODE);
907
908 236 put_bits(&s->pb, 8, profile_and_level_indication);
909
910 236 put_bits32(&s->pb, VISUAL_OBJ_STARTCODE);
911
912 236 put_bits(&s->pb, 1, 1);
913 236 put_bits(&s->pb, 4, vo_ver_id);
914 236 put_bits(&s->pb, 3, 1); // priority
915
916 236 put_bits(&s->pb, 4, 1); // visual obj type== video obj
917
918 236 put_bits(&s->pb, 1, 0); // video signal type == no clue // FIXME
919
920 236 ff_mpeg4_stuffing(&s->pb);
921 236 }
922
923 236 static void mpeg4_encode_vol_header(Mpeg4EncContext *const m4,
924 int vo_number,
925 int vol_number)
926 {
927 236 MPVEncContext *const s = &m4->m.s;
928 int vo_ver_id, vo_type, aspect_ratio_info;
929
930
3/4
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 107 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 129 times.
236 if (m4->m.max_b_frames || s->c.quarter_sample) {
931 107 vo_ver_id = 5;
932 107 vo_type = ADV_SIMPLE_VO_TYPE;
933 } else {
934 129 vo_ver_id = 1;
935 129 vo_type = SIMPLE_VO_TYPE;
936 }
937
938 236 put_bits32(&s->pb, 0x100 + vo_number); /* video obj */
939 236 put_bits32(&s->pb, 0x120 + vol_number); /* video obj layer */
940
941 236 put_bits(&s->pb, 1, 0); /* random access vol */
942 236 put_bits(&s->pb, 8, vo_type); /* video obj type indication */
943 236 put_bits(&s->pb, 1, 1); /* is obj layer id= yes */
944 236 put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
945 236 put_bits(&s->pb, 3, 1); /* is obj layer priority */
946
947 236 aspect_ratio_info = ff_h263_aspect_to_info(s->c.avctx->sample_aspect_ratio);
948
949 236 put_bits(&s->pb, 4, aspect_ratio_info); /* aspect ratio info */
950
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 if (aspect_ratio_info == FF_ASPECT_EXTENDED) {
951 ✗ av_reduce(&s->c.avctx->sample_aspect_ratio.num, &s->c.avctx->sample_aspect_ratio.den,
952 ✗ s->c.avctx->sample_aspect_ratio.num, s->c.avctx->sample_aspect_ratio.den, 255);
953 ✗ put_bits(&s->pb, 8, s->c.avctx->sample_aspect_ratio.num);
954 ✗ put_bits(&s->pb, 8, s->c.avctx->sample_aspect_ratio.den);
955 }
956
957 236 put_bits(&s->pb, 1, 1); /* vol control parameters= yes */
958 236 put_bits(&s->pb, 2, 1); /* chroma format YUV 420/YV12 */
959 236 put_bits(&s->pb, 1, s->c.low_delay);
960 236 put_bits(&s->pb, 1, 0); /* vbv parameters= no */
961
962 236 put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */
963 236 put_bits(&s->pb, 1, 1); /* marker bit */
964
965 236 put_bits(&s->pb, 16, s->c.avctx->time_base.den);
966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 if (m4->time_increment_bits < 1)
967 ✗ m4->time_increment_bits = 1;
968 236 put_bits(&s->pb, 1, 1); /* marker bit */
969 236 put_bits(&s->pb, 1, 0); /* fixed vop rate=no */
970 236 put_bits(&s->pb, 1, 1); /* marker bit */
971 236 put_bits(&s->pb, 13, s->c.width); /* vol width */
972 236 put_bits(&s->pb, 1, 1); /* marker bit */
973 236 put_bits(&s->pb, 13, s->c.height); /* vol height */
974 236 put_bits(&s->pb, 1, 1); /* marker bit */
975 236 put_bits(&s->pb, 1, s->c.progressive_sequence ? 0 : 1);
976 236 put_bits(&s->pb, 1, 1); /* obmc disable */
977
2/2
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 107 times.
236 if (vo_ver_id == 1)
978 129 put_bits(&s->pb, 1, 0); /* sprite enable */
979 else
980 107 put_bits(&s->pb, 2, 0); /* sprite enable */
981
982 236 put_bits(&s->pb, 1, 0); /* not 8 bit == false */
983 236 put_bits(&s->pb, 1, s->mpeg_quant); /* quant type = (0 = H.263 style) */
984
985
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 216 times.
236 if (s->mpeg_quant) {
986 20 ff_write_quant_matrix(&s->pb, s->c.avctx->intra_matrix);
987 20 ff_write_quant_matrix(&s->pb, s->c.avctx->inter_matrix);
988 }
989
990
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 129 times.
236 if (vo_ver_id != 1)
991 107 put_bits(&s->pb, 1, s->c.quarter_sample);
992 236 put_bits(&s->pb, 1, 1); /* complexity estimation disable */
993 236 put_bits(&s->pb, 1, s->rtp_mode ? 0 : 1); /* resync marker disable */
994 236 put_bits(&s->pb, 1, s->data_partitioning);
995
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 176 times.
236 if (s->data_partitioning)
996 60 put_bits(&s->pb, 1, 0); /* no rvlc */
997
998
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 129 times.
236 if (vo_ver_id != 1) {
999 107 put_bits(&s->pb, 1, 0); /* newpred */
1000 107 put_bits(&s->pb, 1, 0); /* reduced res vop */
1001 }
1002 236 put_bits(&s->pb, 1, 0); /* scalability */
1003
1004 236 ff_mpeg4_stuffing(&s->pb);
1005
1006 /* user data */
1007
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 232 times.
236 if (!(s->c.avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
1008 4 put_bits32(&s->pb, USER_DATA_STARTCODE);
1009 4 ff_put_string(&s->pb, LIBAVCODEC_IDENT, 0);
1010 }
1011 236 }
1012
1013 /* write MPEG-4 VOP header */
1014 2749 static int mpeg4_encode_picture_header(MPVMainEncContext *const m)
1015 {
1016 2749 Mpeg4EncContext *const m4 = mainctx_to_mpeg4(m);
1017 2749 MPVEncContext *const s = &m->s;
1018 uint64_t time_incr;
1019 int64_t time_div, time_mod;
1020
1021 2749 put_bits_assume_flushed(&s->pb);
1022
1023
2/2
✓ Branch 0 taken 290 times.
✓ Branch 1 taken 2459 times.
2749 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
1024
2/2
✓ Branch 0 taken 206 times.
✓ Branch 1 taken 84 times.
290 if (!(s->c.avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) {
1025
1/2
✓ Branch 0 taken 206 times.
✗ Branch 1 not taken.
206 if (s->c.avctx->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT) // HACK, the reference sw is buggy
1026 206 mpeg4_encode_visual_object_header(m);
1027
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 206 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
206 if (s->c.avctx->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT || s->picture_number == 0) // HACK, the reference sw is buggy
1028 206 mpeg4_encode_vol_header(m4, 0, 0);
1029 }
1030 290 mpeg4_encode_gop_header(m);
1031 }
1032
1033
4/4
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 2149 times.
✓ Branch 2 taken 472 times.
✓ Branch 3 taken 128 times.
2749 s->partitioned_frame = s->data_partitioning && s->c.pict_type != AV_PICTURE_TYPE_B;
1034
1035 2749 put_bits32(&s->pb, VOP_STARTCODE); /* vop header */
1036 2749 put_bits(&s->pb, 2, s->c.pict_type - 1); /* pict type: I = 0 , P = 1 */
1037
1038
2/2
✓ Branch 0 taken 2676 times.
✓ Branch 1 taken 73 times.
2749 time_div = FFUDIV(s->c.time, s->c.avctx->time_base.den);
1039
2/2
✓ Branch 0 taken 2676 times.
✓ Branch 1 taken 73 times.
2749 time_mod = FFUMOD(s->c.time, s->c.avctx->time_base.den);
1040 2749 time_incr = time_div - s->c.last_time_base;
1041
1042 // This limits the frame duration to max 1 day
1043
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2749 times.
2749 if (time_incr > 3600*24) {
1044 ✗ av_log(s->c.avctx, AV_LOG_ERROR, "time_incr %"PRIu64" too large\n", time_incr);
1045 ✗ return AVERROR(EINVAL);
1046 }
1047
2/2
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 2749 times.
2983 while (time_incr--)
1048 234 put_bits(&s->pb, 1, 1);
1049
1050 2749 put_bits(&s->pb, 1, 0);
1051
1052 2749 put_bits(&s->pb, 1, 1); /* marker */
1053 2749 put_bits(&s->pb, m4->time_increment_bits, time_mod); /* time increment */
1054 2749 put_bits(&s->pb, 1, 1); /* marker */
1055 2749 put_bits(&s->pb, 1, 1); /* vop coded */
1056
2/2
✓ Branch 0 taken 1857 times.
✓ Branch 1 taken 892 times.
2749 if (s->c.pict_type == AV_PICTURE_TYPE_P) {
1057 1857 put_bits(&s->pb, 1, s->c.no_rounding); /* rounding type */
1058 }
1059 2749 put_bits(&s->pb, 3, 0); /* intra dc VLC threshold */
1060
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2749 times.
2749 if (!s->c.progressive_sequence) {
1061 ✗ put_bits(&s->pb, 1, !!(s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST));
1062 ✗ put_bits(&s->pb, 1, s->c.alternate_scan);
1063 }
1064 // FIXME sprite stuff
1065
1066 2749 put_bits(&s->pb, 5, s->c.qscale);
1067
1068
2/2
✓ Branch 0 taken 2459 times.
✓ Branch 1 taken 290 times.
2749 if (s->c.pict_type != AV_PICTURE_TYPE_I)
1069 2459 put_bits(&s->pb, 3, s->f_code); /* fcode_for */
1070
2/2
✓ Branch 0 taken 602 times.
✓ Branch 1 taken 2147 times.
2749 if (s->c.pict_type == AV_PICTURE_TYPE_B)
1071 602 put_bits(&s->pb, 3, s->b_code); /* fcode_back */
1072
1073 2749 return 0;
1074 }
1075
1076 72 static av_cold void init_uni_dc_tab(void)
1077 {
1078 int level, uni_code, uni_len;
1079
1080
2/2
✓ Branch 0 taken 36864 times.
✓ Branch 1 taken 72 times.
36936 for (level = -256; level < 256; level++) {
1081 int size, v, l;
1082 /* find number of bits */
1083 36864 size = 0;
1084 36864 v = abs(level);
1085
2/2
✓ Branch 0 taken 258840 times.
✓ Branch 1 taken 36864 times.
295704 while (v) {
1086 258840 v >>= 1;
1087 258840 size++;
1088 }
1089
1090
2/2
✓ Branch 0 taken 18432 times.
✓ Branch 1 taken 18432 times.
36864 if (level < 0)
1091 18432 l = (-level) ^ ((1 << size) - 1);
1092 else
1093 18432 l = level;
1094
1095 /* luminance */
1096 36864 uni_code = ff_mpeg4_DCtab_lum[size][0];
1097 36864 uni_len = ff_mpeg4_DCtab_lum[size][1];
1098
1099
2/2
✓ Branch 0 taken 36792 times.
✓ Branch 1 taken 72 times.
36864 if (size > 0) {
1100 36792 uni_code <<= size;
1101 36792 uni_code |= l;
1102 36792 uni_len += size;
1103
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 36720 times.
36792 if (size > 8) {
1104 72 uni_code <<= 1;
1105 72 uni_code |= 1;
1106 72 uni_len++;
1107 }
1108 }
1109 36864 uni_DCtab_lum_bits[level + 256] = uni_code;
1110 36864 uni_DCtab_lum_len[level + 256] = uni_len;
1111
1112 /* chrominance */
1113 36864 uni_code = ff_mpeg4_DCtab_chrom[size][0];
1114 36864 uni_len = ff_mpeg4_DCtab_chrom[size][1];
1115
1116
2/2
✓ Branch 0 taken 36792 times.
✓ Branch 1 taken 72 times.
36864 if (size > 0) {
1117 36792 uni_code <<= size;
1118 36792 uni_code |= l;
1119 36792 uni_len += size;
1120
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 36720 times.
36792 if (size > 8) {
1121 72 uni_code <<= 1;
1122 72 uni_code |= 1;
1123 72 uni_len++;
1124 }
1125 }
1126 36864 uni_DCtab_chrom_bits[level + 256] = uni_code;
1127 36864 uni_DCtab_chrom_len[level + 256] = uni_len;
1128 }
1129 72 }
1130
1131 144 static av_cold void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab,
1132 uint8_t *len_tab)
1133 {
1134 // Type 3 escape method. The escape code is the same for both VLCs
1135 // (0x3, seven bits), so it is hardcoded.
1136 144 memset(len_tab, 30, 2 * 2 * 64 * 64);
1137 144 len_tab += 64;
1138 144 bits_tab += 64;
1139
2/2
✓ Branch 0 taken 9216 times.
✓ Branch 1 taken 144 times.
9360 for (int run = 0; run < 64; ++run) {
1140 9216 for (int level = 1;; ++level) {
1141 // Escape code type 3 not last run (6 bits) marker marker
1142 589824 unsigned code = (3 << 23) | (3 << 21) | (0 << 20) | (run << 14) | (1 << 13) | 1;
1143 // first the negative levels
1144 589824 bits_tab[UNI_MPEG4_ENC_INDEX(0, run, -level)] = code | (-level & 0xfff) << 1;
1145 589824 bits_tab[UNI_MPEG4_ENC_INDEX(1, run, -level)] =
1146 589824 bits_tab[UNI_MPEG4_ENC_INDEX(0, run, -level)] | (1 << 20) /* last */;
1147
1148
2/2
✓ Branch 0 taken 9216 times.
✓ Branch 1 taken 580608 times.
589824 if (level == 64) // positive levels have a range of 1..63
1149 9216 break;
1150 580608 bits_tab[UNI_MPEG4_ENC_INDEX(0, run, level)] = code | level << 1;
1151 580608 bits_tab[UNI_MPEG4_ENC_INDEX(1, run, level)] =
1152 580608 bits_tab[UNI_MPEG4_ENC_INDEX(0, run, level)] | (1 << 20) /* last */;
1153 }
1154 // Is this needed at all?
1155 9216 len_tab[UNI_MPEG4_ENC_INDEX(0, run, 0)] =
1156 9216 len_tab[UNI_MPEG4_ENC_INDEX(1, run, 0)] = 0;
1157 }
1158
1159 144 uint8_t max_run[2][32] = { 0 };
1160
1161 #define VLC_NUM_CODES 102 // excluding the escape
1162 av_assert2(rl->n == VLC_NUM_CODES);
1163
2/2
✓ Branch 0 taken 14688 times.
✓ Branch 1 taken 144 times.
14832 for (int i = VLC_NUM_CODES - 1, max_level, cur_run = 0; i >= 0; --i) {
1164 14688 int run = rl->table_run[i], level = rl->table_level[i];
1165 14688 int last = i >= rl->last;
1166 14688 unsigned code = rl->table_vlc[i][0] << 1;
1167 14688 int len = rl->table_vlc[i][1] + 1;
1168
1169 14688 bits_tab[UNI_MPEG4_ENC_INDEX(last, run, level)] = code;
1170 14688 len_tab [UNI_MPEG4_ENC_INDEX(last, run, level)] = len;
1171 14688 bits_tab[UNI_MPEG4_ENC_INDEX(last, run, -level)] = code | 1;
1172 14688 len_tab [UNI_MPEG4_ENC_INDEX(last, run, -level)] = len;
1173
1174
2/2
✓ Branch 0 taken 3600 times.
✓ Branch 1 taken 11088 times.
14688 if (!max_run[last][level])
1175 3600 max_run[last][level] = run + 1;
1176 av_assert2(run + 1 <= max_run[last][level]);
1177
1178 14688 int run3 = run + max_run[last][level];
1179 14688 int len3 = len + 7 + 2;
1180
1181
4/4
✓ Branch 0 taken 13392 times.
✓ Branch 1 taken 1296 times.
✓ Branch 2 taken 8928 times.
✓ Branch 3 taken 4464 times.
14688 if (run3 < 64 && len3 < len_tab[UNI_MPEG4_ENC_INDEX(last, run3, level)]) {
1182 8928 unsigned code3 = code | (0x3 << 2 | 0x2) << len;
1183 8928 bits_tab[UNI_MPEG4_ENC_INDEX(last, run3, level)] = code3;
1184 8928 len_tab [UNI_MPEG4_ENC_INDEX(last, run3, level)] = len3;
1185 8928 bits_tab[UNI_MPEG4_ENC_INDEX(last, run3, -level)] = code3 | 1;
1186 8928 len_tab [UNI_MPEG4_ENC_INDEX(last, run3, -level)] = len3;
1187 }
1188 // table_run and table_level are ordered so that all the entries
1189 // with the same last and run are consecutive and level is ascending
1190 // among these entries. By traversing downwards we therefore automatically
1191 // encounter max_level of a given run first, needed for escape method 1.
1192
2/2
✓ Branch 0 taken 7488 times.
✓ Branch 1 taken 7200 times.
14688 if (run != cur_run) {
1193 7488 max_level = level;
1194 7488 cur_run = run;
1195 } else
1196 av_assert2(max_level > level);
1197
1198 14688 code |= 0x3 << (len + 1);
1199 14688 len += 7 + 1;
1200 14688 level += max_level;
1201 av_assert2(len_tab [UNI_MPEG4_ENC_INDEX(last, run, level)] >= len);
1202 14688 bits_tab[UNI_MPEG4_ENC_INDEX(last, run, level)] = code;
1203 14688 len_tab [UNI_MPEG4_ENC_INDEX(last, run, level)] = len;
1204 14688 bits_tab[UNI_MPEG4_ENC_INDEX(last, run, -level)] = code | 1;
1205 14688 len_tab [UNI_MPEG4_ENC_INDEX(last, run, -level)] = len;
1206 }
1207 144 }
1208
1209 72 static av_cold void mpeg4_encode_init_static(void)
1210 {
1211 72 init_uni_dc_tab();
1212
1213 72 init_uni_mpeg4_rl_tab(&ff_mpeg4_rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
1214 72 init_uni_mpeg4_rl_tab(&ff_h263_rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
1215
1216
2/2
✓ Branch 0 taken 504 times.
✓ Branch 1 taken 72 times.
576 for (int f_code = MAX_FCODE; f_code > 0; f_code--) {
1217
2/2
✓ Branch 0 taken 585216 times.
✓ Branch 1 taken 504 times.
585720 for (int mv = -(16 << f_code); mv < (16 << f_code); mv++)
1218 585216 fcode_tab[mv + MAX_MV] = f_code;
1219 }
1220 72 }
1221
1222 73 static av_cold int encode_init(AVCodecContext *avctx)
1223 {
1224 static AVOnce init_static_once = AV_ONCE_INIT;
1225 73 Mpeg4EncContext *const m4 = avctx->priv_data;
1226 73 MPVMainEncContext *const m = &m4->m;
1227 73 MPVEncContext *const s = &m->s;
1228 int ret;
1229
1230
2/4
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 73 times.
73 if (avctx->width >= (1<<13) || avctx->height >= (1<<13)) {
1231 ✗ av_log(avctx, AV_LOG_ERROR, "dimensions too large for MPEG-4\n");
1232 ✗ return AVERROR(EINVAL);
1233 }
1234
1235 73 m->encode_picture_header = mpeg4_encode_picture_header;
1236 73 s->encode_mb = mpeg4_encode_mb;
1237
1238 73 m->fcode_tab = fcode_tab + MAX_MV;
1239
1240 73 s->min_qcoeff = -2048;
1241 73 s->max_qcoeff = 2047;
1242 73 s->intra_ac_vlc_length = uni_mpeg4_intra_rl_len;
1243 73 s->intra_ac_vlc_last_length = uni_mpeg4_intra_rl_len + 128 * 64;
1244 73 s->inter_ac_vlc_length = uni_mpeg4_inter_rl_len;
1245 73 s->inter_ac_vlc_last_length = uni_mpeg4_inter_rl_len + 128 * 64;
1246 73 s->luma_dc_vlc_length = uni_DCtab_lum_len;
1247 73 s->ac_esc_length = 7 + 2 + 1 + 6 + 1 + 12 + 1;
1248 73 s->c.y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
1249 73 s->c.c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
1250
1251 73 ff_qpeldsp_init(&s->c.qdsp);
1252
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 73 times.
73 if ((ret = ff_mpv_encode_init(avctx)) < 0)
1253 ✗ return ret;
1254
1255 73 ff_thread_once(&init_static_once, mpeg4_encode_init_static);
1256
1257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (avctx->time_base.den > (1 << 16) - 1) {
1258 ✗ av_log(avctx, AV_LOG_ERROR,
1259 "timebase %d/%d not supported by MPEG 4 standard, "
1260 "the maximum admitted value for the timebase denominator "
1261 "is %d\n", avctx->time_base.num, avctx->time_base.den,
1262 (1 << 16) - 1);
1263 ✗ return AVERROR(EINVAL);
1264 }
1265
1266 73 m4->time_increment_bits = av_log2(avctx->time_base.den - 1) + 1;
1267
1268
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 43 times.
73 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
1269 30 avctx->extradata = av_malloc(1024);
1270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (!avctx->extradata)
1271 ✗ return AVERROR(ENOMEM);
1272 30 init_put_bits(&s->pb, avctx->extradata, 1024);
1273
1274 30 mpeg4_encode_visual_object_header(m);
1275 30 mpeg4_encode_vol_header(m4, 0, 0);
1276
1277 // ff_mpeg4_stuffing(&s->pb); ?
1278 30 flush_put_bits(&s->pb);
1279 30 avctx->extradata_size = put_bytes_output(&s->pb);
1280 }
1281 73 return 0;
1282 }
1283
1284 1756 void ff_mpeg4_init_partitions(MPVEncContext *const s)
1285 {
1286 1756 uint8_t *start = put_bits_ptr(&s->pb);
1287 1756 uint8_t *end = s->pb.buf_end;
1288 1756 int size = end - start;
1289 1756 int pb_size = (((intptr_t)start + size / 3) & (~3)) - (intptr_t)start;
1290 1756 int tex_size = (size - 2 * pb_size) & (~3);
1291
1292 1756 set_put_bits_buffer_size(&s->pb, pb_size);
1293 1756 init_put_bits(&s->tex_pb, start + pb_size, tex_size);
1294 1756 init_put_bits(&s->pb2, start + pb_size + tex_size, pb_size);
1295 1756 }
1296
1297 1756 void ff_mpeg4_merge_partitions(MPVEncContext *const s)
1298 {
1299 1756 const int pb2_len = put_bits_count(&s->pb2);
1300 1756 const int tex_pb_len = put_bits_count(&s->tex_pb);
1301 1756 const int bits = put_bits_count(&s->pb);
1302
1303
2/2
✓ Branch 0 taken 511 times.
✓ Branch 1 taken 1245 times.
1756 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
1304 511 put_bits(&s->pb, 19, DC_MARKER);
1305 511 s->misc_bits += 19 + pb2_len + bits - s->last_bits;
1306 511 s->i_tex_bits += tex_pb_len;
1307 } else {
1308 1245 put_bits(&s->pb, 17, MOTION_MARKER);
1309 1245 s->misc_bits += 17 + pb2_len;
1310 1245 s->mv_bits += bits - s->last_bits;
1311 1245 s->p_tex_bits += tex_pb_len;
1312 }
1313
1314 1756 flush_put_bits(&s->pb2);
1315 1756 flush_put_bits(&s->tex_pb);
1316
1317 1756 set_put_bits_buffer_size(&s->pb, s->pb2.buf_end - s->pb.buf);
1318 1756 ff_copy_bits(&s->pb, s->pb2.buf, pb2_len);
1319 1756 ff_copy_bits(&s->pb, s->tex_pb.buf, tex_pb_len);
1320 1756 s->last_bits = put_bits_count(&s->pb);
1321 1756 }
1322
1323 3322 void ff_mpeg4_encode_video_packet_header(MPVEncContext *const s)
1324 {
1325 3322 int mb_num_bits = av_log2(s->c.mb_num - 1) + 1;
1326
1327 3322 put_bits(&s->pb, ff_mpeg4_get_video_packet_prefix_length(s->c.pict_type, s->f_code, s->b_code), 0);
1328 3322 put_bits(&s->pb, 1, 1);
1329
1330 3322 put_bits(&s->pb, mb_num_bits, s->c.mb_x + s->c.mb_y * s->c.mb_width);
1331 3322 put_bits(&s->pb, 5 /* quant_precision */, s->c.qscale);
1332 3322 put_bits(&s->pb, 1, 0); /* no HEC */
1333 3322 }
1334
1335 #define OFFSET(x) offsetof(MPVEncContext, x)
1336 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1337 static const AVOption options[] = {
1338 { "data_partitioning", "Use data partitioning.", FF_MPV_OFFSET(data_partitioning), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1339 { "alternate_scan", "Enable alternate scantable.", OFFSET(c.alternate_scan), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1340 { "mpeg_quant", "Use MPEG quantizers instead of H.263",
1341 OFFSET(mpeg_quant), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, VE },
1342 FF_MPV_COMMON_BFRAME_OPTS
1343 FF_MPV_COMMON_OPTS
1344 FF_MPV_COMMON_MOTION_EST_OPTS
1345 FF_MPEG4_PROFILE_OPTS
1346 { NULL },
1347 };
1348
1349 static const AVClass mpeg4enc_class = {
1350 .class_name = "MPEG4 encoder",
1351 .item_name = av_default_item_name,
1352 .option = options,
1353 .version = LIBAVUTIL_VERSION_INT,
1354 };
1355
1356 const FFCodec ff_mpeg4_encoder = {
1357 .p.name = "mpeg4",
1358 CODEC_LONG_NAME("MPEG-4 part 2"),
1359 .p.type = AVMEDIA_TYPE_VIDEO,
1360 .p.id = AV_CODEC_ID_MPEG4,
1361 .priv_data_size = sizeof(Mpeg4EncContext),
1362 .init = encode_init,
1363 FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
1364 .close = ff_mpv_encode_end,
1365 CODEC_PIXFMTS(AV_PIX_FMT_YUV420P),
1366 CODEC_CHROMA_LOCS(AVCHROMA_LOC_LEFT),
1367 .color_ranges = AVCOL_RANGE_MPEG,
1368 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1369 AV_CODEC_CAP_SLICE_THREADS |
1370 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1371 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1372 .p.priv_class = &mpeg4enc_class,
1373 };
1374