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