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.

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