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.

2317 lines
80KB

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