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.

2374 lines
82KB

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