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.

661 lines
23KB

  1. /*
  2. * VC3/DNxHD decoder.
  3. * Copyright (c) 2007 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
  4. * Copyright (c) 2011 MirriAd Ltd
  5. * Copyright (c) 2015 Christophe Gisquet
  6. *
  7. * 10 bit support added by MirriAd Ltd, Joseph Artsimovich <joseph@mirriad.com>
  8. * Slice multithreading and MB interlaced support added by Christophe Gisquet
  9. *
  10. * This file is part of FFmpeg.
  11. *
  12. * FFmpeg is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * FFmpeg is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with FFmpeg; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #include "libavutil/imgutils.h"
  27. #include "libavutil/timer.h"
  28. #include "avcodec.h"
  29. #include "blockdsp.h"
  30. #define UNCHECKED_BITSTREAM_READER 1
  31. #include "get_bits.h"
  32. #include "dnxhddata.h"
  33. #include "idctdsp.h"
  34. #include "internal.h"
  35. #include "thread.h"
  36. typedef struct RowContext {
  37. DECLARE_ALIGNED(16, int16_t, blocks)[12][64];
  38. int luma_scale[64];
  39. int chroma_scale[64];
  40. GetBitContext gb;
  41. int last_dc[3];
  42. int last_qscale;
  43. int errors;
  44. } RowContext;
  45. typedef struct DNXHDContext {
  46. AVCodecContext *avctx;
  47. RowContext *rows;
  48. BlockDSPContext bdsp;
  49. const uint8_t* buf;
  50. int buf_size;
  51. int64_t cid; ///< compression id
  52. unsigned int width, height;
  53. enum AVPixelFormat pix_fmt;
  54. unsigned int mb_width, mb_height;
  55. uint32_t *mb_scan_index;
  56. int data_offset; // End of mb_scan_index, where macroblocks start
  57. int cur_field; ///< current interlaced field
  58. VLC ac_vlc, dc_vlc, run_vlc;
  59. IDCTDSPContext idsp;
  60. ScanTable scantable;
  61. const CIDEntry *cid_table;
  62. int bit_depth; // 8, 10, 12 or 0 if not initialized at all.
  63. int is_444;
  64. int mbaff;
  65. int act;
  66. int (*decode_dct_block)(const struct DNXHDContext *ctx,
  67. RowContext *row, int n);
  68. } DNXHDContext;
  69. #define DNXHD_VLC_BITS 9
  70. #define DNXHD_DC_VLC_BITS 7
  71. static int dnxhd_decode_dct_block_8(const DNXHDContext *ctx,
  72. RowContext *row, int n);
  73. static int dnxhd_decode_dct_block_10(const DNXHDContext *ctx,
  74. RowContext *row, int n);
  75. static int dnxhd_decode_dct_block_10_444(const DNXHDContext *ctx,
  76. RowContext *row, int n);
  77. static int dnxhd_decode_dct_block_12(const DNXHDContext *ctx,
  78. RowContext *row, int n);
  79. static int dnxhd_decode_dct_block_12_444(const DNXHDContext *ctx,
  80. RowContext *row, int n);
  81. static av_cold int dnxhd_decode_init(AVCodecContext *avctx)
  82. {
  83. DNXHDContext *ctx = avctx->priv_data;
  84. ctx->avctx = avctx;
  85. ctx->cid = -1;
  86. avctx->colorspace = AVCOL_SPC_BT709;
  87. avctx->coded_width = FFALIGN(avctx->width, 16);
  88. avctx->coded_height = FFALIGN(avctx->height, 16);
  89. ctx->rows = av_mallocz_array(avctx->thread_count, sizeof(RowContext));
  90. if (!ctx->rows)
  91. return AVERROR(ENOMEM);
  92. return 0;
  93. }
  94. static int dnxhd_init_vlc(DNXHDContext *ctx, uint32_t cid)
  95. {
  96. if (cid != ctx->cid) {
  97. int index;
  98. if ((index = ff_dnxhd_get_cid_table(cid)) < 0) {
  99. av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %d\n", cid);
  100. return AVERROR(ENOSYS);
  101. }
  102. if (ff_dnxhd_cid_table[index].bit_depth != ctx->bit_depth &&
  103. ff_dnxhd_cid_table[index].bit_depth != DNXHD_VARIABLE) {
  104. av_log(ctx->avctx, AV_LOG_ERROR, "bit depth mismatches %d %d\n", ff_dnxhd_cid_table[index].bit_depth, ctx->bit_depth);
  105. return AVERROR_INVALIDDATA;
  106. }
  107. ctx->cid_table = &ff_dnxhd_cid_table[index];
  108. av_log(ctx->avctx, AV_LOG_VERBOSE, "Profile cid %d.\n", cid);
  109. ff_free_vlc(&ctx->ac_vlc);
  110. ff_free_vlc(&ctx->dc_vlc);
  111. ff_free_vlc(&ctx->run_vlc);
  112. init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
  113. ctx->cid_table->ac_bits, 1, 1,
  114. ctx->cid_table->ac_codes, 2, 2, 0);
  115. init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, ctx->bit_depth + 4,
  116. ctx->cid_table->dc_bits, 1, 1,
  117. ctx->cid_table->dc_codes, 1, 1, 0);
  118. init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
  119. ctx->cid_table->run_bits, 1, 1,
  120. ctx->cid_table->run_codes, 2, 2, 0);
  121. ctx->cid = cid;
  122. }
  123. return 0;
  124. }
  125. static av_cold int dnxhd_decode_init_thread_copy(AVCodecContext *avctx)
  126. {
  127. DNXHDContext *ctx = avctx->priv_data;
  128. // make sure VLC tables will be loaded when cid is parsed
  129. ctx->cid = -1;
  130. ctx->rows = av_mallocz_array(avctx->thread_count, sizeof(RowContext));
  131. if (!ctx->rows)
  132. return AVERROR(ENOMEM);
  133. return 0;
  134. }
  135. static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
  136. const uint8_t *buf, int buf_size,
  137. int first_field)
  138. {
  139. static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
  140. static const uint8_t header_prefix444[] = { 0x00, 0x00, 0x02, 0x80, 0x02 };
  141. static const uint8_t header_prefixhr1[] = { 0x00, 0x00, 0x02, 0x80, 0x03 };
  142. static const uint8_t header_prefixhr2[] = { 0x00, 0x00, 0x03, 0x8C, 0x03 };
  143. int i, cid, ret;
  144. int old_bit_depth = ctx->bit_depth;
  145. int old_mb_height = ctx->mb_height;
  146. if (buf_size < 0x280) {
  147. av_log(ctx->avctx, AV_LOG_ERROR,
  148. "buffer too small (%d < 640).\n", buf_size);
  149. return AVERROR_INVALIDDATA;
  150. }
  151. if (memcmp(buf, header_prefix, 5) && memcmp(buf, header_prefix444, 5) &&
  152. memcmp(buf, header_prefixhr1, 5) && memcmp(buf, header_prefixhr2, 5)) {
  153. av_log(ctx->avctx, AV_LOG_ERROR,
  154. "unknown header 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n",
  155. buf[0], buf[1], buf[2], buf[3], buf[4]);
  156. return AVERROR_INVALIDDATA;
  157. }
  158. if (buf[5] & 2) { /* interlaced */
  159. ctx->cur_field = buf[5] & 1;
  160. frame->interlaced_frame = 1;
  161. frame->top_field_first = first_field ^ ctx->cur_field;
  162. av_log(ctx->avctx, AV_LOG_DEBUG,
  163. "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);
  164. } else {
  165. ctx->cur_field = 0;
  166. }
  167. ctx->mbaff = (buf[0x6] >> 5) & 1;
  168. ctx->height = AV_RB16(buf + 0x18);
  169. ctx->width = AV_RB16(buf + 0x1a);
  170. switch(buf[0x21] >> 5) {
  171. case 1: ctx->bit_depth = 8; break;
  172. case 2: ctx->bit_depth = 10; break;
  173. case 3: ctx->bit_depth = 12; break;
  174. default:
  175. av_log(ctx->avctx, AV_LOG_ERROR,
  176. "Unknown bitdepth indicator (%d)\n", buf[0x21] >> 5);
  177. return AVERROR_INVALIDDATA;
  178. }
  179. ctx->avctx->bits_per_raw_sample = ctx->bit_depth;
  180. ctx->is_444 = (buf[0x2C] >> 6) & 1;
  181. if (ctx->is_444) {
  182. if (ctx->bit_depth == 8) {
  183. avpriv_request_sample(ctx->avctx, "4:4:4 8 bits\n");
  184. return AVERROR_INVALIDDATA;
  185. } else if (ctx->bit_depth == 10) {
  186. ctx->decode_dct_block = dnxhd_decode_dct_block_10_444;
  187. ctx->pix_fmt = AV_PIX_FMT_YUV444P10;
  188. } else {
  189. ctx->decode_dct_block = dnxhd_decode_dct_block_12_444;
  190. ctx->pix_fmt = AV_PIX_FMT_YUV444P12;
  191. }
  192. } else if (ctx->bit_depth == 12) {
  193. ctx->decode_dct_block = dnxhd_decode_dct_block_12;
  194. ctx->pix_fmt = AV_PIX_FMT_YUV422P12;
  195. } else if (ctx->bit_depth == 10) {
  196. ctx->decode_dct_block = dnxhd_decode_dct_block_10;
  197. ctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  198. } else {
  199. ctx->decode_dct_block = dnxhd_decode_dct_block_8;
  200. ctx->pix_fmt = AV_PIX_FMT_YUV422P;
  201. }
  202. if (ctx->bit_depth != old_bit_depth) {
  203. ff_blockdsp_init(&ctx->bdsp, ctx->avctx);
  204. ff_idctdsp_init(&ctx->idsp, ctx->avctx);
  205. ff_init_scantable(ctx->idsp.idct_permutation, &ctx->scantable,
  206. ff_zigzag_direct);
  207. }
  208. cid = AV_RB32(buf + 0x28);
  209. if ((ret = dnxhd_init_vlc(ctx, cid)) < 0)
  210. return ret;
  211. if (ctx->mbaff && ctx->cid_table->cid != 1260)
  212. av_log(ctx->avctx, AV_LOG_WARNING,
  213. "Adaptive MB interlace flag in an unsupported profile.\n");
  214. ctx->act = buf[0x2C] & 7;
  215. if (ctx->act && ctx->cid_table->cid != 1256 && ctx->cid_table->cid != 1270)
  216. av_log(ctx->avctx, AV_LOG_WARNING,
  217. "Adaptive color transform in an unsupported profile.\n");
  218. // make sure profile size constraints are respected
  219. // DNx100 allows 1920->1440 and 1280->960 subsampling
  220. if (ctx->width != ctx->cid_table->width &&
  221. ctx->cid_table->width != DNXHD_VARIABLE) {
  222. av_reduce(&ctx->avctx->sample_aspect_ratio.num,
  223. &ctx->avctx->sample_aspect_ratio.den,
  224. ctx->width, ctx->cid_table->width, 255);
  225. ctx->width = ctx->cid_table->width;
  226. }
  227. if (buf_size < ctx->cid_table->coding_unit_size) {
  228. av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size (%d < %d).\n",
  229. buf_size, ctx->cid_table->coding_unit_size);
  230. return AVERROR_INVALIDDATA;
  231. }
  232. ctx->mb_width = (ctx->width + 15)>> 4;
  233. ctx->mb_height = buf[0x16d];
  234. if ((ctx->height + 15) >> 4 == ctx->mb_height && frame->interlaced_frame)
  235. ctx->height <<= 1;
  236. av_log(ctx->avctx, AV_LOG_VERBOSE, "%dx%d, 4:%s %d bits, MBAFF=%d ACT=%d\n",
  237. ctx->width, ctx->height, ctx->is_444 ? "4:4" : "2:2",
  238. ctx->bit_depth, ctx->mbaff, ctx->act);
  239. // Newer format supports variable mb_scan_index sizes
  240. if (!memcmp(buf, header_prefixhr2, 5)) {
  241. ctx->data_offset = 0x170 + (ctx->mb_height << 2);
  242. } else {
  243. if (ctx->mb_height > 68 ||
  244. (ctx->mb_height << frame->interlaced_frame) > (ctx->height + 15) >> 4) {
  245. av_log(ctx->avctx, AV_LOG_ERROR,
  246. "mb height too big: %d\n", ctx->mb_height);
  247. return AVERROR_INVALIDDATA;
  248. }
  249. ctx->data_offset = 0x280;
  250. }
  251. if (buf_size < ctx->data_offset) {
  252. av_log(ctx->avctx, AV_LOG_ERROR,
  253. "buffer too small (%d < %d).\n", buf_size, ctx->data_offset);
  254. return AVERROR_INVALIDDATA;
  255. }
  256. if (ctx->mb_height != old_mb_height) {
  257. av_freep(&ctx->mb_scan_index);
  258. ctx->mb_scan_index = av_mallocz_array(ctx->mb_height, sizeof(uint32_t));
  259. if (!ctx->mb_scan_index)
  260. return AVERROR(ENOMEM);
  261. }
  262. for (i = 0; i < ctx->mb_height; i++) {
  263. ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i << 2));
  264. ff_dlog(ctx->avctx, "mb scan index %d, pos %d: %u\n", i, 0x170 + (i << 2), ctx->mb_scan_index[i]);
  265. if (buf_size - ctx->data_offset < ctx->mb_scan_index[i]) {
  266. av_log(ctx->avctx, AV_LOG_ERROR,
  267. "invalid mb scan index (%u vs %u).\n",
  268. ctx->mb_scan_index[i], buf_size - ctx->data_offset);
  269. return AVERROR_INVALIDDATA;
  270. }
  271. }
  272. return 0;
  273. }
  274. static av_always_inline int dnxhd_decode_dct_block(const DNXHDContext *ctx,
  275. RowContext *row,
  276. int n,
  277. int index_bits,
  278. int level_bias,
  279. int level_shift,
  280. int dc_shift)
  281. {
  282. int i, j, index1, index2, len, flags;
  283. int level, component, sign;
  284. const int *scale;
  285. const uint8_t *weight_matrix;
  286. const uint8_t *ac_level = ctx->cid_table->ac_level;
  287. const uint8_t *ac_flags = ctx->cid_table->ac_flags;
  288. int16_t *block = row->blocks[n];
  289. const int eob_index = ctx->cid_table->eob_index;
  290. int ret = 0;
  291. OPEN_READER(bs, &row->gb);
  292. ctx->bdsp.clear_block(block);
  293. if (!ctx->is_444) {
  294. if (n & 2) {
  295. component = 1 + (n & 1);
  296. scale = row->chroma_scale;
  297. weight_matrix = ctx->cid_table->chroma_weight;
  298. } else {
  299. component = 0;
  300. scale = row->luma_scale;
  301. weight_matrix = ctx->cid_table->luma_weight;
  302. }
  303. } else {
  304. component = (n >> 1) % 3;
  305. if (component) {
  306. scale = row->chroma_scale;
  307. weight_matrix = ctx->cid_table->chroma_weight;
  308. } else {
  309. scale = row->luma_scale;
  310. weight_matrix = ctx->cid_table->luma_weight;
  311. }
  312. }
  313. UPDATE_CACHE(bs, &row->gb);
  314. GET_VLC(len, bs, &row->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
  315. if (len) {
  316. level = GET_CACHE(bs, &row->gb);
  317. LAST_SKIP_BITS(bs, &row->gb, len);
  318. sign = ~level >> 31;
  319. level = (NEG_USR32(sign ^ level, len) ^ sign) - sign;
  320. row->last_dc[component] += level << dc_shift;
  321. }
  322. block[0] = row->last_dc[component];
  323. i = 0;
  324. UPDATE_CACHE(bs, &row->gb);
  325. GET_VLC(index1, bs, &row->gb, ctx->ac_vlc.table,
  326. DNXHD_VLC_BITS, 2);
  327. while (index1 != eob_index) {
  328. level = ac_level[index1];
  329. flags = ac_flags[index1];
  330. sign = SHOW_SBITS(bs, &row->gb, 1);
  331. SKIP_BITS(bs, &row->gb, 1);
  332. if (flags & 1) {
  333. level += SHOW_UBITS(bs, &row->gb, index_bits) << 7;
  334. SKIP_BITS(bs, &row->gb, index_bits);
  335. }
  336. if (flags & 2) {
  337. UPDATE_CACHE(bs, &row->gb);
  338. GET_VLC(index2, bs, &row->gb, ctx->run_vlc.table,
  339. DNXHD_VLC_BITS, 2);
  340. i += ctx->cid_table->run[index2];
  341. }
  342. if (++i > 63) {
  343. av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
  344. ret = -1;
  345. break;
  346. }
  347. j = ctx->scantable.permutated[i];
  348. level *= scale[i];
  349. level += scale[i] >> 1;
  350. if (level_bias < 32 || weight_matrix[i] != level_bias)
  351. level += level_bias; // 1<<(level_shift-1)
  352. level >>= level_shift;
  353. block[j] = (level ^ sign) - sign;
  354. UPDATE_CACHE(bs, &row->gb);
  355. GET_VLC(index1, bs, &row->gb, ctx->ac_vlc.table,
  356. DNXHD_VLC_BITS, 2);
  357. }
  358. CLOSE_READER(bs, &row->gb);
  359. return ret;
  360. }
  361. static int dnxhd_decode_dct_block_8(const DNXHDContext *ctx,
  362. RowContext *row, int n)
  363. {
  364. return dnxhd_decode_dct_block(ctx, row, n, 4, 32, 6, 0);
  365. }
  366. static int dnxhd_decode_dct_block_10(const DNXHDContext *ctx,
  367. RowContext *row, int n)
  368. {
  369. return dnxhd_decode_dct_block(ctx, row, n, 6, 8, 4, 0);
  370. }
  371. static int dnxhd_decode_dct_block_10_444(const DNXHDContext *ctx,
  372. RowContext *row, int n)
  373. {
  374. return dnxhd_decode_dct_block(ctx, row, n, 6, 32, 6, 0);
  375. }
  376. static int dnxhd_decode_dct_block_12(const DNXHDContext *ctx,
  377. RowContext *row, int n)
  378. {
  379. return dnxhd_decode_dct_block(ctx, row, n, 6, 8, 4, 2);
  380. }
  381. static int dnxhd_decode_dct_block_12_444(const DNXHDContext *ctx,
  382. RowContext *row, int n)
  383. {
  384. return dnxhd_decode_dct_block(ctx, row, n, 6, 32, 4, 2);
  385. }
  386. static int dnxhd_decode_macroblock(const DNXHDContext *ctx, RowContext *row,
  387. AVFrame *frame, int x, int y)
  388. {
  389. int shift1 = ctx->bit_depth >= 10;
  390. int dct_linesize_luma = frame->linesize[0];
  391. int dct_linesize_chroma = frame->linesize[1];
  392. uint8_t *dest_y, *dest_u, *dest_v;
  393. int dct_y_offset, dct_x_offset;
  394. int qscale, i, act;
  395. int interlaced_mb = 0;
  396. if (ctx->mbaff) {
  397. interlaced_mb = get_bits1(&row->gb);
  398. qscale = get_bits(&row->gb, 10);
  399. } else {
  400. qscale = get_bits(&row->gb, 11);
  401. }
  402. act = get_bits1(&row->gb);
  403. if (act) {
  404. static int warned = 0;
  405. if (!warned) {
  406. warned = 1;
  407. av_log(ctx->avctx, AV_LOG_ERROR,
  408. "Unsupported adaptive color transform, patch welcome.\n");
  409. }
  410. }
  411. if (qscale != row->last_qscale) {
  412. for (i = 0; i < 64; i++) {
  413. row->luma_scale[i] = qscale * ctx->cid_table->luma_weight[i];
  414. row->chroma_scale[i] = qscale * ctx->cid_table->chroma_weight[i];
  415. }
  416. row->last_qscale = qscale;
  417. }
  418. for (i = 0; i < 8 + 4 * ctx->is_444; i++) {
  419. if (ctx->decode_dct_block(ctx, row, i) < 0)
  420. return AVERROR_INVALIDDATA;
  421. }
  422. if (frame->interlaced_frame) {
  423. dct_linesize_luma <<= 1;
  424. dct_linesize_chroma <<= 1;
  425. }
  426. dest_y = frame->data[0] + ((y * dct_linesize_luma) << 4) + (x << (4 + shift1));
  427. dest_u = frame->data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->is_444));
  428. dest_v = frame->data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->is_444));
  429. if (frame->interlaced_frame && ctx->cur_field) {
  430. dest_y += frame->linesize[0];
  431. dest_u += frame->linesize[1];
  432. dest_v += frame->linesize[2];
  433. }
  434. if (interlaced_mb) {
  435. dct_linesize_luma <<= 1;
  436. dct_linesize_chroma <<= 1;
  437. }
  438. dct_y_offset = interlaced_mb ? frame->linesize[0] : (dct_linesize_luma << 3);
  439. dct_x_offset = 8 << shift1;
  440. if (!ctx->is_444) {
  441. ctx->idsp.idct_put(dest_y, dct_linesize_luma, row->blocks[0]);
  442. ctx->idsp.idct_put(dest_y + dct_x_offset, dct_linesize_luma, row->blocks[1]);
  443. ctx->idsp.idct_put(dest_y + dct_y_offset, dct_linesize_luma, row->blocks[4]);
  444. ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, row->blocks[5]);
  445. if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  446. dct_y_offset = interlaced_mb ? frame->linesize[1] : (dct_linesize_chroma << 3);
  447. ctx->idsp.idct_put(dest_u, dct_linesize_chroma, row->blocks[2]);
  448. ctx->idsp.idct_put(dest_v, dct_linesize_chroma, row->blocks[3]);
  449. ctx->idsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, row->blocks[6]);
  450. ctx->idsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, row->blocks[7]);
  451. }
  452. } else {
  453. ctx->idsp.idct_put(dest_y, dct_linesize_luma, row->blocks[0]);
  454. ctx->idsp.idct_put(dest_y + dct_x_offset, dct_linesize_luma, row->blocks[1]);
  455. ctx->idsp.idct_put(dest_y + dct_y_offset, dct_linesize_luma, row->blocks[6]);
  456. ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, row->blocks[7]);
  457. if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  458. dct_y_offset = interlaced_mb ? frame->linesize[1] : (dct_linesize_chroma << 3);
  459. ctx->idsp.idct_put(dest_u, dct_linesize_chroma, row->blocks[2]);
  460. ctx->idsp.idct_put(dest_u + dct_x_offset, dct_linesize_chroma, row->blocks[3]);
  461. ctx->idsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, row->blocks[8]);
  462. ctx->idsp.idct_put(dest_u + dct_y_offset + dct_x_offset, dct_linesize_chroma, row->blocks[9]);
  463. ctx->idsp.idct_put(dest_v, dct_linesize_chroma, row->blocks[4]);
  464. ctx->idsp.idct_put(dest_v + dct_x_offset, dct_linesize_chroma, row->blocks[5]);
  465. ctx->idsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, row->blocks[10]);
  466. ctx->idsp.idct_put(dest_v + dct_y_offset + dct_x_offset, dct_linesize_chroma, row->blocks[11]);
  467. }
  468. }
  469. return 0;
  470. }
  471. static int dnxhd_decode_row(AVCodecContext *avctx, void *data,
  472. int rownb, int threadnb)
  473. {
  474. const DNXHDContext *ctx = avctx->priv_data;
  475. uint32_t offset = ctx->mb_scan_index[rownb];
  476. RowContext *row = ctx->rows + threadnb;
  477. int x;
  478. row->last_dc[0] =
  479. row->last_dc[1] =
  480. row->last_dc[2] = 1 << (ctx->bit_depth + 2); // for levels +2^(bitdepth-1)
  481. init_get_bits(&row->gb, ctx->buf + offset, (ctx->buf_size - offset) << 3);
  482. for (x = 0; x < ctx->mb_width; x++) {
  483. //START_TIMER;
  484. int ret = dnxhd_decode_macroblock(ctx, row, data, x, rownb);
  485. if (ret < 0) {
  486. row->errors++;
  487. return ret;
  488. }
  489. //STOP_TIMER("decode macroblock");
  490. }
  491. return 0;
  492. }
  493. static int dnxhd_decode_frame(AVCodecContext *avctx, void *data,
  494. int *got_frame, AVPacket *avpkt)
  495. {
  496. const uint8_t *buf = avpkt->data;
  497. int buf_size = avpkt->size;
  498. DNXHDContext *ctx = avctx->priv_data;
  499. ThreadFrame frame = { .f = data };
  500. AVFrame *picture = data;
  501. int first_field = 1;
  502. int ret, i;
  503. ff_dlog(avctx, "frame size %d\n", buf_size);
  504. decode_coding_unit:
  505. if ((ret = dnxhd_decode_header(ctx, picture, buf, buf_size, first_field)) < 0)
  506. return ret;
  507. if ((avctx->width || avctx->height) &&
  508. (ctx->width != avctx->width || ctx->height != avctx->height)) {
  509. av_log(avctx, AV_LOG_WARNING, "frame size changed: %dx%d -> %dx%d\n",
  510. avctx->width, avctx->height, ctx->width, ctx->height);
  511. first_field = 1;
  512. }
  513. if (avctx->pix_fmt != AV_PIX_FMT_NONE && avctx->pix_fmt != ctx->pix_fmt) {
  514. av_log(avctx, AV_LOG_WARNING, "pix_fmt changed: %s -> %s\n",
  515. av_get_pix_fmt_name(avctx->pix_fmt), av_get_pix_fmt_name(ctx->pix_fmt));
  516. first_field = 1;
  517. }
  518. avctx->pix_fmt = ctx->pix_fmt;
  519. ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
  520. if (ret < 0)
  521. return ret;
  522. if (first_field) {
  523. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  524. return ret;
  525. picture->pict_type = AV_PICTURE_TYPE_I;
  526. picture->key_frame = 1;
  527. }
  528. ctx->buf_size = buf_size - ctx->data_offset;
  529. ctx->buf = buf + ctx->data_offset;
  530. avctx->execute2(avctx, dnxhd_decode_row, picture, NULL, ctx->mb_height);
  531. if (first_field && picture->interlaced_frame) {
  532. buf += ctx->cid_table->coding_unit_size;
  533. buf_size -= ctx->cid_table->coding_unit_size;
  534. first_field = 0;
  535. goto decode_coding_unit;
  536. }
  537. ret = 0;
  538. for (i = 0; i < avctx->thread_count; i++) {
  539. ret += ctx->rows[i].errors;
  540. ctx->rows[i].errors = 0;
  541. }
  542. if (ret) {
  543. av_log(ctx->avctx, AV_LOG_ERROR, "%d lines with errors\n", ret);
  544. return AVERROR_INVALIDDATA;
  545. }
  546. *got_frame = 1;
  547. return avpkt->size;
  548. }
  549. static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
  550. {
  551. DNXHDContext *ctx = avctx->priv_data;
  552. ff_free_vlc(&ctx->ac_vlc);
  553. ff_free_vlc(&ctx->dc_vlc);
  554. ff_free_vlc(&ctx->run_vlc);
  555. av_freep(&ctx->mb_scan_index);
  556. av_freep(&ctx->rows);
  557. return 0;
  558. }
  559. AVCodec ff_dnxhd_decoder = {
  560. .name = "dnxhd",
  561. .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
  562. .type = AVMEDIA_TYPE_VIDEO,
  563. .id = AV_CODEC_ID_DNXHD,
  564. .priv_data_size = sizeof(DNXHDContext),
  565. .init = dnxhd_decode_init,
  566. .close = dnxhd_decode_close,
  567. .decode = dnxhd_decode_frame,
  568. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
  569. AV_CODEC_CAP_SLICE_THREADS,
  570. .init_thread_copy = ONLY_IF_THREADS_ENABLED(dnxhd_decode_init_thread_copy),
  571. };