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.

359 lines
11KB

  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 "get_bits.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. 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. dprintf(ctx->avctx, "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);
  110. if ((ctx->height+15)>>4 == ctx->mb_height && ctx->picture.interlaced_frame)
  111. ctx->height <<= 1;
  112. if (ctx->mb_height > 68 ||
  113. (ctx->mb_height<<ctx->picture.interlaced_frame) > (ctx->height+15)>>4) {
  114. av_log(ctx->avctx, AV_LOG_ERROR, "mb height too big: %d\n", ctx->mb_height);
  115. return -1;
  116. }
  117. for (i = 0; i < ctx->mb_height; i++) {
  118. ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i<<2));
  119. dprintf(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]);
  120. if (buf_size < ctx->mb_scan_index[i] + 0x280) {
  121. av_log(ctx->avctx, AV_LOG_ERROR, "invalid mb scan index\n");
  122. return -1;
  123. }
  124. }
  125. return 0;
  126. }
  127. static int dnxhd_decode_dc(DNXHDContext *ctx)
  128. {
  129. int len;
  130. len = get_vlc2(&ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
  131. return len ? get_xbits(&ctx->gb, len) : 0;
  132. }
  133. static void dnxhd_decode_dct_block(DNXHDContext *ctx, DCTELEM *block, int n, int qscale)
  134. {
  135. int i, j, index, index2;
  136. int level, component, sign;
  137. const uint8_t *weigth_matrix;
  138. if (n&2) {
  139. component = 1 + (n&1);
  140. weigth_matrix = ctx->cid_table->chroma_weight;
  141. } else {
  142. component = 0;
  143. weigth_matrix = ctx->cid_table->luma_weight;
  144. }
  145. ctx->last_dc[component] += dnxhd_decode_dc(ctx);
  146. block[0] = ctx->last_dc[component];
  147. //av_log(ctx->avctx, AV_LOG_DEBUG, "dc %d\n", block[0]);
  148. for (i = 1; ; i++) {
  149. index = get_vlc2(&ctx->gb, ctx->ac_vlc.table, DNXHD_VLC_BITS, 2);
  150. //av_log(ctx->avctx, AV_LOG_DEBUG, "index %d\n", index);
  151. level = ctx->cid_table->ac_level[index];
  152. if (!level) { /* EOB */
  153. //av_log(ctx->avctx, AV_LOG_DEBUG, "EOB\n");
  154. return;
  155. }
  156. sign = get_sbits(&ctx->gb, 1);
  157. if (ctx->cid_table->ac_index_flag[index]) {
  158. level += get_bits(&ctx->gb, ctx->cid_table->index_bits)<<6;
  159. }
  160. if (ctx->cid_table->ac_run_flag[index]) {
  161. index2 = get_vlc2(&ctx->gb, ctx->run_vlc.table, DNXHD_VLC_BITS, 2);
  162. i += ctx->cid_table->run[index2];
  163. }
  164. if (i > 63) {
  165. av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
  166. return;
  167. }
  168. j = ctx->scantable.permutated[i];
  169. //av_log(ctx->avctx, AV_LOG_DEBUG, "j %d\n", j);
  170. //av_log(ctx->avctx, AV_LOG_DEBUG, "level %d, weigth %d\n", level, weigth_matrix[i]);
  171. level = (2*level+1) * qscale * weigth_matrix[i];
  172. if (ctx->cid_table->bit_depth == 10) {
  173. if (weigth_matrix[i] != 8)
  174. level += 8;
  175. level >>= 4;
  176. } else {
  177. if (weigth_matrix[i] != 32)
  178. level += 32;
  179. level >>= 6;
  180. }
  181. //av_log(NULL, AV_LOG_DEBUG, "i %d, j %d, end level %d\n", i, j, level);
  182. block[j] = (level^sign) - sign;
  183. }
  184. }
  185. static int dnxhd_decode_macroblock(DNXHDContext *ctx, int x, int y)
  186. {
  187. int dct_linesize_luma = ctx->picture.linesize[0];
  188. int dct_linesize_chroma = ctx->picture.linesize[1];
  189. uint8_t *dest_y, *dest_u, *dest_v;
  190. int dct_offset;
  191. int qscale, i;
  192. qscale = get_bits(&ctx->gb, 11);
  193. skip_bits1(&ctx->gb);
  194. //av_log(ctx->avctx, AV_LOG_DEBUG, "qscale %d\n", qscale);
  195. for (i = 0; i < 8; i++) {
  196. ctx->dsp.clear_block(ctx->blocks[i]);
  197. dnxhd_decode_dct_block(ctx, ctx->blocks[i], i, qscale);
  198. }
  199. if (ctx->picture.interlaced_frame) {
  200. dct_linesize_luma <<= 1;
  201. dct_linesize_chroma <<= 1;
  202. }
  203. dest_y = ctx->picture.data[0] + ((y * dct_linesize_luma) << 4) + (x << 4);
  204. dest_u = ctx->picture.data[1] + ((y * dct_linesize_chroma) << 4) + (x << 3);
  205. dest_v = ctx->picture.data[2] + ((y * dct_linesize_chroma) << 4) + (x << 3);
  206. if (ctx->cur_field) {
  207. dest_y += ctx->picture.linesize[0];
  208. dest_u += ctx->picture.linesize[1];
  209. dest_v += ctx->picture.linesize[2];
  210. }
  211. dct_offset = dct_linesize_luma << 3;
  212. ctx->dsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]);
  213. ctx->dsp.idct_put(dest_y + 8, dct_linesize_luma, ctx->blocks[1]);
  214. ctx->dsp.idct_put(dest_y + dct_offset, dct_linesize_luma, ctx->blocks[4]);
  215. ctx->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize_luma, ctx->blocks[5]);
  216. if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
  217. dct_offset = dct_linesize_chroma << 3;
  218. ctx->dsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]);
  219. ctx->dsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[3]);
  220. ctx->dsp.idct_put(dest_u + dct_offset, dct_linesize_chroma, ctx->blocks[6]);
  221. ctx->dsp.idct_put(dest_v + dct_offset, dct_linesize_chroma, ctx->blocks[7]);
  222. }
  223. return 0;
  224. }
  225. static int dnxhd_decode_macroblocks(DNXHDContext *ctx, const uint8_t *buf, int buf_size)
  226. {
  227. int x, y;
  228. for (y = 0; y < ctx->mb_height; y++) {
  229. ctx->last_dc[0] =
  230. ctx->last_dc[1] =
  231. ctx->last_dc[2] = 1<<(ctx->cid_table->bit_depth+2); // for levels +2^(bitdepth-1)
  232. init_get_bits(&ctx->gb, buf + ctx->mb_scan_index[y], (buf_size - ctx->mb_scan_index[y]) << 3);
  233. for (x = 0; x < ctx->mb_width; x++) {
  234. //START_TIMER;
  235. dnxhd_decode_macroblock(ctx, x, y);
  236. //STOP_TIMER("decode macroblock");
  237. }
  238. }
  239. return 0;
  240. }
  241. static int dnxhd_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  242. AVPacket *avpkt)
  243. {
  244. const uint8_t *buf = avpkt->data;
  245. int buf_size = avpkt->size;
  246. DNXHDContext *ctx = avctx->priv_data;
  247. AVFrame *picture = data;
  248. int first_field = 1;
  249. dprintf(avctx, "frame size %d\n", buf_size);
  250. decode_coding_unit:
  251. if (dnxhd_decode_header(ctx, buf, buf_size, first_field) < 0)
  252. return -1;
  253. if ((avctx->width || avctx->height) &&
  254. (ctx->width != avctx->width || ctx->height != avctx->height)) {
  255. av_log(avctx, AV_LOG_WARNING, "frame size changed: %dx%d -> %dx%d\n",
  256. avctx->width, avctx->height, ctx->width, ctx->height);
  257. first_field = 1;
  258. }
  259. avctx->pix_fmt = PIX_FMT_YUV422P;
  260. if (avcodec_check_dimensions(avctx, ctx->width, ctx->height))
  261. return -1;
  262. avcodec_set_dimensions(avctx, ctx->width, ctx->height);
  263. if (first_field) {
  264. if (ctx->picture.data[0])
  265. avctx->release_buffer(avctx, &ctx->picture);
  266. if (avctx->get_buffer(avctx, &ctx->picture) < 0) {
  267. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  268. return -1;
  269. }
  270. }
  271. dnxhd_decode_macroblocks(ctx, buf + 0x280, buf_size - 0x280);
  272. if (first_field && ctx->picture.interlaced_frame) {
  273. buf += ctx->cid_table->coding_unit_size;
  274. buf_size -= ctx->cid_table->coding_unit_size;
  275. first_field = 0;
  276. goto decode_coding_unit;
  277. }
  278. *picture = ctx->picture;
  279. *data_size = sizeof(AVPicture);
  280. return buf_size;
  281. }
  282. static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
  283. {
  284. DNXHDContext *ctx = avctx->priv_data;
  285. if (ctx->picture.data[0])
  286. avctx->release_buffer(avctx, &ctx->picture);
  287. free_vlc(&ctx->ac_vlc);
  288. free_vlc(&ctx->dc_vlc);
  289. free_vlc(&ctx->run_vlc);
  290. return 0;
  291. }
  292. AVCodec dnxhd_decoder = {
  293. "dnxhd",
  294. AVMEDIA_TYPE_VIDEO,
  295. CODEC_ID_DNXHD,
  296. sizeof(DNXHDContext),
  297. dnxhd_decode_init,
  298. NULL,
  299. dnxhd_decode_close,
  300. dnxhd_decode_frame,
  301. CODEC_CAP_DR1,
  302. .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
  303. };