1234
|
1 /*
|
|
2 * Copyright (c) 2003 The FFmpeg Project.
|
|
3 *
|
|
4 * This library is free software; you can redistribute it and/or
|
|
5 * modify it under the terms of the GNU Lesser General Public
|
|
6 * License as published by the Free Software Foundation; either
|
|
7 * version 2 of the License, or (at your option) any later version.
|
|
8 *
|
|
9 * This library is distributed in the hope that it will be useful,
|
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12 * Lesser General Public License for more details.
|
|
13 *
|
|
14 * You should have received a copy of the GNU Lesser General Public
|
|
15 * License along with this library; if not, write to the Free Software
|
|
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
17 *
|
|
18 *
|
|
19 * How to use this decoder:
|
|
20 * SVQ3 data is transported within Apple Quicktime files. Quicktime files
|
|
21 * have stsd atoms to describe media trak properties. Sometimes the stsd
|
|
22 * atom contains information that the decoder must know in order to function
|
|
23 * properly. Such is the case with SVQ3. In order to get the best use out
|
|
24 * of this decoder, the calling app must make the video stsd atom available
|
|
25 * via the AVCodecContext's extradata[_size] field:
|
|
26 *
|
|
27 * AVCodecContext.extradata = pointer to stsd, first characters are expected
|
|
28 * to be 's', 't', 's', and 'd', NOT the atom length
|
|
29 * AVCodecContext.extradata_size = size of stsd atom memory buffer (which
|
|
30 * will be the same as the stsd atom size field from the QT file, minus 4
|
|
31 * bytes since the length is missing.
|
|
32 *
|
|
33 */
|
|
34
|
|
35 /**
|
|
36 * @file svq3.c
|
|
37 * svq3 decoder.
|
|
38 */
|
|
39
|
|
40 static const uint8_t svq3_scan[16]={
|
|
41 0+0*4, 1+0*4, 2+0*4, 2+1*4,
|
|
42 2+2*4, 3+0*4, 3+1*4, 3+2*4,
|
|
43 0+1*4, 0+2*4, 1+1*4, 1+2*4,
|
|
44 0+3*4, 1+3*4, 2+3*4, 3+3*4,
|
|
45 };
|
|
46
|
|
47 static const uint8_t svq3_pred_0[25][2] = {
|
|
48 { 0, 0 },
|
|
49 { 1, 0 }, { 0, 1 },
|
|
50 { 0, 2 }, { 1, 1 }, { 2, 0 },
|
|
51 { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
|
|
52 { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
|
|
53 { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
|
|
54 { 2, 4 }, { 3, 3 }, { 4, 2 },
|
|
55 { 4, 3 }, { 3, 4 },
|
|
56 { 4, 4 }
|
|
57 };
|
|
58
|
|
59 static const int8_t svq3_pred_1[6][6][5] = {
|
|
60 { { 2,-1,-1,-1,-1 }, { 2, 1,-1,-1,-1 }, { 1, 2,-1,-1,-1 },
|
|
61 { 2, 1,-1,-1,-1 }, { 1, 2,-1,-1,-1 }, { 1, 2,-1,-1,-1 } },
|
|
62 { { 0, 2,-1,-1,-1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 },
|
|
63 { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } },
|
|
64 { { 2, 0,-1,-1,-1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 },
|
|
65 { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } },
|
|
66 { { 2, 0,-1,-1,-1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 },
|
|
67 { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } },
|
|
68 { { 0, 2,-1,-1,-1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 },
|
|
69 { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } },
|
|
70 { { 0, 2,-1,-1,-1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 },
|
|
71 { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } },
|
|
72 };
|
|
73
|
|
74 static const struct { uint8_t run; uint8_t level; } svq3_dct_tables[2][16] = {
|
|
75 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
|
|
76 { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
|
|
77 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
|
|
78 { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
|
|
79 };
|
|
80
|
|
81 static const uint32_t svq3_dequant_coeff[32] = {
|
|
82 3881, 4351, 4890, 5481, 6154, 6914, 7761, 8718,
|
|
83 9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873,
|
|
84 24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683,
|
|
85 61694, 68745, 77615, 89113,100253,109366,126635,141533
|
|
86 };
|
|
87
|
|
88
|
|
89 static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp){
|
|
90 const int qmul= svq3_dequant_coeff[qp];
|
|
91 #define stride 16
|
|
92 int i;
|
|
93 int temp[16];
|
|
94 static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
|
|
95 static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
|
|
96
|
|
97 for(i=0; i<4; i++){
|
|
98 const int offset= y_offset[i];
|
|
99 const int z0= 13*(block[offset+stride*0] + block[offset+stride*4]);
|
|
100 const int z1= 13*(block[offset+stride*0] - block[offset+stride*4]);
|
|
101 const int z2= 7* block[offset+stride*1] - 17*block[offset+stride*5];
|
|
102 const int z3= 17* block[offset+stride*1] + 7*block[offset+stride*5];
|
|
103
|
|
104 temp[4*i+0]= z0+z3;
|
|
105 temp[4*i+1]= z1+z2;
|
|
106 temp[4*i+2]= z1-z2;
|
|
107 temp[4*i+3]= z0-z3;
|
|
108 }
|
|
109
|
|
110 for(i=0; i<4; i++){
|
|
111 const int offset= x_offset[i];
|
|
112 const int z0= 13*(temp[4*0+i] + temp[4*2+i]);
|
|
113 const int z1= 13*(temp[4*0+i] - temp[4*2+i]);
|
|
114 const int z2= 7* temp[4*1+i] - 17*temp[4*3+i];
|
|
115 const int z3= 17* temp[4*1+i] + 7*temp[4*3+i];
|
|
116
|
|
117 block[stride*0 +offset]= ((z0 + z3)*qmul + 0x80000)>>20;
|
|
118 block[stride*2 +offset]= ((z1 + z2)*qmul + 0x80000)>>20;
|
|
119 block[stride*8 +offset]= ((z1 - z2)*qmul + 0x80000)>>20;
|
|
120 block[stride*10+offset]= ((z0 - z3)*qmul + 0x80000)>>20;
|
|
121 }
|
|
122 }
|
|
123 #undef stride
|
|
124
|
|
125 static void svq3_add_idct_c (uint8_t *dst, DCTELEM *block, int stride, int qp, int dc){
|
|
126 const int qmul= svq3_dequant_coeff[qp];
|
|
127 int i;
|
|
128 uint8_t *cm = cropTbl + MAX_NEG_CROP;
|
|
129
|
|
130 if (dc) {
|
|
131 dc = 13*13*((dc == 1) ? 1538*block[0] : ((qmul*(block[0] >> 3)) / 2));
|
|
132 block[0] = 0;
|
|
133 }
|
|
134
|
|
135 for (i=0; i < 4; i++) {
|
|
136 const int z0= 13*(block[0 + 4*i] + block[2 + 4*i]);
|
|
137 const int z1= 13*(block[0 + 4*i] - block[2 + 4*i]);
|
|
138 const int z2= 7* block[1 + 4*i] - 17*block[3 + 4*i];
|
|
139 const int z3= 17* block[1 + 4*i] + 7*block[3 + 4*i];
|
|
140
|
|
141 block[0 + 4*i]= z0 + z3;
|
|
142 block[1 + 4*i]= z1 + z2;
|
|
143 block[2 + 4*i]= z1 - z2;
|
|
144 block[3 + 4*i]= z0 - z3;
|
|
145 }
|
|
146
|
|
147 for (i=0; i < 4; i++) {
|
|
148 const int z0= 13*(block[i + 4*0] + block[i + 4*2]);
|
|
149 const int z1= 13*(block[i + 4*0] - block[i + 4*2]);
|
|
150 const int z2= 7* block[i + 4*1] - 17*block[i + 4*3];
|
|
151 const int z3= 17* block[i + 4*1] + 7*block[i + 4*3];
|
|
152 const int rr= (dc + 0x80000);
|
|
153
|
|
154 dst[i + stride*0]= cm[ dst[i + stride*0] + (((z0 + z3)*qmul + rr) >> 20) ];
|
|
155 dst[i + stride*1]= cm[ dst[i + stride*1] + (((z1 + z2)*qmul + rr) >> 20) ];
|
|
156 dst[i + stride*2]= cm[ dst[i + stride*2] + (((z1 - z2)*qmul + rr) >> 20) ];
|
|
157 dst[i + stride*3]= cm[ dst[i + stride*3] + (((z0 - z3)*qmul + rr) >> 20) ];
|
|
158 }
|
|
159 }
|
|
160
|
|
161 static void pred4x4_down_left_svq3_c(uint8_t *src, uint8_t *topright, int stride){
|
|
162 LOAD_TOP_EDGE
|
|
163 LOAD_LEFT_EDGE
|
|
164 const __attribute__((unused)) int unu0= t0;
|
|
165 const __attribute__((unused)) int unu1= l0;
|
|
166
|
|
167 src[0+0*stride]=(l1 + t1)>>1;
|
|
168 src[1+0*stride]=
|
|
169 src[0+1*stride]=(l2 + t2)>>1;
|
|
170 src[2+0*stride]=
|
|
171 src[1+1*stride]=
|
|
172 src[0+2*stride]=
|
|
173 src[3+0*stride]=
|
|
174 src[2+1*stride]=
|
|
175 src[1+2*stride]=
|
|
176 src[0+3*stride]=
|
|
177 src[3+1*stride]=
|
|
178 src[2+2*stride]=
|
|
179 src[1+3*stride]=
|
|
180 src[3+2*stride]=
|
|
181 src[2+3*stride]=
|
|
182 src[3+3*stride]=(l3 + t3)>>1;
|
|
183 };
|
|
184
|
|
185 static void pred16x16_plane_svq3_c(uint8_t *src, int stride){
|
|
186 pred16x16_plane_compat_c(src, stride, 1);
|
|
187 }
|
|
188
|
|
189 static inline int svq3_decode_block (GetBitContext *gb, DCTELEM *block,
|
|
190 int index, const int type) {
|
|
191
|
|
192 static const uint8_t *const scan_patterns[4] =
|
|
193 { luma_dc_zigzag_scan, zigzag_scan, svq3_scan, chroma_dc_scan };
|
|
194
|
|
195 int run, level, sign, vlc, limit;
|
|
196 const int intra = (3 * type) >> 2;
|
|
197 const uint8_t *const scan = scan_patterns[type];
|
|
198
|
|
199 for (limit=(16 >> intra); index < 16; index=limit, limit+=8) {
|
|
200 for (; (vlc = svq3_get_ue_golomb (gb)) != 0; index++) {
|
|
201
|
|
202 if (vlc == INVALID_VLC)
|
|
203 return -1;
|
|
204
|
|
205 sign = (vlc & 0x1) - 1;
|
|
206 vlc = (vlc + 1) >> 1;
|
|
207
|
|
208 if (type == 3) {
|
|
209 if (vlc < 3) {
|
|
210 run = 0;
|
|
211 level = vlc;
|
|
212 } else if (vlc < 4) {
|
|
213 run = 1;
|
|
214 level = 1;
|
|
215 } else {
|
|
216 run = (vlc & 0x3);
|
|
217 level = ((vlc + 9) >> 2) - run;
|
|
218 }
|
|
219 } else {
|
|
220 if (vlc < 16) {
|
|
221 run = svq3_dct_tables[intra][vlc].run;
|
|
222 level = svq3_dct_tables[intra][vlc].level;
|
|
223 } else if (intra) {
|
|
224 run = (vlc & 0x7);
|
|
225 level = (vlc >> 3) + ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
|
|
226 } else {
|
|
227 run = (vlc & 0xF);
|
|
228 level = (vlc >> 4) + ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
|
|
229 }
|
|
230 }
|
|
231
|
|
232 if ((index += run) >= limit)
|
|
233 return -1;
|
|
234
|
|
235 block[scan[index]] = (level ^ sign) - sign;
|
|
236 }
|
|
237
|
|
238 if (type != 2) {
|
|
239 break;
|
|
240 }
|
|
241 }
|
|
242
|
|
243 return 0;
|
|
244 }
|
|
245
|
|
246 static void sixpel_mc_put (MpegEncContext *s,
|
|
247 uint8_t *src, uint8_t *dst, int stride,
|
|
248 int dxy, int width, int height) {
|
|
249 int i, j;
|
|
250
|
|
251 switch (dxy) {
|
|
252 case 6*0+0:
|
|
253 for (i=0; i < height; i++) {
|
|
254 memcpy (dst, src, width);
|
|
255 src += stride;
|
|
256 dst += stride;
|
|
257 }
|
|
258 break;
|
|
259 case 6*0+2:
|
|
260 for (i=0; i < height; i++) {
|
|
261 for (j=0; j < width; j++) {
|
|
262 dst[j] = (683*(2*src[j] + src[j+1] + 1)) >> 11;
|
|
263 }
|
|
264 src += stride;
|
|
265 dst += stride;
|
|
266 }
|
|
267 break;
|
|
268 case 6*0+3:
|
|
269 for (i=0; i < height; i++) {
|
|
270 for (j=0; j < width; j++) {
|
|
271 dst[j] = (src[j] + src[j+1] + 1) >> 1;
|
|
272 }
|
|
273 src += stride;
|
|
274 dst += stride;
|
|
275 }
|
|
276 break;
|
|
277 case 6*0+4:
|
|
278 for (i=0; i < height; i++) {
|
|
279 for (j=0; j < width; j++) {
|
|
280 dst[j] = (683*(src[j] + 2*src[j+1] + 1)) >> 11;
|
|
281 }
|
|
282 src += stride;
|
|
283 dst += stride;
|
|
284 }
|
|
285 break;
|
|
286 case 6*2+0:
|
|
287 for (i=0; i < height; i++) {
|
|
288 for (j=0; j < width; j++) {
|
|
289 dst[j] = (683*(2*src[j] + src[j+stride] + 1)) >> 11;
|
|
290 }
|
|
291 src += stride;
|
|
292 dst += stride;
|
|
293 }
|
|
294 break;
|
|
295 case 6*2+2:
|
|
296 for (i=0; i < height; i++) {
|
|
297 for (j=0; j < width; j++) {
|
|
298 dst[j] = (2731*(4*src[j] + 3*src[j+1] + 3*src[j+stride] + 2*src[j+stride+1] + 6)) >> 15;
|
|
299 }
|
|
300 src += stride;
|
|
301 dst += stride;
|
|
302 }
|
|
303 break;
|
|
304 case 6*2+4:
|
|
305 for (i=0; i < height; i++) {
|
|
306 for (j=0; j < width; j++) {
|
|
307 dst[j] = (2731*(3*src[j] + 4*src[j+1] + 2*src[j+stride] + 3*src[j+stride+1] + 6)) >> 15;
|
|
308 }
|
|
309 src += stride;
|
|
310 dst += stride;
|
|
311 }
|
|
312 break;
|
|
313 case 6*3+0:
|
|
314 for (i=0; i < height; i++) {
|
|
315 for (j=0; j < width; j++) {
|
|
316 dst[j] = (src[j] + src[j+stride]+1) >> 1;
|
|
317 }
|
|
318 src += stride;
|
|
319 dst += stride;
|
|
320 }
|
|
321 break;
|
|
322 case 6*3+3:
|
|
323 for (i=0; i < height; i++) {
|
|
324 for (j=0; j < width; j++) {
|
|
325 dst[j] = (src[j] + src[j+1] + src[j+stride] + src[j+stride+1] + 2) >> 2;
|
|
326 }
|
|
327 src += stride;
|
|
328 dst += stride;
|
|
329 }
|
|
330 break;
|
|
331 case 6*4+0:
|
|
332 for (i=0; i < height; i++) {
|
|
333 for (j=0; j < width; j++) {
|
|
334 dst[j] = (683*(src[j] + 2*src[j+stride] + 1)) >> 11;
|
|
335 }
|
|
336 src += stride;
|
|
337 dst += stride;
|
|
338 }
|
|
339 break;
|
|
340 case 6*4+2:
|
|
341 for (i=0; i < height; i++) {
|
|
342 for (j=0; j < width; j++) {
|
|
343 dst[j] = (2731*(3*src[j] + 2*src[j+1] + 4*src[j+stride] + 3*src[j+stride+1] + 6)) >> 15;
|
|
344 }
|
|
345 src += stride;
|
|
346 dst += stride;
|
|
347 }
|
|
348 break;
|
|
349 case 6*4+4:
|
|
350 for (i=0; i < height; i++) {
|
|
351 for (j=0; j < width; j++) {
|
|
352 dst[j] = (2731*(2*src[j] + 3*src[j+1] + 3*src[j+stride] + 4*src[j+stride+1] + 6)) >> 15;
|
|
353 }
|
|
354 src += stride;
|
|
355 dst += stride;
|
|
356 }
|
|
357 break;
|
|
358 }
|
|
359 }
|
|
360
|
|
361 static inline void svq3_mc_dir_part (MpegEncContext *s, int x, int y,
|
|
362 int width, int height, int mx, int my) {
|
|
363 uint8_t *src, *dest;
|
|
364 int i, emu = 0;
|
|
365 const int sx = ((unsigned) (mx + 0x7FFFFFFE)) % 6;
|
|
366 const int sy = ((unsigned) (my + 0x7FFFFFFE)) % 6;
|
|
367 const int dxy= 6*sy + sx;
|
|
368
|
|
369 /* decode and clip motion vector to frame border (+16) */
|
|
370 mx = x + (mx - sx) / 6;
|
|
371 my = y + (my - sy) / 6;
|
|
372
|
|
373 if (mx < 0 || mx >= (s->width - width - 1) ||
|
|
374 my < 0 || my >= (s->height - height - 1)) {
|
|
375
|
|
376 if ((s->flags & CODEC_FLAG_EMU_EDGE)) {
|
|
377 emu = 1;
|
|
378 }
|
|
379
|
|
380 mx = clip (mx, -16, (s->width - width + 15));
|
|
381 my = clip (my, -16, (s->height - height + 15));
|
|
382 }
|
|
383
|
|
384 /* form component predictions */
|
|
385 dest = s->current_picture.data[0] + x + y*s->linesize;
|
|
386 src = s->last_picture.data[0] + mx + my*s->linesize;
|
|
387
|
|
388 if (emu) {
|
|
389 ff_emulated_edge_mc (s, src, s->linesize, (width + 1), (height + 1),
|
|
390 mx, my, s->width, s->height);
|
|
391 src = s->edge_emu_buffer;
|
|
392 }
|
|
393 sixpel_mc_put (s, src, dest, s->linesize, dxy, width, height);
|
|
394
|
|
395 if (!(s->flags & CODEC_FLAG_GRAY)) {
|
|
396 mx = (mx + (mx < (int) x)) >> 1;
|
|
397 my = (my + (my < (int) y)) >> 1;
|
|
398 width = (width >> 1);
|
|
399 height = (height >> 1);
|
|
400
|
|
401 for (i=1; i < 3; i++) {
|
|
402 dest = s->current_picture.data[i] + (x >> 1) + (y >> 1)*s->uvlinesize;
|
|
403 src = s->last_picture.data[i] + mx + my*s->uvlinesize;
|
|
404
|
|
405 if (emu) {
|
|
406 ff_emulated_edge_mc (s, src, s->uvlinesize, (width + 1), (height + 1),
|
|
407 mx, my, (s->width >> 1), (s->height >> 1));
|
|
408 src = s->edge_emu_buffer;
|
|
409 }
|
|
410 sixpel_mc_put (s, src, dest, s->uvlinesize, dxy, width, height);
|
|
411 }
|
|
412 }
|
|
413 }
|
|
414
|
|
415 static int svq3_decode_mb (H264Context *h, unsigned int mb_type) {
|
|
416 int cbp, dir, mode, mx, my, dx, dy, x, y, part_width, part_height;
|
|
417 int i, j, k, l, m;
|
|
418 uint32_t vlc;
|
|
419 int8_t *top, *left;
|
|
420 MpegEncContext *const s = (MpegEncContext *) h;
|
|
421 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
|
|
422 const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
|
|
423
|
|
424 h->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
|
|
425 h->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
|
|
426 h->topright_samples_available = 0xFFFF;
|
|
427
|
|
428 if (mb_type == 0) { /* SKIP */
|
|
429 svq3_mc_dir_part (s, 16*s->mb_x, 16*s->mb_y, 16, 16, 0, 0);
|
|
430
|
|
431 cbp = 0;
|
|
432 mb_type = MB_TYPE_SKIP;
|
|
433 } else if (mb_type < 8) { /* INTER */
|
|
434 if (h->thirdpel_flag && h->halfpel_flag == !get_bits (&s->gb, 1)) {
|
|
435 mode = 3; /* thirdpel */
|
|
436 } else if (h->halfpel_flag && h->thirdpel_flag == !get_bits (&s->gb, 1)) {
|
|
437 mode = 2; /* halfpel */
|
|
438 } else {
|
|
439 mode = 1; /* fullpel */
|
|
440 }
|
|
441
|
|
442 /* fill caches */
|
|
443 memset (h->ref_cache[0], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
|
|
444
|
|
445 if (s->mb_x > 0) {
|
|
446 for (i=0; i < 4; i++) {
|
|
447 *(uint32_t *) h->mv_cache[0][scan8[0] - 1 + i*8] = *(uint32_t *) s->current_picture.motion_val[0][b_xy - 1 + i*h->b_stride];
|
|
448 h->ref_cache[0][scan8[0] - 1 + i*8] = 1;
|
|
449 }
|
|
450 } else {
|
|
451 for (i=0; i < 4; i++) {
|
|
452 *(uint32_t *) h->mv_cache[0][scan8[0] - 1 + i*8] = 0;
|
|
453 h->ref_cache[0][scan8[0] - 1 + i*8] = 1;
|
|
454 }
|
|
455 }
|
|
456 if (s->mb_y > 0) {
|
|
457 memcpy (h->mv_cache[0][scan8[0] - 1*8], s->current_picture.motion_val[0][b_xy - h->b_stride], 4*2*sizeof(int16_t));
|
|
458 memset (&h->ref_cache[0][scan8[0] - 1*8], 1, 4);
|
|
459
|
|
460 if (s->mb_x < (s->mb_width - 1)) {
|
|
461 *(uint32_t *) h->mv_cache[0][scan8[0] + 4 - 1*8] = *(uint32_t *) s->current_picture.motion_val[0][b_xy - h->b_stride + 4];
|
|
462 h->ref_cache[0][scan8[0] + 4 - 1*8] = 1;
|
|
463 }
|
|
464 if (s->mb_x > 0) {
|
|
465 *(uint32_t *) h->mv_cache[0][scan8[0] - 1 - 1*8] = *(uint32_t *) s->current_picture.motion_val[0][b_xy - h->b_stride - 1];
|
|
466 h->ref_cache[0][scan8[0] - 1 - 1*8] = 1;
|
|
467 }
|
|
468 }
|
|
469
|
|
470 /* decode motion vector(s) and form prediction(s) */
|
|
471 part_width = ((mb_type & 5) == 5) ? 4 : 8 << (mb_type & 1);
|
|
472 part_height = 16 >> ((unsigned) mb_type / 3);
|
|
473
|
|
474 for (i=0; i < 16; i+=part_height) {
|
|
475 for (j=0; j < 16; j+=part_width) {
|
|
476 x = 16*s->mb_x + j;
|
|
477 y = 16*s->mb_y + i;
|
|
478 k = ((j>>2)&1) + ((i>>1)&2) + ((j>>1)&4) + (i&8);
|
|
479
|
|
480 pred_motion (h, k, (part_width >> 2), 0, 1, &mx, &my);
|
|
481
|
|
482 /* clip motion vector prediction to frame border */
|
|
483 mx = clip (mx, -6*x, 6*(s->width - part_width - x));
|
|
484 my = clip (my, -6*y, 6*(s->height - part_height - y));
|
|
485
|
|
486 /* get motion vector differential */
|
|
487 dy = svq3_get_se_golomb (&s->gb);
|
|
488 dx = svq3_get_se_golomb (&s->gb);
|
|
489
|
|
490 if (dx == INVALID_VLC || dy == INVALID_VLC) {
|
|
491 return -1;
|
|
492 }
|
|
493
|
|
494 /* compute motion vector */
|
|
495 if (mode == 3) {
|
|
496 mx = ((mx + 1) & ~0x1) + 2*dx;
|
|
497 my = ((my + 1) & ~0x1) + 2*dy;
|
|
498 } else if (mode == 2) {
|
|
499 mx = (mx + 1) - ((unsigned) (0x7FFFFFFF + mx) % 3) + 3*dx;
|
|
500 my = (my + 1) - ((unsigned) (0x7FFFFFFF + my) % 3) + 3*dy;
|
|
501 } else if (mode == 1) {
|
|
502 mx = (mx + 3) - ((unsigned) (0x7FFFFFFB + mx) % 6) + 6*dx;
|
|
503 my = (my + 3) - ((unsigned) (0x7FFFFFFB + my) % 6) + 6*dy;
|
|
504 }
|
|
505
|
|
506 /* update mv_cache */
|
|
507 for (l=0; l < part_height; l+=4) {
|
|
508 for (m=0; m < part_width; m+=4) {
|
|
509 k = scan8[0] + ((m + j) >> 2) + ((l + i) << 1);
|
|
510 h->mv_cache [0][k][0] = mx;
|
|
511 h->mv_cache [0][k][1] = my;
|
|
512 h->ref_cache[0][k] = 1;
|
|
513 }
|
|
514 }
|
|
515
|
|
516 svq3_mc_dir_part (s, x, y, part_width, part_height, mx, my);
|
|
517 }
|
|
518 }
|
|
519
|
|
520 for (i=0; i < 4; i++) {
|
|
521 memcpy (s->current_picture.motion_val[0][b_xy + i*h->b_stride], h->mv_cache[0][scan8[0] + 8*i], 4*2*sizeof(int16_t));
|
|
522 }
|
|
523
|
|
524 if ((vlc = svq3_get_ue_golomb (&s->gb)) >= 48)
|
|
525 return -1;
|
|
526
|
|
527 cbp = golomb_to_inter_cbp[vlc];
|
|
528 mb_type = MB_TYPE_16x16;
|
|
529 } else if (mb_type == 8) { /* INTRA4x4 */
|
|
530 memset (h->intra4x4_pred_mode_cache, -1, 8*5*sizeof(int8_t));
|
|
531
|
|
532 if (s->mb_x > 0) {
|
|
533 for (i=0; i < 4; i++) {
|
|
534 h->intra4x4_pred_mode_cache[scan8[0] - 1 + i*8] = h->intra4x4_pred_mode[mb_xy - 1][i];
|
|
535 }
|
|
536 }
|
|
537 if (s->mb_y > 0) {
|
|
538 h->intra4x4_pred_mode_cache[4+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][4];
|
|
539 h->intra4x4_pred_mode_cache[5+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][5];
|
|
540 h->intra4x4_pred_mode_cache[6+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][6];
|
|
541 h->intra4x4_pred_mode_cache[7+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][3];
|
|
542 }
|
|
543
|
|
544 /* decode prediction codes for luma blocks */
|
|
545 for (i=0; i < 16; i+=2) {
|
|
546 vlc = svq3_get_ue_golomb (&s->gb);
|
|
547
|
|
548 if (vlc >= 25)
|
|
549 return -1;
|
|
550
|
|
551 left = &h->intra4x4_pred_mode_cache[scan8[i] - 1];
|
|
552 top = &h->intra4x4_pred_mode_cache[scan8[i] - 8];
|
|
553
|
|
554 left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
|
|
555 left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
|
|
556
|
|
557 if (left[1] == -1 || left[2] == -1)
|
|
558 return -1;
|
|
559 }
|
|
560
|
|
561 write_back_intra_pred_mode (h);
|
|
562 check_intra4x4_pred_mode (h);
|
|
563
|
|
564 if ((vlc = svq3_get_ue_golomb (&s->gb)) >= 48)
|
|
565 return -1;
|
|
566
|
|
567 cbp = golomb_to_intra4x4_cbp[vlc];
|
|
568 mb_type = MB_TYPE_INTRA4x4;
|
|
569 } else { /* INTRA16x16 */
|
|
570 dir = i_mb_type_info[mb_type - 8].pred_mode;
|
|
571 dir = (dir >> 1) ^ 3*(dir & 1) ^ 1;
|
|
572
|
|
573 if ((h->intra16x16_pred_mode = check_intra_pred_mode (h, dir)) == -1)
|
|
574 return -1;
|
|
575
|
|
576 cbp = i_mb_type_info[mb_type - 8].cbp;
|
|
577 mb_type = MB_TYPE_INTRA16x16;
|
|
578 }
|
|
579
|
|
580 if (!IS_INTER(mb_type) && s->pict_type != I_TYPE) {
|
|
581 for (i=0; i < 4; i++) {
|
|
582 memset (s->current_picture.motion_val[0][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
|
|
583 }
|
|
584 }
|
|
585 if (!IS_INTRA4x4(mb_type)) {
|
|
586 memset (h->intra4x4_pred_mode[mb_xy], DC_PRED, 8);
|
|
587 }
|
|
588 if (!IS_SKIP(mb_type)) {
|
|
589 memset (h->mb, 0, 24*16*sizeof(DCTELEM));
|
|
590 memset (h->non_zero_count_cache, 0, 8*6*sizeof(uint8_t));
|
|
591 }
|
|
592
|
|
593 if (IS_INTRA16x16(mb_type) || (s->pict_type != I_TYPE && s->adaptive_quant && cbp)) {
|
|
594 s->qscale += svq3_get_se_golomb (&s->gb);
|
|
595
|
|
596 if (s->qscale > 31)
|
|
597 return -1;
|
|
598 }
|
|
599 if (IS_INTRA16x16(mb_type)) {
|
|
600 if (svq3_decode_block (&s->gb, h->mb, 0, 0))
|
|
601 return -1;
|
|
602 }
|
|
603
|
|
604 if (!IS_SKIP(mb_type) && cbp) {
|
|
605 l = IS_INTRA16x16(mb_type) ? 1 : 0;
|
|
606 m = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
|
|
607
|
|
608 for (i=0; i < 4; i++) {
|
|
609 if ((cbp & (1 << i))) {
|
|
610 for (j=0; j < 4; j++) {
|
|
611 k = l ? ((j&1) + 2*(i&1) + 2*(j&2) + 4*(i&2)) : (4*i + j);
|
|
612 h->non_zero_count_cache[ scan8[k] ] = 1;
|
|
613
|
|
614 if (svq3_decode_block (&s->gb, &h->mb[16*k], l, m))
|
|
615 return -1;
|
|
616 }
|
|
617 }
|
|
618 }
|
|
619
|
|
620 if ((cbp & 0x30)) {
|
|
621 for (i=0; i < 2; ++i) {
|
|
622 if (svq3_decode_block (&s->gb, &h->mb[16*(16 + 4*i)], 0, 3))
|
|
623 return -1;
|
|
624 }
|
|
625
|
|
626 if ((cbp & 0x20)) {
|
|
627 for (i=0; i < 8; i++) {
|
|
628 h->non_zero_count_cache[ scan8[16+i] ] = 1;
|
|
629
|
|
630 if (svq3_decode_block (&s->gb, &h->mb[16*(16 + i)], 1, 1))
|
|
631 return -1;
|
|
632 }
|
|
633 }
|
|
634 }
|
|
635 }
|
|
636
|
|
637 s->current_picture.mb_type[mb_xy] = mb_type;
|
|
638
|
|
639 if (IS_INTRA(mb_type)) {
|
|
640 h->chroma_pred_mode = check_intra_pred_mode (h, DC_PRED8x8);
|
|
641 }
|
|
642
|
|
643 return 0;
|
|
644 }
|
|
645
|
|
646 static int svq3_decode_frame (AVCodecContext *avctx,
|
|
647 void *data, int *data_size,
|
|
648 uint8_t *buf, int buf_size) {
|
|
649 MpegEncContext *const s = avctx->priv_data;
|
|
650 H264Context *const h = avctx->priv_data;
|
|
651 int i;
|
|
652
|
|
653 s->flags = avctx->flags;
|
|
654
|
|
655 if (!s->context_initialized) {
|
|
656 s->width = (avctx->width + 15) & ~15;
|
|
657 s->height = (avctx->height + 15) & ~15;
|
|
658 h->b_stride = (s->width >> 2);
|
|
659 h->pred4x4[DIAG_DOWN_LEFT_PRED] = pred4x4_down_left_svq3_c;
|
|
660 h->pred16x16[PLANE_PRED8x8] = pred16x16_plane_svq3_c;
|
|
661 h->halfpel_flag = 1;
|
|
662 h->thirdpel_flag = 1;
|
|
663 h->chroma_qp = 4;
|
|
664
|
|
665 if (MPV_common_init (s) < 0)
|
|
666 return -1;
|
|
667
|
|
668 alloc_tables (h);
|
|
669 }
|
|
670 if (avctx->extradata && avctx->extradata_size >= 115
|
|
671 && !memcmp (avctx->extradata, "stsd", 4)) {
|
|
672
|
|
673 uint8_t *stsd = (uint8_t *) avctx->extradata + 114;
|
|
674
|
|
675 if ((*stsd >> 5) != 7 || avctx->extradata_size >= 118) {
|
|
676
|
|
677 if ((*stsd >> 5) == 7) {
|
|
678 stsd += 3; /* skip width, height (12 bits each) */
|
|
679 }
|
|
680
|
|
681 h->halfpel_flag = (*stsd >> 4) & 1;
|
|
682 h->thirdpel_flag = (*stsd >> 3) & 1;
|
|
683 }
|
|
684 }
|
|
685
|
|
686 if ((buf[0] & 0x9F) != 1) {
|
|
687 /* TODO: what? */
|
|
688 fprintf (stderr, "unsupported header (%02X)\n", buf[0]);
|
|
689 return -1;
|
|
690 } else {
|
|
691 int length = (buf[0] >> 5) & 3;
|
|
692 int offset = 0;
|
|
693
|
|
694 for (i=0; i < length; i++) {
|
|
695 offset = (offset << 8) | buf[i + 1];
|
|
696 }
|
|
697
|
|
698 if (buf_size < (offset + length + 1) || length == 0)
|
|
699 return -1;
|
|
700
|
|
701 memcpy (&buf[2], &buf[offset + 2], (length - 1));
|
|
702 }
|
|
703
|
|
704 init_get_bits (&s->gb, &buf[2], 8*(buf_size - 2));
|
|
705
|
|
706 if ((i = svq3_get_ue_golomb (&s->gb)) == INVALID_VLC || i >= 3)
|
|
707 return -1;
|
|
708
|
|
709 s->pict_type = golomb_to_pict_type[i];
|
|
710
|
|
711 /* unknown fields */
|
|
712 get_bits (&s->gb, 1);
|
|
713 get_bits (&s->gb, 8);
|
|
714
|
|
715 s->qscale = get_bits (&s->gb, 5);
|
|
716 s->adaptive_quant = get_bits (&s->gb, 1);
|
|
717
|
|
718 /* unknown fields */
|
|
719 get_bits (&s->gb, 1);
|
|
720 get_bits (&s->gb, 1);
|
|
721 get_bits (&s->gb, 2);
|
|
722
|
|
723 while (get_bits (&s->gb, 1)) {
|
|
724 get_bits (&s->gb, 8);
|
|
725 }
|
|
726
|
|
727 /* B-frames are not supported */
|
|
728 if (s->pict_type == B_TYPE/* && avctx->hurry_up*/)
|
|
729 return buf_size;
|
|
730
|
|
731 frame_start (h);
|
|
732
|
|
733 for (s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
|
|
734 for (s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
|
|
735 int mb_type = svq3_get_ue_golomb (&s->gb);
|
|
736
|
|
737 if (s->pict_type == I_TYPE) {
|
|
738 mb_type += 8;
|
|
739 }
|
|
740 if (mb_type > 32 || svq3_decode_mb (h, mb_type)) {
|
|
741 fprintf (stderr, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
|
|
742 return -1;
|
|
743 }
|
|
744
|
|
745 if (mb_type != 0) {
|
|
746 hl_decode_mb (h);
|
|
747 }
|
|
748 }
|
|
749 }
|
|
750
|
|
751 *(AVFrame *) data = *(AVFrame *) &s->current_picture;
|
|
752 *data_size = sizeof(AVFrame);
|
|
753
|
|
754 MPV_frame_end(s);
|
|
755
|
|
756 return buf_size;
|
|
757 }
|
|
758
|
|
759
|
|
760 AVCodec svq3_decoder = {
|
|
761 "svq3",
|
|
762 CODEC_TYPE_VIDEO,
|
|
763 CODEC_ID_SVQ3,
|
|
764 sizeof(H264Context),
|
|
765 decode_init,
|
|
766 NULL,
|
|
767 decode_end,
|
|
768 svq3_decode_frame,
|
|
769 CODEC_CAP_DR1,
|
|
770 };
|