2 * Copyright (c) 2003 The FFmpeg Project.
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.
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.
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
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. 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
27 * via the AVCodecContext's extradata[_size] field:
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)
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
46 #define FULLPEL_MODE 1
47 #define HALFPEL_MODE 2
48 #define THIRDPEL_MODE 3
50 /* dual scan (from some older h264 draft)
59 static const uint8_t svq3_scan[16]={
60 0+0*4, 1+0*4, 2+0*4, 2+1*4,
61 2+2*4, 3+0*4, 3+1*4, 3+2*4,
62 0+1*4, 0+2*4, 1+1*4, 1+2*4,
63 0+3*4, 1+3*4, 2+3*4, 3+3*4,
66 static const uint8_t svq3_pred_0[25][2] = {
69 { 0, 2 }, { 1, 1 }, { 2, 0 },
70 { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
71 { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
72 { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
73 { 2, 4 }, { 3, 3 }, { 4, 2 },
78 static const int8_t svq3_pred_1[6][6][5] = {
79 { { 2,-1,-1,-1,-1 }, { 2, 1,-1,-1,-1 }, { 1, 2,-1,-1,-1 },
80 { 2, 1,-1,-1,-1 }, { 1, 2,-1,-1,-1 }, { 1, 2,-1,-1,-1 } },
81 { { 0, 2,-1,-1,-1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 },
82 { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } },
83 { { 2, 0,-1,-1,-1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 },
84 { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } },
85 { { 2, 0,-1,-1,-1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 },
86 { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } },
87 { { 0, 2,-1,-1,-1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 },
88 { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } },
89 { { 0, 2,-1,-1,-1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 },
90 { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } },
93 static const struct { uint8_t run; uint8_t level; } svq3_dct_tables[2][16] = {
94 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
95 { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
96 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
97 { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
100 static const uint32_t svq3_dequant_coeff[32] = {
101 3881, 4351, 4890, 5481, 6154, 6914, 7761, 8718,
102 9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873,
103 24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683,
104 61694, 68745, 77615, 89113,100253,109366,126635,141533
108 static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp){
109 const int qmul= svq3_dequant_coeff[qp];
113 static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
114 static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
117 const int offset= y_offset[i];
118 const int z0= 13*(block[offset+stride*0] + block[offset+stride*4]);
119 const int z1= 13*(block[offset+stride*0] - block[offset+stride*4]);
120 const int z2= 7* block[offset+stride*1] - 17*block[offset+stride*5];
121 const int z3= 17* block[offset+stride*1] + 7*block[offset+stride*5];
130 const int offset= x_offset[i];
131 const int z0= 13*(temp[4*0+i] + temp[4*2+i]);
132 const int z1= 13*(temp[4*0+i] - temp[4*2+i]);
133 const int z2= 7* temp[4*1+i] - 17*temp[4*3+i];
134 const int z3= 17* temp[4*1+i] + 7*temp[4*3+i];
136 block[stride*0 +offset]= ((z0 + z3)*qmul + 0x80000)>>20;
137 block[stride*2 +offset]= ((z1 + z2)*qmul + 0x80000)>>20;
138 block[stride*8 +offset]= ((z1 - z2)*qmul + 0x80000)>>20;
139 block[stride*10+offset]= ((z0 - z3)*qmul + 0x80000)>>20;
144 static void svq3_add_idct_c (uint8_t *dst, DCTELEM *block, int stride, int qp, int dc){
145 const int qmul= svq3_dequant_coeff[qp];
147 uint8_t *cm = cropTbl + MAX_NEG_CROP;
150 dc = 13*13*((dc == 1) ? 1538*block[0] : ((qmul*(block[0] >> 3)) / 2));
154 for (i=0; i < 4; i++) {
155 const int z0= 13*(block[0 + 4*i] + block[2 + 4*i]);
156 const int z1= 13*(block[0 + 4*i] - block[2 + 4*i]);
157 const int z2= 7* block[1 + 4*i] - 17*block[3 + 4*i];
158 const int z3= 17* block[1 + 4*i] + 7*block[3 + 4*i];
160 block[0 + 4*i]= z0 + z3;
161 block[1 + 4*i]= z1 + z2;
162 block[2 + 4*i]= z1 - z2;
163 block[3 + 4*i]= z0 - z3;
166 for (i=0; i < 4; i++) {
167 const int z0= 13*(block[i + 4*0] + block[i + 4*2]);
168 const int z1= 13*(block[i + 4*0] - block[i + 4*2]);
169 const int z2= 7* block[i + 4*1] - 17*block[i + 4*3];
170 const int z3= 17* block[i + 4*1] + 7*block[i + 4*3];
171 const int rr= (dc + 0x80000);
173 dst[i + stride*0]= cm[ dst[i + stride*0] + (((z0 + z3)*qmul + rr) >> 20) ];
174 dst[i + stride*1]= cm[ dst[i + stride*1] + (((z1 + z2)*qmul + rr) >> 20) ];
175 dst[i + stride*2]= cm[ dst[i + stride*2] + (((z1 - z2)*qmul + rr) >> 20) ];
176 dst[i + stride*3]= cm[ dst[i + stride*3] + (((z0 - z3)*qmul + rr) >> 20) ];
180 static void pred4x4_down_left_svq3_c(uint8_t *src, uint8_t *topright, int stride){
183 const __attribute__((unused)) int unu0= t0;
184 const __attribute__((unused)) int unu1= l0;
186 src[0+0*stride]=(l1 + t1)>>1;
188 src[0+1*stride]=(l2 + t2)>>1;
201 src[3+3*stride]=(l3 + t3)>>1;
204 static void pred16x16_plane_svq3_c(uint8_t *src, int stride){
205 pred16x16_plane_compat_c(src, stride, 1);
208 static inline int svq3_decode_block (GetBitContext *gb, DCTELEM *block,
209 int index, const int type) {
211 static const uint8_t *const scan_patterns[4] =
212 { luma_dc_zigzag_scan, zigzag_scan, svq3_scan, chroma_dc_scan };
214 int run, level, sign, vlc, limit;
215 const int intra = (3 * type) >> 2;
216 const uint8_t *const scan = scan_patterns[type];
218 for (limit=(16 >> intra); index < 16; index=limit, limit+=8) {
219 for (; (vlc = svq3_get_ue_golomb (gb)) != 0; index++) {
221 if (vlc == INVALID_VLC)
224 sign = (vlc & 0x1) - 1;
225 vlc = (vlc + 1) >> 1;
231 } else if (vlc < 4) {
236 level = ((vlc + 9) >> 2) - run;
240 run = svq3_dct_tables[intra][vlc].run;
241 level = svq3_dct_tables[intra][vlc].level;
244 level = (vlc >> 3) + ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
247 level = (vlc >> 4) + ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
251 if ((index += run) >= limit)
254 block[scan[index]] = (level ^ sign) - sign;
265 static inline void svq3_mc_dir_part (MpegEncContext *s,
266 int x, int y, int width, int height,
267 int mx, int my, int dxy,
268 int thirdpel, int dir, int avg) {
270 const Picture *pic = (dir == 0) ? &s->last_picture : &s->next_picture;
273 int blocksize= 2 - (width>>3); //16->0, 8->1, 4->2
278 if (mx < 0 || mx >= (s->h_edge_pos - width - 1) ||
279 my < 0 || my >= (s->v_edge_pos - height - 1)) {
281 if ((s->flags & CODEC_FLAG_EMU_EDGE)) {
285 mx = clip (mx, -16, (s->h_edge_pos - width + 15));
286 my = clip (my, -16, (s->v_edge_pos - height + 15));
289 /* form component predictions */
290 dest = s->current_picture.data[0] + x + y*s->linesize;
291 src = pic->data[0] + mx + my*s->linesize;
294 ff_emulated_edge_mc (s, src, s->linesize, (width + 1), (height + 1),
295 mx, my, s->h_edge_pos, s->v_edge_pos);
296 src = s->edge_emu_buffer;
299 (avg ? s->dsp.avg_tpel_pixels_tab : s->dsp.put_tpel_pixels_tab)[dxy](dest, src, s->linesize, width, height);
301 (avg ? s->dsp.avg_pixels_tab : s->dsp.put_pixels_tab)[blocksize][dxy](dest, src, s->linesize, height);
303 if (!(s->flags & CODEC_FLAG_GRAY)) {
304 mx = (mx + (mx < (int) x)) >> 1;
305 my = (my + (my < (int) y)) >> 1;
306 width = (width >> 1);
307 height = (height >> 1);
310 for (i=1; i < 3; i++) {
311 dest = s->current_picture.data[i] + (x >> 1) + (y >> 1)*s->uvlinesize;
312 src = pic->data[i] + mx + my*s->uvlinesize;
315 ff_emulated_edge_mc (s, src, s->uvlinesize, (width + 1), (height + 1),
316 mx, my, (s->h_edge_pos >> 1), (s->v_edge_pos >> 1));
317 src = s->edge_emu_buffer;
320 (avg ? s->dsp.avg_tpel_pixels_tab : s->dsp.put_tpel_pixels_tab)[dxy](dest, src, s->uvlinesize, width, height);
322 (avg ? s->dsp.avg_pixels_tab : s->dsp.put_pixels_tab)[blocksize][dxy](dest, src, s->uvlinesize, height);
327 static int svq3_decode_mb (H264Context *h, unsigned int mb_type) {
328 int cbp, dir, mode, mx, my, dx, dy, x, y, part_width, part_height;
332 MpegEncContext *const s = (MpegEncContext *) h;
333 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
334 const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
336 h->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
337 h->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
338 h->topright_samples_available = 0xFFFF;
340 if (mb_type == 0) { /* SKIP */
341 if (s->pict_type == P_TYPE) {
342 svq3_mc_dir_part (s, 16*s->mb_x, 16*s->mb_y, 16, 16, 0, 0, 0, 0, 0, 0);
345 mb_type = MB_TYPE_SKIP;
347 for (dir=0; dir < 2; dir++) {
348 for (i=0; i < 4; i++) {
349 for (j=0; j < 4; j++) {
351 x = 16*s->mb_x + 4*j;
352 y = 16*s->mb_y + 4*i;
354 mx = 2*s->next_picture.motion_val[0][b_xy + j + i*h->b_stride][0];
355 my = 2*s->next_picture.motion_val[0][b_xy + j + i*h->b_stride][1];
358 mx = (mx * h->frame_num_offset) / h->prev_frame_num_offset;
359 my = (my * h->frame_num_offset) / h->prev_frame_num_offset;
361 mx = (mx * (h->frame_num_offset - h->prev_frame_num_offset)) / h->prev_frame_num_offset;
362 my = (my * (h->frame_num_offset - h->prev_frame_num_offset)) / h->prev_frame_num_offset;
365 mx = ((unsigned)(mx + 3 + 0x6000))/6 - 0x1000;
366 my = ((unsigned)(my + 3 + 0x6000))/6 - 0x1000;
367 dxy= (mx&1) + 2*(my&1);
369 /* update mv_cache */
370 s->current_picture.motion_val[dir][b_xy + j + i*h->b_stride][0] = 3*mx;
371 s->current_picture.motion_val[dir][b_xy + j + i*h->b_stride][1] = 3*my;
373 svq3_mc_dir_part (s, x, y, 4, 4, mx>>1, my>>1, dxy, 0, dir, (dir == 1));
378 if ((vlc = svq3_get_ue_golomb (&s->gb)) >= 48)
381 cbp = golomb_to_inter_cbp[vlc];
382 mb_type = MB_TYPE_16x16;
384 } else if (mb_type < 8) { /* INTER */
387 if (h->thirdpel_flag && h->halfpel_flag == !get_bits (&s->gb, 1)) {
388 mode = THIRDPEL_MODE;
389 } else if (h->halfpel_flag && h->thirdpel_flag == !get_bits (&s->gb, 1)) {
396 /* note ref_cache should contain here:
405 for (m=0; m < 2; m++) {
406 if (s->mb_x > 0 && h->intra4x4_pred_mode[mb_xy - 1][0] != -1) {
407 for (i=0; i < 4; i++) {
408 *(uint32_t *) h->mv_cache[m][scan8[0] - 1 + i*8] = *(uint32_t *) s->current_picture.motion_val[m][b_xy - 1 + i*h->b_stride];
411 for (i=0; i < 4; i++) {
412 *(uint32_t *) h->mv_cache[m][scan8[0] - 1 + i*8] = 0;
416 memcpy (h->mv_cache[m][scan8[0] - 1*8], s->current_picture.motion_val[m][b_xy - h->b_stride], 4*2*sizeof(int16_t));
417 memset (&h->ref_cache[m][scan8[0] - 1*8], (h->intra4x4_pred_mode[mb_xy - s->mb_stride][4] == -1) ? PART_NOT_AVAILABLE : 1, 4);
419 if (s->mb_x < (s->mb_width - 1)) {
420 *(uint32_t *) h->mv_cache[m][scan8[0] + 4 - 1*8] = *(uint32_t *) s->current_picture.motion_val[m][b_xy - h->b_stride + 4];
421 h->ref_cache[m][scan8[0] + 4 - 1*8] =
422 (h->intra4x4_pred_mode[mb_xy - s->mb_stride + 1][0] == -1 ||
423 h->intra4x4_pred_mode[mb_xy - s->mb_stride][4] == -1) ? PART_NOT_AVAILABLE : 1;
425 h->ref_cache[m][scan8[0] + 4 - 1*8] = PART_NOT_AVAILABLE;
427 *(uint32_t *) h->mv_cache[0][scan8[0] - 1 - 1*8] = *(uint32_t *) s->current_picture.motion_val[m][b_xy - h->b_stride - 1];
428 h->ref_cache[m][scan8[0] - 1 - 1*8] = (h->intra4x4_pred_mode[mb_xy - s->mb_stride - 1][3] == -1) ? PART_NOT_AVAILABLE : 1;
430 h->ref_cache[m][scan8[0] - 1 - 1*8] = PART_NOT_AVAILABLE;
432 memset (&h->ref_cache[m][scan8[0] - 1*8 - 1], PART_NOT_AVAILABLE, 8);
434 if (s->pict_type != B_TYPE)
438 /* decode motion vector(s) and form prediction(s) */
439 if (s->pict_type == P_TYPE) {
440 part_width = ((mb_type & 5) == 5) ? 4 : 8 << (mb_type & 1);
441 part_height = 16 >> ((unsigned) mb_type / 3);
444 } else { /* B_TYPE */
447 dir0 = (mb_type == 2) ? 1 : 0;
448 dir1 = (mb_type == 1) ? 0 : 1;
451 for (dir=dir0; dir <= dir1; dir++) {
452 for (i=0; i < 16; i+=part_height) {
453 for (j=0; j < 16; j+=part_width) {
454 int avg=(dir == 1 && dir0 != dir1);
458 k = ((j>>2)&1) + ((i>>1)&2) + ((j>>1)&4) + (i&8);
460 pred_motion (h, k, (part_width >> 2), dir, 1, &mx, &my);
462 /* clip motion vector prediction to frame border */
463 mx = clip (mx, -6*x, 6*(s->h_edge_pos - part_width - x));
464 my = clip (my, -6*y, 6*(s->v_edge_pos - part_height - y));
466 /* get motion vector differential */
467 dy = svq3_get_se_golomb (&s->gb);
468 dx = svq3_get_se_golomb (&s->gb);
470 if (dx == INVALID_VLC || dy == INVALID_VLC) {
473 /* compute motion vector */
474 if (mode == THIRDPEL_MODE) {
476 mx = ((mx + 1)>>1) + dx;
477 my = ((my + 1)>>1) + dy;
478 fx= ((unsigned)(mx + 0x3000))/3 - 0x1000;
479 fy= ((unsigned)(my + 0x3000))/3 - 0x1000;
480 dxy= (mx - 3*fx) + 4*(my - 3*fy);
482 svq3_mc_dir_part (s, x, y, part_width, part_height, fx, fy, dxy, 1, dir, avg);
485 } else if (mode == HALFPEL_MODE) {
486 mx = ((unsigned)(mx + 1 + 0x3000))/3 + dx - 0x1000;
487 my = ((unsigned)(my + 1 + 0x3000))/3 + dy - 0x1000;
488 dxy= (mx&1) + 2*(my&1);
490 svq3_mc_dir_part (s, x, y, part_width, part_height, mx>>1, my>>1, dxy, 0, dir, avg);
494 assert(mode == FULLPEL_MODE);
495 mx = ((unsigned)(mx + 3 + 0x6000))/6 + dx - 0x1000;
496 my = ((unsigned)(my + 3 + 0x6000))/6 + dy - 0x1000;
498 svq3_mc_dir_part (s, x, y, part_width, part_height, mx, my, 0, 0, dir, avg);
503 /* update mv_cache */
504 fill_rectangle(h->mv_cache[dir][scan8[k]], part_width>>2, part_height>>2, 8, pack16to32(mx,my), 4);
509 /* write back or clear motion vectors */
510 if (s->pict_type == P_TYPE || mb_type != 2) {
511 for (i=0; i < 4; i++) {
512 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));
515 for (i=0; i < 4; i++) {
516 memset (s->current_picture.motion_val[0][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
519 if (s->pict_type == B_TYPE) {
521 for (i=0; i < 4; i++) {
522 memcpy (s->current_picture.motion_val[1][b_xy + i*h->b_stride], h->mv_cache[1][scan8[0] + 8*i], 4*2*sizeof(int16_t));
525 for (i=0; i < 4; i++) {
526 memset (s->current_picture.motion_val[1][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
531 if ((vlc = svq3_get_ue_golomb (&s->gb)) >= 48)
534 cbp = golomb_to_inter_cbp[vlc];
535 mb_type = MB_TYPE_16x16;
536 } else if (mb_type == 8 || mb_type == 33) { /* INTRA4x4 */
537 memset (h->intra4x4_pred_mode_cache, -1, 8*5*sizeof(int8_t));
541 for (i=0; i < 4; i++) {
542 h->intra4x4_pred_mode_cache[scan8[0] - 1 + i*8] = h->intra4x4_pred_mode[mb_xy - 1][i];
544 if (h->intra4x4_pred_mode_cache[scan8[0] - 1] == -1) {
545 h->left_samples_available = 0x5F5F;
549 h->intra4x4_pred_mode_cache[4+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][4];
550 h->intra4x4_pred_mode_cache[5+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][5];
551 h->intra4x4_pred_mode_cache[6+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][6];
552 h->intra4x4_pred_mode_cache[7+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][3];
554 if (h->intra4x4_pred_mode_cache[4+8*0] == -1) {
555 h->top_samples_available = 0x33FF;
559 /* decode prediction codes for luma blocks */
560 for (i=0; i < 16; i+=2) {
561 vlc = svq3_get_ue_golomb (&s->gb);
566 left = &h->intra4x4_pred_mode_cache[scan8[i] - 1];
567 top = &h->intra4x4_pred_mode_cache[scan8[i] - 8];
569 left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
570 left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
572 if (left[1] == -1 || left[2] == -1)
576 /* DC_128_PRED block type */
577 for (i=0; i < 4; i++) {
578 memset (&h->intra4x4_pred_mode_cache[scan8[0] + 8*i], DC_PRED, 4);
582 write_back_intra_pred_mode (h);
585 check_intra4x4_pred_mode (h);
587 h->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
588 h->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
590 for (i=0; i < 4; i++) {
591 memset (&h->intra4x4_pred_mode_cache[scan8[0] + 8*i], DC_128_PRED, 4);
594 h->top_samples_available = 0x33FF;
595 h->left_samples_available = 0x5F5F;
598 if ((vlc = svq3_get_ue_golomb (&s->gb)) >= 48)
601 cbp = golomb_to_intra4x4_cbp[vlc];
602 mb_type = MB_TYPE_INTRA4x4;
603 } else { /* INTRA16x16 */
604 dir = i_mb_type_info[mb_type - 8].pred_mode;
605 dir = (dir >> 1) ^ 3*(dir & 1) ^ 1;
607 if ((h->intra16x16_pred_mode = check_intra_pred_mode (h, dir)) == -1)
610 cbp = i_mb_type_info[mb_type - 8].cbp;
611 mb_type = MB_TYPE_INTRA16x16;
614 if (!IS_INTER(mb_type) && s->pict_type != I_TYPE) {
615 for (i=0; i < 4; i++) {
616 memset (s->current_picture.motion_val[0][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
618 if (s->pict_type == B_TYPE) {
619 for (i=0; i < 4; i++) {
620 memset (s->current_picture.motion_val[1][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
624 if (!IS_INTRA4x4(mb_type)) {
625 memset (h->intra4x4_pred_mode[mb_xy], DC_PRED, 8);
627 if (!IS_SKIP(mb_type)) {
628 memset (h->non_zero_count_cache + 8, 0, 4*9*sizeof(uint8_t));
629 s->dsp.clear_blocks(h->mb);
632 if (IS_INTRA16x16(mb_type) || (s->pict_type != I_TYPE && s->adaptive_quant && cbp)) {
633 s->qscale += svq3_get_se_golomb (&s->gb);
638 if (IS_INTRA16x16(mb_type)) {
639 if (svq3_decode_block (&s->gb, h->mb, 0, 0))
643 if (!IS_SKIP(mb_type) && cbp) {
644 l = IS_INTRA16x16(mb_type) ? 1 : 0;
645 m = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
647 for (i=0; i < 4; i++) {
648 if ((cbp & (1 << i))) {
649 for (j=0; j < 4; j++) {
650 k = l ? ((j&1) + 2*(i&1) + 2*(j&2) + 4*(i&2)) : (4*i + j);
651 h->non_zero_count_cache[ scan8[k] ] = 1;
653 if (svq3_decode_block (&s->gb, &h->mb[16*k], l, m))
660 for (i=0; i < 2; ++i) {
661 if (svq3_decode_block (&s->gb, &h->mb[16*(16 + 4*i)], 0, 3))
666 for (i=0; i < 8; i++) {
667 h->non_zero_count_cache[ scan8[16+i] ] = 1;
669 if (svq3_decode_block (&s->gb, &h->mb[16*(16 + i)], 1, 1))
676 s->current_picture.mb_type[mb_xy] = mb_type;
678 if (IS_INTRA(mb_type)) {
679 h->chroma_pred_mode = check_intra_pred_mode (h, DC_PRED8x8);
685 static int svq3_decode_slice_header (H264Context *h) {
686 MpegEncContext *const s = (MpegEncContext *) h;
687 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
690 header = get_bits (&s->gb, 8);
692 if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
694 fprintf (stderr, "unsupported slice header (%02X)\n", header);
697 int length = (header >> 5) & 3;
699 h->next_slice_index = s->gb.index + 8*show_bits (&s->gb, 8*length) + 8*length;
701 if (h->next_slice_index > s->gb.size_in_bits)
704 s->gb.size_in_bits = h->next_slice_index - 8*(length - 1);
708 memcpy ((uint8_t *) &s->gb.buffer[s->gb.index >> 3],
709 &s->gb.buffer[s->gb.size_in_bits >> 3], (length - 1));
713 if ((i = svq3_get_ue_golomb (&s->gb)) == INVALID_VLC || i >= 3)
716 h->slice_type = golomb_to_pict_type[i];
718 if ((header & 0x9F) == 2) {
719 i = (s->mb_num < 64) ? 6 : (1 + av_log2 (s->mb_num - 1));
720 s->mb_skip_run = get_bits (&s->gb, i) - (s->mb_x + (s->mb_y * s->mb_width));
726 h->slice_num = get_bits (&s->gb, 8);
727 s->qscale = get_bits (&s->gb, 5);
728 s->adaptive_quant = get_bits1 (&s->gb);
733 if (h->unknown_svq3_flag) {
738 get_bits (&s->gb, 2);
740 while (get_bits1 (&s->gb)) {
741 get_bits (&s->gb, 8);
744 /* reset intra predictors and invalidate motion vector references */
746 memset (h->intra4x4_pred_mode[mb_xy - 1], -1, 4*sizeof(int8_t));
747 memset (h->intra4x4_pred_mode[mb_xy - s->mb_x], -1, 8*sizeof(int8_t)*s->mb_x);
750 memset (h->intra4x4_pred_mode[mb_xy - s->mb_stride], -1, 8*sizeof(int8_t)*(s->mb_width - s->mb_x));
753 h->intra4x4_pred_mode[mb_xy - s->mb_stride - 1][3] = -1;
760 static int svq3_decode_frame (AVCodecContext *avctx,
761 void *data, int *data_size,
762 uint8_t *buf, int buf_size) {
763 MpegEncContext *const s = avctx->priv_data;
764 H264Context *const h = avctx->priv_data;
769 s->flags = avctx->flags;
771 if (!s->context_initialized) {
772 s->width = avctx->width;
773 s->height = avctx->height;
774 h->pred4x4[DIAG_DOWN_LEFT_PRED] = pred4x4_down_left_svq3_c;
775 h->pred16x16[PLANE_PRED8x8] = pred16x16_plane_svq3_c;
777 h->thirdpel_flag = 1;
778 h->unknown_svq3_flag = 0;
781 if (MPV_common_init (s) < 0)
784 h->b_stride = 4*s->mb_width;
788 if (avctx->extradata && avctx->extradata_size >= 0x64
789 && !memcmp (avctx->extradata, "SVQ3", 4)) {
793 init_get_bits (&gb, (uint8_t *) avctx->extradata + 0x62,
794 8*(avctx->extradata_size - 0x62));
796 /* 'frame size code' and optional 'width, height' */
797 if (get_bits (&gb, 3) == 7) {
802 h->halfpel_flag = get_bits1 (&gb);
803 h->thirdpel_flag = get_bits1 (&gb);
811 s->low_delay = get_bits1 (&gb);
816 while (get_bits1 (&gb)) {
820 h->unknown_svq3_flag = get_bits1 (&gb);
821 avctx->has_b_frames = !s->low_delay;
825 /* special case for last picture */
827 if (s->next_picture_ptr && !s->low_delay) {
828 *(AVFrame *) data = *(AVFrame *) &s->next_picture;
829 *data_size = sizeof(AVFrame);
834 init_get_bits (&s->gb, buf, 8*buf_size);
836 s->mb_x = s->mb_y = 0;
838 if (svq3_decode_slice_header (h))
841 s->pict_type = h->slice_type;
842 s->picture_number = h->slice_num;
844 if(avctx->debug&FF_DEBUG_PICT_INFO){
845 printf("%c hpel:%d, tpel:%d aqp:%d qp:%d\n",
846 av_get_pict_type_char(s->pict_type), h->halfpel_flag, h->thirdpel_flag,
847 s->adaptive_quant, s->qscale
851 /* for hurry_up==5 */
852 s->current_picture.pict_type = s->pict_type;
853 s->current_picture.key_frame = (s->pict_type == I_TYPE);
855 /* skip b frames if we dont have reference frames */
856 if (s->last_picture_ptr == NULL && s->pict_type == B_TYPE) return 0;
857 /* skip b frames if we are in a hurry */
858 if (avctx->hurry_up && s->pict_type == B_TYPE) return 0;
859 /* skip everything if we are in a hurry >= 5 */
860 if (avctx->hurry_up >= 5) return 0;
862 if (s->next_p_frame_damaged) {
863 if (s->pict_type == B_TYPE)
866 s->next_p_frame_damaged = 0;
871 if (s->pict_type == B_TYPE) {
872 h->frame_num_offset = (h->slice_num - h->prev_frame_num);
874 if (h->frame_num_offset < 0) {
875 h->frame_num_offset += 256;
877 if (h->frame_num_offset == 0 || h->frame_num_offset >= h->prev_frame_num_offset) {
878 printf ("error in B-frame picture id\n");
882 h->prev_frame_num = h->frame_num;
883 h->frame_num = h->slice_num;
884 h->prev_frame_num_offset = (h->frame_num - h->prev_frame_num);
886 if (h->prev_frame_num_offset < 0) {
887 h->prev_frame_num_offset += 256;
896 h->ref_cache[m][scan8[0] + 8*i + j]= 1;
897 h->ref_cache[m][scan8[0] + 8*i + j]= PART_NOT_AVAILABLE;
901 for (s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
902 for (s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
904 if ( (s->gb.index + 7) >= s->gb.size_in_bits &&
905 ((s->gb.index & 7) == 0 || show_bits (&s->gb, (-s->gb.index & 7)) == 0)) {
907 s->gb.index = h->next_slice_index;
908 s->gb.size_in_bits = 8*buf_size;
910 if (svq3_decode_slice_header (h))
913 /* TODO: support s->mb_skip_run */
916 mb_type = svq3_get_ue_golomb (&s->gb);
918 if (s->pict_type == I_TYPE) {
920 } else if (s->pict_type == B_TYPE && mb_type >= 4) {
923 if (mb_type > 33 || svq3_decode_mb (h, mb_type)) {
924 fprintf (stderr, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
933 ff_draw_horiz_band(s, 16*s->mb_y, 16);
938 if (s->pict_type == B_TYPE || s->low_delay) {
939 *(AVFrame *) data = *(AVFrame *) &s->current_picture;
941 *(AVFrame *) data = *(AVFrame *) &s->last_picture;
944 avctx->frame_number = s->picture_number - 1;
946 /* dont output the last pic after seeking */
947 if (s->last_picture_ptr || s->low_delay) {
948 *data_size = sizeof(AVFrame);
955 AVCodec svq3_decoder = {
964 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,