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 | 2665 | static inline Mpeg4EncContext *mainctx_to_mpeg4(MPVMainEncContext *m) | |
83 | { | ||
84 | 2665 | 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 | 585288 | 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 | 585288 | int last = 0; | |
95 | int j; | ||
96 | 585288 | int rate = 0; | |
97 | |||
98 |
2/2✓ Branch 0 taken 11962580 times.
✓ Branch 1 taken 585288 times.
|
12547868 | for (j = 1; j <= block_last_index; j++) { |
99 | 11962580 | const int index = scantable[j]; | |
100 | 11962580 | int level = block[index]; | |
101 |
2/2✓ Branch 0 taken 5655201 times.
✓ Branch 1 taken 6307379 times.
|
11962580 | if (level) { |
102 | 5655201 | level += 64; | |
103 |
2/2✓ Branch 0 taken 5653593 times.
✓ Branch 1 taken 1608 times.
|
5655201 | if ((level & (~127)) == 0) { |
104 |
2/2✓ Branch 0 taken 5215921 times.
✓ Branch 1 taken 437672 times.
|
5653593 | if (j < block_last_index) |
105 | 5215921 | rate += s->intra_ac_vlc_length[UNI_AC_ENC_INDEX(j - last - 1, level)]; | |
106 | else | ||
107 | 437672 | rate += s->intra_ac_vlc_last_length[UNI_AC_ENC_INDEX(j - last - 1, level)]; | |
108 | } else | ||
109 | 1608 | rate += s->ac_esc_length; | |
110 | |||
111 | 5655201 | last = j; | |
112 | } | ||
113 | } | ||
114 | |||
115 | 585288 | 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 | 48774 | 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 | 48774 | memcpy(s->c.block_last_index, zigzag_last_index, sizeof(int) * 6); | |
132 | |||
133 |
2/2✓ Branch 0 taken 292644 times.
✓ Branch 1 taken 48774 times.
|
341418 | for (n = 0; n < 6; n++) { |
134 | 292644 | int16_t *ac_val = &s->c.ac_val[0][0] + s->c.block_index[n] * 16; | |
135 | |||
136 | 292644 | st[n] = s->c.intra_scantable.permutated; | |
137 |
2/2✓ Branch 0 taken 80145 times.
✓ Branch 1 taken 212499 times.
|
292644 | if (dir[n]) { |
138 | /* top prediction */ | ||
139 |
2/2✓ Branch 0 taken 561015 times.
✓ Branch 1 taken 80145 times.
|
641160 | for (i = 1; i < 8; i++) |
140 | 561015 | block[n][s->c.idsp.idct_permutation[i]] = ac_val[i + 8]; | |
141 | } else { | ||
142 | /* left prediction */ | ||
143 |
2/2✓ Branch 0 taken 1487493 times.
✓ Branch 1 taken 212499 times.
|
1699992 | for (i = 1; i < 8; i++) |
144 | 1487493 | block[n][s->c.idsp.idct_permutation[i << 3]] = ac_val[i]; | |
145 | } | ||
146 | } | ||
147 | 48774 | } | |
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 | 1291776 | static int mpeg4_pred_dc(MpegEncContext *s, int n, int *dir_ptr) | |
155 | { | ||
156 | 1291776 | const int16_t *const dc_val = s->dc_val + s->block_index[n]; | |
157 | 1291776 | const int wrap = s->block_wrap[n]; | |
158 | |||
159 | /* B C | ||
160 | * A X | ||
161 | */ | ||
162 | 1291776 | const int a = dc_val[-1]; | |
163 | 1291776 | const int b = dc_val[-1 - wrap]; | |
164 | 1291776 | 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 452403 times.
✓ Branch 1 taken 839373 times.
|
1291776 | if (abs(a - b) < abs(b - c)) { |
170 | 452403 | pred = c; | |
171 | 452403 | *dir_ptr = 1; /* top */ | |
172 | } else { | ||
173 | 839373 | pred = a; | |
174 | 839373 | *dir_ptr = 0; /* left */ | |
175 | } | ||
176 | 1291776 | 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 | 48774 | 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 | 48774 | int score = 0; | |
192 | int i, n; | ||
193 | 48774 | const int8_t *const qscale_table = s->c.cur_pic.qscale_table; | |
194 | |||
195 | 48774 | memcpy(zigzag_last_index, s->c.block_last_index, sizeof(int) * 6); | |
196 | |||
197 |
2/2✓ Branch 0 taken 292644 times.
✓ Branch 1 taken 48774 times.
|
341418 | for (n = 0; n < 6; n++) { |
198 | int16_t *ac_val, *ac_val1; | ||
199 | |||
200 | 585288 | score -= get_block_rate(s, block[n], s->c.block_last_index[n], | |
201 | 292644 | s->c.intra_scantable.permutated); | |
202 | |||
203 | 292644 | ac_val = &s->c.ac_val[0][0] + s->c.block_index[n] * 16; | |
204 | 292644 | ac_val1 = ac_val; | |
205 |
2/2✓ Branch 0 taken 80145 times.
✓ Branch 1 taken 212499 times.
|
292644 | if (dir[n]) { |
206 | 80145 | const int xy = s->c.mb_x + s->c.mb_y * s->c.mb_stride - s->c.mb_stride; | |
207 | /* top prediction */ | ||
208 | 80145 | ac_val -= s->c.block_wrap[n] * 16; | |
209 |
3/8✓ Branch 0 taken 62260 times.
✓ Branch 1 taken 17885 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 62260 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
80145 | 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 561015 times.
✓ Branch 1 taken 80145 times.
|
641160 | for (i = 1; i < 8; i++) { |
212 | 561015 | const int level = block[n][s->c.idsp.idct_permutation[i]]; | |
213 | 561015 | block[n][s->c.idsp.idct_permutation[i]] = level - ac_val[i + 8]; | |
214 | 561015 | ac_val1[i] = block[n][s->c.idsp.idct_permutation[i << 3]]; | |
215 | 561015 | 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 | 80145 | st[n] = s->c.permutated_intra_h_scantable; | |
227 | } else { | ||
228 | 212499 | const int xy = s->c.mb_x - 1 + s->c.mb_y * s->c.mb_stride; | |
229 | /* left prediction */ | ||
230 | 212499 | ac_val -= 16; | |
231 |
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) { |
232 | /* same qscale */ | ||
233 |
2/2✓ Branch 0 taken 1487493 times.
✓ Branch 1 taken 212499 times.
|
1699992 | for (i = 1; i < 8; i++) { |
234 | 1487493 | const int level = block[n][s->c.idsp.idct_permutation[i << 3]]; | |
235 | 1487493 | block[n][s->c.idsp.idct_permutation[i << 3]] = level - ac_val[i]; | |
236 | 1487493 | ac_val1[i] = level; | |
237 | 1487493 | 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 | 212499 | st[n] = s->c.permutated_intra_v_scantable; | |
249 | } | ||
250 | |||
251 |
2/2✓ Branch 0 taken 12470313 times.
✓ Branch 1 taken 68527 times.
|
12538840 | for (i = 63; i > 0; i--) // FIXME optimize |
252 |
2/2✓ Branch 0 taken 224117 times.
✓ Branch 1 taken 12246196 times.
|
12470313 | if (block[n][st[n][i]]) |
253 | 224117 | break; | |
254 | 292644 | s->c.block_last_index[n] = i; | |
255 | |||
256 | 292644 | score += get_block_rate(s, block[n], s->c.block_last_index[n], st[n]); | |
257 | } | ||
258 | |||
259 |
2/2✓ Branch 0 taken 7332 times.
✓ Branch 1 taken 41442 times.
|
48774 | if (score < 0) { |
260 | 7332 | return 1; | |
261 | } else { | ||
262 | 41442 | restore_ac_coeffs(s, block, dir, st, zigzag_last_index); | |
263 | 41442 | return 0; | |
264 | } | ||
265 | } | ||
266 | |||
267 | /** | ||
268 | * modify mb_type & qscale so that encoding is actually possible in MPEG-4 | ||
269 | */ | ||
270 | 400 | void ff_clean_mpeg4_qscales(MPVEncContext *const s) | |
271 | { | ||
272 | 400 | ff_clean_h263_qscales(s); | |
273 | |||
274 |
2/2✓ Branch 0 taken 203 times.
✓ Branch 1 taken 197 times.
|
400 | if (s->c.pict_type == AV_PICTURE_TYPE_B) { |
275 | 203 | int8_t *const qscale_table = s->c.cur_pic.qscale_table; | |
276 | 203 | 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 65295 times.
✓ Branch 1 taken 203 times.
|
65498 | for (int i = 0; i < s->c.mb_num; i++) { |
281 | 65295 | int mb_xy = s->c.mb_index2xy[i]; | |
282 | 65295 | odd += qscale_table[mb_xy] & 1; | |
283 | } | ||
284 | |||
285 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 91 times.
|
203 | if (2 * odd > s->c.mb_num) |
286 | 112 | odd = 1; | |
287 | else | ||
288 | 91 | odd = 0; | |
289 | |||
290 |
2/2✓ Branch 0 taken 65295 times.
✓ Branch 1 taken 203 times.
|
65498 | for (int i = 0; i < s->c.mb_num; i++) { |
291 | 65295 | int mb_xy = s->c.mb_index2xy[i]; | |
292 |
2/2✓ Branch 0 taken 12015 times.
✓ Branch 1 taken 53280 times.
|
65295 | if ((qscale_table[mb_xy] & 1) != odd) |
293 | 12015 | qscale_table[mb_xy]++; | |
294 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65295 times.
|
65295 | if (qscale_table[mb_xy] > 31) |
295 | ✗ | qscale_table[mb_xy] = 31; | |
296 | } | ||
297 | |||
298 |
2/2✓ Branch 0 taken 65092 times.
✓ Branch 1 taken 203 times.
|
65295 | for (int i = 1; i < s->c.mb_num; i++) { |
299 | 65092 | int mb_xy = s->c.mb_index2xy[i]; | |
300 |
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]] && |
301 |
1/2✓ Branch 0 taken 14465 times.
✗ Branch 1 not taken.
|
14465 | (s->mb_type[mb_xy] & CANDIDATE_MB_TYPE_DIRECT)) { |
302 | 14465 | s->mb_type[mb_xy] |= CANDIDATE_MB_TYPE_BIDIR; | |
303 | } | ||
304 | } | ||
305 | } | ||
306 | 400 | } | |
307 | |||
308 | /** | ||
309 | * Encode the dc value. | ||
310 | * @param n block index (0-3 are luma, 4-5 are chroma) | ||
311 | */ | ||
312 | 1291776 | 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 | 1291776 | level += 256; | |
316 |
2/2✓ Branch 0 taken 861184 times.
✓ Branch 1 taken 430592 times.
|
1291776 | if (n < 4) { |
317 | /* luminance */ | ||
318 | 861184 | put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]); | |
319 | } else { | ||
320 | /* chrominance */ | ||
321 | 430592 | put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]); | |
322 | } | ||
323 | 1291776 | } | |
324 | |||
325 | /** | ||
326 | * Encode the AC coefficients of an 8x8 block. | ||
327 | */ | ||
328 | 4836617 | 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 | 4836617 | int last_non_zero = i - 1; | |
336 | |||
337 | /* AC coefs */ | ||
338 |
2/2✓ Branch 0 taken 131260469 times.
✓ Branch 1 taken 4836617 times.
|
136097086 | for (; i < last_index; i++) { |
339 | 131260469 | int level = block[scan_table[i]]; | |
340 |
2/2✓ Branch 0 taken 42506746 times.
✓ Branch 1 taken 88753723 times.
|
131260469 | if (level) { |
341 | 42506746 | int run = i - last_non_zero - 1; | |
342 | 42506746 | level += 64; | |
343 |
2/2✓ Branch 0 taken 42488792 times.
✓ Branch 1 taken 17954 times.
|
42506746 | if ((level & (~127)) == 0) { |
344 | 42488792 | const int index = UNI_MPEG4_ENC_INDEX(0, run, level); | |
345 | 42488792 | put_bits(ac_pb, len_tab[index], bits_tab[index]); | |
346 | } else { // ESC3 | ||
347 | 17954 | put_bits(ac_pb, | |
348 | 7 + 2 + 1 + 6 + 1 + 12 + 1, | ||
349 | 17954 | (3 << 23) + (3 << 21) + (0 << 20) + (run << 14) + | |
350 | 17954 | (1 << 13) + (((level - 64) & 0xfff) << 1) + 1); | |
351 | } | ||
352 | 42506746 | last_non_zero = i; | |
353 | } | ||
354 | } | ||
355 | /* if (i <= last_index) */ { | ||
356 | 4836617 | int level = block[scan_table[i]]; | |
357 | 4836617 | int run = i - last_non_zero - 1; | |
358 | 4836617 | level += 64; | |
359 |
2/2✓ Branch 0 taken 4836216 times.
✓ Branch 1 taken 401 times.
|
4836617 | if ((level & (~127)) == 0) { |
360 | 4836216 | const int index = UNI_MPEG4_ENC_INDEX(1, run, level); | |
361 | 4836216 | put_bits(ac_pb, len_tab[index], bits_tab[index]); | |
362 | } else { // ESC3 | ||
363 | 401 | put_bits(ac_pb, | |
364 | 7 + 2 + 1 + 6 + 1 + 12 + 1, | ||
365 | 401 | (3 << 23) + (3 << 21) + (1 << 20) + (run << 14) + | |
366 | 401 | (1 << 13) + (((level - 64) & 0xfff) << 1) + 1); | |
367 | } | ||
368 | } | ||
369 | 4836617 | } | |
370 | |||
371 | 1518363 | 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 9110178 times.
✓ Branch 1 taken 1518363 times.
|
10628541 | for (int n = 0; n < 6; ++n) { |
377 | 9110178 | const int last_index = s->c.block_last_index[n]; | |
378 |
2/2✓ Branch 0 taken 5274315 times.
✓ Branch 1 taken 3835863 times.
|
9110178 | if (last_index < 0) |
379 | 5274315 | continue; | |
380 | |||
381 | 3835863 | mpeg4_encode_ac_coeffs(block[n], last_index, 0, | |
382 | 3835863 | s->c.intra_scantable.permutated, ac_pb, | |
383 | uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len); | ||
384 | } | ||
385 | 1518363 | } | |
386 | |||
387 | 215296 | 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 1291776 times.
✓ Branch 1 taken 215296 times.
|
1507072 | for (int n = 0; n < 6; ++n) { |
396 | 1291776 | mpeg4_encode_dc(dc_pb, intra_dc[n], n); | |
397 | |||
398 | 1291776 | const int last_index = s->c.block_last_index[n]; | |
399 |
2/2✓ Branch 0 taken 291022 times.
✓ Branch 1 taken 1000754 times.
|
1291776 | if (last_index <= 0) |
400 | 291022 | continue; | |
401 | |||
402 | 1000754 | mpeg4_encode_ac_coeffs(block[n], last_index, 1, | |
403 | 1000754 | scan_table[n], ac_pb, | |
404 | uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len); | ||
405 | } | ||
406 | 215296 | } | |
407 | |||
408 | 709928 | 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 | 709928 | int cbp = 0, i; | |
412 | |||
413 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 709928 times.
|
709928 | 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 4259568 times.
✓ Branch 1 taken 709928 times.
|
4969496 | for (i = 0; i < 6; i++) { |
442 |
2/2✓ Branch 0 taken 1423197 times.
✓ Branch 1 taken 2836371 times.
|
4259568 | if (s->c.block_last_index[i] >= 0) |
443 | 1423197 | cbp |= 1 << (5 - i); | |
444 | } | ||
445 | } | ||
446 | 709928 | 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 | 1799573 | 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 414859 times.
✓ Branch 1 taken 1384714 times.
|
1799573 | PutBitContext *const pb2 = s->data_partitioning ? &s->pb2 : &s->pb; |
457 |
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->data_partitioning && s->c.pict_type != AV_PICTURE_TYPE_B ? &s->tex_pb : &s->pb; |
458 |
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->data_partitioning && s->c.pict_type != AV_PICTURE_TYPE_I ? &s->pb2 : &s->pb; |
459 |
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->data_partitioning; |
460 | |||
461 |
2/2✓ Branch 0 taken 1584277 times.
✓ Branch 1 taken 215296 times.
|
1799573 | if (!s->c.mb_intra) { |
462 | int i, cbp; | ||
463 | |||
464 |
2/2✓ Branch 0 taken 710018 times.
✓ Branch 1 taken 874259 times.
|
1584277 | 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 | 710018 | int mb_type = mb_type_table[s->c.mv_dir]; | |
468 | |||
469 |
2/2✓ Branch 0 taken 33934 times.
✓ Branch 1 taken 676084 times.
|
710018 | if (s->c.mb_x == 0) { |
470 |
2/2✓ Branch 0 taken 67868 times.
✓ Branch 1 taken 33934 times.
|
101802 | for (i = 0; i < 2; i++) |
471 | 67868 | s->c.last_mv[i][0][0] = | |
472 | 67868 | s->c.last_mv[i][0][1] = | |
473 | 67868 | s->c.last_mv[i][1][0] = | |
474 | 67868 | 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 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 & ... |
483 | 90 | s->c.mv[0][0][0] = | |
484 | 90 | s->c.mv[0][0][1] = | |
485 | 90 | s->c.mv[1][0][0] = | |
486 | 90 | s->c.mv[1][0][1] = 0; | |
487 | 90 | s->c.mv_dir = MV_DIR_FORWARD; // doesn't matter | |
488 | 90 | s->c.qscale -= s->dquant; | |
489 | // s->c.mb_skipped = 1; | ||
490 | |||
491 | 65914 | return; | |
492 | } | ||
493 | |||
494 | 709928 | cbp = get_b_cbp(s, block, motion_x, motion_y, mb_type); | |
495 | |||
496 |
2/2✓ Branch 0 taken 43224 times.
✓ Branch 1 taken 666704 times.
|
709928 | if ((cbp | motion_x | motion_y | mb_type) == 0) { |
497 | /* direct MB with MV={0,0} */ | ||
498 | av_assert2(s->dquant == 0); | ||
499 | |||
500 | 43224 | put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */ | |
501 | |||
502 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43224 times.
|
43224 | if (interleaved_stats) { |
503 | ✗ | s->misc_bits++; | |
504 | ✗ | s->last_bits++; | |
505 | } | ||
506 | 43224 | return; | |
507 | } | ||
508 | |||
509 | 666704 | put_bits(&s->pb, 1, 0); /* mb coded modb1=0 */ | |
510 | 666704 | put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ // FIXME merge | |
511 | 666704 | 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 438341 times.
✓ Branch 1 taken 228363 times.
|
666704 | if (cbp) |
513 | 438341 | put_bits(&s->pb, 6, cbp); | |
514 | |||
515 |
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) { |
516 |
2/2✓ Branch 0 taken 41637 times.
✓ Branch 1 taken 267942 times.
|
309579 | if (s->dquant) |
517 | 41637 | put_bits(&s->pb, 2, (s->dquant >> 2) + 3); | |
518 | else | ||
519 | 267942 | put_bits(&s->pb, 1, 0); | |
520 | } else | ||
521 | 357125 | s->c.qscale -= s->dquant; | |
522 | |||
523 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 666704 times.
|
666704 | 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 666704 times.
|
666704 | if (interleaved_stats) |
531 | ✗ | s->misc_bits += get_bits_diff(s); | |
532 | |||
533 |
2/2✓ Branch 0 taken 151361 times.
✓ Branch 1 taken 515343 times.
|
666704 | if (!mb_type) { |
534 | av_assert2(s->c.mv_dir & MV_DIRECT); | ||
535 | 151361 | 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 515343 times.
✗ Branch 1 not taken.
|
515343 | if (s->c.mv_type != MV_TYPE_FIELD) { |
539 |
2/2✓ Branch 0 taken 349971 times.
✓ Branch 1 taken 165372 times.
|
515343 | if (s->c.mv_dir & MV_DIR_FORWARD) { |
540 | 349971 | ff_h263_encode_motion_vector(s, | |
541 | 349971 | s->c.mv[0][0][0] - s->c.last_mv[0][0][0], | |
542 | 349971 | s->c.mv[0][0][1] - s->c.last_mv[0][0][1], | |
543 | s->f_code); | ||
544 | 349971 | s->c.last_mv[0][0][0] = | |
545 | 349971 | s->c.last_mv[0][1][0] = s->c.mv[0][0][0]; | |
546 | 349971 | s->c.last_mv[0][0][1] = | |
547 | 349971 | s->c.last_mv[0][1][1] = s->c.mv[0][0][1]; | |
548 | } | ||
549 |
2/2✓ Branch 0 taken 352997 times.
✓ Branch 1 taken 162346 times.
|
515343 | if (s->c.mv_dir & MV_DIR_BACKWARD) { |
550 | 352997 | ff_h263_encode_motion_vector(s, | |
551 | 352997 | s->c.mv[1][0][0] - s->c.last_mv[1][0][0], | |
552 | 352997 | s->c.mv[1][0][1] - s->c.last_mv[1][0][1], | |
553 | s->b_code); | ||
554 | 352997 | s->c.last_mv[1][0][0] = | |
555 | 352997 | s->c.last_mv[1][1][0] = s->c.mv[1][0][0]; | |
556 | 352997 | s->c.last_mv[1][0][1] = | |
557 | 352997 | 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 666704 times.
|
666704 | if (interleaved_stats) |
592 | ✗ | s->mv_bits += get_bits_diff(s); | |
593 | |||
594 | 666704 | mpeg4_encode_blocks_inter(s, block, &s->pb); | |
595 | |||
596 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 666704 times.
|
666704 | if (interleaved_stats) |
597 | ✗ | s->p_tex_bits += get_bits_diff(s); | |
598 | } else { /* s->c.pict_type == AV_PICTURE_TYPE_B */ | ||
599 | 874259 | cbp = get_p_cbp(s, block, motion_x, motion_y); | |
600 | |||
601 |
2/2✓ Branch 0 taken 56129 times.
✓ Branch 1 taken 818130 times.
|
874259 | if ((cbp | motion_x | motion_y | s->dquant) == 0 && |
602 |
2/2✓ Branch 0 taken 22811 times.
✓ Branch 1 taken 33318 times.
|
56129 | s->c.mv_type == MV_TYPE_16X16) { |
603 | 22811 | 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 720 times.
✓ Branch 1 taken 22091 times.
|
22811 | if (m->max_b_frames > 0) { |
608 | int x, y, offset; | ||
609 | const uint8_t *p_pic; | ||
610 | |||
611 | 720 | x = s->c.mb_x * 16; | |
612 | 720 | y = s->c.mb_y * 16; | |
613 | |||
614 | 720 | offset = x + y * s->c.linesize; | |
615 | 720 | p_pic = s->new_pic->data[0] + offset; | |
616 | |||
617 | 720 | s->c.mb_skipped = 1; | |
618 |
2/2✓ Branch 0 taken 791 times.
✓ Branch 1 taken 71 times.
|
862 | for (int i = 0; i < m->max_b_frames; i++) { |
619 | const uint8_t *b_pic; | ||
620 | int diff; | ||
621 | 791 | const MPVPicture *pic = m->reordered_input_picture[i + 1]; | |
622 | |||
623 |
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) |
624 | break; | ||
625 | |||
626 | 353 | b_pic = pic->f->data[0] + offset; | |
627 |
1/2✓ Branch 0 taken 353 times.
✗ Branch 1 not taken.
|
353 | if (!pic->shared) |
628 | 353 | b_pic += INPLACE_OFFSET; | |
629 | |||
630 |
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) { |
631 | int x1, y1; | ||
632 | ✗ | int xe = FFMIN(16, s->c.width - x); | |
633 | ✗ | int ye = FFMIN(16, s->c.height - y); | |
634 | ✗ | diff = 0; | |
635 | ✗ | for (y1 = 0; y1 < ye; y1++) { | |
636 | ✗ | for (x1 = 0; x1 < xe; x1++) { | |
637 | ✗ | diff += FFABS(p_pic[x1 + y1 * s->c.linesize] - b_pic[x1 + y1 * s->c.linesize]); | |
638 | } | ||
639 | } | ||
640 | ✗ | diff = diff * 256 / (xe * ye); | |
641 | } else { | ||
642 | 353 | diff = s->sad_cmp[0](NULL, p_pic, b_pic, s->c.linesize, 16); | |
643 | } | ||
644 |
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 |
645 | 211 | s->c.mb_skipped = 0; | |
646 | 211 | break; | |
647 | } | ||
648 | } | ||
649 | } else | ||
650 | 22091 | s->c.mb_skipped = 1; | |
651 | |||
652 |
2/2✓ Branch 0 taken 22600 times.
✓ Branch 1 taken 211 times.
|
22811 | if (s->c.mb_skipped == 1) { |
653 | /* skip macroblock */ | ||
654 | 22600 | put_bits(&s->pb, 1, 1); | |
655 | |||
656 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22600 times.
|
22600 | if (interleaved_stats) { |
657 | ✗ | s->misc_bits++; | |
658 | ✗ | s->last_bits++; | |
659 | } | ||
660 | |||
661 | 22600 | return; | |
662 | } | ||
663 | } | ||
664 | |||
665 | 851659 | put_bits(&s->pb, 1, 0); /* mb coded */ | |
666 | 851659 | cbpc = cbp & 3; | |
667 | 851659 | cbpy = cbp >> 2; | |
668 | 851659 | cbpy ^= 0xf; | |
669 |
2/2✓ Branch 0 taken 631610 times.
✓ Branch 1 taken 220049 times.
|
851659 | if (s->c.mv_type == MV_TYPE_16X16) { |
670 |
2/2✓ Branch 0 taken 63254 times.
✓ Branch 1 taken 568356 times.
|
631610 | if (s->dquant) |
671 | 63254 | cbpc += 8; | |
672 | 631610 | put_bits(&s->pb, | |
673 | 631610 | ff_h263_inter_MCBPC_bits[cbpc], | |
674 | 631610 | ff_h263_inter_MCBPC_code[cbpc]); | |
675 | |||
676 | 631610 | put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]); | |
677 |
2/2✓ Branch 0 taken 63254 times.
✓ Branch 1 taken 568356 times.
|
631610 | if (s->dquant) |
678 | 63254 | put_bits(pb2, 2, dquant_code[s->dquant + 2]); | |
679 | |||
680 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 631610 times.
|
631610 | if (!s->c.progressive_sequence) { |
681 | ✗ | if (cbp) | |
682 | ✗ | put_bits(pb2, 1, s->c.interlaced_dct); | |
683 | ✗ | put_bits(pb2, 1, 0); | |
684 | } | ||
685 | |||
686 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 631610 times.
|
631610 | if (interleaved_stats) |
687 | ✗ | s->misc_bits += get_bits_diff(s); | |
688 | |||
689 | /* motion vectors: 16x16 mode */ | ||
690 | 631610 | ff_h263_pred_motion(&s->c, 0, 0, &pred_x, &pred_y); | |
691 | |||
692 | 631610 | ff_h263_encode_motion_vector(s, | |
693 | motion_x - pred_x, | ||
694 | motion_y - pred_y, | ||
695 | s->f_code); | ||
696 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 220049 times.
|
220049 | } else if (s->c.mv_type == MV_TYPE_FIELD) { |
697 | ✗ | if (s->dquant) | |
698 | ✗ | cbpc += 8; | |
699 | ✗ | put_bits(&s->pb, | |
700 | ✗ | ff_h263_inter_MCBPC_bits[cbpc], | |
701 | ✗ | ff_h263_inter_MCBPC_code[cbpc]); | |
702 | |||
703 | ✗ | put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]); | |
704 | ✗ | if (s->dquant) | |
705 | ✗ | put_bits(pb2, 2, dquant_code[s->dquant + 2]); | |
706 | |||
707 | av_assert2(!s->c.progressive_sequence); | ||
708 | ✗ | if (cbp) | |
709 | ✗ | put_bits(pb2, 1, s->c.interlaced_dct); | |
710 | ✗ | put_bits(pb2, 1, 1); | |
711 | |||
712 | ✗ | if (interleaved_stats) | |
713 | ✗ | s->misc_bits += get_bits_diff(s); | |
714 | |||
715 | /* motion vectors: 16x8 interlaced mode */ | ||
716 | ✗ | ff_h263_pred_motion(&s->c, 0, 0, &pred_x, &pred_y); | |
717 | ✗ | pred_y /= 2; | |
718 | |||
719 | ✗ | put_bits(&s->pb, 1, s->c.field_select[0][0]); | |
720 | ✗ | put_bits(&s->pb, 1, s->c.field_select[0][1]); | |
721 | |||
722 | ✗ | ff_h263_encode_motion_vector(s, | |
723 | ✗ | s->c.mv[0][0][0] - pred_x, | |
724 | ✗ | s->c.mv[0][0][1] - pred_y, | |
725 | s->f_code); | ||
726 | ✗ | ff_h263_encode_motion_vector(s, | |
727 | ✗ | s->c.mv[0][1][0] - pred_x, | |
728 | ✗ | s->c.mv[0][1][1] - pred_y, | |
729 | s->f_code); | ||
730 | } else { | ||
731 | av_assert2(s->c.mv_type == MV_TYPE_8X8); | ||
732 | 220049 | put_bits(&s->pb, | |
733 | 220049 | ff_h263_inter_MCBPC_bits[cbpc + 16], | |
734 | 220049 | ff_h263_inter_MCBPC_code[cbpc + 16]); | |
735 | 220049 | put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]); | |
736 | |||
737 |
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) |
738 | ✗ | put_bits(pb2, 1, s->c.interlaced_dct); | |
739 | |||
740 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 220049 times.
|
220049 | if (interleaved_stats) |
741 | ✗ | s->misc_bits += get_bits_diff(s); | |
742 | |||
743 |
2/2✓ Branch 0 taken 880196 times.
✓ Branch 1 taken 220049 times.
|
1100245 | for (i = 0; i < 4; i++) { |
744 | /* motion vectors: 8x8 mode*/ | ||
745 | 880196 | ff_h263_pred_motion(&s->c, i, 0, &pred_x, &pred_y); | |
746 | |||
747 | 880196 | ff_h263_encode_motion_vector(s, | |
748 | 880196 | s->c.cur_pic.motion_val[0][s->c.block_index[i]][0] - pred_x, | |
749 | 880196 | s->c.cur_pic.motion_val[0][s->c.block_index[i]][1] - pred_y, | |
750 | s->f_code); | ||
751 | } | ||
752 | } | ||
753 | |||
754 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 851659 times.
|
851659 | if (interleaved_stats) |
755 | ✗ | s->mv_bits += get_bits_diff(s); | |
756 | |||
757 | 851659 | mpeg4_encode_blocks_inter(s, block, tex_pb); | |
758 | |||
759 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 851659 times.
|
851659 | if (interleaved_stats) |
760 | ✗ | s->p_tex_bits += get_bits_diff(s); | |
761 | } | ||
762 | } else { | ||
763 | int cbp; | ||
764 | int dc_diff[6]; // dc values with the dc prediction subtracted | ||
765 | int dir[6]; // prediction direction | ||
766 | int zigzag_last_index[6]; | ||
767 | const uint8_t *scan_table[6]; | ||
768 | int i; | ||
769 | |||
770 |
2/2✓ Branch 0 taken 1291776 times.
✓ Branch 1 taken 215296 times.
|
1507072 | for (int i = 0; i < 6; i++) { |
771 | 1291776 | int pred = mpeg4_pred_dc(&s->c, i, &dir[i]); | |
772 |
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; |
773 | |||
774 | 1291776 | pred = FASTDIV((pred + (scale >> 1)), scale); | |
775 | 1291776 | dc_diff[i] = block[i][0] - pred; | |
776 | 1291776 | s->c.dc_val[s->c.block_index[i]] = av_clip_uintp2(block[i][0] * scale, 11); | |
777 | } | ||
778 | |||
779 |
2/2✓ Branch 0 taken 48774 times.
✓ Branch 1 taken 166522 times.
|
215296 | if (s->c.avctx->flags & AV_CODEC_FLAG_AC_PRED) { |
780 | 48774 | s->c.ac_pred = decide_ac_pred(s, block, dir, scan_table, zigzag_last_index); | |
781 | } else { | ||
782 |
2/2✓ Branch 0 taken 999132 times.
✓ Branch 1 taken 166522 times.
|
1165654 | for (i = 0; i < 6; i++) |
783 | 999132 | scan_table[i] = s->c.intra_scantable.permutated; | |
784 | } | ||
785 | |||
786 | /* compute cbp */ | ||
787 | 215296 | cbp = 0; | |
788 |
2/2✓ Branch 0 taken 1291776 times.
✓ Branch 1 taken 215296 times.
|
1507072 | for (i = 0; i < 6; i++) |
789 |
2/2✓ Branch 0 taken 1000754 times.
✓ Branch 1 taken 291022 times.
|
1291776 | if (s->c.block_last_index[i] >= 1) |
790 | 1000754 | cbp |= 1 << (5 - i); | |
791 | |||
792 | 215296 | cbpc = cbp & 3; | |
793 |
2/2✓ Branch 0 taken 113910 times.
✓ Branch 1 taken 101386 times.
|
215296 | if (s->c.pict_type == AV_PICTURE_TYPE_I) { |
794 |
2/2✓ Branch 0 taken 26626 times.
✓ Branch 1 taken 87284 times.
|
113910 | if (s->dquant) |
795 | 26626 | cbpc += 4; | |
796 | 113910 | put_bits(&s->pb, | |
797 | 113910 | ff_h263_intra_MCBPC_bits[cbpc], | |
798 | 113910 | ff_h263_intra_MCBPC_code[cbpc]); | |
799 | } else { | ||
800 |
2/2✓ Branch 0 taken 9460 times.
✓ Branch 1 taken 91926 times.
|
101386 | if (s->dquant) |
801 | 9460 | cbpc += 8; | |
802 | 101386 | put_bits(&s->pb, 1, 0); /* mb coded */ | |
803 | 101386 | put_bits(&s->pb, | |
804 | 101386 | ff_h263_inter_MCBPC_bits[cbpc + 4], | |
805 | 101386 | ff_h263_inter_MCBPC_code[cbpc + 4]); | |
806 | } | ||
807 | 215296 | put_bits(pb2, 1, s->c.ac_pred); | |
808 | 215296 | cbpy = cbp >> 2; | |
809 | 215296 | put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]); | |
810 |
2/2✓ Branch 0 taken 36086 times.
✓ Branch 1 taken 179210 times.
|
215296 | if (s->dquant) |
811 | 36086 | put_bits(dc_pb, 2, dquant_code[s->dquant + 2]); | |
812 | |||
813 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 215296 times.
|
215296 | if (!s->c.progressive_sequence) |
814 | ✗ | put_bits(dc_pb, 1, s->c.interlaced_dct); | |
815 | |||
816 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 215296 times.
|
215296 | if (interleaved_stats) |
817 | ✗ | s->misc_bits += get_bits_diff(s); | |
818 | |||
819 | 215296 | mpeg4_encode_blocks_intra(s, block, dc_diff, scan_table, dc_pb, tex_pb); | |
820 | |||
821 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 215296 times.
|
215296 | if (interleaved_stats) |
822 | ✗ | s->i_tex_bits += get_bits_diff(s); | |
823 | 215296 | s->i_count++; | |
824 | |||
825 | /* restore ac coeffs & last_index stuff | ||
826 | * if we messed them up with the prediction */ | ||
827 |
2/2✓ Branch 0 taken 7332 times.
✓ Branch 1 taken 207964 times.
|
215296 | if (s->c.ac_pred) |
828 | 7332 | restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index); | |
829 | } | ||
830 | } | ||
831 | |||
832 | /** | ||
833 | * add MPEG-4 stuffing bits (01...1) | ||
834 | */ | ||
835 | 6706 | void ff_mpeg4_stuffing(PutBitContext *pbc) | |
836 | { | ||
837 | 6706 | int length = 8 - (put_bits_count(pbc) & 7); | |
838 | |||
839 | 6706 | put_bits(pbc, length, (1 << (length - 1)) - 1); | |
840 | 6706 | } | |
841 | |||
842 | /* must be called before writing the header */ | ||
843 | 2665 | void ff_set_mpeg4_time(MPVEncContext *const s) | |
844 | { | ||
845 |
2/2✓ Branch 0 taken 587 times.
✓ Branch 1 taken 2078 times.
|
2665 | if (s->c.pict_type == AV_PICTURE_TYPE_B) { |
846 | 587 | ff_mpeg4_init_direct_mv(&s->c); | |
847 | } else { | ||
848 | 2078 | s->c.last_time_base = s->c.time_base; | |
849 |
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); |
850 | } | ||
851 | 2665 | } | |
852 | |||
853 | 274 | static void mpeg4_encode_gop_header(MPVMainEncContext *const m) | |
854 | { | ||
855 | 274 | MPVEncContext *const s = &m->s; | |
856 | int64_t hours, minutes, seconds; | ||
857 | int64_t time; | ||
858 | |||
859 | 274 | put_bits32(&s->pb, GOP_STARTCODE); | |
860 | |||
861 | 274 | time = s->c.cur_pic.ptr->f->pts; | |
862 |
2/2✓ Branch 0 taken 76 times.
✓ Branch 1 taken 198 times.
|
274 | if (m->reordered_input_picture[1]) |
863 | 76 | time = FFMIN(time, m->reordered_input_picture[1]->f->pts); | |
864 | 274 | time = time * s->c.avctx->time_base.num; | |
865 |
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); |
866 | |||
867 |
2/2✓ Branch 0 taken 63 times.
✓ Branch 1 taken 211 times.
|
274 | seconds = FFUDIV(time, s->c.avctx->time_base.den); |
868 |
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); |
869 |
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); |
870 |
1/2✓ Branch 0 taken 274 times.
✗ Branch 1 not taken.
|
274 | hours = FFUMOD(hours , 24); |
871 | |||
872 | 274 | put_bits(&s->pb, 5, hours); | |
873 | 274 | put_bits(&s->pb, 6, minutes); | |
874 | 274 | put_bits(&s->pb, 1, 1); | |
875 | 274 | put_bits(&s->pb, 6, seconds); | |
876 | |||
877 | 274 | put_bits(&s->pb, 1, !!(s->c.avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)); | |
878 | 274 | put_bits(&s->pb, 1, 0); // broken link == NO | |
879 | |||
880 | 274 | ff_mpeg4_stuffing(&s->pb); | |
881 | 274 | } | |
882 | |||
883 | 222 | static void mpeg4_encode_visual_object_header(MPVMainEncContext *const m) | |
884 | { | ||
885 | 222 | MPVEncContext *const s = &m->s; | |
886 | int profile_and_level_indication; | ||
887 | int vo_ver_id; | ||
888 | |||
889 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 222 times.
|
222 | if (s->c.avctx->profile != AV_PROFILE_UNKNOWN) { |
890 | ✗ | profile_and_level_indication = s->c.avctx->profile << 4; | |
891 |
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) { |
892 | 101 | profile_and_level_indication = 0xF0; // adv simple | |
893 | } else { | ||
894 | 121 | profile_and_level_indication = 0x00; // simple | |
895 | } | ||
896 | |||
897 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 222 times.
|
222 | if (s->c.avctx->level != AV_LEVEL_UNKNOWN) |
898 | ✗ | profile_and_level_indication |= s->c.avctx->level; | |
899 | else | ||
900 | 222 | profile_and_level_indication |= 1; // level 1 | |
901 | |||
902 |
2/2✓ Branch 0 taken 101 times.
✓ Branch 1 taken 121 times.
|
222 | if (profile_and_level_indication >> 4 == 0xF) |
903 | 101 | vo_ver_id = 5; | |
904 | else | ||
905 | 121 | vo_ver_id = 1; | |
906 | |||
907 | // FIXME levels | ||
908 | |||
909 | 222 | put_bits32(&s->pb, VOS_STARTCODE); | |
910 | |||
911 | 222 | put_bits(&s->pb, 8, profile_and_level_indication); | |
912 | |||
913 | 222 | put_bits32(&s->pb, VISUAL_OBJ_STARTCODE); | |
914 | |||
915 | 222 | put_bits(&s->pb, 1, 1); | |
916 | 222 | put_bits(&s->pb, 4, vo_ver_id); | |
917 | 222 | put_bits(&s->pb, 3, 1); // priority | |
918 | |||
919 | 222 | put_bits(&s->pb, 4, 1); // visual obj type== video obj | |
920 | |||
921 | 222 | put_bits(&s->pb, 1, 0); // video signal type == no clue // FIXME | |
922 | |||
923 | 222 | ff_mpeg4_stuffing(&s->pb); | |
924 | 222 | } | |
925 | |||
926 | 222 | static void mpeg4_encode_vol_header(Mpeg4EncContext *const m4, | |
927 | int vo_number, | ||
928 | int vol_number) | ||
929 | { | ||
930 | 222 | MPVEncContext *const s = &m4->m.s; | |
931 | int vo_ver_id, vo_type, aspect_ratio_info; | ||
932 | |||
933 |
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) { |
934 | 101 | vo_ver_id = 5; | |
935 | 101 | vo_type = ADV_SIMPLE_VO_TYPE; | |
936 | } else { | ||
937 | 121 | vo_ver_id = 1; | |
938 | 121 | vo_type = SIMPLE_VO_TYPE; | |
939 | } | ||
940 | |||
941 | 222 | put_bits32(&s->pb, 0x100 + vo_number); /* video obj */ | |
942 | 222 | put_bits32(&s->pb, 0x120 + vol_number); /* video obj layer */ | |
943 | |||
944 | 222 | put_bits(&s->pb, 1, 0); /* random access vol */ | |
945 | 222 | put_bits(&s->pb, 8, vo_type); /* video obj type indication */ | |
946 | 222 | put_bits(&s->pb, 1, 1); /* is obj layer id= yes */ | |
947 | 222 | put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */ | |
948 | 222 | put_bits(&s->pb, 3, 1); /* is obj layer priority */ | |
949 | |||
950 | 222 | aspect_ratio_info = ff_h263_aspect_to_info(s->c.avctx->sample_aspect_ratio); | |
951 | |||
952 | 222 | put_bits(&s->pb, 4, aspect_ratio_info); /* aspect ratio info */ | |
953 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 222 times.
|
222 | if (aspect_ratio_info == FF_ASPECT_EXTENDED) { |
954 | ✗ | av_reduce(&s->c.avctx->sample_aspect_ratio.num, &s->c.avctx->sample_aspect_ratio.den, | |
955 | ✗ | s->c.avctx->sample_aspect_ratio.num, s->c.avctx->sample_aspect_ratio.den, 255); | |
956 | ✗ | put_bits(&s->pb, 8, s->c.avctx->sample_aspect_ratio.num); | |
957 | ✗ | put_bits(&s->pb, 8, s->c.avctx->sample_aspect_ratio.den); | |
958 | } | ||
959 | |||
960 | 222 | put_bits(&s->pb, 1, 1); /* vol control parameters= yes */ | |
961 | 222 | put_bits(&s->pb, 2, 1); /* chroma format YUV 420/YV12 */ | |
962 | 222 | put_bits(&s->pb, 1, s->c.low_delay); | |
963 | 222 | put_bits(&s->pb, 1, 0); /* vbv parameters= no */ | |
964 | |||
965 | 222 | put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */ | |
966 | 222 | put_bits(&s->pb, 1, 1); /* marker bit */ | |
967 | |||
968 | 222 | put_bits(&s->pb, 16, s->c.avctx->time_base.den); | |
969 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 222 times.
|
222 | if (m4->time_increment_bits < 1) |
970 | ✗ | m4->time_increment_bits = 1; | |
971 | 222 | put_bits(&s->pb, 1, 1); /* marker bit */ | |
972 | 222 | put_bits(&s->pb, 1, 0); /* fixed vop rate=no */ | |
973 | 222 | put_bits(&s->pb, 1, 1); /* marker bit */ | |
974 | 222 | put_bits(&s->pb, 13, s->c.width); /* vol width */ | |
975 | 222 | put_bits(&s->pb, 1, 1); /* marker bit */ | |
976 | 222 | put_bits(&s->pb, 13, s->c.height); /* vol height */ | |
977 | 222 | put_bits(&s->pb, 1, 1); /* marker bit */ | |
978 | 222 | put_bits(&s->pb, 1, s->c.progressive_sequence ? 0 : 1); | |
979 | 222 | put_bits(&s->pb, 1, 1); /* obmc disable */ | |
980 |
2/2✓ Branch 0 taken 121 times.
✓ Branch 1 taken 101 times.
|
222 | if (vo_ver_id == 1) |
981 | 121 | put_bits(&s->pb, 1, 0); /* sprite enable */ | |
982 | else | ||
983 | 101 | put_bits(&s->pb, 2, 0); /* sprite enable */ | |
984 | |||
985 | 222 | put_bits(&s->pb, 1, 0); /* not 8 bit == false */ | |
986 | 222 | put_bits(&s->pb, 1, s->mpeg_quant); /* quant type = (0 = H.263 style) */ | |
987 | |||
988 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 202 times.
|
222 | if (s->mpeg_quant) { |
989 | 20 | ff_write_quant_matrix(&s->pb, s->c.avctx->intra_matrix); | |
990 | 20 | ff_write_quant_matrix(&s->pb, s->c.avctx->inter_matrix); | |
991 | } | ||
992 | |||
993 |
2/2✓ Branch 0 taken 101 times.
✓ Branch 1 taken 121 times.
|
222 | if (vo_ver_id != 1) |
994 | 101 | put_bits(&s->pb, 1, s->c.quarter_sample); | |
995 | 222 | put_bits(&s->pb, 1, 1); /* complexity estimation disable */ | |
996 | 222 | put_bits(&s->pb, 1, s->rtp_mode ? 0 : 1); /* resync marker disable */ | |
997 | 222 | put_bits(&s->pb, 1, s->data_partitioning); | |
998 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 162 times.
|
222 | if (s->data_partitioning) |
999 | 60 | put_bits(&s->pb, 1, 0); /* no rvlc */ | |
1000 | |||
1001 |
2/2✓ Branch 0 taken 101 times.
✓ Branch 1 taken 121 times.
|
222 | if (vo_ver_id != 1) { |
1002 | 101 | put_bits(&s->pb, 1, 0); /* newpred */ | |
1003 | 101 | put_bits(&s->pb, 1, 0); /* reduced res vop */ | |
1004 | } | ||
1005 | 222 | put_bits(&s->pb, 1, 0); /* scalability */ | |
1006 | |||
1007 | 222 | ff_mpeg4_stuffing(&s->pb); | |
1008 | |||
1009 | /* user data */ | ||
1010 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 222 times.
|
222 | if (!(s->c.avctx->flags & AV_CODEC_FLAG_BITEXACT)) { |
1011 | ✗ | put_bits32(&s->pb, USER_DATA_STARTCODE); | |
1012 | ✗ | ff_put_string(&s->pb, LIBAVCODEC_IDENT, 0); | |
1013 | } | ||
1014 | 222 | } | |
1015 | |||
1016 | /* write MPEG-4 VOP header */ | ||
1017 | 2665 | static int mpeg4_encode_picture_header(MPVMainEncContext *const m) | |
1018 | { | ||
1019 | 2665 | Mpeg4EncContext *const m4 = mainctx_to_mpeg4(m); | |
1020 | 2665 | MPVEncContext *const s = &m->s; | |
1021 | uint64_t time_incr; | ||
1022 | int64_t time_div, time_mod; | ||
1023 | |||
1024 | 2665 | put_bits_assume_flushed(&s->pb); | |
1025 | |||
1026 |
2/2✓ Branch 0 taken 274 times.
✓ Branch 1 taken 2391 times.
|
2665 | if (s->c.pict_type == AV_PICTURE_TYPE_I) { |
1027 |
2/2✓ Branch 0 taken 201 times.
✓ Branch 1 taken 73 times.
|
274 | if (!(s->c.avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) { |
1028 |
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 |
1029 | 201 | mpeg4_encode_visual_object_header(m); | |
1030 |
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->picture_number == 0) // HACK, the reference sw is buggy |
1031 | 201 | mpeg4_encode_vol_header(m4, 0, 0); | |
1032 | } | ||
1033 | 274 | mpeg4_encode_gop_header(m); | |
1034 | } | ||
1035 | |||
1036 |
4/4✓ Branch 0 taken 600 times.
✓ Branch 1 taken 2065 times.
✓ Branch 2 taken 472 times.
✓ Branch 3 taken 128 times.
|
2665 | s->partitioned_frame = s->data_partitioning && s->c.pict_type != AV_PICTURE_TYPE_B; |
1037 | |||
1038 | 2665 | put_bits32(&s->pb, VOP_STARTCODE); /* vop header */ | |
1039 | 2665 | put_bits(&s->pb, 2, s->c.pict_type - 1); /* pict type: I = 0 , P = 1 */ | |
1040 | |||
1041 |
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); |
1042 |
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); |
1043 | 2665 | time_incr = time_div - s->c.last_time_base; | |
1044 | |||
1045 | // This limits the frame duration to max 1 day | ||
1046 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2665 times.
|
2665 | if (time_incr > 3600*24) { |
1047 | ✗ | av_log(s->c.avctx, AV_LOG_ERROR, "time_incr %"PRIu64" too large\n", time_incr); | |
1048 | ✗ | return AVERROR(EINVAL); | |
1049 | } | ||
1050 |
2/2✓ Branch 0 taken 81 times.
✓ Branch 1 taken 2665 times.
|
2746 | while (time_incr--) |
1051 | 81 | put_bits(&s->pb, 1, 1); | |
1052 | |||
1053 | 2665 | put_bits(&s->pb, 1, 0); | |
1054 | |||
1055 | 2665 | put_bits(&s->pb, 1, 1); /* marker */ | |
1056 | 2665 | put_bits(&s->pb, m4->time_increment_bits, time_mod); /* time increment */ | |
1057 | 2665 | put_bits(&s->pb, 1, 1); /* marker */ | |
1058 | 2665 | put_bits(&s->pb, 1, 1); /* vop coded */ | |
1059 |
2/2✓ Branch 0 taken 1804 times.
✓ Branch 1 taken 861 times.
|
2665 | if (s->c.pict_type == AV_PICTURE_TYPE_P) { |
1060 | 1804 | put_bits(&s->pb, 1, s->c.no_rounding); /* rounding type */ | |
1061 | } | ||
1062 | 2665 | put_bits(&s->pb, 3, 0); /* intra dc VLC threshold */ | |
1063 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2665 times.
|
2665 | if (!s->c.progressive_sequence) { |
1064 | ✗ | put_bits(&s->pb, 1, !!(s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST)); | |
1065 | ✗ | put_bits(&s->pb, 1, s->c.alternate_scan); | |
1066 | } | ||
1067 | // FIXME sprite stuff | ||
1068 | |||
1069 | 2665 | put_bits(&s->pb, 5, s->c.qscale); | |
1070 | |||
1071 |
2/2✓ Branch 0 taken 2391 times.
✓ Branch 1 taken 274 times.
|
2665 | if (s->c.pict_type != AV_PICTURE_TYPE_I) |
1072 | 2391 | put_bits(&s->pb, 3, s->f_code); /* fcode_for */ | |
1073 |
2/2✓ Branch 0 taken 587 times.
✓ Branch 1 taken 2078 times.
|
2665 | if (s->c.pict_type == AV_PICTURE_TYPE_B) |
1074 | 587 | put_bits(&s->pb, 3, s->b_code); /* fcode_back */ | |
1075 | |||
1076 | 2665 | return 0; | |
1077 | } | ||
1078 | |||
1079 | 63 | static av_cold void init_uni_dc_tab(void) | |
1080 | { | ||
1081 | int level, uni_code, uni_len; | ||
1082 | |||
1083 |
2/2✓ Branch 0 taken 32256 times.
✓ Branch 1 taken 63 times.
|
32319 | for (level = -256; level < 256; level++) { |
1084 | int size, v, l; | ||
1085 | /* find number of bits */ | ||
1086 | 32256 | size = 0; | |
1087 | 32256 | v = abs(level); | |
1088 |
2/2✓ Branch 0 taken 226485 times.
✓ Branch 1 taken 32256 times.
|
258741 | while (v) { |
1089 | 226485 | v >>= 1; | |
1090 | 226485 | size++; | |
1091 | } | ||
1092 | |||
1093 |
2/2✓ Branch 0 taken 16128 times.
✓ Branch 1 taken 16128 times.
|
32256 | if (level < 0) |
1094 | 16128 | l = (-level) ^ ((1 << size) - 1); | |
1095 | else | ||
1096 | 16128 | l = level; | |
1097 | |||
1098 | /* luminance */ | ||
1099 | 32256 | uni_code = ff_mpeg4_DCtab_lum[size][0]; | |
1100 | 32256 | uni_len = ff_mpeg4_DCtab_lum[size][1]; | |
1101 | |||
1102 |
2/2✓ Branch 0 taken 32193 times.
✓ Branch 1 taken 63 times.
|
32256 | if (size > 0) { |
1103 | 32193 | uni_code <<= size; | |
1104 | 32193 | uni_code |= l; | |
1105 | 32193 | uni_len += size; | |
1106 |
2/2✓ Branch 0 taken 63 times.
✓ Branch 1 taken 32130 times.
|
32193 | if (size > 8) { |
1107 | 63 | uni_code <<= 1; | |
1108 | 63 | uni_code |= 1; | |
1109 | 63 | uni_len++; | |
1110 | } | ||
1111 | } | ||
1112 | 32256 | uni_DCtab_lum_bits[level + 256] = uni_code; | |
1113 | 32256 | uni_DCtab_lum_len[level + 256] = uni_len; | |
1114 | |||
1115 | /* chrominance */ | ||
1116 | 32256 | uni_code = ff_mpeg4_DCtab_chrom[size][0]; | |
1117 | 32256 | uni_len = ff_mpeg4_DCtab_chrom[size][1]; | |
1118 | |||
1119 |
2/2✓ Branch 0 taken 32193 times.
✓ Branch 1 taken 63 times.
|
32256 | if (size > 0) { |
1120 | 32193 | uni_code <<= size; | |
1121 | 32193 | uni_code |= l; | |
1122 | 32193 | uni_len += size; | |
1123 |
2/2✓ Branch 0 taken 63 times.
✓ Branch 1 taken 32130 times.
|
32193 | if (size > 8) { |
1124 | 63 | uni_code <<= 1; | |
1125 | 63 | uni_code |= 1; | |
1126 | 63 | uni_len++; | |
1127 | } | ||
1128 | } | ||
1129 | 32256 | uni_DCtab_chrom_bits[level + 256] = uni_code; | |
1130 | 32256 | uni_DCtab_chrom_len[level + 256] = uni_len; | |
1131 | } | ||
1132 | 63 | } | |
1133 | |||
1134 | 126 | static av_cold void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab, | |
1135 | uint8_t *len_tab) | ||
1136 | { | ||
1137 | // Type 3 escape method. The escape code is the same for both VLCs | ||
1138 | // (0x3, seven bits), so it is hardcoded. | ||
1139 | 126 | memset(len_tab, 30, 2 * 2 * 64 * 64); | |
1140 | 126 | len_tab += 64; | |
1141 | 126 | bits_tab += 64; | |
1142 |
2/2✓ Branch 0 taken 8064 times.
✓ Branch 1 taken 126 times.
|
8190 | for (int run = 0; run < 64; ++run) { |
1143 | 8064 | for (int level = 1;; ++level) { | |
1144 | // Escape code type 3 not last run (6 bits) marker marker | ||
1145 | 516096 | unsigned code = (3 << 23) | (3 << 21) | (0 << 20) | (run << 14) | (1 << 13) | 1; | |
1146 | // first the negative levels | ||
1147 | 516096 | bits_tab[UNI_MPEG4_ENC_INDEX(0, run, -level)] = code | (-level & 0xfff) << 1; | |
1148 | 516096 | bits_tab[UNI_MPEG4_ENC_INDEX(1, run, -level)] = | |
1149 | 516096 | bits_tab[UNI_MPEG4_ENC_INDEX(0, run, -level)] | (1 << 20) /* last */; | |
1150 | |||
1151 |
2/2✓ Branch 0 taken 8064 times.
✓ Branch 1 taken 508032 times.
|
516096 | if (level == 64) // positive levels have a range of 1..63 |
1152 | 8064 | break; | |
1153 | 508032 | bits_tab[UNI_MPEG4_ENC_INDEX(0, run, level)] = code | level << 1; | |
1154 | 508032 | bits_tab[UNI_MPEG4_ENC_INDEX(1, run, level)] = | |
1155 | 508032 | bits_tab[UNI_MPEG4_ENC_INDEX(0, run, level)] | (1 << 20) /* last */; | |
1156 | } | ||
1157 | // Is this needed at all? | ||
1158 | 8064 | len_tab[UNI_MPEG4_ENC_INDEX(0, run, 0)] = | |
1159 | 8064 | len_tab[UNI_MPEG4_ENC_INDEX(1, run, 0)] = 0; | |
1160 | } | ||
1161 | |||
1162 | 126 | uint8_t max_run[2][32] = { 0 }; | |
1163 | |||
1164 | #define VLC_NUM_CODES 102 // excluding the escape | ||
1165 | av_assert2(rl->n == VLC_NUM_CODES); | ||
1166 |
2/2✓ Branch 0 taken 12852 times.
✓ Branch 1 taken 126 times.
|
12978 | for (int i = VLC_NUM_CODES - 1, max_level, cur_run = 0; i >= 0; --i) { |
1167 | 12852 | int run = rl->table_run[i], level = rl->table_level[i]; | |
1168 | 12852 | int last = i >= rl->last; | |
1169 | 12852 | unsigned code = rl->table_vlc[i][0] << 1; | |
1170 | 12852 | int len = rl->table_vlc[i][1] + 1; | |
1171 | |||
1172 | 12852 | bits_tab[UNI_MPEG4_ENC_INDEX(last, run, level)] = code; | |
1173 | 12852 | len_tab [UNI_MPEG4_ENC_INDEX(last, run, level)] = len; | |
1174 | 12852 | bits_tab[UNI_MPEG4_ENC_INDEX(last, run, -level)] = code | 1; | |
1175 | 12852 | len_tab [UNI_MPEG4_ENC_INDEX(last, run, -level)] = len; | |
1176 | |||
1177 |
2/2✓ Branch 0 taken 3150 times.
✓ Branch 1 taken 9702 times.
|
12852 | if (!max_run[last][level]) |
1178 | 3150 | max_run[last][level] = run + 1; | |
1179 | av_assert2(run + 1 <= max_run[last][level]); | ||
1180 | |||
1181 | 12852 | int run3 = run + max_run[last][level]; | |
1182 | 12852 | int len3 = len + 7 + 2; | |
1183 | |||
1184 |
4/4✓ Branch 0 taken 11718 times.
✓ Branch 1 taken 1134 times.
✓ Branch 2 taken 7812 times.
✓ Branch 3 taken 3906 times.
|
12852 | if (run3 < 64 && len3 < len_tab[UNI_MPEG4_ENC_INDEX(last, run3, level)]) { |
1185 | 7812 | unsigned code3 = code | (0x3 << 2 | 0x2) << len; | |
1186 | 7812 | bits_tab[UNI_MPEG4_ENC_INDEX(last, run3, level)] = code3; | |
1187 | 7812 | len_tab [UNI_MPEG4_ENC_INDEX(last, run3, level)] = len3; | |
1188 | 7812 | bits_tab[UNI_MPEG4_ENC_INDEX(last, run3, -level)] = code3 | 1; | |
1189 | 7812 | len_tab [UNI_MPEG4_ENC_INDEX(last, run3, -level)] = len3; | |
1190 | } | ||
1191 | // table_run and table_level are ordered so that all the entries | ||
1192 | // with the same last and run are consecutive and level is ascending | ||
1193 | // among these entries. By traversing downwards we therefore automatically | ||
1194 | // encounter max_level of a given run first, needed for escape method 1. | ||
1195 |
2/2✓ Branch 0 taken 6552 times.
✓ Branch 1 taken 6300 times.
|
12852 | if (run != cur_run) { |
1196 | 6552 | max_level = level; | |
1197 | 6552 | cur_run = run; | |
1198 | } else | ||
1199 | av_assert2(max_level > level); | ||
1200 | |||
1201 | 12852 | code |= 0x3 << (len + 1); | |
1202 | 12852 | len += 7 + 1; | |
1203 | 12852 | level += max_level; | |
1204 | av_assert2(len_tab [UNI_MPEG4_ENC_INDEX(last, run, level)] >= len); | ||
1205 | 12852 | bits_tab[UNI_MPEG4_ENC_INDEX(last, run, level)] = code; | |
1206 | 12852 | len_tab [UNI_MPEG4_ENC_INDEX(last, run, level)] = len; | |
1207 | 12852 | bits_tab[UNI_MPEG4_ENC_INDEX(last, run, -level)] = code | 1; | |
1208 | 12852 | len_tab [UNI_MPEG4_ENC_INDEX(last, run, -level)] = len; | |
1209 | } | ||
1210 | 126 | } | |
1211 | |||
1212 | 63 | static av_cold void mpeg4_encode_init_static(void) | |
1213 | { | ||
1214 | 63 | init_uni_dc_tab(); | |
1215 | |||
1216 | 63 | init_uni_mpeg4_rl_tab(&ff_mpeg4_rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len); | |
1217 | 63 | init_uni_mpeg4_rl_tab(&ff_h263_rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len); | |
1218 | |||
1219 |
2/2✓ Branch 0 taken 441 times.
✓ Branch 1 taken 63 times.
|
504 | for (int f_code = MAX_FCODE; f_code > 0; f_code--) { |
1220 |
2/2✓ Branch 0 taken 512064 times.
✓ Branch 1 taken 441 times.
|
512505 | for (int mv = -(16 << f_code); mv < (16 << f_code); mv++) |
1221 | 512064 | fcode_tab[mv + MAX_MV] = f_code; | |
1222 | } | ||
1223 | 63 | } | |
1224 | |||
1225 | 63 | static av_cold int encode_init(AVCodecContext *avctx) | |
1226 | { | ||
1227 | static AVOnce init_static_once = AV_ONCE_INIT; | ||
1228 | 63 | Mpeg4EncContext *const m4 = avctx->priv_data; | |
1229 | 63 | MPVMainEncContext *const m = &m4->m; | |
1230 | 63 | MPVEncContext *const s = &m->s; | |
1231 | int ret; | ||
1232 | |||
1233 |
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)) { |
1234 | ✗ | av_log(avctx, AV_LOG_ERROR, "dimensions too large for MPEG-4\n"); | |
1235 | ✗ | return AVERROR(EINVAL); | |
1236 | } | ||
1237 | |||
1238 | 63 | m->encode_picture_header = mpeg4_encode_picture_header; | |
1239 | 63 | s->encode_mb = mpeg4_encode_mb; | |
1240 | |||
1241 | 63 | m->fcode_tab = fcode_tab + MAX_MV; | |
1242 | |||
1243 | 63 | s->min_qcoeff = -2048; | |
1244 | 63 | s->max_qcoeff = 2047; | |
1245 | 63 | s->intra_ac_vlc_length = uni_mpeg4_intra_rl_len; | |
1246 | 63 | s->intra_ac_vlc_last_length = uni_mpeg4_intra_rl_len + 128 * 64; | |
1247 | 63 | s->inter_ac_vlc_length = uni_mpeg4_inter_rl_len; | |
1248 | 63 | s->inter_ac_vlc_last_length = uni_mpeg4_inter_rl_len + 128 * 64; | |
1249 | 63 | s->luma_dc_vlc_length = uni_DCtab_lum_len; | |
1250 | 63 | s->ac_esc_length = 7 + 2 + 1 + 6 + 1 + 12 + 1; | |
1251 | 63 | s->c.y_dc_scale_table = ff_mpeg4_y_dc_scale_table; | |
1252 | 63 | s->c.c_dc_scale_table = ff_mpeg4_c_dc_scale_table; | |
1253 | |||
1254 | 63 | ff_qpeldsp_init(&s->c.qdsp); | |
1255 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
|
63 | if ((ret = ff_mpv_encode_init(avctx)) < 0) |
1256 | ✗ | return ret; | |
1257 | |||
1258 | 63 | ff_thread_once(&init_static_once, mpeg4_encode_init_static); | |
1259 | |||
1260 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
|
63 | if (avctx->time_base.den > (1 << 16) - 1) { |
1261 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
1262 | "timebase %d/%d not supported by MPEG 4 standard, " | ||
1263 | "the maximum admitted value for the timebase denominator " | ||
1264 | "is %d\n", avctx->time_base.num, avctx->time_base.den, | ||
1265 | (1 << 16) - 1); | ||
1266 | ✗ | return AVERROR(EINVAL); | |
1267 | } | ||
1268 | |||
1269 | 63 | m4->time_increment_bits = av_log2(avctx->time_base.den - 1) + 1; | |
1270 | |||
1271 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 42 times.
|
63 | if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) { |
1272 | 21 | avctx->extradata = av_malloc(1024); | |
1273 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (!avctx->extradata) |
1274 | ✗ | return AVERROR(ENOMEM); | |
1275 | 21 | init_put_bits(&s->pb, avctx->extradata, 1024); | |
1276 | |||
1277 | 21 | mpeg4_encode_visual_object_header(m); | |
1278 | 21 | mpeg4_encode_vol_header(m4, 0, 0); | |
1279 | |||
1280 | // ff_mpeg4_stuffing(&s->pb); ? | ||
1281 | 21 | flush_put_bits(&s->pb); | |
1282 | 21 | avctx->extradata_size = put_bytes_output(&s->pb); | |
1283 | } | ||
1284 | 63 | return 0; | |
1285 | } | ||
1286 | |||
1287 | 1760 | void ff_mpeg4_init_partitions(MPVEncContext *const s) | |
1288 | { | ||
1289 | 1760 | uint8_t *start = put_bits_ptr(&s->pb); | |
1290 | 1760 | uint8_t *end = s->pb.buf_end; | |
1291 | 1760 | int size = end - start; | |
1292 | 1760 | int pb_size = (((intptr_t)start + size / 3) & (~3)) - (intptr_t)start; | |
1293 | 1760 | int tex_size = (size - 2 * pb_size) & (~3); | |
1294 | |||
1295 | 1760 | set_put_bits_buffer_size(&s->pb, pb_size); | |
1296 | 1760 | init_put_bits(&s->tex_pb, start + pb_size, tex_size); | |
1297 | 1760 | init_put_bits(&s->pb2, start + pb_size + tex_size, pb_size); | |
1298 | 1760 | } | |
1299 | |||
1300 | 1760 | void ff_mpeg4_merge_partitions(MPVEncContext *const s) | |
1301 | { | ||
1302 | 1760 | const int pb2_len = put_bits_count(&s->pb2); | |
1303 | 1760 | const int tex_pb_len = put_bits_count(&s->tex_pb); | |
1304 | 1760 | const int bits = put_bits_count(&s->pb); | |
1305 | |||
1306 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 1248 times.
|
1760 | if (s->c.pict_type == AV_PICTURE_TYPE_I) { |
1307 | 512 | put_bits(&s->pb, 19, DC_MARKER); | |
1308 | 512 | s->misc_bits += 19 + pb2_len + bits - s->last_bits; | |
1309 | 512 | s->i_tex_bits += tex_pb_len; | |
1310 | } else { | ||
1311 | 1248 | put_bits(&s->pb, 17, MOTION_MARKER); | |
1312 | 1248 | s->misc_bits += 17 + pb2_len; | |
1313 | 1248 | s->mv_bits += bits - s->last_bits; | |
1314 | 1248 | s->p_tex_bits += tex_pb_len; | |
1315 | } | ||
1316 | |||
1317 | 1760 | flush_put_bits(&s->pb2); | |
1318 | 1760 | flush_put_bits(&s->tex_pb); | |
1319 | |||
1320 | 1760 | set_put_bits_buffer_size(&s->pb, s->pb2.buf_end - s->pb.buf); | |
1321 | 1760 | ff_copy_bits(&s->pb, s->pb2.buf, pb2_len); | |
1322 | 1760 | ff_copy_bits(&s->pb, s->tex_pb.buf, tex_pb_len); | |
1323 | 1760 | s->last_bits = put_bits_count(&s->pb); | |
1324 | 1760 | } | |
1325 | |||
1326 | 3323 | void ff_mpeg4_encode_video_packet_header(MPVEncContext *const s) | |
1327 | { | ||
1328 | 3323 | int mb_num_bits = av_log2(s->c.mb_num - 1) + 1; | |
1329 | |||
1330 | 3323 | put_bits(&s->pb, ff_mpeg4_get_video_packet_prefix_length(s->c.pict_type, s->f_code, s->b_code), 0); | |
1331 | 3323 | put_bits(&s->pb, 1, 1); | |
1332 | |||
1333 | 3323 | put_bits(&s->pb, mb_num_bits, s->c.mb_x + s->c.mb_y * s->c.mb_width); | |
1334 | 3323 | put_bits(&s->pb, 5 /* quant_precision */, s->c.qscale); | |
1335 | 3323 | put_bits(&s->pb, 1, 0); /* no HEC */ | |
1336 | 3323 | } | |
1337 | |||
1338 | #define OFFSET(x) offsetof(MPVEncContext, x) | ||
1339 | #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM | ||
1340 | static const AVOption options[] = { | ||
1341 | { "data_partitioning", "Use data partitioning.", FF_MPV_OFFSET(data_partitioning), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, | ||
1342 | { "alternate_scan", "Enable alternate scantable.", OFFSET(c.alternate_scan), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, | ||
1343 | { "mpeg_quant", "Use MPEG quantizers instead of H.263", | ||
1344 | OFFSET(mpeg_quant), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, VE }, | ||
1345 | FF_MPV_COMMON_BFRAME_OPTS | ||
1346 | FF_MPV_COMMON_OPTS | ||
1347 | FF_MPV_COMMON_MOTION_EST_OPTS | ||
1348 | FF_MPEG4_PROFILE_OPTS | ||
1349 | { NULL }, | ||
1350 | }; | ||
1351 | |||
1352 | static const AVClass mpeg4enc_class = { | ||
1353 | .class_name = "MPEG4 encoder", | ||
1354 | .item_name = av_default_item_name, | ||
1355 | .option = options, | ||
1356 | .version = LIBAVUTIL_VERSION_INT, | ||
1357 | }; | ||
1358 | |||
1359 | const FFCodec ff_mpeg4_encoder = { | ||
1360 | .p.name = "mpeg4", | ||
1361 | CODEC_LONG_NAME("MPEG-4 part 2"), | ||
1362 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
1363 | .p.id = AV_CODEC_ID_MPEG4, | ||
1364 | .priv_data_size = sizeof(Mpeg4EncContext), | ||
1365 | .init = encode_init, | ||
1366 | FF_CODEC_ENCODE_CB(ff_mpv_encode_picture), | ||
1367 | .close = ff_mpv_encode_end, | ||
1368 | CODEC_PIXFMTS(AV_PIX_FMT_YUV420P), | ||
1369 | .color_ranges = AVCOL_RANGE_MPEG, | ||
1370 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | | ||
1371 | AV_CODEC_CAP_SLICE_THREADS | | ||
1372 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
1373 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
1374 | .p.priv_class = &mpeg4enc_class, | ||
1375 | }; | ||
1376 |