comparison h263.c @ 78:92ed1c4f94fa libavcodec

Added support to Unrestricted Motion Vectors (UMV) on H.263+ encoding/decoding. According to Annex D. Now MV on H.263+ are 32x32 long.
author pulento
date Tue, 28 Aug 2001 23:32:34 +0000
parents c7767df463f4
children 82e579c37bc3
comparison
equal deleted inserted replaced
77:9e8ae8222ddc 78:92ed1c4f94fa
1 /* 1 /*
2 * H263/MPEG4 backend for ffmpeg encoder and decoder 2 * H263/MPEG4 backend for ffmpeg encoder and decoder
3 * Copyright (c) 2000,2001 Gerard Lantau. 3 * Copyright (c) 2000,2001 Gerard Lantau.
4 * H263+ support for custom picture format. 4 * H263+ support.
5 * Copyright (c) 2001 Juan J. Sierralta P. 5 * Copyright (c) 2001 Juan J. Sierralta P.
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
26 #include "mpeg4data.h" 26 #include "mpeg4data.h"
27 27
28 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, 28 static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
29 int n); 29 int n);
30 static void h263_encode_motion(MpegEncContext * s, int val); 30 static void h263_encode_motion(MpegEncContext * s, int val);
31 static void h263p_encode_umotion(MpegEncContext * s, int val);
31 static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, 32 static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
32 int n); 33 int n);
33 static int h263_decode_motion(MpegEncContext * s, int pred); 34 static int h263_decode_motion(MpegEncContext * s, int pred);
35 static int h263p_decode_umotion(MpegEncContext * s, int pred);
34 static int h263_decode_block(MpegEncContext * s, DCTELEM * block, 36 static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
35 int n, int coded); 37 int n, int coded);
36 static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block, 38 static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
37 int n, int coded); 39 int n, int coded);
40
41 /* This are for H.263+ UMV */
42 /* Shouldn't be here */
43 static int umvplus = 0;
44 static int umvplus_dec = 0;
38 45
39 int h263_get_picture_format(int width, int height) 46 int h263_get_picture_format(int width, int height)
40 { 47 {
41 int format; 48 int format;
42 49
55 return format; 62 return format;
56 } 63 }
57 64
58 void h263_encode_picture_header(MpegEncContext * s, int picture_number) 65 void h263_encode_picture_header(MpegEncContext * s, int picture_number)
59 { 66 {
60 int format, umvplus; 67 int format;
61 68
62 align_put_bits(&s->pb); 69 align_put_bits(&s->pb);
63 put_bits(&s->pb, 22, 0x20); 70 put_bits(&s->pb, 22, 0x20);
64 put_bits(&s->pb, 8, ((s->picture_number * 30 * FRAME_RATE_BASE) / 71 put_bits(&s->pb, 8, ((s->picture_number * 30 * FRAME_RATE_BASE) /
65 s->frame_rate) & 0xff); 72 s->frame_rate) & 0xff);
67 put_bits(&s->pb, 1, 1); /* marker */ 74 put_bits(&s->pb, 1, 1); /* marker */
68 put_bits(&s->pb, 1, 0); /* h263 id */ 75 put_bits(&s->pb, 1, 0); /* h263 id */
69 put_bits(&s->pb, 1, 0); /* split screen off */ 76 put_bits(&s->pb, 1, 0); /* split screen off */
70 put_bits(&s->pb, 1, 0); /* camera off */ 77 put_bits(&s->pb, 1, 0); /* camera off */
71 put_bits(&s->pb, 1, 0); /* freeze picture release off */ 78 put_bits(&s->pb, 1, 0); /* freeze picture release off */
72 79
80 format = h263_get_picture_format(s->width, s->height);
73 if (!s->h263_plus) { 81 if (!s->h263_plus) {
74 /* H.263v1 */ 82 /* H.263v1 */
75 format = h263_get_picture_format(s->width, s->height);
76 put_bits(&s->pb, 3, format); 83 put_bits(&s->pb, 3, format);
77 put_bits(&s->pb, 1, (s->pict_type == P_TYPE)); 84 put_bits(&s->pb, 1, (s->pict_type == P_TYPE));
78 /* By now UMV IS DISABLED ON H.263v1, since the restrictions 85 /* By now UMV IS DISABLED ON H.263v1, since the restrictions
79 of H.263v1 UMV implies to check the predicted MV after 86 of H.263v1 UMV implies to check the predicted MV after
80 calculation of the current MB to see if we're on the limits */ 87 calculation of the current MB to see if we're on the limits */
87 } else { 94 } else {
88 /* H.263v2 */ 95 /* H.263v2 */
89 /* H.263 Plus PTYPE */ 96 /* H.263 Plus PTYPE */
90 put_bits(&s->pb, 3, 7); 97 put_bits(&s->pb, 3, 7);
91 put_bits(&s->pb,3,1); /* Update Full Extended PTYPE */ 98 put_bits(&s->pb,3,1); /* Update Full Extended PTYPE */
92 put_bits(&s->pb,3,6); /* Custom Source Format */ 99 if (format == 7)
100 put_bits(&s->pb,3,6); /* Custom Source Format */
101 else
102 put_bits(&s->pb, 3, format);
103
93 put_bits(&s->pb,1,0); /* Custom PCF: off */ 104 put_bits(&s->pb,1,0); /* Custom PCF: off */
94 umvplus = (s->pict_type == P_TYPE) && s->unrestricted_mv; 105 umvplus = (s->pict_type == P_TYPE) && s->unrestricted_mv;
95 put_bits(&s->pb, 1, umvplus); /* Unrestricted Motion Vector */ 106 put_bits(&s->pb, 1, umvplus); /* Unrestricted Motion Vector */
96 put_bits(&s->pb,1,0); /* SAC: off */ 107 put_bits(&s->pb,1,0); /* SAC: off */
97 put_bits(&s->pb,1,0); /* Advanced Prediction Mode: off */ 108 put_bits(&s->pb,1,0); /* Advanced Prediction Mode: off */
114 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */ 125 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
115 126
116 /* This should be here if PLUSPTYPE */ 127 /* This should be here if PLUSPTYPE */
117 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */ 128 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
118 129
119 /* Custom Picture Format (CPFMT) */ 130 if (format == 7) {
131 /* Custom Picture Format (CPFMT) */
120 132
121 put_bits(&s->pb,4,2); /* Aspect ratio: CIF 12:11 (4:3) picture */ 133 put_bits(&s->pb,4,2); /* Aspect ratio: CIF 12:11 (4:3) picture */
122 put_bits(&s->pb,9,(s->width >> 2) - 1); 134 put_bits(&s->pb,9,(s->width >> 2) - 1);
123 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */ 135 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
124 put_bits(&s->pb,9,(s->height >> 2)); 136 put_bits(&s->pb,9,(s->height >> 2));
137 }
138
125 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */ 139 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
126 if (umvplus) 140 if (umvplus)
127 put_bits(&s->pb,1,1); /* Limited according tables of Annex D */ 141 put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
128 put_bits(&s->pb, 5, s->qscale); 142 put_bits(&s->pb, 5, s->qscale);
129 } 143 }
136 int motion_x, int motion_y) 150 int motion_x, int motion_y)
137 { 151 {
138 int cbpc, cbpy, i, cbp, pred_x, pred_y; 152 int cbpc, cbpy, i, cbp, pred_x, pred_y;
139 153
140 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y); 154 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
141 if (!s->mb_intra) { 155 if (!s->mb_intra) {
142 /* compute cbp */ 156 /* compute cbp */
143 cbp = 0; 157 cbp = 0;
144 for (i = 0; i < 6; i++) { 158 for (i = 0; i < 6; i++) {
145 if (s->block_last_index[i] >= 0) 159 if (s->block_last_index[i] >= 0)
146 cbp |= 1 << (5 - i); 160 cbp |= 1 << (5 - i);
147 } 161 }
148 if ((cbp | motion_x | motion_y) == 0) { 162 if ((cbp | motion_x | motion_y) == 0) {
149 /* skip macroblock */ 163 /* skip macroblock */
150 put_bits(&s->pb, 1, 1); 164 put_bits(&s->pb, 1, 1);
151 return; 165 return;
152 } 166 }
153 put_bits(&s->pb, 1, 0); /* mb coded */ 167 put_bits(&s->pb, 1, 0); /* mb coded */
154 cbpc = cbp & 3; 168 cbpc = cbp & 3;
155 put_bits(&s->pb, 169 put_bits(&s->pb,
156 inter_MCBPC_bits[cbpc], 170 inter_MCBPC_bits[cbpc],
157 inter_MCBPC_code[cbpc]); 171 inter_MCBPC_code[cbpc]);
158 cbpy = cbp >> 2; 172 cbpy = cbp >> 2;
159 cbpy ^= 0xf; 173 cbpy ^= 0xf;
160 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]); 174 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
161 175
162 /* motion vectors: 16x16 mode only now */ 176 /* motion vectors: 16x16 mode only now */
163 h263_pred_motion(s, 0, &pred_x, &pred_y); 177 h263_pred_motion(s, 0, &pred_x, &pred_y);
164 178
165 h263_encode_motion(s, motion_x - pred_x); 179 if (!umvplus) {
166 h263_encode_motion(s, motion_y - pred_y); 180 h263_encode_motion(s, motion_x - pred_x);
167 } else { 181 h263_encode_motion(s, motion_y - pred_y);
182 }
183 else {
184 h263p_encode_umotion(s, motion_x - pred_x);
185 h263p_encode_umotion(s, motion_y - pred_y);
186 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
187 /* To prevent Start Code emulation */
188 put_bits(&s->pb,1,1);
189 }
190 } else {
168 /* compute cbp */ 191 /* compute cbp */
169 cbp = 0; 192 cbp = 0;
170 for (i = 0; i < 6; i++) { 193 for (i = 0; i < 6; i++) {
171 if (s->block_last_index[i] >= 1) 194 if (s->block_last_index[i] >= 1)
172 cbp |= 1 << (5 - i); 195 cbp |= 1 << (5 - i);
300 323
301 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); 324 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
302 if (bit_size > 0) { 325 if (bit_size > 0) {
303 put_bits(&s->pb, bit_size, bits); 326 put_bits(&s->pb, bit_size, bits);
304 } 327 }
328 }
329 }
330
331 /* Encode MV differences on H.263+ with Unrestricted MV mode */
332 static void h263p_encode_umotion(MpegEncContext * s, int val)
333 {
334 short sval = 0;
335 short i = 0;
336 short n_bits = 0;
337 short temp_val;
338 int code = 0;
339 int tcode;
340
341 if ( val == 0)
342 put_bits(&s->pb, 1, 1);
343 else if (val == 1)
344 put_bits(&s->pb, 3, 0);
345 else if (val == -1)
346 put_bits(&s->pb, 3, 2);
347 else {
348
349 sval = ((val < 0) ? (short)(-val):(short)val);
350 temp_val = sval;
351
352 while (temp_val != 0) {
353 temp_val = temp_val >> 1;
354 n_bits++;
355 }
356
357 i = n_bits - 1;
358 while (i > 0) {
359 tcode = (sval & (1 << (i-1))) >> (i-1);
360 tcode = (tcode << 1) | 1;
361 code = (code << 2) | tcode;
362 i--;
363 }
364 code = ((code << 1) | (val < 0)) << 1;
365 put_bits(&s->pb, (2*n_bits)+1, code);
366 //printf("\nVal = %d\tCode = %d", sval, code);
305 } 367 }
306 } 368 }
307 369
308 void h263_encode_init_vlc(MpegEncContext *s) 370 void h263_encode_init_vlc(MpegEncContext *s)
309 { 371 {
761 s->mv_dir = MV_DIR_FORWARD; 823 s->mv_dir = MV_DIR_FORWARD;
762 if ((cbpc & 16) == 0) { 824 if ((cbpc & 16) == 0) {
763 /* 16x16 motion prediction */ 825 /* 16x16 motion prediction */
764 s->mv_type = MV_TYPE_16X16; 826 s->mv_type = MV_TYPE_16X16;
765 h263_pred_motion(s, 0, &pred_x, &pred_y); 827 h263_pred_motion(s, 0, &pred_x, &pred_y);
766 mx = h263_decode_motion(s, pred_x); 828 if (umvplus_dec)
829 mx = h263p_decode_umotion(s, pred_x);
830 else
831 mx = h263_decode_motion(s, pred_x);
767 if (mx >= 0xffff) 832 if (mx >= 0xffff)
768 return -1; 833 return -1;
769 my = h263_decode_motion(s, pred_y); 834
835 if (umvplus_dec)
836 my = h263p_decode_umotion(s, pred_y);
837 else
838 my = h263_decode_motion(s, pred_y);
770 if (my >= 0xffff) 839 if (my >= 0xffff)
771 return -1; 840 return -1;
772 s->mv[0][0][0] = mx; 841 s->mv[0][0][0] = mx;
773 s->mv[0][0][1] = my; 842 s->mv[0][0][1] = my;
843 if (umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
844 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
845
774 } else { 846 } else {
775 s->mv_type = MV_TYPE_8X8; 847 s->mv_type = MV_TYPE_8X8;
776 for(i=0;i<4;i++) { 848 for(i=0;i<4;i++) {
777 mot_val = h263_pred_motion(s, i, &pred_x, &pred_y); 849 mot_val = h263_pred_motion(s, i, &pred_x, &pred_y);
778 mx = h263_decode_motion(s, pred_x); 850 if (umvplus_dec)
851 mx = h263p_decode_umotion(s, pred_x);
852 else
853 mx = h263_decode_motion(s, pred_x);
779 if (mx >= 0xffff) 854 if (mx >= 0xffff)
780 return -1; 855 return -1;
781 my = h263_decode_motion(s, pred_y); 856
857 if (umvplus_dec)
858 my = h263p_decode_umotion(s, pred_y);
859 else
860 my = h263_decode_motion(s, pred_y);
782 if (my >= 0xffff) 861 if (my >= 0xffff)
783 return -1; 862 return -1;
784 s->mv[0][i][0] = mx; 863 s->mv[0][i][0] = mx;
785 s->mv[0][i][1] = my; 864 s->mv[0][i][1] = my;
865 if (umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
866 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
786 mot_val[0] = mx; 867 mot_val[0] = mx;
787 mot_val[1] = my; 868 mot_val[1] = my;
788 } 869 }
789 } 870 }
790 } else { 871 } else {
853 val += 64; 934 val += 64;
854 if (pred > 32 && val > 63) 935 if (pred > 32 && val > 63)
855 val -= 64; 936 val -= 64;
856 } 937 }
857 return val; 938 return val;
939 }
940
941 /* Decodes RVLC of H.263+ UMV */
942 static int h263p_decode_umotion(MpegEncContext * s, int pred)
943 {
944 int code = 0, sign;
945
946 if (get_bits1(&s->gb)) /* Motion difference = 0 */
947 return pred;
948
949 code = 2 + get_bits1(&s->gb);
950
951 while (get_bits1(&s->gb))
952 {
953 code <<= 1;
954 code += get_bits1(&s->gb);
955 }
956 sign = code & 1;
957 code >>= 1;
958
959 code = (sign) ? (pred - code) : (pred + code);
960 #ifdef DEBUG
961 fprintf(stderr,"H.263+ UMV Motion = %d\n", code);
962 #endif
963 return code;
964
858 } 965 }
859 966
860 static int h263_decode_block(MpegEncContext * s, DCTELEM * block, 967 static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
861 int n, int coded) 968 int n, int coded)
862 { 969 {
1106 s->qscale = get_bits(&s->gb, 5); 1213 s->qscale = get_bits(&s->gb, 5);
1107 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */ 1214 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
1108 } else { 1215 } else {
1109 s->h263_plus = 1; 1216 s->h263_plus = 1;
1110 /* H.263v2 */ 1217 /* H.263v2 */
1111 if (get_bits(&s->gb, 3) != 1) 1218 /* OPPTYPE */
1219
1220 if (get_bits(&s->gb, 3) != 1) /* Update Full Extended PTYPE */
1112 return -1; 1221 return -1;
1113 if (get_bits(&s->gb, 3) != 6) /* custom source format */ 1222 format = get_bits(&s->gb, 3);
1114 return -1; 1223
1115 skip_bits(&s->gb, 12); 1224 skip_bits(&s->gb,1); /* Custom PCF */
1116 skip_bits(&s->gb, 3); 1225 umvplus_dec = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */
1226 skip_bits(&s->gb, 10);
1227 skip_bits(&s->gb, 3); /* Reserved */
1228
1229 /* MPPTYPE */
1117 s->pict_type = get_bits(&s->gb, 3) + 1; 1230 s->pict_type = get_bits(&s->gb, 3) + 1;
1118 if (s->pict_type != I_TYPE && 1231 if (s->pict_type != I_TYPE &&
1119 s->pict_type != P_TYPE) 1232 s->pict_type != P_TYPE)
1120 return -1; 1233 return -1;
1121 skip_bits(&s->gb, 7); 1234 skip_bits(&s->gb, 7);
1122 skip_bits(&s->gb, 4); /* aspect ratio */ 1235
1123 width = (get_bits(&s->gb, 9) + 1) * 4; 1236 /* Get the picture dimensions */
1124 skip_bits1(&s->gb); 1237 if (format == 6) {
1125 height = get_bits(&s->gb, 9) * 4; 1238 /* Custom Picture Format (CPFMT) */
1126 if (height == 0) 1239 skip_bits(&s->gb, 4); /* aspect ratio */
1240 width = (get_bits(&s->gb, 9) + 1) * 4;
1241 skip_bits1(&s->gb);
1242 height = get_bits(&s->gb, 9) * 4;
1243 #ifdef DEBUG
1244 fprintf(stderr,"\nH.263+ Custom picture: %dx%d\n",width,height);
1245 #endif
1246 }
1247 else {
1248 width = h263_format[format][0];
1249 height = h263_format[format][1];
1250 }
1251
1252 if ((width == 0) || (height == 0))
1127 return -1; 1253 return -1;
1254
1255 if (umvplus_dec) {
1256 skip_bits1(&s->gb); /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
1257 }
1258
1128 s->qscale = get_bits(&s->gb, 5); 1259 s->qscale = get_bits(&s->gb, 5);
1129 } 1260 }
1130 /* PEI */ 1261 /* PEI */
1131 while (get_bits1(&s->gb) != 0) { 1262 while (get_bits1(&s->gb) != 0) {
1132 skip_bits(&s->gb, 8); 1263 skip_bits(&s->gb, 8);