2 * Copyright (c) 2003 The Libav Project
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * How to use this decoder:
23 * SVQ3 data is transported within Apple Quicktime files. Quicktime files
24 * have stsd atoms to describe media trak properties. A stsd atom for a
25 * video trak contains 1 or more ImageDescription atoms. These atoms begin
26 * with the 4-byte length of the atom followed by the codec fourcc. Some
27 * decoders need information in this atom to operate correctly. Such
28 * is the case with SVQ3. In order to get the best use out of this decoder,
29 * the calling app must make the SVQ3 ImageDescription atom available
30 * via the AVCodecContext's extradata[_size] field:
32 * AVCodecContext.extradata = pointer to ImageDescription, first characters
33 * are expected to be 'S', 'V', 'Q', and '3', NOT the 4-byte atom length
34 * AVCodecContext.extradata_size = size of ImageDescription atom memory
35 * buffer (which will be the same as the ImageDescription atom size field
36 * from the QT file, minus 4 bytes since the length is missing)
38 * You will know you have these parameters passed correctly when the decoder
39 * correctly decodes this file:
40 * http://samples.libav.org/V-codecs/SVQ3/Vertical400kbit.sorenson3.mov
45 #include "mpegvideo.h"
48 #include "h264data.h" // FIXME FIXME FIXME
50 #include "h264_mvpred.h"
52 #include "rectangle.h"
53 #include "vdpau_internal.h"
76 uint32_t watermark_key;
78 int next_p_frame_damaged;
81 int last_frame_output;
84 #define FULLPEL_MODE 1
85 #define HALFPEL_MODE 2
86 #define THIRDPEL_MODE 3
87 #define PREDICT_MODE 4
89 /* dual scan (from some older h264 draft)
98 static const uint8_t svq3_scan[16] = {
99 0 + 0 * 4, 1 + 0 * 4, 2 + 0 * 4, 2 + 1 * 4,
100 2 + 2 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4,
101 0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 1 + 2 * 4,
102 0 + 3 * 4, 1 + 3 * 4, 2 + 3 * 4, 3 + 3 * 4,
105 static const uint8_t svq3_pred_0[25][2] = {
108 { 0, 2 }, { 1, 1 }, { 2, 0 },
109 { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
110 { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
111 { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
112 { 2, 4 }, { 3, 3 }, { 4, 2 },
117 static const int8_t svq3_pred_1[6][6][5] = {
118 { { 2, -1, -1, -1, -1 }, { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 },
119 { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, { 1, 2, -1, -1, -1 } },
120 { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 },
121 { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } },
122 { { 2, 0, -1, -1, -1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 },
123 { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } },
124 { { 2, 0, -1, -1, -1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 },
125 { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } },
126 { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 },
127 { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } },
128 { { 0, 2, -1, -1, -1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 },
129 { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } },
132 static const struct {
135 } svq3_dct_tables[2][16] = {
136 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
137 { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
138 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
139 { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
142 static const uint32_t svq3_dequant_coeff[32] = {
143 3881, 4351, 4890, 5481, 6154, 6914, 7761, 8718,
144 9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873,
145 24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683,
146 61694, 68745, 77615, 89113, 100253, 109366, 126635, 141533
149 void ff_svq3_luma_dc_dequant_idct_c(int16_t *output, int16_t *input, int qp)
151 const int qmul = svq3_dequant_coeff[qp];
155 static const uint8_t x_offset[4] = { 0, 1 * stride, 4 * stride, 5 * stride };
157 for (i = 0; i < 4; i++) {
158 const int z0 = 13 * (input[4 * i + 0] + input[4 * i + 2]);
159 const int z1 = 13 * (input[4 * i + 0] - input[4 * i + 2]);
160 const int z2 = 7 * input[4 * i + 1] - 17 * input[4 * i + 3];
161 const int z3 = 17 * input[4 * i + 1] + 7 * input[4 * i + 3];
163 temp[4 * i + 0] = z0 + z3;
164 temp[4 * i + 1] = z1 + z2;
165 temp[4 * i + 2] = z1 - z2;
166 temp[4 * i + 3] = z0 - z3;
169 for (i = 0; i < 4; i++) {
170 const int offset = x_offset[i];
171 const int z0 = 13 * (temp[4 * 0 + i] + temp[4 * 2 + i]);
172 const int z1 = 13 * (temp[4 * 0 + i] - temp[4 * 2 + i]);
173 const int z2 = 7 * temp[4 * 1 + i] - 17 * temp[4 * 3 + i];
174 const int z3 = 17 * temp[4 * 1 + i] + 7 * temp[4 * 3 + i];
176 output[stride * 0 + offset] = (z0 + z3) * qmul + 0x80000 >> 20;
177 output[stride * 2 + offset] = (z1 + z2) * qmul + 0x80000 >> 20;
178 output[stride * 8 + offset] = (z1 - z2) * qmul + 0x80000 >> 20;
179 output[stride * 10 + offset] = (z0 - z3) * qmul + 0x80000 >> 20;
184 void ff_svq3_add_idct_c(uint8_t *dst, int16_t *block,
185 int stride, int qp, int dc)
187 const int qmul = svq3_dequant_coeff[qp];
191 dc = 13 * 13 * (dc == 1 ? 1538 * block[0]
192 : qmul * (block[0] >> 3) / 2);
196 for (i = 0; i < 4; i++) {
197 const int z0 = 13 * (block[0 + 4 * i] + block[2 + 4 * i]);
198 const int z1 = 13 * (block[0 + 4 * i] - block[2 + 4 * i]);
199 const int z2 = 7 * block[1 + 4 * i] - 17 * block[3 + 4 * i];
200 const int z3 = 17 * block[1 + 4 * i] + 7 * block[3 + 4 * i];
202 block[0 + 4 * i] = z0 + z3;
203 block[1 + 4 * i] = z1 + z2;
204 block[2 + 4 * i] = z1 - z2;
205 block[3 + 4 * i] = z0 - z3;
208 for (i = 0; i < 4; i++) {
209 const int z0 = 13 * (block[i + 4 * 0] + block[i + 4 * 2]);
210 const int z1 = 13 * (block[i + 4 * 0] - block[i + 4 * 2]);
211 const int z2 = 7 * block[i + 4 * 1] - 17 * block[i + 4 * 3];
212 const int z3 = 17 * block[i + 4 * 1] + 7 * block[i + 4 * 3];
213 const int rr = (dc + 0x80000);
215 dst[i + stride * 0] = av_clip_uint8(dst[i + stride * 0] + ((z0 + z3) * qmul + rr >> 20));
216 dst[i + stride * 1] = av_clip_uint8(dst[i + stride * 1] + ((z1 + z2) * qmul + rr >> 20));
217 dst[i + stride * 2] = av_clip_uint8(dst[i + stride * 2] + ((z1 - z2) * qmul + rr >> 20));
218 dst[i + stride * 3] = av_clip_uint8(dst[i + stride * 3] + ((z0 - z3) * qmul + rr >> 20));
222 static inline int svq3_decode_block(GetBitContext *gb, int16_t *block,
223 int index, const int type)
225 static const uint8_t *const scan_patterns[4] =
226 { luma_dc_zigzag_scan, zigzag_scan, svq3_scan, chroma_dc_scan };
228 int run, level, limit;
230 const int intra = 3 * type >> 2;
231 const uint8_t *const scan = scan_patterns[type];
233 for (limit = (16 >> intra); index < 16; index = limit, limit += 8) {
234 for (; (vlc = svq3_get_ue_golomb(gb)) != 0; index++) {
235 int sign = (vlc & 1) ? 0 : -1;
242 } else if (vlc < 4) {
247 level = (vlc + 9 >> 2) - run;
251 run = svq3_dct_tables[intra][vlc].run;
252 level = svq3_dct_tables[intra][vlc].level;
256 ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
260 ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
264 if ((index += run) >= limit)
267 block[scan[index]] = (level ^ sign) - sign;
278 static inline void svq3_mc_dir_part(SVQ3Context *s,
279 int x, int y, int width, int height,
280 int mx, int my, int dxy,
281 int thirdpel, int dir, int avg)
283 H264Context *h = &s->h;
284 const Picture *pic = (dir == 0) ? s->last_pic : s->next_pic;
287 int blocksize = 2 - (width >> 3); // 16->0, 8->1, 4->2
292 if (mx < 0 || mx >= s->h_edge_pos - width - 1 ||
293 my < 0 || my >= s->v_edge_pos - height - 1) {
294 if ((h->flags & CODEC_FLAG_EMU_EDGE))
297 mx = av_clip(mx, -16, s->h_edge_pos - width + 15);
298 my = av_clip(my, -16, s->v_edge_pos - height + 15);
301 /* form component predictions */
302 dest = h->cur_pic.f.data[0] + x + y * h->linesize;
303 src = pic->f.data[0] + mx + my * h->linesize;
306 h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src, h->linesize,
307 width + 1, height + 1,
308 mx, my, s->h_edge_pos, s->v_edge_pos);
309 src = h->edge_emu_buffer;
312 (avg ? h->dsp.avg_tpel_pixels_tab
313 : h->dsp.put_tpel_pixels_tab)[dxy](dest, src, h->linesize,
316 (avg ? h->dsp.avg_pixels_tab
317 : h->dsp.put_pixels_tab)[blocksize][dxy](dest, src, h->linesize,
320 if (!(h->flags & CODEC_FLAG_GRAY)) {
321 mx = mx + (mx < (int) x) >> 1;
322 my = my + (my < (int) y) >> 1;
324 height = height >> 1;
327 for (i = 1; i < 3; i++) {
328 dest = h->cur_pic.f.data[i] + (x >> 1) + (y >> 1) * h->uvlinesize;
329 src = pic->f.data[i] + mx + my * h->uvlinesize;
332 h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src, h->uvlinesize,
333 width + 1, height + 1,
334 mx, my, (s->h_edge_pos >> 1),
336 src = h->edge_emu_buffer;
339 (avg ? h->dsp.avg_tpel_pixels_tab
340 : h->dsp.put_tpel_pixels_tab)[dxy](dest, src,
344 (avg ? h->dsp.avg_pixels_tab
345 : h->dsp.put_pixels_tab)[blocksize][dxy](dest, src,
352 static inline int svq3_mc_dir(SVQ3Context *s, int size, int mode,
355 int i, j, k, mx, my, dx, dy, x, y;
356 H264Context *h = &s->h;
357 const int part_width = ((size & 5) == 4) ? 4 : 16 >> (size & 1);
358 const int part_height = 16 >> ((unsigned)(size + 1) / 3);
359 const int extra_width = (mode == PREDICT_MODE) ? -16 * 6 : 0;
360 const int h_edge_pos = 6 * (s->h_edge_pos - part_width) - extra_width;
361 const int v_edge_pos = 6 * (s->v_edge_pos - part_height) - extra_width;
363 for (i = 0; i < 16; i += part_height)
364 for (j = 0; j < 16; j += part_width) {
365 const int b_xy = (4 * h->mb_x + (j >> 2)) +
366 (4 * h->mb_y + (i >> 2)) * h->b_stride;
368 x = 16 * h->mb_x + j;
369 y = 16 * h->mb_y + i;
370 k = (j >> 2 & 1) + (i >> 1 & 2) +
371 (j >> 1 & 4) + (i & 8);
373 if (mode != PREDICT_MODE) {
374 pred_motion(h, k, part_width >> 2, dir, 1, &mx, &my);
376 mx = s->next_pic->f.motion_val[0][b_xy][0] << 1;
377 my = s->next_pic->f.motion_val[0][b_xy][1] << 1;
380 mx = mx * h->frame_num_offset /
381 h->prev_frame_num_offset + 1 >> 1;
382 my = my * h->frame_num_offset /
383 h->prev_frame_num_offset + 1 >> 1;
385 mx = mx * (h->frame_num_offset - h->prev_frame_num_offset) /
386 h->prev_frame_num_offset + 1 >> 1;
387 my = my * (h->frame_num_offset - h->prev_frame_num_offset) /
388 h->prev_frame_num_offset + 1 >> 1;
392 /* clip motion vector prediction to frame border */
393 mx = av_clip(mx, extra_width - 6 * x, h_edge_pos - 6 * x);
394 my = av_clip(my, extra_width - 6 * y, v_edge_pos - 6 * y);
396 /* get (optional) motion vector differential */
397 if (mode == PREDICT_MODE) {
400 dy = svq3_get_se_golomb(&h->gb);
401 dx = svq3_get_se_golomb(&h->gb);
403 if (dx == INVALID_VLC || dy == INVALID_VLC) {
404 av_log(h->avctx, AV_LOG_ERROR, "invalid MV vlc\n");
409 /* compute motion vector */
410 if (mode == THIRDPEL_MODE) {
412 mx = (mx + 1 >> 1) + dx;
413 my = (my + 1 >> 1) + dy;
414 fx = (unsigned)(mx + 0x3000) / 3 - 0x1000;
415 fy = (unsigned)(my + 0x3000) / 3 - 0x1000;
416 dxy = (mx - 3 * fx) + 4 * (my - 3 * fy);
418 svq3_mc_dir_part(s, x, y, part_width, part_height,
419 fx, fy, dxy, 1, dir, avg);
422 } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) {
423 mx = (unsigned)(mx + 1 + 0x3000) / 3 + dx - 0x1000;
424 my = (unsigned)(my + 1 + 0x3000) / 3 + dy - 0x1000;
425 dxy = (mx & 1) + 2 * (my & 1);
427 svq3_mc_dir_part(s, x, y, part_width, part_height,
428 mx >> 1, my >> 1, dxy, 0, dir, avg);
432 mx = (unsigned)(mx + 3 + 0x6000) / 6 + dx - 0x1000;
433 my = (unsigned)(my + 3 + 0x6000) / 6 + dy - 0x1000;
435 svq3_mc_dir_part(s, x, y, part_width, part_height,
436 mx, my, 0, 0, dir, avg);
441 /* update mv_cache */
442 if (mode != PREDICT_MODE) {
443 int32_t mv = pack16to32(mx, my);
445 if (part_height == 8 && i < 8) {
446 AV_WN32A(h->mv_cache[dir][scan8[k] + 1 * 8], mv);
448 if (part_width == 8 && j < 8)
449 AV_WN32A(h->mv_cache[dir][scan8[k] + 1 + 1 * 8], mv);
451 if (part_width == 8 && j < 8)
452 AV_WN32A(h->mv_cache[dir][scan8[k] + 1], mv);
453 if (part_width == 4 || part_height == 4)
454 AV_WN32A(h->mv_cache[dir][scan8[k]], mv);
457 /* write back motion vectors */
458 fill_rectangle(h->cur_pic.f.motion_val[dir][b_xy],
459 part_width >> 2, part_height >> 2, h->b_stride,
460 pack16to32(mx, my), 4);
466 static int svq3_decode_mb(SVQ3Context *s, unsigned int mb_type)
468 H264Context *h = &s->h;
469 int i, j, k, m, dir, mode;
473 const int mb_xy = h->mb_xy;
474 const int b_xy = 4 * h->mb_x + 4 * h->mb_y * h->b_stride;
476 h->top_samples_available = (h->mb_y == 0) ? 0x33FF : 0xFFFF;
477 h->left_samples_available = (h->mb_x == 0) ? 0x5F5F : 0xFFFF;
478 h->topright_samples_available = 0xFFFF;
480 if (mb_type == 0) { /* SKIP */
481 if (h->pict_type == AV_PICTURE_TYPE_P ||
482 s->next_pic->f.mb_type[mb_xy] == -1) {
483 svq3_mc_dir_part(s, 16 * h->mb_x, 16 * h->mb_y, 16, 16,
486 if (h->pict_type == AV_PICTURE_TYPE_B)
487 svq3_mc_dir_part(s, 16 * h->mb_x, 16 * h->mb_y, 16, 16,
490 mb_type = MB_TYPE_SKIP;
492 mb_type = FFMIN(s->next_pic->f.mb_type[mb_xy], 6);
493 if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 0, 0) < 0)
495 if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 1, 1) < 0)
498 mb_type = MB_TYPE_16x16;
500 } else if (mb_type < 8) { /* INTER */
501 if (s->thirdpel_flag && s->halfpel_flag == !get_bits1(&h->gb))
502 mode = THIRDPEL_MODE;
503 else if (s->halfpel_flag &&
504 s->thirdpel_flag == !get_bits1(&h->gb))
510 /* note ref_cache should contain here:
518 for (m = 0; m < 2; m++) {
519 if (h->mb_x > 0 && h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6] != -1) {
520 for (i = 0; i < 4; i++)
521 AV_COPY32(h->mv_cache[m][scan8[0] - 1 + i * 8],
522 h->cur_pic.f.motion_val[m][b_xy - 1 + i * h->b_stride]);
524 for (i = 0; i < 4; i++)
525 AV_ZERO32(h->mv_cache[m][scan8[0] - 1 + i * 8]);
528 memcpy(h->mv_cache[m][scan8[0] - 1 * 8],
529 h->cur_pic.f.motion_val[m][b_xy - h->b_stride],
530 4 * 2 * sizeof(int16_t));
531 memset(&h->ref_cache[m][scan8[0] - 1 * 8],
532 (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);
534 if (h->mb_x < h->mb_width - 1) {
535 AV_COPY32(h->mv_cache[m][scan8[0] + 4 - 1 * 8],
536 h->cur_pic.f.motion_val[m][b_xy - h->b_stride + 4]);
537 h->ref_cache[m][scan8[0] + 4 - 1 * 8] =
538 (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride + 1] + 6] == -1 ||
539 h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1;
541 h->ref_cache[m][scan8[0] + 4 - 1 * 8] = PART_NOT_AVAILABLE;
543 AV_COPY32(h->mv_cache[m][scan8[0] - 1 - 1 * 8],
544 h->cur_pic.f.motion_val[m][b_xy - h->b_stride - 1]);
545 h->ref_cache[m][scan8[0] - 1 - 1 * 8] =
546 (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride - 1] + 3] == -1) ? PART_NOT_AVAILABLE : 1;
548 h->ref_cache[m][scan8[0] - 1 - 1 * 8] = PART_NOT_AVAILABLE;
550 memset(&h->ref_cache[m][scan8[0] - 1 * 8 - 1],
551 PART_NOT_AVAILABLE, 8);
553 if (h->pict_type != AV_PICTURE_TYPE_B)
557 /* decode motion vector(s) and form prediction(s) */
558 if (h->pict_type == AV_PICTURE_TYPE_P) {
559 if (svq3_mc_dir(s, mb_type - 1, mode, 0, 0) < 0)
561 } else { /* AV_PICTURE_TYPE_B */
563 if (svq3_mc_dir(s, 0, mode, 0, 0) < 0)
566 for (i = 0; i < 4; i++)
567 memset(h->cur_pic.f.motion_val[0][b_xy + i * h->b_stride],
568 0, 4 * 2 * sizeof(int16_t));
571 if (svq3_mc_dir(s, 0, mode, 1, mb_type == 3) < 0)
574 for (i = 0; i < 4; i++)
575 memset(h->cur_pic.f.motion_val[1][b_xy + i * h->b_stride],
576 0, 4 * 2 * sizeof(int16_t));
580 mb_type = MB_TYPE_16x16;
581 } else if (mb_type == 8 || mb_type == 33) { /* INTRA4x4 */
582 memset(h->intra4x4_pred_mode_cache, -1, 8 * 5 * sizeof(int8_t));
586 for (i = 0; i < 4; i++)
587 h->intra4x4_pred_mode_cache[scan8[0] - 1 + i * 8] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6 - i];
588 if (h->intra4x4_pred_mode_cache[scan8[0] - 1] == -1)
589 h->left_samples_available = 0x5F5F;
592 h->intra4x4_pred_mode_cache[4 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 0];
593 h->intra4x4_pred_mode_cache[5 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 1];
594 h->intra4x4_pred_mode_cache[6 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 2];
595 h->intra4x4_pred_mode_cache[7 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 3];
597 if (h->intra4x4_pred_mode_cache[4 + 8 * 0] == -1)
598 h->top_samples_available = 0x33FF;
601 /* decode prediction codes for luma blocks */
602 for (i = 0; i < 16; i += 2) {
603 vlc = svq3_get_ue_golomb(&h->gb);
606 av_log(h->avctx, AV_LOG_ERROR, "luma prediction:%d\n", vlc);
610 left = &h->intra4x4_pred_mode_cache[scan8[i] - 1];
611 top = &h->intra4x4_pred_mode_cache[scan8[i] - 8];
613 left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
614 left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
616 if (left[1] == -1 || left[2] == -1) {
617 av_log(h->avctx, AV_LOG_ERROR, "weird prediction\n");
621 } else { /* mb_type == 33, DC_128_PRED block type */
622 for (i = 0; i < 4; i++)
623 memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_PRED, 4);
626 write_back_intra_pred_mode(h);
629 ff_h264_check_intra4x4_pred_mode(h);
631 h->top_samples_available = (h->mb_y == 0) ? 0x33FF : 0xFFFF;
632 h->left_samples_available = (h->mb_x == 0) ? 0x5F5F : 0xFFFF;
634 for (i = 0; i < 4; i++)
635 memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_128_PRED, 4);
637 h->top_samples_available = 0x33FF;
638 h->left_samples_available = 0x5F5F;
641 mb_type = MB_TYPE_INTRA4x4;
642 } else { /* INTRA16x16 */
643 dir = i_mb_type_info[mb_type - 8].pred_mode;
644 dir = (dir >> 1) ^ 3 * (dir & 1) ^ 1;
646 if ((h->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(h, dir, 0)) == -1) {
647 av_log(h->avctx, AV_LOG_ERROR, "check_intra_pred_mode = -1\n");
651 cbp = i_mb_type_info[mb_type - 8].cbp;
652 mb_type = MB_TYPE_INTRA16x16;
655 if (!IS_INTER(mb_type) && h->pict_type != AV_PICTURE_TYPE_I) {
656 for (i = 0; i < 4; i++)
657 memset(h->cur_pic.f.motion_val[0][b_xy + i * h->b_stride],
658 0, 4 * 2 * sizeof(int16_t));
659 if (h->pict_type == AV_PICTURE_TYPE_B) {
660 for (i = 0; i < 4; i++)
661 memset(h->cur_pic.f.motion_val[1][b_xy + i * h->b_stride],
662 0, 4 * 2 * sizeof(int16_t));
665 if (!IS_INTRA4x4(mb_type)) {
666 memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy], DC_PRED, 8);
668 if (!IS_SKIP(mb_type) || h->pict_type == AV_PICTURE_TYPE_B) {
669 memset(h->non_zero_count_cache + 8, 0, 14 * 8 * sizeof(uint8_t));
670 h->dsp.clear_blocks(h->mb + 0);
671 h->dsp.clear_blocks(h->mb + 384);
674 if (!IS_INTRA16x16(mb_type) &&
675 (!IS_SKIP(mb_type) || h->pict_type == AV_PICTURE_TYPE_B)) {
676 if ((vlc = svq3_get_ue_golomb(&h->gb)) >= 48) {
677 av_log(h->avctx, AV_LOG_ERROR, "cbp_vlc=%d\n", vlc);
681 cbp = IS_INTRA(mb_type) ? golomb_to_intra4x4_cbp[vlc]
682 : golomb_to_inter_cbp[vlc];
684 if (IS_INTRA16x16(mb_type) ||
685 (h->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
686 h->qscale += svq3_get_se_golomb(&h->gb);
688 if (h->qscale > 31u) {
689 av_log(h->avctx, AV_LOG_ERROR, "qscale:%d\n", h->qscale);
693 if (IS_INTRA16x16(mb_type)) {
694 AV_ZERO128(h->mb_luma_dc[0] + 0);
695 AV_ZERO128(h->mb_luma_dc[0] + 8);
696 if (svq3_decode_block(&h->gb, h->mb_luma_dc[0], 0, 1)) {
697 av_log(h->avctx, AV_LOG_ERROR,
698 "error while decoding intra luma dc\n");
704 const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
705 const int type = ((h->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
707 for (i = 0; i < 4; i++)
708 if ((cbp & (1 << i))) {
709 for (j = 0; j < 4; j++) {
710 k = index ? (1 * (j & 1) + 2 * (i & 1) +
711 2 * (j & 2) + 4 * (i & 2))
713 h->non_zero_count_cache[scan8[k]] = 1;
715 if (svq3_decode_block(&h->gb, &h->mb[16 * k], index, type)) {
716 av_log(h->avctx, AV_LOG_ERROR,
717 "error while decoding block\n");
724 for (i = 1; i < 3; ++i)
725 if (svq3_decode_block(&h->gb, &h->mb[16 * 16 * i], 0, 3)) {
726 av_log(h->avctx, AV_LOG_ERROR,
727 "error while decoding chroma dc block\n");
732 for (i = 1; i < 3; i++) {
733 for (j = 0; j < 4; j++) {
735 h->non_zero_count_cache[scan8[k]] = 1;
737 if (svq3_decode_block(&h->gb, &h->mb[16 * k], 1, 1)) {
738 av_log(h->avctx, AV_LOG_ERROR,
739 "error while decoding chroma ac block\n");
749 h->cur_pic.f.mb_type[mb_xy] = mb_type;
751 if (IS_INTRA(mb_type))
752 h->chroma_pred_mode = ff_h264_check_intra_pred_mode(h, DC_PRED8x8, 1);
757 static int svq3_decode_slice_header(AVCodecContext *avctx)
759 SVQ3Context *s = avctx->priv_data;
760 H264Context *h = &s->h;
761 const int mb_xy = h->mb_xy;
765 header = get_bits(&h->gb, 8);
767 if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
769 av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
772 int length = header >> 5 & 3;
774 s->next_slice_index = get_bits_count(&h->gb) +
775 8 * show_bits(&h->gb, 8 * length) +
778 if (s->next_slice_index > h->gb.size_in_bits) {
779 av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
783 h->gb.size_in_bits = s->next_slice_index - 8 * (length - 1);
784 skip_bits(&h->gb, 8);
786 if (s->watermark_key) {
787 uint32_t header = AV_RL32(&h->gb.buffer[(get_bits_count(&h->gb) >> 3) + 1]);
788 AV_WL32(&h->gb.buffer[(get_bits_count(&h->gb) >> 3) + 1],
789 header ^ s->watermark_key);
792 memcpy((uint8_t *) &h->gb.buffer[get_bits_count(&h->gb) >> 3],
793 &h->gb.buffer[h->gb.size_in_bits >> 3], length - 1);
795 skip_bits_long(&h->gb, 0);
798 if ((slice_id = svq3_get_ue_golomb(&h->gb)) >= 3) {
799 av_log(h->avctx, AV_LOG_ERROR, "illegal slice type %d \n", slice_id);
803 h->slice_type = golomb_to_pict_type[slice_id];
805 if ((header & 0x9F) == 2) {
806 i = (h->mb_num < 64) ? 6 : (1 + av_log2(h->mb_num - 1));
807 h->mb_skip_run = get_bits(&h->gb, i) -
808 (h->mb_y * h->mb_width + h->mb_x);
814 h->slice_num = get_bits(&h->gb, 8);
815 h->qscale = get_bits(&h->gb, 5);
816 s->adaptive_quant = get_bits1(&h->gb);
825 skip_bits(&h->gb, 2);
827 while (get_bits1(&h->gb))
828 skip_bits(&h->gb, 8);
830 /* reset intra predictors and invalidate motion vector references */
832 memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - 1] + 3,
833 -1, 4 * sizeof(int8_t));
834 memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - h->mb_x],
835 -1, 8 * sizeof(int8_t) * h->mb_x);
838 memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - h->mb_stride],
839 -1, 8 * sizeof(int8_t) * (h->mb_width - h->mb_x));
842 h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride - 1] + 3] = -1;
848 static av_cold int svq3_decode_init(AVCodecContext *avctx)
850 SVQ3Context *s = avctx->priv_data;
851 H264Context *h = &s->h;
853 unsigned char *extradata;
854 unsigned char *extradata_end;
856 int marker_found = 0;
858 s->cur_pic = av_mallocz(sizeof(*s->cur_pic));
859 s->last_pic = av_mallocz(sizeof(*s->last_pic));
860 s->next_pic = av_mallocz(sizeof(*s->next_pic));
861 if (!s->next_pic || !s->last_pic || !s->cur_pic) {
862 av_freep(&s->cur_pic);
863 av_freep(&s->last_pic);
864 av_freep(&s->next_pic);
865 return AVERROR(ENOMEM);
868 if (ff_h264_decode_init(avctx) < 0)
871 h->flags = avctx->flags;
873 h->picture_structure = PICT_FRAME;
874 avctx->pix_fmt = avctx->codec->pix_fmts[0];
876 h->chroma_qp[0] = h->chroma_qp[1] = 4;
877 h->chroma_x_shift = h->chroma_y_shift = 1;
880 s->thirdpel_flag = 1;
883 /* prowl for the "SEQH" marker in the extradata */
884 extradata = (unsigned char *)avctx->extradata;
885 extradata_end = avctx->extradata + avctx->extradata_size;
887 for (m = 0; m + 8 < avctx->extradata_size; m++) {
888 if (!memcmp(extradata, "SEQH", 4)) {
896 /* if a match was found, parse the extra data */
901 size = AV_RB32(&extradata[4]);
902 if (size > extradata_end - extradata - 8)
903 return AVERROR_INVALIDDATA;
904 init_get_bits(&gb, extradata + 8, size * 8);
906 /* 'frame size code' and optional 'width, height' */
907 frame_size_code = get_bits(&gb, 3);
908 switch (frame_size_code) {
938 avctx->width = get_bits(&gb, 12);
939 avctx->height = get_bits(&gb, 12);
943 s->halfpel_flag = get_bits1(&gb);
944 s->thirdpel_flag = get_bits1(&gb);
952 h->low_delay = get_bits1(&gb);
957 while (get_bits1(&gb))
960 s->unknown_flag = get_bits1(&gb);
961 avctx->has_b_frames = !h->low_delay;
962 if (s->unknown_flag) {
964 unsigned watermark_width = svq3_get_ue_golomb(&gb);
965 unsigned watermark_height = svq3_get_ue_golomb(&gb);
966 int u1 = svq3_get_ue_golomb(&gb);
967 int u2 = get_bits(&gb, 8);
968 int u3 = get_bits(&gb, 2);
969 int u4 = svq3_get_ue_golomb(&gb);
970 unsigned long buf_len = watermark_width *
971 watermark_height * 4;
972 int offset = get_bits_count(&gb) + 7 >> 3;
975 if ((uint64_t)watermark_width * 4 > UINT_MAX / watermark_height)
978 buf = av_malloc(buf_len);
979 av_log(avctx, AV_LOG_DEBUG, "watermark size: %dx%d\n",
980 watermark_width, watermark_height);
981 av_log(avctx, AV_LOG_DEBUG,
982 "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n",
983 u1, u2, u3, u4, offset);
984 if (uncompress(buf, &buf_len, extradata + 8 + offset,
985 size - offset) != Z_OK) {
986 av_log(avctx, AV_LOG_ERROR,
987 "could not uncompress watermark logo\n");
991 s->watermark_key = ff_svq1_packet_checksum(buf, buf_len, 0);
992 s->watermark_key = s->watermark_key << 16 | s->watermark_key;
993 av_log(avctx, AV_LOG_DEBUG,
994 "watermark key %#x\n", s->watermark_key);
997 av_log(avctx, AV_LOG_ERROR,
998 "this svq3 file contains watermark which need zlib support compiled in\n");
1004 h->width = avctx->width;
1005 h->height = avctx->height;
1006 h->mb_width = (h->width + 15) / 16;
1007 h->mb_height = (h->height + 15) / 16;
1008 h->mb_stride = h->mb_width + 1;
1009 h->mb_num = h->mb_width * h->mb_height;
1010 h->b_stride = 4 * h->mb_width;
1011 s->h_edge_pos = h->mb_width * 16;
1012 s->v_edge_pos = h->mb_height * 16;
1014 if (ff_h264_alloc_tables(h) < 0) {
1015 av_log(avctx, AV_LOG_ERROR, "svq3 memory allocation failed\n");
1016 return AVERROR(ENOMEM);
1022 static int get_buffer(AVCodecContext *avctx, Picture *pic)
1024 SVQ3Context *s = avctx->priv_data;
1025 H264Context *h = &s->h;
1026 const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
1027 const int mb_array_size = h->mb_stride * h->mb_height;
1028 const int b4_stride = h->mb_width * 4 + 1;
1029 const int b4_array_size = b4_stride * h->mb_height * 4;
1032 if (!pic->motion_val_base[0]) {
1035 pic->mb_type_base = av_mallocz((big_mb_num + h->mb_stride) * sizeof(uint32_t));
1036 if (!pic->mb_type_base)
1037 return AVERROR(ENOMEM);
1038 pic->f.mb_type = pic->mb_type_base + 2 * h->mb_stride + 1;
1040 for (i = 0; i < 2; i++) {
1041 pic->motion_val_base[i] = av_mallocz(2 * (b4_array_size + 4) * sizeof(int16_t));
1042 pic->f.ref_index[i] = av_mallocz(4 * mb_array_size);
1043 if (!pic->motion_val_base[i] || !pic->f.ref_index[i])
1044 return AVERROR(ENOMEM);
1046 pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
1049 pic->f.motion_subsample_log2 = 2;
1050 pic->f.reference = !(h->pict_type == AV_PICTURE_TYPE_B);
1052 ret = ff_get_buffer(avctx, &pic->f);
1054 h->linesize = pic->f.linesize[0];
1055 h->uvlinesize = pic->f.linesize[1];
1060 static int svq3_decode_frame(AVCodecContext *avctx, void *data,
1061 int *got_frame, AVPacket *avpkt)
1063 const uint8_t *buf = avpkt->data;
1064 SVQ3Context *s = avctx->priv_data;
1065 H264Context *h = &s->h;
1066 int buf_size = avpkt->size;
1069 /* special case for last picture */
1070 if (buf_size == 0) {
1071 if (s->next_pic->f.data[0] && !h->low_delay && !s->last_frame_output) {
1072 *(AVFrame *) data = s->next_pic->f;
1073 s->last_frame_output = 1;
1079 init_get_bits(&h->gb, buf, 8 * buf_size);
1081 h->mb_x = h->mb_y = h->mb_xy = 0;
1083 if (svq3_decode_slice_header(avctx))
1086 h->pict_type = h->slice_type;
1088 if (h->pict_type != AV_PICTURE_TYPE_B)
1089 FFSWAP(Picture*, s->next_pic, s->last_pic);
1091 if (s->cur_pic->f.data[0])
1092 avctx->release_buffer(avctx, &s->cur_pic->f);
1094 /* for skipping the frame */
1095 s->cur_pic->f.pict_type = h->pict_type;
1096 s->cur_pic->f.key_frame = (h->pict_type == AV_PICTURE_TYPE_I);
1098 ret = get_buffer(avctx, s->cur_pic);
1102 h->cur_pic_ptr = s->cur_pic;
1103 h->cur_pic = *s->cur_pic;
1105 for (i = 0; i < 16; i++) {
1106 h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
1107 h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
1109 for (i = 0; i < 16; i++) {
1110 h->block_offset[16 + i] =
1111 h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
1112 h->block_offset[48 + 16 + i] =
1113 h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
1116 if (h->pict_type != AV_PICTURE_TYPE_I) {
1117 if (!s->last_pic->f.data[0]) {
1118 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1119 ret = get_buffer(avctx, s->last_pic);
1122 memset(s->last_pic->f.data[0], 0, avctx->height * s->last_pic->f.linesize[0]);
1123 memset(s->last_pic->f.data[1], 0x80, (avctx->height / 2) *
1124 s->last_pic->f.linesize[1]);
1125 memset(s->last_pic->f.data[2], 0x80, (avctx->height / 2) *
1126 s->last_pic->f.linesize[2]);
1129 if (h->pict_type == AV_PICTURE_TYPE_B && !s->next_pic->f.data[0]) {
1130 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1131 ret = get_buffer(avctx, s->next_pic);
1134 memset(s->next_pic->f.data[0], 0, avctx->height * s->next_pic->f.linesize[0]);
1135 memset(s->next_pic->f.data[1], 0x80, (avctx->height / 2) *
1136 s->next_pic->f.linesize[1]);
1137 memset(s->next_pic->f.data[2], 0x80, (avctx->height / 2) *
1138 s->next_pic->f.linesize[2]);
1142 if (avctx->debug & FF_DEBUG_PICT_INFO)
1143 av_log(h->avctx, AV_LOG_DEBUG,
1144 "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
1145 av_get_picture_type_char(h->pict_type),
1146 s->halfpel_flag, s->thirdpel_flag,
1147 s->adaptive_quant, h->qscale, h->slice_num);
1149 if (avctx->skip_frame >= AVDISCARD_NONREF && h->pict_type == AV_PICTURE_TYPE_B ||
1150 avctx->skip_frame >= AVDISCARD_NONKEY && h->pict_type != AV_PICTURE_TYPE_I ||
1151 avctx->skip_frame >= AVDISCARD_ALL)
1154 if (s->next_p_frame_damaged) {
1155 if (h->pict_type == AV_PICTURE_TYPE_B)
1158 s->next_p_frame_damaged = 0;
1161 if (h->pict_type == AV_PICTURE_TYPE_B) {
1162 h->frame_num_offset = h->slice_num - h->prev_frame_num;
1164 if (h->frame_num_offset < 0)
1165 h->frame_num_offset += 256;
1166 if (h->frame_num_offset == 0 ||
1167 h->frame_num_offset >= h->prev_frame_num_offset) {
1168 av_log(h->avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
1172 h->prev_frame_num = h->frame_num;
1173 h->frame_num = h->slice_num;
1174 h->prev_frame_num_offset = h->frame_num - h->prev_frame_num;
1176 if (h->prev_frame_num_offset < 0)
1177 h->prev_frame_num_offset += 256;
1180 for (m = 0; m < 2; m++) {
1182 for (i = 0; i < 4; i++) {
1184 for (j = -1; j < 4; j++)
1185 h->ref_cache[m][scan8[0] + 8 * i + j] = 1;
1187 h->ref_cache[m][scan8[0] + 8 * i + j] = PART_NOT_AVAILABLE;
1191 for (h->mb_y = 0; h->mb_y < h->mb_height; h->mb_y++) {
1192 for (h->mb_x = 0; h->mb_x < h->mb_width; h->mb_x++) {
1194 h->mb_xy = h->mb_x + h->mb_y * h->mb_stride;
1196 if ((get_bits_count(&h->gb) + 7) >= h->gb.size_in_bits &&
1197 ((get_bits_count(&h->gb) & 7) == 0 ||
1198 show_bits(&h->gb, -get_bits_count(&h->gb) & 7) == 0)) {
1199 skip_bits(&h->gb, s->next_slice_index - get_bits_count(&h->gb));
1200 h->gb.size_in_bits = 8 * buf_size;
1202 if (svq3_decode_slice_header(avctx))
1205 /* TODO: support s->mb_skip_run */
1208 mb_type = svq3_get_ue_golomb(&h->gb);
1210 if (h->pict_type == AV_PICTURE_TYPE_I)
1212 else if (h->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4)
1214 if (mb_type > 33 || svq3_decode_mb(s, mb_type)) {
1215 av_log(h->avctx, AV_LOG_ERROR,
1216 "error while decoding MB %d %d\n", h->mb_x, h->mb_y);
1221 ff_h264_hl_decode_mb(h);
1223 if (h->pict_type != AV_PICTURE_TYPE_B && !h->low_delay)
1224 h->cur_pic.f.mb_type[h->mb_x + h->mb_y * h->mb_stride] =
1225 (h->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
1228 ff_draw_horiz_band(avctx, &h->dsp, s->cur_pic, s->last_pic->f.data[0] ? s->last_pic : NULL,
1229 16 * h->mb_y, 16, h->picture_structure, 0, 1,
1230 h->low_delay, h->mb_height * 16, h->mb_width * 16);
1233 if (h->pict_type == AV_PICTURE_TYPE_B || h->low_delay)
1234 *(AVFrame *)data = s->cur_pic->f;
1236 *(AVFrame *)data = s->last_pic->f;
1238 /* Do not output the last pic after seeking. */
1239 if (s->last_pic->f.data[0] || h->low_delay)
1242 if (h->pict_type != AV_PICTURE_TYPE_B) {
1243 FFSWAP(Picture*, s->cur_pic, s->next_pic);
1249 static void free_picture(AVCodecContext *avctx, Picture *pic)
1252 for (i = 0; i < 2; i++) {
1253 av_freep(&pic->motion_val_base[i]);
1254 av_freep(&pic->f.ref_index[i]);
1256 av_freep(&pic->mb_type_base);
1259 avctx->release_buffer(avctx, &pic->f);
1263 static int svq3_decode_end(AVCodecContext *avctx)
1265 SVQ3Context *s = avctx->priv_data;
1266 H264Context *h = &s->h;
1268 free_picture(avctx, s->cur_pic);
1269 free_picture(avctx, s->next_pic);
1270 free_picture(avctx, s->last_pic);
1272 ff_h264_free_context(h);
1277 AVCodec ff_svq3_decoder = {
1279 .type = AVMEDIA_TYPE_VIDEO,
1280 .id = AV_CODEC_ID_SVQ3,
1281 .priv_data_size = sizeof(SVQ3Context),
1282 .init = svq3_decode_init,
1283 .close = svq3_decode_end,
1284 .decode = svq3_decode_frame,
1285 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND |
1288 .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
1289 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,