/src/ffmpeg/libavcodec/wmv2dec.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2002 The FFmpeg Project |
3 | | * |
4 | | * This file is part of FFmpeg. |
5 | | * |
6 | | * FFmpeg is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * FFmpeg is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with FFmpeg; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | | */ |
20 | | |
21 | | #include "libavutil/mem_internal.h" |
22 | | |
23 | | #include "avcodec.h" |
24 | | #include "codec_internal.h" |
25 | | #include "h263dec.h" |
26 | | #include "intrax8.h" |
27 | | #include "mathops.h" |
28 | | #include "mpegutils.h" |
29 | | #include "mpegvideo.h" |
30 | | #include "mpegvideodec.h" |
31 | | #include "msmpeg4.h" |
32 | | #include "msmpeg4_vc1_data.h" |
33 | | #include "msmpeg4dec.h" |
34 | | #include "simple_idct.h" |
35 | | #include "wmv2.h" |
36 | | #include "wmv2data.h" |
37 | | #include "wmv2dec.h" |
38 | | |
39 | | typedef struct WMV2DecContext { |
40 | | MSMP4DecContext ms; |
41 | | WMV2Context common; |
42 | | IntraX8Context x8; |
43 | | int j_type_bit; |
44 | | int j_type; |
45 | | int abt_flag; |
46 | | int abt_type; |
47 | | int abt_type_table[6]; |
48 | | int per_mb_abt; |
49 | | int per_block_abt; |
50 | | int mspel_bit; |
51 | | int cbp_table_index; |
52 | | int top_left_mv_flag; |
53 | | int per_mb_rl_bit; |
54 | | int skip_type; |
55 | | |
56 | | DECLARE_ALIGNED(32, int16_t, abt_block2)[6][64]; |
57 | | } WMV2DecContext; |
58 | | |
59 | | static void wmv2_add_block(WMV2DecContext *w, int16_t blocks1[][64], |
60 | | uint8_t *dst, int stride, int n) |
61 | 378M | { |
62 | 378M | H263DecContext *const h = &w->ms.h; |
63 | | |
64 | 378M | if (h->c.block_last_index[n] >= 0) { |
65 | 8.35M | int16_t *block1 = blocks1[n]; |
66 | 8.35M | switch (w->abt_type_table[n]) { |
67 | 6.50M | case 0: |
68 | 6.50M | w->common.wdsp.idct_add(dst, stride, block1); |
69 | 6.50M | break; |
70 | 298k | case 1: |
71 | 298k | ff_simple_idct84_add(dst, stride, block1); |
72 | 298k | ff_simple_idct84_add(dst + 4 * stride, stride, w->abt_block2[n]); |
73 | 298k | h->c.bdsp.clear_block(w->abt_block2[n]); |
74 | 298k | break; |
75 | 1.54M | case 2: |
76 | 1.54M | ff_simple_idct48_add(dst, stride, block1); |
77 | 1.54M | ff_simple_idct48_add(dst + 4, stride, w->abt_block2[n]); |
78 | 1.54M | h->c.bdsp.clear_block(w->abt_block2[n]); |
79 | 1.54M | break; |
80 | 0 | default: |
81 | 0 | av_log(h->c.avctx, AV_LOG_ERROR, "internal error in WMV2 abt\n"); |
82 | 8.35M | } |
83 | 8.35M | } |
84 | 378M | } |
85 | | |
86 | | void ff_wmv2_add_mb(MpegEncContext *s, int16_t block1[6][64], |
87 | | uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr) |
88 | 63.1M | { |
89 | 63.1M | WMV2DecContext *const w = (WMV2DecContext *) s; |
90 | | |
91 | 63.1M | wmv2_add_block(w, block1, dest_y, s->linesize, 0); |
92 | 63.1M | wmv2_add_block(w, block1, dest_y + 8, s->linesize, 1); |
93 | 63.1M | wmv2_add_block(w, block1, dest_y + 8 * s->linesize, s->linesize, 2); |
94 | 63.1M | wmv2_add_block(w, block1, dest_y + 8 + 8 * s->linesize, s->linesize, 3); |
95 | | |
96 | 63.1M | if (s->avctx->flags & AV_CODEC_FLAG_GRAY) |
97 | 0 | return; |
98 | | |
99 | 63.1M | wmv2_add_block(w, block1, dest_cb, s->uvlinesize, 4); |
100 | 63.1M | wmv2_add_block(w, block1, dest_cr, s->uvlinesize, 5); |
101 | 63.1M | } |
102 | | |
103 | | static int parse_mb_skip(WMV2DecContext *w) |
104 | 58.7k | { |
105 | 58.7k | H263DecContext *const h = &w->ms.h; |
106 | 58.7k | int coded_mb_count = 0; |
107 | 58.7k | uint32_t *const mb_type = h->c.cur_pic.mb_type; |
108 | | |
109 | 58.7k | w->skip_type = get_bits(&h->gb, 2); |
110 | 58.7k | switch (w->skip_type) { |
111 | 9.73k | case SKIP_TYPE_NONE: |
112 | 839k | for (int mb_y = 0; mb_y < h->c.mb_height; mb_y++) |
113 | 11.4M | for (int mb_x = 0; mb_x < h->c.mb_width; mb_x++) |
114 | 10.6M | mb_type[mb_y * h->c.mb_stride + mb_x] = |
115 | 10.6M | MB_TYPE_16x16 | MB_TYPE_FORWARD_MV; |
116 | 9.73k | break; |
117 | 10.6k | case SKIP_TYPE_MPEG: |
118 | 10.6k | if (get_bits_left(&h->gb) < h->c.mb_height * h->c.mb_width) |
119 | 4.38k | return AVERROR_INVALIDDATA; |
120 | 163k | for (int mb_y = 0; mb_y < h->c.mb_height; mb_y++) |
121 | 1.83M | for (int mb_x = 0; mb_x < h->c.mb_width; mb_x++) |
122 | 1.67M | mb_type[mb_y * h->c.mb_stride + mb_x] = |
123 | 1.67M | (get_bits1(&h->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_FORWARD_MV; |
124 | 6.23k | break; |
125 | 30.8k | case SKIP_TYPE_ROW: |
126 | 323k | for (int mb_y = 0; mb_y < h->c.mb_height; mb_y++) { |
127 | 308k | if (get_bits_left(&h->gb) < 1) |
128 | 16.0k | return AVERROR_INVALIDDATA; |
129 | 292k | if (get_bits1(&h->gb)) { |
130 | 2.02M | for (int mb_x = 0; mb_x < h->c.mb_width; mb_x++) |
131 | 1.84M | mb_type[mb_y * h->c.mb_stride + mb_x] = |
132 | 1.84M | MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_FORWARD_MV; |
133 | 179k | } else { |
134 | 2.60M | for (int mb_x = 0; mb_x < h->c.mb_width; mb_x++) |
135 | 2.49M | mb_type[mb_y * h->c.mb_stride + mb_x] = |
136 | 2.49M | (get_bits1(&h->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_FORWARD_MV; |
137 | 112k | } |
138 | 292k | } |
139 | 14.7k | break; |
140 | 14.7k | case SKIP_TYPE_COL: |
141 | 476k | for (int mb_x = 0; mb_x < h->c.mb_width; mb_x++) { |
142 | 473k | if (get_bits_left(&h->gb) < 1) |
143 | 4.29k | return AVERROR_INVALIDDATA; |
144 | 468k | if (get_bits1(&h->gb)) { |
145 | 4.76M | for (int mb_y = 0; mb_y < h->c.mb_height; mb_y++) |
146 | 4.51M | mb_type[mb_y * h->c.mb_stride + mb_x] = |
147 | 4.51M | MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_FORWARD_MV; |
148 | 255k | } else { |
149 | 4.17M | for (int mb_y = 0; mb_y < h->c.mb_height; mb_y++) |
150 | 3.96M | mb_type[mb_y * h->c.mb_stride + mb_x] = |
151 | 3.96M | (get_bits1(&h->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_FORWARD_MV; |
152 | 213k | } |
153 | 468k | } |
154 | 3.20k | break; |
155 | 58.7k | } |
156 | | |
157 | 1.92M | for (int mb_y = 0; mb_y < h->c.mb_height; mb_y++) |
158 | 25.6M | for (int mb_x = 0; mb_x < h->c.mb_width; mb_x++) |
159 | 23.7M | coded_mb_count += !IS_SKIP(mb_type[mb_y * h->c.mb_stride + mb_x]); |
160 | | |
161 | 33.9k | if (coded_mb_count > get_bits_left(&h->gb)) |
162 | 4.01k | return AVERROR_INVALIDDATA; |
163 | | |
164 | 29.9k | return 0; |
165 | 33.9k | } |
166 | | |
167 | | static int decode_ext_header(WMV2DecContext *w) |
168 | 2.91k | { |
169 | 2.91k | H263DecContext *const h = &w->ms.h; |
170 | 2.91k | GetBitContext gb; |
171 | 2.91k | int fps; |
172 | 2.91k | int code; |
173 | | |
174 | 2.91k | if (h->c.avctx->extradata_size < 4) |
175 | 1.30k | return AVERROR_INVALIDDATA; |
176 | | |
177 | 1.61k | init_get_bits(&gb, h->c.avctx->extradata, 32); |
178 | | |
179 | 1.61k | fps = get_bits(&gb, 5); |
180 | 1.61k | w->ms.bit_rate = get_bits(&gb, 11) * 1024; |
181 | 1.61k | w->mspel_bit = get_bits1(&gb); |
182 | 1.61k | h->loop_filter = get_bits1(&gb); |
183 | 1.61k | w->abt_flag = get_bits1(&gb); |
184 | 1.61k | w->j_type_bit = get_bits1(&gb); |
185 | 1.61k | w->top_left_mv_flag = get_bits1(&gb); |
186 | 1.61k | w->per_mb_rl_bit = get_bits1(&gb); |
187 | 1.61k | code = get_bits(&gb, 3); |
188 | | |
189 | 1.61k | if (code == 0) |
190 | 249 | return AVERROR_INVALIDDATA; |
191 | | |
192 | 1.37k | h->slice_height = h->c.mb_height / code; |
193 | | |
194 | 1.37k | if (h->c.avctx->debug & FF_DEBUG_PICT_INFO) |
195 | 0 | av_log(h->c.avctx, AV_LOG_DEBUG, |
196 | 0 | "fps:%d, br:%d, qpbit:%d, abt_flag:%d, j_type_bit:%d, " |
197 | 0 | "tl_mv_flag:%d, mbrl_bit:%d, code:%d, loop_filter:%d, " |
198 | 0 | "slices:%d\n", |
199 | 0 | fps, w->ms.bit_rate, w->mspel_bit, w->abt_flag, w->j_type_bit, |
200 | 0 | w->top_left_mv_flag, w->per_mb_rl_bit, code, h->loop_filter, |
201 | 0 | code); |
202 | 1.37k | return 0; |
203 | 1.61k | } |
204 | | |
205 | | static int wmv2_decode_picture_header(H263DecContext *const h) |
206 | 241k | { |
207 | 241k | int code; |
208 | | |
209 | 241k | h->c.pict_type = get_bits1(&h->gb) + 1; |
210 | 241k | if (h->c.pict_type == AV_PICTURE_TYPE_I) { |
211 | 176k | code = get_bits(&h->gb, 7); |
212 | 176k | av_log(h->c.avctx, AV_LOG_DEBUG, "I7:%X/\n", code); |
213 | 176k | } |
214 | 241k | h->c.chroma_qscale = h->c.qscale = get_bits(&h->gb, 5); |
215 | 241k | if (h->c.qscale <= 0) |
216 | 56.3k | return AVERROR_INVALIDDATA; |
217 | | |
218 | 185k | if (h->c.pict_type != AV_PICTURE_TYPE_I && show_bits(&h->gb, 1)) { |
219 | 41.8k | GetBitContext gb = h->gb; |
220 | 41.8k | int skip_type = get_bits(&gb, 2); |
221 | 41.8k | int run = skip_type == SKIP_TYPE_COL ? h->c.mb_width : h->c.mb_height; |
222 | | |
223 | 43.4k | while (run > 0) { |
224 | 42.0k | int block = FFMIN(run, 25); |
225 | 42.0k | if (get_bits(&gb, block) + 1 != 1<<block) |
226 | 40.4k | break; |
227 | 1.59k | run -= block; |
228 | 1.59k | } |
229 | 41.8k | if (!run) |
230 | 1.37k | return FRAME_SKIPPED; |
231 | 41.8k | } |
232 | | |
233 | 183k | return 0; |
234 | 185k | } |
235 | | |
236 | | int ff_wmv2_decode_secondary_picture_header(H263DecContext *const h) |
237 | 170k | { |
238 | 170k | WMV2DecContext *const w = (WMV2DecContext *)h; |
239 | | |
240 | 170k | if (h->c.pict_type == AV_PICTURE_TYPE_I) { |
241 | | /* Is filling with zeroes really the right thing to do? */ |
242 | 112k | memset(h->c.cur_pic.mb_type, 0, |
243 | 112k | sizeof(*h->c.cur_pic.mb_type) * h->c.mb_height * h->c.mb_stride); |
244 | 112k | if (w->j_type_bit) |
245 | 52.8k | w->j_type = get_bits1(&h->gb); |
246 | 59.2k | else |
247 | 59.2k | w->j_type = 0; // FIXME check |
248 | | |
249 | 112k | if (!w->j_type) { |
250 | 71.5k | if (w->per_mb_rl_bit) |
251 | 17.4k | w->ms.per_mb_rl_table = get_bits1(&h->gb); |
252 | 54.0k | else |
253 | 54.0k | w->ms.per_mb_rl_table = 0; |
254 | | |
255 | 71.5k | if (!w->ms.per_mb_rl_table) { |
256 | 61.6k | w->ms.rl_chroma_table_index = decode012(&h->gb); |
257 | 61.6k | w->ms.rl_table_index = decode012(&h->gb); |
258 | 61.6k | } |
259 | | |
260 | 71.5k | w->ms.dc_table_index = get_bits1(&h->gb); |
261 | | |
262 | | // at minimum one bit per macroblock is required at least in a valid frame, |
263 | | // we discard frames much smaller than this. Frames smaller than 1/8 of the |
264 | | // smallest "black/skip" frame generally contain not much recoverable content |
265 | | // while at the same time they have the highest computational requirements |
266 | | // per byte |
267 | 71.5k | if (get_bits_left(&h->gb) * 8LL < (h->c.width+15)/16 * ((h->c.height+15)/16)) |
268 | 30.3k | return AVERROR_INVALIDDATA; |
269 | 71.5k | } |
270 | 81.7k | h->c.inter_intra_pred = 0; |
271 | 81.7k | h->c.no_rounding = 1; |
272 | 81.7k | if (h->c.avctx->debug & FF_DEBUG_PICT_INFO) { |
273 | 0 | av_log(h->c.avctx, AV_LOG_DEBUG, |
274 | 0 | "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d j_type:%d \n", |
275 | 0 | h->c.qscale, w->ms.rl_chroma_table_index, w->ms.rl_table_index, |
276 | 0 | w->ms.dc_table_index, w->ms.per_mb_rl_table, w->j_type); |
277 | 0 | } |
278 | 81.7k | } else { |
279 | 58.7k | int cbp_index; |
280 | 58.7k | int ret; |
281 | 58.7k | w->j_type = 0; |
282 | | |
283 | 58.7k | ret = parse_mb_skip(w); |
284 | 58.7k | if (ret < 0) |
285 | 28.7k | return ret; |
286 | 29.9k | cbp_index = decode012(&h->gb); |
287 | 29.9k | w->cbp_table_index = wmv2_get_cbp_table_index(h->c.qscale, cbp_index); |
288 | | |
289 | 29.9k | if (w->mspel_bit) |
290 | 7.17k | h->c.mspel = get_bits1(&h->gb); |
291 | 22.7k | else |
292 | 22.7k | h->c.mspel = 0; // FIXME check |
293 | | |
294 | 29.9k | if (w->abt_flag) { |
295 | 8.73k | w->per_mb_abt = get_bits1(&h->gb) ^ 1; |
296 | 8.73k | if (!w->per_mb_abt) |
297 | 4.69k | w->abt_type = decode012(&h->gb); |
298 | 8.73k | } |
299 | | |
300 | 29.9k | if (w->per_mb_rl_bit) |
301 | 8.62k | w->ms.per_mb_rl_table = get_bits1(&h->gb); |
302 | 21.3k | else |
303 | 21.3k | w->ms.per_mb_rl_table = 0; |
304 | | |
305 | 29.9k | if (!w->ms.per_mb_rl_table) { |
306 | 25.2k | w->ms.rl_table_index = decode012(&h->gb); |
307 | 25.2k | w->ms.rl_chroma_table_index = w->ms.rl_table_index; |
308 | 25.2k | } |
309 | | |
310 | 29.9k | if (get_bits_left(&h->gb) < 2) |
311 | 1.18k | return AVERROR_INVALIDDATA; |
312 | | |
313 | 28.7k | w->ms.dc_table_index = get_bits1(&h->gb); |
314 | 28.7k | w->ms.mv_table_index = get_bits1(&h->gb); |
315 | | |
316 | 28.7k | h->c.inter_intra_pred = 0; // (h->c.width * h->c.height < 320 * 240 && w->ms.bit_rate <= II_BITRATE); |
317 | 28.7k | h->c.no_rounding ^= 1; |
318 | | |
319 | 28.7k | if (h->c.avctx->debug & FF_DEBUG_PICT_INFO) { |
320 | 0 | av_log(h->c.avctx, AV_LOG_DEBUG, |
321 | 0 | "rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d mspel:%d " |
322 | 0 | "per_mb_abt:%d abt_type:%d cbp:%d ii:%d\n", |
323 | 0 | w->ms.rl_table_index, w->ms.rl_chroma_table_index, |
324 | 0 | w->ms.dc_table_index, w->ms.mv_table_index, |
325 | 0 | w->ms.per_mb_rl_table, h->c.qscale, h->c.mspel, |
326 | 0 | w->per_mb_abt, w->abt_type, w->cbp_table_index, |
327 | 0 | h->c.inter_intra_pred); |
328 | 0 | } |
329 | 28.7k | } |
330 | 110k | w->ms.esc3_level_length = 0; |
331 | 110k | w->ms.esc3_run_length = 0; |
332 | | |
333 | 110k | if (w->j_type) { |
334 | 40.5k | ff_intrax8_decode_picture(&w->x8, h->c.cur_pic.ptr, |
335 | 40.5k | &h->gb, &h->c.mb_x, &h->c.mb_y, |
336 | 40.5k | 2 * h->c.qscale, (h->c.qscale - 1) | 1, |
337 | 40.5k | h->loop_filter, h->c.low_delay); |
338 | | |
339 | 40.5k | ff_er_add_slice(&h->c.er, 0, 0, |
340 | 40.5k | (h->c.mb_x >> 1) - 1, (h->c.mb_y >> 1) - 1, |
341 | 40.5k | ER_MB_END); |
342 | 40.5k | return 1; |
343 | 40.5k | } |
344 | | |
345 | 70.0k | return 0; |
346 | 110k | } |
347 | | |
348 | | static inline void wmv2_decode_motion(WMV2DecContext *w, int *mx_ptr, int *my_ptr) |
349 | 4.95M | { |
350 | 4.95M | H263DecContext *const h = &w->ms.h; |
351 | | |
352 | 4.95M | ff_msmpeg4_decode_motion(&w->ms, mx_ptr, my_ptr); |
353 | | |
354 | 4.95M | if ((((*mx_ptr) | (*my_ptr)) & 1) && h->c.mspel) |
355 | 300k | w->common.hshift = get_bits1(&h->gb); |
356 | 4.65M | else |
357 | 4.65M | w->common.hshift = 0; |
358 | 4.95M | } |
359 | | |
360 | | static int16_t *wmv2_pred_motion(WMV2DecContext *w, int *px, int *py) |
361 | 4.95M | { |
362 | 4.95M | H263DecContext *const h = &w->ms.h; |
363 | 4.95M | int diff, type; |
364 | | |
365 | 4.95M | int wrap = h->c.b8_stride; |
366 | 4.95M | int xy = h->c.block_index[0]; |
367 | | |
368 | 4.95M | int16_t *mot_val = h->c.cur_pic.motion_val[0][xy]; |
369 | | |
370 | 4.95M | const int16_t *A = h->c.cur_pic.motion_val[0][xy - 1]; |
371 | 4.95M | const int16_t *B = h->c.cur_pic.motion_val[0][xy - wrap]; |
372 | 4.95M | const int16_t *C = h->c.cur_pic.motion_val[0][xy + 2 - wrap]; |
373 | | |
374 | 4.95M | if (h->c.mb_x && !h->c.first_slice_line && !h->c.mspel && w->top_left_mv_flag) |
375 | 621k | diff = FFMAX(FFABS(A[0] - B[0]), FFABS(A[1] - B[1])); |
376 | 4.33M | else |
377 | 4.33M | diff = 0; |
378 | | |
379 | 4.95M | if (diff >= 8) |
380 | 270k | type = get_bits1(&h->gb); |
381 | 4.68M | else |
382 | 4.68M | type = 2; |
383 | | |
384 | 4.95M | if (type == 0) { |
385 | 189k | *px = A[0]; |
386 | 189k | *py = A[1]; |
387 | 4.76M | } else if (type == 1) { |
388 | 81.4k | *px = B[0]; |
389 | 81.4k | *py = B[1]; |
390 | 4.68M | } else { |
391 | | /* special case for first (slice) line */ |
392 | 4.68M | if (h->c.first_slice_line) { |
393 | 586k | *px = A[0]; |
394 | 586k | *py = A[1]; |
395 | 4.09M | } else { |
396 | 4.09M | *px = mid_pred(A[0], B[0], C[0]); |
397 | 4.09M | *py = mid_pred(A[1], B[1], C[1]); |
398 | 4.09M | } |
399 | 4.68M | } |
400 | | |
401 | 4.95M | return mot_val; |
402 | 4.95M | } |
403 | | |
404 | | static inline int wmv2_decode_inter_block(WMV2DecContext *w, int16_t *block, |
405 | | int n, int cbp) |
406 | 29.7M | { |
407 | 29.7M | H263DecContext *const h = &w->ms.h; |
408 | 29.7M | static const int sub_cbp_table[3] = { 2, 3, 1 }; |
409 | 29.7M | int sub_cbp, ret; |
410 | | |
411 | 29.7M | if (!cbp) { |
412 | 21.3M | h->c.block_last_index[n] = -1; |
413 | 21.3M | return 0; |
414 | 21.3M | } |
415 | | |
416 | 8.36M | if (w->per_block_abt) |
417 | 515k | w->abt_type = decode012(&h->gb); |
418 | 8.36M | w->abt_type_table[n] = w->abt_type; |
419 | | |
420 | 8.36M | if (w->abt_type) { |
421 | 1.84M | const uint8_t *scantable = w->abt_type == 1 ? ff_wmv2_scantableA : ff_wmv2_scantableB; |
422 | | |
423 | 1.84M | sub_cbp = sub_cbp_table[decode012(&h->gb)]; |
424 | | |
425 | 1.84M | if (sub_cbp & 1) { |
426 | 763k | ret = ff_msmpeg4_decode_block(&w->ms, block, n, 1, scantable); |
427 | 763k | if (ret < 0) |
428 | 665 | return ret; |
429 | 763k | } |
430 | | |
431 | 1.84M | if (sub_cbp & 2) { |
432 | 1.43M | ret = ff_msmpeg4_decode_block(&w->ms, w->abt_block2[n], n, 1, scantable); |
433 | 1.43M | if (ret < 0) |
434 | 1.15k | return ret; |
435 | 1.43M | } |
436 | | |
437 | 1.84M | h->c.block_last_index[n] = 63; |
438 | | |
439 | 1.84M | return 0; |
440 | 6.51M | } else { |
441 | 6.51M | return ff_msmpeg4_decode_block(&w->ms, block, n, 1, |
442 | 6.51M | h->c.inter_scantable.permutated); |
443 | 6.51M | } |
444 | 8.36M | } |
445 | | |
446 | | static int wmv2_decode_mb(H263DecContext *const h) |
447 | 9.86M | { |
448 | | /* The following is only allowed because this decoder |
449 | | * does not use slice threading. */ |
450 | 9.86M | WMV2DecContext *const w = (WMV2DecContext *) h; |
451 | 9.86M | MSMP4DecContext *const ms = &w->ms; |
452 | 9.86M | int cbp, code, i, ret; |
453 | 9.86M | uint8_t *coded_val; |
454 | | |
455 | 9.86M | if (w->j_type) |
456 | 0 | return 0; |
457 | | |
458 | 9.86M | if (h->c.pict_type == AV_PICTURE_TYPE_P) { |
459 | 8.28M | if (IS_SKIP(h->c.cur_pic.mb_type[h->c.mb_y * h->c.mb_stride + h->c.mb_x])) { |
460 | | /* skip mb */ |
461 | 3.29M | h->c.mb_intra = 0; |
462 | 23.0M | for (i = 0; i < 6; i++) |
463 | 19.7M | h->c.block_last_index[i] = -1; |
464 | 3.29M | h->c.mv_dir = MV_DIR_FORWARD; |
465 | 3.29M | h->c.mv_type = MV_TYPE_16X16; |
466 | 3.29M | h->c.mv[0][0][0] = 0; |
467 | 3.29M | h->c.mv[0][0][1] = 0; |
468 | 3.29M | h->c.mb_skipped = 1; |
469 | 3.29M | w->common.hshift = 0; |
470 | 3.29M | return 0; |
471 | 3.29M | } |
472 | 4.98M | if (get_bits_left(&h->gb) <= 0) |
473 | 6.95k | return AVERROR_INVALIDDATA; |
474 | | |
475 | 4.98M | code = get_vlc2(&h->gb, ff_mb_non_intra_vlc[w->cbp_table_index], |
476 | 4.98M | MB_NON_INTRA_VLC_BITS, 3); |
477 | 4.98M | h->c.mb_intra = (~code & 0x40) >> 6; |
478 | | |
479 | 4.98M | cbp = code & 0x3f; |
480 | 4.98M | } else { |
481 | 1.57M | h->c.mb_intra = 1; |
482 | 1.57M | if (get_bits_left(&h->gb) <= 0) |
483 | 12.5k | return AVERROR_INVALIDDATA; |
484 | 1.56M | code = get_vlc2(&h->gb, ff_msmp4_mb_i_vlc, |
485 | 1.56M | MSMP4_MB_INTRA_VLC_BITS, 2); |
486 | | /* predict coded block pattern */ |
487 | 1.56M | cbp = 0; |
488 | 10.9M | for (i = 0; i < 6; i++) { |
489 | 9.37M | int val = ((code >> (5 - i)) & 1); |
490 | 9.37M | if (i < 4) { |
491 | 6.25M | int pred = ff_msmpeg4_coded_block_pred(&h->c, i, &coded_val); |
492 | 6.25M | val = val ^ pred; |
493 | 6.25M | *coded_val = val; |
494 | 6.25M | } |
495 | 9.37M | cbp |= val << (5 - i); |
496 | 9.37M | } |
497 | 1.56M | } |
498 | | |
499 | 6.54M | if (!h->c.mb_intra) { |
500 | 4.95M | int mx, my; |
501 | 4.95M | wmv2_pred_motion(w, &mx, &my); |
502 | | |
503 | 4.95M | if (cbp) { |
504 | 3.49M | h->c.bdsp.clear_blocks(h->block[0]); |
505 | 3.49M | if (ms->per_mb_rl_table) { |
506 | 730k | ms->rl_table_index = decode012(&h->gb); |
507 | 730k | ms->rl_chroma_table_index = ms->rl_table_index; |
508 | 730k | } |
509 | | |
510 | 3.49M | if (w->abt_flag && w->per_mb_abt) { |
511 | 655k | w->per_block_abt = get_bits1(&h->gb); |
512 | 655k | if (!w->per_block_abt) |
513 | 425k | w->abt_type = decode012(&h->gb); |
514 | 655k | } else |
515 | 2.83M | w->per_block_abt = 0; |
516 | 3.49M | } |
517 | | |
518 | 4.95M | wmv2_decode_motion(w, &mx, &my); |
519 | | |
520 | 4.95M | h->c.mv_dir = MV_DIR_FORWARD; |
521 | 4.95M | h->c.mv_type = MV_TYPE_16X16; |
522 | 4.95M | h->c.mv[0][0][0] = mx; |
523 | 4.95M | h->c.mv[0][0][1] = my; |
524 | | |
525 | 34.6M | for (i = 0; i < 6; i++) { |
526 | 29.7M | if ((ret = wmv2_decode_inter_block(w, h->block[i], i, (cbp >> (5 - i)) & 1)) < 0) { |
527 | 5.70k | av_log(h->c.avctx, AV_LOG_ERROR, |
528 | 5.70k | "\nerror while decoding inter block: %d x %d (%d)\n", |
529 | 5.70k | h->c.mb_x, h->c.mb_y, i); |
530 | 5.70k | return ret; |
531 | 5.70k | } |
532 | 29.7M | } |
533 | 4.95M | } else { |
534 | 1.58M | if (h->c.pict_type == AV_PICTURE_TYPE_P) |
535 | 26.8k | ff_dlog(h->c.avctx, "%d%d ", h->c.inter_intra_pred, cbp); |
536 | 1.58M | ff_dlog(h->c.avctx, "I at %d %d %d %06X\n", h->c.mb_x, h->c.mb_y, |
537 | 1.58M | ((cbp & 3) ? 1 : 0) + ((cbp & 0x3C) ? 2 : 0), |
538 | 1.58M | show_bits(&h->gb, 24)); |
539 | 1.58M | h->c.ac_pred = get_bits1(&h->gb); |
540 | 1.58M | if (h->c.inter_intra_pred) { |
541 | 0 | h->c.h263_aic_dir = get_vlc2(&h->gb, ff_inter_intra_vlc, |
542 | 0 | INTER_INTRA_VLC_BITS, 1); |
543 | 0 | ff_dlog(h->c.avctx, "%d%d %d %d/", |
544 | 0 | h->c.ac_pred, h->c.h263_aic_dir, h->c.mb_x, h->c.mb_y); |
545 | 0 | } |
546 | 1.58M | if (ms->per_mb_rl_table && cbp) { |
547 | 982k | ms->rl_table_index = decode012(&h->gb); |
548 | 982k | ms->rl_chroma_table_index = ms->rl_table_index; |
549 | 982k | } |
550 | | |
551 | 1.58M | h->c.bdsp.clear_blocks(h->block[0]); |
552 | 11.0M | for (i = 0; i < 6; i++) { |
553 | 9.49M | ret = ff_msmpeg4_decode_block(ms, h->block[i], i, (cbp >> (5 - i)) & 1, NULL); |
554 | 9.49M | if (ret < 0) { |
555 | 14.3k | av_log(h->c.avctx, AV_LOG_ERROR, |
556 | 14.3k | "\nerror while decoding intra block: %d x %d (%d)\n", |
557 | 14.3k | h->c.mb_x, h->c.mb_y, i); |
558 | 14.3k | return ret; |
559 | 14.3k | } |
560 | 9.49M | } |
561 | 1.58M | } |
562 | | |
563 | 6.52M | return 0; |
564 | 6.54M | } |
565 | | |
566 | | static av_cold int wmv2_decode_init(AVCodecContext *avctx) |
567 | 3.04k | { |
568 | 3.04k | WMV2DecContext *const w = avctx->priv_data; |
569 | 3.04k | H263DecContext *const h = &w->ms.h; |
570 | 3.04k | MpegEncContext *const s = &h->c; |
571 | 3.04k | int ret; |
572 | | |
573 | 3.04k | s->private_ctx = &w->common; |
574 | | |
575 | 3.04k | if ((ret = ff_msmpeg4_decode_init(avctx)) < 0) |
576 | 125 | return ret; |
577 | | |
578 | 2.91k | h->decode_header = wmv2_decode_picture_header; |
579 | 2.91k | h->decode_mb = wmv2_decode_mb; |
580 | | |
581 | 2.91k | ff_wmv2_common_init(s); |
582 | | |
583 | 2.91k | decode_ext_header(w); |
584 | | |
585 | 2.91k | return ff_intrax8_common_init(avctx, &w->x8, h->block[0], |
586 | 2.91k | s->mb_width, s->mb_height); |
587 | 3.04k | } |
588 | | |
589 | | static av_cold int wmv2_decode_end(AVCodecContext *avctx) |
590 | 3.04k | { |
591 | 3.04k | WMV2DecContext *const w = avctx->priv_data; |
592 | | |
593 | 3.04k | ff_intrax8_common_end(&w->x8); |
594 | 3.04k | return ff_mpv_decode_close(avctx); |
595 | 3.04k | } |
596 | | |
597 | | const FFCodec ff_wmv2_decoder = { |
598 | | .p.name = "wmv2", |
599 | | CODEC_LONG_NAME("Windows Media Video 8"), |
600 | | .p.type = AVMEDIA_TYPE_VIDEO, |
601 | | .p.id = AV_CODEC_ID_WMV2, |
602 | | .priv_data_size = sizeof(WMV2DecContext), |
603 | | .init = wmv2_decode_init, |
604 | | .close = wmv2_decode_end, |
605 | | FF_CODEC_DECODE_CB(ff_h263_decode_frame), |
606 | | .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1, |
607 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
608 | | }; |