LCOV - code coverage report
Current view: top level - libavcodec - diracdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 889 1147 77.5 %
Date: 2017-12-17 11:58:42 Functions: 48 52 92.3 %

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

Generated by: LCOV version 1.13