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.

517 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 FFmpeg.
  9. *
  10. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. int64_t cid; ///< compression id
  38. unsigned int width, height;
  39. enum AVPixelFormat pix_fmt;
  40. unsigned int mb_width, mb_height;
  41. uint32_t mb_scan_index[68]; /* max for 1080p */
  42. int cur_field; ///< current interlaced field
  43. VLC ac_vlc, dc_vlc, run_vlc;
  44. int last_dc[3];
  45. IDCTDSPContext idsp;
  46. DECLARE_ALIGNED(16, int16_t, blocks)[12][64];
  47. ScanTable scantable;
  48. const CIDEntry *cid_table;
  49. int bit_depth; // 8, 10 or 0 if not initialized at all.
  50. int is_444;
  51. void (*decode_dct_block)(struct DNXHDContext *ctx, int16_t *block,
  52. int n, int qscale);
  53. int last_qscale;
  54. int luma_scale[64];
  55. int chroma_scale[64];
  56. } DNXHDContext;
  57. #define DNXHD_VLC_BITS 9
  58. #define DNXHD_DC_VLC_BITS 7
  59. static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, int16_t *block,
  60. int n, int qscale);
  61. static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, int16_t *block,
  62. int n, int qscale);
  63. static void dnxhd_decode_dct_block_10_444(DNXHDContext *ctx, int16_t *block,
  64. int n, int qscale);
  65. static av_cold int dnxhd_decode_init(AVCodecContext *avctx)
  66. {
  67. DNXHDContext *ctx = avctx->priv_data;
  68. ctx->avctx = avctx;
  69. ctx->cid = -1;
  70. return 0;
  71. }
  72. static int dnxhd_init_vlc(DNXHDContext *ctx, uint32_t cid)
  73. {
  74. if (cid != ctx->cid) {
  75. int index;
  76. if ((index = ff_dnxhd_get_cid_table(cid)) < 0) {
  77. av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %d\n", cid);
  78. return AVERROR(ENOSYS);
  79. }
  80. if (ff_dnxhd_cid_table[index].bit_depth != ctx->bit_depth) {
  81. av_log(ctx->avctx, AV_LOG_ERROR, "bit depth mismatches %d %d\n", ff_dnxhd_cid_table[index].bit_depth, ctx->bit_depth);
  82. return AVERROR_INVALIDDATA;
  83. }
  84. ctx->cid_table = &ff_dnxhd_cid_table[index];
  85. ff_free_vlc(&ctx->ac_vlc);
  86. ff_free_vlc(&ctx->dc_vlc);
  87. ff_free_vlc(&ctx->run_vlc);
  88. init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
  89. ctx->cid_table->ac_bits, 1, 1,
  90. ctx->cid_table->ac_codes, 2, 2, 0);
  91. init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, ctx->bit_depth + 4,
  92. ctx->cid_table->dc_bits, 1, 1,
  93. ctx->cid_table->dc_codes, 1, 1, 0);
  94. init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
  95. ctx->cid_table->run_bits, 1, 1,
  96. ctx->cid_table->run_codes, 2, 2, 0);
  97. ff_init_scantable(ctx->idsp.idct_permutation, &ctx->scantable,
  98. ff_zigzag_direct);
  99. ctx->cid = cid;
  100. }
  101. return 0;
  102. }
  103. static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
  104. const uint8_t *buf, int buf_size,
  105. int first_field)
  106. {
  107. static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
  108. static const uint8_t header_prefix444[] = { 0x00, 0x00, 0x02, 0x80, 0x02 };
  109. int i, cid, ret;
  110. int old_bit_depth = ctx->bit_depth;
  111. if (buf_size < 0x280) {
  112. av_log(ctx->avctx, AV_LOG_ERROR, "buffer too small (%d < 640).\n",
  113. buf_size);
  114. return AVERROR_INVALIDDATA;
  115. }
  116. if (memcmp(buf, header_prefix, 5) && memcmp(buf, header_prefix444, 5)) {
  117. av_log(ctx->avctx, AV_LOG_ERROR, "error in header\n");
  118. return AVERROR_INVALIDDATA;
  119. }
  120. if (buf[5] & 2) { /* interlaced */
  121. ctx->cur_field = buf[5] & 1;
  122. frame->interlaced_frame = 1;
  123. frame->top_field_first = first_field ^ ctx->cur_field;
  124. av_log(ctx->avctx, AV_LOG_DEBUG,
  125. "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);
  126. } else {
  127. ctx->cur_field = 0;
  128. }
  129. ctx->height = AV_RB16(buf + 0x18);
  130. ctx->width = AV_RB16(buf + 0x1a);
  131. av_dlog(ctx->avctx, "width %d, height %d\n", ctx->width, ctx->height);
  132. if (buf[0x21] == 0x58) { /* 10 bit */
  133. ctx->bit_depth = ctx->avctx->bits_per_raw_sample = 10;
  134. if (buf[0x4] == 0x2) {
  135. ctx->decode_dct_block = dnxhd_decode_dct_block_10_444;
  136. ctx->pix_fmt = AV_PIX_FMT_YUV444P10;
  137. ctx->is_444 = 1;
  138. } else {
  139. ctx->decode_dct_block = dnxhd_decode_dct_block_10;
  140. ctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  141. ctx->is_444 = 0;
  142. }
  143. } else if (buf[0x21] == 0x38) { /* 8 bit */
  144. ctx->bit_depth = ctx->avctx->bits_per_raw_sample = 8;
  145. ctx->pix_fmt = AV_PIX_FMT_YUV422P;
  146. ctx->is_444 = 0;
  147. ctx->decode_dct_block = dnxhd_decode_dct_block_8;
  148. } else {
  149. av_log(ctx->avctx, AV_LOG_ERROR, "invalid bit depth value (%d).\n",
  150. buf[0x21]);
  151. return AVERROR_INVALIDDATA;
  152. }
  153. if (ctx->bit_depth != old_bit_depth) {
  154. ff_blockdsp_init(&ctx->bdsp, ctx->avctx);
  155. ff_idctdsp_init(&ctx->idsp, ctx->avctx);
  156. }
  157. cid = AV_RB32(buf + 0x28);
  158. av_dlog(ctx->avctx, "compression id %d\n", cid);
  159. if ((ret = dnxhd_init_vlc(ctx, cid)) < 0)
  160. return ret;
  161. // make sure profile size constraints are respected
  162. // DNx100 allows 1920->1440 and 1280->960 subsampling
  163. if (ctx->width != ctx->cid_table->width) {
  164. av_reduce(&ctx->avctx->sample_aspect_ratio.num,
  165. &ctx->avctx->sample_aspect_ratio.den,
  166. ctx->width, ctx->cid_table->width, 255);
  167. ctx->width = ctx->cid_table->width;
  168. }
  169. if (buf_size < ctx->cid_table->coding_unit_size) {
  170. av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size (%d < %d).\n",
  171. buf_size, ctx->cid_table->coding_unit_size);
  172. return AVERROR_INVALIDDATA;
  173. }
  174. ctx->mb_width = ctx->width >> 4;
  175. ctx->mb_height = buf[0x16d];
  176. av_dlog(ctx->avctx,
  177. "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);
  178. if ((ctx->height + 15) >> 4 == ctx->mb_height && frame->interlaced_frame)
  179. ctx->height <<= 1;
  180. if (ctx->mb_height > 68 ||
  181. (ctx->mb_height << frame->interlaced_frame) > (ctx->height + 15) >> 4) {
  182. av_log(ctx->avctx, AV_LOG_ERROR,
  183. "mb height too big: %d\n", ctx->mb_height);
  184. return AVERROR_INVALIDDATA;
  185. }
  186. for (i = 0; i < ctx->mb_height; i++) {
  187. ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i << 2));
  188. av_dlog(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]);
  189. if (buf_size < ctx->mb_scan_index[i] + 0x280LL) {
  190. av_log(ctx->avctx, AV_LOG_ERROR,
  191. "invalid mb scan index (%d < %d).\n",
  192. buf_size, ctx->mb_scan_index[i] + 0x280);
  193. return AVERROR_INVALIDDATA;
  194. }
  195. }
  196. return 0;
  197. }
  198. static av_always_inline void dnxhd_decode_dct_block(DNXHDContext *ctx,
  199. int16_t *block, int n,
  200. int qscale,
  201. int index_bits,
  202. int level_bias,
  203. int level_shift)
  204. {
  205. int i, j, index1, index2, len, flags;
  206. int level, component, sign;
  207. const int *scale;
  208. const uint8_t *weight_matrix;
  209. const uint8_t *ac_level = ctx->cid_table->ac_level;
  210. const uint8_t *ac_flags = ctx->cid_table->ac_flags;
  211. const int eob_index = ctx->cid_table->eob_index;
  212. OPEN_READER(bs, &ctx->gb);
  213. if (!ctx->is_444) {
  214. if (n & 2) {
  215. component = 1 + (n & 1);
  216. scale = ctx->chroma_scale;
  217. weight_matrix = ctx->cid_table->chroma_weight;
  218. } else {
  219. component = 0;
  220. scale = ctx->luma_scale;
  221. weight_matrix = ctx->cid_table->luma_weight;
  222. }
  223. } else {
  224. component = (n >> 1) % 3;
  225. if (component) {
  226. scale = ctx->chroma_scale;
  227. weight_matrix = ctx->cid_table->chroma_weight;
  228. } else {
  229. scale = ctx->luma_scale;
  230. weight_matrix = ctx->cid_table->luma_weight;
  231. }
  232. }
  233. UPDATE_CACHE(bs, &ctx->gb);
  234. GET_VLC(len, bs, &ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
  235. if (len) {
  236. level = GET_CACHE(bs, &ctx->gb);
  237. LAST_SKIP_BITS(bs, &ctx->gb, len);
  238. sign = ~level >> 31;
  239. level = (NEG_USR32(sign ^ level, len) ^ sign) - sign;
  240. ctx->last_dc[component] += level;
  241. }
  242. block[0] = ctx->last_dc[component];
  243. i = 0;
  244. UPDATE_CACHE(bs, &ctx->gb);
  245. GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,
  246. DNXHD_VLC_BITS, 2);
  247. while (index1 != eob_index) {
  248. level = ac_level[index1];
  249. flags = ac_flags[index1];
  250. sign = SHOW_SBITS(bs, &ctx->gb, 1);
  251. SKIP_BITS(bs, &ctx->gb, 1);
  252. if (flags & 1) {
  253. level += SHOW_UBITS(bs, &ctx->gb, index_bits) << 7;
  254. SKIP_BITS(bs, &ctx->gb, index_bits);
  255. }
  256. if (flags & 2) {
  257. UPDATE_CACHE(bs, &ctx->gb);
  258. GET_VLC(index2, bs, &ctx->gb, ctx->run_vlc.table,
  259. DNXHD_VLC_BITS, 2);
  260. i += ctx->cid_table->run[index2];
  261. }
  262. if (++i > 63) {
  263. av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
  264. break;
  265. }
  266. j = ctx->scantable.permutated[i];
  267. level *= scale[i];
  268. if (level_bias < 32 || weight_matrix[i] != level_bias)
  269. level += level_bias;
  270. level >>= level_shift;
  271. block[j] = (level ^ sign) - sign;
  272. UPDATE_CACHE(bs, &ctx->gb);
  273. GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,
  274. DNXHD_VLC_BITS, 2);
  275. }
  276. CLOSE_READER(bs, &ctx->gb);
  277. }
  278. static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, int16_t *block,
  279. int n, int qscale)
  280. {
  281. dnxhd_decode_dct_block(ctx, block, n, qscale, 4, 32, 6);
  282. }
  283. static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, int16_t *block,
  284. int n, int qscale)
  285. {
  286. dnxhd_decode_dct_block(ctx, block, n, qscale, 6, 8, 4);
  287. }
  288. static void dnxhd_decode_dct_block_10_444(DNXHDContext *ctx, int16_t *block,
  289. int n, int qscale)
  290. {
  291. dnxhd_decode_dct_block(ctx, block, n, qscale, 6, 32, 6);
  292. }
  293. static int dnxhd_decode_macroblock(DNXHDContext *ctx, AVFrame *frame,
  294. int x, int y)
  295. {
  296. int shift1 = ctx->bit_depth == 10;
  297. int dct_linesize_luma = frame->linesize[0];
  298. int dct_linesize_chroma = frame->linesize[1];
  299. uint8_t *dest_y, *dest_u, *dest_v;
  300. int dct_y_offset, dct_x_offset;
  301. int qscale, i;
  302. qscale = get_bits(&ctx->gb, 11);
  303. skip_bits1(&ctx->gb);
  304. if (qscale != ctx->last_qscale) {
  305. for (i = 0; i < 64; i++) {
  306. ctx->luma_scale[i] = qscale * ctx->cid_table->luma_weight[i];
  307. ctx->chroma_scale[i] = qscale * ctx->cid_table->chroma_weight[i];
  308. }
  309. ctx->last_qscale = qscale;
  310. }
  311. for (i = 0; i < 8; i++) {
  312. ctx->bdsp.clear_block(ctx->blocks[i]);
  313. ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
  314. }
  315. if (ctx->is_444) {
  316. for (; i < 12; i++) {
  317. ctx->bdsp.clear_block(ctx->blocks[i]);
  318. ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
  319. }
  320. }
  321. if (frame->interlaced_frame) {
  322. dct_linesize_luma <<= 1;
  323. dct_linesize_chroma <<= 1;
  324. }
  325. dest_y = frame->data[0] + ((y * dct_linesize_luma) << 4) + (x << (4 + shift1));
  326. dest_u = frame->data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->is_444));
  327. dest_v = frame->data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->is_444));
  328. if (frame->interlaced_frame && ctx->cur_field) {
  329. dest_y += frame->linesize[0];
  330. dest_u += frame->linesize[1];
  331. dest_v += frame->linesize[2];
  332. }
  333. dct_y_offset = dct_linesize_luma << 3;
  334. dct_x_offset = 8 << shift1;
  335. if (!ctx->is_444) {
  336. ctx->idsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]);
  337. ctx->idsp.idct_put(dest_y + dct_x_offset, dct_linesize_luma, ctx->blocks[1]);
  338. ctx->idsp.idct_put(dest_y + dct_y_offset, dct_linesize_luma, ctx->blocks[4]);
  339. ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, ctx->blocks[5]);
  340. if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
  341. dct_y_offset = dct_linesize_chroma << 3;
  342. ctx->idsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]);
  343. ctx->idsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[3]);
  344. ctx->idsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, ctx->blocks[6]);
  345. ctx->idsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, ctx->blocks[7]);
  346. }
  347. } else {
  348. ctx->idsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]);
  349. ctx->idsp.idct_put(dest_y + dct_x_offset, dct_linesize_luma, ctx->blocks[1]);
  350. ctx->idsp.idct_put(dest_y + dct_y_offset, dct_linesize_luma, ctx->blocks[6]);
  351. ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, ctx->blocks[7]);
  352. if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
  353. dct_y_offset = dct_linesize_chroma << 3;
  354. ctx->idsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]);
  355. ctx->idsp.idct_put(dest_u + dct_x_offset, dct_linesize_chroma, ctx->blocks[3]);
  356. ctx->idsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, ctx->blocks[8]);
  357. ctx->idsp.idct_put(dest_u + dct_y_offset + dct_x_offset, dct_linesize_chroma, ctx->blocks[9]);
  358. ctx->idsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[4]);
  359. ctx->idsp.idct_put(dest_v + dct_x_offset, dct_linesize_chroma, ctx->blocks[5]);
  360. ctx->idsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, ctx->blocks[10]);
  361. ctx->idsp.idct_put(dest_v + dct_y_offset + dct_x_offset, dct_linesize_chroma, ctx->blocks[11]);
  362. }
  363. }
  364. return 0;
  365. }
  366. static int dnxhd_decode_macroblocks(DNXHDContext *ctx, AVFrame *frame,
  367. const uint8_t *buf, int buf_size)
  368. {
  369. int x, y;
  370. for (y = 0; y < ctx->mb_height; y++) {
  371. ctx->last_dc[0] =
  372. ctx->last_dc[1] =
  373. ctx->last_dc[2] = 1 << (ctx->bit_depth + 2); // for levels +2^(bitdepth-1)
  374. init_get_bits(&ctx->gb, buf + ctx->mb_scan_index[y], (buf_size - ctx->mb_scan_index[y]) << 3);
  375. for (x = 0; x < ctx->mb_width; x++) {
  376. //START_TIMER;
  377. dnxhd_decode_macroblock(ctx, frame, x, y);
  378. //STOP_TIMER("decode macroblock");
  379. }
  380. }
  381. return 0;
  382. }
  383. static int dnxhd_decode_frame(AVCodecContext *avctx, void *data,
  384. int *got_frame, AVPacket *avpkt)
  385. {
  386. const uint8_t *buf = avpkt->data;
  387. int buf_size = avpkt->size;
  388. DNXHDContext *ctx = avctx->priv_data;
  389. ThreadFrame frame = { .f = data };
  390. AVFrame *picture = data;
  391. int first_field = 1;
  392. int ret;
  393. av_dlog(avctx, "frame size %d\n", buf_size);
  394. decode_coding_unit:
  395. if ((ret = dnxhd_decode_header(ctx, picture, buf, buf_size, first_field)) < 0)
  396. return ret;
  397. if ((avctx->width || avctx->height) &&
  398. (ctx->width != avctx->width || ctx->height != avctx->height)) {
  399. av_log(avctx, AV_LOG_WARNING, "frame size changed: %dx%d -> %dx%d\n",
  400. avctx->width, avctx->height, ctx->width, ctx->height);
  401. first_field = 1;
  402. }
  403. if (avctx->pix_fmt != AV_PIX_FMT_NONE && avctx->pix_fmt != ctx->pix_fmt) {
  404. av_log(avctx, AV_LOG_WARNING, "pix_fmt changed: %s -> %s\n",
  405. av_get_pix_fmt_name(avctx->pix_fmt), av_get_pix_fmt_name(ctx->pix_fmt));
  406. first_field = 1;
  407. }
  408. avctx->pix_fmt = ctx->pix_fmt;
  409. ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
  410. if (ret < 0)
  411. return ret;
  412. if (first_field) {
  413. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  414. return ret;
  415. picture->pict_type = AV_PICTURE_TYPE_I;
  416. picture->key_frame = 1;
  417. }
  418. dnxhd_decode_macroblocks(ctx, picture, buf + 0x280, buf_size - 0x280);
  419. if (first_field && picture->interlaced_frame) {
  420. buf += ctx->cid_table->coding_unit_size;
  421. buf_size -= ctx->cid_table->coding_unit_size;
  422. first_field = 0;
  423. goto decode_coding_unit;
  424. }
  425. *got_frame = 1;
  426. return avpkt->size;
  427. }
  428. static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
  429. {
  430. DNXHDContext *ctx = avctx->priv_data;
  431. ff_free_vlc(&ctx->ac_vlc);
  432. ff_free_vlc(&ctx->dc_vlc);
  433. ff_free_vlc(&ctx->run_vlc);
  434. return 0;
  435. }
  436. AVCodec ff_dnxhd_decoder = {
  437. .name = "dnxhd",
  438. .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
  439. .type = AVMEDIA_TYPE_VIDEO,
  440. .id = AV_CODEC_ID_DNXHD,
  441. .priv_data_size = sizeof(DNXHDContext),
  442. .init = dnxhd_decode_init,
  443. .close = dnxhd_decode_close,
  444. .decode = dnxhd_decode_frame,
  445. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  446. };