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
47
49 {
50 /* MPEG-4 Studio Profile only, not supported by hardware */
54 }
55
58
63 }
64
66 }
67
69 {
71 int ret;
72
74
75 // set defaults
78
83
84 /* select sub codec */
90 break;
92 break;
96 break;
100 break;
104 break;
108 break;
112 break;
121 break;
123 break;
126 break;
127 default:
131 }
133
137
138 /* for H.263, we allocate the images after having read the header */
145 return ret;
146 }
147
151
152 return 0;
153 }
154
156 {
158
160 return 0;
161 }
162
163 /**
164 * Return the number of bytes consumed for building the current frame.
165 */
167 {
169
171 /* We would have to scan through the whole buf to handle the weird
172 * reordering ... */
173 return buf_size;
176 // padding is not really read so this might be -1
177 if (pos < 0)
178 pos = 0;
179 return pos;
180 } else {
181 // avoid infinite loops (maybe not needed...)
182 if (pos == 0)
183 pos = 1;
184 // oops ;)
185 if (pos + 10 > buf_size)
186 pos = buf_size;
187
188 return pos;
189 }
190 }
191
193 {
197 int ret;
198
203
205
208 return ret;
209 }
210
214 // ensure we exit decode loop
216 return ret;
217 }
218
220 const int qscale = s->
qscale;
221
224 return ret;
225
226 /* restore variables which were modified */
231 }
232
234 /* per-row end of slice checks */
239
240 return 0;
241 }
242 }
243
248 }
249
252 int ret;
253
255
258
259 /* DCT & quantize */
260
265
268
271
272 if (ret < 0) {
278
281
283
289 }
290 return 0;
293 "Slice mismatch at MB: %d\n", xy);
298 }
302
304 continue;
306 }
307
311 }
312
315
317 }
318
320
327
328 /* try to detect the padding bug */
336
337 if (bits_left == 0) {
339 } else if (bits_left != 1) {
341 v |= 0x7F >> (7 - (bits_count & 7));
342
343 if (v == 0x7F && bits_left <= 8)
346 bits_left <= 16)
348 else
350 }
351 }
352
360
362 }
363
368
370 }
371
373 if (
376 else
378 }
379
380 // handle formats which don't have unique end markers
383 int max_extra = 7;
384
385 /* no markers in M$ crap */
387 max_extra += 17;
388
389 /* buggy padding but the frame should still end approximately at
390 * the bitstream end */
393 max_extra += 48;
395 max_extra += 256 * 256 * 256 * 64;
396
397 if (left > max_extra)
399 "discarding %d junk bits at end, next would be %X\n",
401 else if (left < 0)
403 else
406
407 return 0;
408 }
409
411 "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
413
416
418 }
419
422 {
424 int buf_size = avpkt->
size;
426 int ret;
427 int slice_ret = 0;
429
430 /* no supplementary picture */
431 if (buf_size == 0) {
432 /* special case for last picture */
435 return ret;
437
438 *got_frame = 1;
439 }
440
441 return 0;
442 }
443
445 int next;
446
453 } else {
455 "this codec does not support truncated bitstreams\n");
457 }
458
460 &buf_size) < 0)
461 return buf_size;
462 }
463
464 retry:
466 int i;
467 for(i=0; i < buf_size-3; i++) {
468 if (buf[i]==0 && buf[i+1]==0 && buf[i+2]==1) {
469 if (buf[i+3]==0xB0) {
472 }
473 break;
474 }
475 }
476 }
477
481 else
483
485 if (ret < 0)
486 return ret;
487
489 // we need the idct permutation for reading a custom matrix
491
492 /* let's go :-) */
500
503 }
507 }
else if (CONFIG_FLV_DECODER && s->
h263_flv) {
509 } else {
511 }
512
519 }
520 }
523
524 /* skip if the header was thrashed */
525 if (ret < 0) {
527 return ret;
528 }
529
533 return ret;
534 }
535
538 if (i < 0)
539 return i;
541 }
542
544
547 goto retry;
548 }
549
550 /* After H.263 & MPEG-4 header decode we have the height, width,
551 * and other parameters. So then we could init the picture.
552 * FIXME: By the way H.263 decoder is evolving it should have
553 * an H263EncContext */
557 /* H.263 could change picture size any time */
559
561 if (ret < 0)
562 return ret;
563
565
567 return ret;
568
573 }
574 }
575
580
581 // for skipping the frame
584
585 /* skip B-frames if we don't have reference frames */
595
599 else
601 }
602
606 } else {
609 }
610
612 return ret;
613
616
620 if (ret < 0 )
621 return ret;
622 }
623
625
626 /* the second part of the wmv2 header contains the MB skip bits which
627 * are stored in current_picture->mb_type which is not available before
628 * ff_mpv_frame_start() */
631 if (ret < 0)
632 return ret;
633 if (ret == 1)
635 }
636
637 /* decode each macroblock */
640
646 break;
647 } else {
648 int prev_x = s->
mb_x, prev_y = s->
mb_y;
650 break;
653 }
654
657
660 }
661
667
671
674 if (ret < 0)
675 return ret;
676 }
677
679
682
685
690 return ret;
695 return ret;
698 }
699
703 int x, y, p;
705 for (p=0; p<3; p++) {
709 for (y=0; y<(h>>1); y++)
712 pict->
data[p][x + y*linesize],
713 pict->
data[p][x + (h-1-y)*linesize]);
714 }
715 }
716 *got_frame = 1;
717 }
718
720 return slice_ret;
721 else
723 }
724
726 #if CONFIG_H263_VAAPI_HWACCEL || CONFIG_MPEG4_VAAPI_HWACCEL
728 #endif
729 #if CONFIG_MPEG4_NVDEC_HWACCEL
731 #endif
732 #if CONFIG_MPEG4_VDPAU_HWACCEL
734 #endif
735 #if CONFIG_H263_VIDEOTOOLBOX_HWACCEL || CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL
737 #endif
740 };
741
755 .max_lowres = 3,
757 };
758
772 .max_lowres = 3,
775 #if CONFIG_H263_VAAPI_HWACCEL
777 #endif
778 #if CONFIG_MPEG4_VDPAU_HWACCEL
780 #endif
781 #if CONFIG_H263_VIDEOTOOLBOX_HWACCEL
783 #endif
785 },
786 };
int bitstream_buffer_size
#define AV_EF_AGGRESSIVE
consider things that a sane encoder should not do as an error
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
const struct AVCodec * codec
discard all frames except keyframes
void ff_init_block_index(MpegEncContext *s)
int(* start_frame)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Called at the beginning of each frame or field picture.
qpel_mc_func avg_qpel_pixels_tab[2][16]
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
static void flush(AVCodecContext *avctx)
int coded_width
Bitstream width / height, may be different from width/height e.g.
#define SLICE_NOEND
no end marker or error found but mb count exceeded
#define AV_LOG_WARNING
Something somehow does not look correct.
qpel_mc_func put_no_rnd_qpel_pixels_tab[2][16]
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
hardware decoding through Videotoolbox
static av_cold int init(AVCodecContext *avctx)
static int decode_slice(MpegEncContext *s)
void ff_er_frame_end(ERContext *s)
enum AVColorRange color_range
MPEG vs JPEG YUV range.
int msmpeg4_version
0=not msmpeg4, 1=mp41, 2=mp42, 3=mp43/divx3 4=wmv1/7 5=wmv2/8
av_cold void ff_h263dsp_init(H263DSPContext *ctx)
uint8_t * bitstream_buffer
#define AV_EF_BUFFER
detect improper bitstream length
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
#define FF_PROFILE_MPEG4_SIMPLE_STUDIO
int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
int ff_msmpeg4_decode_ext_header(MpegEncContext *s, int buf_size)
int ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
int padding_bug_score
used to detect the VERY common padding bug in MPEG-4
int mb_num
number of MBs of a picture
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
int ff_h263_decode_mb(MpegEncContext *s, int16_t block[6][64])
enum AVDiscard skip_frame
Skip decoding for selected frames.
void ff_mpeg4_clean_buffers(MpegEncContext *s)
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
static enum AVPixelFormat h263_get_format(AVCodecContext *avctx)
void ff_h263_decode_init_vlc(void)
enum OutputFormat out_format
output format
int ff_mpv_common_frame_size_change(MpegEncContext *s)
Multithreading support functions.
qpel_mc_func(* qpel_put)[16]
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int no_rounding
apply no rounding to motion compensation (MPEG-4, msmpeg4, ...) for B-frames rounding mode is always ...
int ff_intel_h263_decode_picture_header(MpegEncContext *s)
Picture current_picture
copy of the current picture structure.
GetBitContext last_resync_gb
used to search for the next resync marker
int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
static int get_bits_count(const GetBitContext *s)
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
Check that the provided sample aspect ratio is valid and set it on the codec context.
av_cold void ff_mpv_idct_init(MpegEncContext *s)
int mb_height
number of MBs horizontally & vertically
int lowres
low resolution decoding, 1-> 1/2 size, 2->1/4 size
int codec_tag
internal codec_tag upper case converted from avctx codec_tag
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
static void ff_update_block_index(MpegEncContext *s)
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
int ff_msmpeg4_decode_picture_header(MpegEncContext *s)
static int get_bits_left(GetBitContext *gb)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int has_b_frames
Size of the frame reordering buffer in the decoder.
int last_dc[3]
last DC values for MPEG-1
void ff_er_add_slice(ERContext *s, int startx, int starty, int endx, int endy, int status)
Add a slice.
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
int partitioned_frame
is current frame partitioned
int unrestricted_mv
mv can point outside of the coded picture
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
int flags
AV_CODEC_FLAG_*.
#define FF_BUG_NO_PADDING
const char * name
Name of the codec implementation.
int low_delay
no reordering needed / has no B-frames
qpel_mc_func put_qpel_pixels_tab[2][16]
void ff_mpv_common_end(MpegEncContext *s)
int ff_mpeg4_decode_studio_slice_header(Mpeg4DecContext *ctx)
Decode the next video packet.
int ff_flv_decode_picture_header(MpegEncContext *s)
void ff_mpeg_flush(AVCodecContext *avctx)
static void frame_end(MpegEncContext *s)
int resync_mb_x
x position of last resync marker
uint8_t * error_status_table
void ff_h263_loop_filter(MpegEncContext *s)
#define AV_CODEC_CAP_DRAW_HORIZ_BAND
Decoder can use draw_horiz_band callback.
int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
enum AVPictureType pict_type
Picture type of the frame.
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
static int get_consumed_bytes(MpegEncContext *s, int buf_size)
Return the number of bytes consumed for building the current frame.
int next_p_frame_damaged
set if the next p frame is damaged, to avoid showing trashed B-frames
Picture * current_picture_ptr
pointer to the current picture
void ff_mpeg_er_frame_start(MpegEncContext *s)
int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
Decode MPEG-4 headers.
int ff_wmv2_decode_secondary_picture_header(MpegEncContext *s)
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
#define AV_EF_EXPLODE
abort decoding on minor error detection
#define FRAME_SKIPPED
Return value for header parsers if frame is not coded.
int ff_h263_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
HW acceleration through CUDA.
void ff_mpv_decode_defaults(MpegEncContext *s)
Set the given MpegEncContext to defaults for decoding.
av_cold int ff_h263_decode_end(AVCodecContext *avctx)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
#define MV_TYPE_16X16
1 vector for the whole mb
int first_slice_line
used in MPEG-4 too to handle resync markers
Libavcodec external API header.
void ff_h263_update_motion_val(MpegEncContext *s)
int h263_flv
use flv H.263 header
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
main external API structure.
int height
picture size. must be a multiple of 16
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
#define SLICE_END
end marker found
Picture * picture
main picture buffer
int data_partitioning
data partitioning flag from header
int ff_mpeg4_decode_partitions(Mpeg4DecContext *ctx)
Decode the first and second partition.
HW acceleration through VDPAU, Picture.data[3] contains a VdpVideoSurface.
#define AV_EF_IGNORE_ERR
ignore errors and continue
int slice_height
in macroblocks
int ff_h263_decode_picture_header(MpegEncContext *s)
int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
generic function called after decoding the header and before a frame is decoded.
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
static enum AVPixelFormat pix_fmts[]
#define AV_CODEC_CAP_TRUNCATED
#define FF_BUG_AUTODETECT
autodetection
int h263_pred
use MPEG-4/H.263 ac/dc predictions
#define HWACCEL_VAAPI(codec)
av_cold int ff_h263_decode_init(AVCodecContext *avctx)
int ff_h263_resync(MpegEncContext *s)
Decode the group of blocks / video packet header / slice header (MPEG-4 Studio).
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
qpel_mc_func(* qpel_avg)[16]
the normal 219*2^(n-8) "MPEG" YUV ranges
Picture * next_picture_ptr
pointer to the next picture (for bidir pred)
struct AVCodecContext * avctx
discard all non reference
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_RB64
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
common internal api header.
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 ...
void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx)
#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...
const uint8_t * buffer_end
int ff_wmv2_decode_picture_header(MpegEncContext *s)
enum AVPixelFormat ff_h263_hwaccel_pixfmt_list_420[]
Picture * last_picture_ptr
pointer to the previous picture.
#define H263_GOB_HEIGHT(h)
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
av_cold int ff_mpv_common_init(MpegEncContext *s)
init common structure for both encoder and decoder.
void ff_mpv_frame_end(MpegEncContext *s)
void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64])
int resync_mb_y
y position of last resync marker
int16_t(* block)[64]
points to one of the following blocks
ParseContext parse_context
int key_frame
1 -> keyframe, 0-> not
#define AV_CODEC_FLAG_TRUNCATED
Input bitstream might be truncated at a random location instead of only at frame boundaries.
#define HWACCEL_VDPAU(codec)
int workaround_bugs
workaround bugs in encoders which cannot be detected automatically
int(* decode_mb)(struct MpegEncContext *s, int16_t block[12][64])
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
Find the end of the current frame in the bitstream.
av_cold void ff_qpeldsp_init(QpelDSPContext *c)
#define FFSWAP(type, a, b)
#define CONFIG_MSMPEG4_DECODER
int(* decode_slice)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Callback for each slice.
int ff_h263_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
#define FF_QSCALE_TYPE_MPEG1
int(* end_frame)(AVCodecContext *avctx)
Called at the end of each frame or field picture.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
AVPixelFormat
Pixel format.
This structure stores compressed data.
void ff_mpv_report_decode_progress(MpegEncContext *s)
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
#define AV_CEIL_RSHIFT(a, b)