1 /*
2 * H.261 decoder
3 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
4 * Copyright (c) 2004 Maarten Daniels
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.261 decoder.
26 */
27
36
37 #define H261_MBA_VLC_BITS 9
38 #define H261_MTYPE_VLC_BITS 6
39 #define H261_MV_VLC_BITS 7
40 #define H261_CBP_VLC_BITS 9
41 #define TCOEFF_VLC_BITS 9
42 #define MBA_STUFFING 33
43 #define MBA_STARTCODE 34
44
49
51 {
52 static int done = 0;
53
54 if (!done) {
55 done = 1;
69 }
70 }
71
73 {
76
77 // set defaults
80
84
87
89
90 return 0;
91 }
92
93 /**
94 * Decode the group of blocks header or slice header.
95 * @return <0 if an error occurred
96 */
98 {
101
103 /* Check for GOB Start Code */
105 if (val)
106 return -1;
107
108 /* We have a GBSC */
110 }
111
113
116
117 /* Check if gob_number is valid */
120 return -1;
121 } else { // QCIF
124 return -1;
125 }
126
127 /* GEI */
130
134 return -1;
135 }
136
137 /* For the first transmitted macroblock in a GOB, MBA is the absolute
138 * address. For subsequent macroblocks, MBA is the difference between
139 * the absolute addresses of the macroblock and the last transmitted
140 * macroblock. */
143
144 return 0;
145 }
146
147 /**
148 * Decode the group of blocks / video packet header.
149 * @return <0 if no resync found
150 */
152 {
154 int left, ret;
155
158 if (ret >= 0)
159 return 0;
160 } else {
163 if (ret >= 0)
164 return 0;
165 }
166 // OK, it is not where it is supposed to be ...
170
171 for (; left > 15 + 1 + 4 + 5; left -= 8) {
174
176 if (ret >= 0)
177 return 0;
178
180 }
182 }
183 }
184
185 return -1;
186 }
187
188 /**
189 * Decode skipped macroblocks.
190 * @return 0
191 */
193 {
195 int i;
196
198
199 for (i = mba1; i < mba2; i++) {
200 int j, xy;
201
207
208 for (j = 0; j < 6; j++)
210
218
221 int b_xy = 2 * s->
mb_x + (2 * s->
mb_y) * b_stride;
224 }
225
227 }
228
229 return 0;
230 }
231
233 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16
234 };
235
237 {
239
240 /* check if mv_diff is valid */
241 if (mv_diff < 0)
242 return v;
243
244 mv_diff =
mvmap[mv_diff];
245
247 mv_diff = -mv_diff;
248
249 v += mv_diff;
250 if (v <= -16)
251 v += 32;
252 else if (v >= 16)
253 v -= 32;
254
255 return v;
256 }
257
258 /**
259 * Decode a macroblock.
260 * @return <0 if an error occurred
261 */
263 {
268
269 /* For the variable length encoding there are two code tables, one being
270 * used for the first transmitted LEVEL in INTER, INTER + MC and
271 * INTER + MC + FIL blocks, the second for all other LEVELs except the
272 * first one in INTRA blocks which is fixed length coded with 8 bits.
273 * NOTE: The two code tables only differ in one VLC so we handle that
274 * manually. */
277 /* DC coef */
279 // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
280 if ((level & 0x7F) == 0) {
283 return -1;
284 }
285 /* The code 1000 0000 is not used, the reconstruction level of 1024
286 * being coded as 1111 1111. */
287 if (level == 255)
288 level = 128;
290 i = 1;
291 } else if (coded) {
292 // Run Level Code
293 // EOB Not possible for first level when cbp is available (that's why the table is different)
294 // 0 1 1s
295 // * * 0*
297 i = 0;
298 if (check & 0x2) {
300 block[0] = (check & 0x1) ? -1 : 1;
301 i = 1;
302 }
303 } else {
304 i = 0;
305 }
306 if (!coded) {
308 return 0;
309 }
310 {
312 i--; // offset by -1 to allow direct indexing of scan_table
313 for (;;) {
316 if (run == 66) {
317 if (level) {
321 return -1;
322 }
323 /* escape */
324 /* The remaining combinations of (run, level) are encoded with a
325 * 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits
326 * level. */
331 } else if (level == 0) {
332 break;
333 } else {
337 }
339 if (i >= 64) {
343 return -1;
344 }
345 j = scan_table[i];
347 }
349 }
351 return 0;
352 }
353
355 {
357 int i, cbp, xy;
358
359 cbp = 63;
360 // Read mba
361 do {
364
365 /* Check for slice end */
366 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
370 }
372
376
379 }
380
383
386
392
393 // Read mtype
399 }
402
403 // Read mquant
406
408
409 // Read mv
411 /* Motion vector data is included for all MC macroblocks. MVD is
412 * obtained from the macroblock vector by subtracting the vector
413 * of the preceding macroblock. For this calculation the vector
414 * of the preceding macroblock is regarded as zero in the
415 * following three situations:
416 * 1) evaluating MVD for macroblocks 1, 12 and 23;
417 * 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
418 * 3) MTYPE of the previous macroblock was not MC. */
423 }
424
427 } else {
430 }
431
432 // Read cbp
435
438 goto intra;
439 }
440
441 //set motion vectors
445 s->
mv[0][0][0] = h->
current_mv_x * 2;
// gets divided by 2 in motion compensation
447
450 int b_xy = 2 * s->
mb_x + (2 * s->
mb_y) * b_stride;
453 }
454
455 intra:
456 /* decode each block */
459 for (i = 0; i < 6; i++) {
462 cbp += cbp;
463 }
464 } else {
465 for (i = 0; i < 6; i++)
467 }
468
470
472 }
473
474 /**
475 * Decode the H.261 picture header.
476 * @return <0 if no startcode found
477 */
479 {
482 uint32_t startcode = 0;
483
485 startcode = ((startcode << 1) |
get_bits(&s->
gb, 1)) & 0x000FFFFF;
486
487 if (startcode == 0x10)
488 break;
489 }
490
491 if (startcode != 0x10) {
493 return -1;
494 }
495
496 /* temporal reference */
497 i =
get_bits(&s->
gb, 5);
/* picture timestamp */
499 i += 32;
501
503
504 /* PTYPE starts here */
508
510
511 // only 2 formats possible
512 if (format == 0) { // QCIF
517 } else { // CIF
522 }
523
525
528
529 /* PEI */
532
533 /* H.261 has no I-frames, but if we pass AV_PICTURE_TYPE_I for the first
534 * frame, the codec crashes if it does not contain all I-blocks
535 * (e.g. when a packet is lost). */
537
539 return 0;
540 }
541
543 {
545
547
548 /* decode mb's */
550 int ret;
551 /* DCT & quantize */
553 if (ret < 0) {
556 return 0;
557 }
560 return -1;
561 }
562
566 }
567
568 return -1;
569 }
570
571 /**
572 * returns the number of bytes consumed for building the current frame
573 */
575 {
577 if (pos == 0)
578 pos = 1; // avoid infinite loops (i doubt that is needed but ...)
579 if (pos + 10 > buf_size)
580 pos = buf_size; // oops ;)
581
582 return pos;
583 }
584
587 {
589 int buf_size = avpkt->
size;
592 int ret;
594
596 ff_dlog(avctx,
"bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
597
599
600 retry:
602
604 // we need the IDCT permutation for reading a custom matrix
606
608
609 /* skip if the header was thrashed */
610 if (ret < 0) {
612 return -1;
613 }
614
620 }
621
624 return ret;
625
627 if (ret < 0)
628 return ret;
629
630 goto retry;
631 }
632
633 // for skipping the frame
636
641
643 return -1;
644
646
647 /* decode each macroblock */
650
653 break;
655 }
657
660
662 return ret;
664
665 *got_frame = 1;
666
668 }
669
671 {
674
676 return 0;
677 }
678
689 .max_lowres = 3,
690 };
const char const char void * val
discard all frames except keyframes
void ff_init_block_index(MpegEncContext *s)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define H261_CBP_VLC_BITS
static const char * format[]
This structure describes decoded (raw) audio or video data.
static int get_consumed_bytes(MpegEncContext *s, int buf_size)
returns the number of bytes consumed for building the current frame
ptrdiff_t const GLvoid * data
int coded_width
Bitstream width / height, may be different from width/height e.g.
#define H261_MBA_VLC_BITS
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
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.
static av_cold int init(AVCodecContext *avctx)
#define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size)
#define SKIP_COUNTER(name, gb, num)
const uint8_t ff_h261_mba_bits[35]
#define AV_EF_COMPLIANT
consider all spec non compliances as errors
void(* clear_blocks)(int16_t *blocks)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
#define AV_EF_BITSTREAM
detect bitstream specification deviations
int mb_num
number of MBs of a picture
const uint8_t ff_h261_mba_code[35]
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
static VLC h261_mtype_vlc
const uint8_t ff_h261_cbp_tab[63][2]
static int h261_decode_picture_header(H261Context *h)
Decode the H.261 picture header.
enum AVDiscard skip_frame
Skip decoding for selected frames.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
enum OutputFormat out_format
output format
static int h261_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Picture current_picture
copy of the current picture structure.
GetBitContext last_resync_gb
used to search for the next resync marker
static int get_bits_count(const GetBitContext *s)
static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded)
Decode a macroblock.
av_cold void ff_mpv_idct_init(MpegEncContext *s)
int mb_height
number of MBs horizontally & vertically
static av_cold void h261_decode_init_vlc(H261Context *h)
static void ff_update_block_index(MpegEncContext *s)
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
av_cold void ff_h261_common_init(void)
static int get_bits_left(GetBitContext *gb)
#define UPDATE_CACHE(name, gb)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static av_cold int h261_decode_init(AVCodecContext *avctx)
int mb_skipped
MUST BE SET only during DECODING.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
int low_delay
no reordering needed / has no B-frames
#define CLOSE_READER(name, gb)
void ff_mpv_common_end(MpegEncContext *s)
#define INIT_VLC_RL(rl, static_size)
#define GET_RL_VLC(level, run, name, gb, table, bits,max_depth, need_update)
enum AVPictureType pict_type
Picture type of the frame.
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
static const int mvmap[17]
int16_t(*[2] motion_val)[2]
Picture * current_picture_ptr
pointer to the current picture
void ff_mpeg_er_frame_start(MpegEncContext *s)
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
int block_last_index[12]
last non zero coefficient in block
const uint8_t ff_h261_mtype_code[10]
static av_cold int h261_decode_end(AVCodecContext *avctx)
const uint8_t ff_h261_mtype_bits[10]
static int h261_decode_gob_header(H261Context *h)
Decode the group of blocks header or slice header.
RL_VLC_ELEM * rl_vlc[32]
decoding only
#define SHOW_UBITS(name, gb, num)
#define FF_ARRAY_ELEMS(a)
void ff_mpv_decode_defaults(MpegEncContext *s)
Set the given MpegEncContext to defaults for decoding.
const uint8_t ff_h261_mv_tab[17][2]
#define MV_TYPE_16X16
1 vector for the whole mb
const int ff_h261_mtype_map[10]
Libavcodec external API header.
main external API structure.
ScanTable intra_scantable
int height
picture size. must be a multiple of 16
#define OPEN_READER(name, gb)
void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
#define SLICE_END
end marker found
static int h261_decode_gob(H261Context *h)
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits1(GetBitContext *s)
static void skip_bits(GetBitContext *s, int n)
Rational number (pair of numerator and denominator).
static int h261_resync(H261Context *h)
Decode the group of blocks / video packet header.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
#define H261_MTYPE_VLC_BITS
int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
generic function called after decoding the header and before a frame is decoded.
#define SKIP_CACHE(name, gb, num)
RLTable ff_h261_rl_tcoeff
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
int gob_start_code_skipped
int mv[2][4][2]
motion vectors for a macroblock first coordinate : 0 = forward 1 = backward second " : depend...
struct AVCodecContext * avctx
#define SHOW_SBITS(name, gb, num)
discard all non reference
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)
static int h261_decode_mb(H261Context *h)
av_cold int ff_mpv_common_init(MpegEncContext *s)
init common structure for both encoder and decoder.
static int decode_mv_component(GetBitContext *gb, int v)
void ff_mpv_frame_end(MpegEncContext *s)
void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64])
int16_t(* block)[64]
points to one of the following blocks
ParseContext parse_context
VLC_TYPE(* table)[2]
code, bits
static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2)
Decode skipped macroblocks.
int key_frame
1 -> keyframe, 0-> not
static const uint8_t * align_get_bits(GetBitContext *s)
int frame_number
Frame counter, set by libavcodec.
uint32_t * mb_type
types and macros are defined in mpegutils.h
static int skip_1stop_8data_bits(GetBitContext *gb)
This structure stores compressed data.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
#define check(x, y, S, v)