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.

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