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