Go to the documentation of this file. 1 /*
2 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * VP5 and VP6 compatible video decoder (common features)
24 */
25
34
35
37 {
38 if (
s->quantizer != quantizer)
40 s->quantizer = quantizer;
43 }
44
47 {
48 int nb_pred = 0;
49 VP56mv vect[2] = {{0,0}, {0,0}};
52
56 if (
mvp.x < 0 ||
mvp.x >=
s->mb_width ||
57 mvp.y < 0 ||
mvp.y >=
s->mb_height)
58 continue;
60
62 continue;
63 if ((
s->macroblocks[
offset].mv.x == vect[0].
x &&
64 s->macroblocks[
offset].mv.y == vect[0].
y) ||
65 (
s->macroblocks[
offset].mv.x == 0 &&
66 s->macroblocks[
offset].mv.y == 0))
67 continue;
68
69 vect[nb_pred++] =
s->macroblocks[
offset].mv;
70 if (nb_pred > 1) {
71 nb_pred = -1;
72 break;
73 }
74 s->vector_candidate_pos =
pos;
75 }
76
77 s->vector_candidate[0] = vect[0];
78 s->vector_candidate[1] = vect[1];
79
80 return nb_pred+1;
81 }
82
84 {
88
95 }
101
107 }
108 }
109 }
110 }
111 }
112
113 /* compute MB type probability tables based on previous MB type */
116
119
121 int p02, p34, p0234, p17, p56, p89, p5689, p156789;
122
123 /* conservative MB type probability */
125
126 p[
type] = 0;
/* same MB type => weight is null */
127
128 /* binary tree parsing probabilities */
131 p0234 = p02 + p34;
135 p5689 = p56 + p89;
136 p156789 = p17 + p5689;
137
147
148 /* restore initial value */
150 }
151 }
152 }
153
156 {
157 uint8_t *mb_type_model =
s->modelp->mb_type[
ctx][prev_type];
159
161 return prev_type;
162 else
164 }
165
167 {
171
172 /* parse each block type */
173 for (
b=0;
b<4;
b++) {
176 type[
b]++;
/* only returns 0, 2, 3 or 4 (all INTER_PF) */
177 }
178
179 /* get vectors */
180 for (
b=0;
b<4;
b++) {
184 break;
186 s->parse_vector_adjustment(
s, &
s->mv[
b]);
187 break;
189 s->mv[
b] =
s->vector_candidate[0];
190 break;
192 s->mv[
b] =
s->vector_candidate[1];
193 break;
194 }
197 }
198
199 /* this is the one selected for the whole MB for prediction */
200 s->macroblocks[row *
s->mb_width + col].mv =
s->mv[3];
201
202 /* chroma vectors are average luma vectors */
205 }
206
208 {
211
214 s->macroblocks[row *
s->mb_width + col].type =
s->mb_type;
215
216 switch (
s->mb_type) {
218 mv = &
s->vector_candidate[0];
219 break;
220
222 mv = &
s->vector_candidate[1];
223 break;
224
227 mv = &
s->vector_candidate[0];
228 break;
229
232 mv = &
s->vector_candidate[1];
233 break;
234
236 s->parse_vector_adjustment(
s, &vect);
238 break;
239
242 s->parse_vector_adjustment(
s, &vect);
244 break;
245
249
250 default:
252 break;
253 }
254
255 s->macroblocks[row*
s->mb_width + col].mv = *
mv;
256
257 /* same vector for all blocks */
260
262 }
263
265 {
268
270 s->macroblocks[row *
s->mb_width + col].type =
s->mb_type;
271
273
274 s->macroblocks[row*
s->mb_width + col].mv = *
mv;
275
276 /* same vector for all blocks */
279
281 }
282
284 {
285 int idx =
s->idct_scantable[0];
287
288 for (
b=0;
b<6;
b++) {
289 VP56RefDc *ab = &
s->above_blocks[
s->above_block_idx[
b]];
291 int count = 0;
294
297 count++;
298 }
301 count++;
302 }
307 count++;
308 }
309 if (count == 0)
311 else if (count == 2)
313
314 s->block_coeff[
b][idx] +=
dc;
320 s->block_coeff[
b][idx] *=
s->dequant_dc;
321 }
322 }
323
325 ptrdiff_t
stride,
int dx,
int dy)
326 {
329 if (dx)
s->vp56dsp.edge_filter_hor(yuv + 10-dx ,
stride, t);
330 if (dy)
s->vp56dsp.edge_filter_ver(yuv +
stride*(10-dy),
stride, t);
331 } else {
332 int * bounding_values =
s->bounding_values_array + 127;
333 if (dx)
335 if (dy)
337 }
338 }
339
341 ptrdiff_t
stride,
int x,
int y, ptrdiff_t ref_stride)
342 {
344 uint8_t *src_block;
345 int src_offset;
346 int overlap_offset = 0;
347 int mask =
s->vp56_coord_div[
b] - 1;
348 int deblock_filtering =
s->deblock_filtering;
349 int dx;
350 int dy;
351
355 deblock_filtering = 0;
356
357 dx =
s->mv[
b].x /
s->vp56_coord_div[
b];
358 dy =
s->mv[
b].y /
s->vp56_coord_div[
b];
359
361 x /= 2;
362 y /= 2;
363 }
364 x += dx - 2;
365 y += dy - 2;
366
367 if (
s->interlaced &&
s->il_block) {
368 /* extract 12*(4+16+4) block from frame (containing both fields), then treat src_block as specific field */
369 s->vdsp.emulated_edge_mc(
s->edge_emu_buffer,
370 src +
s->block_offset[
b] + (dy-4)*ref_stride + (dx-2),
371 ref_stride, ref_stride,
372 12, 24, x, y - 2,
373 s->plane_width[plane],
374 s->plane_height[plane]);
375 src_block =
s->edge_emu_buffer;
376 src_offset = 2 + 4*ref_stride;
377 }
else if (x<0 || x+12>=
s->plane_width[plane] ||
378 y<0 || y+12>=
s->plane_height[plane]) {
379 s->vdsp.emulated_edge_mc(
s->edge_emu_buffer,
382 12, 12, x, y,
383 s->plane_width[plane],
384 s->plane_height[plane]);
385 src_block =
s->edge_emu_buffer;
386 src_offset = 2 + 2*
stride;
387 } else if (deblock_filtering) {
388 /* only need a 12x12 block, but there is no such dsp function, */
389 /* so copy a 16x12 block */
390 s->hdsp.put_pixels_tab[0][0](
s->edge_emu_buffer,
393 src_block =
s->edge_emu_buffer;
394 src_offset = 2 + 2*
stride;
395 } else {
397 src_offset =
s->block_offset[
b] + dy*
stride + dx;
398 }
399
400 if (deblock_filtering)
402
404 overlap_offset += (
s->mv[
b].x > 0) ? 1 : -1;
407
408 if (overlap_offset) {
410 s->filter(
s,
dst, src_block, src_offset, src_offset+overlap_offset,
412 else
413 s->vp3dsp.put_no_rnd_pixels_l2(
dst, src_block+src_offset,
414 src_block+src_offset+overlap_offset,
416 } else {
417 s->hdsp.put_pixels_tab[1][0](
dst, src_block+src_offset,
stride, 8);
418 }
419 }
420
422 {
423 if (selector > 10 || selector == 1)
425 else
427 }
428
430 {
431 if (selector > 10)
433 else if (selector > 1)
435 else
437 }
438
440 {
441 int b, ab, b_max, plane, off;
444 ptrdiff_t ref_stride[4];
445
447
451 return;
452
453 memcpy(ref_stride,
s->stride,
sizeof(
s->stride));
454 if (
s->interlaced &&
s->il_block) {
455 s->block_offset[2] -=
s->stride[0] * 7;
456 s->block_offset[3] -=
s->stride[0] * 7;
458 }
459
460 ab = 6*is_alpha;
461 b_max = 6 - 2*is_alpha;
462
463 switch (mb_type) {
465 for (
b=0;
b<b_max;
b++) {
468 s->stride[plane],
s->block_coeff[
b],
s->idct_selector[
b]);
469 }
470 break;
471
474 for (
b=0;
b<b_max;
b++) {
476 off =
s->block_offset[
b];
477 s->hdsp.put_pixels_tab[1][0](frame_current->
data[plane] + off,
479 s->stride[plane], 8);
481 s->stride[plane],
s->block_coeff[
b],
s->idct_selector[
b]);
482 }
483 break;
484
492 for (
b=0;
b<b_max;
b++) {
493 int x_off =
b==1 ||
b==3 ? 8 : 0;
494 int y_off =
b==2 ||
b==3 ? (
s->interlaced &&
s->il_block ? 1 : 8) : 0;
497 16*col+x_off, 16*row+y_off, ref_stride[plane]);
499 s->stride[plane],
s->block_coeff[
b],
s->idct_selector[
b]);
500 }
501 break;
502 }
503
504 if (is_alpha) {
505 s->block_coeff[4][0] = 0;
506 s->block_coeff[5][0] = 0;
507 }
508
509 if (
s->interlaced &&
s->il_block) {
511 s->block_offset[2] +=
s->stride[0] * 7;
512 s->block_offset[3] +=
s->stride[0] * 7;
513 }
514 }
515
517 {
520
522 int prob =
s->il_prob;
523
524 if (col > 0) {
527 else
528 prob += (256 -
prob) >> 1;
/* can be simplified/combined */
529 }
530
532 }
533
536 else
538
539 ret =
s->parse_coeff(
s);
542
544
545 return 0;
546 }
547
549 {
551
554 else
556
558
559 return 0;
560 }
561
563 {
567
569 s->plane_width[1] =
s->plane_width[2] = avctx->
coded_width/2;
572
573 s->have_undamaged_frame = 0;
574
577
580
581 if (
s->mb_width > 1000 ||
s->mb_height > 1000) {
585 }
586
588 sizeof(*
s->above_blocks));
590 sizeof(*
s->macroblocks));
593 s->edge_emu_buffer =
s->edge_emu_buffer_alloc;
594 if (!
s->above_blocks || !
s->macroblocks || !
s->edge_emu_buffer_alloc)
597 s->edge_emu_buffer += 15 *
stride * 2;
598
599 if (
s->alpha_context)
601
602 return 0;
603 }
604
606
609 {
610 const uint8_t *buf = avpkt->
data;
613 int remaining_buf_size = avpkt->
size;
614 int alpha_offset = remaining_buf_size;
617
619 if (remaining_buf_size < 3)
621 alpha_offset = bytestream_get_be24(&buf);
622 remaining_buf_size -= 3;
623 if (remaining_buf_size < alpha_offset)
625 }
626
627 res =
s->parse_header(
s, buf, alpha_offset);
628 if (res < 0)
629 return res;
630
632 for (
i = 0;
i < 4;
i++) {
634 if (
s->alpha_context)
636 }
638 }
639
645 }
646
653 }
654 }
655
660 }
661 }
662
664 int bak_w = avctx->
width;
665 int bak_h = avctx->
height;
668 buf += alpha_offset;
669 remaining_buf_size -= alpha_offset;
670
671 res =
s->alpha_context->parse_header(
s->alpha_context, buf, remaining_buf_size);
672 if (res != 0) {
675 avctx->
width = bak_w;
679 }
682 }
683 }
684
685 s->discard_frame = 0;
687
688 if (
s->discard_frame)
690
692 return res;
693 *got_frame = 1;
694
696 }
697
699 int jobnr, int threadnr)
700 {
702 int is_alpha = (jobnr == 1);
703 VP56Context *
s = is_alpha ? s0->alpha_context : s0;
705 int mb_row, mb_col, mb_row_flip, mb_offset = 0;
707 ptrdiff_t stride_y, stride_uv;
708 int res;
709 int damaged = 0;
710
713 s->default_models_init(
s);
716 } else {
719 s->parse_vector_models(
s);
721 }
722
723 if (
s->parse_coeff_models(
s))
724 goto next;
725
729 }
730
731 memset(
s->prev_dc, 0,
sizeof(
s->prev_dc));
734
737 s->above_blocks[
block].dc_coeff = 0;
738 s->above_blocks[
block].not_null_dc = 0;
739 }
742
743 stride_y =
p->linesize[0];
744 stride_uv =
p->linesize[1];
745
747 mb_offset = 7;
748
749 /* main macroblocks loop */
750 for (mb_row=0; mb_row<
s->mb_height; mb_row++) {
752 mb_row_flip =
s->mb_height - mb_row - 1;
753 else
754 mb_row_flip = mb_row;
755
758 s->left_block[
block].dc_coeff = 0;
759 s->left_block[
block].not_null_dc = 0;
760 }
761 memset(
s->coeff_ctx, 0,
sizeof(
s->coeff_ctx));
762 memset(
s->coeff_ctx_last, 24,
sizeof(
s->coeff_ctx_last));
763
764 s->above_block_idx[0] = 1;
765 s->above_block_idx[1] = 2;
766 s->above_block_idx[2] = 1;
767 s->above_block_idx[3] = 2;
768 s->above_block_idx[4] = 2*
s->mb_width + 2 + 1;
769 s->above_block_idx[5] = 3*
s->mb_width + 4 + 1;
770
771 s->block_offset[
s->frbi] = (mb_row_flip*16 + mb_offset) * stride_y;
772 s->block_offset[
s->srbi] =
s->block_offset[
s->frbi] + 8*stride_y;
773 s->block_offset[1] =
s->block_offset[0] + 8;
774 s->block_offset[3] =
s->block_offset[2] + 8;
775 s->block_offset[4] = (mb_row_flip*8 + mb_offset) * stride_uv;
776 s->block_offset[5] =
s->block_offset[4];
777
778 for (mb_col=0; mb_col<
s->mb_width; mb_col++) {
779 if (!damaged) {
782 damaged = 1;
784 s->discard_frame = 1;
786 }
787 }
788 }
789 if (damaged)
791
792 for (y=0; y<4; y++) {
793 s->above_block_idx[y] += 2;
794 s->block_offset[y] += 16;
795 }
796
797 for (uv=4; uv<6; uv++) {
798 s->above_block_idx[uv] += 1;
799 s->block_offset[uv] += 8;
800 }
801 }
802 }
803
804 if (!damaged)
805 s->have_undamaged_frame = 1;
806
807 next:
810 return res;
811 }
812
816 return 0;
817 }
818
820 int flip,
int has_alpha)
821 {
823
827
832 for (
i = 0;
i < 64;
i++) {
833 #define TRANSPOSE(x) (((x) >> 3) | (((x) & 7) << 3))
835 #undef TRANSPOSE
836 }
837
842 }
843 s->edge_emu_buffer_alloc =
NULL;
844
845 s->above_blocks =
NULL;
846 s->macroblocks =
NULL;
848 s->deblock_filtering = 1;
850
852
853 s->has_alpha = has_alpha;
854
855 s->modelp = &
s->model;
856
861 } else {
865 }
866
867 return 0;
868 }
869
871 {
873
877
880
881 return 0;
882 }
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 top and top right vectors is used as motion vector prediction the used motion vector is the sum of the predictor and(mvx_diff, mvy_diff) *mv_scale Intra DC Prediction block[y][x] dc[1]