2 * Copyright (c) 2003 The FFmpeg Project
4 * This file is part of FFmpeg.
6 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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.mplayerhq.hu/V-codecs/SVQ3/Vertical400kbit.sorenson3.mov
45 #include "libavutil/attributes.h"
46 #include "libavutil/crc.h"
50 #include "mpegutils.h"
56 #include "rectangle.h"
70 typedef struct SVQ3Frame {
73 AVBufferRef *motion_val_buf[2];
74 int16_t (*motion_val[2])[2];
76 AVBufferRef *mb_type_buf;
80 AVBufferRef *ref_index_buf[2];
84 typedef struct SVQ3Context {
85 AVCodecContext *avctx;
87 H264DSPContext h264dsp;
97 GetBitContext gb_slice;
103 uint32_t watermark_key;
107 int next_p_frame_damaged;
110 int last_frame_output;
115 int frame_num_offset;
116 int prev_frame_num_offset;
119 enum AVPictureType pict_type;
120 enum AVPictureType slice_type;
125 int mb_width, mb_height;
126 int mb_stride, mb_num;
131 int chroma_pred_mode;
132 int intra16x16_pred_mode;
134 int8_t intra4x4_pred_mode_cache[5 * 8];
135 int8_t (*intra4x4_pred_mode);
137 unsigned int top_samples_available;
138 unsigned int topright_samples_available;
139 unsigned int left_samples_available;
141 uint8_t *edge_emu_buffer;
143 DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5 * 8][2];
144 DECLARE_ALIGNED(8, int8_t, ref_cache)[2][5 * 8];
145 DECLARE_ALIGNED(16, int16_t, mb)[16 * 48 * 2];
146 DECLARE_ALIGNED(16, int16_t, mb_luma_dc)[3][16 * 2];
147 DECLARE_ALIGNED(8, uint8_t, non_zero_count_cache)[15 * 8];
148 uint32_t dequant4_coeff[QP_MAX_NUM + 1][16];
149 int block_offset[2 * (16 * 3)];
153 #define FULLPEL_MODE 1
154 #define HALFPEL_MODE 2
155 #define THIRDPEL_MODE 3
156 #define PREDICT_MODE 4
158 /* dual scan (from some older H.264 draft)
167 static const uint8_t svq3_scan[16] = {
168 0 + 0 * 4, 1 + 0 * 4, 2 + 0 * 4, 2 + 1 * 4,
169 2 + 2 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4,
170 0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 1 + 2 * 4,
171 0 + 3 * 4, 1 + 3 * 4, 2 + 3 * 4, 3 + 3 * 4,
174 static const uint8_t luma_dc_zigzag_scan[16] = {
175 0 * 16 + 0 * 64, 1 * 16 + 0 * 64, 2 * 16 + 0 * 64, 0 * 16 + 2 * 64,
176 3 * 16 + 0 * 64, 0 * 16 + 1 * 64, 1 * 16 + 1 * 64, 2 * 16 + 1 * 64,
177 1 * 16 + 2 * 64, 2 * 16 + 2 * 64, 3 * 16 + 2 * 64, 0 * 16 + 3 * 64,
178 3 * 16 + 1 * 64, 1 * 16 + 3 * 64, 2 * 16 + 3 * 64, 3 * 16 + 3 * 64,
181 static const uint8_t svq3_pred_0[25][2] = {
184 { 0, 2 }, { 1, 1 }, { 2, 0 },
185 { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
186 { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
187 { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
188 { 2, 4 }, { 3, 3 }, { 4, 2 },
193 static const int8_t svq3_pred_1[6][6][5] = {
194 { { 2, -1, -1, -1, -1 }, { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 },
195 { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, { 1, 2, -1, -1, -1 } },
196 { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 },
197 { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } },
198 { { 2, 0, -1, -1, -1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 },
199 { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } },
200 { { 2, 0, -1, -1, -1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 },
201 { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } },
202 { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 },
203 { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } },
204 { { 0, 2, -1, -1, -1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 },
205 { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } },
208 static const struct {
211 } svq3_dct_tables[2][16] = {
212 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
213 { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
214 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
215 { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
218 static const uint32_t svq3_dequant_coeff[32] = {
219 3881, 4351, 4890, 5481, 6154, 6914, 7761, 8718,
220 9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873,
221 24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683,
222 61694, 68745, 77615, 89113, 100253, 109366, 126635, 141533
225 static void svq3_luma_dc_dequant_idct_c(int16_t *output, int16_t *input, int qp)
227 const unsigned qmul = svq3_dequant_coeff[qp];
231 static const uint8_t x_offset[4] = { 0, 1 * stride, 4 * stride, 5 * stride };
233 for (i = 0; i < 4; i++) {
234 const int z0 = 13 * (input[4 * i + 0] + input[4 * i + 2]);
235 const int z1 = 13 * (input[4 * i + 0] - input[4 * i + 2]);
236 const int z2 = 7 * input[4 * i + 1] - 17 * input[4 * i + 3];
237 const int z3 = 17 * input[4 * i + 1] + 7 * input[4 * i + 3];
239 temp[4 * i + 0] = z0 + z3;
240 temp[4 * i + 1] = z1 + z2;
241 temp[4 * i + 2] = z1 - z2;
242 temp[4 * i + 3] = z0 - z3;
245 for (i = 0; i < 4; i++) {
246 const int offset = x_offset[i];
247 const int z0 = 13 * (temp[4 * 0 + i] + temp[4 * 2 + i]);
248 const int z1 = 13 * (temp[4 * 0 + i] - temp[4 * 2 + i]);
249 const int z2 = 7 * temp[4 * 1 + i] - 17 * temp[4 * 3 + i];
250 const int z3 = 17 * temp[4 * 1 + i] + 7 * temp[4 * 3 + i];
252 output[stride * 0 + offset] = (int)((z0 + z3) * qmul + 0x80000) >> 20;
253 output[stride * 2 + offset] = (int)((z1 + z2) * qmul + 0x80000) >> 20;
254 output[stride * 8 + offset] = (int)((z1 - z2) * qmul + 0x80000) >> 20;
255 output[stride * 10 + offset] = (int)((z0 - z3) * qmul + 0x80000) >> 20;
260 static void svq3_add_idct_c(uint8_t *dst, int16_t *block,
261 int stride, int qp, int dc)
263 const int qmul = svq3_dequant_coeff[qp];
267 dc = 13 * 13 * (dc == 1 ? 1538U* block[0]
268 : qmul * (block[0] >> 3) / 2);
272 for (i = 0; i < 4; i++) {
273 const int z0 = 13 * (block[0 + 4 * i] + block[2 + 4 * i]);
274 const int z1 = 13 * (block[0 + 4 * i] - block[2 + 4 * i]);
275 const int z2 = 7 * block[1 + 4 * i] - 17 * block[3 + 4 * i];
276 const int z3 = 17 * block[1 + 4 * i] + 7 * block[3 + 4 * i];
278 block[0 + 4 * i] = z0 + z3;
279 block[1 + 4 * i] = z1 + z2;
280 block[2 + 4 * i] = z1 - z2;
281 block[3 + 4 * i] = z0 - z3;
284 for (i = 0; i < 4; i++) {
285 const unsigned z0 = 13 * (block[i + 4 * 0] + block[i + 4 * 2]);
286 const unsigned z1 = 13 * (block[i + 4 * 0] - block[i + 4 * 2]);
287 const unsigned z2 = 7 * block[i + 4 * 1] - 17 * block[i + 4 * 3];
288 const unsigned z3 = 17 * block[i + 4 * 1] + 7 * block[i + 4 * 3];
289 const int rr = (dc + 0x80000u);
291 dst[i + stride * 0] = av_clip_uint8(dst[i + stride * 0] + ((int)((z0 + z3) * qmul + rr) >> 20));
292 dst[i + stride * 1] = av_clip_uint8(dst[i + stride * 1] + ((int)((z1 + z2) * qmul + rr) >> 20));
293 dst[i + stride * 2] = av_clip_uint8(dst[i + stride * 2] + ((int)((z1 - z2) * qmul + rr) >> 20));
294 dst[i + stride * 3] = av_clip_uint8(dst[i + stride * 3] + ((int)((z0 - z3) * qmul + rr) >> 20));
297 memset(block, 0, 16 * sizeof(int16_t));
300 static inline int svq3_decode_block(GetBitContext *gb, int16_t *block,
301 int index, const int type)
303 static const uint8_t *const scan_patterns[4] = {
304 luma_dc_zigzag_scan, ff_zigzag_scan, svq3_scan, ff_h264_chroma_dc_scan
307 int run, level, sign, limit;
309 const int intra = 3 * type >> 2;
310 const uint8_t *const scan = scan_patterns[type];
312 for (limit = (16 >> intra); index < 16; index = limit, limit += 8) {
313 for (; (vlc = get_interleaved_ue_golomb(gb)) != 0; index++) {
314 if ((int32_t)vlc < 0)
317 sign = (vlc & 1) ? 0 : -1;
324 } else if (vlc < 4) {
329 level = (vlc + 9 >> 2) - run;
333 run = svq3_dct_tables[intra][vlc].run;
334 level = svq3_dct_tables[intra][vlc].level;
337 level = (vlc >> 3) + ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
340 level = (vlc >> 4) + ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
345 if ((index += run) >= limit)
348 block[scan[index]] = (level ^ sign) - sign;
359 static av_always_inline int
360 svq3_fetch_diagonal_mv(const SVQ3Context *s, const int16_t **C,
361 int i, int list, int part_width)
363 const int topright_ref = s->ref_cache[list][i - 8 + part_width];
365 if (topright_ref != PART_NOT_AVAILABLE) {
366 *C = s->mv_cache[list][i - 8 + part_width];
369 *C = s->mv_cache[list][i - 8 - 1];
370 return s->ref_cache[list][i - 8 - 1];
375 * Get the predicted MV.
376 * @param n the block index
377 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
378 * @param mx the x component of the predicted motion vector
379 * @param my the y component of the predicted motion vector
381 static av_always_inline void svq3_pred_motion(const SVQ3Context *s, int n,
382 int part_width, int list,
383 int ref, int *const mx, int *const my)
385 const int index8 = scan8[n];
386 const int top_ref = s->ref_cache[list][index8 - 8];
387 const int left_ref = s->ref_cache[list][index8 - 1];
388 const int16_t *const A = s->mv_cache[list][index8 - 1];
389 const int16_t *const B = s->mv_cache[list][index8 - 8];
391 int diagonal_ref, match_count;
401 diagonal_ref = svq3_fetch_diagonal_mv(s, &C, index8, list, part_width);
402 match_count = (diagonal_ref == ref) + (top_ref == ref) + (left_ref == ref);
403 if (match_count > 1) { //most common
404 *mx = mid_pred(A[0], B[0], C[0]);
405 *my = mid_pred(A[1], B[1], C[1]);
406 } else if (match_count == 1) {
407 if (left_ref == ref) {
410 } else if (top_ref == ref) {
418 if (top_ref == PART_NOT_AVAILABLE &&
419 diagonal_ref == PART_NOT_AVAILABLE &&
420 left_ref != PART_NOT_AVAILABLE) {
424 *mx = mid_pred(A[0], B[0], C[0]);
425 *my = mid_pred(A[1], B[1], C[1]);
430 static inline void svq3_mc_dir_part(SVQ3Context *s,
431 int x, int y, int width, int height,
432 int mx, int my, int dxy,
433 int thirdpel, int dir, int avg)
435 const SVQ3Frame *pic = (dir == 0) ? s->last_pic : s->next_pic;
438 int blocksize = 2 - (width >> 3); // 16->0, 8->1, 4->2
439 int linesize = s->cur_pic->f->linesize[0];
440 int uvlinesize = s->cur_pic->f->linesize[1];
445 if (mx < 0 || mx >= s->h_edge_pos - width - 1 ||
446 my < 0 || my >= s->v_edge_pos - height - 1) {
448 mx = av_clip(mx, -16, s->h_edge_pos - width + 15);
449 my = av_clip(my, -16, s->v_edge_pos - height + 15);
452 /* form component predictions */
453 dest = s->cur_pic->f->data[0] + x + y * linesize;
454 src = pic->f->data[0] + mx + my * linesize;
457 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
459 width + 1, height + 1,
460 mx, my, s->h_edge_pos, s->v_edge_pos);
461 src = s->edge_emu_buffer;
464 (avg ? s->tdsp.avg_tpel_pixels_tab
465 : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src, linesize,
468 (avg ? s->hdsp.avg_pixels_tab
469 : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src, linesize,
472 if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
473 mx = mx + (mx < (int) x) >> 1;
474 my = my + (my < (int) y) >> 1;
476 height = height >> 1;
479 for (i = 1; i < 3; i++) {
480 dest = s->cur_pic->f->data[i] + (x >> 1) + (y >> 1) * uvlinesize;
481 src = pic->f->data[i] + mx + my * uvlinesize;
484 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
485 uvlinesize, uvlinesize,
486 width + 1, height + 1,
487 mx, my, (s->h_edge_pos >> 1),
489 src = s->edge_emu_buffer;
492 (avg ? s->tdsp.avg_tpel_pixels_tab
493 : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src,
497 (avg ? s->hdsp.avg_pixels_tab
498 : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src,
505 static inline int svq3_mc_dir(SVQ3Context *s, int size, int mode,
508 int i, j, k, mx, my, dx, dy, x, y;
509 const int part_width = ((size & 5) == 4) ? 4 : 16 >> (size & 1);
510 const int part_height = 16 >> ((unsigned)(size + 1) / 3);
511 const int extra_width = (mode == PREDICT_MODE) ? -16 * 6 : 0;
512 const int h_edge_pos = 6 * (s->h_edge_pos - part_width) - extra_width;
513 const int v_edge_pos = 6 * (s->v_edge_pos - part_height) - extra_width;
515 for (i = 0; i < 16; i += part_height)
516 for (j = 0; j < 16; j += part_width) {
517 const int b_xy = (4 * s->mb_x + (j >> 2)) +
518 (4 * s->mb_y + (i >> 2)) * s->b_stride;
520 x = 16 * s->mb_x + j;
521 y = 16 * s->mb_y + i;
522 k = (j >> 2 & 1) + (i >> 1 & 2) +
523 (j >> 1 & 4) + (i & 8);
525 if (mode != PREDICT_MODE) {
526 svq3_pred_motion(s, k, part_width >> 2, dir, 1, &mx, &my);
528 mx = s->next_pic->motion_val[0][b_xy][0] * 2;
529 my = s->next_pic->motion_val[0][b_xy][1] * 2;
532 mx = mx * s->frame_num_offset /
533 s->prev_frame_num_offset + 1 >> 1;
534 my = my * s->frame_num_offset /
535 s->prev_frame_num_offset + 1 >> 1;
537 mx = mx * (s->frame_num_offset - s->prev_frame_num_offset) /
538 s->prev_frame_num_offset + 1 >> 1;
539 my = my * (s->frame_num_offset - s->prev_frame_num_offset) /
540 s->prev_frame_num_offset + 1 >> 1;
544 /* clip motion vector prediction to frame border */
545 mx = av_clip(mx, extra_width - 6 * x, h_edge_pos - 6 * x);
546 my = av_clip(my, extra_width - 6 * y, v_edge_pos - 6 * y);
548 /* get (optional) motion vector differential */
549 if (mode == PREDICT_MODE) {
552 dy = get_interleaved_se_golomb(&s->gb_slice);
553 dx = get_interleaved_se_golomb(&s->gb_slice);
555 if (dx != (int16_t)dx || dy != (int16_t)dy) {
556 av_log(s->avctx, AV_LOG_ERROR, "invalid MV vlc\n");
561 /* compute motion vector */
562 if (mode == THIRDPEL_MODE) {
564 mx = (mx + 1 >> 1) + dx;
565 my = (my + 1 >> 1) + dy;
566 fx = (unsigned)(mx + 0x30000) / 3 - 0x10000;
567 fy = (unsigned)(my + 0x30000) / 3 - 0x10000;
568 dxy = (mx - 3 * fx) + 4 * (my - 3 * fy);
570 svq3_mc_dir_part(s, x, y, part_width, part_height,
571 fx, fy, dxy, 1, dir, avg);
574 } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) {
575 mx = (unsigned)(mx + 1 + 0x30000) / 3 + dx - 0x10000;
576 my = (unsigned)(my + 1 + 0x30000) / 3 + dy - 0x10000;
577 dxy = (mx & 1) + 2 * (my & 1);
579 svq3_mc_dir_part(s, x, y, part_width, part_height,
580 mx >> 1, my >> 1, dxy, 0, dir, avg);
584 mx = (unsigned)(mx + 3 + 0x60000) / 6 + dx - 0x10000;
585 my = (unsigned)(my + 3 + 0x60000) / 6 + dy - 0x10000;
587 svq3_mc_dir_part(s, x, y, part_width, part_height,
588 mx, my, 0, 0, dir, avg);
593 /* update mv_cache */
594 if (mode != PREDICT_MODE) {
595 int32_t mv = pack16to32(mx, my);
597 if (part_height == 8 && i < 8) {
598 AV_WN32A(s->mv_cache[dir][scan8[k] + 1 * 8], mv);
600 if (part_width == 8 && j < 8)
601 AV_WN32A(s->mv_cache[dir][scan8[k] + 1 + 1 * 8], mv);
603 if (part_width == 8 && j < 8)
604 AV_WN32A(s->mv_cache[dir][scan8[k] + 1], mv);
605 if (part_width == 4 || part_height == 4)
606 AV_WN32A(s->mv_cache[dir][scan8[k]], mv);
609 /* write back motion vectors */
610 fill_rectangle(s->cur_pic->motion_val[dir][b_xy],
611 part_width >> 2, part_height >> 2, s->b_stride,
612 pack16to32(mx, my), 4);
618 static av_always_inline void hl_decode_mb_idct_luma(SVQ3Context *s,
619 int mb_type, const int *block_offset,
620 int linesize, uint8_t *dest_y)
623 if (!IS_INTRA4x4(mb_type)) {
624 for (i = 0; i < 16; i++)
625 if (s->non_zero_count_cache[scan8[i]] || s->mb[i * 16]) {
626 uint8_t *const ptr = dest_y + block_offset[i];
627 svq3_add_idct_c(ptr, s->mb + i * 16, linesize,
628 s->qscale, IS_INTRA(mb_type) ? 1 : 0);
633 static av_always_inline void hl_decode_mb_predict_luma(SVQ3Context *s,
635 const int *block_offset,
640 int qscale = s->qscale;
642 if (IS_INTRA4x4(mb_type)) {
643 for (i = 0; i < 16; i++) {
644 uint8_t *const ptr = dest_y + block_offset[i];
645 const int dir = s->intra4x4_pred_mode_cache[scan8[i]];
649 if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
650 const int topright_avail = (s->topright_samples_available << i) & 0x8000;
651 av_assert2(s->mb_y || linesize <= block_offset[i]);
652 if (!topright_avail) {
653 tr = ptr[3 - linesize] * 0x01010101u;
654 topright = (uint8_t *)&tr;
656 topright = ptr + 4 - linesize;
660 s->hpc.pred4x4[dir](ptr, topright, linesize);
661 nnz = s->non_zero_count_cache[scan8[i]];
663 svq3_add_idct_c(ptr, s->mb + i * 16, linesize, qscale, 0);
667 s->hpc.pred16x16[s->intra16x16_pred_mode](dest_y, linesize);
668 svq3_luma_dc_dequant_idct_c(s->mb, s->mb_luma_dc[0], qscale);
672 static void hl_decode_mb(SVQ3Context *s)
674 const int mb_x = s->mb_x;
675 const int mb_y = s->mb_y;
676 const int mb_xy = s->mb_xy;
677 const int mb_type = s->cur_pic->mb_type[mb_xy];
678 uint8_t *dest_y, *dest_cb, *dest_cr;
679 int linesize, uvlinesize;
681 const int *block_offset = &s->block_offset[0];
682 const int block_h = 16 >> 1;
684 linesize = s->cur_pic->f->linesize[0];
685 uvlinesize = s->cur_pic->f->linesize[1];
687 dest_y = s->cur_pic->f->data[0] + (mb_x + mb_y * linesize) * 16;
688 dest_cb = s->cur_pic->f->data[1] + mb_x * 8 + mb_y * uvlinesize * block_h;
689 dest_cr = s->cur_pic->f->data[2] + mb_x * 8 + mb_y * uvlinesize * block_h;
691 s->vdsp.prefetch(dest_y + (s->mb_x & 3) * 4 * linesize + 64, linesize, 4);
692 s->vdsp.prefetch(dest_cb + (s->mb_x & 7) * uvlinesize + 64, dest_cr - dest_cb, 2);
694 if (IS_INTRA(mb_type)) {
695 s->hpc.pred8x8[s->chroma_pred_mode](dest_cb, uvlinesize);
696 s->hpc.pred8x8[s->chroma_pred_mode](dest_cr, uvlinesize);
698 hl_decode_mb_predict_luma(s, mb_type, block_offset, linesize, dest_y);
701 hl_decode_mb_idct_luma(s, mb_type, block_offset, linesize, dest_y);
704 uint8_t *dest[2] = { dest_cb, dest_cr };
705 s->h264dsp.h264_chroma_dc_dequant_idct(s->mb + 16 * 16 * 1,
706 s->dequant4_coeff[4][0]);
707 s->h264dsp.h264_chroma_dc_dequant_idct(s->mb + 16 * 16 * 2,
708 s->dequant4_coeff[4][0]);
709 for (j = 1; j < 3; j++) {
710 for (i = j * 16; i < j * 16 + 4; i++)
711 if (s->non_zero_count_cache[scan8[i]] || s->mb[i * 16]) {
712 uint8_t *const ptr = dest[j - 1] + block_offset[i];
713 svq3_add_idct_c(ptr, s->mb + i * 16,
714 uvlinesize, ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
720 static int svq3_decode_mb(SVQ3Context *s, unsigned int mb_type)
722 int i, j, k, m, dir, mode;
726 const int mb_xy = s->mb_xy;
727 const int b_xy = 4 * s->mb_x + 4 * s->mb_y * s->b_stride;
729 s->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
730 s->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
731 s->topright_samples_available = 0xFFFF;
733 if (mb_type == 0) { /* SKIP */
734 if (s->pict_type == AV_PICTURE_TYPE_P ||
735 s->next_pic->mb_type[mb_xy] == -1) {
736 svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
739 if (s->pict_type == AV_PICTURE_TYPE_B)
740 svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
743 mb_type = MB_TYPE_SKIP;
745 mb_type = FFMIN(s->next_pic->mb_type[mb_xy], 6);
746 if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 0, 0) < 0)
748 if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 1, 1) < 0)
751 mb_type = MB_TYPE_16x16;
753 } else if (mb_type < 8) { /* INTER */
754 if (s->thirdpel_flag && s->halfpel_flag == !get_bits1(&s->gb_slice))
755 mode = THIRDPEL_MODE;
756 else if (s->halfpel_flag &&
757 s->thirdpel_flag == !get_bits1(&s->gb_slice))
763 /* note ref_cache should contain here:
771 for (m = 0; m < 2; m++) {
772 if (s->mb_x > 0 && s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - 1] + 6] != -1) {
773 for (i = 0; i < 4; i++)
774 AV_COPY32(s->mv_cache[m][scan8[0] - 1 + i * 8],
775 s->cur_pic->motion_val[m][b_xy - 1 + i * s->b_stride]);
777 for (i = 0; i < 4; i++)
778 AV_ZERO32(s->mv_cache[m][scan8[0] - 1 + i * 8]);
781 memcpy(s->mv_cache[m][scan8[0] - 1 * 8],
782 s->cur_pic->motion_val[m][b_xy - s->b_stride],
783 4 * 2 * sizeof(int16_t));
784 memset(&s->ref_cache[m][scan8[0] - 1 * 8],
785 (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);
787 if (s->mb_x < s->mb_width - 1) {
788 AV_COPY32(s->mv_cache[m][scan8[0] + 4 - 1 * 8],
789 s->cur_pic->motion_val[m][b_xy - s->b_stride + 4]);
790 s->ref_cache[m][scan8[0] + 4 - 1 * 8] =
791 (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride + 1] + 6] == -1 ||
792 s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1;
794 s->ref_cache[m][scan8[0] + 4 - 1 * 8] = PART_NOT_AVAILABLE;
796 AV_COPY32(s->mv_cache[m][scan8[0] - 1 - 1 * 8],
797 s->cur_pic->motion_val[m][b_xy - s->b_stride - 1]);
798 s->ref_cache[m][scan8[0] - 1 - 1 * 8] =
799 (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] == -1) ? PART_NOT_AVAILABLE : 1;
801 s->ref_cache[m][scan8[0] - 1 - 1 * 8] = PART_NOT_AVAILABLE;
803 memset(&s->ref_cache[m][scan8[0] - 1 * 8 - 1],
804 PART_NOT_AVAILABLE, 8);
806 if (s->pict_type != AV_PICTURE_TYPE_B)
810 /* decode motion vector(s) and form prediction(s) */
811 if (s->pict_type == AV_PICTURE_TYPE_P) {
812 if (svq3_mc_dir(s, mb_type - 1, mode, 0, 0) < 0)
814 } else { /* AV_PICTURE_TYPE_B */
816 if (svq3_mc_dir(s, 0, mode, 0, 0) < 0)
819 for (i = 0; i < 4; i++)
820 memset(s->cur_pic->motion_val[0][b_xy + i * s->b_stride],
821 0, 4 * 2 * sizeof(int16_t));
824 if (svq3_mc_dir(s, 0, mode, 1, mb_type == 3) < 0)
827 for (i = 0; i < 4; i++)
828 memset(s->cur_pic->motion_val[1][b_xy + i * s->b_stride],
829 0, 4 * 2 * sizeof(int16_t));
833 mb_type = MB_TYPE_16x16;
834 } else if (mb_type == 8 || mb_type == 33) { /* INTRA4x4 */
835 int8_t *i4x4 = s->intra4x4_pred_mode + s->mb2br_xy[s->mb_xy];
836 int8_t *i4x4_cache = s->intra4x4_pred_mode_cache;
838 memset(s->intra4x4_pred_mode_cache, -1, 8 * 5 * sizeof(int8_t));
842 for (i = 0; i < 4; i++)
843 s->intra4x4_pred_mode_cache[scan8[0] - 1 + i * 8] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - 1] + 6 - i];
844 if (s->intra4x4_pred_mode_cache[scan8[0] - 1] == -1)
845 s->left_samples_available = 0x5F5F;
848 s->intra4x4_pred_mode_cache[4 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 0];
849 s->intra4x4_pred_mode_cache[5 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 1];
850 s->intra4x4_pred_mode_cache[6 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 2];
851 s->intra4x4_pred_mode_cache[7 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 3];
853 if (s->intra4x4_pred_mode_cache[4 + 8 * 0] == -1)
854 s->top_samples_available = 0x33FF;
857 /* decode prediction codes for luma blocks */
858 for (i = 0; i < 16; i += 2) {
859 vlc = get_interleaved_ue_golomb(&s->gb_slice);
862 av_log(s->avctx, AV_LOG_ERROR,
863 "luma prediction:%"PRIu32"\n", vlc);
867 left = &s->intra4x4_pred_mode_cache[scan8[i] - 1];
868 top = &s->intra4x4_pred_mode_cache[scan8[i] - 8];
870 left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
871 left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
873 if (left[1] == -1 || left[2] == -1) {
874 av_log(s->avctx, AV_LOG_ERROR, "weird prediction\n");
878 } else { /* mb_type == 33, DC_128_PRED block type */
879 for (i = 0; i < 4; i++)
880 memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_PRED, 4);
883 AV_COPY32(i4x4, i4x4_cache + 4 + 8 * 4);
884 i4x4[4] = i4x4_cache[7 + 8 * 3];
885 i4x4[5] = i4x4_cache[7 + 8 * 2];
886 i4x4[6] = i4x4_cache[7 + 8 * 1];
889 ff_h264_check_intra4x4_pred_mode(s->intra4x4_pred_mode_cache,
890 s->avctx, s->top_samples_available,
891 s->left_samples_available);
893 s->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
894 s->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
896 for (i = 0; i < 4; i++)
897 memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_128_PRED, 4);
899 s->top_samples_available = 0x33FF;
900 s->left_samples_available = 0x5F5F;
903 mb_type = MB_TYPE_INTRA4x4;
904 } else { /* INTRA16x16 */
905 dir = ff_h264_i_mb_type_info[mb_type - 8].pred_mode;
906 dir = (dir >> 1) ^ 3 * (dir & 1) ^ 1;
908 if ((s->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(s->avctx, s->top_samples_available,
909 s->left_samples_available, dir, 0)) < 0) {
910 av_log(s->avctx, AV_LOG_ERROR, "ff_h264_check_intra_pred_mode < 0\n");
911 return s->intra16x16_pred_mode;
914 cbp = ff_h264_i_mb_type_info[mb_type - 8].cbp;
915 mb_type = MB_TYPE_INTRA16x16;
918 if (!IS_INTER(mb_type) && s->pict_type != AV_PICTURE_TYPE_I) {
919 for (i = 0; i < 4; i++)
920 memset(s->cur_pic->motion_val[0][b_xy + i * s->b_stride],
921 0, 4 * 2 * sizeof(int16_t));
922 if (s->pict_type == AV_PICTURE_TYPE_B) {
923 for (i = 0; i < 4; i++)
924 memset(s->cur_pic->motion_val[1][b_xy + i * s->b_stride],
925 0, 4 * 2 * sizeof(int16_t));
928 if (!IS_INTRA4x4(mb_type)) {
929 memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy], DC_PRED, 8);
931 if (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B) {
932 memset(s->non_zero_count_cache + 8, 0, 14 * 8 * sizeof(uint8_t));
935 if (!IS_INTRA16x16(mb_type) &&
936 (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B)) {
937 if ((vlc = get_interleaved_ue_golomb(&s->gb_slice)) >= 48U){
938 av_log(s->avctx, AV_LOG_ERROR, "cbp_vlc=%"PRIu32"\n", vlc);
942 cbp = IS_INTRA(mb_type) ? ff_h264_golomb_to_intra4x4_cbp[vlc]
943 : ff_h264_golomb_to_inter_cbp[vlc];
945 if (IS_INTRA16x16(mb_type) ||
946 (s->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
947 s->qscale += get_interleaved_se_golomb(&s->gb_slice);
949 if (s->qscale > 31u) {
950 av_log(s->avctx, AV_LOG_ERROR, "qscale:%d\n", s->qscale);
954 if (IS_INTRA16x16(mb_type)) {
955 AV_ZERO128(s->mb_luma_dc[0] + 0);
956 AV_ZERO128(s->mb_luma_dc[0] + 8);
957 if (svq3_decode_block(&s->gb_slice, s->mb_luma_dc[0], 0, 1)) {
958 av_log(s->avctx, AV_LOG_ERROR,
959 "error while decoding intra luma dc\n");
965 const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
966 const int type = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
968 for (i = 0; i < 4; i++)
969 if ((cbp & (1 << i))) {
970 for (j = 0; j < 4; j++) {
971 k = index ? (1 * (j & 1) + 2 * (i & 1) +
972 2 * (j & 2) + 4 * (i & 2))
974 s->non_zero_count_cache[scan8[k]] = 1;
976 if (svq3_decode_block(&s->gb_slice, &s->mb[16 * k], index, type)) {
977 av_log(s->avctx, AV_LOG_ERROR,
978 "error while decoding block\n");
985 for (i = 1; i < 3; ++i)
986 if (svq3_decode_block(&s->gb_slice, &s->mb[16 * 16 * i], 0, 3)) {
987 av_log(s->avctx, AV_LOG_ERROR,
988 "error while decoding chroma dc block\n");
993 for (i = 1; i < 3; i++) {
994 for (j = 0; j < 4; j++) {
996 s->non_zero_count_cache[scan8[k]] = 1;
998 if (svq3_decode_block(&s->gb_slice, &s->mb[16 * k], 1, 1)) {
999 av_log(s->avctx, AV_LOG_ERROR,
1000 "error while decoding chroma ac block\n");
1010 s->cur_pic->mb_type[mb_xy] = mb_type;
1012 if (IS_INTRA(mb_type))
1013 s->chroma_pred_mode = ff_h264_check_intra_pred_mode(s->avctx, s->top_samples_available,
1014 s->left_samples_available, DC_PRED8x8, 1);
1019 static int svq3_decode_slice_header(AVCodecContext *avctx)
1021 SVQ3Context *s = avctx->priv_data;
1022 const int mb_xy = s->mb_xy;
1026 header = get_bits(&s->gb, 8);
1028 if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
1030 av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
1033 int slice_bits, slice_bytes, slice_length;
1034 int length = header >> 5 & 3;
1036 slice_length = show_bits(&s->gb, 8 * length);
1037 slice_bits = slice_length * 8;
1038 slice_bytes = slice_length + length - 1;
1040 skip_bits(&s->gb, 8);
1042 av_fast_malloc(&s->slice_buf, &s->slice_size, slice_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
1044 return AVERROR(ENOMEM);
1046 if (slice_bytes * 8LL > get_bits_left(&s->gb)) {
1047 av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
1048 return AVERROR_INVALIDDATA;
1050 memcpy(s->slice_buf, s->gb.buffer + s->gb.index / 8, slice_bytes);
1052 if (s->watermark_key) {
1053 uint32_t header = AV_RL32(&s->slice_buf[1]);
1054 AV_WL32(&s->slice_buf[1], header ^ s->watermark_key);
1056 init_get_bits(&s->gb_slice, s->slice_buf, slice_bits);
1059 memmove(s->slice_buf, &s->slice_buf[slice_length], length - 1);
1061 skip_bits_long(&s->gb, slice_bytes * 8);
1064 if ((slice_id = get_interleaved_ue_golomb(&s->gb_slice)) >= 3) {
1065 av_log(s->avctx, AV_LOG_ERROR, "illegal slice type %u \n", slice_id);
1069 s->slice_type = ff_h264_golomb_to_pict_type[slice_id];
1071 if ((header & 0x9F) == 2) {
1072 i = (s->mb_num < 64) ? 6 : (1 + av_log2(s->mb_num - 1));
1073 get_bits(&s->gb_slice, i);
1074 } else if (get_bits1(&s->gb_slice)) {
1075 avpriv_report_missing_feature(s->avctx, "Media key encryption");
1076 return AVERROR_PATCHWELCOME;
1079 s->slice_num = get_bits(&s->gb_slice, 8);
1080 s->qscale = get_bits(&s->gb_slice, 5);
1081 s->adaptive_quant = get_bits1(&s->gb_slice);
1083 /* unknown fields */
1084 skip_bits1(&s->gb_slice);
1086 if (s->has_watermark)
1087 skip_bits1(&s->gb_slice);
1089 skip_bits1(&s->gb_slice);
1090 skip_bits(&s->gb_slice, 2);
1092 if (skip_1stop_8data_bits(&s->gb_slice) < 0)
1093 return AVERROR_INVALIDDATA;
1095 /* reset intra predictors and invalidate motion vector references */
1097 memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - 1] + 3,
1098 -1, 4 * sizeof(int8_t));
1099 memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_x],
1100 -1, 8 * sizeof(int8_t) * s->mb_x);
1103 memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_stride],
1104 -1, 8 * sizeof(int8_t) * (s->mb_width - s->mb_x));
1107 s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] = -1;
1113 static void init_dequant4_coeff_table(SVQ3Context *s)
1116 const int max_qp = 51;
1118 for (q = 0; q < max_qp + 1; q++) {
1119 int shift = ff_h264_quant_div6[q] + 2;
1120 int idx = ff_h264_quant_rem6[q];
1121 for (x = 0; x < 16; x++)
1122 s->dequant4_coeff[q][(x >> 2) | ((x << 2) & 0xF)] =
1123 ((uint32_t)ff_h264_dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] * 16) << shift;
1127 static av_cold int svq3_decode_init(AVCodecContext *avctx)
1129 SVQ3Context *s = avctx->priv_data;
1131 unsigned char *extradata;
1132 unsigned char *extradata_end;
1134 int marker_found = 0;
1137 s->cur_pic = &s->frames[0];
1138 s->last_pic = &s->frames[1];
1139 s->next_pic = &s->frames[2];
1141 s->cur_pic->f = av_frame_alloc();
1142 s->last_pic->f = av_frame_alloc();
1143 s->next_pic->f = av_frame_alloc();
1144 if (!s->cur_pic->f || !s->last_pic->f || !s->next_pic->f)
1145 return AVERROR(ENOMEM);
1147 ff_h264dsp_init(&s->h264dsp, 8, 1);
1148 ff_h264_pred_init(&s->hpc, AV_CODEC_ID_SVQ3, 8, 1);
1149 ff_videodsp_init(&s->vdsp, 8);
1152 avctx->bits_per_raw_sample = 8;
1154 ff_hpeldsp_init(&s->hdsp, avctx->flags);
1155 ff_tpeldsp_init(&s->tdsp);
1157 avctx->pix_fmt = AV_PIX_FMT_YUVJ420P;
1158 avctx->color_range = AVCOL_RANGE_JPEG;
1161 s->halfpel_flag = 1;
1162 s->thirdpel_flag = 1;
1163 s->has_watermark = 0;
1165 /* prowl for the "SEQH" marker in the extradata */
1166 extradata = (unsigned char *)avctx->extradata;
1167 extradata_end = avctx->extradata + avctx->extradata_size;
1169 for (m = 0; m + 8 < avctx->extradata_size; m++) {
1170 if (!memcmp(extradata, "SEQH", 4)) {
1178 /* if a match was found, parse the extra data */
1181 int frame_size_code;
1182 int unk0, unk1, unk2, unk3, unk4;
1185 size = AV_RB32(&extradata[4]);
1186 if (size > extradata_end - extradata - 8)
1187 return AVERROR_INVALIDDATA;
1188 init_get_bits(&gb, extradata + 8, size * 8);
1190 /* 'frame size code' and optional 'width, height' */
1191 frame_size_code = get_bits(&gb, 3);
1192 switch (frame_size_code) {
1222 w = get_bits(&gb, 12);
1223 h = get_bits(&gb, 12);
1226 ret = ff_set_dimensions(avctx, w, h);
1230 s->halfpel_flag = get_bits1(&gb);
1231 s->thirdpel_flag = get_bits1(&gb);
1233 /* unknown fields */
1234 unk0 = get_bits1(&gb);
1235 unk1 = get_bits1(&gb);
1236 unk2 = get_bits1(&gb);
1237 unk3 = get_bits1(&gb);
1239 s->low_delay = get_bits1(&gb);
1242 unk4 = get_bits1(&gb);
1244 av_log(avctx, AV_LOG_DEBUG, "Unknown fields %d %d %d %d %d\n",
1245 unk0, unk1, unk2, unk3, unk4);
1247 if (skip_1stop_8data_bits(&gb) < 0)
1248 return AVERROR_INVALIDDATA;
1250 s->has_watermark = get_bits1(&gb);
1251 avctx->has_b_frames = !s->low_delay;
1252 if (s->has_watermark) {
1254 unsigned watermark_width = get_interleaved_ue_golomb(&gb);
1255 unsigned watermark_height = get_interleaved_ue_golomb(&gb);
1256 int u1 = get_interleaved_ue_golomb(&gb);
1257 int u2 = get_bits(&gb, 8);
1258 int u3 = get_bits(&gb, 2);
1259 int u4 = get_interleaved_ue_golomb(&gb);
1260 unsigned long buf_len = watermark_width *
1261 watermark_height * 4;
1262 int offset = get_bits_count(&gb) + 7 >> 3;
1265 if (watermark_height <= 0 ||
1266 (uint64_t)watermark_width * 4 > UINT_MAX / watermark_height)
1267 return AVERROR_INVALIDDATA;
1269 buf = av_malloc(buf_len);
1271 return AVERROR(ENOMEM);
1273 av_log(avctx, AV_LOG_DEBUG, "watermark size: %ux%u\n",
1274 watermark_width, watermark_height);
1275 av_log(avctx, AV_LOG_DEBUG,
1276 "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n",
1277 u1, u2, u3, u4, offset);
1278 if (uncompress(buf, &buf_len, extradata + 8 + offset,
1279 size - offset) != Z_OK) {
1280 av_log(avctx, AV_LOG_ERROR,
1281 "could not uncompress watermark logo\n");
1285 s->watermark_key = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_CCITT), 0, buf, buf_len));
1287 s->watermark_key = s->watermark_key << 16 | s->watermark_key;
1288 av_log(avctx, AV_LOG_DEBUG,
1289 "watermark key %#"PRIx32"\n", s->watermark_key);
1292 av_log(avctx, AV_LOG_ERROR,
1293 "this svq3 file contains watermark which need zlib support compiled in\n");
1294 return AVERROR(ENOSYS);
1299 s->mb_width = (avctx->width + 15) / 16;
1300 s->mb_height = (avctx->height + 15) / 16;
1301 s->mb_stride = s->mb_width + 1;
1302 s->mb_num = s->mb_width * s->mb_height;
1303 s->b_stride = 4 * s->mb_width;
1304 s->h_edge_pos = s->mb_width * 16;
1305 s->v_edge_pos = s->mb_height * 16;
1307 s->intra4x4_pred_mode = av_mallocz(s->mb_stride * 2 * 8);
1308 if (!s->intra4x4_pred_mode)
1309 return AVERROR(ENOMEM);
1311 s->mb2br_xy = av_mallocz(s->mb_stride * (s->mb_height + 1) *
1312 sizeof(*s->mb2br_xy));
1314 return AVERROR(ENOMEM);
1316 for (y = 0; y < s->mb_height; y++)
1317 for (x = 0; x < s->mb_width; x++) {
1318 const int mb_xy = x + y * s->mb_stride;
1320 s->mb2br_xy[mb_xy] = 8 * (mb_xy % (2 * s->mb_stride));
1323 init_dequant4_coeff_table(s);
1328 static void free_picture(AVCodecContext *avctx, SVQ3Frame *pic)
1331 for (i = 0; i < 2; i++) {
1332 av_buffer_unref(&pic->motion_val_buf[i]);
1333 av_buffer_unref(&pic->ref_index_buf[i]);
1335 av_buffer_unref(&pic->mb_type_buf);
1337 av_frame_unref(pic->f);
1340 static int get_buffer(AVCodecContext *avctx, SVQ3Frame *pic)
1342 SVQ3Context *s = avctx->priv_data;
1343 const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
1344 const int mb_array_size = s->mb_stride * s->mb_height;
1345 const int b4_stride = s->mb_width * 4 + 1;
1346 const int b4_array_size = b4_stride * s->mb_height * 4;
1349 if (!pic->motion_val_buf[0]) {
1352 pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) * sizeof(uint32_t));
1353 if (!pic->mb_type_buf)
1354 return AVERROR(ENOMEM);
1355 pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
1357 for (i = 0; i < 2; i++) {
1358 pic->motion_val_buf[i] = av_buffer_allocz(2 * (b4_array_size + 4) * sizeof(int16_t));
1359 pic->ref_index_buf[i] = av_buffer_allocz(4 * mb_array_size);
1360 if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i]) {
1361 ret = AVERROR(ENOMEM);
1365 pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
1366 pic->ref_index[i] = pic->ref_index_buf[i]->data;
1370 ret = ff_get_buffer(avctx, pic->f,
1371 (s->pict_type != AV_PICTURE_TYPE_B) ?
1372 AV_GET_BUFFER_FLAG_REF : 0);
1376 if (!s->edge_emu_buffer) {
1377 s->edge_emu_buffer = av_mallocz_array(pic->f->linesize[0], 17);
1378 if (!s->edge_emu_buffer)
1379 return AVERROR(ENOMEM);
1384 free_picture(avctx, pic);
1388 static int svq3_decode_frame(AVCodecContext *avctx, void *data,
1389 int *got_frame, AVPacket *avpkt)
1391 SVQ3Context *s = avctx->priv_data;
1392 int buf_size = avpkt->size;
1397 /* special case for last picture */
1398 if (buf_size == 0) {
1399 if (s->next_pic->f->data[0] && !s->low_delay && !s->last_frame_output) {
1400 ret = av_frame_ref(data, s->next_pic->f);
1403 s->last_frame_output = 1;
1409 s->mb_x = s->mb_y = s->mb_xy = 0;
1411 if (s->watermark_key) {
1412 av_fast_padded_malloc(&s->buf, &s->buf_size, buf_size);
1414 return AVERROR(ENOMEM);
1415 memcpy(s->buf, avpkt->data, buf_size);
1421 ret = init_get_bits(&s->gb, buf, 8 * buf_size);
1425 if (svq3_decode_slice_header(avctx))
1428 s->pict_type = s->slice_type;
1430 if (s->pict_type != AV_PICTURE_TYPE_B)
1431 FFSWAP(SVQ3Frame*, s->next_pic, s->last_pic);
1433 av_frame_unref(s->cur_pic->f);
1435 /* for skipping the frame */
1436 s->cur_pic->f->pict_type = s->pict_type;
1437 s->cur_pic->f->key_frame = (s->pict_type == AV_PICTURE_TYPE_I);
1439 ret = get_buffer(avctx, s->cur_pic);
1443 for (i = 0; i < 16; i++) {
1444 s->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * s->cur_pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
1445 s->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * s->cur_pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
1447 for (i = 0; i < 16; i++) {
1448 s->block_offset[16 + i] =
1449 s->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * s->cur_pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
1450 s->block_offset[48 + 16 + i] =
1451 s->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * s->cur_pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
1454 if (s->pict_type != AV_PICTURE_TYPE_I) {
1455 if (!s->last_pic->f->data[0]) {
1456 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1457 av_frame_unref(s->last_pic->f);
1458 ret = get_buffer(avctx, s->last_pic);
1461 memset(s->last_pic->f->data[0], 0, avctx->height * s->last_pic->f->linesize[0]);
1462 memset(s->last_pic->f->data[1], 0x80, (avctx->height / 2) *
1463 s->last_pic->f->linesize[1]);
1464 memset(s->last_pic->f->data[2], 0x80, (avctx->height / 2) *
1465 s->last_pic->f->linesize[2]);
1468 if (s->pict_type == AV_PICTURE_TYPE_B && !s->next_pic->f->data[0]) {
1469 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1470 av_frame_unref(s->next_pic->f);
1471 ret = get_buffer(avctx, s->next_pic);
1474 memset(s->next_pic->f->data[0], 0, avctx->height * s->next_pic->f->linesize[0]);
1475 memset(s->next_pic->f->data[1], 0x80, (avctx->height / 2) *
1476 s->next_pic->f->linesize[1]);
1477 memset(s->next_pic->f->data[2], 0x80, (avctx->height / 2) *
1478 s->next_pic->f->linesize[2]);
1482 if (avctx->debug & FF_DEBUG_PICT_INFO)
1483 av_log(s->avctx, AV_LOG_DEBUG,
1484 "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
1485 av_get_picture_type_char(s->pict_type),
1486 s->halfpel_flag, s->thirdpel_flag,
1487 s->adaptive_quant, s->qscale, s->slice_num);
1489 if (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B ||
1490 avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I ||
1491 avctx->skip_frame >= AVDISCARD_ALL)
1494 if (s->next_p_frame_damaged) {
1495 if (s->pict_type == AV_PICTURE_TYPE_B)
1498 s->next_p_frame_damaged = 0;
1501 if (s->pict_type == AV_PICTURE_TYPE_B) {
1502 s->frame_num_offset = s->slice_num - s->prev_frame_num;
1504 if (s->frame_num_offset < 0)
1505 s->frame_num_offset += 256;
1506 if (s->frame_num_offset == 0 ||
1507 s->frame_num_offset >= s->prev_frame_num_offset) {
1508 av_log(s->avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
1512 s->prev_frame_num = s->frame_num;
1513 s->frame_num = s->slice_num;
1514 s->prev_frame_num_offset = s->frame_num - s->prev_frame_num;
1516 if (s->prev_frame_num_offset < 0)
1517 s->prev_frame_num_offset += 256;
1520 for (m = 0; m < 2; m++) {
1522 for (i = 0; i < 4; i++) {
1524 for (j = -1; j < 4; j++)
1525 s->ref_cache[m][scan8[0] + 8 * i + j] = 1;
1527 s->ref_cache[m][scan8[0] + 8 * i + j] = PART_NOT_AVAILABLE;
1531 for (s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
1532 for (s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
1534 s->mb_xy = s->mb_x + s->mb_y * s->mb_stride;
1536 if ((get_bits_left(&s->gb_slice)) <= 7) {
1537 if (((get_bits_count(&s->gb_slice) & 7) == 0 ||
1538 show_bits(&s->gb_slice, get_bits_left(&s->gb_slice) & 7) == 0)) {
1540 if (svq3_decode_slice_header(avctx))
1543 if (s->slice_type != s->pict_type) {
1544 avpriv_request_sample(avctx, "non constant slice type");
1546 /* TODO: support s->mb_skip_run */
1549 mb_type = get_interleaved_ue_golomb(&s->gb_slice);
1551 if (s->pict_type == AV_PICTURE_TYPE_I)
1553 else if (s->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4)
1555 if (mb_type > 33 || svq3_decode_mb(s, mb_type)) {
1556 av_log(s->avctx, AV_LOG_ERROR,
1557 "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
1561 if (mb_type != 0 || s->cbp)
1564 if (s->pict_type != AV_PICTURE_TYPE_B && !s->low_delay)
1565 s->cur_pic->mb_type[s->mb_x + s->mb_y * s->mb_stride] =
1566 (s->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
1569 ff_draw_horiz_band(avctx, s->cur_pic->f,
1570 s->last_pic->f->data[0] ? s->last_pic->f : NULL,
1571 16 * s->mb_y, 16, PICT_FRAME, 0,
1575 left = buf_size*8 - get_bits_count(&s->gb_slice);
1577 if (s->mb_y != s->mb_height || s->mb_x != s->mb_width) {
1578 av_log(avctx, AV_LOG_INFO, "frame num %d incomplete pic x %d y %d left %d\n", avctx->frame_number, s->mb_y, s->mb_x, left);
1579 //av_hex_dump(stderr, buf+buf_size-8, 8);
1583 av_log(avctx, AV_LOG_ERROR, "frame num %d left %d\n", avctx->frame_number, left);
1587 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay)
1588 ret = av_frame_ref(data, s->cur_pic->f);
1589 else if (s->last_pic->f->data[0])
1590 ret = av_frame_ref(data, s->last_pic->f);
1594 /* Do not output the last pic after seeking. */
1595 if (s->last_pic->f->data[0] || s->low_delay)
1598 if (s->pict_type != AV_PICTURE_TYPE_B) {
1599 FFSWAP(SVQ3Frame*, s->cur_pic, s->next_pic);
1601 av_frame_unref(s->cur_pic->f);
1607 static av_cold int svq3_decode_end(AVCodecContext *avctx)
1609 SVQ3Context *s = avctx->priv_data;
1611 free_picture(avctx, s->cur_pic);
1612 free_picture(avctx, s->next_pic);
1613 free_picture(avctx, s->last_pic);
1614 av_frame_free(&s->cur_pic->f);
1615 av_frame_free(&s->next_pic->f);
1616 av_frame_free(&s->last_pic->f);
1617 av_freep(&s->slice_buf);
1618 av_freep(&s->intra4x4_pred_mode);
1619 av_freep(&s->edge_emu_buffer);
1620 av_freep(&s->mb2br_xy);
1629 AVCodec ff_svq3_decoder = {
1631 .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
1632 .type = AVMEDIA_TYPE_VIDEO,
1633 .id = AV_CODEC_ID_SVQ3,
1634 .priv_data_size = sizeof(SVQ3Context),
1635 .init = svq3_decode_init,
1636 .close = svq3_decode_end,
1637 .decode = svq3_decode_frame,
1638 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND |
1641 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,
1643 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,