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