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