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(
h->c.avctx,
"pict_type=%d pb_frame=%d\n",
h->c.pict_type, pb_frame);
117
118 if (pb_frame) {
121 }
122
124 if (
h->c.qscale == 0) {
127 }
128
130 if (
h->rv10_version == 3) {
131 /* specific MPEG like DC coding not used */
135 ff_dlog(
h->c.avctx,
"DC:%d %d %d\n",
h->c.last_dc[0],
136 h->c.last_dc[1],
h->c.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 =
h->c.mb_x +
h->c.mb_y *
h->c.mb_width;
143 if (
show_bits(&
h->gb, 12) == 0 || (mb_xy && mb_xy < h->
c.mb_num)) {
147 } else {
150 mb_count =
h->c.mb_width *
h->c.mb_height;
151 }
153
154 return mb_count;
155 }
156
158 {
163 int seq, mb_pos,
ret;
164 int rpr_max;
165
166 h->c.pict_type = pict_types[
get_bits(&
h->gb, 2)];
167
170 return -1;
171 }
175 }
176
180 }
181
183 if (
h->c.qscale == 0) {
186 }
187
189 h->loop_filter =
get_bits1(&
h->gb) && !
h->c.avctx->lowres;
190
193 else
195
196 rpr_max =
h->c.avctx->extradata[1] & 7;
197 if (rpr_max) {
199 int rpr_bits =
av_log2(rpr_max) + 1;
200
202
204 if (
h->c.avctx->extradata_size < 8 + 2 *
f) {
207 }
208
209 new_w = 4 *
h->c.avctx->extradata[6 + 2 *
f];
210 new_h = 4 *
h->c.avctx->extradata[7 + 2 *
f];
211 } else {
214 }
215 if (new_w !=
h->c.width || new_h !=
h->c.height || !
h->c.context_initialized) {
216 AVRational old_aspect =
h->c.avctx->sample_aspect_ratio;
218 "attempting to change resolution to %dx%d\n", new_w, new_h);
221
222 if (whole_size < (new_w + 15)/16 * ((new_h + 15)/16) / 8)
224
226
227 // attempt to keep aspect during typical resolution switches
234
238
243 }
244
247 }
248 }
251
253
254 seq |=
h->c.time & ~0x7FFF;
255 if (seq -
h->c.time > 0x4000)
256 seq -= 0x8000;
257 if (seq -
h->c.time < -0x4000)
258 seq += 0x8000;
259
260 if (seq !=
h->c.time) {
263 h->c.pp_time =
h->c.time -
h->c.last_non_b_time;
264 h->c.last_non_b_time =
h->c.time;
265 } else {
267 h->c.pb_time =
h->c.pp_time - (
h->c.last_non_b_time -
h->c.time);
268 }
269 }
271 if (
h->c.pp_time <=
h->c.pb_time ||
h->c.pp_time <=
h->c.pp_time -
h->c.pb_time ||
h->c.pp_time<=0) {
273 "messed up order, possible from seeking? skipping current B-frame\n");
274 #define ERROR_SKIP_FRAME -123
276 }
278 }
279
281
283 // binary decoder reads 3+2 bits here but they don't seem to be used
285
288 h->c.y_dc_scale_table =
290 } else {
291 h->c.y_dc_scale_table =
293 }
294 if (!
h->c.avctx->lowres)
296
299 "num:%5d x:%2d y:%2d type:%d qscale:%2d rnd:%d\n",
300 seq,
h->c.mb_x,
h->c.mb_y,
h->c.pict_type,
h->c.qscale,
302 }
303
305
306 return h->c.mb_width *
h->c.mb_height - mb_pos;
307 }
308
310 const uint16_t len_count[15],
311 const uint8_t sym_rl[][2], int sym_rl_elems)
312 {
315 unsigned nb_syms = 0, nb_lens = 0;
316
317 for (
unsigned i = 0;
i < sym_rl_elems;
i++) {
318 unsigned cur_sym = sym_rl[
i][0];
319 for (
unsigned tmp = nb_syms + sym_rl[
i][1]; nb_syms <=
tmp; nb_syms++)
320 syms[nb_syms] = 0xFF & cur_sym--;
321 }
322
323 for (
unsigned i = 0;
i < 15;
i++)
324 for (
unsigned tmp = nb_lens + len_count[
i]; nb_lens <
tmp; nb_lens++)
325 lens[nb_lens] =
i + 2;
328 lens, 1, syms, 2, 2, 0, 0);
329 }
330
332 {
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 }
343 for (
int i = 0;
i < 1 << (
DC_VLC_BITS - 9
/* Length of skip prefix */);
i++) {
344 /* Same as above. */
347 }
348 }
349
351 {
355 int major_ver, minor_ver, micro_ver,
ret;
356
360 }
364
368
371
372 h->h263_long_vectors = avctx->
extradata[3] & 1;
375 h->modified_quant = 1;
377 }
378
382
383 switch (major_ver) {
384 case 1:
385 h->rv10_version = micro_ver ? 3 : 1;
386 h->c.obmc = micro_ver == 2;
387 break;
388 case 2:
389 if (minor_ver >= 2) {
392 }
393 break;
394 default:
398 }
399
403 }
404
405 /* init static VLCs */
407
408 return 0;
409 }
410
412 int buf_size, int buf_size2, int whole_size)
413 {
416 int mb_count, mb_pos,
left, start_mb_x, active_bits_size,
ret;
417
418 active_bits_size = buf_size * 8;
422 else
424 if (mb_count < 0) {
428 }
429
430 if (
h->c.mb_x >=
h->c.mb_width ||
431 h->c.mb_y >=
h->c.mb_height) {
434 }
435 mb_pos =
h->c.mb_y *
h->c.mb_width +
h->c.mb_x;
436 left =
h->c.mb_width *
h->c.mb_height - mb_pos;
437 if (mb_count >
left) {
440 }
441
442 if (whole_size < h->
c.mb_width *
h->c.mb_height / 8)
444
445 if ((
h->c.mb_x == 0 &&
h->c.mb_y == 0) || !
h->c.cur_pic.ptr) {
446 // FIXME write parser so we always have complete frames?
447 if (
h->c.cur_pic.ptr) {
450 h->c.mb_x =
h->c.mb_y =
h->c.resync_mb_x =
h->c.resync_mb_y = 0;
451 }
455 } else {
456 if (
h->c.cur_pic.ptr->f->pict_type !=
h->c.pict_type) {
459 }
460 }
461
462
463 ff_dlog(avctx,
"qscale=%d\n",
h->c.qscale);
464
465 /* default quantization values */
468 h->c.first_slice_line = 1;
469 } else {
470 h->c.first_slice_line = 1;
471 h->c.resync_mb_x =
h->c.mb_x;
472 }
473 start_mb_x =
h->c.mb_x;
474 h->c.resync_mb_y =
h->c.mb_y;
475
477
478 h->rv10_first_dc_coded[0] = 0;
479 h->rv10_first_dc_coded[1] = 0;
480 h->rv10_first_dc_coded[2] = 0;
482
483 /* decode each macroblock */
484 for (
h->mb_num_left = mb_count;
h->mb_num_left > 0;
h->mb_num_left--) {
487 ff_tlog(avctx,
"**mb x=%d y=%d\n",
h->c.mb_x,
h->c.mb_y);
488
492
493 // Repeat the slice end check from ff_h263_decode_mb with our active
494 // bitstream size
497
500
501 if (!v)
503 }
506 active_bits_size = buf_size2 * 8;
508 8 * buf_size, active_bits_size);
510 }
511
516 }
522
523 if (++
h->c.mb_x ==
h->c.mb_width) {
527 }
528 if (
h->c.mb_x ==
h->c.resync_mb_x)
529 h->c.first_slice_line = 0;
531 break;
532 }
533
536
537 return active_bits_size;
538 }
539
541 {
543 }
544
547 {
548 const uint8_t *buf = avpkt->
data;
549 int buf_size = avpkt->
size;
552 int slice_count;
553 const uint8_t *slices_hdr =
NULL;
554
555 ff_dlog(avctx,
"*****frame %"PRId64
" size=%d\n", avctx->
frame_num, buf_size);
556
557 /* no supplementary picture */
558 if (buf_size == 0) {
559 return 0;
560 }
561
562 slice_count = (*buf++) + 1;
563 buf_size--;
564
565 if (!slice_count || buf_size <= 8 * slice_count) {
567 slice_count);
569 }
570
571 slices_hdr = buf + 4;
572 buf += 8 * slice_count;
573 buf_size -= 8 * slice_count;
574
575 for (
i = 0;
i < slice_count;
i++) {
578
581
582 if (
i + 1 == slice_count)
584 else
586
587 if (
i + 2 >= slice_count)
588 size2 = buf_size -
offset;
589 else
591
592 if (
size <= 0 || size2 <= 0 ||
595
598
601 }
602
603 if (
s->cur_pic.ptr &&
s->mb_y >=
s->mb_height) {
606
612 }
else if (
s->last_pic.ptr) {
617 }
618
619 if (
s->last_pic.ptr ||
s->low_delay) {
620 *got_frame = 1;
621 }
622
623 // so we can detect if frame_end was not called (find some nicer solution...)
625 }
626
628 }
629
640 .p.max_lowres = 3,
642 };
643
655 .p.max_lowres = 3,
657 };
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