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