svq3: K&R formatting cosmetics
[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 */
903d58f6
MN
42#include "internal.h"
43#include "dsputil.h"
44#include "avcodec.h"
45#include "mpegvideo.h"
46#include "h264.h"
47
a7d2861d 48#include "h264data.h" // FIXME FIXME FIXME
903d58f6 49
188d3c51 50#include "h264_mvpred.h"
903d58f6
MN
51#include "golomb.h"
52#include "rectangle.h"
53#include "vdpau_internal.h"
54
b250f9c6 55#if CONFIG_ZLIB
c4864924
BC
56#include <zlib.h>
57#endif
58
2be3fe39
DB
59#include "svq1.h"
60
8b82a956 61/**
ba87f080 62 * @file
8b82a956
MN
63 * svq3 decoder.
64 */
65
8dfc6d1f
BC
66typedef struct {
67 H264Context h;
68 int halfpel_flag;
69 int thirdpel_flag;
70 int unknown_flag;
71 int next_slice_index;
72 uint32_t watermark_key;
73} SVQ3Context;
74
115329f1
DB
75#define FULLPEL_MODE 1
76#define HALFPEL_MODE 2
94d44f45 77#define THIRDPEL_MODE 3
2e26c8d2 78#define PREDICT_MODE 4
115329f1 79
f7a8c179 80/* dual scan (from some older h264 draft)
a7d2861d
DB
81 * o-->o-->o o
82 * | /|
83 * o o o / o
84 * | / | |/ |
85 * o o o o
86 * /
87 * o-->o-->o-->o
88 */
76de302d 89static const uint8_t svq3_scan[16] = {
a7d2861d
DB
90 0 + 0 * 4, 1 + 0 * 4, 2 + 0 * 4, 2 + 1 * 4,
91 2 + 2 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4,
92 0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 1 + 2 * 4,
93 0 + 3 * 4, 1 + 3 * 4, 2 + 3 * 4, 3 + 3 * 4,
8b82a956
MN
94};
95
96static const uint8_t svq3_pred_0[25][2] = {
76de302d
DB
97 { 0, 0 },
98 { 1, 0 }, { 0, 1 },
99 { 0, 2 }, { 1, 1 }, { 2, 0 },
100 { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
101 { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
102 { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
103 { 2, 4 }, { 3, 3 }, { 4, 2 },
104 { 4, 3 }, { 3, 4 },
105 { 4, 4 }
8b82a956
MN
106};
107
108static const int8_t svq3_pred_1[6][6][5] = {
a7d2861d
DB
109 { { 2, -1, -1, -1, -1 }, { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 },
110 { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, { 1, 2, -1, -1, -1 } },
111 { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 },
112 { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } },
113 { { 2, 0, -1, -1, -1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 },
114 { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } },
115 { { 2, 0, -1, -1, -1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 },
116 { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } },
117 { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 },
118 { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } },
119 { { 0, 2, -1, -1, -1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 },
120 { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } },
8b82a956
MN
121};
122
a7d2861d
DB
123static const struct {
124 uint8_t run;
125 uint8_t level;
126} svq3_dct_tables[2][16] = {
76de302d
DB
127 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
128 { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
129 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
130 { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
8b82a956
MN
131};
132
133static const uint32_t svq3_dequant_coeff[32] = {
a7d2861d
DB
134 3881, 4351, 4890, 5481, 6154, 6914, 7761, 8718,
135 9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873,
136 24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683,
137 61694, 68745, 77615, 89113, 100253, 109366, 126635, 141533
8b82a956
MN
138};
139
a7d2861d
DB
140void ff_svq3_luma_dc_dequant_idct_c(DCTELEM *output, DCTELEM *input, int qp)
141{
76de302d 142 const int qmul = svq3_dequant_coeff[qp];
8b82a956
MN
143#define stride 16
144 int i;
145 int temp[16];
a7d2861d
DB
146 static const uint8_t x_offset[4] = { 0, 1 * stride, 4 * stride, 5 * stride };
147
148 for (i = 0; i < 4; i++) {
149 const int z0 = 13 * (input[4 * i + 0] + input[4 * i + 2]);
150 const int z1 = 13 * (input[4 * i + 0] - input[4 * i + 2]);
151 const int z2 = 7 * input[4 * i + 1] - 17 * input[4 * i + 3];
152 const int z3 = 17 * input[4 * i + 1] + 7 * input[4 * i + 3];
153
154 temp[4 * i + 0] = z0 + z3;
155 temp[4 * i + 1] = z1 + z2;
156 temp[4 * i + 2] = z1 - z2;
157 temp[4 * i + 3] = z0 - z3;
8b82a956
MN
158 }
159
a7d2861d
DB
160 for (i = 0; i < 4; i++) {
161 const int offset = x_offset[i];
162 const int z0 = 13 * (temp[4 * 0 + i] + temp[4 * 2 + i]);
163 const int z1 = 13 * (temp[4 * 0 + i] - temp[4 * 2 + i]);
164 const int z2 = 7 * temp[4 * 1 + i] - 17 * temp[4 * 3 + i];
165 const int z3 = 17 * temp[4 * 1 + i] + 7 * temp[4 * 3 + i];
166
167 output[stride * 0 + offset] = ((z0 + z3) * qmul + 0x80000) >> 20;
168 output[stride * 2 + offset] = ((z1 + z2) * qmul + 0x80000) >> 20;
169 output[stride * 8 + offset] = ((z1 - z2) * qmul + 0x80000) >> 20;
170 output[stride * 10 + offset] = ((z0 - z3) * qmul + 0x80000) >> 20;
8b82a956
MN
171 }
172}
173#undef stride
174
a7d2861d
DB
175void ff_svq3_add_idct_c(uint8_t *dst, DCTELEM *block,
176 int stride, int qp, int dc)
7f8205da 177{
76de302d 178 const int qmul = svq3_dequant_coeff[qp];
8b82a956 179 int i;
8b82a956
MN
180
181 if (dc) {
a7d2861d
DB
182 dc = 13 * 13 * ((dc == 1) ? 1538 * block[0]
183 : ((qmul * (block[0] >> 3)) / 2));
8b82a956
MN
184 block[0] = 0;
185 }
186
76de302d 187 for (i = 0; i < 4; i++) {
a7d2861d
DB
188 const int z0 = 13 * (block[0 + 4 * i] + block[2 + 4 * i]);
189 const int z1 = 13 * (block[0 + 4 * i] - block[2 + 4 * i]);
190 const int z2 = 7 * block[1 + 4 * i] - 17 * block[3 + 4 * i];
191 const int z3 = 17 * block[1 + 4 * i] + 7 * block[3 + 4 * i];
192
193 block[0 + 4 * i] = z0 + z3;
194 block[1 + 4 * i] = z1 + z2;
195 block[2 + 4 * i] = z1 - z2;
196 block[3 + 4 * i] = z0 - z3;
8b82a956
MN
197 }
198
76de302d 199 for (i = 0; i < 4; i++) {
a7d2861d
DB
200 const int z0 = 13 * (block[i + 4 * 0] + block[i + 4 * 2]);
201 const int z1 = 13 * (block[i + 4 * 0] - block[i + 4 * 2]);
202 const int z2 = 7 * block[i + 4 * 1] - 17 * block[i + 4 * 3];
203 const int z3 = 17 * block[i + 4 * 1] + 7 * block[i + 4 * 3];
76de302d
DB
204 const int rr = (dc + 0x80000);
205
a7d2861d
DB
206 dst[i + stride * 0] = av_clip_uint8(dst[i + stride * 0] + (((z0 + z3) * qmul + rr) >> 20));
207 dst[i + stride * 1] = av_clip_uint8(dst[i + stride * 1] + (((z1 + z2) * qmul + rr) >> 20));
208 dst[i + stride * 2] = av_clip_uint8(dst[i + stride * 2] + (((z1 - z2) * qmul + rr) >> 20));
209 dst[i + stride * 3] = av_clip_uint8(dst[i + stride * 3] + (((z0 - z3) * qmul + rr) >> 20));
8b82a956
MN
210 }
211}
212
7f8205da
DB
213static inline int svq3_decode_block(GetBitContext *gb, DCTELEM *block,
214 int index, const int type)
215{
76de302d
DB
216 static const uint8_t *const scan_patterns[4] =
217 { luma_dc_zigzag_scan, zigzag_scan, svq3_scan, chroma_dc_scan };
8b82a956 218
76de302d 219 int run, level, sign, vlc, limit;
a7d2861d 220 const int intra = (3 * type) >> 2;
76de302d 221 const uint8_t *const scan = scan_patterns[type];
8b82a956 222
76de302d
DB
223 for (limit = (16 >> intra); index < 16; index = limit, limit += 8) {
224 for (; (vlc = svq3_get_ue_golomb(gb)) != 0; index++) {
a7d2861d
DB
225 if (vlc == INVALID_VLC)
226 return -1;
227
228 sign = (vlc & 0x1) - 1;
229 vlc = (vlc + 1) >> 1;
230
231 if (type == 3) {
232 if (vlc < 3) {
233 run = 0;
234 level = vlc;
235 } else if (vlc < 4) {
236 run = 1;
237 level = 1;
238 } else {
239 run = (vlc & 0x3);
240 level = ((vlc + 9) >> 2) - run;
241 }
242 } else {
243 if (vlc < 16) {
244 run = svq3_dct_tables[intra][vlc].run;
245 level = svq3_dct_tables[intra][vlc].level;
246 } else if (intra) {
247 run = (vlc & 0x7);
248 level = (vlc >> 3) +
249 ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
250 } else {
251 run = (vlc & 0xF);
252 level = (vlc >> 4) +
253 ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
254 }
255 }
8b82a956 256
a7d2861d
DB
257 if ((index += run) >= limit)
258 return -1;
259
260 block[scan[index]] = (level ^ sign) - sign;
76de302d 261 }
8b82a956 262
76de302d
DB
263 if (type != 2) {
264 break;
265 }
8b82a956 266 }
8b82a956 267
76de302d 268 return 0;
8b82a956
MN
269}
270
7f8205da
DB
271static inline void svq3_mc_dir_part(MpegEncContext *s,
272 int x, int y, int width, int height,
273 int mx, int my, int dxy,
274 int thirdpel, int dir, int avg)
275{
76de302d
DB
276 const Picture *pic = (dir == 0) ? &s->last_picture : &s->next_picture;
277 uint8_t *src, *dest;
278 int i, emu = 0;
a7d2861d 279 int blocksize = 2 - (width >> 3); // 16->0, 8->1, 4->2
76de302d
DB
280
281 mx += x;
282 my += y;
8b82a956 283
76de302d
DB
284 if (mx < 0 || mx >= (s->h_edge_pos - width - 1) ||
285 my < 0 || my >= (s->v_edge_pos - height - 1)) {
a7d2861d 286 if ((s->flags & CODEC_FLAG_EMU_EDGE))
76de302d 287 emu = 1;
8b82a956 288
a7d2861d
DB
289 mx = av_clip(mx, -16, (s->h_edge_pos - width + 15));
290 my = av_clip(my, -16, (s->v_edge_pos - height + 15));
8b82a956
MN
291 }
292
76de302d 293 /* form component predictions */
a7d2861d
DB
294 dest = s->current_picture.f.data[0] + x + y * s->linesize;
295 src = pic->f.data[0] + mx + my * s->linesize;
76de302d
DB
296
297 if (emu) {
a7d2861d
DB
298 s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize,
299 (width + 1), (height + 1),
300 mx, my, s->h_edge_pos, s->v_edge_pos);
a7d3e772 301 src = s->edge_emu_buffer;
8b82a956 302 }
76de302d 303 if (thirdpel)
a7d2861d
DB
304 (avg ? s->dsp.avg_tpel_pixels_tab
305 : s->dsp.put_tpel_pixels_tab)[dxy](dest, src, s->linesize,
306 width, height);
76de302d 307 else
a7d2861d
DB
308 (avg ? s->dsp.avg_pixels_tab
309 : s->dsp.put_pixels_tab)[blocksize][dxy](dest, src, s->linesize,
310 height);
76de302d
DB
311
312 if (!(s->flags & CODEC_FLAG_GRAY)) {
313 mx = (mx + (mx < (int) x)) >> 1;
314 my = (my + (my < (int) y)) >> 1;
315 width = (width >> 1);
316 height = (height >> 1);
317 blocksize++;
318
319 for (i = 1; i < 3; i++) {
657ccb5a
DB
320 dest = s->current_picture.f.data[i] + (x >> 1) + (y >> 1) * s->uvlinesize;
321 src = pic->f.data[i] + mx + my * s->uvlinesize;
76de302d
DB
322
323 if (emu) {
a7d2861d
DB
324 s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->uvlinesize,
325 (width + 1), (height + 1),
326 mx, my, (s->h_edge_pos >> 1),
327 (s->v_edge_pos >> 1));
76de302d
DB
328 src = s->edge_emu_buffer;
329 }
330 if (thirdpel)
a7d2861d
DB
331 (avg ? s->dsp.avg_tpel_pixels_tab
332 : s->dsp.put_tpel_pixels_tab)[dxy](dest, src,
333 s->uvlinesize,
334 width, height);
76de302d 335 else
a7d2861d
DB
336 (avg ? s->dsp.avg_pixels_tab
337 : s->dsp.put_pixels_tab)[blocksize][dxy](dest, src,
338 s->uvlinesize,
339 height);
76de302d
DB
340 }
341 }
8b82a956
MN
342}
343
a7d2861d
DB
344static inline int svq3_mc_dir(H264Context *h, int size, int mode,
345 int dir, int avg)
7f8205da 346{
76de302d 347 int i, j, k, mx, my, dx, dy, x, y;
a7d2861d
DB
348 MpegEncContext *const s = (MpegEncContext *)h;
349 const int part_width = ((size & 5) == 4) ? 4 : 16 >> (size & 1);
350 const int part_height = 16 >> ((unsigned)(size + 1) / 3);
351 const int extra_width = (mode == PREDICT_MODE) ? -16 * 6 : 0;
352 const int h_edge_pos = 6 * (s->h_edge_pos - part_width) - extra_width;
353 const int v_edge_pos = 6 * (s->v_edge_pos - part_height) - extra_width;
354
355 for (i = 0; i < 16; i += part_height)
76de302d 356 for (j = 0; j < 16; j += part_width) {
a7d2861d
DB
357 const int b_xy = (4 * s->mb_x + (j >> 2)) +
358 (4 * s->mb_y + (i >> 2)) * h->b_stride;
76de302d 359 int dxy;
a7d2861d
DB
360 x = 16 * s->mb_x + j;
361 y = 16 * s->mb_y + i;
362 k = ((j >> 2) & 1) + ((i >> 1) & 2) +
363 ((j >> 1) & 4) + (i & 8);
76de302d
DB
364
365 if (mode != PREDICT_MODE) {
366 pred_motion(h, k, (part_width >> 2), dir, 1, &mx, &my);
367 } else {
657ccb5a
DB
368 mx = s->next_picture.f.motion_val[0][b_xy][0] << 1;
369 my = s->next_picture.f.motion_val[0][b_xy][1] << 1;
76de302d
DB
370
371 if (dir == 0) {
a7d2861d
DB
372 mx = ((mx * h->frame_num_offset) /
373 h->prev_frame_num_offset + 1) >> 1;
374 my = ((my * h->frame_num_offset) /
375 h->prev_frame_num_offset + 1) >> 1;
76de302d 376 } else {
a7d2861d
DB
377 mx = ((mx * (h->frame_num_offset - h->prev_frame_num_offset)) /
378 h->prev_frame_num_offset + 1) >> 1;
379 my = ((my * (h->frame_num_offset - h->prev_frame_num_offset)) /
380 h->prev_frame_num_offset + 1) >> 1;
76de302d
DB
381 }
382 }
383
384 /* clip motion vector prediction to frame border */
a7d2861d
DB
385 mx = av_clip(mx, extra_width - 6 * x, h_edge_pos - 6 * x);
386 my = av_clip(my, extra_width - 6 * y, v_edge_pos - 6 * y);
76de302d
DB
387
388 /* get (optional) motion vector differential */
389 if (mode == PREDICT_MODE) {
390 dx = dy = 0;
391 } else {
392 dy = svq3_get_se_golomb(&s->gb);
393 dx = svq3_get_se_golomb(&s->gb);
394
395 if (dx == INVALID_VLC || dy == INVALID_VLC) {
396 av_log(h->s.avctx, AV_LOG_ERROR, "invalid MV vlc\n");
397 return -1;
398 }
399 }
400
401 /* compute motion vector */
402 if (mode == THIRDPEL_MODE) {
403 int fx, fy;
a7d2861d
DB
404 mx = ((mx + 1) >> 1) + dx;
405 my = ((my + 1) >> 1) + dy;
406 fx = ((unsigned)(mx + 0x3000)) / 3 - 0x1000;
407 fy = ((unsigned)(my + 0x3000)) / 3 - 0x1000;
408 dxy = (mx - 3 * fx) + 4 * (my - 3 * fy);
409
410 svq3_mc_dir_part(s, x, y, part_width, part_height,
411 fx, fy, dxy, 1, dir, avg);
76de302d
DB
412 mx += mx;
413 my += my;
414 } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) {
a7d2861d
DB
415 mx = ((unsigned)(mx + 1 + 0x3000)) / 3 + dx - 0x1000;
416 my = ((unsigned)(my + 1 + 0x3000)) / 3 + dy - 0x1000;
417 dxy = (mx & 1) + 2 * (my & 1);
76de302d 418
a7d2861d
DB
419 svq3_mc_dir_part(s, x, y, part_width, part_height,
420 mx >> 1, my >> 1, dxy, 0, dir, avg);
76de302d
DB
421 mx *= 3;
422 my *= 3;
423 } else {
a7d2861d
DB
424 mx = ((unsigned)(mx + 3 + 0x6000)) / 6 + dx - 0x1000;
425 my = ((unsigned)(my + 3 + 0x6000)) / 6 + dy - 0x1000;
76de302d 426
a7d2861d
DB
427 svq3_mc_dir_part(s, x, y, part_width, part_height,
428 mx, my, 0, 0, dir, avg);
76de302d
DB
429 mx *= 6;
430 my *= 6;
431 }
432
433 /* update mv_cache */
434 if (mode != PREDICT_MODE) {
a7d2861d 435 int32_t mv = pack16to32(mx, my);
76de302d
DB
436
437 if (part_height == 8 && i < 8) {
a7d2861d 438 AV_WN32A(h->mv_cache[dir][scan8[k] + 1 * 8], mv);
76de302d 439
a7d2861d
DB
440 if (part_width == 8 && j < 8)
441 AV_WN32A(h->mv_cache[dir][scan8[k] + 1 + 1 * 8], mv);
76de302d 442 }
a7d2861d 443 if (part_width == 8 && j < 8)
366b72f1 444 AV_WN32A(h->mv_cache[dir][scan8[k] + 1], mv);
a7d2861d 445 if (part_width == 4 || part_height == 4)
366b72f1 446 AV_WN32A(h->mv_cache[dir][scan8[k]], mv);
76de302d
DB
447 }
448
449 /* write back motion vectors */
657ccb5a
DB
450 fill_rectangle(s->current_picture.f.motion_val[dir][b_xy],
451 part_width >> 2, part_height >> 2, h->b_stride,
452 pack16to32(mx, my), 4);
bb270c08 453 }
2e26c8d2 454
76de302d 455 return 0;
2e26c8d2
MM
456}
457
8dfc6d1f 458static int svq3_decode_mb(SVQ3Context *svq3, unsigned int mb_type)
7f8205da 459{
8dfc6d1f 460 H264Context *h = &svq3->h;
76de302d
DB
461 int i, j, k, m, dir, mode;
462 int cbp = 0;
463 uint32_t vlc;
464 int8_t *top, *left;
a7d2861d
DB
465 MpegEncContext *const s = (MpegEncContext *)h;
466 const int mb_xy = h->mb_xy;
467 const int b_xy = 4 * s->mb_x + 4 * s->mb_y * h->b_stride;
76de302d
DB
468
469 h->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
470 h->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
471 h->topright_samples_available = 0xFFFF;
472
473 if (mb_type == 0) { /* SKIP */
a7d2861d
DB
474 if (s->pict_type == AV_PICTURE_TYPE_P ||
475 s->next_picture.f.mb_type[mb_xy] == -1) {
476 svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
477 0, 0, 0, 0, 0, 0);
76de302d 478
a7d2861d
DB
479 if (s->pict_type == AV_PICTURE_TYPE_B)
480 svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
481 0, 0, 0, 0, 1, 1);
76de302d
DB
482
483 mb_type = MB_TYPE_SKIP;
484 } else {
657ccb5a 485 mb_type = FFMIN(s->next_picture.f.mb_type[mb_xy], 6);
76de302d
DB
486 if (svq3_mc_dir(h, mb_type, PREDICT_MODE, 0, 0) < 0)
487 return -1;
488 if (svq3_mc_dir(h, mb_type, PREDICT_MODE, 1, 1) < 0)
489 return -1;
8b82a956 490
76de302d 491 mb_type = MB_TYPE_16x16;
bb270c08 492 }
76de302d 493 } else if (mb_type < 8) { /* INTER */
a7d2861d 494 if (svq3->thirdpel_flag && svq3->halfpel_flag == !get_bits1(&s->gb))
76de302d 495 mode = THIRDPEL_MODE;
a7d2861d
DB
496 else if (svq3->halfpel_flag &&
497 svq3->thirdpel_flag == !get_bits1(&s->gb))
76de302d 498 mode = HALFPEL_MODE;
a7d2861d 499 else
76de302d 500 mode = FULLPEL_MODE;
8b82a956 501
76de302d
DB
502 /* fill caches */
503 /* note ref_cache should contain here:
a7d2861d
DB
504 * ????????
505 * ???11111
506 * N??11111
507 * N??11111
508 * N??11111
509 */
76de302d
DB
510
511 for (m = 0; m < 2; m++) {
a7d2861d
DB
512 if (s->mb_x > 0 && h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6] != -1) {
513 for (i = 0; i < 4; i++)
514 AV_COPY32(h->mv_cache[m][scan8[0] - 1 + i * 8],
515 s->current_picture.f.motion_val[m][b_xy - 1 + i * h->b_stride]);
76de302d 516 } else {
a7d2861d
DB
517 for (i = 0; i < 4; i++)
518 AV_ZERO32(h->mv_cache[m][scan8[0] - 1 + i * 8]);
76de302d
DB
519 }
520 if (s->mb_y > 0) {
a7d2861d
DB
521 memcpy(h->mv_cache[m][scan8[0] - 1 * 8],
522 s->current_picture.f.motion_val[m][b_xy - h->b_stride],
523 4 * 2 * sizeof(int16_t));
524 memset(&h->ref_cache[m][scan8[0] - 1 * 8],
525 (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);
76de302d
DB
526
527 if (s->mb_x < (s->mb_width - 1)) {
a7d2861d
DB
528 AV_COPY32(h->mv_cache[m][scan8[0] + 4 - 1 * 8],
529 s->current_picture.f.motion_val[m][b_xy - h->b_stride + 4]);
530 h->ref_cache[m][scan8[0] + 4 - 1 * 8] =
531 (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride + 1] + 6] == -1 ||
532 h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1;
533 } else
534 h->ref_cache[m][scan8[0] + 4 - 1 * 8] = PART_NOT_AVAILABLE;
76de302d 535 if (s->mb_x > 0) {
a7d2861d
DB
536 AV_COPY32(h->mv_cache[m][scan8[0] - 1 - 1 * 8],
537 s->current_picture.f.motion_val[m][b_xy - h->b_stride - 1]);
538 h->ref_cache[m][scan8[0] - 1 - 1 * 8] =
539 (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] == -1) ? PART_NOT_AVAILABLE : 1;
540 } else
541 h->ref_cache[m][scan8[0] - 1 - 1 * 8] = PART_NOT_AVAILABLE;
542 } else
543 memset(&h->ref_cache[m][scan8[0] - 1 * 8 - 1],
544 PART_NOT_AVAILABLE, 8);
76de302d 545
975a1447 546 if (s->pict_type != AV_PICTURE_TYPE_B)
76de302d 547 break;
bb270c08 548 }
8b82a956 549
76de302d 550 /* decode motion vector(s) and form prediction(s) */
975a1447 551 if (s->pict_type == AV_PICTURE_TYPE_P) {
76de302d
DB
552 if (svq3_mc_dir(h, (mb_type - 1), mode, 0, 0) < 0)
553 return -1;
975a1447 554 } else { /* AV_PICTURE_TYPE_B */
a7d2861d 555 if (mb_type != 2)
76de302d
DB
556 if (svq3_mc_dir(h, 0, mode, 0, 0) < 0)
557 return -1;
a7d2861d
DB
558 else
559 for (i = 0; i < 4; i++)
560 memset(s->current_picture.f.motion_val[0][b_xy + i * h->b_stride],
561 0, 4 * 2 * sizeof(int16_t));
562 if (mb_type != 1)
76de302d
DB
563 if (svq3_mc_dir(h, 0, mode, 1, (mb_type == 3)) < 0)
564 return -1;
a7d2861d
DB
565 else
566 for (i = 0; i < 4; i++)
567 memset(s->current_picture.f.motion_val[1][b_xy + i * h->b_stride],
568 0, 4 * 2 * sizeof(int16_t));
bb270c08 569 }
8b82a956 570
76de302d
DB
571 mb_type = MB_TYPE_16x16;
572 } else if (mb_type == 8 || mb_type == 33) { /* INTRA4x4 */
a7d2861d 573 memset(h->intra4x4_pred_mode_cache, -1, 8 * 5 * sizeof(int8_t));
76de302d
DB
574
575 if (mb_type == 8) {
576 if (s->mb_x > 0) {
a7d2861d
DB
577 for (i = 0; i < 4; i++)
578 h->intra4x4_pred_mode_cache[scan8[0] - 1 + i * 8] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6 - i];
579 if (h->intra4x4_pred_mode_cache[scan8[0] - 1] == -1)
76de302d 580 h->left_samples_available = 0x5F5F;
76de302d
DB
581 }
582 if (s->mb_y > 0) {
a7d2861d
DB
583 h->intra4x4_pred_mode_cache[4 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride] + 0];
584 h->intra4x4_pred_mode_cache[5 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride] + 1];
585 h->intra4x4_pred_mode_cache[6 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride] + 2];
586 h->intra4x4_pred_mode_cache[7 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride] + 3];
76de302d 587
a7d2861d 588 if (h->intra4x4_pred_mode_cache[4 + 8 * 0] == -1)
76de302d 589 h->top_samples_available = 0x33FF;
76de302d
DB
590 }
591
592 /* decode prediction codes for luma blocks */
a7d2861d 593 for (i = 0; i < 16; i += 2) {
76de302d
DB
594 vlc = svq3_get_ue_golomb(&s->gb);
595
a7d2861d 596 if (vlc >= 25) {
76de302d
DB
597 av_log(h->s.avctx, AV_LOG_ERROR, "luma prediction:%d\n", vlc);
598 return -1;
599 }
600
a7d2861d
DB
601 left = &h->intra4x4_pred_mode_cache[scan8[i] - 1];
602 top = &h->intra4x4_pred_mode_cache[scan8[i] - 8];
76de302d
DB
603
604 left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
605 left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
606
a7d2861d 607 if (left[1] == -1 || left[2] == -1) {
76de302d
DB
608 av_log(h->s.avctx, AV_LOG_ERROR, "weird prediction\n");
609 return -1;
610 }
611 }
612 } else { /* mb_type == 33, DC_128_PRED block type */
a7d2861d
DB
613 for (i = 0; i < 4; i++)
614 memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_PRED, 4);
884182b3 615 }
8b82a956 616
3b7ebeb4 617 write_back_intra_pred_mode(h);
8b82a956 618
76de302d 619 if (mb_type == 8) {
2bedc0e8 620 ff_h264_check_intra4x4_pred_mode(h);
da3b9756 621
76de302d
DB
622 h->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
623 h->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
624 } else {
a7d2861d
DB
625 for (i = 0; i < 4; i++)
626 memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_128_PRED, 4);
da3b9756 627
76de302d
DB
628 h->top_samples_available = 0x33FF;
629 h->left_samples_available = 0x5F5F;
630 }
da3b9756 631
76de302d
DB
632 mb_type = MB_TYPE_INTRA4x4;
633 } else { /* INTRA16x16 */
634 dir = i_mb_type_info[mb_type - 8].pred_mode;
a7d2861d 635 dir = (dir >> 1) ^ 3 * (dir & 1) ^ 1;
8b82a956 636
a7d2861d 637 if ((h->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(h, dir, 0)) == -1) {
76de302d
DB
638 av_log(h->s.avctx, AV_LOG_ERROR, "check_intra_pred_mode = -1\n");
639 return -1;
640 }
8b82a956 641
a7d2861d 642 cbp = i_mb_type_info[mb_type - 8].cbp;
76de302d 643 mb_type = MB_TYPE_INTRA16x16;
884182b3 644 }
8b82a956 645
975a1447 646 if (!IS_INTER(mb_type) && s->pict_type != AV_PICTURE_TYPE_I) {
a7d2861d
DB
647 for (i = 0; i < 4; i++)
648 memset(s->current_picture.f.motion_val[0][b_xy + i * h->b_stride],
649 0, 4 * 2 * sizeof(int16_t));
975a1447 650 if (s->pict_type == AV_PICTURE_TYPE_B) {
a7d2861d
DB
651 for (i = 0; i < 4; i++)
652 memset(s->current_picture.f.motion_val[1][b_xy + i * h->b_stride],
653 0, 4 * 2 * sizeof(int16_t));
76de302d 654 }
8b82a956 655 }
76de302d 656 if (!IS_INTRA4x4(mb_type)) {
a7d2861d 657 memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy], DC_PRED, 8);
da3b9756 658 }
975a1447 659 if (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B) {
a7d2861d
DB
660 memset(h->non_zero_count_cache + 8, 0, 14 * 8 * sizeof(uint8_t));
661 s->dsp.clear_blocks(h->mb + 0);
662 s->dsp.clear_blocks(h->mb + 384);
884182b3 663 }
2e26c8d2 664
a7d2861d
DB
665 if (!IS_INTRA16x16(mb_type) &&
666 (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B)) {
667 if ((vlc = svq3_get_ue_golomb(&s->gb)) >= 48) {
76de302d
DB
668 av_log(h->s.avctx, AV_LOG_ERROR, "cbp_vlc=%d\n", vlc);
669 return -1;
670 }
8b82a956 671
a7d2861d
DB
672 cbp = IS_INTRA(mb_type) ? golomb_to_intra4x4_cbp[vlc]
673 : golomb_to_inter_cbp[vlc];
884182b3 674 }
a7d2861d
DB
675 if (IS_INTRA16x16(mb_type) ||
676 (s->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
76de302d 677 s->qscale += svq3_get_se_golomb(&s->gb);
8b82a956 678
a7d2861d 679 if (s->qscale > 31u) {
76de302d 680 av_log(h->s.avctx, AV_LOG_ERROR, "qscale:%d\n", s->qscale);
bb270c08 681 return -1;
bb270c08 682 }
8b82a956 683 }
76de302d 684 if (IS_INTRA16x16(mb_type)) {
a7d2861d
DB
685 AV_ZERO128(h->mb_luma_dc[0] + 0);
686 AV_ZERO128(h->mb_luma_dc[0] + 8);
687 if (svq3_decode_block(&s->gb, h->mb_luma_dc[0], 0, 1)) {
688 av_log(h->s.avctx, AV_LOG_ERROR,
689 "error while decoding intra luma dc\n");
76de302d 690 return -1;
884182b3 691 }
76de302d 692 }
8b82a956 693
76de302d
DB
694 if (cbp) {
695 const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
a7d2861d 696 const int type = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
76de302d 697
a7d2861d 698 for (i = 0; i < 4; i++)
76de302d
DB
699 if ((cbp & (1 << i))) {
700 for (j = 0; j < 4; j++) {
a7d2861d
DB
701 k = index ? (1 * (j & 1) + 2 * (i & 1) +
702 2 * (j & 2) + 4 * (i & 2))
703 : (4 * i + j);
704 h->non_zero_count_cache[scan8[k]] = 1;
705
706 if (svq3_decode_block(&s->gb, &h->mb[16 * k], index, type)) {
707 av_log(h->s.avctx, AV_LOG_ERROR,
708 "error while decoding block\n");
76de302d
DB
709 return -1;
710 }
711 }
712 }
8b82a956 713
76de302d 714 if ((cbp & 0x30)) {
a7d2861d
DB
715 for (i = 1; i < 3; ++i)
716 if (svq3_decode_block(&s->gb, &h->mb[16 * 16 * i], 0, 3)) {
717 av_log(h->s.avctx, AV_LOG_ERROR,
718 "error while decoding chroma dc block\n");
719 return -1;
720 }
76de302d
DB
721
722 if ((cbp & 0x20)) {
11177a4d
JGG
723 for (i = 1; i < 3; i++) {
724 for (j = 0; j < 4; j++) {
a7d2861d
DB
725 k = 16 * i + j;
726 h->non_zero_count_cache[scan8[k]] = 1;
11177a4d 727
a7d2861d
DB
728 if (svq3_decode_block(&s->gb, &h->mb[16 * k], 1, 1)) {
729 av_log(h->s.avctx, AV_LOG_ERROR,
730 "error while decoding chroma ac block\n");
11177a4d
JGG
731 return -1;
732 }
76de302d
DB
733 }
734 }
735 }
bb270c08 736 }
8b82a956 737 }
8b82a956 738
a7d2861d 739 h->cbp = cbp;
657ccb5a 740 s->current_picture.f.mb_type[mb_xy] = mb_type;
8b82a956 741
a7d2861d 742 if (IS_INTRA(mb_type))
45b7bd7c 743 h->chroma_pred_mode = ff_h264_check_intra_pred_mode(h, DC_PRED8x8, 1);
8b82a956 744
76de302d 745 return 0;
8b82a956
MN
746}
747
8dfc6d1f 748static int svq3_decode_slice_header(AVCodecContext *avctx)
7f8205da 749{
8dfc6d1f 750 SVQ3Context *svq3 = avctx->priv_data;
a7d2861d 751 H264Context *h = &svq3->h;
8dfc6d1f 752 MpegEncContext *s = &h->s;
a7d2861d 753 const int mb_xy = h->mb_xy;
76de302d 754 int i, header;
da3b9756 755
76de302d 756 header = get_bits(&s->gb, 8);
da3b9756 757
76de302d
DB
758 if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
759 /* TODO: what? */
8dfc6d1f 760 av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
76de302d
DB
761 return -1;
762 } else {
763 int length = (header >> 5) & 3;
da3b9756 764
a7d2861d
DB
765 svq3->next_slice_index = get_bits_count(&s->gb) +
766 8 * show_bits(&s->gb, 8 * length) +
767 8 * length;
da3b9756 768
8dfc6d1f
BC
769 if (svq3->next_slice_index > s->gb.size_in_bits) {
770 av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
76de302d 771 return -1;
a7d2861d 772 }
da3b9756 773
a7d2861d 774 s->gb.size_in_bits = svq3->next_slice_index - 8 * (length - 1);
76de302d 775 skip_bits(&s->gb, 8);
da3b9756 776
8dfc6d1f 777 if (svq3->watermark_key) {
a7d2861d
DB
778 uint32_t header = AV_RL32(&s->gb.buffer[(get_bits_count(&s->gb) >> 3) + 1]);
779 AV_WL32(&s->gb.buffer[(get_bits_count(&s->gb) >> 3) + 1],
780 header ^ svq3->watermark_key);
76de302d
DB
781 }
782 if (length > 0) {
783 memcpy((uint8_t *) &s->gb.buffer[get_bits_count(&s->gb) >> 3],
784 &s->gb.buffer[s->gb.size_in_bits >> 3], (length - 1));
785 }
db794291 786 skip_bits_long(&s->gb, 0);
da3b9756 787 }
da3b9756 788
a7d2861d 789 if ((i = svq3_get_ue_golomb(&s->gb)) == INVALID_VLC || i >= 3) {
76de302d
DB
790 av_log(h->s.avctx, AV_LOG_ERROR, "illegal slice type %d \n", i);
791 return -1;
792 }
da3b9756 793
76de302d 794 h->slice_type = golomb_to_pict_type[i];
da3b9756 795
76de302d 796 if ((header & 0x9F) == 2) {
a7d2861d
DB
797 i = (s->mb_num < 64) ? 6 : (1 + av_log2(s->mb_num - 1));
798 s->mb_skip_run = get_bits(&s->gb, i) -
799 (s->mb_x + (s->mb_y * s->mb_width));
76de302d
DB
800 } else {
801 skip_bits1(&s->gb);
802 s->mb_skip_run = 0;
803 }
da3b9756 804
a7d2861d
DB
805 h->slice_num = get_bits(&s->gb, 8);
806 s->qscale = get_bits(&s->gb, 5);
76de302d 807 s->adaptive_quant = get_bits1(&s->gb);
da3b9756 808
76de302d
DB
809 /* unknown fields */
810 skip_bits1(&s->gb);
da3b9756 811
a7d2861d 812 if (svq3->unknown_flag)
76de302d 813 skip_bits1(&s->gb);
da3b9756 814
76de302d
DB
815 skip_bits1(&s->gb);
816 skip_bits(&s->gb, 2);
da3b9756 817
a7d2861d 818 while (get_bits1(&s->gb))
76de302d 819 skip_bits(&s->gb, 8);
da3b9756 820
76de302d 821 /* reset intra predictors and invalidate motion vector references */
da3b9756 822 if (s->mb_x > 0) {
a7d2861d
DB
823 memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - 1] + 3,
824 -1, 4 * sizeof(int8_t));
825 memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - s->mb_x],
826 -1, 8 * sizeof(int8_t) * s->mb_x);
76de302d
DB
827 }
828 if (s->mb_y > 0) {
a7d2861d
DB
829 memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - s->mb_stride],
830 -1, 8 * sizeof(int8_t) * (s->mb_width - s->mb_x));
76de302d 831
a7d2861d
DB
832 if (s->mb_x > 0)
833 h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] = -1;
da3b9756 834 }
da3b9756 835
76de302d 836 return 0;
da3b9756
MM
837}
838
5ef251e5 839static av_cold int svq3_decode_init(AVCodecContext *avctx)
7f8205da 840{
8dfc6d1f 841 SVQ3Context *svq3 = avctx->priv_data;
a7d2861d 842 H264Context *h = &svq3->h;
8dfc6d1f 843 MpegEncContext *s = &h->s;
f4cca718 844 int m;
76de302d 845 unsigned char *extradata;
9e1db721 846 unsigned char *extradata_end;
76de302d 847 unsigned int size;
9e1db721 848 int marker_found = 0;
76de302d 849
903d58f6 850 if (ff_h264_decode_init(avctx) < 0)
f4cca718
BC
851 return -1;
852
a7d2861d
DB
853 s->flags = avctx->flags;
854 s->flags2 = avctx->flags2;
76de302d 855 s->unrestricted_mv = 1;
a7d2861d
DB
856 h->is_complex = 1;
857 avctx->pix_fmt = avctx->codec->pix_fmts[0];
76de302d
DB
858
859 if (!s->context_initialized) {
8dfc6d1f
BC
860 h->chroma_qp[0] = h->chroma_qp[1] = 4;
861
862 svq3->halfpel_flag = 1;
863 svq3->thirdpel_flag = 1;
864 svq3->unknown_flag = 0;
76de302d 865
76de302d 866 /* prowl for the "SEQH" marker in the extradata */
a7d2861d 867 extradata = (unsigned char *)avctx->extradata;
9e1db721
AC
868 extradata_end = avctx->extradata + avctx->extradata_size;
869 if (extradata) {
870 for (m = 0; m + 8 < avctx->extradata_size; m++) {
871 if (!memcmp(extradata, "SEQH", 4)) {
872 marker_found = 1;
873 break;
874 }
875 extradata++;
876 }
76de302d 877 }
8b82a956 878
76de302d 879 /* if a match was found, parse the extra data */
9e1db721 880 if (marker_found) {
76de302d 881 GetBitContext gb;
faccfeec 882 int frame_size_code;
8b82a956 883
76de302d 884 size = AV_RB32(&extradata[4]);
9e1db721
AC
885 if (size > extradata_end - extradata - 8)
886 return AVERROR_INVALIDDATA;
a7d2861d 887 init_get_bits(&gb, extradata + 8, size * 8);
8b82a956 888
76de302d 889 /* 'frame size code' and optional 'width, height' */
faccfeec
RB
890 frame_size_code = get_bits(&gb, 3);
891 switch (frame_size_code) {
a7d2861d
DB
892 case 0:
893 avctx->width = 160;
894 avctx->height = 120;
895 break;
896 case 1:
897 avctx->width = 128;
898 avctx->height = 96;
899 break;
900 case 2:
901 avctx->width = 176;
902 avctx->height = 144;
903 break;
904 case 3:
905 avctx->width = 352;
906 avctx->height = 288;
907 break;
908 case 4:
909 avctx->width = 704;
910 avctx->height = 576;
911 break;
912 case 5:
913 avctx->width = 240;
914 avctx->height = 180;
915 break;
916 case 6:
917 avctx->width = 320;
918 avctx->height = 240;
919 break;
920 case 7:
921 avctx->width = get_bits(&gb, 12);
922 avctx->height = get_bits(&gb, 12);
923 break;
76de302d 924 }
8b82a956 925
8dfc6d1f
BC
926 svq3->halfpel_flag = get_bits1(&gb);
927 svq3->thirdpel_flag = get_bits1(&gb);
8b82a956 928
76de302d
DB
929 /* unknown fields */
930 skip_bits1(&gb);
931 skip_bits1(&gb);
932 skip_bits1(&gb);
933 skip_bits1(&gb);
8b82a956 934
76de302d 935 s->low_delay = get_bits1(&gb);
1e002b60 936
76de302d
DB
937 /* unknown field */
938 skip_bits1(&gb);
939
a7d2861d 940 while (get_bits1(&gb))
76de302d 941 skip_bits(&gb, 8);
76de302d 942
a7d2861d 943 svq3->unknown_flag = get_bits1(&gb);
76de302d 944 avctx->has_b_frames = !s->low_delay;
8dfc6d1f 945 if (svq3->unknown_flag) {
b250f9c6 946#if CONFIG_ZLIB
76de302d
DB
947 unsigned watermark_width = svq3_get_ue_golomb(&gb);
948 unsigned watermark_height = svq3_get_ue_golomb(&gb);
a7d2861d
DB
949 int u1 = svq3_get_ue_golomb(&gb);
950 int u2 = get_bits(&gb, 8);
951 int u3 = get_bits(&gb, 2);
952 int u4 = svq3_get_ue_golomb(&gb);
953 unsigned long buf_len = watermark_width *
954 watermark_height * 4;
955 int offset = (get_bits_count(&gb) + 7) >> 3;
76de302d
DB
956 uint8_t *buf;
957
a7d2861d 958 if ((uint64_t)watermark_width * 4 > UINT_MAX / watermark_height)
76de302d
DB
959 return -1;
960
961 buf = av_malloc(buf_len);
a7d2861d
DB
962 av_log(avctx, AV_LOG_DEBUG, "watermark size: %dx%d\n",
963 watermark_width, watermark_height);
964 av_log(avctx, AV_LOG_DEBUG,
965 "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n",
966 u1, u2, u3, u4, offset);
967 if (uncompress(buf, &buf_len, extradata + 8 + offset,
968 size - offset) != Z_OK) {
969 av_log(avctx, AV_LOG_ERROR,
970 "could not uncompress watermark logo\n");
76de302d
DB
971 av_free(buf);
972 return -1;
973 }
8dfc6d1f 974 svq3->watermark_key = ff_svq1_packet_checksum(buf, buf_len, 0);
a7d2861d
DB
975 svq3->watermark_key = svq3->watermark_key << 16 |
976 svq3->watermark_key;
977 av_log(avctx, AV_LOG_DEBUG,
978 "watermark key %#x\n", svq3->watermark_key);
76de302d 979 av_free(buf);
1e002b60 980#else
a7d2861d
DB
981 av_log(avctx, AV_LOG_ERROR,
982 "this svq3 file contains watermark which need zlib support compiled in\n");
76de302d 983 return -1;
1e002b60 984#endif
76de302d
DB
985 }
986 }
cdca7c37
RB
987
988 s->width = avctx->width;
989 s->height = avctx->height;
990
efd29844 991 if (ff_MPV_common_init(s) < 0)
cdca7c37
RB
992 return -1;
993
a7d2861d 994 h->b_stride = 4 * s->mb_width;
cdca7c37 995
a3e215cd
DB
996 if (ff_h264_alloc_tables(h) < 0) {
997 av_log(avctx, AV_LOG_ERROR, "svq3 memory allocation failed\n");
998 return AVERROR(ENOMEM);
999 }
da3b9756 1000 }
76de302d 1001
f4cca718
BC
1002 return 0;
1003}
1004
a7d2861d
DB
1005static int svq3_decode_frame(AVCodecContext *avctx, void *data,
1006 int *data_size, AVPacket *avpkt)
f4cca718 1007{
7a00bbad 1008 const uint8_t *buf = avpkt->data;
a7d2861d
DB
1009 SVQ3Context *svq3 = avctx->priv_data;
1010 H264Context *h = &svq3->h;
1011 MpegEncContext *s = &h->s;
1012 int buf_size = avpkt->size;
f4cca718
BC
1013 int m, mb_type;
1014
76de302d
DB
1015 /* special case for last picture */
1016 if (buf_size == 0) {
1017 if (s->next_picture_ptr && !s->low_delay) {
324deaa2 1018 *(AVFrame *) data = s->next_picture.f;
76de302d 1019 s->next_picture_ptr = NULL;
a7d2861d 1020 *data_size = sizeof(AVFrame);
76de302d
DB
1021 }
1022 return 0;
da3b9756 1023 }
8b82a956 1024
a7d2861d 1025 init_get_bits(&s->gb, buf, 8 * buf_size);
8b82a956 1026
76de302d 1027 s->mb_x = s->mb_y = h->mb_xy = 0;
8b82a956 1028
8dfc6d1f 1029 if (svq3_decode_slice_header(avctx))
76de302d 1030 return -1;
8b82a956 1031
a7d2861d 1032 s->pict_type = h->slice_type;
76de302d 1033 s->picture_number = h->slice_num;
8b82a956 1034
a7d2861d
DB
1035 if (avctx->debug & FF_DEBUG_PICT_INFO)
1036 av_log(h->s.avctx, AV_LOG_DEBUG,
1037 "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
1038 av_get_picture_type_char(s->pict_type),
1039 svq3->halfpel_flag, svq3->thirdpel_flag,
76de302d 1040 s->adaptive_quant, s->qscale, h->slice_num);
8b82a956 1041
8ed2ae09 1042 /* for skipping the frame */
657ccb5a
DB
1043 s->current_picture.f.pict_type = s->pict_type;
1044 s->current_picture.f.key_frame = (s->pict_type == AV_PICTURE_TYPE_I);
76de302d
DB
1045
1046 /* Skip B-frames if we do not have reference frames. */
975a1447 1047 if (s->last_picture_ptr == NULL && s->pict_type == AV_PICTURE_TYPE_B)
76de302d 1048 return 0;
a7d2861d
DB
1049 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
1050 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
1051 avctx->skip_frame >= AVDISCARD_ALL)
76de302d
DB
1052 return 0;
1053
1054 if (s->next_p_frame_damaged) {
975a1447 1055 if (s->pict_type == AV_PICTURE_TYPE_B)
76de302d
DB
1056 return 0;
1057 else
1058 s->next_p_frame_damaged = 0;
1059 }
da3b9756 1060
903d58f6 1061 if (ff_h264_frame_start(h) < 0)
76de302d 1062 return -1;
8b82a956 1063
975a1447 1064 if (s->pict_type == AV_PICTURE_TYPE_B) {
76de302d 1065 h->frame_num_offset = (h->slice_num - h->prev_frame_num);
8b82a956 1066
a7d2861d 1067 if (h->frame_num_offset < 0)
76de302d 1068 h->frame_num_offset += 256;
a7d2861d
DB
1069 if (h->frame_num_offset == 0 ||
1070 h->frame_num_offset >= h->prev_frame_num_offset) {
76de302d
DB
1071 av_log(h->s.avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
1072 return -1;
1073 }
1074 } else {
a7d2861d
DB
1075 h->prev_frame_num = h->frame_num;
1076 h->frame_num = h->slice_num;
76de302d 1077 h->prev_frame_num_offset = (h->frame_num - h->prev_frame_num);
da3b9756 1078
a7d2861d 1079 if (h->prev_frame_num_offset < 0)
76de302d 1080 h->prev_frame_num_offset += 256;
da3b9756 1081 }
da3b9756 1082
a7d2861d 1083 for (m = 0; m < 2; m++) {
76de302d 1084 int i;
a7d2861d 1085 for (i = 0; i < 4; i++) {
76de302d
DB
1086 int j;
1087 for (j = -1; j < 4; j++)
a7d2861d 1088 h->ref_cache[m][scan8[0] + 8 * i + j] = 1;
76de302d 1089 if (i < 3)
a7d2861d 1090 h->ref_cache[m][scan8[0] + 8 * i + j] = PART_NOT_AVAILABLE;
76de302d 1091 }
da3b9756 1092 }
da3b9756 1093
76de302d
DB
1094 for (s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
1095 for (s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
a7d2861d 1096 h->mb_xy = s->mb_x + s->mb_y * s->mb_stride;
da3b9756 1097
a7d2861d
DB
1098 if ((get_bits_count(&s->gb) + 7) >= s->gb.size_in_bits &&
1099 ((get_bits_count(&s->gb) & 7) == 0 ||
1100 show_bits(&s->gb, (-get_bits_count(&s->gb) & 7)) == 0)) {
8dfc6d1f 1101 skip_bits(&s->gb, svq3->next_slice_index - get_bits_count(&s->gb));
a7d2861d 1102 s->gb.size_in_bits = 8 * buf_size;
da3b9756 1103
8dfc6d1f 1104 if (svq3_decode_slice_header(avctx))
76de302d 1105 return -1;
da3b9756 1106
76de302d
DB
1107 /* TODO: support s->mb_skip_run */
1108 }
da3b9756 1109
76de302d 1110 mb_type = svq3_get_ue_golomb(&s->gb);
da3b9756 1111
a7d2861d 1112 if (s->pict_type == AV_PICTURE_TYPE_I)
76de302d 1113 mb_type += 8;
a7d2861d 1114 else if (s->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4)
76de302d 1115 mb_type += 4;
93d06bd1 1116 if ((unsigned)mb_type > 33 || svq3_decode_mb(svq3, mb_type)) {
a7d2861d
DB
1117 av_log(h->s.avctx, AV_LOG_ERROR,
1118 "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
76de302d
DB
1119 return -1;
1120 }
8b82a956 1121
a7d2861d
DB
1122 if (mb_type != 0)
1123 ff_h264_hl_decode_mb(h);
8b82a956 1124
a7d2861d 1125 if (s->pict_type != AV_PICTURE_TYPE_B && !s->low_delay)
657ccb5a 1126 s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
975a1447 1127 (s->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
76de302d 1128 }
2e26c8d2 1129
a7d2861d 1130 ff_draw_horiz_band(s, 16 * s->mb_y, 16);
8b82a956 1131 }
4c701ac8 1132
efd29844 1133 ff_MPV_frame_end(s);
8b82a956 1134
a7d2861d
DB
1135 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay)
1136 *(AVFrame *)data = s->current_picture.f;
1137 else
1138 *(AVFrame *)data = s->last_picture.f;
da3b9756 1139
76de302d 1140 /* Do not output the last pic after seeking. */
a7d2861d 1141 if (s->last_picture_ptr || s->low_delay)
76de302d 1142 *data_size = sizeof(AVFrame);
da3b9756 1143
76de302d 1144 return buf_size;
8b82a956
MN
1145}
1146
8dfc6d1f
BC
1147static int svq3_decode_end(AVCodecContext *avctx)
1148{
1149 SVQ3Context *svq3 = avctx->priv_data;
a7d2861d 1150 H264Context *h = &svq3->h;
8dfc6d1f
BC
1151 MpegEncContext *s = &h->s;
1152
1153 ff_h264_free_context(h);
1154
efd29844 1155 ff_MPV_common_end(s);
8dfc6d1f
BC
1156
1157 return 0;
1158}
8b82a956 1159
d36beb3f 1160AVCodec ff_svq3_decoder = {
ec6402b7
AK
1161 .name = "svq3",
1162 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 1163 .id = AV_CODEC_ID_SVQ3,
ec6402b7
AK
1164 .priv_data_size = sizeof(SVQ3Context),
1165 .init = svq3_decode_init,
1166 .close = svq3_decode_end,
1167 .decode = svq3_decode_frame,
a7d2861d
DB
1168 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND |
1169 CODEC_CAP_DR1 |
00c3b67b
MS
1170 CODEC_CAP_DELAY,
1171 .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
a7d2861d
DB
1172 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,
1173 AV_PIX_FMT_NONE},
8b82a956 1174};