intrax8: Keep a reference to the context idctdsp
[libav.git] / libavcodec / wmv2dec.c
1 /*
2 * Copyright (c) 2002 The Libav Project
3 *
4 * This file is part of Libav.
5 *
6 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "avcodec.h"
22 #include "h263.h"
23 #include "internal.h"
24 #include "intrax8.h"
25 #include "mathops.h"
26 #include "mpegutils.h"
27 #include "mpegvideo.h"
28 #include "msmpeg4.h"
29 #include "msmpeg4data.h"
30 #include "wmv2.h"
31
32
33 static void parse_mb_skip(Wmv2Context *w)
34 {
35 int mb_x, mb_y;
36 MpegEncContext *const s = &w->s;
37 uint32_t *const mb_type = s->current_picture_ptr->mb_type;
38
39 w->skip_type = get_bits(&s->gb, 2);
40 switch (w->skip_type) {
41 case SKIP_TYPE_NONE:
42 for (mb_y = 0; mb_y < s->mb_height; mb_y++)
43 for (mb_x = 0; mb_x < s->mb_width; mb_x++)
44 mb_type[mb_y * s->mb_stride + mb_x] =
45 MB_TYPE_16x16 | MB_TYPE_L0;
46 break;
47 case SKIP_TYPE_MPEG:
48 for (mb_y = 0; mb_y < s->mb_height; mb_y++)
49 for (mb_x = 0; mb_x < s->mb_width; mb_x++)
50 mb_type[mb_y * s->mb_stride + mb_x] =
51 (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
52 break;
53 case SKIP_TYPE_ROW:
54 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
55 if (get_bits1(&s->gb)) {
56 for (mb_x = 0; mb_x < s->mb_width; mb_x++)
57 mb_type[mb_y * s->mb_stride + mb_x] =
58 MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
59 } else {
60 for (mb_x = 0; mb_x < s->mb_width; mb_x++)
61 mb_type[mb_y * s->mb_stride + mb_x] =
62 (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
63 }
64 }
65 break;
66 case SKIP_TYPE_COL:
67 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
68 if (get_bits1(&s->gb)) {
69 for (mb_y = 0; mb_y < s->mb_height; mb_y++)
70 mb_type[mb_y * s->mb_stride + mb_x] =
71 MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
72 } else {
73 for (mb_y = 0; mb_y < s->mb_height; mb_y++)
74 mb_type[mb_y * s->mb_stride + mb_x] =
75 (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
76 }
77 }
78 break;
79 }
80 }
81
82 static int decode_ext_header(Wmv2Context *w)
83 {
84 MpegEncContext *const s = &w->s;
85 GetBitContext gb;
86 int fps;
87 int code;
88
89 if (s->avctx->extradata_size < 4)
90 return AVERROR_INVALIDDATA;
91
92 init_get_bits(&gb, s->avctx->extradata, 32);
93
94 fps = get_bits(&gb, 5);
95 s->bit_rate = get_bits(&gb, 11) * 1024;
96 w->mspel_bit = get_bits1(&gb);
97 s->loop_filter = get_bits1(&gb);
98 w->abt_flag = get_bits1(&gb);
99 w->j_type_bit = get_bits1(&gb);
100 w->top_left_mv_flag = get_bits1(&gb);
101 w->per_mb_rl_bit = get_bits1(&gb);
102 code = get_bits(&gb, 3);
103
104 if (code == 0)
105 return AVERROR_INVALIDDATA;
106
107 s->slice_height = s->mb_height / code;
108
109 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
110 av_log(s->avctx, AV_LOG_DEBUG,
111 "fps:%d, br:%d, qpbit:%d, abt_flag:%d, j_type_bit:%d, "
112 "tl_mv_flag:%d, mbrl_bit:%d, code:%d, loop_filter:%d, "
113 "slices:%d\n",
114 fps, s->bit_rate, w->mspel_bit, w->abt_flag, w->j_type_bit,
115 w->top_left_mv_flag, w->per_mb_rl_bit, code, s->loop_filter,
116 code);
117 return 0;
118 }
119
120 int ff_wmv2_decode_picture_header(MpegEncContext *s)
121 {
122 Wmv2Context *const w = (Wmv2Context *) s;
123 int code;
124
125 if (s->picture_number == 0)
126 decode_ext_header(w);
127
128 s->pict_type = get_bits1(&s->gb) + 1;
129 if (s->pict_type == AV_PICTURE_TYPE_I) {
130 code = get_bits(&s->gb, 7);
131 av_log(s->avctx, AV_LOG_DEBUG, "I7:%X/\n", code);
132 }
133 s->chroma_qscale = s->qscale = get_bits(&s->gb, 5);
134 if (s->qscale <= 0)
135 return AVERROR_INVALIDDATA;
136
137 return 0;
138 }
139
140 int ff_wmv2_decode_secondary_picture_header(MpegEncContext *s)
141 {
142 Wmv2Context *const w = (Wmv2Context *) s;
143
144 if (s->pict_type == AV_PICTURE_TYPE_I) {
145 if (w->j_type_bit)
146 w->j_type = get_bits1(&s->gb);
147 else
148 w->j_type = 0; // FIXME check
149
150 if (!w->j_type) {
151 if (w->per_mb_rl_bit)
152 s->per_mb_rl_table = get_bits1(&s->gb);
153 else
154 s->per_mb_rl_table = 0;
155
156 if (!s->per_mb_rl_table) {
157 s->rl_chroma_table_index = decode012(&s->gb);
158 s->rl_table_index = decode012(&s->gb);
159 }
160
161 s->dc_table_index = get_bits1(&s->gb);
162 }
163 s->inter_intra_pred = 0;
164 s->no_rounding = 1;
165 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
166 av_log(s->avctx, AV_LOG_DEBUG,
167 "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d j_type:%d \n",
168 s->qscale, s->rl_chroma_table_index, s->rl_table_index,
169 s->dc_table_index, s->per_mb_rl_table, w->j_type);
170 }
171 } else {
172 int cbp_index;
173 w->j_type = 0;
174
175 parse_mb_skip(w);
176 cbp_index = decode012(&s->gb);
177 if (s->qscale <= 10) {
178 int map[3] = { 0, 2, 1 };
179 w->cbp_table_index = map[cbp_index];
180 } else if (s->qscale <= 20) {
181 int map[3] = { 1, 0, 2 };
182 w->cbp_table_index = map[cbp_index];
183 } else {
184 int map[3] = {2,1,0};
185 w->cbp_table_index = map[cbp_index];
186 }
187
188 if (w->mspel_bit)
189 s->mspel = get_bits1(&s->gb);
190 else
191 s->mspel = 0; // FIXME check
192
193 if (w->abt_flag) {
194 w->per_mb_abt = get_bits1(&s->gb) ^ 1;
195 if (!w->per_mb_abt)
196 w->abt_type = decode012(&s->gb);
197 }
198
199 if (w->per_mb_rl_bit)
200 s->per_mb_rl_table = get_bits1(&s->gb);
201 else
202 s->per_mb_rl_table = 0;
203
204 if (!s->per_mb_rl_table) {
205 s->rl_table_index = decode012(&s->gb);
206 s->rl_chroma_table_index = s->rl_table_index;
207 }
208
209 s->dc_table_index = get_bits1(&s->gb);
210 s->mv_table_index = get_bits1(&s->gb);
211
212 s->inter_intra_pred = 0; // (s->width * s->height < 320 * 240 && s->bit_rate <= II_BITRATE);
213 s->no_rounding ^= 1;
214
215 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
216 av_log(s->avctx, AV_LOG_DEBUG,
217 "rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d mspel:%d "
218 "per_mb_abt:%d abt_type:%d cbp:%d ii:%d\n",
219 s->rl_table_index, s->rl_chroma_table_index,
220 s->dc_table_index, s->mv_table_index,
221 s->per_mb_rl_table, s->qscale, s->mspel,
222 w->per_mb_abt, w->abt_type, w->cbp_table_index,
223 s->inter_intra_pred);
224 }
225 }
226 s->esc3_level_length = 0;
227 s->esc3_run_length = 0;
228 s->picture_number++; // FIXME ?
229
230 if (w->j_type) {
231 ff_intrax8_decode_picture(&w->x8, 2 * s->qscale, (s->qscale - 1) | 1);
232
233 ff_er_add_slice(&w->s.er, 0, 0,
234 (w->s.mb_x >> 1) - 1, (w->s.mb_y >> 1) - 1,
235 ER_MB_END);
236 return 1;
237 }
238
239 return 0;
240 }
241
242 static inline int wmv2_decode_motion(Wmv2Context *w, int *mx_ptr, int *my_ptr)
243 {
244 MpegEncContext *const s = &w->s;
245 int ret;
246
247 ret = ff_msmpeg4_decode_motion(s, mx_ptr, my_ptr);
248
249 if (ret < 0)
250 return ret;
251
252 if ((((*mx_ptr) | (*my_ptr)) & 1) && s->mspel)
253 w->hshift = get_bits1(&s->gb);
254 else
255 w->hshift = 0;
256
257 return 0;
258 }
259
260 static int16_t *wmv2_pred_motion(Wmv2Context *w, int *px, int *py)
261 {
262 MpegEncContext *const s = &w->s;
263 int xy, wrap, diff, type;
264 int16_t *A, *B, *C, *mot_val;
265
266 wrap = s->b8_stride;
267 xy = s->block_index[0];
268
269 mot_val = s->current_picture.motion_val[0][xy];
270
271 A = s->current_picture.motion_val[0][xy - 1];
272 B = s->current_picture.motion_val[0][xy - wrap];
273 C = s->current_picture.motion_val[0][xy + 2 - wrap];
274
275 if (s->mb_x && !s->first_slice_line && !s->mspel && w->top_left_mv_flag)
276 diff = FFMAX(FFABS(A[0] - B[0]), FFABS(A[1] - B[1]));
277 else
278 diff = 0;
279
280 if (diff >= 8)
281 type = get_bits1(&s->gb);
282 else
283 type = 2;
284
285 if (type == 0) {
286 *px = A[0];
287 *py = A[1];
288 } else if (type == 1) {
289 *px = B[0];
290 *py = B[1];
291 } else {
292 /* special case for first (slice) line */
293 if (s->first_slice_line) {
294 *px = A[0];
295 *py = A[1];
296 } else {
297 *px = mid_pred(A[0], B[0], C[0]);
298 *py = mid_pred(A[1], B[1], C[1]);
299 }
300 }
301
302 return mot_val;
303 }
304
305 static inline int wmv2_decode_inter_block(Wmv2Context *w, int16_t *block,
306 int n, int cbp)
307 {
308 MpegEncContext *const s = &w->s;
309 static const int sub_cbp_table[3] = { 2, 3, 1 };
310 int sub_cbp, ret;
311
312 if (!cbp) {
313 s->block_last_index[n] = -1;
314 return 0;
315 }
316
317 if (w->per_block_abt)
318 w->abt_type = decode012(&s->gb);
319 w->abt_type_table[n] = w->abt_type;
320
321 if (w->abt_type) {
322 // const uint8_t *scantable = w->abt_scantable[w->abt_type - 1].permutated;
323 const uint8_t *scantable = w->abt_scantable[w->abt_type - 1].scantable;
324 // const uint8_t *scantable = w->abt_type - 1 ? w->abt_scantable[1].permutated : w->abt_scantable[0].scantable;
325
326 sub_cbp = sub_cbp_table[decode012(&s->gb)];
327
328 if (sub_cbp & 1)
329 if ((ret = ff_msmpeg4_decode_block(s, block, n, 1, scantable)) < 0)
330 return ret;
331
332 if (sub_cbp & 2)
333 if ((ret = ff_msmpeg4_decode_block(s, w->abt_block2[n], n, 1, scantable)) < 0)
334 return ret;
335
336 s->block_last_index[n] = 63;
337
338 return 0;
339 } else {
340 return ff_msmpeg4_decode_block(s, block, n, 1,
341 s->inter_scantable.permutated);
342 }
343 }
344
345 int ff_wmv2_decode_mb(MpegEncContext *s, int16_t block[6][64])
346 {
347 Wmv2Context *const w = (Wmv2Context *) s;
348 int cbp, code, i, ret;
349 uint8_t *coded_val;
350
351 if (w->j_type)
352 return 0;
353
354 if (s->pict_type == AV_PICTURE_TYPE_P) {
355 if (IS_SKIP(s->current_picture.mb_type[s->mb_y * s->mb_stride + s->mb_x])) {
356 /* skip mb */
357 s->mb_intra = 0;
358 for (i = 0; i < 6; i++)
359 s->block_last_index[i] = -1;
360 s->mv_dir = MV_DIR_FORWARD;
361 s->mv_type = MV_TYPE_16X16;
362 s->mv[0][0][0] = 0;
363 s->mv[0][0][1] = 0;
364 s->mb_skipped = 1;
365 w->hshift = 0;
366 return 0;
367 }
368
369 code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[w->cbp_table_index].table,
370 MB_NON_INTRA_VLC_BITS, 3);
371 if (code < 0)
372 return AVERROR_INVALIDDATA;
373 s->mb_intra = (~code & 0x40) >> 6;
374
375 cbp = code & 0x3f;
376 } else {
377 s->mb_intra = 1;
378 code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
379 if (code < 0) {
380 av_log(s->avctx, AV_LOG_ERROR,
381 "II-cbp illegal at %d %d\n", s->mb_x, s->mb_y);
382 return AVERROR_INVALIDDATA;
383 }
384 /* predict coded block pattern */
385 cbp = 0;
386 for (i = 0; i < 6; i++) {
387 int val = ((code >> (5 - i)) & 1);
388 if (i < 4) {
389 int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
390 val = val ^ pred;
391 *coded_val = val;
392 }
393 cbp |= val << (5 - i);
394 }
395 }
396
397 if (!s->mb_intra) {
398 int mx, my;
399 wmv2_pred_motion(w, &mx, &my);
400
401 if (cbp) {
402 s->bdsp.clear_blocks(s->block[0]);
403 if (s->per_mb_rl_table) {
404 s->rl_table_index = decode012(&s->gb);
405 s->rl_chroma_table_index = s->rl_table_index;
406 }
407
408 if (w->abt_flag && w->per_mb_abt) {
409 w->per_block_abt = get_bits1(&s->gb);
410 if (!w->per_block_abt)
411 w->abt_type = decode012(&s->gb);
412 } else
413 w->per_block_abt = 0;
414 }
415
416 if ((ret = wmv2_decode_motion(w, &mx, &my)) < 0)
417 return ret;
418
419 s->mv_dir = MV_DIR_FORWARD;
420 s->mv_type = MV_TYPE_16X16;
421 s->mv[0][0][0] = mx;
422 s->mv[0][0][1] = my;
423
424 for (i = 0; i < 6; i++) {
425 if ((ret = wmv2_decode_inter_block(w, block[i], i, (cbp >> (5 - i)) & 1)) < 0) {
426 av_log(s->avctx, AV_LOG_ERROR,
427 "\nerror while decoding inter block: %d x %d (%d)\n",
428 s->mb_x, s->mb_y, i);
429 return ret;
430 }
431 }
432 } else {
433 if (s->pict_type == AV_PICTURE_TYPE_P)
434 ff_dlog(s->avctx, "%d%d ", s->inter_intra_pred, cbp);
435 ff_dlog(s->avctx, "I at %d %d %d %06X\n", s->mb_x, s->mb_y,
436 ((cbp & 3) ? 1 : 0) + ((cbp & 0x3C) ? 2 : 0),
437 show_bits(&s->gb, 24));
438 s->ac_pred = get_bits1(&s->gb);
439 if (s->inter_intra_pred) {
440 s->h263_aic_dir = get_vlc2(&s->gb, ff_inter_intra_vlc.table,
441 INTER_INTRA_VLC_BITS, 1);
442 ff_dlog(s->avctx, "%d%d %d %d/",
443 s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
444 }
445 if (s->per_mb_rl_table && cbp) {
446 s->rl_table_index = decode012(&s->gb);
447 s->rl_chroma_table_index = s->rl_table_index;
448 }
449
450 s->bdsp.clear_blocks(s->block[0]);
451 for (i = 0; i < 6; i++) {
452 if ((ret = ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL)) < 0) {
453 av_log(s->avctx, AV_LOG_ERROR,
454 "\nerror while decoding intra block: %d x %d (%d)\n",
455 s->mb_x, s->mb_y, i);
456 return ret;
457 }
458 }
459 }
460
461 return 0;
462 }
463
464 static av_cold int wmv2_decode_init(AVCodecContext *avctx)
465 {
466 Wmv2Context *const w = avctx->priv_data;
467 int ret;
468
469 if ((ret = ff_msmpeg4_decode_init(avctx)) < 0)
470 return ret;
471
472 ff_wmv2_common_init(w);
473
474 return ff_intrax8_common_init(&w->x8, &w->s.idsp, &w->s);
475 }
476
477 static av_cold int wmv2_decode_end(AVCodecContext *avctx)
478 {
479 Wmv2Context *w = avctx->priv_data;
480
481 ff_intrax8_common_end(&w->x8);
482 return ff_h263_decode_end(avctx);
483 }
484
485 AVCodec ff_wmv2_decoder = {
486 .name = "wmv2",
487 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 8"),
488 .type = AVMEDIA_TYPE_VIDEO,
489 .id = AV_CODEC_ID_WMV2,
490 .priv_data_size = sizeof(Wmv2Context),
491 .init = wmv2_decode_init,
492 .close = wmv2_decode_end,
493 .decode = ff_h263_decode_frame,
494 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
495 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
496 AV_PIX_FMT_NONE },
497 };