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;
144 if (s->pict_type != AV_PICTURE_TYPE_I && show_bits(&s->gb, 1)) {
145 GetBitContext gb = s->gb;
146 int skip_type = get_bits(&gb, 2);
147 int run = skip_type == SKIP_TYPE_COL ? s->mb_width : s->mb_height;
150 int block = FFMIN(run, 25);
151 if (get_bits(&gb, block) + 1 != 1<<block)
156 return FRAME_SKIPPED;
162 int ff_wmv2_decode_secondary_picture_header(MpegEncContext *s)
164 Wmv2Context *const w = (Wmv2Context *) s;
166 if (s->pict_type == AV_PICTURE_TYPE_I) {
168 w->j_type = get_bits1(&s->gb);
170 w->j_type = 0; // FIXME check
173 if (w->per_mb_rl_bit)
174 s->per_mb_rl_table = get_bits1(&s->gb);
176 s->per_mb_rl_table = 0;
178 if (!s->per_mb_rl_table) {
179 s->rl_chroma_table_index = decode012(&s->gb);
180 s->rl_table_index = decode012(&s->gb);
183 s->dc_table_index = get_bits1(&s->gb);
185 // at minimum one bit per macroblock is required at least in a valid frame,
186 // we discard frames much smaller than this. Frames smaller than 1/8 of the
187 // smallest "black/skip" frame generally contain not much recoverable content
188 // while at the same time they have the highest computational requirements
190 if (get_bits_left(&s->gb) * 8LL < (s->width+15)/16 * ((s->height+15)/16))
191 return AVERROR_INVALIDDATA;
193 s->inter_intra_pred = 0;
195 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
196 av_log(s->avctx, AV_LOG_DEBUG,
197 "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d j_type:%d \n",
198 s->qscale, s->rl_chroma_table_index, s->rl_table_index,
199 s->dc_table_index, s->per_mb_rl_table, w->j_type);
206 ret = parse_mb_skip(w);
209 cbp_index = decode012(&s->gb);
210 w->cbp_table_index = wmv2_get_cbp_table_index(s, cbp_index);
213 s->mspel = get_bits1(&s->gb);
215 s->mspel = 0; // FIXME check
218 w->per_mb_abt = get_bits1(&s->gb) ^ 1;
220 w->abt_type = decode012(&s->gb);
223 if (w->per_mb_rl_bit)
224 s->per_mb_rl_table = get_bits1(&s->gb);
226 s->per_mb_rl_table = 0;
228 if (!s->per_mb_rl_table) {
229 s->rl_table_index = decode012(&s->gb);
230 s->rl_chroma_table_index = s->rl_table_index;
233 if (get_bits_left(&s->gb) < 2)
234 return AVERROR_INVALIDDATA;
236 s->dc_table_index = get_bits1(&s->gb);
237 s->mv_table_index = get_bits1(&s->gb);
239 s->inter_intra_pred = 0; // (s->width * s->height < 320 * 240 && s->bit_rate <= II_BITRATE);
242 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
243 av_log(s->avctx, AV_LOG_DEBUG,
244 "rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d mspel:%d "
245 "per_mb_abt:%d abt_type:%d cbp:%d ii:%d\n",
246 s->rl_table_index, s->rl_chroma_table_index,
247 s->dc_table_index, s->mv_table_index,
248 s->per_mb_rl_table, s->qscale, s->mspel,
249 w->per_mb_abt, w->abt_type, w->cbp_table_index,
250 s->inter_intra_pred);
253 s->esc3_level_length = 0;
254 s->esc3_run_length = 0;
255 s->picture_number++; // FIXME ?
258 ff_intrax8_decode_picture(&w->x8, &s->current_picture,
259 &s->gb, &s->mb_x, &s->mb_y,
260 2 * s->qscale, (s->qscale - 1) | 1,
261 s->loop_filter, s->low_delay);
263 ff_er_add_slice(&w->s.er, 0, 0,
264 (w->s.mb_x >> 1) - 1, (w->s.mb_y >> 1) - 1,
272 static inline int wmv2_decode_motion(Wmv2Context *w, int *mx_ptr, int *my_ptr)
274 MpegEncContext *const s = &w->s;
277 ret = ff_msmpeg4_decode_motion(s, mx_ptr, my_ptr);
282 if ((((*mx_ptr) | (*my_ptr)) & 1) && s->mspel)
283 w->hshift = get_bits1(&s->gb);
290 static int16_t *wmv2_pred_motion(Wmv2Context *w, int *px, int *py)
292 MpegEncContext *const s = &w->s;
293 int xy, wrap, diff, type;
294 int16_t *A, *B, *C, *mot_val;
297 xy = s->block_index[0];
299 mot_val = s->current_picture.motion_val[0][xy];
301 A = s->current_picture.motion_val[0][xy - 1];
302 B = s->current_picture.motion_val[0][xy - wrap];
303 C = s->current_picture.motion_val[0][xy + 2 - wrap];
305 if (s->mb_x && !s->first_slice_line && !s->mspel && w->top_left_mv_flag)
306 diff = FFMAX(FFABS(A[0] - B[0]), FFABS(A[1] - B[1]));
311 type = get_bits1(&s->gb);
318 } else if (type == 1) {
322 /* special case for first (slice) line */
323 if (s->first_slice_line) {
327 *px = mid_pred(A[0], B[0], C[0]);
328 *py = mid_pred(A[1], B[1], C[1]);
335 static inline int wmv2_decode_inter_block(Wmv2Context *w, int16_t *block,
338 MpegEncContext *const s = &w->s;
339 static const int sub_cbp_table[3] = { 2, 3, 1 };
343 s->block_last_index[n] = -1;
347 if (w->per_block_abt)
348 w->abt_type = decode012(&s->gb);
349 w->abt_type_table[n] = w->abt_type;
352 // const uint8_t *scantable = w->abt_scantable[w->abt_type - 1].permutated;
353 const uint8_t *scantable = w->abt_scantable[w->abt_type - 1].scantable;
354 // const uint8_t *scantable = w->abt_type - 1 ? w->abt_scantable[1].permutated : w->abt_scantable[0].scantable;
356 sub_cbp = sub_cbp_table[decode012(&s->gb)];
359 if ((ret = ff_msmpeg4_decode_block(s, block, n, 1, scantable)) < 0)
363 if ((ret = ff_msmpeg4_decode_block(s, w->abt_block2[n], n, 1, scantable)) < 0)
366 s->block_last_index[n] = 63;
370 return ff_msmpeg4_decode_block(s, block, n, 1,
371 s->inter_scantable.permutated);
375 int ff_wmv2_decode_mb(MpegEncContext *s, int16_t block[6][64])
377 Wmv2Context *const w = (Wmv2Context *) s;
378 int cbp, code, i, ret;
384 if (s->pict_type == AV_PICTURE_TYPE_P) {
385 if (IS_SKIP(s->current_picture.mb_type[s->mb_y * s->mb_stride + s->mb_x])) {
388 for (i = 0; i < 6; i++)
389 s->block_last_index[i] = -1;
390 s->mv_dir = MV_DIR_FORWARD;
391 s->mv_type = MV_TYPE_16X16;
398 if (get_bits_left(&s->gb) <= 0)
399 return AVERROR_INVALIDDATA;
401 code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[w->cbp_table_index].table,
402 MB_NON_INTRA_VLC_BITS, 3);
404 return AVERROR_INVALIDDATA;
405 s->mb_intra = (~code & 0x40) >> 6;
410 if (get_bits_left(&s->gb) <= 0)
411 return AVERROR_INVALIDDATA;
412 code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
414 av_log(s->avctx, AV_LOG_ERROR,
415 "II-cbp illegal at %d %d\n", s->mb_x, s->mb_y);
416 return AVERROR_INVALIDDATA;
418 /* predict coded block pattern */
420 for (i = 0; i < 6; i++) {
421 int val = ((code >> (5 - i)) & 1);
423 int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
427 cbp |= val << (5 - i);
433 wmv2_pred_motion(w, &mx, &my);
436 s->bdsp.clear_blocks(s->block[0]);
437 if (s->per_mb_rl_table) {
438 s->rl_table_index = decode012(&s->gb);
439 s->rl_chroma_table_index = s->rl_table_index;
442 if (w->abt_flag && w->per_mb_abt) {
443 w->per_block_abt = get_bits1(&s->gb);
444 if (!w->per_block_abt)
445 w->abt_type = decode012(&s->gb);
447 w->per_block_abt = 0;
450 if ((ret = wmv2_decode_motion(w, &mx, &my)) < 0)
453 s->mv_dir = MV_DIR_FORWARD;
454 s->mv_type = MV_TYPE_16X16;
458 for (i = 0; i < 6; i++) {
459 if ((ret = wmv2_decode_inter_block(w, block[i], i, (cbp >> (5 - i)) & 1)) < 0) {
460 av_log(s->avctx, AV_LOG_ERROR,
461 "\nerror while decoding inter block: %d x %d (%d)\n",
462 s->mb_x, s->mb_y, i);
467 if (s->pict_type == AV_PICTURE_TYPE_P)
468 ff_dlog(s->avctx, "%d%d ", s->inter_intra_pred, cbp);
469 ff_dlog(s->avctx, "I at %d %d %d %06X\n", s->mb_x, s->mb_y,
470 ((cbp & 3) ? 1 : 0) + ((cbp & 0x3C) ? 2 : 0),
471 show_bits(&s->gb, 24));
472 s->ac_pred = get_bits1(&s->gb);
473 if (s->inter_intra_pred) {
474 s->h263_aic_dir = get_vlc2(&s->gb, ff_inter_intra_vlc.table,
475 INTER_INTRA_VLC_BITS, 1);
476 ff_dlog(s->avctx, "%d%d %d %d/",
477 s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
479 if (s->per_mb_rl_table && cbp) {
480 s->rl_table_index = decode012(&s->gb);
481 s->rl_chroma_table_index = s->rl_table_index;
484 s->bdsp.clear_blocks(s->block[0]);
485 for (i = 0; i < 6; i++) {
486 if ((ret = ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL)) < 0) {
487 av_log(s->avctx, AV_LOG_ERROR,
488 "\nerror while decoding intra block: %d x %d (%d)\n",
489 s->mb_x, s->mb_y, i);
498 static av_cold int wmv2_decode_init(AVCodecContext *avctx)
500 Wmv2Context *const w = avctx->priv_data;
503 if ((ret = ff_msmpeg4_decode_init(avctx)) < 0)
506 ff_wmv2_common_init(w);
508 return ff_intrax8_common_init(avctx, &w->x8, &w->s.idsp,
509 w->s.block, w->s.block_last_index,
510 w->s.mb_width, w->s.mb_height);
513 static av_cold int wmv2_decode_end(AVCodecContext *avctx)
515 Wmv2Context *w = avctx->priv_data;
517 ff_intrax8_common_end(&w->x8);
518 return ff_h263_decode_end(avctx);
521 AVCodec ff_wmv2_decoder = {
523 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 8"),
524 .type = AVMEDIA_TYPE_VIDEO,
525 .id = AV_CODEC_ID_WMV2,
526 .priv_data_size = sizeof(Wmv2Context),
527 .init = wmv2_decode_init,
528 .close = wmv2_decode_end,
529 .decode = ff_h263_decode_frame,
530 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
531 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,