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