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
44
45 #define RV_GET_MAJOR_VER(x) ((x) >> 28)
46 #define RV_GET_MINOR_VER(x) (((x) >> 20) & 0xFF)
47 #define RV_GET_MICRO_VER(x) (((x) >> 12) & 0xFF)
48
49 #define MAX_VLC_ENTRIES 1023 // Note: Does not include the skip entries.
51
57
58 /* (run, length) encoded value for the symbols table. The actual symbols
59 * are run..run - length (mod 256).
60 * The last two entries in the following table apply to luma only.
61 * The skip values are not included in this list. */
63 { 0, 0 }, { 1, 0 }, { 255, 0 }, { 3, 1 }, { 254, 1 },
64 { 7, 3 }, { 252, 3 }, { 15, 7 }, { 248, 7 }, { 31, 15 },
65 { 240, 15 }, { 63, 31 }, { 224, 31 }, { 127, 63 }, { 192, 63 },
66 { 255, 127 }, { 128, 127 }, { 127, 255 }, { 128, 255 },
67 };
68
69 /* entry[i] of the following tables gives
70 * the number of VLC codes of length i + 2. */
72 1, 0, 2, 4, 8, 16, 32, 0, 64, 0, 128, 0, 256, 0, 512,
73 };
74
76 1, 2, 4, 0, 8, 0, 16, 0, 32, 0, 64, 0, 128, 0, 256,
77 };
78
80
82 {
84
85 if (n < 4) {
87 } else {
91 return -1;
92 }
93 }
95 }
96
97 /* read RV 1.0 compatible frame header */
99 {
100 int mb_count, pb_frame, marker, mb_xy;
101
103
106 else
108
109 if (!marker)
111
113
114 ff_dlog(
s->avctx,
"pict_type=%d pb_frame=%d\n",
s->pict_type, pb_frame);
115
116 if (pb_frame) {
119 }
120
122 if (
s->qscale == 0) {
125 }
126
128 if (
s->rv10_version == 3) {
129 /* specific MPEG like DC coding not used */
133 ff_dlog(
s->avctx,
"DC:%d %d %d\n",
s->last_dc[0],
134 s->last_dc[1],
s->last_dc[2]);
135 }
136 }
137 /* if multiple packets per frame are sent, the position at which
138 * to display the macroblocks is coded here */
139
140 mb_xy =
s->mb_x +
s->mb_y *
s->mb_width;
141 if (
show_bits(&
s->gb, 12) == 0 || (mb_xy && mb_xy < s->mb_num)) {
145 } else {
148 mb_count =
s->mb_width *
s->mb_height;
149 }
152 s->unrestricted_mv = 1;
153
154 return mb_count;
155 }
156
158 {
160 int seq, mb_pos,
i,
ret;
161 int rpr_max;
162
165 case 0:
167 break;
168 case 1:
170 break; // hmm ...
171 case 2:
173 break;
174 case 3:
176 break;
177 default:
180 }
181
184 return -1;
185 }
189 }
190
194 }
195
197 if (
s->qscale == 0) {
200 }
201
203 s->loop_filter =
get_bits1(&
s->gb) && !
s->avctx->lowres;
204
207 else
209
210 rpr_max =
s->avctx->extradata[1] & 7;
211 if (rpr_max) {
213 int rpr_bits =
av_log2(rpr_max) + 1;
214
216
218 if (
s->avctx->extradata_size < 8 + 2 *
f) {
221 }
222
223 new_w = 4 * ((uint8_t *)
s->avctx->extradata)[6 + 2 *
f];
224 new_h = 4 * ((uint8_t *)
s->avctx->extradata)[7 + 2 *
f];
225 } else {
228 }
229 if (new_w !=
s->width || new_h !=
s->height || !
s->context_initialized) {
230 AVRational old_aspect =
s->avctx->sample_aspect_ratio;
232 "attempting to change resolution to %dx%d\n", new_w, new_h);
235
236 if (whole_size < (new_w + 15)/16 * ((new_h + 15)/16) / 8)
238
240
241 // attempt to keep aspect during typical resolution switches
244 if (2 * (int64_t)new_w *
s->height == (int64_t)new_h *
s->width)
246 if ((int64_t)new_w *
s->height == 2 * (int64_t)new_h *
s->width)
248
252
257 }
258
261 }
262 }
265
267
268 seq |=
s->time & ~0x7FFF;
269 if (seq -
s->time > 0x4000)
270 seq -= 0x8000;
271 if (seq -
s->time < -0x4000)
272 seq += 0x8000;
273
274 if (seq !=
s->time) {
277 s->pp_time =
s->time -
s->last_non_b_time;
278 s->last_non_b_time =
s->time;
279 } else {
281 s->pb_time =
s->pp_time - (
s->last_non_b_time -
s->time);
282 }
283 }
285 if (
s->pp_time <=
s->pb_time ||
s->pp_time <=
s->pp_time -
s->pb_time ||
s->pp_time<=0) {
287 "messed up order, possible from seeking? skipping current B-frame\n");
288 #define ERROR_SKIP_FRAME -123
290 }
292 }
293
295
297 // binary decoder reads 3+2 bits here but they don't seem to be used
299
301 s->unrestricted_mv = 1;
303 s->modified_quant = 1;
304 if (!
s->avctx->lowres)
306
309 "num:%5d x:%2d y:%2d type:%d qscale:%2d rnd:%d\n",
310 seq,
s->mb_x,
s->mb_y,
s->pict_type,
s->qscale,
312 }
313
315
316 return s->mb_width *
s->mb_height - mb_pos;
317 }
318
320 const uint8_t sym_rl[][2], int sym_rl_elems)
321 {
324 unsigned nb_syms = 0, nb_lens = 0;
325
326 for (
unsigned i = 0;
i < sym_rl_elems;
i++) {
327 unsigned cur_sym = sym_rl[
i][0];
328 for (
unsigned tmp = nb_syms + sym_rl[
i][1]; nb_syms <=
tmp; nb_syms++)
329 syms[nb_syms] = 0xFF & cur_sym--;
330 }
331
332 for (
unsigned i = 0;
i < 15;
i++)
333 for (
unsigned tmp = nb_lens + len_count[
i]; nb_lens <
tmp; nb_lens++)
334 lens[nb_lens] =
i + 2;
338 }
339
341 {
343
348 for (
int i = 0;
i < 1 << (
DC_VLC_BITS - 7
/* Length of skip prefix */);
i++) {
349 /* All codes beginning with 0x7F have the same length and value.
350 * Modifying the table directly saves us the useless subtables. */
353 }
358 for (
int i = 0;
i < 1 << (
DC_VLC_BITS - 9
/* Length of skip prefix */);
i++) {
359 /* Same as above. */
362 }
364 }
365
367 {
371 int major_ver, minor_ver, micro_ver,
ret;
372
376 }
380
382
384
389
390 s->h263_long_vectors = ((uint8_t *) avctx->
extradata)[3] & 1;
392
396
398 switch (major_ver) {
399 case 1:
400 s->rv10_version = micro_ver ? 3 : 1;
401 s->obmc = micro_ver == 2;
402 break;
403 case 2:
404 if (minor_ver >= 2) {
406 s->avctx->has_b_frames = 1;
407 }
408 break;
409 default:
413 }
414
418 }
419
421
425
427
428 /* init static VLCs */
430
431 return 0;
432 }
433
435 {
437
439 return 0;
440 }
441
443 int buf_size, int buf_size2, int whole_size)
444 {
447 int mb_count, mb_pos,
left, start_mb_x, active_bits_size,
ret;
448
449 active_bits_size = buf_size * 8;
453 else
455 if (mb_count < 0) {
459 }
460
461 if (
s->mb_x >=
s->mb_width ||
462 s->mb_y >=
s->mb_height) {
465 }
466 mb_pos =
s->mb_y *
s->mb_width +
s->mb_x;
467 left =
s->mb_width *
s->mb_height - mb_pos;
468 if (mb_count >
left) {
471 }
472
473 if (whole_size < s->mb_width *
s->mb_height / 8)
475
476 if ((
s->mb_x == 0 &&
s->mb_y == 0) || !
s->current_picture_ptr) {
477 // FIXME write parser so we always have complete frames?
478 if (
s->current_picture_ptr) {
481 s->mb_x =
s->mb_y =
s->resync_mb_x =
s->resync_mb_y = 0;
482 }
486 } else {
487 if (
s->current_picture_ptr->f->pict_type !=
s->pict_type) {
490 }
491 }
492
493
494 ff_dlog(avctx,
"qscale=%d\n",
s->qscale);
495
496 /* default quantization values */
499 s->first_slice_line = 1;
500 } else {
501 s->first_slice_line = 1;
502 s->resync_mb_x =
s->mb_x;
503 }
504 start_mb_x =
s->mb_x;
505 s->resync_mb_y =
s->mb_y;
507 s->y_dc_scale_table =
509 } else {
510 s->y_dc_scale_table =
512 }
513
514 if (
s->modified_quant)
516
518
519 s->rv10_first_dc_coded[0] = 0;
520 s->rv10_first_dc_coded[1] = 0;
521 s->rv10_first_dc_coded[2] = 0;
525 s->block_wrap[3] =
s->b8_stride;
527 s->block_wrap[5] =
s->mb_stride;
529
530 /* decode each macroblock */
531 for (
s->mb_num_left = mb_count;
s->mb_num_left > 0;
s->mb_num_left--) {
534 ff_tlog(avctx,
"**mb x=%d y=%d\n",
s->mb_x,
s->mb_y);
535
539
540 // Repeat the slice end check from ff_h263_decode_mb with our active
541 // bitstream size
544
547
548 if (!v)
550 }
553 active_bits_size = buf_size2 * 8;
555 8 * buf_size, active_bits_size);
557 }
558
563 }
569
570 if (++
s->mb_x ==
s->mb_width) {
574 }
575 if (
s->mb_x ==
s->resync_mb_x)
576 s->first_slice_line = 0;
578 break;
579 }
580
583
584 return active_bits_size;
585 }
586
588 {
591 else
593 }
594
597 {
598 const uint8_t *buf = avpkt->
data;
599 int buf_size = avpkt->
size;
603 int slice_count;
604 const uint8_t *slices_hdr =
NULL;
605
607
608 /* no supplementary picture */
609 if (buf_size == 0) {
610 return 0;
611 }
612
614 slice_count = (*buf++) + 1;
615 buf_size--;
616
617 if (!slice_count || buf_size <= 8 * slice_count) {
619 slice_count);
621 }
622
623 slices_hdr = buf + 4;
624 buf += 8 * slice_count;
625 buf_size -= 8 * slice_count;
626 } else
628
629 for (
i = 0;
i < slice_count;
i++) {
632
635
636 if (
i + 1 == slice_count)
638 else
640
641 if (
i + 2 >= slice_count)
642 size2 = buf_size -
offset;
643 else
645
646 if (
size <= 0 || size2 <= 0 ||
649
652
655 }
656
657 if (
s->current_picture_ptr &&
s->mb_y >=
s->mb_height) {
660
666 }
else if (
s->last_picture_ptr) {
671 }
672
673 if (
s->last_picture_ptr ||
s->low_delay) {
674 *got_frame = 1;
675 }
676
677 // so we can detect if frame_end was not called (find some nicer solution...)
678 s->current_picture_ptr =
NULL;
679 }
680
682 }
683
694 .max_lowres = 3,
698 },
699 };
700
712 .max_lowres = 3,
716 },
717 };
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