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
45
47 {
50
52 }
53
55 {
58
60
61 // set defaults
64
69
70 /* select sub codec */
76 break;
78 break;
82 break;
86 break;
90 break;
94 break;
98 break;
107 break;
109 break;
112 break;
113 default:
117 }
119
123
124 /* for h263, we allocate the images after having read the header */
132 }
133
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
157 /* We would have to scan through the whole buf to handle the weird
158 * reordering ... */
159 return buf_size;
162 // padding is not really read so this might be -1
163 if (pos < 0)
164 pos = 0;
165 return pos;
166 } else {
167 // avoid infinite loops (maybe not needed...)
168 if (pos == 0)
169 pos = 1;
170 // oops ;)
171 if (pos + 10 > buf_size)
172 pos = buf_size;
173
174 return pos;
175 }
176 }
177
179 {
184
189
191
195 // ensure we exit decode loop
198 }
199
201 const int qscale = s->
qscale;
202
206
207 /* restore variables which were modified */
212 }
213
215 /* per-row end of slice checks */
220
221 return 0;
222 }
223 }
224
229 }
230
234
236
239
240 /* DCT & quantize */
241
246
249
252
253 if (ret < 0) {
259
262
264
270 }
271 return 0;
274 "Slice mismatch at MB: %d\n", xy);
279 }
283
285 continue;
287 }
288
292 }
293
296
298 }
299
301
308
309 /* try to detect the padding bug */
317
318 if (bits_left == 0) {
320 } else if (bits_left != 1) {
322 v |= 0x7F >> (7 - (bits_count & 7));
323
324 if (v == 0x7F && bits_left <= 8)
327 bits_left <= 16)
329 else
331 }
332 }
333
341
343 }
344
349
351 }
352
354 if (
357 else
359 }
360
361 // handle formats which don't have unique end markers
364 int max_extra = 7;
365
366 /* no markers in M$ crap */
368 max_extra += 17;
369
370 /* buggy padding but the frame should still end approximately at
371 * the bitstream end */
374 max_extra += 48;
376 max_extra += 256 * 256 * 256 * 64;
377
378 if (left > max_extra)
380 "discarding %d junk bits at end, next would be %X\n",
382 else if (left < 0)
384 else
387
388 return 0;
389 }
390
392 "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
394
397
399 }
400
403 {
405 int buf_size = avpkt->
size;
408 int slice_ret = 0;
410
413
414 /* no supplementary picture */
415 if (buf_size == 0) {
416 /* special case for last picture */
421
422 *got_frame = 1;
423 }
424
425 return 0;
426 }
427
429 int next;
430
437 } else {
439 "this codec does not support truncated bitstreams\n");
441 }
442
444 &buf_size) < 0)
445 return buf_size;
446 }
447
448 retry:
450 int i;
451 for(i=0; i < buf_size-3; i++) {
452 if (buf[i]==0 && buf[i+1]==0 && buf[i+2]==1) {
453 if (buf[i+3]==0xB0) {
456 }
457 break;
458 }
459 }
460 }
461
465 else
467
469 if (ret < 0)
471
473 // we need the idct permutaton for reading a custom matrix
475
476 /* let's go :-) */
484
487 }
491 }
else if (CONFIG_FLV_DECODER && s->
h263_flv) {
493 } else {
495 }
496
503 }
504 }
507
508 /* skip if the header was thrashed */
509 if (ret < 0) {
512 }
513
518 }
519
522 if (i < 0)
523 return i;
525 }
526
528
531 goto retry;
532 }
533
534 /* After H263 & mpeg4 header decode we have the height, width,
535 * and other parameters. So then we could init the picture.
536 * FIXME: By the way H263 decoder is evolving it should have
537 * an H263EncContext */
541 /* H.263 could change picture size any time */
543
545 if (ret < 0)
547
549
552
557 }
558 }
559
564
565 // for skipping the frame
568
569 /* skip B-frames if we don't have reference frames */
579
583 else
585 }
586
590 } else {
593 }
594
597
600
604 }
605
609 if (ret < 0 )
611 }
612
614
615 /* the second part of the wmv2 header contains the MB skip bits which
616 * are stored in current_picture->mb_type which is not available before
617 * ff_mpv_frame_start() */
620 if (ret < 0)
622 if (ret == 1)
624 }
625
626 /* decode each macroblock */
629
635 break;
636 } else {
637 int prev_x = s->
mb_x, prev_y = s->
mb_y;
639 break;
642 }
643
646
649 }
650
656
660
663 if (ret < 0)
665 }
666
668
671
674
687 }
688
694 for (p=0; p<3; p++) {
698 for (y=0; y<(h>>1); y++)
699 for (x=0; x<w; x++)
701 pict->
data[p][x + y*linesize],
702 pict->
data[p][x + (h-1-y)*linesize]);
703 }
704 }
705 *got_frame = 1;
706 }
707
709 return ret;
710 else
712 }
713
715 #if CONFIG_H263_VAAPI_HWACCEL || CONFIG_MPEG4_VAAPI_HWACCEL
717 #endif
718 #if CONFIG_H263_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL
720 #endif
723 };
724
737 .max_lowres = 3,
739 };
740
753 .max_lowres = 3,
755 };