dnxhddec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * VC3/DNxHD decoder.
  3. * Copyright (c) 2007 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. //#define TRACE
  22. //#define DEBUG
  23. #include "avcodec.h"
  24. #include "bitstream.h"
  25. #include "dnxhddata.h"
  26. #include "dsputil.h"
  27. typedef struct {
  28. AVCodecContext *avctx;
  29. AVFrame picture;
  30. GetBitContext gb;
  31. int cid; ///< compression id
  32. unsigned int width, height;
  33. unsigned int mb_width, mb_height;
  34. uint32_t mb_scan_index[68]; /* max for 1080p */
  35. int cur_field; ///< current interlaced field
  36. VLC ac_vlc, dc_vlc, run_vlc;
  37. int last_dc[3];
  38. DSPContext dsp;
  39. DECLARE_ALIGNED_16(DCTELEM, blocks[8][64]);
  40. DECLARE_ALIGNED_8(ScanTable, scantable);
  41. const CIDEntry *cid_table;
  42. } DNXHDContext;
  43. #define DNXHD_VLC_BITS 9
  44. #define DNXHD_DC_VLC_BITS 7
  45. static av_cold int dnxhd_decode_init(AVCodecContext *avctx)
  46. {
  47. DNXHDContext *ctx = avctx->priv_data;
  48. ctx->avctx = avctx;
  49. dsputil_init(&ctx->dsp, avctx);
  50. avctx->coded_frame = &ctx->picture;
  51. ctx->picture.type = FF_I_TYPE;
  52. return 0;
  53. }
  54. static int dnxhd_init_vlc(DNXHDContext *ctx, int cid)
  55. {
  56. if (!ctx->cid_table) {
  57. int index;
  58. if ((index = ff_dnxhd_get_cid_table(cid)) < 0) {
  59. av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %d\n", cid);
  60. return -1;
  61. }
  62. ctx->cid_table = &ff_dnxhd_cid_table[index];
  63. init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
  64. ctx->cid_table->ac_bits, 1, 1,
  65. ctx->cid_table->ac_codes, 2, 2, 0);
  66. init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, ctx->cid_table->bit_depth+4,
  67. ctx->cid_table->dc_bits, 1, 1,
  68. ctx->cid_table->dc_codes, 1, 1, 0);
  69. init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
  70. ctx->cid_table->run_bits, 1, 1,
  71. ctx->cid_table->run_codes, 2, 2, 0);
  72. ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, ff_zigzag_direct);
  73. }
  74. return 0;
  75. }
  76. static int dnxhd_decode_header(DNXHDContext *ctx, const uint8_t *buf, int buf_size, int first_field)
  77. {
  78. static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
  79. int i;
  80. if (buf_size < 0x280)
  81. return -1;
  82. if (memcmp(buf, header_prefix, 5)) {
  83. av_log(ctx->avctx, AV_LOG_ERROR, "error in header\n");
  84. return -1;
  85. }
  86. if (buf[5] & 2) { /* interlaced */
  87. ctx->cur_field = buf[5] & 1;
  88. ctx->picture.interlaced_frame = 1;
  89. ctx->picture.top_field_first = first_field ^ ctx->cur_field;
  90. av_log(ctx->avctx, AV_LOG_DEBUG, "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);
  91. }
  92. ctx->height = AV_RB16(buf + 0x18);
  93. ctx->width = AV_RB16(buf + 0x1a);
  94. dprintf(ctx->avctx, "width %d, heigth %d\n", ctx->width, ctx->height);
  95. if (buf[0x21] & 0x40) {
  96. av_log(ctx->avctx, AV_LOG_ERROR, "10 bit per component\n");
  97. return -1;
  98. }
  99. ctx->cid = AV_RB32(buf + 0x28);
  100. dprintf(ctx->avctx, "compression id %d\n", ctx->cid);
  101. if (dnxhd_init_vlc(ctx, ctx->cid) < 0)
  102. return -1;
  103. if (buf_size < ctx->cid_table->coding_unit_size) {
  104. av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size\n");
  105. return -1;
  106. }
  107. ctx->mb_width = ctx->width>>4;
  108. ctx->mb_height = buf[0x16d];
  109. if (ctx->mb_height > 68) {
  110. av_log(ctx->avctx, AV_LOG_ERROR, "mb height too big\n");
  111. return -1;
  112. }
  113. dprintf(ctx->avctx, "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);
  114. for (i = 0; i < ctx->mb_height; i++) {
  115. ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i<<2));
  116. dprintf(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]);
  117. if (buf_size < ctx->mb_scan_index[i] + 0x280) {
  118. av_log(ctx->avctx, AV_LOG_ERROR, "invalid mb scan index\n");
  119. return -1;
  120. }
  121. }
  122. return 0;
  123. }
  124. static int dnxhd_decode_dc(DNXHDContext *ctx)
  125. {
  126. int len;
  127. len = get_vlc2(&ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
  128. return len ? get_xbits(&ctx->gb, len) : 0;
  129. }
  130. static void dnxhd_decode_dct_block(DNXHDContext *ctx, DCTELEM *block, int n, int qscale)
  131. {
  132. int i, j, index, index2;
  133. int level, component, sign;
  134. const uint8_t *weigth_matrix;
  135. if (n&2) {
  136. component = 1 + (n&1);
  137. weigth_matrix = ctx->cid_table->chroma_weight;
  138. } else {
  139. component = 0;
  140. weigth_matrix = ctx->cid_table->luma_weight;
  141. }
  142. ctx->last_dc[component] += dnxhd_decode_dc(ctx);
  143. block[0] = ctx->last_dc[component];
  144. //av_log(ctx->avctx, AV_LOG_DEBUG, "dc %d\n", block[0]);
  145. for (i = 1; ; i++) {
  146. index = get_vlc2(&ctx->gb, ctx->ac_vlc.table, DNXHD_VLC_BITS, 2);
  147. //av_log(ctx->avctx, AV_LOG_DEBUG, "index %d\n", index);
  148. level = ctx->cid_table->ac_level[index];
  149. if (!level) { /* EOB */
  150. //av_log(ctx->avctx, AV_LOG_DEBUG, "EOB\n");
  151. return;
  152. }
  153. sign = get_sbits(&ctx->gb, 1);
  154. if (ctx->cid_table->ac_index_flag[index]) {
  155. level += get_bits(&ctx->gb, ctx->cid_table->index_bits)<<6;
  156. }
  157. if (ctx->cid_table->ac_run_flag[index]) {
  158. index2 = get_vlc2(&ctx->gb, ctx->run_vlc.table, DNXHD_VLC_BITS, 2);
  159. i += ctx->cid_table->run[index2];
  160. }
  161. if (i > 63) {
  162. av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
  163. return;
  164. }
  165. j = ctx->scantable.permutated[i];
  166. //av_log(ctx->avctx, AV_LOG_DEBUG, "j %d\n", j);
  167. //av_log(ctx->avctx, AV_LOG_DEBUG, "level %d, weigth %d\n", level, weigth_matrix[i]);
  168. level = (2*level+1) * qscale * weigth_matrix[i];
  169. if (ctx->cid_table->bit_depth == 10) {
  170. if (weigth_matrix[i] != 8)
  171. level += 8;
  172. level >>= 4;
  173. } else {
  174. if (weigth_matrix[i] != 32)
  175. level += 32;
  176. level >>= 6;
  177. }
  178. //av_log(NULL, AV_LOG_DEBUG, "i %d, j %d, end level %d\n", i, j, level);
  179. block[j] = (level^sign) - sign;
  180. }
  181. }
  182. static int dnxhd_decode_macroblock(DNXHDContext *ctx, int x, int y)
  183. {
  184. int dct_linesize_luma = ctx->picture.linesize[0];
  185. int dct_linesize_chroma = ctx->picture.linesize[1];
  186. uint8_t *dest_y, *dest_u, *dest_v;
  187. int dct_offset;
  188. int qscale, i;
  189. qscale = get_bits(&ctx->gb, 11);
  190. skip_bits1(&ctx->gb);
  191. //av_log(ctx->avctx, AV_LOG_DEBUG, "qscale %d\n", qscale);
  192. for (i = 0; i < 8; i++) {
  193. ctx->dsp.clear_block(ctx->blocks[i]);
  194. dnxhd_decode_dct_block(ctx, ctx->blocks[i], i, qscale);
  195. }
  196. if (ctx->picture.interlaced_frame) {
  197. dct_linesize_luma <<= 1;
  198. dct_linesize_chroma <<= 1;
  199. }
  200. dest_y = ctx->picture.data[0] + ((y * dct_linesize_luma) << 4) + (x << 4);
  201. dest_u = ctx->picture.data[1] + ((y * dct_linesize_chroma) << 4) + (x << 3);
  202. dest_v = ctx->picture.data[2] + ((y * dct_linesize_chroma) << 4) + (x << 3);
  203. if (ctx->cur_field) {
  204. dest_y += ctx->picture.linesize[0];
  205. dest_u += ctx->picture.linesize[1];
  206. dest_v += ctx->picture.linesize[2];
  207. }
  208. dct_offset = dct_linesize_luma << 3;
  209. ctx->dsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]);
  210. ctx->dsp.idct_put(dest_y + 8, dct_linesize_luma, ctx->blocks[1]);
  211. ctx->dsp.idct_put(dest_y + dct_offset, dct_linesize_luma, ctx->blocks[4]);
  212. ctx->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize_luma, ctx->blocks[5]);
  213. if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
  214. dct_offset = dct_linesize_chroma << 3;
  215. ctx->dsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]);
  216. ctx->dsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[3]);
  217. ctx->dsp.idct_put(dest_u + dct_offset, dct_linesize_chroma, ctx->blocks[6]);
  218. ctx->dsp.idct_put(dest_v + dct_offset, dct_linesize_chroma, ctx->blocks[7]);
  219. }
  220. return 0;
  221. }
  222. static int dnxhd_decode_macroblocks(DNXHDContext *ctx, const uint8_t *buf, int buf_size)
  223. {
  224. int x, y;
  225. for (y = 0; y < ctx->mb_height; y++) {
  226. ctx->last_dc[0] =
  227. ctx->last_dc[1] =
  228. ctx->last_dc[2] = 1<<(ctx->cid_table->bit_depth+2); // for levels +2^(bitdepth-1)
  229. init_get_bits(&ctx->gb, buf + ctx->mb_scan_index[y], (buf_size - ctx->mb_scan_index[y]) << 3);
  230. for (x = 0; x < ctx->mb_width; x++) {
  231. //START_TIMER;
  232. dnxhd_decode_macroblock(ctx, x, y);
  233. //STOP_TIMER("decode macroblock");
  234. }
  235. }
  236. return 0;
  237. }
  238. static int dnxhd_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  239. const uint8_t *buf, int buf_size)
  240. {
  241. DNXHDContext *ctx = avctx->priv_data;
  242. AVFrame *picture = data;
  243. int first_field = 1;
  244. dprintf(avctx, "frame size %d\n", buf_size);
  245. decode_coding_unit:
  246. if (dnxhd_decode_header(ctx, buf, buf_size, first_field) < 0)
  247. return -1;
  248. avctx->pix_fmt = PIX_FMT_YUV422P;
  249. if (avcodec_check_dimensions(avctx, ctx->width, ctx->height))
  250. return -1;
  251. avcodec_set_dimensions(avctx, ctx->width, ctx->height);
  252. if (first_field) {
  253. if (ctx->picture.data[0])
  254. avctx->release_buffer(avctx, &ctx->picture);
  255. if (avctx->get_buffer(avctx, &ctx->picture) < 0) {
  256. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  257. return -1;
  258. }
  259. }
  260. dnxhd_decode_macroblocks(ctx, buf + 0x280, buf_size - 0x280);
  261. if (first_field && ctx->picture.interlaced_frame) {
  262. buf += ctx->cid_table->coding_unit_size;
  263. buf_size -= ctx->cid_table->coding_unit_size;
  264. first_field = 0;
  265. goto decode_coding_unit;
  266. }
  267. *picture = ctx->picture;
  268. *data_size = sizeof(AVPicture);
  269. return buf_size;
  270. }
  271. static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
  272. {
  273. DNXHDContext *ctx = avctx->priv_data;
  274. if (ctx->picture.data[0])
  275. avctx->release_buffer(avctx, &ctx->picture);
  276. free_vlc(&ctx->ac_vlc);
  277. free_vlc(&ctx->dc_vlc);
  278. free_vlc(&ctx->run_vlc);
  279. return 0;
  280. }
  281. AVCodec dnxhd_decoder = {
  282. "dnxhd",
  283. CODEC_TYPE_VIDEO,
  284. CODEC_ID_DNXHD,
  285. sizeof(DNXHDContext),
  286. dnxhd_decode_init,
  287. NULL,
  288. dnxhd_decode_close,
  289. dnxhd_decode_frame,
  290. CODEC_CAP_DR1,
  291. .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
  292. };