Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * MSMPEG4 backend for encoder and decoder | ||
3 | * Copyright (c) 2001 Fabrice Bellard | ||
4 | * Copyright (c) 2002-2013 Michael Niedermayer <michaelni@gmx.at> | ||
5 | * | ||
6 | * msmpeg4v1 & v2 stuff by 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 | #include "libavutil/thread.h" | ||
26 | |||
27 | #include "avcodec.h" | ||
28 | #include "codec_internal.h" | ||
29 | #include "mpegutils.h" | ||
30 | #include "mpegvideo.h" | ||
31 | #include "mpegvideodec.h" | ||
32 | #include "msmpeg4.h" | ||
33 | #include "msmpeg4dec.h" | ||
34 | #include "libavutil/imgutils.h" | ||
35 | #include "h263.h" | ||
36 | #include "h263data.h" | ||
37 | #include "h263dec.h" | ||
38 | #include "mpeg4videodec.h" | ||
39 | #include "msmpeg4data.h" | ||
40 | #include "msmpeg4_vc1_data.h" | ||
41 | |||
42 | #define V2_INTRA_CBPC_VLC_BITS 3 | ||
43 | #define V2_MB_TYPE_VLC_BITS 7 | ||
44 | #define MV_VLC_BITS 9 | ||
45 | #define TEX_VLC_BITS 9 | ||
46 | |||
47 | #define DEFAULT_INTER_INDEX 3 | ||
48 | |||
49 | 5490 | static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n, | |
50 | int32_t **dc_val_ptr) | ||
51 | { | ||
52 | int i; | ||
53 | |||
54 |
2/2✓ Branch 0 taken 3660 times.
✓ Branch 1 taken 1830 times.
|
5490 | if (n < 4) { |
55 | 3660 | i= 0; | |
56 | } else { | ||
57 | 1830 | i= n-3; | |
58 | } | ||
59 | |||
60 | 5490 | *dc_val_ptr= &s->last_dc[i]; | |
61 | 5490 | return s->last_dc[i]; | |
62 | } | ||
63 | |||
64 | /****************************************/ | ||
65 | /* decoding stuff */ | ||
66 | |||
67 | const VLCElem *ff_mb_non_intra_vlc[4]; | ||
68 | static VLCElem v2_dc_lum_vlc[1472]; | ||
69 | static VLCElem v2_dc_chroma_vlc[1506]; | ||
70 | static VLCElem v2_intra_cbpc_vlc[8]; | ||
71 | static VLCElem v2_mb_type_vlc[128]; | ||
72 | VLCElem ff_inter_intra_vlc[8]; | ||
73 | |||
74 | /* This is identical to H.263 except that its range is multiplied by 2. */ | ||
75 | 132268 | static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code) | |
76 | { | ||
77 | int code, val, sign, shift; | ||
78 | |||
79 | 132268 | code = get_vlc2(&s->gb, ff_h263_mv_vlc, H263_MV_VLC_BITS, 2); | |
80 | ff_dlog(s, "MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred); | ||
81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 132268 times.
|
132268 | if (code < 0) |
82 | ✗ | return 0xffff; | |
83 | |||
84 |
2/2✓ Branch 0 taken 68571 times.
✓ Branch 1 taken 63697 times.
|
132268 | if (code == 0) |
85 | 68571 | return pred; | |
86 | 63697 | sign = get_bits1(&s->gb); | |
87 | 63697 | shift = f_code - 1; | |
88 | 63697 | val = code; | |
89 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 63697 times.
|
63697 | if (shift) { |
90 | ✗ | val = (val - 1) << shift; | |
91 | ✗ | val |= get_bits(&s->gb, shift); | |
92 | ✗ | val++; | |
93 | } | ||
94 |
2/2✓ Branch 0 taken 26289 times.
✓ Branch 1 taken 37408 times.
|
63697 | if (sign) |
95 | 26289 | val = -val; | |
96 | |||
97 | 63697 | val += pred; | |
98 |
2/2✓ Branch 0 taken 138 times.
✓ Branch 1 taken 63559 times.
|
63697 | if (val <= -64) |
99 | 138 | val += 64; | |
100 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 63546 times.
|
63559 | else if (val >= 64) |
101 | 13 | val -= 64; | |
102 | |||
103 | 63697 | return val; | |
104 | } | ||
105 | |||
106 | 76350 | static int msmpeg4v12_decode_mb(MpegEncContext *s, int16_t block[6][64]) | |
107 | { | ||
108 | int cbp, code, i; | ||
109 | 76350 | uint32_t * const mb_type_ptr = &s->cur_pic.mb_type[s->mb_x + s->mb_y*s->mb_stride]; | |
110 | |||
111 |
2/2✓ Branch 0 taken 70035 times.
✓ Branch 1 taken 6315 times.
|
76350 | if (s->pict_type == AV_PICTURE_TYPE_P) { |
112 |
1/2✓ Branch 0 taken 70035 times.
✗ Branch 1 not taken.
|
70035 | if (s->use_skip_mb_code) { |
113 |
2/2✓ Branch 1 taken 1159 times.
✓ Branch 2 taken 68876 times.
|
70035 | if (get_bits1(&s->gb)) { |
114 | /* skip mb */ | ||
115 | 1159 | s->mb_intra = 0; | |
116 |
2/2✓ Branch 0 taken 6954 times.
✓ Branch 1 taken 1159 times.
|
8113 | for(i=0;i<6;i++) |
117 | 6954 | s->block_last_index[i] = -1; | |
118 | 1159 | s->mv_dir = MV_DIR_FORWARD; | |
119 | 1159 | s->mv_type = MV_TYPE_16X16; | |
120 | 1159 | s->mv[0][0][0] = 0; | |
121 | 1159 | s->mv[0][0][1] = 0; | |
122 | 1159 | s->mb_skipped = 1; | |
123 | 1159 | *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_FORWARD_MV | MB_TYPE_16x16; | |
124 | 1159 | return 0; | |
125 | } | ||
126 | } | ||
127 | |||
128 |
2/2✓ Branch 0 taken 53160 times.
✓ Branch 1 taken 15716 times.
|
68876 | if (s->msmpeg4_version == MSMP4_V2) |
129 | 53160 | code = get_vlc2(&s->gb, v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 1); | |
130 | else | ||
131 | 15716 | code = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 2); | |
132 |
2/4✓ Branch 0 taken 68876 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 68876 times.
|
68876 | if(code<0 || code>7){ |
133 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y); | |
134 | ✗ | return -1; | |
135 | } | ||
136 | |||
137 | 68876 | s->mb_intra = code >>2; | |
138 | |||
139 | 68876 | cbp = code & 0x3; | |
140 | } else { | ||
141 | 6315 | s->mb_intra = 1; | |
142 |
2/2✓ Branch 0 taken 5985 times.
✓ Branch 1 taken 330 times.
|
6315 | if (s->msmpeg4_version == MSMP4_V2) |
143 | 5985 | cbp = get_vlc2(&s->gb, v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 1); | |
144 | else | ||
145 | 330 | cbp = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 2); | |
146 |
2/4✓ Branch 0 taken 6315 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6315 times.
|
6315 | if(cbp<0 || cbp>3){ |
147 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y); | |
148 | ✗ | return -1; | |
149 | } | ||
150 | } | ||
151 | |||
152 |
2/2✓ Branch 0 taken 66134 times.
✓ Branch 1 taken 9057 times.
|
75191 | if (!s->mb_intra) { |
153 | int mx, my, cbpy; | ||
154 | |||
155 | 66134 | cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); | |
156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66134 times.
|
66134 | if(cbpy<0){ |
157 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y); | |
158 | ✗ | return -1; | |
159 | } | ||
160 | |||
161 | 66134 | cbp|= cbpy<<2; | |
162 |
4/4✓ Branch 0 taken 51003 times.
✓ Branch 1 taken 15131 times.
✓ Branch 2 taken 40593 times.
✓ Branch 3 taken 10410 times.
|
66134 | if (s->msmpeg4_version == MSMP4_V1 || (cbp&3) != 3) |
163 | 55724 | cbp ^= 0x3C; | |
164 | |||
165 | 66134 | ff_h263_pred_motion(s, 0, 0, &mx, &my); | |
166 | 66134 | mx= msmpeg4v2_decode_motion(s, mx, 1); | |
167 | 66134 | my= msmpeg4v2_decode_motion(s, my, 1); | |
168 | |||
169 | 66134 | s->mv_dir = MV_DIR_FORWARD; | |
170 | 66134 | s->mv_type = MV_TYPE_16X16; | |
171 | 66134 | s->mv[0][0][0] = mx; | |
172 | 66134 | s->mv[0][0][1] = my; | |
173 | 66134 | *mb_type_ptr = MB_TYPE_FORWARD_MV | MB_TYPE_16x16; | |
174 | } else { | ||
175 | int v; | ||
176 |
2/2✓ Branch 0 taken 8142 times.
✓ Branch 1 taken 915 times.
|
9057 | if (s->msmpeg4_version == MSMP4_V2) { |
177 | 8142 | s->ac_pred = get_bits1(&s->gb); | |
178 | 8142 | v = get_vlc2(&s->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); | |
179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8142 times.
|
8142 | if (v < 0) { |
180 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "cbpy vlc invalid\n"); | |
181 | ✗ | return -1; | |
182 | } | ||
183 | 8142 | cbp|= v<<2; | |
184 | } else{ | ||
185 | 915 | s->ac_pred = 0; | |
186 | 915 | v = get_vlc2(&s->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); | |
187 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 915 times.
|
915 | if (v < 0) { |
188 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "cbpy vlc invalid\n"); | |
189 | ✗ | return -1; | |
190 | } | ||
191 | 915 | cbp|= v<<2; | |
192 |
2/2✓ Branch 0 taken 585 times.
✓ Branch 1 taken 330 times.
|
915 | if(s->pict_type==AV_PICTURE_TYPE_P) cbp^=0x3C; |
193 | } | ||
194 | 9057 | *mb_type_ptr = MB_TYPE_INTRA; | |
195 | } | ||
196 | |||
197 | 75191 | s->bdsp.clear_blocks(s->block[0]); | |
198 |
2/2✓ Branch 0 taken 451146 times.
✓ Branch 1 taken 75191 times.
|
526337 | for (i = 0; i < 6; i++) { |
199 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 451146 times.
|
451146 | if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) |
200 | { | ||
201 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | |
202 | ✗ | return -1; | |
203 | } | ||
204 | } | ||
205 | 75191 | return 0; | |
206 | } | ||
207 | |||
208 | 129600 | static int msmpeg4v34_decode_mb(MpegEncContext *s, int16_t block[6][64]) | |
209 | { | ||
210 | int cbp, code, i; | ||
211 | uint8_t *coded_val; | ||
212 | 129600 | uint32_t * const mb_type_ptr = &s->cur_pic.mb_type[s->mb_x + s->mb_y*s->mb_stride]; | |
213 | |||
214 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 129600 times.
|
129600 | if (get_bits_left(&s->gb) <= 0) |
215 | ✗ | return AVERROR_INVALIDDATA; | |
216 | |||
217 |
2/2✓ Branch 0 taken 116442 times.
✓ Branch 1 taken 13158 times.
|
129600 | if (s->pict_type == AV_PICTURE_TYPE_P) { |
218 |
1/2✓ Branch 0 taken 116442 times.
✗ Branch 1 not taken.
|
116442 | if (s->use_skip_mb_code) { |
219 |
2/2✓ Branch 1 taken 1554 times.
✓ Branch 2 taken 114888 times.
|
116442 | if (get_bits1(&s->gb)) { |
220 | /* skip mb */ | ||
221 | 1554 | s->mb_intra = 0; | |
222 |
2/2✓ Branch 0 taken 9324 times.
✓ Branch 1 taken 1554 times.
|
10878 | for(i=0;i<6;i++) |
223 | 9324 | s->block_last_index[i] = -1; | |
224 | 1554 | s->mv_dir = MV_DIR_FORWARD; | |
225 | 1554 | s->mv_type = MV_TYPE_16X16; | |
226 | 1554 | s->mv[0][0][0] = 0; | |
227 | 1554 | s->mv[0][0][1] = 0; | |
228 | 1554 | s->mb_skipped = 1; | |
229 | 1554 | *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_FORWARD_MV | MB_TYPE_16x16; | |
230 | |||
231 | 1554 | return 0; | |
232 | } | ||
233 | } | ||
234 | |||
235 | 114888 | code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX], MB_NON_INTRA_VLC_BITS, 3); | |
236 | //s->mb_intra = (code & 0x40) ? 0 : 1; | ||
237 | 114888 | s->mb_intra = (~code & 0x40) >> 6; | |
238 | |||
239 | 114888 | cbp = code & 0x3f; | |
240 | } else { | ||
241 | 13158 | s->mb_intra = 1; | |
242 | 13158 | code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc, MSMP4_MB_INTRA_VLC_BITS, 2); | |
243 | /* predict coded block pattern */ | ||
244 | 13158 | cbp = 0; | |
245 |
2/2✓ Branch 0 taken 78948 times.
✓ Branch 1 taken 13158 times.
|
92106 | for(i=0;i<6;i++) { |
246 | 78948 | int val = ((code >> (5 - i)) & 1); | |
247 |
2/2✓ Branch 0 taken 52632 times.
✓ Branch 1 taken 26316 times.
|
78948 | if (i < 4) { |
248 | 52632 | int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val); | |
249 | 52632 | val = val ^ pred; | |
250 | 52632 | *coded_val = val; | |
251 | } | ||
252 | 78948 | cbp |= val << (5 - i); | |
253 | } | ||
254 | } | ||
255 | |||
256 |
2/2✓ Branch 0 taken 109652 times.
✓ Branch 1 taken 18394 times.
|
128046 | if (!s->mb_intra) { |
257 | int mx, my; | ||
258 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 109652 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
109652 | if(s->per_mb_rl_table && cbp){ |
259 | ✗ | s->rl_table_index = decode012(&s->gb); | |
260 | ✗ | s->rl_chroma_table_index = s->rl_table_index; | |
261 | } | ||
262 | 109652 | ff_h263_pred_motion(s, 0, 0, &mx, &my); | |
263 | 109652 | ff_msmpeg4_decode_motion(s, &mx, &my); | |
264 | 109652 | s->mv_dir = MV_DIR_FORWARD; | |
265 | 109652 | s->mv_type = MV_TYPE_16X16; | |
266 | 109652 | s->mv[0][0][0] = mx; | |
267 | 109652 | s->mv[0][0][1] = my; | |
268 | 109652 | *mb_type_ptr = MB_TYPE_FORWARD_MV | MB_TYPE_16x16; | |
269 | } else { | ||
270 | ff_dlog(s, "I at %d %d %d %06X\n", s->mb_x, s->mb_y, | ||
271 | ((cbp & 3) ? 1 : 0) +((cbp & 0x3C)? 2 : 0), | ||
272 | show_bits(&s->gb, 24)); | ||
273 | 18394 | s->ac_pred = get_bits1(&s->gb); | |
274 | 18394 | *mb_type_ptr = MB_TYPE_INTRA; | |
275 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18394 times.
|
18394 | if(s->inter_intra_pred){ |
276 | ✗ | s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 1); | |
277 | ff_dlog(s, "%d%d %d %d/", | ||
278 | s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y); | ||
279 | } | ||
280 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 18394 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
18394 | if(s->per_mb_rl_table && cbp){ |
281 | ✗ | s->rl_table_index = decode012(&s->gb); | |
282 | ✗ | s->rl_chroma_table_index = s->rl_table_index; | |
283 | } | ||
284 | } | ||
285 | |||
286 | 128046 | s->bdsp.clear_blocks(s->block[0]); | |
287 |
2/2✓ Branch 0 taken 768276 times.
✓ Branch 1 taken 128046 times.
|
896322 | for (i = 0; i < 6; i++) { |
288 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 768276 times.
|
768276 | if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) |
289 | { | ||
290 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | |
291 | ✗ | return -1; | |
292 | } | ||
293 | } | ||
294 | |||
295 | 128046 | return 0; | |
296 | } | ||
297 | |||
298 | /* init all vlc decoding tables */ | ||
299 | 26 | static av_cold void msmpeg4_decode_init_static(void) | |
300 | { | ||
301 | static VLCElem vlc_buf[3714 + 2694 + 1636 + 2648 + 1532 + 2488]; | ||
302 | 26 | VLCInitState state = VLC_INIT_STATE(vlc_buf); | |
303 | MVTable *mv; | ||
304 | |||
305 | 26 | INIT_FIRST_VLC_RL(ff_rl_table[0], 642); | |
306 | 26 | INIT_FIRST_VLC_RL(ff_rl_table[1], 1104); | |
307 | 26 | INIT_FIRST_VLC_RL(ff_rl_table[2], 554); | |
308 |
2/2✓ Branch 0 taken 832 times.
✓ Branch 1 taken 26 times.
|
858 | VLC_INIT_RL(ff_rl_table[3], 940); |
309 |
2/2✓ Branch 0 taken 832 times.
✓ Branch 1 taken 26 times.
|
858 | VLC_INIT_RL(ff_rl_table[4], 962); |
310 | /* ff_rl_table[5] coincides with ff_h263_rl_inter which has just been | ||
311 | * initialized in ff_h263_decode_init() earlier. So just copy the VLCs. */ | ||
312 | av_assert1(ff_h263_rl_inter.rl_vlc[0]); | ||
313 | 26 | memcpy(ff_rl_table[5].rl_vlc, ff_h263_rl_inter.rl_vlc, sizeof(ff_rl_table[5].rl_vlc)); | |
314 | |||
315 | 26 | VLC_INIT_STATIC_TABLE(v2_dc_lum_vlc, MSMP4_DC_VLC_BITS, 512, | |
316 | &ff_v2_dc_lum_table[0][1], 8, 4, | ||
317 | &ff_v2_dc_lum_table[0][0], 8, 4, 0); | ||
318 | 26 | VLC_INIT_STATIC_TABLE(v2_dc_chroma_vlc, MSMP4_DC_VLC_BITS, 512, | |
319 | &ff_v2_dc_chroma_table[0][1], 8, 4, | ||
320 | &ff_v2_dc_chroma_table[0][0], 8, 4, 0); | ||
321 | |||
322 | 26 | VLC_INIT_STATIC_TABLE(v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4, | |
323 | &ff_v2_intra_cbpc[0][1], 2, 1, | ||
324 | &ff_v2_intra_cbpc[0][0], 2, 1, 0); | ||
325 | 26 | VLC_INIT_STATIC_TABLE(v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8, | |
326 | &ff_v2_mb_type[0][1], 2, 1, | ||
327 | &ff_v2_mb_type[0][0], 2, 1, 0); | ||
328 | |||
329 | 26 | mv = &ff_mv_tables[0]; | |
330 | 52 | mv->vlc = ff_vlc_init_tables_sparse(&state, MV_VLC_BITS, | |
331 | MSMPEG4_MV_TABLES_NB_ELEMS + 1, | ||
332 | 26 | mv->table_mv_bits, 1, 1, | |
333 | 26 | mv->table_mv_code, 2, 2, | |
334 | NULL, 0, 0, 0); | ||
335 | 26 | mv = &ff_mv_tables[1]; | |
336 | 52 | mv->vlc = ff_vlc_init_tables_sparse(&state, MV_VLC_BITS, | |
337 | MSMPEG4_MV_TABLES_NB_ELEMS + 1, | ||
338 | 26 | mv->table_mv_bits, 1, 1, | |
339 | 26 | mv->table_mv_code, 2, 2, | |
340 | NULL, 0, 0, 0); | ||
341 | |||
342 |
2/2✓ Branch 0 taken 104 times.
✓ Branch 1 taken 26 times.
|
130 | for (unsigned i = 0; i < 4; i++) { |
343 | 104 | ff_mb_non_intra_vlc[i] = | |
344 | 104 | ff_vlc_init_tables_sparse(&state, MB_NON_INTRA_VLC_BITS, 128, | |
345 | 104 | &ff_wmv2_inter_table[i][0][1], 8, 4, | |
346 | 104 | &ff_wmv2_inter_table[i][0][0], 8, 4, | |
347 | NULL, 0, 0, 0); | ||
348 | } | ||
349 | |||
350 | 26 | VLC_INIT_STATIC_TABLE(ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4, | |
351 | &ff_table_inter_intra[0][1], 2, 1, | ||
352 | &ff_table_inter_intra[0][0], 2, 1, 0); | ||
353 | 26 | ff_msmp4_vc1_vlcs_init_once(); | |
354 | 26 | } | |
355 | |||
356 | 45 | av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx) | |
357 | { | ||
358 | static AVOnce init_static_once = AV_ONCE_INIT; | ||
359 | 45 | MpegEncContext *s = avctx->priv_data; | |
360 | int ret; | ||
361 | |||
362 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
|
45 | if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0) |
363 | ✗ | return ret; | |
364 | |||
365 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
|
45 | if (ff_h263_decode_init(avctx) < 0) |
366 | ✗ | return -1; | |
367 | |||
368 | 45 | ff_msmpeg4_common_init(s); | |
369 | |||
370 |
3/4✓ Branch 0 taken 11 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
45 | switch (s->msmpeg4_version) { |
371 | 11 | case MSMP4_V1: | |
372 | case MSMP4_V2: | ||
373 | 11 | s->decode_mb= msmpeg4v12_decode_mb; | |
374 | 11 | break; | |
375 | 22 | case MSMP4_V3: | |
376 | case MSMP4_WMV1: | ||
377 | 22 | s->decode_mb= msmpeg4v34_decode_mb; | |
378 | 22 | break; | |
379 | 12 | case MSMP4_WMV2: | |
380 | 12 | break; | |
381 | } | ||
382 | |||
383 | 45 | s->slice_height= s->mb_height; //to avoid 1/0 if the first frame is not a keyframe | |
384 | |||
385 | 45 | ff_thread_once(&init_static_once, msmpeg4_decode_init_static); | |
386 | |||
387 | 45 | return 0; | |
388 | } | ||
389 | |||
390 | 675 | int ff_msmpeg4_decode_picture_header(MpegEncContext * s) | |
391 | { | ||
392 | int code; | ||
393 | |||
394 | // at minimum one bit per macroblock is required at least in a valid frame, | ||
395 | // we discard frames much smaller than this. Frames smaller than 1/8 of the | ||
396 | // smallest "black/skip" frame generally contain not much recoverable content | ||
397 | // while at the same time they have the highest computational requirements | ||
398 | // per byte | ||
399 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 675 times.
|
675 | if (get_bits_left(&s->gb) * 8LL < (s->width+15)/16 * ((s->height+15)/16)) |
400 | ✗ | return AVERROR_INVALIDDATA; | |
401 | |||
402 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 625 times.
|
675 | if (s->msmpeg4_version == MSMP4_V1) { |
403 | 50 | int start_code = get_bits_long(&s->gb, 32); | |
404 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | if(start_code!=0x00000100){ |
405 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n"); | |
406 | ✗ | return -1; | |
407 | } | ||
408 | |||
409 | 50 | skip_bits(&s->gb, 5); // frame number */ | |
410 | } | ||
411 | |||
412 | 675 | s->pict_type = get_bits(&s->gb, 2) + 1; | |
413 |
2/2✓ Branch 0 taken 611 times.
✓ Branch 1 taken 64 times.
|
675 | if (s->pict_type != AV_PICTURE_TYPE_I && |
414 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 611 times.
|
611 | s->pict_type != AV_PICTURE_TYPE_P){ |
415 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n"); | |
416 | ✗ | return -1; | |
417 | } | ||
418 | 675 | s->chroma_qscale= s->qscale = get_bits(&s->gb, 5); | |
419 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 675 times.
|
675 | if(s->qscale==0){ |
420 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n"); | |
421 | ✗ | return -1; | |
422 | } | ||
423 | |||
424 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 611 times.
|
675 | if (s->pict_type == AV_PICTURE_TYPE_I) { |
425 | 64 | code = get_bits(&s->gb, 5); | |
426 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 63 times.
|
64 | if (s->msmpeg4_version == MSMP4_V1) { |
427 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if(code==0 || code>s->mb_height){ |
428 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code); | |
429 | ✗ | return -1; | |
430 | } | ||
431 | |||
432 | 1 | s->slice_height = code; | |
433 | }else{ | ||
434 | /* 0x17: one slice, 0x18: two slices, ... */ | ||
435 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
|
63 | if (code < 0x17){ |
436 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code); | |
437 | ✗ | return -1; | |
438 | } | ||
439 | |||
440 | 63 | s->slice_height = s->mb_height / (code - 0x16); | |
441 | } | ||
442 | |||
443 |
3/4✓ Branch 0 taken 21 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
|
64 | switch(s->msmpeg4_version){ |
444 | 21 | case MSMP4_V1: | |
445 | case MSMP4_V2: | ||
446 | 21 | s->rl_chroma_table_index = 2; | |
447 | 21 | s->rl_table_index = 2; | |
448 | |||
449 | 21 | s->dc_table_index = 0; //not used | |
450 | 21 | break; | |
451 | 23 | case MSMP4_V3: | |
452 | 23 | s->rl_chroma_table_index = decode012(&s->gb); | |
453 | 23 | s->rl_table_index = decode012(&s->gb); | |
454 | |||
455 | 23 | s->dc_table_index = get_bits1(&s->gb); | |
456 | 23 | break; | |
457 | 20 | case MSMP4_WMV1: | |
458 | 20 | ff_msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8); | |
459 | |||
460 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb); |
461 | ✗ | else s->per_mb_rl_table= 0; | |
462 | |||
463 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | if(!s->per_mb_rl_table){ |
464 | 20 | s->rl_chroma_table_index = decode012(&s->gb); | |
465 | 20 | s->rl_table_index = decode012(&s->gb); | |
466 | } | ||
467 | |||
468 | 20 | s->dc_table_index = get_bits1(&s->gb); | |
469 | 20 | s->inter_intra_pred= 0; | |
470 | 20 | break; | |
471 | } | ||
472 | 64 | s->no_rounding = 1; | |
473 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | if(s->avctx->debug&FF_DEBUG_PICT_INFO) |
474 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n", | |
475 | s->qscale, | ||
476 | s->rl_chroma_table_index, | ||
477 | s->rl_table_index, | ||
478 | s->dc_table_index, | ||
479 | s->per_mb_rl_table, | ||
480 | s->slice_height); | ||
481 | } else { | ||
482 |
3/4✓ Branch 0 taken 229 times.
✓ Branch 1 taken 202 times.
✓ Branch 2 taken 180 times.
✗ Branch 3 not taken.
|
611 | switch(s->msmpeg4_version){ |
483 | 229 | case MSMP4_V1: | |
484 | case MSMP4_V2: | ||
485 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 180 times.
|
229 | if (s->msmpeg4_version == MSMP4_V1) |
486 | 49 | s->use_skip_mb_code = 1; | |
487 | else | ||
488 | 180 | s->use_skip_mb_code = get_bits1(&s->gb); | |
489 | 229 | s->rl_table_index = 2; | |
490 | 229 | s->rl_chroma_table_index = s->rl_table_index; | |
491 | 229 | s->dc_table_index = 0; //not used | |
492 | 229 | s->mv_table_index = 0; | |
493 | 229 | break; | |
494 | 202 | case MSMP4_V3: | |
495 | 202 | s->use_skip_mb_code = get_bits1(&s->gb); | |
496 | 202 | s->rl_table_index = decode012(&s->gb); | |
497 | 202 | s->rl_chroma_table_index = s->rl_table_index; | |
498 | |||
499 | 202 | s->dc_table_index = get_bits1(&s->gb); | |
500 | |||
501 | 202 | s->mv_table_index = get_bits1(&s->gb); | |
502 | 202 | break; | |
503 | 180 | case MSMP4_WMV1: | |
504 | 180 | s->use_skip_mb_code = get_bits1(&s->gb); | |
505 | |||
506 |
1/2✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
|
180 | if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb); |
507 | ✗ | else s->per_mb_rl_table= 0; | |
508 | |||
509 |
1/2✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
|
180 | if(!s->per_mb_rl_table){ |
510 | 180 | s->rl_table_index = decode012(&s->gb); | |
511 | 180 | s->rl_chroma_table_index = s->rl_table_index; | |
512 | } | ||
513 | |||
514 | 180 | s->dc_table_index = get_bits1(&s->gb); | |
515 | |||
516 | 180 | s->mv_table_index = get_bits1(&s->gb); | |
517 |
3/4✓ Branch 0 taken 45 times.
✓ Branch 1 taken 135 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 45 times.
|
180 | s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE); |
518 | 180 | break; | |
519 | } | ||
520 | |||
521 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 611 times.
|
611 | if(s->avctx->debug&FF_DEBUG_PICT_INFO) |
522 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n", | |
523 | s->use_skip_mb_code, | ||
524 | s->rl_table_index, | ||
525 | s->rl_chroma_table_index, | ||
526 | s->dc_table_index, | ||
527 | s->mv_table_index, | ||
528 | s->per_mb_rl_table, | ||
529 | s->qscale); | ||
530 | |||
531 |
2/2✓ Branch 0 taken 382 times.
✓ Branch 1 taken 229 times.
|
611 | if(s->flipflop_rounding){ |
532 | 382 | s->no_rounding ^= 1; | |
533 | }else{ | ||
534 | 229 | s->no_rounding = 0; | |
535 | } | ||
536 | } | ||
537 | ff_dlog(s->avctx, "%d %"PRId64" %d %d %d\n", s->pict_type, s->bit_rate, | ||
538 | s->inter_intra_pred, s->width, s->height); | ||
539 | |||
540 | 675 | s->esc3_level_length= 0; | |
541 | 675 | s->esc3_run_length= 0; | |
542 | |||
543 | 675 | return 0; | |
544 | } | ||
545 | |||
546 | 64 | int ff_msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size) | |
547 | { | ||
548 | 64 | int left= buf_size*8 - get_bits_count(&s->gb); | |
549 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 21 times.
|
64 | int length = s->msmpeg4_version >= MSMP4_V3 ? 17 : 16; |
550 | /* the alt_bitstream reader could read over the end so we need to check it */ | ||
551 |
3/4✓ Branch 0 taken 63 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
|
64 | if(left>=length && left<length+8) |
552 | { | ||
553 | 63 | skip_bits(&s->gb, 5); /* fps */ | |
554 | 63 | s->bit_rate= get_bits(&s->gb, 11)*1024; | |
555 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 20 times.
|
63 | if (s->msmpeg4_version >= MSMP4_V3) |
556 | 43 | s->flipflop_rounding= get_bits1(&s->gb); | |
557 | else | ||
558 | 20 | s->flipflop_rounding= 0; | |
559 | } | ||
560 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | else if(left<length+8) |
561 | { | ||
562 | 1 | s->flipflop_rounding= 0; | |
563 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (s->msmpeg4_version != MSMP4_V2) |
564 | 1 | av_log(s->avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left); | |
565 | } | ||
566 | else | ||
567 | { | ||
568 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "I-frame too long, ignoring ext header\n"); | |
569 | } | ||
570 | |||
571 | 64 | return 0; | |
572 | } | ||
573 | |||
574 | 230712 | static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) | |
575 | { | ||
576 | int level, pred; | ||
577 | |||
578 |
2/2✓ Branch 0 taken 54342 times.
✓ Branch 1 taken 176370 times.
|
230712 | if (s->msmpeg4_version <= MSMP4_V2) { |
579 |
2/2✓ Branch 0 taken 36228 times.
✓ Branch 1 taken 18114 times.
|
54342 | if (n < 4) { |
580 | 36228 | level = get_vlc2(&s->gb, v2_dc_lum_vlc, MSMP4_DC_VLC_BITS, 3); | |
581 | } else { | ||
582 | 18114 | level = get_vlc2(&s->gb, v2_dc_chroma_vlc, MSMP4_DC_VLC_BITS, 3); | |
583 | } | ||
584 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54342 times.
|
54342 | if (level < 0) { |
585 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n"); | |
586 | ✗ | *dir_ptr = 0; | |
587 | ✗ | return -1; | |
588 | } | ||
589 | 54342 | level-=256; | |
590 | } else { | ||
591 | 176370 | level = get_vlc2(&s->gb, ff_msmp4_dc_vlc[s->dc_table_index][n >= 4], | |
592 | MSMP4_DC_VLC_BITS, 3); | ||
593 | |||
594 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 176322 times.
|
176370 | if (level == DC_MAX) { |
595 | 48 | level = get_bits(&s->gb, 8); | |
596 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 42 times.
|
48 | if (get_bits1(&s->gb)) |
597 | 6 | level = -level; | |
598 |
2/2✓ Branch 0 taken 153813 times.
✓ Branch 1 taken 22509 times.
|
176322 | } else if (level != 0) { |
599 |
2/2✓ Branch 1 taken 74512 times.
✓ Branch 2 taken 79301 times.
|
153813 | if (get_bits1(&s->gb)) |
600 | 74512 | level = -level; | |
601 | } | ||
602 | } | ||
603 | |||
604 |
2/2✓ Branch 0 taken 5490 times.
✓ Branch 1 taken 225222 times.
|
230712 | if (s->msmpeg4_version == MSMP4_V1) { |
605 | int32_t *dc_val; | ||
606 | 5490 | pred = msmpeg4v1_pred_dc(s, n, &dc_val); | |
607 | 5490 | level += pred; | |
608 | |||
609 | /* update predictor */ | ||
610 | 5490 | *dc_val= level; | |
611 | }else{ | ||
612 | int16_t *dc_val; | ||
613 | 225222 | pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
614 | 225222 | level += pred; | |
615 | |||
616 | /* update predictor */ | ||
617 |
2/2✓ Branch 0 taken 150148 times.
✓ Branch 1 taken 75074 times.
|
225222 | if (n < 4) { |
618 | 150148 | *dc_val = level * s->y_dc_scale; | |
619 | } else { | ||
620 | 75074 | *dc_val = level * s->c_dc_scale; | |
621 | } | ||
622 | } | ||
623 | |||
624 | 230712 | return level; | |
625 | } | ||
626 | |||
627 | 1472610 | int ff_msmpeg4_decode_block(MpegEncContext * s, int16_t * block, | |
628 | int n, int coded, const uint8_t *scan_table) | ||
629 | { | ||
630 | int level, i, last, run, run_diff; | ||
631 | 1472610 | int dc_pred_dir = -1; //unused but its passed around, so it needs to be initialized | |
632 | const RLTable *rl; | ||
633 | const RL_VLC_ELEM *rl_vlc; | ||
634 | int qmul, qadd; | ||
635 | |||
636 |
2/2✓ Branch 0 taken 230712 times.
✓ Branch 1 taken 1241898 times.
|
1472610 | if (s->mb_intra) { |
637 | 230712 | qmul=1; | |
638 | 230712 | qadd=0; | |
639 | |||
640 | /* DC coef */ | ||
641 | 230712 | level = msmpeg4_decode_dc(s, n, &dc_pred_dir); | |
642 | |||
643 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 230712 times.
|
230712 | if (level < 0){ |
644 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale); | |
645 | ✗ | if(s->inter_intra_pred) level=0; | |
646 | } | ||
647 |
2/2✓ Branch 0 taken 153808 times.
✓ Branch 1 taken 76904 times.
|
230712 | if (n < 4) { |
648 | 153808 | rl = &ff_rl_table[s->rl_table_index]; | |
649 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 153808 times.
|
153808 | if(level > 256*s->y_dc_scale){ |
650 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale); | |
651 | ✗ | if(!s->inter_intra_pred) return -1; | |
652 | } | ||
653 | } else { | ||
654 | 76904 | rl = &ff_rl_table[3 + s->rl_chroma_table_index]; | |
655 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 76904 times.
|
76904 | if(level > 256*s->c_dc_scale){ |
656 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale); | |
657 | ✗ | if(!s->inter_intra_pred) return -1; | |
658 | } | ||
659 | } | ||
660 | 230712 | block[0] = level; | |
661 | |||
662 | 230712 | run_diff = s->msmpeg4_version >= MSMP4_WMV1; | |
663 | 230712 | i = 0; | |
664 |
2/2✓ Branch 0 taken 42456 times.
✓ Branch 1 taken 188256 times.
|
230712 | if (!coded) { |
665 | 42456 | goto not_coded; | |
666 | } | ||
667 |
2/2✓ Branch 0 taken 3224 times.
✓ Branch 1 taken 185032 times.
|
188256 | if (s->ac_pred) { |
668 |
2/2✓ Branch 0 taken 1928 times.
✓ Branch 1 taken 1296 times.
|
3224 | if (dc_pred_dir == 0) |
669 | 1928 | scan_table = s->permutated_intra_v_scantable; /* left */ | |
670 | else | ||
671 | 1296 | scan_table = s->permutated_intra_h_scantable; /* top */ | |
672 | } else { | ||
673 | 185032 | scan_table = s->intra_scantable.permutated; | |
674 | } | ||
675 | 188256 | rl_vlc= rl->rl_vlc[0]; | |
676 | } else { | ||
677 | 1241898 | qmul = s->qscale << 1; | |
678 | 1241898 | qadd = (s->qscale - 1) | 1; | |
679 | 1241898 | i = -1; | |
680 | 1241898 | rl = &ff_rl_table[3 + s->rl_table_index]; | |
681 | |||
682 |
2/2✓ Branch 0 taken 306018 times.
✓ Branch 1 taken 935880 times.
|
1241898 | if (s->msmpeg4_version == MSMP4_V2) |
683 | 306018 | run_diff = 0; | |
684 | else | ||
685 | 935880 | run_diff = 1; | |
686 | |||
687 |
2/2✓ Branch 0 taken 633723 times.
✓ Branch 1 taken 608175 times.
|
1241898 | if (!coded) { |
688 | 633723 | s->block_last_index[n] = i; | |
689 | 633723 | return 0; | |
690 | } | ||
691 |
2/2✓ Branch 0 taken 420993 times.
✓ Branch 1 taken 187182 times.
|
608175 | if(!scan_table) |
692 | 420993 | scan_table = s->inter_scantable.permutated; | |
693 | 608175 | rl_vlc= rl->rl_vlc[s->qscale]; | |
694 | } | ||
695 | { | ||
696 | 796431 | OPEN_READER(re, &s->gb); | |
697 | for(;;) { | ||
698 | 9175903 | UPDATE_CACHE(re, &s->gb); | |
699 |
2/2✓ Branch 1 taken 297834 times.
✓ Branch 2 taken 4688333 times.
|
4986167 | GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0); |
700 |
2/2✓ Branch 0 taken 162066 times.
✓ Branch 1 taken 4824101 times.
|
4986167 | if (level==0) { |
701 | int cache; | ||
702 | 162066 | cache= GET_CACHE(re, &s->gb); | |
703 | /* escape */ | ||
704 |
4/4✓ Branch 0 taken 140175 times.
✓ Branch 1 taken 21891 times.
✓ Branch 2 taken 74501 times.
✓ Branch 3 taken 65674 times.
|
162066 | if (s->msmpeg4_version == MSMP4_V1 || (cache&0x80000000)==0) { |
705 |
4/4✓ Branch 0 taken 74501 times.
✓ Branch 1 taken 21891 times.
✓ Branch 2 taken 19659 times.
✓ Branch 3 taken 54842 times.
|
96392 | if (s->msmpeg4_version == MSMP4_V1 || (cache&0x40000000)==0) { |
706 | /* third escape */ | ||
707 |
2/2✓ Branch 0 taken 19659 times.
✓ Branch 1 taken 21891 times.
|
41550 | if (s->msmpeg4_version != MSMP4_V1) |
708 | 19659 | LAST_SKIP_BITS(re, &s->gb, 2); | |
709 | 41550 | UPDATE_CACHE(re, &s->gb); | |
710 |
2/2✓ Branch 0 taken 34734 times.
✓ Branch 1 taken 6816 times.
|
41550 | if (s->msmpeg4_version <= MSMP4_V3) { |
711 | 34734 | last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1); | |
712 | 34734 | run= SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6); | |
713 | 34734 | level= SHOW_SBITS(re, &s->gb, 8); | |
714 | 34734 | SKIP_COUNTER(re, &s->gb, 1+6+8); | |
715 | }else{ | ||
716 | int sign; | ||
717 | 6816 | last= SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1); | |
718 |
2/2✓ Branch 0 taken 384 times.
✓ Branch 1 taken 6432 times.
|
6816 | if(!s->esc3_level_length){ |
719 | int ll; | ||
720 | ff_dlog(s->avctx, "ESC-3 %X at %d %d\n", | ||
721 | show_bits(&s->gb, 24), s->mb_x, s->mb_y); | ||
722 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 306 times.
|
384 | if(s->qscale<8){ |
723 | 78 | ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3); | |
724 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
|
78 | if(ll==0){ |
725 | ✗ | ll= 8+SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1); | |
726 | } | ||
727 | }else{ | ||
728 | 306 | ll=2; | |
729 |
4/4✓ Branch 0 taken 1734 times.
✓ Branch 1 taken 280 times.
✓ Branch 3 taken 1708 times.
✓ Branch 4 taken 26 times.
|
2014 | while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){ |
730 | 1708 | ll++; | |
731 | 1708 | SKIP_BITS(re, &s->gb, 1); | |
732 | } | ||
733 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 280 times.
|
306 | if(ll<8) SKIP_BITS(re, &s->gb, 1); |
734 | } | ||
735 | |||
736 | 384 | s->esc3_level_length= ll; | |
737 | 384 | s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2); | |
738 | 384 | UPDATE_CACHE(re, &s->gb); | |
739 | } | ||
740 | 6816 | run= SHOW_UBITS(re, &s->gb, s->esc3_run_length); | |
741 | 6816 | SKIP_BITS(re, &s->gb, s->esc3_run_length); | |
742 | |||
743 | 6816 | sign= SHOW_UBITS(re, &s->gb, 1); | |
744 | 6816 | SKIP_BITS(re, &s->gb, 1); | |
745 | |||
746 | 6816 | level= SHOW_UBITS(re, &s->gb, s->esc3_level_length); | |
747 | 6816 | SKIP_BITS(re, &s->gb, s->esc3_level_length); | |
748 |
2/2✓ Branch 0 taken 3395 times.
✓ Branch 1 taken 3421 times.
|
6816 | if(sign) level= -level; |
749 | } | ||
750 | |||
751 | //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ; | ||
752 |
2/2✓ Branch 0 taken 20943 times.
✓ Branch 1 taken 20607 times.
|
41550 | if (level>0) level= level * qmul + qadd; |
753 | 20607 | else level= level * qmul - qadd; | |
754 | 41550 | i+= run + 1; | |
755 |
2/2✓ Branch 0 taken 18888 times.
✓ Branch 1 taken 22662 times.
|
41550 | if(last) i+=192; |
756 | } else { | ||
757 | /* second escape */ | ||
758 | 54842 | SKIP_BITS(re, &s->gb, 2); | |
759 |
2/2✓ Branch 1 taken 7490 times.
✓ Branch 2 taken 47352 times.
|
54842 | GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1); |
760 | 54842 | i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing | |
761 | 54842 | level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); | |
762 | 54842 | LAST_SKIP_BITS(re, &s->gb, 1); | |
763 | } | ||
764 | } else { | ||
765 | /* first escape */ | ||
766 | 65674 | SKIP_BITS(re, &s->gb, 1); | |
767 |
2/2✓ Branch 1 taken 8895 times.
✓ Branch 2 taken 56779 times.
|
65674 | GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1); |
768 | 65674 | i+= run; | |
769 | 65674 | level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing | |
770 | 65674 | level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); | |
771 | 65674 | LAST_SKIP_BITS(re, &s->gb, 1); | |
772 | } | ||
773 | } else { | ||
774 | 4824101 | i+= run; | |
775 | 4824101 | level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); | |
776 | 4824101 | LAST_SKIP_BITS(re, &s->gb, 1); | |
777 | } | ||
778 |
2/2✓ Branch 0 taken 796431 times.
✓ Branch 1 taken 4189736 times.
|
4986167 | if (i > 62){ |
779 | 796431 | i-= 192; | |
780 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 796431 times.
|
796431 | if(i&(~63)){ |
781 | ✗ | const int left= get_bits_left(&s->gb); | |
782 | ✗ | if (((i + 192 == 64 && level / qmul == -1) || | |
783 | ✗ | !(s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))) && | |
784 | left >= 0) { | ||
785 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y); | |
786 | ✗ | i = 63; | |
787 | ✗ | break; | |
788 | }else{ | ||
789 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); | |
790 | ✗ | return -1; | |
791 | } | ||
792 | } | ||
793 | |||
794 | 796431 | block[scan_table[i]] = level; | |
795 | 796431 | break; | |
796 | } | ||
797 | |||
798 | 4189736 | block[scan_table[i]] = level; | |
799 | } | ||
800 | 796431 | CLOSE_READER(re, &s->gb); | |
801 | } | ||
802 | 838887 | not_coded: | |
803 |
2/2✓ Branch 0 taken 230712 times.
✓ Branch 1 taken 608175 times.
|
838887 | if (s->mb_intra) { |
804 | 230712 | ff_mpeg4_pred_ac(s, block, n, dc_pred_dir); | |
805 |
2/2✓ Branch 0 taken 4194 times.
✓ Branch 1 taken 226518 times.
|
230712 | if (s->ac_pred) { |
806 | 4194 | i = 63; /* XXX: not optimal */ | |
807 | } | ||
808 | } | ||
809 |
4/4✓ Branch 0 taken 411234 times.
✓ Branch 1 taken 427653 times.
✓ Branch 2 taken 358446 times.
✓ Branch 3 taken 52788 times.
|
838887 | if (s->msmpeg4_version >= MSMP4_WMV1 && i > 0) i=63; //FIXME/XXX optimize |
810 | 838887 | s->block_last_index[n] = i; | |
811 | |||
812 | 838887 | return 0; | |
813 | } | ||
814 | |||
815 | 228076 | void ff_msmpeg4_decode_motion(MpegEncContext *s, int *mx_ptr, int *my_ptr) | |
816 | { | ||
817 | const MVTable *mv; | ||
818 | int code, mx, my; | ||
819 | |||
820 | 228076 | mv = &ff_mv_tables[s->mv_table_index]; | |
821 | |||
822 | 228076 | code = get_vlc2(&s->gb, mv->vlc, MV_VLC_BITS, 2); | |
823 |
2/2✓ Branch 0 taken 1856 times.
✓ Branch 1 taken 226220 times.
|
228076 | if (code == MSMPEG4_MV_TABLES_NB_ELEMS) { |
824 | 1856 | mx = get_bits(&s->gb, 6); | |
825 | 1856 | my = get_bits(&s->gb, 6); | |
826 | } else { | ||
827 | 226220 | mx = mv->table_mvx[code]; | |
828 | 226220 | my = mv->table_mvy[code]; | |
829 | } | ||
830 | |||
831 | 228076 | mx += *mx_ptr - 32; | |
832 | 228076 | my += *my_ptr - 32; | |
833 | /* WARNING : they do not do exactly modulo encoding */ | ||
834 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 228054 times.
|
228076 | if (mx <= -64) |
835 | 22 | mx += 64; | |
836 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 228044 times.
|
228054 | else if (mx >= 64) |
837 | 10 | mx -= 64; | |
838 | |||
839 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 228070 times.
|
228076 | if (my <= -64) |
840 | 6 | my += 64; | |
841 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 228066 times.
|
228070 | else if (my >= 64) |
842 | 4 | my -= 64; | |
843 | 228076 | *mx_ptr = mx; | |
844 | 228076 | *my_ptr = my; | |
845 | 228076 | } | |
846 | |||
847 | const FFCodec ff_msmpeg4v1_decoder = { | ||
848 | .p.name = "msmpeg4v1", | ||
849 | CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 1"), | ||
850 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
851 | .p.id = AV_CODEC_ID_MSMPEG4V1, | ||
852 | .priv_data_size = sizeof(MpegEncContext), | ||
853 | .init = ff_msmpeg4_decode_init, | ||
854 | FF_CODEC_DECODE_CB(ff_h263_decode_frame), | ||
855 | .close = ff_mpv_decode_close, | ||
856 | .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1, | ||
857 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | | ||
858 | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, | ||
859 | .p.max_lowres = 3, | ||
860 | }; | ||
861 | |||
862 | const FFCodec ff_msmpeg4v2_decoder = { | ||
863 | .p.name = "msmpeg4v2", | ||
864 | CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 2"), | ||
865 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
866 | .p.id = AV_CODEC_ID_MSMPEG4V2, | ||
867 | .priv_data_size = sizeof(MpegEncContext), | ||
868 | .init = ff_msmpeg4_decode_init, | ||
869 | FF_CODEC_DECODE_CB(ff_h263_decode_frame), | ||
870 | .close = ff_mpv_decode_close, | ||
871 | .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1, | ||
872 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | | ||
873 | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, | ||
874 | .p.max_lowres = 3, | ||
875 | }; | ||
876 | |||
877 | const FFCodec ff_msmpeg4v3_decoder = { | ||
878 | .p.name = "msmpeg4", | ||
879 | CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 3"), | ||
880 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
881 | .p.id = AV_CODEC_ID_MSMPEG4V3, | ||
882 | .priv_data_size = sizeof(MpegEncContext), | ||
883 | .init = ff_msmpeg4_decode_init, | ||
884 | FF_CODEC_DECODE_CB(ff_h263_decode_frame), | ||
885 | .close = ff_mpv_decode_close, | ||
886 | .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1, | ||
887 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | | ||
888 | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, | ||
889 | .p.max_lowres = 3, | ||
890 | }; | ||
891 | |||
892 | const FFCodec ff_wmv1_decoder = { | ||
893 | .p.name = "wmv1", | ||
894 | CODEC_LONG_NAME("Windows Media Video 7"), | ||
895 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
896 | .p.id = AV_CODEC_ID_WMV1, | ||
897 | .priv_data_size = sizeof(MpegEncContext), | ||
898 | .init = ff_msmpeg4_decode_init, | ||
899 | FF_CODEC_DECODE_CB(ff_h263_decode_frame), | ||
900 | .close = ff_mpv_decode_close, | ||
901 | .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1, | ||
902 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | | ||
903 | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, | ||
904 | .p.max_lowres = 3, | ||
905 | }; | ||
906 |