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 | 45 | ff_msmpeg4_common_init(s); | |
370 | |||
371 |
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) { |
372 | 11 | case MSMP4_V1: | |
373 | case MSMP4_V2: | ||
374 | 11 | s->decode_mb= msmpeg4v12_decode_mb; | |
375 | 11 | break; | |
376 | 22 | case MSMP4_V3: | |
377 | case MSMP4_WMV1: | ||
378 | 22 | s->decode_mb= msmpeg4v34_decode_mb; | |
379 | 22 | break; | |
380 | 12 | case MSMP4_WMV2: | |
381 | 12 | break; | |
382 | } | ||
383 | |||
384 | 45 | s->slice_height= s->mb_height; //to avoid 1/0 if the first frame is not a keyframe | |
385 | |||
386 | 45 | ff_thread_once(&init_static_once, msmpeg4_decode_init_static); | |
387 | |||
388 | 45 | return 0; | |
389 | } | ||
390 | |||
391 | 675 | int ff_msmpeg4_decode_picture_header(MpegEncContext * s) | |
392 | { | ||
393 | 675 | MSMP4DecContext *const ms = mpv_to_msmpeg4(s); | |
394 | int code; | ||
395 | |||
396 | // at minimum one bit per macroblock is required at least in a valid frame, | ||
397 | // we discard frames much smaller than this. Frames smaller than 1/8 of the | ||
398 | // smallest "black/skip" frame generally contain not much recoverable content | ||
399 | // while at the same time they have the highest computational requirements | ||
400 | // per byte | ||
401 |
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)) |
402 | ✗ | return AVERROR_INVALIDDATA; | |
403 | |||
404 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 625 times.
|
675 | if (s->msmpeg4_version == MSMP4_V1) { |
405 | 50 | int start_code = get_bits_long(&s->gb, 32); | |
406 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | if(start_code!=0x00000100){ |
407 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n"); | |
408 | ✗ | return -1; | |
409 | } | ||
410 | |||
411 | 50 | skip_bits(&s->gb, 5); // frame number */ | |
412 | } | ||
413 | |||
414 | 675 | s->pict_type = get_bits(&s->gb, 2) + 1; | |
415 |
2/2✓ Branch 0 taken 611 times.
✓ Branch 1 taken 64 times.
|
675 | if (s->pict_type != AV_PICTURE_TYPE_I && |
416 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 611 times.
|
611 | s->pict_type != AV_PICTURE_TYPE_P){ |
417 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n"); | |
418 | ✗ | return -1; | |
419 | } | ||
420 | 675 | s->chroma_qscale= s->qscale = get_bits(&s->gb, 5); | |
421 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 675 times.
|
675 | if(s->qscale==0){ |
422 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n"); | |
423 | ✗ | return -1; | |
424 | } | ||
425 | |||
426 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 611 times.
|
675 | if (s->pict_type == AV_PICTURE_TYPE_I) { |
427 | 64 | code = get_bits(&s->gb, 5); | |
428 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 63 times.
|
64 | if (s->msmpeg4_version == MSMP4_V1) { |
429 |
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){ |
430 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code); | |
431 | ✗ | return -1; | |
432 | } | ||
433 | |||
434 | 1 | s->slice_height = code; | |
435 | }else{ | ||
436 | /* 0x17: one slice, 0x18: two slices, ... */ | ||
437 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
|
63 | if (code < 0x17){ |
438 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code); | |
439 | ✗ | return -1; | |
440 | } | ||
441 | |||
442 | 63 | s->slice_height = s->mb_height / (code - 0x16); | |
443 | } | ||
444 | |||
445 |
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){ |
446 | 21 | case MSMP4_V1: | |
447 | case MSMP4_V2: | ||
448 | 21 | ms->rl_chroma_table_index = 2; | |
449 | 21 | ms->rl_table_index = 2; | |
450 | |||
451 | 21 | ms->dc_table_index = 0; //not used | |
452 | 21 | break; | |
453 | 23 | case MSMP4_V3: | |
454 | 23 | ms->rl_chroma_table_index = decode012(&s->gb); | |
455 | 23 | ms->rl_table_index = decode012(&s->gb); | |
456 | |||
457 | 23 | ms->dc_table_index = get_bits1(&s->gb); | |
458 | 23 | break; | |
459 | 20 | case MSMP4_WMV1: | |
460 | 20 | ff_msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8); | |
461 | |||
462 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | if (ms->bit_rate > MBAC_BITRATE) |
463 | 20 | ms->per_mb_rl_table = get_bits1(&s->gb); | |
464 | else | ||
465 | ✗ | ms->per_mb_rl_table = 0; | |
466 | |||
467 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | if (!ms->per_mb_rl_table) { |
468 | 20 | ms->rl_chroma_table_index = decode012(&s->gb); | |
469 | 20 | ms->rl_table_index = decode012(&s->gb); | |
470 | } | ||
471 | |||
472 | 20 | ms->dc_table_index = get_bits1(&s->gb); | |
473 | 20 | s->inter_intra_pred= 0; | |
474 | 20 | break; | |
475 | } | ||
476 | 64 | s->no_rounding = 1; | |
477 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | if(s->avctx->debug&FF_DEBUG_PICT_INFO) |
478 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n", | |
479 | s->qscale, | ||
480 | ms->rl_chroma_table_index, | ||
481 | ms->rl_table_index, | ||
482 | ms->dc_table_index, | ||
483 | ms->per_mb_rl_table, | ||
484 | s->slice_height); | ||
485 | } else { | ||
486 |
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){ |
487 | 229 | case MSMP4_V1: | |
488 | case MSMP4_V2: | ||
489 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 180 times.
|
229 | if (s->msmpeg4_version == MSMP4_V1) |
490 | 49 | ms->use_skip_mb_code = 1; | |
491 | else | ||
492 | 180 | ms->use_skip_mb_code = get_bits1(&s->gb); | |
493 | 229 | ms->rl_table_index = 2; | |
494 | 229 | ms->rl_chroma_table_index = ms->rl_table_index; | |
495 | 229 | ms->dc_table_index = 0; //not used | |
496 | 229 | ms->mv_table_index = 0; | |
497 | 229 | break; | |
498 | 202 | case MSMP4_V3: | |
499 | 202 | ms->use_skip_mb_code = get_bits1(&s->gb); | |
500 | 202 | ms->rl_table_index = decode012(&s->gb); | |
501 | 202 | ms->rl_chroma_table_index = ms->rl_table_index; | |
502 | |||
503 | 202 | ms->dc_table_index = get_bits1(&s->gb); | |
504 | |||
505 | 202 | ms->mv_table_index = get_bits1(&s->gb); | |
506 | 202 | break; | |
507 | 180 | case MSMP4_WMV1: | |
508 | 180 | ms->use_skip_mb_code = get_bits1(&s->gb); | |
509 | |||
510 |
1/2✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
|
180 | if (ms->bit_rate > MBAC_BITRATE) |
511 | 180 | ms->per_mb_rl_table = get_bits1(&s->gb); | |
512 | else | ||
513 | ✗ | ms->per_mb_rl_table = 0; | |
514 | |||
515 |
1/2✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
|
180 | if (!ms->per_mb_rl_table) { |
516 | 180 | ms->rl_table_index = decode012(&s->gb); | |
517 | 180 | ms->rl_chroma_table_index = ms->rl_table_index; | |
518 | } | ||
519 | |||
520 | 180 | ms->dc_table_index = get_bits1(&s->gb); | |
521 | |||
522 | 180 | ms->mv_table_index = get_bits1(&s->gb); | |
523 |
2/2✓ Branch 0 taken 45 times.
✓ Branch 1 taken 135 times.
|
225 | s->inter_intra_pred = s->width*s->height < 320*240 && |
524 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
|
45 | ms->bit_rate <= II_BITRATE; |
525 | 180 | break; | |
526 | } | ||
527 | |||
528 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 611 times.
|
611 | if(s->avctx->debug&FF_DEBUG_PICT_INFO) |
529 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n", | |
530 | ms->use_skip_mb_code, | ||
531 | ms->rl_table_index, | ||
532 | ms->rl_chroma_table_index, | ||
533 | ms->dc_table_index, | ||
534 | ms->mv_table_index, | ||
535 | ms->per_mb_rl_table, | ||
536 | s->qscale); | ||
537 | |||
538 |
2/2✓ Branch 0 taken 382 times.
✓ Branch 1 taken 229 times.
|
611 | if(s->flipflop_rounding){ |
539 | 382 | s->no_rounding ^= 1; | |
540 | }else{ | ||
541 | 229 | s->no_rounding = 0; | |
542 | } | ||
543 | } | ||
544 | ff_dlog(s->avctx, "%d %d %d %d %d\n", s->pict_type, ms->bit_rate, | ||
545 | s->inter_intra_pred, s->width, s->height); | ||
546 | |||
547 | 675 | ms->esc3_level_length = 0; | |
548 | 675 | ms->esc3_run_length = 0; | |
549 | |||
550 | 675 | return 0; | |
551 | } | ||
552 | |||
553 | 64 | int ff_msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size) | |
554 | { | ||
555 | 64 | MSMP4DecContext *const ms = mpv_to_msmpeg4(s); | |
556 | 64 | int left= buf_size*8 - get_bits_count(&s->gb); | |
557 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 21 times.
|
64 | int length = s->msmpeg4_version >= MSMP4_V3 ? 17 : 16; |
558 | /* the alt_bitstream reader could read over the end so we need to check it */ | ||
559 |
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) |
560 | { | ||
561 | 63 | skip_bits(&s->gb, 5); /* fps */ | |
562 | 63 | ms->bit_rate = get_bits(&s->gb, 11) * 1024; | |
563 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 20 times.
|
63 | if (s->msmpeg4_version >= MSMP4_V3) |
564 | 43 | s->flipflop_rounding= get_bits1(&s->gb); | |
565 | else | ||
566 | 20 | s->flipflop_rounding= 0; | |
567 | } | ||
568 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | else if(left<length+8) |
569 | { | ||
570 | 1 | s->flipflop_rounding= 0; | |
571 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (s->msmpeg4_version != MSMP4_V2) |
572 | 1 | av_log(s->avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left); | |
573 | } | ||
574 | else | ||
575 | { | ||
576 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "I-frame too long, ignoring ext header\n"); | |
577 | } | ||
578 | |||
579 | 64 | return 0; | |
580 | } | ||
581 | |||
582 | 230712 | static int msmpeg4_decode_dc(MSMP4DecContext *const ms, int n, int *dir_ptr) | |
583 | { | ||
584 | 230712 | MpegEncContext *const s = &ms->m; | |
585 | int level, pred; | ||
586 | |||
587 |
2/2✓ Branch 0 taken 54342 times.
✓ Branch 1 taken 176370 times.
|
230712 | if (s->msmpeg4_version <= MSMP4_V2) { |
588 |
2/2✓ Branch 0 taken 36228 times.
✓ Branch 1 taken 18114 times.
|
54342 | if (n < 4) { |
589 | 36228 | level = get_vlc2(&s->gb, v2_dc_lum_vlc, MSMP4_DC_VLC_BITS, 3); | |
590 | } else { | ||
591 | 18114 | level = get_vlc2(&s->gb, v2_dc_chroma_vlc, MSMP4_DC_VLC_BITS, 3); | |
592 | } | ||
593 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54342 times.
|
54342 | if (level < 0) { |
594 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n"); | |
595 | ✗ | *dir_ptr = 0; | |
596 | ✗ | return -1; | |
597 | } | ||
598 | 54342 | level-=256; | |
599 | } else { | ||
600 | 176370 | level = get_vlc2(&s->gb, ff_msmp4_dc_vlc[ms->dc_table_index][n >= 4], | |
601 | MSMP4_DC_VLC_BITS, 3); | ||
602 | |||
603 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 176322 times.
|
176370 | if (level == DC_MAX) { |
604 | 48 | level = get_bits(&s->gb, 8); | |
605 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 42 times.
|
48 | if (get_bits1(&s->gb)) |
606 | 6 | level = -level; | |
607 |
2/2✓ Branch 0 taken 153813 times.
✓ Branch 1 taken 22509 times.
|
176322 | } else if (level != 0) { |
608 |
2/2✓ Branch 1 taken 74512 times.
✓ Branch 2 taken 79301 times.
|
153813 | if (get_bits1(&s->gb)) |
609 | 74512 | level = -level; | |
610 | } | ||
611 | } | ||
612 | |||
613 |
2/2✓ Branch 0 taken 5490 times.
✓ Branch 1 taken 225222 times.
|
230712 | if (s->msmpeg4_version == MSMP4_V1) { |
614 | int32_t *dc_val; | ||
615 | 5490 | pred = msmpeg4v1_pred_dc(s, n, &dc_val); | |
616 | 5490 | level += pred; | |
617 | |||
618 | /* update predictor */ | ||
619 | 5490 | *dc_val= level; | |
620 | }else{ | ||
621 | int16_t *dc_val; | ||
622 | 225222 | pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
623 | 225222 | level += pred; | |
624 | |||
625 | /* update predictor */ | ||
626 |
2/2✓ Branch 0 taken 150148 times.
✓ Branch 1 taken 75074 times.
|
225222 | if (n < 4) { |
627 | 150148 | *dc_val = level * s->y_dc_scale; | |
628 | } else { | ||
629 | 75074 | *dc_val = level * s->c_dc_scale; | |
630 | } | ||
631 | } | ||
632 | |||
633 | 230712 | return level; | |
634 | } | ||
635 | |||
636 | 1472610 | int ff_msmpeg4_decode_block(MSMP4DecContext *const ms, int16_t * block, | |
637 | int n, int coded, const uint8_t *scan_table) | ||
638 | { | ||
639 | 1472610 | MpegEncContext *const s = &ms->m; | |
640 | int level, i, last, run, run_diff; | ||
641 | 1472610 | int dc_pred_dir = -1; //unused but its passed around, so it needs to be initialized | |
642 | const RLTable *rl; | ||
643 | const RL_VLC_ELEM *rl_vlc; | ||
644 | int qmul, qadd; | ||
645 | |||
646 |
2/2✓ Branch 0 taken 230712 times.
✓ Branch 1 taken 1241898 times.
|
1472610 | if (s->mb_intra) { |
647 | 230712 | qmul=1; | |
648 | 230712 | qadd=0; | |
649 | |||
650 | /* DC coef */ | ||
651 | 230712 | level = msmpeg4_decode_dc(ms, n, &dc_pred_dir); | |
652 | |||
653 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 230712 times.
|
230712 | if (level < 0){ |
654 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale); | |
655 | ✗ | if(s->inter_intra_pred) level=0; | |
656 | } | ||
657 |
2/2✓ Branch 0 taken 153808 times.
✓ Branch 1 taken 76904 times.
|
230712 | if (n < 4) { |
658 | 153808 | rl = &ff_rl_table[ms->rl_table_index]; | |
659 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 153808 times.
|
153808 | if(level > 256*s->y_dc_scale){ |
660 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale); | |
661 | ✗ | if(!s->inter_intra_pred) return -1; | |
662 | } | ||
663 | } else { | ||
664 | 76904 | rl = &ff_rl_table[3 + ms->rl_chroma_table_index]; | |
665 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 76904 times.
|
76904 | if(level > 256*s->c_dc_scale){ |
666 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale); | |
667 | ✗ | if(!s->inter_intra_pred) return -1; | |
668 | } | ||
669 | } | ||
670 | 230712 | block[0] = level; | |
671 | |||
672 | 230712 | run_diff = s->msmpeg4_version >= MSMP4_WMV1; | |
673 | 230712 | i = 0; | |
674 |
2/2✓ Branch 0 taken 42456 times.
✓ Branch 1 taken 188256 times.
|
230712 | if (!coded) { |
675 | 42456 | goto not_coded; | |
676 | } | ||
677 |
2/2✓ Branch 0 taken 3224 times.
✓ Branch 1 taken 185032 times.
|
188256 | if (s->ac_pred) { |
678 |
2/2✓ Branch 0 taken 1928 times.
✓ Branch 1 taken 1296 times.
|
3224 | if (dc_pred_dir == 0) |
679 | 1928 | scan_table = s->permutated_intra_v_scantable; /* left */ | |
680 | else | ||
681 | 1296 | scan_table = s->permutated_intra_h_scantable; /* top */ | |
682 | } else { | ||
683 | 185032 | scan_table = s->intra_scantable.permutated; | |
684 | } | ||
685 | 188256 | rl_vlc= rl->rl_vlc[0]; | |
686 | } else { | ||
687 | 1241898 | qmul = s->qscale << 1; | |
688 | 1241898 | qadd = (s->qscale - 1) | 1; | |
689 | 1241898 | i = -1; | |
690 | 1241898 | rl = &ff_rl_table[3 + ms->rl_table_index]; | |
691 | |||
692 |
2/2✓ Branch 0 taken 306018 times.
✓ Branch 1 taken 935880 times.
|
1241898 | if (s->msmpeg4_version == MSMP4_V2) |
693 | 306018 | run_diff = 0; | |
694 | else | ||
695 | 935880 | run_diff = 1; | |
696 | |||
697 |
2/2✓ Branch 0 taken 633723 times.
✓ Branch 1 taken 608175 times.
|
1241898 | if (!coded) { |
698 | 633723 | s->block_last_index[n] = i; | |
699 | 633723 | return 0; | |
700 | } | ||
701 |
2/2✓ Branch 0 taken 420993 times.
✓ Branch 1 taken 187182 times.
|
608175 | if(!scan_table) |
702 | 420993 | scan_table = s->inter_scantable.permutated; | |
703 | 608175 | rl_vlc= rl->rl_vlc[s->qscale]; | |
704 | } | ||
705 | { | ||
706 | 796431 | OPEN_READER(re, &s->gb); | |
707 | for(;;) { | ||
708 | 9175903 | UPDATE_CACHE(re, &s->gb); | |
709 |
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); |
710 |
2/2✓ Branch 0 taken 162066 times.
✓ Branch 1 taken 4824101 times.
|
4986167 | if (level==0) { |
711 | int cache; | ||
712 | 162066 | cache= GET_CACHE(re, &s->gb); | |
713 | /* escape */ | ||
714 |
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) { |
715 |
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) { |
716 | /* third escape */ | ||
717 |
2/2✓ Branch 0 taken 19659 times.
✓ Branch 1 taken 21891 times.
|
41550 | if (s->msmpeg4_version != MSMP4_V1) |
718 | 19659 | LAST_SKIP_BITS(re, &s->gb, 2); | |
719 | 41550 | UPDATE_CACHE(re, &s->gb); | |
720 |
2/2✓ Branch 0 taken 34734 times.
✓ Branch 1 taken 6816 times.
|
41550 | if (s->msmpeg4_version <= MSMP4_V3) { |
721 | 34734 | last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1); | |
722 | 34734 | run= SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6); | |
723 | 34734 | level= SHOW_SBITS(re, &s->gb, 8); | |
724 | 34734 | SKIP_COUNTER(re, &s->gb, 1+6+8); | |
725 | }else{ | ||
726 | int sign; | ||
727 | 6816 | last= SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1); | |
728 |
2/2✓ Branch 0 taken 384 times.
✓ Branch 1 taken 6432 times.
|
6816 | if (!ms->esc3_level_length) { |
729 | int ll; | ||
730 | ff_dlog(s->avctx, "ESC-3 %X at %d %d\n", | ||
731 | show_bits(&s->gb, 24), s->mb_x, s->mb_y); | ||
732 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 306 times.
|
384 | if(s->qscale<8){ |
733 | 78 | ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3); | |
734 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
|
78 | if(ll==0){ |
735 | ✗ | ll= 8+SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1); | |
736 | } | ||
737 | }else{ | ||
738 | 306 | ll=2; | |
739 |
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){ |
740 | 1708 | ll++; | |
741 | 1708 | SKIP_BITS(re, &s->gb, 1); | |
742 | } | ||
743 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 280 times.
|
306 | if(ll<8) SKIP_BITS(re, &s->gb, 1); |
744 | } | ||
745 | |||
746 | 384 | ms->esc3_level_length = ll; | |
747 | 384 | ms->esc3_run_length = SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2); | |
748 | 384 | UPDATE_CACHE(re, &s->gb); | |
749 | } | ||
750 | 6816 | run = SHOW_UBITS(re, &s->gb, ms->esc3_run_length); | |
751 | 6816 | SKIP_BITS(re, &s->gb, ms->esc3_run_length); | |
752 | |||
753 | 6816 | sign= SHOW_UBITS(re, &s->gb, 1); | |
754 | 6816 | SKIP_BITS(re, &s->gb, 1); | |
755 | |||
756 | 6816 | level = SHOW_UBITS(re, &s->gb, ms->esc3_level_length); | |
757 | 6816 | SKIP_BITS(re, &s->gb, ms->esc3_level_length); | |
758 |
2/2✓ Branch 0 taken 3395 times.
✓ Branch 1 taken 3421 times.
|
6816 | if(sign) level= -level; |
759 | } | ||
760 | |||
761 | //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ; | ||
762 |
2/2✓ Branch 0 taken 20943 times.
✓ Branch 1 taken 20607 times.
|
41550 | if (level>0) level= level * qmul + qadd; |
763 | 20607 | else level= level * qmul - qadd; | |
764 | 41550 | i+= run + 1; | |
765 |
2/2✓ Branch 0 taken 18888 times.
✓ Branch 1 taken 22662 times.
|
41550 | if(last) i+=192; |
766 | } else { | ||
767 | /* second escape */ | ||
768 | 54842 | SKIP_BITS(re, &s->gb, 2); | |
769 |
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); |
770 | 54842 | i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing | |
771 | 54842 | level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); | |
772 | 54842 | LAST_SKIP_BITS(re, &s->gb, 1); | |
773 | } | ||
774 | } else { | ||
775 | /* first escape */ | ||
776 | 65674 | SKIP_BITS(re, &s->gb, 1); | |
777 |
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); |
778 | 65674 | i+= run; | |
779 | 65674 | level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing | |
780 | 65674 | level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); | |
781 | 65674 | LAST_SKIP_BITS(re, &s->gb, 1); | |
782 | } | ||
783 | } else { | ||
784 | 4824101 | i+= run; | |
785 | 4824101 | level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); | |
786 | 4824101 | LAST_SKIP_BITS(re, &s->gb, 1); | |
787 | } | ||
788 |
2/2✓ Branch 0 taken 796431 times.
✓ Branch 1 taken 4189736 times.
|
4986167 | if (i > 62){ |
789 | 796431 | i-= 192; | |
790 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 796431 times.
|
796431 | if(i&(~63)){ |
791 | ✗ | const int left= get_bits_left(&s->gb); | |
792 | ✗ | if (((i + 192 == 64 && level / qmul == -1) || | |
793 | ✗ | !(s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))) && | |
794 | left >= 0) { | ||
795 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y); | |
796 | ✗ | i = 63; | |
797 | ✗ | break; | |
798 | }else{ | ||
799 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); | |
800 | ✗ | return -1; | |
801 | } | ||
802 | } | ||
803 | |||
804 | 796431 | block[scan_table[i]] = level; | |
805 | 796431 | break; | |
806 | } | ||
807 | |||
808 | 4189736 | block[scan_table[i]] = level; | |
809 | } | ||
810 | 796431 | CLOSE_READER(re, &s->gb); | |
811 | } | ||
812 |
2/2✓ Branch 0 taken 188256 times.
✓ Branch 1 taken 608175 times.
|
796431 | if (s->mb_intra) { |
813 | 188256 | not_coded: | |
814 | 230712 | ff_mpeg4_pred_ac(s, block, n, dc_pred_dir); | |
815 | } | ||
816 | 838887 | s->block_last_index[n] = i; | |
817 | |||
818 | 838887 | return 0; | |
819 | } | ||
820 | |||
821 | 228076 | void ff_msmpeg4_decode_motion(MSMP4DecContext *const ms, int *mx_ptr, int *my_ptr) | |
822 | { | ||
823 | 228076 | const VLCElem *const mv_vlc = mv_tables[ms->mv_table_index]; | |
824 | 228076 | MpegEncContext *const s = &ms->m; | |
825 | int sym, mx, my; | ||
826 | |||
827 | 228076 | sym = get_vlc2(&s->gb, mv_vlc, MV_VLC_BITS, 2); | |
828 |
2/2✓ Branch 0 taken 226220 times.
✓ Branch 1 taken 1856 times.
|
228076 | if (sym) { |
829 | 226220 | mx = sym >> 8; | |
830 | 226220 | my = sym & 0xFF; | |
831 | } else { | ||
832 | /* Escape */ | ||
833 | 1856 | mx = get_bits(&s->gb, 6); | |
834 | 1856 | my = get_bits(&s->gb, 6); | |
835 | } | ||
836 | |||
837 | 228076 | mx += *mx_ptr - 32; | |
838 | 228076 | my += *my_ptr - 32; | |
839 | /* WARNING : they do not do exactly modulo encoding */ | ||
840 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 228054 times.
|
228076 | if (mx <= -64) |
841 | 22 | mx += 64; | |
842 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 228044 times.
|
228054 | else if (mx >= 64) |
843 | 10 | mx -= 64; | |
844 | |||
845 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 228070 times.
|
228076 | if (my <= -64) |
846 | 6 | my += 64; | |
847 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 228066 times.
|
228070 | else if (my >= 64) |
848 | 4 | my -= 64; | |
849 | 228076 | *mx_ptr = mx; | |
850 | 228076 | *my_ptr = my; | |
851 | 228076 | } | |
852 | |||
853 | const FFCodec ff_msmpeg4v1_decoder = { | ||
854 | .p.name = "msmpeg4v1", | ||
855 | CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 1"), | ||
856 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
857 | .p.id = AV_CODEC_ID_MSMPEG4V1, | ||
858 | .priv_data_size = sizeof(MSMP4DecContext), | ||
859 | .init = ff_msmpeg4_decode_init, | ||
860 | FF_CODEC_DECODE_CB(ff_h263_decode_frame), | ||
861 | .close = ff_mpv_decode_close, | ||
862 | .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1, | ||
863 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | | ||
864 | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, | ||
865 | .p.max_lowres = 3, | ||
866 | }; | ||
867 | |||
868 | const FFCodec ff_msmpeg4v2_decoder = { | ||
869 | .p.name = "msmpeg4v2", | ||
870 | CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 2"), | ||
871 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
872 | .p.id = AV_CODEC_ID_MSMPEG4V2, | ||
873 | .priv_data_size = sizeof(MSMP4DecContext), | ||
874 | .init = ff_msmpeg4_decode_init, | ||
875 | FF_CODEC_DECODE_CB(ff_h263_decode_frame), | ||
876 | .close = ff_mpv_decode_close, | ||
877 | .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1, | ||
878 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | | ||
879 | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, | ||
880 | .p.max_lowres = 3, | ||
881 | }; | ||
882 | |||
883 | const FFCodec ff_msmpeg4v3_decoder = { | ||
884 | .p.name = "msmpeg4", | ||
885 | CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 3"), | ||
886 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
887 | .p.id = AV_CODEC_ID_MSMPEG4V3, | ||
888 | .priv_data_size = sizeof(MSMP4DecContext), | ||
889 | .init = ff_msmpeg4_decode_init, | ||
890 | FF_CODEC_DECODE_CB(ff_h263_decode_frame), | ||
891 | .close = ff_mpv_decode_close, | ||
892 | .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1, | ||
893 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | | ||
894 | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, | ||
895 | .p.max_lowres = 3, | ||
896 | }; | ||
897 | |||
898 | const FFCodec ff_wmv1_decoder = { | ||
899 | .p.name = "wmv1", | ||
900 | CODEC_LONG_NAME("Windows Media Video 7"), | ||
901 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
902 | .p.id = AV_CODEC_ID_WMV1, | ||
903 | .priv_data_size = sizeof(MSMP4DecContext), | ||
904 | .init = ff_msmpeg4_decode_init, | ||
905 | FF_CODEC_DECODE_CB(ff_h263_decode_frame), | ||
906 | .close = ff_mpv_decode_close, | ||
907 | .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1, | ||
908 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | | ||
909 | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, | ||
910 | .p.max_lowres = 3, | ||
911 | }; | ||
912 |