mpegvideo: split ff_draw_horiz_band().
[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 858
d9ebb00d
AK
859 h->chroma_qp[0] = h->chroma_qp[1] = 4;
860
861 svq3->halfpel_flag = 1;
862 svq3->thirdpel_flag = 1;
863 svq3->unknown_flag = 0;
864
865 /* prowl for the "SEQH" marker in the extradata */
866 extradata = (unsigned char *)avctx->extradata;
867 extradata_end = avctx->extradata + avctx->extradata_size;
868 if (extradata) {
869 for (m = 0; m + 8 < avctx->extradata_size; m++) {
870 if (!memcmp(extradata, "SEQH", 4)) {
871 marker_found = 1;
872 break;
9e1db721 873 }
d9ebb00d 874 extradata++;
76de302d 875 }
d9ebb00d 876 }
8b82a956 877
d9ebb00d
AK
878 /* if a match was found, parse the extra data */
879 if (marker_found) {
880 GetBitContext gb;
881 int frame_size_code;
882
883 size = AV_RB32(&extradata[4]);
884 if (size > extradata_end - extradata - 8)
885 return AVERROR_INVALIDDATA;
886 init_get_bits(&gb, extradata + 8, size * 8);
887
888 /* 'frame size code' and optional 'width, height' */
889 frame_size_code = get_bits(&gb, 3);
890 switch (frame_size_code) {
891 case 0:
892 avctx->width = 160;
893 avctx->height = 120;
894 break;
895 case 1:
896 avctx->width = 128;
897 avctx->height = 96;
898 break;
899 case 2:
900 avctx->width = 176;
901 avctx->height = 144;
902 break;
903 case 3:
904 avctx->width = 352;
905 avctx->height = 288;
906 break;
907 case 4:
908 avctx->width = 704;
909 avctx->height = 576;
910 break;
911 case 5:
912 avctx->width = 240;
913 avctx->height = 180;
914 break;
915 case 6:
916 avctx->width = 320;
917 avctx->height = 240;
918 break;
919 case 7:
920 avctx->width = get_bits(&gb, 12);
921 avctx->height = get_bits(&gb, 12);
922 break;
923 }
8b82a956 924
d9ebb00d
AK
925 svq3->halfpel_flag = get_bits1(&gb);
926 svq3->thirdpel_flag = get_bits1(&gb);
8b82a956 927
d9ebb00d
AK
928 /* unknown fields */
929 skip_bits1(&gb);
930 skip_bits1(&gb);
931 skip_bits1(&gb);
932 skip_bits1(&gb);
8b82a956 933
d9ebb00d 934 s->low_delay = get_bits1(&gb);
1e002b60 935
d9ebb00d
AK
936 /* unknown field */
937 skip_bits1(&gb);
76de302d 938
d9ebb00d
AK
939 while (get_bits1(&gb))
940 skip_bits(&gb, 8);
76de302d 941
d9ebb00d
AK
942 svq3->unknown_flag = get_bits1(&gb);
943 avctx->has_b_frames = !s->low_delay;
944 if (svq3->unknown_flag) {
b250f9c6 945#if CONFIG_ZLIB
d9ebb00d
AK
946 unsigned watermark_width = svq3_get_ue_golomb(&gb);
947 unsigned watermark_height = svq3_get_ue_golomb(&gb);
948 int u1 = svq3_get_ue_golomb(&gb);
949 int u2 = get_bits(&gb, 8);
950 int u3 = get_bits(&gb, 2);
951 int u4 = svq3_get_ue_golomb(&gb);
952 unsigned long buf_len = watermark_width *
953 watermark_height * 4;
954 int offset = get_bits_count(&gb) + 7 >> 3;
955 uint8_t *buf;
956
957 if ((uint64_t)watermark_width * 4 > UINT_MAX / watermark_height)
958 return -1;
76de302d 959
d9ebb00d
AK
960 buf = av_malloc(buf_len);
961 av_log(avctx, AV_LOG_DEBUG, "watermark size: %dx%d\n",
962 watermark_width, watermark_height);
963 av_log(avctx, AV_LOG_DEBUG,
964 "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n",
965 u1, u2, u3, u4, offset);
966 if (uncompress(buf, &buf_len, extradata + 8 + offset,
967 size - offset) != Z_OK) {
a7d2861d 968 av_log(avctx, AV_LOG_ERROR,
d9ebb00d
AK
969 "could not uncompress watermark logo\n");
970 av_free(buf);
76de302d 971 return -1;
76de302d 972 }
d9ebb00d
AK
973 svq3->watermark_key = ff_svq1_packet_checksum(buf, buf_len, 0);
974 svq3->watermark_key = svq3->watermark_key << 16 |
975 svq3->watermark_key;
976 av_log(avctx, AV_LOG_DEBUG,
977 "watermark key %#x\n", svq3->watermark_key);
978 av_free(buf);
979#else
980 av_log(avctx, AV_LOG_ERROR,
981 "this svq3 file contains watermark which need zlib support compiled in\n");
982 return -1;
983#endif
76de302d 984 }
d9ebb00d 985 }
cdca7c37 986
d9ebb00d
AK
987 s->width = avctx->width;
988 s->height = avctx->height;
cdca7c37 989
d9ebb00d
AK
990 if (ff_MPV_common_init(s) < 0)
991 return -1;
cdca7c37 992
d9ebb00d 993 h->b_stride = 4 * s->mb_width;
cdca7c37 994
d9ebb00d
AK
995 if (ff_h264_alloc_tables(h) < 0) {
996 av_log(avctx, AV_LOG_ERROR, "svq3 memory allocation failed\n");
997 return AVERROR(ENOMEM);
da3b9756 998 }
76de302d 999
f4cca718
BC
1000 return 0;
1001}
1002
a7d2861d 1003static int svq3_decode_frame(AVCodecContext *avctx, void *data,
df9b9567 1004 int *got_frame, AVPacket *avpkt)
f4cca718 1005{
7a00bbad 1006 const uint8_t *buf = avpkt->data;
a7d2861d
DB
1007 SVQ3Context *svq3 = avctx->priv_data;
1008 H264Context *h = &svq3->h;
1009 MpegEncContext *s = &h->s;
1010 int buf_size = avpkt->size;
9a2e7911 1011 int m;
f4cca718 1012
76de302d
DB
1013 /* special case for last picture */
1014 if (buf_size == 0) {
1015 if (s->next_picture_ptr && !s->low_delay) {
324deaa2 1016 *(AVFrame *) data = s->next_picture.f;
76de302d 1017 s->next_picture_ptr = NULL;
df9b9567 1018 *got_frame = 1;
76de302d
DB
1019 }
1020 return 0;
da3b9756 1021 }
8b82a956 1022
a7d2861d 1023 init_get_bits(&s->gb, buf, 8 * buf_size);
8b82a956 1024
76de302d 1025 s->mb_x = s->mb_y = h->mb_xy = 0;
8b82a956 1026
8dfc6d1f 1027 if (svq3_decode_slice_header(avctx))
76de302d 1028 return -1;
8b82a956 1029
a7d2861d 1030 s->pict_type = h->slice_type;
76de302d 1031 s->picture_number = h->slice_num;
8b82a956 1032
a7d2861d
DB
1033 if (avctx->debug & FF_DEBUG_PICT_INFO)
1034 av_log(h->s.avctx, AV_LOG_DEBUG,
1035 "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
1036 av_get_picture_type_char(s->pict_type),
1037 svq3->halfpel_flag, svq3->thirdpel_flag,
76de302d 1038 s->adaptive_quant, s->qscale, h->slice_num);
8b82a956 1039
8ed2ae09 1040 /* for skipping the frame */
657ccb5a
DB
1041 s->current_picture.f.pict_type = s->pict_type;
1042 s->current_picture.f.key_frame = (s->pict_type == AV_PICTURE_TYPE_I);
76de302d
DB
1043
1044 /* Skip B-frames if we do not have reference frames. */
975a1447 1045 if (s->last_picture_ptr == NULL && s->pict_type == AV_PICTURE_TYPE_B)
76de302d 1046 return 0;
af1ede06
DB
1047 if (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B ||
1048 avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I ||
1049 avctx->skip_frame >= AVDISCARD_ALL)
76de302d
DB
1050 return 0;
1051
1052 if (s->next_p_frame_damaged) {
975a1447 1053 if (s->pict_type == AV_PICTURE_TYPE_B)
76de302d
DB
1054 return 0;
1055 else
1056 s->next_p_frame_damaged = 0;
1057 }
da3b9756 1058
903d58f6 1059 if (ff_h264_frame_start(h) < 0)
76de302d 1060 return -1;
8b82a956 1061
975a1447 1062 if (s->pict_type == AV_PICTURE_TYPE_B) {
af1ede06 1063 h->frame_num_offset = h->slice_num - h->prev_frame_num;
8b82a956 1064
a7d2861d 1065 if (h->frame_num_offset < 0)
76de302d 1066 h->frame_num_offset += 256;
a7d2861d
DB
1067 if (h->frame_num_offset == 0 ||
1068 h->frame_num_offset >= h->prev_frame_num_offset) {
76de302d
DB
1069 av_log(h->s.avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
1070 return -1;
1071 }
1072 } else {
a7d2861d
DB
1073 h->prev_frame_num = h->frame_num;
1074 h->frame_num = h->slice_num;
af1ede06 1075 h->prev_frame_num_offset = h->frame_num - h->prev_frame_num;
da3b9756 1076
a7d2861d 1077 if (h->prev_frame_num_offset < 0)
76de302d 1078 h->prev_frame_num_offset += 256;
da3b9756 1079 }
da3b9756 1080
a7d2861d 1081 for (m = 0; m < 2; m++) {
76de302d 1082 int i;
a7d2861d 1083 for (i = 0; i < 4; i++) {
76de302d
DB
1084 int j;
1085 for (j = -1; j < 4; j++)
a7d2861d 1086 h->ref_cache[m][scan8[0] + 8 * i + j] = 1;
76de302d 1087 if (i < 3)
a7d2861d 1088 h->ref_cache[m][scan8[0] + 8 * i + j] = PART_NOT_AVAILABLE;
76de302d 1089 }
da3b9756 1090 }
da3b9756 1091
76de302d
DB
1092 for (s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
1093 for (s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
9a2e7911 1094 unsigned mb_type;
a7d2861d 1095 h->mb_xy = s->mb_x + s->mb_y * s->mb_stride;
da3b9756 1096
a7d2861d
DB
1097 if ((get_bits_count(&s->gb) + 7) >= s->gb.size_in_bits &&
1098 ((get_bits_count(&s->gb) & 7) == 0 ||
af1ede06 1099 show_bits(&s->gb, -get_bits_count(&s->gb) & 7) == 0)) {
8dfc6d1f 1100 skip_bits(&s->gb, svq3->next_slice_index - get_bits_count(&s->gb));
a7d2861d 1101 s->gb.size_in_bits = 8 * buf_size;
da3b9756 1102
8dfc6d1f 1103 if (svq3_decode_slice_header(avctx))
76de302d 1104 return -1;
da3b9756 1105
76de302d
DB
1106 /* TODO: support s->mb_skip_run */
1107 }
da3b9756 1108
76de302d 1109 mb_type = svq3_get_ue_golomb(&s->gb);
da3b9756 1110
a7d2861d 1111 if (s->pict_type == AV_PICTURE_TYPE_I)
76de302d 1112 mb_type += 8;
a7d2861d 1113 else if (s->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4)
76de302d 1114 mb_type += 4;
9a2e7911 1115 if (mb_type > 33 || svq3_decode_mb(svq3, mb_type)) {
a7d2861d
DB
1116 av_log(h->s.avctx, AV_LOG_ERROR,
1117 "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
76de302d
DB
1118 return -1;
1119 }
8b82a956 1120
a7d2861d
DB
1121 if (mb_type != 0)
1122 ff_h264_hl_decode_mb(h);
8b82a956 1123
a7d2861d 1124 if (s->pict_type != AV_PICTURE_TYPE_B && !s->low_delay)
657ccb5a 1125 s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
975a1447 1126 (s->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
76de302d 1127 }
2e26c8d2 1128
1d0feb5d 1129 ff_mpeg_draw_horiz_band(s, 16 * s->mb_y, 16);
8b82a956 1130 }
4c701ac8 1131
efd29844 1132 ff_MPV_frame_end(s);
8b82a956 1133
a7d2861d
DB
1134 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay)
1135 *(AVFrame *)data = s->current_picture.f;
1136 else
1137 *(AVFrame *)data = s->last_picture.f;
da3b9756 1138
76de302d 1139 /* Do not output the last pic after seeking. */
a7d2861d 1140 if (s->last_picture_ptr || s->low_delay)
df9b9567 1141 *got_frame = 1;
da3b9756 1142
76de302d 1143 return buf_size;
8b82a956
MN
1144}
1145
8dfc6d1f
BC
1146static int svq3_decode_end(AVCodecContext *avctx)
1147{
1148 SVQ3Context *svq3 = avctx->priv_data;
a7d2861d 1149 H264Context *h = &svq3->h;
8dfc6d1f
BC
1150 MpegEncContext *s = &h->s;
1151
1152 ff_h264_free_context(h);
1153
efd29844 1154 ff_MPV_common_end(s);
8dfc6d1f
BC
1155
1156 return 0;
1157}
8b82a956 1158
d36beb3f 1159AVCodec ff_svq3_decoder = {
ec6402b7
AK
1160 .name = "svq3",
1161 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 1162 .id = AV_CODEC_ID_SVQ3,
ec6402b7
AK
1163 .priv_data_size = sizeof(SVQ3Context),
1164 .init = svq3_decode_init,
1165 .close = svq3_decode_end,
1166 .decode = svq3_decode_frame,
a7d2861d
DB
1167 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND |
1168 CODEC_CAP_DR1 |
00c3b67b
MS
1169 CODEC_CAP_DELAY,
1170 .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
a7d2861d
DB
1171 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,
1172 AV_PIX_FMT_NONE},
8b82a956 1173};