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.

426 lines
14KB

  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 "get_bits.h"
  28. #include "dnxhddata.h"
  29. #include "dsputil.h"
  30. #include "internal.h"
  31. #include "thread.h"
  32. typedef struct DNXHDContext {
  33. AVCodecContext *avctx;
  34. GetBitContext gb;
  35. int64_t cid; ///< compression id
  36. unsigned int width, height;
  37. unsigned int mb_width, mb_height;
  38. uint32_t mb_scan_index[68]; /* max for 1080p */
  39. int cur_field; ///< current interlaced field
  40. VLC ac_vlc, dc_vlc, run_vlc;
  41. int last_dc[3];
  42. DSPContext dsp;
  43. DECLARE_ALIGNED(16, int16_t, blocks)[8][64];
  44. ScanTable scantable;
  45. const CIDEntry *cid_table;
  46. int bit_depth; // 8, 10 or 0 if not initialized at all.
  47. void (*decode_dct_block)(struct DNXHDContext *ctx, int16_t *block,
  48. int n, int qscale);
  49. int last_qscale;
  50. int luma_scale[64];
  51. int chroma_scale[64];
  52. } DNXHDContext;
  53. #define DNXHD_VLC_BITS 9
  54. #define DNXHD_DC_VLC_BITS 7
  55. static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, int16_t *block, int n, int qscale);
  56. static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, int16_t *block, int n, int qscale);
  57. static av_cold int dnxhd_decode_init(AVCodecContext *avctx)
  58. {
  59. DNXHDContext *ctx = avctx->priv_data;
  60. ctx->avctx = avctx;
  61. ctx->cid = -1;
  62. return 0;
  63. }
  64. static int dnxhd_init_vlc(DNXHDContext *ctx, uint32_t cid)
  65. {
  66. if (cid != ctx->cid) {
  67. int index;
  68. if ((index = ff_dnxhd_get_cid_table(cid)) < 0) {
  69. av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %d\n", cid);
  70. return -1;
  71. }
  72. if (ff_dnxhd_cid_table[index].bit_depth != ctx->bit_depth) {
  73. av_log(ctx->avctx, AV_LOG_ERROR, "bit depth mismatches %d %d\n", ff_dnxhd_cid_table[index].bit_depth, ctx->bit_depth);
  74. return AVERROR_INVALIDDATA;
  75. }
  76. ctx->cid_table = &ff_dnxhd_cid_table[index];
  77. ff_free_vlc(&ctx->ac_vlc);
  78. ff_free_vlc(&ctx->dc_vlc);
  79. ff_free_vlc(&ctx->run_vlc);
  80. init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
  81. ctx->cid_table->ac_bits, 1, 1,
  82. ctx->cid_table->ac_codes, 2, 2, 0);
  83. init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, ctx->bit_depth + 4,
  84. ctx->cid_table->dc_bits, 1, 1,
  85. ctx->cid_table->dc_codes, 1, 1, 0);
  86. init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
  87. ctx->cid_table->run_bits, 1, 1,
  88. ctx->cid_table->run_codes, 2, 2, 0);
  89. ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, ff_zigzag_direct);
  90. ctx->cid = cid;
  91. }
  92. return 0;
  93. }
  94. static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
  95. const uint8_t *buf, int buf_size, int first_field)
  96. {
  97. static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
  98. int i, cid;
  99. if (buf_size < 0x280)
  100. return -1;
  101. if (memcmp(buf, header_prefix, 5)) {
  102. av_log(ctx->avctx, AV_LOG_ERROR, "error in header\n");
  103. return -1;
  104. }
  105. if (buf[5] & 2) { /* interlaced */
  106. ctx->cur_field = buf[5] & 1;
  107. frame->interlaced_frame = 1;
  108. frame->top_field_first = first_field ^ ctx->cur_field;
  109. av_log(ctx->avctx, AV_LOG_DEBUG, "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);
  110. }
  111. ctx->height = AV_RB16(buf + 0x18);
  112. ctx->width = AV_RB16(buf + 0x1a);
  113. av_dlog(ctx->avctx, "width %d, height %d\n", ctx->width, ctx->height);
  114. if (buf[0x21] & 0x40) {
  115. ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  116. ctx->avctx->bits_per_raw_sample = 10;
  117. if (ctx->bit_depth != 10) {
  118. ff_dsputil_init(&ctx->dsp, ctx->avctx);
  119. ctx->bit_depth = 10;
  120. ctx->decode_dct_block = dnxhd_decode_dct_block_10;
  121. }
  122. } else {
  123. ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  124. ctx->avctx->bits_per_raw_sample = 8;
  125. if (ctx->bit_depth != 8) {
  126. ff_dsputil_init(&ctx->dsp, ctx->avctx);
  127. ctx->bit_depth = 8;
  128. ctx->decode_dct_block = dnxhd_decode_dct_block_8;
  129. }
  130. }
  131. cid = AV_RB32(buf + 0x28);
  132. av_dlog(ctx->avctx, "compression id %d\n", cid);
  133. if (dnxhd_init_vlc(ctx, cid) < 0)
  134. return -1;
  135. if (buf_size < ctx->cid_table->coding_unit_size) {
  136. av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size\n");
  137. return -1;
  138. }
  139. ctx->mb_width = ctx->width>>4;
  140. ctx->mb_height = buf[0x16d];
  141. av_dlog(ctx->avctx, "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);
  142. if ((ctx->height+15)>>4 == ctx->mb_height && frame->interlaced_frame)
  143. ctx->height <<= 1;
  144. if (ctx->mb_height > 68 ||
  145. (ctx->mb_height << frame->interlaced_frame) > (ctx->height+15)>>4) {
  146. av_log(ctx->avctx, AV_LOG_ERROR, "mb height too big: %d\n", ctx->mb_height);
  147. return -1;
  148. }
  149. for (i = 0; i < ctx->mb_height; i++) {
  150. ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i<<2));
  151. av_dlog(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]);
  152. if (buf_size < ctx->mb_scan_index[i] + 0x280LL) {
  153. av_log(ctx->avctx, AV_LOG_ERROR, "invalid mb scan index\n");
  154. return -1;
  155. }
  156. }
  157. return 0;
  158. }
  159. static av_always_inline void dnxhd_decode_dct_block(DNXHDContext *ctx,
  160. int16_t *block, int n,
  161. int qscale,
  162. int index_bits,
  163. int level_bias,
  164. int level_shift)
  165. {
  166. int i, j, index1, index2, len, flags;
  167. int level, component, sign;
  168. const int *scale;
  169. const uint8_t *weight_matrix;
  170. const uint8_t *ac_level = ctx->cid_table->ac_level;
  171. const uint8_t *ac_flags = ctx->cid_table->ac_flags;
  172. const int eob_index = ctx->cid_table->eob_index;
  173. OPEN_READER(bs, &ctx->gb);
  174. if (n&2) {
  175. component = 1 + (n&1);
  176. scale = ctx->chroma_scale;
  177. weight_matrix = ctx->cid_table->chroma_weight;
  178. } else {
  179. component = 0;
  180. scale = ctx->luma_scale;
  181. weight_matrix = ctx->cid_table->luma_weight;
  182. }
  183. UPDATE_CACHE(bs, &ctx->gb);
  184. GET_VLC(len, bs, &ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
  185. if (len) {
  186. level = GET_CACHE(bs, &ctx->gb);
  187. LAST_SKIP_BITS(bs, &ctx->gb, len);
  188. sign = ~level >> 31;
  189. level = (NEG_USR32(sign ^ level, len) ^ sign) - sign;
  190. ctx->last_dc[component] += level;
  191. }
  192. block[0] = ctx->last_dc[component];
  193. i = 0;
  194. UPDATE_CACHE(bs, &ctx->gb);
  195. GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,
  196. DNXHD_VLC_BITS, 2);
  197. while (index1 != eob_index) {
  198. level = ac_level[index1];
  199. flags = ac_flags[index1];
  200. sign = SHOW_SBITS(bs, &ctx->gb, 1);
  201. SKIP_BITS(bs, &ctx->gb, 1);
  202. if (flags & 1) {
  203. level += SHOW_UBITS(bs, &ctx->gb, index_bits) << 7;
  204. SKIP_BITS(bs, &ctx->gb, index_bits);
  205. }
  206. if (flags & 2) {
  207. UPDATE_CACHE(bs, &ctx->gb);
  208. GET_VLC(index2, bs, &ctx->gb, ctx->run_vlc.table,
  209. DNXHD_VLC_BITS, 2);
  210. i += ctx->cid_table->run[index2];
  211. }
  212. if (++i > 63) {
  213. av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
  214. break;
  215. }
  216. j = ctx->scantable.permutated[i];
  217. level *= scale[i];
  218. if (level_bias < 32 || weight_matrix[i] != level_bias)
  219. level += level_bias;
  220. level >>= level_shift;
  221. block[j] = (level^sign) - sign;
  222. UPDATE_CACHE(bs, &ctx->gb);
  223. GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,
  224. DNXHD_VLC_BITS, 2);
  225. }
  226. CLOSE_READER(bs, &ctx->gb);
  227. }
  228. static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, int16_t *block,
  229. int n, int qscale)
  230. {
  231. dnxhd_decode_dct_block(ctx, block, n, qscale, 4, 32, 6);
  232. }
  233. static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, int16_t *block,
  234. int n, int qscale)
  235. {
  236. dnxhd_decode_dct_block(ctx, block, n, qscale, 6, 8, 4);
  237. }
  238. static int dnxhd_decode_macroblock(DNXHDContext *ctx, AVFrame *frame, int x, int y)
  239. {
  240. int shift1 = ctx->bit_depth == 10;
  241. int dct_linesize_luma = frame->linesize[0];
  242. int dct_linesize_chroma = frame->linesize[1];
  243. uint8_t *dest_y, *dest_u, *dest_v;
  244. int dct_y_offset, dct_x_offset;
  245. int qscale, i;
  246. qscale = get_bits(&ctx->gb, 11);
  247. skip_bits1(&ctx->gb);
  248. if (qscale != ctx->last_qscale) {
  249. for (i = 0; i < 64; i++) {
  250. ctx->luma_scale[i] = qscale * ctx->cid_table->luma_weight[i];
  251. ctx->chroma_scale[i] = qscale * ctx->cid_table->chroma_weight[i];
  252. }
  253. ctx->last_qscale = qscale;
  254. }
  255. for (i = 0; i < 8; i++) {
  256. ctx->dsp.clear_block(ctx->blocks[i]);
  257. ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
  258. }
  259. if (frame->interlaced_frame) {
  260. dct_linesize_luma <<= 1;
  261. dct_linesize_chroma <<= 1;
  262. }
  263. dest_y = frame->data[0] + ((y * dct_linesize_luma) << 4) + (x << (4 + shift1));
  264. dest_u = frame->data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1));
  265. dest_v = frame->data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1));
  266. if (ctx->cur_field) {
  267. dest_y += frame->linesize[0];
  268. dest_u += frame->linesize[1];
  269. dest_v += frame->linesize[2];
  270. }
  271. dct_y_offset = dct_linesize_luma << 3;
  272. dct_x_offset = 8 << shift1;
  273. ctx->dsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]);
  274. ctx->dsp.idct_put(dest_y + dct_x_offset, dct_linesize_luma, ctx->blocks[1]);
  275. ctx->dsp.idct_put(dest_y + dct_y_offset, dct_linesize_luma, ctx->blocks[4]);
  276. ctx->dsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, ctx->blocks[5]);
  277. if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
  278. dct_y_offset = dct_linesize_chroma << 3;
  279. ctx->dsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]);
  280. ctx->dsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[3]);
  281. ctx->dsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, ctx->blocks[6]);
  282. ctx->dsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, ctx->blocks[7]);
  283. }
  284. return 0;
  285. }
  286. static int dnxhd_decode_macroblocks(DNXHDContext *ctx, AVFrame *frame,
  287. const uint8_t *buf, int buf_size)
  288. {
  289. int x, y;
  290. for (y = 0; y < ctx->mb_height; y++) {
  291. ctx->last_dc[0] =
  292. ctx->last_dc[1] =
  293. ctx->last_dc[2] = 1 << (ctx->bit_depth + 2); // for levels +2^(bitdepth-1)
  294. init_get_bits(&ctx->gb, buf + ctx->mb_scan_index[y], (buf_size - ctx->mb_scan_index[y]) << 3);
  295. for (x = 0; x < ctx->mb_width; x++) {
  296. //START_TIMER;
  297. dnxhd_decode_macroblock(ctx, frame, x, y);
  298. //STOP_TIMER("decode macroblock");
  299. }
  300. }
  301. return 0;
  302. }
  303. static int dnxhd_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  304. AVPacket *avpkt)
  305. {
  306. const uint8_t *buf = avpkt->data;
  307. int buf_size = avpkt->size;
  308. DNXHDContext *ctx = avctx->priv_data;
  309. ThreadFrame frame = { .f = data };
  310. AVFrame *picture = data;
  311. int first_field = 1;
  312. int ret;
  313. av_dlog(avctx, "frame size %d\n", buf_size);
  314. decode_coding_unit:
  315. if (dnxhd_decode_header(ctx, picture, buf, buf_size, first_field) < 0)
  316. return -1;
  317. if ((avctx->width || avctx->height) &&
  318. (ctx->width != avctx->width || ctx->height != avctx->height)) {
  319. av_log(avctx, AV_LOG_WARNING, "frame size changed: %dx%d -> %dx%d\n",
  320. avctx->width, avctx->height, ctx->width, ctx->height);
  321. first_field = 1;
  322. }
  323. ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
  324. if (ret < 0)
  325. return ret;
  326. if (first_field) {
  327. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  328. return ret;
  329. picture->pict_type = AV_PICTURE_TYPE_I;
  330. picture->key_frame = 1;
  331. }
  332. dnxhd_decode_macroblocks(ctx, picture, buf + 0x280, buf_size - 0x280);
  333. if (first_field && picture->interlaced_frame) {
  334. buf += ctx->cid_table->coding_unit_size;
  335. buf_size -= ctx->cid_table->coding_unit_size;
  336. first_field = 0;
  337. goto decode_coding_unit;
  338. }
  339. *got_frame = 1;
  340. return avpkt->size;
  341. }
  342. static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
  343. {
  344. DNXHDContext *ctx = avctx->priv_data;
  345. ff_free_vlc(&ctx->ac_vlc);
  346. ff_free_vlc(&ctx->dc_vlc);
  347. ff_free_vlc(&ctx->run_vlc);
  348. return 0;
  349. }
  350. AVCodec ff_dnxhd_decoder = {
  351. .name = "dnxhd",
  352. .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
  353. .type = AVMEDIA_TYPE_VIDEO,
  354. .id = AV_CODEC_ID_DNXHD,
  355. .priv_data_size = sizeof(DNXHDContext),
  356. .init = dnxhd_decode_init,
  357. .close = dnxhd_decode_close,
  358. .decode = dnxhd_decode_frame,
  359. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  360. };