FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
wmv2dec.c
Go to the documentation of this file.
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 "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] =
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] =
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] =
59  } else {
60  for (mb_x = 0; mb_x < s->mb_width; mb_x++)
61  mb_type[mb_y * s->mb_stride + mb_x] =
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] =
72  } else {
73  for (mb_y = 0; mb_y < s->mb_height; mb_y++)
74  mb_type[mb_y * s->mb_stride + mb_x] =
76  }
77  }
78  break;
79  }
80 }
81 
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)
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 
121 {
122  Wmv2Context *const w = (Wmv2Context *) s;
123  int code;
124 
125  if (s->picture_number == 0)
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 
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) {
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) {
167  "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d j_type:%d \n",
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  w->cbp_table_index = wmv2_get_cbp_table_index(s, cbp_index);
178 
179  if (w->mspel_bit)
180  s->mspel = get_bits1(&s->gb);
181  else
182  s->mspel = 0; // FIXME check
183 
184  if (w->abt_flag) {
185  w->per_mb_abt = get_bits1(&s->gb) ^ 1;
186  if (!w->per_mb_abt)
187  w->abt_type = decode012(&s->gb);
188  }
189 
190  if (w->per_mb_rl_bit)
191  s->per_mb_rl_table = get_bits1(&s->gb);
192  else
193  s->per_mb_rl_table = 0;
194 
195  if (!s->per_mb_rl_table) {
196  s->rl_table_index = decode012(&s->gb);
198  }
199 
200  s->dc_table_index = get_bits1(&s->gb);
201  s->mv_table_index = get_bits1(&s->gb);
202 
203  s->inter_intra_pred = 0; // (s->width * s->height < 320 * 240 && s->bit_rate <= II_BITRATE);
204  s->no_rounding ^= 1;
205 
206  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
208  "rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d mspel:%d "
209  "per_mb_abt:%d abt_type:%d cbp:%d ii:%d\n",
212  s->per_mb_rl_table, s->qscale, s->mspel,
214  s->inter_intra_pred);
215  }
216  }
217  s->esc3_level_length = 0;
218  s->esc3_run_length = 0;
219  s->picture_number++; // FIXME ?
220 
221  if (w->j_type) {
222  ff_intrax8_decode_picture(&w->x8, 2 * s->qscale, (s->qscale - 1) | 1);
223  return 1;
224  }
225 
226  return 0;
227 }
228 
229 static inline int wmv2_decode_motion(Wmv2Context *w, int *mx_ptr, int *my_ptr)
230 {
231  MpegEncContext *const s = &w->s;
232  int ret;
233 
234  ret = ff_msmpeg4_decode_motion(s, mx_ptr, my_ptr);
235 
236  if (ret < 0)
237  return ret;
238 
239  if ((((*mx_ptr) | (*my_ptr)) & 1) && s->mspel)
240  w->hshift = get_bits1(&s->gb);
241  else
242  w->hshift = 0;
243 
244  return 0;
245 }
246 
247 static int16_t *wmv2_pred_motion(Wmv2Context *w, int *px, int *py)
248 {
249  MpegEncContext *const s = &w->s;
250  int xy, wrap, diff, type;
251  int16_t *A, *B, *C, *mot_val;
252 
253  wrap = s->b8_stride;
254  xy = s->block_index[0];
255 
256  mot_val = s->current_picture.motion_val[0][xy];
257 
258  A = s->current_picture.motion_val[0][xy - 1];
259  B = s->current_picture.motion_val[0][xy - wrap];
260  C = s->current_picture.motion_val[0][xy + 2 - wrap];
261 
262  if (s->mb_x && !s->first_slice_line && !s->mspel && w->top_left_mv_flag)
263  diff = FFMAX(FFABS(A[0] - B[0]), FFABS(A[1] - B[1]));
264  else
265  diff = 0;
266 
267  if (diff >= 8)
268  type = get_bits1(&s->gb);
269  else
270  type = 2;
271 
272  if (type == 0) {
273  *px = A[0];
274  *py = A[1];
275  } else if (type == 1) {
276  *px = B[0];
277  *py = B[1];
278  } else {
279  /* special case for first (slice) line */
280  if (s->first_slice_line) {
281  *px = A[0];
282  *py = A[1];
283  } else {
284  *px = mid_pred(A[0], B[0], C[0]);
285  *py = mid_pred(A[1], B[1], C[1]);
286  }
287  }
288 
289  return mot_val;
290 }
291 
292 static inline int wmv2_decode_inter_block(Wmv2Context *w, int16_t *block,
293  int n, int cbp)
294 {
295  MpegEncContext *const s = &w->s;
296  static const int sub_cbp_table[3] = { 2, 3, 1 };
297  int sub_cbp, ret;
298 
299  if (!cbp) {
300  s->block_last_index[n] = -1;
301  return 0;
302  }
303 
304  if (w->per_block_abt)
305  w->abt_type = decode012(&s->gb);
306  w->abt_type_table[n] = w->abt_type;
307 
308  if (w->abt_type) {
309 // const uint8_t *scantable = w->abt_scantable[w->abt_type - 1].permutated;
310  const uint8_t *scantable = w->abt_scantable[w->abt_type - 1].scantable;
311 // const uint8_t *scantable = w->abt_type - 1 ? w->abt_scantable[1].permutated : w->abt_scantable[0].scantable;
312 
313  sub_cbp = sub_cbp_table[decode012(&s->gb)];
314 
315  if (sub_cbp & 1)
316  if ((ret = ff_msmpeg4_decode_block(s, block, n, 1, scantable)) < 0)
317  return ret;
318 
319  if (sub_cbp & 2)
320  if ((ret = ff_msmpeg4_decode_block(s, w->abt_block2[n], n, 1, scantable)) < 0)
321  return ret;
322 
323  s->block_last_index[n] = 63;
324 
325  return 0;
326  } else {
327  return ff_msmpeg4_decode_block(s, block, n, 1,
329  }
330 }
331 
333 {
334  Wmv2Context *const w = (Wmv2Context *) s;
335  int cbp, code, i, ret;
336  uint8_t *coded_val;
337 
338  if (w->j_type)
339  return 0;
340 
341  if (s->pict_type == AV_PICTURE_TYPE_P) {
342  if (IS_SKIP(s->current_picture.mb_type[s->mb_y * s->mb_stride + s->mb_x])) {
343  /* skip mb */
344  s->mb_intra = 0;
345  for (i = 0; i < 6; i++)
346  s->block_last_index[i] = -1;
347  s->mv_dir = MV_DIR_FORWARD;
348  s->mv_type = MV_TYPE_16X16;
349  s->mv[0][0][0] = 0;
350  s->mv[0][0][1] = 0;
351  s->mb_skipped = 1;
352  w->hshift = 0;
353  return 0;
354  }
355 
358  if (code < 0)
359  return AVERROR_INVALIDDATA;
360  s->mb_intra = (~code & 0x40) >> 6;
361 
362  cbp = code & 0x3f;
363  } else {
364  s->mb_intra = 1;
366  if (code < 0) {
368  "II-cbp illegal at %d %d\n", s->mb_x, s->mb_y);
369  return AVERROR_INVALIDDATA;
370  }
371  /* predict coded block pattern */
372  cbp = 0;
373  for (i = 0; i < 6; i++) {
374  int val = ((code >> (5 - i)) & 1);
375  if (i < 4) {
376  int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
377  val = val ^ pred;
378  *coded_val = val;
379  }
380  cbp |= val << (5 - i);
381  }
382  }
383 
384  if (!s->mb_intra) {
385  int mx, my;
386  wmv2_pred_motion(w, &mx, &my);
387 
388  if (cbp) {
389  s->bdsp.clear_blocks(s->block[0]);
390  if (s->per_mb_rl_table) {
391  s->rl_table_index = decode012(&s->gb);
393  }
394 
395  if (w->abt_flag && w->per_mb_abt) {
396  w->per_block_abt = get_bits1(&s->gb);
397  if (!w->per_block_abt)
398  w->abt_type = decode012(&s->gb);
399  } else
400  w->per_block_abt = 0;
401  }
402 
403  if ((ret = wmv2_decode_motion(w, &mx, &my)) < 0)
404  return ret;
405 
406  s->mv_dir = MV_DIR_FORWARD;
407  s->mv_type = MV_TYPE_16X16;
408  s->mv[0][0][0] = mx;
409  s->mv[0][0][1] = my;
410 
411  for (i = 0; i < 6; i++) {
412  if ((ret = wmv2_decode_inter_block(w, block[i], i, (cbp >> (5 - i)) & 1)) < 0) {
414  "\nerror while decoding inter block: %d x %d (%d)\n",
415  s->mb_x, s->mb_y, i);
416  return ret;
417  }
418  }
419  } else {
420  if (s->pict_type == AV_PICTURE_TYPE_P)
421  ff_dlog(s->avctx, "%d%d ", s->inter_intra_pred, cbp);
422  ff_dlog(s->avctx, "I at %d %d %d %06X\n", s->mb_x, s->mb_y,
423  ((cbp & 3) ? 1 : 0) + ((cbp & 0x3C) ? 2 : 0),
424  show_bits(&s->gb, 24));
425  s->ac_pred = get_bits1(&s->gb);
426  if (s->inter_intra_pred) {
429  ff_dlog(s->avctx, "%d%d %d %d/",
430  s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
431  }
432  if (s->per_mb_rl_table && cbp) {
433  s->rl_table_index = decode012(&s->gb);
435  }
436 
437  s->bdsp.clear_blocks(s->block[0]);
438  for (i = 0; i < 6; i++) {
439  if ((ret = ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL)) < 0) {
441  "\nerror while decoding intra block: %d x %d (%d)\n",
442  s->mb_x, s->mb_y, i);
443  return ret;
444  }
445  }
446  }
447 
448  return 0;
449 }
450 
452 {
453  Wmv2Context *const w = avctx->priv_data;
454  int ret;
455 
456  avctx->flags |= CODEC_FLAG_EMU_EDGE;
457 
458  if ((ret = ff_msmpeg4_decode_init(avctx)) < 0)
459  return ret;
460 
462 
463  ff_intrax8_common_init(&w->x8, &w->s);
464 
465  return 0;
466 }
467 
469 {
470  Wmv2Context *w = avctx->priv_data;
471 
473  return ff_h263_decode_end(avctx);
474 }
475 
477  .name = "wmv2",
478  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 8"),
479  .type = AVMEDIA_TYPE_VIDEO,
480  .id = AV_CODEC_ID_WMV2,
481  .priv_data_size = sizeof(Wmv2Context),
483  .close = wmv2_decode_end,
485  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
486  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
487  AV_PIX_FMT_NONE },
488 };