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.

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