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.

1951 lines
67KB

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