You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2286 lines
78KB

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