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