/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 | 416k | #define V2_INTRA_CBPC_VLC_BITS 3 |
43 | 823k | #define V2_MB_TYPE_VLC_BITS 7 |
44 | 11.1M | #define MV_VLC_BITS 9 |
45 | | #define TEX_VLC_BITS 9 |
46 | | |
47 | 6.62M | #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.69M | { |
54 | 1.69M | int i; |
55 | | |
56 | 1.69M | if (n < 4) { |
57 | 1.16M | i= 0; |
58 | 1.16M | } else { |
59 | 535k | i= n-3; |
60 | 535k | } |
61 | | |
62 | 1.69M | *dc_val_ptr= &h->last_dc[i]; |
63 | 1.69M | return h->last_dc[i]; |
64 | 1.69M | } |
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.39M | { |
79 | 2.39M | int code, val, sign, shift; |
80 | | |
81 | 2.39M | code = get_vlc2(&h->gb, ff_h263_mv_vlc, H263_MV_VLC_BITS, 2); |
82 | 2.39M | ff_dlog(h->c.avctx, "MV code %d at %d %d pred: %d\n", |
83 | 2.39M | code, h->c.mb_x,h->c.mb_y, pred); |
84 | 2.39M | if (code < 0) |
85 | 5.04k | return 0xffff; |
86 | | |
87 | 2.39M | if (code == 0) |
88 | 1.04M | return pred; |
89 | 1.34M | sign = get_bits1(&h->gb); |
90 | 1.34M | shift = f_code - 1; |
91 | 1.34M | val = code; |
92 | 1.34M | if (shift) { |
93 | 0 | val = (val - 1) << shift; |
94 | 0 | val |= get_bits(&h->gb, shift); |
95 | 0 | val++; |
96 | 0 | } |
97 | 1.34M | if (sign) |
98 | 944k | val = -val; |
99 | | |
100 | 1.34M | val += pred; |
101 | 1.34M | if (val <= -64) |
102 | 22.9k | val += 64; |
103 | 1.32M | else if (val >= 64) |
104 | 1.67k | val -= 64; |
105 | | |
106 | 1.34M | return val; |
107 | 2.39M | } |
108 | | |
109 | | static int msmpeg4v12_decode_mb(H263DecContext *const h) |
110 | 4.51M | { |
111 | 4.51M | MSMP4DecContext *const ms = mpv_to_msmpeg4(h); |
112 | 4.51M | int cbp, code, i; |
113 | 4.51M | 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.51M | if (h->c.pict_type == AV_PICTURE_TYPE_P) { |
116 | 3.87M | if (ms->use_skip_mb_code) { |
117 | 3.25M | if (get_bits1(&h->gb)) { |
118 | | /* skip mb */ |
119 | 2.41M | h->c.mb_intra = 0; |
120 | 16.8M | for(i=0;i<6;i++) |
121 | 14.4M | h->c.block_last_index[i] = -1; |
122 | 2.41M | h->c.mv_dir = MV_DIR_FORWARD; |
123 | 2.41M | h->c.mv_type = MV_TYPE_16X16; |
124 | 2.41M | h->c.mv[0][0][0] = 0; |
125 | 2.41M | h->c.mv[0][0][1] = 0; |
126 | 2.41M | h->c.mb_skipped = 1; |
127 | 2.41M | *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_FORWARD_MV | MB_TYPE_16x16; |
128 | 2.41M | return 0; |
129 | 2.41M | } |
130 | 3.25M | } |
131 | | |
132 | 1.46M | if (h->c.msmpeg4_version == MSMP4_V2) |
133 | 823k | code = get_vlc2(&h->gb, v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 1); |
134 | 639k | else |
135 | 639k | code = get_vlc2(&h->gb, ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 2); |
136 | 1.46M | if(code<0 || code>7){ |
137 | 13.5k | av_log(h->c.avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", |
138 | 13.5k | code, h->c.mb_x, h->c.mb_y); |
139 | 13.5k | return -1; |
140 | 13.5k | } |
141 | | |
142 | 1.44M | h->c.mb_intra = code >>2; |
143 | | |
144 | 1.44M | cbp = code & 0x3; |
145 | 1.44M | } else { |
146 | 640k | h->c.mb_intra = 1; |
147 | 640k | if (h->c.msmpeg4_version == MSMP4_V2) |
148 | 416k | 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 | 640k | if(cbp<0 || cbp>3){ |
152 | 2.48k | av_log(h->c.avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", |
153 | 2.48k | cbp, h->c.mb_x, h->c.mb_y); |
154 | 2.48k | return -1; |
155 | 2.48k | } |
156 | 640k | } |
157 | | |
158 | 2.08M | if (!h->c.mb_intra) { |
159 | 1.32M | int mx, my, cbpy; |
160 | | |
161 | 1.32M | cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); |
162 | 1.32M | if(cbpy<0){ |
163 | 131k | av_log(h->c.avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", |
164 | 131k | cbp, h->c.mb_x, h->c.mb_y); |
165 | 131k | return -1; |
166 | 131k | } |
167 | | |
168 | 1.19M | cbp|= cbpy<<2; |
169 | 1.19M | if (h->c.msmpeg4_version == MSMP4_V1 || (cbp&3) != 3) |
170 | 1.19M | cbp ^= 0x3C; |
171 | | |
172 | 1.19M | ff_h263_pred_motion(&h->c, 0, 0, &mx, &my); |
173 | 1.19M | mx = msmpeg4v2_decode_motion(h, mx, 1); |
174 | 1.19M | my = msmpeg4v2_decode_motion(h, my, 1); |
175 | | |
176 | 1.19M | h->c.mv_dir = MV_DIR_FORWARD; |
177 | 1.19M | h->c.mv_type = MV_TYPE_16X16; |
178 | 1.19M | h->c.mv[0][0][0] = mx; |
179 | 1.19M | h->c.mv[0][0][1] = my; |
180 | 1.19M | *mb_type_ptr = MB_TYPE_FORWARD_MV | MB_TYPE_16x16; |
181 | 1.19M | } else { |
182 | 756k | int v; |
183 | 756k | if (h->c.msmpeg4_version == MSMP4_V2) { |
184 | 437k | h->c.ac_pred = get_bits1(&h->gb); |
185 | 437k | v = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); |
186 | 437k | if (v < 0) { |
187 | 1.50k | av_log(h->c.avctx, AV_LOG_ERROR, "cbpy vlc invalid\n"); |
188 | 1.50k | return -1; |
189 | 1.50k | } |
190 | 436k | cbp|= v<<2; |
191 | 436k | } else{ |
192 | 318k | h->c.ac_pred = 0; |
193 | 318k | v = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); |
194 | 318k | if (v < 0) { |
195 | 324 | av_log(h->c.avctx, AV_LOG_ERROR, "cbpy vlc invalid\n"); |
196 | 324 | return -1; |
197 | 324 | } |
198 | 318k | cbp|= v<<2; |
199 | 318k | if (h->c.pict_type==AV_PICTURE_TYPE_P) cbp^=0x3C; |
200 | 318k | } |
201 | 755k | *mb_type_ptr = MB_TYPE_INTRA; |
202 | 755k | } |
203 | | |
204 | 1.95M | h->c.bdsp.clear_blocks(h->block[0]); |
205 | 13.5M | for (i = 0; i < 6; i++) { |
206 | 11.6M | if (ff_msmpeg4_decode_block(ms, h->block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) |
207 | 20.6k | { |
208 | 20.6k | av_log(h->c.avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", |
209 | 20.6k | h->c.mb_x, h->c.mb_y, i); |
210 | 20.6k | return -1; |
211 | 20.6k | } |
212 | 11.6M | } |
213 | 1.93M | return 0; |
214 | 1.95M | } |
215 | | |
216 | | static int msmpeg4v34_decode_mb(H263DecContext *const h) |
217 | 12.5M | { |
218 | 12.5M | MSMP4DecContext *const ms = mpv_to_msmpeg4(h); |
219 | 12.5M | int cbp, code, i; |
220 | 12.5M | uint8_t *coded_val; |
221 | 12.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 | 12.5M | if (get_bits_left(&h->gb) <= 0) |
224 | 84.7k | return AVERROR_INVALIDDATA; |
225 | | |
226 | 12.4M | if (h->c.pict_type == AV_PICTURE_TYPE_P) { |
227 | 10.2M | if (ms->use_skip_mb_code) { |
228 | 5.31M | if (get_bits1(&h->gb)) { |
229 | | /* skip mb */ |
230 | 3.61M | h->c.mb_intra = 0; |
231 | 25.2M | for(i=0;i<6;i++) |
232 | 21.6M | h->c.block_last_index[i] = -1; |
233 | 3.61M | h->c.mv_dir = MV_DIR_FORWARD; |
234 | 3.61M | h->c.mv_type = MV_TYPE_16X16; |
235 | 3.61M | h->c.mv[0][0][0] = 0; |
236 | 3.61M | h->c.mv[0][0][1] = 0; |
237 | 3.61M | h->c.mb_skipped = 1; |
238 | 3.61M | *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_FORWARD_MV | MB_TYPE_16x16; |
239 | | |
240 | 3.61M | return 0; |
241 | 3.61M | } |
242 | 5.31M | } |
243 | | |
244 | 6.62M | 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.62M | h->c.mb_intra = (~code & 0x40) >> 6; |
247 | | |
248 | 6.62M | cbp = code & 0x3f; |
249 | 6.62M | } else { |
250 | 2.22M | h->c.mb_intra = 1; |
251 | 2.22M | code = get_vlc2(&h->gb, ff_msmp4_mb_i_vlc, MSMP4_MB_INTRA_VLC_BITS, 2); |
252 | | /* predict coded block pattern */ |
253 | 2.22M | cbp = 0; |
254 | 15.5M | for(i=0;i<6;i++) { |
255 | 13.3M | int val = ((code >> (5 - i)) & 1); |
256 | 13.3M | if (i < 4) { |
257 | 8.90M | int pred = ff_msmpeg4_coded_block_pred(&h->c, i, &coded_val); |
258 | 8.90M | val = val ^ pred; |
259 | 8.90M | *coded_val = val; |
260 | 8.90M | } |
261 | 13.3M | cbp |= val << (5 - i); |
262 | 13.3M | } |
263 | 2.22M | } |
264 | | |
265 | 8.84M | if (!h->c.mb_intra) { |
266 | 6.52M | int mx, my; |
267 | 6.52M | if (ms->per_mb_rl_table && cbp) { |
268 | 73.8k | ms->rl_table_index = decode012(&h->gb); |
269 | 73.8k | ms->rl_chroma_table_index = ms->rl_table_index; |
270 | 73.8k | } |
271 | 6.52M | ff_h263_pred_motion(&h->c, 0, 0, &mx, &my); |
272 | 6.52M | ff_msmpeg4_decode_motion(ms, &mx, &my); |
273 | 6.52M | h->c.mv_dir = MV_DIR_FORWARD; |
274 | 6.52M | h->c.mv_type = MV_TYPE_16X16; |
275 | 6.52M | h->c.mv[0][0][0] = mx; |
276 | 6.52M | h->c.mv[0][0][1] = my; |
277 | 6.52M | *mb_type_ptr = MB_TYPE_FORWARD_MV | MB_TYPE_16x16; |
278 | 6.52M | } else { |
279 | 2.31M | ff_dlog(h->c.avctx, "I at %d %d %d %06X\n", h->c.mb_x, h->c.mb_y, |
280 | 2.31M | ((cbp & 3) ? 1 : 0) +((cbp & 0x3C)? 2 : 0), |
281 | 2.31M | show_bits(&h->gb, 24)); |
282 | 2.31M | h->c.ac_pred = get_bits1(&h->gb); |
283 | 2.31M | *mb_type_ptr = MB_TYPE_INTRA; |
284 | 2.31M | if (h->c.inter_intra_pred) { |
285 | 9.59k | h->c.h263_aic_dir = get_vlc2(&h->gb, ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 1); |
286 | 9.59k | ff_dlog(h->c.avctx, "%d%d %d %d/", |
287 | 9.59k | h->c.ac_pred, h->c.h263_aic_dir, h->c.mb_x, h->c.mb_y); |
288 | 9.59k | } |
289 | 2.31M | if (ms->per_mb_rl_table && cbp) { |
290 | 317k | ms->rl_table_index = decode012(&h->gb); |
291 | 317k | ms->rl_chroma_table_index = ms->rl_table_index; |
292 | 317k | } |
293 | 2.31M | } |
294 | | |
295 | 8.84M | h->c.bdsp.clear_blocks(h->block[0]); |
296 | 61.8M | for (i = 0; i < 6; i++) { |
297 | 53.0M | if (ff_msmpeg4_decode_block(ms, h->block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) |
298 | 9.08k | { |
299 | 9.08k | av_log(h->c.avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", |
300 | 9.08k | h->c.mb_x, h->c.mb_y, i); |
301 | 9.08k | return -1; |
302 | 9.08k | } |
303 | 53.0M | } |
304 | | |
305 | 8.83M | return 0; |
306 | 8.84M | } |
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.11M | { |
365 | 1.11M | MSMP4DecContext *const ms = mpv_to_msmpeg4(h); |
366 | 1.11M | 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.11M | if (get_bits_left(&h->gb) * 8LL < (h->c.width+15)/16 * ((h->c.height+15)/16)) |
374 | 627k | return AVERROR_INVALIDDATA; |
375 | | |
376 | 482k | if (h->c.msmpeg4_version == MSMP4_V1) { |
377 | 130k | int start_code = get_bits_long(&h->gb, 32); |
378 | 130k | if(start_code!=0x00000100){ |
379 | 7.45k | av_log(h->c.avctx, AV_LOG_ERROR, "invalid startcode\n"); |
380 | 7.45k | return -1; |
381 | 7.45k | } |
382 | | |
383 | 122k | skip_bits(&h->gb, 5); // frame number */ |
384 | 122k | } |
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 | 325k | h->c.pict_type != AV_PICTURE_TYPE_P){ |
389 | 42.8k | av_log(h->c.avctx, AV_LOG_ERROR, "invalid picture type\n"); |
390 | 42.8k | return -1; |
391 | 42.8k | } |
392 | 432k | h->c.chroma_qscale = h->c.qscale = get_bits(&h->gb, 5); |
393 | 432k | if (h->c.qscale == 0) { |
394 | 46.0k | av_log(h->c.avctx, AV_LOG_ERROR, "invalid qscale\n"); |
395 | 46.0k | return -1; |
396 | 46.0k | } |
397 | | |
398 | 386k | if (h->c.pict_type == AV_PICTURE_TYPE_I) { |
399 | 107k | code = get_bits(&h->gb, 5); |
400 | 107k | if (h->c.msmpeg4_version == MSMP4_V1) { |
401 | 7.42k | if(code==0 || code>h->c.mb_height) { |
402 | 1.08k | av_log(h->c.avctx, AV_LOG_ERROR, "invalid slice height %d\n", code); |
403 | 1.08k | return -1; |
404 | 1.08k | } |
405 | | |
406 | 6.34k | h->slice_height = code; |
407 | 100k | }else{ |
408 | | /* 0x17: one slice, 0x18: two slices, ... */ |
409 | 100k | if (code < 0x17){ |
410 | 69.6k | av_log(h->c.avctx, AV_LOG_ERROR, "error, slice code was %X\n", code); |
411 | 69.6k | return -1; |
412 | 69.6k | } |
413 | | |
414 | 30.3k | h->slice_height = h->c.mb_height / (code - 0x16); |
415 | 30.3k | } |
416 | | |
417 | 36.7k | switch (h->c.msmpeg4_version) { |
418 | 6.34k | case MSMP4_V1: |
419 | 10.0k | case MSMP4_V2: |
420 | 10.0k | ms->rl_chroma_table_index = 2; |
421 | 10.0k | ms->rl_table_index = 2; |
422 | | |
423 | 10.0k | ms->dc_table_index = 0; //not used |
424 | 10.0k | break; |
425 | 4.64k | case MSMP4_V3: |
426 | 4.64k | ms->rl_chroma_table_index = decode012(&h->gb); |
427 | 4.64k | ms->rl_table_index = decode012(&h->gb); |
428 | | |
429 | 4.64k | ms->dc_table_index = get_bits1(&h->gb); |
430 | 4.64k | break; |
431 | 21.9k | case MSMP4_WMV1: |
432 | 21.9k | ff_msmpeg4_decode_ext_header(h, (2+5+5+17+7)/8); |
433 | | |
434 | 21.9k | if (ms->bit_rate > MBAC_BITRATE) |
435 | 9.19k | ms->per_mb_rl_table = get_bits1(&h->gb); |
436 | 12.7k | else |
437 | 12.7k | ms->per_mb_rl_table = 0; |
438 | | |
439 | 21.9k | if (!ms->per_mb_rl_table) { |
440 | 15.9k | ms->rl_chroma_table_index = decode012(&h->gb); |
441 | 15.9k | ms->rl_table_index = decode012(&h->gb); |
442 | 15.9k | } |
443 | | |
444 | 21.9k | ms->dc_table_index = get_bits1(&h->gb); |
445 | 21.9k | h->c.inter_intra_pred= 0; |
446 | 21.9k | break; |
447 | 0 | default: |
448 | 0 | av_unreachable("msmpeg4_decode_picture_header() only used by MSMP4V1-3, WMV1"); |
449 | 36.7k | } |
450 | 36.7k | h->c.no_rounding = 1; |
451 | 36.7k | 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 | 278k | } else { |
460 | 278k | switch (h->c.msmpeg4_version) { |
461 | 114k | case MSMP4_V1: |
462 | 177k | case MSMP4_V2: |
463 | 177k | if (h->c.msmpeg4_version == MSMP4_V1) |
464 | 114k | ms->use_skip_mb_code = 1; |
465 | 62.8k | else |
466 | 62.8k | ms->use_skip_mb_code = get_bits1(&h->gb); |
467 | 177k | ms->rl_table_index = 2; |
468 | 177k | ms->rl_chroma_table_index = ms->rl_table_index; |
469 | 177k | ms->dc_table_index = 0; //not used |
470 | 177k | ms->mv_table_index = 0; |
471 | 177k | break; |
472 | 55.3k | case MSMP4_V3: |
473 | 55.3k | ms->use_skip_mb_code = get_bits1(&h->gb); |
474 | 55.3k | ms->rl_table_index = decode012(&h->gb); |
475 | 55.3k | ms->rl_chroma_table_index = ms->rl_table_index; |
476 | | |
477 | 55.3k | ms->dc_table_index = get_bits1(&h->gb); |
478 | | |
479 | 55.3k | ms->mv_table_index = get_bits1(&h->gb); |
480 | 55.3k | break; |
481 | 46.0k | case MSMP4_WMV1: |
482 | 46.0k | ms->use_skip_mb_code = get_bits1(&h->gb); |
483 | | |
484 | 46.0k | if (ms->bit_rate > MBAC_BITRATE) |
485 | 4.47k | ms->per_mb_rl_table = get_bits1(&h->gb); |
486 | 41.5k | else |
487 | 41.5k | ms->per_mb_rl_table = 0; |
488 | | |
489 | 46.0k | if (!ms->per_mb_rl_table) { |
490 | 45.6k | ms->rl_table_index = decode012(&h->gb); |
491 | 45.6k | ms->rl_chroma_table_index = ms->rl_table_index; |
492 | 45.6k | } |
493 | | |
494 | 46.0k | ms->dc_table_index = get_bits1(&h->gb); |
495 | | |
496 | 46.0k | ms->mv_table_index = get_bits1(&h->gb); |
497 | 46.0k | h->c.inter_intra_pred = h->c.width*h->c.height < 320*240 && |
498 | 42.8k | ms->bit_rate <= II_BITRATE; |
499 | 46.0k | break; |
500 | 0 | default: |
501 | 0 | av_unreachable("msmpeg4_decode_picture_header() only used by MSMP4V1-3, WMV1"); |
502 | 278k | } |
503 | | |
504 | 278k | 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 | 278k | if (ms->flipflop_rounding) { |
515 | 4.02k | h->c.no_rounding ^= 1; |
516 | 274k | }else{ |
517 | 274k | h->c.no_rounding = 0; |
518 | 274k | } |
519 | 278k | } |
520 | 315k | ff_dlog(h->c.avctx, "%d %d %d %d %d\n", h->c.pict_type, ms->bit_rate, |
521 | 315k | h->c.inter_intra_pred, h->c.width, h->c.height); |
522 | | |
523 | 315k | ms->esc3_level_length = 0; |
524 | 315k | ms->esc3_run_length = 0; |
525 | | |
526 | 315k | return 0; |
527 | 386k | } |
528 | | |
529 | | int ff_msmpeg4_decode_ext_header(H263DecContext *const h, int buf_size) |
530 | 35.5k | { |
531 | 35.5k | MSMP4DecContext *const ms = mpv_to_msmpeg4(h); |
532 | 35.5k | int left = buf_size*8 - get_bits_count(&h->gb); |
533 | 35.5k | 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 | 35.5k | if(left>=length && left<length+8) |
536 | 22.9k | { |
537 | 22.9k | skip_bits(&h->gb, 5); /* fps */ |
538 | 22.9k | ms->bit_rate = get_bits(&h->gb, 11) * 1024; |
539 | 22.9k | if (h->c.msmpeg4_version >= MSMP4_V3) |
540 | 22.2k | ms->flipflop_rounding = get_bits1(&h->gb); |
541 | 753 | else |
542 | 753 | ms->flipflop_rounding = 0; |
543 | 22.9k | } |
544 | 12.5k | else if(left<length+8) |
545 | 6.79k | { |
546 | 6.79k | ms->flipflop_rounding = 0; |
547 | 6.79k | if (h->c.msmpeg4_version != MSMP4_V2) |
548 | 5.73k | av_log(h->c.avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left); |
549 | 6.79k | } |
550 | 5.76k | else |
551 | 5.76k | { |
552 | 5.76k | av_log(h->c.avctx, AV_LOG_ERROR, "I-frame too long, ignoring ext header\n"); |
553 | 5.76k | } |
554 | | |
555 | 35.5k | return 0; |
556 | 35.5k | } |
557 | | |
558 | | static int msmpeg4_decode_dc(MSMP4DecContext *const ms, int n, int *dir_ptr) |
559 | 26.1M | { |
560 | 26.1M | H263DecContext *const h = &ms->h; |
561 | 26.1M | int level, pred; |
562 | | |
563 | 26.1M | if (h->c.msmpeg4_version <= MSMP4_V2) { |
564 | 4.51M | if (n < 4) { |
565 | 3.01M | level = get_vlc2(&h->gb, v2_dc_lum_vlc, MSMP4_DC_VLC_BITS, 3); |
566 | 3.01M | } else { |
567 | 1.50M | level = get_vlc2(&h->gb, v2_dc_chroma_vlc, MSMP4_DC_VLC_BITS, 3); |
568 | 1.50M | } |
569 | 4.51M | if (level < 0) { |
570 | 319k | av_log(h->c.avctx, AV_LOG_ERROR, "illegal dc vlc\n"); |
571 | 319k | *dir_ptr = 0; |
572 | 319k | return -1; |
573 | 319k | } |
574 | 4.19M | level-=256; |
575 | 21.6M | } else { |
576 | 21.6M | level = get_vlc2(&h->gb, ff_msmp4_dc_vlc[ms->dc_table_index][n >= 4], |
577 | 21.6M | MSMP4_DC_VLC_BITS, 3); |
578 | | |
579 | 21.6M | if (level == DC_MAX) { |
580 | 1.46k | level = get_bits(&h->gb, 8); |
581 | 1.46k | if (get_bits1(&h->gb)) |
582 | 1.09k | level = -level; |
583 | 21.6M | } else if (level != 0) { |
584 | 14.2M | if (get_bits1(&h->gb)) |
585 | 4.29M | level = -level; |
586 | 14.2M | } |
587 | 21.6M | } |
588 | | |
589 | 25.8M | if (h->c.msmpeg4_version == MSMP4_V1) { |
590 | 1.69M | int32_t *dc_val; |
591 | 1.69M | pred = msmpeg4v1_pred_dc(h, n, &dc_val); |
592 | 1.69M | level += pred; |
593 | | |
594 | | /* update predictor */ |
595 | 1.69M | *dc_val= level; |
596 | 24.1M | }else{ |
597 | 24.1M | int16_t *dc_val; |
598 | 24.1M | pred = ff_msmpeg4_pred_dc(&h->c, n, &dc_val, dir_ptr); |
599 | 24.1M | level += pred; |
600 | | |
601 | | /* update predictor */ |
602 | 24.1M | if (n < 4) { |
603 | 16.1M | *dc_val = level * h->c.y_dc_scale; |
604 | 16.1M | } else { |
605 | 8.03M | *dc_val = level * h->c.c_dc_scale; |
606 | 8.03M | } |
607 | 24.1M | } |
608 | | |
609 | 25.8M | return level; |
610 | 26.1M | } |
611 | | |
612 | | int ff_msmpeg4_decode_block(MSMP4DecContext *const ms, int16_t * block, |
613 | | int n, int coded, const uint8_t *scan_table) |
614 | 80.6M | { |
615 | 80.6M | H263DecContext *const h = &ms->h; |
616 | 80.6M | int level, i, last, run, run_diff; |
617 | 80.6M | int dc_pred_dir = -1; //unused but its passed around, so it needs to be initialized |
618 | 80.6M | const RLTable *rl; |
619 | 80.6M | const RL_VLC_ELEM *rl_vlc; |
620 | 80.6M | int qmul, qadd; |
621 | | |
622 | 80.6M | if (h->c.mb_intra) { |
623 | 26.1M | qmul=1; |
624 | 26.1M | qadd=0; |
625 | | |
626 | | /* DC coef */ |
627 | 26.1M | level = msmpeg4_decode_dc(ms, n, &dc_pred_dir); |
628 | | |
629 | 26.1M | if (level < 0){ |
630 | 9.90M | av_log(h->c.avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, h->c.qscale); |
631 | 9.90M | if (h->c.inter_intra_pred) |
632 | 2.43k | level = 0; |
633 | 9.90M | } |
634 | 26.1M | if (n < 4) { |
635 | 17.4M | rl = &ff_rl_table[ms->rl_table_index]; |
636 | 17.4M | if (level > 256 * h->c.y_dc_scale) { |
637 | 1.14k | av_log(h->c.avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", h->c.qscale); |
638 | 1.14k | if (!h->c.inter_intra_pred) |
639 | 1.14k | return -1; |
640 | 1.14k | } |
641 | 17.4M | } else { |
642 | 8.71M | rl = &ff_rl_table[3 + ms->rl_chroma_table_index]; |
643 | 8.71M | if (level > 256 * h->c.c_dc_scale) { |
644 | 911 | av_log(h->c.avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", h->c.qscale); |
645 | 911 | if (!h->c.inter_intra_pred) |
646 | 911 | return -1; |
647 | 911 | } |
648 | 8.71M | } |
649 | 26.1M | block[0] = level; |
650 | | |
651 | 26.1M | run_diff = h->c.msmpeg4_version >= MSMP4_WMV1; |
652 | 26.1M | i = 0; |
653 | 26.1M | if (!coded) { |
654 | 14.0M | goto not_coded; |
655 | 14.0M | } |
656 | 12.1M | if (h->c.ac_pred) { |
657 | 3.82M | if (dc_pred_dir == 0) |
658 | 2.43M | scan_table = h->permutated_intra_v_scantable; /* left */ |
659 | 1.39M | else |
660 | 1.39M | scan_table = h->permutated_intra_h_scantable; /* top */ |
661 | 8.29M | } else { |
662 | 8.29M | scan_table = h->c.intra_scantable.permutated; |
663 | 8.29M | } |
664 | 12.1M | rl_vlc= rl->rl_vlc[0]; |
665 | 54.4M | } else { |
666 | 54.4M | qmul = h->c.qscale << 1; |
667 | 54.4M | qadd = (h->c.qscale - 1) | 1; |
668 | 54.4M | i = -1; |
669 | 54.4M | rl = &ff_rl_table[3 + ms->rl_table_index]; |
670 | | |
671 | 54.4M | if (h->c.msmpeg4_version == MSMP4_V2) |
672 | 4.53M | run_diff = 0; |
673 | 49.9M | else |
674 | 49.9M | run_diff = 1; |
675 | | |
676 | 54.4M | if (!coded) { |
677 | 36.7M | h->c.block_last_index[n] = i; |
678 | 36.7M | return 0; |
679 | 36.7M | } |
680 | 17.7M | if(!scan_table) |
681 | 9.59M | scan_table = h->c.inter_scantable.permutated; |
682 | 17.7M | rl_vlc= rl->rl_vlc[h->c.qscale]; |
683 | 17.7M | } |
684 | 29.8M | { |
685 | 29.8M | 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.51M | int cache; |
691 | 1.51M | cache= GET_CACHE(re, &h->gb); |
692 | | /* escape */ |
693 | 1.51M | if (h->c.msmpeg4_version == MSMP4_V1 || (cache&0x80000000)==0) { |
694 | 1.28M | if (h->c.msmpeg4_version == MSMP4_V1 || (cache&0x40000000)==0) { |
695 | | /* third escape */ |
696 | 1.21M | if (h->c.msmpeg4_version != MSMP4_V1) |
697 | 1.21M | LAST_SKIP_BITS(re, &h->gb, 2); |
698 | 1.21M | UPDATE_CACHE(re, &h->gb); |
699 | 1.21M | if (h->c.msmpeg4_version <= MSMP4_V3) { |
700 | 124k | last = SHOW_UBITS(re, &h->gb, 1); SKIP_CACHE(re, &h->gb, 1); |
701 | 124k | run = SHOW_UBITS(re, &h->gb, 6); SKIP_CACHE(re, &h->gb, 6); |
702 | 124k | level = SHOW_SBITS(re, &h->gb, 8); |
703 | 124k | SKIP_COUNTER(re, &h->gb, 1 + 6 + 8); |
704 | 1.09M | }else{ |
705 | 1.09M | int sign; |
706 | 1.09M | last = SHOW_UBITS(re, &h->gb, 1); SKIP_BITS(re, &h->gb, 1); |
707 | 1.09M | if (!ms->esc3_level_length) { |
708 | 6.32k | int ll; |
709 | 6.32k | ff_dlog(h->c.avctx, "ESC-3 %X at %d %d\n", |
710 | 6.32k | show_bits(&h->gb, 24), h->c.mb_x, h->c.mb_y); |
711 | 6.32k | if (h->c.qscale < 8) { |
712 | 3.18k | ll = SHOW_UBITS(re, &h->gb, 3); SKIP_BITS(re, &h->gb, 3); |
713 | 3.18k | if(ll==0){ |
714 | 1.44k | ll = 8+SHOW_UBITS(re, &h->gb, 1); SKIP_BITS(re, &h->gb, 1); |
715 | 1.44k | } |
716 | 3.18k | }else{ |
717 | 3.14k | ll=2; |
718 | 15.4k | while (ll < 8 && SHOW_UBITS(re, &h->gb, 1) == 0) { |
719 | 12.3k | ll++; |
720 | 12.3k | SKIP_BITS(re, &h->gb, 1); |
721 | 12.3k | } |
722 | 3.14k | if (ll<8) SKIP_BITS(re, &h->gb, 1); |
723 | 3.14k | } |
724 | | |
725 | 6.32k | ms->esc3_level_length = ll; |
726 | 6.32k | ms->esc3_run_length = SHOW_UBITS(re, &h->gb, 2) + 3; SKIP_BITS(re, &h->gb, 2); |
727 | 6.32k | UPDATE_CACHE(re, &h->gb); |
728 | 6.32k | } |
729 | 1.09M | run = SHOW_UBITS(re, &h->gb, ms->esc3_run_length); |
730 | 1.09M | SKIP_BITS(re, &h->gb, ms->esc3_run_length); |
731 | | |
732 | 1.09M | sign= SHOW_UBITS(re, &h->gb, 1); |
733 | 1.09M | SKIP_BITS(re, &h->gb, 1); |
734 | | |
735 | 1.09M | level = SHOW_UBITS(re, &h->gb, ms->esc3_level_length); |
736 | 1.09M | SKIP_BITS(re, &h->gb, ms->esc3_level_length); |
737 | 1.09M | if(sign) level= -level; |
738 | 1.09M | } |
739 | | |
740 | | //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ; |
741 | 1.21M | if (level>0) level= level * qmul + qadd; |
742 | 1.05M | else level= level * qmul - qadd; |
743 | 1.21M | i+= run + 1; |
744 | 1.21M | if(last) i+=192; |
745 | 1.21M | } else { |
746 | | /* second escape */ |
747 | 64.2k | SKIP_BITS(re, &h->gb, 2); |
748 | 64.2k | GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1); |
749 | 64.2k | i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing |
750 | 64.2k | level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1); |
751 | 64.2k | LAST_SKIP_BITS(re, &h->gb, 1); |
752 | 64.2k | } |
753 | 1.28M | } else { |
754 | | /* first escape */ |
755 | 239k | SKIP_BITS(re, &h->gb, 1); |
756 | 239k | GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1); |
757 | 239k | i+= run; |
758 | 239k | level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing |
759 | 239k | level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1); |
760 | 239k | LAST_SKIP_BITS(re, &h->gb, 1); |
761 | 239k | } |
762 | 114M | } else { |
763 | 114M | i+= run; |
764 | 114M | level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1); |
765 | 114M | LAST_SKIP_BITS(re, &h->gb, 1); |
766 | 114M | } |
767 | 116M | if (i > 62){ |
768 | 29.8M | i-= 192; |
769 | 29.8M | if(i&(~63)){ |
770 | 5.20M | const int left = get_bits_left(&h->gb); |
771 | 5.20M | if (((i + 192 == 64 && level / qmul == -1) || |
772 | 5.14M | !(h->c.avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))) && |
773 | 5.18M | left >= 0) { |
774 | 5.16M | av_log(h->c.avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", |
775 | 5.16M | h->c.mb_x, h->c.mb_y); |
776 | 5.16M | i = 63; |
777 | 5.16M | break; |
778 | 5.16M | }else{ |
779 | 37.3k | av_log(h->c.avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", |
780 | 37.3k | h->c.mb_x, h->c.mb_y); |
781 | 37.3k | return -1; |
782 | 37.3k | } |
783 | 5.20M | } |
784 | | |
785 | 24.6M | block[scan_table[i]] = level; |
786 | 24.6M | break; |
787 | 29.8M | } |
788 | | |
789 | 86.3M | block[scan_table[i]] = level; |
790 | 86.3M | } |
791 | 29.8M | CLOSE_READER(re, &h->gb); |
792 | 29.8M | } |
793 | 29.8M | if (h->c.mb_intra) { |
794 | 26.1M | not_coded: |
795 | 26.1M | ff_mpeg4_pred_ac(h, block, n, dc_pred_dir); |
796 | 26.1M | } |
797 | 43.9M | h->c.block_last_index[n] = i; |
798 | | |
799 | 43.9M | return 0; |
800 | 29.8M | } |
801 | | |
802 | | void ff_msmpeg4_decode_motion(MSMP4DecContext *const ms, int *mx_ptr, int *my_ptr) |
803 | 11.1M | { |
804 | 11.1M | const VLCElem *const mv_vlc = mv_tables[ms->mv_table_index]; |
805 | 11.1M | H263DecContext *const h = &ms->h; |
806 | 11.1M | int sym, mx, my; |
807 | | |
808 | 11.1M | sym = get_vlc2(&h->gb, mv_vlc, MV_VLC_BITS, 2); |
809 | 11.1M | if (sym) { |
810 | 9.73M | mx = sym >> 8; |
811 | 9.73M | my = sym & 0xFF; |
812 | 9.73M | } else { |
813 | | /* Escape */ |
814 | 1.43M | mx = get_bits(&h->gb, 6); |
815 | 1.43M | my = get_bits(&h->gb, 6); |
816 | 1.43M | } |
817 | | |
818 | 11.1M | mx += *mx_ptr - 32; |
819 | 11.1M | my += *my_ptr - 32; |
820 | | /* WARNING : they do not do exactly modulo encoding */ |
821 | 11.1M | if (mx <= -64) |
822 | 301k | mx += 64; |
823 | 10.8M | else if (mx >= 64) |
824 | 8.83k | mx -= 64; |
825 | | |
826 | 11.1M | if (my <= -64) |
827 | 279k | my += 64; |
828 | 10.8M | else if (my >= 64) |
829 | 30.1k | my -= 64; |
830 | 11.1M | *mx_ptr = mx; |
831 | 11.1M | *my_ptr = my; |
832 | 11.1M | } |
833 | | |
834 | | av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx) |
835 | 11.5k | { |
836 | 11.5k | static AVOnce init_static_once = AV_ONCE_INIT; |
837 | 11.5k | H263DecContext *const h = avctx->priv_data; |
838 | 11.5k | int ret; |
839 | | |
840 | 11.5k | ret = av_image_check_size(avctx->width, avctx->height, 0, avctx); |
841 | 11.5k | if (ret < 0) |
842 | 592 | return ret; |
843 | | |
844 | 10.9k | if (ff_h263_decode_init(avctx) < 0) |
845 | 0 | return -1; |
846 | | |
847 | | // We unquantize inter blocks as we parse them. |
848 | 10.9k | h->c.dct_unquantize_inter = NULL; |
849 | | |
850 | 10.9k | h->decode_header = msmpeg4_decode_picture_header; |
851 | | |
852 | 10.9k | ff_msmpeg4_common_init(&h->c, h->permutated_intra_h_scantable, |
853 | 10.9k | h->permutated_intra_v_scantable); |
854 | | |
855 | 10.9k | switch (h->c.msmpeg4_version) { |
856 | 1.67k | case MSMP4_V1: |
857 | 3.86k | case MSMP4_V2: |
858 | 3.86k | h->decode_mb = msmpeg4v12_decode_mb; |
859 | 3.86k | break; |
860 | 2.19k | case MSMP4_V3: |
861 | 4.48k | case MSMP4_WMV1: |
862 | 4.48k | h->decode_mb = msmpeg4v34_decode_mb; |
863 | 4.48k | break; |
864 | 2.58k | case MSMP4_WMV2: |
865 | 2.58k | break; |
866 | 0 | default: |
867 | 0 | av_unreachable("List contains all cases using ff_msmpeg4_decode_init()"); |
868 | 10.9k | } |
869 | | |
870 | 10.9k | h->slice_height = h->c.mb_height; //to avoid 1/0 if the first frame is not a keyframe |
871 | | |
872 | 10.9k | ff_thread_once(&init_static_once, msmpeg4_decode_init_static); |
873 | | |
874 | 10.9k | return 0; |
875 | 10.9k | } |
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 | | }; |