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 "libavutil/attributes.h"
48 #include "mpegutils.h"
54 #include "rectangle.h"
68 typedef struct SVQ3Frame {
71 AVBufferRef *motion_val_buf[2];
72 int16_t (*motion_val[2])[2];
74 AVBufferRef *mb_type_buf;
78 AVBufferRef *ref_index_buf[2];
82 typedef struct SVQ3Context {
83 AVCodecContext *avctx;
85 H264DSPContext h264dsp;
95 GetBitContext gb_slice;
101 uint32_t watermark_key;
103 int next_p_frame_damaged;
106 int last_frame_output;
111 int frame_num_offset;
112 int prev_frame_num_offset;
115 enum AVPictureType pict_type;
120 int mb_width, mb_height;
121 int mb_stride, mb_num;
126 int chroma_pred_mode;
127 int intra16x16_pred_mode;
129 int8_t intra4x4_pred_mode_cache[5 * 8];
130 int8_t (*intra4x4_pred_mode);
132 unsigned int top_samples_available;
133 unsigned int topright_samples_available;
134 unsigned int left_samples_available;
136 uint8_t *edge_emu_buffer;
138 DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5 * 8][2];
139 DECLARE_ALIGNED(8, int8_t, ref_cache)[2][5 * 8];
140 DECLARE_ALIGNED(16, int16_t, mb)[16 * 48 * 2];
141 DECLARE_ALIGNED(16, int16_t, mb_luma_dc)[3][16 * 2];
142 DECLARE_ALIGNED(8, uint8_t, non_zero_count_cache)[15 * 8];
143 uint32_t dequant4_coeff[QP_MAX_NUM + 1][16];
144 int block_offset[2 * (16 * 3)];
147 #define FULLPEL_MODE 1
148 #define HALFPEL_MODE 2
149 #define THIRDPEL_MODE 3
150 #define PREDICT_MODE 4
152 /* dual scan (from some older H.264 draft)
161 static const uint8_t svq3_scan[16] = {
162 0 + 0 * 4, 1 + 0 * 4, 2 + 0 * 4, 2 + 1 * 4,
163 2 + 2 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4,
164 0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 1 + 2 * 4,
165 0 + 3 * 4, 1 + 3 * 4, 2 + 3 * 4, 3 + 3 * 4,
168 static const uint8_t luma_dc_zigzag_scan[16] = {
169 0 * 16 + 0 * 64, 1 * 16 + 0 * 64, 2 * 16 + 0 * 64, 0 * 16 + 2 * 64,
170 3 * 16 + 0 * 64, 0 * 16 + 1 * 64, 1 * 16 + 1 * 64, 2 * 16 + 1 * 64,
171 1 * 16 + 2 * 64, 2 * 16 + 2 * 64, 3 * 16 + 2 * 64, 0 * 16 + 3 * 64,
172 3 * 16 + 1 * 64, 1 * 16 + 3 * 64, 2 * 16 + 3 * 64, 3 * 16 + 3 * 64,
175 static const uint8_t svq3_pred_0[25][2] = {
178 { 0, 2 }, { 1, 1 }, { 2, 0 },
179 { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
180 { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
181 { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
182 { 2, 4 }, { 3, 3 }, { 4, 2 },
187 static const int8_t svq3_pred_1[6][6][5] = {
188 { { 2, -1, -1, -1, -1 }, { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 },
189 { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, { 1, 2, -1, -1, -1 } },
190 { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 },
191 { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } },
192 { { 2, 0, -1, -1, -1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 },
193 { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } },
194 { { 2, 0, -1, -1, -1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 },
195 { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } },
196 { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 },
197 { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } },
198 { { 0, 2, -1, -1, -1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 },
199 { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } },
202 static const struct {
205 } svq3_dct_tables[2][16] = {
206 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
207 { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
208 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
209 { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
212 static const uint32_t svq3_dequant_coeff[32] = {
213 3881, 4351, 4890, 5481, 6154, 6914, 7761, 8718,
214 9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873,
215 24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683,
216 61694, 68745, 77615, 89113, 100253, 109366, 126635, 141533
219 static void svq3_luma_dc_dequant_idct_c(int16_t *output, int16_t *input, int qp)
221 const int qmul = svq3_dequant_coeff[qp];
225 static const uint8_t x_offset[4] = { 0, 1 * stride, 4 * stride, 5 * stride };
227 for (i = 0; i < 4; i++) {
228 const int z0 = 13 * (input[4 * i + 0] + input[4 * i + 2]);
229 const int z1 = 13 * (input[4 * i + 0] - input[4 * i + 2]);
230 const int z2 = 7 * input[4 * i + 1] - 17 * input[4 * i + 3];
231 const int z3 = 17 * input[4 * i + 1] + 7 * input[4 * i + 3];
233 temp[4 * i + 0] = z0 + z3;
234 temp[4 * i + 1] = z1 + z2;
235 temp[4 * i + 2] = z1 - z2;
236 temp[4 * i + 3] = z0 - z3;
239 for (i = 0; i < 4; i++) {
240 const int offset = x_offset[i];
241 const int z0 = 13 * (temp[4 * 0 + i] + temp[4 * 2 + i]);
242 const int z1 = 13 * (temp[4 * 0 + i] - temp[4 * 2 + i]);
243 const int z2 = 7 * temp[4 * 1 + i] - 17 * temp[4 * 3 + i];
244 const int z3 = 17 * temp[4 * 1 + i] + 7 * temp[4 * 3 + i];
246 output[stride * 0 + offset] = (z0 + z3) * qmul + 0x80000 >> 20;
247 output[stride * 2 + offset] = (z1 + z2) * qmul + 0x80000 >> 20;
248 output[stride * 8 + offset] = (z1 - z2) * qmul + 0x80000 >> 20;
249 output[stride * 10 + offset] = (z0 - z3) * qmul + 0x80000 >> 20;
254 static void svq3_add_idct_c(uint8_t *dst, int16_t *block,
255 int stride, int qp, int dc)
257 const int qmul = svq3_dequant_coeff[qp];
261 dc = 13 * 13 * (dc == 1 ? 1538 * block[0]
262 : qmul * (block[0] >> 3) / 2);
266 for (i = 0; i < 4; i++) {
267 const int z0 = 13 * (block[0 + 4 * i] + block[2 + 4 * i]);
268 const int z1 = 13 * (block[0 + 4 * i] - block[2 + 4 * i]);
269 const int z2 = 7 * block[1 + 4 * i] - 17 * block[3 + 4 * i];
270 const int z3 = 17 * block[1 + 4 * i] + 7 * block[3 + 4 * i];
272 block[0 + 4 * i] = z0 + z3;
273 block[1 + 4 * i] = z1 + z2;
274 block[2 + 4 * i] = z1 - z2;
275 block[3 + 4 * i] = z0 - z3;
278 for (i = 0; i < 4; i++) {
279 const int z0 = 13 * (block[i + 4 * 0] + block[i + 4 * 2]);
280 const int z1 = 13 * (block[i + 4 * 0] - block[i + 4 * 2]);
281 const int z2 = 7 * block[i + 4 * 1] - 17 * block[i + 4 * 3];
282 const int z3 = 17 * block[i + 4 * 1] + 7 * block[i + 4 * 3];
283 const int rr = (dc + 0x80000);
285 dst[i + stride * 0] = av_clip_uint8(dst[i + stride * 0] + ((z0 + z3) * qmul + rr >> 20));
286 dst[i + stride * 1] = av_clip_uint8(dst[i + stride * 1] + ((z1 + z2) * qmul + rr >> 20));
287 dst[i + stride * 2] = av_clip_uint8(dst[i + stride * 2] + ((z1 - z2) * qmul + rr >> 20));
288 dst[i + stride * 3] = av_clip_uint8(dst[i + stride * 3] + ((z0 - z3) * qmul + rr >> 20));
291 memset(block, 0, 16 * sizeof(int16_t));
294 static inline int svq3_decode_block(GetBitContext *gb, int16_t *block,
295 int index, const int type)
297 static const uint8_t *const scan_patterns[4] = {
298 luma_dc_zigzag_scan, ff_zigzag_scan, svq3_scan, ff_h264_chroma_dc_scan
301 int run, level, limit;
303 const int intra = 3 * type >> 2;
304 const uint8_t *const scan = scan_patterns[type];
306 for (limit = (16 >> intra); index < 16; index = limit, limit += 8) {
307 for (; (vlc = get_interleaved_ue_golomb(gb)) != 0; index++) {
308 int sign = (vlc & 1) ? 0 : -1;
315 } else if (vlc < 4) {
320 level = (vlc + 9 >> 2) - run;
324 run = svq3_dct_tables[intra][vlc].run;
325 level = svq3_dct_tables[intra][vlc].level;
329 ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
333 ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
337 if ((index += run) >= limit)
340 block[scan[index]] = (level ^ sign) - sign;
351 static av_always_inline int
352 svq3_fetch_diagonal_mv(const SVQ3Context *s, const int16_t **C,
353 int i, int list, int part_width)
355 const int topright_ref = s->ref_cache[list][i - 8 + part_width];
357 if (topright_ref != PART_NOT_AVAILABLE) {
358 *C = s->mv_cache[list][i - 8 + part_width];
361 *C = s->mv_cache[list][i - 8 - 1];
362 return s->ref_cache[list][i - 8 - 1];
367 * Get the predicted MV.
368 * @param n the block index
369 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
370 * @param mx the x component of the predicted motion vector
371 * @param my the y component of the predicted motion vector
373 static av_always_inline void svq3_pred_motion(const SVQ3Context *s, int n,
374 int part_width, int list,
375 int ref, int *const mx, int *const my)
377 const int index8 = scan8[n];
378 const int top_ref = s->ref_cache[list][index8 - 8];
379 const int left_ref = s->ref_cache[list][index8 - 1];
380 const int16_t *const A = s->mv_cache[list][index8 - 1];
381 const int16_t *const B = s->mv_cache[list][index8 - 8];
383 int diagonal_ref, match_count;
393 diagonal_ref = svq3_fetch_diagonal_mv(s, &C, index8, list, part_width);
394 match_count = (diagonal_ref == ref) + (top_ref == ref) + (left_ref == ref);
395 if (match_count > 1) { //most common
396 *mx = mid_pred(A[0], B[0], C[0]);
397 *my = mid_pred(A[1], B[1], C[1]);
398 } else if (match_count == 1) {
399 if (left_ref == ref) {
402 } else if (top_ref == ref) {
410 if (top_ref == PART_NOT_AVAILABLE &&
411 diagonal_ref == PART_NOT_AVAILABLE &&
412 left_ref != PART_NOT_AVAILABLE) {
416 *mx = mid_pred(A[0], B[0], C[0]);
417 *my = mid_pred(A[1], B[1], C[1]);
422 static inline void svq3_mc_dir_part(SVQ3Context *s,
423 int x, int y, int width, int height,
424 int mx, int my, int dxy,
425 int thirdpel, int dir, int avg)
427 const SVQ3Frame *pic = (dir == 0) ? s->last_pic : s->next_pic;
430 int blocksize = 2 - (width >> 3); // 16->0, 8->1, 4->2
431 int linesize = s->cur_pic->f->linesize[0];
432 int uvlinesize = s->cur_pic->f->linesize[1];
437 if (mx < 0 || mx >= s->h_edge_pos - width - 1 ||
438 my < 0 || my >= s->v_edge_pos - height - 1) {
440 mx = av_clip(mx, -16, s->h_edge_pos - width + 15);
441 my = av_clip(my, -16, s->v_edge_pos - height + 15);
444 /* form component predictions */
445 dest = s->cur_pic->f->data[0] + x + y * linesize;
446 src = pic->f->data[0] + mx + my * linesize;
449 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
451 width + 1, height + 1,
452 mx, my, s->h_edge_pos, s->v_edge_pos);
453 src = s->edge_emu_buffer;
456 (avg ? s->tdsp.avg_tpel_pixels_tab
457 : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src, linesize,
460 (avg ? s->hdsp.avg_pixels_tab
461 : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src, linesize,
464 if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
465 mx = mx + (mx < (int) x) >> 1;
466 my = my + (my < (int) y) >> 1;
468 height = height >> 1;
471 for (i = 1; i < 3; i++) {
472 dest = s->cur_pic->f->data[i] + (x >> 1) + (y >> 1) * uvlinesize;
473 src = pic->f->data[i] + mx + my * uvlinesize;
476 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
477 uvlinesize, uvlinesize,
478 width + 1, height + 1,
479 mx, my, (s->h_edge_pos >> 1),
481 src = s->edge_emu_buffer;
484 (avg ? s->tdsp.avg_tpel_pixels_tab
485 : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src,
489 (avg ? s->hdsp.avg_pixels_tab
490 : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src,
497 static inline int svq3_mc_dir(SVQ3Context *s, int size, int mode,
500 int i, j, k, mx, my, dx, dy, x, y;
501 const int part_width = ((size & 5) == 4) ? 4 : 16 >> (size & 1);
502 const int part_height = 16 >> ((unsigned)(size + 1) / 3);
503 const int extra_width = (mode == PREDICT_MODE) ? -16 * 6 : 0;
504 const int h_edge_pos = 6 * (s->h_edge_pos - part_width) - extra_width;
505 const int v_edge_pos = 6 * (s->v_edge_pos - part_height) - extra_width;
507 for (i = 0; i < 16; i += part_height)
508 for (j = 0; j < 16; j += part_width) {
509 const int b_xy = (4 * s->mb_x + (j >> 2)) +
510 (4 * s->mb_y + (i >> 2)) * s->b_stride;
512 x = 16 * s->mb_x + j;
513 y = 16 * s->mb_y + i;
514 k = (j >> 2 & 1) + (i >> 1 & 2) +
515 (j >> 1 & 4) + (i & 8);
517 if (mode != PREDICT_MODE) {
518 svq3_pred_motion(s, k, part_width >> 2, dir, 1, &mx, &my);
520 mx = s->next_pic->motion_val[0][b_xy][0] << 1;
521 my = s->next_pic->motion_val[0][b_xy][1] << 1;
524 mx = mx * s->frame_num_offset /
525 s->prev_frame_num_offset + 1 >> 1;
526 my = my * s->frame_num_offset /
527 s->prev_frame_num_offset + 1 >> 1;
529 mx = mx * (s->frame_num_offset - s->prev_frame_num_offset) /
530 s->prev_frame_num_offset + 1 >> 1;
531 my = my * (s->frame_num_offset - s->prev_frame_num_offset) /
532 s->prev_frame_num_offset + 1 >> 1;
536 /* clip motion vector prediction to frame border */
537 mx = av_clip(mx, extra_width - 6 * x, h_edge_pos - 6 * x);
538 my = av_clip(my, extra_width - 6 * y, v_edge_pos - 6 * y);
540 /* get (optional) motion vector differential */
541 if (mode == PREDICT_MODE) {
544 dy = get_interleaved_se_golomb(&s->gb_slice);
545 dx = get_interleaved_se_golomb(&s->gb_slice);
547 if (dx == INVALID_VLC || dy == INVALID_VLC) {
548 av_log(s->avctx, AV_LOG_ERROR, "invalid MV vlc\n");
553 /* compute motion vector */
554 if (mode == THIRDPEL_MODE) {
556 mx = (mx + 1 >> 1) + dx;
557 my = (my + 1 >> 1) + dy;
558 fx = (unsigned)(mx + 0x3000) / 3 - 0x1000;
559 fy = (unsigned)(my + 0x3000) / 3 - 0x1000;
560 dxy = (mx - 3 * fx) + 4 * (my - 3 * fy);
562 svq3_mc_dir_part(s, x, y, part_width, part_height,
563 fx, fy, dxy, 1, dir, avg);
566 } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) {
567 mx = (unsigned)(mx + 1 + 0x3000) / 3 + dx - 0x1000;
568 my = (unsigned)(my + 1 + 0x3000) / 3 + dy - 0x1000;
569 dxy = (mx & 1) + 2 * (my & 1);
571 svq3_mc_dir_part(s, x, y, part_width, part_height,
572 mx >> 1, my >> 1, dxy, 0, dir, avg);
576 mx = (unsigned)(mx + 3 + 0x6000) / 6 + dx - 0x1000;
577 my = (unsigned)(my + 3 + 0x6000) / 6 + dy - 0x1000;
579 svq3_mc_dir_part(s, x, y, part_width, part_height,
580 mx, my, 0, 0, dir, avg);
585 /* update mv_cache */
586 if (mode != PREDICT_MODE) {
587 int32_t mv = pack16to32(mx, my);
589 if (part_height == 8 && i < 8) {
590 AV_WN32A(s->mv_cache[dir][scan8[k] + 1 * 8], mv);
592 if (part_width == 8 && j < 8)
593 AV_WN32A(s->mv_cache[dir][scan8[k] + 1 + 1 * 8], mv);
595 if (part_width == 8 && j < 8)
596 AV_WN32A(s->mv_cache[dir][scan8[k] + 1], mv);
597 if (part_width == 4 || part_height == 4)
598 AV_WN32A(s->mv_cache[dir][scan8[k]], mv);
601 /* write back motion vectors */
602 fill_rectangle(s->cur_pic->motion_val[dir][b_xy],
603 part_width >> 2, part_height >> 2, s->b_stride,
604 pack16to32(mx, my), 4);
610 static av_always_inline void hl_decode_mb_idct_luma(SVQ3Context *s,
611 int mb_type, const int *block_offset,
612 int linesize, uint8_t *dest_y)
615 if (!IS_INTRA4x4(mb_type)) {
616 for (i = 0; i < 16; i++)
617 if (s->non_zero_count_cache[scan8[i]] || s->mb[i * 16]) {
618 uint8_t *const ptr = dest_y + block_offset[i];
619 svq3_add_idct_c(ptr, s->mb + i * 16, linesize,
620 s->qscale, IS_INTRA(mb_type) ? 1 : 0);
625 static av_always_inline int dctcoef_get(int16_t *mb, int index)
627 return AV_RN16A(mb + index);
630 static av_always_inline void hl_decode_mb_predict_luma(SVQ3Context *s,
632 const int *block_offset,
637 int qscale = s->qscale;
639 if (IS_INTRA4x4(mb_type)) {
640 for (i = 0; i < 16; i++) {
641 uint8_t *const ptr = dest_y + block_offset[i];
642 const int dir = s->intra4x4_pred_mode_cache[scan8[i]];
646 if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
647 const int topright_avail = (s->topright_samples_available << i) & 0x8000;
648 assert(s->mb_y || linesize <= block_offset[i]);
649 if (!topright_avail) {
650 tr = ptr[3 - linesize] * 0x01010101u;
651 topright = (uint8_t *)&tr;
653 topright = ptr + 4 - linesize;
657 s->hpc.pred4x4[dir](ptr, topright, linesize);
658 nnz = s->non_zero_count_cache[scan8[i]];
660 svq3_add_idct_c(ptr, s->mb + i * 16, linesize, qscale, 0);
664 s->hpc.pred16x16[s->intra16x16_pred_mode](dest_y, linesize);
665 svq3_luma_dc_dequant_idct_c(s->mb, s->mb_luma_dc[0], qscale);
669 static void hl_decode_mb(SVQ3Context *s)
671 const int mb_x = s->mb_x;
672 const int mb_y = s->mb_y;
673 const int mb_xy = s->mb_xy;
674 const int mb_type = s->cur_pic->mb_type[mb_xy];
675 uint8_t *dest_y, *dest_cb, *dest_cr;
676 int linesize, uvlinesize;
678 const int *block_offset = &s->block_offset[0];
679 const int block_h = 16 >> 1;
681 linesize = s->cur_pic->f->linesize[0];
682 uvlinesize = s->cur_pic->f->linesize[1];
684 dest_y = s->cur_pic->f->data[0] + (mb_x + mb_y * linesize) * 16;
685 dest_cb = s->cur_pic->f->data[1] + mb_x * 8 + mb_y * uvlinesize * block_h;
686 dest_cr = s->cur_pic->f->data[2] + mb_x * 8 + mb_y * uvlinesize * block_h;
688 s->vdsp.prefetch(dest_y + (s->mb_x & 3) * 4 * linesize + 64, linesize, 4);
689 s->vdsp.prefetch(dest_cb + (s->mb_x & 7) * uvlinesize + 64, dest_cr - dest_cb, 2);
691 if (IS_INTRA(mb_type)) {
692 s->hpc.pred8x8[s->chroma_pred_mode](dest_cb, uvlinesize);
693 s->hpc.pred8x8[s->chroma_pred_mode](dest_cr, uvlinesize);
695 hl_decode_mb_predict_luma(s, mb_type, block_offset, linesize, dest_y);
698 hl_decode_mb_idct_luma(s, mb_type, block_offset, linesize, dest_y);
701 uint8_t *dest[2] = { dest_cb, dest_cr };
702 s->h264dsp.h264_chroma_dc_dequant_idct(s->mb + 16 * 16 * 1,
703 s->dequant4_coeff[4][0]);
704 s->h264dsp.h264_chroma_dc_dequant_idct(s->mb + 16 * 16 * 2,
705 s->dequant4_coeff[4][0]);
706 for (j = 1; j < 3; j++) {
707 for (i = j * 16; i < j * 16 + 4; i++)
708 if (s->non_zero_count_cache[scan8[i]] || s->mb[i * 16]) {
709 uint8_t *const ptr = dest[j - 1] + block_offset[i];
710 svq3_add_idct_c(ptr, s->mb + i * 16,
711 uvlinesize, ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
717 static int svq3_decode_mb(SVQ3Context *s, unsigned int mb_type)
719 int i, j, k, m, dir, mode;
723 const int mb_xy = s->mb_xy;
724 const int b_xy = 4 * s->mb_x + 4 * s->mb_y * s->b_stride;
726 s->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
727 s->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
728 s->topright_samples_available = 0xFFFF;
730 if (mb_type == 0) { /* SKIP */
731 if (s->pict_type == AV_PICTURE_TYPE_P ||
732 s->next_pic->mb_type[mb_xy] == -1) {
733 svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
736 if (s->pict_type == AV_PICTURE_TYPE_B)
737 svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
740 mb_type = MB_TYPE_SKIP;
742 mb_type = FFMIN(s->next_pic->mb_type[mb_xy], 6);
743 if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 0, 0) < 0)
745 if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 1, 1) < 0)
748 mb_type = MB_TYPE_16x16;
750 } else if (mb_type < 8) { /* INTER */
751 if (s->thirdpel_flag && s->halfpel_flag == !get_bits1(&s->gb_slice))
752 mode = THIRDPEL_MODE;
753 else if (s->halfpel_flag &&
754 s->thirdpel_flag == !get_bits1(&s->gb_slice))
760 /* note ref_cache should contain here:
768 for (m = 0; m < 2; m++) {
769 if (s->mb_x > 0 && s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - 1] + 6] != -1) {
770 for (i = 0; i < 4; i++)
771 AV_COPY32(s->mv_cache[m][scan8[0] - 1 + i * 8],
772 s->cur_pic->motion_val[m][b_xy - 1 + i * s->b_stride]);
774 for (i = 0; i < 4; i++)
775 AV_ZERO32(s->mv_cache[m][scan8[0] - 1 + i * 8]);
778 memcpy(s->mv_cache[m][scan8[0] - 1 * 8],
779 s->cur_pic->motion_val[m][b_xy - s->b_stride],
780 4 * 2 * sizeof(int16_t));
781 memset(&s->ref_cache[m][scan8[0] - 1 * 8],
782 (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);
784 if (s->mb_x < s->mb_width - 1) {
785 AV_COPY32(s->mv_cache[m][scan8[0] + 4 - 1 * 8],
786 s->cur_pic->motion_val[m][b_xy - s->b_stride + 4]);
787 s->ref_cache[m][scan8[0] + 4 - 1 * 8] =
788 (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride + 1] + 6] == -1 ||
789 s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1;
791 s->ref_cache[m][scan8[0] + 4 - 1 * 8] = PART_NOT_AVAILABLE;
793 AV_COPY32(s->mv_cache[m][scan8[0] - 1 - 1 * 8],
794 s->cur_pic->motion_val[m][b_xy - s->b_stride - 1]);
795 s->ref_cache[m][scan8[0] - 1 - 1 * 8] =
796 (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] == -1) ? PART_NOT_AVAILABLE : 1;
798 s->ref_cache[m][scan8[0] - 1 - 1 * 8] = PART_NOT_AVAILABLE;
800 memset(&s->ref_cache[m][scan8[0] - 1 * 8 - 1],
801 PART_NOT_AVAILABLE, 8);
803 if (s->pict_type != AV_PICTURE_TYPE_B)
807 /* decode motion vector(s) and form prediction(s) */
808 if (s->pict_type == AV_PICTURE_TYPE_P) {
809 if (svq3_mc_dir(s, mb_type - 1, mode, 0, 0) < 0)
811 } else { /* AV_PICTURE_TYPE_B */
813 if (svq3_mc_dir(s, 0, mode, 0, 0) < 0)
816 for (i = 0; i < 4; i++)
817 memset(s->cur_pic->motion_val[0][b_xy + i * s->b_stride],
818 0, 4 * 2 * sizeof(int16_t));
821 if (svq3_mc_dir(s, 0, mode, 1, mb_type == 3) < 0)
824 for (i = 0; i < 4; i++)
825 memset(s->cur_pic->motion_val[1][b_xy + i * s->b_stride],
826 0, 4 * 2 * sizeof(int16_t));
830 mb_type = MB_TYPE_16x16;
831 } else if (mb_type == 8 || mb_type == 33) { /* INTRA4x4 */
832 int8_t *i4x4 = s->intra4x4_pred_mode + s->mb2br_xy[s->mb_xy];
833 int8_t *i4x4_cache = s->intra4x4_pred_mode_cache;
835 memset(s->intra4x4_pred_mode_cache, -1, 8 * 5 * sizeof(int8_t));
839 for (i = 0; i < 4; i++)
840 s->intra4x4_pred_mode_cache[scan8[0] - 1 + i * 8] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - 1] + 6 - i];
841 if (s->intra4x4_pred_mode_cache[scan8[0] - 1] == -1)
842 s->left_samples_available = 0x5F5F;
845 s->intra4x4_pred_mode_cache[4 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 0];
846 s->intra4x4_pred_mode_cache[5 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 1];
847 s->intra4x4_pred_mode_cache[6 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 2];
848 s->intra4x4_pred_mode_cache[7 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 3];
850 if (s->intra4x4_pred_mode_cache[4 + 8 * 0] == -1)
851 s->top_samples_available = 0x33FF;
854 /* decode prediction codes for luma blocks */
855 for (i = 0; i < 16; i += 2) {
856 vlc = get_interleaved_ue_golomb(&s->gb_slice);
859 av_log(s->avctx, AV_LOG_ERROR,
860 "luma prediction:%"PRIu32"\n", vlc);
864 left = &s->intra4x4_pred_mode_cache[scan8[i] - 1];
865 top = &s->intra4x4_pred_mode_cache[scan8[i] - 8];
867 left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
868 left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
870 if (left[1] == -1 || left[2] == -1) {
871 av_log(s->avctx, AV_LOG_ERROR, "weird prediction\n");
875 } else { /* mb_type == 33, DC_128_PRED block type */
876 for (i = 0; i < 4; i++)
877 memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_PRED, 4);
880 AV_COPY32(i4x4, i4x4_cache + 4 + 8 * 4);
881 i4x4[4] = i4x4_cache[7 + 8 * 3];
882 i4x4[5] = i4x4_cache[7 + 8 * 2];
883 i4x4[6] = i4x4_cache[7 + 8 * 1];
886 ff_h264_check_intra4x4_pred_mode(s->intra4x4_pred_mode_cache,
887 s->avctx, s->top_samples_available,
888 s->left_samples_available);
890 s->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
891 s->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
893 for (i = 0; i < 4; i++)
894 memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_128_PRED, 4);
896 s->top_samples_available = 0x33FF;
897 s->left_samples_available = 0x5F5F;
900 mb_type = MB_TYPE_INTRA4x4;
901 } else { /* INTRA16x16 */
902 dir = ff_h264_i_mb_type_info[mb_type - 8].pred_mode;
903 dir = (dir >> 1) ^ 3 * (dir & 1) ^ 1;
905 if ((s->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(s->avctx, s->top_samples_available,
906 s->left_samples_available, dir, 0)) < 0) {
907 av_log(s->avctx, AV_LOG_ERROR, "ff_h264_check_intra_pred_mode < 0\n");
908 return s->intra16x16_pred_mode;
911 cbp = ff_h264_i_mb_type_info[mb_type - 8].cbp;
912 mb_type = MB_TYPE_INTRA16x16;
915 if (!IS_INTER(mb_type) && s->pict_type != AV_PICTURE_TYPE_I) {
916 for (i = 0; i < 4; i++)
917 memset(s->cur_pic->motion_val[0][b_xy + i * s->b_stride],
918 0, 4 * 2 * sizeof(int16_t));
919 if (s->pict_type == AV_PICTURE_TYPE_B) {
920 for (i = 0; i < 4; i++)
921 memset(s->cur_pic->motion_val[1][b_xy + i * s->b_stride],
922 0, 4 * 2 * sizeof(int16_t));
925 if (!IS_INTRA4x4(mb_type)) {
926 memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy], DC_PRED, 8);
928 if (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B) {
929 memset(s->non_zero_count_cache + 8, 0, 14 * 8 * sizeof(uint8_t));
932 if (!IS_INTRA16x16(mb_type) &&
933 (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B)) {
934 if ((vlc = get_interleaved_ue_golomb(&s->gb_slice)) >= 48) {
935 av_log(s->avctx, AV_LOG_ERROR, "cbp_vlc=%"PRIu32"\n", vlc);
939 cbp = IS_INTRA(mb_type) ? ff_h264_golomb_to_intra4x4_cbp[vlc]
940 : ff_h264_golomb_to_inter_cbp[vlc];
942 if (IS_INTRA16x16(mb_type) ||
943 (s->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
944 s->qscale += get_interleaved_se_golomb(&s->gb_slice);
946 if (s->qscale > 31u) {
947 av_log(s->avctx, AV_LOG_ERROR, "qscale:%d\n", s->qscale);
951 if (IS_INTRA16x16(mb_type)) {
952 AV_ZERO128(s->mb_luma_dc[0] + 0);
953 AV_ZERO128(s->mb_luma_dc[0] + 8);
954 if (svq3_decode_block(&s->gb_slice, s->mb_luma_dc[0], 0, 1)) {
955 av_log(s->avctx, AV_LOG_ERROR,
956 "error while decoding intra luma dc\n");
962 const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
963 const int type = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
965 for (i = 0; i < 4; i++)
966 if ((cbp & (1 << i))) {
967 for (j = 0; j < 4; j++) {
968 k = index ? (1 * (j & 1) + 2 * (i & 1) +
969 2 * (j & 2) + 4 * (i & 2))
971 s->non_zero_count_cache[scan8[k]] = 1;
973 if (svq3_decode_block(&s->gb_slice, &s->mb[16 * k], index, type)) {
974 av_log(s->avctx, AV_LOG_ERROR,
975 "error while decoding block\n");
982 for (i = 1; i < 3; ++i)
983 if (svq3_decode_block(&s->gb_slice, &s->mb[16 * 16 * i], 0, 3)) {
984 av_log(s->avctx, AV_LOG_ERROR,
985 "error while decoding chroma dc block\n");
990 for (i = 1; i < 3; i++) {
991 for (j = 0; j < 4; j++) {
993 s->non_zero_count_cache[scan8[k]] = 1;
995 if (svq3_decode_block(&s->gb_slice, &s->mb[16 * k], 1, 1)) {
996 av_log(s->avctx, AV_LOG_ERROR,
997 "error while decoding chroma ac block\n");
1007 s->cur_pic->mb_type[mb_xy] = mb_type;
1009 if (IS_INTRA(mb_type))
1010 s->chroma_pred_mode = ff_h264_check_intra_pred_mode(s->avctx, s->top_samples_available,
1011 s->left_samples_available, DC_PRED8x8, 1);
1016 static int svq3_decode_slice_header(AVCodecContext *avctx)
1018 SVQ3Context *s = avctx->priv_data;
1019 const int mb_xy = s->mb_xy;
1023 header = get_bits(&s->gb, 8);
1025 if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
1027 av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
1030 int slice_bits, slice_bytes, slice_length;
1031 int length = header >> 5 & 3;
1033 slice_length = show_bits(&s->gb, 8 * length);
1034 slice_bits = slice_length * 8;
1035 slice_bytes = slice_length + length - 1;
1037 if (slice_bytes > get_bits_left(&s->gb)) {
1038 av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
1042 skip_bits(&s->gb, 8);
1044 av_fast_malloc(&s->slice_buf, &s->slice_size, slice_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
1046 return AVERROR(ENOMEM);
1048 memcpy(s->slice_buf, s->gb.buffer + s->gb.index / 8, slice_bytes);
1050 init_get_bits(&s->gb_slice, s->slice_buf, slice_bits);
1052 if (s->watermark_key) {
1053 uint32_t header = AV_RL32(&s->gb_slice.buffer[1]);
1054 AV_WL32(&s->gb_slice.buffer[1], header ^ s->watermark_key);
1057 memcpy(s->slice_buf, &s->slice_buf[slice_length], length - 1);
1059 skip_bits_long(&s->gb, slice_bytes * 8);
1062 if ((slice_id = get_interleaved_ue_golomb(&s->gb_slice)) >= 3) {
1063 av_log(s->avctx, AV_LOG_ERROR, "illegal slice type %u \n", slice_id);
1067 s->pict_type = ff_h264_golomb_to_pict_type[slice_id];
1069 if ((header & 0x9F) == 2) {
1070 i = (s->mb_num < 64) ? 6 : (1 + av_log2(s->mb_num - 1));
1071 get_bits(&s->gb_slice, i);
1073 skip_bits1(&s->gb_slice);
1076 s->slice_num = get_bits(&s->gb_slice, 8);
1077 s->qscale = get_bits(&s->gb_slice, 5);
1078 s->adaptive_quant = get_bits1(&s->gb_slice);
1080 /* unknown fields */
1081 skip_bits1(&s->gb_slice);
1083 if (s->unknown_flag)
1084 skip_bits1(&s->gb_slice);
1086 skip_bits1(&s->gb_slice);
1087 skip_bits(&s->gb_slice, 2);
1089 while (get_bits1(&s->gb_slice))
1090 skip_bits(&s->gb_slice, 8);
1092 /* reset intra predictors and invalidate motion vector references */
1094 memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - 1] + 3,
1095 -1, 4 * sizeof(int8_t));
1096 memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_x],
1097 -1, 8 * sizeof(int8_t) * s->mb_x);
1100 memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_stride],
1101 -1, 8 * sizeof(int8_t) * (s->mb_width - s->mb_x));
1104 s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] = -1;
1110 static void init_dequant4_coeff_table(SVQ3Context *s)
1113 const int max_qp = 51;
1115 for (q = 0; q < max_qp + 1; q++) {
1116 int shift = ff_h264_quant_div6[q] + 2;
1117 int idx = ff_h264_quant_rem6[q];
1118 for (x = 0; x < 16; x++)
1119 s->dequant4_coeff[q][(x >> 2) | ((x << 2) & 0xF)] =
1120 ((uint32_t)ff_h264_dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] * 16) << shift;
1124 static av_cold int svq3_decode_init(AVCodecContext *avctx)
1126 SVQ3Context *s = avctx->priv_data;
1128 unsigned char *extradata;
1129 unsigned char *extradata_end;
1131 int marker_found = 0;
1133 s->cur_pic = av_mallocz(sizeof(*s->cur_pic));
1134 s->last_pic = av_mallocz(sizeof(*s->last_pic));
1135 s->next_pic = av_mallocz(sizeof(*s->next_pic));
1136 if (!s->next_pic || !s->last_pic || !s->cur_pic) {
1137 av_freep(&s->cur_pic);
1138 av_freep(&s->last_pic);
1139 av_freep(&s->next_pic);
1140 return AVERROR(ENOMEM);
1143 s->cur_pic->f = av_frame_alloc();
1144 s->last_pic->f = av_frame_alloc();
1145 s->next_pic->f = av_frame_alloc();
1146 if (!s->cur_pic->f || !s->last_pic->f || !s->next_pic->f)
1147 return AVERROR(ENOMEM);
1149 ff_h264dsp_init(&s->h264dsp, 8, 1);
1150 ff_h264_pred_init(&s->hpc, AV_CODEC_ID_SVQ3, 8, 1);
1151 ff_videodsp_init(&s->vdsp, 8);
1153 ff_hpeldsp_init(&s->hdsp, avctx->flags);
1154 ff_tpeldsp_init(&s->tdsp);
1156 avctx->pix_fmt = AV_PIX_FMT_YUVJ420P;
1157 avctx->color_range = AVCOL_RANGE_JPEG;
1160 s->halfpel_flag = 1;
1161 s->thirdpel_flag = 1;
1162 s->unknown_flag = 0;
1164 /* prowl for the "SEQH" marker in the extradata */
1165 extradata = (unsigned char *)avctx->extradata;
1166 extradata_end = avctx->extradata + avctx->extradata_size;
1168 for (m = 0; m + 8 < avctx->extradata_size; m++) {
1169 if (!memcmp(extradata, "SEQH", 4)) {
1177 /* if a match was found, parse the extra data */
1180 int frame_size_code;
1182 size = AV_RB32(&extradata[4]);
1183 if (size > extradata_end - extradata - 8)
1184 return AVERROR_INVALIDDATA;
1185 init_get_bits(&gb, extradata + 8, size * 8);
1187 /* 'frame size code' and optional 'width, height' */
1188 frame_size_code = get_bits(&gb, 3);
1189 switch (frame_size_code) {
1192 avctx->height = 120;
1200 avctx->height = 144;
1204 avctx->height = 288;
1208 avctx->height = 576;
1212 avctx->height = 180;
1216 avctx->height = 240;
1219 avctx->width = get_bits(&gb, 12);
1220 avctx->height = get_bits(&gb, 12);
1224 s->halfpel_flag = get_bits1(&gb);
1225 s->thirdpel_flag = get_bits1(&gb);
1227 /* unknown fields */
1233 s->low_delay = get_bits1(&gb);
1238 while (get_bits1(&gb))
1241 s->unknown_flag = get_bits1(&gb);
1242 avctx->has_b_frames = !s->low_delay;
1243 if (s->unknown_flag) {
1245 unsigned watermark_width = get_interleaved_ue_golomb(&gb);
1246 unsigned watermark_height = get_interleaved_ue_golomb(&gb);
1247 int u1 = get_interleaved_ue_golomb(&gb);
1248 int u2 = get_bits(&gb, 8);
1249 int u3 = get_bits(&gb, 2);
1250 int u4 = get_interleaved_ue_golomb(&gb);
1251 unsigned long buf_len = watermark_width *
1252 watermark_height * 4;
1253 int offset = get_bits_count(&gb) + 7 >> 3;
1256 if (watermark_height > 0 &&
1257 (uint64_t)watermark_width * 4 > UINT_MAX / watermark_height)
1260 buf = av_malloc(buf_len);
1261 av_log(avctx, AV_LOG_DEBUG, "watermark size: %ux%u\n",
1262 watermark_width, watermark_height);
1263 av_log(avctx, AV_LOG_DEBUG,
1264 "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n",
1265 u1, u2, u3, u4, offset);
1266 if (uncompress(buf, &buf_len, extradata + 8 + offset,
1267 size - offset) != Z_OK) {
1268 av_log(avctx, AV_LOG_ERROR,
1269 "could not uncompress watermark logo\n");
1273 s->watermark_key = ff_svq1_packet_checksum(buf, buf_len, 0);
1274 s->watermark_key = s->watermark_key << 16 | s->watermark_key;
1275 av_log(avctx, AV_LOG_DEBUG,
1276 "watermark key %#"PRIx32"\n", s->watermark_key);
1279 av_log(avctx, AV_LOG_ERROR,
1280 "this svq3 file contains watermark which need zlib support compiled in\n");
1286 s->mb_width = (avctx->width + 15) / 16;
1287 s->mb_height = (avctx->height + 15) / 16;
1288 s->mb_stride = s->mb_width + 1;
1289 s->mb_num = s->mb_width * s->mb_height;
1290 s->b_stride = 4 * s->mb_width;
1291 s->h_edge_pos = s->mb_width * 16;
1292 s->v_edge_pos = s->mb_height * 16;
1294 s->intra4x4_pred_mode = av_mallocz(s->mb_stride * 2 * 8);
1295 if (!s->intra4x4_pred_mode)
1296 return AVERROR(ENOMEM);
1298 s->mb2br_xy = av_mallocz(s->mb_stride * (s->mb_height + 1) *
1299 sizeof(*s->mb2br_xy));
1301 return AVERROR(ENOMEM);
1303 for (y = 0; y < s->mb_height; y++)
1304 for (x = 0; x < s->mb_width; x++) {
1305 const int mb_xy = x + y * s->mb_stride;
1307 s->mb2br_xy[mb_xy] = 8 * (mb_xy % (2 * s->mb_stride));
1310 init_dequant4_coeff_table(s);
1315 static void free_picture(AVCodecContext *avctx, SVQ3Frame *pic)
1318 for (i = 0; i < 2; i++) {
1319 av_buffer_unref(&pic->motion_val_buf[i]);
1320 av_buffer_unref(&pic->ref_index_buf[i]);
1322 av_buffer_unref(&pic->mb_type_buf);
1324 av_frame_unref(pic->f);
1327 static int get_buffer(AVCodecContext *avctx, SVQ3Frame *pic)
1329 SVQ3Context *s = avctx->priv_data;
1330 const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
1331 const int mb_array_size = s->mb_stride * s->mb_height;
1332 const int b4_stride = s->mb_width * 4 + 1;
1333 const int b4_array_size = b4_stride * s->mb_height * 4;
1336 if (!pic->motion_val_buf[0]) {
1339 pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) * sizeof(uint32_t));
1340 if (!pic->mb_type_buf)
1341 return AVERROR(ENOMEM);
1342 pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
1344 for (i = 0; i < 2; i++) {
1345 pic->motion_val_buf[i] = av_buffer_allocz(2 * (b4_array_size + 4) * sizeof(int16_t));
1346 pic->ref_index_buf[i] = av_buffer_allocz(4 * mb_array_size);
1347 if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i]) {
1348 ret = AVERROR(ENOMEM);
1352 pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
1353 pic->ref_index[i] = pic->ref_index_buf[i]->data;
1357 ret = ff_get_buffer(avctx, pic->f,
1358 (s->pict_type != AV_PICTURE_TYPE_B) ?
1359 AV_GET_BUFFER_FLAG_REF : 0);
1363 if (!s->edge_emu_buffer) {
1364 s->edge_emu_buffer = av_mallocz(pic->f->linesize[0] * 17);
1365 if (!s->edge_emu_buffer)
1366 return AVERROR(ENOMEM);
1371 free_picture(avctx, pic);
1375 static int svq3_decode_frame(AVCodecContext *avctx, void *data,
1376 int *got_frame, AVPacket *avpkt)
1378 const uint8_t *buf = avpkt->data;
1379 SVQ3Context *s = avctx->priv_data;
1380 int buf_size = avpkt->size;
1383 /* special case for last picture */
1384 if (buf_size == 0) {
1385 if (s->next_pic->f->data[0] && !s->low_delay && !s->last_frame_output) {
1386 ret = av_frame_ref(data, s->next_pic->f);
1389 s->last_frame_output = 1;
1395 ret = init_get_bits(&s->gb, buf, 8 * buf_size);
1399 s->mb_x = s->mb_y = s->mb_xy = 0;
1401 if (svq3_decode_slice_header(avctx))
1404 if (s->pict_type != AV_PICTURE_TYPE_B)
1405 FFSWAP(SVQ3Frame*, s->next_pic, s->last_pic);
1407 av_frame_unref(s->cur_pic->f);
1409 /* for skipping the frame */
1410 s->cur_pic->f->pict_type = s->pict_type;
1411 s->cur_pic->f->key_frame = (s->pict_type == AV_PICTURE_TYPE_I);
1413 ret = get_buffer(avctx, s->cur_pic);
1417 for (i = 0; i < 16; i++) {
1418 s->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * s->cur_pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
1419 s->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * s->cur_pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
1421 for (i = 0; i < 16; i++) {
1422 s->block_offset[16 + i] =
1423 s->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * s->cur_pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
1424 s->block_offset[48 + 16 + i] =
1425 s->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * s->cur_pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
1428 if (s->pict_type != AV_PICTURE_TYPE_I) {
1429 if (!s->last_pic->f->data[0]) {
1430 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1431 ret = get_buffer(avctx, s->last_pic);
1434 memset(s->last_pic->f->data[0], 0, avctx->height * s->last_pic->f->linesize[0]);
1435 memset(s->last_pic->f->data[1], 0x80, (avctx->height / 2) *
1436 s->last_pic->f->linesize[1]);
1437 memset(s->last_pic->f->data[2], 0x80, (avctx->height / 2) *
1438 s->last_pic->f->linesize[2]);
1441 if (s->pict_type == AV_PICTURE_TYPE_B && !s->next_pic->f->data[0]) {
1442 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1443 ret = get_buffer(avctx, s->next_pic);
1446 memset(s->next_pic->f->data[0], 0, avctx->height * s->next_pic->f->linesize[0]);
1447 memset(s->next_pic->f->data[1], 0x80, (avctx->height / 2) *
1448 s->next_pic->f->linesize[1]);
1449 memset(s->next_pic->f->data[2], 0x80, (avctx->height / 2) *
1450 s->next_pic->f->linesize[2]);
1454 if (avctx->debug & FF_DEBUG_PICT_INFO)
1455 av_log(s->avctx, AV_LOG_DEBUG,
1456 "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
1457 av_get_picture_type_char(s->pict_type),
1458 s->halfpel_flag, s->thirdpel_flag,
1459 s->adaptive_quant, s->qscale, s->slice_num);
1461 if (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B ||
1462 avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I ||
1463 avctx->skip_frame >= AVDISCARD_ALL)
1466 if (s->next_p_frame_damaged) {
1467 if (s->pict_type == AV_PICTURE_TYPE_B)
1470 s->next_p_frame_damaged = 0;
1473 if (s->pict_type == AV_PICTURE_TYPE_B) {
1474 s->frame_num_offset = s->slice_num - s->prev_frame_num;
1476 if (s->frame_num_offset < 0)
1477 s->frame_num_offset += 256;
1478 if (s->frame_num_offset == 0 ||
1479 s->frame_num_offset >= s->prev_frame_num_offset) {
1480 av_log(s->avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
1484 s->prev_frame_num = s->frame_num;
1485 s->frame_num = s->slice_num;
1486 s->prev_frame_num_offset = s->frame_num - s->prev_frame_num;
1488 if (s->prev_frame_num_offset < 0)
1489 s->prev_frame_num_offset += 256;
1492 for (m = 0; m < 2; m++) {
1494 for (i = 0; i < 4; i++) {
1496 for (j = -1; j < 4; j++)
1497 s->ref_cache[m][scan8[0] + 8 * i + j] = 1;
1499 s->ref_cache[m][scan8[0] + 8 * i + j] = PART_NOT_AVAILABLE;
1503 for (s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
1504 for (s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
1506 s->mb_xy = s->mb_x + s->mb_y * s->mb_stride;
1508 if ((get_bits_left(&s->gb_slice)) <= 7) {
1509 if (((get_bits_count(&s->gb_slice) & 7) == 0 ||
1510 show_bits(&s->gb_slice, get_bits_left(&s->gb_slice) & 7) == 0)) {
1512 if (svq3_decode_slice_header(avctx))
1515 /* TODO: support s->mb_skip_run */
1518 mb_type = get_interleaved_ue_golomb(&s->gb_slice);
1520 if (s->pict_type == AV_PICTURE_TYPE_I)
1522 else if (s->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4)
1524 if (mb_type > 33 || svq3_decode_mb(s, mb_type)) {
1525 av_log(s->avctx, AV_LOG_ERROR,
1526 "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
1533 if (s->pict_type != AV_PICTURE_TYPE_B && !s->low_delay)
1534 s->cur_pic->mb_type[s->mb_x + s->mb_y * s->mb_stride] =
1535 (s->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
1538 ff_draw_horiz_band(avctx, s->cur_pic->f,
1539 s->last_pic->f->data[0] ? s->last_pic->f : NULL,
1540 16 * s->mb_y, 16, PICT_FRAME, 0,
1544 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay)
1545 ret = av_frame_ref(data, s->cur_pic->f);
1546 else if (s->last_pic->f->data[0])
1547 ret = av_frame_ref(data, s->last_pic->f);
1551 /* Do not output the last pic after seeking. */
1552 if (s->last_pic->f->data[0] || s->low_delay)
1555 if (s->pict_type != AV_PICTURE_TYPE_B) {
1556 FFSWAP(SVQ3Frame*, s->cur_pic, s->next_pic);
1558 av_frame_unref(s->cur_pic->f);
1564 static av_cold int svq3_decode_end(AVCodecContext *avctx)
1566 SVQ3Context *s = avctx->priv_data;
1568 free_picture(avctx, s->cur_pic);
1569 free_picture(avctx, s->next_pic);
1570 free_picture(avctx, s->last_pic);
1571 av_frame_free(&s->cur_pic->f);
1572 av_frame_free(&s->next_pic->f);
1573 av_frame_free(&s->last_pic->f);
1574 av_freep(&s->cur_pic);
1575 av_freep(&s->next_pic);
1576 av_freep(&s->last_pic);
1577 av_freep(&s->slice_buf);
1578 av_freep(&s->intra4x4_pred_mode);
1579 av_freep(&s->edge_emu_buffer);
1580 av_freep(&s->mb2br_xy);
1585 AVCodec ff_svq3_decoder = {
1587 .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
1588 .type = AVMEDIA_TYPE_VIDEO,
1589 .id = AV_CODEC_ID_SVQ3,
1590 .priv_data_size = sizeof(SVQ3Context),
1591 .init = svq3_decode_init,
1592 .close = svq3_decode_end,
1593 .decode = svq3_decode_frame,
1594 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND |
1597 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,