/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 | 392k | #define V2_INTRA_CBPC_VLC_BITS 3 |
43 | 1.04M | #define V2_MB_TYPE_VLC_BITS 7 |
44 | 11.4M | #define MV_VLC_BITS 9 |
45 | | #define TEX_VLC_BITS 9 |
46 | | |
47 | 6.57M | #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.80M | { |
54 | 1.80M | int i; |
55 | | |
56 | 1.80M | if (n < 4) { |
57 | 1.23M | i= 0; |
58 | 1.23M | } else { |
59 | 572k | i= n-3; |
60 | 572k | } |
61 | | |
62 | 1.80M | *dc_val_ptr= &h->last_dc[i]; |
63 | 1.80M | return h->last_dc[i]; |
64 | 1.80M | } |
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 | 3.18M | { |
79 | 3.18M | int code, val, sign, shift; |
80 | | |
81 | 3.18M | code = get_vlc2(&h->gb, ff_h263_mv_vlc, H263_MV_VLC_BITS, 2); |
82 | 3.18M | ff_dlog(h->c.avctx, "MV code %d at %d %d pred: %d\n", |
83 | 3.18M | code, h->c.mb_x,h->c.mb_y, pred); |
84 | 3.18M | if (code < 0) |
85 | 11.2k | return 0xffff; |
86 | | |
87 | 3.17M | if (code == 0) |
88 | 1.37M | return pred; |
89 | 1.80M | sign = get_bits1(&h->gb); |
90 | 1.80M | shift = f_code - 1; |
91 | 1.80M | val = code; |
92 | 1.80M | if (shift) { |
93 | 0 | val = (val - 1) << shift; |
94 | 0 | val |= get_bits(&h->gb, shift); |
95 | 0 | val++; |
96 | 0 | } |
97 | 1.80M | if (sign) |
98 | 1.27M | val = -val; |
99 | | |
100 | 1.80M | val += pred; |
101 | 1.80M | if (val <= -64) |
102 | 26.5k | val += 64; |
103 | 1.77M | else if (val >= 64) |
104 | 2.15k | val -= 64; |
105 | | |
106 | 1.80M | return val; |
107 | 3.17M | } |
108 | | |
109 | | static int msmpeg4v12_decode_mb(H263DecContext *const h) |
110 | 5.55M | { |
111 | 5.55M | MSMP4DecContext *const ms = mpv_to_msmpeg4(h); |
112 | 5.55M | int cbp, code, i; |
113 | 5.55M | 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 | 5.55M | if (h->c.pict_type == AV_PICTURE_TYPE_P) { |
116 | 4.96M | if (ms->use_skip_mb_code) { |
117 | 4.16M | if (get_bits1(&h->gb)) { |
118 | | /* skip mb */ |
119 | 3.08M | h->c.mb_intra = 0; |
120 | 21.6M | for(i=0;i<6;i++) |
121 | 18.5M | h->c.block_last_index[i] = -1; |
122 | 3.08M | h->c.mv_dir = MV_DIR_FORWARD; |
123 | 3.08M | h->c.mv_type = MV_TYPE_16X16; |
124 | 3.08M | h->c.mv[0][0][0] = 0; |
125 | 3.08M | h->c.mv[0][0][1] = 0; |
126 | 3.08M | h->c.mb_skipped = 1; |
127 | 3.08M | *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_FORWARD_MV | MB_TYPE_16x16; |
128 | 3.08M | return 0; |
129 | 3.08M | } |
130 | 4.16M | } |
131 | | |
132 | 1.87M | if (h->c.msmpeg4_version == MSMP4_V2) |
133 | 1.04M | code = get_vlc2(&h->gb, v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 1); |
134 | 829k | else |
135 | 829k | code = get_vlc2(&h->gb, ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 2); |
136 | 1.87M | if(code<0 || code>7){ |
137 | 10.1k | av_log(h->c.avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", |
138 | 10.1k | code, h->c.mb_x, h->c.mb_y); |
139 | 10.1k | return -1; |
140 | 10.1k | } |
141 | | |
142 | 1.86M | h->c.mb_intra = code >>2; |
143 | | |
144 | 1.86M | cbp = code & 0x3; |
145 | 1.86M | } else { |
146 | 590k | h->c.mb_intra = 1; |
147 | 590k | if (h->c.msmpeg4_version == MSMP4_V2) |
148 | 392k | cbp = get_vlc2(&h->gb, v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 1); |
149 | 198k | else |
150 | 198k | cbp = get_vlc2(&h->gb, ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 2); |
151 | 590k | if(cbp<0 || cbp>3){ |
152 | 2.41k | av_log(h->c.avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", |
153 | 2.41k | cbp, h->c.mb_x, h->c.mb_y); |
154 | 2.41k | return -1; |
155 | 2.41k | } |
156 | 590k | } |
157 | | |
158 | 2.45M | if (!h->c.mb_intra) { |
159 | 1.69M | int mx, my, cbpy; |
160 | | |
161 | 1.69M | cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); |
162 | 1.69M | if(cbpy<0){ |
163 | 105k | av_log(h->c.avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", |
164 | 105k | cbp, h->c.mb_x, h->c.mb_y); |
165 | 105k | return -1; |
166 | 105k | } |
167 | | |
168 | 1.59M | cbp|= cbpy<<2; |
169 | 1.59M | if (h->c.msmpeg4_version == MSMP4_V1 || (cbp&3) != 3) |
170 | 1.59M | cbp ^= 0x3C; |
171 | | |
172 | 1.59M | ff_h263_pred_motion(&h->c, 0, 0, &mx, &my); |
173 | 1.59M | mx = msmpeg4v2_decode_motion(h, mx, 1); |
174 | 1.59M | my = msmpeg4v2_decode_motion(h, my, 1); |
175 | | |
176 | 1.59M | h->c.mv_dir = MV_DIR_FORWARD; |
177 | 1.59M | h->c.mv_type = MV_TYPE_16X16; |
178 | 1.59M | h->c.mv[0][0][0] = mx; |
179 | 1.59M | h->c.mv[0][0][1] = my; |
180 | 1.59M | *mb_type_ptr = MB_TYPE_FORWARD_MV | MB_TYPE_16x16; |
181 | 1.59M | } else { |
182 | 756k | int v; |
183 | 756k | if (h->c.msmpeg4_version == MSMP4_V2) { |
184 | 420k | h->c.ac_pred = get_bits1(&h->gb); |
185 | 420k | v = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); |
186 | 420k | if (v < 0) { |
187 | 1.59k | av_log(h->c.avctx, AV_LOG_ERROR, "cbpy vlc invalid\n"); |
188 | 1.59k | return -1; |
189 | 1.59k | } |
190 | 418k | cbp|= v<<2; |
191 | 418k | } else{ |
192 | 335k | h->c.ac_pred = 0; |
193 | 335k | v = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); |
194 | 335k | if (v < 0) { |
195 | 300 | av_log(h->c.avctx, AV_LOG_ERROR, "cbpy vlc invalid\n"); |
196 | 300 | return -1; |
197 | 300 | } |
198 | 335k | cbp|= v<<2; |
199 | 335k | if (h->c.pict_type==AV_PICTURE_TYPE_P) cbp^=0x3C; |
200 | 335k | } |
201 | 754k | *mb_type_ptr = MB_TYPE_INTRA; |
202 | 754k | } |
203 | | |
204 | 2.34M | h->c.bdsp.clear_blocks(h->block[0]); |
205 | 16.3M | for (i = 0; i < 6; i++) { |
206 | 14.0M | if (ff_msmpeg4_decode_block(ms, h->block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) |
207 | 25.0k | { |
208 | 25.0k | av_log(h->c.avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", |
209 | 25.0k | h->c.mb_x, h->c.mb_y, i); |
210 | 25.0k | return -1; |
211 | 25.0k | } |
212 | 14.0M | } |
213 | 2.32M | return 0; |
214 | 2.34M | } |
215 | | |
216 | | static int msmpeg4v34_decode_mb(H263DecContext *const h) |
217 | 13.7M | { |
218 | 13.7M | MSMP4DecContext *const ms = mpv_to_msmpeg4(h); |
219 | 13.7M | int cbp, code, i; |
220 | 13.7M | uint8_t *coded_val; |
221 | 13.7M | 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 | 13.7M | if (get_bits_left(&h->gb) <= 0) |
224 | 140k | return AVERROR_INVALIDDATA; |
225 | | |
226 | 13.6M | if (h->c.pict_type == AV_PICTURE_TYPE_P) { |
227 | 9.94M | if (ms->use_skip_mb_code) { |
228 | 5.32M | if (get_bits1(&h->gb)) { |
229 | | /* skip mb */ |
230 | 3.36M | h->c.mb_intra = 0; |
231 | 23.5M | for(i=0;i<6;i++) |
232 | 20.2M | h->c.block_last_index[i] = -1; |
233 | 3.36M | h->c.mv_dir = MV_DIR_FORWARD; |
234 | 3.36M | h->c.mv_type = MV_TYPE_16X16; |
235 | 3.36M | h->c.mv[0][0][0] = 0; |
236 | 3.36M | h->c.mv[0][0][1] = 0; |
237 | 3.36M | h->c.mb_skipped = 1; |
238 | 3.36M | *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_FORWARD_MV | MB_TYPE_16x16; |
239 | | |
240 | 3.36M | return 0; |
241 | 3.36M | } |
242 | 5.32M | } |
243 | | |
244 | 6.57M | 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.57M | h->c.mb_intra = (~code & 0x40) >> 6; |
247 | | |
248 | 6.57M | cbp = code & 0x3f; |
249 | 6.57M | } else { |
250 | 3.67M | h->c.mb_intra = 1; |
251 | 3.67M | code = get_vlc2(&h->gb, ff_msmp4_mb_i_vlc, MSMP4_MB_INTRA_VLC_BITS, 2); |
252 | | /* predict coded block pattern */ |
253 | 3.67M | cbp = 0; |
254 | 25.7M | for(i=0;i<6;i++) { |
255 | 22.0M | int val = ((code >> (5 - i)) & 1); |
256 | 22.0M | if (i < 4) { |
257 | 14.6M | int pred = ff_msmpeg4_coded_block_pred(&h->c, i, &coded_val); |
258 | 14.6M | val = val ^ pred; |
259 | 14.6M | *coded_val = val; |
260 | 14.6M | } |
261 | 22.0M | cbp |= val << (5 - i); |
262 | 22.0M | } |
263 | 3.67M | } |
264 | | |
265 | 10.2M | if (!h->c.mb_intra) { |
266 | 6.49M | int mx, my; |
267 | 6.49M | if (ms->per_mb_rl_table && cbp) { |
268 | 197k | ms->rl_table_index = decode012(&h->gb); |
269 | 197k | ms->rl_chroma_table_index = ms->rl_table_index; |
270 | 197k | } |
271 | 6.49M | ff_h263_pred_motion(&h->c, 0, 0, &mx, &my); |
272 | 6.49M | ff_msmpeg4_decode_motion(ms, &mx, &my); |
273 | 6.49M | h->c.mv_dir = MV_DIR_FORWARD; |
274 | 6.49M | h->c.mv_type = MV_TYPE_16X16; |
275 | 6.49M | h->c.mv[0][0][0] = mx; |
276 | 6.49M | h->c.mv[0][0][1] = my; |
277 | 6.49M | *mb_type_ptr = MB_TYPE_FORWARD_MV | MB_TYPE_16x16; |
278 | 6.49M | } else { |
279 | 3.75M | ff_dlog(h->c.avctx, "I at %d %d %d %06X\n", h->c.mb_x, h->c.mb_y, |
280 | 3.75M | ((cbp & 3) ? 1 : 0) +((cbp & 0x3C)? 2 : 0), |
281 | 3.75M | show_bits(&h->gb, 24)); |
282 | 3.75M | h->c.ac_pred = get_bits1(&h->gb); |
283 | 3.75M | *mb_type_ptr = MB_TYPE_INTRA; |
284 | 3.75M | if (h->c.inter_intra_pred) { |
285 | 6.26k | h->c.h263_aic_dir = get_vlc2(&h->gb, ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 1); |
286 | 6.26k | ff_dlog(h->c.avctx, "%d%d %d %d/", |
287 | 6.26k | h->c.ac_pred, h->c.h263_aic_dir, h->c.mb_x, h->c.mb_y); |
288 | 6.26k | } |
289 | 3.75M | if (ms->per_mb_rl_table && cbp) { |
290 | 687k | ms->rl_table_index = decode012(&h->gb); |
291 | 687k | ms->rl_chroma_table_index = ms->rl_table_index; |
292 | 687k | } |
293 | 3.75M | } |
294 | | |
295 | 10.2M | h->c.bdsp.clear_blocks(h->block[0]); |
296 | 71.7M | for (i = 0; i < 6; i++) { |
297 | 61.4M | if (ff_msmpeg4_decode_block(ms, h->block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) |
298 | 11.2k | { |
299 | 11.2k | av_log(h->c.avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", |
300 | 11.2k | h->c.mb_x, h->c.mb_y, i); |
301 | 11.2k | return -1; |
302 | 11.2k | } |
303 | 61.4M | } |
304 | | |
305 | 10.2M | return 0; |
306 | 10.2M | } |
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.10M | { |
365 | 1.10M | MSMP4DecContext *const ms = mpv_to_msmpeg4(h); |
366 | 1.10M | 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.10M | if (get_bits_left(&h->gb) * 8LL < (h->c.width+15)/16 * ((h->c.height+15)/16)) |
374 | 624k | return AVERROR_INVALIDDATA; |
375 | | |
376 | 480k | if (h->c.msmpeg4_version == MSMP4_V1) { |
377 | 87.2k | int start_code = get_bits_long(&h->gb, 32); |
378 | 87.2k | if(start_code!=0x00000100){ |
379 | 6.29k | av_log(h->c.avctx, AV_LOG_ERROR, "invalid startcode\n"); |
380 | 6.29k | return -1; |
381 | 6.29k | } |
382 | | |
383 | 80.9k | skip_bits(&h->gb, 5); // frame number */ |
384 | 80.9k | } |
385 | | |
386 | 474k | h->c.pict_type = get_bits(&h->gb, 2) + 1; |
387 | 474k | if (h->c.pict_type != AV_PICTURE_TYPE_I && |
388 | 297k | h->c.pict_type != AV_PICTURE_TYPE_P){ |
389 | 32.8k | av_log(h->c.avctx, AV_LOG_ERROR, "invalid picture type\n"); |
390 | 32.8k | return -1; |
391 | 32.8k | } |
392 | 441k | h->c.chroma_qscale = h->c.qscale = get_bits(&h->gb, 5); |
393 | 441k | if (h->c.qscale == 0) { |
394 | 64.8k | av_log(h->c.avctx, AV_LOG_ERROR, "invalid qscale\n"); |
395 | 64.8k | return -1; |
396 | 64.8k | } |
397 | | |
398 | 376k | if (h->c.pict_type == AV_PICTURE_TYPE_I) { |
399 | 140k | code = get_bits(&h->gb, 5); |
400 | 140k | if (h->c.msmpeg4_version == MSMP4_V1) { |
401 | 6.07k | if(code==0 || code>h->c.mb_height) { |
402 | 1.07k | av_log(h->c.avctx, AV_LOG_ERROR, "invalid slice height %d\n", code); |
403 | 1.07k | return -1; |
404 | 1.07k | } |
405 | | |
406 | 5.00k | h->slice_height = code; |
407 | 134k | }else{ |
408 | | /* 0x17: one slice, 0x18: two slices, ... */ |
409 | 134k | if (code < 0x17){ |
410 | 42.7k | av_log(h->c.avctx, AV_LOG_ERROR, "error, slice code was %X\n", code); |
411 | 42.7k | return -1; |
412 | 42.7k | } |
413 | | |
414 | 91.8k | h->slice_height = h->c.mb_height / (code - 0x16); |
415 | 91.8k | } |
416 | | |
417 | 96.8k | switch (h->c.msmpeg4_version) { |
418 | 5.00k | case MSMP4_V1: |
419 | 9.05k | case MSMP4_V2: |
420 | 9.05k | ms->rl_chroma_table_index = 2; |
421 | 9.05k | ms->rl_table_index = 2; |
422 | | |
423 | 9.05k | ms->dc_table_index = 0; //not used |
424 | 9.05k | break; |
425 | 4.66k | case MSMP4_V3: |
426 | 4.66k | ms->rl_chroma_table_index = decode012(&h->gb); |
427 | 4.66k | ms->rl_table_index = decode012(&h->gb); |
428 | | |
429 | 4.66k | ms->dc_table_index = get_bits1(&h->gb); |
430 | 4.66k | break; |
431 | 83.1k | case MSMP4_WMV1: |
432 | 83.1k | ff_msmpeg4_decode_ext_header(h, (2+5+5+17+7)/8); |
433 | | |
434 | 83.1k | if (ms->bit_rate > MBAC_BITRATE) |
435 | 25.9k | ms->per_mb_rl_table = get_bits1(&h->gb); |
436 | 57.1k | else |
437 | 57.1k | ms->per_mb_rl_table = 0; |
438 | | |
439 | 83.1k | if (!ms->per_mb_rl_table) { |
440 | 66.2k | ms->rl_chroma_table_index = decode012(&h->gb); |
441 | 66.2k | ms->rl_table_index = decode012(&h->gb); |
442 | 66.2k | } |
443 | | |
444 | 83.1k | ms->dc_table_index = get_bits1(&h->gb); |
445 | 83.1k | h->c.inter_intra_pred= 0; |
446 | 83.1k | break; |
447 | 0 | default: |
448 | 0 | av_unreachable("msmpeg4_decode_picture_header() only used by MSMP4V1-3, WMV1"); |
449 | 96.8k | } |
450 | 96.8k | h->c.no_rounding = 1; |
451 | 96.8k | 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 | 235k | } else { |
460 | 235k | switch (h->c.msmpeg4_version) { |
461 | 73.8k | case MSMP4_V1: |
462 | 152k | case MSMP4_V2: |
463 | 152k | if (h->c.msmpeg4_version == MSMP4_V1) |
464 | 73.8k | ms->use_skip_mb_code = 1; |
465 | 78.8k | else |
466 | 78.8k | ms->use_skip_mb_code = get_bits1(&h->gb); |
467 | 152k | ms->rl_table_index = 2; |
468 | 152k | ms->rl_chroma_table_index = ms->rl_table_index; |
469 | 152k | ms->dc_table_index = 0; //not used |
470 | 152k | ms->mv_table_index = 0; |
471 | 152k | break; |
472 | 62.2k | case MSMP4_V3: |
473 | 62.2k | ms->use_skip_mb_code = get_bits1(&h->gb); |
474 | 62.2k | ms->rl_table_index = decode012(&h->gb); |
475 | 62.2k | ms->rl_chroma_table_index = ms->rl_table_index; |
476 | | |
477 | 62.2k | ms->dc_table_index = get_bits1(&h->gb); |
478 | | |
479 | 62.2k | ms->mv_table_index = get_bits1(&h->gb); |
480 | 62.2k | break; |
481 | 20.6k | case MSMP4_WMV1: |
482 | 20.6k | ms->use_skip_mb_code = get_bits1(&h->gb); |
483 | | |
484 | 20.6k | if (ms->bit_rate > MBAC_BITRATE) |
485 | 7.38k | ms->per_mb_rl_table = get_bits1(&h->gb); |
486 | 13.2k | else |
487 | 13.2k | ms->per_mb_rl_table = 0; |
488 | | |
489 | 20.6k | if (!ms->per_mb_rl_table) { |
490 | 19.9k | ms->rl_table_index = decode012(&h->gb); |
491 | 19.9k | ms->rl_chroma_table_index = ms->rl_table_index; |
492 | 19.9k | } |
493 | | |
494 | 20.6k | ms->dc_table_index = get_bits1(&h->gb); |
495 | | |
496 | 20.6k | ms->mv_table_index = get_bits1(&h->gb); |
497 | 20.6k | h->c.inter_intra_pred = h->c.width*h->c.height < 320*240 && |
498 | 16.7k | ms->bit_rate <= II_BITRATE; |
499 | 20.6k | break; |
500 | 0 | default: |
501 | 0 | av_unreachable("msmpeg4_decode_picture_header() only used by MSMP4V1-3, WMV1"); |
502 | 235k | } |
503 | | |
504 | 235k | 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 | 235k | if (ms->flipflop_rounding) { |
515 | 3.04k | h->c.no_rounding ^= 1; |
516 | 232k | }else{ |
517 | 232k | h->c.no_rounding = 0; |
518 | 232k | } |
519 | 235k | } |
520 | 332k | ff_dlog(h->c.avctx, "%d %d %d %d %d\n", h->c.pict_type, ms->bit_rate, |
521 | 332k | h->c.inter_intra_pred, h->c.width, h->c.height); |
522 | | |
523 | 332k | ms->esc3_level_length = 0; |
524 | 332k | ms->esc3_run_length = 0; |
525 | | |
526 | 332k | return 0; |
527 | 376k | } |
528 | | |
529 | | int ff_msmpeg4_decode_ext_header(H263DecContext *const h, int buf_size) |
530 | 95.6k | { |
531 | 95.6k | MSMP4DecContext *const ms = mpv_to_msmpeg4(h); |
532 | 95.6k | int left = buf_size*8 - get_bits_count(&h->gb); |
533 | 95.6k | 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 | 95.6k | if(left>=length && left<length+8) |
536 | 84.0k | { |
537 | 84.0k | skip_bits(&h->gb, 5); /* fps */ |
538 | 84.0k | ms->bit_rate = get_bits(&h->gb, 11) * 1024; |
539 | 84.0k | if (h->c.msmpeg4_version >= MSMP4_V3) |
540 | 83.3k | ms->flipflop_rounding = get_bits1(&h->gb); |
541 | 719 | else |
542 | 719 | ms->flipflop_rounding = 0; |
543 | 84.0k | } |
544 | 11.5k | else if(left<length+8) |
545 | 5.54k | { |
546 | 5.54k | ms->flipflop_rounding = 0; |
547 | 5.54k | if (h->c.msmpeg4_version != MSMP4_V2) |
548 | 4.54k | av_log(h->c.avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left); |
549 | 5.54k | } |
550 | 5.95k | else |
551 | 5.95k | { |
552 | 5.95k | av_log(h->c.avctx, AV_LOG_ERROR, "I-frame too long, ignoring ext header\n"); |
553 | 5.95k | } |
554 | | |
555 | 95.6k | return 0; |
556 | 95.6k | } |
557 | | |
558 | | static int msmpeg4_decode_dc(MSMP4DecContext *const ms, int n, int *dir_ptr) |
559 | 36.5M | { |
560 | 36.5M | H263DecContext *const h = &ms->h; |
561 | 36.5M | int level, pred; |
562 | | |
563 | 36.5M | if (h->c.msmpeg4_version <= MSMP4_V2) { |
564 | 4.49M | if (n < 4) { |
565 | 3.00M | level = get_vlc2(&h->gb, v2_dc_lum_vlc, MSMP4_DC_VLC_BITS, 3); |
566 | 3.00M | } else { |
567 | 1.48M | level = get_vlc2(&h->gb, v2_dc_chroma_vlc, MSMP4_DC_VLC_BITS, 3); |
568 | 1.48M | } |
569 | 4.49M | if (level < 0) { |
570 | 308k | av_log(h->c.avctx, AV_LOG_ERROR, "illegal dc vlc\n"); |
571 | 308k | *dir_ptr = 0; |
572 | 308k | return -1; |
573 | 308k | } |
574 | 4.18M | level-=256; |
575 | 32.0M | } else { |
576 | 32.0M | level = get_vlc2(&h->gb, ff_msmp4_dc_vlc[ms->dc_table_index][n >= 4], |
577 | 32.0M | MSMP4_DC_VLC_BITS, 3); |
578 | | |
579 | 32.0M | if (level == DC_MAX) { |
580 | 1.08k | level = get_bits(&h->gb, 8); |
581 | 1.08k | if (get_bits1(&h->gb)) |
582 | 909 | level = -level; |
583 | 32.0M | } else if (level != 0) { |
584 | 21.4M | if (get_bits1(&h->gb)) |
585 | 7.10M | level = -level; |
586 | 21.4M | } |
587 | 32.0M | } |
588 | | |
589 | 36.2M | if (h->c.msmpeg4_version == MSMP4_V1) { |
590 | 1.80M | int32_t *dc_val; |
591 | 1.80M | pred = msmpeg4v1_pred_dc(h, n, &dc_val); |
592 | 1.80M | level += pred; |
593 | | |
594 | | /* update predictor */ |
595 | 1.80M | *dc_val= level; |
596 | 34.3M | }else{ |
597 | 34.3M | int16_t *dc_val; |
598 | 34.3M | pred = ff_msmpeg4_pred_dc(&h->c, n, &dc_val, dir_ptr); |
599 | 34.3M | level += pred; |
600 | | |
601 | | /* update predictor */ |
602 | 34.3M | if (n < 4) { |
603 | 22.9M | *dc_val = level * h->c.y_dc_scale; |
604 | 22.9M | } else { |
605 | 11.4M | *dc_val = level * h->c.c_dc_scale; |
606 | 11.4M | } |
607 | 34.3M | } |
608 | | |
609 | 36.2M | return level; |
610 | 36.5M | } |
611 | | |
612 | | int ff_msmpeg4_decode_block(MSMP4DecContext *const ms, int16_t * block, |
613 | | int n, int coded, const uint8_t *scan_table) |
614 | 93.7M | { |
615 | 93.7M | H263DecContext *const h = &ms->h; |
616 | 93.7M | int level, i, last, run, run_diff; |
617 | 93.7M | int dc_pred_dir = -1; //unused but its passed around, so it needs to be initialized |
618 | 93.7M | const RLTable *rl; |
619 | 93.7M | const RL_VLC_ELEM *rl_vlc; |
620 | 93.7M | int qmul, qadd; |
621 | | |
622 | 93.7M | if (h->c.mb_intra) { |
623 | 36.5M | qmul=1; |
624 | 36.5M | qadd=0; |
625 | | |
626 | | /* DC coef */ |
627 | 36.5M | level = msmpeg4_decode_dc(ms, n, &dc_pred_dir); |
628 | | |
629 | 36.5M | if (level < 0){ |
630 | 13.9M | av_log(h->c.avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, h->c.qscale); |
631 | 13.9M | if (h->c.inter_intra_pred) |
632 | 510 | level = 0; |
633 | 13.9M | } |
634 | 36.5M | if (n < 4) { |
635 | 24.3M | rl = &ff_rl_table[ms->rl_table_index]; |
636 | 24.3M | if (level > 256 * h->c.y_dc_scale) { |
637 | 795 | av_log(h->c.avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", h->c.qscale); |
638 | 795 | if (!h->c.inter_intra_pred) |
639 | 795 | return -1; |
640 | 795 | } |
641 | 24.3M | } else { |
642 | 12.1M | rl = &ff_rl_table[3 + ms->rl_chroma_table_index]; |
643 | 12.1M | if (level > 256 * h->c.c_dc_scale) { |
644 | 873 | av_log(h->c.avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", h->c.qscale); |
645 | 873 | if (!h->c.inter_intra_pred) |
646 | 873 | return -1; |
647 | 873 | } |
648 | 12.1M | } |
649 | 36.5M | block[0] = level; |
650 | | |
651 | 36.5M | run_diff = h->c.msmpeg4_version >= MSMP4_WMV1; |
652 | 36.5M | i = 0; |
653 | 36.5M | if (!coded) { |
654 | 19.9M | goto not_coded; |
655 | 19.9M | } |
656 | 16.6M | if (h->c.ac_pred) { |
657 | 5.64M | if (dc_pred_dir == 0) |
658 | 3.63M | scan_table = h->c.permutated_intra_v_scantable; /* left */ |
659 | 2.00M | else |
660 | 2.00M | scan_table = h->c.permutated_intra_h_scantable; /* top */ |
661 | 10.9M | } else { |
662 | 10.9M | scan_table = h->c.intra_scantable.permutated; |
663 | 10.9M | } |
664 | 16.6M | rl_vlc= rl->rl_vlc[0]; |
665 | 57.1M | } else { |
666 | 57.1M | qmul = h->c.qscale << 1; |
667 | 57.1M | qadd = (h->c.qscale - 1) | 1; |
668 | 57.1M | i = -1; |
669 | 57.1M | rl = &ff_rl_table[3 + ms->rl_table_index]; |
670 | | |
671 | 57.1M | if (h->c.msmpeg4_version == MSMP4_V2) |
672 | 5.78M | run_diff = 0; |
673 | 51.4M | else |
674 | 51.4M | run_diff = 1; |
675 | | |
676 | 57.1M | if (!coded) { |
677 | 38.9M | h->c.block_last_index[n] = i; |
678 | 38.9M | return 0; |
679 | 38.9M | } |
680 | 18.2M | if(!scan_table) |
681 | 9.57M | scan_table = h->c.inter_scantable.permutated; |
682 | 18.2M | rl_vlc= rl->rl_vlc[h->c.qscale]; |
683 | 18.2M | } |
684 | 34.8M | { |
685 | 34.8M | OPEN_READER(re, &h->gb); |
686 | 146M | for(;;) { |
687 | 146M | UPDATE_CACHE(re, &h->gb); |
688 | 146M | GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 0); |
689 | 146M | if (level==0) { |
690 | 1.95M | int cache; |
691 | 1.95M | cache= GET_CACHE(re, &h->gb); |
692 | | /* escape */ |
693 | 1.95M | if (h->c.msmpeg4_version == MSMP4_V1 || (cache&0x80000000)==0) { |
694 | 1.67M | if (h->c.msmpeg4_version == MSMP4_V1 || (cache&0x40000000)==0) { |
695 | | /* third escape */ |
696 | 1.57M | if (h->c.msmpeg4_version != MSMP4_V1) |
697 | 1.57M | LAST_SKIP_BITS(re, &h->gb, 2); |
698 | 1.57M | UPDATE_CACHE(re, &h->gb); |
699 | 1.57M | if (h->c.msmpeg4_version <= MSMP4_V3) { |
700 | 194k | last = SHOW_UBITS(re, &h->gb, 1); SKIP_CACHE(re, &h->gb, 1); |
701 | 194k | run = SHOW_UBITS(re, &h->gb, 6); SKIP_CACHE(re, &h->gb, 6); |
702 | 194k | level = SHOW_SBITS(re, &h->gb, 8); |
703 | 194k | SKIP_COUNTER(re, &h->gb, 1 + 6 + 8); |
704 | 1.38M | }else{ |
705 | 1.38M | int sign; |
706 | 1.38M | last = SHOW_UBITS(re, &h->gb, 1); SKIP_BITS(re, &h->gb, 1); |
707 | 1.38M | if (!ms->esc3_level_length) { |
708 | 6.98k | int ll; |
709 | 6.98k | ff_dlog(h->c.avctx, "ESC-3 %X at %d %d\n", |
710 | 6.98k | show_bits(&h->gb, 24), h->c.mb_x, h->c.mb_y); |
711 | 6.98k | 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.24k | ll = 8+SHOW_UBITS(re, &h->gb, 1); SKIP_BITS(re, &h->gb, 1); |
715 | 1.24k | } |
716 | 4.66k | }else{ |
717 | 4.66k | ll=2; |
718 | 21.2k | while (ll < 8 && SHOW_UBITS(re, &h->gb, 1) == 0) { |
719 | 16.5k | ll++; |
720 | 16.5k | SKIP_BITS(re, &h->gb, 1); |
721 | 16.5k | } |
722 | 4.66k | if (ll<8) SKIP_BITS(re, &h->gb, 1); |
723 | 4.66k | } |
724 | | |
725 | 6.98k | ms->esc3_level_length = ll; |
726 | 6.98k | ms->esc3_run_length = SHOW_UBITS(re, &h->gb, 2) + 3; SKIP_BITS(re, &h->gb, 2); |
727 | 6.98k | UPDATE_CACHE(re, &h->gb); |
728 | 6.98k | } |
729 | 1.38M | run = SHOW_UBITS(re, &h->gb, ms->esc3_run_length); |
730 | 1.38M | SKIP_BITS(re, &h->gb, ms->esc3_run_length); |
731 | | |
732 | 1.38M | sign= SHOW_UBITS(re, &h->gb, 1); |
733 | 1.38M | SKIP_BITS(re, &h->gb, 1); |
734 | | |
735 | 1.38M | level = SHOW_UBITS(re, &h->gb, ms->esc3_level_length); |
736 | 1.38M | SKIP_BITS(re, &h->gb, ms->esc3_level_length); |
737 | 1.38M | if(sign) level= -level; |
738 | 1.38M | } |
739 | | |
740 | | //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ; |
741 | 1.57M | if (level>0) level= level * qmul + qadd; |
742 | 1.28M | else level= level * qmul - qadd; |
743 | 1.57M | i+= run + 1; |
744 | 1.57M | if(last) i+=192; |
745 | 1.57M | } else { |
746 | | /* second escape */ |
747 | 97.8k | SKIP_BITS(re, &h->gb, 2); |
748 | 97.8k | GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1); |
749 | 97.8k | i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing |
750 | 97.8k | level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1); |
751 | 97.8k | LAST_SKIP_BITS(re, &h->gb, 1); |
752 | 97.8k | } |
753 | 1.67M | } else { |
754 | | /* first escape */ |
755 | 273k | SKIP_BITS(re, &h->gb, 1); |
756 | 273k | GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1); |
757 | 273k | i+= run; |
758 | 273k | level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing |
759 | 273k | level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1); |
760 | 273k | LAST_SKIP_BITS(re, &h->gb, 1); |
761 | 273k | } |
762 | 144M | } else { |
763 | 144M | i+= run; |
764 | 144M | level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1); |
765 | 144M | LAST_SKIP_BITS(re, &h->gb, 1); |
766 | 144M | } |
767 | 146M | if (i > 62){ |
768 | 34.8M | i-= 192; |
769 | 34.8M | if(i&(~63)){ |
770 | 7.79M | const int left = get_bits_left(&h->gb); |
771 | 7.79M | if (((i + 192 == 64 && level / qmul == -1) || |
772 | 7.71M | !(h->c.avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))) && |
773 | 7.77M | left >= 0) { |
774 | 7.74M | av_log(h->c.avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", |
775 | 7.74M | h->c.mb_x, h->c.mb_y); |
776 | 7.74M | i = 63; |
777 | 7.74M | break; |
778 | 7.74M | }else{ |
779 | 54.7k | av_log(h->c.avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", |
780 | 54.7k | h->c.mb_x, h->c.mb_y); |
781 | 54.7k | return -1; |
782 | 54.7k | } |
783 | 7.79M | } |
784 | | |
785 | 27.1M | block[scan_table[i]] = level; |
786 | 27.1M | break; |
787 | 34.8M | } |
788 | | |
789 | 111M | block[scan_table[i]] = level; |
790 | 111M | } |
791 | 34.8M | CLOSE_READER(re, &h->gb); |
792 | 34.8M | } |
793 | 34.8M | if (h->c.mb_intra) { |
794 | 36.4M | not_coded: |
795 | 36.4M | ff_mpeg4_pred_ac(h, block, n, dc_pred_dir); |
796 | 36.4M | } |
797 | 54.7M | h->c.block_last_index[n] = i; |
798 | | |
799 | 54.7M | return 0; |
800 | 34.8M | } |
801 | | |
802 | | void ff_msmpeg4_decode_motion(MSMP4DecContext *const ms, int *mx_ptr, int *my_ptr) |
803 | 11.4M | { |
804 | 11.4M | const VLCElem *const mv_vlc = mv_tables[ms->mv_table_index]; |
805 | 11.4M | H263DecContext *const h = &ms->h; |
806 | 11.4M | int sym, mx, my; |
807 | | |
808 | 11.4M | sym = get_vlc2(&h->gb, mv_vlc, MV_VLC_BITS, 2); |
809 | 11.4M | if (sym) { |
810 | 10.3M | mx = sym >> 8; |
811 | 10.3M | my = sym & 0xFF; |
812 | 10.3M | } else { |
813 | | /* Escape */ |
814 | 1.10M | mx = get_bits(&h->gb, 6); |
815 | 1.10M | my = get_bits(&h->gb, 6); |
816 | 1.10M | } |
817 | | |
818 | 11.4M | mx += *mx_ptr - 32; |
819 | 11.4M | my += *my_ptr - 32; |
820 | | /* WARNING : they do not do exactly modulo encoding */ |
821 | 11.4M | if (mx <= -64) |
822 | 294k | mx += 64; |
823 | 11.1M | else if (mx >= 64) |
824 | 8.33k | mx -= 64; |
825 | | |
826 | 11.4M | if (my <= -64) |
827 | 256k | my += 64; |
828 | 11.1M | else if (my >= 64) |
829 | 31.8k | my -= 64; |
830 | 11.4M | *mx_ptr = mx; |
831 | 11.4M | *my_ptr = my; |
832 | 11.4M | } |
833 | | |
834 | | av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx) |
835 | 13.0k | { |
836 | 13.0k | static AVOnce init_static_once = AV_ONCE_INIT; |
837 | 13.0k | H263DecContext *const h = avctx->priv_data; |
838 | 13.0k | int ret; |
839 | | |
840 | 13.0k | ret = av_image_check_size(avctx->width, avctx->height, 0, avctx); |
841 | 13.0k | if (ret < 0) |
842 | 632 | return ret; |
843 | | |
844 | 12.4k | if (ff_h263_decode_init(avctx) < 0) |
845 | 0 | return -1; |
846 | | |
847 | | // We unquantize inter blocks as we parse them. |
848 | 12.4k | h->c.dct_unquantize_inter = NULL; |
849 | | |
850 | 12.4k | h->decode_header = msmpeg4_decode_picture_header; |
851 | | |
852 | 12.4k | ff_msmpeg4_common_init(&h->c); |
853 | | |
854 | 12.4k | switch (h->c.msmpeg4_version) { |
855 | 1.85k | case MSMP4_V1: |
856 | 4.44k | case MSMP4_V2: |
857 | 4.44k | h->decode_mb = msmpeg4v12_decode_mb; |
858 | 4.44k | break; |
859 | 2.42k | case MSMP4_V3: |
860 | 5.09k | case MSMP4_WMV1: |
861 | 5.09k | h->decode_mb = msmpeg4v34_decode_mb; |
862 | 5.09k | break; |
863 | 2.91k | case MSMP4_WMV2: |
864 | 2.91k | break; |
865 | 0 | default: |
866 | 0 | av_unreachable("List contains all cases using ff_msmpeg4_decode_init()"); |
867 | 12.4k | } |
868 | | |
869 | 12.4k | h->slice_height = h->c.mb_height; //to avoid 1/0 if the first frame is not a keyframe |
870 | | |
871 | 12.4k | ff_thread_once(&init_static_once, msmpeg4_decode_init_static); |
872 | | |
873 | 12.4k | return 0; |
874 | 12.4k | } |
875 | | |
876 | | const FFCodec ff_msmpeg4v1_decoder = { |
877 | | .p.name = "msmpeg4v1", |
878 | | CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 1"), |
879 | | .p.type = AVMEDIA_TYPE_VIDEO, |
880 | | .p.id = AV_CODEC_ID_MSMPEG4V1, |
881 | | .priv_data_size = sizeof(MSMP4DecContext), |
882 | | .init = ff_msmpeg4_decode_init, |
883 | | FF_CODEC_DECODE_CB(ff_h263_decode_frame), |
884 | | .close = ff_mpv_decode_close, |
885 | | .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1, |
886 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | |
887 | | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, |
888 | | .p.max_lowres = 3, |
889 | | }; |
890 | | |
891 | | const FFCodec ff_msmpeg4v2_decoder = { |
892 | | .p.name = "msmpeg4v2", |
893 | | CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 2"), |
894 | | .p.type = AVMEDIA_TYPE_VIDEO, |
895 | | .p.id = AV_CODEC_ID_MSMPEG4V2, |
896 | | .priv_data_size = sizeof(MSMP4DecContext), |
897 | | .init = ff_msmpeg4_decode_init, |
898 | | FF_CODEC_DECODE_CB(ff_h263_decode_frame), |
899 | | .close = ff_mpv_decode_close, |
900 | | .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1, |
901 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | |
902 | | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, |
903 | | .p.max_lowres = 3, |
904 | | }; |
905 | | |
906 | | const FFCodec ff_msmpeg4v3_decoder = { |
907 | | .p.name = "msmpeg4", |
908 | | CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 3"), |
909 | | .p.type = AVMEDIA_TYPE_VIDEO, |
910 | | .p.id = AV_CODEC_ID_MSMPEG4V3, |
911 | | .priv_data_size = sizeof(MSMP4DecContext), |
912 | | .init = ff_msmpeg4_decode_init, |
913 | | FF_CODEC_DECODE_CB(ff_h263_decode_frame), |
914 | | .close = ff_mpv_decode_close, |
915 | | .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1, |
916 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | |
917 | | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, |
918 | | .p.max_lowres = 3, |
919 | | }; |
920 | | |
921 | | const FFCodec ff_wmv1_decoder = { |
922 | | .p.name = "wmv1", |
923 | | CODEC_LONG_NAME("Windows Media Video 7"), |
924 | | .p.type = AVMEDIA_TYPE_VIDEO, |
925 | | .p.id = AV_CODEC_ID_WMV1, |
926 | | .priv_data_size = sizeof(MSMP4DecContext), |
927 | | .init = ff_msmpeg4_decode_init, |
928 | | FF_CODEC_DECODE_CB(ff_h263_decode_frame), |
929 | | .close = ff_mpv_decode_close, |
930 | | .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1, |
931 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | |
932 | | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, |
933 | | .p.max_lowres = 3, |
934 | | }; |