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