3 * Copyright (c) 2007 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
4 * Copyright (c) 2011 MirriAd Ltd
6 * 10 bit support added by MirriAd Ltd, Joseph Artsimovich <joseph@mirriad.com>
8 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/imgutils.h"
26 #include "libavutil/timer.h"
29 #include "dnxhddata.h"
34 typedef struct DNXHDContext {
35 AVCodecContext *avctx;
37 int64_t cid; ///< compression id
38 unsigned int width, height;
39 unsigned int mb_width, mb_height;
40 uint32_t mb_scan_index[68]; /* max for 1080p */
41 int cur_field; ///< current interlaced field
42 VLC ac_vlc, dc_vlc, run_vlc;
45 DECLARE_ALIGNED(16, int16_t, blocks)[12][64];
47 const CIDEntry *cid_table;
48 int bit_depth; // 8, 10 or 0 if not initialized at all.
50 void (*decode_dct_block)(struct DNXHDContext *ctx, int16_t *block,
57 #define DNXHD_VLC_BITS 9
58 #define DNXHD_DC_VLC_BITS 7
60 static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, int16_t *block, int n, int qscale);
61 static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, int16_t *block, int n, int qscale);
62 static void dnxhd_decode_dct_block_10_444(DNXHDContext *ctx, int16_t *block, int n, int qscale);
64 static av_cold int dnxhd_decode_init(AVCodecContext *avctx)
66 DNXHDContext *ctx = avctx->priv_data;
73 static int dnxhd_init_vlc(DNXHDContext *ctx, uint32_t cid)
75 if (cid != ctx->cid) {
78 if ((index = ff_dnxhd_get_cid_table(cid)) < 0) {
79 av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %d\n", cid);
80 return AVERROR(ENOSYS);
82 if (ff_dnxhd_cid_table[index].bit_depth != ctx->bit_depth) {
83 av_log(ctx->avctx, AV_LOG_ERROR, "bit depth mismatches %d %d\n", ff_dnxhd_cid_table[index].bit_depth, ctx->bit_depth);
84 return AVERROR_INVALIDDATA;
86 ctx->cid_table = &ff_dnxhd_cid_table[index];
88 ff_free_vlc(&ctx->ac_vlc);
89 ff_free_vlc(&ctx->dc_vlc);
90 ff_free_vlc(&ctx->run_vlc);
92 init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
93 ctx->cid_table->ac_bits, 1, 1,
94 ctx->cid_table->ac_codes, 2, 2, 0);
95 init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, ctx->bit_depth + 4,
96 ctx->cid_table->dc_bits, 1, 1,
97 ctx->cid_table->dc_codes, 1, 1, 0);
98 init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
99 ctx->cid_table->run_bits, 1, 1,
100 ctx->cid_table->run_codes, 2, 2, 0);
102 ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, ff_zigzag_direct);
108 static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
109 const uint8_t *buf, int buf_size, int first_field)
111 static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
112 static const uint8_t header_prefix444[] = { 0x00, 0x00, 0x02, 0x80, 0x02 };
115 if (buf_size < 0x280)
116 return AVERROR_INVALIDDATA;
118 if (memcmp(buf, header_prefix, 5) && memcmp(buf, header_prefix444, 5)) {
119 av_log(ctx->avctx, AV_LOG_ERROR, "error in header\n");
120 return AVERROR_INVALIDDATA;
122 if (buf[5] & 2) { /* interlaced */
123 ctx->cur_field = buf[5] & 1;
124 frame->interlaced_frame = 1;
125 frame->top_field_first = first_field ^ ctx->cur_field;
126 av_log(ctx->avctx, AV_LOG_DEBUG, "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);
129 ctx->height = AV_RB16(buf + 0x18);
130 ctx->width = AV_RB16(buf + 0x1a);
132 av_dlog(ctx->avctx, "width %d, height %d\n", ctx->width, ctx->height);
135 if (buf[0x4] == 0x2) {
136 ctx->avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
137 ctx->avctx->bits_per_raw_sample = 10;
138 if (ctx->bit_depth != 10) {
139 ff_dsputil_init(&ctx->dsp, ctx->avctx);
141 ctx->decode_dct_block = dnxhd_decode_dct_block_10_444;
144 } else if (buf[0x21] & 0x40) {
145 ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
146 ctx->avctx->bits_per_raw_sample = 10;
147 if (ctx->bit_depth != 10) {
148 ff_dsputil_init(&ctx->dsp, ctx->avctx);
150 ctx->decode_dct_block = dnxhd_decode_dct_block_10;
153 ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
154 ctx->avctx->bits_per_raw_sample = 8;
155 if (ctx->bit_depth != 8) {
156 ff_dsputil_init(&ctx->dsp, ctx->avctx);
158 ctx->decode_dct_block = dnxhd_decode_dct_block_8;
162 cid = AV_RB32(buf + 0x28);
163 av_dlog(ctx->avctx, "compression id %d\n", cid);
165 if ((ret = dnxhd_init_vlc(ctx, cid)) < 0)
168 if (buf_size < ctx->cid_table->coding_unit_size) {
169 av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size\n");
170 return AVERROR_INVALIDDATA;
173 ctx->mb_width = ctx->width>>4;
174 ctx->mb_height = buf[0x16d];
176 av_dlog(ctx->avctx, "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);
178 if ((ctx->height+15)>>4 == ctx->mb_height && frame->interlaced_frame)
181 if (ctx->mb_height > 68 ||
182 (ctx->mb_height << frame->interlaced_frame) > (ctx->height+15)>>4) {
183 av_log(ctx->avctx, AV_LOG_ERROR, "mb height too big: %d\n", ctx->mb_height);
184 return AVERROR_INVALIDDATA;
187 for (i = 0; i < ctx->mb_height; i++) {
188 ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i<<2));
189 av_dlog(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]);
190 if (buf_size < ctx->mb_scan_index[i] + 0x280LL) {
191 av_log(ctx->avctx, AV_LOG_ERROR, "invalid mb scan index\n");
192 return AVERROR_INVALIDDATA;
199 static av_always_inline void dnxhd_decode_dct_block(DNXHDContext *ctx,
200 int16_t *block, int n,
206 int i, j, index1, index2, len, flags;
207 int level, component, sign;
209 const uint8_t *weight_matrix;
210 const uint8_t *ac_level = ctx->cid_table->ac_level;
211 const uint8_t *ac_flags = ctx->cid_table->ac_flags;
212 const int eob_index = ctx->cid_table->eob_index;
213 OPEN_READER(bs, &ctx->gb);
217 component = 1 + (n&1);
218 scale = ctx->chroma_scale;
219 weight_matrix = ctx->cid_table->chroma_weight;
222 scale = ctx->luma_scale;
223 weight_matrix = ctx->cid_table->luma_weight;
226 component = (n >> 1) % 3;
228 scale = ctx->chroma_scale;
229 weight_matrix = ctx->cid_table->chroma_weight;
231 scale = ctx->luma_scale;
232 weight_matrix = ctx->cid_table->luma_weight;
236 UPDATE_CACHE(bs, &ctx->gb);
237 GET_VLC(len, bs, &ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
239 level = GET_CACHE(bs, &ctx->gb);
240 LAST_SKIP_BITS(bs, &ctx->gb, len);
242 level = (NEG_USR32(sign ^ level, len) ^ sign) - sign;
243 ctx->last_dc[component] += level;
245 block[0] = ctx->last_dc[component];
249 UPDATE_CACHE(bs, &ctx->gb);
250 GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,
253 while (index1 != eob_index) {
254 level = ac_level[index1];
255 flags = ac_flags[index1];
257 sign = SHOW_SBITS(bs, &ctx->gb, 1);
258 SKIP_BITS(bs, &ctx->gb, 1);
261 level += SHOW_UBITS(bs, &ctx->gb, index_bits) << 7;
262 SKIP_BITS(bs, &ctx->gb, index_bits);
266 UPDATE_CACHE(bs, &ctx->gb);
267 GET_VLC(index2, bs, &ctx->gb, ctx->run_vlc.table,
269 i += ctx->cid_table->run[index2];
273 av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
277 j = ctx->scantable.permutated[i];
279 if (level_bias < 32 || weight_matrix[i] != level_bias)
281 level >>= level_shift;
283 block[j] = (level^sign) - sign;
285 UPDATE_CACHE(bs, &ctx->gb);
286 GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,
290 CLOSE_READER(bs, &ctx->gb);
293 static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, int16_t *block,
296 dnxhd_decode_dct_block(ctx, block, n, qscale, 4, 32, 6);
299 static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, int16_t *block,
302 dnxhd_decode_dct_block(ctx, block, n, qscale, 6, 8, 4);
305 static void dnxhd_decode_dct_block_10_444(DNXHDContext *ctx, int16_t *block,
308 dnxhd_decode_dct_block(ctx, block, n, qscale, 6, 32, 6);
311 static int dnxhd_decode_macroblock(DNXHDContext *ctx, AVFrame *frame, int x, int y)
313 int shift1 = ctx->bit_depth == 10;
314 int dct_linesize_luma = frame->linesize[0];
315 int dct_linesize_chroma = frame->linesize[1];
316 uint8_t *dest_y, *dest_u, *dest_v;
317 int dct_y_offset, dct_x_offset;
320 qscale = get_bits(&ctx->gb, 11);
321 skip_bits1(&ctx->gb);
323 if (qscale != ctx->last_qscale) {
324 for (i = 0; i < 64; i++) {
325 ctx->luma_scale[i] = qscale * ctx->cid_table->luma_weight[i];
326 ctx->chroma_scale[i] = qscale * ctx->cid_table->chroma_weight[i];
328 ctx->last_qscale = qscale;
331 for (i = 0; i < 8; i++) {
332 ctx->dsp.clear_block(ctx->blocks[i]);
333 ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
336 for (; i < 12; i++) {
337 ctx->dsp.clear_block(ctx->blocks[i]);
338 ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
342 if (frame->interlaced_frame) {
343 dct_linesize_luma <<= 1;
344 dct_linesize_chroma <<= 1;
347 dest_y = frame->data[0] + ((y * dct_linesize_luma) << 4) + (x << (4 + shift1));
348 dest_u = frame->data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->is_444));
349 dest_v = frame->data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->is_444));
351 if (ctx->cur_field) {
352 dest_y += frame->linesize[0];
353 dest_u += frame->linesize[1];
354 dest_v += frame->linesize[2];
357 dct_y_offset = dct_linesize_luma << 3;
358 dct_x_offset = 8 << shift1;
360 ctx->dsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]);
361 ctx->dsp.idct_put(dest_y + dct_x_offset, dct_linesize_luma, ctx->blocks[1]);
362 ctx->dsp.idct_put(dest_y + dct_y_offset, dct_linesize_luma, ctx->blocks[4]);
363 ctx->dsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, ctx->blocks[5]);
365 if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
366 dct_y_offset = dct_linesize_chroma << 3;
367 ctx->dsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]);
368 ctx->dsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[3]);
369 ctx->dsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, ctx->blocks[6]);
370 ctx->dsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, ctx->blocks[7]);
373 ctx->dsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]);
374 ctx->dsp.idct_put(dest_y + dct_x_offset, dct_linesize_luma, ctx->blocks[1]);
375 ctx->dsp.idct_put(dest_y + dct_y_offset, dct_linesize_luma, ctx->blocks[6]);
376 ctx->dsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, ctx->blocks[7]);
378 if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
379 dct_y_offset = dct_linesize_chroma << 3;
380 ctx->dsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]);
381 ctx->dsp.idct_put(dest_u + dct_x_offset, dct_linesize_chroma, ctx->blocks[3]);
382 ctx->dsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, ctx->blocks[8]);
383 ctx->dsp.idct_put(dest_u + dct_y_offset + dct_x_offset, dct_linesize_chroma, ctx->blocks[9]);
384 ctx->dsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[4]);
385 ctx->dsp.idct_put(dest_v + dct_x_offset, dct_linesize_chroma, ctx->blocks[5]);
386 ctx->dsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, ctx->blocks[10]);
387 ctx->dsp.idct_put(dest_v + dct_y_offset + dct_x_offset, dct_linesize_chroma, ctx->blocks[11]);
394 static int dnxhd_decode_macroblocks(DNXHDContext *ctx, AVFrame *frame,
395 const uint8_t *buf, int buf_size)
398 for (y = 0; y < ctx->mb_height; y++) {
401 ctx->last_dc[2] = 1 << (ctx->bit_depth + 2); // for levels +2^(bitdepth-1)
402 init_get_bits(&ctx->gb, buf + ctx->mb_scan_index[y], (buf_size - ctx->mb_scan_index[y]) << 3);
403 for (x = 0; x < ctx->mb_width; x++) {
405 dnxhd_decode_macroblock(ctx, frame, x, y);
406 //STOP_TIMER("decode macroblock");
412 static int dnxhd_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
415 const uint8_t *buf = avpkt->data;
416 int buf_size = avpkt->size;
417 DNXHDContext *ctx = avctx->priv_data;
418 ThreadFrame frame = { .f = data };
419 AVFrame *picture = data;
423 av_dlog(avctx, "frame size %d\n", buf_size);
426 if ((ret = dnxhd_decode_header(ctx, picture, buf, buf_size, first_field)) < 0)
429 if ((avctx->width || avctx->height) &&
430 (ctx->width != avctx->width || ctx->height != avctx->height)) {
431 av_log(avctx, AV_LOG_WARNING, "frame size changed: %dx%d -> %dx%d\n",
432 avctx->width, avctx->height, ctx->width, ctx->height);
436 ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
441 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
443 picture->pict_type = AV_PICTURE_TYPE_I;
444 picture->key_frame = 1;
447 dnxhd_decode_macroblocks(ctx, picture, buf + 0x280, buf_size - 0x280);
449 if (first_field && picture->interlaced_frame) {
450 buf += ctx->cid_table->coding_unit_size;
451 buf_size -= ctx->cid_table->coding_unit_size;
453 goto decode_coding_unit;
460 static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
462 DNXHDContext *ctx = avctx->priv_data;
464 ff_free_vlc(&ctx->ac_vlc);
465 ff_free_vlc(&ctx->dc_vlc);
466 ff_free_vlc(&ctx->run_vlc);
470 AVCodec ff_dnxhd_decoder = {
472 .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
473 .type = AVMEDIA_TYPE_VIDEO,
474 .id = AV_CODEC_ID_DNXHD,
475 .priv_data_size = sizeof(DNXHDContext),
476 .init = dnxhd_decode_init,
477 .close = dnxhd_decode_close,
478 .decode = dnxhd_decode_frame,
479 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,