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