Go to the documentation of this file. 1 /*
2 * RV10/RV20 decoder
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer
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 * RV10/RV20 decoder
26 */
27
28 #include <inttypes.h>
29
32
47
48 #define RV_GET_MAJOR_VER(x) ((x) >> 28)
49 #define RV_GET_MINOR_VER(x) (((x) >> 20) & 0xFF)
50 #define RV_GET_MICRO_VER(x) (((x) >> 12) & 0xFF)
51
52 #define MAX_VLC_ENTRIES 1023 // Note: Does not include the skip entries.
54
60
61 /* (run, length) encoded value for the symbols table. The actual symbols
62 * are run..run - length (mod 256).
63 * The last two entries in the following table apply to luma only.
64 * The skip values are not included in this list. */
66 { 0, 0 }, { 1, 0 }, { 255, 0 }, { 3, 1 }, { 254, 1 },
67 { 7, 3 }, { 252, 3 }, { 15, 7 }, { 248, 7 }, { 31, 15 },
68 { 240, 15 }, { 63, 31 }, { 224, 31 }, { 127, 63 }, { 192, 63 },
69 { 255, 127 }, { 128, 127 }, { 127, 255 }, { 128, 255 },
70 };
71
72 /* entry[i] of the following tables gives
73 * the number of VLC codes of length i + 2. */
75 1, 0, 2, 4, 8, 16, 32, 0, 64, 0, 128, 0, 256, 0, 512,
76 };
77
79 1, 2, 4, 0, 8, 0, 16, 0, 32, 0, 64, 0, 128, 0, 256,
80 };
81
83
85 {
87
88 if (n < 4) {
90 } else {
94 return -1;
95 }
96 }
98 }
99
100 /* read RV 1.0 compatible frame header */
102 {
103 int mb_count, pb_frame, marker, mb_xy;
104
106
109 else
111
112 if (!marker)
114
116
117 ff_dlog(
s->avctx,
"pict_type=%d pb_frame=%d\n",
s->pict_type, pb_frame);
118
119 if (pb_frame) {
122 }
123
125 if (
s->qscale == 0) {
128 }
129
131 if (
s->rv10_version == 3) {
132 /* specific MPEG like DC coding not used */
136 ff_dlog(
s->avctx,
"DC:%d %d %d\n",
s->last_dc[0],
137 s->last_dc[1],
s->last_dc[2]);
138 }
139 }
140 /* if multiple packets per frame are sent, the position at which
141 * to display the macroblocks is coded here */
142
143 mb_xy =
s->mb_x +
s->mb_y *
s->mb_width;
144 if (
show_bits(&
s->gb, 12) == 0 || (mb_xy && mb_xy < s->mb_num)) {
148 } else {
151 mb_count =
s->mb_width *
s->mb_height;
152 }
155
156 return mb_count;
157 }
158
160 {
165 int seq, mb_pos,
ret;
166 int rpr_max;
167
168 s->pict_type = pict_types[
get_bits(&
s->gb, 2)];
169
172 return -1;
173 }
177 }
178
182 }
183
185 if (
s->qscale == 0) {
188 }
189
191 s->loop_filter =
get_bits1(&
s->gb) && !
s->avctx->lowres;
192
195 else
197
198 rpr_max =
s->avctx->extradata[1] & 7;
199 if (rpr_max) {
201 int rpr_bits =
av_log2(rpr_max) + 1;
202
204
206 if (
s->avctx->extradata_size < 8 + 2 *
f) {
209 }
210
211 new_w = 4 * ((uint8_t *)
s->avctx->extradata)[6 + 2 *
f];
212 new_h = 4 * ((uint8_t *)
s->avctx->extradata)[7 + 2 *
f];
213 } else {
216 }
217 if (new_w !=
s->width || new_h !=
s->height || !
s->context_initialized) {
218 AVRational old_aspect =
s->avctx->sample_aspect_ratio;
220 "attempting to change resolution to %dx%d\n", new_w, new_h);
223
224 if (whole_size < (new_w + 15)/16 * ((new_h + 15)/16) / 8)
226
228
229 // attempt to keep aspect during typical resolution switches
236
240
245 }
246
249 }
250 }
253
255
256 seq |=
s->time & ~0x7FFF;
257 if (seq -
s->time > 0x4000)
258 seq -= 0x8000;
259 if (seq -
s->time < -0x4000)
260 seq += 0x8000;
261
262 if (seq !=
s->time) {
265 s->pp_time =
s->time -
s->last_non_b_time;
266 s->last_non_b_time =
s->time;
267 } else {
269 s->pb_time =
s->pp_time - (
s->last_non_b_time -
s->time);
270 }
271 }
273 if (
s->pp_time <=
s->pb_time ||
s->pp_time <=
s->pp_time -
s->pb_time ||
s->pp_time<=0) {
275 "messed up order, possible from seeking? skipping current B-frame\n");
276 #define ERROR_SKIP_FRAME -123
278 }
280 }
281
283
285 // binary decoder reads 3+2 bits here but they don't seem to be used
287
290 s->modified_quant = 1;
291 if (!
s->avctx->lowres)
293
296 "num:%5d x:%2d y:%2d type:%d qscale:%2d rnd:%d\n",
297 seq,
s->mb_x,
s->mb_y,
s->pict_type,
s->qscale,
299 }
300
302
303 return s->mb_width *
s->mb_height - mb_pos;
304 }
305
307 const uint8_t sym_rl[][2], int sym_rl_elems)
308 {
311 unsigned nb_syms = 0, nb_lens = 0;
312
313 for (
unsigned i = 0;
i < sym_rl_elems;
i++) {
314 unsigned cur_sym = sym_rl[
i][0];
315 for (
unsigned tmp = nb_syms + sym_rl[
i][1]; nb_syms <=
tmp; nb_syms++)
316 syms[nb_syms] = 0xFF & cur_sym--;
317 }
318
319 for (
unsigned i = 0;
i < 15;
i++)
320 for (
unsigned tmp = nb_lens + len_count[
i]; nb_lens <
tmp; nb_lens++)
321 lens[nb_lens] =
i + 2;
325 }
326
328 {
330
335 for (
int i = 0;
i < 1 << (
DC_VLC_BITS - 7
/* Length of skip prefix */);
i++) {
336 /* All codes beginning with 0x7F have the same length and value.
337 * Modifying the table directly saves us the useless subtables. */
340 }
345 for (
int i = 0;
i < 1 << (
DC_VLC_BITS - 9
/* Length of skip prefix */);
i++) {
346 /* Same as above. */
349 }
351 }
352
354 {
358 int major_ver, minor_ver, micro_ver,
ret;
359
363 }
367
369
371
376
377 s->h263_long_vectors = ((uint8_t *) avctx->
extradata)[3] & 1;
379
383
385 switch (major_ver) {
386 case 1:
387 s->rv10_version = micro_ver ? 3 : 1;
388 s->obmc = micro_ver == 2;
389 break;
390 case 2:
391 if (minor_ver >= 2) {
393 s->avctx->has_b_frames = 1;
394 }
395 break;
396 default:
400 }
401
405 }
406
408
411
413
414 /* init static VLCs */
416
417 return 0;
418 }
419
421 {
423
425 return 0;
426 }
427
429 int buf_size, int buf_size2, int whole_size)
430 {
433 int mb_count, mb_pos,
left, start_mb_x, active_bits_size,
ret;
434
435 active_bits_size = buf_size * 8;
439 else
441 if (mb_count < 0) {
445 }
446
447 if (
s->mb_x >=
s->mb_width ||
448 s->mb_y >=
s->mb_height) {
451 }
452 mb_pos =
s->mb_y *
s->mb_width +
s->mb_x;
453 left =
s->mb_width *
s->mb_height - mb_pos;
454 if (mb_count >
left) {
457 }
458
459 if (whole_size < s->mb_width *
s->mb_height / 8)
461
462 if ((
s->mb_x == 0 &&
s->mb_y == 0) || !
s->current_picture_ptr) {
463 // FIXME write parser so we always have complete frames?
464 if (
s->current_picture_ptr) {
467 s->mb_x =
s->mb_y =
s->resync_mb_x =
s->resync_mb_y = 0;
468 }
472 } else {
473 if (
s->current_picture_ptr->f->pict_type !=
s->pict_type) {
476 }
477 }
478
479
480 ff_dlog(avctx,
"qscale=%d\n",
s->qscale);
481
482 /* default quantization values */
485 s->first_slice_line = 1;
486 } else {
487 s->first_slice_line = 1;
488 s->resync_mb_x =
s->mb_x;
489 }
490 start_mb_x =
s->mb_x;
491 s->resync_mb_y =
s->mb_y;
493 s->y_dc_scale_table =
495 } else {
496 s->y_dc_scale_table =
498 }
499
500 if (
s->modified_quant)
502
504
505 s->rv10_first_dc_coded[0] = 0;
506 s->rv10_first_dc_coded[1] = 0;
507 s->rv10_first_dc_coded[2] = 0;
511 s->block_wrap[3] =
s->b8_stride;
513 s->block_wrap[5] =
s->mb_stride;
515
516 /* decode each macroblock */
517 for (
s->mb_num_left = mb_count;
s->mb_num_left > 0;
s->mb_num_left--) {
520 ff_tlog(avctx,
"**mb x=%d y=%d\n",
s->mb_x,
s->mb_y);
521
525
526 // Repeat the slice end check from ff_h263_decode_mb with our active
527 // bitstream size
530
533
534 if (!v)
536 }
539 active_bits_size = buf_size2 * 8;
541 8 * buf_size, active_bits_size);
543 }
544
549 }
555
556 if (++
s->mb_x ==
s->mb_width) {
560 }
561 if (
s->mb_x ==
s->resync_mb_x)
562 s->first_slice_line = 0;
564 break;
565 }
566
569
570 return active_bits_size;
571 }
572
574 {
576 }
577
580 {
581 const uint8_t *buf = avpkt->
data;
582 int buf_size = avpkt->
size;
585 int slice_count;
586 const uint8_t *slices_hdr =
NULL;
587
588 ff_dlog(avctx,
"*****frame %"PRId64
" size=%d\n", avctx->
frame_num, buf_size);
589
590 /* no supplementary picture */
591 if (buf_size == 0) {
592 return 0;
593 }
594
595 slice_count = (*buf++) + 1;
596 buf_size--;
597
598 if (!slice_count || buf_size <= 8 * slice_count) {
600 slice_count);
602 }
603
604 slices_hdr = buf + 4;
605 buf += 8 * slice_count;
606 buf_size -= 8 * slice_count;
607
608 for (
i = 0;
i < slice_count;
i++) {
611
614
615 if (
i + 1 == slice_count)
617 else
619
620 if (
i + 2 >= slice_count)
621 size2 = buf_size -
offset;
622 else
624
625 if (
size <= 0 || size2 <= 0 ||
628
631
634 }
635
636 if (
s->current_picture_ptr &&
s->mb_y >=
s->mb_height) {
639
645 }
else if (
s->last_picture_ptr) {
650 }
651
652 if (
s->last_picture_ptr ||
s->low_delay) {
653 *got_frame = 1;
654 }
655
656 // so we can detect if frame_end was not called (find some nicer solution...)
657 s->current_picture_ptr =
NULL;
658 }
659
661 }
662
673 .p.max_lowres = 3,
674 };
675
687 .p.max_lowres = 3,
688 };
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