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.

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