]> git.sesse.net Git - ffmpeg/blob - libavcodec/svq3.c
avformat/argo_asf: initialise file header inline
[ffmpeg] / libavcodec / svq3.c
1 /*
2  * Copyright (c) 2003 The FFmpeg Project
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
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
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /*
22  * How to use this decoder:
23  * SVQ3 data is transported within Apple Quicktime files. Quicktime files
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
30  * via the AVCodecContext's extradata[_size] field:
31  *
32  * AVCodecContext.extradata = pointer to ImageDescription, first characters
33  * are expected to be 'S', 'V', 'Q', and '3', NOT the 4-byte atom length
34  * AVCodecContext.extradata_size = size of ImageDescription atom memory
35  * buffer (which will be the same as the ImageDescription atom size field
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  *  http://samples.mplayerhq.hu/V-codecs/SVQ3/Vertical400kbit.sorenson3.mov
41  */
42
43 #include <inttypes.h>
44
45 #include "libavutil/attributes.h"
46 #include "libavutil/crc.h"
47
48 #include "internal.h"
49 #include "avcodec.h"
50 #include "mpegutils.h"
51 #include "h264dec.h"
52 #include "h264data.h"
53 #include "golomb.h"
54 #include "hpeldsp.h"
55 #include "mathops.h"
56 #include "rectangle.h"
57 #include "tpeldsp.h"
58
59 #if CONFIG_ZLIB
60 #include <zlib.h>
61 #endif
62
63 #include "svq1.h"
64
65 /**
66  * @file
67  * svq3 decoder.
68  */
69
70 typedef struct SVQ3Frame {
71     AVFrame *f;
72
73     AVBufferRef *motion_val_buf[2];
74     int16_t (*motion_val[2])[2];
75
76     AVBufferRef *mb_type_buf;
77     uint32_t *mb_type;
78
79
80     AVBufferRef *ref_index_buf[2];
81     int8_t *ref_index[2];
82 } SVQ3Frame;
83
84 typedef struct SVQ3Context {
85     AVCodecContext *avctx;
86
87     H264DSPContext  h264dsp;
88     H264PredContext hpc;
89     HpelDSPContext hdsp;
90     TpelDSPContext tdsp;
91     VideoDSPContext vdsp;
92
93     SVQ3Frame *cur_pic;
94     SVQ3Frame *next_pic;
95     SVQ3Frame *last_pic;
96     GetBitContext gb;
97     GetBitContext gb_slice;
98     uint8_t *slice_buf;
99     int slice_size;
100     int halfpel_flag;
101     int thirdpel_flag;
102     int has_watermark;
103     uint32_t watermark_key;
104     uint8_t *buf;
105     int buf_size;
106     int adaptive_quant;
107     int next_p_frame_damaged;
108     int h_edge_pos;
109     int v_edge_pos;
110     int last_frame_output;
111     int slice_num;
112     int qscale;
113     int cbp;
114     int frame_num;
115     int frame_num_offset;
116     int prev_frame_num_offset;
117     int prev_frame_num;
118
119     enum AVPictureType pict_type;
120     enum AVPictureType slice_type;
121     int low_delay;
122
123     int mb_x, mb_y;
124     int mb_xy;
125     int mb_width, mb_height;
126     int mb_stride, mb_num;
127     int b_stride;
128
129     uint32_t *mb2br_xy;
130
131     int chroma_pred_mode;
132     int intra16x16_pred_mode;
133
134     int8_t   intra4x4_pred_mode_cache[5 * 8];
135     int8_t (*intra4x4_pred_mode);
136
137     unsigned int top_samples_available;
138     unsigned int topright_samples_available;
139     unsigned int left_samples_available;
140
141     uint8_t *edge_emu_buffer;
142
143     DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5 * 8][2];
144     DECLARE_ALIGNED(8,  int8_t, ref_cache)[2][5 * 8];
145     DECLARE_ALIGNED(16, int16_t, mb)[16 * 48 * 2];
146     DECLARE_ALIGNED(16, int16_t, mb_luma_dc)[3][16 * 2];
147     DECLARE_ALIGNED(8, uint8_t, non_zero_count_cache)[15 * 8];
148     uint32_t dequant4_coeff[QP_MAX_NUM + 1][16];
149     int block_offset[2 * (16 * 3)];
150 } SVQ3Context;
151
152 #define FULLPEL_MODE  1
153 #define HALFPEL_MODE  2
154 #define THIRDPEL_MODE 3
155 #define PREDICT_MODE  4
156
157 /* dual scan (from some older H.264 draft)
158  * o-->o-->o   o
159  *         |  /|
160  * o   o   o / o
161  * | / |   |/  |
162  * o   o   o   o
163  *   /
164  * o-->o-->o-->o
165  */
166 static const uint8_t svq3_scan[16] = {
167     0 + 0 * 4, 1 + 0 * 4, 2 + 0 * 4, 2 + 1 * 4,
168     2 + 2 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4,
169     0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 1 + 2 * 4,
170     0 + 3 * 4, 1 + 3 * 4, 2 + 3 * 4, 3 + 3 * 4,
171 };
172
173 static const uint8_t luma_dc_zigzag_scan[16] = {
174     0 * 16 + 0 * 64, 1 * 16 + 0 * 64, 2 * 16 + 0 * 64, 0 * 16 + 2 * 64,
175     3 * 16 + 0 * 64, 0 * 16 + 1 * 64, 1 * 16 + 1 * 64, 2 * 16 + 1 * 64,
176     1 * 16 + 2 * 64, 2 * 16 + 2 * 64, 3 * 16 + 2 * 64, 0 * 16 + 3 * 64,
177     3 * 16 + 1 * 64, 1 * 16 + 3 * 64, 2 * 16 + 3 * 64, 3 * 16 + 3 * 64,
178 };
179
180 static const uint8_t svq3_pred_0[25][2] = {
181     { 0, 0 },
182     { 1, 0 }, { 0, 1 },
183     { 0, 2 }, { 1, 1 }, { 2, 0 },
184     { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
185     { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
186     { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
187     { 2, 4 }, { 3, 3 }, { 4, 2 },
188     { 4, 3 }, { 3, 4 },
189     { 4, 4 }
190 };
191
192 static const int8_t svq3_pred_1[6][6][5] = {
193     { { 2, -1, -1, -1, -1 }, { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 },
194       { 2,  1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, { 1, 2, -1, -1, -1 } },
195     { { 0,  2, -1, -1, -1 }, { 0, 2,  1,  4,  3 }, { 0, 1,  2,  4,  3 },
196       { 0,  2,  1,  4,  3 }, { 2, 0,  1,  3,  4 }, { 0, 4,  2,  1,  3 } },
197     { { 2,  0, -1, -1, -1 }, { 2, 1,  0,  4,  3 }, { 1, 2,  4,  0,  3 },
198       { 2,  1,  0,  4,  3 }, { 2, 1,  4,  3,  0 }, { 1, 2,  4,  0,  3 } },
199     { { 2,  0, -1, -1, -1 }, { 2, 0,  1,  4,  3 }, { 1, 2,  0,  4,  3 },
200       { 2,  1,  0,  4,  3 }, { 2, 1,  3,  4,  0 }, { 2, 4,  1,  0,  3 } },
201     { { 0,  2, -1, -1, -1 }, { 0, 2,  1,  3,  4 }, { 1, 2,  3,  0,  4 },
202       { 2,  0,  1,  3,  4 }, { 2, 1,  3,  0,  4 }, { 2, 0,  4,  3,  1 } },
203     { { 0,  2, -1, -1, -1 }, { 0, 2,  4,  1,  3 }, { 1, 4,  2,  0,  3 },
204       { 4,  2,  0,  1,  3 }, { 2, 0,  1,  4,  3 }, { 4, 2,  1,  0,  3 } },
205 };
206
207 static const struct {
208     uint8_t run;
209     uint8_t level;
210 } svq3_dct_tables[2][16] = {
211     { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
212       { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
213     { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
214       { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
215 };
216
217 static const uint32_t svq3_dequant_coeff[32] = {
218      3881,  4351,  4890,  5481,   6154,   6914,   7761,   8718,
219      9781, 10987, 12339, 13828,  15523,  17435,  19561,  21873,
220     24552, 27656, 30847, 34870,  38807,  43747,  49103,  54683,
221     61694, 68745, 77615, 89113, 100253, 109366, 126635, 141533
222 };
223
224 static int svq3_decode_end(AVCodecContext *avctx);
225
226 static void svq3_luma_dc_dequant_idct_c(int16_t *output, int16_t *input, int qp)
227 {
228     const unsigned qmul = svq3_dequant_coeff[qp];
229 #define stride 16
230     int i;
231     int temp[16];
232     static const uint8_t x_offset[4] = { 0, 1 * stride, 4 * stride, 5 * stride };
233
234     for (i = 0; i < 4; i++) {
235         const int z0 = 13 * (input[4 * i + 0] +      input[4 * i + 2]);
236         const int z1 = 13 * (input[4 * i + 0] -      input[4 * i + 2]);
237         const int z2 =  7 *  input[4 * i + 1] - 17 * input[4 * i + 3];
238         const int z3 = 17 *  input[4 * i + 1] +  7 * input[4 * i + 3];
239
240         temp[4 * i + 0] = z0 + z3;
241         temp[4 * i + 1] = z1 + z2;
242         temp[4 * i + 2] = z1 - z2;
243         temp[4 * i + 3] = z0 - z3;
244     }
245
246     for (i = 0; i < 4; i++) {
247         const int offset = x_offset[i];
248         const int z0     = 13 * (temp[4 * 0 + i] +      temp[4 * 2 + i]);
249         const int z1     = 13 * (temp[4 * 0 + i] -      temp[4 * 2 + i]);
250         const int z2     =  7 *  temp[4 * 1 + i] - 17 * temp[4 * 3 + i];
251         const int z3     = 17 *  temp[4 * 1 + i] +  7 * temp[4 * 3 + i];
252
253         output[stride *  0 + offset] = (int)((z0 + z3) * qmul + 0x80000) >> 20;
254         output[stride *  2 + offset] = (int)((z1 + z2) * qmul + 0x80000) >> 20;
255         output[stride *  8 + offset] = (int)((z1 - z2) * qmul + 0x80000) >> 20;
256         output[stride * 10 + offset] = (int)((z0 - z3) * qmul + 0x80000) >> 20;
257     }
258 }
259 #undef stride
260
261 static void svq3_add_idct_c(uint8_t *dst, int16_t *block,
262                             int stride, int qp, int dc)
263 {
264     const int qmul = svq3_dequant_coeff[qp];
265     int i;
266
267     if (dc) {
268         dc       = 13 * 13 * (dc == 1 ? 1538U* block[0]
269                                       : qmul * (block[0] >> 3) / 2);
270         block[0] = 0;
271     }
272
273     for (i = 0; i < 4; i++) {
274         const int z0 = 13 * (block[0 + 4 * i] +      block[2 + 4 * i]);
275         const int z1 = 13 * (block[0 + 4 * i] -      block[2 + 4 * i]);
276         const int z2 =  7 *  block[1 + 4 * i] - 17 * block[3 + 4 * i];
277         const int z3 = 17 *  block[1 + 4 * i] +  7 * block[3 + 4 * i];
278
279         block[0 + 4 * i] = z0 + z3;
280         block[1 + 4 * i] = z1 + z2;
281         block[2 + 4 * i] = z1 - z2;
282         block[3 + 4 * i] = z0 - z3;
283     }
284
285     for (i = 0; i < 4; i++) {
286         const unsigned z0 = 13 * (block[i + 4 * 0] +      block[i + 4 * 2]);
287         const unsigned z1 = 13 * (block[i + 4 * 0] -      block[i + 4 * 2]);
288         const unsigned z2 =  7 *  block[i + 4 * 1] - 17 * block[i + 4 * 3];
289         const unsigned z3 = 17 *  block[i + 4 * 1] +  7 * block[i + 4 * 3];
290         const int rr = (dc + 0x80000u);
291
292         dst[i + stride * 0] = av_clip_uint8(dst[i + stride * 0] + ((int)((z0 + z3) * qmul + rr) >> 20));
293         dst[i + stride * 1] = av_clip_uint8(dst[i + stride * 1] + ((int)((z1 + z2) * qmul + rr) >> 20));
294         dst[i + stride * 2] = av_clip_uint8(dst[i + stride * 2] + ((int)((z1 - z2) * qmul + rr) >> 20));
295         dst[i + stride * 3] = av_clip_uint8(dst[i + stride * 3] + ((int)((z0 - z3) * qmul + rr) >> 20));
296     }
297
298     memset(block, 0, 16 * sizeof(int16_t));
299 }
300
301 static inline int svq3_decode_block(GetBitContext *gb, int16_t *block,
302                                     int index, const int type)
303 {
304     static const uint8_t *const scan_patterns[4] = {
305         luma_dc_zigzag_scan, ff_zigzag_scan, svq3_scan, ff_h264_chroma_dc_scan
306     };
307
308     int run, level, sign, limit;
309     unsigned vlc;
310     const int intra           = 3 * type >> 2;
311     const uint8_t *const scan = scan_patterns[type];
312
313     for (limit = (16 >> intra); index < 16; index = limit, limit += 8) {
314         for (; (vlc = get_interleaved_ue_golomb(gb)) != 0; index++) {
315             if ((int32_t)vlc < 0)
316                 return -1;
317
318             sign     = (vlc & 1) ? 0 : -1;
319             vlc      = vlc + 1 >> 1;
320
321             if (type == 3) {
322                 if (vlc < 3) {
323                     run   = 0;
324                     level = vlc;
325                 } else if (vlc < 4) {
326                     run   = 1;
327                     level = 1;
328                 } else {
329                     run   = vlc & 0x3;
330                     level = (vlc + 9 >> 2) - run;
331                 }
332             } else {
333                 if (vlc < 16U) {
334                     run   = svq3_dct_tables[intra][vlc].run;
335                     level = svq3_dct_tables[intra][vlc].level;
336                 } else if (intra) {
337                     run   = vlc & 0x7;
338                     level = (vlc >> 3) + ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
339                 } else {
340                     run   = vlc & 0xF;
341                     level = (vlc >> 4) + ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
342                 }
343             }
344
345
346             if ((index += run) >= limit)
347                 return -1;
348
349             block[scan[index]] = (level ^ sign) - sign;
350         }
351
352         if (type != 2) {
353             break;
354         }
355     }
356
357     return 0;
358 }
359
360 static av_always_inline int
361 svq3_fetch_diagonal_mv(const SVQ3Context *s, const int16_t **C,
362                        int i, int list, int part_width)
363 {
364     const int topright_ref = s->ref_cache[list][i - 8 + part_width];
365
366     if (topright_ref != PART_NOT_AVAILABLE) {
367         *C = s->mv_cache[list][i - 8 + part_width];
368         return topright_ref;
369     } else {
370         *C = s->mv_cache[list][i - 8 - 1];
371         return s->ref_cache[list][i - 8 - 1];
372     }
373 }
374
375 /**
376  * Get the predicted MV.
377  * @param n the block index
378  * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
379  * @param mx the x component of the predicted motion vector
380  * @param my the y component of the predicted motion vector
381  */
382 static av_always_inline void svq3_pred_motion(const SVQ3Context *s, int n,
383                                               int part_width, int list,
384                                               int ref, int *const mx, int *const my)
385 {
386     const int index8       = scan8[n];
387     const int top_ref      = s->ref_cache[list][index8 - 8];
388     const int left_ref     = s->ref_cache[list][index8 - 1];
389     const int16_t *const A = s->mv_cache[list][index8 - 1];
390     const int16_t *const B = s->mv_cache[list][index8 - 8];
391     const int16_t *C;
392     int diagonal_ref, match_count;
393
394 /* mv_cache
395  * B . . A T T T T
396  * U . . L . . , .
397  * U . . L . . . .
398  * U . . L . . , .
399  * . . . L . . . .
400  */
401
402     diagonal_ref = svq3_fetch_diagonal_mv(s, &C, index8, list, part_width);
403     match_count  = (diagonal_ref == ref) + (top_ref == ref) + (left_ref == ref);
404     if (match_count > 1) { //most common
405         *mx = mid_pred(A[0], B[0], C[0]);
406         *my = mid_pred(A[1], B[1], C[1]);
407     } else if (match_count == 1) {
408         if (left_ref == ref) {
409             *mx = A[0];
410             *my = A[1];
411         } else if (top_ref == ref) {
412             *mx = B[0];
413             *my = B[1];
414         } else {
415             *mx = C[0];
416             *my = C[1];
417         }
418     } else {
419         if (top_ref      == PART_NOT_AVAILABLE &&
420             diagonal_ref == PART_NOT_AVAILABLE &&
421             left_ref     != PART_NOT_AVAILABLE) {
422             *mx = A[0];
423             *my = A[1];
424         } else {
425             *mx = mid_pred(A[0], B[0], C[0]);
426             *my = mid_pred(A[1], B[1], C[1]);
427         }
428     }
429 }
430
431 static inline void svq3_mc_dir_part(SVQ3Context *s,
432                                     int x, int y, int width, int height,
433                                     int mx, int my, int dxy,
434                                     int thirdpel, int dir, int avg)
435 {
436     const SVQ3Frame *pic = (dir == 0) ? s->last_pic : s->next_pic;
437     uint8_t *src, *dest;
438     int i, emu = 0;
439     int blocksize = 2 - (width >> 3); // 16->0, 8->1, 4->2
440     int linesize   = s->cur_pic->f->linesize[0];
441     int uvlinesize = s->cur_pic->f->linesize[1];
442
443     mx += x;
444     my += y;
445
446     if (mx < 0 || mx >= s->h_edge_pos - width  - 1 ||
447         my < 0 || my >= s->v_edge_pos - height - 1) {
448         emu = 1;
449         mx = av_clip(mx, -16, s->h_edge_pos - width  + 15);
450         my = av_clip(my, -16, s->v_edge_pos - height + 15);
451     }
452
453     /* form component predictions */
454     dest = s->cur_pic->f->data[0] + x + y * linesize;
455     src  = pic->f->data[0] + mx + my * linesize;
456
457     if (emu) {
458         s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
459                                  linesize, linesize,
460                                  width + 1, height + 1,
461                                  mx, my, s->h_edge_pos, s->v_edge_pos);
462         src = s->edge_emu_buffer;
463     }
464     if (thirdpel)
465         (avg ? s->tdsp.avg_tpel_pixels_tab
466              : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src, linesize,
467                                                  width, height);
468     else
469         (avg ? s->hdsp.avg_pixels_tab
470              : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src, linesize,
471                                                        height);
472
473     if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
474         mx     = mx + (mx < (int) x) >> 1;
475         my     = my + (my < (int) y) >> 1;
476         width  = width  >> 1;
477         height = height >> 1;
478         blocksize++;
479
480         for (i = 1; i < 3; i++) {
481             dest = s->cur_pic->f->data[i] + (x >> 1) + (y >> 1) * uvlinesize;
482             src  = pic->f->data[i] + mx + my * uvlinesize;
483
484             if (emu) {
485                 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
486                                          uvlinesize, uvlinesize,
487                                          width + 1, height + 1,
488                                          mx, my, (s->h_edge_pos >> 1),
489                                          s->v_edge_pos >> 1);
490                 src = s->edge_emu_buffer;
491             }
492             if (thirdpel)
493                 (avg ? s->tdsp.avg_tpel_pixels_tab
494                      : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src,
495                                                          uvlinesize,
496                                                          width, height);
497             else
498                 (avg ? s->hdsp.avg_pixels_tab
499                      : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src,
500                                                                uvlinesize,
501                                                                height);
502         }
503     }
504 }
505
506 static inline int svq3_mc_dir(SVQ3Context *s, int size, int mode,
507                               int dir, int avg)
508 {
509     int i, j, k, mx, my, dx, dy, x, y;
510     const int part_width    = ((size & 5) == 4) ? 4 : 16 >> (size & 1);
511     const int part_height   = 16 >> ((unsigned)(size + 1) / 3);
512     const int extra_width   = (mode == PREDICT_MODE) ? -16 * 6 : 0;
513     const int h_edge_pos    = 6 * (s->h_edge_pos - part_width)  - extra_width;
514     const int v_edge_pos    = 6 * (s->v_edge_pos - part_height) - extra_width;
515
516     for (i = 0; i < 16; i += part_height)
517         for (j = 0; j < 16; j += part_width) {
518             const int b_xy = (4 * s->mb_x + (j >> 2)) +
519                              (4 * s->mb_y + (i >> 2)) * s->b_stride;
520             int dxy;
521             x = 16 * s->mb_x + j;
522             y = 16 * s->mb_y + i;
523             k = (j >> 2 & 1) + (i >> 1 & 2) +
524                 (j >> 1 & 4) + (i      & 8);
525
526             if (mode != PREDICT_MODE) {
527                 svq3_pred_motion(s, k, part_width >> 2, dir, 1, &mx, &my);
528             } else {
529                 mx = s->next_pic->motion_val[0][b_xy][0] * 2;
530                 my = s->next_pic->motion_val[0][b_xy][1] * 2;
531
532                 if (dir == 0) {
533                     mx = mx * s->frame_num_offset /
534                          s->prev_frame_num_offset + 1 >> 1;
535                     my = my * s->frame_num_offset /
536                          s->prev_frame_num_offset + 1 >> 1;
537                 } else {
538                     mx = mx * (s->frame_num_offset - s->prev_frame_num_offset) /
539                          s->prev_frame_num_offset + 1 >> 1;
540                     my = my * (s->frame_num_offset - s->prev_frame_num_offset) /
541                          s->prev_frame_num_offset + 1 >> 1;
542                 }
543             }
544
545             /* clip motion vector prediction to frame border */
546             mx = av_clip(mx, extra_width - 6 * x, h_edge_pos - 6 * x);
547             my = av_clip(my, extra_width - 6 * y, v_edge_pos - 6 * y);
548
549             /* get (optional) motion vector differential */
550             if (mode == PREDICT_MODE) {
551                 dx = dy = 0;
552             } else {
553                 dy = get_interleaved_se_golomb(&s->gb_slice);
554                 dx = get_interleaved_se_golomb(&s->gb_slice);
555
556                 if (dx != (int16_t)dx || dy != (int16_t)dy) {
557                     av_log(s->avctx, AV_LOG_ERROR, "invalid MV vlc\n");
558                     return -1;
559                 }
560             }
561
562             /* compute motion vector */
563             if (mode == THIRDPEL_MODE) {
564                 int fx, fy;
565                 mx  = (mx + 1 >> 1) + dx;
566                 my  = (my + 1 >> 1) + dy;
567                 fx  = (unsigned)(mx + 0x30000) / 3 - 0x10000;
568                 fy  = (unsigned)(my + 0x30000) / 3 - 0x10000;
569                 dxy = (mx - 3 * fx) + 4 * (my - 3 * fy);
570
571                 svq3_mc_dir_part(s, x, y, part_width, part_height,
572                                  fx, fy, dxy, 1, dir, avg);
573                 mx += mx;
574                 my += my;
575             } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) {
576                 mx  = (unsigned)(mx + 1 + 0x30000) / 3 + dx - 0x10000;
577                 my  = (unsigned)(my + 1 + 0x30000) / 3 + dy - 0x10000;
578                 dxy = (mx & 1) + 2 * (my & 1);
579
580                 svq3_mc_dir_part(s, x, y, part_width, part_height,
581                                  mx >> 1, my >> 1, dxy, 0, dir, avg);
582                 mx *= 3;
583                 my *= 3;
584             } else {
585                 mx = (unsigned)(mx + 3 + 0x60000) / 6 + dx - 0x10000;
586                 my = (unsigned)(my + 3 + 0x60000) / 6 + dy - 0x10000;
587
588                 svq3_mc_dir_part(s, x, y, part_width, part_height,
589                                  mx, my, 0, 0, dir, avg);
590                 mx *= 6;
591                 my *= 6;
592             }
593
594             /* update mv_cache */
595             if (mode != PREDICT_MODE) {
596                 int32_t mv = pack16to32(mx, my);
597
598                 if (part_height == 8 && i < 8) {
599                     AV_WN32A(s->mv_cache[dir][scan8[k] + 1 * 8], mv);
600
601                     if (part_width == 8 && j < 8)
602                         AV_WN32A(s->mv_cache[dir][scan8[k] + 1 + 1 * 8], mv);
603                 }
604                 if (part_width == 8 && j < 8)
605                     AV_WN32A(s->mv_cache[dir][scan8[k] + 1], mv);
606                 if (part_width == 4 || part_height == 4)
607                     AV_WN32A(s->mv_cache[dir][scan8[k]], mv);
608             }
609
610             /* write back motion vectors */
611             fill_rectangle(s->cur_pic->motion_val[dir][b_xy],
612                            part_width >> 2, part_height >> 2, s->b_stride,
613                            pack16to32(mx, my), 4);
614         }
615
616     return 0;
617 }
618
619 static av_always_inline void hl_decode_mb_idct_luma(SVQ3Context *s,
620                                                     int mb_type, const int *block_offset,
621                                                     int linesize, uint8_t *dest_y)
622 {
623     int i;
624     if (!IS_INTRA4x4(mb_type)) {
625         for (i = 0; i < 16; i++)
626             if (s->non_zero_count_cache[scan8[i]] || s->mb[i * 16]) {
627                 uint8_t *const ptr = dest_y + block_offset[i];
628                 svq3_add_idct_c(ptr, s->mb + i * 16, linesize,
629                                 s->qscale, IS_INTRA(mb_type) ? 1 : 0);
630             }
631     }
632 }
633
634 static av_always_inline void hl_decode_mb_predict_luma(SVQ3Context *s,
635                                                        int mb_type,
636                                                        const int *block_offset,
637                                                        int linesize,
638                                                        uint8_t *dest_y)
639 {
640     int i;
641     int qscale = s->qscale;
642
643     if (IS_INTRA4x4(mb_type)) {
644         for (i = 0; i < 16; i++) {
645             uint8_t *const ptr = dest_y + block_offset[i];
646             const int dir      = s->intra4x4_pred_mode_cache[scan8[i]];
647
648             uint8_t *topright;
649             int nnz, tr;
650             if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
651                 const int topright_avail = (s->topright_samples_available << i) & 0x8000;
652                 av_assert2(s->mb_y || linesize <= block_offset[i]);
653                 if (!topright_avail) {
654                     tr       = ptr[3 - linesize] * 0x01010101u;
655                     topright = (uint8_t *)&tr;
656                 } else
657                     topright = ptr + 4 - linesize;
658             } else
659                 topright = NULL;
660
661             s->hpc.pred4x4[dir](ptr, topright, linesize);
662             nnz = s->non_zero_count_cache[scan8[i]];
663             if (nnz) {
664                 svq3_add_idct_c(ptr, s->mb + i * 16, linesize, qscale, 0);
665             }
666         }
667     } else {
668         s->hpc.pred16x16[s->intra16x16_pred_mode](dest_y, linesize);
669         svq3_luma_dc_dequant_idct_c(s->mb, s->mb_luma_dc[0], qscale);
670     }
671 }
672
673 static void hl_decode_mb(SVQ3Context *s)
674 {
675     const int mb_x    = s->mb_x;
676     const int mb_y    = s->mb_y;
677     const int mb_xy   = s->mb_xy;
678     const int mb_type = s->cur_pic->mb_type[mb_xy];
679     uint8_t *dest_y, *dest_cb, *dest_cr;
680     int linesize, uvlinesize;
681     int i, j;
682     const int *block_offset = &s->block_offset[0];
683     const int block_h   = 16 >> 1;
684
685     linesize   = s->cur_pic->f->linesize[0];
686     uvlinesize = s->cur_pic->f->linesize[1];
687
688     dest_y  = s->cur_pic->f->data[0] + (mb_x     + mb_y * linesize)  * 16;
689     dest_cb = s->cur_pic->f->data[1] +  mb_x * 8 + mb_y * uvlinesize * block_h;
690     dest_cr = s->cur_pic->f->data[2] +  mb_x * 8 + mb_y * uvlinesize * block_h;
691
692     s->vdsp.prefetch(dest_y  + (s->mb_x & 3) * 4 * linesize   + 64, linesize,      4);
693     s->vdsp.prefetch(dest_cb + (s->mb_x & 7)     * uvlinesize + 64, dest_cr - dest_cb, 2);
694
695     if (IS_INTRA(mb_type)) {
696         s->hpc.pred8x8[s->chroma_pred_mode](dest_cb, uvlinesize);
697         s->hpc.pred8x8[s->chroma_pred_mode](dest_cr, uvlinesize);
698
699         hl_decode_mb_predict_luma(s, mb_type, block_offset, linesize, dest_y);
700     }
701
702     hl_decode_mb_idct_luma(s, mb_type, block_offset, linesize, dest_y);
703
704     if (s->cbp & 0x30) {
705         uint8_t *dest[2] = { dest_cb, dest_cr };
706         s->h264dsp.h264_chroma_dc_dequant_idct(s->mb + 16 * 16 * 1,
707                                                s->dequant4_coeff[4][0]);
708         s->h264dsp.h264_chroma_dc_dequant_idct(s->mb + 16 * 16 * 2,
709                                                s->dequant4_coeff[4][0]);
710         for (j = 1; j < 3; j++) {
711             for (i = j * 16; i < j * 16 + 4; i++)
712                 if (s->non_zero_count_cache[scan8[i]] || s->mb[i * 16]) {
713                     uint8_t *const ptr = dest[j - 1] + block_offset[i];
714                     svq3_add_idct_c(ptr, s->mb + i * 16,
715                                     uvlinesize, ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
716                 }
717         }
718     }
719 }
720
721 static int svq3_decode_mb(SVQ3Context *s, unsigned int mb_type)
722 {
723     int i, j, k, m, dir, mode;
724     int cbp = 0;
725     uint32_t vlc;
726     int8_t *top, *left;
727     const int mb_xy = s->mb_xy;
728     const int b_xy  = 4 * s->mb_x + 4 * s->mb_y * s->b_stride;
729
730     s->top_samples_available      = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
731     s->left_samples_available     = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
732     s->topright_samples_available = 0xFFFF;
733
734     if (mb_type == 0) {           /* SKIP */
735         if (s->pict_type == AV_PICTURE_TYPE_P ||
736             s->next_pic->mb_type[mb_xy] == -1) {
737             svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
738                              0, 0, 0, 0, 0, 0);
739
740             if (s->pict_type == AV_PICTURE_TYPE_B)
741                 svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
742                                  0, 0, 0, 0, 1, 1);
743
744             mb_type = MB_TYPE_SKIP;
745         } else {
746             mb_type = FFMIN(s->next_pic->mb_type[mb_xy], 6);
747             if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 0, 0) < 0)
748                 return -1;
749             if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 1, 1) < 0)
750                 return -1;
751
752             mb_type = MB_TYPE_16x16;
753         }
754     } else if (mb_type < 8) {     /* INTER */
755         if (s->thirdpel_flag && s->halfpel_flag == !get_bits1(&s->gb_slice))
756             mode = THIRDPEL_MODE;
757         else if (s->halfpel_flag &&
758                  s->thirdpel_flag == !get_bits1(&s->gb_slice))
759             mode = HALFPEL_MODE;
760         else
761             mode = FULLPEL_MODE;
762
763         /* fill caches */
764         /* note ref_cache should contain here:
765          *  ????????
766          *  ???11111
767          *  N??11111
768          *  N??11111
769          *  N??11111
770          */
771
772         for (m = 0; m < 2; m++) {
773             if (s->mb_x > 0 && s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - 1] + 6] != -1) {
774                 for (i = 0; i < 4; i++)
775                     AV_COPY32(s->mv_cache[m][scan8[0] - 1 + i * 8],
776                               s->cur_pic->motion_val[m][b_xy - 1 + i * s->b_stride]);
777             } else {
778                 for (i = 0; i < 4; i++)
779                     AV_ZERO32(s->mv_cache[m][scan8[0] - 1 + i * 8]);
780             }
781             if (s->mb_y > 0) {
782                 memcpy(s->mv_cache[m][scan8[0] - 1 * 8],
783                        s->cur_pic->motion_val[m][b_xy - s->b_stride],
784                        4 * 2 * sizeof(int16_t));
785                 memset(&s->ref_cache[m][scan8[0] - 1 * 8],
786                        (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);
787
788                 if (s->mb_x < s->mb_width - 1) {
789                     AV_COPY32(s->mv_cache[m][scan8[0] + 4 - 1 * 8],
790                               s->cur_pic->motion_val[m][b_xy - s->b_stride + 4]);
791                     s->ref_cache[m][scan8[0] + 4 - 1 * 8] =
792                         (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride + 1] + 6] == -1 ||
793                          s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1;
794                 } else
795                     s->ref_cache[m][scan8[0] + 4 - 1 * 8] = PART_NOT_AVAILABLE;
796                 if (s->mb_x > 0) {
797                     AV_COPY32(s->mv_cache[m][scan8[0] - 1 - 1 * 8],
798                               s->cur_pic->motion_val[m][b_xy - s->b_stride - 1]);
799                     s->ref_cache[m][scan8[0] - 1 - 1 * 8] =
800                         (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] == -1) ? PART_NOT_AVAILABLE : 1;
801                 } else
802                     s->ref_cache[m][scan8[0] - 1 - 1 * 8] = PART_NOT_AVAILABLE;
803             } else
804                 memset(&s->ref_cache[m][scan8[0] - 1 * 8 - 1],
805                        PART_NOT_AVAILABLE, 8);
806
807             if (s->pict_type != AV_PICTURE_TYPE_B)
808                 break;
809         }
810
811         /* decode motion vector(s) and form prediction(s) */
812         if (s->pict_type == AV_PICTURE_TYPE_P) {
813             if (svq3_mc_dir(s, mb_type - 1, mode, 0, 0) < 0)
814                 return -1;
815         } else {        /* AV_PICTURE_TYPE_B */
816             if (mb_type != 2) {
817                 if (svq3_mc_dir(s, 0, mode, 0, 0) < 0)
818                     return -1;
819             } else {
820                 for (i = 0; i < 4; i++)
821                     memset(s->cur_pic->motion_val[0][b_xy + i * s->b_stride],
822                            0, 4 * 2 * sizeof(int16_t));
823             }
824             if (mb_type != 1) {
825                 if (svq3_mc_dir(s, 0, mode, 1, mb_type == 3) < 0)
826                     return -1;
827             } else {
828                 for (i = 0; i < 4; i++)
829                     memset(s->cur_pic->motion_val[1][b_xy + i * s->b_stride],
830                            0, 4 * 2 * sizeof(int16_t));
831             }
832         }
833
834         mb_type = MB_TYPE_16x16;
835     } else if (mb_type == 8 || mb_type == 33) {   /* INTRA4x4 */
836         int8_t *i4x4       = s->intra4x4_pred_mode + s->mb2br_xy[s->mb_xy];
837         int8_t *i4x4_cache = s->intra4x4_pred_mode_cache;
838
839         memset(s->intra4x4_pred_mode_cache, -1, 8 * 5 * sizeof(int8_t));
840
841         if (mb_type == 8) {
842             if (s->mb_x > 0) {
843                 for (i = 0; i < 4; i++)
844                     s->intra4x4_pred_mode_cache[scan8[0] - 1 + i * 8] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - 1] + 6 - i];
845                 if (s->intra4x4_pred_mode_cache[scan8[0] - 1] == -1)
846                     s->left_samples_available = 0x5F5F;
847             }
848             if (s->mb_y > 0) {
849                 s->intra4x4_pred_mode_cache[4 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 0];
850                 s->intra4x4_pred_mode_cache[5 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 1];
851                 s->intra4x4_pred_mode_cache[6 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 2];
852                 s->intra4x4_pred_mode_cache[7 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 3];
853
854                 if (s->intra4x4_pred_mode_cache[4 + 8 * 0] == -1)
855                     s->top_samples_available = 0x33FF;
856             }
857
858             /* decode prediction codes for luma blocks */
859             for (i = 0; i < 16; i += 2) {
860                 vlc = get_interleaved_ue_golomb(&s->gb_slice);
861
862                 if (vlc >= 25U) {
863                     av_log(s->avctx, AV_LOG_ERROR,
864                            "luma prediction:%"PRIu32"\n", vlc);
865                     return -1;
866                 }
867
868                 left = &s->intra4x4_pred_mode_cache[scan8[i] - 1];
869                 top  = &s->intra4x4_pred_mode_cache[scan8[i] - 8];
870
871                 left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
872                 left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
873
874                 if (left[1] == -1 || left[2] == -1) {
875                     av_log(s->avctx, AV_LOG_ERROR, "weird prediction\n");
876                     return -1;
877                 }
878             }
879         } else {    /* mb_type == 33, DC_128_PRED block type */
880             for (i = 0; i < 4; i++)
881                 memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_PRED, 4);
882         }
883
884         AV_COPY32(i4x4, i4x4_cache + 4 + 8 * 4);
885         i4x4[4] = i4x4_cache[7 + 8 * 3];
886         i4x4[5] = i4x4_cache[7 + 8 * 2];
887         i4x4[6] = i4x4_cache[7 + 8 * 1];
888
889         if (mb_type == 8) {
890             ff_h264_check_intra4x4_pred_mode(s->intra4x4_pred_mode_cache,
891                                              s->avctx, s->top_samples_available,
892                                              s->left_samples_available);
893
894             s->top_samples_available  = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
895             s->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
896         } else {
897             for (i = 0; i < 4; i++)
898                 memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_128_PRED, 4);
899
900             s->top_samples_available  = 0x33FF;
901             s->left_samples_available = 0x5F5F;
902         }
903
904         mb_type = MB_TYPE_INTRA4x4;
905     } else {                      /* INTRA16x16 */
906         dir = ff_h264_i_mb_type_info[mb_type - 8].pred_mode;
907         dir = (dir >> 1) ^ 3 * (dir & 1) ^ 1;
908
909         if ((s->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(s->avctx, s->top_samples_available,
910                                                                      s->left_samples_available, dir, 0)) < 0) {
911             av_log(s->avctx, AV_LOG_ERROR, "ff_h264_check_intra_pred_mode < 0\n");
912             return s->intra16x16_pred_mode;
913         }
914
915         cbp     = ff_h264_i_mb_type_info[mb_type - 8].cbp;
916         mb_type = MB_TYPE_INTRA16x16;
917     }
918
919     if (!IS_INTER(mb_type) && s->pict_type != AV_PICTURE_TYPE_I) {
920         for (i = 0; i < 4; i++)
921             memset(s->cur_pic->motion_val[0][b_xy + i * s->b_stride],
922                    0, 4 * 2 * sizeof(int16_t));
923         if (s->pict_type == AV_PICTURE_TYPE_B) {
924             for (i = 0; i < 4; i++)
925                 memset(s->cur_pic->motion_val[1][b_xy + i * s->b_stride],
926                        0, 4 * 2 * sizeof(int16_t));
927         }
928     }
929     if (!IS_INTRA4x4(mb_type)) {
930         memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy], DC_PRED, 8);
931     }
932     if (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B) {
933         memset(s->non_zero_count_cache + 8, 0, 14 * 8 * sizeof(uint8_t));
934     }
935
936     if (!IS_INTRA16x16(mb_type) &&
937         (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B)) {
938         if ((vlc = get_interleaved_ue_golomb(&s->gb_slice)) >= 48U){
939             av_log(s->avctx, AV_LOG_ERROR, "cbp_vlc=%"PRIu32"\n", vlc);
940             return -1;
941         }
942
943         cbp = IS_INTRA(mb_type) ? ff_h264_golomb_to_intra4x4_cbp[vlc]
944                                 : ff_h264_golomb_to_inter_cbp[vlc];
945     }
946     if (IS_INTRA16x16(mb_type) ||
947         (s->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
948         s->qscale += get_interleaved_se_golomb(&s->gb_slice);
949
950         if (s->qscale > 31u) {
951             av_log(s->avctx, AV_LOG_ERROR, "qscale:%d\n", s->qscale);
952             return -1;
953         }
954     }
955     if (IS_INTRA16x16(mb_type)) {
956         AV_ZERO128(s->mb_luma_dc[0] + 0);
957         AV_ZERO128(s->mb_luma_dc[0] + 8);
958         if (svq3_decode_block(&s->gb_slice, s->mb_luma_dc[0], 0, 1)) {
959             av_log(s->avctx, AV_LOG_ERROR,
960                    "error while decoding intra luma dc\n");
961             return -1;
962         }
963     }
964
965     if (cbp) {
966         const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
967         const int type  = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
968
969         for (i = 0; i < 4; i++)
970             if ((cbp & (1 << i))) {
971                 for (j = 0; j < 4; j++) {
972                     k = index ? (1 * (j & 1) + 2 * (i & 1) +
973                                  2 * (j & 2) + 4 * (i & 2))
974                               : (4 * i + j);
975                     s->non_zero_count_cache[scan8[k]] = 1;
976
977                     if (svq3_decode_block(&s->gb_slice, &s->mb[16 * k], index, type)) {
978                         av_log(s->avctx, AV_LOG_ERROR,
979                                "error while decoding block\n");
980                         return -1;
981                     }
982                 }
983             }
984
985         if ((cbp & 0x30)) {
986             for (i = 1; i < 3; ++i)
987                 if (svq3_decode_block(&s->gb_slice, &s->mb[16 * 16 * i], 0, 3)) {
988                     av_log(s->avctx, AV_LOG_ERROR,
989                            "error while decoding chroma dc block\n");
990                     return -1;
991                 }
992
993             if ((cbp & 0x20)) {
994                 for (i = 1; i < 3; i++) {
995                     for (j = 0; j < 4; j++) {
996                         k                                 = 16 * i + j;
997                         s->non_zero_count_cache[scan8[k]] = 1;
998
999                         if (svq3_decode_block(&s->gb_slice, &s->mb[16 * k], 1, 1)) {
1000                             av_log(s->avctx, AV_LOG_ERROR,
1001                                    "error while decoding chroma ac block\n");
1002                             return -1;
1003                         }
1004                     }
1005                 }
1006             }
1007         }
1008     }
1009
1010     s->cbp                     = cbp;
1011     s->cur_pic->mb_type[mb_xy] = mb_type;
1012
1013     if (IS_INTRA(mb_type))
1014         s->chroma_pred_mode = ff_h264_check_intra_pred_mode(s->avctx, s->top_samples_available,
1015                                                             s->left_samples_available, DC_PRED8x8, 1);
1016
1017     return 0;
1018 }
1019
1020 static int svq3_decode_slice_header(AVCodecContext *avctx)
1021 {
1022     SVQ3Context *s = avctx->priv_data;
1023     const int mb_xy   = s->mb_xy;
1024     int i, header;
1025     unsigned slice_id;
1026
1027     header = get_bits(&s->gb, 8);
1028
1029     if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
1030         /* TODO: what? */
1031         av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
1032         return -1;
1033     } else {
1034         int slice_bits, slice_bytes, slice_length;
1035         int length = header >> 5 & 3;
1036
1037         slice_length = show_bits(&s->gb, 8 * length);
1038         slice_bits   = slice_length * 8;
1039         slice_bytes  = slice_length + length - 1;
1040
1041         skip_bits(&s->gb, 8);
1042
1043         av_fast_malloc(&s->slice_buf, &s->slice_size, slice_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
1044         if (!s->slice_buf)
1045             return AVERROR(ENOMEM);
1046
1047         if (slice_bytes * 8LL > get_bits_left(&s->gb)) {
1048             av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
1049             return AVERROR_INVALIDDATA;
1050         }
1051         memcpy(s->slice_buf, s->gb.buffer + s->gb.index / 8, slice_bytes);
1052
1053         if (s->watermark_key) {
1054             uint32_t header = AV_RL32(&s->slice_buf[1]);
1055             AV_WL32(&s->slice_buf[1], header ^ s->watermark_key);
1056         }
1057         init_get_bits(&s->gb_slice, s->slice_buf, slice_bits);
1058
1059         if (length > 0) {
1060             memmove(s->slice_buf, &s->slice_buf[slice_length], length - 1);
1061         }
1062         skip_bits_long(&s->gb, slice_bytes * 8);
1063     }
1064
1065     if ((slice_id = get_interleaved_ue_golomb(&s->gb_slice)) >= 3) {
1066         av_log(s->avctx, AV_LOG_ERROR, "illegal slice type %u \n", slice_id);
1067         return -1;
1068     }
1069
1070     s->slice_type = ff_h264_golomb_to_pict_type[slice_id];
1071
1072     if ((header & 0x9F) == 2) {
1073         i = (s->mb_num < 64) ? 6 : (1 + av_log2(s->mb_num - 1));
1074         get_bits(&s->gb_slice, i);
1075     } else if (get_bits1(&s->gb_slice)) {
1076         avpriv_report_missing_feature(s->avctx, "Media key encryption");
1077         return AVERROR_PATCHWELCOME;
1078     }
1079
1080     s->slice_num      = get_bits(&s->gb_slice, 8);
1081     s->qscale         = get_bits(&s->gb_slice, 5);
1082     s->adaptive_quant = get_bits1(&s->gb_slice);
1083
1084     /* unknown fields */
1085     skip_bits1(&s->gb_slice);
1086
1087     if (s->has_watermark)
1088         skip_bits1(&s->gb_slice);
1089
1090     skip_bits1(&s->gb_slice);
1091     skip_bits(&s->gb_slice, 2);
1092
1093     if (skip_1stop_8data_bits(&s->gb_slice) < 0)
1094         return AVERROR_INVALIDDATA;
1095
1096     /* reset intra predictors and invalidate motion vector references */
1097     if (s->mb_x > 0) {
1098         memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - 1] + 3,
1099                -1, 4 * sizeof(int8_t));
1100         memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_x],
1101                -1, 8 * sizeof(int8_t) * s->mb_x);
1102     }
1103     if (s->mb_y > 0) {
1104         memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_stride],
1105                -1, 8 * sizeof(int8_t) * (s->mb_width - s->mb_x));
1106
1107         if (s->mb_x > 0)
1108             s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] = -1;
1109     }
1110
1111     return 0;
1112 }
1113
1114 static void init_dequant4_coeff_table(SVQ3Context *s)
1115 {
1116     int q, x;
1117     const int max_qp = 51;
1118
1119     for (q = 0; q < max_qp + 1; q++) {
1120         int shift = ff_h264_quant_div6[q] + 2;
1121         int idx   = ff_h264_quant_rem6[q];
1122         for (x = 0; x < 16; x++)
1123             s->dequant4_coeff[q][(x >> 2) | ((x << 2) & 0xF)] =
1124                 ((uint32_t)ff_h264_dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] * 16) << shift;
1125     }
1126 }
1127
1128 static av_cold int svq3_decode_init(AVCodecContext *avctx)
1129 {
1130     SVQ3Context *s = avctx->priv_data;
1131     int m, x, y;
1132     unsigned char *extradata;
1133     unsigned char *extradata_end;
1134     unsigned int size;
1135     int marker_found = 0;
1136     int ret;
1137
1138     s->cur_pic  = av_mallocz(sizeof(*s->cur_pic));
1139     s->last_pic = av_mallocz(sizeof(*s->last_pic));
1140     s->next_pic = av_mallocz(sizeof(*s->next_pic));
1141     if (!s->next_pic || !s->last_pic || !s->cur_pic) {
1142         ret = AVERROR(ENOMEM);
1143         goto fail;
1144     }
1145
1146     s->cur_pic->f  = av_frame_alloc();
1147     s->last_pic->f = av_frame_alloc();
1148     s->next_pic->f = av_frame_alloc();
1149     if (!s->cur_pic->f || !s->last_pic->f || !s->next_pic->f)
1150         return AVERROR(ENOMEM);
1151
1152     ff_h264dsp_init(&s->h264dsp, 8, 1);
1153     ff_h264_pred_init(&s->hpc, AV_CODEC_ID_SVQ3, 8, 1);
1154     ff_videodsp_init(&s->vdsp, 8);
1155
1156
1157     avctx->bits_per_raw_sample = 8;
1158
1159     ff_hpeldsp_init(&s->hdsp, avctx->flags);
1160     ff_tpeldsp_init(&s->tdsp);
1161
1162     avctx->pix_fmt     = AV_PIX_FMT_YUVJ420P;
1163     avctx->color_range = AVCOL_RANGE_JPEG;
1164
1165     s->avctx         = avctx;
1166     s->halfpel_flag  = 1;
1167     s->thirdpel_flag = 1;
1168     s->has_watermark = 0;
1169
1170     /* prowl for the "SEQH" marker in the extradata */
1171     extradata     = (unsigned char *)avctx->extradata;
1172     extradata_end = avctx->extradata + avctx->extradata_size;
1173     if (extradata) {
1174         for (m = 0; m + 8 < avctx->extradata_size; m++) {
1175             if (!memcmp(extradata, "SEQH", 4)) {
1176                 marker_found = 1;
1177                 break;
1178             }
1179             extradata++;
1180         }
1181     }
1182
1183     /* if a match was found, parse the extra data */
1184     if (marker_found) {
1185         GetBitContext gb;
1186         int frame_size_code;
1187         int unk0, unk1, unk2, unk3, unk4;
1188         int w,h;
1189
1190         size = AV_RB32(&extradata[4]);
1191         if (size > extradata_end - extradata - 8) {
1192             ret = AVERROR_INVALIDDATA;
1193             goto fail;
1194         }
1195         init_get_bits(&gb, extradata + 8, size * 8);
1196
1197         /* 'frame size code' and optional 'width, height' */
1198         frame_size_code = get_bits(&gb, 3);
1199         switch (frame_size_code) {
1200         case 0:
1201             w = 160;
1202             h = 120;
1203             break;
1204         case 1:
1205             w = 128;
1206             h =  96;
1207             break;
1208         case 2:
1209             w = 176;
1210             h = 144;
1211             break;
1212         case 3:
1213             w = 352;
1214             h = 288;
1215             break;
1216         case 4:
1217             w = 704;
1218             h = 576;
1219             break;
1220         case 5:
1221             w = 240;
1222             h = 180;
1223             break;
1224         case 6:
1225             w = 320;
1226             h = 240;
1227             break;
1228         case 7:
1229             w = get_bits(&gb, 12);
1230             h = get_bits(&gb, 12);
1231             break;
1232         }
1233         ret = ff_set_dimensions(avctx, w, h);
1234         if (ret < 0)
1235             goto fail;
1236
1237         s->halfpel_flag  = get_bits1(&gb);
1238         s->thirdpel_flag = get_bits1(&gb);
1239
1240         /* unknown fields */
1241         unk0 = get_bits1(&gb);
1242         unk1 = get_bits1(&gb);
1243         unk2 = get_bits1(&gb);
1244         unk3 = get_bits1(&gb);
1245
1246         s->low_delay = get_bits1(&gb);
1247
1248         /* unknown field */
1249         unk4 = get_bits1(&gb);
1250
1251         av_log(avctx, AV_LOG_DEBUG, "Unknown fields %d %d %d %d %d\n",
1252                unk0, unk1, unk2, unk3, unk4);
1253
1254         if (skip_1stop_8data_bits(&gb) < 0) {
1255             ret = AVERROR_INVALIDDATA;
1256             goto fail;
1257         }
1258
1259         s->has_watermark  = get_bits1(&gb);
1260         avctx->has_b_frames = !s->low_delay;
1261         if (s->has_watermark) {
1262 #if CONFIG_ZLIB
1263             unsigned watermark_width  = get_interleaved_ue_golomb(&gb);
1264             unsigned watermark_height = get_interleaved_ue_golomb(&gb);
1265             int u1                    = get_interleaved_ue_golomb(&gb);
1266             int u2                    = get_bits(&gb, 8);
1267             int u3                    = get_bits(&gb, 2);
1268             int u4                    = get_interleaved_ue_golomb(&gb);
1269             unsigned long buf_len     = watermark_width *
1270                                         watermark_height * 4;
1271             int offset                = get_bits_count(&gb) + 7 >> 3;
1272             uint8_t *buf;
1273
1274             if (watermark_height <= 0 ||
1275                 (uint64_t)watermark_width * 4 > UINT_MAX / watermark_height) {
1276                 ret = -1;
1277                 goto fail;
1278             }
1279
1280             buf = av_malloc(buf_len);
1281             if (!buf) {
1282                 ret = AVERROR(ENOMEM);
1283                 goto fail;
1284             }
1285             av_log(avctx, AV_LOG_DEBUG, "watermark size: %ux%u\n",
1286                    watermark_width, watermark_height);
1287             av_log(avctx, AV_LOG_DEBUG,
1288                    "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n",
1289                    u1, u2, u3, u4, offset);
1290             if (uncompress(buf, &buf_len, extradata + 8 + offset,
1291                            size - offset) != Z_OK) {
1292                 av_log(avctx, AV_LOG_ERROR,
1293                        "could not uncompress watermark logo\n");
1294                 av_free(buf);
1295                 ret = -1;
1296                 goto fail;
1297             }
1298             s->watermark_key = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_CCITT), 0, buf, buf_len));
1299
1300             s->watermark_key = s->watermark_key << 16 | s->watermark_key;
1301             av_log(avctx, AV_LOG_DEBUG,
1302                    "watermark key %#"PRIx32"\n", s->watermark_key);
1303             av_free(buf);
1304 #else
1305             av_log(avctx, AV_LOG_ERROR,
1306                    "this svq3 file contains watermark which need zlib support compiled in\n");
1307             ret = -1;
1308             goto fail;
1309 #endif
1310         }
1311     }
1312
1313     s->mb_width   = (avctx->width + 15) / 16;
1314     s->mb_height  = (avctx->height + 15) / 16;
1315     s->mb_stride  = s->mb_width + 1;
1316     s->mb_num     = s->mb_width * s->mb_height;
1317     s->b_stride   = 4 * s->mb_width;
1318     s->h_edge_pos = s->mb_width * 16;
1319     s->v_edge_pos = s->mb_height * 16;
1320
1321     s->intra4x4_pred_mode = av_mallocz(s->mb_stride * 2 * 8);
1322     if (!s->intra4x4_pred_mode)
1323         return AVERROR(ENOMEM);
1324
1325     s->mb2br_xy = av_mallocz(s->mb_stride * (s->mb_height + 1) *
1326                              sizeof(*s->mb2br_xy));
1327     if (!s->mb2br_xy)
1328         return AVERROR(ENOMEM);
1329
1330     for (y = 0; y < s->mb_height; y++)
1331         for (x = 0; x < s->mb_width; x++) {
1332             const int mb_xy = x + y * s->mb_stride;
1333
1334             s->mb2br_xy[mb_xy] = 8 * (mb_xy % (2 * s->mb_stride));
1335         }
1336
1337     init_dequant4_coeff_table(s);
1338
1339     return 0;
1340 fail:
1341     svq3_decode_end(avctx);
1342     return ret;
1343 }
1344
1345 static void free_picture(AVCodecContext *avctx, SVQ3Frame *pic)
1346 {
1347     int i;
1348     for (i = 0; i < 2; i++) {
1349         av_buffer_unref(&pic->motion_val_buf[i]);
1350         av_buffer_unref(&pic->ref_index_buf[i]);
1351     }
1352     av_buffer_unref(&pic->mb_type_buf);
1353
1354     av_frame_unref(pic->f);
1355 }
1356
1357 static int get_buffer(AVCodecContext *avctx, SVQ3Frame *pic)
1358 {
1359     SVQ3Context *s = avctx->priv_data;
1360     const int big_mb_num    = s->mb_stride * (s->mb_height + 1) + 1;
1361     const int mb_array_size = s->mb_stride * s->mb_height;
1362     const int b4_stride     = s->mb_width * 4 + 1;
1363     const int b4_array_size = b4_stride * s->mb_height * 4;
1364     int ret;
1365
1366     if (!pic->motion_val_buf[0]) {
1367         int i;
1368
1369         pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) * sizeof(uint32_t));
1370         if (!pic->mb_type_buf)
1371             return AVERROR(ENOMEM);
1372         pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
1373
1374         for (i = 0; i < 2; i++) {
1375             pic->motion_val_buf[i] = av_buffer_allocz(2 * (b4_array_size + 4) * sizeof(int16_t));
1376             pic->ref_index_buf[i]  = av_buffer_allocz(4 * mb_array_size);
1377             if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i]) {
1378                 ret = AVERROR(ENOMEM);
1379                 goto fail;
1380             }
1381
1382             pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
1383             pic->ref_index[i]  = pic->ref_index_buf[i]->data;
1384         }
1385     }
1386
1387     ret = ff_get_buffer(avctx, pic->f,
1388                         (s->pict_type != AV_PICTURE_TYPE_B) ?
1389                          AV_GET_BUFFER_FLAG_REF : 0);
1390     if (ret < 0)
1391         goto fail;
1392
1393     if (!s->edge_emu_buffer) {
1394         s->edge_emu_buffer = av_mallocz_array(pic->f->linesize[0], 17);
1395         if (!s->edge_emu_buffer)
1396             return AVERROR(ENOMEM);
1397     }
1398
1399     return 0;
1400 fail:
1401     free_picture(avctx, pic);
1402     return ret;
1403 }
1404
1405 static int svq3_decode_frame(AVCodecContext *avctx, void *data,
1406                              int *got_frame, AVPacket *avpkt)
1407 {
1408     SVQ3Context *s     = avctx->priv_data;
1409     int buf_size       = avpkt->size;
1410     int left;
1411     uint8_t *buf;
1412     int ret, m, i;
1413
1414     /* special case for last picture */
1415     if (buf_size == 0) {
1416         if (s->next_pic->f->data[0] && !s->low_delay && !s->last_frame_output) {
1417             ret = av_frame_ref(data, s->next_pic->f);
1418             if (ret < 0)
1419                 return ret;
1420             s->last_frame_output = 1;
1421             *got_frame          = 1;
1422         }
1423         return 0;
1424     }
1425
1426     s->mb_x = s->mb_y = s->mb_xy = 0;
1427
1428     if (s->watermark_key) {
1429         av_fast_padded_malloc(&s->buf, &s->buf_size, buf_size);
1430         if (!s->buf)
1431             return AVERROR(ENOMEM);
1432         memcpy(s->buf, avpkt->data, buf_size);
1433         buf = s->buf;
1434     } else {
1435         buf = avpkt->data;
1436     }
1437
1438     ret = init_get_bits(&s->gb, buf, 8 * buf_size);
1439     if (ret < 0)
1440         return ret;
1441
1442     if (svq3_decode_slice_header(avctx))
1443         return -1;
1444
1445     s->pict_type = s->slice_type;
1446
1447     if (s->pict_type != AV_PICTURE_TYPE_B)
1448         FFSWAP(SVQ3Frame*, s->next_pic, s->last_pic);
1449
1450     av_frame_unref(s->cur_pic->f);
1451
1452     /* for skipping the frame */
1453     s->cur_pic->f->pict_type = s->pict_type;
1454     s->cur_pic->f->key_frame = (s->pict_type == AV_PICTURE_TYPE_I);
1455
1456     ret = get_buffer(avctx, s->cur_pic);
1457     if (ret < 0)
1458         return ret;
1459
1460     for (i = 0; i < 16; i++) {
1461         s->block_offset[i]           = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * s->cur_pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
1462         s->block_offset[48 + i]      = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * s->cur_pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
1463     }
1464     for (i = 0; i < 16; i++) {
1465         s->block_offset[16 + i]      =
1466         s->block_offset[32 + i]      = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * s->cur_pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
1467         s->block_offset[48 + 16 + i] =
1468         s->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * s->cur_pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
1469     }
1470
1471     if (s->pict_type != AV_PICTURE_TYPE_I) {
1472         if (!s->last_pic->f->data[0]) {
1473             av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1474             av_frame_unref(s->last_pic->f);
1475             ret = get_buffer(avctx, s->last_pic);
1476             if (ret < 0)
1477                 return ret;
1478             memset(s->last_pic->f->data[0], 0, avctx->height * s->last_pic->f->linesize[0]);
1479             memset(s->last_pic->f->data[1], 0x80, (avctx->height / 2) *
1480                    s->last_pic->f->linesize[1]);
1481             memset(s->last_pic->f->data[2], 0x80, (avctx->height / 2) *
1482                    s->last_pic->f->linesize[2]);
1483         }
1484
1485         if (s->pict_type == AV_PICTURE_TYPE_B && !s->next_pic->f->data[0]) {
1486             av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
1487             av_frame_unref(s->next_pic->f);
1488             ret = get_buffer(avctx, s->next_pic);
1489             if (ret < 0)
1490                 return ret;
1491             memset(s->next_pic->f->data[0], 0, avctx->height * s->next_pic->f->linesize[0]);
1492             memset(s->next_pic->f->data[1], 0x80, (avctx->height / 2) *
1493                    s->next_pic->f->linesize[1]);
1494             memset(s->next_pic->f->data[2], 0x80, (avctx->height / 2) *
1495                    s->next_pic->f->linesize[2]);
1496         }
1497     }
1498
1499     if (avctx->debug & FF_DEBUG_PICT_INFO)
1500         av_log(s->avctx, AV_LOG_DEBUG,
1501                "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
1502                av_get_picture_type_char(s->pict_type),
1503                s->halfpel_flag, s->thirdpel_flag,
1504                s->adaptive_quant, s->qscale, s->slice_num);
1505
1506     if (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B ||
1507         avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I ||
1508         avctx->skip_frame >= AVDISCARD_ALL)
1509         return 0;
1510
1511     if (s->next_p_frame_damaged) {
1512         if (s->pict_type == AV_PICTURE_TYPE_B)
1513             return 0;
1514         else
1515             s->next_p_frame_damaged = 0;
1516     }
1517
1518     if (s->pict_type == AV_PICTURE_TYPE_B) {
1519         s->frame_num_offset = s->slice_num - s->prev_frame_num;
1520
1521         if (s->frame_num_offset < 0)
1522             s->frame_num_offset += 256;
1523         if (s->frame_num_offset == 0 ||
1524             s->frame_num_offset >= s->prev_frame_num_offset) {
1525             av_log(s->avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
1526             return -1;
1527         }
1528     } else {
1529         s->prev_frame_num        = s->frame_num;
1530         s->frame_num             = s->slice_num;
1531         s->prev_frame_num_offset = s->frame_num - s->prev_frame_num;
1532
1533         if (s->prev_frame_num_offset < 0)
1534             s->prev_frame_num_offset += 256;
1535     }
1536
1537     for (m = 0; m < 2; m++) {
1538         int i;
1539         for (i = 0; i < 4; i++) {
1540             int j;
1541             for (j = -1; j < 4; j++)
1542                 s->ref_cache[m][scan8[0] + 8 * i + j] = 1;
1543             if (i < 3)
1544                 s->ref_cache[m][scan8[0] + 8 * i + j] = PART_NOT_AVAILABLE;
1545         }
1546     }
1547
1548     for (s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
1549         for (s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
1550             unsigned mb_type;
1551             s->mb_xy = s->mb_x + s->mb_y * s->mb_stride;
1552
1553             if ((get_bits_left(&s->gb_slice)) <= 7) {
1554                 if (((get_bits_count(&s->gb_slice) & 7) == 0 ||
1555                     show_bits(&s->gb_slice, get_bits_left(&s->gb_slice) & 7) == 0)) {
1556
1557                     if (svq3_decode_slice_header(avctx))
1558                         return -1;
1559                 }
1560                 if (s->slice_type != s->pict_type) {
1561                     avpriv_request_sample(avctx, "non constant slice type");
1562                 }
1563                 /* TODO: support s->mb_skip_run */
1564             }
1565
1566             mb_type = get_interleaved_ue_golomb(&s->gb_slice);
1567
1568             if (s->pict_type == AV_PICTURE_TYPE_I)
1569                 mb_type += 8;
1570             else if (s->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4)
1571                 mb_type += 4;
1572             if (mb_type > 33 || svq3_decode_mb(s, mb_type)) {
1573                 av_log(s->avctx, AV_LOG_ERROR,
1574                        "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
1575                 return -1;
1576             }
1577
1578             if (mb_type != 0 || s->cbp)
1579                 hl_decode_mb(s);
1580
1581             if (s->pict_type != AV_PICTURE_TYPE_B && !s->low_delay)
1582                 s->cur_pic->mb_type[s->mb_x + s->mb_y * s->mb_stride] =
1583                     (s->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
1584         }
1585
1586         ff_draw_horiz_band(avctx, s->cur_pic->f,
1587                            s->last_pic->f->data[0] ? s->last_pic->f : NULL,
1588                            16 * s->mb_y, 16, PICT_FRAME, 0,
1589                            s->low_delay);
1590     }
1591
1592     left = buf_size*8 - get_bits_count(&s->gb_slice);
1593
1594     if (s->mb_y != s->mb_height || s->mb_x != s->mb_width) {
1595         av_log(avctx, AV_LOG_INFO, "frame num %d incomplete pic x %d y %d left %d\n", avctx->frame_number, s->mb_y, s->mb_x, left);
1596         //av_hex_dump(stderr, buf+buf_size-8, 8);
1597     }
1598
1599     if (left < 0) {
1600         av_log(avctx, AV_LOG_ERROR, "frame num %d left %d\n", avctx->frame_number, left);
1601         return -1;
1602     }
1603
1604     if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay)
1605         ret = av_frame_ref(data, s->cur_pic->f);
1606     else if (s->last_pic->f->data[0])
1607         ret = av_frame_ref(data, s->last_pic->f);
1608     if (ret < 0)
1609         return ret;
1610
1611     /* Do not output the last pic after seeking. */
1612     if (s->last_pic->f->data[0] || s->low_delay)
1613         *got_frame = 1;
1614
1615     if (s->pict_type != AV_PICTURE_TYPE_B) {
1616         FFSWAP(SVQ3Frame*, s->cur_pic, s->next_pic);
1617     } else {
1618         av_frame_unref(s->cur_pic->f);
1619     }
1620
1621     return buf_size;
1622 }
1623
1624 static av_cold int svq3_decode_end(AVCodecContext *avctx)
1625 {
1626     SVQ3Context *s = avctx->priv_data;
1627
1628     free_picture(avctx, s->cur_pic);
1629     free_picture(avctx, s->next_pic);
1630     free_picture(avctx, s->last_pic);
1631     av_frame_free(&s->cur_pic->f);
1632     av_frame_free(&s->next_pic->f);
1633     av_frame_free(&s->last_pic->f);
1634     av_freep(&s->cur_pic);
1635     av_freep(&s->next_pic);
1636     av_freep(&s->last_pic);
1637     av_freep(&s->slice_buf);
1638     av_freep(&s->intra4x4_pred_mode);
1639     av_freep(&s->edge_emu_buffer);
1640     av_freep(&s->mb2br_xy);
1641
1642
1643     av_freep(&s->buf);
1644     s->buf_size = 0;
1645
1646     return 0;
1647 }
1648
1649 AVCodec ff_svq3_decoder = {
1650     .name           = "svq3",
1651     .long_name      = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
1652     .type           = AVMEDIA_TYPE_VIDEO,
1653     .id             = AV_CODEC_ID_SVQ3,
1654     .priv_data_size = sizeof(SVQ3Context),
1655     .init           = svq3_decode_init,
1656     .close          = svq3_decode_end,
1657     .decode         = svq3_decode_frame,
1658     .capabilities   = AV_CODEC_CAP_DRAW_HORIZ_BAND |
1659                       AV_CODEC_CAP_DR1             |
1660                       AV_CODEC_CAP_DELAY,
1661     .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,
1662                                                      AV_PIX_FMT_NONE},
1663 };