Go to the documentation of this file. 1 /*
2 * H.263 decoder
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * H.263 decoder.
26 */
27
28 #define UNCHECKED_BITSTREAM_READER 1
29
30 #include "config_components.h"
31
50
52 {
53 /* MPEG-4 Studio Profile only, not supported by hardware */
57 }
58
63 }
64
66 }
67
69 {
72
74
75 // set defaults
77
78 s->quant_precision = 5;
81
82 /* select sub codec */
87 break;
89 break;
92 s->msmpeg4_version = 1;
93 break;
96 s->msmpeg4_version = 2;
97 break;
100 s->msmpeg4_version = 3;
101 break;
104 s->msmpeg4_version = 4;
105 break;
108 s->msmpeg4_version = 5;
109 break;
111 break;
114 break;
115 default:
119 }
120
124
125 /* for H.263, we allocate the images after having read the header */
133 }
134
137
138 return 0;
139 }
140
142 {
144
146 return 0;
147 }
148
149 /**
150 * Return the number of bytes consumed for building the current frame.
151 */
153 {
155
156 if (
s->divx_packed ||
s->avctx->hwaccel) {
157 /* We would have to scan through the whole buf to handle the weird
158 * reordering ... */
159 return buf_size;
160 } else {
161 // avoid infinite loops (maybe not needed...)
164 // oops ;)
165 if (
pos + 10 > buf_size)
167
169 }
170 }
171
173 {
174 const int part_mask =
s->partitioned_frame
176 const int mb_size = 16 >>
s->avctx->lowres;
178
179 s->last_resync_gb =
s->gb;
180 s->first_slice_line = 1;
181 s->resync_mb_x =
s->mb_x;
182 s->resync_mb_y =
s->mb_y;
183
185
186 if (
s->studio_profile) {
189 }
190
191 if (
s->avctx->hwaccel) {
193 ret =
s->avctx->hwaccel->decode_slice(
s->avctx, start,
s->gb.buffer_end - start);
194 // ensure we exit decode loop
195 s->mb_y =
s->mb_height;
197 }
198
199 if (
s->partitioned_frame) {
200 const int qscale =
s->qscale;
201
205
206 /* restore variables which were modified */
207 s->first_slice_line = 1;
208 s->mb_x =
s->resync_mb_x;
209 s->mb_y =
s->resync_mb_y;
211 }
212
213 for (;
s->mb_y <
s->mb_height;
s->mb_y++) {
214 /* per-row end of slice checks */
215 if (
s->msmpeg4_version) {
216 if (
s->resync_mb_y +
s->slice_height ==
s->mb_y) {
219
220 return 0;
221 }
222 }
223
224 if (
s->msmpeg4_version == 1) {
228 }
229
231 for (;
s->mb_x <
s->mb_width;
s->mb_x++) {
233
235 s->avctx->lowres,
s->chroma_x_shift);
236
237 if (
s->resync_mb_x ==
s->mb_x &&
s->resync_mb_y + 1 ==
s->mb_y)
238 s->first_slice_line = 0;
239
240 /* DCT & quantize */
241
246
248 ret =
s->decode_mb(
s,
s->block);
249
252
254 const int xy =
s->mb_x +
s->mb_y *
s->mb_stride;
259
262
263 s->padding_bug_score--;
264
265 if (++
s->mb_x >=
s->mb_width) {
270 }
271 return 0;
274 "Slice mismatch at MB: %d\n", xy);
276 s->mb_x + 1,
s->mb_y,
279 }
283
285 continue;
287 }
288
292 }
293
296
298 }
299
301
302 // Detect incorrect padding with wrong stuffing codes used by NEC N-02B
307 !
s->data_partitioning)
308 s->padding_bug_score += 32;
309
310 /* try to detect the padding bug */
315 !
s->data_partitioning) {
317 const int bits_left =
s->gb.size_in_bits - bits_count;
318
320 s->padding_bug_score += 16;
323 v |= 0x7F >> (7 - (bits_count & 7));
324
326 s->padding_bug_score--;
329 s->padding_bug_score += 4;
330 else
331 s->padding_bug_score++;
332 }
333 }
334
341 !
s->data_partitioning) {
342
343 s->padding_bug_score += 32;
344 }
345
349 AV_RB64(
s->gb.buffer_end - 8) == 0xCDCDCDCDFC7F0000) {
350
351 s->padding_bug_score += 32;
352 }
353
355 if (
356 (
s->padding_bug_score > -2 && !
s->data_partitioning))
358 else
360 }
361
362 // handle formats which don't have unique end markers
363 if (
s->msmpeg4_version || (
s->workaround_bugs &
FF_BUG_NO_PADDING)) {
// FIXME perhaps solve this more cleanly
365 int max_extra = 7;
366
367 /* no markers in M$ crap */
369 max_extra += 17;
370
371 /* buggy padding but the frame should still end approximately at
372 * the bitstream end */
375 max_extra += 48;
377 max_extra += 256 * 256 * 256 * 64;
378
379 if (
left > max_extra)
381 "discarding %d junk bits at end, next would be %X\n",
385 else
388
389 return 0;
390 }
391
393 "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
395
398
400 }
401
404 {
405 const uint8_t *buf = avpkt->
data;
406 int buf_size = avpkt->
size;
409 int slice_ret = 0;
410
411 /* no supplementary picture */
412 if (buf_size == 0) {
413 /* special case for last picture */
414 if (
s->low_delay == 0 &&
s->next_picture_ptr) {
417 s->next_picture_ptr =
NULL;
418
419 *got_frame = 1;
420 }
else if (
s->skipped_last_frame &&
s->current_picture_ptr) {
421 /* Output the last picture we decoded again if the stream ended with
422 * an NVOP */
425 /* Copy props from the last input packet. Otherwise, props from the last
426 * returned picture would be reused */
429 s->current_picture_ptr =
NULL;
430
431 *got_frame = 1;
432 }
433
434 return 0;
435 }
436
437 retry:
438 if (
s->divx_packed &&
s->bitstream_buffer_size) {
440 for(
i=0;
i < buf_size-3;
i++) {
441 if (buf[
i]==0 && buf[
i+1]==0 && buf[
i+2]==1) {
442 if (buf[
i+3]==0xB0) {
444 s->bitstream_buffer_size = 0;
445 }
446 break;
447 }
448 }
449 }
450
451 if (
s->bitstream_buffer_size && (
s->divx_packed || buf_size <=
MAX_NVOP_SIZE))
// divx 5.01+/xvid frame reorder
453 s->bitstream_buffer_size);
454 else
456
457 s->bitstream_buffer_size = 0;
460
461 if (!
s->context_initialized)
462 // we need the idct permutation for reading a custom matrix
464
465 /* let's go :-) */
466 if (CONFIG_WMV2_DECODER &&
s->msmpeg4_version == 5) {
468 }
else if (CONFIG_MSMPEG4DEC &&
s->msmpeg4_version) {
471 if (
s->avctx->extradata_size && !
s->extradata_parsed) {
473
474 if (
init_get_bits8(&gb,
s->avctx->extradata,
s->avctx->extradata_size) >= 0 )
476 s->extradata_parsed = 1;
477 }
482 }
else if (CONFIG_FLV_DECODER &&
s->h263_flv) {
484 } else {
486 }
487
491 av_log(
s->avctx,
AV_LOG_WARNING,
"Reverting picture dimensions change due to header decoding failure\n");
494 }
495 }
498
499 /* skip if the header was thrashed */
503 }
504
505 if (!
s->context_initialized) {
509 }
510
512
517 goto retry;
518 if (
s->studio_profile != (
s->idsp.idct ==
NULL))
520 }
521
522 /* After H.263 & MPEG-4 header decode we have the height, width,
523 * and other parameters. So then we could init the picture.
524 * FIXME: By the way H.263 decoder is evolving it should have
525 * an H263EncContext */
529 /* H.263 could change picture size any time */
530 s->context_reinit = 0;
531
535
537
540
545 }
546 }
547
552
553 /* skip B-frames if we don't have reference frames */
554 if (!
s->last_picture_ptr &&
563
566
567 if (!
s->divx_packed && !avctx->
hwaccel)
569
572 s->gb.buffer_end -
s->gb.buffer);
575 }
576
578
579 /* the second part of the wmv2 header contains the MB skip bits which
580 * are stored in current_picture->mb_type which is not available before
581 * ff_mpv_frame_start() */
582 if (CONFIG_WMV2_DECODER &&
s->msmpeg4_version == 5) {
588 }
589
590 /* decode each macroblock */
593
595 while (
s->mb_y <
s->mb_height) {
596 if (
s->msmpeg4_version) {
597 if (
s->slice_height == 0 ||
s->mb_x != 0 || slice_ret < 0 ||
599 break;
600 } else {
601 int prev_x =
s->mb_x, prev_y =
s->mb_y;
603 break;
604 if (prev_y *
s->mb_width + prev_x < s->mb_y *
s->mb_width +
s->mb_x)
605 s->er.error_occurred = 1;
606 }
607
608 if (
s->msmpeg4_version < 4 &&
s->h263_pred)
610
613 }
614
615 if (
s->msmpeg4_version &&
s->msmpeg4_version < 4 &&
617 if (!CONFIG_MSMPEG4DEC ||
620
623 if (!
s->studio_profile)
625
630 }
631
633
636
637 if (!
s->divx_packed && avctx->
hwaccel)
639
640 av_assert1(
s->current_picture.f->pict_type ==
s->current_picture_ptr->f->pict_type);
641 av_assert1(
s->current_picture.f->pict_type ==
s->pict_type);
647 }
else if (
s->last_picture_ptr) {
652 }
653
654 if (
s->last_picture_ptr ||
s->low_delay) {
657 for (int p = 0; p < 3; p++) {
659
662 }
663 }
664 *got_frame = 1;
665 }
666
668 return slice_ret;
669 else
671 }
672
674 #if CONFIG_H263_VAAPI_HWACCEL || CONFIG_MPEG4_VAAPI_HWACCEL
676 #endif
677 #if CONFIG_MPEG4_NVDEC_HWACCEL
679 #endif
680 #if CONFIG_MPEG4_VDPAU_HWACCEL
682 #endif
683 #if CONFIG_H263_VIDEOTOOLBOX_HWACCEL || CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL
685 #endif
688 };
689
691 #if CONFIG_H263_VAAPI_HWACCEL
693 #endif
694 #if CONFIG_MPEG4_NVDEC_HWACCEL
696 #endif
697 #if CONFIG_MPEG4_VDPAU_HWACCEL
699 #endif
700 #if CONFIG_H263_VIDEOTOOLBOX_HWACCEL
702 #endif
704 };
705
708 CODEC_LONG_NAME(
"H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
719 .p.max_lowres = 3,
722 };
723
726 CODEC_LONG_NAME(
"H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
737 .p.max_lowres = 3,
740 };
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left