1 /*
2 * H261 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
33
34 #define H261_MBA_VLC_BITS 9
35 #define H261_MTYPE_VLC_BITS 6
36 #define H261_MV_VLC_BITS 7
37 #define H261_CBP_VLC_BITS 9
38 #define TCOEFF_VLC_BITS 9
39 #define MBA_STUFFING 33
40 #define MBA_STARTCODE 34
41
46
48 {
49 static int done = 0;
50
51 if (!done) {
52 done = 1;
66 }
67 }
68
70 {
73
74 // set defaults
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 {
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
220 }
221
222 return 0;
223 }
224
226 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16
227 };
228
230 {
232
233 /* check if mv_diff is valid */
234 if (mv_diff < 0)
236
237 mv_diff =
mvmap[mv_diff];
238
240 mv_diff = -mv_diff;
241
242 v += mv_diff;
243 if (v <= -16)
244 v += 32;
245 else if (v >= 16)
246 v -= 32;
247
249 }
250
251 /**
252 * Decode a macroblock.
253 * @return <0 if an error occurred
254 */
256 {
261
262 /* For the variable length encoding there are two code tables, one being
263 * used for the first transmitted LEVEL in INTER, INTER + MC and
264 * INTER + MC + FIL blocks, the second for all other LEVELs except the
265 * first one in INTRA blocks which is fixed length coded with 8 bits.
266 * NOTE: The two code tables only differ in one VLC so we handle that
267 * manually. */
270 /* DC coef */
272 // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
273 if ((level & 0x7F) == 0) {
276 return -1;
277 }
278 /* The code 1000 0000 is not used, the reconstruction level of 1024
279 * being coded as 1111 1111. */
280 if (level == 255)
281 level = 128;
283 i = 1;
284 } else if (coded) {
285 // Run Level Code
286 // EOB Not possible for first level when cbp is available (that's why the table is different)
287 // 0 1 1s
288 // * * 0*
290 i = 0;
291 if (check & 0x2) {
293 block[0] = (check & 0x1) ? -1 : 1;
294 i = 1;
295 }
296 } else {
297 i = 0;
298 }
299 if (!coded) {
301 return 0;
302 }
303 for (;;) {
305 if (code < 0) {
308 return -1;
309 }
311 /* escape */
312 /* The remaining combinations of (run, level) are encoded with a
313 * 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits
314 * level. */
317 } else if (code == 0) {
318 break;
319 } else {
324 }
326 if (i >= 64) {
329 return -1;
330 }
331 j = scan_table[i];
333 i++;
334 }
336 return 0;
337 }
338
340 {
342 int i, cbp, xy;
343
344 cbp = 63;
345 // Read mba
346 do {
349
350 /* Check for slice end */
351 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
355 }
357
361
364 }
365
368
371
377
378 // Read mtype
383 }
385
386 // Read mquant
389
391
392 // Read mv
394 /* Motion vector data is included for all MC macroblocks. MVD is
395 * obtained from the macroblock vector by subtracting the vector
396 * of the preceding macroblock. For this calculation the vector
397 * of the preceding macroblock is regarded as zero in the
398 * following three situations:
399 * 1) evaluating MVD for macroblocks 1, 12 and 23;
400 * 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
401 * 3) MTYPE of the previous macroblock was not MC. */
406 }
407
410 } else {
413 }
414
415 // Read cbp
418
421 goto intra;
422 }
423
424 //set motion vectors
428 s->
mv[0][0][0] = h->
current_mv_x * 2;
// gets divided by 2 in motion compensation
430
431 intra:
432 /* decode each block */
435 for (i = 0; i < 6; i++) {
438 cbp += cbp;
439 }
440 } else {
441 for (i = 0; i < 6; i++)
443 }
444
446
448 }
449
450 /**
451 * Decode the H.261 picture header.
452 * @return <0 if no startcode found
453 */
455 {
457 int format, i;
458 uint32_t startcode = 0;
459
461 startcode = ((startcode << 1) |
get_bits(&s->
gb, 1)) & 0x000FFFFF;
462
463 if (startcode == 0x10)
464 break;
465 }
466
467 if (startcode != 0x10) {
469 return -1;
470 }
471
472 /* temporal reference */
473 i =
get_bits(&s->
gb, 5);
/* picture timestamp */
475 i += 32;
477
480
481 /* PTYPE starts here */
485
487
488 // only 2 formats possible
489 if (format == 0) { // QCIF
494 } else { // CIF
499 }
500
502
505
506 /* PEI */
509
510 /* H.261 has no I-frames, but if we pass AV_PICTURE_TYPE_I for the first
511 * frame, the codec crashes if it does not contain all I-blocks
512 * (e.g. when a packet is lost). */
514
516 return 0;
517 }
518
520 {
522
524
525 /* decode mb's */
528 /* DCT & quantize */
530 if (ret < 0) {
533 return 0;
534 }
537 return -1;
538 }
539
543 }
544
545 return -1;
546 }
547
548 /**
549 * returns the number of bytes consumed for building the current frame
550 */
552 {
554 if (pos == 0)
555 pos = 1; // avoid infinite loops (i doubt that is needed but ...)
556 if (pos + 10 > buf_size)
557 pos = buf_size; // oops ;)
558
559 return pos;
560 }
561
564 {
566 int buf_size = avpkt->
size;
571
573 av_dlog(avctx,
"bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
576
578
579 retry:
581
583 // we need the IDCT permutaton for reading a custom matrix
585 return -1;
586
587 /* We need to set current_picture_ptr before reading the header,
588 * otherwise we cannot store anything in there. */
591 if (i < 0)
592 return i;
594 }
595
597
598 /* skip if the header was thrashed */
599 if (ret < 0) {
601 return -1;
602 }
603
609 }
612
613 goto retry;
614 }
615
616 // for skipping the frame
619
624
626 return -1;
627
629
630 /* decode each macroblock */
633
636 break;
638 }
640
643
647
648 *got_frame = 1;
649
651 }
652
654 {
657
659 return 0;
660 }
661
671 .max_lowres = 3,
673 };