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.

400 lines
13KB

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