2 * Copyright (c) 2002 The FFmpeg Project
4 * This file is part of FFmpeg.
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.
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.
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
26 #include "mpegutils.h"
27 #include "mpegvideo.h"
29 #include "msmpeg4data.h"
33 static int parse_mb_skip(Wmv2Context *w)
36 MpegEncContext *const s = &w->s;
37 uint32_t *const mb_type = s->current_picture_ptr->mb_type;
39 w->skip_type = get_bits(&s->gb, 2);
40 switch (w->skip_type) {
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;
48 if (get_bits_left(&s->gb) < s->mb_height * s->mb_width)
49 return AVERROR_INVALIDDATA;
50 for (mb_y = 0; mb_y < s->mb_height; mb_y++)
51 for (mb_x = 0; mb_x < s->mb_width; mb_x++)
52 mb_type[mb_y * s->mb_stride + mb_x] =
53 (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
56 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
57 if (get_bits_left(&s->gb) < 1)
58 return AVERROR_INVALIDDATA;
59 if (get_bits1(&s->gb)) {
60 for (mb_x = 0; mb_x < s->mb_width; mb_x++)
61 mb_type[mb_y * s->mb_stride + mb_x] =
62 MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
64 for (mb_x = 0; mb_x < s->mb_width; mb_x++)
65 mb_type[mb_y * s->mb_stride + mb_x] =
66 (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
71 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
72 if (get_bits_left(&s->gb) < 1)
73 return AVERROR_INVALIDDATA;
74 if (get_bits1(&s->gb)) {
75 for (mb_y = 0; mb_y < s->mb_height; mb_y++)
76 mb_type[mb_y * s->mb_stride + mb_x] =
77 MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
79 for (mb_y = 0; mb_y < s->mb_height; mb_y++)
80 mb_type[mb_y * s->mb_stride + mb_x] =
81 (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
89 static int decode_ext_header(Wmv2Context *w)
91 MpegEncContext *const s = &w->s;
96 if (s->avctx->extradata_size < 4)
97 return AVERROR_INVALIDDATA;
99 init_get_bits(&gb, s->avctx->extradata, 32);
101 fps = get_bits(&gb, 5);
102 s->bit_rate = get_bits(&gb, 11) * 1024;
103 w->mspel_bit = get_bits1(&gb);
104 s->loop_filter = get_bits1(&gb);
105 w->abt_flag = get_bits1(&gb);
106 w->j_type_bit = get_bits1(&gb);
107 w->top_left_mv_flag = get_bits1(&gb);
108 w->per_mb_rl_bit = get_bits1(&gb);
109 code = get_bits(&gb, 3);
112 return AVERROR_INVALIDDATA;
114 s->slice_height = s->mb_height / code;
116 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
117 av_log(s->avctx, AV_LOG_DEBUG,
118 "fps:%d, br:%"PRId64", qpbit:%d, abt_flag:%d, j_type_bit:%d, "
119 "tl_mv_flag:%d, mbrl_bit:%d, code:%d, loop_filter:%d, "
121 fps, s->bit_rate, w->mspel_bit, w->abt_flag, w->j_type_bit,
122 w->top_left_mv_flag, w->per_mb_rl_bit, code, s->loop_filter,
127 int ff_wmv2_decode_picture_header(MpegEncContext *s)
129 Wmv2Context *const w = (Wmv2Context *) s;
132 if (s->picture_number == 0)
133 decode_ext_header(w);
135 s->pict_type = get_bits1(&s->gb) + 1;
136 if (s->pict_type == AV_PICTURE_TYPE_I) {
137 code = get_bits(&s->gb, 7);
138 av_log(s->avctx, AV_LOG_DEBUG, "I7:%X/\n", code);
140 s->chroma_qscale = s->qscale = get_bits(&s->gb, 5);
142 return AVERROR_INVALIDDATA;
147 int ff_wmv2_decode_secondary_picture_header(MpegEncContext *s)
149 Wmv2Context *const w = (Wmv2Context *) s;
151 if (s->pict_type == AV_PICTURE_TYPE_I) {
153 w->j_type = get_bits1(&s->gb);
155 w->j_type = 0; // FIXME check
158 if (w->per_mb_rl_bit)
159 s->per_mb_rl_table = get_bits1(&s->gb);
161 s->per_mb_rl_table = 0;
163 if (!s->per_mb_rl_table) {
164 s->rl_chroma_table_index = decode012(&s->gb);
165 s->rl_table_index = decode012(&s->gb);
168 s->dc_table_index = get_bits1(&s->gb);
170 s->inter_intra_pred = 0;
172 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
173 av_log(s->avctx, AV_LOG_DEBUG,
174 "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d j_type:%d \n",
175 s->qscale, s->rl_chroma_table_index, s->rl_table_index,
176 s->dc_table_index, s->per_mb_rl_table, w->j_type);
183 ret = parse_mb_skip(w);
186 cbp_index = decode012(&s->gb);
187 w->cbp_table_index = wmv2_get_cbp_table_index(s, cbp_index);
190 s->mspel = get_bits1(&s->gb);
192 s->mspel = 0; // FIXME check
195 w->per_mb_abt = get_bits1(&s->gb) ^ 1;
197 w->abt_type = decode012(&s->gb);
200 if (w->per_mb_rl_bit)
201 s->per_mb_rl_table = get_bits1(&s->gb);
203 s->per_mb_rl_table = 0;
205 if (!s->per_mb_rl_table) {
206 s->rl_table_index = decode012(&s->gb);
207 s->rl_chroma_table_index = s->rl_table_index;
210 s->dc_table_index = get_bits1(&s->gb);
211 s->mv_table_index = get_bits1(&s->gb);
213 s->inter_intra_pred = 0; // (s->width * s->height < 320 * 240 && s->bit_rate <= II_BITRATE);
216 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
217 av_log(s->avctx, AV_LOG_DEBUG,
218 "rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d mspel:%d "
219 "per_mb_abt:%d abt_type:%d cbp:%d ii:%d\n",
220 s->rl_table_index, s->rl_chroma_table_index,
221 s->dc_table_index, s->mv_table_index,
222 s->per_mb_rl_table, s->qscale, s->mspel,
223 w->per_mb_abt, w->abt_type, w->cbp_table_index,
224 s->inter_intra_pred);
227 s->esc3_level_length = 0;
228 s->esc3_run_length = 0;
229 s->picture_number++; // FIXME ?
232 ff_intrax8_decode_picture(&w->x8, &s->current_picture,
233 &s->gb, &s->mb_x, &s->mb_y,
234 2 * s->qscale, (s->qscale - 1) | 1,
235 s->loop_filter, s->low_delay);
237 ff_er_add_slice(&w->s.er, 0, 0,
238 (w->s.mb_x >> 1) - 1, (w->s.mb_y >> 1) - 1,
246 static inline int wmv2_decode_motion(Wmv2Context *w, int *mx_ptr, int *my_ptr)
248 MpegEncContext *const s = &w->s;
251 ret = ff_msmpeg4_decode_motion(s, mx_ptr, my_ptr);
256 if ((((*mx_ptr) | (*my_ptr)) & 1) && s->mspel)
257 w->hshift = get_bits1(&s->gb);
264 static int16_t *wmv2_pred_motion(Wmv2Context *w, int *px, int *py)
266 MpegEncContext *const s = &w->s;
267 int xy, wrap, diff, type;
268 int16_t *A, *B, *C, *mot_val;
271 xy = s->block_index[0];
273 mot_val = s->current_picture.motion_val[0][xy];
275 A = s->current_picture.motion_val[0][xy - 1];
276 B = s->current_picture.motion_val[0][xy - wrap];
277 C = s->current_picture.motion_val[0][xy + 2 - wrap];
279 if (s->mb_x && !s->first_slice_line && !s->mspel && w->top_left_mv_flag)
280 diff = FFMAX(FFABS(A[0] - B[0]), FFABS(A[1] - B[1]));
285 type = get_bits1(&s->gb);
292 } else if (type == 1) {
296 /* special case for first (slice) line */
297 if (s->first_slice_line) {
301 *px = mid_pred(A[0], B[0], C[0]);
302 *py = mid_pred(A[1], B[1], C[1]);
309 static inline int wmv2_decode_inter_block(Wmv2Context *w, int16_t *block,
312 MpegEncContext *const s = &w->s;
313 static const int sub_cbp_table[3] = { 2, 3, 1 };
317 s->block_last_index[n] = -1;
321 if (w->per_block_abt)
322 w->abt_type = decode012(&s->gb);
323 w->abt_type_table[n] = w->abt_type;
326 // const uint8_t *scantable = w->abt_scantable[w->abt_type - 1].permutated;
327 const uint8_t *scantable = w->abt_scantable[w->abt_type - 1].scantable;
328 // const uint8_t *scantable = w->abt_type - 1 ? w->abt_scantable[1].permutated : w->abt_scantable[0].scantable;
330 sub_cbp = sub_cbp_table[decode012(&s->gb)];
333 if ((ret = ff_msmpeg4_decode_block(s, block, n, 1, scantable)) < 0)
337 if ((ret = ff_msmpeg4_decode_block(s, w->abt_block2[n], n, 1, scantable)) < 0)
340 s->block_last_index[n] = 63;
344 return ff_msmpeg4_decode_block(s, block, n, 1,
345 s->inter_scantable.permutated);
349 int ff_wmv2_decode_mb(MpegEncContext *s, int16_t block[6][64])
351 Wmv2Context *const w = (Wmv2Context *) s;
352 int cbp, code, i, ret;
358 if (s->pict_type == AV_PICTURE_TYPE_P) {
359 if (IS_SKIP(s->current_picture.mb_type[s->mb_y * s->mb_stride + s->mb_x])) {
362 for (i = 0; i < 6; i++)
363 s->block_last_index[i] = -1;
364 s->mv_dir = MV_DIR_FORWARD;
365 s->mv_type = MV_TYPE_16X16;
372 if (get_bits_left(&s->gb) <= 0)
373 return AVERROR_INVALIDDATA;
375 code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[w->cbp_table_index].table,
376 MB_NON_INTRA_VLC_BITS, 3);
378 return AVERROR_INVALIDDATA;
379 s->mb_intra = (~code & 0x40) >> 6;
384 if (get_bits_left(&s->gb) <= 0)
385 return AVERROR_INVALIDDATA;
386 code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
388 av_log(s->avctx, AV_LOG_ERROR,
389 "II-cbp illegal at %d %d\n", s->mb_x, s->mb_y);
390 return AVERROR_INVALIDDATA;
392 /* predict coded block pattern */
394 for (i = 0; i < 6; i++) {
395 int val = ((code >> (5 - i)) & 1);
397 int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
401 cbp |= val << (5 - i);
407 wmv2_pred_motion(w, &mx, &my);
410 s->bdsp.clear_blocks(s->block[0]);
411 if (s->per_mb_rl_table) {
412 s->rl_table_index = decode012(&s->gb);
413 s->rl_chroma_table_index = s->rl_table_index;
416 if (w->abt_flag && w->per_mb_abt) {
417 w->per_block_abt = get_bits1(&s->gb);
418 if (!w->per_block_abt)
419 w->abt_type = decode012(&s->gb);
421 w->per_block_abt = 0;
424 if ((ret = wmv2_decode_motion(w, &mx, &my)) < 0)
427 s->mv_dir = MV_DIR_FORWARD;
428 s->mv_type = MV_TYPE_16X16;
432 for (i = 0; i < 6; i++) {
433 if ((ret = wmv2_decode_inter_block(w, block[i], i, (cbp >> (5 - i)) & 1)) < 0) {
434 av_log(s->avctx, AV_LOG_ERROR,
435 "\nerror while decoding inter block: %d x %d (%d)\n",
436 s->mb_x, s->mb_y, i);
441 if (s->pict_type == AV_PICTURE_TYPE_P)
442 ff_dlog(s->avctx, "%d%d ", s->inter_intra_pred, cbp);
443 ff_dlog(s->avctx, "I at %d %d %d %06X\n", s->mb_x, s->mb_y,
444 ((cbp & 3) ? 1 : 0) + ((cbp & 0x3C) ? 2 : 0),
445 show_bits(&s->gb, 24));
446 s->ac_pred = get_bits1(&s->gb);
447 if (s->inter_intra_pred) {
448 s->h263_aic_dir = get_vlc2(&s->gb, ff_inter_intra_vlc.table,
449 INTER_INTRA_VLC_BITS, 1);
450 ff_dlog(s->avctx, "%d%d %d %d/",
451 s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
453 if (s->per_mb_rl_table && cbp) {
454 s->rl_table_index = decode012(&s->gb);
455 s->rl_chroma_table_index = s->rl_table_index;
458 s->bdsp.clear_blocks(s->block[0]);
459 for (i = 0; i < 6; i++) {
460 if ((ret = ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL)) < 0) {
461 av_log(s->avctx, AV_LOG_ERROR,
462 "\nerror while decoding intra block: %d x %d (%d)\n",
463 s->mb_x, s->mb_y, i);
472 static av_cold int wmv2_decode_init(AVCodecContext *avctx)
474 Wmv2Context *const w = avctx->priv_data;
477 if ((ret = ff_msmpeg4_decode_init(avctx)) < 0)
480 ff_wmv2_common_init(w);
482 return ff_intrax8_common_init(avctx, &w->x8, &w->s.idsp,
483 w->s.block, w->s.block_last_index,
484 w->s.mb_width, w->s.mb_height);
487 static av_cold int wmv2_decode_end(AVCodecContext *avctx)
489 Wmv2Context *w = avctx->priv_data;
491 ff_intrax8_common_end(&w->x8);
492 return ff_h263_decode_end(avctx);
495 AVCodec ff_wmv2_decoder = {
497 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 8"),
498 .type = AVMEDIA_TYPE_VIDEO,
499 .id = AV_CODEC_ID_WMV2,
500 .priv_data_size = sizeof(Wmv2Context),
501 .init = wmv2_decode_init,
502 .close = wmv2_decode_end,
503 .decode = ff_h263_decode_frame,
504 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
505 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,