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