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.

2211 lines
75KB

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