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