FFmpeg
Loading...
Searching...
No Matches
msmpeg4dec.c
Go to the documentation of this file.
1/*
2 * MSMPEG4 backend for encoder and decoder
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2013 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include "libavutil/thread.h"
26
27#include "avcodec.h"
28#include "codec_internal.h"
29#include "mpegutils.h"
30#include "mpegvideo.h"
31#include "mpegvideodec.h"
32#include "msmpeg4.h"
33#include "msmpeg4dec.h"
34#include "libavutil/imgutils.h"
35#include "h263.h"
36#include "h263data.h"
37#include "h263dec.h"
38#include "mpeg4videodec.h"
39#include "msmpeg4data.h"
40#include "msmpeg4_vc1_data.h"
41
42#define V2_INTRA_CBPC_VLC_BITS 3
43#define V2_MB_TYPE_VLC_BITS 7
44#define MV_VLC_BITS 9
45#define TEX_VLC_BITS 9
46
47#define DEFAULT_INTER_INDEX 3
48
49static const VLCElem *mv_tables[2];
50
51static inline int msmpeg4v1_pred_dc(H263DecContext *const h, int n,
52 int32_t **dc_val_ptr)
53{
54 int i;
55
56 if (n < 4) {
57 i= 0;
58 } else {
59 i= n-3;
60 }
61
62 *dc_val_ptr= &h->last_dc[i];
63 return h->last_dc[i];
64}
65
66/****************************************/
67/* decoding stuff */
68
75
76/* This is identical to H.263 except that its range is multiplied by 2. */
77static int msmpeg4v2_decode_motion(H263DecContext *const h, int pred, int f_code)
78{
79 int code, val, sign, shift;
80
82 ff_dlog(h->c.avctx, "MV code %d at %d %d pred: %d\n",
83 code, h->c.mb_x,h->c.mb_y, pred);
84 if (code < 0)
85 return 0xffff;
86
87 if (code == 0)
88 return pred;
89 sign = get_bits1(&h->gb);
90 shift = f_code - 1;
91 val = code;
92 if (shift) {
93 val = (val - 1) << shift;
94 val |= get_bits(&h->gb, shift);
95 val++;
96 }
97 if (sign)
98 val = -val;
99
100 val += pred;
101 if (val <= -64)
102 val += 64;
103 else if (val >= 64)
104 val -= 64;
105
106 return val;
107}
108
110{
111 MSMP4DecContext *const ms = mpv_to_msmpeg4(h);
112 int cbp, code, i;
113 uint32_t * const mb_type_ptr = &h->c.cur_pic.mb_type[h->c.mb_x + h->c.mb_y*h->c.mb_stride];
114
115 if (h->c.pict_type == AV_PICTURE_TYPE_P) {
116 if (ms->use_skip_mb_code) {
117 if (get_bits1(&h->gb)) {
118 /* skip mb */
119 h->c.mb_intra = 0;
120 for(i=0;i<6;i++)
121 h->c.block_last_index[i] = -1;
122 h->c.mv_dir = MV_DIR_FORWARD;
123 h->c.mv_type = MV_TYPE_16X16;
124 h->c.mv[0][0][0] = 0;
125 h->c.mv[0][0][1] = 0;
126 h->c.mb_skipped = 1;
128 return 0;
129 }
130 }
131
132 if (h->c.msmpeg4_version == MSMP4_V2)
134 else
136 if(code<0 || code>7){
137 av_log(h->c.avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n",
138 code, h->c.mb_x, h->c.mb_y);
139 return -1;
140 }
141
142 h->c.mb_intra = code >>2;
143
144 cbp = code & 0x3;
145 } else {
146 h->c.mb_intra = 1;
147 if (h->c.msmpeg4_version == MSMP4_V2)
149 else
151 if(cbp<0 || cbp>3){
152 av_log(h->c.avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n",
153 cbp, h->c.mb_x, h->c.mb_y);
154 return -1;
155 }
156 }
157
158 if (!h->c.mb_intra) {
159 int mx, my, cbpy;
160
161 cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
162 if(cbpy<0){
163 av_log(h->c.avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n",
164 cbp, h->c.mb_x, h->c.mb_y);
165 return -1;
166 }
167
168 cbp|= cbpy<<2;
169 if (h->c.msmpeg4_version == MSMP4_V1 || (cbp&3) != 3)
170 cbp ^= 0x3C;
171
172 ff_h263_pred_motion(&h->c, 0, 0, &mx, &my);
175
176 h->c.mv_dir = MV_DIR_FORWARD;
177 h->c.mv_type = MV_TYPE_16X16;
178 h->c.mv[0][0][0] = mx;
179 h->c.mv[0][0][1] = my;
180 *mb_type_ptr = MB_TYPE_FORWARD_MV | MB_TYPE_16x16;
181 } else {
182 int v;
183 if (h->c.msmpeg4_version == MSMP4_V2) {
184 h->c.ac_pred = get_bits1(&h->gb);
186 if (v < 0) {
187 av_log(h->c.avctx, AV_LOG_ERROR, "cbpy vlc invalid\n");
188 return -1;
189 }
190 cbp|= v<<2;
191 } else{
192 h->c.ac_pred = 0;
194 if (v < 0) {
195 av_log(h->c.avctx, AV_LOG_ERROR, "cbpy vlc invalid\n");
196 return -1;
197 }
198 cbp|= v<<2;
199 if (h->c.pict_type==AV_PICTURE_TYPE_P) cbp^=0x3C;
200 }
201 *mb_type_ptr = MB_TYPE_INTRA;
202 }
203
204 h->c.bdsp.clear_blocks(h->block[0]);
205 for (i = 0; i < 6; i++) {
206 if (ff_msmpeg4_decode_block(ms, h->block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
207 {
208 av_log(h->c.avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n",
209 h->c.mb_x, h->c.mb_y, i);
210 return -1;
211 }
212 }
213 return 0;
214}
215
217{
218 MSMP4DecContext *const ms = mpv_to_msmpeg4(h);
219 int cbp, code, i;
220 uint8_t *coded_val;
221 uint32_t * const mb_type_ptr = &h->c.cur_pic.mb_type[h->c.mb_x + h->c.mb_y*h->c.mb_stride];
222
223 if (get_bits_left(&h->gb) <= 0)
224 return AVERROR_INVALIDDATA;
225
226 if (h->c.pict_type == AV_PICTURE_TYPE_P) {
227 if (ms->use_skip_mb_code) {
228 if (get_bits1(&h->gb)) {
229 /* skip mb */
230 h->c.mb_intra = 0;
231 for(i=0;i<6;i++)
232 h->c.block_last_index[i] = -1;
233 h->c.mv_dir = MV_DIR_FORWARD;
234 h->c.mv_type = MV_TYPE_16X16;
235 h->c.mv[0][0][0] = 0;
236 h->c.mv[0][0][1] = 0;
237 h->c.mb_skipped = 1;
239
240 return 0;
241 }
242 }
243
245 //h->c.mb_intra = (code & 0x40) ? 0 : 1;
246 h->c.mb_intra = (~code & 0x40) >> 6;
247
248 cbp = code & 0x3f;
249 } else {
250 h->c.mb_intra = 1;
252 /* predict coded block pattern */
253 cbp = 0;
254 for(i=0;i<6;i++) {
255 int val = ((code >> (5 - i)) & 1);
256 if (i < 4) {
257 int pred = ff_msmpeg4_coded_block_pred(&h->c, i, &coded_val);
258 val = val ^ pred;
259 *coded_val = val;
260 }
261 cbp |= val << (5 - i);
262 }
263 }
264
265 if (!h->c.mb_intra) {
266 int mx, my;
267 if (ms->per_mb_rl_table && cbp) {
268 ms->rl_table_index = decode012(&h->gb);
270 }
271 ff_h263_pred_motion(&h->c, 0, 0, &mx, &my);
273 h->c.mv_dir = MV_DIR_FORWARD;
274 h->c.mv_type = MV_TYPE_16X16;
275 h->c.mv[0][0][0] = mx;
276 h->c.mv[0][0][1] = my;
277 *mb_type_ptr = MB_TYPE_FORWARD_MV | MB_TYPE_16x16;
278 } else {
279 ff_dlog(h->c.avctx, "I at %d %d %d %06X\n", h->c.mb_x, h->c.mb_y,
280 ((cbp & 3) ? 1 : 0) +((cbp & 0x3C)? 2 : 0),
281 show_bits(&h->gb, 24));
282 h->c.ac_pred = get_bits1(&h->gb);
283 *mb_type_ptr = MB_TYPE_INTRA;
284 if (h->c.inter_intra_pred) {
285 h->c.h263_aic_dir = get_vlc2(&h->gb, ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 1);
286 ff_dlog(h->c.avctx, "%d%d %d %d/",
287 h->c.ac_pred, h->c.h263_aic_dir, h->c.mb_x, h->c.mb_y);
288 }
289 if (ms->per_mb_rl_table && cbp) {
290 ms->rl_table_index = decode012(&h->gb);
292 }
293 }
294
295 h->c.bdsp.clear_blocks(h->block[0]);
296 for (i = 0; i < 6; i++) {
297 if (ff_msmpeg4_decode_block(ms, h->block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
298 {
299 av_log(h->c.avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n",
300 h->c.mb_x, h->c.mb_y, i);
301 return -1;
302 }
303 }
304
305 return 0;
306}
307
308/* init all vlc decoding tables */
310{
311 static VLCElem vlc_buf[3714 + 2694 + 1636 + 2648 + 1532 + 2488];
313
317 VLC_INIT_RL(ff_rl_table[3], 940);
318 VLC_INIT_RL(ff_rl_table[4], 962);
319 /* ff_rl_table[5] coincides with ff_h263_rl_inter which has just been
320 * initialized in ff_h263_decode_init() earlier. So just copy the VLCs. */
321 av_assert1(ff_h263_rl_inter.rl_vlc[0]);
322 memcpy(ff_rl_table[5].rl_vlc, ff_h263_rl_inter.rl_vlc, sizeof(ff_rl_table[5].rl_vlc));
323
325 &ff_v2_dc_lum_table[0][1], 8, 4,
326 &ff_v2_dc_lum_table[0][0], 8, 4, 0);
328 &ff_v2_dc_chroma_table[0][1], 8, 4,
329 &ff_v2_dc_chroma_table[0][0], 8, 4, 0);
330
332 &ff_v2_intra_cbpc[0][1], 2, 1,
333 &ff_v2_intra_cbpc[0][0], 2, 1, 0);
335 &ff_v2_mb_type[0][1], 2, 1,
336 &ff_v2_mb_type[0][0], 2, 1, 0);
337
341 ff_msmp4_mv_table0, 2, 2,
342 0, 0);
346 ff_msmp4_mv_table1, 2, 2,
347 0, 0);
348
349 for (unsigned i = 0; i < 4; i++) {
352 &ff_wmv2_inter_table[i][0][1], 8, 4,
353 &ff_wmv2_inter_table[i][0][0], 8, 4,
354 NULL, 0, 0, 0);
355 }
356
358 &ff_table_inter_intra[0][1], 2, 1,
359 &ff_table_inter_intra[0][0], 2, 1, 0);
361}
362
364{
365 MSMP4DecContext *const ms = mpv_to_msmpeg4(h);
366 int code;
367
368 // at minimum one bit per macroblock is required at least in a valid frame,
369 // we discard frames much smaller than this. Frames smaller than 1/8 of the
370 // smallest "black/skip" frame generally contain not much recoverable content
371 // while at the same time they have the highest computational requirements
372 // per byte
373 if (get_bits_left(&h->gb) * 8LL < (h->c.width+15)/16 * ((h->c.height+15)/16))
374 return AVERROR_INVALIDDATA;
375
376 if (h->c.msmpeg4_version == MSMP4_V1) {
377 int start_code = get_bits_long(&h->gb, 32);
378 if(start_code!=0x00000100){
379 av_log(h->c.avctx, AV_LOG_ERROR, "invalid startcode\n");
380 return -1;
381 }
382
383 skip_bits(&h->gb, 5); // frame number */
384 }
385
386 h->c.pict_type = get_bits(&h->gb, 2) + 1;
387 if (h->c.pict_type != AV_PICTURE_TYPE_I &&
388 h->c.pict_type != AV_PICTURE_TYPE_P){
389 av_log(h->c.avctx, AV_LOG_ERROR, "invalid picture type\n");
390 return -1;
391 }
392 h->c.chroma_qscale = h->c.qscale = get_bits(&h->gb, 5);
393 if (h->c.qscale == 0) {
394 av_log(h->c.avctx, AV_LOG_ERROR, "invalid qscale\n");
395 return -1;
396 }
397
398 if (h->c.pict_type == AV_PICTURE_TYPE_I) {
399 code = get_bits(&h->gb, 5);
400 if (h->c.msmpeg4_version == MSMP4_V1) {
401 if(code==0 || code>h->c.mb_height) {
402 av_log(h->c.avctx, AV_LOG_ERROR, "invalid slice height %d\n", code);
403 return -1;
404 }
405
406 h->slice_height = code;
407 }else{
408 /* 0x17: one slice, 0x18: two slices, ... */
409 if (code < 0x17){
410 av_log(h->c.avctx, AV_LOG_ERROR, "error, slice code was %X\n", code);
411 return -1;
412 }
413
414 h->slice_height = h->c.mb_height / (code - 0x16);
415 }
416
417 switch (h->c.msmpeg4_version) {
418 case MSMP4_V1:
419 case MSMP4_V2:
420 ms->rl_chroma_table_index = 2;
421 ms->rl_table_index = 2;
422
423 ms->dc_table_index = 0; //not used
424 break;
425 case MSMP4_V3:
427 ms->rl_table_index = decode012(&h->gb);
428
429 ms->dc_table_index = get_bits1(&h->gb);
430 break;
431 case MSMP4_WMV1:
432 ff_msmpeg4_decode_ext_header(h, (2+5+5+17+7)/8);
433
434 if (ms->bit_rate > MBAC_BITRATE)
435 ms->per_mb_rl_table = get_bits1(&h->gb);
436 else
437 ms->per_mb_rl_table = 0;
438
439 if (!ms->per_mb_rl_table) {
441 ms->rl_table_index = decode012(&h->gb);
442 }
443
444 ms->dc_table_index = get_bits1(&h->gb);
445 h->c.inter_intra_pred= 0;
446 break;
447 default:
448 av_unreachable("msmpeg4_decode_picture_header() only used by MSMP4V1-3, WMV1");
449 }
450 h->c.no_rounding = 1;
451 if (h->c.avctx->debug & FF_DEBUG_PICT_INFO)
452 av_log(h->c.avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n",
453 h->c.qscale,
455 ms->rl_table_index,
456 ms->dc_table_index,
457 ms->per_mb_rl_table,
458 h->slice_height);
459 } else {
460 switch (h->c.msmpeg4_version) {
461 case MSMP4_V1:
462 case MSMP4_V2:
463 if (h->c.msmpeg4_version == MSMP4_V1)
464 ms->use_skip_mb_code = 1;
465 else
466 ms->use_skip_mb_code = get_bits1(&h->gb);
467 ms->rl_table_index = 2;
469 ms->dc_table_index = 0; //not used
470 ms->mv_table_index = 0;
471 break;
472 case MSMP4_V3:
473 ms->use_skip_mb_code = get_bits1(&h->gb);
474 ms->rl_table_index = decode012(&h->gb);
476
477 ms->dc_table_index = get_bits1(&h->gb);
478
479 ms->mv_table_index = get_bits1(&h->gb);
480 break;
481 case MSMP4_WMV1:
482 ms->use_skip_mb_code = get_bits1(&h->gb);
483
484 if (ms->bit_rate > MBAC_BITRATE)
485 ms->per_mb_rl_table = get_bits1(&h->gb);
486 else
487 ms->per_mb_rl_table = 0;
488
489 if (!ms->per_mb_rl_table) {
490 ms->rl_table_index = decode012(&h->gb);
492 }
493
494 ms->dc_table_index = get_bits1(&h->gb);
495
496 ms->mv_table_index = get_bits1(&h->gb);
497 h->c.inter_intra_pred = h->c.width*h->c.height < 320*240 &&
498 ms->bit_rate <= II_BITRATE;
499 break;
500 default:
501 av_unreachable("msmpeg4_decode_picture_header() only used by MSMP4V1-3, WMV1");
502 }
503
504 if (h->c.avctx->debug&FF_DEBUG_PICT_INFO)
505 av_log(h->c.avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n",
507 ms->rl_table_index,
509 ms->dc_table_index,
510 ms->mv_table_index,
511 ms->per_mb_rl_table,
512 h->c.qscale);
513
514 if (ms->flipflop_rounding) {
515 h->c.no_rounding ^= 1;
516 }else{
517 h->c.no_rounding = 0;
518 }
519 }
520 ff_dlog(h->c.avctx, "%d %d %d %d %d\n", h->c.pict_type, ms->bit_rate,
521 h->c.inter_intra_pred, h->c.width, h->c.height);
522
523 ms->esc3_level_length = 0;
524 ms->esc3_run_length = 0;
525
526 return 0;
527}
528
530{
531 MSMP4DecContext *const ms = mpv_to_msmpeg4(h);
532 int left = buf_size*8 - get_bits_count(&h->gb);
533 int length = h->c.msmpeg4_version >= MSMP4_V3 ? 17 : 16;
534 /* the alt_bitstream reader could read over the end so we need to check it */
535 if(left>=length && left<length+8)
536 {
537 skip_bits(&h->gb, 5); /* fps */
538 ms->bit_rate = get_bits(&h->gb, 11) * 1024;
539 if (h->c.msmpeg4_version >= MSMP4_V3)
540 ms->flipflop_rounding = get_bits1(&h->gb);
541 else
542 ms->flipflop_rounding = 0;
543 }
544 else if(left<length+8)
545 {
546 ms->flipflop_rounding = 0;
547 if (h->c.msmpeg4_version != MSMP4_V2)
548 av_log(h->c.avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left);
549 }
550 else
551 {
552 av_log(h->c.avctx, AV_LOG_ERROR, "I-frame too long, ignoring ext header\n");
553 }
554
555 return 0;
556}
557
558static int msmpeg4_decode_dc(MSMP4DecContext *const ms, int n, int *dir_ptr)
559{
560 H263DecContext *const h = &ms->h;
561 int level, pred;
562
563 if (h->c.msmpeg4_version <= MSMP4_V2) {
564 if (n < 4) {
566 } else {
568 }
569 if (level < 0) {
570 av_log(h->c.avctx, AV_LOG_ERROR, "illegal dc vlc\n");
571 *dir_ptr = 0;
572 return -1;
573 }
574 level-=256;
575 } else {
576 level = get_vlc2(&h->gb, ff_msmp4_dc_vlc[ms->dc_table_index][n >= 4],
578
579 if (level == DC_MAX) {
580 level = get_bits(&h->gb, 8);
581 if (get_bits1(&h->gb))
582 level = -level;
583 } else if (level != 0) {
584 if (get_bits1(&h->gb))
585 level = -level;
586 }
587 }
588
589 if (h->c.msmpeg4_version == MSMP4_V1) {
590 int32_t *dc_val;
591 pred = msmpeg4v1_pred_dc(h, n, &dc_val);
592 level += pred;
593
594 /* update predictor */
595 *dc_val= level;
596 }else{
597 int16_t *dc_val;
598 pred = ff_msmpeg4_pred_dc(&h->c, n, &dc_val, dir_ptr);
599 level += pred;
600
601 /* update predictor */
602 if (n < 4) {
603 *dc_val = level * h->c.y_dc_scale;
604 } else {
605 *dc_val = level * h->c.c_dc_scale;
606 }
607 }
608
609 return level;
610}
611
613 int n, int coded, const uint8_t *scan_table)
614{
615 H263DecContext *const h = &ms->h;
616 int level, i, last, run, run_diff;
617 int dc_pred_dir = -1; //unused but its passed around, so it needs to be initialized
618 const RLTable *rl;
619 const RL_VLC_ELEM *rl_vlc;
620 int qmul, qadd;
621
622 if (h->c.mb_intra) {
623 qmul=1;
624 qadd=0;
625
626 /* DC coef */
627 level = msmpeg4_decode_dc(ms, n, &dc_pred_dir);
628
629 if (level < 0){
630 av_log(h->c.avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, h->c.qscale);
631 if (h->c.inter_intra_pred)
632 level = 0;
633 }
634 if (n < 4) {
635 rl = &ff_rl_table[ms->rl_table_index];
636 if (level > 256 * h->c.y_dc_scale) {
637 av_log(h->c.avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", h->c.qscale);
638 if (!h->c.inter_intra_pred)
639 return -1;
640 }
641 } else {
642 rl = &ff_rl_table[3 + ms->rl_chroma_table_index];
643 if (level > 256 * h->c.c_dc_scale) {
644 av_log(h->c.avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", h->c.qscale);
645 if (!h->c.inter_intra_pred)
646 return -1;
647 }
648 }
649 block[0] = level;
650
651 run_diff = h->c.msmpeg4_version >= MSMP4_WMV1;
652 i = 0;
653 if (!coded) {
654 goto not_coded;
655 }
656 if (h->c.ac_pred) {
657 if (dc_pred_dir == 0)
658 scan_table = h->permutated_intra_v_scantable; /* left */
659 else
660 scan_table = h->permutated_intra_h_scantable; /* top */
661 } else {
662 scan_table = h->c.intra_scantable.permutated;
663 }
664 rl_vlc= rl->rl_vlc[0];
665 } else {
666 qmul = h->c.qscale << 1;
667 qadd = (h->c.qscale - 1) | 1;
668 i = -1;
669 rl = &ff_rl_table[3 + ms->rl_table_index];
670
671 if (h->c.msmpeg4_version == MSMP4_V2)
672 run_diff = 0;
673 else
674 run_diff = 1;
675
676 if (!coded) {
677 h->c.block_last_index[n] = i;
678 return 0;
679 }
680 if(!scan_table)
681 scan_table = h->c.inter_scantable.permutated;
682 rl_vlc= rl->rl_vlc[h->c.qscale];
683 }
684 {
685 OPEN_READER(re, &h->gb);
686 for(;;) {
687 UPDATE_CACHE(re, &h->gb);
688 GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
689 if (level==0) {
690 int cache;
691 cache= GET_CACHE(re, &h->gb);
692 /* escape */
693 if (h->c.msmpeg4_version == MSMP4_V1 || (cache&0x80000000)==0) {
694 if (h->c.msmpeg4_version == MSMP4_V1 || (cache&0x40000000)==0) {
695 /* third escape */
696 if (h->c.msmpeg4_version != MSMP4_V1)
697 LAST_SKIP_BITS(re, &h->gb, 2);
698 UPDATE_CACHE(re, &h->gb);
699 if (h->c.msmpeg4_version <= MSMP4_V3) {
700 last = SHOW_UBITS(re, &h->gb, 1); SKIP_CACHE(re, &h->gb, 1);
701 run = SHOW_UBITS(re, &h->gb, 6); SKIP_CACHE(re, &h->gb, 6);
702 level = SHOW_SBITS(re, &h->gb, 8);
703 SKIP_COUNTER(re, &h->gb, 1 + 6 + 8);
704 }else{
705 int sign;
706 last = SHOW_UBITS(re, &h->gb, 1); SKIP_BITS(re, &h->gb, 1);
707 if (!ms->esc3_level_length) {
708 int ll;
709 ff_dlog(h->c.avctx, "ESC-3 %X at %d %d\n",
710 show_bits(&h->gb, 24), h->c.mb_x, h->c.mb_y);
711 if (h->c.qscale < 8) {
712 ll = SHOW_UBITS(re, &h->gb, 3); SKIP_BITS(re, &h->gb, 3);
713 if(ll==0){
714 ll = 8+SHOW_UBITS(re, &h->gb, 1); SKIP_BITS(re, &h->gb, 1);
715 }
716 }else{
717 ll=2;
718 while (ll < 8 && SHOW_UBITS(re, &h->gb, 1) == 0) {
719 ll++;
720 SKIP_BITS(re, &h->gb, 1);
721 }
722 if (ll<8) SKIP_BITS(re, &h->gb, 1);
723 }
724
725 ms->esc3_level_length = ll;
726 ms->esc3_run_length = SHOW_UBITS(re, &h->gb, 2) + 3; SKIP_BITS(re, &h->gb, 2);
727 UPDATE_CACHE(re, &h->gb);
728 }
729 run = SHOW_UBITS(re, &h->gb, ms->esc3_run_length);
730 SKIP_BITS(re, &h->gb, ms->esc3_run_length);
731
732 sign= SHOW_UBITS(re, &h->gb, 1);
733 SKIP_BITS(re, &h->gb, 1);
734
735 level = SHOW_UBITS(re, &h->gb, ms->esc3_level_length);
736 SKIP_BITS(re, &h->gb, ms->esc3_level_length);
737 if(sign) level= -level;
738 }
739
740 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
741 if (level>0) level= level * qmul + qadd;
742 else level= level * qmul - qadd;
743 i+= run + 1;
744 if(last) i+=192;
745 } else {
746 /* second escape */
747 SKIP_BITS(re, &h->gb, 2);
748 GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
749 i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing
750 level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1);
751 LAST_SKIP_BITS(re, &h->gb, 1);
752 }
753 } else {
754 /* first escape */
755 SKIP_BITS(re, &h->gb, 1);
756 GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
757 i+= run;
758 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
759 level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1);
760 LAST_SKIP_BITS(re, &h->gb, 1);
761 }
762 } else {
763 i+= run;
764 level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1);
765 LAST_SKIP_BITS(re, &h->gb, 1);
766 }
767 if (i > 62){
768 i-= 192;
769 if(i&(~63)){
770 const int left = get_bits_left(&h->gb);
771 if (((i + 192 == 64 && level / qmul == -1) ||
772 !(h->c.avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))) &&
773 left >= 0) {
774 av_log(h->c.avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n",
775 h->c.mb_x, h->c.mb_y);
776 i = 63;
777 break;
778 }else{
779 av_log(h->c.avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n",
780 h->c.mb_x, h->c.mb_y);
781 return -1;
782 }
783 }
784
785 block[scan_table[i]] = level;
786 break;
787 }
788
789 block[scan_table[i]] = level;
790 }
791 CLOSE_READER(re, &h->gb);
792 }
793 if (h->c.mb_intra) {
794 not_coded:
795 ff_mpeg4_pred_ac(h, block, n, dc_pred_dir);
796 }
797 h->c.block_last_index[n] = i;
798
799 return 0;
800}
801
802void ff_msmpeg4_decode_motion(MSMP4DecContext *const ms, int *mx_ptr, int *my_ptr)
803{
804 const VLCElem *const mv_vlc = mv_tables[ms->mv_table_index];
805 H263DecContext *const h = &ms->h;
806 int sym, mx, my;
807
808 sym = get_vlc2(&h->gb, mv_vlc, MV_VLC_BITS, 2);
809 if (sym) {
810 mx = sym >> 8;
811 my = sym & 0xFF;
812 } else {
813 /* Escape */
814 mx = get_bits(&h->gb, 6);
815 my = get_bits(&h->gb, 6);
816 }
817
818 mx += *mx_ptr - 32;
819 my += *my_ptr - 32;
820 /* WARNING : they do not do exactly modulo encoding */
821 if (mx <= -64)
822 mx += 64;
823 else if (mx >= 64)
824 mx -= 64;
825
826 if (my <= -64)
827 my += 64;
828 else if (my >= 64)
829 my -= 64;
830 *mx_ptr = mx;
831 *my_ptr = my;
832}
833
835{
836 static AVOnce init_static_once = AV_ONCE_INIT;
837 H263DecContext *const h = avctx->priv_data;
838 int ret;
839
840 ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
841 if (ret < 0)
842 return ret;
843
844 if (ff_h263_decode_init(avctx) < 0)
845 return -1;
846
847 // We unquantize inter blocks as we parse them.
848 h->c.dct_unquantize_inter = NULL;
849
850 h->decode_header = msmpeg4_decode_picture_header;
851
852 ff_msmpeg4_common_init(&h->c, h->permutated_intra_h_scantable,
853 h->permutated_intra_v_scantable);
854
855 switch (h->c.msmpeg4_version) {
856 case MSMP4_V1:
857 case MSMP4_V2:
858 h->decode_mb = msmpeg4v12_decode_mb;
859 break;
860 case MSMP4_V3:
861 case MSMP4_WMV1:
862 h->decode_mb = msmpeg4v34_decode_mb;
863 break;
864 case MSMP4_WMV2:
865 break;
866 default:
867 av_unreachable("List contains all cases using ff_msmpeg4_decode_init()");
868 }
869
870 h->slice_height = h->c.mb_height; //to avoid 1/0 if the first frame is not a keyframe
871
872 ff_thread_once(&init_static_once, msmpeg4_decode_init_static);
873
874 return 0;
875}
876
878 .p.name = "msmpeg4v1",
879 CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 1"),
880 .p.type = AVMEDIA_TYPE_VIDEO,
881 .p.id = AV_CODEC_ID_MSMPEG4V1,
882 .priv_data_size = sizeof(MSMP4DecContext),
885 .close = ff_mpv_decode_close,
887 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
889 .p.max_lowres = 3,
890};
891
893 .p.name = "msmpeg4v2",
894 CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 2"),
895 .p.type = AVMEDIA_TYPE_VIDEO,
896 .p.id = AV_CODEC_ID_MSMPEG4V2,
897 .priv_data_size = sizeof(MSMP4DecContext),
900 .close = ff_mpv_decode_close,
902 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
904 .p.max_lowres = 3,
905};
906
908 .p.name = "msmpeg4",
909 CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 3"),
910 .p.type = AVMEDIA_TYPE_VIDEO,
911 .p.id = AV_CODEC_ID_MSMPEG4V3,
912 .priv_data_size = sizeof(MSMP4DecContext),
915 .close = ff_mpv_decode_close,
917 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
919 .p.max_lowres = 3,
920};
921
923 .p.name = "wmv1",
924 CODEC_LONG_NAME("Windows Media Video 7"),
925 .p.type = AVMEDIA_TYPE_VIDEO,
926 .p.id = AV_CODEC_ID_WMV1,
927 .priv_data_size = sizeof(MSMP4DecContext),
930 .close = ff_mpv_decode_close,
932 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
934 .p.max_lowres = 3,
935};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t my
Definition dsp.h:57
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t mx
Definition dsp.h:57
static double val(void *priv, double ch)
Definition aeval.c:77
const FFCodec ff_wmv1_decoder
Definition msmpeg4dec.c:922
const FFCodec ff_msmpeg4v2_decoder
Definition msmpeg4dec.c:892
const FFCodec ff_msmpeg4v1_decoder
Definition msmpeg4dec.c:877
const FFCodec ff_msmpeg4v3_decoder
Definition msmpeg4dec.c:907
int32_t
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition avassert.h:109
Libavcodec external API header.
#define FF_DEBUG_PICT_INFO
Definition avcodec.h:1393
static int BS_FUNC decode012(BSCTX *bc)
Return decoded truncated unary code for the values 0, 1, 2.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define NULL
Definition coverity.c:32
static int16_t block[64]
Definition dct.c:125
#define AV_EF_BITSTREAM
detect bitstream specification deviations
Definition defs.h:49
#define AV_EF_COMPLIANT
consider all spec non compliances as errors
Definition defs.h:55
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define TEX_VLC_BITS
Definition dvdec.c:146
static struct @346255127015250356166251341105367306144006377143 state
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition get_bits.h:424
#define GET_CACHE(name, gb)
Definition get_bits.h:251
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition get_bits.h:645
#define SKIP_CACHE(name, gb, num)
Definition get_bits.h:216
#define CLOSE_READER(name, gb)
Definition get_bits.h:189
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
#define SHOW_UBITS(name, gb, num)
Definition get_bits.h:247
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
#define SHOW_SBITS(name, gb, num)
Definition get_bits.h:248
#define OPEN_READER(name, gb)
Definition get_bits.h:182
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
#define SKIP_BITS(name, gb, num)
Definition get_bits.h:229
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)
Definition get_bits.h:602
#define UPDATE_CACHE(name, gb)
Definition get_bits.h:213
#define LAST_SKIP_BITS(name, gb, num)
Definition get_bits.h:235
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define SKIP_COUNTER(name, gb, num)
Definition get_bits.h:223
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition get_bits.h:373
#define AV_CODEC_CAP_DRAW_HORIZ_BAND
Decoder can use draw_horiz_band callback.
Definition codec.h:41
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_MSMPEG4V1
Definition codec_id.h:64
@ AV_CODEC_ID_MSMPEG4V2
Definition codec_id.h:65
@ AV_CODEC_ID_WMV1
Definition codec_id.h:67
@ AV_CODEC_ID_MSMPEG4V3
Definition codec_id.h:66
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition imgutils.c:318
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
Definition h263.c:182
RLTable ff_h263_rl_inter
Definition h263data.c:159
H.263 tables.
VLCElem ff_h263_inter_MCBPC_vlc[]
Definition ituh263dec.c:103
VLCElem ff_h263_mv_vlc[]
Definition ituh263dec.c:105
#define CBPY_VLC_BITS
Definition h263dec.h:35
VLCElem ff_h263_cbpy_vlc[]
Definition ituh263dec.c:104
#define INTRA_MCBPC_VLC_BITS
Definition h263dec.h:33
#define H263_MV_VLC_BITS
Definition h263dec.h:32
VLCElem ff_h263_intra_MCBPC_vlc[]
Definition ituh263dec.c:102
#define INTER_MCBPC_VLC_BITS
Definition h263dec.h:34
misc image utilities
static int shift(int a, int b)
Definition bonk.c:261
av_cold int ff_h263_decode_init(AVCodecContext *avctx)
Definition h263dec.c:94
int ff_h263_decode_frame(AVCodecContext *avctx, AVFrame *pict, int *got_frame, AVPacket *avpkt)
Definition h263dec.c:443
#define av_cold
Definition attributes.h:117
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
static const VLCElem * rl_vlc[2]
Definition mobiclip.c:279
static const VLCElem * mv_vlc[2][16]
Definition mobiclip.c:280
#define MV_VLC_BITS
Definition mpeg12vlc.h:34
void ff_mpeg4_pred_ac(H263DecContext *const h, int16_t *block, int n, int dir)
Predict the ac.
#define MB_TYPE_SKIP
Definition mpegutils.h:61
#define MB_TYPE_INTRA
Definition mpegutils.h:64
#define MB_TYPE_16x16
Definition mpegutils.h:41
#define MB_TYPE_FORWARD_MV
Definition mpegutils.h:49
mpegvideo header.
#define MV_DIR_FORWARD
Definition mpegvideo.h:168
#define MV_TYPE_16X16
1 vector for the whole mb
Definition mpegvideo.h:172
av_cold int ff_mpv_decode_close(AVCodecContext *avctx)
mpegvideo decoder header.
int ff_msmpeg4_pred_dc(MpegEncContext *s, int n, int16_t **dc_val_ptr, int *dir_ptr)
Definition msmpeg4.c:202
int ff_msmpeg4_coded_block_pred(MpegEncContext *s, int n, uint8_t **coded_block_ptr)
Definition msmpeg4.c:162
av_cold void ff_msmpeg4_common_init(MPVContext *const s, uint8_t permutated_intra_h_scantable[64], uint8_t permutated_intra_v_scantable[64])
Definition msmpeg4.c:119
#define II_BITRATE
Definition msmpeg4.h:29
#define DC_MAX
Definition msmpeg4.h:32
#define MBAC_BITRATE
Definition msmpeg4.h:30
av_cold void ff_msmp4_vc1_vlcs_init_once(void)
VLCElem ff_msmp4_mb_i_vlc[536]
const VLCElem * ff_msmp4_dc_vlc[2][2]
#define MSMP4_DC_VLC_BITS
#define MSMP4_MB_INTRA_VLC_BITS
const uint16_t ff_msmp4_mv_table0[MSMPEG4_MV_TABLES_NB_ELEMS]
The entries are of the form (8 << mvx) | mvy. Escape value is zero.
uint32_t ff_v2_dc_chroma_table[512][2]
Definition msmpeg4data.c:36
const uint8_t ff_msmp4_mv_table0_lens[MSMPEG4_MV_TABLES_NB_ELEMS]
const uint16_t ff_msmp4_mv_table1[MSMPEG4_MV_TABLES_NB_ELEMS]
uint32_t ff_v2_dc_lum_table[512][2]
Definition msmpeg4data.c:35
RLTable ff_rl_table[NB_RL_TABLES]
const uint8_t ff_v2_mb_type[8][2]
const uint8_t ff_table_inter_intra[4][2]
const uint32_t(*const [WMV2_INTER_CBP_TABLE_COUNT] ff_wmv2_inter_table)[2]
const uint8_t ff_msmp4_mv_table1_lens[MSMPEG4_MV_TABLES_NB_ELEMS]
const uint8_t ff_v2_intra_cbpc[4][2]
MSMPEG4 data tables.
#define MSMPEG4_MV_TABLES_NB_ELEMS
Definition msmpeg4data.h:51
static av_cold void msmpeg4_decode_init_static(void)
Definition msmpeg4dec.c:309
static int msmpeg4v34_decode_mb(H263DecContext *const h)
Definition msmpeg4dec.c:216
av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx)
Definition msmpeg4dec.c:834
static VLCElem v2_dc_chroma_vlc[1506]
Definition msmpeg4dec.c:71
static const VLCElem * mv_tables[2]
Definition msmpeg4dec.c:49
static int msmpeg4v1_pred_dc(H263DecContext *const h, int n, int32_t **dc_val_ptr)
Definition msmpeg4dec.c:51
int ff_msmpeg4_decode_block(MSMP4DecContext *const ms, int16_t *block, int n, int coded, const uint8_t *scan_table)
Definition msmpeg4dec.c:612
static int msmpeg4_decode_picture_header(H263DecContext *const h)
Definition msmpeg4dec.c:363
#define DEFAULT_INTER_INDEX
Definition msmpeg4dec.c:47
static VLCElem v2_mb_type_vlc[128]
Definition msmpeg4dec.c:73
static VLCElem v2_dc_lum_vlc[1472]
Definition msmpeg4dec.c:70
#define V2_INTRA_CBPC_VLC_BITS
Definition msmpeg4dec.c:42
VLCElem ff_inter_intra_vlc[8]
Definition msmpeg4dec.c:74
void ff_msmpeg4_decode_motion(MSMP4DecContext *const ms, int *mx_ptr, int *my_ptr)
Definition msmpeg4dec.c:802
int ff_msmpeg4_decode_ext_header(H263DecContext *const h, int buf_size)
Definition msmpeg4dec.c:529
static int msmpeg4v12_decode_mb(H263DecContext *const h)
Definition msmpeg4dec.c:109
#define V2_MB_TYPE_VLC_BITS
Definition msmpeg4dec.c:43
static VLCElem v2_intra_cbpc_vlc[8]
Definition msmpeg4dec.c:72
static int msmpeg4_decode_dc(MSMP4DecContext *const ms, int n, int *dir_ptr)
Definition msmpeg4dec.c:558
const VLCElem * ff_mb_non_intra_vlc[4]
Definition msmpeg4dec.c:69
static int msmpeg4v2_decode_motion(H263DecContext *const h, int pred, int f_code)
Definition msmpeg4dec.c:77
#define INTER_INTRA_VLC_BITS
Definition msmpeg4dec.h:29
#define MB_NON_INTRA_VLC_BITS
Definition msmpeg4dec.h:30
static MSMP4DecContext * mpv_to_msmpeg4(H263DecContext *const h)
Definition msmpeg4dec.h:46
#define VLC_INIT_RL(rl, static_size)
Definition rl.h:83
#define INIT_FIRST_VLC_RL(rl, static_size)
Definition rl.h:93
static const float pred[4]
Definition siprdata.h:259
const uint8_t * code
Definition spdifenc.c:433
main external API structure.
Definition avcodec.h:443
int width
picture width / height.
Definition avcodec.h:604
void * priv_data
Definition avcodec.h:470
int rl_chroma_table_index
Definition msmpeg4dec.h:38
H263DecContext h
Definition msmpeg4dec.h:33
RLTable.
Definition rl.h:39
int8_t * max_run[2]
encoding & decoding
Definition rl.h:47
RL_VLC_ELEM * rl_vlc[32]
decoding only
Definition rl.h:48
int8_t * max_level[2]
encoding & decoding
Definition rl.h:46
Definition vlc.h:32
For static VLCs, the number of bits can often be hardcoded at each get_vlc2() callsite.
Definition vlc.h:220
uint8_t run
Definition svq3.c:207
uint8_t level
Definition svq3.c:208
#define ff_dlog(a,...)
#define av_log(a,...)
static const uint8_t start_code[]
av_cold const VLCElem * ff_vlc_init_tables_from_lengths(VLCInitState *state, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags)
Definition vlc.c:366
av_cold const VLCElem * ff_vlc_init_tables_sparse(VLCInitState *state, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
Definition vlc.c:400
#define VLC_INIT_STATIC_TABLE(vlc_table, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition vlc.h:278
#define VLC_INIT_STATE(_table)
Definition vlc.h:225
VLCElem RL_VLC_ELEM
Definition vlc.h:48