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