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.

497 lines
18KB

  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. *
  6. * 10 bit support added by MirriAd Ltd, Joseph Artsimovich <joseph@mirriad.com>
  7. *
  8. * This file is part of Libav.
  9. *
  10. * Libav is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * Libav is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with Libav; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/timer.h"
  26. #include "avcodec.h"
  27. #include "blockdsp.h"
  28. #include "get_bits.h"
  29. #include "dnxhddata.h"
  30. #include "idctdsp.h"
  31. #include "internal.h"
  32. #include "thread.h"
  33. typedef struct DNXHDContext {
  34. AVCodecContext *avctx;
  35. GetBitContext gb;
  36. BlockDSPContext bdsp;
  37. int cid; ///< compression id
  38. unsigned int width, height;
  39. unsigned int mb_width, mb_height;
  40. uint32_t mb_scan_index[68]; /* max for 1080p */
  41. int cur_field; ///< current interlaced field
  42. VLC ac_vlc, dc_vlc, run_vlc;
  43. int last_dc[3];
  44. IDCTDSPContext idsp;
  45. DECLARE_ALIGNED(16, int16_t, blocks)[12][64];
  46. ScanTable scantable;
  47. const CIDEntry *cid_table;
  48. int bit_depth; // 8, 10 or 0 if not initialized at all.
  49. int is_444;
  50. int mbaff;
  51. void (*decode_dct_block)(struct DNXHDContext *ctx, int16_t *block,
  52. int n, int qscale);
  53. } DNXHDContext;
  54. #define DNXHD_VLC_BITS 9
  55. #define DNXHD_DC_VLC_BITS 7
  56. static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, int16_t *block,
  57. int n, int qscale);
  58. static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, int16_t *block,
  59. int n, int qscale);
  60. static void dnxhd_decode_dct_block_10_444(DNXHDContext *ctx, int16_t *block,
  61. int n, int qscale);
  62. static av_cold int dnxhd_decode_init(AVCodecContext *avctx)
  63. {
  64. DNXHDContext *ctx = avctx->priv_data;
  65. ctx->avctx = avctx;
  66. return 0;
  67. }
  68. static int dnxhd_init_vlc(DNXHDContext *ctx, int cid)
  69. {
  70. if (cid != ctx->cid) {
  71. int index;
  72. if ((index = ff_dnxhd_get_cid_table(cid)) < 0) {
  73. av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %d\n", cid);
  74. return AVERROR(ENOSYS);
  75. }
  76. ctx->cid_table = &ff_dnxhd_cid_table[index];
  77. av_log(ctx->avctx, AV_LOG_VERBOSE, "Profile cid %d.\n", cid);
  78. ff_free_vlc(&ctx->ac_vlc);
  79. ff_free_vlc(&ctx->dc_vlc);
  80. ff_free_vlc(&ctx->run_vlc);
  81. init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
  82. ctx->cid_table->ac_bits, 1, 1,
  83. ctx->cid_table->ac_codes, 2, 2, 0);
  84. init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, ctx->bit_depth + 4,
  85. ctx->cid_table->dc_bits, 1, 1,
  86. ctx->cid_table->dc_codes, 1, 1, 0);
  87. init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
  88. ctx->cid_table->run_bits, 1, 1,
  89. ctx->cid_table->run_codes, 2, 2, 0);
  90. ff_init_scantable(ctx->idsp.idct_permutation, &ctx->scantable,
  91. ff_zigzag_direct);
  92. ctx->cid = cid;
  93. }
  94. return 0;
  95. }
  96. static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
  97. const uint8_t *buf, int buf_size,
  98. int first_field)
  99. {
  100. static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
  101. static const uint8_t header_prefix444[] = { 0x00, 0x00, 0x02, 0x80, 0x02 };
  102. int i, cid, ret;
  103. int old_bit_depth = ctx->bit_depth;
  104. if (buf_size < 0x280) {
  105. av_log(ctx->avctx, AV_LOG_ERROR, "buffer too small (%d < 640).\n",
  106. buf_size);
  107. return AVERROR_INVALIDDATA;
  108. }
  109. if (memcmp(buf, header_prefix, 5) && memcmp(buf, header_prefix444, 5)) {
  110. av_log(ctx->avctx, AV_LOG_ERROR,
  111. "unknown header 0x%02"PRIX8" 0x%02"PRIX8" 0x%02"PRIX8" 0x%02"PRIX8" 0x%02"PRIX8"\n",
  112. buf[0], buf[1], buf[2], buf[3], buf[4]);
  113. return AVERROR_INVALIDDATA;
  114. }
  115. if (buf[5] & 2) { /* interlaced */
  116. ctx->cur_field = buf[5] & 1;
  117. frame->interlaced_frame = 1;
  118. frame->top_field_first = first_field ^ ctx->cur_field;
  119. av_log(ctx->avctx, AV_LOG_DEBUG,
  120. "interlaced %"PRId8", cur field %d\n", buf[5] & 3, ctx->cur_field);
  121. }
  122. ctx->mbaff = buf[0x6] & 32;
  123. ctx->height = AV_RB16(buf + 0x18);
  124. ctx->width = AV_RB16(buf + 0x1a);
  125. ff_dlog(ctx->avctx, "width %u, height %u\n", ctx->width, ctx->height);
  126. if (buf[0x21] == 0x58) { /* 10 bit */
  127. ctx->bit_depth = ctx->avctx->bits_per_raw_sample = 10;
  128. if (buf[0x4] == 0x2) {
  129. ctx->decode_dct_block = dnxhd_decode_dct_block_10_444;
  130. ctx->avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
  131. ctx->is_444 = 1;
  132. } else {
  133. ctx->decode_dct_block = dnxhd_decode_dct_block_10;
  134. ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  135. }
  136. } else if (buf[0x21] == 0x38) { /* 8 bit */
  137. ctx->bit_depth = ctx->avctx->bits_per_raw_sample = 8;
  138. ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  139. ctx->decode_dct_block = dnxhd_decode_dct_block_8;
  140. } else {
  141. av_log(ctx->avctx, AV_LOG_ERROR, "invalid bit depth value (%"PRId8").\n",
  142. buf[0x21]);
  143. return AVERROR_INVALIDDATA;
  144. }
  145. if (ctx->bit_depth != old_bit_depth) {
  146. ff_blockdsp_init(&ctx->bdsp);
  147. ff_idctdsp_init(&ctx->idsp, ctx->avctx);
  148. }
  149. cid = AV_RB32(buf + 0x28);
  150. ff_dlog(ctx->avctx, "compression id %d\n", cid);
  151. if ((ret = dnxhd_init_vlc(ctx, cid)) < 0)
  152. return ret;
  153. if (ctx->mbaff && ctx->cid_table->cid != 1260)
  154. av_log(ctx->avctx, AV_LOG_WARNING,
  155. "Adaptive MB interlace flag in an unsupported profile.\n");
  156. // make sure profile size constraints are respected
  157. // DNx100 allows 1920->1440 and 1280->960 subsampling
  158. if (ctx->width != ctx->cid_table->width) {
  159. av_reduce(&ctx->avctx->sample_aspect_ratio.num,
  160. &ctx->avctx->sample_aspect_ratio.den,
  161. ctx->width, ctx->cid_table->width, 255);
  162. ctx->width = ctx->cid_table->width;
  163. }
  164. if (buf_size < ctx->cid_table->coding_unit_size) {
  165. av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size (%d < %u).\n",
  166. buf_size, ctx->cid_table->coding_unit_size);
  167. return AVERROR_INVALIDDATA;
  168. }
  169. ctx->mb_width = ctx->width >> 4;
  170. ctx->mb_height = buf[0x16d];
  171. ff_dlog(ctx->avctx,
  172. "mb width %u, mb height %u\n", ctx->mb_width, ctx->mb_height);
  173. if ((ctx->height + 15) >> 4 == ctx->mb_height && frame->interlaced_frame)
  174. ctx->height <<= 1;
  175. if (ctx->mb_height > 68 ||
  176. (ctx->mb_height << frame->interlaced_frame) > (ctx->height + 15) >> 4) {
  177. av_log(ctx->avctx, AV_LOG_ERROR,
  178. "mb height too big: %u\n", ctx->mb_height);
  179. return AVERROR_INVALIDDATA;
  180. }
  181. for (i = 0; i < ctx->mb_height; i++) {
  182. ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i << 2));
  183. ff_dlog(ctx->avctx, "mb scan index %"PRIu32"\n", ctx->mb_scan_index[i]);
  184. if (buf_size < ctx->mb_scan_index[i] + 0x280) {
  185. av_log(ctx->avctx, AV_LOG_ERROR,
  186. "invalid mb scan index (%d < %"PRIu32").\n",
  187. buf_size, ctx->mb_scan_index[i] + 0x280);
  188. return AVERROR_INVALIDDATA;
  189. }
  190. }
  191. return 0;
  192. }
  193. static av_always_inline void dnxhd_decode_dct_block(DNXHDContext *ctx,
  194. int16_t *block, int n,
  195. int qscale,
  196. int index_bits,
  197. int level_bias,
  198. int level_shift)
  199. {
  200. int i, j, index1, index2, len;
  201. int level, component, sign;
  202. const uint8_t *weight_matrix;
  203. OPEN_READER(bs, &ctx->gb);
  204. if (!ctx->is_444) {
  205. if (n & 2) {
  206. component = 1 + (n & 1);
  207. weight_matrix = ctx->cid_table->chroma_weight;
  208. } else {
  209. component = 0;
  210. weight_matrix = ctx->cid_table->luma_weight;
  211. }
  212. } else {
  213. component = (n >> 1) % 3;
  214. if (component) {
  215. weight_matrix = ctx->cid_table->chroma_weight;
  216. } else {
  217. weight_matrix = ctx->cid_table->luma_weight;
  218. }
  219. }
  220. UPDATE_CACHE(bs, &ctx->gb);
  221. GET_VLC(len, bs, &ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
  222. if (len) {
  223. level = GET_CACHE(bs, &ctx->gb);
  224. LAST_SKIP_BITS(bs, &ctx->gb, len);
  225. sign = ~level >> 31;
  226. level = (NEG_USR32(sign ^ level, len) ^ sign) - sign;
  227. ctx->last_dc[component] += level;
  228. }
  229. block[0] = ctx->last_dc[component];
  230. for (i = 1; ; i++) {
  231. UPDATE_CACHE(bs, &ctx->gb);
  232. GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,
  233. DNXHD_VLC_BITS, 2);
  234. level = ctx->cid_table->ac_level[index1];
  235. if (!level) /* EOB */
  236. break;
  237. sign = SHOW_SBITS(bs, &ctx->gb, 1);
  238. SKIP_BITS(bs, &ctx->gb, 1);
  239. if (ctx->cid_table->ac_index_flag[index1]) {
  240. level += SHOW_UBITS(bs, &ctx->gb, index_bits) << 6;
  241. SKIP_BITS(bs, &ctx->gb, index_bits);
  242. }
  243. if (ctx->cid_table->ac_run_flag[index1]) {
  244. UPDATE_CACHE(bs, &ctx->gb);
  245. GET_VLC(index2, bs, &ctx->gb, ctx->run_vlc.table,
  246. DNXHD_VLC_BITS, 2);
  247. i += ctx->cid_table->run[index2];
  248. }
  249. if (i > 63) {
  250. av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
  251. break;
  252. }
  253. j = ctx->scantable.permutated[i];
  254. level = (2 * level + 1) * qscale * weight_matrix[i];
  255. if (level_bias < 32 || weight_matrix[i] != level_bias)
  256. level += level_bias;
  257. level >>= level_shift;
  258. block[j] = (level ^ sign) - sign;
  259. }
  260. CLOSE_READER(bs, &ctx->gb);
  261. }
  262. static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, int16_t *block,
  263. int n, int qscale)
  264. {
  265. dnxhd_decode_dct_block(ctx, block, n, qscale, 4, 32, 6);
  266. }
  267. static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, int16_t *block,
  268. int n, int qscale)
  269. {
  270. dnxhd_decode_dct_block(ctx, block, n, qscale, 6, 8, 4);
  271. }
  272. static void dnxhd_decode_dct_block_10_444(DNXHDContext *ctx, int16_t *block,
  273. int n, int qscale)
  274. {
  275. dnxhd_decode_dct_block(ctx, block, n, qscale, 6, 32, 6);
  276. }
  277. static int dnxhd_decode_macroblock(DNXHDContext *ctx, AVFrame *frame,
  278. int x, int y)
  279. {
  280. int shift1 = ctx->bit_depth == 10;
  281. int dct_linesize_luma = frame->linesize[0];
  282. int dct_linesize_chroma = frame->linesize[1];
  283. uint8_t *dest_y, *dest_u, *dest_v;
  284. int dct_y_offset, dct_x_offset;
  285. int qscale, i;
  286. int interlaced_mb = 0;
  287. if (ctx->mbaff) {
  288. interlaced_mb = get_bits1(&ctx->gb);
  289. qscale = get_bits(&ctx->gb, 10);
  290. } else {
  291. qscale = get_bits(&ctx->gb, 11);
  292. }
  293. skip_bits1(&ctx->gb);
  294. for (i = 0; i < 8; i++) {
  295. ctx->bdsp.clear_block(ctx->blocks[i]);
  296. ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
  297. }
  298. if (ctx->is_444) {
  299. for (; i < 12; i++) {
  300. ctx->bdsp.clear_block(ctx->blocks[i]);
  301. ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
  302. }
  303. }
  304. if (frame->interlaced_frame) {
  305. dct_linesize_luma <<= 1;
  306. dct_linesize_chroma <<= 1;
  307. }
  308. dest_y = frame->data[0] + ((y * dct_linesize_luma) << 4) + (x << (4 + shift1));
  309. dest_u = frame->data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->is_444));
  310. dest_v = frame->data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->is_444));
  311. if (ctx->cur_field) {
  312. dest_y += frame->linesize[0];
  313. dest_u += frame->linesize[1];
  314. dest_v += frame->linesize[2];
  315. }
  316. if (interlaced_mb) {
  317. dct_linesize_luma <<= 1;
  318. dct_linesize_chroma <<= 1;
  319. }
  320. dct_y_offset = interlaced_mb ? frame->linesize[0] : (dct_linesize_luma << 3);
  321. dct_x_offset = 8 << shift1;
  322. if (!ctx->is_444) {
  323. ctx->idsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]);
  324. ctx->idsp.idct_put(dest_y + dct_x_offset, dct_linesize_luma, ctx->blocks[1]);
  325. ctx->idsp.idct_put(dest_y + dct_y_offset, dct_linesize_luma, ctx->blocks[4]);
  326. ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, ctx->blocks[5]);
  327. if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  328. dct_y_offset = interlaced_mb ? frame->linesize[1] : (dct_linesize_chroma << 3);
  329. ctx->idsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]);
  330. ctx->idsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[3]);
  331. ctx->idsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, ctx->blocks[6]);
  332. ctx->idsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, ctx->blocks[7]);
  333. }
  334. } else {
  335. ctx->idsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]);
  336. ctx->idsp.idct_put(dest_y + dct_x_offset, dct_linesize_luma, ctx->blocks[1]);
  337. ctx->idsp.idct_put(dest_y + dct_y_offset, dct_linesize_luma, ctx->blocks[6]);
  338. ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, ctx->blocks[7]);
  339. if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  340. dct_y_offset = interlaced_mb ? frame->linesize[1] : (dct_linesize_chroma << 3);
  341. ctx->idsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]);
  342. ctx->idsp.idct_put(dest_u + dct_x_offset, dct_linesize_chroma, ctx->blocks[3]);
  343. ctx->idsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, ctx->blocks[8]);
  344. ctx->idsp.idct_put(dest_u + dct_y_offset + dct_x_offset, dct_linesize_chroma, ctx->blocks[9]);
  345. ctx->idsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[4]);
  346. ctx->idsp.idct_put(dest_v + dct_x_offset, dct_linesize_chroma, ctx->blocks[5]);
  347. ctx->idsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, ctx->blocks[10]);
  348. ctx->idsp.idct_put(dest_v + dct_y_offset + dct_x_offset, dct_linesize_chroma, ctx->blocks[11]);
  349. }
  350. }
  351. return 0;
  352. }
  353. static int dnxhd_decode_macroblocks(DNXHDContext *ctx, AVFrame *frame,
  354. const uint8_t *buf, int buf_size)
  355. {
  356. int x, y;
  357. for (y = 0; y < ctx->mb_height; y++) {
  358. ctx->last_dc[0] =
  359. ctx->last_dc[1] =
  360. ctx->last_dc[2] = 1 << (ctx->bit_depth + 2); // for levels +2^(bitdepth-1)
  361. init_get_bits(&ctx->gb, buf + ctx->mb_scan_index[y], (buf_size - ctx->mb_scan_index[y]) << 3);
  362. for (x = 0; x < ctx->mb_width; x++) {
  363. //START_TIMER;
  364. dnxhd_decode_macroblock(ctx, frame, x, y);
  365. //STOP_TIMER("decode macroblock");
  366. }
  367. }
  368. return 0;
  369. }
  370. static int dnxhd_decode_frame(AVCodecContext *avctx, void *data,
  371. int *got_frame, AVPacket *avpkt)
  372. {
  373. const uint8_t *buf = avpkt->data;
  374. int buf_size = avpkt->size;
  375. DNXHDContext *ctx = avctx->priv_data;
  376. ThreadFrame tf;
  377. int first_field = 1;
  378. int ret;
  379. tf.f = data;
  380. ff_dlog(avctx, "frame size %d\n", buf_size);
  381. decode_coding_unit:
  382. if ((ret = dnxhd_decode_header(ctx, tf.f, buf, buf_size, first_field)) < 0)
  383. return ret;
  384. if ((avctx->width || avctx->height) &&
  385. (ctx->width != avctx->width || ctx->height != avctx->height)) {
  386. av_log(avctx, AV_LOG_WARNING, "frame size changed: %dx%d -> %ux%u\n",
  387. avctx->width, avctx->height, ctx->width, ctx->height);
  388. first_field = 1;
  389. }
  390. ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
  391. if (ret < 0)
  392. return ret;
  393. if (first_field) {
  394. if ((ret = ff_thread_get_buffer(avctx, &tf, 0)) < 0) {
  395. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  396. return ret;
  397. }
  398. tf.f->pict_type = AV_PICTURE_TYPE_I;
  399. tf.f->key_frame = 1;
  400. }
  401. dnxhd_decode_macroblocks(ctx, tf.f, buf + 0x280, buf_size - 0x280);
  402. if (first_field && tf.f->interlaced_frame) {
  403. buf += ctx->cid_table->coding_unit_size;
  404. buf_size -= ctx->cid_table->coding_unit_size;
  405. first_field = 0;
  406. goto decode_coding_unit;
  407. }
  408. *got_frame = 1;
  409. return buf_size;
  410. }
  411. static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
  412. {
  413. DNXHDContext *ctx = avctx->priv_data;
  414. ff_free_vlc(&ctx->ac_vlc);
  415. ff_free_vlc(&ctx->dc_vlc);
  416. ff_free_vlc(&ctx->run_vlc);
  417. return 0;
  418. }
  419. AVCodec ff_dnxhd_decoder = {
  420. .name = "dnxhd",
  421. .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
  422. .type = AVMEDIA_TYPE_VIDEO,
  423. .id = AV_CODEC_ID_DNXHD,
  424. .priv_data_size = sizeof(DNXHDContext),
  425. .init = dnxhd_decode_init,
  426. .close = dnxhd_decode_close,
  427. .decode = dnxhd_decode_frame,
  428. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  429. };