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));
570 if (svq3_mc_dir(s, 0, mode, 1, mb_type == 3) < 0)
573 for (i = 0; i < 4; i++)
574 memset(h->cur_pic.f.motion_val[1][b_xy + i * h->b_stride],
575 0, 4 * 2 * sizeof(int16_t));
578 mb_type = MB_TYPE_16x16;
579 } else if (mb_type == 8 || mb_type == 33) { /* INTRA4x4 */
580 memset(h->intra4x4_pred_mode_cache, -1, 8 * 5 * sizeof(int8_t));
584 for (i = 0; i < 4; i++)
585 h->intra4x4_pred_mode_cache[scan8[0] - 1 + i * 8] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6 - i];
586 if (h->intra4x4_pred_mode_cache[scan8[0] - 1] == -1)
587 h->left_samples_available = 0x5F5F;
590 h->intra4x4_pred_mode_cache[4 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 0];
591 h->intra4x4_pred_mode_cache[5 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 1];
592 h->intra4x4_pred_mode_cache[6 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 2];
593 h->intra4x4_pred_mode_cache[7 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 3];
595 if (h->intra4x4_pred_mode_cache[4 + 8 * 0] == -1)
596 h->top_samples_available = 0x33FF;
599 /* decode prediction codes for luma blocks */
600 for (i = 0; i < 16; i += 2) {
601 vlc = svq3_get_ue_golomb(&h->gb);
604 av_log(h->avctx, AV_LOG_ERROR, "luma prediction:%d\n", vlc);
608 left = &h->intra4x4_pred_mode_cache[scan8[i] - 1];
609 top = &h->intra4x4_pred_mode_cache[scan8[i] - 8];
611 left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
612 left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
614 if (left[1] == -1 || left[2] == -1) {
615 av_log(h->avctx, AV_LOG_ERROR, "weird prediction\n");
619 } else { /* mb_type == 33, DC_128_PRED block type */
620 for (i = 0; i < 4; i++)
621 memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_PRED, 4);
624 write_back_intra_pred_mode(h);
627 ff_h264_check_intra4x4_pred_mode(h);
629 h->top_samples_available = (h->mb_y == 0) ? 0x33FF : 0xFFFF;
630 h->left_samples_available = (h->mb_x == 0) ? 0x5F5F : 0xFFFF;
632 for (i = 0; i < 4; i++)
633 memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_128_PRED, 4);
635 h->top_samples_available = 0x33FF;
636 h->left_samples_available = 0x5F5F;
639 mb_type = MB_TYPE_INTRA4x4;
640 } else { /* INTRA16x16 */
641 dir = i_mb_type_info[mb_type - 8].pred_mode;
642 dir = (dir >> 1) ^ 3 * (dir & 1) ^ 1;
644 if ((h->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(h, dir, 0)) == -1) {
645 av_log(h->avctx, AV_LOG_ERROR, "check_intra_pred_mode = -1\n");
649 cbp = i_mb_type_info[mb_type - 8].cbp;
650 mb_type = MB_TYPE_INTRA16x16;
653 if (!IS_INTER(mb_type) && h->pict_type != AV_PICTURE_TYPE_I) {
654 for (i = 0; i < 4; i++)
655 memset(h->cur_pic.f.motion_val[0][b_xy + i * h->b_stride],
656 0, 4 * 2 * sizeof(int16_t));
657 if (h->pict_type == AV_PICTURE_TYPE_B) {
658 for (i = 0; i < 4; i++)
659 memset(h->cur_pic.f.motion_val[1][b_xy + i * h->b_stride],
660 0, 4 * 2 * sizeof(int16_t));
663 if (!IS_INTRA4x4(mb_type)) {
664 memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy], DC_PRED, 8);
666 if (!IS_SKIP(mb_type) || h->pict_type == AV_PICTURE_TYPE_B) {
667 memset(h->non_zero_count_cache + 8, 0, 14 * 8 * sizeof(uint8_t));
668 h->dsp.clear_blocks(h->mb + 0);
669 h->dsp.clear_blocks(h->mb + 384);
672 if (!IS_INTRA16x16(mb_type) &&
673 (!IS_SKIP(mb_type) || h->pict_type == AV_PICTURE_TYPE_B)) {
674 if ((vlc = svq3_get_ue_golomb(&h->gb)) >= 48) {
675 av_log(h->avctx, AV_LOG_ERROR, "cbp_vlc=%d\n", vlc);
679 cbp = IS_INTRA(mb_type) ? golomb_to_intra4x4_cbp[vlc]
680 : golomb_to_inter_cbp[vlc];
682 if (IS_INTRA16x16(mb_type) ||
683 (h->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
684 h->qscale += svq3_get_se_golomb(&h->gb);
686 if (h->qscale > 31u) {
687 av_log(h->avctx, AV_LOG_ERROR, "qscale:%d\n", h->qscale);
691 if (IS_INTRA16x16(mb_type)) {
692 AV_ZERO128(h->mb_luma_dc[0] + 0);
693 AV_ZERO128(h->mb_luma_dc[0] + 8);
694 if (svq3_decode_block(&h->gb, h->mb_luma_dc[0], 0, 1)) {
695 av_log(h->avctx, AV_LOG_ERROR,
696 "error while decoding intra luma dc\n");
702 const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
703 const int type = ((h->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
705 for (i = 0; i < 4; i++)
706 if ((cbp & (1 << i))) {
707 for (j = 0; j < 4; j++) {
708 k = index ? (1 * (j & 1) + 2 * (i & 1) +
709 2 * (j & 2) + 4 * (i & 2))
711 h->non_zero_count_cache[scan8[k]] = 1;
713 if (svq3_decode_block(&h->gb, &h->mb[16 * k], index, type)) {
714 av_log(h->avctx, AV_LOG_ERROR,
715 "error while decoding block\n");
722 for (i = 1; i < 3; ++i)
723 if (svq3_decode_block(&h->gb, &h->mb[16 * 16 * i], 0, 3)) {
724 av_log(h->avctx, AV_LOG_ERROR,
725 "error while decoding chroma dc block\n");
730 for (i = 1; i < 3; i++) {
731 for (j = 0; j < 4; j++) {
733 h->non_zero_count_cache[scan8[k]] = 1;
735 if (svq3_decode_block(&h->gb, &h->mb[16 * k], 1, 1)) {
736 av_log(h->avctx, AV_LOG_ERROR,
737 "error while decoding chroma ac block\n");
747 h->cur_pic.f.mb_type[mb_xy] = mb_type;
749 if (IS_INTRA(mb_type))
750 h->chroma_pred_mode = ff_h264_check_intra_pred_mode(h, DC_PRED8x8, 1);
755 static int svq3_decode_slice_header(AVCodecContext *avctx)
757 SVQ3Context *s = avctx->priv_data;
758 H264Context *h = &s->h;
759 const int mb_xy = h->mb_xy;
763 header = get_bits(&h->gb, 8);
765 if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
767 av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
770 int length = header >> 5 & 3;
772 s->next_slice_index = get_bits_count(&h->gb) +
773 8 * show_bits(&h->gb, 8 * length) +
776 if (s->next_slice_index > h->gb.size_in_bits) {
777 av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
781 h->gb.size_in_bits = s->next_slice_index - 8 * (length - 1);
782 skip_bits(&h->gb, 8);
784 if (s->watermark_key) {
785 uint32_t header = AV_RL32(&h->gb.buffer[(get_bits_count(&h->gb) >> 3) + 1]);
786 AV_WL32(&h->gb.buffer[(get_bits_count(&h->gb) >> 3) + 1],
787 header ^ s->watermark_key);
790 memcpy((uint8_t *) &h->gb.buffer[get_bits_count(&h->gb) >> 3],
791 &h->gb.buffer[h->gb.size_in_bits >> 3], length - 1);
793 skip_bits_long(&h->gb, 0);
796 if ((slice_id = svq3_get_ue_golomb(&h->gb)) >= 3) {
797 av_log(h->avctx, AV_LOG_ERROR, "illegal slice type %d \n", slice_id);
801 h->slice_type = golomb_to_pict_type[slice_id];
803 if ((header & 0x9F) == 2) {
804 i = (h->mb_num < 64) ? 6 : (1 + av_log2(h->mb_num - 1));
805 h->mb_skip_run = get_bits(&h->gb, i) -
806 (h->mb_y * h->mb_width + h->mb_x);
812 h->slice_num = get_bits(&h->gb, 8);
813 h->qscale = get_bits(&h->gb, 5);
814 s->adaptive_quant = get_bits1(&h->gb);
823 skip_bits(&h->gb, 2);
825 while (get_bits1(&h->gb))
826 skip_bits(&h->gb, 8);
828 /* reset intra predictors and invalidate motion vector references */
830 memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - 1] + 3,
831 -1, 4 * sizeof(int8_t));
832 memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - h->mb_x],
833 -1, 8 * sizeof(int8_t) * h->mb_x);
836 memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - h->mb_stride],
837 -1, 8 * sizeof(int8_t) * (h->mb_width - h->mb_x));
840 h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride - 1] + 3] = -1;
846 static av_cold int svq3_decode_init(AVCodecContext *avctx)
848 SVQ3Context *s = avctx->priv_data;
849 H264Context *h = &s->h;
851 unsigned char *extradata;
852 unsigned char *extradata_end;
854 int marker_found = 0;
856 s->cur_pic = av_mallocz(sizeof(*s->cur_pic));
857 s->last_pic = av_mallocz(sizeof(*s->last_pic));
858 s->next_pic = av_mallocz(sizeof(*s->next_pic));
859 if (!s->next_pic || !s->last_pic || !s->cur_pic) {
860 av_freep(&s->cur_pic);
861 av_freep(&s->last_pic);
862 av_freep(&s->next_pic);
863 return AVERROR(ENOMEM);
866 if (ff_h264_decode_init(avctx) < 0)
869 h->flags = avctx->flags;
871 h->picture_structure = PICT_FRAME;
872 avctx->pix_fmt = avctx->codec->pix_fmts[0];
874 h->chroma_qp[0] = h->chroma_qp[1] = 4;
875 h->chroma_x_shift = h->chroma_y_shift = 1;
878 s->thirdpel_flag = 1;
881 /* prowl for the "SEQH" marker in the extradata */
882 extradata = (unsigned char *)avctx->extradata;
883 extradata_end = avctx->extradata + avctx->extradata_size;
885 for (m = 0; m + 8 < avctx->extradata_size; m++) {
886 if (!memcmp(extradata, "SEQH", 4)) {
894 /* if a match was found, parse the extra data */
899 size = AV_RB32(&extradata[4]);
900 if (size > extradata_end - extradata - 8)
901 return AVERROR_INVALIDDATA;
902 init_get_bits(&gb, extradata + 8, size * 8);
904 /* 'frame size code' and optional 'width, height' */
905 frame_size_code = get_bits(&gb, 3);
906 switch (frame_size_code) {
936 avctx->width = get_bits(&gb, 12);
937 avctx->height = get_bits(&gb, 12);
941 s->halfpel_flag = get_bits1(&gb);
942 s->thirdpel_flag = get_bits1(&gb);
950 h->low_delay = get_bits1(&gb);
955 while (get_bits1(&gb))
958 s->unknown_flag = get_bits1(&gb);
959 avctx->has_b_frames = !h->low_delay;
960 if (s->unknown_flag) {
962 unsigned watermark_width = svq3_get_ue_golomb(&gb);
963 unsigned watermark_height = svq3_get_ue_golomb(&gb);
964 int u1 = svq3_get_ue_golomb(&gb);
965 int u2 = get_bits(&gb, 8);
966 int u3 = get_bits(&gb, 2);
967 int u4 = svq3_get_ue_golomb(&gb);
968 unsigned long buf_len = watermark_width *
969 watermark_height * 4;
970 int offset = get_bits_count(&gb) + 7 >> 3;
973 if ((uint64_t)watermark_width * 4 > UINT_MAX / watermark_height)
976 buf = av_malloc(buf_len);
977 av_log(avctx, AV_LOG_DEBUG, "watermark size: %dx%d\n",
978 watermark_width, watermark_height);
979 av_log(avctx, AV_LOG_DEBUG,
980 "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n",
981 u1, u2, u3, u4, offset);
982 if (uncompress(buf, &buf_len, extradata + 8 + offset,
983 size - offset) != Z_OK) {
984 av_log(avctx, AV_LOG_ERROR,
985 "could not uncompress watermark logo\n");
989 s->watermark_key = ff_svq1_packet_checksum(buf, buf_len, 0);
990 s->watermark_key = s->watermark_key << 16 | s->watermark_key;
991 av_log(avctx, AV_LOG_DEBUG,
992 "watermark key %#x\n", s->watermark_key);
995 av_log(avctx, AV_LOG_ERROR,
996 "this svq3 file contains watermark which need zlib support compiled in\n");
1002 h->width = avctx->width;
1003 h->height = avctx->height;
1004 h->mb_width = (h->width + 15) / 16;
1005 h->mb_height = (h->height + 15) / 16;
1006 h->mb_stride = h->mb_width + 1;
1007 h->mb_num = h->mb_width * h->mb_height;
1008 h->b_stride = 4 * h->mb_width;
1009 s->h_edge_pos = h->mb_width * 16;
1010 s->v_edge_pos = h->mb_height * 16;
1012 if (ff_h264_alloc_tables(h) < 0) {
1013 av_log(avctx, AV_LOG_ERROR, "svq3 memory allocation failed\n");
1014 return AVERROR(ENOMEM);
1020 static int get_buffer(AVCodecContext *avctx, Picture *pic)
1022 SVQ3Context *s = avctx->priv_data;
1023 H264Context *h = &s->h;
1024 const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
1025 const int mb_array_size = h->mb_stride * h->mb_height;
1026 const int b4_stride = h->mb_width * 4 + 1;
1027 const int b4_array_size = b4_stride * h->mb_height * 4;
1030 if (!pic->motion_val_base[0]) {
1033 pic->mb_type_base = av_mallocz((big_mb_num + h->mb_stride) * sizeof(uint32_t));
1034 if (!pic->mb_type_base)
1035 return AVERROR(ENOMEM);
1036 pic->f.mb_type = pic->mb_type_base + 2 * h->mb_stride + 1;
1038 for (i = 0; i < 2; i++) {
1039 pic->motion_val_base[i] = av_mallocz(2 * (b4_array_size + 4) * sizeof(int16_t));
1040 pic->f.ref_index[i] = av_mallocz(4 * mb_array_size);
1041 if (!pic->motion_val_base[i] || !pic->f.ref_index[i])
1042 return AVERROR(ENOMEM);
1044 pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
1047 pic->f.motion_subsample_log2 = 2;
1048 pic->f.reference = !(h->pict_type == AV_PICTURE_TYPE_B);
1050 ret = ff_get_buffer(avctx, &pic->f);
1052 h->linesize = pic->f.linesize[0];
1053 h->uvlinesize = pic->f.linesize[1];
1058 static int svq3_decode_frame(AVCodecContext *avctx, void *data,
1059 int *got_frame, AVPacket *avpkt)
1061 const uint8_t *buf = avpkt->data;
1062 SVQ3Context *s = avctx->priv_data;
1063 H264Context *h = &s->h;
1064 int buf_size = avpkt->size;
1067 /* special case for last picture */
1068 if (buf_size == 0) {
1069 if (s->next_pic->f.data[0] && !h->low_delay && !s->last_frame_output) {
1070 *(AVFrame *) data = s->next_pic->f;
1071 s->last_frame_output = 1;
1077 init_get_bits(&h->gb, buf, 8 * buf_size);
1079 h->mb_x = h->mb_y = h->mb_xy = 0;
1081 if (svq3_decode_slice_header(avctx))
1084 h->pict_type = h->slice_type;
1086 if (h->pict_type != AV_PICTURE_TYPE_B)
1087 FFSWAP(Picture*, s->next_pic, s->last_pic);
1089 if (s->cur_pic->f.data[0])
1090 avctx->release_buffer(avctx, &s->cur_pic->f);
1092 /* for skipping the frame */
1093 s->cur_pic->f.pict_type = h->pict_type;
1094 s->cur_pic->f.key_frame = (h->pict_type == AV_PICTURE_TYPE_I);
1096 ret = get_buffer(avctx, s->cur_pic);
1100 h->cur_pic_ptr = s->cur_pic;
1101 h->cur_pic = *s->cur_pic;
1103 for (i = 0; i < 16; i++) {
1104 h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
1105 h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
1107 for (i = 0; i < 16; i++) {
1108 h->block_offset[16 + i] =
1109 h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
1110 h->block_offset[48 + 16 + i] =
1111 h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
1114 if (h->pict_type != AV_PICTURE_TYPE_I) {
1115 if (!s->last_pic->f.data[0]) {
1116 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1117 ret = get_buffer(avctx, s->last_pic);
1120 memset(s->last_pic->f.data[0], 0, avctx->height * s->last_pic->f.linesize[0]);
1121 memset(s->last_pic->f.data[1], 0x80, (avctx->height / 2) *
1122 s->last_pic->f.linesize[1]);
1123 memset(s->last_pic->f.data[2], 0x80, (avctx->height / 2) *
1124 s->last_pic->f.linesize[2]);
1127 if (h->pict_type == AV_PICTURE_TYPE_B && !s->next_pic->f.data[0]) {
1128 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1129 ret = get_buffer(avctx, s->next_pic);
1132 memset(s->next_pic->f.data[0], 0, avctx->height * s->next_pic->f.linesize[0]);
1133 memset(s->next_pic->f.data[1], 0x80, (avctx->height / 2) *
1134 s->next_pic->f.linesize[1]);
1135 memset(s->next_pic->f.data[2], 0x80, (avctx->height / 2) *
1136 s->next_pic->f.linesize[2]);
1140 if (avctx->debug & FF_DEBUG_PICT_INFO)
1141 av_log(h->avctx, AV_LOG_DEBUG,
1142 "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
1143 av_get_picture_type_char(h->pict_type),
1144 s->halfpel_flag, s->thirdpel_flag,
1145 s->adaptive_quant, h->qscale, h->slice_num);
1147 if (avctx->skip_frame >= AVDISCARD_NONREF && h->pict_type == AV_PICTURE_TYPE_B ||
1148 avctx->skip_frame >= AVDISCARD_NONKEY && h->pict_type != AV_PICTURE_TYPE_I ||
1149 avctx->skip_frame >= AVDISCARD_ALL)
1152 if (s->next_p_frame_damaged) {
1153 if (h->pict_type == AV_PICTURE_TYPE_B)
1156 s->next_p_frame_damaged = 0;
1159 if (h->pict_type == AV_PICTURE_TYPE_B) {
1160 h->frame_num_offset = h->slice_num - h->prev_frame_num;
1162 if (h->frame_num_offset < 0)
1163 h->frame_num_offset += 256;
1164 if (h->frame_num_offset == 0 ||
1165 h->frame_num_offset >= h->prev_frame_num_offset) {
1166 av_log(h->avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
1170 h->prev_frame_num = h->frame_num;
1171 h->frame_num = h->slice_num;
1172 h->prev_frame_num_offset = h->frame_num - h->prev_frame_num;
1174 if (h->prev_frame_num_offset < 0)
1175 h->prev_frame_num_offset += 256;
1178 for (m = 0; m < 2; m++) {
1180 for (i = 0; i < 4; i++) {
1182 for (j = -1; j < 4; j++)
1183 h->ref_cache[m][scan8[0] + 8 * i + j] = 1;
1185 h->ref_cache[m][scan8[0] + 8 * i + j] = PART_NOT_AVAILABLE;
1189 for (h->mb_y = 0; h->mb_y < h->mb_height; h->mb_y++) {
1190 for (h->mb_x = 0; h->mb_x < h->mb_width; h->mb_x++) {
1192 h->mb_xy = h->mb_x + h->mb_y * h->mb_stride;
1194 if ((get_bits_count(&h->gb) + 7) >= h->gb.size_in_bits &&
1195 ((get_bits_count(&h->gb) & 7) == 0 ||
1196 show_bits(&h->gb, -get_bits_count(&h->gb) & 7) == 0)) {
1197 skip_bits(&h->gb, s->next_slice_index - get_bits_count(&h->gb));
1198 h->gb.size_in_bits = 8 * buf_size;
1200 if (svq3_decode_slice_header(avctx))
1203 /* TODO: support s->mb_skip_run */
1206 mb_type = svq3_get_ue_golomb(&h->gb);
1208 if (h->pict_type == AV_PICTURE_TYPE_I)
1210 else if (h->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4)
1212 if (mb_type > 33 || svq3_decode_mb(s, mb_type)) {
1213 av_log(h->avctx, AV_LOG_ERROR,
1214 "error while decoding MB %d %d\n", h->mb_x, h->mb_y);
1219 ff_h264_hl_decode_mb(h);
1221 if (h->pict_type != AV_PICTURE_TYPE_B && !h->low_delay)
1222 h->cur_pic.f.mb_type[h->mb_x + h->mb_y * h->mb_stride] =
1223 (h->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
1226 ff_draw_horiz_band(avctx, &h->dsp, s->cur_pic, s->last_pic->f.data[0] ? s->last_pic : NULL,
1227 16 * h->mb_y, 16, h->picture_structure, 0, 1,
1228 h->low_delay, h->mb_height * 16, h->mb_width * 16);
1231 if (h->pict_type == AV_PICTURE_TYPE_B || h->low_delay)
1232 *(AVFrame *)data = s->cur_pic->f;
1234 *(AVFrame *)data = s->last_pic->f;
1236 /* Do not output the last pic after seeking. */
1237 if (s->last_pic->f.data[0] || h->low_delay)
1240 if (h->pict_type != AV_PICTURE_TYPE_B) {
1241 FFSWAP(Picture*, s->cur_pic, s->next_pic);
1247 static void free_picture(AVCodecContext *avctx, Picture *pic)
1250 for (i = 0; i < 2; i++) {
1251 av_freep(&pic->motion_val_base[i]);
1252 av_freep(&pic->f.ref_index[i]);
1254 av_freep(&pic->mb_type_base);
1257 avctx->release_buffer(avctx, &pic->f);
1261 static int svq3_decode_end(AVCodecContext *avctx)
1263 SVQ3Context *s = avctx->priv_data;
1264 H264Context *h = &s->h;
1266 free_picture(avctx, s->cur_pic);
1267 free_picture(avctx, s->next_pic);
1268 free_picture(avctx, s->last_pic);
1270 ff_h264_free_context(h);
1275 AVCodec ff_svq3_decoder = {
1277 .type = AVMEDIA_TYPE_VIDEO,
1278 .id = AV_CODEC_ID_SVQ3,
1279 .priv_data_size = sizeof(SVQ3Context),
1280 .init = svq3_decode_init,
1281 .close = svq3_decode_end,
1282 .decode = svq3_decode_frame,
1283 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND |
1286 .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
1287 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,