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