FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
diracdec.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Marco Gerards <marco@gnu.org>
3  * Copyright (C) 2009 David Conrad
4  * Copyright (C) 2011 Jordi Ortiz
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * Dirac Decoder
26  * @author Marco Gerards <marco@gnu.org>, David Conrad, Jordi Ortiz <nenjordi@gmail.com>
27  */
28 
29 #include "avcodec.h"
30 #include "get_bits.h"
31 #include "bytestream.h"
32 #include "internal.h"
33 #include "golomb.h"
34 #include "dirac_arith.h"
35 #include "mpeg12data.h"
36 #include "libavcodec/mpegvideo.h"
37 #include "mpegvideoencdsp.h"
38 #include "dirac_dwt.h"
39 #include "dirac.h"
40 #include "diractab.h"
41 #include "diracdsp.h"
42 #include "videodsp.h"
43 
44 /**
45  * The spec limits this to 3 for frame coding, but in practice can be as high as 6
46  */
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 */
52 
53 /**
54  * DiracBlock->ref flags, if set then the block does MC from the given ref
55  */
56 #define DIRAC_REF_MASK_REF1 1
57 #define DIRAC_REF_MASK_REF2 2
58 #define DIRAC_REF_MASK_GLOBAL 4
59 
60 /**
61  * Value of Picture.reference when Picture is not a reference picture, but
62  * is held for delayed output.
63  */
64 #define DELAYED_PIC_REF 4
65 
66 #define CALC_PADDING(size, depth) \
67  (((size + (1 << depth) - 1) >> depth) << depth)
68 
69 #define DIVRNDUP(a, b) (((a) + (b) - 1) / (b))
70 
71 typedef struct {
73  int interpolated[3]; /* 1 if hpel[] is valid */
74  uint8_t *hpel[3][4];
75  uint8_t *hpel_base[3][4];
76  int reference;
77 } DiracFrame;
78 
79 typedef struct {
80  union {
81  int16_t mv[2][2];
82  int16_t dc[3];
83  } u; /* anonymous unions aren't in C99 :( */
85 } DiracBlock;
86 
87 typedef struct SubBand {
88  int level;
89  int orientation;
90  int stride; /* in bytes */
91  int width;
92  int height;
93  int pshift;
94  int quant;
95  uint8_t *ibuf;
96  struct SubBand *parent;
97 
98  /* for low delay */
99  unsigned length;
101 } SubBand;
102 
103 typedef struct Plane {
105 
106  int width;
107  int height;
108  ptrdiff_t stride;
109 
110  /* block length */
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) */
119 
121 } Plane;
122 
123 typedef struct DiracContext {
132  int frame_number; /* number of the next frame to display */
136 
137  int bit_depth; /* bit depth */
138  int pshift; /* pixel shift = bit_depth > 8 */
139 
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 */
149 
150  /* wavelet decoding */
151  unsigned wavelet_depth; /* depth of the IDWT */
152  unsigned wavelet_idx;
153 
154  /**
155  * schroedinger older than 1.0.8 doesn't store
156  * quant delta if only one codebook exists in a band
157  */
158  unsigned old_delta_quant;
159  unsigned codeblock_mode;
160 
161  unsigned num_x; /* number of horizontal slices */
162  unsigned num_y; /* number of vertical slices */
163 
164  struct {
165  unsigned width;
166  unsigned height;
168 
169  struct {
170  AVRational bytes; /* average bytes per slice */
171  uint8_t quant[MAX_DWT_LEVELS][4]; /* [DIRAC_STD] E.1 */
172  } lowdelay;
173 
174  struct {
175  unsigned prefix_bytes;
176  unsigned size_scaler;
177  } highquality;
178 
179  struct {
180  int pan_tilt[2]; /* pan/tilt vector */
181  int zrs[2][2]; /* zoom/rotate/shear matrix */
182  int perspective[2]; /* perspective vector */
183  unsigned zrs_exp;
184  unsigned perspective_exp;
185  } globalmc[2];
186 
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 */
191 
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) */
196 
199 
202 
203  uint16_t *mctmp; /* buffer holding the MC data multiplied by OBMC weights */
206 
208 
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);
214 
217 
221 } DiracContext;
222 
229 };
230 
231 /* magic number division by 3 from schroedinger */
232 static inline int divide3(int x)
233 {
234  return ((x+1)*21845 + 10922) >> 16;
235 }
236 
237 static DiracFrame *remove_frame(DiracFrame *framelist[], int picnum)
238 {
239  DiracFrame *remove_pic = NULL;
240  int i, remove_idx = -1;
241 
242  for (i = 0; framelist[i]; i++)
243  if (framelist[i]->avframe->display_picture_number == picnum) {
244  remove_pic = framelist[i];
245  remove_idx = i;
246  }
247 
248  if (remove_pic)
249  for (i = remove_idx; framelist[i]; i++)
250  framelist[i] = framelist[i+1];
251 
252  return remove_pic;
253 }
254 
255 static int add_frame(DiracFrame *framelist[], int maxframes, DiracFrame *frame)
256 {
257  int i;
258  for (i = 0; i < maxframes; i++)
259  if (!framelist[i]) {
260  framelist[i] = frame;
261  return 0;
262  }
263  return -1;
264 }
265 
267 {
268  int sbwidth = DIVRNDUP(s->seq.width, 4);
269  int sbheight = DIVRNDUP(s->seq.height, 4);
270  int i, w, h, top_padding;
271 
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);
278 
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
283  * on each side */
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;
287 
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);
293  }
294 
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));
298 
299  if (!s->sbsplit || !s->blmotion)
300  return AVERROR(ENOMEM);
301  return 0;
302 }
303 
305 {
306  int w = s->seq.width;
307  int h = s->seq.height;
308 
309  av_assert0(stride >= w);
310  stride += 64;
311 
312  if (s->buffer_stride >= stride)
313  return 0;
314  s->buffer_stride = 0;
315 
317  memset(s->edge_emu_buffer, 0, sizeof(s->edge_emu_buffer));
318  av_freep(&s->mctmp);
319  av_freep(&s->mcscratch);
320 
322 
323  s->mctmp = av_malloc_array((stride+MAX_BLOCKSIZE), (h+MAX_BLOCKSIZE) * sizeof(*s->mctmp));
325 
326  if (!s->edge_emu_buffer_base || !s->mctmp || !s->mcscratch)
327  return AVERROR(ENOMEM);
328 
329  s->buffer_stride = stride;
330  return 0;
331 }
332 
334 {
335  int i, j, k;
336 
337  for (i = 0; i < MAX_FRAMES; i++) {
338  if (s->all_frames[i].avframe->data[0]) {
340  memset(s->all_frames[i].interpolated, 0, sizeof(s->all_frames[i].interpolated));
341  }
342 
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]);
346  }
347 
348  memset(s->ref_frames, 0, sizeof(s->ref_frames));
349  memset(s->delay_frames, 0, sizeof(s->delay_frames));
350 
351  for (i = 0; i < 3; i++) {
352  av_freep(&s->plane[i].idwt.buf_base);
353  av_freep(&s->plane[i].idwt.tmp);
354  }
355 
356  s->buffer_stride = 0;
357  av_freep(&s->sbsplit);
358  av_freep(&s->blmotion);
360 
361  av_freep(&s->mctmp);
362  av_freep(&s->mcscratch);
363 }
364 
366 {
367  DiracContext *s = avctx->priv_data;
368  int i;
369 
370  s->avctx = avctx;
371  s->frame_number = -1;
372 
375  ff_videodsp_init(&s->vdsp, 8);
376 
377  for (i = 0; i < MAX_FRAMES; i++) {
379  if (!s->all_frames[i].avframe) {
380  while (i > 0)
381  av_frame_free(&s->all_frames[--i].avframe);
382  return AVERROR(ENOMEM);
383  }
384  }
385 
386  return 0;
387 }
388 
390 {
391  DiracContext *s = avctx->priv_data;
393  s->seen_sequence_header = 0;
394  s->frame_number = -1;
395 }
396 
398 {
399  DiracContext *s = avctx->priv_data;
400  int i;
401 
402  dirac_decode_flush(avctx);
403  for (i = 0; i < MAX_FRAMES; i++)
405 
406  return 0;
407 }
408 
409 #define SIGN_CTX(x) (CTX_SIGN_ZERO + ((x) > 0) - ((x) < 0))
410 
411 static inline int coeff_unpack_golomb(GetBitContext *gb, int qfactor, int qoffset)
412 {
413  int sign, coeff;
414  uint32_t buf;
415 
416  OPEN_READER(re, gb);
417  UPDATE_CACHE(re, gb);
418  buf = GET_CACHE(re, gb);
419 
420  if (buf & 0x80000000) {
421  LAST_SKIP_BITS(re,gb,1);
422  CLOSE_READER(re, gb);
423  return 0;
424  }
425 
426  if (buf & 0xAA800000) {
427  buf >>= 32 - 8;
429 
431  } else {
432  unsigned ret = 1;
433 
434  do {
435  buf >>= 32 - 8;
436  SKIP_BITS(re, gb,
438 
439  if (ff_interleaved_golomb_vlc_len[buf] != 9) {
440  ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
442  break;
443  }
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));
448 
449  coeff = ret - 1;
450  }
451 
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;
456 
457  CLOSE_READER(re, gb);
458  return coeff;
459 }
460 
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) \
464  { \
465  int coeff, sign, sign_pred = 0, pred_ctx = CTX_ZPZN_F1; \
466  const int mstride = -(b->stride >> (1+b->pshift)); \
467  if (b->parent) { \
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; \
471  } \
472  if (b->orientation == subband_hl) \
473  sign_pred = buf[mstride]; \
474  if (x) { \
475  pred_ctx += !(buf[-1] | buf[mstride] | buf[-1 + mstride]); \
476  if (b->orientation == subband_lh) \
477  sign_pred = buf[-1]; \
478  } else { \
479  pred_ctx += !buf[mstride]; \
480  } \
481  coeff = dirac_get_arith_uint(c, pred_ctx, CTX_COEFF_DATA); \
482  if (coeff) { \
483  coeff = (coeff * qfactor + qoffset) >> 2; \
484  sign = dirac_get_arith_bit(c, SIGN_CTX(sign_pred)); \
485  coeff = (coeff ^ -sign) + sign; \
486  } \
487  *buf = coeff; \
488  } \
489 
490 UNPACK_ARITH(8, int16_t)
492 
493 /**
494  * Decode the coeffs in the rectangle defined by left, right, top, bottom
495  * [DIRAC_STD] 13.4.3.2 Codeblock unpacking loop. codeblock()
496  */
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)
501 {
502  int x, y, zero_block;
503  int qoffset, qfactor;
504  uint8_t *buf;
505 
506  /* check for any coded coefficients in this codeblock */
507  if (!blockcnt_one) {
508  if (is_arith)
509  zero_block = dirac_get_arith_bit(c, CTX_ZERO_BLOCK);
510  else
511  zero_block = get_bits1(gb);
512 
513  if (zero_block)
514  return;
515  }
516 
517  if (s->codeblock_mode && !(s->old_delta_quant && blockcnt_one)) {
518  int quant = b->quant;
519  if (is_arith)
521  else
522  quant += dirac_get_se_golomb(gb);
523  if (quant < 0) {
524  av_log(s->avctx, AV_LOG_ERROR, "Invalid quant\n");
525  return;
526  }
527  b->quant = quant;
528  }
529 
530  if (b->quant > 115) {
531  av_log(s->avctx, AV_LOG_ERROR, "Unsupported quant %d\n", b->quant);
532  b->quant = 0;
533  return;
534  }
535 
536  qfactor = ff_dirac_qscale_tab[b->quant];
537  /* TODO: context pointer? */
538  if (!s->num_refs)
539  qoffset = ff_dirac_qoffset_intra_tab[b->quant] + 2;
540  else
541  qoffset = ff_dirac_qoffset_inter_tab[b->quant] + 2;
542 
543  buf = b->ibuf + top * b->stride;
544  if (is_arith) {
545  for (y = top; y < bottom; y++) {
546  for (x = left; x < right; x++) {
547  if (b->pshift) {
548  coeff_unpack_arith_10(c, qfactor, qoffset, b, (int32_t*)(buf)+x, x, y);
549  } else {
550  coeff_unpack_arith_8(c, qfactor, qoffset, b, (int16_t*)(buf)+x, x, y);
551  }
552  }
553  buf += b->stride;
554  }
555  } else {
556  for (y = top; y < bottom; y++) {
557  for (x = left; x < right; x++) {
558  int val = coeff_unpack_golomb(gb, qfactor, qoffset);
559  if (b->pshift) {
560  AV_WN32(&buf[4*x], val);
561  } else {
562  AV_WN16(&buf[2*x], val);
563  }
564  }
565  buf += b->stride;
566  }
567  }
568 }
569 
570 /**
571  * Dirac Specification ->
572  * 13.3 intra_dc_prediction(band)
573  */
574 #define INTRA_DC_PRED(n, type) \
575  static inline void intra_dc_prediction_##n(SubBand *b) \
576  { \
577  type *buf = (type*)b->ibuf; \
578  int x, y; \
579  \
580  for (x = 1; x < b->width; x++) \
581  buf[x] += buf[x-1]; \
582  buf += (b->stride >> (1+b->pshift)); \
583  \
584  for (y = 1; y < b->height; y++) { \
585  buf[0] += buf[-(b->stride >> (1+b->pshift))]; \
586  \
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); \
590  } \
591  buf += (b->stride >> (1+b->pshift)); \
592  } \
593  } \
594 
595 INTRA_DC_PRED(8, int16_t)
597 
598 /**
599  * Dirac Specification ->
600  * 13.4.2 Non-skipped subbands. subband_coeffs()
601  */
603 {
604  int cb_x, cb_y, left, right, top, bottom;
605  DiracArith c;
606  GetBitContext gb;
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;
610 
611  if (!b->length)
612  return;
613 
614  init_get_bits8(&gb, b->coeff_data, b->length);
615 
616  if (is_arith)
617  ff_dirac_init_arith_decoder(&c, &gb, b->length);
618 
619  top = 0;
620  for (cb_y = 0; cb_y < cb_height; cb_y++) {
621  bottom = (b->height * (cb_y+1LL)) / cb_height;
622  left = 0;
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);
626  left = right;
627  }
628  top = bottom;
629  }
630 
631  if (b->orientation == subband_ll && s->num_refs == 0) {
632  if (s->pshift) {
633  intra_dc_prediction_10(b);
634  } else {
635  intra_dc_prediction_8(b);
636  }
637  }
638 }
639 
640 static int decode_subband_arith(AVCodecContext *avctx, void *b)
641 {
642  DiracContext *s = avctx->priv_data;
643  decode_subband_internal(s, b, 1);
644  return 0;
645 }
646 
647 static int decode_subband_golomb(AVCodecContext *avctx, void *arg)
648 {
649  DiracContext *s = avctx->priv_data;
650  SubBand **b = arg;
651  decode_subband_internal(s, *b, 0);
652  return 0;
653 }
654 
655 /**
656  * Dirac Specification ->
657  * [DIRAC_STD] 13.4.1 core_transform_data()
658  */
660 {
661  AVCodecContext *avctx = s->avctx;
662  SubBand *bands[3*MAX_DWT_LEVELS+1];
663  enum dirac_subband orientation;
664  int level, num_bands = 0;
665 
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;
671 
672  align_get_bits(&s->gb);
673  /* [DIRAC_STD] 13.4.2 subband() */
674  b->length = svq3_get_ue_golomb(&s->gb);
675  if (b->length) {
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);
681  }
682  }
683  /* arithmetic coding has inter-level dependencies, so we can only execute one level at a time */
684  if (s->is_arith)
685  avctx->execute(avctx, decode_subband_arith, &s->plane[comp].band[level][!!level],
686  NULL, 4-!!level, sizeof(SubBand));
687  }
688  /* golomb coding has no inter-level dependencies, so we can execute all subbands in parallel */
689  if (!s->is_arith)
690  avctx->execute(avctx, decode_subband_golomb, bands, NULL, num_bands, sizeof(SubBand*));
691 }
692 
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) \
697  return; \
698  if (buf2) { \
699  buf = (type *)buf2; \
700  buf[x] = coeff_unpack_golomb(gb, qfactor, qoffset); \
701  if (get_bits_count(gb) >= ebits) \
702  return; \
703  } \
704 
706  int slice_x, int slice_y, int bits_end,
707  SubBand *b1, SubBand *b2)
708 {
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;
713 
714  int qfactor, qoffset;
715 
716  uint8_t *buf1 = b1->ibuf + top * b1->stride;
717  uint8_t *buf2 = b2 ? b2->ibuf + top * b2->stride: NULL;
718  int x, y;
719 
720  if (quant > 115) {
721  av_log(s->avctx, AV_LOG_ERROR, "Unsupported quant %d\n", quant);
722  return;
723  }
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)
729  return;
730 
731  if (s->pshift) {
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);
735  }
736  buf1 += b1->stride;
737  if (buf2)
738  buf2 += b2->stride;
739  }
740  }
741  else {
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);
745  }
746  buf1 += b1->stride;
747  if (buf2)
748  buf2 += b2->stride;
749  }
750  }
751 }
752 
753 /* Used by Low Delay and High Quality profiles */
754 typedef struct DiracSlice {
756  int slice_x;
757  int slice_y;
758  int bytes;
759 } DiracSlice;
760 
761 
762 /**
763  * Dirac Specification ->
764  * 13.5.2 Slices. slice(sx,sy)
765  */
766 static int decode_lowdelay_slice(AVCodecContext *avctx, void *arg)
767 {
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;
773 
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));
778 
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);
785  }
786 
787  /* consume any unused bits from luma */
788  skip_bits_long(gb, get_bits_count(gb) - luma_end);
789 
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]);
799  }
800 
801  return 0;
802 }
803 
804 /**
805  * VC-2 Specification ->
806  * 13.5.3 hq_slice(sx,sy)
807  */
808 static int decode_hq_slice(AVCodecContext *avctx, void *arg)
809 {
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;
815 
817  quant_idx = get_bits(gb, 8);
818 
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;
824  }
825  }
826 
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);
836  }
837  }
838  skip_bits_long(gb, bits_end - get_bits_count(gb));
839  }
840 
841  return 0;
842 }
843 
844 /**
845  * Dirac Specification ->
846  * 13.5.1 low_delay_transform_data()
847  */
849 {
850  AVCodecContext *avctx = s->avctx;
851  int slice_x, slice_y, bytes = 0, bufsize;
852  const uint8_t *buf;
853  DiracSlice *slices;
854  int slice_num = 0;
855 
856  slices = av_mallocz_array(s->num_x, s->num_y * sizeof(DiracSlice));
857  if (!slices)
858  return AVERROR(ENOMEM);
859 
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);
864 
865  if (s->hq_picture) {
866  int i;
867 
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;
874  }
875 
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);
880  slice_num++;
881 
882  buf += bytes;
883  if (bufsize/8 >= bytes)
884  bufsize -= bytes*8;
885  else
886  bufsize = 0;
887  }
888  }
889  avctx->execute(avctx, decode_hq_slice, slices, NULL, slice_num,
890  sizeof(DiracSlice));
891  } else {
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);
900  slice_num++;
901 
902  buf += bytes;
903  if (bufsize/8 >= bytes)
904  bufsize -= bytes*8;
905  else
906  bufsize = 0;
907  }
908  }
909  avctx->execute(avctx, decode_lowdelay_slice, slices, NULL, slice_num,
910  sizeof(DiracSlice)); /* [DIRAC_STD] 13.5.2 Slices */
911  }
912 
913  if (s->dc_prediction) {
914  if (s->pshift) {
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() */
918  } else {
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]);
922  }
923  }
924  av_free(slices);
925  return 0;
926 }
927 
929 {
930  int i, w, h, level, orientation;
931 
932  for (i = 0; i < 3; i++) {
933  Plane *p = &s->plane[i];
934 
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);
940 
941  for (level = s->wavelet_depth-1; level >= 0; level--) {
942  w = w>>1;
943  h = h>>1;
944  for (orientation = !!level; orientation < 4; orientation++) {
945  SubBand *b = &p->band[level][orientation];
946 
947  b->pshift = s->pshift;
948  b->ibuf = p->idwt.buf;
949  b->level = level;
950  b->stride = p->idwt.stride << (s->wavelet_depth - level);
951  b->width = w;
952  b->height = h;
953  b->orientation = orientation;
954 
955  if (orientation & 1)
956  b->ibuf += w << (1+b->pshift);
957  if (orientation > 1)
958  b->ibuf += (b->stride>>1);
959 
960  if (level)
961  b->parent = &p->band[level-1][orientation];
962  }
963  }
964 
965  if (i > 0) {
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;
970  }
971 
972  p->xoffset = (p->xblen - p->xbsep)/2;
973  p->yoffset = (p->yblen - p->ybsep)/2;
974  }
975 }
976 
977 /**
978  * Unpack the motion compensation parameters
979  * Dirac Specification ->
980  * 11.2 Picture prediction data. picture_prediction()
981  */
983 {
984  static const uint8_t default_blen[] = { 4, 12, 16, 24 };
985 
986  GetBitContext *gb = &s->gb;
987  unsigned idx, ref;
988 
989  align_get_bits(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 */
993 
994  if (idx > 4) {
995  av_log(s->avctx, AV_LOG_ERROR, "Block prediction index too high\n");
996  return AVERROR_INVALIDDATA;
997  }
998 
999  if (idx == 0) {
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);
1004  } else {
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;
1010  }
1011  /*[DIRAC_STD] 11.2.4 motion_data_dimensions()
1012  Calculated in function dirac_unpack_block_motion_data */
1013 
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) {
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;
1021  }
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;
1025  }
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;
1029  }
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;
1033  }
1034 
1035  /*[DIRAC_STD] 11.2.5 Motion vector precision. motion_vector_precision()
1036  Read motion vector precision */
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;
1041  }
1042 
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);
1053  }
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);
1062  } else {
1063  s->globalmc[ref].zrs[0][0] = 1;
1064  s->globalmc[ref].zrs[1][1] = 1;
1065  }
1066  /* [DIRAC_STD] perspective(gparams) */
1067  if (get_bits1(gb)) {
1069  s->globalmc[ref].perspective[0] = dirac_get_se_golomb(gb);
1070  s->globalmc[ref].perspective[1] = dirac_get_se_golomb(gb);
1071  }
1072  }
1073  }
1074 
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;
1080  }
1081 
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;
1085  s->weight[0] = 1;
1086  s->weight[1] = 1;
1087 
1088  if (get_bits1(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);
1093  }
1094  return 0;
1095 }
1096 
1097 /**
1098  * Dirac Specification ->
1099  * 11.3 Wavelet transform data. wavelet_transform()
1100  */
1102 {
1103  GetBitContext *gb = &s->gb;
1104  int i, level;
1105  unsigned tmp;
1106 
1107 #define CHECKEDREAD(dst, cond, errmsg) \
1108  tmp = svq3_get_ue_golomb(gb); \
1109  if (cond) { \
1110  av_log(s->avctx, AV_LOG_ERROR, errmsg); \
1111  return AVERROR_INVALIDDATA; \
1112  }\
1113  dst = tmp;
1114 
1115  align_get_bits(gb);
1116 
1117  s->zero_res = s->num_refs ? get_bits1(gb) : 0;
1118  if (s->zero_res)
1119  return 0;
1120 
1121  /*[DIRAC_STD] 11.3.1 Transform parameters. transform_parameters() */
1122  CHECKEDREAD(s->wavelet_idx, tmp > 6, "wavelet_idx is too big\n")
1123 
1124  CHECKEDREAD(s->wavelet_depth, tmp > MAX_DWT_LEVELS || tmp < 1, "invalid number of DWT decompositions\n")
1125 
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")
1132  }
1133 
1134  CHECKEDREAD(s->codeblock_mode, tmp > 1, "unknown codeblock mode\n")
1135  }
1136  else {
1137  for (i = 0; i <= s->wavelet_depth; i++)
1138  s->codeblock[i].width = s->codeblock[i].height = 1;
1139  }
1140  }
1141  else {
1142  s->num_x = svq3_get_ue_golomb(gb);
1143  s->num_y = svq3_get_ue_golomb(gb);
1144  if (s->ld_picture) {
1147  if (s->lowdelay.bytes.den <= 0) {
1148  av_log(s->avctx,AV_LOG_ERROR,"Invalid lowdelay.bytes.den\n");
1149  return AVERROR_INVALIDDATA;
1150  }
1151  } else if (s->hq_picture) {
1154  }
1155 
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);
1165  }
1166  } else {
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;
1170  }
1171  /* default quantization matrix */
1172  for (level = 0; level < s->wavelet_depth; level++)
1173  for (i = 0; i < 4; 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);
1178  }
1179  }
1180  }
1181  return 0;
1182 }
1183 
1184 static inline int pred_sbsplit(uint8_t *sbsplit, int stride, int x, int y)
1185 {
1186  static const uint8_t avgsplit[7] = { 0, 0, 1, 1, 1, 2, 2 };
1187 
1188  if (!(x|y))
1189  return 0;
1190  else if (!y)
1191  return sbsplit[-1];
1192  else if (!x)
1193  return sbsplit[-stride];
1194 
1195  return avgsplit[sbsplit[-1] + sbsplit[-stride] + sbsplit[-stride-1]];
1196 }
1197 
1198 static inline int pred_block_mode(DiracBlock *block, int stride, int x, int y, int refmask)
1199 {
1200  int pred;
1201 
1202  if (!(x|y))
1203  return 0;
1204  else if (!y)
1205  return block[-1].ref & refmask;
1206  else if (!x)
1207  return block[-stride].ref & refmask;
1208 
1209  /* return the majority */
1210  pred = (block[-1].ref & refmask) + (block[-stride].ref & refmask) + (block[-stride-1].ref & refmask);
1211  return (pred >> 1) & refmask;
1212 }
1213 
1214 static inline void pred_block_dc(DiracBlock *block, int stride, int x, int y)
1215 {
1216  int i, n = 0;
1217 
1218  memset(block->u.dc, 0, sizeof(block->u.dc));
1219 
1220  if (x && !(block[-1].ref & 3)) {
1221  for (i = 0; i < 3; i++)
1222  block->u.dc[i] += block[-1].u.dc[i];
1223  n++;
1224  }
1225 
1226  if (y && !(block[-stride].ref & 3)) {
1227  for (i = 0; i < 3; i++)
1228  block->u.dc[i] += block[-stride].u.dc[i];
1229  n++;
1230  }
1231 
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];
1235  n++;
1236  }
1237 
1238  if (n == 2) {
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]);
1244  }
1245 }
1246 
1247 static inline void pred_mv(DiracBlock *block, int stride, int x, int y, int ref)
1248 {
1249  int16_t *pred[3];
1250  int refmask = ref+1;
1251  int mask = refmask | DIRAC_REF_MASK_GLOBAL; /* exclude gmc blocks */
1252  int n = 0;
1253 
1254  if (x && (block[-1].ref & mask) == refmask)
1255  pred[n++] = block[-1].u.mv[ref];
1256 
1257  if (y && (block[-stride].ref & mask) == refmask)
1258  pred[n++] = block[-stride].u.mv[ref];
1259 
1260  if (x && y && (block[-stride-1].ref & mask) == refmask)
1261  pred[n++] = block[-stride-1].u.mv[ref];
1262 
1263  switch (n) {
1264  case 0:
1265  block->u.mv[ref][0] = 0;
1266  block->u.mv[ref][1] = 0;
1267  break;
1268  case 1:
1269  block->u.mv[ref][0] = pred[0][0];
1270  block->u.mv[ref][1] = pred[0][1];
1271  break;
1272  case 2:
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;
1275  break;
1276  case 3:
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]);
1279  break;
1280  }
1281 }
1282 
1283 static void global_mv(DiracContext *s, DiracBlock *block, int x, int y, int ref)
1284 {
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;
1290 
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]);
1294 
1295  block->u.mv[ref][0] = (mx + (1<<(ez+ep))) >> (ez+ep);
1296  block->u.mv[ref][1] = (my + (1<<(ez+ep))) >> (ez+ep);
1297 }
1298 
1300  int stride, int x, int y)
1301 {
1302  int i;
1303 
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);
1306 
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;
1310  }
1311 
1312  if (!block->ref) {
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);
1316  return;
1317  }
1318 
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;
1322  }
1323 
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);
1328  } else {
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);
1332  }
1333  }
1334 }
1335 
1336 /**
1337  * Copies the current block to the other blocks covered by the current superblock split mode
1338  */
1340 {
1341  int x, y;
1342  DiracBlock *dst = block;
1343 
1344  for (x = 1; x < size; x++)
1345  dst[x] = *block;
1346 
1347  for (y = 1; y < size; y++) {
1348  dst += stride;
1349  for (x = 0; x < size; x++)
1350  dst[x] = *block;
1351  }
1352 }
1353 
1354 /**
1355  * Dirac Specification ->
1356  * 12. Block motion data syntax
1357  */
1359 {
1360  GetBitContext *gb = &s->gb;
1361  uint8_t *sbsplit = s->sbsplit;
1362  int i, x, y, q, p;
1363  DiracArith arith[8];
1364 
1365  align_get_bits(gb);
1366 
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;
1372 
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);
1379  if (split > 2)
1380  return AVERROR_INVALIDDATA;
1381  sbsplit[x] = (split + pred_sbsplit(sbsplit+x, s->sbwidth, x, y)) % 3;
1382  }
1383  sbsplit += s->sbwidth;
1384  }
1385 
1386  /* setup arith decoding */
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));
1391  }
1392  for (i = 0; i < 3; i++)
1393  ff_dirac_init_arith_decoder(arith+1+i, gb, svq3_get_ue_golomb(gb));
1394 
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];
1399 
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);
1407  }
1408  }
1409 
1410  return 0;
1411 }
1412 
1413 static int weight(int i, int blen, int offset)
1414 {
1415 #define ROLLOFF(i) offset == 1 ? ((i) ? 5 : 3) : \
1416  (1 + (6*(i) + offset - 1) / (2*offset - 1))
1417 
1418  if (i < 2*offset)
1419  return ROLLOFF(i);
1420  else if (i > blen-1 - 2*offset)
1421  return ROLLOFF(blen-1 - i);
1422  return 8;
1423 }
1424 
1425 static void init_obmc_weight_row(Plane *p, uint8_t *obmc_weight, int stride,
1426  int left, int right, int wy)
1427 {
1428  int x;
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++)
1436  obmc_weight[x] = 0;
1437 }
1438 
1439 static void init_obmc_weight(Plane *p, uint8_t *obmc_weight, int stride,
1440  int left, int right, int top, int bottom)
1441 {
1442  int y;
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;
1446  }
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;
1451  }
1452  for (; y < p->yblen; y++) {
1453  init_obmc_weight_row(p, obmc_weight, stride, left, right, 8);
1454  obmc_weight += stride;
1455  }
1456 }
1457 
1458 static void init_obmc_weights(DiracContext *s, Plane *p, int by)
1459 {
1460  int top = !by;
1461  int bottom = by == s->blheight-1;
1462 
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);
1468  }
1469 }
1470 
1471 static const uint8_t epel_weights[4][4][4] = {
1472  {{ 16, 0, 0, 0 },
1473  { 12, 4, 0, 0 },
1474  { 8, 8, 0, 0 },
1475  { 4, 12, 0, 0 }},
1476  {{ 12, 0, 4, 0 },
1477  { 9, 3, 3, 1 },
1478  { 6, 6, 2, 2 },
1479  { 3, 9, 1, 3 }},
1480  {{ 8, 0, 8, 0 },
1481  { 6, 2, 6, 2 },
1482  { 4, 4, 4, 4 },
1483  { 2, 6, 2, 6 }},
1484  {{ 4, 0, 12, 0 },
1485  { 3, 1, 9, 3 },
1486  { 2, 2, 6, 6 },
1487  { 1, 3, 3, 9 }}
1488 };
1489 
1490 /**
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
1493  * to MC from.
1494  *
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
1497  */
1499  int x, int y, int ref, int plane)
1500 {
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;
1506 
1507  if (plane) {
1508  motion_x >>= s->chroma_x_shift;
1509  motion_y >>= s->chroma_y_shift;
1510  }
1511 
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;
1520 
1521  x += motion_x;
1522  y += motion_y;
1523  epel = (mx|my)&1;
1524 
1525  /* hpel position */
1526  if (!((mx|my)&3)) {
1527  nplanes = 1;
1528  src[0] = ref_hpel[(my>>1)+(mx>>2)] + y*p->stride + x;
1529  } else {
1530  /* qpel or epel */
1531  nplanes = 4;
1532  for (i = 0; i < 4; i++)
1533  src[i] = ref_hpel[i] + y*p->stride + x;
1534 
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 */
1537  if (mx > 4) {
1538  src[0] += 1;
1539  src[2] += 1;
1540  x++;
1541  }
1542  if (my > 4) {
1543  src[0] += p->stride;
1544  src[1] += p->stride;
1545  y++;
1546  }
1547 
1548  /* hpel planes are:
1549  [0]: F [1]: H
1550  [2]: V [3]: C */
1551  if (!epel) {
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) */
1554  if (!(mx&3)) {
1555  /* mx == 0: average [0] and [2]
1556  mx == 4: average [1] and [3] */
1557  src[!mx] = src[2 + !!mx];
1558  nplanes = 2;
1559  } else if (!(my&3)) {
1560  src[0] = src[(my>>1) ];
1561  src[1] = src[(my>>1)+1];
1562  nplanes = 2;
1563  }
1564  } else {
1565  /* adjust the ordering if needed so the weights work */
1566  if (mx > 4) {
1567  FFSWAP(const uint8_t *, src[0], src[1]);
1568  FFSWAP(const uint8_t *, src[2], src[3]);
1569  }
1570  if (my > 4) {
1571  FFSWAP(const uint8_t *, src[0], src[2]);
1572  FFSWAP(const uint8_t *, src[1], src[3]);
1573  }
1574  src[4] = epel_weights[my&3][mx&3];
1575  }
1576  }
1577 
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 ||
1581  x < 0 || y < 0) {
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];
1588  }
1589  }
1590  return (nplanes>>1) + epel;
1591 }
1592 
1593 static void add_dc(uint16_t *dst, int dc, int stride,
1594  uint8_t *obmc_weight, int xblen, int yblen)
1595 {
1596  int x, y;
1597  dc += 128;
1598 
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];
1603  }
1604  dst += stride;
1605  obmc_weight += MAX_BLOCKSIZE;
1606  }
1607 }
1608 
1610  uint16_t *mctmp, uint8_t *obmc_weight,
1611  int plane, int dstx, int dsty)
1612 {
1613  Plane *p = &s->plane[plane];
1614  const uint8_t *src[5];
1615  int idx;
1616 
1617  switch (block->ref&3) {
1618  case 0: /* DC */
1619  add_dc(mctmp, block->u.dc[plane], p->stride, obmc_weight, p->xblen, p->yblen);
1620  return;
1621  case 1:
1622  case 2:
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);
1625  if (s->weight_func)
1627  s->weight[0] + s->weight[1], p->yblen);
1628  break;
1629  case 3:
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);
1637  s->weight[0], s->weight[1], p->yblen);
1638  } else
1639  s->avg_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen);
1640  break;
1641  }
1642  s->add_obmc(mctmp, s->mcscratch, p->stride, obmc_weight, p->yblen);
1643 }
1644 
1645 static void mc_row(DiracContext *s, DiracBlock *block, uint16_t *mctmp, int plane, int dsty)
1646 {
1647  Plane *p = &s->plane[plane];
1648  int x, dstx = p->xbsep - p->xoffset;
1649 
1650  block_mc(s, block, mctmp, s->obmc_weight[0], plane, -p->xoffset, dsty);
1651  mctmp += p->xbsep;
1652 
1653  for (x = 1; x < s->blwidth-1; x++) {
1654  block_mc(s, block+x, mctmp, s->obmc_weight[1], plane, dstx, dsty);
1655  dstx += p->xbsep;
1656  mctmp += p->xbsep;
1657  }
1658  block_mc(s, block+x, mctmp, s->obmc_weight[2], plane, dstx, dsty);
1659 }
1660 
1661 static void select_dsp_funcs(DiracContext *s, int width, int height, int xblen, int yblen)
1662 {
1663  int idx = 0;
1664  if (xblen > 8)
1665  idx = 1;
1666  if (xblen > 16)
1667  idx = 2;
1668 
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) {
1675  } else {
1676  s->weight_func = NULL;
1677  s->biweight_func = NULL;
1678  }
1679 }
1680 
1682 {
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;
1687 
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 */
1690 
1691  /* no need for hpel if we only have fpel vectors */
1692  if (!s->mv_precision)
1693  return 0;
1694 
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);
1700  }
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;
1703  }
1704 
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);
1712  }
1713  ref->interpolated[plane] = 1;
1714 
1715  return 0;
1716 }
1717 
1718 /**
1719  * Dirac Specification ->
1720  * 13.0 Transform data syntax. transform_data()
1721  */
1723 {
1724  DWTContext d;
1725  int y, i, comp, dsty;
1726  int ret;
1727 
1728  if (s->low_delay) {
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);
1733  }
1734  if (!s->zero_res) {
1735  if ((ret = decode_lowdelay(s)) < 0)
1736  return ret;
1737  }
1738  }
1739 
1740  for (comp = 0; comp < 3; comp++) {
1741  Plane *p = &s->plane[comp];
1743 
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);
1747 
1748  if (!s->zero_res && !s->low_delay)
1749  {
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() */
1752  }
1753  ret = ff_spatial_idwt_init(&d, &p->idwt, s->wavelet_idx+2,
1754  s->wavelet_depth, s->bit_depth);
1755  if (ret < 0)
1756  return ret;
1757 
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,
1763  p->stride,
1764  p->idwt.buf + y*p->idwt.stride,
1765  p->idwt.stride, p->width, 16);
1766  }
1767  } else { /* inter */
1768  int rowheight = p->ybsep*p->stride;
1769 
1770  select_dsp_funcs(s, p->width, p->height, p->xblen, p->yblen);
1771 
1772  for (i = 0; i < s->num_refs; i++) {
1773  int ret = interpolate_refplane(s, s->ref_pics[i], comp, p->width, p->height);
1774  if (ret < 0)
1775  return ret;
1776  }
1777 
1778  memset(s->mctmp, 0, 4*p->yoffset*p->stride);
1779 
1780  dsty = -p->yoffset;
1781  for (y = 0; y < s->blheight; y++) {
1782  int h = 0,
1783  start = FFMAX(dsty, 0);
1784  uint16_t *mctmp = s->mctmp + y*rowheight;
1785  DiracBlock *blocks = s->blmotion + y*s->blwidth;
1786 
1787  init_obmc_weights(s, p, y);
1788 
1789  if (y == s->blheight-1 || start+p->ybsep > p->height)
1790  h = p->height - start;
1791  else
1792  h = p->ybsep - (start - dsty);
1793  if (h < 0)
1794  break;
1795 
1796  memset(mctmp+2*p->yoffset*p->stride, 0, 2*rowheight);
1797  mc_row(s, blocks, mctmp, comp, dsty);
1798 
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);
1805 
1806  dsty += p->ybsep;
1807  }
1808  }
1809  }
1810 
1811 
1812  return 0;
1813 }
1814 
1816 {
1817  int ret, i;
1818  int chroma_x_shift, chroma_y_shift;
1819  avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_x_shift, &chroma_y_shift);
1820 
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);
1824  if (ret < 0)
1825  return ret;
1826 
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;
1831  }
1832  f->width = avctx->width;
1833  f->height = avctx->height;
1834 
1835  return 0;
1836 }
1837 
1838 /**
1839  * Dirac Specification ->
1840  * 11.1.1 Picture Header. picture_header()
1841  */
1843 {
1844  unsigned retire, picnum;
1845  int i, j, ret;
1846  int64_t refdist, refnum;
1847  GetBitContext *gb = &s->gb;
1848 
1849  /* [DIRAC_STD] 11.1.1 Picture Header. picture_header() PICTURE_NUM */
1851 
1852 
1853  av_log(s->avctx,AV_LOG_DEBUG,"PICTURE_NUM: %d\n",picnum);
1854 
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;
1859 
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;
1864 
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);
1872  }
1873 
1874  if (!s->ref_pics[i] || refdist)
1875  av_log(s->avctx, AV_LOG_DEBUG, "Reference not found\n");
1876 
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];
1883  break;
1884  }
1885 
1886  if (!s->ref_pics[i]) {
1887  av_log(s->avctx, AV_LOG_ERROR, "Reference could not be allocated\n");
1888  return AVERROR_INVALIDDATA;
1889  }
1890 
1891  }
1892 
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);
1898 
1899  if (retire_pic)
1900  retire_pic->reference &= DELAYED_PIC_REF;
1901  else
1902  av_log(s->avctx, AV_LOG_DEBUG, "Frame to retire not found\n");
1903  }
1904 
1905  /* if reference array is full, remove the oldest as per the spec */
1907  av_log(s->avctx, AV_LOG_ERROR, "Reference frame overflow\n");
1909  }
1910  }
1911 
1912  if (s->num_refs) {
1913  ret = dirac_unpack_prediction_parameters(s); /* [DIRAC_STD] 11.2 Picture Prediction Data. picture_prediction() */
1914  if (ret < 0)
1915  return ret;
1916  ret = dirac_unpack_block_motion_data(s); /* [DIRAC_STD] 12. Block motion data syntax */
1917  if (ret < 0)
1918  return ret;
1919  }
1920  ret = dirac_unpack_idwt_params(s); /* [DIRAC_STD] 11.3 Wavelet transform data */
1921  if (ret < 0)
1922  return ret;
1923 
1924  init_planes(s);
1925  return 0;
1926 }
1927 
1928 static int get_delayed_pic(DiracContext *s, AVFrame *picture, int *got_frame)
1929 {
1930  DiracFrame *out = s->delay_frames[0];
1931  int i, out_idx = 0;
1932  int ret;
1933 
1934  /* find frame with lowest picture number */
1935  for (i = 1; s->delay_frames[i]; i++)
1937  out = s->delay_frames[i];
1938  out_idx = i;
1939  }
1940 
1941  for (i = out_idx; s->delay_frames[i]; i++)
1942  s->delay_frames[i] = s->delay_frames[i+1];
1943 
1944  if (out) {
1945  out->reference ^= DELAYED_PIC_REF;
1946  *got_frame = 1;
1947  if((ret = av_frame_ref(picture, out->avframe)) < 0)
1948  return ret;
1949  }
1950 
1951  return 0;
1952 }
1953 
1954 /**
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
1958  */
1959 #define DATA_UNIT_HEADER_SIZE 13
1960 
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)
1964 {
1965  DiracContext *s = avctx->priv_data;
1966  DiracFrame *pic = NULL;
1967  AVDiracSeqHeader *dsh;
1968  int ret, i;
1969  uint8_t parse_code;
1970  unsigned tmp;
1971 
1972  if (size < DATA_UNIT_HEADER_SIZE)
1973  return AVERROR_INVALIDDATA;
1974 
1975  parse_code = buf[4];
1976 
1977  init_get_bits(&s->gb, &buf[13], 8*(size - DATA_UNIT_HEADER_SIZE));
1978 
1979  if (parse_code == DIRAC_PCODE_SEQ_HEADER) {
1980  if (s->seen_sequence_header)
1981  return 0;
1982 
1983  /* [DIRAC_STD] 10. Sequence header */
1985  if (ret < 0) {
1986  av_log(avctx, AV_LOG_ERROR, "error parsing sequence header");
1987  return ret;
1988  }
1989 
1990  ret = ff_set_dimensions(avctx, dsh->width, dsh->height);
1991  if (ret < 0) {
1992  av_freep(&dsh);
1993  return ret;
1994  }
1995 
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;
2008  s->seq = *dsh;
2009  av_freep(&dsh);
2010 
2011  s->pshift = s->bit_depth > 8;
2012 
2014 
2015  ret = alloc_sequence_buffers(s);
2016  if (ret < 0)
2017  return ret;
2018 
2019  s->seen_sequence_header = 1;
2020  } else if (parse_code == DIRAC_PCODE_END_SEQ) { /* [DIRAC_STD] End of Sequence */
2022  s->seen_sequence_header = 0;
2023  } else if (parse_code == DIRAC_PCODE_AUX) {
2024  if (buf[13] == 1) { /* encoder implementation/version */
2025  int ver[3];
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;
2031  }
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;
2036  }
2037 
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];
2042  if (!pic) {
2043  av_log(avctx, AV_LOG_ERROR, "framelist full\n");
2044  return AVERROR_INVALIDDATA;
2045  }
2046 
2047  av_frame_unref(pic->avframe);
2048 
2049  /* [DIRAC_STD] Defined in 9.6.1 ... */
2050  tmp = parse_code & 0x03; /* [DIRAC_STD] num_refs() */
2051  if (tmp > 2) {
2052  av_log(avctx, AV_LOG_ERROR, "num_refs of 3\n");
2053  return AVERROR_INVALIDDATA;
2054  }
2055  s->num_refs = tmp;
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 */
2065 
2066  /* VC-2 Low Delay has a different parse code than the Dirac Low Delay */
2067  if (s->version.minor == 2 && parse_code == 0x88)
2068  s->ld_picture = 1;
2069 
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;
2073  }
2074 
2075  if ((ret = get_buffer_with_edge(avctx, pic->avframe, (parse_code & 0x0C) == 0x0C ? AV_GET_BUFFER_FLAG_REF : 0)) < 0)
2076  return ret;
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];
2081 
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);
2084 
2085  /* [DIRAC_STD] 11.1 Picture parse. picture_parse() */
2086  ret = dirac_decode_picture_header(s);
2087  if (ret < 0)
2088  return ret;
2089 
2090  /* [DIRAC_STD] 13.0 Transform data syntax. transform_data() */
2091  ret = dirac_decode_frame_internal(s);
2092  if (ret < 0)
2093  return ret;
2094  }
2095  return 0;
2096 }
2097 
2098 static int dirac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
2099 {
2100  DiracContext *s = avctx->priv_data;
2101  AVFrame *picture = data;
2102  uint8_t *buf = pkt->data;
2103  int buf_size = pkt->size;
2104  int i, buf_idx = 0;
2105  int ret;
2106  unsigned data_unit_size;
2107 
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) {
2112  memset(s->all_frames[i].interpolated, 0, sizeof(s->all_frames[i].interpolated));
2113  }
2114 
2115  s->current_picture = NULL;
2116  *got_frame = 0;
2117 
2118  /* end of stream, so flush delayed pics */
2119  if (buf_size == 0)
2120  return get_delayed_pic(s, (AVFrame *)data, got_frame);
2121 
2122  for (;;) {
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')
2129  break;
2130  }
2131  /* BBCD found or end of data */
2132  if (buf_idx + DATA_UNIT_HEADER_SIZE >= buf_size)
2133  break;
2134 
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)
2139  "Data unit with size %d is larger than input buffer, discarding\n",
2140  data_unit_size);
2141  buf_idx += 4;
2142  continue;
2143  }
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);
2146  if (ret < 0)
2147  {
2148  av_log(s->avctx, AV_LOG_ERROR,"Error in dirac_decode_data_unit\n");
2149  return ret;
2150  }
2151  buf_idx += data_unit_size;
2152  }
2153 
2154  if (!s->current_picture)
2155  return buf_size;
2156 
2158  DiracFrame *delayed_frame = remove_frame(s->delay_frames, s->frame_number);
2159 
2161 
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");
2166 
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;
2170 
2171  delayed_frame = remove_frame(s->delay_frames, min_num);
2173  }
2174 
2175  if (delayed_frame) {
2176  delayed_frame->reference ^= DELAYED_PIC_REF;
2177  if((ret=av_frame_ref(data, delayed_frame->avframe)) < 0)
2178  return ret;
2179  *got_frame = 1;
2180  }
2182  /* The right frame at the right time :-) */
2183  if((ret=av_frame_ref(data, s->current_picture->avframe)) < 0)
2184  return ret;
2185  *got_frame = 1;
2186  }
2187 
2188  if (*got_frame)
2189  s->frame_number = picture->display_picture_number + 1;
2190 
2191  return buf_idx;
2192 }
2193 
2195  .name = "dirac",
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),
2201  .close = dirac_decode_end,
2205 };
const uint8_t ff_interleaved_dirac_golomb_vlc_code[256]
Definition: golomb.c:157
#define CHECKEDREAD(dst, cond, errmsg)
int quant
Definition: cfhd.h:52
int plane
Definition: avisynth_c.h:291
void(* add_obmc)(uint16_t *dst, const uint8_t *src, int stride, const uint8_t *obmc_weight, int yblen)
Definition: diracdec.c:211
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:38
#define NULL
Definition: coverity.c:32
#define UNPACK_ARITH(n, type)
Definition: diracdec.c:461
#define BITS_AVAILABLE(name, gb)
Definition: get_bits.h:135
AVRational framerate
Definition: avcodec.h:3212
const char const char void * val
Definition: avisynth_c.h:634
const int32_t ff_dirac_qscale_tab[116]
Definition: diractab.c:34
struct DiracContext::@43 codeblock[MAX_DWT_LEVELS+1]
#define PARSE_VALUES(type, x, gb, ebits, buf1, buf2)
Definition: diracdec.c:693
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int blheight
Definition: diracdec.c:193
static av_cold int dirac_decode_end(AVCodecContext *avctx)
Definition: diracdec.c:397
static void codeblock(DiracContext *s, SubBand *b, GetBitContext *gb, DiracArith *c, int left, int right, int top, int bottom, int blockcnt_one, int is_arith)
Decode the coeffs in the rectangle defined by left, right, top, bottom [DIRAC_STD] 13...
Definition: diracdec.c:497
enum AVColorRange color_range
Definition: dirac.h:107
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
dirac_weight_func weight_func
Definition: diracdec.c:212
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static void flush(AVCodecContext *avctx)
uint8_t * sbsplit
Definition: diracdec.c:197
#define CTX_SB_DATA
Definition: dirac_arith.h:66
#define CTX_PMODE_REF2
Definition: dirac_arith.h:68
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:260
enum AVColorTransferCharacteristic color_trc
Definition: dirac.h:109
DiracFrame * ref_frames[MAX_REFERENCE_FRAMES+1]
Definition: diracdec.c:218
static int divide3(int x)
Definition: diracdec.c:232
static int dirac_decode_frame_internal(DiracContext *s)
Dirac Specification -> 13.0 Transform data syntax.
Definition: diracdec.c:1722
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:209
DiracVersionInfo version
Definition: dirac.h:112
int ld_picture
Definition: diracdec.c:145
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:217
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int bit_depth
Definition: diracdec.c:137
static void propagate_block_data(DiracBlock *block, int stride, int size)
Copies the current block to the other blocks covered by the current superblock split mode...
Definition: diracdec.c:1339
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2262
dirac_weight_func weight_dirac_pixels_tab[3]
Definition: diracdsp.h:49
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:1468
const char * b
Definition: vf_curves.c:109
#define DELAYED_PIC_REF
Value of Picture.reference when Picture is not a reference picture, but is held for delayed output...
Definition: diracdec.c:64
void ff_dirac_init_arith_decoder(DiracArith *c, GetBitContext *gb, int length)
Definition: dirac_arith.c:86
unsigned width
Definition: diracdec.c:165
#define DATA_UNIT_HEADER_SIZE
Dirac Specification -> 9.6 Parse Info Header Syntax.
Definition: diracdec.c:1959
const uint8_t * buffer
Definition: get_bits.h:55
int av_log2(unsigned v)
Definition: intmath.c:26
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:53
uint8_t * buf
Definition: dirac_dwt.h:41
static unsigned svq3_get_ue_golomb(GetBitContext *gb)
Definition: golomb.h:115
uint8_t yoffset
Definition: diracdec.c:118
static void global_mv(DiracContext *s, DiracBlock *block, int x, int y, int ref)
Definition: diracdec.c:1283
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1752
GetBitContext gb
Definition: diracdec.c:755
static int dirac_get_arith_uint(DiracArith *c, int follow_ctx, int data_ctx)
Definition: dirac_arith.h:170
static int alloc_buffers(DiracContext *s, int stride)
Definition: diracdec.c:304
mpegvideo header.
DiracVersionInfo version
Definition: diracdec.c:128
unsigned height
Definition: diracdec.c:166
static int decode_hq_slice(AVCodecContext *avctx, void *arg)
VC-2 Specification -> 13.5.3 hq_slice(sx,sy)
Definition: diracdec.c:808
static AVPacket pkt
#define EDGE_TOP
static void dirac_decode_flush(AVCodecContext *avctx)
Definition: diracdec.c:389
const uint8_t * coeff_data
Definition: diracdec.c:100
static int get_delayed_pic(DiracContext *s, AVFrame *picture, int *got_frame)
Definition: diracdec.c:1928
static int dirac_unpack_idwt_params(DiracContext *s)
Dirac Specification -> 11.3 Wavelet transform data.
Definition: diracdec.c:1101
int profile
profile
Definition: avcodec.h:3028
#define DIRAC_REF_MASK_REF2
Definition: diracdec.c:57
AVCodec.
Definition: avcodec.h:3392
int zrs[2][2]
Definition: diracdec.c:181
unsigned codeblock_mode
Definition: diracdec.c:159
int num_refs
Definition: diracdec.c:148
int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh, const uint8_t *buf, size_t buf_size, void *log_ctx)
Parse a Dirac sequence header.
Definition: dirac.c:398
uint8_t xoffset
Definition: diracdec.c:117
uint8_t * tmp
Definition: dirac_dwt.h:43
unsigned weight_log2denom
Definition: diracdec.c:190
#define CTX_GLOBAL_BLOCK
Definition: dirac_arith.h:69
int width
Definition: cfhd.h:48
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:881
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
DiracFrame * delay_frames[MAX_DELAY+1]
Definition: diracdec.c:219
void(* emulated_edge_mc)(uint8_t *dst, const uint8_t *src, ptrdiff_t dst_linesize, ptrdiff_t src_linesize, int block_w, int block_h, int src_x, int src_y, int w, int h)
Copy a rectangular area of samples to a temporary buffer and replicate the border samples...
Definition: videodsp.h:63
uint8_t * mcscratch
Definition: diracdec.c:204
int dc_prediction
Definition: diracdec.c:146
av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c, AVCodecContext *avctx)
void(* add_rect_clamped)(uint8_t *dst, const uint16_t *src, int stride, const int16_t *idwt, int idwt_stride, int width, int height)
Definition: diracdsp.h:46
uint8_t
#define av_cold
Definition: attributes.h:82
unsigned wavelet_idx
Definition: diracdec.c:152
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:141
Interface to Dirac Decoder/Encoder.
#define CTX_PMODE_REF1
Definition: dirac_arith.h:67
static int coeff_unpack_golomb(GetBitContext *gb, int qfactor, int qoffset)
Definition: diracdec.c:411
#define DIVRNDUP(a, b)
Definition: diracdec.c:69
int hq_picture
Definition: diracdec.c:144
static av_cold int dirac_decode_init(AVCodecContext *avctx)
Definition: diracdec.c:365
uint8_t quant[MAX_DWT_LEVELS][4]
Definition: diracdec.c:171
unsigned prefix_bytes
Definition: diracdec.c:175
AVRational sample_aspect_ratio
Definition: dirac.h:104
unsigned num_x
Definition: diracdec.c:161
int low_delay
Definition: diracdec.c:143
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:375
Plane plane[3]
Definition: diracdec.c:133
static int dirac_get_se_golomb(GetBitContext *gb)
Definition: golomb.h:255
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1467
static void free_sequence_buffers(DiracContext *s)
Definition: diracdec.c:333
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:212
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
Check that the provided sample aspect ratio is valid and set it on the codec context.
Definition: utils.c:224
int height
Definition: dirac_dwt.h:39
bitstream reader API header.
static const uint8_t epel_weights[4][4][4]
Definition: diracdec.c:1471
ptrdiff_t size
Definition: opengl_enc.c:101
uint8_t xblen
Definition: diracdec.c:111
static int decode_lowdelay_slice(AVCodecContext *avctx, void *arg)
Dirac Specification -> 13.5.2 Slices.
Definition: diracdec.c:766
#define CTX_DC_DATA
Definition: dirac_arith.h:73
#define A(x)
Definition: vp56_arith.h:28
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
unsigned m
Definition: audioconvert.c:187
Definition: cfhd.h:43
void(* avg_dirac_pixels_tab[3][4])(uint8_t *dst, const uint8_t *src[5], int stride, int h)
Definition: diracdsp.h:42
static void pred_block_dc(DiracBlock *block, int stride, int x, int y)
Definition: diracdec.c:1214
AVRational framerate
Definition: dirac.h:103
int pan_tilt[2]
Definition: diracdec.c:180
int interpolated[3]
Definition: diracdec.c:73
#define EDGE_WIDTH
Definition: mpegpicture.h:33
#define ROLLOFF(i)
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:607
dirac_subband
Definition: diracdec.c:223
const int32_t ff_dirac_qoffset_intra_tab[120]
Definition: diractab.c:53
av_cold void ff_diracdsp_init(DiracDSPContext *c)
Definition: diracdsp.c:198
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:173
int width
width and height of the video frame
Definition: frame.h:230
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
unsigned length
Definition: diracdec.c:99
static int dirac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
Definition: diracdec.c:2098
void(* dirac_hpel_filter)(uint8_t *dsth, uint8_t *dstv, uint8_t *dstc, const uint8_t *src, int stride, int width, int height)
Definition: diracdsp.h:30
uint8_t * hpel[3][4]
Definition: diracdec.c:74
int slice_x
Definition: diracdec.c:756
static const uint16_t mask[17]
Definition: lzw.c:38
uint16_t * mctmp
Definition: diracdec.c:203
#define AVERROR(e)
Definition: error.h:43
#define DIRAC_REF_MASK_GLOBAL
Definition: diracdec.c:58
int width
Definition: dirac_dwt.h:38
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:154
static int pred_sbsplit(uint8_t *sbsplit, int stride, int x, int y)
Definition: diracdec.c:1184
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
struct DiracContext::@45 highquality
static int add_frame(DiracFrame *framelist[], int maxframes, DiracFrame *frame)
Definition: diracdec.c:255
static void pred_mv(DiracBlock *block, int stride, int x, int y, int ref)
Definition: diracdec.c:1247
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
const char * arg
Definition: jacosubdec.c:66
const uint8_t ff_dirac_default_qmat[7][4][4]
Definition: diractab.c:24
unsigned num_y
Definition: diracdec.c:162
static int interpolate_refplane(DiracContext *s, DiracFrame *ref, int plane, int width, int height)
Definition: diracdec.c:1681
static int mc_subpel(DiracContext *s, DiracBlock *block, const uint8_t *src[5], int x, int y, int ref, int plane)
For block x,y, determine which of the hpel planes to do bilinear interpolation from and set src[] to ...
Definition: diracdec.c:1498
unsigned wavelet_depth
Definition: diracdec.c:151
#define CTX_MV_DATA
Definition: dirac_arith.h:71
int stride
Definition: cfhd.h:46
GLsizei GLsizei * length
Definition: opengl_enc.c:115
const char * name
Name of the codec implementation.
Definition: avcodec.h:3399
DiracFrame * current_picture
Definition: diracdec.c:215
int slice_y
Definition: diracdec.c:757
unsigned old_delta_quant
schroedinger older than 1.0.8 doesn't store quant delta if only one codebook exists in a band ...
Definition: diracdec.c:158
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define CLOSE_READER(name, gb)
Definition: get_bits.h:144
#define FFMAX(a, b)
Definition: common.h:94
static int dirac_decode_data_unit(AVCodecContext *avctx, const uint8_t *buf, int size)
Definition: diracdec.c:1963
const uint8_t ff_interleaved_ue_golomb_vlc_code[256]
Definition: golomb.c:119
DiracDSPContext diracdsp
Definition: diracdec.c:127
int orientation
Definition: cfhd.h:45
#define MAX_BLOCKSIZE
Definition: diracdec.c:51
static char * split(char *message, char delim)
Definition: af_channelmap.c:81
int bytes
Definition: diracdec.c:758
#define SKIP_BITS(name, gb, num)
Definition: get_bits.h:188
#define INTRA_DC_PRED(n, type)
Dirac Specification -> 13.3 intra_dc_prediction(band)
Definition: diracdec.c:574
static void init_planes(DiracContext *s)
Definition: diracdec.c:928
int globalmc_flag
Definition: diracdec.c:147
AVCodec ff_dirac_decoder
Definition: diracdec.c:2194
static void decode_block_params(DiracContext *s, DiracArith arith[8], DiracBlock *block, int stride, int x, int y)
Definition: diracdec.c:1299
SubBand band[DWT_LEVELS][4]
Definition: cfhd.h:68
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:252
uint8_t * ibuf
Definition: cfhd.h:53
#define FFMIN(a, b)
Definition: common.h:96
int display_picture_number
picture number in display order
Definition: frame.h:283
#define CALC_PADDING(size, depth)
Definition: diracdec.c:66
DiracFrame * ref_pics[2]
Definition: diracdec.c:216
void(* avg_pixels_tab[4])(uint8_t *dst, const uint8_t *src[5], int stride, int h)
Definition: diracdec.c:210
static void block_mc(DiracContext *s, DiracBlock *block, uint16_t *mctmp, uint8_t *obmc_weight, int plane, int dstx, int dsty)
Definition: diracdec.c:1609
enum AVColorSpace colorspace
Definition: dirac.h:110
static DiracFrame * remove_frame(DiracFrame *framelist[], int picnum)
Definition: diracdec.c:237
void ff_spatial_idwt_slice2(DWTContext *d, int y)
Definition: dirac_dwt.c:67
int width
picture width / height.
Definition: avcodec.h:1711
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
union DiracBlock::@42 u
int perspective[2]
Definition: diracdec.c:182
static int dirac_unpack_prediction_parameters(DiracContext *s)
Unpack the motion compensation parameters Dirac Specification -> 11.2 Picture prediction data...
Definition: diracdec.c:982
int32_t
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2241
MpegvideoEncDSPContext mpvencdsp
Definition: diracdec.c:125
static void mc_row(DiracContext *s, DiracBlock *block, uint16_t *mctmp, int plane, int dsty)
Definition: diracdec.c:1645
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
int level
level
Definition: avcodec.h:3117
unsigned perspective_exp
Definition: diracdec.c:184
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:194
int chroma_y_shift
Definition: diracdec.c:135
static void select_dsp_funcs(DiracContext *s, int width, int height, int xblen, int yblen)
Definition: diracdec.c:1661
int n
Definition: avisynth_c.h:547
int16_t dc[3]
Definition: diracdec.c:82
#define src
Definition: vp9dsp.c:530
void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: imgconvert.c:38
uint8_t * edge_emu_buffer_base
Definition: diracdec.c:201
static void decode_component(DiracContext *s, int comp)
Dirac Specification -> [DIRAC_STD] 13.4.1 core_transform_data()
Definition: diracdec.c:659
unsigned size_scaler
Definition: diracdec.c:176
static void init_obmc_weights(DiracContext *s, Plane *p, int by)
Definition: diracdec.c:1458
static const float pred[4]
Definition: siprdata.h:259
void(* add_dirac_obmc[3])(uint16_t *dst, const uint8_t *src, int stride, const uint8_t *obmc_weight, int yblen)
Definition: diracdsp.h:47
FILE * out
Definition: movenc-test.c:54
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
uint8_t * buf_base
Definition: dirac_dwt.h:42
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:923
static const int8_t mv[256][2]
Definition: 4xm.c:77
static int get_buffer_with_edge(AVCodecContext *avctx, AVFrame *f, int flags)
Definition: diracdec.c:1815
VideoDSPContext vdsp
Definition: diracdec.c:126
uint8_t ybsep
Definition: diracdec.c:115
static av_always_inline void decode_subband_internal(DiracContext *s, SubBand *b, int is_arith)
Dirac Specification -> 13.4.2 Non-skipped subbands.
Definition: diracdec.c:602
Libavcodec external API header.
uint8_t * edge_emu_buffer[4]
Definition: diracdec.c:200
int seen_sequence_header
Definition: diracdec.c:131
enum AVPixelFormat pix_fmt
Definition: dirac.h:106
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:209
const int ff_dirac_qoffset_inter_tab[122]
Definition: diractab.c:72
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:449
void(* dirac_weight_func)(uint8_t *block, int stride, int log2_denom, int weight, int h)
Definition: diracdsp.h:26
main external API structure.
Definition: avcodec.h:1532
int buffer_stride
Definition: diracdec.c:205
MPEG1/2 tables.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:894
DiracFrame all_frames[MAX_FRAMES]
Definition: diracdec.c:220
#define OPEN_READER(name, gb)
Definition: get_bits.h:133
int reference
Definition: diracdec.c:76
Arithmetic decoder for Dirac.
struct SubBand * parent
Definition: diracdec.c:96
void * buf
Definition: avisynth_c.h:553
dirac_biweight_func biweight_dirac_pixels_tab[3]
Definition: diracdsp.h:50
#define MAX_DWT_LEVELS
The spec limits the number of wavelet decompositions to 4 for both level 1 (VC-2) and 128 (long-gop d...
Definition: dirac.h:45
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:312
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
static int decode_lowdelay(DiracContext *s)
Dirac Specification -> 13.5.1 low_delay_transform_data()
Definition: diracdec.c:848
int core_syntax
Definition: diracdec.c:142
int frame_number
Definition: diracdec.c:132
static int dirac_get_arith_bit(DiracArith *c, int ctx)
Definition: dirac_arith.h:129
AVCodecContext * avctx
Definition: diracdec.c:124
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2255
rational number numerator/denominator
Definition: rational.h:43
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2248
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:418
#define GET_CACHE(name, gb)
Definition: get_bits.h:210
GetBitContext gb
Definition: diracdec.c:129
#define mid_pred
Definition: mathops.h:95
dirac_biweight_func biweight_func
Definition: diracdec.c:213
uint8_t xbsep
Definition: diracdec.c:114
int chroma_x_shift
Definition: diracdec.c:134
AVRational bytes
Definition: diracdec.c:170
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:345
static int decode_subband_arith(AVCodecContext *avctx, void *b)
Definition: diracdec.c:640
static int weight(int i, int blen, int offset)
Definition: diracdec.c:1413
#define MAX_DELAY
Definition: diracdec.c:48
unsigned height
Definition: dirac.h:83
int zero_res
Definition: diracdec.c:140
const uint8_t * quant
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:474
#define MAX_FRAMES
Definition: diracdec.c:49
int pshift
Definition: cfhd.h:51
static int flags
Definition: cpu.c:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:192
uint8_t level
Definition: svq3.c:150
static int pred_block_mode(DiracBlock *block, int stride, int x, int y, int refmask)
Definition: diracdec.c:1198
AVFrame * avframe
Definition: diracdec.c:72
DiracBlock * blmotion
Definition: diracdec.c:198
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:572
#define SHOW_SBITS(name, gb, num)
Definition: get_bits.h:207
#define MAX_REFERENCE_FRAMES
The spec limits this to 3 for frame coding, but in practice can be as high as 6.
Definition: diracdec.c:47
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
static int dirac_decode_picture_header(DiracContext *s)
Dirac Specification -> 11.1.1 Picture Header.
Definition: diracdec.c:1842
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:282
AVDiracSeqHeader seq
Definition: diracdec.c:130
#define AV_WN32(p, v)
Definition: intreadwrite.h:376
struct DiracContext::@44 lowdelay
ptrdiff_t stride
Definition: cfhd.h:59
static double c[64]
static void decode_subband(DiracContext *s, GetBitContext *gb, int quant, int slice_x, int slice_y, int bits_end, SubBand *b1, SubBand *b2)
Definition: diracdec.c:705
static int decode_subband_golomb(AVCodecContext *avctx, void *arg)
Definition: diracdec.c:647
int16_t weight[2]
Definition: diracdec.c:189
int16_t mv[2][2]
Definition: diracdec.c:81
static int dirac_get_arith_int(DiracArith *c, int follow_ctx, int data_ctx)
Definition: dirac_arith.h:185
#define CTX_MV_F1
Definition: dirac_arith.h:70
int sbheight
Definition: diracdec.c:195
int den
denominator
Definition: rational.h:45
static void init_obmc_weight_row(Plane *p, uint8_t *obmc_weight, int stride, int left, int right, int wy)
Definition: diracdec.c:1425
Core video DSP helper functions.
void(* put_dirac_pixels_tab[3][4])(uint8_t *dst, const uint8_t *src[5], int stride, int h)
dirac_pixels_tab[width][subpel] width is 2 for 32, 1 for 16, 0 for 8 subpel is 0 for fpel and hpel (o...
Definition: diracdsp.h:41
#define CTX_DC_F1
Definition: dirac_arith.h:72
void * priv_data
Definition: avcodec.h:1574
DWTPlane idwt
Definition: diracdec.c:104
static int alloc_sequence_buffers(DiracContext *s)
Definition: diracdec.c:266
void(* put_pixels_tab[4])(uint8_t *dst, const uint8_t *src[5], int stride, int h)
Definition: diracdec.c:209
float re
Definition: fft-test.c:73
#define av_free(p)
static void init_obmc_weight(Plane *p, uint8_t *obmc_weight, int stride, int left, int right, int top, int bottom)
Definition: diracdec.c:1439
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
Definition: avcodec.h:2994
struct DiracContext::@46 globalmc[2]
void(* draw_edges)(uint8_t *buf, int wrap, int width, int height, int w, int h, int sides)
#define CTX_SB_F1
Definition: dirac_arith.h:65
void(* dirac_biweight_func)(uint8_t *dst, const uint8_t *src, int stride, int log2_denom, int weightd, int weights, int h)
Definition: diracdsp.h:27
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:247
const uint8_t ff_interleaved_golomb_vlc_len[256]
Definition: golomb.c:100
int height
Definition: cfhd.h:50
static const double coeff[2][5]
Definition: vf_owdenoise.c:71
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:457
#define EDGE_BOTTOM
int width
Definition: cfhd.h:57
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> dc
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:229
int height
Definition: frame.h:230
uint8_t ref
Definition: diracdec.c:84
int is_arith
Definition: diracdec.c:141
#define av_freep(p)
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:83
static void add_dc(uint16_t *dst, int dc, int stride, uint8_t *obmc_weight, int xblen, int yblen)
Definition: diracdec.c:1593
enum AVColorPrimaries color_primaries
Definition: dirac.h:108
void INT64 start
Definition: avisynth_c.h:553
#define AV_WN16(p, v)
Definition: intreadwrite.h:372
#define av_always_inline
Definition: attributes.h:39
#define av_malloc_array(a, b)
uint8_t * hpel_base[3][4]
Definition: diracdec.c:75
unsigned width
Definition: dirac.h:82
#define FFSWAP(type, a, b)
Definition: common.h:99
#define stride
int stride
Definition: dirac_dwt.h:40
int height
Definition: cfhd.h:58
exp golomb vlc stuff
This structure stores compressed data.
Definition: avcodec.h:1444
void(* put_signed_rect_clamped[3])(uint8_t *dst, int dst_stride, const uint8_t *src, int src_stride, int width, int height)
Definition: diracdsp.h:44
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1241
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:856
#define DIRAC_REF_MASK_REF1
DiracBlock->ref flags, if set then the block does MC from the given ref.
Definition: diracdec.c:56
unsigned zrs_exp
Definition: diracdec.c:183
#define FFMAX3(a, b, c)
Definition: common.h:95
uint8_t mv_precision
Definition: diracdec.c:188
static int dirac_unpack_block_motion_data(DiracContext *s)
Dirac Specification ->
Definition: diracdec.c:1358
Definition: cfhd.h:56
uint8_t obmc_weight[3][MAX_BLOCKSIZE *MAX_BLOCKSIZE]
Definition: diracdec.c:207
int ff_spatial_idwt_init(DWTContext *d, DWTPlane *p, enum dwt_type type, int decomposition_count, int bit_depth)
Definition: dirac_dwt.c:36
int level
Definition: cfhd.h:44
uint8_t yblen
Definition: diracdec.c:112
static int width
static int16_t block[64]
Definition: dct-test.c:112