Go to the documentation of this file. 1 /*
2 * ITU H.263 bitstream decoder
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * H.263+ support.
5 * Copyright (c) 2001 Juan J. Sierralta P
6 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 /**
26 * @file
27 * H.263 decoder.
28 */
29
30 #define UNCHECKED_BITSTREAM_READER 1
31
32 #include "config_components.h"
33
54
55 // The defines below define the number of bits that are read at once for
56 // reading vlc values. Changing these may improve speed and data cache needs
57 // be aware though that decreasing them may need the number of stages that is
58 // passed to get_vlc* to be increased.
59 #define H263_MBTYPE_B_VLC_BITS 6
60 #define CBPC_B_VLC_BITS 3
61
75 0, //stuffing
78 };
79
84 s->gb.size_in_bits, 1-
s->no_rounding,
86 s->umvplus ?
" UMV" :
"",
87 s->h263_long_vectors ?
" LONG" :
"",
88 s->h263_plus ?
" +" :
"",
89 s->h263_aic ?
" AIC" :
"",
90 s->alt_inter_vlc ?
" AIV" :
"",
91 s->modified_quant ?
" MQ" :
"",
92 s->loop_filter ?
" LOOP" :
"",
93 s->h263_slice_structured ?
" SS" :
"",
94 s->avctx->framerate.num,
s->avctx->framerate.den
95 );
96 }
97 }
98
99 /***********************************************/
100 /* decoding */
101
108
109 /* init vlcs */
110
112 {
134 }
135
137 {
140 }
141
143 {
145
146 for (
i = 0;
i < 6;
i++)
148 break;
150 s->mb_x = mb_pos %
s->mb_width;
151 s->mb_y = mb_pos /
s->mb_width;
152
153 return mb_pos;
154 }
155
156 /**
157 * Decode the group of blocks header or slice header.
158 * @return <0 if an error occurred
159 */
161 {
162 unsigned int val, gob_number;
164
165 /* Check for GOB Start Code */
168 return -1;
169
170 /* We have a GBSC probably with GSTUFF */
174 //MN: we must check the bits left or we might end in an infinite loop (or segfault)
176 if(
get_bits1(&
s->gb))
break;
/* Seek the '1' bit */
177 }
179 return -1;
180
181 if(
s->h263_slice_structured){
183 return -1;
184
186
189 return -1;
190
193 return -1;
195 }else{
196 gob_number =
get_bits(&
s->gb, 5);
/* GN */
198 s->mb_y=
s->gob_index* gob_number;
201 }
202
203 if(
s->mb_y >=
s->mb_height)
204 return -1;
205
207 return -1;
208
209 return 0;
210 }
211
212 /**
213 * Decode the group of blocks / video packet header / slice header (MPEG-4 Studio).
214 * @return bit position of the resync_marker, or <0 if none was found
215 */
218
219 /* In MPEG-4 studio mode look for a new slice startcode
220 * and decode slice header */
223
226 }
227
230 else
231 return -1;
232 }
233
237 }
238
243 else
247 }
248 //OK, it's not where it is supposed to be ...
249 s->gb=
s->last_resync_gb;
252
256
260 else
264
266 }
268 }
269
270 return -1;
271 }
272
274 {
277
281 return 0xffff;
282
290 }
291 if (sign)
294
295 /* modulo decoding */
296 if (!
s->h263_long_vectors) {
298 } else {
299 /* horrible H.263 long vector mode */
304
305 }
307 }
308
309
310 /* Decode RVLC of H.263+ UMV */
312 {
314
315 if (
get_bits1(&
s->gb))
/* Motion difference = 0 */
317
319
321 {
326 return 0xffff;
327 }
328 }
331
335
336 }
337
338 /**
339 * read the next MVs for OBMC. yes this is an ugly hack, feel free to send a patch :)
340 */
343
344 int cbpc,
i, pred_x, pred_y, mx, my;
345 int16_t *mot_val;
346 const int xy=
s->mb_x + 1 +
s->mb_y *
s->mb_stride;
347 const int stride=
s->b8_stride*2;
348
350 s->block_index[
i]+= 2;
352 s->block_index[
i]+= 1;
354
356
357 do{
359 /* skip mb */
360 mot_val =
s->current_picture.motion_val[0][
s->block_index[0]];
361 mot_val[0 ]= mot_val[2 ]=
363 mot_val[1 ]= mot_val[3 ]=
365
367 goto end;
368 }
370 }while(cbpc == 20);
371
372 if(cbpc & 4){
374 }else{
376 if (cbpc & 8) {
377 if(
s->modified_quant){
380 }else
382 }
383
384 if ((cbpc & 16) == 0) {
386 /* 16x16 motion prediction */
390 else
392
395 else
397
398 mot_val[0 ]= mot_val[2 ]=
400 mot_val[1 ]= mot_val[3 ]=
402 } else {
408 else
410
413 else
415 if (
s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
416 skip_bits1(&
s->gb);
/* Bit stuffing to prevent PSC */
417 mot_val[0] = mx;
418 mot_val[1] = my;
419 }
420 }
421 }
422 end:
423
425 s->block_index[
i]-= 2;
427 s->block_index[
i]-= 1;
429
431 }
432
434 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
435
436 if(
s->modified_quant){
439 else
441 }else
444 }
445
447 {
449 int16_t *dc_val, *ac_val, *ac_val1;
450
451 /* find prediction */
452 if (n < 4) {
453 x = 2 *
s->mb_x + (n & 1);
454 y = 2 *
s->mb_y + (n>> 1);
456 dc_val =
s->dc_val[0];
457 ac_val =
s->ac_val[0][0];
459 } else {
463 dc_val =
s->dc_val[n - 4 + 1];
464 ac_val =
s->ac_val[n - 4 + 1][0];
466 }
467
468 ac_val += ((y) *
wrap + (x)) * 16;
469 ac_val1 = ac_val;
470
471 /* B C
472 * A X
473 */
474 a = dc_val[(x - 1) + (y) *
wrap];
475 c = dc_val[(x) + (y - 1) *
wrap];
476
477 /* No prediction outside GOB boundary */
478 if (
s->first_slice_line && n != 3) {
480 if (n != 1 &&
s->mb_x ==
s->resync_mb_x)
a= 1024;
481 }
482
485 if (
s->h263_aic_dir) {
486 /* left prediction */
488 ac_val -= 16;
489 for (
int i = 1;
i < 8;
i++) {
490 block[
s->idsp.idct_permutation[
i << 3]] += ac_val[
i];
491 }
493 }
494 } else {
495 /* top prediction */
498 for (
int i = 1;
i < 8;
i++) {
499 block[
s->idsp.idct_permutation[
i]] += ac_val[
i + 8];
500 }
502 }
503 }
504 } else {
505 /* just DC prediction */
506 if (
a != 1024 &&
c != 1024)
510 else
512 }
513
514 /* we assume pred is positive */
516
519 else
521
522 /* Update AC/DC tables */
524
525 /* left copy */
526 for (
int i = 1;
i < 8;
i++)
527 ac_val1[
i] =
block[
s->idsp.idct_permutation[
i << 3]];
528 /* top copy */
529 for (
int i = 1;
i < 8;
i++)
530 ac_val1[8 +
i] =
block[
s->idsp.idct_permutation[
i]];
531 }
532
534 int n, int coded)
535 {
538 const uint8_t *scan_table;
540
541 scan_table =
s->intra_scantable.permutated;
542 if (
s->h263_aic &&
s->mb_intra) {
547 scan_table =
s->permutated_intra_v_scantable;
/* left */
548 else
549 scan_table =
s->permutated_intra_h_scantable;
/* top */
550 }
551 }
else if (
s->mb_intra) {
552 /* DC coef */
556 component = (n <= 3 ? 0 : n - 4 + 1);
557 level =
s->last_dc[component];
558 if (
s->rv10_first_dc_coded[component]) {
561 return -1;
564 s->last_dc[component] =
level;
565 } else {
566 s->rv10_first_dc_coded[component] = 1;
567 }
568 } else {
572 }
573 }else{
575 if((
level&0x7F) == 0){
578 return -1;
579 }
582 }
585 } else {
587 }
588 if (!coded) {
589 if (
s->mb_intra &&
s->h263_aic)
590 goto not_coded;
591 s->block_last_index[n] =
i - 1;
592 return 0;
593 }
594 retry:
595 {
597 i--;
// offset by -1 to allow direct indexing of scan_table
598 for(;;) {
605 return -1;
606 }
607 /* escape */
608 if (CONFIG_FLV_DECODER &&
s->h263_flv > 1) {
612 if (is11) {
617 } else {
621 }
622 } else {
630 /* XXX: should patch encoder too */
633 }else{
638 }
639 }
640 }
641 } else {
645 }
649 // redo update without last flag, revert -1 offset
652 // only last marker, no overrun
654 break;
655 }
657 //Looks like a hack but no, it's the way it is supposed to work ...
661 s->bdsp.clear_block(
block);
662 goto retry;
663 }
665 return -1;
666 }
669 }
670 }
671 not_coded:
672 if (
s->mb_intra &&
s->h263_aic) {
675 }
676 s->block_last_index[n] =
i;
677 return 0;
678 }
679
681 {
684 int bli[6];
685
686 /* we have to set s->mb_intra to zero to decode B-part of PB-frame correctly
687 * but real value should be restored in order to be used later (in OBMC condition)
688 */
690 memcpy(bli,
s->block_last_index,
sizeof(bli));
692 for (
i = 0;
i < 6;
i++) {
694 return -1;
695 cbp+=cbp;
696 }
698 memcpy(
s->block_last_index, bli,
sizeof(bli));
699 return 0;
700 }
701
703 {
705
706 if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame
708 if (pb_frame == 2 &&
c)
710 } else { // h.263 Annex M improved PB-frame
714 }
718 }
719
720 #define tab_size ((signed)FF_ARRAY_ELEMS(s->direct_scale_mv[0]))
721 #define tab_bias (tab_size / 2)
723 {
724 int xy =
s->block_index[
i];
725 uint16_t time_pp =
s->pp_time;
726 uint16_t time_pb =
s->pb_time;
727 int p_mx, p_my;
728
731 s->mv[0][
i][0] =
s->direct_scale_mv[0][p_mx +
tab_bias];
732 s->mv[1][
i][0] =
s->direct_scale_mv[1][p_mx +
tab_bias];
733 } else {
734 s->mv[0][
i][0] = p_mx * time_pb / time_pp;
735 s->mv[1][
i][0] = p_mx * (time_pb - time_pp) / time_pp;
736 }
739 s->mv[0][
i][1] =
s->direct_scale_mv[0][p_my +
tab_bias];
740 s->mv[1][
i][1] =
s->direct_scale_mv[1][p_my +
tab_bias];
741 } else {
742 s->mv[0][
i][1] = p_my * time_pb / time_pp;
743 s->mv[1][
i][1] = p_my * (time_pb - time_pp) / time_pp;
744 }
745 }
746
747 /**
748 * @return the mb_type
749 */
751 {
752 const int mb_index =
s->mb_x +
s->mb_y *
s->mb_stride;
754 int colocated_mb_type = p->
mb_type[mb_index];
756
758 p = &
s->last_picture;
759 colocated_mb_type = p->
mb_type[mb_index];
760 }
761
762 if (
IS_8X8(colocated_mb_type)) {
764 for (
i = 0;
i < 4;
i++)
767 } else {
771 s->mv[0][3][0] =
s->mv[0][0][0];
774 s->mv[0][3][1] =
s->mv[0][0][1];
777 s->mv[1][3][0] =
s->mv[1][0][0];
780 s->mv[1][3][1] =
s->mv[1][0][1];
782 // Note see prev line
784 }
785 }
786
788 int16_t
block[6][64])
789 {
790 int cbpc, cbpy,
i, cbp, pred_x, pred_y, mx, my, dquant;
791 int16_t *mot_val;
792 const int xy=
s->mb_x +
s->mb_y *
s->mb_stride;
793 int cbpb = 0, pb_mv_count = 0;
794
796
798 do{
800 /* skip mb */
803 s->block_last_index[
i] = -1;
809 s->mb_skipped = !(
s->obmc |
s->loop_filter);
810 goto end;
811 }
813 if (cbpc < 0){
816 }
817 }while(cbpc == 20);
818
819 s->bdsp.clear_blocks(
s->block[0]);
820
821 dquant = cbpc & 8;
822 s->mb_intra = ((cbpc & 4) != 0);
823 if (
s->mb_intra)
goto intra;
824
828
829 if (cbpy < 0) {
832 }
833
834 if(
s->alt_inter_vlc==0 || (cbpc & 3)!=3)
835 cbpy ^= 0xF;
836
837 cbp = (cbpc & 3) | (cbpy << 2);
838 if (dquant) {
840 }
841
843 if ((cbpc & 16) == 0) {
845 /* 16x16 motion prediction */
850 else
852
853 if (mx >= 0xffff)
855
858 else
860
861 if (my >= 0xffff)
865
866 if (
s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
867 skip_bits1(&
s->gb);
/* Bit stuffing to prevent PSC */
868 } else {
875 else
877 if (mx >= 0xffff)
879
882 else
884 if (my >= 0xffff)
888 if (
s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
889 skip_bits1(&
s->gb);
/* Bit stuffing to prevent PSC */
890 mot_val[0] = mx;
891 mot_val[1] = my;
892 }
893 }
895 int mb_type;
896 const int stride=
s->b8_stride;
897 int16_t *mot_val0 =
s->current_picture.motion_val[0][2 * (
s->mb_x +
s->mb_y *
stride)];
898 int16_t *mot_val1 =
s->current_picture.motion_val[1][2 * (
s->mb_x +
s->mb_y *
stride)];
899 // const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
900
901 //FIXME ugly
902 mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+2*
stride]= mot_val0[2+2*
stride]=
903 mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+2*
stride]= mot_val0[3+2*
stride]=
904 mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+2*
stride]= mot_val1[2+2*
stride]=
905 mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+2*
stride]= mot_val1[3+2*
stride]= 0;
906
907 do{
909 if (mb_type < 0){
912 }
913
915 }while(!mb_type);
916
919 s->bdsp.clear_blocks(
s->block[0]);
923 goto intra;
924 }
925
927
928 if (cbpy < 0){
931 }
932
933 if(
s->alt_inter_vlc==0 || (cbpc & 3)!=3)
934 cbpy ^= 0xF;
935
936 cbp = (cbpc & 3) | (cbpy << 2);
937 }else
938 cbp=0;
939
941
944 }
945
949 }else{
952 //FIXME UMV
953
957
960 else
962 if (mx >= 0xffff)
964
967 else
969 if (my >= 0xffff)
971
972 if (
s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
973 skip_bits1(&
s->gb);
/* Bit stuffing to prevent PSC */
974
977 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*
stride]= mot_val[2+2*
stride]= mx;
978 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*
stride]= mot_val[3+2*
stride]= my;
979 }
980
984
987 else
989 if (mx >= 0xffff)
991
994 else
996 if (my >= 0xffff)
998
999 if (
s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
1000 skip_bits1(&
s->gb);
/* Bit stuffing to prevent PSC */
1001
1002 s->mv[1][0][0] = mx;
1003 s->mv[1][0][1] = my;
1004 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*
stride]= mot_val[2+2*
stride]= mx;
1005 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*
stride]= mot_val[3+2*
stride]= my;
1006 }
1007 }
1008
1009 s->current_picture.mb_type[xy] = mb_type;
1010 } else { /* I-Frame */
1011 do{
1013 if (cbpc < 0){
1016 }
1017 }while(cbpc == 8);
1018
1019 s->bdsp.clear_blocks(
s->block[0]);
1020
1021 dquant = cbpc & 4;
1023 intra:
1029
1031 }
1032 }else
1034
1038 if(cbpy<0){
1041 }
1042 cbp = (cbpc & 3) | (cbpy << 2);
1043 if (dquant) {
1045 }
1046
1047 pb_mv_count += !!
s->pb_frame;
1048 }
1049
1050 while(pb_mv_count--){
1053 }
1054
1055 /* decode each block */
1056 for (
i = 0;
i < 6;
i++) {
1058 return -1;
1059 cbp+=cbp;
1060 }
1061
1063 return -1;
1064 if(
s->obmc && !
s->mb_intra){
1067 }
1068 end:
1069
1072
1073 /* per-MB end of slice check */
1074 {
1076
1079 }
1080
1081 if(v==0)
1083 }
1084
1086 }
1087
1088 /* Most is hardcoded; should extend to handle all H.263 streams. */
1090 {
1092 uint32_t startcode;
1093
1095
1096 if (
show_bits(&
s->gb, 2) == 2 &&
s->avctx->frame_num == 0) {
1098 }
1099
1101
1103 startcode = ((startcode << 8) |
get_bits(&
s->gb, 8)) & 0x003FFFFF;
1104
1105 if(startcode == 0x20)
1106 break;
1107 }
1108
1109 if (startcode != 0x20) {
1111 return -1;
1112 }
1113 /* temporal reference */
1114 i =
get_bits(&
s->gb, 8);
/* picture timestamp */
1115
1116 i -= (
i - (
s->picture_number & 0xFF) + 128) & ~0xFF;
1117
1118 s->picture_number= (
s->picture_number&~0xFF) +
i;
1119
1120 /* PTYPE starts here */
1122 return -1;
1123 }
1126 return -1; /* H.263 id */
1127 }
1130 skip_bits1(&
s->gb);
/* freeze picture release off */
1131
1133 /*
1134 0 forbidden
1135 1 sub-QCIF
1136 10 QCIF
1137 7 extended PTYPE (PLUSPTYPE)
1138 */
1139
1142 /* H.263v1 */
1146 return -1;
1147
1149
1151
1154 return -1; /* SAC: off */
1155 }
1156 s->obmc=
get_bits1(&
s->gb);
/* Advanced prediction mode */
1157
1159 s->chroma_qscale=
s->qscale =
get_bits(&
s->gb, 5);
1160 skip_bits1(&
s->gb);
/* Continuous Presence Multipoint mode: off */
1161
1164 s->avctx->sample_aspect_ratio= (
AVRational){12,11};
1165 s->avctx->framerate = (
AVRational){ 30000, 1001 };
1166 } else {
1167 int ufep;
1168
1169 /* H.263v2 */
1171 ufep =
get_bits(&
s->gb, 3);
/* Update Full Extended PTYPE */
1172
1173 /* ufep other than 0 and 1 are reserved */
1174 if (ufep == 1) {
1175 /* OPPTYPE */
1179 s->umvplus =
get_bits1(&
s->gb);
/* Unrestricted Motion Vector */
1182 }
1183 s->obmc=
get_bits1(&
s->gb);
/* Advanced prediction mode */
1184 s->h263_aic =
get_bits1(&
s->gb);
/* Advanced Intra Coding (AIC) */
1186 if(
s->avctx->lowres)
1188
1192 }
1195 }
1198 if(
s->modified_quant)
1200
1201 skip_bits(&
s->gb, 1);
/* Prevent start code emulation */
1202
1204 } else if (ufep != 0) {
1206 return -1;
1207 }
1208
1209 /* MPPTYPE */
1211 switch(
s->pict_type){
1217 default:
1218 return -1;
1219 }
1223
1224 /* Get the picture dimensions */
1225 if (ufep) {
1227 /* Custom Picture Format (CPFMT) */
1228 int aspect_ratio_info =
get_bits(&
s->gb, 4);
1229 ff_dlog(
s->avctx,
"aspect: %d\n", aspect_ratio_info);
1230 /* aspect ratios:
1231 0 - forbidden
1232 1 - 1:1
1233 2 - 12:11 (CIF 4:3)
1234 3 - 10:11 (525-type 4:3)
1235 4 - 16:11 (CIF 16:9)
1236 5 - 40:33 (525-type 16:9)
1237 6-14 - reserved
1238 */
1244 /* expected dimensions */
1245 s->avctx->sample_aspect_ratio.num=
get_bits(&
s->gb, 8);
1246 s->avctx->sample_aspect_ratio.den=
get_bits(&
s->gb, 8);
1247 }else{
1249 }
1250 } else {
1253 s->avctx->sample_aspect_ratio= (
AVRational){12,11};
1254 }
1255 s->avctx->sample_aspect_ratio.den <<=
s->ehc_mode;
1257 return -1;
1260
1262 int gcd;
1263 s->avctx->framerate.num = 1800000;
1264 s->avctx->framerate.den = 1000 +
get_bits1(&
s->gb);
1265 s->avctx->framerate.den *=
get_bits(&
s->gb, 7);
1266 if(
s->avctx->framerate.den == 0){
1268 return -1;
1269 }
1270 gcd=
av_gcd(
s->avctx->framerate.den,
s->avctx->framerate.num);
1271 s->avctx->framerate.den /= gcd;
1272 s->avctx->framerate.num /= gcd;
1273 }else{
1274 s->avctx->framerate = (
AVRational){ 30000, 1001 };
1275 }
1276 }
1277
1279 skip_bits(&
s->gb, 2);
//extended Temporal reference
1280 }
1281
1282 if (ufep) {
1284 if(
get_bits1(&
s->gb)==0)
/* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
1286 }
1287 if(
s->h263_slice_structured){
1290 }
1293 }
1294 }
1297 if (ufep == 1) {
1299 }
1300 }
1301 }
1302
1304 }
1305
1308
1312 }
1313
1314 s->mb_width = (
s->width + 15) / 16;
1315 s->mb_height = (
s->height + 15) / 16;
1316 s->mb_num =
s->mb_width *
s->mb_height;
1317
1319 skip_bits(&
s->gb, 3);
/* Temporal reference for B-pictures */
1321 skip_bits(&
s->gb, 2);
//extended Temporal reference
1322 skip_bits(&
s->gb, 2);
/* Quantization information for B-pictures */
1323 }
1324
1326 s->time =
s->picture_number;
1327 s->pp_time =
s->time -
s->last_non_b_time;
1328 s->last_non_b_time =
s->time;
1329 }else{
1330 s->time =
s->picture_number;
1331 s->pb_time =
s->pp_time - (
s->last_non_b_time -
s->time);
1332 if (
s->pp_time <=
s->pb_time ||
1333 s->pp_time <=
s->pp_time -
s->pb_time ||
1337 }
1339 }
1340
1341 /* PEI */
1344
1345 if(
s->h263_slice_structured){
1347 return -1;
1348 }
1349
1351
1353 return -1;
1354 }
1355 }
1357
1360
1362 s->y_dc_scale_table=
1364 }else{
1365 s->y_dc_scale_table=
1367 }
1368
1374 for(
i=0;
i<13;
i++){
1375 for(j=0; j<3; j++){
1379 }
1381 }
1383 }
1384
1385 return 0;
1386 }
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