h264: remove a pointless if()
[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 59#include "svq1.h"
75d5156a 60#include "svq3.h"
2be3fe39 61
8b82a956 62/**
ba87f080 63 * @file
8b82a956
MN
64 * svq3 decoder.
65 */
66
8dfc6d1f
BC
67typedef struct {
68 H264Context h;
69 int halfpel_flag;
70 int thirdpel_flag;
71 int unknown_flag;
72 int next_slice_index;
73 uint32_t watermark_key;
74} SVQ3Context;
75
115329f1
DB
76#define FULLPEL_MODE 1
77#define HALFPEL_MODE 2
94d44f45 78#define THIRDPEL_MODE 3
2e26c8d2 79#define PREDICT_MODE 4
115329f1 80
f7a8c179 81/* dual scan (from some older h264 draft)
a7d2861d
DB
82 * o-->o-->o o
83 * | /|
84 * o o o / o
85 * | / | |/ |
86 * o o o o
87 * /
88 * o-->o-->o-->o
89 */
76de302d 90static const uint8_t svq3_scan[16] = {
a7d2861d
DB
91 0 + 0 * 4, 1 + 0 * 4, 2 + 0 * 4, 2 + 1 * 4,
92 2 + 2 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4,
93 0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 1 + 2 * 4,
94 0 + 3 * 4, 1 + 3 * 4, 2 + 3 * 4, 3 + 3 * 4,
8b82a956
MN
95};
96
97static const uint8_t svq3_pred_0[25][2] = {
76de302d
DB
98 { 0, 0 },
99 { 1, 0 }, { 0, 1 },
100 { 0, 2 }, { 1, 1 }, { 2, 0 },
101 { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
102 { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
103 { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
104 { 2, 4 }, { 3, 3 }, { 4, 2 },
105 { 4, 3 }, { 3, 4 },
106 { 4, 4 }
8b82a956
MN
107};
108
109static const int8_t svq3_pred_1[6][6][5] = {
a7d2861d
DB
110 { { 2, -1, -1, -1, -1 }, { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 },
111 { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, { 1, 2, -1, -1, -1 } },
112 { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 },
113 { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } },
114 { { 2, 0, -1, -1, -1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 },
115 { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } },
116 { { 2, 0, -1, -1, -1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 },
117 { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } },
118 { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 },
119 { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } },
120 { { 0, 2, -1, -1, -1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 },
121 { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } },
8b82a956
MN
122};
123
a7d2861d
DB
124static const struct {
125 uint8_t run;
126 uint8_t level;
127} svq3_dct_tables[2][16] = {
76de302d
DB
128 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
129 { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
130 { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
131 { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
8b82a956
MN
132};
133
134static const uint32_t svq3_dequant_coeff[32] = {
a7d2861d
DB
135 3881, 4351, 4890, 5481, 6154, 6914, 7761, 8718,
136 9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873,
137 24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683,
138 61694, 68745, 77615, 89113, 100253, 109366, 126635, 141533
8b82a956
MN
139};
140
88bd7fdc 141void ff_svq3_luma_dc_dequant_idct_c(int16_t *output, int16_t *input, int qp)
a7d2861d 142{
76de302d 143 const int qmul = svq3_dequant_coeff[qp];
8b82a956
MN
144#define stride 16
145 int i;
146 int temp[16];
a7d2861d
DB
147 static const uint8_t x_offset[4] = { 0, 1 * stride, 4 * stride, 5 * stride };
148
149 for (i = 0; i < 4; i++) {
150 const int z0 = 13 * (input[4 * i + 0] + input[4 * i + 2]);
151 const int z1 = 13 * (input[4 * i + 0] - input[4 * i + 2]);
152 const int z2 = 7 * input[4 * i + 1] - 17 * input[4 * i + 3];
153 const int z3 = 17 * input[4 * i + 1] + 7 * input[4 * i + 3];
154
155 temp[4 * i + 0] = z0 + z3;
156 temp[4 * i + 1] = z1 + z2;
157 temp[4 * i + 2] = z1 - z2;
158 temp[4 * i + 3] = z0 - z3;
8b82a956
MN
159 }
160
a7d2861d
DB
161 for (i = 0; i < 4; i++) {
162 const int offset = x_offset[i];
163 const int z0 = 13 * (temp[4 * 0 + i] + temp[4 * 2 + i]);
164 const int z1 = 13 * (temp[4 * 0 + i] - temp[4 * 2 + i]);
165 const int z2 = 7 * temp[4 * 1 + i] - 17 * temp[4 * 3 + i];
166 const int z3 = 17 * temp[4 * 1 + i] + 7 * temp[4 * 3 + i];
167
af1ede06
DB
168 output[stride * 0 + offset] = (z0 + z3) * qmul + 0x80000 >> 20;
169 output[stride * 2 + offset] = (z1 + z2) * qmul + 0x80000 >> 20;
170 output[stride * 8 + offset] = (z1 - z2) * qmul + 0x80000 >> 20;
171 output[stride * 10 + offset] = (z0 - z3) * qmul + 0x80000 >> 20;
8b82a956
MN
172 }
173}
174#undef stride
175
88bd7fdc 176void ff_svq3_add_idct_c(uint8_t *dst, int16_t *block,
a7d2861d 177 int stride, int qp, int dc)
7f8205da 178{
76de302d 179 const int qmul = svq3_dequant_coeff[qp];
8b82a956 180 int i;
8b82a956
MN
181
182 if (dc) {
af1ede06
DB
183 dc = 13 * 13 * (dc == 1 ? 1538 * block[0]
184 : qmul * (block[0] >> 3) / 2);
8b82a956
MN
185 block[0] = 0;
186 }
187
76de302d 188 for (i = 0; i < 4; i++) {
a7d2861d
DB
189 const int z0 = 13 * (block[0 + 4 * i] + block[2 + 4 * i]);
190 const int z1 = 13 * (block[0 + 4 * i] - block[2 + 4 * i]);
191 const int z2 = 7 * block[1 + 4 * i] - 17 * block[3 + 4 * i];
192 const int z3 = 17 * block[1 + 4 * i] + 7 * block[3 + 4 * i];
193
194 block[0 + 4 * i] = z0 + z3;
195 block[1 + 4 * i] = z1 + z2;
196 block[2 + 4 * i] = z1 - z2;
197 block[3 + 4 * i] = z0 - z3;
8b82a956
MN
198 }
199
76de302d 200 for (i = 0; i < 4; i++) {
a7d2861d
DB
201 const int z0 = 13 * (block[i + 4 * 0] + block[i + 4 * 2]);
202 const int z1 = 13 * (block[i + 4 * 0] - block[i + 4 * 2]);
203 const int z2 = 7 * block[i + 4 * 1] - 17 * block[i + 4 * 3];
204 const int z3 = 17 * block[i + 4 * 1] + 7 * block[i + 4 * 3];
76de302d
DB
205 const int rr = (dc + 0x80000);
206
af1ede06
DB
207 dst[i + stride * 0] = av_clip_uint8(dst[i + stride * 0] + ((z0 + z3) * qmul + rr >> 20));
208 dst[i + stride * 1] = av_clip_uint8(dst[i + stride * 1] + ((z1 + z2) * qmul + rr >> 20));
209 dst[i + stride * 2] = av_clip_uint8(dst[i + stride * 2] + ((z1 - z2) * qmul + rr >> 20));
210 dst[i + stride * 3] = av_clip_uint8(dst[i + stride * 3] + ((z0 - z3) * qmul + rr >> 20));
8b82a956
MN
211 }
212}
213
88bd7fdc 214static inline int svq3_decode_block(GetBitContext *gb, int16_t *block,
7f8205da
DB
215 int index, const int type)
216{
76de302d
DB
217 static const uint8_t *const scan_patterns[4] =
218 { luma_dc_zigzag_scan, zigzag_scan, svq3_scan, chroma_dc_scan };
8b82a956 219
9a2e7911
JG
220 int run, level, limit;
221 unsigned vlc;
af1ede06 222 const int intra = 3 * type >> 2;
76de302d 223 const uint8_t *const scan = scan_patterns[type];
8b82a956 224
76de302d
DB
225 for (limit = (16 >> intra); index < 16; index = limit, limit += 8) {
226 for (; (vlc = svq3_get_ue_golomb(gb)) != 0; index++) {
9a2e7911
JG
227 int sign = (vlc & 1) ? 0 : -1;
228 vlc = vlc + 1 >> 1;
a7d2861d
DB
229
230 if (type == 3) {
231 if (vlc < 3) {
232 run = 0;
233 level = vlc;
234 } else if (vlc < 4) {
235 run = 1;
236 level = 1;
237 } else {
af1ede06
DB
238 run = vlc & 0x3;
239 level = (vlc + 9 >> 2) - run;
a7d2861d
DB
240 }
241 } else {
242 if (vlc < 16) {
243 run = svq3_dct_tables[intra][vlc].run;
244 level = svq3_dct_tables[intra][vlc].level;
245 } else if (intra) {
af1ede06 246 run = vlc & 0x7;
a7d2861d
DB
247 level = (vlc >> 3) +
248 ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
249 } else {
af1ede06 250 run = vlc & 0xF;
a7d2861d
DB
251 level = (vlc >> 4) +
252 ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
253 }
254 }
8b82a956 255
a7d2861d
DB
256 if ((index += run) >= limit)
257 return -1;
258
259 block[scan[index]] = (level ^ sign) - sign;
76de302d 260 }
8b82a956 261
76de302d
DB
262 if (type != 2) {
263 break;
264 }
8b82a956 265 }
8b82a956 266
76de302d 267 return 0;
8b82a956
MN
268}
269
7f8205da
DB
270static inline void svq3_mc_dir_part(MpegEncContext *s,
271 int x, int y, int width, int height,
272 int mx, int my, int dxy,
273 int thirdpel, int dir, int avg)
274{
76de302d
DB
275 const Picture *pic = (dir == 0) ? &s->last_picture : &s->next_picture;
276 uint8_t *src, *dest;
277 int i, emu = 0;
a7d2861d 278 int blocksize = 2 - (width >> 3); // 16->0, 8->1, 4->2
76de302d
DB
279
280 mx += x;
281 my += y;
8b82a956 282
af1ede06
DB
283 if (mx < 0 || mx >= s->h_edge_pos - width - 1 ||
284 my < 0 || my >= s->v_edge_pos - height - 1) {
a7d2861d 285 if ((s->flags & CODEC_FLAG_EMU_EDGE))
76de302d 286 emu = 1;
8b82a956 287
af1ede06
DB
288 mx = av_clip(mx, -16, s->h_edge_pos - width + 15);
289 my = av_clip(my, -16, s->v_edge_pos - height + 15);
8b82a956
MN
290 }
291
76de302d 292 /* form component predictions */
a7d2861d
DB
293 dest = s->current_picture.f.data[0] + x + y * s->linesize;
294 src = pic->f.data[0] + mx + my * s->linesize;
76de302d
DB
295
296 if (emu) {
8c53d39e
RB
297 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize,
298 width + 1, height + 1,
299 mx, my, s->h_edge_pos, s->v_edge_pos);
a7d3e772 300 src = s->edge_emu_buffer;
8b82a956 301 }
76de302d 302 if (thirdpel)
a7d2861d
DB
303 (avg ? s->dsp.avg_tpel_pixels_tab
304 : s->dsp.put_tpel_pixels_tab)[dxy](dest, src, s->linesize,
305 width, height);
76de302d 306 else
a7d2861d
DB
307 (avg ? s->dsp.avg_pixels_tab
308 : s->dsp.put_pixels_tab)[blocksize][dxy](dest, src, s->linesize,
309 height);
76de302d
DB
310
311 if (!(s->flags & CODEC_FLAG_GRAY)) {
af1ede06
DB
312 mx = mx + (mx < (int) x) >> 1;
313 my = my + (my < (int) y) >> 1;
314 width = width >> 1;
315 height = height >> 1;
76de302d
DB
316 blocksize++;
317
318 for (i = 1; i < 3; i++) {
657ccb5a
DB
319 dest = s->current_picture.f.data[i] + (x >> 1) + (y >> 1) * s->uvlinesize;
320 src = pic->f.data[i] + mx + my * s->uvlinesize;
76de302d
DB
321
322 if (emu) {
8c53d39e
RB
323 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src, s->uvlinesize,
324 width + 1, height + 1,
325 mx, my, (s->h_edge_pos >> 1),
326 s->v_edge_pos >> 1);
76de302d
DB
327 src = s->edge_emu_buffer;
328 }
329 if (thirdpel)
a7d2861d
DB
330 (avg ? s->dsp.avg_tpel_pixels_tab
331 : s->dsp.put_tpel_pixels_tab)[dxy](dest, src,
332 s->uvlinesize,
333 width, height);
76de302d 334 else
a7d2861d
DB
335 (avg ? s->dsp.avg_pixels_tab
336 : s->dsp.put_pixels_tab)[blocksize][dxy](dest, src,
337 s->uvlinesize,
338 height);
76de302d
DB
339 }
340 }
8b82a956
MN
341}
342
a7d2861d
DB
343static inline int svq3_mc_dir(H264Context *h, int size, int mode,
344 int dir, int avg)
7f8205da 345{
76de302d 346 int i, j, k, mx, my, dx, dy, x, y;
a7d2861d
DB
347 MpegEncContext *const s = (MpegEncContext *)h;
348 const int part_width = ((size & 5) == 4) ? 4 : 16 >> (size & 1);
349 const int part_height = 16 >> ((unsigned)(size + 1) / 3);
350 const int extra_width = (mode == PREDICT_MODE) ? -16 * 6 : 0;
351 const int h_edge_pos = 6 * (s->h_edge_pos - part_width) - extra_width;
352 const int v_edge_pos = 6 * (s->v_edge_pos - part_height) - extra_width;
353
354 for (i = 0; i < 16; i += part_height)
76de302d 355 for (j = 0; j < 16; j += part_width) {
a7d2861d
DB
356 const int b_xy = (4 * s->mb_x + (j >> 2)) +
357 (4 * s->mb_y + (i >> 2)) * h->b_stride;
76de302d 358 int dxy;
a7d2861d
DB
359 x = 16 * s->mb_x + j;
360 y = 16 * s->mb_y + i;
af1ede06
DB
361 k = (j >> 2 & 1) + (i >> 1 & 2) +
362 (j >> 1 & 4) + (i & 8);
76de302d
DB
363
364 if (mode != PREDICT_MODE) {
af1ede06 365 pred_motion(h, k, part_width >> 2, dir, 1, &mx, &my);
76de302d 366 } else {
657ccb5a
DB
367 mx = s->next_picture.f.motion_val[0][b_xy][0] << 1;
368 my = s->next_picture.f.motion_val[0][b_xy][1] << 1;
76de302d
DB
369
370 if (dir == 0) {
af1ede06
DB
371 mx = mx * h->frame_num_offset /
372 h->prev_frame_num_offset + 1 >> 1;
373 my = my * h->frame_num_offset /
374 h->prev_frame_num_offset + 1 >> 1;
76de302d 375 } else {
af1ede06
DB
376 mx = mx * (h->frame_num_offset - h->prev_frame_num_offset) /
377 h->prev_frame_num_offset + 1 >> 1;
378 my = my * (h->frame_num_offset - h->prev_frame_num_offset) /
379 h->prev_frame_num_offset + 1 >> 1;
76de302d
DB
380 }
381 }
382
383 /* clip motion vector prediction to frame border */
a7d2861d
DB
384 mx = av_clip(mx, extra_width - 6 * x, h_edge_pos - 6 * x);
385 my = av_clip(my, extra_width - 6 * y, v_edge_pos - 6 * y);
76de302d
DB
386
387 /* get (optional) motion vector differential */
388 if (mode == PREDICT_MODE) {
389 dx = dy = 0;
390 } else {
391 dy = svq3_get_se_golomb(&s->gb);
392 dx = svq3_get_se_golomb(&s->gb);
393
394 if (dx == INVALID_VLC || dy == INVALID_VLC) {
395 av_log(h->s.avctx, AV_LOG_ERROR, "invalid MV vlc\n");
396 return -1;
397 }
398 }
399
400 /* compute motion vector */
401 if (mode == THIRDPEL_MODE) {
402 int fx, fy;
af1ede06
DB
403 mx = (mx + 1 >> 1) + dx;
404 my = (my + 1 >> 1) + dy;
405 fx = (unsigned)(mx + 0x3000) / 3 - 0x1000;
406 fy = (unsigned)(my + 0x3000) / 3 - 0x1000;
a7d2861d
DB
407 dxy = (mx - 3 * fx) + 4 * (my - 3 * fy);
408
409 svq3_mc_dir_part(s, x, y, part_width, part_height,
410 fx, fy, dxy, 1, dir, avg);
76de302d
DB
411 mx += mx;
412 my += my;
413 } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) {
af1ede06
DB
414 mx = (unsigned)(mx + 1 + 0x3000) / 3 + dx - 0x1000;
415 my = (unsigned)(my + 1 + 0x3000) / 3 + dy - 0x1000;
a7d2861d 416 dxy = (mx & 1) + 2 * (my & 1);
76de302d 417
a7d2861d
DB
418 svq3_mc_dir_part(s, x, y, part_width, part_height,
419 mx >> 1, my >> 1, dxy, 0, dir, avg);
76de302d
DB
420 mx *= 3;
421 my *= 3;
422 } else {
af1ede06
DB
423 mx = (unsigned)(mx + 3 + 0x6000) / 6 + dx - 0x1000;
424 my = (unsigned)(my + 3 + 0x6000) / 6 + dy - 0x1000;
76de302d 425
a7d2861d
DB
426 svq3_mc_dir_part(s, x, y, part_width, part_height,
427 mx, my, 0, 0, dir, avg);
76de302d
DB
428 mx *= 6;
429 my *= 6;
430 }
431
432 /* update mv_cache */
433 if (mode != PREDICT_MODE) {
a7d2861d 434 int32_t mv = pack16to32(mx, my);
76de302d
DB
435
436 if (part_height == 8 && i < 8) {
a7d2861d 437 AV_WN32A(h->mv_cache[dir][scan8[k] + 1 * 8], mv);
76de302d 438
a7d2861d
DB
439 if (part_width == 8 && j < 8)
440 AV_WN32A(h->mv_cache[dir][scan8[k] + 1 + 1 * 8], mv);
76de302d 441 }
a7d2861d 442 if (part_width == 8 && j < 8)
366b72f1 443 AV_WN32A(h->mv_cache[dir][scan8[k] + 1], mv);
a7d2861d 444 if (part_width == 4 || part_height == 4)
366b72f1 445 AV_WN32A(h->mv_cache[dir][scan8[k]], mv);
76de302d
DB
446 }
447
448 /* write back motion vectors */
657ccb5a
DB
449 fill_rectangle(s->current_picture.f.motion_val[dir][b_xy],
450 part_width >> 2, part_height >> 2, h->b_stride,
451 pack16to32(mx, my), 4);
bb270c08 452 }
2e26c8d2 453
76de302d 454 return 0;
2e26c8d2
MM
455}
456
8dfc6d1f 457static int svq3_decode_mb(SVQ3Context *svq3, unsigned int mb_type)
7f8205da 458{
8dfc6d1f 459 H264Context *h = &svq3->h;
76de302d
DB
460 int i, j, k, m, dir, mode;
461 int cbp = 0;
462 uint32_t vlc;
463 int8_t *top, *left;
a7d2861d
DB
464 MpegEncContext *const s = (MpegEncContext *)h;
465 const int mb_xy = h->mb_xy;
466 const int b_xy = 4 * s->mb_x + 4 * s->mb_y * h->b_stride;
76de302d
DB
467
468 h->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
469 h->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
470 h->topright_samples_available = 0xFFFF;
471
472 if (mb_type == 0) { /* SKIP */
a7d2861d
DB
473 if (s->pict_type == AV_PICTURE_TYPE_P ||
474 s->next_picture.f.mb_type[mb_xy] == -1) {
475 svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
476 0, 0, 0, 0, 0, 0);
76de302d 477
a7d2861d
DB
478 if (s->pict_type == AV_PICTURE_TYPE_B)
479 svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
480 0, 0, 0, 0, 1, 1);
76de302d
DB
481
482 mb_type = MB_TYPE_SKIP;
483 } else {
657ccb5a 484 mb_type = FFMIN(s->next_picture.f.mb_type[mb_xy], 6);
76de302d
DB
485 if (svq3_mc_dir(h, mb_type, PREDICT_MODE, 0, 0) < 0)
486 return -1;
487 if (svq3_mc_dir(h, mb_type, PREDICT_MODE, 1, 1) < 0)
488 return -1;
8b82a956 489
76de302d 490 mb_type = MB_TYPE_16x16;
bb270c08 491 }
76de302d 492 } else if (mb_type < 8) { /* INTER */
a7d2861d 493 if (svq3->thirdpel_flag && svq3->halfpel_flag == !get_bits1(&s->gb))
76de302d 494 mode = THIRDPEL_MODE;
a7d2861d
DB
495 else if (svq3->halfpel_flag &&
496 svq3->thirdpel_flag == !get_bits1(&s->gb))
76de302d 497 mode = HALFPEL_MODE;
a7d2861d 498 else
76de302d 499 mode = FULLPEL_MODE;
8b82a956 500
76de302d
DB
501 /* fill caches */
502 /* note ref_cache should contain here:
a7d2861d
DB
503 * ????????
504 * ???11111
505 * N??11111
506 * N??11111
507 * N??11111
508 */
76de302d
DB
509
510 for (m = 0; m < 2; m++) {
a7d2861d
DB
511 if (s->mb_x > 0 && h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6] != -1) {
512 for (i = 0; i < 4; i++)
513 AV_COPY32(h->mv_cache[m][scan8[0] - 1 + i * 8],
514 s->current_picture.f.motion_val[m][b_xy - 1 + i * h->b_stride]);
76de302d 515 } else {
a7d2861d
DB
516 for (i = 0; i < 4; i++)
517 AV_ZERO32(h->mv_cache[m][scan8[0] - 1 + i * 8]);
76de302d
DB
518 }
519 if (s->mb_y > 0) {
a7d2861d
DB
520 memcpy(h->mv_cache[m][scan8[0] - 1 * 8],
521 s->current_picture.f.motion_val[m][b_xy - h->b_stride],
522 4 * 2 * sizeof(int16_t));
523 memset(&h->ref_cache[m][scan8[0] - 1 * 8],
524 (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);
76de302d 525
af1ede06 526 if (s->mb_x < s->mb_width - 1) {
a7d2861d
DB
527 AV_COPY32(h->mv_cache[m][scan8[0] + 4 - 1 * 8],
528 s->current_picture.f.motion_val[m][b_xy - h->b_stride + 4]);
529 h->ref_cache[m][scan8[0] + 4 - 1 * 8] =
530 (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride + 1] + 6] == -1 ||
531 h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1;
532 } else
533 h->ref_cache[m][scan8[0] + 4 - 1 * 8] = PART_NOT_AVAILABLE;
76de302d 534 if (s->mb_x > 0) {
a7d2861d
DB
535 AV_COPY32(h->mv_cache[m][scan8[0] - 1 - 1 * 8],
536 s->current_picture.f.motion_val[m][b_xy - h->b_stride - 1]);
537 h->ref_cache[m][scan8[0] - 1 - 1 * 8] =
538 (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] == -1) ? PART_NOT_AVAILABLE : 1;
539 } else
540 h->ref_cache[m][scan8[0] - 1 - 1 * 8] = PART_NOT_AVAILABLE;
541 } else
542 memset(&h->ref_cache[m][scan8[0] - 1 * 8 - 1],
543 PART_NOT_AVAILABLE, 8);
76de302d 544
975a1447 545 if (s->pict_type != AV_PICTURE_TYPE_B)
76de302d 546 break;
bb270c08 547 }
8b82a956 548
76de302d 549 /* decode motion vector(s) and form prediction(s) */
975a1447 550 if (s->pict_type == AV_PICTURE_TYPE_P) {
af1ede06 551 if (svq3_mc_dir(h, mb_type - 1, mode, 0, 0) < 0)
76de302d 552 return -1;
975a1447 553 } else { /* AV_PICTURE_TYPE_B */
a7d2861d 554 if (mb_type != 2)
76de302d
DB
555 if (svq3_mc_dir(h, 0, mode, 0, 0) < 0)
556 return -1;
a7d2861d
DB
557 else
558 for (i = 0; i < 4; i++)
559 memset(s->current_picture.f.motion_val[0][b_xy + i * h->b_stride],
560 0, 4 * 2 * sizeof(int16_t));
561 if (mb_type != 1)
af1ede06 562 if (svq3_mc_dir(h, 0, mode, 1, mb_type == 3) < 0)
76de302d 563 return -1;
a7d2861d
DB
564 else
565 for (i = 0; i < 4; i++)
566 memset(s->current_picture.f.motion_val[1][b_xy + i * h->b_stride],
567 0, 4 * 2 * sizeof(int16_t));
bb270c08 568 }
8b82a956 569
76de302d
DB
570 mb_type = MB_TYPE_16x16;
571 } else if (mb_type == 8 || mb_type == 33) { /* INTRA4x4 */
a7d2861d 572 memset(h->intra4x4_pred_mode_cache, -1, 8 * 5 * sizeof(int8_t));
76de302d
DB
573
574 if (mb_type == 8) {
575 if (s->mb_x > 0) {
a7d2861d
DB
576 for (i = 0; i < 4; i++)
577 h->intra4x4_pred_mode_cache[scan8[0] - 1 + i * 8] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6 - i];
578 if (h->intra4x4_pred_mode_cache[scan8[0] - 1] == -1)
76de302d 579 h->left_samples_available = 0x5F5F;
76de302d
DB
580 }
581 if (s->mb_y > 0) {
a7d2861d
DB
582 h->intra4x4_pred_mode_cache[4 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride] + 0];
583 h->intra4x4_pred_mode_cache[5 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride] + 1];
584 h->intra4x4_pred_mode_cache[6 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride] + 2];
585 h->intra4x4_pred_mode_cache[7 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride] + 3];
76de302d 586
a7d2861d 587 if (h->intra4x4_pred_mode_cache[4 + 8 * 0] == -1)
76de302d 588 h->top_samples_available = 0x33FF;
76de302d
DB
589 }
590
591 /* decode prediction codes for luma blocks */
a7d2861d 592 for (i = 0; i < 16; i += 2) {
76de302d
DB
593 vlc = svq3_get_ue_golomb(&s->gb);
594
a7d2861d 595 if (vlc >= 25) {
76de302d
DB
596 av_log(h->s.avctx, AV_LOG_ERROR, "luma prediction:%d\n", vlc);
597 return -1;
598 }
599
a7d2861d
DB
600 left = &h->intra4x4_pred_mode_cache[scan8[i] - 1];
601 top = &h->intra4x4_pred_mode_cache[scan8[i] - 8];
76de302d
DB
602
603 left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
604 left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
605
a7d2861d 606 if (left[1] == -1 || left[2] == -1) {
76de302d
DB
607 av_log(h->s.avctx, AV_LOG_ERROR, "weird prediction\n");
608 return -1;
609 }
610 }
611 } else { /* mb_type == 33, DC_128_PRED block type */
a7d2861d
DB
612 for (i = 0; i < 4; i++)
613 memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_PRED, 4);
884182b3 614 }
8b82a956 615
3b7ebeb4 616 write_back_intra_pred_mode(h);
8b82a956 617
76de302d 618 if (mb_type == 8) {
2bedc0e8 619 ff_h264_check_intra4x4_pred_mode(h);
da3b9756 620
76de302d
DB
621 h->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
622 h->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
623 } else {
a7d2861d
DB
624 for (i = 0; i < 4; i++)
625 memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_128_PRED, 4);
da3b9756 626
76de302d
DB
627 h->top_samples_available = 0x33FF;
628 h->left_samples_available = 0x5F5F;
629 }
da3b9756 630
76de302d
DB
631 mb_type = MB_TYPE_INTRA4x4;
632 } else { /* INTRA16x16 */
633 dir = i_mb_type_info[mb_type - 8].pred_mode;
a7d2861d 634 dir = (dir >> 1) ^ 3 * (dir & 1) ^ 1;
8b82a956 635
a7d2861d 636 if ((h->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(h, dir, 0)) == -1) {
76de302d
DB
637 av_log(h->s.avctx, AV_LOG_ERROR, "check_intra_pred_mode = -1\n");
638 return -1;
639 }
8b82a956 640
a7d2861d 641 cbp = i_mb_type_info[mb_type - 8].cbp;
76de302d 642 mb_type = MB_TYPE_INTRA16x16;
884182b3 643 }
8b82a956 644
975a1447 645 if (!IS_INTER(mb_type) && s->pict_type != AV_PICTURE_TYPE_I) {
a7d2861d
DB
646 for (i = 0; i < 4; i++)
647 memset(s->current_picture.f.motion_val[0][b_xy + i * h->b_stride],
648 0, 4 * 2 * sizeof(int16_t));
975a1447 649 if (s->pict_type == AV_PICTURE_TYPE_B) {
a7d2861d
DB
650 for (i = 0; i < 4; i++)
651 memset(s->current_picture.f.motion_val[1][b_xy + i * h->b_stride],
652 0, 4 * 2 * sizeof(int16_t));
76de302d 653 }
8b82a956 654 }
76de302d 655 if (!IS_INTRA4x4(mb_type)) {
a7d2861d 656 memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy], DC_PRED, 8);
da3b9756 657 }
975a1447 658 if (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B) {
a7d2861d
DB
659 memset(h->non_zero_count_cache + 8, 0, 14 * 8 * sizeof(uint8_t));
660 s->dsp.clear_blocks(h->mb + 0);
661 s->dsp.clear_blocks(h->mb + 384);
884182b3 662 }
2e26c8d2 663
a7d2861d
DB
664 if (!IS_INTRA16x16(mb_type) &&
665 (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B)) {
666 if ((vlc = svq3_get_ue_golomb(&s->gb)) >= 48) {
76de302d
DB
667 av_log(h->s.avctx, AV_LOG_ERROR, "cbp_vlc=%d\n", vlc);
668 return -1;
669 }
8b82a956 670
a7d2861d
DB
671 cbp = IS_INTRA(mb_type) ? golomb_to_intra4x4_cbp[vlc]
672 : golomb_to_inter_cbp[vlc];
884182b3 673 }
a7d2861d
DB
674 if (IS_INTRA16x16(mb_type) ||
675 (s->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
76de302d 676 s->qscale += svq3_get_se_golomb(&s->gb);
8b82a956 677
a7d2861d 678 if (s->qscale > 31u) {
76de302d 679 av_log(h->s.avctx, AV_LOG_ERROR, "qscale:%d\n", s->qscale);
bb270c08 680 return -1;
bb270c08 681 }
8b82a956 682 }
76de302d 683 if (IS_INTRA16x16(mb_type)) {
a7d2861d
DB
684 AV_ZERO128(h->mb_luma_dc[0] + 0);
685 AV_ZERO128(h->mb_luma_dc[0] + 8);
686 if (svq3_decode_block(&s->gb, h->mb_luma_dc[0], 0, 1)) {
687 av_log(h->s.avctx, AV_LOG_ERROR,
688 "error while decoding intra luma dc\n");
76de302d 689 return -1;
884182b3 690 }
76de302d 691 }
8b82a956 692
76de302d
DB
693 if (cbp) {
694 const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
a7d2861d 695 const int type = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
76de302d 696
a7d2861d 697 for (i = 0; i < 4; i++)
76de302d
DB
698 if ((cbp & (1 << i))) {
699 for (j = 0; j < 4; j++) {
a7d2861d
DB
700 k = index ? (1 * (j & 1) + 2 * (i & 1) +
701 2 * (j & 2) + 4 * (i & 2))
702 : (4 * i + j);
703 h->non_zero_count_cache[scan8[k]] = 1;
704
705 if (svq3_decode_block(&s->gb, &h->mb[16 * k], index, type)) {
706 av_log(h->s.avctx, AV_LOG_ERROR,
707 "error while decoding block\n");
76de302d
DB
708 return -1;
709 }
710 }
711 }
8b82a956 712
76de302d 713 if ((cbp & 0x30)) {
a7d2861d
DB
714 for (i = 1; i < 3; ++i)
715 if (svq3_decode_block(&s->gb, &h->mb[16 * 16 * i], 0, 3)) {
716 av_log(h->s.avctx, AV_LOG_ERROR,
717 "error while decoding chroma dc block\n");
718 return -1;
719 }
76de302d
DB
720
721 if ((cbp & 0x20)) {
11177a4d
JGG
722 for (i = 1; i < 3; i++) {
723 for (j = 0; j < 4; j++) {
a7d2861d
DB
724 k = 16 * i + j;
725 h->non_zero_count_cache[scan8[k]] = 1;
11177a4d 726
a7d2861d
DB
727 if (svq3_decode_block(&s->gb, &h->mb[16 * k], 1, 1)) {
728 av_log(h->s.avctx, AV_LOG_ERROR,
729 "error while decoding chroma ac block\n");
11177a4d
JGG
730 return -1;
731 }
76de302d
DB
732 }
733 }
734 }
bb270c08 735 }
8b82a956 736 }
8b82a956 737
a7d2861d 738 h->cbp = cbp;
657ccb5a 739 s->current_picture.f.mb_type[mb_xy] = mb_type;
8b82a956 740
a7d2861d 741 if (IS_INTRA(mb_type))
45b7bd7c 742 h->chroma_pred_mode = ff_h264_check_intra_pred_mode(h, DC_PRED8x8, 1);
8b82a956 743
76de302d 744 return 0;
8b82a956
MN
745}
746
8dfc6d1f 747static int svq3_decode_slice_header(AVCodecContext *avctx)
7f8205da 748{
8dfc6d1f 749 SVQ3Context *svq3 = avctx->priv_data;
a7d2861d 750 H264Context *h = &svq3->h;
8dfc6d1f 751 MpegEncContext *s = &h->s;
a7d2861d 752 const int mb_xy = h->mb_xy;
76de302d 753 int i, header;
288bb3da 754 unsigned slice_id;
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 {
af1ede06 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],
af1ede06 784 &s->gb.buffer[s->gb.size_in_bits >> 3], length - 1);
76de302d 785 }
db794291 786 skip_bits_long(&s->gb, 0);
da3b9756 787 }
da3b9756 788
288bb3da
JG
789 if ((slice_id = svq3_get_ue_golomb(&s->gb)) >= 3) {
790 av_log(h->s.avctx, AV_LOG_ERROR, "illegal slice type %d \n", slice_id);
76de302d
DB
791 return -1;
792 }
da3b9756 793
288bb3da 794 h->slice_type = golomb_to_pict_type[slice_id];
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) -
af1ede06 799 (s->mb_y * s->mb_width + s->mb_x);
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;
af1ede06 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 1005static int svq3_decode_frame(AVCodecContext *avctx, void *data,
df9b9567 1006 int *got_frame, 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;
9a2e7911 1013 int m;
f4cca718 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;
df9b9567 1020 *got_frame = 1;
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;
af1ede06
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) {
af1ede06 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;
af1ede06 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++) {
9a2e7911 1096 unsigned mb_type;
a7d2861d 1097 h->mb_xy = s->mb_x + s->mb_y * s->mb_stride;
da3b9756 1098
a7d2861d
DB
1099 if ((get_bits_count(&s->gb) + 7) >= s->gb.size_in_bits &&
1100 ((get_bits_count(&s->gb) & 7) == 0 ||
af1ede06 1101 show_bits(&s->gb, -get_bits_count(&s->gb) & 7) == 0)) {
8dfc6d1f 1102 skip_bits(&s->gb, svq3->next_slice_index - get_bits_count(&s->gb));
a7d2861d 1103 s->gb.size_in_bits = 8 * buf_size;
da3b9756 1104
8dfc6d1f 1105 if (svq3_decode_slice_header(avctx))
76de302d 1106 return -1;
da3b9756 1107
76de302d
DB
1108 /* TODO: support s->mb_skip_run */
1109 }
da3b9756 1110
76de302d 1111 mb_type = svq3_get_ue_golomb(&s->gb);
da3b9756 1112
a7d2861d 1113 if (s->pict_type == AV_PICTURE_TYPE_I)
76de302d 1114 mb_type += 8;
a7d2861d 1115 else if (s->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4)
76de302d 1116 mb_type += 4;
9a2e7911 1117 if (mb_type > 33 || svq3_decode_mb(svq3, mb_type)) {
a7d2861d
DB
1118 av_log(h->s.avctx, AV_LOG_ERROR,
1119 "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
76de302d
DB
1120 return -1;
1121 }
8b82a956 1122
a7d2861d
DB
1123 if (mb_type != 0)
1124 ff_h264_hl_decode_mb(h);
8b82a956 1125
a7d2861d 1126 if (s->pict_type != AV_PICTURE_TYPE_B && !s->low_delay)
657ccb5a 1127 s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
975a1447 1128 (s->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
76de302d 1129 }
2e26c8d2 1130
a7d2861d 1131 ff_draw_horiz_band(s, 16 * s->mb_y, 16);
8b82a956 1132 }
4c701ac8 1133
efd29844 1134 ff_MPV_frame_end(s);
8b82a956 1135
a7d2861d
DB
1136 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay)
1137 *(AVFrame *)data = s->current_picture.f;
1138 else
1139 *(AVFrame *)data = s->last_picture.f;
da3b9756 1140
76de302d 1141 /* Do not output the last pic after seeking. */
a7d2861d 1142 if (s->last_picture_ptr || s->low_delay)
df9b9567 1143 *got_frame = 1;
da3b9756 1144
76de302d 1145 return buf_size;
8b82a956
MN
1146}
1147
8dfc6d1f
BC
1148static int svq3_decode_end(AVCodecContext *avctx)
1149{
1150 SVQ3Context *svq3 = avctx->priv_data;
a7d2861d 1151 H264Context *h = &svq3->h;
8dfc6d1f
BC
1152 MpegEncContext *s = &h->s;
1153
1154 ff_h264_free_context(h);
1155
efd29844 1156 ff_MPV_common_end(s);
8dfc6d1f
BC
1157
1158 return 0;
1159}
8b82a956 1160
d36beb3f 1161AVCodec ff_svq3_decoder = {
ec6402b7
AK
1162 .name = "svq3",
1163 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 1164 .id = AV_CODEC_ID_SVQ3,
ec6402b7
AK
1165 .priv_data_size = sizeof(SVQ3Context),
1166 .init = svq3_decode_init,
1167 .close = svq3_decode_end,
1168 .decode = svq3_decode_frame,
a7d2861d
DB
1169 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND |
1170 CODEC_CAP_DR1 |
00c3b67b
MS
1171 CODEC_CAP_DELAY,
1172 .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
a7d2861d
DB
1173 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,
1174 AV_PIX_FMT_NONE},
8b82a956 1175};