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