]> git.sesse.net Git - ffmpeg/blob - libavcodec/diracdec.c
Merge commit '688417399c69aadd4c287bdb0dec82ef8799011c'
[ffmpeg] / libavcodec / diracdec.c
1 /*
2  * Copyright (C) 2007 Marco Gerards <marco@gnu.org>
3  * Copyright (C) 2009 David Conrad
4  * Copyright (C) 2011 Jordi Ortiz
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * Dirac Decoder
26  * @author Marco Gerards <marco@gnu.org>, David Conrad, Jordi Ortiz <nenjordi@gmail.com>
27  */
28
29 #include "avcodec.h"
30 #include "get_bits.h"
31 #include "bytestream.h"
32 #include "internal.h"
33 #include "golomb.h"
34 #include "dirac_arith.h"
35 #include "mpeg12data.h"
36 #include "libavcodec/mpegvideo.h"
37 #include "mpegvideoencdsp.h"
38 #include "dirac_dwt.h"
39 #include "dirac.h"
40 #include "diracdsp.h"
41 #include "videodsp.h"
42
43 /**
44  * The spec limits the number of wavelet decompositions to 4 for both
45  * level 1 (VC-2) and 128 (long-gop default).
46  * 5 decompositions is the maximum before >16-bit buffers are needed.
47  * Schroedinger allows this for DD 9,7 and 13,7 wavelets only, limiting
48  * the others to 4 decompositions (or 3 for the fidelity filter).
49  *
50  * We use this instead of MAX_DECOMPOSITIONS to save some memory.
51  */
52 #define MAX_DWT_LEVELS 5
53
54 /**
55  * The spec limits this to 3 for frame coding, but in practice can be as high as 6
56  */
57 #define MAX_REFERENCE_FRAMES 8
58 #define MAX_DELAY 5         /* limit for main profile for frame coding (TODO: field coding) */
59 #define MAX_FRAMES (MAX_REFERENCE_FRAMES + MAX_DELAY + 1)
60 #define MAX_QUANT 255        /* max quant for VC-2 */
61 #define MAX_BLOCKSIZE 32    /* maximum xblen/yblen we support */
62
63 /**
64  * DiracBlock->ref flags, if set then the block does MC from the given ref
65  */
66 #define DIRAC_REF_MASK_REF1   1
67 #define DIRAC_REF_MASK_REF2   2
68 #define DIRAC_REF_MASK_GLOBAL 4
69
70 /**
71  * Value of Picture.reference when Picture is not a reference picture, but
72  * is held for delayed output.
73  */
74 #define DELAYED_PIC_REF 4
75
76 #define CALC_PADDING(size, depth)                       \
77     (((size + (1 << depth) - 1) >> depth) << depth)
78
79 #define DIVRNDUP(a, b) (((a) + (b) - 1) / (b))
80
81 typedef struct {
82     AVFrame *avframe;
83     int interpolated[3];    /* 1 if hpel[] is valid */
84     uint8_t *hpel[3][4];
85     uint8_t *hpel_base[3][4];
86     int reference;
87 } DiracFrame;
88
89 typedef struct {
90     union {
91         int16_t mv[2][2];
92         int16_t dc[3];
93     } u; /* anonymous unions aren't in C99 :( */
94     uint8_t ref;
95 } DiracBlock;
96
97 typedef struct SubBand {
98     int level;
99     int orientation;
100     int stride; /* in bytes */
101     int width;
102     int height;
103     int pshift;
104     int quant;
105     uint8_t *ibuf;
106     struct SubBand *parent;
107
108     /* for low delay */
109     unsigned length;
110     const uint8_t *coeff_data;
111 } SubBand;
112
113 typedef struct Plane {
114     int width;
115     int height;
116     ptrdiff_t stride;
117
118     int idwt_width;
119     int idwt_height;
120     int idwt_stride;
121     uint8_t *idwt_buf;
122     uint8_t *idwt_buf_base;
123     uint8_t *idwt_tmp;
124
125     /* block length */
126     uint8_t xblen;
127     uint8_t yblen;
128     /* block separation (block n+1 starts after this many pixels in block n) */
129     uint8_t xbsep;
130     uint8_t ybsep;
131     /* amount of overspill on each edge (half of the overlap between blocks) */
132     uint8_t xoffset;
133     uint8_t yoffset;
134
135     SubBand band[MAX_DWT_LEVELS][4];
136 } Plane;
137
138 typedef struct DiracContext {
139     AVCodecContext *avctx;
140     MpegvideoEncDSPContext mpvencdsp;
141     VideoDSPContext vdsp;
142     DiracDSPContext diracdsp;
143     DiracVersionInfo version;
144     GetBitContext gb;
145     dirac_source_params source;
146     int seen_sequence_header;
147     int frame_number;           /* number of the next frame to display       */
148     Plane plane[3];
149     int chroma_x_shift;
150     int chroma_y_shift;
151
152     int bit_depth;              /* bit depth                                 */
153     int pshift;                 /* pixel shift = bit_depth > 8               */
154
155     int zero_res;               /* zero residue flag                         */
156     int is_arith;               /* whether coeffs use arith or golomb coding */
157     int core_syntax;            /* use core syntax only                      */
158     int low_delay;              /* use the low delay syntax                  */
159     int hq_picture;             /* high quality picture, enables low_delay   */
160     int ld_picture;             /* use low delay picture, turns on low_delay */
161     int dc_prediction;          /* has dc prediction                         */
162     int globalmc_flag;          /* use global motion compensation            */
163     int num_refs;               /* number of reference pictures              */
164
165     /* wavelet decoding */
166     unsigned wavelet_depth;     /* depth of the IDWT                         */
167     unsigned wavelet_idx;
168
169     /**
170      * schroedinger older than 1.0.8 doesn't store
171      * quant delta if only one codebook exists in a band
172      */
173     unsigned old_delta_quant;
174     unsigned codeblock_mode;
175
176     unsigned num_x;              /* number of horizontal slices               */
177     unsigned num_y;              /* number of vertical slices                 */
178
179     struct {
180         unsigned width;
181         unsigned height;
182     } codeblock[MAX_DWT_LEVELS+1];
183
184     struct {
185         AVRational bytes;       /* average bytes per slice                   */
186         uint8_t quant[MAX_DWT_LEVELS][4]; /* [DIRAC_STD] E.1 */
187     } lowdelay;
188
189     struct {
190         int pan_tilt[2];        /* pan/tilt vector                           */
191         int zrs[2][2];          /* zoom/rotate/shear matrix                  */
192         int perspective[2];     /* perspective vector                        */
193         unsigned zrs_exp;
194         unsigned perspective_exp;
195     } globalmc[2];
196
197     /* motion compensation */
198     uint8_t mv_precision;       /* [DIRAC_STD] REFS_WT_PRECISION             */
199     int16_t weight[2];          /* [DIRAC_STD] REF1_WT and REF2_WT           */
200     unsigned weight_log2denom;  /* [DIRAC_STD] REFS_WT_PRECISION             */
201
202     int blwidth;                /* number of blocks (horizontally)           */
203     int blheight;               /* number of blocks (vertically)             */
204     int sbwidth;                /* number of superblocks (horizontally)      */
205     int sbheight;               /* number of superblocks (vertically)        */
206
207     uint8_t *sbsplit;
208     DiracBlock *blmotion;
209
210     uint8_t *edge_emu_buffer[4];
211     uint8_t *edge_emu_buffer_base;
212
213     uint16_t *mctmp;            /* buffer holding the MC data multiplied by OBMC weights */
214     uint8_t *mcscratch;
215     int buffer_stride;
216
217     DECLARE_ALIGNED(16, uint8_t, obmc_weight)[3][MAX_BLOCKSIZE*MAX_BLOCKSIZE];
218
219     void (*put_pixels_tab[4])(uint8_t *dst, const uint8_t *src[5], int stride, int h);
220     void (*avg_pixels_tab[4])(uint8_t *dst, const uint8_t *src[5], int stride, int h);
221     void (*add_obmc)(uint16_t *dst, const uint8_t *src, int stride, const uint8_t *obmc_weight, int yblen);
222     dirac_weight_func weight_func;
223     dirac_biweight_func biweight_func;
224
225     DiracFrame *current_picture;
226     DiracFrame *ref_pics[2];
227
228     DiracFrame *ref_frames[MAX_REFERENCE_FRAMES+1];
229     DiracFrame *delay_frames[MAX_DELAY+1];
230     DiracFrame all_frames[MAX_FRAMES];
231 } DiracContext;
232
233 enum dirac_subband {
234     subband_ll = 0,
235     subband_hl = 1,
236     subband_lh = 2,
237     subband_hh = 3,
238     subband_nb,
239 };
240
241 static const uint8_t default_qmat[][4][4] = {
242     { { 5,  3,  3,  0}, { 0,  4,  4,  1}, { 0,  5,  5,  2}, { 0,  6,  6,  3} },
243     { { 4,  2,  2,  0}, { 0,  4,  4,  2}, { 0,  5,  5,  3}, { 0,  7,  7,  5} },
244     { { 5,  3,  3,  0}, { 0,  4,  4,  1}, { 0,  5,  5,  2}, { 0,  6,  6,  3} },
245     { { 8,  4,  4,  0}, { 0,  4,  4,  0}, { 0,  4,  4,  0}, { 0,  4,  4,  0} },
246     { { 8,  4,  4,  0}, { 0,  4,  4,  0}, { 0,  4,  4,  0}, { 0,  4,  4,  0} },
247     { { 0,  4,  4,  8}, { 0,  8,  8, 12}, { 0, 13, 13, 17}, { 0, 17, 17, 21} },
248     { { 3,  1,  1,  0}, { 0,  4,  4,  2}, { 0,  6,  6,  5}, { 0,  9,  9,  7} },
249 };
250
251 static const int32_t qscale_tab[128] = {
252         4,         5,         6,         7,          8,        10,        11,        13,
253        16,        19,        23,        27,         32,        38,        45,        54,
254        64,        76,        91,       108,        128,       152,       181,       215,
255       256,       304,       362,       431,        512,       609,       724,       861,
256      1024,      1218,      1448,      1722,       2048,      2435,      2896,      3444,
257      4096,      4871,      5793,      6889,       8192,      9742,     11585,     13777,
258     16384,     19484,    -13317,     27554,      32768,     -1581,      9853,    -10518,
259     65536,     -3164,    -16782,    -21037,     131072,     -6328,      2922,     23552,
260    262144,    -12658,      5844,    -18524,     524288,     15232,     11689,     28578,
261   1048576,    -10085,    -13110,     -8471,    2097152,    -20170,     10267,    -16943,
262   4194304,       208,    -15954,     31741,    8388608,       416,      4579,     -2146,
263  16777216,       832,      9158,     -4293,   33554432,      1663,    -18172,     -8587,
264  67108864,      3326,       143,    -17175,   134217728,     6653,       285,     31276,
265 268435456,     13306,       570,     -3075,   536870912,   -13938,      1140,     -6152,
266 1073741824,    12672,      2281,    -12304, -2147483648,   -15205,      4561,    -24610,
267          0,    10138,      9122,     16407,           0,   -20274,    -18243,    -32813,
268 };
269
270 static const int32_t qoffset_intra_tab[128] = {
271         1,         2,         3,         4,         4,         5,         6,         7,
272         8,        10,        12,        14,          16,        19,        23,        27,
273        32,        38,        46,        54,          64,        76,        91,       108,
274       128,       152,       181,       216,         256,       305,       362,       431,
275       512,       609,       724,       861,        1024,      1218,      1448,      1722,
276      2048,      2436,      2897,      3445,        4096,      4871,      5793,      6889,
277      8192,      9742,     -6658,     13777,       16384,      -790,      4927,     -5258,
278     32768,     -1581,     -8390,    -10518,       65536,     -3163,      1461,     11776,
279    131072,     -6328,      2922,     -9261,      262144,      7616,      5845,     14289,
280    524288,     -5042,     -6554,     -4235,     1048576,    -10084,      5134,     -8471,
281   2097152,       104,     -7976,     15871,     4194304,       208,      2290,     -1072,
282   8388608,       416,      4579,     -2146,    16777216,       832,     -9085,     -4293,
283  33554432,      1663,        72,     -8587,    67108864,      3327,       143,     15638,
284 134217728,      6653,       285,     -1537,   268435456,     -6968,       570,     -3075,
285 536870912,      6336,      1141,     -6151, -1073741823,     -7602,      2281,    -12304,
286         0,      5069,      4561,      8204,           0,    -10136,     -9121,    -16406,
287 };
288
289 static const int qoffset_inter_tab[MAX_QUANT+1] = {
290     1,     2,     2,     3,     3,     4,     4,     5,
291     6,     7,     9,    10,    12,    14,    17,    20,
292     24,    29,    34,    41,    48,    57,    68,    81,
293     96,   114,   136,   162,   192,   228,   272,   323,
294     384,   457,   543,   646,   768,   913,  1086,  1292,
295     1536,  1827,  2172,  2583,  3072,  3653,  4344,  5166,
296     6144,  7307,  8689, 10333, 12288, 14613, 17378, 20666,
297     24576, 29226
298 };
299
300 /* magic number division by 3 from schroedinger */
301 static inline int divide3(int x)
302 {
303     return ((x+1)*21845 + 10922) >> 16;
304 }
305
306 static DiracFrame *remove_frame(DiracFrame *framelist[], int picnum)
307 {
308     DiracFrame *remove_pic = NULL;
309     int i, remove_idx = -1;
310
311     for (i = 0; framelist[i]; i++)
312         if (framelist[i]->avframe->display_picture_number == picnum) {
313             remove_pic = framelist[i];
314             remove_idx = i;
315         }
316
317     if (remove_pic)
318         for (i = remove_idx; framelist[i]; i++)
319             framelist[i] = framelist[i+1];
320
321     return remove_pic;
322 }
323
324 static int add_frame(DiracFrame *framelist[], int maxframes, DiracFrame *frame)
325 {
326     int i;
327     for (i = 0; i < maxframes; i++)
328         if (!framelist[i]) {
329             framelist[i] = frame;
330             return 0;
331         }
332     return -1;
333 }
334
335 static int alloc_sequence_buffers(DiracContext *s)
336 {
337     int sbwidth  = DIVRNDUP(s->source.width,  4);
338     int sbheight = DIVRNDUP(s->source.height, 4);
339     int i, w, h, top_padding;
340
341     /* todo: think more about this / use or set Plane here */
342     for (i = 0; i < 3; i++) {
343         int max_xblen = MAX_BLOCKSIZE >> (i ? s->chroma_x_shift : 0);
344         int max_yblen = MAX_BLOCKSIZE >> (i ? s->chroma_y_shift : 0);
345         w = s->source.width  >> (i ? s->chroma_x_shift : 0);
346         h = s->source.height >> (i ? s->chroma_y_shift : 0);
347
348         /* we allocate the max we support here since num decompositions can
349          * change from frame to frame. Stride is aligned to 16 for SIMD, and
350          * 1<<MAX_DWT_LEVELS top padding to avoid if(y>0) in arith decoding
351          * MAX_BLOCKSIZE padding for MC: blocks can spill up to half of that
352          * on each side */
353         top_padding = FFMAX(1<<MAX_DWT_LEVELS, max_yblen/2);
354         w = FFALIGN(CALC_PADDING(w, MAX_DWT_LEVELS), 8); /* FIXME: Should this be 16 for SSE??? */
355         h = top_padding + CALC_PADDING(h, MAX_DWT_LEVELS) + max_yblen/2;
356
357         s->plane[i].idwt_buf_base = av_mallocz_array((w+max_xblen), h * (2 << s->pshift));
358         s->plane[i].idwt_tmp      = av_malloc_array((w+16), 2 << s->pshift);
359         s->plane[i].idwt_buf      = s->plane[i].idwt_buf_base + (top_padding*w)*(2 << s->pshift);
360         if (!s->plane[i].idwt_buf_base || !s->plane[i].idwt_tmp)
361             return AVERROR(ENOMEM);
362     }
363
364     /* fixme: allocate using real stride here */
365     s->sbsplit  = av_malloc_array(sbwidth, sbheight);
366     s->blmotion = av_malloc_array(sbwidth, sbheight * 16 * sizeof(*s->blmotion));
367
368     if (!s->sbsplit || !s->blmotion)
369         return AVERROR(ENOMEM);
370     return 0;
371 }
372
373 static int alloc_buffers(DiracContext *s, int stride)
374 {
375     int w = s->source.width;
376     int h = s->source.height;
377
378     av_assert0(stride >= w);
379     stride += 64;
380
381     if (s->buffer_stride >= stride)
382         return 0;
383     s->buffer_stride = 0;
384
385     av_freep(&s->edge_emu_buffer_base);
386     memset(s->edge_emu_buffer, 0, sizeof(s->edge_emu_buffer));
387     av_freep(&s->mctmp);
388     av_freep(&s->mcscratch);
389
390     s->edge_emu_buffer_base = av_malloc_array(stride, MAX_BLOCKSIZE);
391
392     s->mctmp     = av_malloc_array((stride+MAX_BLOCKSIZE), (h+MAX_BLOCKSIZE) * sizeof(*s->mctmp));
393     s->mcscratch = av_malloc_array(stride, MAX_BLOCKSIZE);
394
395     if (!s->edge_emu_buffer_base || !s->mctmp || !s->mcscratch)
396         return AVERROR(ENOMEM);
397
398     s->buffer_stride = stride;
399     return 0;
400 }
401
402 static void free_sequence_buffers(DiracContext *s)
403 {
404     int i, j, k;
405
406     for (i = 0; i < MAX_FRAMES; i++) {
407         if (s->all_frames[i].avframe->data[0]) {
408             av_frame_unref(s->all_frames[i].avframe);
409             memset(s->all_frames[i].interpolated, 0, sizeof(s->all_frames[i].interpolated));
410         }
411
412         for (j = 0; j < 3; j++)
413             for (k = 1; k < 4; k++)
414                 av_freep(&s->all_frames[i].hpel_base[j][k]);
415     }
416
417     memset(s->ref_frames, 0, sizeof(s->ref_frames));
418     memset(s->delay_frames, 0, sizeof(s->delay_frames));
419
420     for (i = 0; i < 3; i++) {
421         av_freep(&s->plane[i].idwt_buf_base);
422         av_freep(&s->plane[i].idwt_tmp);
423     }
424
425     s->buffer_stride = 0;
426     av_freep(&s->sbsplit);
427     av_freep(&s->blmotion);
428     av_freep(&s->edge_emu_buffer_base);
429
430     av_freep(&s->mctmp);
431     av_freep(&s->mcscratch);
432 }
433
434 static av_cold int dirac_decode_init(AVCodecContext *avctx)
435 {
436     DiracContext *s = avctx->priv_data;
437     int i;
438
439     s->avctx = avctx;
440     s->frame_number = -1;
441
442     ff_diracdsp_init(&s->diracdsp);
443     ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
444     ff_videodsp_init(&s->vdsp, 8);
445
446     for (i = 0; i < MAX_FRAMES; i++) {
447         s->all_frames[i].avframe = av_frame_alloc();
448         if (!s->all_frames[i].avframe) {
449             while (i > 0)
450                 av_frame_free(&s->all_frames[--i].avframe);
451             return AVERROR(ENOMEM);
452         }
453     }
454
455     return 0;
456 }
457
458 static void dirac_decode_flush(AVCodecContext *avctx)
459 {
460     DiracContext *s = avctx->priv_data;
461     free_sequence_buffers(s);
462     s->seen_sequence_header = 0;
463     s->frame_number = -1;
464 }
465
466 static av_cold int dirac_decode_end(AVCodecContext *avctx)
467 {
468     DiracContext *s = avctx->priv_data;
469     int i;
470
471     dirac_decode_flush(avctx);
472     for (i = 0; i < MAX_FRAMES; i++)
473         av_frame_free(&s->all_frames[i].avframe);
474
475     return 0;
476 }
477
478 #define SIGN_CTX(x) (CTX_SIGN_ZERO + ((x) > 0) - ((x) < 0))
479
480 static inline int coeff_unpack_golomb(GetBitContext *gb, int qfactor, int qoffset)
481 {
482     int sign, coeff;
483
484     coeff = svq3_get_ue_golomb(gb);
485     if (coeff) {
486         coeff = (coeff * qfactor + qoffset + 2) >> 2;
487         sign  = get_bits1(gb);
488         coeff = (coeff ^ -sign) + sign;
489     }
490     return coeff;
491 }
492
493 #define UNPACK_ARITH(n, type) \
494     static inline void coeff_unpack_arith_##n(DiracArith *c, int qfactor, int qoffset, \
495                                               SubBand *b, type *buf, int x, int y) \
496     { \
497         int coeff, sign, sign_pred = 0, pred_ctx = CTX_ZPZN_F1; \
498         const int mstride = -(b->stride >> (1+b->pshift)); \
499         if (b->parent) { \
500             const type *pbuf = (type *)b->parent->ibuf; \
501             const int stride = b->parent->stride >> (1+b->parent->pshift); \
502             pred_ctx += !!pbuf[stride * (y>>1) + (x>>1)] << 1; \
503         } \
504         if (b->orientation == subband_hl) \
505             sign_pred = buf[mstride]; \
506         if (x) { \
507             pred_ctx += !(buf[-1] | buf[mstride] | buf[-1 + mstride]); \
508             if (b->orientation == subband_lh) \
509                 sign_pred = buf[-1]; \
510         } else { \
511             pred_ctx += !buf[mstride]; \
512         } \
513         coeff = dirac_get_arith_uint(c, pred_ctx, CTX_COEFF_DATA); \
514         if (coeff) { \
515             coeff = (coeff * qfactor + qoffset + 2) >> 2; \
516             sign  = dirac_get_arith_bit(c, SIGN_CTX(sign_pred)); \
517             coeff = (coeff ^ -sign) + sign; \
518         } \
519         *buf = coeff; \
520     } \
521
522 UNPACK_ARITH(8, int16_t)
523 UNPACK_ARITH(10, int32_t)
524
525 /**
526  * Decode the coeffs in the rectangle defined by left, right, top, bottom
527  * [DIRAC_STD] 13.4.3.2 Codeblock unpacking loop. codeblock()
528  */
529 static inline void codeblock(DiracContext *s, SubBand *b,
530                              GetBitContext *gb, DiracArith *c,
531                              int left, int right, int top, int bottom,
532                              int blockcnt_one, int is_arith)
533 {
534     int x, y, zero_block;
535     int qoffset, qfactor;
536     uint8_t *buf;
537
538     /* check for any coded coefficients in this codeblock */
539     if (!blockcnt_one) {
540         if (is_arith)
541             zero_block = dirac_get_arith_bit(c, CTX_ZERO_BLOCK);
542         else
543             zero_block = get_bits1(gb);
544
545         if (zero_block)
546             return;
547     }
548
549     if (s->codeblock_mode && !(s->old_delta_quant && blockcnt_one)) {
550         int quant = b->quant;
551         if (is_arith)
552             quant += dirac_get_arith_int(c, CTX_DELTA_Q_F, CTX_DELTA_Q_DATA);
553         else
554             quant += dirac_get_se_golomb(gb);
555         if (quant < 0) {
556             av_log(s->avctx, AV_LOG_ERROR, "Invalid quant\n");
557             return;
558         }
559         b->quant = quant;
560     }
561
562     b->quant = FFMIN(b->quant, MAX_QUANT);
563
564     qfactor = qscale_tab[b->quant];
565     /* TODO: context pointer? */
566     if (!s->num_refs)
567         qoffset = qoffset_intra_tab[b->quant];
568     else
569         qoffset = qoffset_inter_tab[b->quant];
570
571     buf = b->ibuf + top * b->stride;
572     if (is_arith) {
573         for (y = top; y < bottom; y++) {
574             for (x = left; x < right; x++) {
575                 if (b->pshift) {
576                     coeff_unpack_arith_10(c, qfactor, qoffset, b, (int32_t*)(buf)+x, x, y);
577                 } else {
578                     coeff_unpack_arith_8(c, qfactor, qoffset, b, (int16_t*)(buf)+x, x, y);
579                 }
580             }
581             buf += b->stride;
582         }
583     } else {
584         for (y = top; y < bottom; y++) {
585             for (x = left; x < right; x++) {
586                 int val = coeff_unpack_golomb(gb, qfactor, qoffset);
587                 if (b->pshift) {
588                     AV_WN32(&buf[4*x], val);
589                 } else {
590                     AV_WN16(&buf[2*x], val);
591                 }
592             }
593             buf += b->stride;
594          }
595      }
596 }
597
598 /**
599  * Dirac Specification ->
600  * 13.3 intra_dc_prediction(band)
601  */
602 #define INTRA_DC_PRED(n, type) \
603     static inline void intra_dc_prediction_##n(SubBand *b) \
604     { \
605         type *buf = (type*)b->ibuf; \
606         int x, y; \
607         \
608         for (x = 1; x < b->width; x++) \
609             buf[x] += buf[x-1]; \
610         buf += (b->stride >> (1+b->pshift)); \
611         \
612         for (y = 1; y < b->height; y++) { \
613             buf[0] += buf[-(b->stride >> (1+b->pshift))]; \
614             \
615             for (x = 1; x < b->width; x++) { \
616                 int pred = buf[x - 1] + buf[x - (b->stride >> (1+b->pshift))] + buf[x - (b->stride >> (1+b->pshift))-1]; \
617                 buf[x]  += divide3(pred); \
618             } \
619             buf += (b->stride >> (1+b->pshift)); \
620         } \
621     } \
622
623 INTRA_DC_PRED(8, int16_t)
624 INTRA_DC_PRED(10, int32_t)
625
626 /**
627  * Dirac Specification ->
628  * 13.4.2 Non-skipped subbands.  subband_coeffs()
629  */
630 static av_always_inline void decode_subband_internal(DiracContext *s, SubBand *b, int is_arith)
631 {
632     int cb_x, cb_y, left, right, top, bottom;
633     DiracArith c;
634     GetBitContext gb;
635     int cb_width  = s->codeblock[b->level + (b->orientation != subband_ll)].width;
636     int cb_height = s->codeblock[b->level + (b->orientation != subband_ll)].height;
637     int blockcnt_one = (cb_width + cb_height) == 2;
638
639     if (!b->length)
640         return;
641
642     init_get_bits8(&gb, b->coeff_data, b->length);
643
644     if (is_arith)
645         ff_dirac_init_arith_decoder(&c, &gb, b->length);
646
647     top = 0;
648     for (cb_y = 0; cb_y < cb_height; cb_y++) {
649         bottom = (b->height * (cb_y+1LL)) / cb_height;
650         left = 0;
651         for (cb_x = 0; cb_x < cb_width; cb_x++) {
652             right = (b->width * (cb_x+1LL)) / cb_width;
653             codeblock(s, b, &gb, &c, left, right, top, bottom, blockcnt_one, is_arith);
654             left = right;
655         }
656         top = bottom;
657     }
658
659     if (b->orientation == subband_ll && s->num_refs == 0) {
660         if (s->pshift) {
661             intra_dc_prediction_10(b);
662         } else {
663             intra_dc_prediction_8(b);
664         }
665     }
666 }
667
668 static int decode_subband_arith(AVCodecContext *avctx, void *b)
669 {
670     DiracContext *s = avctx->priv_data;
671     decode_subband_internal(s, b, 1);
672     return 0;
673 }
674
675 static int decode_subband_golomb(AVCodecContext *avctx, void *arg)
676 {
677     DiracContext *s = avctx->priv_data;
678     SubBand **b     = arg;
679     decode_subband_internal(s, *b, 0);
680     return 0;
681 }
682
683 /**
684  * Dirac Specification ->
685  * [DIRAC_STD] 13.4.1 core_transform_data()
686  */
687 static void decode_component(DiracContext *s, int comp)
688 {
689     AVCodecContext *avctx = s->avctx;
690     SubBand *bands[3*MAX_DWT_LEVELS+1];
691     enum dirac_subband orientation;
692     int level, num_bands = 0;
693
694     /* Unpack all subbands at all levels. */
695     for (level = 0; level < s->wavelet_depth; level++) {
696         for (orientation = !!level; orientation < 4; orientation++) {
697             SubBand *b = &s->plane[comp].band[level][orientation];
698             bands[num_bands++] = b;
699
700             align_get_bits(&s->gb);
701             /* [DIRAC_STD] 13.4.2 subband() */
702             b->length = svq3_get_ue_golomb(&s->gb);
703             if (b->length) {
704                 b->quant = svq3_get_ue_golomb(&s->gb);
705                 align_get_bits(&s->gb);
706                 b->coeff_data = s->gb.buffer + get_bits_count(&s->gb)/8;
707                 b->length = FFMIN(b->length, FFMAX(get_bits_left(&s->gb)/8, 0));
708                 skip_bits_long(&s->gb, b->length*8);
709             }
710         }
711         /* arithmetic coding has inter-level dependencies, so we can only execute one level at a time */
712         if (s->is_arith)
713             avctx->execute(avctx, decode_subband_arith, &s->plane[comp].band[level][!!level],
714                            NULL, 4-!!level, sizeof(SubBand));
715     }
716     /* golomb coding has no inter-level dependencies, so we can execute all subbands in parallel */
717     if (!s->is_arith)
718         avctx->execute(avctx, decode_subband_golomb, bands, NULL, num_bands, sizeof(SubBand*));
719 }
720
721 #define PARSE_VALUES(type, x, gb, ebits, buf1, buf2) \
722     type *buf = (type *)buf1; \
723     buf[x] = coeff_unpack_golomb(gb, qfactor, qoffset); \
724     if (get_bits_count(gb) >= ebits) \
725         return; \
726     if (buf2) { \
727         buf = (type *)buf2; \
728         buf[x] = coeff_unpack_golomb(gb, qfactor, qoffset); \
729         if (get_bits_count(gb) >= ebits) \
730             return; \
731     } \
732
733 static void decode_subband(DiracContext *s, GetBitContext *gb, int quant,
734                            int slice_x, int slice_y, int bits_end,
735                            SubBand *b1, SubBand *b2)
736 {
737     int left   = b1->width  * slice_x    / s->num_x;
738     int right  = b1->width  *(slice_x+1) / s->num_x;
739     int top    = b1->height * slice_y    / s->num_y;
740     int bottom = b1->height *(slice_y+1) / s->num_y;
741
742     int qfactor = qscale_tab[quant & 0x7f];
743     int qoffset = qoffset_intra_tab[quant & 0x7f];
744
745     uint8_t *buf1 =      b1->ibuf + top * b1->stride;
746     uint8_t *buf2 = b2 ? b2->ibuf + top * b2->stride: NULL;
747     int x, y;
748     /* we have to constantly check for overread since the spec explicitly
749        requires this, with the meaning that all remaining coeffs are set to 0 */
750     if (get_bits_count(gb) >= bits_end)
751         return;
752
753     if (s->pshift) {
754         for (y = top; y < bottom; y++) {
755             for (x = left; x < right; x++) {
756                 PARSE_VALUES(int32_t, x, gb, bits_end, buf1, buf2);
757             }
758             buf1 += b1->stride;
759             if (buf2)
760                 buf2 += b2->stride;
761         }
762     }
763     else {
764         for (y = top; y < bottom; y++) {
765             for (x = left; x < right; x++) {
766                 PARSE_VALUES(int16_t, x, gb, bits_end, buf1, buf2);
767             }
768             buf1 += b1->stride;
769             if (buf2)
770                 buf2 += b2->stride;
771         }
772     }
773 }
774
775 /* Used by Low Delay and High Quality profiles */
776 typedef struct DiracSlice {
777     GetBitContext gb;
778     int slice_x;
779     int slice_y;
780     int bytes;
781 } DiracSlice;
782
783
784 /**
785  * Dirac Specification ->
786  * 13.5.2 Slices. slice(sx,sy)
787  */
788 static int decode_lowdelay_slice(AVCodecContext *avctx, void *arg)
789 {
790     DiracContext *s = avctx->priv_data;
791     DiracSlice *slice = arg;
792     GetBitContext *gb = &slice->gb;
793     enum dirac_subband orientation;
794     int level, quant, chroma_bits, chroma_end;
795
796     int quant_base  = get_bits(gb, 7); /*[DIRAC_STD] qindex */
797     int length_bits = av_log2(8 * slice->bytes)+1;
798     int luma_bits   = get_bits_long(gb, length_bits);
799     int luma_end    = get_bits_count(gb) + FFMIN(luma_bits, get_bits_left(gb));
800
801     /* [DIRAC_STD] 13.5.5.2 luma_slice_band */
802     for (level = 0; level < s->wavelet_depth; level++)
803         for (orientation = !!level; orientation < 4; orientation++) {
804             quant = FFMAX(quant_base - s->lowdelay.quant[level][orientation], 0);
805             decode_subband(s, gb, quant, slice->slice_x, slice->slice_y, luma_end,
806                            &s->plane[0].band[level][orientation], NULL);
807         }
808
809     /* consume any unused bits from luma */
810     skip_bits_long(gb, get_bits_count(gb) - luma_end);
811
812     chroma_bits = 8*slice->bytes - 7 - length_bits - luma_bits;
813     chroma_end  = get_bits_count(gb) + FFMIN(chroma_bits, get_bits_left(gb));
814     /* [DIRAC_STD] 13.5.5.3 chroma_slice_band */
815     for (level = 0; level < s->wavelet_depth; level++)
816         for (orientation = !!level; orientation < 4; orientation++) {
817             quant = FFMAX(quant_base - s->lowdelay.quant[level][orientation], 0);
818             decode_subband(s, gb, quant, slice->slice_x, slice->slice_y, chroma_end,
819                            &s->plane[1].band[level][orientation],
820                            &s->plane[2].band[level][orientation]);
821         }
822
823     return 0;
824 }
825
826 /**
827  * Dirac Specification ->
828  * 13.5.1 low_delay_transform_data()
829  */
830 static int decode_lowdelay(DiracContext *s)
831 {
832     AVCodecContext *avctx = s->avctx;
833     int slice_x, slice_y, bytes, bufsize;
834     const uint8_t *buf;
835     DiracSlice *slices;
836     int slice_num = 0;
837
838     slices = av_mallocz_array(s->num_x, s->num_y * sizeof(DiracSlice));
839     if (!slices)
840         return AVERROR(ENOMEM);
841
842     align_get_bits(&s->gb);
843     /*[DIRAC_STD] 13.5.2 Slices. slice(sx,sy) */
844     buf = s->gb.buffer + get_bits_count(&s->gb)/8;
845     bufsize = get_bits_left(&s->gb);
846
847     for (slice_y = 0; bufsize > 0 && slice_y < s->num_y; slice_y++) {
848         for (slice_x = 0; bufsize > 0 && slice_x < s->num_x; slice_x++) {
849             bytes = (slice_num+1) * s->lowdelay.bytes.num / s->lowdelay.bytes.den
850                 - slice_num    * s->lowdelay.bytes.num / s->lowdelay.bytes.den;
851             slices[slice_num].bytes   = bytes;
852             slices[slice_num].slice_x = slice_x;
853             slices[slice_num].slice_y = slice_y;
854             init_get_bits(&slices[slice_num].gb, buf, bufsize);
855             slice_num++;
856
857             buf     += bytes;
858             if (bufsize/8 >= bytes)
859                 bufsize -= bytes*8;
860             else
861                 bufsize = 0;
862         }
863     }
864
865     avctx->execute(avctx, decode_lowdelay_slice, slices, NULL, slice_num,
866                    sizeof(struct DiracSlice)); /* [DIRAC_STD] 13.5.2 Slices */
867     if (s->dc_prediction) {
868         if (s->pshift) {
869             intra_dc_prediction_10(&s->plane[0].band[0][0]); /* [DIRAC_STD] 13.3 intra_dc_prediction() */
870             intra_dc_prediction_10(&s->plane[1].band[0][0]); /* [DIRAC_STD] 13.3 intra_dc_prediction() */
871             intra_dc_prediction_10(&s->plane[2].band[0][0]); /* [DIRAC_STD] 13.3 intra_dc_prediction() */
872         } else {
873             intra_dc_prediction_8(&s->plane[0].band[0][0]);
874             intra_dc_prediction_8(&s->plane[1].band[0][0]);
875             intra_dc_prediction_8(&s->plane[2].band[0][0]);
876         }
877     }
878     av_free(slices);
879     return 0;
880 }
881
882 static void init_planes(DiracContext *s)
883 {
884     int i, w, h, level, orientation;
885
886     for (i = 0; i < 3; i++) {
887         Plane *p = &s->plane[i];
888
889         p->width       = s->source.width  >> (i ? s->chroma_x_shift : 0);
890         p->height      = s->source.height >> (i ? s->chroma_y_shift : 0);
891         p->idwt_width  = w = CALC_PADDING(p->width , s->wavelet_depth);
892         p->idwt_height = h = CALC_PADDING(p->height, s->wavelet_depth);
893         p->idwt_stride = FFALIGN(p->idwt_width << (1 + s->pshift), 8);
894
895         for (level = s->wavelet_depth-1; level >= 0; level--) {
896             w = w>>1;
897             h = h>>1;
898             for (orientation = !!level; orientation < 4; orientation++) {
899                 SubBand *b = &p->band[level][orientation];
900
901                 b->pshift = s->pshift;
902                 b->ibuf   = p->idwt_buf;
903                 b->level  = level;
904                 b->stride = p->idwt_stride << (s->wavelet_depth - level);
905                 b->width  = w;
906                 b->height = h;
907                 b->orientation = orientation;
908
909                 if (orientation & 1)
910                     b->ibuf += w << (1+b->pshift);
911                 if (orientation > 1)
912                     b->ibuf += (b->stride>>1);
913
914                 if (level)
915                     b->parent = &p->band[level-1][orientation];
916             }
917         }
918
919         if (i > 0) {
920             p->xblen = s->plane[0].xblen >> s->chroma_x_shift;
921             p->yblen = s->plane[0].yblen >> s->chroma_y_shift;
922             p->xbsep = s->plane[0].xbsep >> s->chroma_x_shift;
923             p->ybsep = s->plane[0].ybsep >> s->chroma_y_shift;
924         }
925
926         p->xoffset = (p->xblen - p->xbsep)/2;
927         p->yoffset = (p->yblen - p->ybsep)/2;
928     }
929 }
930
931 /**
932  * Unpack the motion compensation parameters
933  * Dirac Specification ->
934  * 11.2 Picture prediction data. picture_prediction()
935  */
936 static int dirac_unpack_prediction_parameters(DiracContext *s)
937 {
938     static const uint8_t default_blen[] = { 4, 12, 16, 24 };
939
940     GetBitContext *gb = &s->gb;
941     unsigned idx, ref;
942
943     align_get_bits(gb);
944     /* [DIRAC_STD] 11.2.2 Block parameters. block_parameters() */
945     /* Luma and Chroma are equal. 11.2.3 */
946     idx = svq3_get_ue_golomb(gb); /* [DIRAC_STD] index */
947
948     if (idx > 4) {
949         av_log(s->avctx, AV_LOG_ERROR, "Block prediction index too high\n");
950         return AVERROR_INVALIDDATA;
951     }
952
953     if (idx == 0) {
954         s->plane[0].xblen = svq3_get_ue_golomb(gb);
955         s->plane[0].yblen = svq3_get_ue_golomb(gb);
956         s->plane[0].xbsep = svq3_get_ue_golomb(gb);
957         s->plane[0].ybsep = svq3_get_ue_golomb(gb);
958     } else {
959         /*[DIRAC_STD] preset_block_params(index). Table 11.1 */
960         s->plane[0].xblen = default_blen[idx-1];
961         s->plane[0].yblen = default_blen[idx-1];
962         s->plane[0].xbsep = 4 * idx;
963         s->plane[0].ybsep = 4 * idx;
964     }
965     /*[DIRAC_STD] 11.2.4 motion_data_dimensions()
966       Calculated in function dirac_unpack_block_motion_data */
967
968     if (s->plane[0].xblen % (1 << s->chroma_x_shift) != 0 ||
969         s->plane[0].yblen % (1 << s->chroma_y_shift) != 0 ||
970         !s->plane[0].xblen || !s->plane[0].yblen) {
971         av_log(s->avctx, AV_LOG_ERROR,
972                "invalid x/y block length (%d/%d) for x/y chroma shift (%d/%d)\n",
973                s->plane[0].xblen, s->plane[0].yblen, s->chroma_x_shift, s->chroma_y_shift);
974         return AVERROR_INVALIDDATA;
975     }
976     if (!s->plane[0].xbsep || !s->plane[0].ybsep || s->plane[0].xbsep < s->plane[0].xblen/2 || s->plane[0].ybsep < s->plane[0].yblen/2) {
977         av_log(s->avctx, AV_LOG_ERROR, "Block separation too small\n");
978         return AVERROR_INVALIDDATA;
979     }
980     if (s->plane[0].xbsep > s->plane[0].xblen || s->plane[0].ybsep > s->plane[0].yblen) {
981         av_log(s->avctx, AV_LOG_ERROR, "Block separation greater than size\n");
982         return AVERROR_INVALIDDATA;
983     }
984     if (FFMAX(s->plane[0].xblen, s->plane[0].yblen) > MAX_BLOCKSIZE) {
985         av_log(s->avctx, AV_LOG_ERROR, "Unsupported large block size\n");
986         return AVERROR_PATCHWELCOME;
987     }
988
989     /*[DIRAC_STD] 11.2.5 Motion vector precision. motion_vector_precision()
990       Read motion vector precision */
991     s->mv_precision = svq3_get_ue_golomb(gb);
992     if (s->mv_precision > 3) {
993         av_log(s->avctx, AV_LOG_ERROR, "MV precision finer than eighth-pel\n");
994         return AVERROR_INVALIDDATA;
995     }
996
997     /*[DIRAC_STD] 11.2.6 Global motion. global_motion()
998       Read the global motion compensation parameters */
999     s->globalmc_flag = get_bits1(gb);
1000     if (s->globalmc_flag) {
1001         memset(s->globalmc, 0, sizeof(s->globalmc));
1002         /* [DIRAC_STD] pan_tilt(gparams) */
1003         for (ref = 0; ref < s->num_refs; ref++) {
1004             if (get_bits1(gb)) {
1005                 s->globalmc[ref].pan_tilt[0] = dirac_get_se_golomb(gb);
1006                 s->globalmc[ref].pan_tilt[1] = dirac_get_se_golomb(gb);
1007             }
1008             /* [DIRAC_STD] zoom_rotate_shear(gparams)
1009                zoom/rotation/shear parameters */
1010             if (get_bits1(gb)) {
1011                 s->globalmc[ref].zrs_exp   = svq3_get_ue_golomb(gb);
1012                 s->globalmc[ref].zrs[0][0] = dirac_get_se_golomb(gb);
1013                 s->globalmc[ref].zrs[0][1] = dirac_get_se_golomb(gb);
1014                 s->globalmc[ref].zrs[1][0] = dirac_get_se_golomb(gb);
1015                 s->globalmc[ref].zrs[1][1] = dirac_get_se_golomb(gb);
1016             } else {
1017                 s->globalmc[ref].zrs[0][0] = 1;
1018                 s->globalmc[ref].zrs[1][1] = 1;
1019             }
1020             /* [DIRAC_STD] perspective(gparams) */
1021             if (get_bits1(gb)) {
1022                 s->globalmc[ref].perspective_exp = svq3_get_ue_golomb(gb);
1023                 s->globalmc[ref].perspective[0]  = dirac_get_se_golomb(gb);
1024                 s->globalmc[ref].perspective[1]  = dirac_get_se_golomb(gb);
1025             }
1026         }
1027     }
1028
1029     /*[DIRAC_STD] 11.2.7 Picture prediction mode. prediction_mode()
1030       Picture prediction mode, not currently used. */
1031     if (svq3_get_ue_golomb(gb)) {
1032         av_log(s->avctx, AV_LOG_ERROR, "Unknown picture prediction mode\n");
1033         return AVERROR_INVALIDDATA;
1034     }
1035
1036     /* [DIRAC_STD] 11.2.8 Reference picture weight. reference_picture_weights()
1037        just data read, weight calculation will be done later on. */
1038     s->weight_log2denom = 1;
1039     s->weight[0]        = 1;
1040     s->weight[1]        = 1;
1041
1042     if (get_bits1(gb)) {
1043         s->weight_log2denom = svq3_get_ue_golomb(gb);
1044         s->weight[0] = dirac_get_se_golomb(gb);
1045         if (s->num_refs == 2)
1046             s->weight[1] = dirac_get_se_golomb(gb);
1047     }
1048     return 0;
1049 }
1050
1051 /**
1052  * Dirac Specification ->
1053  * 11.3 Wavelet transform data. wavelet_transform()
1054  */
1055 static int dirac_unpack_idwt_params(DiracContext *s)
1056 {
1057     GetBitContext *gb = &s->gb;
1058     int i, level;
1059     unsigned tmp;
1060
1061 #define CHECKEDREAD(dst, cond, errmsg) \
1062     tmp = svq3_get_ue_golomb(gb); \
1063     if (cond) { \
1064         av_log(s->avctx, AV_LOG_ERROR, errmsg); \
1065         return AVERROR_INVALIDDATA; \
1066     }\
1067     dst = tmp;
1068
1069     align_get_bits(gb);
1070
1071     s->zero_res = s->num_refs ? get_bits1(gb) : 0;
1072     if (s->zero_res)
1073         return 0;
1074
1075     /*[DIRAC_STD] 11.3.1 Transform parameters. transform_parameters() */
1076     CHECKEDREAD(s->wavelet_idx, tmp > 6, "wavelet_idx is too big\n")
1077
1078     CHECKEDREAD(s->wavelet_depth, tmp > MAX_DWT_LEVELS || tmp < 1, "invalid number of DWT decompositions\n")
1079
1080     if (!s->low_delay) {
1081         /* Codeblock parameters (core syntax only) */
1082         if (get_bits1(gb)) {
1083             for (i = 0; i <= s->wavelet_depth; i++) {
1084                 CHECKEDREAD(s->codeblock[i].width , tmp < 1 || tmp > (s->avctx->width >>s->wavelet_depth-i), "codeblock width invalid\n")
1085                 CHECKEDREAD(s->codeblock[i].height, tmp < 1 || tmp > (s->avctx->height>>s->wavelet_depth-i), "codeblock height invalid\n")
1086             }
1087
1088             CHECKEDREAD(s->codeblock_mode, tmp > 1, "unknown codeblock mode\n")
1089         } else
1090             for (i = 0; i <= s->wavelet_depth; i++)
1091                 s->codeblock[i].width = s->codeblock[i].height = 1;
1092     } else {
1093         /* Slice parameters + quantization matrix*/
1094         /*[DIRAC_STD] 11.3.4 Slice coding Parameters (low delay syntax only). slice_parameters() */
1095         s->num_x     = svq3_get_ue_golomb(gb);
1096         s->num_y     = svq3_get_ue_golomb(gb);
1097         s->lowdelay.bytes.num = svq3_get_ue_golomb(gb);
1098         s->lowdelay.bytes.den = svq3_get_ue_golomb(gb);
1099
1100         if (s->lowdelay.bytes.den <= 0) {
1101             av_log(s->avctx,AV_LOG_ERROR,"Invalid lowdelay.bytes.den\n");
1102             return AVERROR_INVALIDDATA;
1103         }
1104
1105         /* [DIRAC_STD] 11.3.5 Quantisation matrices (low-delay syntax). quant_matrix() */
1106         if (get_bits1(gb)) {
1107             av_log(s->avctx,AV_LOG_DEBUG,"Low Delay: Has Custom Quantization Matrix!\n");
1108             /* custom quantization matrix */
1109             s->lowdelay.quant[0][0] = svq3_get_ue_golomb(gb);
1110             for (level = 0; level < s->wavelet_depth; level++) {
1111                 s->lowdelay.quant[level][1] = svq3_get_ue_golomb(gb);
1112                 s->lowdelay.quant[level][2] = svq3_get_ue_golomb(gb);
1113                 s->lowdelay.quant[level][3] = svq3_get_ue_golomb(gb);
1114             }
1115         } else {
1116             if (s->wavelet_depth > 4) {
1117                 av_log(s->avctx,AV_LOG_ERROR,"Mandatory custom low delay matrix missing for depth %d\n", s->wavelet_depth);
1118                 return AVERROR_INVALIDDATA;
1119             }
1120             /* default quantization matrix */
1121             for (level = 0; level < s->wavelet_depth; level++)
1122                 for (i = 0; i < 4; i++) {
1123                     s->lowdelay.quant[level][i] = default_qmat[s->wavelet_idx][level][i];
1124                     /* haar with no shift differs for different depths */
1125                     if (s->wavelet_idx == 3)
1126                         s->lowdelay.quant[level][i] += 4*(s->wavelet_depth-1 - level);
1127                 }
1128         }
1129     }
1130     return 0;
1131 }
1132
1133 static inline int pred_sbsplit(uint8_t *sbsplit, int stride, int x, int y)
1134 {
1135     static const uint8_t avgsplit[7] = { 0, 0, 1, 1, 1, 2, 2 };
1136
1137     if (!(x|y))
1138         return 0;
1139     else if (!y)
1140         return sbsplit[-1];
1141     else if (!x)
1142         return sbsplit[-stride];
1143
1144     return avgsplit[sbsplit[-1] + sbsplit[-stride] + sbsplit[-stride-1]];
1145 }
1146
1147 static inline int pred_block_mode(DiracBlock *block, int stride, int x, int y, int refmask)
1148 {
1149     int pred;
1150
1151     if (!(x|y))
1152         return 0;
1153     else if (!y)
1154         return block[-1].ref & refmask;
1155     else if (!x)
1156         return block[-stride].ref & refmask;
1157
1158     /* return the majority */
1159     pred = (block[-1].ref & refmask) + (block[-stride].ref & refmask) + (block[-stride-1].ref & refmask);
1160     return (pred >> 1) & refmask;
1161 }
1162
1163 static inline void pred_block_dc(DiracBlock *block, int stride, int x, int y)
1164 {
1165     int i, n = 0;
1166
1167     memset(block->u.dc, 0, sizeof(block->u.dc));
1168
1169     if (x && !(block[-1].ref & 3)) {
1170         for (i = 0; i < 3; i++)
1171             block->u.dc[i] += block[-1].u.dc[i];
1172         n++;
1173     }
1174
1175     if (y && !(block[-stride].ref & 3)) {
1176         for (i = 0; i < 3; i++)
1177             block->u.dc[i] += block[-stride].u.dc[i];
1178         n++;
1179     }
1180
1181     if (x && y && !(block[-1-stride].ref & 3)) {
1182         for (i = 0; i < 3; i++)
1183             block->u.dc[i] += block[-1-stride].u.dc[i];
1184         n++;
1185     }
1186
1187     if (n == 2) {
1188         for (i = 0; i < 3; i++)
1189             block->u.dc[i] = (block->u.dc[i]+1)>>1;
1190     } else if (n == 3) {
1191         for (i = 0; i < 3; i++)
1192             block->u.dc[i] = divide3(block->u.dc[i]);
1193     }
1194 }
1195
1196 static inline void pred_mv(DiracBlock *block, int stride, int x, int y, int ref)
1197 {
1198     int16_t *pred[3];
1199     int refmask = ref+1;
1200     int mask = refmask | DIRAC_REF_MASK_GLOBAL; /*  exclude gmc blocks */
1201     int n = 0;
1202
1203     if (x && (block[-1].ref & mask) == refmask)
1204         pred[n++] = block[-1].u.mv[ref];
1205
1206     if (y && (block[-stride].ref & mask) == refmask)
1207         pred[n++] = block[-stride].u.mv[ref];
1208
1209     if (x && y && (block[-stride-1].ref & mask) == refmask)
1210         pred[n++] = block[-stride-1].u.mv[ref];
1211
1212     switch (n) {
1213     case 0:
1214         block->u.mv[ref][0] = 0;
1215         block->u.mv[ref][1] = 0;
1216         break;
1217     case 1:
1218         block->u.mv[ref][0] = pred[0][0];
1219         block->u.mv[ref][1] = pred[0][1];
1220         break;
1221     case 2:
1222         block->u.mv[ref][0] = (pred[0][0] + pred[1][0] + 1) >> 1;
1223         block->u.mv[ref][1] = (pred[0][1] + pred[1][1] + 1) >> 1;
1224         break;
1225     case 3:
1226         block->u.mv[ref][0] = mid_pred(pred[0][0], pred[1][0], pred[2][0]);
1227         block->u.mv[ref][1] = mid_pred(pred[0][1], pred[1][1], pred[2][1]);
1228         break;
1229     }
1230 }
1231
1232 static void global_mv(DiracContext *s, DiracBlock *block, int x, int y, int ref)
1233 {
1234     int ez      = s->globalmc[ref].zrs_exp;
1235     int ep      = s->globalmc[ref].perspective_exp;
1236     int (*A)[2] = s->globalmc[ref].zrs;
1237     int *b      = s->globalmc[ref].pan_tilt;
1238     int *c      = s->globalmc[ref].perspective;
1239
1240     int m       = (1<<ep) - (c[0]*x + c[1]*y);
1241     int mx      = m * ((A[0][0] * x + A[0][1]*y) + (1<<ez) * b[0]);
1242     int my      = m * ((A[1][0] * x + A[1][1]*y) + (1<<ez) * b[1]);
1243
1244     block->u.mv[ref][0] = (mx + (1<<(ez+ep))) >> (ez+ep);
1245     block->u.mv[ref][1] = (my + (1<<(ez+ep))) >> (ez+ep);
1246 }
1247
1248 static void decode_block_params(DiracContext *s, DiracArith arith[8], DiracBlock *block,
1249                                 int stride, int x, int y)
1250 {
1251     int i;
1252
1253     block->ref  = pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_REF1);
1254     block->ref ^= dirac_get_arith_bit(arith, CTX_PMODE_REF1);
1255
1256     if (s->num_refs == 2) {
1257         block->ref |= pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_REF2);
1258         block->ref ^= dirac_get_arith_bit(arith, CTX_PMODE_REF2) << 1;
1259     }
1260
1261     if (!block->ref) {
1262         pred_block_dc(block, stride, x, y);
1263         for (i = 0; i < 3; i++)
1264             block->u.dc[i] += dirac_get_arith_int(arith+1+i, CTX_DC_F1, CTX_DC_DATA);
1265         return;
1266     }
1267
1268     if (s->globalmc_flag) {
1269         block->ref |= pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_GLOBAL);
1270         block->ref ^= dirac_get_arith_bit(arith, CTX_GLOBAL_BLOCK) << 2;
1271     }
1272
1273     for (i = 0; i < s->num_refs; i++)
1274         if (block->ref & (i+1)) {
1275             if (block->ref & DIRAC_REF_MASK_GLOBAL) {
1276                 global_mv(s, block, x, y, i);
1277             } else {
1278                 pred_mv(block, stride, x, y, i);
1279                 block->u.mv[i][0] += dirac_get_arith_int(arith + 4 + 2 * i, CTX_MV_F1, CTX_MV_DATA);
1280                 block->u.mv[i][1] += dirac_get_arith_int(arith + 5 + 2 * i, CTX_MV_F1, CTX_MV_DATA);
1281             }
1282         }
1283 }
1284
1285 /**
1286  * Copies the current block to the other blocks covered by the current superblock split mode
1287  */
1288 static void propagate_block_data(DiracBlock *block, int stride, int size)
1289 {
1290     int x, y;
1291     DiracBlock *dst = block;
1292
1293     for (x = 1; x < size; x++)
1294         dst[x] = *block;
1295
1296     for (y = 1; y < size; y++) {
1297         dst += stride;
1298         for (x = 0; x < size; x++)
1299             dst[x] = *block;
1300     }
1301 }
1302
1303 /**
1304  * Dirac Specification ->
1305  * 12. Block motion data syntax
1306  */
1307 static int dirac_unpack_block_motion_data(DiracContext *s)
1308 {
1309     GetBitContext *gb = &s->gb;
1310     uint8_t *sbsplit = s->sbsplit;
1311     int i, x, y, q, p;
1312     DiracArith arith[8];
1313
1314     align_get_bits(gb);
1315
1316     /* [DIRAC_STD] 11.2.4 and 12.2.1 Number of blocks and superblocks */
1317     s->sbwidth  = DIVRNDUP(s->source.width,  4*s->plane[0].xbsep);
1318     s->sbheight = DIVRNDUP(s->source.height, 4*s->plane[0].ybsep);
1319     s->blwidth  = 4 * s->sbwidth;
1320     s->blheight = 4 * s->sbheight;
1321
1322     /* [DIRAC_STD] 12.3.1 Superblock splitting modes. superblock_split_modes()
1323        decode superblock split modes */
1324     ff_dirac_init_arith_decoder(arith, gb, svq3_get_ue_golomb(gb));     /* svq3_get_ue_golomb(gb) is the length */
1325     for (y = 0; y < s->sbheight; y++) {
1326         for (x = 0; x < s->sbwidth; x++) {
1327             unsigned int split  = dirac_get_arith_uint(arith, CTX_SB_F1, CTX_SB_DATA);
1328             if (split > 2)
1329                 return AVERROR_INVALIDDATA;
1330             sbsplit[x] = (split + pred_sbsplit(sbsplit+x, s->sbwidth, x, y)) % 3;
1331         }
1332         sbsplit += s->sbwidth;
1333     }
1334
1335     /* setup arith decoding */
1336     ff_dirac_init_arith_decoder(arith, gb, svq3_get_ue_golomb(gb));
1337     for (i = 0; i < s->num_refs; i++) {
1338         ff_dirac_init_arith_decoder(arith + 4 + 2 * i, gb, svq3_get_ue_golomb(gb));
1339         ff_dirac_init_arith_decoder(arith + 5 + 2 * i, gb, svq3_get_ue_golomb(gb));
1340     }
1341     for (i = 0; i < 3; i++)
1342         ff_dirac_init_arith_decoder(arith+1+i, gb, svq3_get_ue_golomb(gb));
1343
1344     for (y = 0; y < s->sbheight; y++)
1345         for (x = 0; x < s->sbwidth; x++) {
1346             int blkcnt = 1 << s->sbsplit[y * s->sbwidth + x];
1347             int step   = 4 >> s->sbsplit[y * s->sbwidth + x];
1348
1349             for (q = 0; q < blkcnt; q++)
1350                 for (p = 0; p < blkcnt; p++) {
1351                     int bx = 4 * x + p*step;
1352                     int by = 4 * y + q*step;
1353                     DiracBlock *block = &s->blmotion[by*s->blwidth + bx];
1354                     decode_block_params(s, arith, block, s->blwidth, bx, by);
1355                     propagate_block_data(block, s->blwidth, step);
1356                 }
1357         }
1358
1359     return 0;
1360 }
1361
1362 static int weight(int i, int blen, int offset)
1363 {
1364 #define ROLLOFF(i) offset == 1 ? ((i) ? 5 : 3) :        \
1365     (1 + (6*(i) + offset - 1) / (2*offset - 1))
1366
1367     if (i < 2*offset)
1368         return ROLLOFF(i);
1369     else if (i > blen-1 - 2*offset)
1370         return ROLLOFF(blen-1 - i);
1371     return 8;
1372 }
1373
1374 static void init_obmc_weight_row(Plane *p, uint8_t *obmc_weight, int stride,
1375                                  int left, int right, int wy)
1376 {
1377     int x;
1378     for (x = 0; left && x < p->xblen >> 1; x++)
1379         obmc_weight[x] = wy*8;
1380     for (; x < p->xblen >> right; x++)
1381         obmc_weight[x] = wy*weight(x, p->xblen, p->xoffset);
1382     for (; x < p->xblen; x++)
1383         obmc_weight[x] = wy*8;
1384     for (; x < stride; x++)
1385         obmc_weight[x] = 0;
1386 }
1387
1388 static void init_obmc_weight(Plane *p, uint8_t *obmc_weight, int stride,
1389                              int left, int right, int top, int bottom)
1390 {
1391     int y;
1392     for (y = 0; top && y < p->yblen >> 1; y++) {
1393         init_obmc_weight_row(p, obmc_weight, stride, left, right, 8);
1394         obmc_weight += stride;
1395     }
1396     for (; y < p->yblen >> bottom; y++) {
1397         int wy = weight(y, p->yblen, p->yoffset);
1398         init_obmc_weight_row(p, obmc_weight, stride, left, right, wy);
1399         obmc_weight += stride;
1400     }
1401     for (; y < p->yblen; y++) {
1402         init_obmc_weight_row(p, obmc_weight, stride, left, right, 8);
1403         obmc_weight += stride;
1404     }
1405 }
1406
1407 static void init_obmc_weights(DiracContext *s, Plane *p, int by)
1408 {
1409     int top = !by;
1410     int bottom = by == s->blheight-1;
1411
1412     /* don't bother re-initing for rows 2 to blheight-2, the weights don't change */
1413     if (top || bottom || by == 1) {
1414         init_obmc_weight(p, s->obmc_weight[0], MAX_BLOCKSIZE, 1, 0, top, bottom);
1415         init_obmc_weight(p, s->obmc_weight[1], MAX_BLOCKSIZE, 0, 0, top, bottom);
1416         init_obmc_weight(p, s->obmc_weight[2], MAX_BLOCKSIZE, 0, 1, top, bottom);
1417     }
1418 }
1419
1420 static const uint8_t epel_weights[4][4][4] = {
1421     {{ 16,  0,  0,  0 },
1422      { 12,  4,  0,  0 },
1423      {  8,  8,  0,  0 },
1424      {  4, 12,  0,  0 }},
1425     {{ 12,  0,  4,  0 },
1426      {  9,  3,  3,  1 },
1427      {  6,  6,  2,  2 },
1428      {  3,  9,  1,  3 }},
1429     {{  8,  0,  8,  0 },
1430      {  6,  2,  6,  2 },
1431      {  4,  4,  4,  4 },
1432      {  2,  6,  2,  6 }},
1433     {{  4,  0, 12,  0 },
1434      {  3,  1,  9,  3 },
1435      {  2,  2,  6,  6 },
1436      {  1,  3,  3,  9 }}
1437 };
1438
1439 /**
1440  * For block x,y, determine which of the hpel planes to do bilinear
1441  * interpolation from and set src[] to the location in each hpel plane
1442  * to MC from.
1443  *
1444  * @return the index of the put_dirac_pixels_tab function to use
1445  *  0 for 1 plane (fpel,hpel), 1 for 2 planes (qpel), 2 for 4 planes (qpel), and 3 for epel
1446  */
1447 static int mc_subpel(DiracContext *s, DiracBlock *block, const uint8_t *src[5],
1448                      int x, int y, int ref, int plane)
1449 {
1450     Plane *p = &s->plane[plane];
1451     uint8_t **ref_hpel = s->ref_pics[ref]->hpel[plane];
1452     int motion_x = block->u.mv[ref][0];
1453     int motion_y = block->u.mv[ref][1];
1454     int mx, my, i, epel, nplanes = 0;
1455
1456     if (plane) {
1457         motion_x >>= s->chroma_x_shift;
1458         motion_y >>= s->chroma_y_shift;
1459     }
1460
1461     mx         = motion_x & ~(-1U << s->mv_precision);
1462     my         = motion_y & ~(-1U << s->mv_precision);
1463     motion_x >>= s->mv_precision;
1464     motion_y >>= s->mv_precision;
1465     /* normalize subpel coordinates to epel */
1466     /* TODO: template this function? */
1467     mx      <<= 3 - s->mv_precision;
1468     my      <<= 3 - s->mv_precision;
1469
1470     x += motion_x;
1471     y += motion_y;
1472     epel = (mx|my)&1;
1473
1474     /* hpel position */
1475     if (!((mx|my)&3)) {
1476         nplanes = 1;
1477         src[0] = ref_hpel[(my>>1)+(mx>>2)] + y*p->stride + x;
1478     } else {
1479         /* qpel or epel */
1480         nplanes = 4;
1481         for (i = 0; i < 4; i++)
1482             src[i] = ref_hpel[i] + y*p->stride + x;
1483
1484         /* if we're interpolating in the right/bottom halves, adjust the planes as needed
1485            we increment x/y because the edge changes for half of the pixels */
1486         if (mx > 4) {
1487             src[0] += 1;
1488             src[2] += 1;
1489             x++;
1490         }
1491         if (my > 4) {
1492             src[0] += p->stride;
1493             src[1] += p->stride;
1494             y++;
1495         }
1496
1497         /* hpel planes are:
1498            [0]: F  [1]: H
1499            [2]: V  [3]: C */
1500         if (!epel) {
1501             /* check if we really only need 2 planes since either mx or my is
1502                a hpel position. (epel weights of 0 handle this there) */
1503             if (!(mx&3)) {
1504                 /* mx == 0: average [0] and [2]
1505                    mx == 4: average [1] and [3] */
1506                 src[!mx] = src[2 + !!mx];
1507                 nplanes = 2;
1508             } else if (!(my&3)) {
1509                 src[0] = src[(my>>1)  ];
1510                 src[1] = src[(my>>1)+1];
1511                 nplanes = 2;
1512             }
1513         } else {
1514             /* adjust the ordering if needed so the weights work */
1515             if (mx > 4) {
1516                 FFSWAP(const uint8_t *, src[0], src[1]);
1517                 FFSWAP(const uint8_t *, src[2], src[3]);
1518             }
1519             if (my > 4) {
1520                 FFSWAP(const uint8_t *, src[0], src[2]);
1521                 FFSWAP(const uint8_t *, src[1], src[3]);
1522             }
1523             src[4] = epel_weights[my&3][mx&3];
1524         }
1525     }
1526
1527     /* fixme: v/h _edge_pos */
1528     if (x + p->xblen > p->width +EDGE_WIDTH/2 ||
1529         y + p->yblen > p->height+EDGE_WIDTH/2 ||
1530         x < 0 || y < 0) {
1531         for (i = 0; i < nplanes; i++) {
1532             s->vdsp.emulated_edge_mc(s->edge_emu_buffer[i], src[i],
1533                                      p->stride, p->stride,
1534                                      p->xblen, p->yblen, x, y,
1535                                      p->width+EDGE_WIDTH/2, p->height+EDGE_WIDTH/2);
1536             src[i] = s->edge_emu_buffer[i];
1537         }
1538     }
1539     return (nplanes>>1) + epel;
1540 }
1541
1542 static void add_dc(uint16_t *dst, int dc, int stride,
1543                    uint8_t *obmc_weight, int xblen, int yblen)
1544 {
1545     int x, y;
1546     dc += 128;
1547
1548     for (y = 0; y < yblen; y++) {
1549         for (x = 0; x < xblen; x += 2) {
1550             dst[x  ] += dc * obmc_weight[x  ];
1551             dst[x+1] += dc * obmc_weight[x+1];
1552         }
1553         dst          += stride;
1554         obmc_weight  += MAX_BLOCKSIZE;
1555     }
1556 }
1557
1558 static void block_mc(DiracContext *s, DiracBlock *block,
1559                      uint16_t *mctmp, uint8_t *obmc_weight,
1560                      int plane, int dstx, int dsty)
1561 {
1562     Plane *p = &s->plane[plane];
1563     const uint8_t *src[5];
1564     int idx;
1565
1566     switch (block->ref&3) {
1567     case 0: /* DC */
1568         add_dc(mctmp, block->u.dc[plane], p->stride, obmc_weight, p->xblen, p->yblen);
1569         return;
1570     case 1:
1571     case 2:
1572         idx = mc_subpel(s, block, src, dstx, dsty, (block->ref&3)-1, plane);
1573         s->put_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen);
1574         if (s->weight_func)
1575             s->weight_func(s->mcscratch, p->stride, s->weight_log2denom,
1576                            s->weight[0] + s->weight[1], p->yblen);
1577         break;
1578     case 3:
1579         idx = mc_subpel(s, block, src, dstx, dsty, 0, plane);
1580         s->put_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen);
1581         idx = mc_subpel(s, block, src, dstx, dsty, 1, plane);
1582         if (s->biweight_func) {
1583             /* fixme: +32 is a quick hack */
1584             s->put_pixels_tab[idx](s->mcscratch + 32, src, p->stride, p->yblen);
1585             s->biweight_func(s->mcscratch, s->mcscratch+32, p->stride, s->weight_log2denom,
1586                              s->weight[0], s->weight[1], p->yblen);
1587         } else
1588             s->avg_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen);
1589         break;
1590     }
1591     s->add_obmc(mctmp, s->mcscratch, p->stride, obmc_weight, p->yblen);
1592 }
1593
1594 static void mc_row(DiracContext *s, DiracBlock *block, uint16_t *mctmp, int plane, int dsty)
1595 {
1596     Plane *p = &s->plane[plane];
1597     int x, dstx = p->xbsep - p->xoffset;
1598
1599     block_mc(s, block, mctmp, s->obmc_weight[0], plane, -p->xoffset, dsty);
1600     mctmp += p->xbsep;
1601
1602     for (x = 1; x < s->blwidth-1; x++) {
1603         block_mc(s, block+x, mctmp, s->obmc_weight[1], plane, dstx, dsty);
1604         dstx  += p->xbsep;
1605         mctmp += p->xbsep;
1606     }
1607     block_mc(s, block+x, mctmp, s->obmc_weight[2], plane, dstx, dsty);
1608 }
1609
1610 static void select_dsp_funcs(DiracContext *s, int width, int height, int xblen, int yblen)
1611 {
1612     int idx = 0;
1613     if (xblen > 8)
1614         idx = 1;
1615     if (xblen > 16)
1616         idx = 2;
1617
1618     memcpy(s->put_pixels_tab, s->diracdsp.put_dirac_pixels_tab[idx], sizeof(s->put_pixels_tab));
1619     memcpy(s->avg_pixels_tab, s->diracdsp.avg_dirac_pixels_tab[idx], sizeof(s->avg_pixels_tab));
1620     s->add_obmc = s->diracdsp.add_dirac_obmc[idx];
1621     if (s->weight_log2denom > 1 || s->weight[0] != 1 || s->weight[1] != 1) {
1622         s->weight_func   = s->diracdsp.weight_dirac_pixels_tab[idx];
1623         s->biweight_func = s->diracdsp.biweight_dirac_pixels_tab[idx];
1624     } else {
1625         s->weight_func   = NULL;
1626         s->biweight_func = NULL;
1627     }
1628 }
1629
1630 static int interpolate_refplane(DiracContext *s, DiracFrame *ref, int plane, int width, int height)
1631 {
1632     /* chroma allocates an edge of 8 when subsampled
1633        which for 4:2:2 means an h edge of 16 and v edge of 8
1634        just use 8 for everything for the moment */
1635     int i, edge = EDGE_WIDTH/2;
1636
1637     ref->hpel[plane][0] = ref->avframe->data[plane];
1638     s->mpvencdsp.draw_edges(ref->hpel[plane][0], ref->avframe->linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM); /* EDGE_TOP | EDGE_BOTTOM values just copied to make it build, this needs to be ensured */
1639
1640     /* no need for hpel if we only have fpel vectors */
1641     if (!s->mv_precision)
1642         return 0;
1643
1644     for (i = 1; i < 4; i++) {
1645         if (!ref->hpel_base[plane][i])
1646             ref->hpel_base[plane][i] = av_malloc((height+2*edge) * ref->avframe->linesize[plane] + 32);
1647         if (!ref->hpel_base[plane][i]) {
1648             return AVERROR(ENOMEM);
1649         }
1650         /* we need to be 16-byte aligned even for chroma */
1651         ref->hpel[plane][i] = ref->hpel_base[plane][i] + edge*ref->avframe->linesize[plane] + 16;
1652     }
1653
1654     if (!ref->interpolated[plane]) {
1655         s->diracdsp.dirac_hpel_filter(ref->hpel[plane][1], ref->hpel[plane][2],
1656                                       ref->hpel[plane][3], ref->hpel[plane][0],
1657                                       ref->avframe->linesize[plane], width, height);
1658         s->mpvencdsp.draw_edges(ref->hpel[plane][1], ref->avframe->linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
1659         s->mpvencdsp.draw_edges(ref->hpel[plane][2], ref->avframe->linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
1660         s->mpvencdsp.draw_edges(ref->hpel[plane][3], ref->avframe->linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
1661     }
1662     ref->interpolated[plane] = 1;
1663
1664     return 0;
1665 }
1666
1667 /**
1668  * Dirac Specification ->
1669  * 13.0 Transform data syntax. transform_data()
1670  */
1671 static int dirac_decode_frame_internal(DiracContext *s)
1672 {
1673     DWTContext d;
1674     int y, i, comp, dsty;
1675     int ret;
1676
1677     if (s->low_delay) {
1678         /* [DIRAC_STD] 13.5.1 low_delay_transform_data() */
1679         for (comp = 0; comp < 3; comp++) {
1680             Plane *p = &s->plane[comp];
1681             memset(p->idwt_buf, 0, p->idwt_stride * p->idwt_height);
1682         }
1683         if (!s->zero_res) {
1684             if ((ret = decode_lowdelay(s)) < 0)
1685                 return ret;
1686         }
1687     }
1688
1689     for (comp = 0; comp < 3; comp++) {
1690         Plane *p       = &s->plane[comp];
1691         uint8_t *frame = s->current_picture->avframe->data[comp];
1692
1693         /* FIXME: small resolutions */
1694         for (i = 0; i < 4; i++)
1695             s->edge_emu_buffer[i] = s->edge_emu_buffer_base + i*FFALIGN(p->width, 16);
1696
1697         if (!s->zero_res && !s->low_delay)
1698         {
1699             memset(p->idwt_buf, 0, p->idwt_stride * p->idwt_height);
1700             decode_component(s, comp); /* [DIRAC_STD] 13.4.1 core_transform_data() */
1701         }
1702         ret = ff_spatial_idwt_init2(&d, p->idwt_buf, p->idwt_width, p->idwt_height, p->idwt_stride,
1703                                     s->wavelet_idx+2, s->wavelet_depth, p->idwt_tmp, s->bit_depth);
1704         if (ret < 0)
1705             return ret;
1706
1707         if (!s->num_refs) { /* intra */
1708             for (y = 0; y < p->height; y += 16) {
1709                 ff_spatial_idwt_slice2(&d, y+16); /* decode */
1710                 s->diracdsp.put_signed_rect_clamped[s->pshift](frame + y*p->stride, p->stride,
1711                                                                p->idwt_buf + y*p->idwt_stride, p->idwt_stride, p->width, 16);
1712             }
1713         } else { /* inter */
1714             int rowheight = p->ybsep*p->stride;
1715
1716             select_dsp_funcs(s, p->width, p->height, p->xblen, p->yblen);
1717
1718             for (i = 0; i < s->num_refs; i++) {
1719                 int ret = interpolate_refplane(s, s->ref_pics[i], comp, p->width, p->height);
1720                 if (ret < 0)
1721                     return ret;
1722             }
1723
1724             memset(s->mctmp, 0, 4*p->yoffset*p->stride);
1725
1726             dsty = -p->yoffset;
1727             for (y = 0; y < s->blheight; y++) {
1728                 int h     = 0,
1729                     start = FFMAX(dsty, 0);
1730                 uint16_t *mctmp    = s->mctmp + y*rowheight;
1731                 DiracBlock *blocks = s->blmotion + y*s->blwidth;
1732
1733                 init_obmc_weights(s, p, y);
1734
1735                 if (y == s->blheight-1 || start+p->ybsep > p->height)
1736                     h = p->height - start;
1737                 else
1738                     h = p->ybsep - (start - dsty);
1739                 if (h < 0)
1740                     break;
1741
1742                 memset(mctmp+2*p->yoffset*p->stride, 0, 2*rowheight);
1743                 mc_row(s, blocks, mctmp, comp, dsty);
1744
1745                 mctmp += (start - dsty)*p->stride + p->xoffset;
1746                 ff_spatial_idwt_slice2(&d, start + h); /* decode */
1747                 /* NOTE: add_rect_clamped hasn't been templated hence the shifts.
1748                  * idwt_stride is passed as pixels, not in bytes as in the rest of the decoder */
1749                 s->diracdsp.add_rect_clamped(frame + start*p->stride, mctmp, p->stride,
1750                                              (int16_t*)(p->idwt_buf) + start*(p->idwt_stride >> 1), (p->idwt_stride >> 1), p->width, h);
1751
1752                 dsty += p->ybsep;
1753             }
1754         }
1755     }
1756
1757
1758     return 0;
1759 }
1760
1761 static int get_buffer_with_edge(AVCodecContext *avctx, AVFrame *f, int flags)
1762 {
1763     int ret, i;
1764     int chroma_x_shift, chroma_y_shift;
1765     avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_x_shift, &chroma_y_shift);
1766
1767     f->width  = avctx->width  + 2 * EDGE_WIDTH;
1768     f->height = avctx->height + 2 * EDGE_WIDTH + 2;
1769     ret = ff_get_buffer(avctx, f, flags);
1770     if (ret < 0)
1771         return ret;
1772
1773     for (i = 0; f->data[i]; i++) {
1774         int offset = (EDGE_WIDTH >> (i && i<3 ? chroma_y_shift : 0)) *
1775                      f->linesize[i] + 32;
1776         f->data[i] += offset;
1777     }
1778     f->width  = avctx->width;
1779     f->height = avctx->height;
1780
1781     return 0;
1782 }
1783
1784 /**
1785  * Dirac Specification ->
1786  * 11.1.1 Picture Header. picture_header()
1787  */
1788 static int dirac_decode_picture_header(DiracContext *s)
1789 {
1790     unsigned retire, picnum;
1791     int i, j, ret;
1792     int64_t refdist, refnum;
1793     GetBitContext *gb = &s->gb;
1794
1795     /* [DIRAC_STD] 11.1.1 Picture Header. picture_header() PICTURE_NUM */
1796     picnum = s->current_picture->avframe->display_picture_number = get_bits_long(gb, 32);
1797
1798
1799     av_log(s->avctx,AV_LOG_DEBUG,"PICTURE_NUM: %d\n",picnum);
1800
1801     /* if this is the first keyframe after a sequence header, start our
1802        reordering from here */
1803     if (s->frame_number < 0)
1804         s->frame_number = picnum;
1805
1806     s->ref_pics[0] = s->ref_pics[1] = NULL;
1807     for (i = 0; i < s->num_refs; i++) {
1808         refnum = (picnum + dirac_get_se_golomb(gb)) & 0xFFFFFFFF;
1809         refdist = INT64_MAX;
1810
1811         /* find the closest reference to the one we want */
1812         /* Jordi: this is needed if the referenced picture hasn't yet arrived */
1813         for (j = 0; j < MAX_REFERENCE_FRAMES && refdist; j++)
1814             if (s->ref_frames[j]
1815                 && FFABS(s->ref_frames[j]->avframe->display_picture_number - refnum) < refdist) {
1816                 s->ref_pics[i] = s->ref_frames[j];
1817                 refdist = FFABS(s->ref_frames[j]->avframe->display_picture_number - refnum);
1818             }
1819
1820         if (!s->ref_pics[i] || refdist)
1821             av_log(s->avctx, AV_LOG_DEBUG, "Reference not found\n");
1822
1823         /* if there were no references at all, allocate one */
1824         if (!s->ref_pics[i])
1825             for (j = 0; j < MAX_FRAMES; j++)
1826                 if (!s->all_frames[j].avframe->data[0]) {
1827                     s->ref_pics[i] = &s->all_frames[j];
1828                     get_buffer_with_edge(s->avctx, s->ref_pics[i]->avframe, AV_GET_BUFFER_FLAG_REF);
1829                     break;
1830                 }
1831
1832         if (!s->ref_pics[i]) {
1833             av_log(s->avctx, AV_LOG_ERROR, "Reference could not be allocated\n");
1834             return AVERROR_INVALIDDATA;
1835         }
1836
1837     }
1838
1839     /* retire the reference frames that are not used anymore */
1840     if (s->current_picture->reference) {
1841         retire = (picnum + dirac_get_se_golomb(gb)) & 0xFFFFFFFF;
1842         if (retire != picnum) {
1843             DiracFrame *retire_pic = remove_frame(s->ref_frames, retire);
1844
1845             if (retire_pic)
1846                 retire_pic->reference &= DELAYED_PIC_REF;
1847             else
1848                 av_log(s->avctx, AV_LOG_DEBUG, "Frame to retire not found\n");
1849         }
1850
1851         /* if reference array is full, remove the oldest as per the spec */
1852         while (add_frame(s->ref_frames, MAX_REFERENCE_FRAMES, s->current_picture)) {
1853             av_log(s->avctx, AV_LOG_ERROR, "Reference frame overflow\n");
1854             remove_frame(s->ref_frames, s->ref_frames[0]->avframe->display_picture_number)->reference &= DELAYED_PIC_REF;
1855         }
1856     }
1857
1858     if (s->num_refs) {
1859         ret = dirac_unpack_prediction_parameters(s);  /* [DIRAC_STD] 11.2 Picture Prediction Data. picture_prediction() */
1860         if (ret < 0)
1861             return ret;
1862         ret = dirac_unpack_block_motion_data(s);      /* [DIRAC_STD] 12. Block motion data syntax                       */
1863         if (ret < 0)
1864             return ret;
1865     }
1866     ret = dirac_unpack_idwt_params(s);                /* [DIRAC_STD] 11.3 Wavelet transform data                        */
1867     if (ret < 0)
1868         return ret;
1869
1870     init_planes(s);
1871     return 0;
1872 }
1873
1874 static int get_delayed_pic(DiracContext *s, AVFrame *picture, int *got_frame)
1875 {
1876     DiracFrame *out = s->delay_frames[0];
1877     int i, out_idx  = 0;
1878     int ret;
1879
1880     /* find frame with lowest picture number */
1881     for (i = 1; s->delay_frames[i]; i++)
1882         if (s->delay_frames[i]->avframe->display_picture_number < out->avframe->display_picture_number) {
1883             out     = s->delay_frames[i];
1884             out_idx = i;
1885         }
1886
1887     for (i = out_idx; s->delay_frames[i]; i++)
1888         s->delay_frames[i] = s->delay_frames[i+1];
1889
1890     if (out) {
1891         out->reference ^= DELAYED_PIC_REF;
1892         *got_frame = 1;
1893         if((ret = av_frame_ref(picture, out->avframe)) < 0)
1894             return ret;
1895     }
1896
1897     return 0;
1898 }
1899
1900 /**
1901  * Dirac Specification ->
1902  * 9.6 Parse Info Header Syntax. parse_info()
1903  * 4 byte start code + byte parse code + 4 byte size + 4 byte previous size
1904  */
1905 #define DATA_UNIT_HEADER_SIZE 13
1906
1907 /* [DIRAC_STD] dirac_decode_data_unit makes reference to the while defined in 9.3
1908    inside the function parse_sequence() */
1909 static int dirac_decode_data_unit(AVCodecContext *avctx, const uint8_t *buf, int size)
1910 {
1911     DiracContext *s   = avctx->priv_data;
1912     DiracFrame *pic   = NULL;
1913     int ret, i;
1914     uint8_t parse_code;
1915     unsigned tmp;
1916
1917     if (size < DATA_UNIT_HEADER_SIZE)
1918         return AVERROR_INVALIDDATA;
1919
1920     parse_code = buf[4];
1921
1922     init_get_bits(&s->gb, &buf[13], 8*(size - DATA_UNIT_HEADER_SIZE));
1923
1924     if (parse_code == DIRAC_PCODE_SEQ_HEADER) {
1925         if (s->seen_sequence_header)
1926             return 0;
1927
1928         /* [DIRAC_STD] 10. Sequence header */
1929         ret = avpriv_dirac_parse_sequence_header(avctx, &s->gb, &s->source,
1930                                                  &s->version, &s->bit_depth);
1931         if (ret < 0)
1932             return ret;
1933
1934         s->pshift = s->bit_depth > 8;
1935
1936         avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
1937
1938         ret = alloc_sequence_buffers(s);
1939         if (ret < 0)
1940             return ret;
1941
1942         s->seen_sequence_header = 1;
1943     } else if (parse_code == DIRAC_PCODE_END_SEQ) { /* [DIRAC_STD] End of Sequence */
1944         free_sequence_buffers(s);
1945         s->seen_sequence_header = 0;
1946     } else if (parse_code == DIRAC_PCODE_AUX) {
1947         if (buf[13] == 1) {     /* encoder implementation/version */
1948             int ver[3];
1949             /* versions older than 1.0.8 don't store quant delta for
1950                subbands with only one codeblock */
1951             if (sscanf(buf+14, "Schroedinger %d.%d.%d", ver, ver+1, ver+2) == 3)
1952                 if (ver[0] == 1 && ver[1] == 0 && ver[2] <= 7)
1953                     s->old_delta_quant = 1;
1954         }
1955     } else if (parse_code & 0x8) {  /* picture data unit */
1956         if (!s->seen_sequence_header) {
1957             av_log(avctx, AV_LOG_DEBUG, "Dropping frame without sequence header\n");
1958             return AVERROR_INVALIDDATA;
1959         }
1960
1961         /* find an unused frame */
1962         for (i = 0; i < MAX_FRAMES; i++)
1963             if (s->all_frames[i].avframe->data[0] == NULL)
1964                 pic = &s->all_frames[i];
1965         if (!pic) {
1966             av_log(avctx, AV_LOG_ERROR, "framelist full\n");
1967             return AVERROR_INVALIDDATA;
1968         }
1969
1970         av_frame_unref(pic->avframe);
1971
1972         /* [DIRAC_STD] Defined in 9.6.1 ... */
1973         tmp            =  parse_code & 0x03;                   /* [DIRAC_STD] num_refs()      */
1974         if (tmp > 2) {
1975             av_log(avctx, AV_LOG_ERROR, "num_refs of 3\n");
1976             return AVERROR_INVALIDDATA;
1977         }
1978         s->num_refs      = tmp;
1979         s->is_arith      = (parse_code & 0x48) == 0x08;          /* [DIRAC_STD] using_ac()            */
1980         s->low_delay     = (parse_code & 0x88) == 0x88;          /* [DIRAC_STD] is_low_delay()        */
1981         s->core_syntax   = (parse_code & 0x88) == 0x08;          /* [DIRAC_STD] is_core_syntax()      */
1982         s->ld_picture    = (parse_code & 0xF8) == 0xC8;          /* [DIRAC_STD] is_ld_picture()       */
1983         s->hq_picture    = (parse_code & 0xF8) == 0xE8;          /* [DIRAC_STD] is_hq_picture()       */
1984         s->dc_prediction = (parse_code & 0x28) == 0x08;          /* [DIRAC_STD] using_dc_prediction() */
1985         pic->reference   = (parse_code & 0x0C) == 0x0C;          /* [DIRAC_STD] is_reference()        */
1986         pic->avframe->key_frame = s->num_refs == 0;              /* [DIRAC_STD] is_intra()            */
1987         pic->avframe->pict_type = s->num_refs + 1;               /* Definition of AVPictureType in avutil.h */
1988
1989         if (s->version.minor == 2 && parse_code == 0x88)
1990             s->ld_picture = 1;
1991
1992         if (s->low_delay && !(s->ld_picture || s->hq_picture) ) {
1993             av_log(avctx, AV_LOG_ERROR, "Invalid low delay flag\n");
1994             return AVERROR_INVALIDDATA;
1995         }
1996
1997         if ((ret = get_buffer_with_edge(avctx, pic->avframe, (parse_code & 0x0C) == 0x0C ? AV_GET_BUFFER_FLAG_REF : 0)) < 0)
1998             return ret;
1999         s->current_picture = pic;
2000         s->plane[0].stride = pic->avframe->linesize[0];
2001         s->plane[1].stride = pic->avframe->linesize[1];
2002         s->plane[2].stride = pic->avframe->linesize[2];
2003
2004         if (alloc_buffers(s, FFMAX3(FFABS(s->plane[0].stride), FFABS(s->plane[1].stride), FFABS(s->plane[2].stride))) < 0)
2005             return AVERROR(ENOMEM);
2006
2007         /* [DIRAC_STD] 11.1 Picture parse. picture_parse() */
2008         ret = dirac_decode_picture_header(s);
2009         if (ret < 0)
2010             return ret;
2011
2012         /* [DIRAC_STD] 13.0 Transform data syntax. transform_data() */
2013         ret = dirac_decode_frame_internal(s);
2014         if (ret < 0)
2015             return ret;
2016     }
2017     return 0;
2018 }
2019
2020 static int dirac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
2021 {
2022     DiracContext *s     = avctx->priv_data;
2023     AVFrame *picture    = data;
2024     uint8_t *buf        = pkt->data;
2025     int buf_size        = pkt->size;
2026     int i, buf_idx      = 0;
2027     int ret;
2028     unsigned data_unit_size;
2029
2030     /* release unused frames */
2031     for (i = 0; i < MAX_FRAMES; i++)
2032         if (s->all_frames[i].avframe->data[0] && !s->all_frames[i].reference) {
2033             av_frame_unref(s->all_frames[i].avframe);
2034             memset(s->all_frames[i].interpolated, 0, sizeof(s->all_frames[i].interpolated));
2035         }
2036
2037     s->current_picture = NULL;
2038     *got_frame = 0;
2039
2040     /* end of stream, so flush delayed pics */
2041     if (buf_size == 0)
2042         return get_delayed_pic(s, (AVFrame *)data, got_frame);
2043
2044     for (;;) {
2045         /*[DIRAC_STD] Here starts the code from parse_info() defined in 9.6
2046           [DIRAC_STD] PARSE_INFO_PREFIX = "BBCD" as defined in ISO/IEC 646
2047           BBCD start code search */
2048         for (; buf_idx + DATA_UNIT_HEADER_SIZE < buf_size; buf_idx++) {
2049             if (buf[buf_idx  ] == 'B' && buf[buf_idx+1] == 'B' &&
2050                 buf[buf_idx+2] == 'C' && buf[buf_idx+3] == 'D')
2051                 break;
2052         }
2053         /* BBCD found or end of data */
2054         if (buf_idx + DATA_UNIT_HEADER_SIZE >= buf_size)
2055             break;
2056
2057         data_unit_size = AV_RB32(buf+buf_idx+5);
2058         if (data_unit_size > buf_size - buf_idx || !data_unit_size) {
2059             if(data_unit_size > buf_size - buf_idx)
2060             av_log(s->avctx, AV_LOG_ERROR,
2061                    "Data unit with size %d is larger than input buffer, discarding\n",
2062                    data_unit_size);
2063             buf_idx += 4;
2064             continue;
2065         }
2066         /* [DIRAC_STD] dirac_decode_data_unit makes reference to the while defined in 9.3 inside the function parse_sequence() */
2067         ret = dirac_decode_data_unit(avctx, buf+buf_idx, data_unit_size);
2068         if (ret < 0)
2069         {
2070             av_log(s->avctx, AV_LOG_ERROR,"Error in dirac_decode_data_unit\n");
2071             return ret;
2072         }
2073         buf_idx += data_unit_size;
2074     }
2075
2076     if (!s->current_picture)
2077         return buf_size;
2078
2079     if (s->current_picture->avframe->display_picture_number > s->frame_number) {
2080         DiracFrame *delayed_frame = remove_frame(s->delay_frames, s->frame_number);
2081
2082         s->current_picture->reference |= DELAYED_PIC_REF;
2083
2084         if (add_frame(s->delay_frames, MAX_DELAY, s->current_picture)) {
2085             int min_num = s->delay_frames[0]->avframe->display_picture_number;
2086             /* Too many delayed frames, so we display the frame with the lowest pts */
2087             av_log(avctx, AV_LOG_ERROR, "Delay frame overflow\n");
2088
2089             for (i = 1; s->delay_frames[i]; i++)
2090                 if (s->delay_frames[i]->avframe->display_picture_number < min_num)
2091                     min_num = s->delay_frames[i]->avframe->display_picture_number;
2092
2093             delayed_frame = remove_frame(s->delay_frames, min_num);
2094             add_frame(s->delay_frames, MAX_DELAY, s->current_picture);
2095         }
2096
2097         if (delayed_frame) {
2098             delayed_frame->reference ^= DELAYED_PIC_REF;
2099             if((ret=av_frame_ref(data, delayed_frame->avframe)) < 0)
2100                 return ret;
2101             *got_frame = 1;
2102         }
2103     } else if (s->current_picture->avframe->display_picture_number == s->frame_number) {
2104         /* The right frame at the right time :-) */
2105         if((ret=av_frame_ref(data, s->current_picture->avframe)) < 0)
2106             return ret;
2107         *got_frame = 1;
2108     }
2109
2110     if (*got_frame)
2111         s->frame_number = picture->display_picture_number + 1;
2112
2113     return buf_idx;
2114 }
2115
2116 AVCodec ff_dirac_decoder = {
2117     .name           = "dirac",
2118     .long_name      = NULL_IF_CONFIG_SMALL("BBC Dirac VC-2"),
2119     .type           = AVMEDIA_TYPE_VIDEO,
2120     .id             = AV_CODEC_ID_DIRAC,
2121     .priv_data_size = sizeof(DiracContext),
2122     .init           = dirac_decode_init,
2123     .close          = dirac_decode_end,
2124     .decode         = dirac_decode_frame,
2125     .capabilities   = AV_CODEC_CAP_DELAY,
2126     .flush          = dirac_decode_flush,
2127 };