2 * Copyright (C) 2007 Marco Gerards <marco@gnu.org>
3 * Copyright (C) 2009 David Conrad
4 * Copyright (C) 2011 Jordi Ortiz
6 * This file is part of FFmpeg.
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.
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.
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
26 * @author Marco Gerards <marco@gnu.org>, David Conrad, Jordi Ortiz <nenjordi@gmail.com>
31 #include "bytestream.h"
34 #include "dirac_arith.h"
35 #include "mpeg12data.h"
36 #include "libavcodec/mpegvideo.h"
37 #include "mpegvideoencdsp.h"
38 #include "dirac_dwt.h"
45 * The spec limits this to 3 for frame coding, but in practice can be as high as 6
47 #define MAX_REFERENCE_FRAMES 8
48 #define MAX_DELAY 5 /* limit for main profile for frame coding (TODO: field coding) */
49 #define MAX_FRAMES (MAX_REFERENCE_FRAMES + MAX_DELAY + 1)
50 #define MAX_QUANT 255 /* max quant for VC-2 */
51 #define MAX_BLOCKSIZE 32 /* maximum xblen/yblen we support */
54 * DiracBlock->ref flags, if set then the block does MC from the given ref
56 #define DIRAC_REF_MASK_REF1 1
57 #define DIRAC_REF_MASK_REF2 2
58 #define DIRAC_REF_MASK_GLOBAL 4
61 * Value of Picture.reference when Picture is not a reference picture, but
62 * is held for delayed output.
64 #define DELAYED_PIC_REF 4
66 #define CALC_PADDING(size, depth) \
67 (((size + (1 << depth) - 1) >> depth) << depth)
69 #define DIVRNDUP(a, b) (((a) + (b) - 1) / (b))
73 int interpolated[3]; /* 1 if hpel[] is valid */
75 uint8_t *hpel_base[3][4];
83 } u; /* anonymous unions aren't in C99 :( */
87 typedef struct SubBand {
90 int stride; /* in bytes */
96 struct SubBand *parent;
100 const uint8_t *coeff_data;
103 typedef struct Plane {
113 /* block separation (block n+1 starts after this many pixels in block n) */
116 /* amount of overspill on each edge (half of the overlap between blocks) */
120 SubBand band[MAX_DWT_LEVELS][4];
123 typedef struct DiracContext {
124 AVCodecContext *avctx;
125 MpegvideoEncDSPContext mpvencdsp;
126 VideoDSPContext vdsp;
127 DiracDSPContext diracdsp;
128 DiracVersionInfo version;
130 AVDiracSeqHeader seq;
131 int seen_sequence_header;
132 int frame_number; /* number of the next frame to display */
137 int bit_depth; /* bit depth */
138 int pshift; /* pixel shift = bit_depth > 8 */
140 int zero_res; /* zero residue flag */
141 int is_arith; /* whether coeffs use arith or golomb coding */
142 int core_syntax; /* use core syntax only */
143 int low_delay; /* use the low delay syntax */
144 int hq_picture; /* high quality picture, enables low_delay */
145 int ld_picture; /* use low delay picture, turns on low_delay */
146 int dc_prediction; /* has dc prediction */
147 int globalmc_flag; /* use global motion compensation */
148 int num_refs; /* number of reference pictures */
150 /* wavelet decoding */
151 unsigned wavelet_depth; /* depth of the IDWT */
152 unsigned wavelet_idx;
155 * schroedinger older than 1.0.8 doesn't store
156 * quant delta if only one codebook exists in a band
158 unsigned old_delta_quant;
159 unsigned codeblock_mode;
161 unsigned num_x; /* number of horizontal slices */
162 unsigned num_y; /* number of vertical slices */
167 } codeblock[MAX_DWT_LEVELS+1];
170 AVRational bytes; /* average bytes per slice */
171 uint8_t quant[MAX_DWT_LEVELS][4]; /* [DIRAC_STD] E.1 */
175 unsigned prefix_bytes;
176 unsigned size_scaler;
180 int pan_tilt[2]; /* pan/tilt vector */
181 int zrs[2][2]; /* zoom/rotate/shear matrix */
182 int perspective[2]; /* perspective vector */
184 unsigned perspective_exp;
187 /* motion compensation */
188 uint8_t mv_precision; /* [DIRAC_STD] REFS_WT_PRECISION */
189 int16_t weight[2]; /* [DIRAC_STD] REF1_WT and REF2_WT */
190 unsigned weight_log2denom; /* [DIRAC_STD] REFS_WT_PRECISION */
192 int blwidth; /* number of blocks (horizontally) */
193 int blheight; /* number of blocks (vertically) */
194 int sbwidth; /* number of superblocks (horizontally) */
195 int sbheight; /* number of superblocks (vertically) */
198 DiracBlock *blmotion;
200 uint8_t *edge_emu_buffer[4];
201 uint8_t *edge_emu_buffer_base;
203 uint16_t *mctmp; /* buffer holding the MC data multiplied by OBMC weights */
207 DECLARE_ALIGNED(16, uint8_t, obmc_weight)[3][MAX_BLOCKSIZE*MAX_BLOCKSIZE];
209 void (*put_pixels_tab[4])(uint8_t *dst, const uint8_t *src[5], int stride, int h);
210 void (*avg_pixels_tab[4])(uint8_t *dst, const uint8_t *src[5], int stride, int h);
211 void (*add_obmc)(uint16_t *dst, const uint8_t *src, int stride, const uint8_t *obmc_weight, int yblen);
212 dirac_weight_func weight_func;
213 dirac_biweight_func biweight_func;
215 DiracFrame *current_picture;
216 DiracFrame *ref_pics[2];
218 DiracFrame *ref_frames[MAX_REFERENCE_FRAMES+1];
219 DiracFrame *delay_frames[MAX_DELAY+1];
220 DiracFrame all_frames[MAX_FRAMES];
231 /* magic number division by 3 from schroedinger */
232 static inline int divide3(int x)
234 return ((x+1)*21845 + 10922) >> 16;
237 static DiracFrame *remove_frame(DiracFrame *framelist[], int picnum)
239 DiracFrame *remove_pic = NULL;
240 int i, remove_idx = -1;
242 for (i = 0; framelist[i]; i++)
243 if (framelist[i]->avframe->display_picture_number == picnum) {
244 remove_pic = framelist[i];
249 for (i = remove_idx; framelist[i]; i++)
250 framelist[i] = framelist[i+1];
255 static int add_frame(DiracFrame *framelist[], int maxframes, DiracFrame *frame)
258 for (i = 0; i < maxframes; i++)
260 framelist[i] = frame;
266 static int alloc_sequence_buffers(DiracContext *s)
268 int sbwidth = DIVRNDUP(s->seq.width, 4);
269 int sbheight = DIVRNDUP(s->seq.height, 4);
270 int i, w, h, top_padding;
272 /* todo: think more about this / use or set Plane here */
273 for (i = 0; i < 3; i++) {
274 int max_xblen = MAX_BLOCKSIZE >> (i ? s->chroma_x_shift : 0);
275 int max_yblen = MAX_BLOCKSIZE >> (i ? s->chroma_y_shift : 0);
276 w = s->seq.width >> (i ? s->chroma_x_shift : 0);
277 h = s->seq.height >> (i ? s->chroma_y_shift : 0);
279 /* we allocate the max we support here since num decompositions can
280 * change from frame to frame. Stride is aligned to 16 for SIMD, and
281 * 1<<MAX_DWT_LEVELS top padding to avoid if(y>0) in arith decoding
282 * MAX_BLOCKSIZE padding for MC: blocks can spill up to half of that
284 top_padding = FFMAX(1<<MAX_DWT_LEVELS, max_yblen/2);
285 w = FFALIGN(CALC_PADDING(w, MAX_DWT_LEVELS), 8); /* FIXME: Should this be 16 for SSE??? */
286 h = top_padding + CALC_PADDING(h, MAX_DWT_LEVELS) + max_yblen/2;
288 s->plane[i].idwt.buf_base = av_mallocz_array((w+max_xblen), h * (2 << s->pshift));
289 s->plane[i].idwt.tmp = av_malloc_array((w+16), 2 << s->pshift);
290 s->plane[i].idwt.buf = s->plane[i].idwt.buf_base + (top_padding*w)*(2 << s->pshift);
291 if (!s->plane[i].idwt.buf_base || !s->plane[i].idwt.tmp)
292 return AVERROR(ENOMEM);
295 /* fixme: allocate using real stride here */
296 s->sbsplit = av_malloc_array(sbwidth, sbheight);
297 s->blmotion = av_malloc_array(sbwidth, sbheight * 16 * sizeof(*s->blmotion));
299 if (!s->sbsplit || !s->blmotion)
300 return AVERROR(ENOMEM);
304 static int alloc_buffers(DiracContext *s, int stride)
306 int w = s->seq.width;
307 int h = s->seq.height;
309 av_assert0(stride >= w);
312 if (s->buffer_stride >= stride)
314 s->buffer_stride = 0;
316 av_freep(&s->edge_emu_buffer_base);
317 memset(s->edge_emu_buffer, 0, sizeof(s->edge_emu_buffer));
319 av_freep(&s->mcscratch);
321 s->edge_emu_buffer_base = av_malloc_array(stride, MAX_BLOCKSIZE);
323 s->mctmp = av_malloc_array((stride+MAX_BLOCKSIZE), (h+MAX_BLOCKSIZE) * sizeof(*s->mctmp));
324 s->mcscratch = av_malloc_array(stride, MAX_BLOCKSIZE);
326 if (!s->edge_emu_buffer_base || !s->mctmp || !s->mcscratch)
327 return AVERROR(ENOMEM);
329 s->buffer_stride = stride;
333 static void free_sequence_buffers(DiracContext *s)
337 for (i = 0; i < MAX_FRAMES; i++) {
338 if (s->all_frames[i].avframe->data[0]) {
339 av_frame_unref(s->all_frames[i].avframe);
340 memset(s->all_frames[i].interpolated, 0, sizeof(s->all_frames[i].interpolated));
343 for (j = 0; j < 3; j++)
344 for (k = 1; k < 4; k++)
345 av_freep(&s->all_frames[i].hpel_base[j][k]);
348 memset(s->ref_frames, 0, sizeof(s->ref_frames));
349 memset(s->delay_frames, 0, sizeof(s->delay_frames));
351 for (i = 0; i < 3; i++) {
352 av_freep(&s->plane[i].idwt.buf_base);
353 av_freep(&s->plane[i].idwt.tmp);
356 s->buffer_stride = 0;
357 av_freep(&s->sbsplit);
358 av_freep(&s->blmotion);
359 av_freep(&s->edge_emu_buffer_base);
362 av_freep(&s->mcscratch);
365 static av_cold int dirac_decode_init(AVCodecContext *avctx)
367 DiracContext *s = avctx->priv_data;
371 s->frame_number = -1;
373 ff_diracdsp_init(&s->diracdsp);
374 ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
375 ff_videodsp_init(&s->vdsp, 8);
377 for (i = 0; i < MAX_FRAMES; i++) {
378 s->all_frames[i].avframe = av_frame_alloc();
379 if (!s->all_frames[i].avframe) {
381 av_frame_free(&s->all_frames[--i].avframe);
382 return AVERROR(ENOMEM);
389 static void dirac_decode_flush(AVCodecContext *avctx)
391 DiracContext *s = avctx->priv_data;
392 free_sequence_buffers(s);
393 s->seen_sequence_header = 0;
394 s->frame_number = -1;
397 static av_cold int dirac_decode_end(AVCodecContext *avctx)
399 DiracContext *s = avctx->priv_data;
402 dirac_decode_flush(avctx);
403 for (i = 0; i < MAX_FRAMES; i++)
404 av_frame_free(&s->all_frames[i].avframe);
409 #define SIGN_CTX(x) (CTX_SIGN_ZERO + ((x) > 0) - ((x) < 0))
411 static inline int coeff_unpack_golomb(GetBitContext *gb, int qfactor, int qoffset)
417 UPDATE_CACHE(re, gb);
418 buf = GET_CACHE(re, gb);
420 if (buf & 0x80000000) {
421 LAST_SKIP_BITS(re,gb,1);
422 CLOSE_READER(re, gb);
426 if (buf & 0xAA800000) {
428 SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
430 coeff = ff_interleaved_ue_golomb_vlc_code[buf];
437 FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
439 if (ff_interleaved_golomb_vlc_len[buf] != 9) {
440 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
441 ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
444 ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
445 UPDATE_CACHE(re, gb);
446 buf = GET_CACHE(re, gb);
447 } while (ret<0x8000000U && BITS_AVAILABLE(re, gb));
452 coeff = (coeff * qfactor + qoffset) >> 2;
453 sign = SHOW_SBITS(re, gb, 1);
454 LAST_SKIP_BITS(re, gb, 1);
455 coeff = (coeff ^ sign) - sign;
457 CLOSE_READER(re, gb);
461 #define UNPACK_ARITH(n, type) \
462 static inline void coeff_unpack_arith_##n(DiracArith *c, int qfactor, int qoffset, \
463 SubBand *b, type *buf, int x, int y) \
465 int coeff, sign, sign_pred = 0, pred_ctx = CTX_ZPZN_F1; \
466 const int mstride = -(b->stride >> (1+b->pshift)); \
468 const type *pbuf = (type *)b->parent->ibuf; \
469 const int stride = b->parent->stride >> (1+b->parent->pshift); \
470 pred_ctx += !!pbuf[stride * (y>>1) + (x>>1)] << 1; \
472 if (b->orientation == subband_hl) \
473 sign_pred = buf[mstride]; \
475 pred_ctx += !(buf[-1] | buf[mstride] | buf[-1 + mstride]); \
476 if (b->orientation == subband_lh) \
477 sign_pred = buf[-1]; \
479 pred_ctx += !buf[mstride]; \
481 coeff = dirac_get_arith_uint(c, pred_ctx, CTX_COEFF_DATA); \
483 coeff = (coeff * qfactor + qoffset) >> 2; \
484 sign = dirac_get_arith_bit(c, SIGN_CTX(sign_pred)); \
485 coeff = (coeff ^ -sign) + sign; \
490 UNPACK_ARITH(8, int16_t)
491 UNPACK_ARITH(10, int32_t)
494 * Decode the coeffs in the rectangle defined by left, right, top, bottom
495 * [DIRAC_STD] 13.4.3.2 Codeblock unpacking loop. codeblock()
497 static inline void codeblock(DiracContext *s, SubBand *b,
498 GetBitContext *gb, DiracArith *c,
499 int left, int right, int top, int bottom,
500 int blockcnt_one, int is_arith)
502 int x, y, zero_block;
503 int qoffset, qfactor;
506 /* check for any coded coefficients in this codeblock */
509 zero_block = dirac_get_arith_bit(c, CTX_ZERO_BLOCK);
511 zero_block = get_bits1(gb);
517 if (s->codeblock_mode && !(s->old_delta_quant && blockcnt_one)) {
518 int quant = b->quant;
520 quant += dirac_get_arith_int(c, CTX_DELTA_Q_F, CTX_DELTA_Q_DATA);
522 quant += dirac_get_se_golomb(gb);
524 av_log(s->avctx, AV_LOG_ERROR, "Invalid quant\n");
530 if (b->quant > 115) {
531 av_log(s->avctx, AV_LOG_ERROR, "Unsupported quant %d\n", b->quant);
536 qfactor = ff_dirac_qscale_tab[b->quant];
537 /* TODO: context pointer? */
539 qoffset = ff_dirac_qoffset_intra_tab[b->quant] + 2;
541 qoffset = ff_dirac_qoffset_inter_tab[b->quant] + 2;
543 buf = b->ibuf + top * b->stride;
545 for (y = top; y < bottom; y++) {
546 for (x = left; x < right; x++) {
548 coeff_unpack_arith_10(c, qfactor, qoffset, b, (int32_t*)(buf)+x, x, y);
550 coeff_unpack_arith_8(c, qfactor, qoffset, b, (int16_t*)(buf)+x, x, y);
556 for (y = top; y < bottom; y++) {
557 for (x = left; x < right; x++) {
558 int val = coeff_unpack_golomb(gb, qfactor, qoffset);
560 AV_WN32(&buf[4*x], val);
562 AV_WN16(&buf[2*x], val);
571 * Dirac Specification ->
572 * 13.3 intra_dc_prediction(band)
574 #define INTRA_DC_PRED(n, type) \
575 static inline void intra_dc_prediction_##n(SubBand *b) \
577 type *buf = (type*)b->ibuf; \
580 for (x = 1; x < b->width; x++) \
581 buf[x] += buf[x-1]; \
582 buf += (b->stride >> (1+b->pshift)); \
584 for (y = 1; y < b->height; y++) { \
585 buf[0] += buf[-(b->stride >> (1+b->pshift))]; \
587 for (x = 1; x < b->width; x++) { \
588 int pred = buf[x - 1] + buf[x - (b->stride >> (1+b->pshift))] + buf[x - (b->stride >> (1+b->pshift))-1]; \
589 buf[x] += divide3(pred); \
591 buf += (b->stride >> (1+b->pshift)); \
595 INTRA_DC_PRED(8, int16_t)
596 INTRA_DC_PRED(10, int32_t)
599 * Dirac Specification ->
600 * 13.4.2 Non-skipped subbands. subband_coeffs()
602 static av_always_inline void decode_subband_internal(DiracContext *s, SubBand *b, int is_arith)
604 int cb_x, cb_y, left, right, top, bottom;
607 int cb_width = s->codeblock[b->level + (b->orientation != subband_ll)].width;
608 int cb_height = s->codeblock[b->level + (b->orientation != subband_ll)].height;
609 int blockcnt_one = (cb_width + cb_height) == 2;
614 init_get_bits8(&gb, b->coeff_data, b->length);
617 ff_dirac_init_arith_decoder(&c, &gb, b->length);
620 for (cb_y = 0; cb_y < cb_height; cb_y++) {
621 bottom = (b->height * (cb_y+1LL)) / cb_height;
623 for (cb_x = 0; cb_x < cb_width; cb_x++) {
624 right = (b->width * (cb_x+1LL)) / cb_width;
625 codeblock(s, b, &gb, &c, left, right, top, bottom, blockcnt_one, is_arith);
631 if (b->orientation == subband_ll && s->num_refs == 0) {
633 intra_dc_prediction_10(b);
635 intra_dc_prediction_8(b);
640 static int decode_subband_arith(AVCodecContext *avctx, void *b)
642 DiracContext *s = avctx->priv_data;
643 decode_subband_internal(s, b, 1);
647 static int decode_subband_golomb(AVCodecContext *avctx, void *arg)
649 DiracContext *s = avctx->priv_data;
651 decode_subband_internal(s, *b, 0);
656 * Dirac Specification ->
657 * [DIRAC_STD] 13.4.1 core_transform_data()
659 static void decode_component(DiracContext *s, int comp)
661 AVCodecContext *avctx = s->avctx;
662 SubBand *bands[3*MAX_DWT_LEVELS+1];
663 enum dirac_subband orientation;
664 int level, num_bands = 0;
666 /* Unpack all subbands at all levels. */
667 for (level = 0; level < s->wavelet_depth; level++) {
668 for (orientation = !!level; orientation < 4; orientation++) {
669 SubBand *b = &s->plane[comp].band[level][orientation];
670 bands[num_bands++] = b;
672 align_get_bits(&s->gb);
673 /* [DIRAC_STD] 13.4.2 subband() */
674 b->length = svq3_get_ue_golomb(&s->gb);
676 b->quant = svq3_get_ue_golomb(&s->gb);
677 align_get_bits(&s->gb);
678 b->coeff_data = s->gb.buffer + get_bits_count(&s->gb)/8;
679 b->length = FFMIN(b->length, FFMAX(get_bits_left(&s->gb)/8, 0));
680 skip_bits_long(&s->gb, b->length*8);
683 /* arithmetic coding has inter-level dependencies, so we can only execute one level at a time */
685 avctx->execute(avctx, decode_subband_arith, &s->plane[comp].band[level][!!level],
686 NULL, 4-!!level, sizeof(SubBand));
688 /* golomb coding has no inter-level dependencies, so we can execute all subbands in parallel */
690 avctx->execute(avctx, decode_subband_golomb, bands, NULL, num_bands, sizeof(SubBand*));
693 #define PARSE_VALUES(type, x, gb, ebits, buf1, buf2) \
694 type *buf = (type *)buf1; \
695 buf[x] = coeff_unpack_golomb(gb, qfactor, qoffset); \
696 if (get_bits_count(gb) >= ebits) \
699 buf = (type *)buf2; \
700 buf[x] = coeff_unpack_golomb(gb, qfactor, qoffset); \
701 if (get_bits_count(gb) >= ebits) \
705 static void decode_subband(DiracContext *s, GetBitContext *gb, int quant,
706 int slice_x, int slice_y, int bits_end,
707 SubBand *b1, SubBand *b2)
709 int left = b1->width * slice_x / s->num_x;
710 int right = b1->width *(slice_x+1) / s->num_x;
711 int top = b1->height * slice_y / s->num_y;
712 int bottom = b1->height *(slice_y+1) / s->num_y;
714 int qfactor, qoffset;
716 uint8_t *buf1 = b1->ibuf + top * b1->stride;
717 uint8_t *buf2 = b2 ? b2->ibuf + top * b2->stride: NULL;
721 av_log(s->avctx, AV_LOG_ERROR, "Unsupported quant %d\n", quant);
724 qfactor = ff_dirac_qscale_tab[quant & 0x7f];
725 qoffset = ff_dirac_qoffset_intra_tab[quant & 0x7f] + 2;
726 /* we have to constantly check for overread since the spec explicitly
727 requires this, with the meaning that all remaining coeffs are set to 0 */
728 if (get_bits_count(gb) >= bits_end)
732 for (y = top; y < bottom; y++) {
733 for (x = left; x < right; x++) {
734 PARSE_VALUES(int32_t, x, gb, bits_end, buf1, buf2);
742 for (y = top; y < bottom; y++) {
743 for (x = left; x < right; x++) {
744 PARSE_VALUES(int16_t, x, gb, bits_end, buf1, buf2);
753 /* Used by Low Delay and High Quality profiles */
754 typedef struct DiracSlice {
763 * Dirac Specification ->
764 * 13.5.2 Slices. slice(sx,sy)
766 static int decode_lowdelay_slice(AVCodecContext *avctx, void *arg)
768 DiracContext *s = avctx->priv_data;
769 DiracSlice *slice = arg;
770 GetBitContext *gb = &slice->gb;
771 enum dirac_subband orientation;
772 int level, quant, chroma_bits, chroma_end;
774 int quant_base = get_bits(gb, 7); /*[DIRAC_STD] qindex */
775 int length_bits = av_log2(8 * slice->bytes)+1;
776 int luma_bits = get_bits_long(gb, length_bits);
777 int luma_end = get_bits_count(gb) + FFMIN(luma_bits, get_bits_left(gb));
779 /* [DIRAC_STD] 13.5.5.2 luma_slice_band */
780 for (level = 0; level < s->wavelet_depth; level++)
781 for (orientation = !!level; orientation < 4; orientation++) {
782 quant = FFMAX(quant_base - s->lowdelay.quant[level][orientation], 0);
783 decode_subband(s, gb, quant, slice->slice_x, slice->slice_y, luma_end,
784 &s->plane[0].band[level][orientation], NULL);
787 /* consume any unused bits from luma */
788 skip_bits_long(gb, get_bits_count(gb) - luma_end);
790 chroma_bits = 8*slice->bytes - 7 - length_bits - luma_bits;
791 chroma_end = get_bits_count(gb) + FFMIN(chroma_bits, get_bits_left(gb));
792 /* [DIRAC_STD] 13.5.5.3 chroma_slice_band */
793 for (level = 0; level < s->wavelet_depth; level++)
794 for (orientation = !!level; orientation < 4; orientation++) {
795 quant = FFMAX(quant_base - s->lowdelay.quant[level][orientation], 0);
796 decode_subband(s, gb, quant, slice->slice_x, slice->slice_y, chroma_end,
797 &s->plane[1].band[level][orientation],
798 &s->plane[2].band[level][orientation]);
805 * VC-2 Specification ->
806 * 13.5.3 hq_slice(sx,sy)
808 static int decode_hq_slice(AVCodecContext *avctx, void *arg)
810 int i, quant, level, orientation, quant_idx;
811 uint8_t quants[MAX_DWT_LEVELS][4];
812 DiracContext *s = avctx->priv_data;
813 DiracSlice *slice = arg;
814 GetBitContext *gb = &slice->gb;
816 skip_bits_long(gb, 8*s->highquality.prefix_bytes);
817 quant_idx = get_bits(gb, 8);
819 /* Slice quantization (slice_quantizers() in the specs) */
820 for (level = 0; level < s->wavelet_depth; level++) {
821 for (orientation = !!level; orientation < 4; orientation++) {
822 quant = FFMAX(quant_idx - s->lowdelay.quant[level][orientation], 0);
823 quants[level][orientation] = quant;
827 /* Luma + 2 Chroma planes */
828 for (i = 0; i < 3; i++) {
829 int length = s->highquality.size_scaler * get_bits(gb, 8);
830 int bits_left = 8 * length;
831 int bits_end = get_bits_count(gb) + bits_left;
832 for (level = 0; level < s->wavelet_depth; level++) {
833 for (orientation = !!level; orientation < 4; orientation++) {
834 decode_subband(s, gb, quants[level][orientation], slice->slice_x, slice->slice_y, bits_end,
835 &s->plane[i].band[level][orientation], NULL);
838 skip_bits_long(gb, bits_end - get_bits_count(gb));
845 * Dirac Specification ->
846 * 13.5.1 low_delay_transform_data()
848 static int decode_lowdelay(DiracContext *s)
850 AVCodecContext *avctx = s->avctx;
851 int slice_x, slice_y, bytes = 0, bufsize;
856 slices = av_mallocz_array(s->num_x, s->num_y * sizeof(DiracSlice));
858 return AVERROR(ENOMEM);
860 align_get_bits(&s->gb);
861 /*[DIRAC_STD] 13.5.2 Slices. slice(sx,sy) */
862 buf = s->gb.buffer + get_bits_count(&s->gb)/8;
863 bufsize = get_bits_left(&s->gb);
868 for (slice_y = 0; bufsize > 0 && slice_y < s->num_y; slice_y++) {
869 for (slice_x = 0; bufsize > 0 && slice_x < s->num_x; slice_x++) {
870 bytes = s->highquality.prefix_bytes + 1;
871 for (i = 0; i < 3; i++) {
872 if (bytes <= bufsize/8)
873 bytes += buf[bytes] * s->highquality.size_scaler + 1;
876 slices[slice_num].bytes = bytes;
877 slices[slice_num].slice_x = slice_x;
878 slices[slice_num].slice_y = slice_y;
879 init_get_bits(&slices[slice_num].gb, buf, bufsize);
883 if (bufsize/8 >= bytes)
889 avctx->execute(avctx, decode_hq_slice, slices, NULL, slice_num,
892 for (slice_y = 0; bufsize > 0 && slice_y < s->num_y; slice_y++) {
893 for (slice_x = 0; bufsize > 0 && slice_x < s->num_x; slice_x++) {
894 bytes = (slice_num+1) * s->lowdelay.bytes.num / s->lowdelay.bytes.den
895 - slice_num * s->lowdelay.bytes.num / s->lowdelay.bytes.den;
896 slices[slice_num].bytes = bytes;
897 slices[slice_num].slice_x = slice_x;
898 slices[slice_num].slice_y = slice_y;
899 init_get_bits(&slices[slice_num].gb, buf, bufsize);
903 if (bufsize/8 >= bytes)
909 avctx->execute(avctx, decode_lowdelay_slice, slices, NULL, slice_num,
910 sizeof(DiracSlice)); /* [DIRAC_STD] 13.5.2 Slices */
913 if (s->dc_prediction) {
915 intra_dc_prediction_10(&s->plane[0].band[0][0]); /* [DIRAC_STD] 13.3 intra_dc_prediction() */
916 intra_dc_prediction_10(&s->plane[1].band[0][0]); /* [DIRAC_STD] 13.3 intra_dc_prediction() */
917 intra_dc_prediction_10(&s->plane[2].band[0][0]); /* [DIRAC_STD] 13.3 intra_dc_prediction() */
919 intra_dc_prediction_8(&s->plane[0].band[0][0]);
920 intra_dc_prediction_8(&s->plane[1].band[0][0]);
921 intra_dc_prediction_8(&s->plane[2].band[0][0]);
928 static void init_planes(DiracContext *s)
930 int i, w, h, level, orientation;
932 for (i = 0; i < 3; i++) {
933 Plane *p = &s->plane[i];
935 p->width = s->seq.width >> (i ? s->chroma_x_shift : 0);
936 p->height = s->seq.height >> (i ? s->chroma_y_shift : 0);
937 p->idwt.width = w = CALC_PADDING(p->width , s->wavelet_depth);
938 p->idwt.height = h = CALC_PADDING(p->height, s->wavelet_depth);
939 p->idwt.stride = FFALIGN(p->idwt.width, 8) << (1 + s->pshift);
941 for (level = s->wavelet_depth-1; level >= 0; level--) {
944 for (orientation = !!level; orientation < 4; orientation++) {
945 SubBand *b = &p->band[level][orientation];
947 b->pshift = s->pshift;
948 b->ibuf = p->idwt.buf;
950 b->stride = p->idwt.stride << (s->wavelet_depth - level);
953 b->orientation = orientation;
956 b->ibuf += w << (1+b->pshift);
958 b->ibuf += (b->stride>>1);
961 b->parent = &p->band[level-1][orientation];
966 p->xblen = s->plane[0].xblen >> s->chroma_x_shift;
967 p->yblen = s->plane[0].yblen >> s->chroma_y_shift;
968 p->xbsep = s->plane[0].xbsep >> s->chroma_x_shift;
969 p->ybsep = s->plane[0].ybsep >> s->chroma_y_shift;
972 p->xoffset = (p->xblen - p->xbsep)/2;
973 p->yoffset = (p->yblen - p->ybsep)/2;
978 * Unpack the motion compensation parameters
979 * Dirac Specification ->
980 * 11.2 Picture prediction data. picture_prediction()
982 static int dirac_unpack_prediction_parameters(DiracContext *s)
984 static const uint8_t default_blen[] = { 4, 12, 16, 24 };
986 GetBitContext *gb = &s->gb;
990 /* [DIRAC_STD] 11.2.2 Block parameters. block_parameters() */
991 /* Luma and Chroma are equal. 11.2.3 */
992 idx = svq3_get_ue_golomb(gb); /* [DIRAC_STD] index */
995 av_log(s->avctx, AV_LOG_ERROR, "Block prediction index too high\n");
996 return AVERROR_INVALIDDATA;
1000 s->plane[0].xblen = svq3_get_ue_golomb(gb);
1001 s->plane[0].yblen = svq3_get_ue_golomb(gb);
1002 s->plane[0].xbsep = svq3_get_ue_golomb(gb);
1003 s->plane[0].ybsep = svq3_get_ue_golomb(gb);
1005 /*[DIRAC_STD] preset_block_params(index). Table 11.1 */
1006 s->plane[0].xblen = default_blen[idx-1];
1007 s->plane[0].yblen = default_blen[idx-1];
1008 s->plane[0].xbsep = 4 * idx;
1009 s->plane[0].ybsep = 4 * idx;
1011 /*[DIRAC_STD] 11.2.4 motion_data_dimensions()
1012 Calculated in function dirac_unpack_block_motion_data */
1014 if (s->plane[0].xblen % (1 << s->chroma_x_shift) != 0 ||
1015 s->plane[0].yblen % (1 << s->chroma_y_shift) != 0 ||
1016 !s->plane[0].xblen || !s->plane[0].yblen) {
1017 av_log(s->avctx, AV_LOG_ERROR,
1018 "invalid x/y block length (%d/%d) for x/y chroma shift (%d/%d)\n",
1019 s->plane[0].xblen, s->plane[0].yblen, s->chroma_x_shift, s->chroma_y_shift);
1020 return AVERROR_INVALIDDATA;
1022 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) {
1023 av_log(s->avctx, AV_LOG_ERROR, "Block separation too small\n");
1024 return AVERROR_INVALIDDATA;
1026 if (s->plane[0].xbsep > s->plane[0].xblen || s->plane[0].ybsep > s->plane[0].yblen) {
1027 av_log(s->avctx, AV_LOG_ERROR, "Block separation greater than size\n");
1028 return AVERROR_INVALIDDATA;
1030 if (FFMAX(s->plane[0].xblen, s->plane[0].yblen) > MAX_BLOCKSIZE) {
1031 av_log(s->avctx, AV_LOG_ERROR, "Unsupported large block size\n");
1032 return AVERROR_PATCHWELCOME;
1035 /*[DIRAC_STD] 11.2.5 Motion vector precision. motion_vector_precision()
1036 Read motion vector precision */
1037 s->mv_precision = svq3_get_ue_golomb(gb);
1038 if (s->mv_precision > 3) {
1039 av_log(s->avctx, AV_LOG_ERROR, "MV precision finer than eighth-pel\n");
1040 return AVERROR_INVALIDDATA;
1043 /*[DIRAC_STD] 11.2.6 Global motion. global_motion()
1044 Read the global motion compensation parameters */
1045 s->globalmc_flag = get_bits1(gb);
1046 if (s->globalmc_flag) {
1047 memset(s->globalmc, 0, sizeof(s->globalmc));
1048 /* [DIRAC_STD] pan_tilt(gparams) */
1049 for (ref = 0; ref < s->num_refs; ref++) {
1050 if (get_bits1(gb)) {
1051 s->globalmc[ref].pan_tilt[0] = dirac_get_se_golomb(gb);
1052 s->globalmc[ref].pan_tilt[1] = dirac_get_se_golomb(gb);
1054 /* [DIRAC_STD] zoom_rotate_shear(gparams)
1055 zoom/rotation/shear parameters */
1056 if (get_bits1(gb)) {
1057 s->globalmc[ref].zrs_exp = svq3_get_ue_golomb(gb);
1058 s->globalmc[ref].zrs[0][0] = dirac_get_se_golomb(gb);
1059 s->globalmc[ref].zrs[0][1] = dirac_get_se_golomb(gb);
1060 s->globalmc[ref].zrs[1][0] = dirac_get_se_golomb(gb);
1061 s->globalmc[ref].zrs[1][1] = dirac_get_se_golomb(gb);
1063 s->globalmc[ref].zrs[0][0] = 1;
1064 s->globalmc[ref].zrs[1][1] = 1;
1066 /* [DIRAC_STD] perspective(gparams) */
1067 if (get_bits1(gb)) {
1068 s->globalmc[ref].perspective_exp = svq3_get_ue_golomb(gb);
1069 s->globalmc[ref].perspective[0] = dirac_get_se_golomb(gb);
1070 s->globalmc[ref].perspective[1] = dirac_get_se_golomb(gb);
1075 /*[DIRAC_STD] 11.2.7 Picture prediction mode. prediction_mode()
1076 Picture prediction mode, not currently used. */
1077 if (svq3_get_ue_golomb(gb)) {
1078 av_log(s->avctx, AV_LOG_ERROR, "Unknown picture prediction mode\n");
1079 return AVERROR_INVALIDDATA;
1082 /* [DIRAC_STD] 11.2.8 Reference picture weight. reference_picture_weights()
1083 just data read, weight calculation will be done later on. */
1084 s->weight_log2denom = 1;
1088 if (get_bits1(gb)) {
1089 s->weight_log2denom = svq3_get_ue_golomb(gb);
1090 s->weight[0] = dirac_get_se_golomb(gb);
1091 if (s->num_refs == 2)
1092 s->weight[1] = dirac_get_se_golomb(gb);
1098 * Dirac Specification ->
1099 * 11.3 Wavelet transform data. wavelet_transform()
1101 static int dirac_unpack_idwt_params(DiracContext *s)
1103 GetBitContext *gb = &s->gb;
1107 #define CHECKEDREAD(dst, cond, errmsg) \
1108 tmp = svq3_get_ue_golomb(gb); \
1110 av_log(s->avctx, AV_LOG_ERROR, errmsg); \
1111 return AVERROR_INVALIDDATA; \
1117 s->zero_res = s->num_refs ? get_bits1(gb) : 0;
1121 /*[DIRAC_STD] 11.3.1 Transform parameters. transform_parameters() */
1122 CHECKEDREAD(s->wavelet_idx, tmp > 6, "wavelet_idx is too big\n")
1124 CHECKEDREAD(s->wavelet_depth, tmp > MAX_DWT_LEVELS || tmp < 1, "invalid number of DWT decompositions\n")
1126 if (!s->low_delay) {
1127 /* Codeblock parameters (core syntax only) */
1128 if (get_bits1(gb)) {
1129 for (i = 0; i <= s->wavelet_depth; i++) {
1130 CHECKEDREAD(s->codeblock[i].width , tmp < 1 || tmp > (s->avctx->width >>s->wavelet_depth-i), "codeblock width invalid\n")
1131 CHECKEDREAD(s->codeblock[i].height, tmp < 1 || tmp > (s->avctx->height>>s->wavelet_depth-i), "codeblock height invalid\n")
1134 CHECKEDREAD(s->codeblock_mode, tmp > 1, "unknown codeblock mode\n")
1137 for (i = 0; i <= s->wavelet_depth; i++)
1138 s->codeblock[i].width = s->codeblock[i].height = 1;
1142 s->num_x = svq3_get_ue_golomb(gb);
1143 s->num_y = svq3_get_ue_golomb(gb);
1144 if (s->ld_picture) {
1145 s->lowdelay.bytes.num = svq3_get_ue_golomb(gb);
1146 s->lowdelay.bytes.den = svq3_get_ue_golomb(gb);
1147 if (s->lowdelay.bytes.den <= 0) {
1148 av_log(s->avctx,AV_LOG_ERROR,"Invalid lowdelay.bytes.den\n");
1149 return AVERROR_INVALIDDATA;
1151 } else if (s->hq_picture) {
1152 s->highquality.prefix_bytes = svq3_get_ue_golomb(gb);
1153 s->highquality.size_scaler = svq3_get_ue_golomb(gb);
1156 /* [DIRAC_STD] 11.3.5 Quantisation matrices (low-delay syntax). quant_matrix() */
1157 if (get_bits1(gb)) {
1158 av_log(s->avctx,AV_LOG_DEBUG,"Low Delay: Has Custom Quantization Matrix!\n");
1159 /* custom quantization matrix */
1160 s->lowdelay.quant[0][0] = svq3_get_ue_golomb(gb);
1161 for (level = 0; level < s->wavelet_depth; level++) {
1162 s->lowdelay.quant[level][1] = svq3_get_ue_golomb(gb);
1163 s->lowdelay.quant[level][2] = svq3_get_ue_golomb(gb);
1164 s->lowdelay.quant[level][3] = svq3_get_ue_golomb(gb);
1167 if (s->wavelet_depth > 4) {
1168 av_log(s->avctx,AV_LOG_ERROR,"Mandatory custom low delay matrix missing for depth %d\n", s->wavelet_depth);
1169 return AVERROR_INVALIDDATA;
1171 /* default quantization matrix */
1172 for (level = 0; level < s->wavelet_depth; level++)
1173 for (i = 0; i < 4; i++) {
1174 s->lowdelay.quant[level][i] = ff_dirac_default_qmat[s->wavelet_idx][level][i];
1175 /* haar with no shift differs for different depths */
1176 if (s->wavelet_idx == 3)
1177 s->lowdelay.quant[level][i] += 4*(s->wavelet_depth-1 - level);
1184 static inline int pred_sbsplit(uint8_t *sbsplit, int stride, int x, int y)
1186 static const uint8_t avgsplit[7] = { 0, 0, 1, 1, 1, 2, 2 };
1193 return sbsplit[-stride];
1195 return avgsplit[sbsplit[-1] + sbsplit[-stride] + sbsplit[-stride-1]];
1198 static inline int pred_block_mode(DiracBlock *block, int stride, int x, int y, int refmask)
1205 return block[-1].ref & refmask;
1207 return block[-stride].ref & refmask;
1209 /* return the majority */
1210 pred = (block[-1].ref & refmask) + (block[-stride].ref & refmask) + (block[-stride-1].ref & refmask);
1211 return (pred >> 1) & refmask;
1214 static inline void pred_block_dc(DiracBlock *block, int stride, int x, int y)
1218 memset(block->u.dc, 0, sizeof(block->u.dc));
1220 if (x && !(block[-1].ref & 3)) {
1221 for (i = 0; i < 3; i++)
1222 block->u.dc[i] += block[-1].u.dc[i];
1226 if (y && !(block[-stride].ref & 3)) {
1227 for (i = 0; i < 3; i++)
1228 block->u.dc[i] += block[-stride].u.dc[i];
1232 if (x && y && !(block[-1-stride].ref & 3)) {
1233 for (i = 0; i < 3; i++)
1234 block->u.dc[i] += block[-1-stride].u.dc[i];
1239 for (i = 0; i < 3; i++)
1240 block->u.dc[i] = (block->u.dc[i]+1)>>1;
1241 } else if (n == 3) {
1242 for (i = 0; i < 3; i++)
1243 block->u.dc[i] = divide3(block->u.dc[i]);
1247 static inline void pred_mv(DiracBlock *block, int stride, int x, int y, int ref)
1250 int refmask = ref+1;
1251 int mask = refmask | DIRAC_REF_MASK_GLOBAL; /* exclude gmc blocks */
1254 if (x && (block[-1].ref & mask) == refmask)
1255 pred[n++] = block[-1].u.mv[ref];
1257 if (y && (block[-stride].ref & mask) == refmask)
1258 pred[n++] = block[-stride].u.mv[ref];
1260 if (x && y && (block[-stride-1].ref & mask) == refmask)
1261 pred[n++] = block[-stride-1].u.mv[ref];
1265 block->u.mv[ref][0] = 0;
1266 block->u.mv[ref][1] = 0;
1269 block->u.mv[ref][0] = pred[0][0];
1270 block->u.mv[ref][1] = pred[0][1];
1273 block->u.mv[ref][0] = (pred[0][0] + pred[1][0] + 1) >> 1;
1274 block->u.mv[ref][1] = (pred[0][1] + pred[1][1] + 1) >> 1;
1277 block->u.mv[ref][0] = mid_pred(pred[0][0], pred[1][0], pred[2][0]);
1278 block->u.mv[ref][1] = mid_pred(pred[0][1], pred[1][1], pred[2][1]);
1283 static void global_mv(DiracContext *s, DiracBlock *block, int x, int y, int ref)
1285 int ez = s->globalmc[ref].zrs_exp;
1286 int ep = s->globalmc[ref].perspective_exp;
1287 int (*A)[2] = s->globalmc[ref].zrs;
1288 int *b = s->globalmc[ref].pan_tilt;
1289 int *c = s->globalmc[ref].perspective;
1291 int m = (1<<ep) - (c[0]*x + c[1]*y);
1292 int mx = m * ((A[0][0] * x + A[0][1]*y) + (1<<ez) * b[0]);
1293 int my = m * ((A[1][0] * x + A[1][1]*y) + (1<<ez) * b[1]);
1295 block->u.mv[ref][0] = (mx + (1<<(ez+ep))) >> (ez+ep);
1296 block->u.mv[ref][1] = (my + (1<<(ez+ep))) >> (ez+ep);
1299 static void decode_block_params(DiracContext *s, DiracArith arith[8], DiracBlock *block,
1300 int stride, int x, int y)
1304 block->ref = pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_REF1);
1305 block->ref ^= dirac_get_arith_bit(arith, CTX_PMODE_REF1);
1307 if (s->num_refs == 2) {
1308 block->ref |= pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_REF2);
1309 block->ref ^= dirac_get_arith_bit(arith, CTX_PMODE_REF2) << 1;
1313 pred_block_dc(block, stride, x, y);
1314 for (i = 0; i < 3; i++)
1315 block->u.dc[i] += dirac_get_arith_int(arith+1+i, CTX_DC_F1, CTX_DC_DATA);
1319 if (s->globalmc_flag) {
1320 block->ref |= pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_GLOBAL);
1321 block->ref ^= dirac_get_arith_bit(arith, CTX_GLOBAL_BLOCK) << 2;
1324 for (i = 0; i < s->num_refs; i++)
1325 if (block->ref & (i+1)) {
1326 if (block->ref & DIRAC_REF_MASK_GLOBAL) {
1327 global_mv(s, block, x, y, i);
1329 pred_mv(block, stride, x, y, i);
1330 block->u.mv[i][0] += dirac_get_arith_int(arith + 4 + 2 * i, CTX_MV_F1, CTX_MV_DATA);
1331 block->u.mv[i][1] += dirac_get_arith_int(arith + 5 + 2 * i, CTX_MV_F1, CTX_MV_DATA);
1337 * Copies the current block to the other blocks covered by the current superblock split mode
1339 static void propagate_block_data(DiracBlock *block, int stride, int size)
1342 DiracBlock *dst = block;
1344 for (x = 1; x < size; x++)
1347 for (y = 1; y < size; y++) {
1349 for (x = 0; x < size; x++)
1355 * Dirac Specification ->
1356 * 12. Block motion data syntax
1358 static int dirac_unpack_block_motion_data(DiracContext *s)
1360 GetBitContext *gb = &s->gb;
1361 uint8_t *sbsplit = s->sbsplit;
1363 DiracArith arith[8];
1367 /* [DIRAC_STD] 11.2.4 and 12.2.1 Number of blocks and superblocks */
1368 s->sbwidth = DIVRNDUP(s->seq.width, 4*s->plane[0].xbsep);
1369 s->sbheight = DIVRNDUP(s->seq.height, 4*s->plane[0].ybsep);
1370 s->blwidth = 4 * s->sbwidth;
1371 s->blheight = 4 * s->sbheight;
1373 /* [DIRAC_STD] 12.3.1 Superblock splitting modes. superblock_split_modes()
1374 decode superblock split modes */
1375 ff_dirac_init_arith_decoder(arith, gb, svq3_get_ue_golomb(gb)); /* svq3_get_ue_golomb(gb) is the length */
1376 for (y = 0; y < s->sbheight; y++) {
1377 for (x = 0; x < s->sbwidth; x++) {
1378 unsigned int split = dirac_get_arith_uint(arith, CTX_SB_F1, CTX_SB_DATA);
1380 return AVERROR_INVALIDDATA;
1381 sbsplit[x] = (split + pred_sbsplit(sbsplit+x, s->sbwidth, x, y)) % 3;
1383 sbsplit += s->sbwidth;
1386 /* setup arith decoding */
1387 ff_dirac_init_arith_decoder(arith, gb, svq3_get_ue_golomb(gb));
1388 for (i = 0; i < s->num_refs; i++) {
1389 ff_dirac_init_arith_decoder(arith + 4 + 2 * i, gb, svq3_get_ue_golomb(gb));
1390 ff_dirac_init_arith_decoder(arith + 5 + 2 * i, gb, svq3_get_ue_golomb(gb));
1392 for (i = 0; i < 3; i++)
1393 ff_dirac_init_arith_decoder(arith+1+i, gb, svq3_get_ue_golomb(gb));
1395 for (y = 0; y < s->sbheight; y++)
1396 for (x = 0; x < s->sbwidth; x++) {
1397 int blkcnt = 1 << s->sbsplit[y * s->sbwidth + x];
1398 int step = 4 >> s->sbsplit[y * s->sbwidth + x];
1400 for (q = 0; q < blkcnt; q++)
1401 for (p = 0; p < blkcnt; p++) {
1402 int bx = 4 * x + p*step;
1403 int by = 4 * y + q*step;
1404 DiracBlock *block = &s->blmotion[by*s->blwidth + bx];
1405 decode_block_params(s, arith, block, s->blwidth, bx, by);
1406 propagate_block_data(block, s->blwidth, step);
1413 static int weight(int i, int blen, int offset)
1415 #define ROLLOFF(i) offset == 1 ? ((i) ? 5 : 3) : \
1416 (1 + (6*(i) + offset - 1) / (2*offset - 1))
1420 else if (i > blen-1 - 2*offset)
1421 return ROLLOFF(blen-1 - i);
1425 static void init_obmc_weight_row(Plane *p, uint8_t *obmc_weight, int stride,
1426 int left, int right, int wy)
1429 for (x = 0; left && x < p->xblen >> 1; x++)
1430 obmc_weight[x] = wy*8;
1431 for (; x < p->xblen >> right; x++)
1432 obmc_weight[x] = wy*weight(x, p->xblen, p->xoffset);
1433 for (; x < p->xblen; x++)
1434 obmc_weight[x] = wy*8;
1435 for (; x < stride; x++)
1439 static void init_obmc_weight(Plane *p, uint8_t *obmc_weight, int stride,
1440 int left, int right, int top, int bottom)
1443 for (y = 0; top && y < p->yblen >> 1; y++) {
1444 init_obmc_weight_row(p, obmc_weight, stride, left, right, 8);
1445 obmc_weight += stride;
1447 for (; y < p->yblen >> bottom; y++) {
1448 int wy = weight(y, p->yblen, p->yoffset);
1449 init_obmc_weight_row(p, obmc_weight, stride, left, right, wy);
1450 obmc_weight += stride;
1452 for (; y < p->yblen; y++) {
1453 init_obmc_weight_row(p, obmc_weight, stride, left, right, 8);
1454 obmc_weight += stride;
1458 static void init_obmc_weights(DiracContext *s, Plane *p, int by)
1461 int bottom = by == s->blheight-1;
1463 /* don't bother re-initing for rows 2 to blheight-2, the weights don't change */
1464 if (top || bottom || by == 1) {
1465 init_obmc_weight(p, s->obmc_weight[0], MAX_BLOCKSIZE, 1, 0, top, bottom);
1466 init_obmc_weight(p, s->obmc_weight[1], MAX_BLOCKSIZE, 0, 0, top, bottom);
1467 init_obmc_weight(p, s->obmc_weight[2], MAX_BLOCKSIZE, 0, 1, top, bottom);
1471 static const uint8_t epel_weights[4][4][4] = {
1491 * For block x,y, determine which of the hpel planes to do bilinear
1492 * interpolation from and set src[] to the location in each hpel plane
1495 * @return the index of the put_dirac_pixels_tab function to use
1496 * 0 for 1 plane (fpel,hpel), 1 for 2 planes (qpel), 2 for 4 planes (qpel), and 3 for epel
1498 static int mc_subpel(DiracContext *s, DiracBlock *block, const uint8_t *src[5],
1499 int x, int y, int ref, int plane)
1501 Plane *p = &s->plane[plane];
1502 uint8_t **ref_hpel = s->ref_pics[ref]->hpel[plane];
1503 int motion_x = block->u.mv[ref][0];
1504 int motion_y = block->u.mv[ref][1];
1505 int mx, my, i, epel, nplanes = 0;
1508 motion_x >>= s->chroma_x_shift;
1509 motion_y >>= s->chroma_y_shift;
1512 mx = motion_x & ~(-1U << s->mv_precision);
1513 my = motion_y & ~(-1U << s->mv_precision);
1514 motion_x >>= s->mv_precision;
1515 motion_y >>= s->mv_precision;
1516 /* normalize subpel coordinates to epel */
1517 /* TODO: template this function? */
1518 mx <<= 3 - s->mv_precision;
1519 my <<= 3 - s->mv_precision;
1528 src[0] = ref_hpel[(my>>1)+(mx>>2)] + y*p->stride + x;
1532 for (i = 0; i < 4; i++)
1533 src[i] = ref_hpel[i] + y*p->stride + x;
1535 /* if we're interpolating in the right/bottom halves, adjust the planes as needed
1536 we increment x/y because the edge changes for half of the pixels */
1543 src[0] += p->stride;
1544 src[1] += p->stride;
1552 /* check if we really only need 2 planes since either mx or my is
1553 a hpel position. (epel weights of 0 handle this there) */
1555 /* mx == 0: average [0] and [2]
1556 mx == 4: average [1] and [3] */
1557 src[!mx] = src[2 + !!mx];
1559 } else if (!(my&3)) {
1560 src[0] = src[(my>>1) ];
1561 src[1] = src[(my>>1)+1];
1565 /* adjust the ordering if needed so the weights work */
1567 FFSWAP(const uint8_t *, src[0], src[1]);
1568 FFSWAP(const uint8_t *, src[2], src[3]);
1571 FFSWAP(const uint8_t *, src[0], src[2]);
1572 FFSWAP(const uint8_t *, src[1], src[3]);
1574 src[4] = epel_weights[my&3][mx&3];
1578 /* fixme: v/h _edge_pos */
1579 if (x + p->xblen > p->width +EDGE_WIDTH/2 ||
1580 y + p->yblen > p->height+EDGE_WIDTH/2 ||
1582 for (i = 0; i < nplanes; i++) {
1583 s->vdsp.emulated_edge_mc(s->edge_emu_buffer[i], src[i],
1584 p->stride, p->stride,
1585 p->xblen, p->yblen, x, y,
1586 p->width+EDGE_WIDTH/2, p->height+EDGE_WIDTH/2);
1587 src[i] = s->edge_emu_buffer[i];
1590 return (nplanes>>1) + epel;
1593 static void add_dc(uint16_t *dst, int dc, int stride,
1594 uint8_t *obmc_weight, int xblen, int yblen)
1599 for (y = 0; y < yblen; y++) {
1600 for (x = 0; x < xblen; x += 2) {
1601 dst[x ] += dc * obmc_weight[x ];
1602 dst[x+1] += dc * obmc_weight[x+1];
1605 obmc_weight += MAX_BLOCKSIZE;
1609 static void block_mc(DiracContext *s, DiracBlock *block,
1610 uint16_t *mctmp, uint8_t *obmc_weight,
1611 int plane, int dstx, int dsty)
1613 Plane *p = &s->plane[plane];
1614 const uint8_t *src[5];
1617 switch (block->ref&3) {
1619 add_dc(mctmp, block->u.dc[plane], p->stride, obmc_weight, p->xblen, p->yblen);
1623 idx = mc_subpel(s, block, src, dstx, dsty, (block->ref&3)-1, plane);
1624 s->put_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen);
1626 s->weight_func(s->mcscratch, p->stride, s->weight_log2denom,
1627 s->weight[0] + s->weight[1], p->yblen);
1630 idx = mc_subpel(s, block, src, dstx, dsty, 0, plane);
1631 s->put_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen);
1632 idx = mc_subpel(s, block, src, dstx, dsty, 1, plane);
1633 if (s->biweight_func) {
1634 /* fixme: +32 is a quick hack */
1635 s->put_pixels_tab[idx](s->mcscratch + 32, src, p->stride, p->yblen);
1636 s->biweight_func(s->mcscratch, s->mcscratch+32, p->stride, s->weight_log2denom,
1637 s->weight[0], s->weight[1], p->yblen);
1639 s->avg_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen);
1642 s->add_obmc(mctmp, s->mcscratch, p->stride, obmc_weight, p->yblen);
1645 static void mc_row(DiracContext *s, DiracBlock *block, uint16_t *mctmp, int plane, int dsty)
1647 Plane *p = &s->plane[plane];
1648 int x, dstx = p->xbsep - p->xoffset;
1650 block_mc(s, block, mctmp, s->obmc_weight[0], plane, -p->xoffset, dsty);
1653 for (x = 1; x < s->blwidth-1; x++) {
1654 block_mc(s, block+x, mctmp, s->obmc_weight[1], plane, dstx, dsty);
1658 block_mc(s, block+x, mctmp, s->obmc_weight[2], plane, dstx, dsty);
1661 static void select_dsp_funcs(DiracContext *s, int width, int height, int xblen, int yblen)
1669 memcpy(s->put_pixels_tab, s->diracdsp.put_dirac_pixels_tab[idx], sizeof(s->put_pixels_tab));
1670 memcpy(s->avg_pixels_tab, s->diracdsp.avg_dirac_pixels_tab[idx], sizeof(s->avg_pixels_tab));
1671 s->add_obmc = s->diracdsp.add_dirac_obmc[idx];
1672 if (s->weight_log2denom > 1 || s->weight[0] != 1 || s->weight[1] != 1) {
1673 s->weight_func = s->diracdsp.weight_dirac_pixels_tab[idx];
1674 s->biweight_func = s->diracdsp.biweight_dirac_pixels_tab[idx];
1676 s->weight_func = NULL;
1677 s->biweight_func = NULL;
1681 static int interpolate_refplane(DiracContext *s, DiracFrame *ref, int plane, int width, int height)
1683 /* chroma allocates an edge of 8 when subsampled
1684 which for 4:2:2 means an h edge of 16 and v edge of 8
1685 just use 8 for everything for the moment */
1686 int i, edge = EDGE_WIDTH/2;
1688 ref->hpel[plane][0] = ref->avframe->data[plane];
1689 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 */
1691 /* no need for hpel if we only have fpel vectors */
1692 if (!s->mv_precision)
1695 for (i = 1; i < 4; i++) {
1696 if (!ref->hpel_base[plane][i])
1697 ref->hpel_base[plane][i] = av_malloc((height+2*edge) * ref->avframe->linesize[plane] + 32);
1698 if (!ref->hpel_base[plane][i]) {
1699 return AVERROR(ENOMEM);
1701 /* we need to be 16-byte aligned even for chroma */
1702 ref->hpel[plane][i] = ref->hpel_base[plane][i] + edge*ref->avframe->linesize[plane] + 16;
1705 if (!ref->interpolated[plane]) {
1706 s->diracdsp.dirac_hpel_filter(ref->hpel[plane][1], ref->hpel[plane][2],
1707 ref->hpel[plane][3], ref->hpel[plane][0],
1708 ref->avframe->linesize[plane], width, height);
1709 s->mpvencdsp.draw_edges(ref->hpel[plane][1], ref->avframe->linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
1710 s->mpvencdsp.draw_edges(ref->hpel[plane][2], ref->avframe->linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
1711 s->mpvencdsp.draw_edges(ref->hpel[plane][3], ref->avframe->linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
1713 ref->interpolated[plane] = 1;
1719 * Dirac Specification ->
1720 * 13.0 Transform data syntax. transform_data()
1722 static int dirac_decode_frame_internal(DiracContext *s)
1725 int y, i, comp, dsty;
1729 /* [DIRAC_STD] 13.5.1 low_delay_transform_data() */
1730 for (comp = 0; comp < 3; comp++) {
1731 Plane *p = &s->plane[comp];
1732 memset(p->idwt.buf, 0, p->idwt.stride * p->idwt.height);
1735 if ((ret = decode_lowdelay(s)) < 0)
1740 for (comp = 0; comp < 3; comp++) {
1741 Plane *p = &s->plane[comp];
1742 uint8_t *frame = s->current_picture->avframe->data[comp];
1744 /* FIXME: small resolutions */
1745 for (i = 0; i < 4; i++)
1746 s->edge_emu_buffer[i] = s->edge_emu_buffer_base + i*FFALIGN(p->width, 16);
1748 if (!s->zero_res && !s->low_delay)
1750 memset(p->idwt.buf, 0, p->idwt.stride * p->idwt.height);
1751 decode_component(s, comp); /* [DIRAC_STD] 13.4.1 core_transform_data() */
1753 ret = ff_spatial_idwt_init(&d, &p->idwt, s->wavelet_idx+2,
1754 s->wavelet_depth, s->bit_depth);
1758 if (!s->num_refs) { /* intra */
1759 for (y = 0; y < p->height; y += 16) {
1760 int idx = (s->bit_depth - 8) >> 1;
1761 ff_spatial_idwt_slice2(&d, y+16); /* decode */
1762 s->diracdsp.put_signed_rect_clamped[idx](frame + y*p->stride,
1764 p->idwt.buf + y*p->idwt.stride,
1765 p->idwt.stride, p->width, 16);
1767 } else { /* inter */
1768 int rowheight = p->ybsep*p->stride;
1770 select_dsp_funcs(s, p->width, p->height, p->xblen, p->yblen);
1772 for (i = 0; i < s->num_refs; i++) {
1773 int ret = interpolate_refplane(s, s->ref_pics[i], comp, p->width, p->height);
1778 memset(s->mctmp, 0, 4*p->yoffset*p->stride);
1781 for (y = 0; y < s->blheight; y++) {
1783 start = FFMAX(dsty, 0);
1784 uint16_t *mctmp = s->mctmp + y*rowheight;
1785 DiracBlock *blocks = s->blmotion + y*s->blwidth;
1787 init_obmc_weights(s, p, y);
1789 if (y == s->blheight-1 || start+p->ybsep > p->height)
1790 h = p->height - start;
1792 h = p->ybsep - (start - dsty);
1796 memset(mctmp+2*p->yoffset*p->stride, 0, 2*rowheight);
1797 mc_row(s, blocks, mctmp, comp, dsty);
1799 mctmp += (start - dsty)*p->stride + p->xoffset;
1800 ff_spatial_idwt_slice2(&d, start + h); /* decode */
1801 /* NOTE: add_rect_clamped hasn't been templated hence the shifts.
1802 * idwt.stride is passed as pixels, not in bytes as in the rest of the decoder */
1803 s->diracdsp.add_rect_clamped(frame + start*p->stride, mctmp, p->stride,
1804 (int16_t*)(p->idwt.buf) + start*(p->idwt.stride >> 1), (p->idwt.stride >> 1), p->width, h);
1815 static int get_buffer_with_edge(AVCodecContext *avctx, AVFrame *f, int flags)
1818 int chroma_x_shift, chroma_y_shift;
1819 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_x_shift, &chroma_y_shift);
1821 f->width = avctx->width + 2 * EDGE_WIDTH;
1822 f->height = avctx->height + 2 * EDGE_WIDTH + 2;
1823 ret = ff_get_buffer(avctx, f, flags);
1827 for (i = 0; f->data[i]; i++) {
1828 int offset = (EDGE_WIDTH >> (i && i<3 ? chroma_y_shift : 0)) *
1829 f->linesize[i] + 32;
1830 f->data[i] += offset;
1832 f->width = avctx->width;
1833 f->height = avctx->height;
1839 * Dirac Specification ->
1840 * 11.1.1 Picture Header. picture_header()
1842 static int dirac_decode_picture_header(DiracContext *s)
1844 unsigned retire, picnum;
1846 int64_t refdist, refnum;
1847 GetBitContext *gb = &s->gb;
1849 /* [DIRAC_STD] 11.1.1 Picture Header. picture_header() PICTURE_NUM */
1850 picnum = s->current_picture->avframe->display_picture_number = get_bits_long(gb, 32);
1853 av_log(s->avctx,AV_LOG_DEBUG,"PICTURE_NUM: %d\n",picnum);
1855 /* if this is the first keyframe after a sequence header, start our
1856 reordering from here */
1857 if (s->frame_number < 0)
1858 s->frame_number = picnum;
1860 s->ref_pics[0] = s->ref_pics[1] = NULL;
1861 for (i = 0; i < s->num_refs; i++) {
1862 refnum = (picnum + dirac_get_se_golomb(gb)) & 0xFFFFFFFF;
1863 refdist = INT64_MAX;
1865 /* find the closest reference to the one we want */
1866 /* Jordi: this is needed if the referenced picture hasn't yet arrived */
1867 for (j = 0; j < MAX_REFERENCE_FRAMES && refdist; j++)
1868 if (s->ref_frames[j]
1869 && FFABS(s->ref_frames[j]->avframe->display_picture_number - refnum) < refdist) {
1870 s->ref_pics[i] = s->ref_frames[j];
1871 refdist = FFABS(s->ref_frames[j]->avframe->display_picture_number - refnum);
1874 if (!s->ref_pics[i] || refdist)
1875 av_log(s->avctx, AV_LOG_DEBUG, "Reference not found\n");
1877 /* if there were no references at all, allocate one */
1878 if (!s->ref_pics[i])
1879 for (j = 0; j < MAX_FRAMES; j++)
1880 if (!s->all_frames[j].avframe->data[0]) {
1881 s->ref_pics[i] = &s->all_frames[j];
1882 get_buffer_with_edge(s->avctx, s->ref_pics[i]->avframe, AV_GET_BUFFER_FLAG_REF);
1886 if (!s->ref_pics[i]) {
1887 av_log(s->avctx, AV_LOG_ERROR, "Reference could not be allocated\n");
1888 return AVERROR_INVALIDDATA;
1893 /* retire the reference frames that are not used anymore */
1894 if (s->current_picture->reference) {
1895 retire = (picnum + dirac_get_se_golomb(gb)) & 0xFFFFFFFF;
1896 if (retire != picnum) {
1897 DiracFrame *retire_pic = remove_frame(s->ref_frames, retire);
1900 retire_pic->reference &= DELAYED_PIC_REF;
1902 av_log(s->avctx, AV_LOG_DEBUG, "Frame to retire not found\n");
1905 /* if reference array is full, remove the oldest as per the spec */
1906 while (add_frame(s->ref_frames, MAX_REFERENCE_FRAMES, s->current_picture)) {
1907 av_log(s->avctx, AV_LOG_ERROR, "Reference frame overflow\n");
1908 remove_frame(s->ref_frames, s->ref_frames[0]->avframe->display_picture_number)->reference &= DELAYED_PIC_REF;
1913 ret = dirac_unpack_prediction_parameters(s); /* [DIRAC_STD] 11.2 Picture Prediction Data. picture_prediction() */
1916 ret = dirac_unpack_block_motion_data(s); /* [DIRAC_STD] 12. Block motion data syntax */
1920 ret = dirac_unpack_idwt_params(s); /* [DIRAC_STD] 11.3 Wavelet transform data */
1928 static int get_delayed_pic(DiracContext *s, AVFrame *picture, int *got_frame)
1930 DiracFrame *out = s->delay_frames[0];
1934 /* find frame with lowest picture number */
1935 for (i = 1; s->delay_frames[i]; i++)
1936 if (s->delay_frames[i]->avframe->display_picture_number < out->avframe->display_picture_number) {
1937 out = s->delay_frames[i];
1941 for (i = out_idx; s->delay_frames[i]; i++)
1942 s->delay_frames[i] = s->delay_frames[i+1];
1945 out->reference ^= DELAYED_PIC_REF;
1947 if((ret = av_frame_ref(picture, out->avframe)) < 0)
1955 * Dirac Specification ->
1956 * 9.6 Parse Info Header Syntax. parse_info()
1957 * 4 byte start code + byte parse code + 4 byte size + 4 byte previous size
1959 #define DATA_UNIT_HEADER_SIZE 13
1961 /* [DIRAC_STD] dirac_decode_data_unit makes reference to the while defined in 9.3
1962 inside the function parse_sequence() */
1963 static int dirac_decode_data_unit(AVCodecContext *avctx, const uint8_t *buf, int size)
1965 DiracContext *s = avctx->priv_data;
1966 DiracFrame *pic = NULL;
1967 AVDiracSeqHeader *dsh;
1972 if (size < DATA_UNIT_HEADER_SIZE)
1973 return AVERROR_INVALIDDATA;
1975 parse_code = buf[4];
1977 init_get_bits(&s->gb, &buf[13], 8*(size - DATA_UNIT_HEADER_SIZE));
1979 if (parse_code == DIRAC_PCODE_SEQ_HEADER) {
1980 if (s->seen_sequence_header)
1983 /* [DIRAC_STD] 10. Sequence header */
1984 ret = av_dirac_parse_sequence_header(&dsh, buf + DATA_UNIT_HEADER_SIZE, size - DATA_UNIT_HEADER_SIZE, avctx);
1986 av_log(avctx, AV_LOG_ERROR, "error parsing sequence header");
1990 ret = ff_set_dimensions(avctx, dsh->width, dsh->height);
1996 ff_set_sar(avctx, dsh->sample_aspect_ratio);
1997 avctx->pix_fmt = dsh->pix_fmt;
1998 avctx->color_range = dsh->color_range;
1999 avctx->color_trc = dsh->color_trc;
2000 avctx->color_primaries = dsh->color_primaries;
2001 avctx->colorspace = dsh->colorspace;
2002 avctx->profile = dsh->profile;
2003 avctx->level = dsh->level;
2004 avctx->framerate = dsh->framerate;
2005 s->bit_depth = dsh->bit_depth;
2006 s->version.major = dsh->version.major;
2007 s->version.minor = dsh->version.minor;
2011 s->pshift = s->bit_depth > 8;
2013 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
2015 ret = alloc_sequence_buffers(s);
2019 s->seen_sequence_header = 1;
2020 } else if (parse_code == DIRAC_PCODE_END_SEQ) { /* [DIRAC_STD] End of Sequence */
2021 free_sequence_buffers(s);
2022 s->seen_sequence_header = 0;
2023 } else if (parse_code == DIRAC_PCODE_AUX) {
2024 if (buf[13] == 1) { /* encoder implementation/version */
2026 /* versions older than 1.0.8 don't store quant delta for
2027 subbands with only one codeblock */
2028 if (sscanf(buf+14, "Schroedinger %d.%d.%d", ver, ver+1, ver+2) == 3)
2029 if (ver[0] == 1 && ver[1] == 0 && ver[2] <= 7)
2030 s->old_delta_quant = 1;
2032 } else if (parse_code & 0x8) { /* picture data unit */
2033 if (!s->seen_sequence_header) {
2034 av_log(avctx, AV_LOG_DEBUG, "Dropping frame without sequence header\n");
2035 return AVERROR_INVALIDDATA;
2038 /* find an unused frame */
2039 for (i = 0; i < MAX_FRAMES; i++)
2040 if (s->all_frames[i].avframe->data[0] == NULL)
2041 pic = &s->all_frames[i];
2043 av_log(avctx, AV_LOG_ERROR, "framelist full\n");
2044 return AVERROR_INVALIDDATA;
2047 av_frame_unref(pic->avframe);
2049 /* [DIRAC_STD] Defined in 9.6.1 ... */
2050 tmp = parse_code & 0x03; /* [DIRAC_STD] num_refs() */
2052 av_log(avctx, AV_LOG_ERROR, "num_refs of 3\n");
2053 return AVERROR_INVALIDDATA;
2056 s->is_arith = (parse_code & 0x48) == 0x08; /* [DIRAC_STD] using_ac() */
2057 s->low_delay = (parse_code & 0x88) == 0x88; /* [DIRAC_STD] is_low_delay() */
2058 s->core_syntax = (parse_code & 0x88) == 0x08; /* [DIRAC_STD] is_core_syntax() */
2059 s->ld_picture = (parse_code & 0xF8) == 0xC8; /* [DIRAC_STD] is_ld_picture() */
2060 s->hq_picture = (parse_code & 0xF8) == 0xE8; /* [DIRAC_STD] is_hq_picture() */
2061 s->dc_prediction = (parse_code & 0x28) == 0x08; /* [DIRAC_STD] using_dc_prediction() */
2062 pic->reference = (parse_code & 0x0C) == 0x0C; /* [DIRAC_STD] is_reference() */
2063 pic->avframe->key_frame = s->num_refs == 0; /* [DIRAC_STD] is_intra() */
2064 pic->avframe->pict_type = s->num_refs + 1; /* Definition of AVPictureType in avutil.h */
2066 /* VC-2 Low Delay has a different parse code than the Dirac Low Delay */
2067 if (s->version.minor == 2 && parse_code == 0x88)
2070 if (s->low_delay && !(s->ld_picture || s->hq_picture) ) {
2071 av_log(avctx, AV_LOG_ERROR, "Invalid low delay flag\n");
2072 return AVERROR_INVALIDDATA;
2075 if ((ret = get_buffer_with_edge(avctx, pic->avframe, (parse_code & 0x0C) == 0x0C ? AV_GET_BUFFER_FLAG_REF : 0)) < 0)
2077 s->current_picture = pic;
2078 s->plane[0].stride = pic->avframe->linesize[0];
2079 s->plane[1].stride = pic->avframe->linesize[1];
2080 s->plane[2].stride = pic->avframe->linesize[2];
2082 if (alloc_buffers(s, FFMAX3(FFABS(s->plane[0].stride), FFABS(s->plane[1].stride), FFABS(s->plane[2].stride))) < 0)
2083 return AVERROR(ENOMEM);
2085 /* [DIRAC_STD] 11.1 Picture parse. picture_parse() */
2086 ret = dirac_decode_picture_header(s);
2090 /* [DIRAC_STD] 13.0 Transform data syntax. transform_data() */
2091 ret = dirac_decode_frame_internal(s);
2098 static int dirac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
2100 DiracContext *s = avctx->priv_data;
2101 AVFrame *picture = data;
2102 uint8_t *buf = pkt->data;
2103 int buf_size = pkt->size;
2106 unsigned data_unit_size;
2108 /* release unused frames */
2109 for (i = 0; i < MAX_FRAMES; i++)
2110 if (s->all_frames[i].avframe->data[0] && !s->all_frames[i].reference) {
2111 av_frame_unref(s->all_frames[i].avframe);
2112 memset(s->all_frames[i].interpolated, 0, sizeof(s->all_frames[i].interpolated));
2115 s->current_picture = NULL;
2118 /* end of stream, so flush delayed pics */
2120 return get_delayed_pic(s, (AVFrame *)data, got_frame);
2123 /*[DIRAC_STD] Here starts the code from parse_info() defined in 9.6
2124 [DIRAC_STD] PARSE_INFO_PREFIX = "BBCD" as defined in ISO/IEC 646
2125 BBCD start code search */
2126 for (; buf_idx + DATA_UNIT_HEADER_SIZE < buf_size; buf_idx++) {
2127 if (buf[buf_idx ] == 'B' && buf[buf_idx+1] == 'B' &&
2128 buf[buf_idx+2] == 'C' && buf[buf_idx+3] == 'D')
2131 /* BBCD found or end of data */
2132 if (buf_idx + DATA_UNIT_HEADER_SIZE >= buf_size)
2135 data_unit_size = AV_RB32(buf+buf_idx+5);
2136 if (data_unit_size > buf_size - buf_idx || !data_unit_size) {
2137 if(data_unit_size > buf_size - buf_idx)
2138 av_log(s->avctx, AV_LOG_ERROR,
2139 "Data unit with size %d is larger than input buffer, discarding\n",
2144 /* [DIRAC_STD] dirac_decode_data_unit makes reference to the while defined in 9.3 inside the function parse_sequence() */
2145 ret = dirac_decode_data_unit(avctx, buf+buf_idx, data_unit_size);
2148 av_log(s->avctx, AV_LOG_ERROR,"Error in dirac_decode_data_unit\n");
2151 buf_idx += data_unit_size;
2154 if (!s->current_picture)
2157 if (s->current_picture->avframe->display_picture_number > s->frame_number) {
2158 DiracFrame *delayed_frame = remove_frame(s->delay_frames, s->frame_number);
2160 s->current_picture->reference |= DELAYED_PIC_REF;
2162 if (add_frame(s->delay_frames, MAX_DELAY, s->current_picture)) {
2163 int min_num = s->delay_frames[0]->avframe->display_picture_number;
2164 /* Too many delayed frames, so we display the frame with the lowest pts */
2165 av_log(avctx, AV_LOG_ERROR, "Delay frame overflow\n");
2167 for (i = 1; s->delay_frames[i]; i++)
2168 if (s->delay_frames[i]->avframe->display_picture_number < min_num)
2169 min_num = s->delay_frames[i]->avframe->display_picture_number;
2171 delayed_frame = remove_frame(s->delay_frames, min_num);
2172 add_frame(s->delay_frames, MAX_DELAY, s->current_picture);
2175 if (delayed_frame) {
2176 delayed_frame->reference ^= DELAYED_PIC_REF;
2177 if((ret=av_frame_ref(data, delayed_frame->avframe)) < 0)
2181 } else if (s->current_picture->avframe->display_picture_number == s->frame_number) {
2182 /* The right frame at the right time :-) */
2183 if((ret=av_frame_ref(data, s->current_picture->avframe)) < 0)
2189 s->frame_number = picture->display_picture_number + 1;
2194 AVCodec ff_dirac_decoder = {
2196 .long_name = NULL_IF_CONFIG_SMALL("BBC Dirac VC-2"),
2197 .type = AVMEDIA_TYPE_VIDEO,
2198 .id = AV_CODEC_ID_DIRAC,
2199 .priv_data_size = sizeof(DiracContext),
2200 .init = dirac_decode_init,
2201 .close = dirac_decode_end,
2202 .decode = dirac_decode_frame,
2203 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_DR1,
2204 .flush = dirac_decode_flush,