svq3: move the frame num variables to the SVQ3Context
[libav.git] / libavcodec / svq3.c
CommitLineData
8b82a956 1/*
2912e87a 2 * Copyright (c) 2003 The Libav Project
8b82a956 3 *
2912e87a 4 * This file is part of Libav.
b78e7197 5 *
2912e87a 6 * Libav is free software; you can redistribute it and/or
8b82a956
MN
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
b78e7197 9 * version 2.1 of the License, or (at your option) any later version.
8b82a956 10 *
2912e87a 11 * Libav is distributed in the hope that it will be useful,
8b82a956
MN
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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
2912e87a 17 * License along with Libav; if not, write to the Free Software
5509bffa 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
e5a389a1
DB
19 */
20
21/*
8b82a956
MN
22 * How to use this decoder:
23 * SVQ3 data is transported within Apple Quicktime files. Quicktime files
89a79364
MM
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
8b82a956
MN
30 * via the AVCodecContext's extradata[_size] field:
31 *
115329f1 32 * AVCodecContext.extradata = pointer to ImageDescription, first characters
89a79364 33 * are expected to be 'S', 'V', 'Q', and '3', NOT the 4-byte atom length
115329f1
DB
34 * AVCodecContext.extradata_size = size of ImageDescription atom memory
35 * buffer (which will be the same as the ImageDescription atom size field
89a79364
MM
36 * from the QT file, minus 4 bytes since the length is missing)
37 *
38 * You will know you have these parameters passed correctly when the decoder
39 * correctly decodes this file:
f0a41afd 40 * http://samples.libav.org/V-codecs/SVQ3/Vertical400kbit.sorenson3.mov
8b82a956 41 */
bd8ac882 42
cc8163e1
DB
43#include <inttypes.h>
44
bd8ac882 45#include "libavutil/attributes.h"
903d58f6 46#include "internal.h"
903d58f6 47#include "avcodec.h"
e0c16e4e 48#include "mpegutils.h"
903d58f6 49#include "h264.h"
188d3c51 50#include "h264_mvpred.h"
a7da517f 51#include "h264data.h"
903d58f6 52#include "golomb.h"
2f6bc5f7 53#include "hpeldsp.h"
f4d581cd 54#include "mathops.h"
903d58f6 55#include "rectangle.h"
57f09608 56#include "tpeldsp.h"
903d58f6 57
b250f9c6 58#if CONFIG_ZLIB
c4864924
BC
59#include <zlib.h>
60#endif
61
2be3fe39
DB
62#include "svq1.h"
63
8b82a956 64/**
ba87f080 65 * @file
8b82a956
MN
66 * svq3 decoder.
67 */
68
7f9f771e 69typedef struct SVQ3Context {
8dfc6d1f 70 H264Context h;
c73fb9ef
AK
71
72 H264DSPContext h264dsp;
73 H264PredContext hpc;
2f6bc5f7 74 HpelDSPContext hdsp;
57f09608 75 TpelDSPContext tdsp;
c73fb9ef
AK
76 VideoDSPContext vdsp;
77
9b749c82
VG
78 H264Picture *cur_pic;
79 H264Picture *next_pic;
80 H264Picture *last_pic;
1098f5c0
LB
81 GetBitContext gb;
82 uint8_t *slice_buf;
83 int slice_size;
8dfc6d1f
BC
84 int halfpel_flag;
85 int thirdpel_flag;
86 int unknown_flag;
8dfc6d1f 87 uint32_t watermark_key;
2c541554
AK
88 int adaptive_quant;
89 int next_p_frame_damaged;
90 int h_edge_pos;
91 int v_edge_pos;
92 int last_frame_output;
939b3883
AK
93 int slice_num;
94 int qscale;
95 int cbp;
ea6ab02a
AK
96 int frame_num;
97 int frame_num_offset;
98 int prev_frame_num_offset;
99 int prev_frame_num;
1877712c 100
5a5db90e
AK
101 enum AVPictureType pict_type;
102
1877712c
AK
103 int mb_x, mb_y;
104 int mb_xy;
12f13ecb
AK
105 int mb_width, mb_height;
106 int mb_stride, mb_num;
107 int b_stride;
527bf5f7 108
549fc772
AK
109 uint32_t *mb2br_xy;
110
527bf5f7
AK
111 int chroma_pred_mode;
112 int intra16x16_pred_mode;
113
114 int8_t intra4x4_pred_mode_cache[5 * 8];
115 int8_t (*intra4x4_pred_mode);
116
117 unsigned int top_samples_available;
118 unsigned int topright_samples_available;
119 unsigned int left_samples_available;
8eecae77
AK
120
121 uint8_t *edge_emu_buffer;
99dde603
AK
122
123 DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5 * 8][2];
124 DECLARE_ALIGNED(8, int8_t, ref_cache)[2][5 * 8];
939b3883
AK
125 DECLARE_ALIGNED(16, int16_t, mb)[16 * 48 * 2];
126 DECLARE_ALIGNED(16, int16_t, mb_luma_dc)[3][16 * 2];
127 DECLARE_ALIGNED(8, uint8_t, non_zero_count_cache)[15 * 8];
ad9d3384 128 uint32_t dequant4_coeff[QP_MAX_NUM + 1][16];
8dfc6d1f
BC
129} SVQ3Context;
130
115329f1
DB
131#define FULLPEL_MODE 1
132#define HALFPEL_MODE 2
94d44f45 133#define THIRDPEL_MODE 3
2e26c8d2 134#define PREDICT_MODE 4
115329f1 135
f7a8c179 136/* dual scan (from some older h264 draft)
a7d2861d
DB
137 * o-->o-->o o
138 * | /|
139 * o o o / o
140 * | / | |/ |
141 * o o o o
142 * /
143 * o-->o-->o-->o
144 */
76de302d 145static const uint8_t svq3_scan[16] = {
a7d2861d
DB
146 0 + 0 * 4, 1 + 0 * 4, 2 + 0 * 4, 2 + 1 * 4,
147 2 + 2 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4,
148 0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 1 + 2 * 4,
149 0 + 3 * 4, 1 + 3 * 4, 2 + 3 * 4, 3 + 3 * 4,
8b82a956
MN
150};
151
c4e43560
DB
152static const uint8_t luma_dc_zigzag_scan[16] = {
153 0 * 16 + 0 * 64, 1 * 16 + 0 * 64, 2 * 16 + 0 * 64, 0 * 16 + 2 * 64,
154 3 * 16 + 0 * 64, 0 * 16 + 1 * 64, 1 * 16 + 1 * 64, 2 * 16 + 1 * 64,
155 1 * 16 + 2 * 64, 2 * 16 + 2 * 64, 3 * 16 + 2 * 64, 0 * 16 + 3 * 64,
156 3 * 16 + 1 * 64, 1 * 16 + 3 * 64, 2 * 16 + 3 * 64, 3 * 16 + 3 * 64,
157};
158
8b82a956 159static const uint8_t svq3_pred_0[25][2] = {
76de302d
DB
160 { 0, 0 },
161 { 1, 0 }, { 0, 1 },
162 { 0, 2 }, { 1, 1 }, { 2, 0 },
163 { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
164 { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
165 { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
166 { 2, 4 }, { 3, 3 }, { 4, 2 },
167 { 4, 3 }, { 3, 4 },
168 { 4, 4 }
8b82a956
MN
169};
170
171static const int8_t svq3_pred_1[6][6][5] = {
a7d2861d
DB
172 { { 2, -1, -1, -1, -1 }, { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 },
173 { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, { 1, 2, -1, -1, -1 } },
174 { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 },
175 { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } },
176 { { 2, 0, -1, -1, -1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 },
177 { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } },
178 { { 2, 0, -1, -1, -1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 },
179 { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } },
180 { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 },
181 { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } },
182 { { 0, 2, -1, -1, -1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 },
183 { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } },
8b82a956
MN
184};
185
a7d2861d
DB
186static const struct {
187 uint8_t run;
188 uint8_t level;
189} svq3_dct_tables[2][16] = {
76de302d
DB
190 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
191 { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
192 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
193 { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
8b82a956
MN
194};
195
196static const uint32_t svq3_dequant_coeff[32] = {
a7d2861d
DB
197 3881, 4351, 4890, 5481, 6154, 6914, 7761, 8718,
198 9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873,
199 24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683,
200 61694, 68745, 77615, 89113, 100253, 109366, 126635, 141533
8b82a956
MN
201};
202
15b0517d 203static void svq3_luma_dc_dequant_idct_c(int16_t *output, int16_t *input, int qp)
a7d2861d 204{
76de302d 205 const int qmul = svq3_dequant_coeff[qp];
8b82a956
MN
206#define stride 16
207 int i;
208 int temp[16];
a7d2861d
DB
209 static const uint8_t x_offset[4] = { 0, 1 * stride, 4 * stride, 5 * stride };
210
211 for (i = 0; i < 4; i++) {
212 const int z0 = 13 * (input[4 * i + 0] + input[4 * i + 2]);
213 const int z1 = 13 * (input[4 * i + 0] - input[4 * i + 2]);
214 const int z2 = 7 * input[4 * i + 1] - 17 * input[4 * i + 3];
215 const int z3 = 17 * input[4 * i + 1] + 7 * input[4 * i + 3];
216
217 temp[4 * i + 0] = z0 + z3;
218 temp[4 * i + 1] = z1 + z2;
219 temp[4 * i + 2] = z1 - z2;
220 temp[4 * i + 3] = z0 - z3;
8b82a956
MN
221 }
222
a7d2861d
DB
223 for (i = 0; i < 4; i++) {
224 const int offset = x_offset[i];
225 const int z0 = 13 * (temp[4 * 0 + i] + temp[4 * 2 + i]);
226 const int z1 = 13 * (temp[4 * 0 + i] - temp[4 * 2 + i]);
227 const int z2 = 7 * temp[4 * 1 + i] - 17 * temp[4 * 3 + i];
228 const int z3 = 17 * temp[4 * 1 + i] + 7 * temp[4 * 3 + i];
229
af1ede06
DB
230 output[stride * 0 + offset] = (z0 + z3) * qmul + 0x80000 >> 20;
231 output[stride * 2 + offset] = (z1 + z2) * qmul + 0x80000 >> 20;
232 output[stride * 8 + offset] = (z1 - z2) * qmul + 0x80000 >> 20;
233 output[stride * 10 + offset] = (z0 - z3) * qmul + 0x80000 >> 20;
8b82a956
MN
234 }
235}
236#undef stride
237
15b0517d
AK
238static void svq3_add_idct_c(uint8_t *dst, int16_t *block,
239 int stride, int qp, int dc)
7f8205da 240{
76de302d 241 const int qmul = svq3_dequant_coeff[qp];
8b82a956 242 int i;
8b82a956
MN
243
244 if (dc) {
af1ede06
DB
245 dc = 13 * 13 * (dc == 1 ? 1538 * block[0]
246 : qmul * (block[0] >> 3) / 2);
8b82a956
MN
247 block[0] = 0;
248 }
249
76de302d 250 for (i = 0; i < 4; i++) {
a7d2861d
DB
251 const int z0 = 13 * (block[0 + 4 * i] + block[2 + 4 * i]);
252 const int z1 = 13 * (block[0 + 4 * i] - block[2 + 4 * i]);
253 const int z2 = 7 * block[1 + 4 * i] - 17 * block[3 + 4 * i];
254 const int z3 = 17 * block[1 + 4 * i] + 7 * block[3 + 4 * i];
255
256 block[0 + 4 * i] = z0 + z3;
257 block[1 + 4 * i] = z1 + z2;
258 block[2 + 4 * i] = z1 - z2;
259 block[3 + 4 * i] = z0 - z3;
8b82a956
MN
260 }
261
76de302d 262 for (i = 0; i < 4; i++) {
a7d2861d
DB
263 const int z0 = 13 * (block[i + 4 * 0] + block[i + 4 * 2]);
264 const int z1 = 13 * (block[i + 4 * 0] - block[i + 4 * 2]);
265 const int z2 = 7 * block[i + 4 * 1] - 17 * block[i + 4 * 3];
266 const int z3 = 17 * block[i + 4 * 1] + 7 * block[i + 4 * 3];
76de302d
DB
267 const int rr = (dc + 0x80000);
268
af1ede06
DB
269 dst[i + stride * 0] = av_clip_uint8(dst[i + stride * 0] + ((z0 + z3) * qmul + rr >> 20));
270 dst[i + stride * 1] = av_clip_uint8(dst[i + stride * 1] + ((z1 + z2) * qmul + rr >> 20));
271 dst[i + stride * 2] = av_clip_uint8(dst[i + stride * 2] + ((z1 - z2) * qmul + rr >> 20));
272 dst[i + stride * 3] = av_clip_uint8(dst[i + stride * 3] + ((z0 - z3) * qmul + rr >> 20));
8b82a956 273 }
62844c3f
RB
274
275 memset(block, 0, 16 * sizeof(int16_t));
8b82a956
MN
276}
277
88bd7fdc 278static inline int svq3_decode_block(GetBitContext *gb, int16_t *block,
7f8205da
DB
279 int index, const int type)
280{
f4d581cd 281 static const uint8_t *const scan_patterns[4] = {
a7da517f 282 luma_dc_zigzag_scan, ff_zigzag_scan, svq3_scan, ff_h264_chroma_dc_scan
f4d581cd 283 };
8b82a956 284
9a2e7911
JG
285 int run, level, limit;
286 unsigned vlc;
af1ede06 287 const int intra = 3 * type >> 2;
76de302d 288 const uint8_t *const scan = scan_patterns[type];
8b82a956 289
76de302d
DB
290 for (limit = (16 >> intra); index < 16; index = limit, limit += 8) {
291 for (; (vlc = svq3_get_ue_golomb(gb)) != 0; index++) {
9a2e7911
JG
292 int sign = (vlc & 1) ? 0 : -1;
293 vlc = vlc + 1 >> 1;
a7d2861d
DB
294
295 if (type == 3) {
296 if (vlc < 3) {
297 run = 0;
298 level = vlc;
299 } else if (vlc < 4) {
300 run = 1;
301 level = 1;
302 } else {
af1ede06
DB
303 run = vlc & 0x3;
304 level = (vlc + 9 >> 2) - run;
a7d2861d
DB
305 }
306 } else {
307 if (vlc < 16) {
308 run = svq3_dct_tables[intra][vlc].run;
309 level = svq3_dct_tables[intra][vlc].level;
310 } else if (intra) {
af1ede06 311 run = vlc & 0x7;
a7d2861d
DB
312 level = (vlc >> 3) +
313 ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
314 } else {
af1ede06 315 run = vlc & 0xF;
a7d2861d
DB
316 level = (vlc >> 4) +
317 ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
318 }
319 }
8b82a956 320
a7d2861d
DB
321 if ((index += run) >= limit)
322 return -1;
323
324 block[scan[index]] = (level ^ sign) - sign;
76de302d 325 }
8b82a956 326
76de302d
DB
327 if (type != 2) {
328 break;
329 }
8b82a956 330 }
8b82a956 331
76de302d 332 return 0;
8b82a956
MN
333}
334
89a13998 335static av_always_inline int
99dde603 336svq3_fetch_diagonal_mv(const SVQ3Context *s, const int16_t **C,
89a13998
AK
337 int i, int list, int part_width)
338{
99dde603 339 const int topright_ref = s->ref_cache[list][i - 8 + part_width];
89a13998
AK
340
341 if (topright_ref != PART_NOT_AVAILABLE) {
99dde603 342 *C = s->mv_cache[list][i - 8 + part_width];
89a13998
AK
343 return topright_ref;
344 } else {
99dde603
AK
345 *C = s->mv_cache[list][i - 8 - 1];
346 return s->ref_cache[list][i - 8 - 1];
89a13998
AK
347 }
348}
349
350/**
351 * Get the predicted MV.
352 * @param n the block index
353 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
354 * @param mx the x component of the predicted motion vector
355 * @param my the y component of the predicted motion vector
356 */
99dde603 357static av_always_inline void svq3_pred_motion(const SVQ3Context *s, int n,
89a13998
AK
358 int part_width, int list,
359 int ref, int *const mx, int *const my)
360{
361 const int index8 = scan8[n];
99dde603
AK
362 const int top_ref = s->ref_cache[list][index8 - 8];
363 const int left_ref = s->ref_cache[list][index8 - 1];
364 const int16_t *const A = s->mv_cache[list][index8 - 1];
365 const int16_t *const B = s->mv_cache[list][index8 - 8];
89a13998
AK
366 const int16_t *C;
367 int diagonal_ref, match_count;
368
369/* mv_cache
370 * B . . A T T T T
371 * U . . L . . , .
372 * U . . L . . . .
373 * U . . L . . , .
374 * . . . L . . . .
375 */
376
99dde603 377 diagonal_ref = svq3_fetch_diagonal_mv(s, &C, index8, list, part_width);
89a13998
AK
378 match_count = (diagonal_ref == ref) + (top_ref == ref) + (left_ref == ref);
379 if (match_count > 1) { //most common
380 *mx = mid_pred(A[0], B[0], C[0]);
381 *my = mid_pred(A[1], B[1], C[1]);
382 } else if (match_count == 1) {
383 if (left_ref == ref) {
384 *mx = A[0];
385 *my = A[1];
386 } else if (top_ref == ref) {
387 *mx = B[0];
388 *my = B[1];
389 } else {
390 *mx = C[0];
391 *my = C[1];
392 }
393 } else {
394 if (top_ref == PART_NOT_AVAILABLE &&
395 diagonal_ref == PART_NOT_AVAILABLE &&
396 left_ref != PART_NOT_AVAILABLE) {
397 *mx = A[0];
398 *my = A[1];
399 } else {
400 *mx = mid_pred(A[0], B[0], C[0]);
401 *my = mid_pred(A[1], B[1], C[1]);
402 }
403 }
404}
405
2c541554 406static inline void svq3_mc_dir_part(SVQ3Context *s,
7f8205da
DB
407 int x, int y, int width, int height,
408 int mx, int my, int dxy,
409 int thirdpel, int dir, int avg)
410{
9b749c82
VG
411 H264Context *h = &s->h;
412 const H264Picture *pic = (dir == 0) ? s->last_pic : s->next_pic;
76de302d
DB
413 uint8_t *src, *dest;
414 int i, emu = 0;
a7d2861d 415 int blocksize = 2 - (width >> 3); // 16->0, 8->1, 4->2
939b3883
AK
416 int linesize = s->cur_pic->f->linesize[0];
417 int uvlinesize = s->cur_pic->f->linesize[1];
76de302d
DB
418
419 mx += x;
420 my += y;
8b82a956 421
af1ede06
DB
422 if (mx < 0 || mx >= s->h_edge_pos - width - 1 ||
423 my < 0 || my >= s->v_edge_pos - height - 1) {
fae6fd5b 424 emu = 1;
af1ede06
DB
425 mx = av_clip(mx, -16, s->h_edge_pos - width + 15);
426 my = av_clip(my, -16, s->v_edge_pos - height + 15);
8b82a956
MN
427 }
428
76de302d 429 /* form component predictions */
21b74693 430 dest = s->cur_pic->f->data[0] + x + y * linesize;
939b3883 431 src = pic->f->data[0] + mx + my * linesize;
76de302d
DB
432
433 if (emu) {
8eecae77 434 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
939b3883 435 linesize, linesize,
8c53d39e
RB
436 width + 1, height + 1,
437 mx, my, s->h_edge_pos, s->v_edge_pos);
8eecae77 438 src = s->edge_emu_buffer;
8b82a956 439 }
76de302d 440 if (thirdpel)
57f09608 441 (avg ? s->tdsp.avg_tpel_pixels_tab
939b3883 442 : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src, linesize,
57f09608 443 width, height);
76de302d 444 else
2f6bc5f7 445 (avg ? s->hdsp.avg_pixels_tab
939b3883 446 : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src, linesize,
2f6bc5f7 447 height);
76de302d 448
7c6eb0a1 449 if (!(h->flags & AV_CODEC_FLAG_GRAY)) {
af1ede06
DB
450 mx = mx + (mx < (int) x) >> 1;
451 my = my + (my < (int) y) >> 1;
452 width = width >> 1;
453 height = height >> 1;
76de302d
DB
454 blocksize++;
455
456 for (i = 1; i < 3; i++) {
21b74693 457 dest = s->cur_pic->f->data[i] + (x >> 1) + (y >> 1) * uvlinesize;
939b3883 458 src = pic->f->data[i] + mx + my * uvlinesize;
76de302d
DB
459
460 if (emu) {
8eecae77 461 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
939b3883 462 uvlinesize, uvlinesize,
8c53d39e
RB
463 width + 1, height + 1,
464 mx, my, (s->h_edge_pos >> 1),
465 s->v_edge_pos >> 1);
8eecae77 466 src = s->edge_emu_buffer;
76de302d
DB
467 }
468 if (thirdpel)
57f09608
DB
469 (avg ? s->tdsp.avg_tpel_pixels_tab
470 : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src,
939b3883 471 uvlinesize,
57f09608 472 width, height);
76de302d 473 else
2f6bc5f7
RB
474 (avg ? s->hdsp.avg_pixels_tab
475 : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src,
939b3883 476 uvlinesize,
2f6bc5f7 477 height);
76de302d
DB
478 }
479 }
8b82a956
MN
480}
481
2c541554 482static inline int svq3_mc_dir(SVQ3Context *s, int size, int mode,
a7d2861d 483 int dir, int avg)
7f8205da 484{
76de302d 485 int i, j, k, mx, my, dx, dy, x, y;
2c541554 486 H264Context *h = &s->h;
a7d2861d
DB
487 const int part_width = ((size & 5) == 4) ? 4 : 16 >> (size & 1);
488 const int part_height = 16 >> ((unsigned)(size + 1) / 3);
489 const int extra_width = (mode == PREDICT_MODE) ? -16 * 6 : 0;
490 const int h_edge_pos = 6 * (s->h_edge_pos - part_width) - extra_width;
491 const int v_edge_pos = 6 * (s->v_edge_pos - part_height) - extra_width;
492
493 for (i = 0; i < 16; i += part_height)
76de302d 494 for (j = 0; j < 16; j += part_width) {
1877712c 495 const int b_xy = (4 * s->mb_x + (j >> 2)) +
12f13ecb 496 (4 * s->mb_y + (i >> 2)) * s->b_stride;
76de302d 497 int dxy;
1877712c
AK
498 x = 16 * s->mb_x + j;
499 y = 16 * s->mb_y + i;
af1ede06
DB
500 k = (j >> 2 & 1) + (i >> 1 & 2) +
501 (j >> 1 & 4) + (i & 8);
76de302d
DB
502
503 if (mode != PREDICT_MODE) {
99dde603 504 svq3_pred_motion(s, k, part_width >> 2, dir, 1, &mx, &my);
76de302d 505 } else {
759001c5
AK
506 mx = s->next_pic->motion_val[0][b_xy][0] << 1;
507 my = s->next_pic->motion_val[0][b_xy][1] << 1;
76de302d
DB
508
509 if (dir == 0) {
ea6ab02a
AK
510 mx = mx * s->frame_num_offset /
511 s->prev_frame_num_offset + 1 >> 1;
512 my = my * s->frame_num_offset /
513 s->prev_frame_num_offset + 1 >> 1;
76de302d 514 } else {
ea6ab02a
AK
515 mx = mx * (s->frame_num_offset - s->prev_frame_num_offset) /
516 s->prev_frame_num_offset + 1 >> 1;
517 my = my * (s->frame_num_offset - s->prev_frame_num_offset) /
518 s->prev_frame_num_offset + 1 >> 1;
76de302d
DB
519 }
520 }
521
522 /* clip motion vector prediction to frame border */
a7d2861d
DB
523 mx = av_clip(mx, extra_width - 6 * x, h_edge_pos - 6 * x);
524 my = av_clip(my, extra_width - 6 * y, v_edge_pos - 6 * y);
76de302d
DB
525
526 /* get (optional) motion vector differential */
527 if (mode == PREDICT_MODE) {
528 dx = dy = 0;
529 } else {
2c541554
AK
530 dy = svq3_get_se_golomb(&h->gb);
531 dx = svq3_get_se_golomb(&h->gb);
76de302d
DB
532
533 if (dx == INVALID_VLC || dy == INVALID_VLC) {
2c541554 534 av_log(h->avctx, AV_LOG_ERROR, "invalid MV vlc\n");
76de302d
DB
535 return -1;
536 }
537 }
538
539 /* compute motion vector */
540 if (mode == THIRDPEL_MODE) {
541 int fx, fy;
af1ede06
DB
542 mx = (mx + 1 >> 1) + dx;
543 my = (my + 1 >> 1) + dy;
544 fx = (unsigned)(mx + 0x3000) / 3 - 0x1000;
545 fy = (unsigned)(my + 0x3000) / 3 - 0x1000;
a7d2861d
DB
546 dxy = (mx - 3 * fx) + 4 * (my - 3 * fy);
547
548 svq3_mc_dir_part(s, x, y, part_width, part_height,
549 fx, fy, dxy, 1, dir, avg);
76de302d
DB
550 mx += mx;
551 my += my;
552 } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) {
af1ede06
DB
553 mx = (unsigned)(mx + 1 + 0x3000) / 3 + dx - 0x1000;
554 my = (unsigned)(my + 1 + 0x3000) / 3 + dy - 0x1000;
a7d2861d 555 dxy = (mx & 1) + 2 * (my & 1);
76de302d 556
a7d2861d
DB
557 svq3_mc_dir_part(s, x, y, part_width, part_height,
558 mx >> 1, my >> 1, dxy, 0, dir, avg);
76de302d
DB
559 mx *= 3;
560 my *= 3;
561 } else {
af1ede06
DB
562 mx = (unsigned)(mx + 3 + 0x6000) / 6 + dx - 0x1000;
563 my = (unsigned)(my + 3 + 0x6000) / 6 + dy - 0x1000;
76de302d 564
a7d2861d
DB
565 svq3_mc_dir_part(s, x, y, part_width, part_height,
566 mx, my, 0, 0, dir, avg);
76de302d
DB
567 mx *= 6;
568 my *= 6;
569 }
570
571 /* update mv_cache */
572 if (mode != PREDICT_MODE) {
a7d2861d 573 int32_t mv = pack16to32(mx, my);
76de302d
DB
574
575 if (part_height == 8 && i < 8) {
99dde603 576 AV_WN32A(s->mv_cache[dir][scan8[k] + 1 * 8], mv);
76de302d 577
a7d2861d 578 if (part_width == 8 && j < 8)
99dde603 579 AV_WN32A(s->mv_cache[dir][scan8[k] + 1 + 1 * 8], mv);
76de302d 580 }
a7d2861d 581 if (part_width == 8 && j < 8)
99dde603 582 AV_WN32A(s->mv_cache[dir][scan8[k] + 1], mv);
a7d2861d 583 if (part_width == 4 || part_height == 4)
99dde603 584 AV_WN32A(s->mv_cache[dir][scan8[k]], mv);
76de302d
DB
585 }
586
587 /* write back motion vectors */
21b74693 588 fill_rectangle(s->cur_pic->motion_val[dir][b_xy],
12f13ecb 589 part_width >> 2, part_height >> 2, s->b_stride,
657ccb5a 590 pack16to32(mx, my), 4);
bb270c08 591 }
2e26c8d2 592
76de302d 593 return 0;
2e26c8d2
MM
594}
595
939b3883 596static av_always_inline void hl_decode_mb_idct_luma(SVQ3Context *s,
e42ca48a
AK
597 int mb_type, const int *block_offset,
598 int linesize, uint8_t *dest_y)
599{
600 int i;
601 if (!IS_INTRA4x4(mb_type)) {
602 for (i = 0; i < 16; i++)
939b3883 603 if (s->non_zero_count_cache[scan8[i]] || s->mb[i * 16]) {
e42ca48a 604 uint8_t *const ptr = dest_y + block_offset[i];
939b3883
AK
605 svq3_add_idct_c(ptr, s->mb + i * 16, linesize,
606 s->qscale, IS_INTRA(mb_type) ? 1 : 0);
e42ca48a
AK
607 }
608 }
609}
610
611static av_always_inline int dctcoef_get(int16_t *mb, int index)
612{
613 return AV_RN16A(mb + index);
614}
615
c73fb9ef
AK
616static av_always_inline void hl_decode_mb_predict_luma(SVQ3Context *s,
617 const H264Context *h,
e42ca48a
AK
618 int mb_type,
619 const int *block_offset,
620 int linesize,
621 uint8_t *dest_y)
622{
623 int i;
939b3883 624 int qscale = s->qscale;
e42ca48a
AK
625
626 if (IS_INTRA4x4(mb_type)) {
627 for (i = 0; i < 16; i++) {
628 uint8_t *const ptr = dest_y + block_offset[i];
527bf5f7 629 const int dir = s->intra4x4_pred_mode_cache[scan8[i]];
e42ca48a
AK
630
631 uint8_t *topright;
632 int nnz, tr;
633 if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
527bf5f7 634 const int topright_avail = (s->topright_samples_available << i) & 0x8000;
1877712c 635 assert(s->mb_y || linesize <= block_offset[i]);
e42ca48a
AK
636 if (!topright_avail) {
637 tr = ptr[3 - linesize] * 0x01010101u;
638 topright = (uint8_t *)&tr;
639 } else
640 topright = ptr + 4 - linesize;
641 } else
642 topright = NULL;
643
c73fb9ef 644 s->hpc.pred4x4[dir](ptr, topright, linesize);
939b3883 645 nnz = s->non_zero_count_cache[scan8[i]];
e42ca48a 646 if (nnz) {
939b3883 647 svq3_add_idct_c(ptr, s->mb + i * 16, linesize, qscale, 0);
e42ca48a
AK
648 }
649 }
650 } else {
527bf5f7 651 s->hpc.pred16x16[s->intra16x16_pred_mode](dest_y, linesize);
939b3883 652 svq3_luma_dc_dequant_idct_c(s->mb, s->mb_luma_dc[0], qscale);
e42ca48a
AK
653 }
654}
655
939b3883 656static void hl_decode_mb(SVQ3Context *s, const H264Context *h)
e42ca48a 657{
1877712c
AK
658 const int mb_x = s->mb_x;
659 const int mb_y = s->mb_y;
660 const int mb_xy = s->mb_xy;
21b74693 661 const int mb_type = s->cur_pic->mb_type[mb_xy];
e42ca48a
AK
662 uint8_t *dest_y, *dest_cb, *dest_cr;
663 int linesize, uvlinesize;
664 int i, j;
665 const int *block_offset = &h->block_offset[0];
666 const int block_h = 16 >> h->chroma_y_shift;
667
939b3883
AK
668 linesize = s->cur_pic->f->linesize[0];
669 uvlinesize = s->cur_pic->f->linesize[1];
e42ca48a 670
21b74693
AK
671 dest_y = s->cur_pic->f->data[0] + (mb_x + mb_y * linesize) * 16;
672 dest_cb = s->cur_pic->f->data[1] + mb_x * 8 + mb_y * uvlinesize * block_h;
673 dest_cr = s->cur_pic->f->data[2] + mb_x * 8 + mb_y * uvlinesize * block_h;
e42ca48a 674
939b3883
AK
675 s->vdsp.prefetch(dest_y + (s->mb_x & 3) * 4 * linesize + 64, linesize, 4);
676 s->vdsp.prefetch(dest_cb + (s->mb_x & 7) * uvlinesize + 64, dest_cr - dest_cb, 2);
e42ca48a
AK
677
678 if (IS_INTRA(mb_type)) {
527bf5f7
AK
679 s->hpc.pred8x8[s->chroma_pred_mode](dest_cb, uvlinesize);
680 s->hpc.pred8x8[s->chroma_pred_mode](dest_cr, uvlinesize);
e42ca48a 681
939b3883 682 hl_decode_mb_predict_luma(s, h, mb_type, block_offset, linesize, dest_y);
e42ca48a
AK
683 }
684
939b3883 685 hl_decode_mb_idct_luma(s, mb_type, block_offset, linesize, dest_y);
e42ca48a 686
939b3883 687 if (s->cbp & 0x30) {
e42ca48a 688 uint8_t *dest[2] = { dest_cb, dest_cr };
939b3883
AK
689 s->h264dsp.h264_chroma_dc_dequant_idct(s->mb + 16 * 16 * 1,
690 s->dequant4_coeff[4][0]);
691 s->h264dsp.h264_chroma_dc_dequant_idct(s->mb + 16 * 16 * 2,
692 s->dequant4_coeff[4][0]);
e42ca48a
AK
693 for (j = 1; j < 3; j++) {
694 for (i = j * 16; i < j * 16 + 4; i++)
939b3883 695 if (s->non_zero_count_cache[scan8[i]] || s->mb[i * 16]) {
e42ca48a 696 uint8_t *const ptr = dest[j - 1] + block_offset[i];
939b3883
AK
697 svq3_add_idct_c(ptr, s->mb + i * 16,
698 uvlinesize, ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
e42ca48a
AK
699 }
700 }
701 }
702}
703
2c541554 704static int svq3_decode_mb(SVQ3Context *s, unsigned int mb_type)
7f8205da 705{
2c541554 706 H264Context *h = &s->h;
76de302d
DB
707 int i, j, k, m, dir, mode;
708 int cbp = 0;
709 uint32_t vlc;
710 int8_t *top, *left;
1877712c 711 const int mb_xy = s->mb_xy;
12f13ecb 712 const int b_xy = 4 * s->mb_x + 4 * s->mb_y * s->b_stride;
76de302d 713
527bf5f7
AK
714 s->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
715 s->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
716 s->topright_samples_available = 0xFFFF;
76de302d
DB
717
718 if (mb_type == 0) { /* SKIP */
5a5db90e 719 if (s->pict_type == AV_PICTURE_TYPE_P ||
759001c5 720 s->next_pic->mb_type[mb_xy] == -1) {
1877712c 721 svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
a7d2861d 722 0, 0, 0, 0, 0, 0);
76de302d 723
5a5db90e 724 if (s->pict_type == AV_PICTURE_TYPE_B)
1877712c 725 svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
a7d2861d 726 0, 0, 0, 0, 1, 1);
76de302d
DB
727
728 mb_type = MB_TYPE_SKIP;
729 } else {
759001c5 730 mb_type = FFMIN(s->next_pic->mb_type[mb_xy], 6);
2c541554 731 if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 0, 0) < 0)
76de302d 732 return -1;
2c541554 733 if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 1, 1) < 0)
76de302d 734 return -1;
8b82a956 735
76de302d 736 mb_type = MB_TYPE_16x16;
bb270c08 737 }
76de302d 738 } else if (mb_type < 8) { /* INTER */
2c541554 739 if (s->thirdpel_flag && s->halfpel_flag == !get_bits1(&h->gb))
76de302d 740 mode = THIRDPEL_MODE;
2c541554
AK
741 else if (s->halfpel_flag &&
742 s->thirdpel_flag == !get_bits1(&h->gb))
76de302d 743 mode = HALFPEL_MODE;
a7d2861d 744 else
76de302d 745 mode = FULLPEL_MODE;
8b82a956 746
76de302d
DB
747 /* fill caches */
748 /* note ref_cache should contain here:
a7d2861d
DB
749 * ????????
750 * ???11111
751 * N??11111
752 * N??11111
753 * N??11111
754 */
76de302d
DB
755
756 for (m = 0; m < 2; m++) {
549fc772 757 if (s->mb_x > 0 && s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - 1] + 6] != -1) {
a7d2861d 758 for (i = 0; i < 4; i++)
99dde603 759 AV_COPY32(s->mv_cache[m][scan8[0] - 1 + i * 8],
21b74693 760 s->cur_pic->motion_val[m][b_xy - 1 + i * s->b_stride]);
76de302d 761 } else {
a7d2861d 762 for (i = 0; i < 4; i++)
99dde603 763 AV_ZERO32(s->mv_cache[m][scan8[0] - 1 + i * 8]);
76de302d 764 }
1877712c 765 if (s->mb_y > 0) {
99dde603 766 memcpy(s->mv_cache[m][scan8[0] - 1 * 8],
21b74693 767 s->cur_pic->motion_val[m][b_xy - s->b_stride],
a7d2861d 768 4 * 2 * sizeof(int16_t));
99dde603 769 memset(&s->ref_cache[m][scan8[0] - 1 * 8],
12f13ecb 770 (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);
76de302d 771
12f13ecb 772 if (s->mb_x < s->mb_width - 1) {
99dde603 773 AV_COPY32(s->mv_cache[m][scan8[0] + 4 - 1 * 8],
21b74693 774 s->cur_pic->motion_val[m][b_xy - s->b_stride + 4]);
99dde603 775 s->ref_cache[m][scan8[0] + 4 - 1 * 8] =
12f13ecb
AK
776 (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride + 1] + 6] == -1 ||
777 s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1;
a7d2861d 778 } else
99dde603 779 s->ref_cache[m][scan8[0] + 4 - 1 * 8] = PART_NOT_AVAILABLE;
1877712c 780 if (s->mb_x > 0) {
99dde603 781 AV_COPY32(s->mv_cache[m][scan8[0] - 1 - 1 * 8],
21b74693 782 s->cur_pic->motion_val[m][b_xy - s->b_stride - 1]);
99dde603 783 s->ref_cache[m][scan8[0] - 1 - 1 * 8] =
12f13ecb 784 (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] == -1) ? PART_NOT_AVAILABLE : 1;
a7d2861d 785 } else
99dde603 786 s->ref_cache[m][scan8[0] - 1 - 1 * 8] = PART_NOT_AVAILABLE;
a7d2861d 787 } else
99dde603 788 memset(&s->ref_cache[m][scan8[0] - 1 * 8 - 1],
a7d2861d 789 PART_NOT_AVAILABLE, 8);
76de302d 790
5a5db90e 791 if (s->pict_type != AV_PICTURE_TYPE_B)
76de302d 792 break;
bb270c08 793 }
8b82a956 794
76de302d 795 /* decode motion vector(s) and form prediction(s) */
5a5db90e 796 if (s->pict_type == AV_PICTURE_TYPE_P) {
2c541554 797 if (svq3_mc_dir(s, mb_type - 1, mode, 0, 0) < 0)
76de302d 798 return -1;
975a1447 799 } else { /* AV_PICTURE_TYPE_B */
7f3624dc 800 if (mb_type != 2) {
2c541554 801 if (svq3_mc_dir(s, 0, mode, 0, 0) < 0)
76de302d 802 return -1;
7f3624dc 803 } else {
a7d2861d 804 for (i = 0; i < 4; i++)
21b74693 805 memset(s->cur_pic->motion_val[0][b_xy + i * s->b_stride],
a7d2861d 806 0, 4 * 2 * sizeof(int16_t));
7f3624dc
MH
807 }
808 if (mb_type != 1) {
2c541554 809 if (svq3_mc_dir(s, 0, mode, 1, mb_type == 3) < 0)
76de302d 810 return -1;
7f3624dc 811 } else {
a7d2861d 812 for (i = 0; i < 4; i++)
21b74693 813 memset(s->cur_pic->motion_val[1][b_xy + i * s->b_stride],
a7d2861d 814 0, 4 * 2 * sizeof(int16_t));
7f3624dc 815 }
bb270c08 816 }
8b82a956 817
76de302d
DB
818 mb_type = MB_TYPE_16x16;
819 } else if (mb_type == 8 || mb_type == 33) { /* INTRA4x4 */
549fc772 820 int8_t *i4x4 = s->intra4x4_pred_mode + s->mb2br_xy[s->mb_xy];
527bf5f7 821 int8_t *i4x4_cache = s->intra4x4_pred_mode_cache;
c2a4ca94 822
527bf5f7 823 memset(s->intra4x4_pred_mode_cache, -1, 8 * 5 * sizeof(int8_t));
76de302d
DB
824
825 if (mb_type == 8) {
1877712c 826 if (s->mb_x > 0) {
a7d2861d 827 for (i = 0; i < 4; i++)
549fc772 828 s->intra4x4_pred_mode_cache[scan8[0] - 1 + i * 8] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - 1] + 6 - i];
527bf5f7
AK
829 if (s->intra4x4_pred_mode_cache[scan8[0] - 1] == -1)
830 s->left_samples_available = 0x5F5F;
76de302d 831 }
1877712c 832 if (s->mb_y > 0) {
12f13ecb
AK
833 s->intra4x4_pred_mode_cache[4 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 0];
834 s->intra4x4_pred_mode_cache[5 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 1];
835 s->intra4x4_pred_mode_cache[6 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 2];
836 s->intra4x4_pred_mode_cache[7 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 3];
76de302d 837
527bf5f7
AK
838 if (s->intra4x4_pred_mode_cache[4 + 8 * 0] == -1)
839 s->top_samples_available = 0x33FF;
76de302d
DB
840 }
841
842 /* decode prediction codes for luma blocks */
a7d2861d 843 for (i = 0; i < 16; i += 2) {
2c541554 844 vlc = svq3_get_ue_golomb(&h->gb);
76de302d 845
a7d2861d 846 if (vlc >= 25) {
cc8163e1
DB
847 av_log(h->avctx, AV_LOG_ERROR,
848 "luma prediction:%"PRIu32"\n", vlc);
76de302d
DB
849 return -1;
850 }
851
527bf5f7
AK
852 left = &s->intra4x4_pred_mode_cache[scan8[i] - 1];
853 top = &s->intra4x4_pred_mode_cache[scan8[i] - 8];
76de302d
DB
854
855 left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
856 left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
857
a7d2861d 858 if (left[1] == -1 || left[2] == -1) {
2c541554 859 av_log(h->avctx, AV_LOG_ERROR, "weird prediction\n");
76de302d
DB
860 return -1;
861 }
862 }
863 } else { /* mb_type == 33, DC_128_PRED block type */
a7d2861d 864 for (i = 0; i < 4; i++)
527bf5f7 865 memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_PRED, 4);
884182b3 866 }
8b82a956 867
c2a4ca94
AK
868 AV_COPY32(i4x4, i4x4_cache + 4 + 8 * 4);
869 i4x4[4] = i4x4_cache[7 + 8 * 3];
870 i4x4[5] = i4x4_cache[7 + 8 * 2];
871 i4x4[6] = i4x4_cache[7 + 8 * 1];
8b82a956 872
76de302d 873 if (mb_type == 8) {
527bf5f7
AK
874 ff_h264_check_intra4x4_pred_mode(s->intra4x4_pred_mode_cache,
875 h->avctx, s->top_samples_available,
876 s->left_samples_available);
da3b9756 877
527bf5f7
AK
878 s->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
879 s->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
76de302d 880 } else {
a7d2861d 881 for (i = 0; i < 4; i++)
527bf5f7 882 memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_128_PRED, 4);
da3b9756 883
527bf5f7
AK
884 s->top_samples_available = 0x33FF;
885 s->left_samples_available = 0x5F5F;
76de302d 886 }
da3b9756 887
76de302d
DB
888 mb_type = MB_TYPE_INTRA4x4;
889 } else { /* INTRA16x16 */
a7da517f 890 dir = ff_h264_i_mb_type_info[mb_type - 8].pred_mode;
a7d2861d 891 dir = (dir >> 1) ^ 3 * (dir & 1) ^ 1;
8b82a956 892
527bf5f7
AK
893 if ((s->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(h->avctx, s->top_samples_available,
894 s->left_samples_available, dir, 0)) < 0) {
1115689d 895 av_log(h->avctx, AV_LOG_ERROR, "ff_h264_check_intra_pred_mode < 0\n");
527bf5f7 896 return s->intra16x16_pred_mode;
76de302d 897 }
8b82a956 898
a7da517f 899 cbp = ff_h264_i_mb_type_info[mb_type - 8].cbp;
76de302d 900 mb_type = MB_TYPE_INTRA16x16;
884182b3 901 }
8b82a956 902
5a5db90e 903 if (!IS_INTER(mb_type) && s->pict_type != AV_PICTURE_TYPE_I) {
a7d2861d 904 for (i = 0; i < 4; i++)
21b74693 905 memset(s->cur_pic->motion_val[0][b_xy + i * s->b_stride],
a7d2861d 906 0, 4 * 2 * sizeof(int16_t));
5a5db90e 907 if (s->pict_type == AV_PICTURE_TYPE_B) {
a7d2861d 908 for (i = 0; i < 4; i++)
21b74693 909 memset(s->cur_pic->motion_val[1][b_xy + i * s->b_stride],
a7d2861d 910 0, 4 * 2 * sizeof(int16_t));
76de302d 911 }
8b82a956 912 }
76de302d 913 if (!IS_INTRA4x4(mb_type)) {
549fc772 914 memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy], DC_PRED, 8);
da3b9756 915 }
5a5db90e 916 if (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B) {
939b3883 917 memset(s->non_zero_count_cache + 8, 0, 14 * 8 * sizeof(uint8_t));
884182b3 918 }
2e26c8d2 919
a7d2861d 920 if (!IS_INTRA16x16(mb_type) &&
5a5db90e 921 (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B)) {
2c541554 922 if ((vlc = svq3_get_ue_golomb(&h->gb)) >= 48) {
cc8163e1 923 av_log(h->avctx, AV_LOG_ERROR, "cbp_vlc=%"PRIu32"\n", vlc);
76de302d
DB
924 return -1;
925 }
8b82a956 926
a7da517f
DB
927 cbp = IS_INTRA(mb_type) ? ff_h264_golomb_to_intra4x4_cbp[vlc]
928 : ff_h264_golomb_to_inter_cbp[vlc];
884182b3 929 }
a7d2861d 930 if (IS_INTRA16x16(mb_type) ||
5a5db90e 931 (s->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
939b3883 932 s->qscale += svq3_get_se_golomb(&h->gb);
8b82a956 933
939b3883
AK
934 if (s->qscale > 31u) {
935 av_log(h->avctx, AV_LOG_ERROR, "qscale:%d\n", s->qscale);
bb270c08 936 return -1;
bb270c08 937 }
8b82a956 938 }
76de302d 939 if (IS_INTRA16x16(mb_type)) {
939b3883
AK
940 AV_ZERO128(s->mb_luma_dc[0] + 0);
941 AV_ZERO128(s->mb_luma_dc[0] + 8);
942 if (svq3_decode_block(&h->gb, s->mb_luma_dc[0], 0, 1)) {
2c541554 943 av_log(h->avctx, AV_LOG_ERROR,
a7d2861d 944 "error while decoding intra luma dc\n");
76de302d 945 return -1;
884182b3 946 }
76de302d 947 }
8b82a956 948
76de302d
DB
949 if (cbp) {
950 const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
939b3883 951 const int type = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
76de302d 952
a7d2861d 953 for (i = 0; i < 4; i++)
76de302d
DB
954 if ((cbp & (1 << i))) {
955 for (j = 0; j < 4; j++) {
a7d2861d
DB
956 k = index ? (1 * (j & 1) + 2 * (i & 1) +
957 2 * (j & 2) + 4 * (i & 2))
958 : (4 * i + j);
939b3883 959 s->non_zero_count_cache[scan8[k]] = 1;
a7d2861d 960
939b3883 961 if (svq3_decode_block(&h->gb, &s->mb[16 * k], index, type)) {
2c541554 962 av_log(h->avctx, AV_LOG_ERROR,
a7d2861d 963 "error while decoding block\n");
76de302d
DB
964 return -1;
965 }
966 }
967 }
8b82a956 968
76de302d 969 if ((cbp & 0x30)) {
a7d2861d 970 for (i = 1; i < 3; ++i)
939b3883 971 if (svq3_decode_block(&h->gb, &s->mb[16 * 16 * i], 0, 3)) {
2c541554 972 av_log(h->avctx, AV_LOG_ERROR,
a7d2861d
DB
973 "error while decoding chroma dc block\n");
974 return -1;
975 }
76de302d
DB
976
977 if ((cbp & 0x20)) {
11177a4d
JGG
978 for (i = 1; i < 3; i++) {
979 for (j = 0; j < 4; j++) {
a7d2861d 980 k = 16 * i + j;
939b3883 981 s->non_zero_count_cache[scan8[k]] = 1;
11177a4d 982
939b3883 983 if (svq3_decode_block(&h->gb, &s->mb[16 * k], 1, 1)) {
2c541554 984 av_log(h->avctx, AV_LOG_ERROR,
a7d2861d 985 "error while decoding chroma ac block\n");
11177a4d
JGG
986 return -1;
987 }
76de302d
DB
988 }
989 }
990 }
bb270c08 991 }
8b82a956 992 }
8b82a956 993
21b74693
AK
994 s->cbp = cbp;
995 s->cur_pic->mb_type[mb_xy] = mb_type;
8b82a956 996
a7d2861d 997 if (IS_INTRA(mb_type))
527bf5f7
AK
998 s->chroma_pred_mode = ff_h264_check_intra_pred_mode(h->avctx, s->top_samples_available,
999 s->left_samples_available, DC_PRED8x8, 1);
8b82a956 1000
76de302d 1001 return 0;
8b82a956
MN
1002}
1003
8dfc6d1f 1004static int svq3_decode_slice_header(AVCodecContext *avctx)
7f8205da 1005{
2c541554
AK
1006 SVQ3Context *s = avctx->priv_data;
1007 H264Context *h = &s->h;
1877712c 1008 const int mb_xy = s->mb_xy;
76de302d 1009 int i, header;
288bb3da 1010 unsigned slice_id;
da3b9756 1011
1098f5c0 1012 header = get_bits(&s->gb, 8);
da3b9756 1013
76de302d
DB
1014 if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
1015 /* TODO: what? */
8dfc6d1f 1016 av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
76de302d
DB
1017 return -1;
1018 } else {
1098f5c0 1019 int slice_bits, slice_bytes, slice_length;
af1ede06 1020 int length = header >> 5 & 3;
da3b9756 1021
1098f5c0
LB
1022 slice_length = show_bits(&s->gb, 8 * length);
1023 slice_bits = slice_length * 8;
1024 slice_bytes = slice_length + length - 1;
da3b9756 1025
1098f5c0 1026 if (slice_bytes > get_bits_left(&s->gb)) {
8dfc6d1f 1027 av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
76de302d 1028 return -1;
a7d2861d 1029 }
da3b9756 1030
1098f5c0
LB
1031 skip_bits(&s->gb, 8);
1032
1033 av_fast_malloc(&s->slice_buf, &s->slice_size, slice_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
1034 if (!s->slice_buf)
1035 return AVERROR(ENOMEM);
1036
1037 memcpy(s->slice_buf, s->gb.buffer + s->gb.index / 8, slice_bytes);
1038
1039 init_get_bits(&h->gb, s->slice_buf, slice_bits);
da3b9756 1040
2c541554 1041 if (s->watermark_key) {
1098f5c0
LB
1042 uint32_t header = AV_RL32(&h->gb.buffer[1]);
1043 AV_WL32(&h->gb.buffer[1], header ^ s->watermark_key);
76de302d
DB
1044 }
1045 if (length > 0) {
1098f5c0 1046 memcpy(s->slice_buf, &s->slice_buf[slice_length], length - 1);
76de302d 1047 }
1098f5c0 1048 skip_bits_long(&s->gb, slice_bytes * 8);
da3b9756 1049 }
da3b9756 1050
2c541554 1051 if ((slice_id = svq3_get_ue_golomb(&h->gb)) >= 3) {
cc8163e1 1052 av_log(h->avctx, AV_LOG_ERROR, "illegal slice type %u \n", slice_id);
76de302d
DB
1053 return -1;
1054 }
da3b9756 1055
5a5db90e 1056 s->pict_type = ff_h264_golomb_to_pict_type[slice_id];
da3b9756 1057
76de302d 1058 if ((header & 0x9F) == 2) {
939b3883
AK
1059 i = (s->mb_num < 64) ? 6 : (1 + av_log2(s->mb_num - 1));
1060 get_bits(&h->gb, i);
76de302d 1061 } else {
2c541554 1062 skip_bits1(&h->gb);
76de302d 1063 }
da3b9756 1064
939b3883
AK
1065 s->slice_num = get_bits(&h->gb, 8);
1066 s->qscale = get_bits(&h->gb, 5);
2c541554 1067 s->adaptive_quant = get_bits1(&h->gb);
da3b9756 1068
76de302d 1069 /* unknown fields */
2c541554 1070 skip_bits1(&h->gb);
da3b9756 1071
2c541554
AK
1072 if (s->unknown_flag)
1073 skip_bits1(&h->gb);
da3b9756 1074
2c541554
AK
1075 skip_bits1(&h->gb);
1076 skip_bits(&h->gb, 2);
da3b9756 1077
2c541554
AK
1078 while (get_bits1(&h->gb))
1079 skip_bits(&h->gb, 8);
da3b9756 1080
76de302d 1081 /* reset intra predictors and invalidate motion vector references */
1877712c 1082 if (s->mb_x > 0) {
549fc772 1083 memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - 1] + 3,
a7d2861d 1084 -1, 4 * sizeof(int8_t));
549fc772 1085 memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_x],
1877712c 1086 -1, 8 * sizeof(int8_t) * s->mb_x);
76de302d 1087 }
1877712c 1088 if (s->mb_y > 0) {
12f13ecb
AK
1089 memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_stride],
1090 -1, 8 * sizeof(int8_t) * (s->mb_width - s->mb_x));
76de302d 1091
1877712c 1092 if (s->mb_x > 0)
12f13ecb 1093 s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] = -1;
da3b9756 1094 }
da3b9756 1095
76de302d 1096 return 0;
da3b9756
MM
1097}
1098
ad9d3384
AK
1099static void init_dequant4_coeff_table(SVQ3Context *s)
1100{
1101 int q, x;
1102 const int max_qp = 51;
1103
1104 for (q = 0; q < max_qp + 1; q++) {
1105 int shift = ff_h264_quant_div6[q] + 2;
1106 int idx = ff_h264_quant_rem6[q];
1107 for (x = 0; x < 16; x++)
1108 s->dequant4_coeff[q][(x >> 2) | ((x << 2) & 0xF)] =
1109 ((uint32_t)ff_h264_dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] * 16) << shift;
1110 }
1111}
1112
5ef251e5 1113static av_cold int svq3_decode_init(AVCodecContext *avctx)
7f8205da 1114{
2c541554
AK
1115 SVQ3Context *s = avctx->priv_data;
1116 H264Context *h = &s->h;
549fc772 1117 int m, x, y;
76de302d 1118 unsigned char *extradata;
9e1db721 1119 unsigned char *extradata_end;
76de302d 1120 unsigned int size;
9e1db721 1121 int marker_found = 0;
76de302d 1122
2c541554
AK
1123 s->cur_pic = av_mallocz(sizeof(*s->cur_pic));
1124 s->last_pic = av_mallocz(sizeof(*s->last_pic));
1125 s->next_pic = av_mallocz(sizeof(*s->next_pic));
1126 if (!s->next_pic || !s->last_pic || !s->cur_pic) {
1127 av_freep(&s->cur_pic);
1128 av_freep(&s->last_pic);
1129 av_freep(&s->next_pic);
1130 return AVERROR(ENOMEM);
1131 }
1132
a0f29460
AK
1133 s->cur_pic->f = av_frame_alloc();
1134 s->last_pic->f = av_frame_alloc();
1135 s->next_pic->f = av_frame_alloc();
1136 if (!s->cur_pic->f || !s->last_pic->f || !s->next_pic->f)
1137 return AVERROR(ENOMEM);
1138
903d58f6 1139 if (ff_h264_decode_init(avctx) < 0)
f4cca718
BC
1140 return -1;
1141
a0f29460
AK
1142 // we will overwrite it later during decoding
1143 av_frame_free(&h->cur_pic.f);
1144
c73fb9ef
AK
1145 ff_h264dsp_init(&s->h264dsp, 8, 1);
1146 ff_h264_pred_init(&s->hpc, AV_CODEC_ID_SVQ3, 8, 1);
1147 ff_videodsp_init(&s->vdsp, 8);
249796e2 1148
249796e2
AK
1149 h->sps.bit_depth_luma = 8;
1150 h->chroma_format_idc = 1;
1151
2f6bc5f7 1152 ff_hpeldsp_init(&s->hdsp, avctx->flags);
57f09608
DB
1153 ff_tpeldsp_init(&s->tdsp);
1154
2c541554 1155 h->flags = avctx->flags;
2c541554 1156 h->picture_structure = PICT_FRAME;
3795ec68
VG
1157 avctx->pix_fmt = AV_PIX_FMT_YUVJ420P;
1158 avctx->color_range = AVCOL_RANGE_JPEG;
76de302d 1159
2c541554 1160 h->chroma_x_shift = h->chroma_y_shift = 1;
d9ebb00d 1161
2c541554
AK
1162 s->halfpel_flag = 1;
1163 s->thirdpel_flag = 1;
1164 s->unknown_flag = 0;
d9ebb00d
AK
1165
1166 /* prowl for the "SEQH" marker in the extradata */
1167 extradata = (unsigned char *)avctx->extradata;
1168 extradata_end = avctx->extradata + avctx->extradata_size;
1169 if (extradata) {
1170 for (m = 0; m + 8 < avctx->extradata_size; m++) {
1171 if (!memcmp(extradata, "SEQH", 4)) {
1172 marker_found = 1;
1173 break;
9e1db721 1174 }
d9ebb00d 1175 extradata++;
76de302d 1176 }
d9ebb00d 1177 }
8b82a956 1178
d9ebb00d
AK
1179 /* if a match was found, parse the extra data */
1180 if (marker_found) {
1181 GetBitContext gb;
1182 int frame_size_code;
1183
1184 size = AV_RB32(&extradata[4]);
1185 if (size > extradata_end - extradata - 8)
1186 return AVERROR_INVALIDDATA;
1187 init_get_bits(&gb, extradata + 8, size * 8);
1188
1189 /* 'frame size code' and optional 'width, height' */
1190 frame_size_code = get_bits(&gb, 3);
1191 switch (frame_size_code) {
1192 case 0:
1193 avctx->width = 160;
1194 avctx->height = 120;
1195 break;
1196 case 1:
1197 avctx->width = 128;
1198 avctx->height = 96;
1199 break;
1200 case 2:
1201 avctx->width = 176;
1202 avctx->height = 144;
1203 break;
1204 case 3:
1205 avctx->width = 352;
1206 avctx->height = 288;
1207 break;
1208 case 4:
1209 avctx->width = 704;
1210 avctx->height = 576;
1211 break;
1212 case 5:
1213 avctx->width = 240;
1214 avctx->height = 180;
1215 break;
1216 case 6:
1217 avctx->width = 320;
1218 avctx->height = 240;
1219 break;
1220 case 7:
1221 avctx->width = get_bits(&gb, 12);
1222 avctx->height = get_bits(&gb, 12);
1223 break;
1224 }
8b82a956 1225
2c541554
AK
1226 s->halfpel_flag = get_bits1(&gb);
1227 s->thirdpel_flag = get_bits1(&gb);
8b82a956 1228
d9ebb00d
AK
1229 /* unknown fields */
1230 skip_bits1(&gb);
1231 skip_bits1(&gb);
1232 skip_bits1(&gb);
1233 skip_bits1(&gb);
8b82a956 1234
2c541554 1235 h->low_delay = get_bits1(&gb);
1e002b60 1236
d9ebb00d
AK
1237 /* unknown field */
1238 skip_bits1(&gb);
76de302d 1239
d9ebb00d
AK
1240 while (get_bits1(&gb))
1241 skip_bits(&gb, 8);
76de302d 1242
2c541554
AK
1243 s->unknown_flag = get_bits1(&gb);
1244 avctx->has_b_frames = !h->low_delay;
1245 if (s->unknown_flag) {
b250f9c6 1246#if CONFIG_ZLIB
d9ebb00d
AK
1247 unsigned watermark_width = svq3_get_ue_golomb(&gb);
1248 unsigned watermark_height = svq3_get_ue_golomb(&gb);
1249 int u1 = svq3_get_ue_golomb(&gb);
1250 int u2 = get_bits(&gb, 8);
1251 int u3 = get_bits(&gb, 2);
1252 int u4 = svq3_get_ue_golomb(&gb);
1253 unsigned long buf_len = watermark_width *
1254 watermark_height * 4;
1255 int offset = get_bits_count(&gb) + 7 >> 3;
1256 uint8_t *buf;
1257
601c2015
MS
1258 if (watermark_height > 0 &&
1259 (uint64_t)watermark_width * 4 > UINT_MAX / watermark_height)
d9ebb00d 1260 return -1;
76de302d 1261
d9ebb00d 1262 buf = av_malloc(buf_len);
cc8163e1 1263 av_log(avctx, AV_LOG_DEBUG, "watermark size: %ux%u\n",
d9ebb00d
AK
1264 watermark_width, watermark_height);
1265 av_log(avctx, AV_LOG_DEBUG,
1266 "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n",
1267 u1, u2, u3, u4, offset);
1268 if (uncompress(buf, &buf_len, extradata + 8 + offset,
1269 size - offset) != Z_OK) {
a7d2861d 1270 av_log(avctx, AV_LOG_ERROR,
d9ebb00d
AK
1271 "could not uncompress watermark logo\n");
1272 av_free(buf);
76de302d 1273 return -1;
76de302d 1274 }
2c541554
AK
1275 s->watermark_key = ff_svq1_packet_checksum(buf, buf_len, 0);
1276 s->watermark_key = s->watermark_key << 16 | s->watermark_key;
d9ebb00d 1277 av_log(avctx, AV_LOG_DEBUG,
cc8163e1 1278 "watermark key %#"PRIx32"\n", s->watermark_key);
d9ebb00d
AK
1279 av_free(buf);
1280#else
1281 av_log(avctx, AV_LOG_ERROR,
1282 "this svq3 file contains watermark which need zlib support compiled in\n");
1283 return -1;
1284#endif
76de302d 1285 }
d9ebb00d 1286 }
cdca7c37 1287
12f13ecb
AK
1288 s->mb_width = (avctx->width + 15) / 16;
1289 s->mb_height = (avctx->height + 15) / 16;
1290 s->mb_stride = s->mb_width + 1;
1291 s->mb_num = s->mb_width * s->mb_height;
1292 s->b_stride = 4 * s->mb_width;
1293 s->h_edge_pos = s->mb_width * 16;
1294 s->v_edge_pos = s->mb_height * 16;
1295
1296 s->intra4x4_pred_mode = av_mallocz(s->mb_stride * 2 * 8);
527bf5f7
AK
1297 if (!s->intra4x4_pred_mode)
1298 return AVERROR(ENOMEM);
1299
12f13ecb 1300 s->mb2br_xy = av_mallocz(s->mb_stride * (s->mb_height + 1) *
549fc772
AK
1301 sizeof(*s->mb2br_xy));
1302 if (!s->mb2br_xy)
1303 return AVERROR(ENOMEM);
1304
12f13ecb
AK
1305 for (y = 0; y < s->mb_height; y++)
1306 for (x = 0; x < s->mb_width; x++) {
1307 const int mb_xy = x + y * s->mb_stride;
549fc772 1308
12f13ecb 1309 s->mb2br_xy[mb_xy] = 8 * (mb_xy % (2 * s->mb_stride));
549fc772
AK
1310 }
1311
ad9d3384 1312 init_dequant4_coeff_table(s);
76de302d 1313
f4cca718
BC
1314 return 0;
1315}
1316
9b749c82 1317static void free_picture(AVCodecContext *avctx, H264Picture *pic)
759001c5
AK
1318{
1319 int i;
1320 for (i = 0; i < 2; i++) {
1321 av_buffer_unref(&pic->motion_val_buf[i]);
1322 av_buffer_unref(&pic->ref_index_buf[i]);
1323 }
1324 av_buffer_unref(&pic->mb_type_buf);
1325
a0f29460 1326 av_frame_unref(pic->f);
759001c5
AK
1327}
1328
9b749c82 1329static int get_buffer(AVCodecContext *avctx, H264Picture *pic)
2c541554
AK
1330{
1331 SVQ3Context *s = avctx->priv_data;
12f13ecb
AK
1332 const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
1333 const int mb_array_size = s->mb_stride * s->mb_height;
1334 const int b4_stride = s->mb_width * 4 + 1;
1335 const int b4_array_size = b4_stride * s->mb_height * 4;
2c541554
AK
1336 int ret;
1337
759001c5 1338 if (!pic->motion_val_buf[0]) {
2c541554
AK
1339 int i;
1340
12f13ecb 1341 pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) * sizeof(uint32_t));
759001c5 1342 if (!pic->mb_type_buf)
2c541554 1343 return AVERROR(ENOMEM);
12f13ecb 1344 pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
2c541554
AK
1345
1346 for (i = 0; i < 2; i++) {
759001c5
AK
1347 pic->motion_val_buf[i] = av_buffer_allocz(2 * (b4_array_size + 4) * sizeof(int16_t));
1348 pic->ref_index_buf[i] = av_buffer_allocz(4 * mb_array_size);
1349 if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i]) {
1350 ret = AVERROR(ENOMEM);
1351 goto fail;
1352 }
2c541554 1353
759001c5
AK
1354 pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
1355 pic->ref_index[i] = pic->ref_index_buf[i]->data;
2c541554
AK
1356 }
1357 }
5a5db90e 1358 pic->reference = !(s->pict_type == AV_PICTURE_TYPE_B);
759001c5 1359
a0f29460 1360 ret = ff_get_buffer(avctx, pic->f,
759001c5
AK
1361 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
1362 if (ret < 0)
1363 goto fail;
2c541554 1364
8eecae77
AK
1365 if (!s->edge_emu_buffer) {
1366 s->edge_emu_buffer = av_mallocz(pic->f->linesize[0] * 17);
1367 if (!s->edge_emu_buffer)
fae6fd5b
RB
1368 return AVERROR(ENOMEM);
1369 }
2c541554 1370
759001c5
AK
1371 return 0;
1372fail:
1373 free_picture(avctx, pic);
2c541554
AK
1374 return ret;
1375}
1376
a7d2861d 1377static int svq3_decode_frame(AVCodecContext *avctx, void *data,
df9b9567 1378 int *got_frame, AVPacket *avpkt)
f4cca718 1379{
7a00bbad 1380 const uint8_t *buf = avpkt->data;
2c541554
AK
1381 SVQ3Context *s = avctx->priv_data;
1382 H264Context *h = &s->h;
a7d2861d 1383 int buf_size = avpkt->size;
2c541554 1384 int ret, m, i;
f4cca718 1385
76de302d
DB
1386 /* special case for last picture */
1387 if (buf_size == 0) {
a0f29460
AK
1388 if (s->next_pic->f->data[0] && !h->low_delay && !s->last_frame_output) {
1389 ret = av_frame_ref(data, s->next_pic->f);
759001c5
AK
1390 if (ret < 0)
1391 return ret;
2c541554 1392 s->last_frame_output = 1;
df9b9567 1393 *got_frame = 1;
76de302d
DB
1394 }
1395 return 0;
da3b9756 1396 }
8b82a956 1397
1098f5c0
LB
1398 ret = init_get_bits(&s->gb, buf, 8 * buf_size);
1399 if (ret < 0)
1400 return ret;
8b82a956 1401
1877712c 1402 s->mb_x = s->mb_y = s->mb_xy = 0;
8b82a956 1403
8dfc6d1f 1404 if (svq3_decode_slice_header(avctx))
76de302d 1405 return -1;
8b82a956 1406
5a5db90e 1407 if (s->pict_type != AV_PICTURE_TYPE_B)
9b749c82 1408 FFSWAP(H264Picture*, s->next_pic, s->last_pic);
2c541554 1409
a0f29460 1410 av_frame_unref(s->cur_pic->f);
8b82a956 1411
8ed2ae09 1412 /* for skipping the frame */
5a5db90e
AK
1413 s->cur_pic->f->pict_type = s->pict_type;
1414 s->cur_pic->f->key_frame = (s->pict_type == AV_PICTURE_TYPE_I);
76de302d 1415
2c541554
AK
1416 ret = get_buffer(avctx, s->cur_pic);
1417 if (ret < 0)
1418 return ret;
1419
2c541554 1420 for (i = 0; i < 16; i++) {
939b3883
AK
1421 h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * s->cur_pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
1422 h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * s->cur_pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
2c541554
AK
1423 }
1424 for (i = 0; i < 16; i++) {
1425 h->block_offset[16 + i] =
939b3883 1426 h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * s->cur_pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
2c541554 1427 h->block_offset[48 + 16 + i] =
939b3883 1428 h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * s->cur_pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
2c541554
AK
1429 }
1430
5a5db90e 1431 if (s->pict_type != AV_PICTURE_TYPE_I) {
a0f29460 1432 if (!s->last_pic->f->data[0]) {
2c541554
AK
1433 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1434 ret = get_buffer(avctx, s->last_pic);
1435 if (ret < 0)
1436 return ret;
a0f29460
AK
1437 memset(s->last_pic->f->data[0], 0, avctx->height * s->last_pic->f->linesize[0]);
1438 memset(s->last_pic->f->data[1], 0x80, (avctx->height / 2) *
1439 s->last_pic->f->linesize[1]);
1440 memset(s->last_pic->f->data[2], 0x80, (avctx->height / 2) *
1441 s->last_pic->f->linesize[2]);
2c541554
AK
1442 }
1443
5a5db90e 1444 if (s->pict_type == AV_PICTURE_TYPE_B && !s->next_pic->f->data[0]) {
2c541554
AK
1445 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1446 ret = get_buffer(avctx, s->next_pic);
1447 if (ret < 0)
1448 return ret;
a0f29460
AK
1449 memset(s->next_pic->f->data[0], 0, avctx->height * s->next_pic->f->linesize[0]);
1450 memset(s->next_pic->f->data[1], 0x80, (avctx->height / 2) *
1451 s->next_pic->f->linesize[1]);
1452 memset(s->next_pic->f->data[2], 0x80, (avctx->height / 2) *
1453 s->next_pic->f->linesize[2]);
2c541554
AK
1454 }
1455 }
1456
1457 if (avctx->debug & FF_DEBUG_PICT_INFO)
1458 av_log(h->avctx, AV_LOG_DEBUG,
1459 "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
5a5db90e 1460 av_get_picture_type_char(s->pict_type),
2c541554 1461 s->halfpel_flag, s->thirdpel_flag,
939b3883 1462 s->adaptive_quant, s->qscale, s->slice_num);
2c541554 1463
5a5db90e
AK
1464 if (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B ||
1465 avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I ||
af1ede06 1466 avctx->skip_frame >= AVDISCARD_ALL)
76de302d
DB
1467 return 0;
1468
1469 if (s->next_p_frame_damaged) {
5a5db90e 1470 if (s->pict_type == AV_PICTURE_TYPE_B)
76de302d
DB
1471 return 0;
1472 else
1473 s->next_p_frame_damaged = 0;
1474 }
da3b9756 1475
5a5db90e 1476 if (s->pict_type == AV_PICTURE_TYPE_B) {
ea6ab02a 1477 s->frame_num_offset = s->slice_num - s->prev_frame_num;
8b82a956 1478
ea6ab02a
AK
1479 if (s->frame_num_offset < 0)
1480 s->frame_num_offset += 256;
1481 if (s->frame_num_offset == 0 ||
1482 s->frame_num_offset >= s->prev_frame_num_offset) {
2c541554 1483 av_log(h->avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
76de302d
DB
1484 return -1;
1485 }
1486 } else {
ea6ab02a
AK
1487 s->prev_frame_num = s->frame_num;
1488 s->frame_num = s->slice_num;
1489 s->prev_frame_num_offset = s->frame_num - s->prev_frame_num;
da3b9756 1490
ea6ab02a
AK
1491 if (s->prev_frame_num_offset < 0)
1492 s->prev_frame_num_offset += 256;
da3b9756 1493 }
da3b9756 1494
a7d2861d 1495 for (m = 0; m < 2; m++) {
76de302d 1496 int i;
a7d2861d 1497 for (i = 0; i < 4; i++) {
76de302d
DB
1498 int j;
1499 for (j = -1; j < 4; j++)
99dde603 1500 s->ref_cache[m][scan8[0] + 8 * i + j] = 1;
76de302d 1501 if (i < 3)
99dde603 1502 s->ref_cache[m][scan8[0] + 8 * i + j] = PART_NOT_AVAILABLE;
76de302d 1503 }
da3b9756 1504 }
da3b9756 1505
12f13ecb
AK
1506 for (s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
1507 for (s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
9a2e7911 1508 unsigned mb_type;
12f13ecb 1509 s->mb_xy = s->mb_x + s->mb_y * s->mb_stride;
da3b9756 1510
1098f5c0
LB
1511 if ((get_bits_left(&h->gb)) <= 7) {
1512 if (((get_bits_count(&h->gb) & 7) == 0 ||
1513 show_bits(&h->gb, get_bits_left(&h->gb) & 7) == 0)) {
da3b9756 1514
1098f5c0
LB
1515 if (svq3_decode_slice_header(avctx))
1516 return -1;
1517 }
76de302d
DB
1518 /* TODO: support s->mb_skip_run */
1519 }
da3b9756 1520
2c541554 1521 mb_type = svq3_get_ue_golomb(&h->gb);
da3b9756 1522
5a5db90e 1523 if (s->pict_type == AV_PICTURE_TYPE_I)
76de302d 1524 mb_type += 8;
5a5db90e 1525 else if (s->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4)
76de302d 1526 mb_type += 4;
2c541554
AK
1527 if (mb_type > 33 || svq3_decode_mb(s, mb_type)) {
1528 av_log(h->avctx, AV_LOG_ERROR,
1877712c 1529 "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
76de302d
DB
1530 return -1;
1531 }
8b82a956 1532
a7d2861d 1533 if (mb_type != 0)
939b3883 1534 hl_decode_mb(s, h);
8b82a956 1535
5a5db90e 1536 if (s->pict_type != AV_PICTURE_TYPE_B && !h->low_delay)
21b74693 1537 s->cur_pic->mb_type[s->mb_x + s->mb_y * s->mb_stride] =
5a5db90e 1538 (s->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
76de302d 1539 }
2e26c8d2 1540
a0f29460
AK
1541 ff_draw_horiz_band(avctx, s->cur_pic->f,
1542 s->last_pic->f->data[0] ? s->last_pic->f : NULL,
1877712c 1543 16 * s->mb_y, 16, h->picture_structure, 0,
54b2ce74 1544 h->low_delay);
8b82a956 1545 }
4c701ac8 1546
5a5db90e 1547 if (s->pict_type == AV_PICTURE_TYPE_B || h->low_delay)
a0f29460
AK
1548 ret = av_frame_ref(data, s->cur_pic->f);
1549 else if (s->last_pic->f->data[0])
1550 ret = av_frame_ref(data, s->last_pic->f);
759001c5
AK
1551 if (ret < 0)
1552 return ret;
da3b9756 1553
76de302d 1554 /* Do not output the last pic after seeking. */
a0f29460 1555 if (s->last_pic->f->data[0] || h->low_delay)
df9b9567 1556 *got_frame = 1;
da3b9756 1557
5a5db90e 1558 if (s->pict_type != AV_PICTURE_TYPE_B) {
9b749c82 1559 FFSWAP(H264Picture*, s->cur_pic, s->next_pic);
759001c5 1560 } else {
a0f29460 1561 av_frame_unref(s->cur_pic->f);
2c541554
AK
1562 }
1563
76de302d 1564 return buf_size;
8b82a956
MN
1565}
1566
bd8ac882 1567static av_cold int svq3_decode_end(AVCodecContext *avctx)
8dfc6d1f 1568{
2c541554
AK
1569 SVQ3Context *s = avctx->priv_data;
1570 H264Context *h = &s->h;
8dfc6d1f 1571
2c541554
AK
1572 free_picture(avctx, s->cur_pic);
1573 free_picture(avctx, s->next_pic);
1574 free_picture(avctx, s->last_pic);
a0f29460
AK
1575 av_frame_free(&s->cur_pic->f);
1576 av_frame_free(&s->next_pic->f);
1577 av_frame_free(&s->last_pic->f);
759001c5
AK
1578 av_freep(&s->cur_pic);
1579 av_freep(&s->next_pic);
1580 av_freep(&s->last_pic);
1098f5c0 1581 av_freep(&s->slice_buf);
527bf5f7 1582 av_freep(&s->intra4x4_pred_mode);
8eecae77 1583 av_freep(&s->edge_emu_buffer);
549fc772 1584 av_freep(&s->mb2br_xy);
759001c5 1585
2c541554 1586 ff_h264_free_context(h);
8dfc6d1f
BC
1587
1588 return 0;
1589}
8b82a956 1590
d36beb3f 1591AVCodec ff_svq3_decoder = {
ec6402b7 1592 .name = "svq3",
b2bed932 1593 .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
ec6402b7 1594 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 1595 .id = AV_CODEC_ID_SVQ3,
ec6402b7
AK
1596 .priv_data_size = sizeof(SVQ3Context),
1597 .init = svq3_decode_init,
1598 .close = svq3_decode_end,
1599 .decode = svq3_decode_frame,
def97856
VG
1600 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND |
1601 AV_CODEC_CAP_DR1 |
1602 AV_CODEC_CAP_DELAY,
a7d2861d
DB
1603 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,
1604 AV_PIX_FMT_NONE},
8b82a956 1605};