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