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.

2301 lines
79KB

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