Coverage Report

Created: 2026-01-16 07:48

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