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"
48 #include "mpegutils.h"
50 #include "h264_mvpred.h"
55 #include "rectangle.h"
57 #include "vdpau_internal.h"
70 typedef struct SVQ3Context {
73 H264DSPContext h264dsp;
80 H264Picture *next_pic;
81 H264Picture *last_pic;
88 uint32_t watermark_key;
92 int next_p_frame_damaged;
95 int last_frame_output;
100 int chroma_pred_mode;
101 int intra16x16_pred_mode;
103 int8_t intra4x4_pred_mode_cache[5 * 8];
104 int8_t (*intra4x4_pred_mode);
106 unsigned int top_samples_available;
107 unsigned int topright_samples_available;
108 unsigned int left_samples_available;
110 uint8_t *edge_emu_buffer;
112 DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5 * 8][2];
113 DECLARE_ALIGNED(8, int8_t, ref_cache)[2][5 * 8];
116 #define FULLPEL_MODE 1
117 #define HALFPEL_MODE 2
118 #define THIRDPEL_MODE 3
119 #define PREDICT_MODE 4
121 /* dual scan (from some older h264 draft)
130 static const uint8_t svq3_scan[16] = {
131 0 + 0 * 4, 1 + 0 * 4, 2 + 0 * 4, 2 + 1 * 4,
132 2 + 2 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4,
133 0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 1 + 2 * 4,
134 0 + 3 * 4, 1 + 3 * 4, 2 + 3 * 4, 3 + 3 * 4,
137 static const uint8_t luma_dc_zigzag_scan[16] = {
138 0 * 16 + 0 * 64, 1 * 16 + 0 * 64, 2 * 16 + 0 * 64, 0 * 16 + 2 * 64,
139 3 * 16 + 0 * 64, 0 * 16 + 1 * 64, 1 * 16 + 1 * 64, 2 * 16 + 1 * 64,
140 1 * 16 + 2 * 64, 2 * 16 + 2 * 64, 3 * 16 + 2 * 64, 0 * 16 + 3 * 64,
141 3 * 16 + 1 * 64, 1 * 16 + 3 * 64, 2 * 16 + 3 * 64, 3 * 16 + 3 * 64,
144 static const uint8_t svq3_pred_0[25][2] = {
147 { 0, 2 }, { 1, 1 }, { 2, 0 },
148 { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
149 { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
150 { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
151 { 2, 4 }, { 3, 3 }, { 4, 2 },
156 static const int8_t svq3_pred_1[6][6][5] = {
157 { { 2, -1, -1, -1, -1 }, { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 },
158 { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, { 1, 2, -1, -1, -1 } },
159 { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 },
160 { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } },
161 { { 2, 0, -1, -1, -1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 },
162 { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } },
163 { { 2, 0, -1, -1, -1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 },
164 { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } },
165 { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 },
166 { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } },
167 { { 0, 2, -1, -1, -1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 },
168 { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } },
171 static const struct {
174 } svq3_dct_tables[2][16] = {
175 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
176 { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
177 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
178 { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
181 static const uint32_t svq3_dequant_coeff[32] = {
182 3881, 4351, 4890, 5481, 6154, 6914, 7761, 8718,
183 9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873,
184 24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683,
185 61694, 68745, 77615, 89113, 100253, 109366, 126635, 141533
188 static int svq3_decode_end(AVCodecContext *avctx);
190 static void svq3_luma_dc_dequant_idct_c(int16_t *output, int16_t *input, int qp)
192 const int qmul = svq3_dequant_coeff[qp];
196 static const uint8_t x_offset[4] = { 0, 1 * stride, 4 * stride, 5 * stride };
198 for (i = 0; i < 4; i++) {
199 const int z0 = 13 * (input[4 * i + 0] + input[4 * i + 2]);
200 const int z1 = 13 * (input[4 * i + 0] - input[4 * i + 2]);
201 const int z2 = 7 * input[4 * i + 1] - 17 * input[4 * i + 3];
202 const int z3 = 17 * input[4 * i + 1] + 7 * input[4 * i + 3];
204 temp[4 * i + 0] = z0 + z3;
205 temp[4 * i + 1] = z1 + z2;
206 temp[4 * i + 2] = z1 - z2;
207 temp[4 * i + 3] = z0 - z3;
210 for (i = 0; i < 4; i++) {
211 const int offset = x_offset[i];
212 const int z0 = 13 * (temp[4 * 0 + i] + temp[4 * 2 + i]);
213 const int z1 = 13 * (temp[4 * 0 + i] - temp[4 * 2 + i]);
214 const int z2 = 7 * temp[4 * 1 + i] - 17 * temp[4 * 3 + i];
215 const int z3 = 17 * temp[4 * 1 + i] + 7 * temp[4 * 3 + i];
217 output[stride * 0 + offset] = (z0 + z3) * qmul + 0x80000 >> 20;
218 output[stride * 2 + offset] = (z1 + z2) * qmul + 0x80000 >> 20;
219 output[stride * 8 + offset] = (z1 - z2) * qmul + 0x80000 >> 20;
220 output[stride * 10 + offset] = (z0 - z3) * qmul + 0x80000 >> 20;
225 static void svq3_add_idct_c(uint8_t *dst, int16_t *block,
226 int stride, int qp, int dc)
228 const int qmul = svq3_dequant_coeff[qp];
232 dc = 13 * 13 * (dc == 1 ? 1538 * block[0]
233 : qmul * (block[0] >> 3) / 2);
237 for (i = 0; i < 4; i++) {
238 const int z0 = 13 * (block[0 + 4 * i] + block[2 + 4 * i]);
239 const int z1 = 13 * (block[0 + 4 * i] - block[2 + 4 * i]);
240 const int z2 = 7 * block[1 + 4 * i] - 17 * block[3 + 4 * i];
241 const int z3 = 17 * block[1 + 4 * i] + 7 * block[3 + 4 * i];
243 block[0 + 4 * i] = z0 + z3;
244 block[1 + 4 * i] = z1 + z2;
245 block[2 + 4 * i] = z1 - z2;
246 block[3 + 4 * i] = z0 - z3;
249 for (i = 0; i < 4; i++) {
250 const int z0 = 13 * (block[i + 4 * 0] + block[i + 4 * 2]);
251 const int z1 = 13 * (block[i + 4 * 0] - block[i + 4 * 2]);
252 const int z2 = 7 * block[i + 4 * 1] - 17 * block[i + 4 * 3];
253 const int z3 = 17 * block[i + 4 * 1] + 7 * block[i + 4 * 3];
254 const int rr = (dc + 0x80000);
256 dst[i + stride * 0] = av_clip_uint8(dst[i + stride * 0] + ((z0 + z3) * qmul + rr >> 20));
257 dst[i + stride * 1] = av_clip_uint8(dst[i + stride * 1] + ((z1 + z2) * qmul + rr >> 20));
258 dst[i + stride * 2] = av_clip_uint8(dst[i + stride * 2] + ((z1 - z2) * qmul + rr >> 20));
259 dst[i + stride * 3] = av_clip_uint8(dst[i + stride * 3] + ((z0 - z3) * qmul + rr >> 20));
262 memset(block, 0, 16 * sizeof(int16_t));
265 static inline int svq3_decode_block(GetBitContext *gb, int16_t *block,
266 int index, const int type)
268 static const uint8_t *const scan_patterns[4] = {
269 luma_dc_zigzag_scan, ff_zigzag_scan, svq3_scan, ff_h264_chroma_dc_scan
272 int run, level, sign, limit;
274 const int intra = 3 * type >> 2;
275 const uint8_t *const scan = scan_patterns[type];
277 for (limit = (16 >> intra); index < 16; index = limit, limit += 8) {
278 for (; (vlc = svq3_get_ue_golomb(gb)) != 0; index++) {
279 if ((int32_t)vlc < 0)
282 sign = (vlc & 1) ? 0 : -1;
289 } else if (vlc < 4) {
294 level = (vlc + 9 >> 2) - run;
298 run = svq3_dct_tables[intra][vlc].run;
299 level = svq3_dct_tables[intra][vlc].level;
302 level = (vlc >> 3) + ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
305 level = (vlc >> 4) + ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
310 if ((index += run) >= limit)
313 block[scan[index]] = (level ^ sign) - sign;
324 static av_always_inline int
325 svq3_fetch_diagonal_mv(const SVQ3Context *s, const int16_t **C,
326 int i, int list, int part_width)
328 const int topright_ref = s->ref_cache[list][i - 8 + part_width];
330 if (topright_ref != PART_NOT_AVAILABLE) {
331 *C = s->mv_cache[list][i - 8 + part_width];
334 *C = s->mv_cache[list][i - 8 - 1];
335 return s->ref_cache[list][i - 8 - 1];
340 * Get the predicted MV.
341 * @param n the block index
342 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
343 * @param mx the x component of the predicted motion vector
344 * @param my the y component of the predicted motion vector
346 static av_always_inline void svq3_pred_motion(const SVQ3Context *s, int n,
347 int part_width, int list,
348 int ref, int *const mx, int *const my)
350 const int index8 = scan8[n];
351 const int top_ref = s->ref_cache[list][index8 - 8];
352 const int left_ref = s->ref_cache[list][index8 - 1];
353 const int16_t *const A = s->mv_cache[list][index8 - 1];
354 const int16_t *const B = s->mv_cache[list][index8 - 8];
356 int diagonal_ref, match_count;
366 diagonal_ref = svq3_fetch_diagonal_mv(s, &C, index8, list, part_width);
367 match_count = (diagonal_ref == ref) + (top_ref == ref) + (left_ref == ref);
368 if (match_count > 1) { //most common
369 *mx = mid_pred(A[0], B[0], C[0]);
370 *my = mid_pred(A[1], B[1], C[1]);
371 } else if (match_count == 1) {
372 if (left_ref == ref) {
375 } else if (top_ref == ref) {
383 if (top_ref == PART_NOT_AVAILABLE &&
384 diagonal_ref == PART_NOT_AVAILABLE &&
385 left_ref != PART_NOT_AVAILABLE) {
389 *mx = mid_pred(A[0], B[0], C[0]);
390 *my = mid_pred(A[1], B[1], C[1]);
395 static inline void svq3_mc_dir_part(SVQ3Context *s,
396 int x, int y, int width, int height,
397 int mx, int my, int dxy,
398 int thirdpel, int dir, int avg)
400 H264Context *h = &s->h;
401 H264SliceContext *sl = &h->slice_ctx[0];
402 const H264Picture *pic = (dir == 0) ? s->last_pic : s->next_pic;
405 int blocksize = 2 - (width >> 3); // 16->0, 8->1, 4->2
410 if (mx < 0 || mx >= s->h_edge_pos - width - 1 ||
411 my < 0 || my >= s->v_edge_pos - height - 1) {
413 mx = av_clip(mx, -16, s->h_edge_pos - width + 15);
414 my = av_clip(my, -16, s->v_edge_pos - height + 15);
417 /* form component predictions */
418 dest = h->cur_pic.f->data[0] + x + y * sl->linesize;
419 src = pic->f->data[0] + mx + my * sl->linesize;
422 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
423 sl->linesize, sl->linesize,
424 width + 1, height + 1,
425 mx, my, s->h_edge_pos, s->v_edge_pos);
426 src = s->edge_emu_buffer;
429 (avg ? s->tdsp.avg_tpel_pixels_tab
430 : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src, sl->linesize,
433 (avg ? s->hdsp.avg_pixels_tab
434 : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src, sl->linesize,
437 if (!(h->flags & AV_CODEC_FLAG_GRAY)) {
438 mx = mx + (mx < (int) x) >> 1;
439 my = my + (my < (int) y) >> 1;
441 height = height >> 1;
444 for (i = 1; i < 3; i++) {
445 dest = h->cur_pic.f->data[i] + (x >> 1) + (y >> 1) * sl->uvlinesize;
446 src = pic->f->data[i] + mx + my * sl->uvlinesize;
449 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
450 sl->uvlinesize, sl->uvlinesize,
451 width + 1, height + 1,
452 mx, my, (s->h_edge_pos >> 1),
454 src = s->edge_emu_buffer;
457 (avg ? s->tdsp.avg_tpel_pixels_tab
458 : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src,
462 (avg ? s->hdsp.avg_pixels_tab
463 : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src,
470 static inline int svq3_mc_dir(SVQ3Context *s, int size, int mode,
473 int i, j, k, mx, my, dx, dy, x, y;
474 H264Context *h = &s->h;
475 const int part_width = ((size & 5) == 4) ? 4 : 16 >> (size & 1);
476 const int part_height = 16 >> ((unsigned)(size + 1) / 3);
477 const int extra_width = (mode == PREDICT_MODE) ? -16 * 6 : 0;
478 const int h_edge_pos = 6 * (s->h_edge_pos - part_width) - extra_width;
479 const int v_edge_pos = 6 * (s->v_edge_pos - part_height) - extra_width;
481 for (i = 0; i < 16; i += part_height)
482 for (j = 0; j < 16; j += part_width) {
483 const int b_xy = (4 * s->mb_x + (j >> 2)) +
484 (4 * s->mb_y + (i >> 2)) * h->b_stride;
486 x = 16 * s->mb_x + j;
487 y = 16 * s->mb_y + i;
488 k = (j >> 2 & 1) + (i >> 1 & 2) +
489 (j >> 1 & 4) + (i & 8);
491 if (mode != PREDICT_MODE) {
492 svq3_pred_motion(s, k, part_width >> 2, dir, 1, &mx, &my);
494 mx = s->next_pic->motion_val[0][b_xy][0] << 1;
495 my = s->next_pic->motion_val[0][b_xy][1] << 1;
498 mx = mx * h->frame_num_offset /
499 h->prev_frame_num_offset + 1 >> 1;
500 my = my * h->frame_num_offset /
501 h->prev_frame_num_offset + 1 >> 1;
503 mx = mx * (h->frame_num_offset - h->prev_frame_num_offset) /
504 h->prev_frame_num_offset + 1 >> 1;
505 my = my * (h->frame_num_offset - h->prev_frame_num_offset) /
506 h->prev_frame_num_offset + 1 >> 1;
510 /* clip motion vector prediction to frame border */
511 mx = av_clip(mx, extra_width - 6 * x, h_edge_pos - 6 * x);
512 my = av_clip(my, extra_width - 6 * y, v_edge_pos - 6 * y);
514 /* get (optional) motion vector differential */
515 if (mode == PREDICT_MODE) {
518 dy = svq3_get_se_golomb(&h->gb);
519 dx = svq3_get_se_golomb(&h->gb);
521 if (dx == INVALID_VLC || dy == INVALID_VLC) {
522 av_log(h->avctx, AV_LOG_ERROR, "invalid MV vlc\n");
527 /* compute motion vector */
528 if (mode == THIRDPEL_MODE) {
530 mx = (mx + 1 >> 1) + dx;
531 my = (my + 1 >> 1) + dy;
532 fx = (unsigned)(mx + 0x3000) / 3 - 0x1000;
533 fy = (unsigned)(my + 0x3000) / 3 - 0x1000;
534 dxy = (mx - 3 * fx) + 4 * (my - 3 * fy);
536 svq3_mc_dir_part(s, x, y, part_width, part_height,
537 fx, fy, dxy, 1, dir, avg);
540 } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) {
541 mx = (unsigned)(mx + 1 + 0x3000) / 3 + dx - 0x1000;
542 my = (unsigned)(my + 1 + 0x3000) / 3 + dy - 0x1000;
543 dxy = (mx & 1) + 2 * (my & 1);
545 svq3_mc_dir_part(s, x, y, part_width, part_height,
546 mx >> 1, my >> 1, dxy, 0, dir, avg);
550 mx = (unsigned)(mx + 3 + 0x6000) / 6 + dx - 0x1000;
551 my = (unsigned)(my + 3 + 0x6000) / 6 + dy - 0x1000;
553 svq3_mc_dir_part(s, x, y, part_width, part_height,
554 mx, my, 0, 0, dir, avg);
559 /* update mv_cache */
560 if (mode != PREDICT_MODE) {
561 int32_t mv = pack16to32(mx, my);
563 if (part_height == 8 && i < 8) {
564 AV_WN32A(s->mv_cache[dir][scan8[k] + 1 * 8], mv);
566 if (part_width == 8 && j < 8)
567 AV_WN32A(s->mv_cache[dir][scan8[k] + 1 + 1 * 8], mv);
569 if (part_width == 8 && j < 8)
570 AV_WN32A(s->mv_cache[dir][scan8[k] + 1], mv);
571 if (part_width == 4 || part_height == 4)
572 AV_WN32A(s->mv_cache[dir][scan8[k]], mv);
575 /* write back motion vectors */
576 fill_rectangle(h->cur_pic.motion_val[dir][b_xy],
577 part_width >> 2, part_height >> 2, h->b_stride,
578 pack16to32(mx, my), 4);
584 static av_always_inline void hl_decode_mb_idct_luma(const H264Context *h, H264SliceContext *sl,
585 int mb_type, const int *block_offset,
586 int linesize, uint8_t *dest_y)
589 if (!IS_INTRA4x4(mb_type)) {
590 for (i = 0; i < 16; i++)
591 if (sl->non_zero_count_cache[scan8[i]] || sl->mb[i * 16]) {
592 uint8_t *const ptr = dest_y + block_offset[i];
593 svq3_add_idct_c(ptr, sl->mb + i * 16, linesize,
594 sl->qscale, IS_INTRA(mb_type) ? 1 : 0);
599 static av_always_inline int dctcoef_get(int16_t *mb, int index)
601 return AV_RN16A(mb + index);
604 static av_always_inline void hl_decode_mb_predict_luma(SVQ3Context *s,
605 const H264Context *h,
606 H264SliceContext *sl,
608 const int *block_offset,
613 int qscale = sl->qscale;
615 if (IS_INTRA4x4(mb_type)) {
616 for (i = 0; i < 16; i++) {
617 uint8_t *const ptr = dest_y + block_offset[i];
618 const int dir = s->intra4x4_pred_mode_cache[scan8[i]];
622 if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
623 const int topright_avail = (s->topright_samples_available << i) & 0x8000;
624 av_assert2(s->mb_y || linesize <= block_offset[i]);
625 if (!topright_avail) {
626 tr = ptr[3 - linesize] * 0x01010101u;
627 topright = (uint8_t *)&tr;
629 topright = ptr + 4 - linesize;
633 s->hpc.pred4x4[dir](ptr, topright, linesize);
634 nnz = sl->non_zero_count_cache[scan8[i]];
636 svq3_add_idct_c(ptr, sl->mb + i * 16, linesize, qscale, 0);
640 s->hpc.pred16x16[s->intra16x16_pred_mode](dest_y, linesize);
641 svq3_luma_dc_dequant_idct_c(sl->mb, sl->mb_luma_dc[0], qscale);
645 static void hl_decode_mb(SVQ3Context *s, const H264Context *h, H264SliceContext *sl)
647 const int mb_x = s->mb_x;
648 const int mb_y = s->mb_y;
649 const int mb_xy = s->mb_xy;
650 const int mb_type = h->cur_pic.mb_type[mb_xy];
651 uint8_t *dest_y, *dest_cb, *dest_cr;
652 int linesize, uvlinesize;
654 const int *block_offset = &h->block_offset[0];
655 const int block_h = 16 >> h->chroma_y_shift;
657 dest_y = h->cur_pic.f->data[0] + (mb_x + mb_y * sl->linesize) * 16;
658 dest_cb = h->cur_pic.f->data[1] + mb_x * 8 + mb_y * sl->uvlinesize * block_h;
659 dest_cr = h->cur_pic.f->data[2] + mb_x * 8 + mb_y * sl->uvlinesize * block_h;
661 s->vdsp.prefetch(dest_y + (s->mb_x & 3) * 4 * sl->linesize + 64, sl->linesize, 4);
662 s->vdsp.prefetch(dest_cb + (s->mb_x & 7) * sl->uvlinesize + 64, dest_cr - dest_cb, 2);
664 h->list_counts[mb_xy] = sl->list_count;
666 linesize = sl->mb_linesize = sl->linesize;
667 uvlinesize = sl->mb_uvlinesize = sl->uvlinesize;
669 if (IS_INTRA(mb_type)) {
670 s->hpc.pred8x8[s->chroma_pred_mode](dest_cb, uvlinesize);
671 s->hpc.pred8x8[s->chroma_pred_mode](dest_cr, uvlinesize);
673 hl_decode_mb_predict_luma(s, h, sl, mb_type, block_offset, linesize, dest_y);
676 hl_decode_mb_idct_luma(h, sl, mb_type, block_offset, linesize, dest_y);
678 if (sl->cbp & 0x30) {
679 uint8_t *dest[2] = { dest_cb, dest_cr };
680 s->h264dsp.h264_chroma_dc_dequant_idct(sl->mb + 16 * 16 * 1,
681 h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][sl->chroma_qp[0]][0]);
682 s->h264dsp.h264_chroma_dc_dequant_idct(sl->mb + 16 * 16 * 2,
683 h->dequant4_coeff[IS_INTRA(mb_type) ? 2 : 5][sl->chroma_qp[1]][0]);
684 for (j = 1; j < 3; j++) {
685 for (i = j * 16; i < j * 16 + 4; i++)
686 if (sl->non_zero_count_cache[scan8[i]] || sl->mb[i * 16]) {
687 uint8_t *const ptr = dest[j - 1] + block_offset[i];
688 svq3_add_idct_c(ptr, sl->mb + i * 16,
689 uvlinesize, ff_h264_chroma_qp[0][sl->qscale + 12] - 12, 2);
695 static int svq3_decode_mb(SVQ3Context *s, unsigned int mb_type)
697 H264Context *h = &s->h;
698 H264SliceContext *sl = &h->slice_ctx[0];
699 int i, j, k, m, dir, mode;
703 const int mb_xy = s->mb_xy;
704 const int b_xy = 4 * s->mb_x + 4 * s->mb_y * h->b_stride;
706 s->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
707 s->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
708 s->topright_samples_available = 0xFFFF;
710 if (mb_type == 0) { /* SKIP */
711 if (h->pict_type == AV_PICTURE_TYPE_P ||
712 s->next_pic->mb_type[mb_xy] == -1) {
713 svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
716 if (h->pict_type == AV_PICTURE_TYPE_B)
717 svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
720 mb_type = MB_TYPE_SKIP;
722 mb_type = FFMIN(s->next_pic->mb_type[mb_xy], 6);
723 if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 0, 0) < 0)
725 if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 1, 1) < 0)
728 mb_type = MB_TYPE_16x16;
730 } else if (mb_type < 8) { /* INTER */
731 if (s->thirdpel_flag && s->halfpel_flag == !get_bits1(&h->gb))
732 mode = THIRDPEL_MODE;
733 else if (s->halfpel_flag &&
734 s->thirdpel_flag == !get_bits1(&h->gb))
740 /* note ref_cache should contain here:
748 for (m = 0; m < 2; m++) {
749 if (s->mb_x > 0 && s->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6] != -1) {
750 for (i = 0; i < 4; i++)
751 AV_COPY32(s->mv_cache[m][scan8[0] - 1 + i * 8],
752 h->cur_pic.motion_val[m][b_xy - 1 + i * h->b_stride]);
754 for (i = 0; i < 4; i++)
755 AV_ZERO32(s->mv_cache[m][scan8[0] - 1 + i * 8]);
758 memcpy(s->mv_cache[m][scan8[0] - 1 * 8],
759 h->cur_pic.motion_val[m][b_xy - h->b_stride],
760 4 * 2 * sizeof(int16_t));
761 memset(&s->ref_cache[m][scan8[0] - 1 * 8],
762 (s->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);
764 if (s->mb_x < h->mb_width - 1) {
765 AV_COPY32(s->mv_cache[m][scan8[0] + 4 - 1 * 8],
766 h->cur_pic.motion_val[m][b_xy - h->b_stride + 4]);
767 s->ref_cache[m][scan8[0] + 4 - 1 * 8] =
768 (s->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride + 1] + 6] == -1 ||
769 s->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1;
771 s->ref_cache[m][scan8[0] + 4 - 1 * 8] = PART_NOT_AVAILABLE;
773 AV_COPY32(s->mv_cache[m][scan8[0] - 1 - 1 * 8],
774 h->cur_pic.motion_val[m][b_xy - h->b_stride - 1]);
775 s->ref_cache[m][scan8[0] - 1 - 1 * 8] =
776 (s->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride - 1] + 3] == -1) ? PART_NOT_AVAILABLE : 1;
778 s->ref_cache[m][scan8[0] - 1 - 1 * 8] = PART_NOT_AVAILABLE;
780 memset(&s->ref_cache[m][scan8[0] - 1 * 8 - 1],
781 PART_NOT_AVAILABLE, 8);
783 if (h->pict_type != AV_PICTURE_TYPE_B)
787 /* decode motion vector(s) and form prediction(s) */
788 if (h->pict_type == AV_PICTURE_TYPE_P) {
789 if (svq3_mc_dir(s, mb_type - 1, mode, 0, 0) < 0)
791 } else { /* AV_PICTURE_TYPE_B */
793 if (svq3_mc_dir(s, 0, mode, 0, 0) < 0)
796 for (i = 0; i < 4; i++)
797 memset(h->cur_pic.motion_val[0][b_xy + i * h->b_stride],
798 0, 4 * 2 * sizeof(int16_t));
801 if (svq3_mc_dir(s, 0, mode, 1, mb_type == 3) < 0)
804 for (i = 0; i < 4; i++)
805 memset(h->cur_pic.motion_val[1][b_xy + i * h->b_stride],
806 0, 4 * 2 * sizeof(int16_t));
810 mb_type = MB_TYPE_16x16;
811 } else if (mb_type == 8 || mb_type == 33) { /* INTRA4x4 */
812 int8_t *i4x4 = s->intra4x4_pred_mode + h->mb2br_xy[s->mb_xy];
813 int8_t *i4x4_cache = s->intra4x4_pred_mode_cache;
815 memset(s->intra4x4_pred_mode_cache, -1, 8 * 5 * sizeof(int8_t));
819 for (i = 0; i < 4; i++)
820 s->intra4x4_pred_mode_cache[scan8[0] - 1 + i * 8] = s->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6 - i];
821 if (s->intra4x4_pred_mode_cache[scan8[0] - 1] == -1)
822 s->left_samples_available = 0x5F5F;
825 s->intra4x4_pred_mode_cache[4 + 8 * 0] = s->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 0];
826 s->intra4x4_pred_mode_cache[5 + 8 * 0] = s->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 1];
827 s->intra4x4_pred_mode_cache[6 + 8 * 0] = s->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 2];
828 s->intra4x4_pred_mode_cache[7 + 8 * 0] = s->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 3];
830 if (s->intra4x4_pred_mode_cache[4 + 8 * 0] == -1)
831 s->top_samples_available = 0x33FF;
834 /* decode prediction codes for luma blocks */
835 for (i = 0; i < 16; i += 2) {
836 vlc = svq3_get_ue_golomb(&h->gb);
839 av_log(h->avctx, AV_LOG_ERROR,
840 "luma prediction:%"PRIu32"\n", vlc);
844 left = &s->intra4x4_pred_mode_cache[scan8[i] - 1];
845 top = &s->intra4x4_pred_mode_cache[scan8[i] - 8];
847 left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
848 left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
850 if (left[1] == -1 || left[2] == -1) {
851 av_log(h->avctx, AV_LOG_ERROR, "weird prediction\n");
855 } else { /* mb_type == 33, DC_128_PRED block type */
856 for (i = 0; i < 4; i++)
857 memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_PRED, 4);
860 AV_COPY32(i4x4, i4x4_cache + 4 + 8 * 4);
861 i4x4[4] = i4x4_cache[7 + 8 * 3];
862 i4x4[5] = i4x4_cache[7 + 8 * 2];
863 i4x4[6] = i4x4_cache[7 + 8 * 1];
866 ff_h264_check_intra4x4_pred_mode(s->intra4x4_pred_mode_cache,
867 h->avctx, s->top_samples_available,
868 s->left_samples_available);
870 s->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
871 s->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
873 for (i = 0; i < 4; i++)
874 memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_128_PRED, 4);
876 s->top_samples_available = 0x33FF;
877 s->left_samples_available = 0x5F5F;
880 mb_type = MB_TYPE_INTRA4x4;
881 } else { /* INTRA16x16 */
882 dir = ff_h264_i_mb_type_info[mb_type - 8].pred_mode;
883 dir = (dir >> 1) ^ 3 * (dir & 1) ^ 1;
885 if ((s->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(h->avctx, s->top_samples_available,
886 s->left_samples_available, dir, 0)) < 0) {
887 av_log(h->avctx, AV_LOG_ERROR, "ff_h264_check_intra_pred_mode < 0\n");
888 return s->intra16x16_pred_mode;
891 cbp = ff_h264_i_mb_type_info[mb_type - 8].cbp;
892 mb_type = MB_TYPE_INTRA16x16;
895 if (!IS_INTER(mb_type) && h->pict_type != AV_PICTURE_TYPE_I) {
896 for (i = 0; i < 4; i++)
897 memset(h->cur_pic.motion_val[0][b_xy + i * h->b_stride],
898 0, 4 * 2 * sizeof(int16_t));
899 if (h->pict_type == AV_PICTURE_TYPE_B) {
900 for (i = 0; i < 4; i++)
901 memset(h->cur_pic.motion_val[1][b_xy + i * h->b_stride],
902 0, 4 * 2 * sizeof(int16_t));
905 if (!IS_INTRA4x4(mb_type)) {
906 memset(s->intra4x4_pred_mode + h->mb2br_xy[mb_xy], DC_PRED, 8);
908 if (!IS_SKIP(mb_type) || h->pict_type == AV_PICTURE_TYPE_B) {
909 memset(sl->non_zero_count_cache + 8, 0, 14 * 8 * sizeof(uint8_t));
912 if (!IS_INTRA16x16(mb_type) &&
913 (!IS_SKIP(mb_type) || h->pict_type == AV_PICTURE_TYPE_B)) {
914 if ((vlc = svq3_get_ue_golomb(&h->gb)) >= 48U){
915 av_log(h->avctx, AV_LOG_ERROR, "cbp_vlc=%"PRIu32"\n", vlc);
919 cbp = IS_INTRA(mb_type) ? ff_h264_golomb_to_intra4x4_cbp[vlc]
920 : ff_h264_golomb_to_inter_cbp[vlc];
922 if (IS_INTRA16x16(mb_type) ||
923 (h->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
924 sl->qscale += svq3_get_se_golomb(&h->gb);
926 if (sl->qscale > 31u) {
927 av_log(h->avctx, AV_LOG_ERROR, "qscale:%d\n", sl->qscale);
931 if (IS_INTRA16x16(mb_type)) {
932 AV_ZERO128(sl->mb_luma_dc[0] + 0);
933 AV_ZERO128(sl->mb_luma_dc[0] + 8);
934 if (svq3_decode_block(&h->gb, sl->mb_luma_dc[0], 0, 1)) {
935 av_log(h->avctx, AV_LOG_ERROR,
936 "error while decoding intra luma dc\n");
942 const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
943 const int type = ((sl->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
945 for (i = 0; i < 4; i++)
946 if ((cbp & (1 << i))) {
947 for (j = 0; j < 4; j++) {
948 k = index ? (1 * (j & 1) + 2 * (i & 1) +
949 2 * (j & 2) + 4 * (i & 2))
951 sl->non_zero_count_cache[scan8[k]] = 1;
953 if (svq3_decode_block(&h->gb, &sl->mb[16 * k], index, type)) {
954 av_log(h->avctx, AV_LOG_ERROR,
955 "error while decoding block\n");
962 for (i = 1; i < 3; ++i)
963 if (svq3_decode_block(&h->gb, &sl->mb[16 * 16 * i], 0, 3)) {
964 av_log(h->avctx, AV_LOG_ERROR,
965 "error while decoding chroma dc block\n");
970 for (i = 1; i < 3; i++) {
971 for (j = 0; j < 4; j++) {
973 sl->non_zero_count_cache[scan8[k]] = 1;
975 if (svq3_decode_block(&h->gb, &sl->mb[16 * k], 1, 1)) {
976 av_log(h->avctx, AV_LOG_ERROR,
977 "error while decoding chroma ac block\n");
987 h->cur_pic.mb_type[mb_xy] = mb_type;
989 if (IS_INTRA(mb_type))
990 s->chroma_pred_mode = ff_h264_check_intra_pred_mode(h->avctx, s->top_samples_available,
991 s->left_samples_available, DC_PRED8x8, 1);
996 static int svq3_decode_slice_header(AVCodecContext *avctx)
998 SVQ3Context *s = avctx->priv_data;
999 H264Context *h = &s->h;
1000 H264SliceContext *sl = &h->slice_ctx[0];
1001 const int mb_xy = s->mb_xy;
1005 header = get_bits(&s->gb, 8);
1007 if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
1009 av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
1012 int slice_bits, slice_bytes, slice_length;
1013 int length = header >> 5 & 3;
1015 slice_length = show_bits(&s->gb, 8 * length);
1016 slice_bits = slice_length * 8;
1017 slice_bytes = slice_length + length - 1;
1019 if (slice_bytes > get_bits_left(&s->gb)) {
1020 av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
1024 skip_bits(&s->gb, 8);
1026 av_fast_malloc(&s->slice_buf, &s->slice_size, slice_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
1028 return AVERROR(ENOMEM);
1030 memcpy(s->slice_buf, s->gb.buffer + s->gb.index / 8, slice_bytes);
1032 init_get_bits(&h->gb, s->slice_buf, slice_bits);
1034 if (s->watermark_key) {
1035 uint32_t header = AV_RL32(&h->gb.buffer[1]);
1036 AV_WL32(&h->gb.buffer[1], header ^ s->watermark_key);
1039 memmove(s->slice_buf, &s->slice_buf[slice_length], length - 1);
1041 skip_bits_long(&s->gb, slice_bytes * 8);
1044 if ((slice_id = svq3_get_ue_golomb(&h->gb)) >= 3) {
1045 av_log(h->avctx, AV_LOG_ERROR, "illegal slice type %u \n", slice_id);
1049 sl->slice_type = ff_h264_golomb_to_pict_type[slice_id];
1051 if ((header & 0x9F) == 2) {
1052 i = (h->mb_num < 64) ? 6 : (1 + av_log2(h->mb_num - 1));
1053 sl->mb_skip_run = get_bits(&h->gb, i) -
1054 (s->mb_y * h->mb_width + s->mb_x);
1057 sl->mb_skip_run = 0;
1060 sl->slice_num = get_bits(&h->gb, 8);
1061 sl->qscale = get_bits(&h->gb, 5);
1062 s->adaptive_quant = get_bits1(&h->gb);
1064 /* unknown fields */
1067 if (s->has_watermark)
1071 skip_bits(&h->gb, 2);
1073 if (skip_1stop_8data_bits(&h->gb) < 0)
1074 return AVERROR_INVALIDDATA;
1076 /* reset intra predictors and invalidate motion vector references */
1078 memset(s->intra4x4_pred_mode + h->mb2br_xy[mb_xy - 1] + 3,
1079 -1, 4 * sizeof(int8_t));
1080 memset(s->intra4x4_pred_mode + h->mb2br_xy[mb_xy - s->mb_x],
1081 -1, 8 * sizeof(int8_t) * s->mb_x);
1084 memset(s->intra4x4_pred_mode + h->mb2br_xy[mb_xy - h->mb_stride],
1085 -1, 8 * sizeof(int8_t) * (h->mb_width - s->mb_x));
1088 s->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride - 1] + 3] = -1;
1094 static av_cold int svq3_decode_init(AVCodecContext *avctx)
1096 SVQ3Context *s = avctx->priv_data;
1097 H264Context *h = &s->h;
1098 H264SliceContext *sl;
1100 unsigned char *extradata;
1101 unsigned char *extradata_end;
1103 int marker_found = 0;
1106 s->cur_pic = av_mallocz(sizeof(*s->cur_pic));
1107 s->last_pic = av_mallocz(sizeof(*s->last_pic));
1108 s->next_pic = av_mallocz(sizeof(*s->next_pic));
1109 if (!s->next_pic || !s->last_pic || !s->cur_pic) {
1110 ret = AVERROR(ENOMEM);
1114 s->cur_pic->f = av_frame_alloc();
1115 s->last_pic->f = av_frame_alloc();
1116 s->next_pic->f = av_frame_alloc();
1117 if (!s->cur_pic->f || !s->last_pic->f || !s->next_pic->f)
1118 return AVERROR(ENOMEM);
1120 if ((ret = ff_h264_decode_init(avctx)) < 0)
1123 // we will overwrite it later during decoding
1124 av_frame_free(&h->cur_pic.f);
1126 av_frame_free(&h->last_pic_for_ec.f);
1128 ff_h264dsp_init(&s->h264dsp, 8, 1);
1129 av_assert0(h->sps.bit_depth_chroma == 0);
1130 ff_h264_pred_init(&s->hpc, AV_CODEC_ID_SVQ3, 8, 1);
1131 ff_videodsp_init(&s->vdsp, 8);
1133 memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
1134 memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
1136 avctx->bits_per_raw_sample = 8;
1137 h->sps.bit_depth_luma = 8;
1138 h->chroma_format_idc = 1;
1140 ff_hpeldsp_init(&s->hdsp, avctx->flags);
1141 ff_tpeldsp_init(&s->tdsp);
1145 h->flags = avctx->flags;
1147 h->sps.chroma_format_idc = 1;
1148 h->picture_structure = PICT_FRAME;
1149 avctx->pix_fmt = AV_PIX_FMT_YUVJ420P;
1150 avctx->color_range = AVCOL_RANGE_JPEG;
1152 h->slice_ctx[0].chroma_qp[0] = h->slice_ctx[0].chroma_qp[1] = 4;
1153 h->chroma_x_shift = h->chroma_y_shift = 1;
1155 s->halfpel_flag = 1;
1156 s->thirdpel_flag = 1;
1157 s->has_watermark = 0;
1159 /* prowl for the "SEQH" marker in the extradata */
1160 extradata = (unsigned char *)avctx->extradata;
1161 extradata_end = avctx->extradata + avctx->extradata_size;
1163 for (m = 0; m + 8 < avctx->extradata_size; m++) {
1164 if (!memcmp(extradata, "SEQH", 4)) {
1172 /* if a match was found, parse the extra data */
1175 int frame_size_code;
1176 int unk0, unk1, unk2, unk3, unk4;
1178 size = AV_RB32(&extradata[4]);
1179 if (size > extradata_end - extradata - 8) {
1180 ret = AVERROR_INVALIDDATA;
1183 init_get_bits(&gb, extradata + 8, size * 8);
1185 /* 'frame size code' and optional 'width, height' */
1186 frame_size_code = get_bits(&gb, 3);
1187 switch (frame_size_code) {
1190 avctx->height = 120;
1198 avctx->height = 144;
1202 avctx->height = 288;
1206 avctx->height = 576;
1210 avctx->height = 180;
1214 avctx->height = 240;
1217 avctx->width = get_bits(&gb, 12);
1218 avctx->height = get_bits(&gb, 12);
1222 s->halfpel_flag = get_bits1(&gb);
1223 s->thirdpel_flag = get_bits1(&gb);
1225 /* unknown fields */
1226 unk0 = get_bits1(&gb);
1227 unk1 = get_bits1(&gb);
1228 unk2 = get_bits1(&gb);
1229 unk3 = get_bits1(&gb);
1231 h->low_delay = get_bits1(&gb);
1234 unk4 = get_bits1(&gb);
1236 av_log(avctx, AV_LOG_DEBUG, "Unknown fields %d %d %d %d %d\n",
1237 unk0, unk1, unk2, unk3, unk4);
1239 if (skip_1stop_8data_bits(&gb) < 0) {
1240 ret = AVERROR_INVALIDDATA;
1244 s->has_watermark = get_bits1(&gb);
1245 avctx->has_b_frames = !h->low_delay;
1246 if (s->has_watermark) {
1248 unsigned watermark_width = svq3_get_ue_golomb(&gb);
1249 unsigned watermark_height = svq3_get_ue_golomb(&gb);
1250 int u1 = svq3_get_ue_golomb(&gb);
1251 int u2 = get_bits(&gb, 8);
1252 int u3 = get_bits(&gb, 2);
1253 int u4 = svq3_get_ue_golomb(&gb);
1254 unsigned long buf_len = watermark_width *
1255 watermark_height * 4;
1256 int offset = get_bits_count(&gb) + 7 >> 3;
1259 if (watermark_height <= 0 ||
1260 (uint64_t)watermark_width * 4 > UINT_MAX / watermark_height) {
1265 buf = av_malloc(buf_len);
1267 ret = AVERROR(ENOMEM);
1270 av_log(avctx, AV_LOG_DEBUG, "watermark size: %ux%u\n",
1271 watermark_width, watermark_height);
1272 av_log(avctx, AV_LOG_DEBUG,
1273 "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n",
1274 u1, u2, u3, u4, offset);
1275 if (uncompress(buf, &buf_len, extradata + 8 + offset,
1276 size - offset) != Z_OK) {
1277 av_log(avctx, AV_LOG_ERROR,
1278 "could not uncompress watermark logo\n");
1283 s->watermark_key = ff_svq1_packet_checksum(buf, buf_len, 0);
1284 s->watermark_key = s->watermark_key << 16 | s->watermark_key;
1285 av_log(avctx, AV_LOG_DEBUG,
1286 "watermark key %#"PRIx32"\n", s->watermark_key);
1289 av_log(avctx, AV_LOG_ERROR,
1290 "this svq3 file contains watermark which need zlib support compiled in\n");
1297 h->width = avctx->width;
1298 h->height = avctx->height;
1299 h->mb_width = (h->width + 15) / 16;
1300 h->mb_height = (h->height + 15) / 16;
1301 h->mb_stride = h->mb_width + 1;
1302 h->mb_num = h->mb_width * h->mb_height;
1303 h->b_stride = 4 * h->mb_width;
1304 s->h_edge_pos = h->mb_width * 16;
1305 s->v_edge_pos = h->mb_height * 16;
1307 if ((ret = ff_h264_alloc_tables(h)) < 0) {
1308 av_log(avctx, AV_LOG_ERROR, "svq3 memory allocation failed\n");
1314 svq3_decode_end(avctx);
1318 static void free_picture(AVCodecContext *avctx, H264Picture *pic)
1321 for (i = 0; i < 2; i++) {
1322 av_buffer_unref(&pic->motion_val_buf[i]);
1323 av_buffer_unref(&pic->ref_index_buf[i]);
1325 av_buffer_unref(&pic->mb_type_buf);
1327 av_frame_unref(pic->f);
1330 static int get_buffer(AVCodecContext *avctx, H264Picture *pic)
1332 SVQ3Context *s = avctx->priv_data;
1333 H264Context *h = &s->h;
1334 H264SliceContext *sl = &h->slice_ctx[0];
1335 const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
1336 const int mb_array_size = h->mb_stride * h->mb_height;
1337 const int b4_stride = h->mb_width * 4 + 1;
1338 const int b4_array_size = b4_stride * h->mb_height * 4;
1341 if (!pic->motion_val_buf[0]) {
1344 pic->mb_type_buf = av_buffer_allocz((big_mb_num + h->mb_stride) * sizeof(uint32_t));
1345 if (!pic->mb_type_buf)
1346 return AVERROR(ENOMEM);
1347 pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
1349 for (i = 0; i < 2; i++) {
1350 pic->motion_val_buf[i] = av_buffer_allocz(2 * (b4_array_size + 4) * sizeof(int16_t));
1351 pic->ref_index_buf[i] = av_buffer_allocz(4 * mb_array_size);
1352 if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i]) {
1353 ret = AVERROR(ENOMEM);
1357 pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
1358 pic->ref_index[i] = pic->ref_index_buf[i]->data;
1361 pic->reference = !(h->pict_type == AV_PICTURE_TYPE_B);
1363 ret = ff_get_buffer(avctx, pic->f,
1364 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
1368 if (!s->edge_emu_buffer) {
1369 s->edge_emu_buffer = av_mallocz_array(pic->f->linesize[0], 17);
1370 if (!s->edge_emu_buffer)
1371 return AVERROR(ENOMEM);
1374 sl->linesize = pic->f->linesize[0];
1375 sl->uvlinesize = pic->f->linesize[1];
1379 free_picture(avctx, pic);
1383 static int svq3_decode_frame(AVCodecContext *avctx, void *data,
1384 int *got_frame, AVPacket *avpkt)
1386 SVQ3Context *s = avctx->priv_data;
1387 H264Context *h = &s->h;
1388 H264SliceContext *sl = &h->slice_ctx[0];
1389 int buf_size = avpkt->size;
1394 /* special case for last picture */
1395 if (buf_size == 0) {
1396 if (s->next_pic->f->data[0] && !h->low_delay && !s->last_frame_output) {
1397 ret = av_frame_ref(data, s->next_pic->f);
1400 s->last_frame_output = 1;
1406 sl->mb_x = sl->mb_y = sl->mb_xy = 0;
1408 if (s->watermark_key) {
1409 av_fast_padded_malloc(&s->buf, &s->buf_size, buf_size);
1411 return AVERROR(ENOMEM);
1412 memcpy(s->buf, avpkt->data, buf_size);
1418 ret = init_get_bits(&s->gb, buf, 8 * buf_size);
1422 if (svq3_decode_slice_header(avctx))
1425 h->pict_type = sl->slice_type;
1427 if (h->pict_type != AV_PICTURE_TYPE_B)
1428 FFSWAP(H264Picture*, s->next_pic, s->last_pic);
1430 av_frame_unref(s->cur_pic->f);
1432 /* for skipping the frame */
1433 s->cur_pic->f->pict_type = h->pict_type;
1434 s->cur_pic->f->key_frame = (h->pict_type == AV_PICTURE_TYPE_I);
1436 ret = get_buffer(avctx, s->cur_pic);
1440 h->cur_pic_ptr = s->cur_pic;
1441 h->cur_pic = *s->cur_pic;
1443 for (i = 0; i < 16; i++) {
1444 h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * sl->linesize * ((scan8[i] - scan8[0]) >> 3);
1445 h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * sl->linesize * ((scan8[i] - scan8[0]) >> 3);
1447 for (i = 0; i < 16; i++) {
1448 h->block_offset[16 + i] =
1449 h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * sl->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
1450 h->block_offset[48 + 16 + i] =
1451 h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * sl->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
1454 if (h->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 (h->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(h->avctx, AV_LOG_DEBUG,
1484 "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
1485 av_get_picture_type_char(h->pict_type),
1486 s->halfpel_flag, s->thirdpel_flag,
1487 s->adaptive_quant, h->slice_ctx[0].qscale, sl->slice_num);
1489 if (avctx->skip_frame >= AVDISCARD_NONREF && h->pict_type == AV_PICTURE_TYPE_B ||
1490 avctx->skip_frame >= AVDISCARD_NONKEY && h->pict_type != AV_PICTURE_TYPE_I ||
1491 avctx->skip_frame >= AVDISCARD_ALL)
1494 if (s->next_p_frame_damaged) {
1495 if (h->pict_type == AV_PICTURE_TYPE_B)
1498 s->next_p_frame_damaged = 0;
1501 if (h->pict_type == AV_PICTURE_TYPE_B) {
1502 h->frame_num_offset = sl->slice_num - h->prev_frame_num;
1504 if (h->frame_num_offset < 0)
1505 h->frame_num_offset += 256;
1506 if (h->frame_num_offset == 0 ||
1507 h->frame_num_offset >= h->prev_frame_num_offset) {
1508 av_log(h->avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
1512 h->prev_frame_num = h->frame_num;
1513 h->frame_num = sl->slice_num;
1514 h->prev_frame_num_offset = h->frame_num - h->prev_frame_num;
1516 if (h->prev_frame_num_offset < 0)
1517 h->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 < h->mb_height; s->mb_y++) {
1532 for (s->mb_x = 0; s->mb_x < h->mb_width; s->mb_x++) {
1534 s->mb_xy = s->mb_x + s->mb_y * h->mb_stride;
1536 if ((get_bits_left(&h->gb)) <= 7) {
1537 if (((get_bits_count(&h->gb) & 7) == 0 ||
1538 show_bits(&h->gb, get_bits_left(&h->gb) & 7) == 0)) {
1540 if (svq3_decode_slice_header(avctx))
1543 /* TODO: support s->mb_skip_run */
1546 mb_type = svq3_get_ue_golomb(&h->gb);
1548 if (h->pict_type == AV_PICTURE_TYPE_I)
1550 else if (h->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4)
1552 if (mb_type > 33 || svq3_decode_mb(s, mb_type)) {
1553 av_log(h->avctx, AV_LOG_ERROR,
1554 "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
1558 if (mb_type != 0 || sl->cbp)
1559 hl_decode_mb(s, h, &h->slice_ctx[0]);
1561 if (h->pict_type != AV_PICTURE_TYPE_B && !h->low_delay)
1562 h->cur_pic.mb_type[s->mb_x + s->mb_y * h->mb_stride] =
1563 (h->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
1566 ff_draw_horiz_band(avctx, s->cur_pic->f,
1567 s->last_pic->f->data[0] ? s->last_pic->f : NULL,
1568 16 * s->mb_y, 16, h->picture_structure, 0,
1572 left = buf_size*8 - get_bits_count(&h->gb);
1574 if (sl->mb_y != h->mb_height || sl->mb_x != h->mb_width) {
1575 av_log(avctx, AV_LOG_INFO, "frame num %d incomplete pic x %d y %d left %d\n", avctx->frame_number, sl->mb_y, sl->mb_x, left);
1576 //av_hex_dump(stderr, buf+buf_size-8, 8);
1580 av_log(avctx, AV_LOG_ERROR, "frame num %d left %d\n", avctx->frame_number, left);
1584 if (h->pict_type == AV_PICTURE_TYPE_B || h->low_delay)
1585 ret = av_frame_ref(data, s->cur_pic->f);
1586 else if (s->last_pic->f->data[0])
1587 ret = av_frame_ref(data, s->last_pic->f);
1591 /* Do not output the last pic after seeking. */
1592 if (s->last_pic->f->data[0] || h->low_delay)
1595 if (h->pict_type != AV_PICTURE_TYPE_B) {
1596 FFSWAP(H264Picture*, s->cur_pic, s->next_pic);
1598 av_frame_unref(s->cur_pic->f);
1604 static av_cold int svq3_decode_end(AVCodecContext *avctx)
1606 SVQ3Context *s = avctx->priv_data;
1607 H264Context *h = &s->h;
1609 free_picture(avctx, s->cur_pic);
1610 free_picture(avctx, s->next_pic);
1611 free_picture(avctx, s->last_pic);
1612 av_frame_free(&s->cur_pic->f);
1613 av_frame_free(&s->next_pic->f);
1614 av_frame_free(&s->last_pic->f);
1615 av_freep(&s->cur_pic);
1616 av_freep(&s->next_pic);
1617 av_freep(&s->last_pic);
1618 av_freep(&s->slice_buf);
1619 av_freep(&s->intra4x4_pred_mode);
1620 av_freep(&s->edge_emu_buffer);
1622 memset(&h->cur_pic, 0, sizeof(h->cur_pic));
1624 ff_h264_free_context(h);
1632 AVCodec ff_svq3_decoder = {
1634 .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
1635 .type = AVMEDIA_TYPE_VIDEO,
1636 .id = AV_CODEC_ID_SVQ3,
1637 .priv_data_size = sizeof(SVQ3Context),
1638 .init = svq3_decode_init,
1639 .close = svq3_decode_end,
1640 .decode = svq3_decode_frame,
1641 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND |
1644 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,