Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * ITU H.263 bitstream encoder | ||
3 | * Copyright (c) 2000,2001 Fabrice Bellard | ||
4 | * H.263+ support. | ||
5 | * Copyright (c) 2001 Juan J. Sierralta P | ||
6 | * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> | ||
7 | * | ||
8 | * This file is part of FFmpeg. | ||
9 | * | ||
10 | * FFmpeg is free software; you can redistribute it and/or | ||
11 | * modify it under the terms of the GNU Lesser General Public | ||
12 | * License as published by the Free Software Foundation; either | ||
13 | * version 2.1 of the License, or (at your option) any later version. | ||
14 | * | ||
15 | * FFmpeg is distributed in the hope that it will be useful, | ||
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
18 | * Lesser General Public License for more details. | ||
19 | * | ||
20 | * You should have received a copy of the GNU Lesser General Public | ||
21 | * License along with FFmpeg; if not, write to the Free Software | ||
22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
23 | */ | ||
24 | |||
25 | /** | ||
26 | * @file | ||
27 | * H.263 bitstream encoder. | ||
28 | */ | ||
29 | |||
30 | #include "config_components.h" | ||
31 | |||
32 | #include <limits.h> | ||
33 | #include <string.h> | ||
34 | |||
35 | #include "libavutil/attributes.h" | ||
36 | #include "libavutil/thread.h" | ||
37 | #include "avcodec.h" | ||
38 | #include "codec_internal.h" | ||
39 | #include "mpegvideo.h" | ||
40 | #include "flvenc.h" | ||
41 | #include "mpegvideoenc.h" | ||
42 | #include "h263.h" | ||
43 | #include "h263enc.h" | ||
44 | #include "h263data.h" | ||
45 | #include "h263dsp.h" | ||
46 | #include "mathops.h" | ||
47 | #include "mpegutils.h" | ||
48 | #include "internal.h" | ||
49 | #include "put_bits.h" | ||
50 | |||
51 | /** | ||
52 | * Table of number of bits a motion vector component needs. | ||
53 | */ | ||
54 | static uint8_t mv_penalty[MAX_FCODE+1][MAX_DMV*2+1]; | ||
55 | |||
56 | 118 | static av_cold void init_mv_penalty(void) | |
57 | { | ||
58 |
2/2✓ Branch 0 taken 826 times.
✓ Branch 1 taken 118 times.
|
944 | for (int f_code = 1; f_code <= MAX_FCODE; f_code++) { |
59 |
2/2✓ Branch 0 taken 13534010 times.
✓ Branch 1 taken 826 times.
|
13534836 | for (int mv = -MAX_DMV; mv <= MAX_DMV; mv++) { |
60 | int len; | ||
61 | |||
62 |
2/2✓ Branch 0 taken 826 times.
✓ Branch 1 taken 13533184 times.
|
13534010 | if (mv == 0) len = 1; // ff_mvtab[0][1] |
63 | else { | ||
64 | int val, bit_size, code; | ||
65 | |||
66 | 13533184 | bit_size = f_code - 1; | |
67 | |||
68 | 13533184 | val = mv; | |
69 |
2/2✓ Branch 0 taken 6766592 times.
✓ Branch 1 taken 6766592 times.
|
13533184 | if (val < 0) |
70 | 6766592 | val = -val; | |
71 | 13533184 | val--; | |
72 | 13533184 | code = (val >> bit_size) + 1; | |
73 |
2/2✓ Branch 0 taken 959104 times.
✓ Branch 1 taken 12574080 times.
|
13533184 | if (code < 33) { |
74 | 959104 | len = ff_mvtab[code][1] + 1 + bit_size; | |
75 | } else { | ||
76 | 12574080 | len = 12 /* ff_mvtab[32][1] */ + av_log2(code>>5) + 2 + bit_size; | |
77 | } | ||
78 | } | ||
79 | |||
80 | 13534010 | mv_penalty[f_code][mv + MAX_DMV] = len; | |
81 | } | ||
82 | } | ||
83 | 118 | } | |
84 | |||
85 | #if CONFIG_H263_ENCODER | ||
86 | /** | ||
87 | * Minimal fcode that a motion vector component would need in umv. | ||
88 | * All entries in this table are 1. | ||
89 | */ | ||
90 | static uint8_t umv_fcode_tab[MAX_MV*2+1]; | ||
91 | |||
92 | //unified encoding tables for run length encoding of coefficients | ||
93 | //unified in the sense that the specification specifies the encoding in several steps. | ||
94 | static uint8_t uni_h263_intra_aic_rl_len [64*64*2*2]; | ||
95 | static uint8_t uni_h263_inter_rl_len [64*64*2*2]; | ||
96 | //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level)) | ||
97 | //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64) | ||
98 | #define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level)) | ||
99 | |||
100 | 236 | static av_cold void init_uni_h263_rl_tab(const RLTable *rl, uint8_t *len_tab) | |
101 | { | ||
102 | av_assert0(MAX_LEVEL >= 64); | ||
103 | av_assert0(MAX_RUN >= 63); | ||
104 | |||
105 |
2/2✓ Branch 0 taken 30208 times.
✓ Branch 1 taken 236 times.
|
30444 | for (int slevel = -64; slevel < 64; slevel++) { |
106 |
2/2✓ Branch 0 taken 236 times.
✓ Branch 1 taken 29972 times.
|
30208 | if (slevel == 0) continue; |
107 |
2/2✓ Branch 0 taken 1918208 times.
✓ Branch 1 taken 29972 times.
|
1948180 | for (int run = 0; run < 64; run++) { |
108 |
2/2✓ Branch 0 taken 3836416 times.
✓ Branch 1 taken 1918208 times.
|
5754624 | for (int last = 0; last <= 1; last++) { |
109 | 3836416 | const int index = UNI_MPEG4_ENC_INDEX(last, run, slevel + 64); | |
110 | 3836416 | int level = slevel < 0 ? -slevel : slevel; | |
111 | 3836416 | int sign = slevel < 0 ? 1 : 0; | |
112 | int bits, len, code; | ||
113 | |||
114 | 3836416 | len_tab[index] = 100; | |
115 | |||
116 | /* ESC0 */ | ||
117 | 3836416 | code = get_rl_index(rl, last, run, level); | |
118 | 3836416 | bits = rl->table_vlc[code][0]; | |
119 | 3836416 | len = rl->table_vlc[code][1]; | |
120 | 3836416 | bits = bits * 2 + sign; | |
121 | 3836416 | len++; | |
122 | |||
123 |
3/4✓ Branch 0 taken 48144 times.
✓ Branch 1 taken 3788272 times.
✓ Branch 2 taken 48144 times.
✗ Branch 3 not taken.
|
3836416 | if (code != rl->n && len < len_tab[index]) |
124 | 48144 | len_tab[index] = len; | |
125 | |||
126 | /* ESC */ | ||
127 | 3836416 | bits = rl->table_vlc[rl->n][0]; | |
128 | 3836416 | len = rl->table_vlc[rl->n][1]; | |
129 | 3836416 | bits = bits * 2 + last; len++; | |
130 | 3836416 | bits = bits * 64 + run; len += 6; | |
131 | 3836416 | bits = bits * 256 + (level & 0xff); len += 8; | |
132 | |||
133 |
2/2✓ Branch 0 taken 3788272 times.
✓ Branch 1 taken 48144 times.
|
3836416 | if (len < len_tab[index]) |
134 | 3788272 | len_tab[index] = len; | |
135 | } | ||
136 | } | ||
137 | } | ||
138 | 236 | } | |
139 | #endif | ||
140 | |||
141 | 118 | static av_cold void h263_encode_init_static(void) | |
142 | { | ||
143 | #if CONFIG_H263_ENCODER | ||
144 | static uint8_t rl_intra_table[2][2 * MAX_RUN + MAX_LEVEL + 3]; | ||
145 | 118 | ff_rl_init(&ff_rl_intra_aic, rl_intra_table); | |
146 | 118 | ff_h263_init_rl_inter(); | |
147 | |||
148 | 118 | init_uni_h263_rl_tab(&ff_rl_intra_aic, uni_h263_intra_aic_rl_len); | |
149 | 118 | init_uni_h263_rl_tab(&ff_h263_rl_inter, uni_h263_inter_rl_len); | |
150 | |||
151 | 118 | memset(umv_fcode_tab, 1, sizeof(umv_fcode_tab)); | |
152 | #endif | ||
153 | |||
154 | 118 | init_mv_penalty(); | |
155 | 118 | } | |
156 | |||
157 | 118 | av_cold const uint8_t (*ff_h263_get_mv_penalty(void))[MAX_DMV*2+1] | |
158 | { | ||
159 | static AVOnce init_static_once = AV_ONCE_INIT; | ||
160 | |||
161 | 118 | ff_thread_once(&init_static_once, h263_encode_init_static); | |
162 | |||
163 | 118 | return mv_penalty; | |
164 | } | ||
165 | |||
166 | 5426130 | void ff_h263_encode_motion(PutBitContext *pb, int val, int f_code) | |
167 | { | ||
168 |
2/2✓ Branch 0 taken 2264719 times.
✓ Branch 1 taken 3161411 times.
|
5426130 | if (val == 0) { |
169 | /* zero vector -- corresponds to ff_mvtab[0] */ | ||
170 | 2264719 | put_bits(pb, 1, 1); | |
171 | } else { | ||
172 | int sign, code, bits; | ||
173 | 3161411 | int bit_size = f_code - 1; | |
174 | 3161411 | int range = 1 << bit_size; | |
175 | /* modulo encoding */ | ||
176 | 3161411 | val = sign_extend(val, 6 + bit_size); | |
177 | 3161411 | sign = val>>31; | |
178 | 3161411 | val= (val^sign)-sign; | |
179 | 3161411 | sign&=1; | |
180 | |||
181 | 3161411 | val--; | |
182 | 3161411 | code = (val >> bit_size) + 1; | |
183 | 3161411 | bits = val & (range - 1); | |
184 | |||
185 | 3161411 | put_bits(pb, ff_mvtab[code][1] + 1, (ff_mvtab[code][0] << 1) | sign); | |
186 |
2/2✓ Branch 0 taken 367240 times.
✓ Branch 1 taken 2794171 times.
|
3161411 | if (bit_size > 0) { |
187 | 367240 | put_bits(pb, bit_size, bits); | |
188 | } | ||
189 | } | ||
190 | 5426130 | } | |
191 | |||
192 | #if CONFIG_H263_ENCODER // Snow and SVQ1 need the above | ||
193 | static const uint8_t wrong_run[102] = { | ||
194 | 1, 2, 3, 5, 4, 10, 9, 8, | ||
195 | 11, 15, 17, 16, 23, 22, 21, 20, | ||
196 | 19, 18, 25, 24, 27, 26, 11, 7, | ||
197 | 6, 1, 2, 13, 2, 2, 2, 2, | ||
198 | 6, 12, 3, 9, 1, 3, 4, 3, | ||
199 | 7, 4, 1, 1, 5, 5, 14, 6, | ||
200 | 1, 7, 1, 8, 1, 1, 1, 1, | ||
201 | 10, 1, 1, 5, 9, 17, 25, 24, | ||
202 | 29, 33, 32, 41, 2, 23, 28, 31, | ||
203 | 3, 22, 30, 4, 27, 40, 8, 26, | ||
204 | 6, 39, 7, 38, 16, 37, 15, 10, | ||
205 | 11, 12, 13, 14, 1, 21, 20, 18, | ||
206 | 19, 2, 1, 34, 35, 36 | ||
207 | }; | ||
208 | |||
209 | /** | ||
210 | * Return the 4 bit value that specifies the given aspect ratio. | ||
211 | * This may be one of the standard aspect ratios or it specifies | ||
212 | * that the aspect will be stored explicitly later. | ||
213 | */ | ||
214 | 222 | av_const int ff_h263_aspect_to_info(AVRational aspect){ | |
215 | int i; | ||
216 | |||
217 |
3/4✓ Branch 0 taken 9 times.
✓ Branch 1 taken 213 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
|
222 | if(aspect.num==0 || aspect.den==0) aspect= (AVRational){1,1}; |
218 | |||
219 |
1/2✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
|
222 | for(i=1; i<6; i++){ |
220 |
1/2✓ Branch 1 taken 222 times.
✗ Branch 2 not taken.
|
222 | if(av_cmp_q(ff_h263_pixel_aspect[i], aspect) == 0){ |
221 | 222 | return i; | |
222 | } | ||
223 | } | ||
224 | |||
225 | ✗ | return FF_ASPECT_EXTENDED; | |
226 | } | ||
227 | |||
228 | 450 | static int h263_encode_picture_header(MPVMainEncContext *const m) | |
229 | { | ||
230 | 450 | MPVEncContext *const s = &m->s; | |
231 | int format, coded_frame_rate, coded_frame_rate_base, i, temp_ref; | ||
232 | 450 | int best_clock_code=1; | |
233 | 450 | int best_divisor=60; | |
234 | 450 | int best_error= INT_MAX; | |
235 | int custom_pcf; | ||
236 | |||
237 | 450 | put_bits_assume_flushed(&s->pb); | |
238 | |||
239 |
2/2✓ Branch 0 taken 150 times.
✓ Branch 1 taken 300 times.
|
450 | if (s->c.codec_id == AV_CODEC_ID_H263P) { |
240 |
2/2✓ Branch 0 taken 300 times.
✓ Branch 1 taken 150 times.
|
450 | for(i=0; i<2; i++){ |
241 | int div, error; | ||
242 | 300 | div= (s->c.avctx->time_base.num*1800000LL + 500LL*s->c.avctx->time_base.den) / ((1000LL+i)*s->c.avctx->time_base.den); | |
243 | 300 | div= av_clip(div, 1, 127); | |
244 |
2/2✓ Branch 0 taken 150 times.
✓ Branch 1 taken 150 times.
|
300 | error= FFABS(s->c.avctx->time_base.num*1800000LL - (1000LL+i)*s->c.avctx->time_base.den*div); |
245 |
2/2✓ Branch 0 taken 150 times.
✓ Branch 1 taken 150 times.
|
300 | if(error < best_error){ |
246 | 150 | best_error= error; | |
247 | 150 | best_divisor= div; | |
248 | 150 | best_clock_code= i; | |
249 | } | ||
250 | } | ||
251 | } | ||
252 |
3/4✓ Branch 0 taken 300 times.
✓ Branch 1 taken 150 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 300 times.
|
450 | custom_pcf = best_clock_code != 1 || best_divisor != 60; |
253 | 450 | coded_frame_rate= 1800000; | |
254 | 450 | coded_frame_rate_base= (1000+best_clock_code)*best_divisor; | |
255 | |||
256 | 450 | put_bits(&s->pb, 22, 0x20); /* PSC */ | |
257 | 450 | temp_ref= s->c.picture_number * (int64_t)coded_frame_rate * s->c.avctx->time_base.num / //FIXME use timestamp | |
258 | 450 | (coded_frame_rate_base * (int64_t)s->c.avctx->time_base.den); | |
259 | 450 | put_sbits(&s->pb, 8, temp_ref); /* TemporalReference */ | |
260 | |||
261 | 450 | put_bits(&s->pb, 1, 1); /* marker */ | |
262 | 450 | put_bits(&s->pb, 1, 0); /* H.263 id */ | |
263 | 450 | put_bits(&s->pb, 1, 0); /* split screen off */ | |
264 | 450 | put_bits(&s->pb, 1, 0); /* camera off */ | |
265 | 450 | put_bits(&s->pb, 1, 0); /* freeze picture release off */ | |
266 | |||
267 | 450 | format = ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format), s->c.width, s->c.height); | |
268 |
2/2✓ Branch 0 taken 300 times.
✓ Branch 1 taken 150 times.
|
450 | if (s->c.codec_id != AV_CODEC_ID_H263P) { |
269 | /* H.263v1 */ | ||
270 | 300 | put_bits(&s->pb, 3, format); | |
271 | 300 | put_bits(&s->pb, 1, (s->c.pict_type == AV_PICTURE_TYPE_P)); | |
272 | /* By now UMV IS DISABLED ON H.263v1, since the restrictions | ||
273 | of H.263v1 UMV implies to check the predicted MV after | ||
274 | calculation of the current MB to see if we're on the limits */ | ||
275 | 300 | put_bits(&s->pb, 1, 0); /* Unrestricted Motion Vector: off */ | |
276 | 300 | put_bits(&s->pb, 1, 0); /* SAC: off */ | |
277 | 300 | put_bits(&s->pb, 1, s->c.obmc); /* Advanced Prediction */ | |
278 | 300 | put_bits(&s->pb, 1, 0); /* only I/P-frames, no PB-frame */ | |
279 | 300 | put_bits(&s->pb, 5, s->c.qscale); | |
280 | 300 | put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */ | |
281 | } else { | ||
282 | 150 | int ufep=1; | |
283 | /* H.263v2 */ | ||
284 | /* H.263 Plus PTYPE */ | ||
285 | |||
286 | 150 | put_bits(&s->pb, 3, 7); | |
287 | 150 | put_bits(&s->pb,3,ufep); /* Update Full Extended PTYPE */ | |
288 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
|
150 | if (format == 8) |
289 | ✗ | put_bits(&s->pb,3,6); /* Custom Source Format */ | |
290 | else | ||
291 | 150 | put_bits(&s->pb, 3, format); | |
292 | |||
293 | 150 | put_bits(&s->pb,1, custom_pcf); | |
294 | 150 | put_bits(&s->pb,1, s->c.umvplus); /* Unrestricted Motion Vector */ | |
295 | 150 | put_bits(&s->pb,1,0); /* SAC: off */ | |
296 | 150 | put_bits(&s->pb,1,s->c.obmc); /* Advanced Prediction Mode */ | |
297 | 150 | put_bits(&s->pb,1,s->c.h263_aic); /* Advanced Intra Coding */ | |
298 | 150 | put_bits(&s->pb,1,s->c.loop_filter); /* Deblocking Filter */ | |
299 | 150 | put_bits(&s->pb,1,s->c.h263_slice_structured); /* Slice Structured */ | |
300 | 150 | put_bits(&s->pb,1,0); /* Reference Picture Selection: off */ | |
301 | 150 | put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */ | |
302 | 150 | put_bits(&s->pb,1,s->c.alt_inter_vlc); /* Alternative Inter VLC */ | |
303 | 150 | put_bits(&s->pb,1,s->c.modified_quant); /* Modified Quantization: */ | |
304 | 150 | put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */ | |
305 | 150 | put_bits(&s->pb,3,0); /* Reserved */ | |
306 | |||
307 | 150 | put_bits(&s->pb, 3, s->c.pict_type == AV_PICTURE_TYPE_P); | |
308 | |||
309 | 150 | put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */ | |
310 | 150 | put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */ | |
311 | 150 | put_bits(&s->pb,1,s->c.no_rounding); /* Rounding Type */ | |
312 | 150 | put_bits(&s->pb,2,0); /* Reserved */ | |
313 | 150 | put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */ | |
314 | |||
315 | /* This should be here if PLUSPTYPE */ | ||
316 | 150 | put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */ | |
317 | |||
318 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
|
150 | if (format == 8) { |
319 | /* Custom Picture Format (CPFMT) */ | ||
320 | ✗ | unsigned aspect_ratio_info = ff_h263_aspect_to_info(s->c.avctx->sample_aspect_ratio); | |
321 | |||
322 | ✗ | put_bits(&s->pb,4, aspect_ratio_info); | |
323 | ✗ | put_bits(&s->pb,9,(s->c.width >> 2) - 1); | |
324 | ✗ | put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */ | |
325 | ✗ | put_bits(&s->pb,9,(s->c.height >> 2)); | |
326 | ✗ | if (aspect_ratio_info == FF_ASPECT_EXTENDED){ | |
327 | ✗ | put_bits(&s->pb, 8, s->c.avctx->sample_aspect_ratio.num); | |
328 | ✗ | put_bits(&s->pb, 8, s->c.avctx->sample_aspect_ratio.den); | |
329 | } | ||
330 | } | ||
331 |
1/2✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
|
150 | if (custom_pcf) { |
332 |
1/2✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
|
150 | if(ufep){ |
333 | 150 | put_bits(&s->pb, 1, best_clock_code); | |
334 | 150 | put_bits(&s->pb, 7, best_divisor); | |
335 | } | ||
336 | 150 | put_sbits(&s->pb, 2, temp_ref>>8); | |
337 | } | ||
338 | |||
339 | /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */ | ||
340 |
1/2✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
|
150 | if (s->c.umvplus) |
341 | // put_bits(&s->pb,1,1); /* Limited according tables of Annex D */ | ||
342 | //FIXME check actual requested range | ||
343 | 150 | put_bits(&s->pb,2,1); /* unlimited */ | |
344 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
|
150 | if(s->c.h263_slice_structured) |
345 | ✗ | put_bits(&s->pb,2,0); /* no weird submodes */ | |
346 | |||
347 | 150 | put_bits(&s->pb, 5, s->c.qscale); | |
348 | } | ||
349 | |||
350 | 450 | put_bits(&s->pb, 1, 0); /* no PEI */ | |
351 | |||
352 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
|
450 | if(s->c.h263_slice_structured){ |
353 | ✗ | put_bits(&s->pb, 1, 1); | |
354 | |||
355 | av_assert1(s->c.mb_x == 0 && s->c.mb_y == 0); | ||
356 | ✗ | ff_h263_encode_mba(s); | |
357 | |||
358 | ✗ | put_bits(&s->pb, 1, 1); | |
359 | } | ||
360 | |||
361 | 450 | return 0; | |
362 | } | ||
363 | |||
364 | 5867 | void ff_h263_mpeg4_reset_dc(MPVEncContext *s) | |
365 | { | ||
366 | 5867 | int16_t *dc = s->c.dc_val; | |
367 | |||
368 | // The "- 1" is for the top-left entry | ||
369 | 5867 | const int l_xy = s->c.block_index[2]; | |
370 |
2/2✓ Branch 0 taken 263498 times.
✓ Branch 1 taken 5867 times.
|
269365 | for (int i = l_xy - 2 * s->c.b8_stride - 1; i < l_xy; i += 2) |
371 | 263498 | AV_WN32A(dc + i, 1024 << 16 | 1024); | |
372 | |||
373 | 5867 | const int u_xy = s->c.block_index[4]; | |
374 | 5867 | const int v_xy = s->c.block_index[5]; | |
375 | 5867 | int16_t *dc2 = dc + v_xy - u_xy; | |
376 |
2/2✓ Branch 0 taken 137616 times.
✓ Branch 1 taken 5867 times.
|
143483 | for (int i = u_xy - s->c.mb_stride - 1; i < u_xy; ++i) |
377 | 137616 | dc[i] = dc2[i] = 1024; | |
378 | 5867 | } | |
379 | |||
380 | /** | ||
381 | * Encode a group of blocks header. | ||
382 | */ | ||
383 | 2544 | void ff_h263_encode_gob_header(MPVEncContext *const s, int mb_line) | |
384 | { | ||
385 | 2544 | put_bits(&s->pb, 17, 1); /* GBSC */ | |
386 | |||
387 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2544 times.
|
2544 | if(s->c.h263_slice_structured){ |
388 | ✗ | put_bits(&s->pb, 1, 1); | |
389 | |||
390 | ✗ | ff_h263_encode_mba(s); | |
391 | |||
392 | ✗ | if(s->c.mb_num > 1583) | |
393 | ✗ | put_bits(&s->pb, 1, 1); | |
394 | ✗ | put_bits(&s->pb, 5, s->c.qscale); /* GQUANT */ | |
395 | ✗ | put_bits(&s->pb, 1, 1); | |
396 | ✗ | put_bits(&s->pb, 2, s->c.pict_type == AV_PICTURE_TYPE_I); /* GFID */ | |
397 | }else{ | ||
398 | 2544 | int gob_number= mb_line / s->c.gob_index; | |
399 | |||
400 | 2544 | put_bits(&s->pb, 5, gob_number); /* GN */ | |
401 | 2544 | put_bits(&s->pb, 2, s->c.pict_type == AV_PICTURE_TYPE_I); /* GFID */ | |
402 | 2544 | put_bits(&s->pb, 5, s->c.qscale); /* GQUANT */ | |
403 | } | ||
404 | 2544 | } | |
405 | |||
406 | /** | ||
407 | * modify qscale so that encoding is actually possible in H.263 (limit difference to -2..2) | ||
408 | */ | ||
409 | 400 | void ff_clean_h263_qscales(MPVEncContext *const s) | |
410 | { | ||
411 | 400 | int8_t * const qscale_table = s->c.cur_pic.qscale_table; | |
412 | |||
413 |
2/2✓ Branch 0 taken 119300 times.
✓ Branch 1 taken 400 times.
|
119700 | for (int i = 1; i < s->c.mb_num; i++) { |
414 |
2/2✓ Branch 0 taken 5845 times.
✓ Branch 1 taken 113455 times.
|
119300 | if (qscale_table[ s->c.mb_index2xy[i] ] - qscale_table[ s->c.mb_index2xy[i-1] ] > 2) |
415 | 5845 | qscale_table[ s->c.mb_index2xy[i] ] = qscale_table[ s->c.mb_index2xy[i-1] ] + 2; | |
416 | } | ||
417 |
2/2✓ Branch 0 taken 119300 times.
✓ Branch 1 taken 400 times.
|
119700 | for(int i = s->c.mb_num - 2; i >= 0; i--) { |
418 |
2/2✓ Branch 0 taken 4815 times.
✓ Branch 1 taken 114485 times.
|
119300 | if (qscale_table[ s->c.mb_index2xy[i] ] - qscale_table[ s->c.mb_index2xy[i+1] ] > 2) |
419 | 4815 | qscale_table[ s->c.mb_index2xy[i] ] = qscale_table[ s->c.mb_index2xy[i+1] ] + 2; | |
420 | } | ||
421 | |||
422 |
1/2✓ Branch 0 taken 400 times.
✗ Branch 1 not taken.
|
400 | if (s->c.codec_id != AV_CODEC_ID_H263P) { |
423 |
2/2✓ Branch 0 taken 119300 times.
✓ Branch 1 taken 400 times.
|
119700 | for (int i = 1; i < s->c.mb_num; i++) { |
424 | 119300 | int mb_xy = s->c.mb_index2xy[i]; | |
425 | |||
426 |
2/2✓ Branch 0 taken 41665 times.
✓ Branch 1 taken 77635 times.
|
119300 | if (qscale_table[mb_xy] != qscale_table[s->c.mb_index2xy[i - 1]] && |
427 |
2/2✓ Branch 0 taken 16880 times.
✓ Branch 1 taken 24785 times.
|
41665 | (s->mb_type[mb_xy] & CANDIDATE_MB_TYPE_INTER4V)) { |
428 | 16880 | s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER; | |
429 | } | ||
430 | } | ||
431 | } | ||
432 | 400 | } | |
433 | |||
434 | static const int dquant_code[5]= {1,0,9,2,3}; | ||
435 | |||
436 | 187500 | static void flv2_encode_ac_esc(PutBitContext *pb, int slevel, int level, | |
437 | int run, int last) | ||
438 | { | ||
439 | unsigned code; | ||
440 | int bits; | ||
441 |
2/2✓ Branch 0 taken 175592 times.
✓ Branch 1 taken 11908 times.
|
187500 | if (level < 64) { // 7-bit level |
442 | 175592 | bits = 1 + 1 + 6 + 7; | |
443 | 175592 | code = (0 << (1 + 6 + 7)) | | |
444 | 175592 | (last << (6 + 7)) | | |
445 | 175592 | (run << 7) | | |
446 | 175592 | (slevel & 0x7f); | |
447 | } else { | ||
448 | /* 11-bit level */ | ||
449 | 11908 | bits = 1 + 1 + 6 + 11; | |
450 | 11908 | code = (1 << (1 + 6 + 11)) | | |
451 | 11908 | (last << (6 + 11)) | | |
452 | 11908 | (run << 11) | | |
453 | 11908 | (slevel & 0x7ff); | |
454 | } | ||
455 | 187500 | put_bits(pb, bits, code); | |
456 | 187500 | } | |
457 | |||
458 | /** | ||
459 | * Encode an 8x8 block. | ||
460 | * @param block the 8x8 block | ||
461 | * @param n block index (0-3 are luma, 4-5 are chroma) | ||
462 | */ | ||
463 | 2388636 | static void h263_encode_block(MPVEncContext *const s, int16_t block[], int n) | |
464 | { | ||
465 | int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code; | ||
466 | const RLTable *rl; | ||
467 | |||
468 | 2388636 | rl = &ff_h263_rl_inter; | |
469 |
4/4✓ Branch 0 taken 348792 times.
✓ Branch 1 taken 2039844 times.
✓ Branch 2 taken 266694 times.
✓ Branch 3 taken 82098 times.
|
2388636 | if (s->c.mb_intra && !s->c.h263_aic) { |
470 | /* DC coef */ | ||
471 | 266694 | level = block[0]; | |
472 | /* 255 cannot be represented, so we clamp */ | ||
473 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 266694 times.
|
266694 | if (level > 254) { |
474 | ✗ | level = 254; | |
475 | ✗ | block[0] = 254; | |
476 | } | ||
477 | /* 0 cannot be represented also */ | ||
478 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 266694 times.
|
266694 | else if (level < 1) { |
479 | ✗ | level = 1; | |
480 | ✗ | block[0] = 1; | |
481 | } | ||
482 |
2/2✓ Branch 0 taken 5826 times.
✓ Branch 1 taken 260868 times.
|
266694 | if (level == 128) //FIXME check rv10 |
483 | 5826 | put_bits(&s->pb, 8, 0xff); | |
484 | else | ||
485 | 260868 | put_bits(&s->pb, 8, level); | |
486 | 266694 | i = 1; | |
487 | } else { | ||
488 | 2121942 | i = 0; | |
489 |
4/4✓ Branch 0 taken 391992 times.
✓ Branch 1 taken 1729950 times.
✓ Branch 2 taken 82098 times.
✓ Branch 3 taken 309894 times.
|
2121942 | if (s->c.h263_aic && s->c.mb_intra) |
490 | 82098 | rl = &ff_rl_intra_aic; | |
491 | |||
492 |
4/4✓ Branch 0 taken 356352 times.
✓ Branch 1 taken 1765590 times.
✓ Branch 2 taken 309894 times.
✓ Branch 3 taken 46458 times.
|
2121942 | if(s->c.alt_inter_vlc && !s->c.mb_intra){ |
493 | 309894 | int aic_vlc_bits=0; | |
494 | 309894 | int inter_vlc_bits=0; | |
495 | 309894 | int wrong_pos=-1; | |
496 | int aic_code; | ||
497 | |||
498 | 309894 | last_index = s->c.block_last_index[n]; | |
499 | 309894 | last_non_zero = i - 1; | |
500 |
2/2✓ Branch 0 taken 9817100 times.
✓ Branch 1 taken 309894 times.
|
10126994 | for (; i <= last_index; i++) { |
501 | 9817100 | j = s->c.intra_scantable.permutated[i]; | |
502 | 9817100 | level = block[j]; | |
503 |
2/2✓ Branch 0 taken 3646040 times.
✓ Branch 1 taken 6171060 times.
|
9817100 | if (level) { |
504 | 3646040 | run = i - last_non_zero - 1; | |
505 | 3646040 | last = (i == last_index); | |
506 | |||
507 |
2/2✓ Branch 0 taken 1800387 times.
✓ Branch 1 taken 1845653 times.
|
3646040 | if(level<0) level= -level; |
508 | |||
509 | 3646040 | code = get_rl_index(rl, last, run, level); | |
510 | 3646040 | aic_code = get_rl_index(&ff_rl_intra_aic, last, run, level); | |
511 | 3646040 | inter_vlc_bits += rl->table_vlc[code][1]+1; | |
512 | 3646040 | aic_vlc_bits += ff_rl_intra_aic.table_vlc[aic_code][1]+1; | |
513 | |||
514 |
2/2✓ Branch 0 taken 238217 times.
✓ Branch 1 taken 3407823 times.
|
3646040 | if (code == rl->n) { |
515 | 238217 | inter_vlc_bits += 1+6+8-1; | |
516 | } | ||
517 |
2/2✓ Branch 0 taken 160580 times.
✓ Branch 1 taken 3485460 times.
|
3646040 | if (aic_code == ff_rl_intra_aic.n) { |
518 | 160580 | aic_vlc_bits += 1+6+8-1; | |
519 | 160580 | wrong_pos += run + 1; | |
520 | }else | ||
521 | 3485460 | wrong_pos += wrong_run[aic_code]; | |
522 | 3646040 | last_non_zero = i; | |
523 | } | ||
524 | } | ||
525 | 309894 | i = 0; | |
526 |
4/4✓ Branch 0 taken 102063 times.
✓ Branch 1 taken 207831 times.
✓ Branch 2 taken 66459 times.
✓ Branch 3 taken 35604 times.
|
309894 | if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63) |
527 | 66459 | rl = &ff_rl_intra_aic; | |
528 | } | ||
529 | } | ||
530 | |||
531 | /* AC coefs */ | ||
532 | 2388636 | last_index = s->c.block_last_index[n]; | |
533 | 2388636 | last_non_zero = i - 1; | |
534 |
2/2✓ Branch 0 taken 37329308 times.
✓ Branch 1 taken 2388636 times.
|
39717944 | for (; i <= last_index; i++) { |
535 | 37329308 | j = s->c.intra_scantable.permutated[i]; | |
536 | 37329308 | level = block[j]; | |
537 |
2/2✓ Branch 0 taken 11712914 times.
✓ Branch 1 taken 25616394 times.
|
37329308 | if (level) { |
538 | 11712914 | run = i - last_non_zero - 1; | |
539 | 11712914 | last = (i == last_index); | |
540 | 11712914 | sign = 0; | |
541 | 11712914 | slevel = level; | |
542 |
2/2✓ Branch 0 taken 5838423 times.
✓ Branch 1 taken 5874491 times.
|
11712914 | if (level < 0) { |
543 | 5838423 | sign = 1; | |
544 | 5838423 | level = -level; | |
545 | } | ||
546 | 11712914 | code = get_rl_index(rl, last, run, level); | |
547 | 11712914 | put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
548 |
2/2✓ Branch 0 taken 590306 times.
✓ Branch 1 taken 11122608 times.
|
11712914 | if (code == rl->n) { |
549 |
2/2✓ Branch 0 taken 402806 times.
✓ Branch 1 taken 187500 times.
|
590306 | if (!CONFIG_FLV_ENCODER || s->c.h263_flv <= 1) { |
550 | 402806 | put_bits(&s->pb, 1, last); | |
551 | 402806 | put_bits(&s->pb, 6, run); | |
552 | |||
553 | av_assert2(slevel != 0); | ||
554 | |||
555 |
2/2✓ Branch 0 taken 400394 times.
✓ Branch 1 taken 2412 times.
|
402806 | if (level < 128) { |
556 | 400394 | put_sbits(&s->pb, 8, slevel); | |
557 | } else { | ||
558 | 2412 | put_bits(&s->pb, 8, 128); | |
559 | 2412 | put_sbits(&s->pb, 5, slevel); | |
560 | 2412 | put_sbits(&s->pb, 6, slevel>>5); | |
561 | } | ||
562 | } else { | ||
563 | 187500 | flv2_encode_ac_esc(&s->pb, slevel, level, run, last); | |
564 | } | ||
565 | } else { | ||
566 | 11122608 | put_bits(&s->pb, 1, sign); | |
567 | } | ||
568 | 11712914 | last_non_zero = i; | |
569 | } | ||
570 | } | ||
571 | 2388636 | } | |
572 | |||
573 | /* Encode MV differences on H.263+ with Unrestricted MV mode */ | ||
574 | 103298 | static void h263p_encode_umotion(PutBitContext *pb, int val) | |
575 | { | ||
576 |
2/2✓ Branch 0 taken 51255 times.
✓ Branch 1 taken 52043 times.
|
103298 | if ( val == 0) |
577 | 51255 | put_bits(pb, 1, 1); | |
578 | else { | ||
579 | 52043 | unsigned code = (val < 0) << 1; | |
580 | 52043 | unsigned aval = val < 0 ? -val : val; | |
581 | 52043 | unsigned n_bits = 2; | |
582 | |||
583 |
2/2✓ Branch 0 taken 28759 times.
✓ Branch 1 taken 52043 times.
|
80802 | while (aval != 1) { // The leading digit is implicitly coded via length |
584 | 28759 | unsigned tmp = (aval & 1) << 1 | 1; | |
585 | 28759 | aval >>= 1; | |
586 | 28759 | code |= tmp << n_bits; | |
587 | 28759 | n_bits += 2; | |
588 | } | ||
589 | 52043 | put_bits(pb, n_bits + 1, code); | |
590 | } | ||
591 | 103298 | } | |
592 | |||
593 | 82098 | static int h263_pred_dc(MPVEncContext *const s, int n, int16_t **dc_val_ptr) | |
594 | { | ||
595 | 82098 | const int wrap = s->c.block_wrap[n]; | |
596 | 82098 | const int xy = s->c.block_index[n]; | |
597 | 82098 | int16_t *const dc_val = s->c.dc_val + xy; | |
598 | int pred_dc; | ||
599 | |||
600 | /* find prediction */ | ||
601 | /* B C | ||
602 | * A X | ||
603 | */ | ||
604 | 82098 | int a = dc_val[-1]; | |
605 | 82098 | int c = dc_val[-wrap]; | |
606 | |||
607 | /* just DC prediction */ | ||
608 |
4/4✓ Branch 0 taken 76882 times.
✓ Branch 1 taken 5216 times.
✓ Branch 2 taken 47737 times.
✓ Branch 3 taken 29145 times.
|
82098 | if (a != 1024 && c != 1024) |
609 | 47737 | pred_dc = (a + c) >> 1; | |
610 |
2/2✓ Branch 0 taken 29145 times.
✓ Branch 1 taken 5216 times.
|
34361 | else if (a != 1024) |
611 | 29145 | pred_dc = a; | |
612 | else | ||
613 | 5216 | pred_dc = c; | |
614 | |||
615 | /* we assume pred is positive */ | ||
616 | 82098 | *dc_val_ptr = dc_val; | |
617 | 82098 | return pred_dc; | |
618 | } | ||
619 | |||
620 | 428550 | static void h263_encode_mb(MPVEncContext *const s, | |
621 | int16_t block[][64], | ||
622 | int motion_x, int motion_y) | ||
623 | { | ||
624 | int cbpc, cbpy, i, cbp, pred_x, pred_y; | ||
625 | int16_t pred_dc; | ||
626 | int16_t rec_intradc[6]; | ||
627 | 428550 | const int interleaved_stats = s->c.avctx->flags & AV_CODEC_FLAG_PASS1; | |
628 | |||
629 |
2/2✓ Branch 0 taken 370418 times.
✓ Branch 1 taken 58132 times.
|
428550 | if (!s->c.mb_intra) { |
630 | /* compute cbp */ | ||
631 | 370418 | cbp= get_p_cbp(s, block, motion_x, motion_y); | |
632 | |||
633 |
2/2✓ Branch 0 taken 30444 times.
✓ Branch 1 taken 339974 times.
|
370418 | if ((cbp | motion_x | motion_y | s->dquant | (s->c.mv_type - MV_TYPE_16X16)) == 0) { |
634 | /* skip macroblock */ | ||
635 | 30444 | put_bits(&s->pb, 1, 1); | |
636 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30444 times.
|
30444 | if(interleaved_stats){ |
637 | ✗ | s->misc_bits++; | |
638 | ✗ | s->last_bits++; | |
639 | } | ||
640 | |||
641 | 30444 | return; | |
642 | } | ||
643 | 339974 | put_bits(&s->pb, 1, 0); /* mb coded */ | |
644 | |||
645 | 339974 | cbpc = cbp & 3; | |
646 | 339974 | cbpy = cbp >> 2; | |
647 |
4/4✓ Branch 0 taken 51649 times.
✓ Branch 1 taken 288325 times.
✓ Branch 2 taken 13805 times.
✓ Branch 3 taken 37844 times.
|
339974 | if(s->c.alt_inter_vlc==0 || cbpc!=3) |
648 | 302130 | cbpy ^= 0xF; | |
649 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 339974 times.
|
339974 | if(s->dquant) cbpc+= 8; |
650 |
1/2✓ Branch 0 taken 339974 times.
✗ Branch 1 not taken.
|
339974 | if(s->c.mv_type==MV_TYPE_16X16){ |
651 | 339974 | put_bits(&s->pb, | |
652 | 339974 | ff_h263_inter_MCBPC_bits[cbpc], | |
653 | 339974 | ff_h263_inter_MCBPC_code[cbpc]); | |
654 | |||
655 | 339974 | put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]); | |
656 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 339974 times.
|
339974 | if(s->dquant) |
657 | ✗ | put_bits(&s->pb, 2, dquant_code[s->dquant+2]); | |
658 | |||
659 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 339974 times.
|
339974 | if(interleaved_stats){ |
660 | ✗ | s->misc_bits+= get_bits_diff(s); | |
661 | } | ||
662 | |||
663 | /* motion vectors: 16x16 mode */ | ||
664 | 339974 | ff_h263_pred_motion(&s->c, 0, 0, &pred_x, &pred_y); | |
665 | |||
666 |
2/2✓ Branch 0 taken 288325 times.
✓ Branch 1 taken 51649 times.
|
339974 | if (!s->c.umvplus) { |
667 | 288325 | ff_h263_encode_motion_vector(s, motion_x - pred_x, | |
668 | motion_y - pred_y, 1); | ||
669 | } | ||
670 | else { | ||
671 | 51649 | h263p_encode_umotion(&s->pb, motion_x - pred_x); | |
672 | 51649 | h263p_encode_umotion(&s->pb, motion_y - pred_y); | |
673 |
4/4✓ Branch 0 taken 10131 times.
✓ Branch 1 taken 41518 times.
✓ Branch 2 taken 340 times.
✓ Branch 3 taken 9791 times.
|
51649 | if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1)) |
674 | /* To prevent Start Code emulation */ | ||
675 | 340 | put_bits(&s->pb,1,1); | |
676 | } | ||
677 | }else{ | ||
678 | ✗ | put_bits(&s->pb, | |
679 | ✗ | ff_h263_inter_MCBPC_bits[cbpc+16], | |
680 | ✗ | ff_h263_inter_MCBPC_code[cbpc+16]); | |
681 | ✗ | put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]); | |
682 | ✗ | if(s->dquant) | |
683 | ✗ | put_bits(&s->pb, 2, dquant_code[s->dquant+2]); | |
684 | |||
685 | ✗ | if(interleaved_stats){ | |
686 | ✗ | s->misc_bits+= get_bits_diff(s); | |
687 | } | ||
688 | |||
689 | ✗ | for(i=0; i<4; i++){ | |
690 | /* motion vectors: 8x8 mode*/ | ||
691 | ✗ | ff_h263_pred_motion(&s->c, i, 0, &pred_x, &pred_y); | |
692 | |||
693 | ✗ | motion_x = s->c.cur_pic.motion_val[0][s->c.block_index[i]][0]; | |
694 | ✗ | motion_y = s->c.cur_pic.motion_val[0][s->c.block_index[i]][1]; | |
695 | ✗ | if (!s->c.umvplus) { | |
696 | ✗ | ff_h263_encode_motion_vector(s, motion_x - pred_x, | |
697 | motion_y - pred_y, 1); | ||
698 | } | ||
699 | else { | ||
700 | ✗ | h263p_encode_umotion(&s->pb, motion_x - pred_x); | |
701 | ✗ | h263p_encode_umotion(&s->pb, motion_y - pred_y); | |
702 | ✗ | if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1)) | |
703 | /* To prevent Start Code emulation */ | ||
704 | ✗ | put_bits(&s->pb,1,1); | |
705 | } | ||
706 | } | ||
707 | } | ||
708 | |||
709 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 339974 times.
|
339974 | if(interleaved_stats){ |
710 | ✗ | s->mv_bits+= get_bits_diff(s); | |
711 | } | ||
712 | } else { | ||
713 | av_assert2(s->c.mb_intra); | ||
714 | |||
715 | 58132 | cbp = 0; | |
716 |
2/2✓ Branch 0 taken 13683 times.
✓ Branch 1 taken 44449 times.
|
58132 | if (s->c.h263_aic) { |
717 | /* Predict DC */ | ||
718 |
2/2✓ Branch 0 taken 82098 times.
✓ Branch 1 taken 13683 times.
|
95781 | for(i=0; i<6; i++) { |
719 | 82098 | int16_t level = block[i][0]; | |
720 | int16_t *dc_ptr; | ||
721 |
2/2✓ Branch 0 taken 54732 times.
✓ Branch 1 taken 27366 times.
|
82098 | int scale = i < 4 ? s->c.y_dc_scale : s->c.c_dc_scale; |
722 | |||
723 | 82098 | pred_dc = h263_pred_dc(s, i, &dc_ptr); | |
724 | 82098 | level -= pred_dc; | |
725 | /* Quant */ | ||
726 |
2/2✓ Branch 0 taken 43407 times.
✓ Branch 1 taken 38691 times.
|
82098 | if (level >= 0) |
727 | 43407 | level = (level + (scale>>1))/scale; | |
728 | else | ||
729 | 38691 | level = (level - (scale>>1))/scale; | |
730 | |||
731 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 82098 times.
|
82098 | if (!s->c.modified_quant) { |
732 | ✗ | if (level < -127) | |
733 | ✗ | level = -127; | |
734 | ✗ | else if (level > 127) | |
735 | ✗ | level = 127; | |
736 | } | ||
737 | |||
738 | 82098 | block[i][0] = level; | |
739 | /* Reconstruction */ | ||
740 | 82098 | rec_intradc[i] = scale*level + pred_dc; | |
741 | /* Oddify */ | ||
742 | 82098 | rec_intradc[i] |= 1; | |
743 | //if ((rec_intradc[i] % 2) == 0) | ||
744 | // rec_intradc[i]++; | ||
745 | /* Clipping */ | ||
746 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 82098 times.
|
82098 | if (rec_intradc[i] < 0) |
747 | ✗ | rec_intradc[i] = 0; | |
748 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 82098 times.
|
82098 | else if (rec_intradc[i] > 2047) |
749 | ✗ | rec_intradc[i] = 2047; | |
750 | |||
751 | /* Update AC/DC tables */ | ||
752 | 82098 | *dc_ptr = rec_intradc[i]; | |
753 | /* AIC can change CBP */ | ||
754 |
2/2✓ Branch 0 taken 6224 times.
✓ Branch 1 taken 75874 times.
|
82098 | if (s->c.block_last_index[i] > 0 || |
755 |
3/4✓ Branch 0 taken 6224 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4973 times.
✓ Branch 3 taken 1251 times.
|
6224 | (s->c.block_last_index[i] == 0 && level !=0)) |
756 | 80847 | cbp |= 1 << (5 - i); | |
757 | } | ||
758 | }else{ | ||
759 |
2/2✓ Branch 0 taken 266694 times.
✓ Branch 1 taken 44449 times.
|
311143 | for(i=0; i<6; i++) { |
760 | /* compute cbp */ | ||
761 |
2/2✓ Branch 0 taken 206994 times.
✓ Branch 1 taken 59700 times.
|
266694 | if (s->c.block_last_index[i] >= 1) |
762 | 206994 | cbp |= 1 << (5 - i); | |
763 | } | ||
764 | } | ||
765 | |||
766 | 58132 | cbpc = cbp & 3; | |
767 |
2/2✓ Branch 0 taken 45249 times.
✓ Branch 1 taken 12883 times.
|
58132 | if (s->c.pict_type == AV_PICTURE_TYPE_I) { |
768 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 45249 times.
|
45249 | if(s->dquant) cbpc+=4; |
769 | 45249 | put_bits(&s->pb, | |
770 | 45249 | ff_h263_intra_MCBPC_bits[cbpc], | |
771 | 45249 | ff_h263_intra_MCBPC_code[cbpc]); | |
772 | } else { | ||
773 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12883 times.
|
12883 | if(s->dquant) cbpc+=8; |
774 | 12883 | put_bits(&s->pb, 1, 0); /* mb coded */ | |
775 | 12883 | put_bits(&s->pb, | |
776 | 12883 | ff_h263_inter_MCBPC_bits[cbpc + 4], | |
777 | 12883 | ff_h263_inter_MCBPC_code[cbpc + 4]); | |
778 | } | ||
779 |
2/2✓ Branch 0 taken 13683 times.
✓ Branch 1 taken 44449 times.
|
58132 | if (s->c.h263_aic) { |
780 | /* XXX: currently, we do not try to use ac prediction */ | ||
781 | 13683 | put_bits(&s->pb, 1, 0); /* no AC prediction */ | |
782 | } | ||
783 | 58132 | cbpy = cbp >> 2; | |
784 | 58132 | put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]); | |
785 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58132 times.
|
58132 | if(s->dquant) |
786 | ✗ | put_bits(&s->pb, 2, dquant_code[s->dquant+2]); | |
787 | |||
788 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58132 times.
|
58132 | if(interleaved_stats){ |
789 | ✗ | s->misc_bits+= get_bits_diff(s); | |
790 | } | ||
791 | } | ||
792 | |||
793 |
2/2✓ Branch 0 taken 2388636 times.
✓ Branch 1 taken 398106 times.
|
2786742 | for(i=0; i<6; i++) { |
794 | /* encode each block */ | ||
795 | 2388636 | h263_encode_block(s, block[i], i); | |
796 | |||
797 | /* Update INTRADC for decoding */ | ||
798 |
4/4✓ Branch 0 taken 391992 times.
✓ Branch 1 taken 1996644 times.
✓ Branch 2 taken 82098 times.
✓ Branch 3 taken 309894 times.
|
2388636 | if (s->c.h263_aic && s->c.mb_intra) |
799 | 82098 | block[i][0] = rec_intradc[i]; | |
800 | } | ||
801 | |||
802 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 398106 times.
|
398106 | if(interleaved_stats){ |
803 | ✗ | if (!s->c.mb_intra) { | |
804 | ✗ | s->p_tex_bits+= get_bits_diff(s); | |
805 | }else{ | ||
806 | ✗ | s->i_tex_bits+= get_bits_diff(s); | |
807 | ✗ | s->i_count++; | |
808 | } | ||
809 | } | ||
810 | } | ||
811 | |||
812 | 1380408 | void ff_h263_update_mb(MPVEncContext *const s) | |
813 | { | ||
814 | 1380408 | const int mb_xy = s->c.mb_y * s->c.mb_stride + s->c.mb_x; | |
815 | |||
816 |
2/2✓ Branch 0 taken 702558 times.
✓ Branch 1 taken 677850 times.
|
1380408 | if (s->c.cur_pic.mbskip_table) |
817 | 702558 | s->c.cur_pic.mbskip_table[mb_xy] = s->c.mb_skipped; | |
818 | |||
819 |
2/2✓ Branch 0 taken 82021 times.
✓ Branch 1 taken 1298387 times.
|
1380408 | if (s->c.mv_type == MV_TYPE_8X8) |
820 | 82021 | s->c.cur_pic.mb_type[mb_xy] = MB_TYPE_FORWARD_MV | MB_TYPE_8x8; | |
821 |
2/2✓ Branch 0 taken 221358 times.
✓ Branch 1 taken 1077029 times.
|
1298387 | else if(s->c.mb_intra) |
822 | 221358 | s->c.cur_pic.mb_type[mb_xy] = MB_TYPE_INTRA; | |
823 | else | ||
824 | 1077029 | s->c.cur_pic.mb_type[mb_xy] = MB_TYPE_FORWARD_MV | MB_TYPE_16x16; | |
825 | |||
826 | 1380408 | ff_h263_update_motion_val(&s->c); | |
827 | 1380408 | } | |
828 | |||
829 | 103 | av_cold void ff_h263_encode_init(MPVMainEncContext *const m) | |
830 | { | ||
831 | 103 | MPVEncContext *const s = &m->s; | |
832 | |||
833 | 103 | s->me.mv_penalty = ff_h263_get_mv_penalty(); // FIXME exact table for MSMPEG4 & H.263+ | |
834 | |||
835 | 103 | ff_h263dsp_init(&s->c.h263dsp); | |
836 | |||
837 |
2/2✓ Branch 0 taken 63 times.
✓ Branch 1 taken 40 times.
|
103 | if (s->c.codec_id == AV_CODEC_ID_MPEG4) |
838 | 63 | return; | |
839 | |||
840 | 40 | s->intra_ac_vlc_length =s->inter_ac_vlc_length = uni_h263_inter_rl_len; | |
841 | 40 | s->intra_ac_vlc_last_length=s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64; | |
842 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 34 times.
|
40 | if (s->c.h263_aic) { |
843 | 6 | s->intra_ac_vlc_length = uni_h263_intra_aic_rl_len; | |
844 | 6 | s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64; | |
845 | |||
846 | 6 | s->c.y_dc_scale_table = | |
847 | 6 | s->c.c_dc_scale_table = ff_aic_dc_scale_table; | |
848 | } | ||
849 | 40 | s->ac_esc_length= 7+1+6+8; | |
850 | |||
851 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 34 times.
|
40 | if (s->c.modified_quant) |
852 | 6 | s->c.chroma_qscale_table = ff_h263_chroma_qscale_table; | |
853 | |||
854 | // Only used for H.263 and H.263+ | ||
855 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
40 | s->c.gob_index = H263_GOB_HEIGHT(s->c.height); |
856 | |||
857 | // use fcodes >1 only for MPEG-4 & H.263 & H.263+ FIXME | ||
858 |
3/3✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 30 times.
|
40 | switch(s->c.codec_id){ |
859 | 3 | case AV_CODEC_ID_H263P: | |
860 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (s->c.umvplus) |
861 | 3 | m->fcode_tab = umv_fcode_tab + MAX_MV; | |
862 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (s->c.modified_quant) { |
863 | 3 | s->min_qcoeff= -2047; | |
864 | 3 | s->max_qcoeff= 2047; | |
865 | }else{ | ||
866 | ✗ | s->min_qcoeff= -127; | |
867 | ✗ | s->max_qcoeff= 127; | |
868 | } | ||
869 | 3 | break; | |
870 | // Note for MPEG-4 & H.263 the dc-scale table will be set per frame as needed later | ||
871 | #if CONFIG_FLV_ENCODER | ||
872 | 7 | case AV_CODEC_ID_FLV1: | |
873 | 7 | m->encode_picture_header = ff_flv_encode_picture_header; | |
874 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | if (s->c.h263_flv > 1) { |
875 | 7 | s->min_qcoeff= -1023; | |
876 | 7 | s->max_qcoeff= 1023; | |
877 | } else { | ||
878 | ✗ | s->min_qcoeff= -127; | |
879 | ✗ | s->max_qcoeff= 127; | |
880 | } | ||
881 | 7 | break; | |
882 | #endif | ||
883 | 30 | default: //nothing needed - default table already set in mpegvideo.c | |
884 | 30 | s->min_qcoeff= -127; | |
885 | 30 | s->max_qcoeff= 127; | |
886 | } | ||
887 | // H.263, H.263+; will be overwritten for MSMPEG-4 later | ||
888 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 18 times.
|
40 | if (!m->encode_picture_header) |
889 | 22 | m->encode_picture_header = h263_encode_picture_header; | |
890 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 4 times.
|
40 | if (!s->encode_mb) |
891 | 36 | s->encode_mb = h263_encode_mb; | |
892 | } | ||
893 | |||
894 | 150 | void ff_h263_encode_mba(MPVEncContext *const s) | |
895 | { | ||
896 | int i, mb_pos; | ||
897 | |||
898 |
1/2✓ Branch 0 taken 450 times.
✗ Branch 1 not taken.
|
450 | for(i=0; i<6; i++){ |
899 |
2/2✓ Branch 0 taken 150 times.
✓ Branch 1 taken 300 times.
|
450 | if(s->c.mb_num-1 <= ff_mba_max[i]) break; |
900 | } | ||
901 | 150 | mb_pos= s->c.mb_x + s->c.mb_width*s->c.mb_y; | |
902 | 150 | put_bits(&s->pb, ff_mba_length[i], mb_pos); | |
903 | 150 | } | |
904 | |||
905 | #define OFFSET(x) offsetof(MpegEncContext, x) | ||
906 | #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM | ||
907 | static const AVOption h263_options[] = { | ||
908 | { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, | ||
909 | { "mb_info", "emit macroblock info for RFC 2190 packetization, the parameter value is the maximum payload size", FF_MPV_OFFSET(mb_info), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE }, | ||
910 | FF_MPV_COMMON_OPTS | ||
911 | FF_MPV_COMMON_MOTION_EST_OPTS | ||
912 | { NULL }, | ||
913 | }; | ||
914 | |||
915 | static const AVClass h263_class = { | ||
916 | .class_name = "H.263 encoder", | ||
917 | .item_name = av_default_item_name, | ||
918 | .option = h263_options, | ||
919 | .version = LIBAVUTIL_VERSION_INT, | ||
920 | }; | ||
921 | |||
922 | const FFCodec ff_h263_encoder = { | ||
923 | .p.name = "h263", | ||
924 | CODEC_LONG_NAME("H.263 / H.263-1996"), | ||
925 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
926 | .p.id = AV_CODEC_ID_H263, | ||
927 | CODEC_PIXFMTS(AV_PIX_FMT_YUV420P), | ||
928 | .color_ranges = AVCOL_RANGE_MPEG, | ||
929 | .p.priv_class = &h263_class, | ||
930 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
931 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
932 | .priv_data_size = sizeof(MPVMainEncContext), | ||
933 | .init = ff_mpv_encode_init, | ||
934 | FF_CODEC_ENCODE_CB(ff_mpv_encode_picture), | ||
935 | .close = ff_mpv_encode_end, | ||
936 | }; | ||
937 | |||
938 | static const AVOption h263p_options[] = { | ||
939 | { "umv", "Use unlimited motion vectors.", OFFSET(umvplus), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, | ||
940 | { "aiv", "Use alternative inter VLC.", OFFSET(alt_inter_vlc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, | ||
941 | { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, | ||
942 | { "structured_slices", "Write slice start position at every GOB header instead of just GOB number.", OFFSET(h263_slice_structured), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE}, | ||
943 | FF_MPV_COMMON_OPTS | ||
944 | FF_MPV_COMMON_MOTION_EST_OPTS | ||
945 | { NULL }, | ||
946 | }; | ||
947 | static const AVClass h263p_class = { | ||
948 | .class_name = "H.263p encoder", | ||
949 | .item_name = av_default_item_name, | ||
950 | .option = h263p_options, | ||
951 | .version = LIBAVUTIL_VERSION_INT, | ||
952 | }; | ||
953 | |||
954 | const FFCodec ff_h263p_encoder = { | ||
955 | .p.name = "h263p", | ||
956 | CODEC_LONG_NAME("H.263+ / H.263-1998 / H.263 version 2"), | ||
957 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
958 | .p.id = AV_CODEC_ID_H263P, | ||
959 | CODEC_PIXFMTS(AV_PIX_FMT_YUV420P), | ||
960 | .color_ranges = AVCOL_RANGE_MPEG, | ||
961 | .p.priv_class = &h263p_class, | ||
962 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS | | ||
963 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
964 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
965 | .priv_data_size = sizeof(MPVMainEncContext), | ||
966 | .init = ff_mpv_encode_init, | ||
967 | FF_CODEC_ENCODE_CB(ff_mpv_encode_picture), | ||
968 | .close = ff_mpv_encode_end, | ||
969 | }; | ||
970 | #endif | ||
971 |