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.

390 lines
12KB

  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. #include "mpegvideo.h"
  28. typedef struct {
  29. int cid;
  30. unsigned int width, height;
  31. int interlaced;
  32. unsigned int frame_size;
  33. unsigned int coding_unit_size;
  34. int index_bits;
  35. int bit_depth;
  36. const uint8_t *luma_weigth, *chroma_weigth;
  37. const uint8_t *dc_codes, *dc_bits;
  38. const uint16_t *ac_codes;
  39. const uint8_t *ac_bits, *ac_level;
  40. const uint8_t *ac_run_flag, *ac_index_flag;
  41. const uint16_t *run_codes;
  42. const uint8_t *run_bits, *run;
  43. } CIDEntry;
  44. typedef struct {
  45. AVCodecContext *avctx;
  46. AVFrame picture;
  47. GetBitContext gb;
  48. int cid; ///< compression id
  49. unsigned int width, height;
  50. unsigned int mb_width, mb_height;
  51. uint32_t mb_scan_index[68]; /* max for 1080p */
  52. int cur_field; ///< current interlaced field
  53. VLC ac_vlc, dc_vlc, run_vlc;
  54. int last_dc[3];
  55. DSPContext dsp;
  56. DECLARE_ALIGNED_16(DCTELEM, blocks[8][64]);
  57. DECLARE_ALIGNED_8(ScanTable, scantable);
  58. const CIDEntry *cid_table;
  59. } DNXHDContext;
  60. static const CIDEntry cid_table[] = {
  61. { 1237, 1920, 1080, 0, 606208, 606208, 4, 8,
  62. dnxhd_1237_luma_weigth, dnxhd_1237_chroma_weigth,
  63. dnxhd_1237_dc_codes, dnxhd_1237_dc_bits,
  64. dnxhd_1237_ac_codes, dnxhd_1237_ac_bits, dnxhd_1237_ac_level,
  65. dnxhd_1237_ac_run_flag, dnxhd_1237_ac_index_flag,
  66. dnxhd_1237_run_codes, dnxhd_1237_run_bits, dnxhd_1237_run },
  67. { 1238, 1920, 1080, 0, 917504, 917504, 4, 8,
  68. dnxhd_1238_luma_weigth, dnxhd_1238_chroma_weigth,
  69. dnxhd_1238_dc_codes, dnxhd_1238_dc_bits,
  70. dnxhd_1238_ac_codes, dnxhd_1238_ac_bits, dnxhd_1238_ac_level,
  71. dnxhd_1238_ac_run_flag, dnxhd_1238_ac_index_flag,
  72. dnxhd_1238_run_codes, dnxhd_1238_run_bits, dnxhd_1238_run },
  73. { 1243, 1920, 1080, 1, 917504, 458752, 4, 8,
  74. dnxhd_1243_luma_weigth, dnxhd_1243_chroma_weigth,
  75. dnxhd_1238_dc_codes, dnxhd_1238_dc_bits,
  76. dnxhd_1238_ac_codes, dnxhd_1238_ac_bits, dnxhd_1238_ac_level,
  77. dnxhd_1238_ac_run_flag, dnxhd_1238_ac_index_flag,
  78. dnxhd_1238_run_codes, dnxhd_1238_run_bits, dnxhd_1238_run },
  79. };
  80. static int dnxhd_get_cid_table(int cid)
  81. {
  82. int i;
  83. for (i = 0; i < sizeof(cid_table)/sizeof(CIDEntry); i++)
  84. if (cid_table[i].cid == cid)
  85. return i;
  86. return -1;
  87. }
  88. #define DNXHD_VLC_BITS 9
  89. #define DNXHD_DC_VLC_BITS 6
  90. static int dnxhd_decode_init(AVCodecContext *avctx)
  91. {
  92. DNXHDContext *ctx = avctx->priv_data;
  93. ctx->avctx = avctx;
  94. dsputil_init(&ctx->dsp, avctx);
  95. avctx->coded_frame = &ctx->picture;
  96. ctx->picture.type = FF_I_TYPE;
  97. return 0;
  98. }
  99. static int dnxhd_init_vlc(DNXHDContext *ctx, int cid)
  100. {
  101. if (!ctx->cid_table) {
  102. int index;
  103. if ((index = dnxhd_get_cid_table(cid)) < 0) {
  104. av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %d\n", cid);
  105. return -1;
  106. }
  107. ctx->cid_table = &cid_table[index];
  108. init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
  109. ctx->cid_table->ac_bits, 1, 1,
  110. ctx->cid_table->ac_codes, 2, 2, 0);
  111. init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, 12,
  112. ctx->cid_table->dc_bits, 1, 1,
  113. ctx->cid_table->dc_codes, 1, 1, 0);
  114. init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
  115. ctx->cid_table->run_bits, 1, 1,
  116. ctx->cid_table->run_codes, 2, 2, 0);
  117. ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, ff_zigzag_direct);
  118. }
  119. return 0;
  120. }
  121. static int dnxhd_decode_header(DNXHDContext *ctx, uint8_t *buf, int buf_size, int first_field)
  122. {
  123. static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
  124. int i;
  125. if (buf_size < 0x280)
  126. return -1;
  127. if (memcmp(buf, header_prefix, 5)) {
  128. av_log(ctx->avctx, AV_LOG_ERROR, "error in header\n");
  129. return -1;
  130. }
  131. if (buf[5] & 2) { /* interlaced */
  132. ctx->cur_field = buf[5] & 1;
  133. ctx->picture.interlaced_frame = 1;
  134. ctx->picture.top_field_first = first_field && ctx->cur_field == 1;
  135. av_log(ctx->avctx, AV_LOG_DEBUG, "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);
  136. }
  137. ctx->height = AV_RB16(buf + 0x18);
  138. ctx->width = AV_RB16(buf + 0x1a);
  139. dprintf(ctx->avctx, "width %d, heigth %d\n", ctx->width, ctx->height);
  140. if (buf[0x21] & 0x80) {
  141. av_log(ctx->avctx, AV_LOG_ERROR, "10 bit per component\n");
  142. return -1;
  143. }
  144. ctx->cid = AV_RB32(buf + 0x28);
  145. dprintf(ctx->avctx, "compression id %d\n", ctx->cid);
  146. if (dnxhd_init_vlc(ctx, ctx->cid) < 0)
  147. return -1;
  148. if (buf_size < ctx->cid_table->coding_unit_size) {
  149. av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size\n");
  150. return -1;
  151. }
  152. ctx->mb_width = ctx->width>>4;
  153. ctx->mb_height = buf[0x16d];
  154. if (ctx->mb_height > 68) {
  155. av_log(ctx->avctx, AV_LOG_ERROR, "mb height too big\n");
  156. return -1;
  157. }
  158. dprintf(ctx->avctx, "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);
  159. for (i = 0; i < ctx->mb_height; i++) {
  160. ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i<<2));
  161. dprintf(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]);
  162. if (buf_size < ctx->mb_scan_index[i] + 0x280) {
  163. av_log(ctx->avctx, AV_LOG_ERROR, "invalid mb scan index\n");
  164. return -1;
  165. }
  166. }
  167. return 0;
  168. }
  169. static int dnxhd_decode_dc(DNXHDContext *ctx)
  170. {
  171. int len;
  172. len = get_vlc2(&ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
  173. return len ? get_xbits(&ctx->gb, len) : 0;
  174. }
  175. static void dnxhd_decode_dct_block(DNXHDContext *ctx, DCTELEM *block, int n, int qscale)
  176. {
  177. int i, j, index, index2;
  178. int level, component, sign;
  179. const uint8_t *weigth_matrix;
  180. if (n&2) {
  181. component = 1 + (n&1);
  182. weigth_matrix = ctx->cid_table->chroma_weigth;
  183. } else {
  184. component = 0;
  185. weigth_matrix = ctx->cid_table->luma_weigth;
  186. }
  187. ctx->last_dc[component] += dnxhd_decode_dc(ctx);
  188. block[0] = ctx->last_dc[component];
  189. //av_log(ctx->avctx, AV_LOG_DEBUG, "dc %d\n", block[0]);
  190. for (i = 1; ; i++) {
  191. index = get_vlc2(&ctx->gb, ctx->ac_vlc.table, DNXHD_VLC_BITS, 2);
  192. //av_log(ctx->avctx, AV_LOG_DEBUG, "index %d\n", index);
  193. level = ctx->cid_table->ac_level[index];
  194. if (!level) { /* EOB */
  195. //av_log(ctx->avctx, AV_LOG_DEBUG, "EOB\n");
  196. return;
  197. }
  198. sign = get_sbits(&ctx->gb, 1);
  199. if (ctx->cid_table->ac_index_flag[index]) {
  200. level += get_bits(&ctx->gb, ctx->cid_table->index_bits)<<6;
  201. }
  202. if (ctx->cid_table->ac_run_flag[index]) {
  203. index2 = get_vlc2(&ctx->gb, ctx->run_vlc.table, DNXHD_VLC_BITS, 2);
  204. i += ctx->cid_table->run[index2];
  205. }
  206. j = ctx->scantable.permutated[i];
  207. //av_log(ctx->avctx, AV_LOG_DEBUG, "j %d\n", j);
  208. //av_log(ctx->avctx, AV_LOG_DEBUG, "level %d, weigth %d\n", level, weigth_matrix[i]);
  209. level = (2*level+1) * qscale * weigth_matrix[i];
  210. if (weigth_matrix[i] != 32) // FIXME 10bit
  211. level += 32;
  212. level >>= 6;
  213. level = (level^sign) - sign;
  214. if (i > 63) {
  215. av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
  216. return;
  217. }
  218. //av_log(NULL, AV_LOG_DEBUG, "i %d, j %d, end level %d\n", i, j, level);
  219. block[j] = level;
  220. }
  221. }
  222. static int dnxhd_decode_macroblock(DNXHDContext *ctx, int x, int y)
  223. {
  224. int dct_linesize_luma = ctx->picture.linesize[0];
  225. int dct_linesize_chroma = ctx->picture.linesize[1];
  226. uint8_t *dest_y, *dest_u, *dest_v;
  227. int dct_offset;
  228. int qscale, i;
  229. ctx->dsp.clear_blocks(ctx->blocks[0]);
  230. ctx->dsp.clear_blocks(ctx->blocks[2]); // FIXME change clear blocks to take block amount
  231. qscale = get_bits(&ctx->gb, 11);
  232. skip_bits1(&ctx->gb);
  233. //av_log(ctx->avctx, AV_LOG_DEBUG, "qscale %d\n", qscale);
  234. for (i = 0; i < 8; i++) {
  235. dnxhd_decode_dct_block(ctx, ctx->blocks[i], i, qscale);
  236. }
  237. if (ctx->picture.interlaced_frame) {
  238. dct_linesize_luma <<= 1;
  239. dct_linesize_chroma <<= 1;
  240. }
  241. dest_y = ctx->picture.data[0] + ((y * dct_linesize_luma) << 4) + (x << 4);
  242. dest_u = ctx->picture.data[1] + ((y * dct_linesize_chroma) << 4) + (x << 3);
  243. dest_v = ctx->picture.data[2] + ((y * dct_linesize_chroma) << 4) + (x << 3);
  244. if (ctx->cur_field) {
  245. dest_y += ctx->picture.linesize[0];
  246. dest_u += ctx->picture.linesize[1];
  247. dest_v += ctx->picture.linesize[2];
  248. }
  249. dct_offset = dct_linesize_luma << 3;
  250. ctx->dsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]);
  251. ctx->dsp.idct_put(dest_y + 8, dct_linesize_luma, ctx->blocks[1]);
  252. ctx->dsp.idct_put(dest_y + dct_offset, dct_linesize_luma, ctx->blocks[4]);
  253. ctx->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize_luma, ctx->blocks[5]);
  254. if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
  255. dct_offset = dct_linesize_chroma << 3;
  256. ctx->dsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]);
  257. ctx->dsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[3]);
  258. ctx->dsp.idct_put(dest_u + dct_offset, dct_linesize_chroma, ctx->blocks[6]);
  259. ctx->dsp.idct_put(dest_v + dct_offset, dct_linesize_chroma, ctx->blocks[7]);
  260. }
  261. return 0;
  262. }
  263. static int dnxhd_decode_macroblocks(DNXHDContext *ctx, 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] = 1024; // 1024 for levels +128
  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, x, y);
  274. //STOP_TIMER("decode macroblock");
  275. }
  276. }
  277. return 0;
  278. }
  279. static int dnxhd_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  280. uint8_t *buf, int buf_size)
  281. {
  282. DNXHDContext *ctx = avctx->priv_data;
  283. AVFrame *picture = data;
  284. int first_field = 1;
  285. dprintf(avctx, "frame size %d\n", buf_size);
  286. decode_coding_unit:
  287. if (dnxhd_decode_header(ctx, buf, buf_size, first_field) < 0)
  288. return -1;
  289. avctx->pix_fmt = PIX_FMT_YUV422P;
  290. if (avcodec_check_dimensions(avctx, ctx->width, ctx->height))
  291. return -1;
  292. avcodec_set_dimensions(avctx, ctx->width, ctx->height);
  293. if (first_field) {
  294. if (ctx->picture.data[0])
  295. avctx->release_buffer(avctx, &ctx->picture);
  296. if (avctx->get_buffer(avctx, &ctx->picture) < 0) {
  297. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  298. return -1;
  299. }
  300. }
  301. dnxhd_decode_macroblocks(ctx, buf + 0x280, buf_size - 0x280);
  302. if (first_field && ctx->picture.interlaced_frame) {
  303. buf += ctx->cid_table->coding_unit_size;
  304. buf_size -= ctx->cid_table->coding_unit_size;
  305. first_field = 0;
  306. goto decode_coding_unit;
  307. }
  308. *picture = ctx->picture;
  309. *data_size = sizeof(AVPicture);
  310. return buf_size;
  311. }
  312. static int dnxhd_decode_close(AVCodecContext *avctx)
  313. {
  314. DNXHDContext *ctx = avctx->priv_data;
  315. if (ctx->picture.data[0])
  316. avctx->release_buffer(avctx, &ctx->picture);
  317. free_vlc(&ctx->ac_vlc);
  318. free_vlc(&ctx->dc_vlc);
  319. free_vlc(&ctx->run_vlc);
  320. return 0;
  321. }
  322. AVCodec dnxhd_decoder = {
  323. "dnxhd",
  324. CODEC_TYPE_VIDEO,
  325. CODEC_ID_DNXHD,
  326. sizeof(DNXHDContext),
  327. dnxhd_decode_init,
  328. NULL,
  329. dnxhd_decode_close,
  330. dnxhd_decode_frame,
  331. CODEC_CAP_DR1,
  332. };