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
46
47 #define RV_GET_MAJOR_VER(x) ((x) >> 28)
48 #define RV_GET_MINOR_VER(x) (((x) >> 20) & 0xFF)
49 #define RV_GET_MICRO_VER(x) (((x) >> 12) & 0xFF)
50
51 #define MAX_VLC_ENTRIES 1023 // Note: Does not include the skip entries.
53
59
60 /* (run, length) encoded value for the symbols table. The actual symbols
61 * are run..run - length (mod 256).
62 * The last two entries in the following table apply to luma only.
63 * The skip values are not included in this list. */
65 { 0, 0 }, { 1, 0 }, { 255, 0 }, { 3, 1 }, { 254, 1 },
66 { 7, 3 }, { 252, 3 }, { 15, 7 }, { 248, 7 }, { 31, 15 },
67 { 240, 15 }, { 63, 31 }, { 224, 31 }, { 127, 63 }, { 192, 63 },
68 { 255, 127 }, { 128, 127 }, { 127, 255 }, { 128, 255 },
69 };
70
71 /* entry[i] of the following tables gives
72 * the number of VLC codes of length i + 2. */
74 1, 0, 2, 4, 8, 16, 32, 0, 64, 0, 128, 0, 256, 0, 512,
75 };
76
78 1, 2, 4, 0, 8, 0, 16, 0, 32, 0, 64, 0, 128, 0, 256,
79 };
80
82
84 {
86
87 if (n < 4) {
89 } else {
93 return -1;
94 }
95 }
97 }
98
99 /* read RV 1.0 compatible frame header */
101 {
102 int mb_count, pb_frame, marker, mb_xy;
103
105
108 else
110
111 if (!marker)
113
115
116 ff_dlog(
s->avctx,
"pict_type=%d pb_frame=%d\n",
s->pict_type, pb_frame);
117
118 if (pb_frame) {
121 }
122
124 if (
s->qscale == 0) {
127 }
128
130 if (
s->rv10_version == 3) {
131 /* specific MPEG like DC coding not used */
135 ff_dlog(
s->avctx,
"DC:%d %d %d\n",
s->last_dc[0],
136 s->last_dc[1],
s->last_dc[2]);
137 }
138 }
139 /* if multiple packets per frame are sent, the position at which
140 * to display the macroblocks is coded here */
141
142 mb_xy =
s->mb_x +
s->mb_y *
s->mb_width;
143 if (
show_bits(&
s->gb, 12) == 0 || (mb_xy && mb_xy < s->mb_num)) {
147 } else {
150 mb_count =
s->mb_width *
s->mb_height;
151 }
154
155 return mb_count;
156 }
157
159 {
164 int seq, mb_pos,
ret;
165 int rpr_max;
166
167 s->pict_type = pict_types[
get_bits(&
s->gb, 2)];
168
171 return -1;
172 }
176 }
177
181 }
182
184 if (
s->qscale == 0) {
187 }
188
190 s->loop_filter =
get_bits1(&
s->gb) && !
s->avctx->lowres;
191
194 else
196
197 rpr_max =
s->avctx->extradata[1] & 7;
198 if (rpr_max) {
200 int rpr_bits =
av_log2(rpr_max) + 1;
201
203
205 if (
s->avctx->extradata_size < 8 + 2 *
f) {
208 }
209
210 new_w = 4 * ((uint8_t *)
s->avctx->extradata)[6 + 2 *
f];
211 new_h = 4 * ((uint8_t *)
s->avctx->extradata)[7 + 2 *
f];
212 } else {
215 }
216 if (new_w !=
s->width || new_h !=
s->height || !
s->context_initialized) {
217 AVRational old_aspect =
s->avctx->sample_aspect_ratio;
219 "attempting to change resolution to %dx%d\n", new_w, new_h);
222
223 if (whole_size < (new_w + 15)/16 * ((new_h + 15)/16) / 8)
225
227
228 // attempt to keep aspect during typical resolution switches
235
239
244 }
245
248 }
249 }
252
254
255 seq |=
s->time & ~0x7FFF;
256 if (seq -
s->time > 0x4000)
257 seq -= 0x8000;
258 if (seq -
s->time < -0x4000)
259 seq += 0x8000;
260
261 if (seq !=
s->time) {
264 s->pp_time =
s->time -
s->last_non_b_time;
265 s->last_non_b_time =
s->time;
266 } else {
268 s->pb_time =
s->pp_time - (
s->last_non_b_time -
s->time);
269 }
270 }
272 if (
s->pp_time <=
s->pb_time ||
s->pp_time <=
s->pp_time -
s->pb_time ||
s->pp_time<=0) {
274 "messed up order, possible from seeking? skipping current B-frame\n");
275 #define ERROR_SKIP_FRAME -123
277 }
279 }
280
282
284 // binary decoder reads 3+2 bits here but they don't seem to be used
286
289 s->modified_quant = 1;
290 if (!
s->avctx->lowres)
292
295 "num:%5d x:%2d y:%2d type:%d qscale:%2d rnd:%d\n",
296 seq,
s->mb_x,
s->mb_y,
s->pict_type,
s->qscale,
298 }
299
301
302 return s->mb_width *
s->mb_height - mb_pos;
303 }
304
306 const uint8_t sym_rl[][2], int sym_rl_elems)
307 {
310 unsigned nb_syms = 0, nb_lens = 0;
311
312 for (
unsigned i = 0;
i < sym_rl_elems;
i++) {
313 unsigned cur_sym = sym_rl[
i][0];
314 for (
unsigned tmp = nb_syms + sym_rl[
i][1]; nb_syms <=
tmp; nb_syms++)
315 syms[nb_syms] = 0xFF & cur_sym--;
316 }
317
318 for (
unsigned i = 0;
i < 15;
i++)
319 for (
unsigned tmp = nb_lens + len_count[
i]; nb_lens <
tmp; nb_lens++)
320 lens[nb_lens] =
i + 2;
324 }
325
327 {
329
334 for (
int i = 0;
i < 1 << (
DC_VLC_BITS - 7
/* Length of skip prefix */);
i++) {
335 /* All codes beginning with 0x7F have the same length and value.
336 * Modifying the table directly saves us the useless subtables. */
339 }
344 for (
int i = 0;
i < 1 << (
DC_VLC_BITS - 9
/* Length of skip prefix */);
i++) {
345 /* Same as above. */
348 }
349 }
350
352 {
356 int major_ver, minor_ver, micro_ver,
ret;
357
361 }
365
369
372
373 s->h263_long_vectors = ((uint8_t *) avctx->
extradata)[3] & 1;
375
379
380 switch (major_ver) {
381 case 1:
382 s->rv10_version = micro_ver ? 3 : 1;
383 s->obmc = micro_ver == 2;
384 break;
385 case 2:
386 if (minor_ver >= 2) {
389 }
390 break;
391 default:
395 }
396
400 }
401
402 /* init static VLCs */
404
405 return 0;
406 }
407
409 int buf_size, int buf_size2, int whole_size)
410 {
413 int mb_count, mb_pos,
left, start_mb_x, active_bits_size,
ret;
414
415 active_bits_size = buf_size * 8;
419 else
421 if (mb_count < 0) {
425 }
426
427 if (
s->mb_x >=
s->mb_width ||
428 s->mb_y >=
s->mb_height) {
431 }
432 mb_pos =
s->mb_y *
s->mb_width +
s->mb_x;
433 left =
s->mb_width *
s->mb_height - mb_pos;
434 if (mb_count >
left) {
437 }
438
439 if (whole_size < s->mb_width *
s->mb_height / 8)
441
442 if ((
s->mb_x == 0 &&
s->mb_y == 0) || !
s->cur_pic.ptr) {
443 // FIXME write parser so we always have complete frames?
444 if (
s->cur_pic.ptr) {
447 s->mb_x =
s->mb_y =
s->resync_mb_x =
s->resync_mb_y = 0;
448 }
452 } else {
453 if (
s->cur_pic.ptr->f->pict_type !=
s->pict_type) {
456 }
457 }
458
459
460 ff_dlog(avctx,
"qscale=%d\n",
s->qscale);
461
462 /* default quantization values */
465 s->first_slice_line = 1;
466 } else {
467 s->first_slice_line = 1;
468 s->resync_mb_x =
s->mb_x;
469 }
470 start_mb_x =
s->mb_x;
471 s->resync_mb_y =
s->mb_y;
473 s->y_dc_scale_table =
475 } else {
476 s->y_dc_scale_table =
478 }
479
480 if (
s->modified_quant)
482
484
485 s->rv10_first_dc_coded[0] = 0;
486 s->rv10_first_dc_coded[1] = 0;
487 s->rv10_first_dc_coded[2] = 0;
489
490 /* decode each macroblock */
491 for (
s->mb_num_left = mb_count;
s->mb_num_left > 0;
s->mb_num_left--) {
494 ff_tlog(avctx,
"**mb x=%d y=%d\n",
s->mb_x,
s->mb_y);
495
499
500 // Repeat the slice end check from ff_h263_decode_mb with our active
501 // bitstream size
504
507
508 if (!v)
510 }
513 active_bits_size = buf_size2 * 8;
515 8 * buf_size, active_bits_size);
517 }
518
523 }
529
530 if (++
s->mb_x ==
s->mb_width) {
534 }
535 if (
s->mb_x ==
s->resync_mb_x)
536 s->first_slice_line = 0;
538 break;
539 }
540
543
544 return active_bits_size;
545 }
546
548 {
550 }
551
554 {
555 const uint8_t *buf = avpkt->
data;
556 int buf_size = avpkt->
size;
559 int slice_count;
560 const uint8_t *slices_hdr =
NULL;
561
562 ff_dlog(avctx,
"*****frame %"PRId64
" size=%d\n", avctx->
frame_num, buf_size);
563
564 /* no supplementary picture */
565 if (buf_size == 0) {
566 return 0;
567 }
568
569 slice_count = (*buf++) + 1;
570 buf_size--;
571
572 if (!slice_count || buf_size <= 8 * slice_count) {
574 slice_count);
576 }
577
578 slices_hdr = buf + 4;
579 buf += 8 * slice_count;
580 buf_size -= 8 * slice_count;
581
582 for (
i = 0;
i < slice_count;
i++) {
585
588
589 if (
i + 1 == slice_count)
591 else
593
594 if (
i + 2 >= slice_count)
595 size2 = buf_size -
offset;
596 else
598
599 if (
size <= 0 || size2 <= 0 ||
602
605
608 }
609
610 if (
s->cur_pic.ptr &&
s->mb_y >=
s->mb_height) {
613
619 }
else if (
s->last_pic.ptr) {
624 }
625
626 if (
s->last_pic.ptr ||
s->low_delay) {
627 *got_frame = 1;
628 }
629
630 // so we can detect if frame_end was not called (find some nicer solution...)
632 }
633
635 }
636
647 .p.max_lowres = 3,
649 };
650
662 .p.max_lowres = 3,
664 };
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