/src/ffmpeg/libavcodec/msmpeg4dec.c
Line | Count | 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 | 318k | #define V2_INTRA_CBPC_VLC_BITS 3 |
43 | 945k | #define V2_MB_TYPE_VLC_BITS 7 |
44 | 10.1M | #define MV_VLC_BITS 9 |
45 | | #define TEX_VLC_BITS 9 |
46 | | |
47 | 6.13M | #define DEFAULT_INTER_INDEX 3 |
48 | | |
49 | | static const VLCElem *mv_tables[2]; |
50 | | |
51 | | static inline int msmpeg4v1_pred_dc(H263DecContext *const h, int n, |
52 | | int32_t **dc_val_ptr) |
53 | 1.63M | { |
54 | 1.63M | int i; |
55 | | |
56 | 1.63M | if (n < 4) { |
57 | 1.11M | i= 0; |
58 | 1.11M | } else { |
59 | 527k | i= n-3; |
60 | 527k | } |
61 | | |
62 | 1.63M | *dc_val_ptr= &h->last_dc[i]; |
63 | 1.63M | return h->last_dc[i]; |
64 | 1.63M | } |
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 | | static int msmpeg4v2_decode_motion(H263DecContext *const h, int pred, int f_code) |
78 | 2.68M | { |
79 | 2.68M | int code, val, sign, shift; |
80 | | |
81 | 2.68M | code = get_vlc2(&h->gb, ff_h263_mv_vlc, H263_MV_VLC_BITS, 2); |
82 | 2.68M | ff_dlog(h->c.avctx, "MV code %d at %d %d pred: %d\n", |
83 | 2.68M | code, h->c.mb_x,h->c.mb_y, pred); |
84 | 2.68M | if (code < 0) |
85 | 5.14k | return 0xffff; |
86 | | |
87 | 2.67M | if (code == 0) |
88 | 1.10M | return pred; |
89 | 1.57M | sign = get_bits1(&h->gb); |
90 | 1.57M | shift = f_code - 1; |
91 | 1.57M | val = code; |
92 | 1.57M | if (shift) { |
93 | 0 | val = (val - 1) << shift; |
94 | 0 | val |= get_bits(&h->gb, shift); |
95 | 0 | val++; |
96 | 0 | } |
97 | 1.57M | if (sign) |
98 | 1.08M | val = -val; |
99 | | |
100 | 1.57M | val += pred; |
101 | 1.57M | if (val <= -64) |
102 | 27.8k | val += 64; |
103 | 1.54M | else if (val >= 64) |
104 | 2.60k | val -= 64; |
105 | | |
106 | 1.57M | return val; |
107 | 2.67M | } |
108 | | |
109 | | static int msmpeg4v12_decode_mb(H263DecContext *const h) |
110 | 4.12M | { |
111 | 4.12M | MSMP4DecContext *const ms = mpv_to_msmpeg4(h); |
112 | 4.12M | int cbp, code, i; |
113 | 4.12M | uint32_t * const mb_type_ptr = &h->c.cur_pic.mb_type[h->c.mb_x + h->c.mb_y*h->c.mb_stride]; |
114 | | |
115 | 4.12M | if (h->c.pict_type == AV_PICTURE_TYPE_P) { |
116 | 3.58M | if (ms->use_skip_mb_code) { |
117 | 2.83M | if (get_bits1(&h->gb)) { |
118 | | /* skip mb */ |
119 | 2.04M | h->c.mb_intra = 0; |
120 | 14.3M | for(i=0;i<6;i++) |
121 | 12.2M | h->c.block_last_index[i] = -1; |
122 | 2.04M | h->c.mv_dir = MV_DIR_FORWARD; |
123 | 2.04M | h->c.mv_type = MV_TYPE_16X16; |
124 | 2.04M | h->c.mv[0][0][0] = 0; |
125 | 2.04M | h->c.mv[0][0][1] = 0; |
126 | 2.04M | h->c.mb_skipped = 1; |
127 | 2.04M | *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_FORWARD_MV | MB_TYPE_16x16; |
128 | 2.04M | return 0; |
129 | 2.04M | } |
130 | 2.83M | } |
131 | | |
132 | 1.53M | if (h->c.msmpeg4_version == MSMP4_V2) |
133 | 945k | code = get_vlc2(&h->gb, v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 1); |
134 | 587k | else |
135 | 587k | code = get_vlc2(&h->gb, ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 2); |
136 | 1.53M | if(code<0 || code>7){ |
137 | 8.15k | av_log(h->c.avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", |
138 | 8.15k | code, h->c.mb_x, h->c.mb_y); |
139 | 8.15k | return -1; |
140 | 8.15k | } |
141 | | |
142 | 1.52M | h->c.mb_intra = code >>2; |
143 | | |
144 | 1.52M | cbp = code & 0x3; |
145 | 1.52M | } else { |
146 | 541k | h->c.mb_intra = 1; |
147 | 541k | if (h->c.msmpeg4_version == MSMP4_V2) |
148 | 318k | cbp = get_vlc2(&h->gb, v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 1); |
149 | 223k | else |
150 | 223k | cbp = get_vlc2(&h->gb, ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 2); |
151 | 541k | if(cbp<0 || cbp>3){ |
152 | 13.6k | av_log(h->c.avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", |
153 | 13.6k | cbp, h->c.mb_x, h->c.mb_y); |
154 | 13.6k | return -1; |
155 | 13.6k | } |
156 | 541k | } |
157 | | |
158 | 2.05M | if (!h->c.mb_intra) { |
159 | 1.41M | int mx, my, cbpy; |
160 | | |
161 | 1.41M | cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); |
162 | 1.41M | if(cbpy<0){ |
163 | 74.2k | av_log(h->c.avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", |
164 | 74.2k | cbp, h->c.mb_x, h->c.mb_y); |
165 | 74.2k | return -1; |
166 | 74.2k | } |
167 | | |
168 | 1.34M | cbp|= cbpy<<2; |
169 | 1.34M | if (h->c.msmpeg4_version == MSMP4_V1 || (cbp&3) != 3) |
170 | 1.33M | cbp ^= 0x3C; |
171 | | |
172 | 1.34M | ff_h263_pred_motion(&h->c, 0, 0, &mx, &my); |
173 | 1.34M | mx = msmpeg4v2_decode_motion(h, mx, 1); |
174 | 1.34M | my = msmpeg4v2_decode_motion(h, my, 1); |
175 | | |
176 | 1.34M | h->c.mv_dir = MV_DIR_FORWARD; |
177 | 1.34M | h->c.mv_type = MV_TYPE_16X16; |
178 | 1.34M | h->c.mv[0][0][0] = mx; |
179 | 1.34M | h->c.mv[0][0][1] = my; |
180 | 1.34M | *mb_type_ptr = MB_TYPE_FORWARD_MV | MB_TYPE_16x16; |
181 | 1.34M | } else { |
182 | 636k | int v; |
183 | 636k | if (h->c.msmpeg4_version == MSMP4_V2) { |
184 | 337k | h->c.ac_pred = get_bits1(&h->gb); |
185 | 337k | v = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); |
186 | 337k | if (v < 0) { |
187 | 1.64k | av_log(h->c.avctx, AV_LOG_ERROR, "cbpy vlc invalid\n"); |
188 | 1.64k | return -1; |
189 | 1.64k | } |
190 | 336k | cbp|= v<<2; |
191 | 336k | } else{ |
192 | 298k | h->c.ac_pred = 0; |
193 | 298k | v = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); |
194 | 298k | if (v < 0) { |
195 | 711 | av_log(h->c.avctx, AV_LOG_ERROR, "cbpy vlc invalid\n"); |
196 | 711 | return -1; |
197 | 711 | } |
198 | 297k | cbp|= v<<2; |
199 | 297k | if (h->c.pict_type==AV_PICTURE_TYPE_P) cbp^=0x3C; |
200 | 297k | } |
201 | 634k | *mb_type_ptr = MB_TYPE_INTRA; |
202 | 634k | } |
203 | | |
204 | 1.97M | h->c.bdsp.clear_blocks(h->block[0]); |
205 | 13.7M | for (i = 0; i < 6; i++) { |
206 | 11.8M | if (ff_msmpeg4_decode_block(ms, h->block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) |
207 | 19.6k | { |
208 | 19.6k | av_log(h->c.avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", |
209 | 19.6k | h->c.mb_x, h->c.mb_y, i); |
210 | 19.6k | return -1; |
211 | 19.6k | } |
212 | 11.8M | } |
213 | 1.95M | return 0; |
214 | 1.97M | } |
215 | | |
216 | | static int msmpeg4v34_decode_mb(H263DecContext *const h) |
217 | 11.5M | { |
218 | 11.5M | MSMP4DecContext *const ms = mpv_to_msmpeg4(h); |
219 | 11.5M | int cbp, code, i; |
220 | 11.5M | uint8_t *coded_val; |
221 | 11.5M | uint32_t * const mb_type_ptr = &h->c.cur_pic.mb_type[h->c.mb_x + h->c.mb_y*h->c.mb_stride]; |
222 | | |
223 | 11.5M | if (get_bits_left(&h->gb) <= 0) |
224 | 71.7k | return AVERROR_INVALIDDATA; |
225 | | |
226 | 11.5M | if (h->c.pict_type == AV_PICTURE_TYPE_P) { |
227 | 8.59M | if (ms->use_skip_mb_code) { |
228 | 3.75M | if (get_bits1(&h->gb)) { |
229 | | /* skip mb */ |
230 | 2.45M | h->c.mb_intra = 0; |
231 | 17.2M | for(i=0;i<6;i++) |
232 | 14.7M | h->c.block_last_index[i] = -1; |
233 | 2.45M | h->c.mv_dir = MV_DIR_FORWARD; |
234 | 2.45M | h->c.mv_type = MV_TYPE_16X16; |
235 | 2.45M | h->c.mv[0][0][0] = 0; |
236 | 2.45M | h->c.mv[0][0][1] = 0; |
237 | 2.45M | h->c.mb_skipped = 1; |
238 | 2.45M | *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_FORWARD_MV | MB_TYPE_16x16; |
239 | | |
240 | 2.45M | return 0; |
241 | 2.45M | } |
242 | 3.75M | } |
243 | | |
244 | 6.13M | code = get_vlc2(&h->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX], MB_NON_INTRA_VLC_BITS, 3); |
245 | | //h->c.mb_intra = (code & 0x40) ? 0 : 1; |
246 | 6.13M | h->c.mb_intra = (~code & 0x40) >> 6; |
247 | | |
248 | 6.13M | cbp = code & 0x3f; |
249 | 6.13M | } else { |
250 | 2.92M | h->c.mb_intra = 1; |
251 | 2.92M | code = get_vlc2(&h->gb, ff_msmp4_mb_i_vlc, MSMP4_MB_INTRA_VLC_BITS, 2); |
252 | | /* predict coded block pattern */ |
253 | 2.92M | cbp = 0; |
254 | 20.4M | for(i=0;i<6;i++) { |
255 | 17.5M | int val = ((code >> (5 - i)) & 1); |
256 | 17.5M | if (i < 4) { |
257 | 11.7M | int pred = ff_msmpeg4_coded_block_pred(&h->c, i, &coded_val); |
258 | 11.7M | val = val ^ pred; |
259 | 11.7M | *coded_val = val; |
260 | 11.7M | } |
261 | 17.5M | cbp |= val << (5 - i); |
262 | 17.5M | } |
263 | 2.92M | } |
264 | | |
265 | 9.06M | if (!h->c.mb_intra) { |
266 | 6.05M | int mx, my; |
267 | 6.05M | if (ms->per_mb_rl_table && cbp) { |
268 | 139k | ms->rl_table_index = decode012(&h->gb); |
269 | 139k | ms->rl_chroma_table_index = ms->rl_table_index; |
270 | 139k | } |
271 | 6.05M | ff_h263_pred_motion(&h->c, 0, 0, &mx, &my); |
272 | 6.05M | ff_msmpeg4_decode_motion(ms, &mx, &my); |
273 | 6.05M | h->c.mv_dir = MV_DIR_FORWARD; |
274 | 6.05M | h->c.mv_type = MV_TYPE_16X16; |
275 | 6.05M | h->c.mv[0][0][0] = mx; |
276 | 6.05M | h->c.mv[0][0][1] = my; |
277 | 6.05M | *mb_type_ptr = MB_TYPE_FORWARD_MV | MB_TYPE_16x16; |
278 | 6.05M | } else { |
279 | 3.00M | ff_dlog(h->c.avctx, "I at %d %d %d %06X\n", h->c.mb_x, h->c.mb_y, |
280 | 3.00M | ((cbp & 3) ? 1 : 0) +((cbp & 0x3C)? 2 : 0), |
281 | 3.00M | show_bits(&h->gb, 24)); |
282 | 3.00M | h->c.ac_pred = get_bits1(&h->gb); |
283 | 3.00M | *mb_type_ptr = MB_TYPE_INTRA; |
284 | 3.00M | if (h->c.inter_intra_pred) { |
285 | 7.19k | h->c.h263_aic_dir = get_vlc2(&h->gb, ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 1); |
286 | 7.19k | ff_dlog(h->c.avctx, "%d%d %d %d/", |
287 | 7.19k | h->c.ac_pred, h->c.h263_aic_dir, h->c.mb_x, h->c.mb_y); |
288 | 7.19k | } |
289 | 3.00M | if (ms->per_mb_rl_table && cbp) { |
290 | 543k | ms->rl_table_index = decode012(&h->gb); |
291 | 543k | ms->rl_chroma_table_index = ms->rl_table_index; |
292 | 543k | } |
293 | 3.00M | } |
294 | | |
295 | 9.06M | h->c.bdsp.clear_blocks(h->block[0]); |
296 | 63.4M | for (i = 0; i < 6; i++) { |
297 | 54.3M | if (ff_msmpeg4_decode_block(ms, h->block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) |
298 | 8.88k | { |
299 | 8.88k | av_log(h->c.avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", |
300 | 8.88k | h->c.mb_x, h->c.mb_y, i); |
301 | 8.88k | return -1; |
302 | 8.88k | } |
303 | 54.3M | } |
304 | | |
305 | 9.05M | return 0; |
306 | 9.06M | } |
307 | | |
308 | | /* init all vlc decoding tables */ |
309 | | static av_cold void msmpeg4_decode_init_static(void) |
310 | 5 | { |
311 | 5 | static VLCElem vlc_buf[3714 + 2694 + 1636 + 2648 + 1532 + 2488]; |
312 | 5 | VLCInitState state = VLC_INIT_STATE(vlc_buf); |
313 | | |
314 | 5 | INIT_FIRST_VLC_RL(ff_rl_table[0], 642); |
315 | 5 | INIT_FIRST_VLC_RL(ff_rl_table[1], 1104); |
316 | 5 | INIT_FIRST_VLC_RL(ff_rl_table[2], 554); |
317 | 5 | VLC_INIT_RL(ff_rl_table[3], 940); |
318 | 5 | VLC_INIT_RL(ff_rl_table[4], 962); |
319 | | /* ff_rl_table[5] coincides with ff_h263_rl_inter which has just been |
320 | | * initialized in ff_h263_decode_init() earlier. So just copy the VLCs. */ |
321 | 5 | av_assert1(ff_h263_rl_inter.rl_vlc[0]); |
322 | 5 | memcpy(ff_rl_table[5].rl_vlc, ff_h263_rl_inter.rl_vlc, sizeof(ff_rl_table[5].rl_vlc)); |
323 | | |
324 | 5 | VLC_INIT_STATIC_TABLE(v2_dc_lum_vlc, MSMP4_DC_VLC_BITS, 512, |
325 | 5 | &ff_v2_dc_lum_table[0][1], 8, 4, |
326 | 5 | &ff_v2_dc_lum_table[0][0], 8, 4, 0); |
327 | 5 | VLC_INIT_STATIC_TABLE(v2_dc_chroma_vlc, MSMP4_DC_VLC_BITS, 512, |
328 | 5 | &ff_v2_dc_chroma_table[0][1], 8, 4, |
329 | 5 | &ff_v2_dc_chroma_table[0][0], 8, 4, 0); |
330 | | |
331 | 5 | VLC_INIT_STATIC_TABLE(v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4, |
332 | 5 | &ff_v2_intra_cbpc[0][1], 2, 1, |
333 | 5 | &ff_v2_intra_cbpc[0][0], 2, 1, 0); |
334 | 5 | VLC_INIT_STATIC_TABLE(v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8, |
335 | 5 | &ff_v2_mb_type[0][1], 2, 1, |
336 | 5 | &ff_v2_mb_type[0][0], 2, 1, 0); |
337 | | |
338 | 5 | mv_tables[0] = ff_vlc_init_tables_from_lengths(&state, MV_VLC_BITS, |
339 | 5 | MSMPEG4_MV_TABLES_NB_ELEMS, |
340 | 5 | ff_msmp4_mv_table0_lens, 1, |
341 | 5 | ff_msmp4_mv_table0, 2, 2, |
342 | 5 | 0, 0); |
343 | 5 | mv_tables[1] = ff_vlc_init_tables_from_lengths(&state, MV_VLC_BITS, |
344 | 5 | MSMPEG4_MV_TABLES_NB_ELEMS, |
345 | 5 | ff_msmp4_mv_table1_lens, 1, |
346 | 5 | ff_msmp4_mv_table1, 2, 2, |
347 | 5 | 0, 0); |
348 | | |
349 | 25 | for (unsigned i = 0; i < 4; i++) { |
350 | 20 | ff_mb_non_intra_vlc[i] = |
351 | 20 | ff_vlc_init_tables_sparse(&state, MB_NON_INTRA_VLC_BITS, 128, |
352 | 20 | &ff_wmv2_inter_table[i][0][1], 8, 4, |
353 | 20 | &ff_wmv2_inter_table[i][0][0], 8, 4, |
354 | 20 | NULL, 0, 0, 0); |
355 | 20 | } |
356 | | |
357 | 5 | VLC_INIT_STATIC_TABLE(ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4, |
358 | 5 | &ff_table_inter_intra[0][1], 2, 1, |
359 | 5 | &ff_table_inter_intra[0][0], 2, 1, 0); |
360 | 5 | ff_msmp4_vc1_vlcs_init_once(); |
361 | 5 | } |
362 | | |
363 | | static int msmpeg4_decode_picture_header(H263DecContext *const h) |
364 | 1.07M | { |
365 | 1.07M | MSMP4DecContext *const ms = mpv_to_msmpeg4(h); |
366 | 1.07M | int code; |
367 | | |
368 | | // at minimum one bit per macroblock is required at least in a valid frame, |
369 | | // we discard frames much smaller than this. Frames smaller than 1/8 of the |
370 | | // smallest "black/skip" frame generally contain not much recoverable content |
371 | | // while at the same time they have the highest computational requirements |
372 | | // per byte |
373 | 1.07M | if (get_bits_left(&h->gb) * 8LL < (h->c.width+15)/16 * ((h->c.height+15)/16)) |
374 | 644k | return AVERROR_INVALIDDATA; |
375 | | |
376 | 431k | if (h->c.msmpeg4_version == MSMP4_V1) { |
377 | 92.4k | int start_code = get_bits_long(&h->gb, 32); |
378 | 92.4k | if(start_code!=0x00000100){ |
379 | 17.9k | av_log(h->c.avctx, AV_LOG_ERROR, "invalid startcode\n"); |
380 | 17.9k | return -1; |
381 | 17.9k | } |
382 | | |
383 | 74.5k | skip_bits(&h->gb, 5); // frame number */ |
384 | 74.5k | } |
385 | | |
386 | 413k | h->c.pict_type = get_bits(&h->gb, 2) + 1; |
387 | 413k | if (h->c.pict_type != AV_PICTURE_TYPE_I && |
388 | 258k | h->c.pict_type != AV_PICTURE_TYPE_P){ |
389 | 40.0k | av_log(h->c.avctx, AV_LOG_ERROR, "invalid picture type\n"); |
390 | 40.0k | return -1; |
391 | 40.0k | } |
392 | 373k | h->c.chroma_qscale = h->c.qscale = get_bits(&h->gb, 5); |
393 | 373k | if (h->c.qscale == 0) { |
394 | 43.5k | av_log(h->c.avctx, AV_LOG_ERROR, "invalid qscale\n"); |
395 | 43.5k | return -1; |
396 | 43.5k | } |
397 | | |
398 | 329k | if (h->c.pict_type == AV_PICTURE_TYPE_I) { |
399 | 116k | code = get_bits(&h->gb, 5); |
400 | 116k | if (h->c.msmpeg4_version == MSMP4_V1) { |
401 | 23.3k | if(code==0 || code>h->c.mb_height) { |
402 | 1.60k | av_log(h->c.avctx, AV_LOG_ERROR, "invalid slice height %d\n", code); |
403 | 1.60k | return -1; |
404 | 1.60k | } |
405 | | |
406 | 21.6k | h->slice_height = code; |
407 | 92.8k | }else{ |
408 | | /* 0x17: one slice, 0x18: two slices, ... */ |
409 | 92.8k | if (code < 0x17){ |
410 | 62.2k | av_log(h->c.avctx, AV_LOG_ERROR, "error, slice code was %X\n", code); |
411 | 62.2k | return -1; |
412 | 62.2k | } |
413 | | |
414 | 30.6k | h->slice_height = h->c.mb_height / (code - 0x16); |
415 | 30.6k | } |
416 | | |
417 | 52.3k | switch (h->c.msmpeg4_version) { |
418 | 21.6k | case MSMP4_V1: |
419 | 25.8k | case MSMP4_V2: |
420 | 25.8k | ms->rl_chroma_table_index = 2; |
421 | 25.8k | ms->rl_table_index = 2; |
422 | | |
423 | 25.8k | ms->dc_table_index = 0; //not used |
424 | 25.8k | break; |
425 | 5.54k | case MSMP4_V3: |
426 | 5.54k | ms->rl_chroma_table_index = decode012(&h->gb); |
427 | 5.54k | ms->rl_table_index = decode012(&h->gb); |
428 | | |
429 | 5.54k | ms->dc_table_index = get_bits1(&h->gb); |
430 | 5.54k | break; |
431 | 20.9k | case MSMP4_WMV1: |
432 | 20.9k | ff_msmpeg4_decode_ext_header(h, (2+5+5+17+7)/8); |
433 | | |
434 | 20.9k | if (ms->bit_rate > MBAC_BITRATE) |
435 | 8.74k | ms->per_mb_rl_table = get_bits1(&h->gb); |
436 | 12.1k | else |
437 | 12.1k | ms->per_mb_rl_table = 0; |
438 | | |
439 | 20.9k | if (!ms->per_mb_rl_table) { |
440 | 15.3k | ms->rl_chroma_table_index = decode012(&h->gb); |
441 | 15.3k | ms->rl_table_index = decode012(&h->gb); |
442 | 15.3k | } |
443 | | |
444 | 20.9k | ms->dc_table_index = get_bits1(&h->gb); |
445 | 20.9k | h->c.inter_intra_pred= 0; |
446 | 20.9k | break; |
447 | 0 | default: |
448 | 0 | av_unreachable("msmpeg4_decode_picture_header() only used by MSMP4V1-3, WMV1"); |
449 | 52.3k | } |
450 | 52.3k | h->c.no_rounding = 1; |
451 | 52.3k | if (h->c.avctx->debug & FF_DEBUG_PICT_INFO) |
452 | 0 | av_log(h->c.avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n", |
453 | 0 | h->c.qscale, |
454 | 0 | ms->rl_chroma_table_index, |
455 | 0 | ms->rl_table_index, |
456 | 0 | ms->dc_table_index, |
457 | 0 | ms->per_mb_rl_table, |
458 | 0 | h->slice_height); |
459 | 213k | } else { |
460 | 213k | switch (h->c.msmpeg4_version) { |
461 | 49.8k | case MSMP4_V1: |
462 | 121k | case MSMP4_V2: |
463 | 121k | if (h->c.msmpeg4_version == MSMP4_V1) |
464 | 49.8k | ms->use_skip_mb_code = 1; |
465 | 71.9k | else |
466 | 71.9k | ms->use_skip_mb_code = get_bits1(&h->gb); |
467 | 121k | ms->rl_table_index = 2; |
468 | 121k | ms->rl_chroma_table_index = ms->rl_table_index; |
469 | 121k | ms->dc_table_index = 0; //not used |
470 | 121k | ms->mv_table_index = 0; |
471 | 121k | break; |
472 | 52.0k | case MSMP4_V3: |
473 | 52.0k | ms->use_skip_mb_code = get_bits1(&h->gb); |
474 | 52.0k | ms->rl_table_index = decode012(&h->gb); |
475 | 52.0k | ms->rl_chroma_table_index = ms->rl_table_index; |
476 | | |
477 | 52.0k | ms->dc_table_index = get_bits1(&h->gb); |
478 | | |
479 | 52.0k | ms->mv_table_index = get_bits1(&h->gb); |
480 | 52.0k | break; |
481 | 39.8k | case MSMP4_WMV1: |
482 | 39.8k | ms->use_skip_mb_code = get_bits1(&h->gb); |
483 | | |
484 | 39.8k | if (ms->bit_rate > MBAC_BITRATE) |
485 | 6.45k | ms->per_mb_rl_table = get_bits1(&h->gb); |
486 | 33.3k | else |
487 | 33.3k | ms->per_mb_rl_table = 0; |
488 | | |
489 | 39.8k | if (!ms->per_mb_rl_table) { |
490 | 39.2k | ms->rl_table_index = decode012(&h->gb); |
491 | 39.2k | ms->rl_chroma_table_index = ms->rl_table_index; |
492 | 39.2k | } |
493 | | |
494 | 39.8k | ms->dc_table_index = get_bits1(&h->gb); |
495 | | |
496 | 39.8k | ms->mv_table_index = get_bits1(&h->gb); |
497 | 39.8k | h->c.inter_intra_pred = h->c.width*h->c.height < 320*240 && |
498 | 36.6k | ms->bit_rate <= II_BITRATE; |
499 | 39.8k | break; |
500 | 0 | default: |
501 | 0 | av_unreachable("msmpeg4_decode_picture_header() only used by MSMP4V1-3, WMV1"); |
502 | 213k | } |
503 | | |
504 | 213k | if (h->c.avctx->debug&FF_DEBUG_PICT_INFO) |
505 | 0 | av_log(h->c.avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n", |
506 | 0 | ms->use_skip_mb_code, |
507 | 0 | ms->rl_table_index, |
508 | 0 | ms->rl_chroma_table_index, |
509 | 0 | ms->dc_table_index, |
510 | 0 | ms->mv_table_index, |
511 | 0 | ms->per_mb_rl_table, |
512 | 0 | h->c.qscale); |
513 | | |
514 | 213k | if (ms->flipflop_rounding) { |
515 | 2.89k | h->c.no_rounding ^= 1; |
516 | 210k | }else{ |
517 | 210k | h->c.no_rounding = 0; |
518 | 210k | } |
519 | 213k | } |
520 | 265k | ff_dlog(h->c.avctx, "%d %d %d %d %d\n", h->c.pict_type, ms->bit_rate, |
521 | 265k | h->c.inter_intra_pred, h->c.width, h->c.height); |
522 | | |
523 | 265k | ms->esc3_level_length = 0; |
524 | 265k | ms->esc3_run_length = 0; |
525 | | |
526 | 265k | return 0; |
527 | 329k | } |
528 | | |
529 | | int ff_msmpeg4_decode_ext_header(H263DecContext *const h, int buf_size) |
530 | 50.9k | { |
531 | 50.9k | MSMP4DecContext *const ms = mpv_to_msmpeg4(h); |
532 | 50.9k | int left = buf_size*8 - get_bits_count(&h->gb); |
533 | 50.9k | int length = h->c.msmpeg4_version >= MSMP4_V3 ? 17 : 16; |
534 | | /* the alt_bitstream reader could read over the end so we need to check it */ |
535 | 50.9k | if(left>=length && left<length+8) |
536 | 22.0k | { |
537 | 22.0k | skip_bits(&h->gb, 5); /* fps */ |
538 | 22.0k | ms->bit_rate = get_bits(&h->gb, 11) * 1024; |
539 | 22.0k | if (h->c.msmpeg4_version >= MSMP4_V3) |
540 | 21.1k | ms->flipflop_rounding = get_bits1(&h->gb); |
541 | 922 | else |
542 | 922 | ms->flipflop_rounding = 0; |
543 | 22.0k | } |
544 | 28.8k | else if(left<length+8) |
545 | 21.0k | { |
546 | 21.0k | ms->flipflop_rounding = 0; |
547 | 21.0k | if (h->c.msmpeg4_version != MSMP4_V2) |
548 | 19.8k | av_log(h->c.avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left); |
549 | 21.0k | } |
550 | 7.84k | else |
551 | 7.84k | { |
552 | 7.84k | av_log(h->c.avctx, AV_LOG_ERROR, "I-frame too long, ignoring ext header\n"); |
553 | 7.84k | } |
554 | | |
555 | 50.9k | return 0; |
556 | 50.9k | } |
557 | | |
558 | | static int msmpeg4_decode_dc(MSMP4DecContext *const ms, int n, int *dir_ptr) |
559 | 29.3M | { |
560 | 29.3M | H263DecContext *const h = &ms->h; |
561 | 29.3M | int level, pred; |
562 | | |
563 | 29.3M | if (h->c.msmpeg4_version <= MSMP4_V2) { |
564 | 3.78M | if (n < 4) { |
565 | 2.52M | level = get_vlc2(&h->gb, v2_dc_lum_vlc, MSMP4_DC_VLC_BITS, 3); |
566 | 2.52M | } else { |
567 | 1.25M | level = get_vlc2(&h->gb, v2_dc_chroma_vlc, MSMP4_DC_VLC_BITS, 3); |
568 | 1.25M | } |
569 | 3.78M | if (level < 0) { |
570 | 227k | av_log(h->c.avctx, AV_LOG_ERROR, "illegal dc vlc\n"); |
571 | 227k | *dir_ptr = 0; |
572 | 227k | return -1; |
573 | 227k | } |
574 | 3.55M | level-=256; |
575 | 25.5M | } else { |
576 | 25.5M | level = get_vlc2(&h->gb, ff_msmp4_dc_vlc[ms->dc_table_index][n >= 4], |
577 | 25.5M | MSMP4_DC_VLC_BITS, 3); |
578 | | |
579 | 25.5M | if (level == DC_MAX) { |
580 | 2.24k | level = get_bits(&h->gb, 8); |
581 | 2.24k | if (get_bits1(&h->gb)) |
582 | 1.91k | level = -level; |
583 | 25.5M | } else if (level != 0) { |
584 | 16.9M | if (get_bits1(&h->gb)) |
585 | 5.50M | level = -level; |
586 | 16.9M | } |
587 | 25.5M | } |
588 | | |
589 | 29.0M | if (h->c.msmpeg4_version == MSMP4_V1) { |
590 | 1.63M | int32_t *dc_val; |
591 | 1.63M | pred = msmpeg4v1_pred_dc(h, n, &dc_val); |
592 | 1.63M | level += pred; |
593 | | |
594 | | /* update predictor */ |
595 | 1.63M | *dc_val= level; |
596 | 27.4M | }else{ |
597 | 27.4M | int16_t *dc_val; |
598 | 27.4M | pred = ff_msmpeg4_pred_dc(&h->c, n, &dc_val, dir_ptr); |
599 | 27.4M | level += pred; |
600 | | |
601 | | /* update predictor */ |
602 | 27.4M | if (n < 4) { |
603 | 18.3M | *dc_val = level * h->c.y_dc_scale; |
604 | 18.3M | } else { |
605 | 9.12M | *dc_val = level * h->c.c_dc_scale; |
606 | 9.12M | } |
607 | 27.4M | } |
608 | | |
609 | 29.0M | return level; |
610 | 29.3M | } |
611 | | |
612 | | int ff_msmpeg4_decode_block(MSMP4DecContext *const ms, int16_t * block, |
613 | | int n, int coded, const uint8_t *scan_table) |
614 | 81.2M | { |
615 | 81.2M | H263DecContext *const h = &ms->h; |
616 | 81.2M | int level, i, last, run, run_diff; |
617 | 81.2M | int dc_pred_dir = -1; //unused but its passed around, so it needs to be initialized |
618 | 81.2M | const RLTable *rl; |
619 | 81.2M | const RL_VLC_ELEM *rl_vlc; |
620 | 81.2M | int qmul, qadd; |
621 | | |
622 | 81.2M | if (h->c.mb_intra) { |
623 | 29.3M | qmul=1; |
624 | 29.3M | qadd=0; |
625 | | |
626 | | /* DC coef */ |
627 | 29.3M | level = msmpeg4_decode_dc(ms, n, &dc_pred_dir); |
628 | | |
629 | 29.3M | if (level < 0){ |
630 | 10.7M | av_log(h->c.avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, h->c.qscale); |
631 | 10.7M | if (h->c.inter_intra_pred) |
632 | 657 | level = 0; |
633 | 10.7M | } |
634 | 29.3M | if (n < 4) { |
635 | 19.5M | rl = &ff_rl_table[ms->rl_table_index]; |
636 | 19.5M | if (level > 256 * h->c.y_dc_scale) { |
637 | 1.17k | av_log(h->c.avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", h->c.qscale); |
638 | 1.17k | if (!h->c.inter_intra_pred) |
639 | 1.17k | return -1; |
640 | 1.17k | } |
641 | 19.5M | } else { |
642 | 9.75M | rl = &ff_rl_table[3 + ms->rl_chroma_table_index]; |
643 | 9.75M | if (level > 256 * h->c.c_dc_scale) { |
644 | 1.14k | av_log(h->c.avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", h->c.qscale); |
645 | 1.14k | if (!h->c.inter_intra_pred) |
646 | 1.14k | return -1; |
647 | 1.14k | } |
648 | 9.75M | } |
649 | 29.3M | block[0] = level; |
650 | | |
651 | 29.3M | run_diff = h->c.msmpeg4_version >= MSMP4_WMV1; |
652 | 29.3M | i = 0; |
653 | 29.3M | if (!coded) { |
654 | 15.7M | goto not_coded; |
655 | 15.7M | } |
656 | 13.5M | if (h->c.ac_pred) { |
657 | 4.43M | if (dc_pred_dir == 0) |
658 | 2.64M | scan_table = h->permutated_intra_v_scantable; /* left */ |
659 | 1.79M | else |
660 | 1.79M | scan_table = h->permutated_intra_h_scantable; /* top */ |
661 | 9.10M | } else { |
662 | 9.10M | scan_table = h->c.intra_scantable.permutated; |
663 | 9.10M | } |
664 | 13.5M | rl_vlc= rl->rl_vlc[0]; |
665 | 51.9M | } else { |
666 | 51.9M | qmul = h->c.qscale << 1; |
667 | 51.9M | qadd = (h->c.qscale - 1) | 1; |
668 | 51.9M | i = -1; |
669 | 51.9M | rl = &ff_rl_table[3 + ms->rl_table_index]; |
670 | | |
671 | 51.9M | if (h->c.msmpeg4_version == MSMP4_V2) |
672 | 5.28M | run_diff = 0; |
673 | 46.7M | else |
674 | 46.7M | run_diff = 1; |
675 | | |
676 | 51.9M | if (!coded) { |
677 | 35.3M | h->c.block_last_index[n] = i; |
678 | 35.3M | return 0; |
679 | 35.3M | } |
680 | 16.6M | if(!scan_table) |
681 | 9.05M | scan_table = h->c.inter_scantable.permutated; |
682 | 16.6M | rl_vlc= rl->rl_vlc[h->c.qscale]; |
683 | 16.6M | } |
684 | 30.2M | { |
685 | 30.2M | OPEN_READER(re, &h->gb); |
686 | 116M | for(;;) { |
687 | 116M | UPDATE_CACHE(re, &h->gb); |
688 | 116M | GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 0); |
689 | 116M | if (level==0) { |
690 | 1.47M | int cache; |
691 | 1.47M | cache= GET_CACHE(re, &h->gb); |
692 | | /* escape */ |
693 | 1.47M | if (h->c.msmpeg4_version == MSMP4_V1 || (cache&0x80000000)==0) { |
694 | 1.21M | if (h->c.msmpeg4_version == MSMP4_V1 || (cache&0x40000000)==0) { |
695 | | /* third escape */ |
696 | 1.13M | if (h->c.msmpeg4_version != MSMP4_V1) |
697 | 1.13M | LAST_SKIP_BITS(re, &h->gb, 2); |
698 | 1.13M | UPDATE_CACHE(re, &h->gb); |
699 | 1.13M | if (h->c.msmpeg4_version <= MSMP4_V3) { |
700 | 160k | last = SHOW_UBITS(re, &h->gb, 1); SKIP_CACHE(re, &h->gb, 1); |
701 | 160k | run = SHOW_UBITS(re, &h->gb, 6); SKIP_CACHE(re, &h->gb, 6); |
702 | 160k | level = SHOW_SBITS(re, &h->gb, 8); |
703 | 160k | SKIP_COUNTER(re, &h->gb, 1 + 6 + 8); |
704 | 978k | }else{ |
705 | 978k | int sign; |
706 | 978k | last = SHOW_UBITS(re, &h->gb, 1); SKIP_BITS(re, &h->gb, 1); |
707 | 978k | if (!ms->esc3_level_length) { |
708 | 5.90k | int ll; |
709 | 5.90k | ff_dlog(h->c.avctx, "ESC-3 %X at %d %d\n", |
710 | 5.90k | show_bits(&h->gb, 24), h->c.mb_x, h->c.mb_y); |
711 | 5.90k | if (h->c.qscale < 8) { |
712 | 2.31k | ll = SHOW_UBITS(re, &h->gb, 3); SKIP_BITS(re, &h->gb, 3); |
713 | 2.31k | if(ll==0){ |
714 | 1.31k | ll = 8+SHOW_UBITS(re, &h->gb, 1); SKIP_BITS(re, &h->gb, 1); |
715 | 1.31k | } |
716 | 3.59k | }else{ |
717 | 3.59k | ll=2; |
718 | 18.5k | while (ll < 8 && SHOW_UBITS(re, &h->gb, 1) == 0) { |
719 | 14.9k | ll++; |
720 | 14.9k | SKIP_BITS(re, &h->gb, 1); |
721 | 14.9k | } |
722 | 3.59k | if (ll<8) SKIP_BITS(re, &h->gb, 1); |
723 | 3.59k | } |
724 | | |
725 | 5.90k | ms->esc3_level_length = ll; |
726 | 5.90k | ms->esc3_run_length = SHOW_UBITS(re, &h->gb, 2) + 3; SKIP_BITS(re, &h->gb, 2); |
727 | 5.90k | UPDATE_CACHE(re, &h->gb); |
728 | 5.90k | } |
729 | 978k | run = SHOW_UBITS(re, &h->gb, ms->esc3_run_length); |
730 | 978k | SKIP_BITS(re, &h->gb, ms->esc3_run_length); |
731 | | |
732 | 978k | sign= SHOW_UBITS(re, &h->gb, 1); |
733 | 978k | SKIP_BITS(re, &h->gb, 1); |
734 | | |
735 | 978k | level = SHOW_UBITS(re, &h->gb, ms->esc3_level_length); |
736 | 978k | SKIP_BITS(re, &h->gb, ms->esc3_level_length); |
737 | 978k | if(sign) level= -level; |
738 | 978k | } |
739 | | |
740 | | //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ; |
741 | 1.13M | if (level>0) level= level * qmul + qadd; |
742 | 849k | else level= level * qmul - qadd; |
743 | 1.13M | i+= run + 1; |
744 | 1.13M | if(last) i+=192; |
745 | 1.13M | } else { |
746 | | /* second escape */ |
747 | 75.4k | SKIP_BITS(re, &h->gb, 2); |
748 | 75.4k | GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1); |
749 | 75.4k | i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing |
750 | 75.4k | level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1); |
751 | 75.4k | LAST_SKIP_BITS(re, &h->gb, 1); |
752 | 75.4k | } |
753 | 1.21M | } else { |
754 | | /* first escape */ |
755 | 258k | SKIP_BITS(re, &h->gb, 1); |
756 | 258k | GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1); |
757 | 258k | i+= run; |
758 | 258k | level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing |
759 | 258k | level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1); |
760 | 258k | LAST_SKIP_BITS(re, &h->gb, 1); |
761 | 258k | } |
762 | 115M | } else { |
763 | 115M | i+= run; |
764 | 115M | level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1); |
765 | 115M | LAST_SKIP_BITS(re, &h->gb, 1); |
766 | 115M | } |
767 | 116M | if (i > 62){ |
768 | 30.2M | i-= 192; |
769 | 30.2M | if(i&(~63)){ |
770 | 5.60M | const int left = get_bits_left(&h->gb); |
771 | 5.60M | if (((i + 192 == 64 && level / qmul == -1) || |
772 | 5.55M | !(h->c.avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))) && |
773 | 5.59M | left >= 0) { |
774 | 5.57M | av_log(h->c.avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", |
775 | 5.57M | h->c.mb_x, h->c.mb_y); |
776 | 5.57M | i = 63; |
777 | 5.57M | break; |
778 | 5.57M | }else{ |
779 | 37.1k | av_log(h->c.avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", |
780 | 37.1k | h->c.mb_x, h->c.mb_y); |
781 | 37.1k | return -1; |
782 | 37.1k | } |
783 | 5.60M | } |
784 | | |
785 | 24.6M | block[scan_table[i]] = level; |
786 | 24.6M | break; |
787 | 30.2M | } |
788 | | |
789 | 86.3M | block[scan_table[i]] = level; |
790 | 86.3M | } |
791 | 30.1M | CLOSE_READER(re, &h->gb); |
792 | 30.1M | } |
793 | 30.1M | if (h->c.mb_intra) { |
794 | 29.2M | not_coded: |
795 | 29.2M | ff_mpeg4_pred_ac(h, block, n, dc_pred_dir); |
796 | 29.2M | } |
797 | 45.9M | h->c.block_last_index[n] = i; |
798 | | |
799 | 45.9M | return 0; |
800 | 30.1M | } |
801 | | |
802 | | void ff_msmpeg4_decode_motion(MSMP4DecContext *const ms, int *mx_ptr, int *my_ptr) |
803 | 10.1M | { |
804 | 10.1M | const VLCElem *const mv_vlc = mv_tables[ms->mv_table_index]; |
805 | 10.1M | H263DecContext *const h = &ms->h; |
806 | 10.1M | int sym, mx, my; |
807 | | |
808 | 10.1M | sym = get_vlc2(&h->gb, mv_vlc, MV_VLC_BITS, 2); |
809 | 10.1M | if (sym) { |
810 | 8.82M | mx = sym >> 8; |
811 | 8.82M | my = sym & 0xFF; |
812 | 8.82M | } else { |
813 | | /* Escape */ |
814 | 1.28M | mx = get_bits(&h->gb, 6); |
815 | 1.28M | my = get_bits(&h->gb, 6); |
816 | 1.28M | } |
817 | | |
818 | 10.1M | mx += *mx_ptr - 32; |
819 | 10.1M | my += *my_ptr - 32; |
820 | | /* WARNING : they do not do exactly modulo encoding */ |
821 | 10.1M | if (mx <= -64) |
822 | 253k | mx += 64; |
823 | 9.85M | else if (mx >= 64) |
824 | 10.2k | mx -= 64; |
825 | | |
826 | 10.1M | if (my <= -64) |
827 | 240k | my += 64; |
828 | 9.87M | else if (my >= 64) |
829 | 33.2k | my -= 64; |
830 | 10.1M | *mx_ptr = mx; |
831 | 10.1M | *my_ptr = my; |
832 | 10.1M | } |
833 | | |
834 | | av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx) |
835 | 11.4k | { |
836 | 11.4k | static AVOnce init_static_once = AV_ONCE_INIT; |
837 | 11.4k | H263DecContext *const h = avctx->priv_data; |
838 | 11.4k | int ret; |
839 | | |
840 | 11.4k | ret = av_image_check_size(avctx->width, avctx->height, 0, avctx); |
841 | 11.4k | if (ret < 0) |
842 | 590 | return ret; |
843 | | |
844 | 10.8k | if (ff_h263_decode_init(avctx) < 0) |
845 | 0 | return -1; |
846 | | |
847 | | // We unquantize inter blocks as we parse them. |
848 | 10.8k | h->c.dct_unquantize_inter = NULL; |
849 | | |
850 | 10.8k | h->decode_header = msmpeg4_decode_picture_header; |
851 | | |
852 | 10.8k | ff_msmpeg4_common_init(&h->c, h->permutated_intra_h_scantable, |
853 | 10.8k | h->permutated_intra_v_scantable); |
854 | | |
855 | 10.8k | switch (h->c.msmpeg4_version) { |
856 | 1.63k | case MSMP4_V1: |
857 | 3.88k | case MSMP4_V2: |
858 | 3.88k | h->decode_mb = msmpeg4v12_decode_mb; |
859 | 3.88k | break; |
860 | 2.21k | case MSMP4_V3: |
861 | 4.53k | case MSMP4_WMV1: |
862 | 4.53k | h->decode_mb = msmpeg4v34_decode_mb; |
863 | 4.53k | break; |
864 | 2.47k | case MSMP4_WMV2: |
865 | 2.47k | break; |
866 | 0 | default: |
867 | 0 | av_unreachable("List contains all cases using ff_msmpeg4_decode_init()"); |
868 | 10.8k | } |
869 | | |
870 | 10.8k | h->slice_height = h->c.mb_height; //to avoid 1/0 if the first frame is not a keyframe |
871 | | |
872 | 10.8k | ff_thread_once(&init_static_once, msmpeg4_decode_init_static); |
873 | | |
874 | 10.8k | return 0; |
875 | 10.8k | } |
876 | | |
877 | | const FFCodec ff_msmpeg4v1_decoder = { |
878 | | .p.name = "msmpeg4v1", |
879 | | CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 1"), |
880 | | .p.type = AVMEDIA_TYPE_VIDEO, |
881 | | .p.id = AV_CODEC_ID_MSMPEG4V1, |
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_msmpeg4v2_decoder = { |
893 | | .p.name = "msmpeg4v2", |
894 | | CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 2"), |
895 | | .p.type = AVMEDIA_TYPE_VIDEO, |
896 | | .p.id = AV_CODEC_ID_MSMPEG4V2, |
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_msmpeg4v3_decoder = { |
908 | | .p.name = "msmpeg4", |
909 | | CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 3"), |
910 | | .p.type = AVMEDIA_TYPE_VIDEO, |
911 | | .p.id = AV_CODEC_ID_MSMPEG4V3, |
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 | | |
922 | | const FFCodec ff_wmv1_decoder = { |
923 | | .p.name = "wmv1", |
924 | | CODEC_LONG_NAME("Windows Media Video 7"), |
925 | | .p.type = AVMEDIA_TYPE_VIDEO, |
926 | | .p.id = AV_CODEC_ID_WMV1, |
927 | | .priv_data_size = sizeof(MSMP4DecContext), |
928 | | .init = ff_msmpeg4_decode_init, |
929 | | FF_CODEC_DECODE_CB(ff_h263_decode_frame), |
930 | | .close = ff_mpv_decode_close, |
931 | | .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1, |
932 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | |
933 | | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, |
934 | | .p.max_lowres = 3, |
935 | | }; |