00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "libavutil/imgutils.h"
00029 #include "avcodec.h"
00030 #include "get_bits.h"
00031 #include "dnxhddata.h"
00032 #include "dsputil.h"
00033 #include "thread.h"
00034
00035 typedef struct DNXHDContext {
00036 AVCodecContext *avctx;
00037 AVFrame picture;
00038 GetBitContext gb;
00039 int cid;
00040 unsigned int width, height;
00041 unsigned int mb_width, mb_height;
00042 uint32_t mb_scan_index[68];
00043 int cur_field;
00044 VLC ac_vlc, dc_vlc, run_vlc;
00045 int last_dc[3];
00046 DSPContext dsp;
00047 DECLARE_ALIGNED(16, DCTELEM, blocks)[8][64];
00048 ScanTable scantable;
00049 const CIDEntry *cid_table;
00050 int bit_depth;
00051 void (*decode_dct_block)(struct DNXHDContext *ctx, DCTELEM *block,
00052 int n, int qscale);
00053 int last_qscale;
00054 int luma_scale[64];
00055 int chroma_scale[64];
00056 } DNXHDContext;
00057
00058 #define DNXHD_VLC_BITS 9
00059 #define DNXHD_DC_VLC_BITS 7
00060
00061 static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, DCTELEM *block, int n, int qscale);
00062 static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, DCTELEM *block, int n, int qscale);
00063
00064 static av_cold int dnxhd_decode_init(AVCodecContext *avctx)
00065 {
00066 DNXHDContext *ctx = avctx->priv_data;
00067
00068 ctx->avctx = avctx;
00069 avctx->coded_frame = &ctx->picture;
00070 avcodec_get_frame_defaults(&ctx->picture);
00071 ctx->picture.type = AV_PICTURE_TYPE_I;
00072 ctx->picture.key_frame = 1;
00073 return 0;
00074 }
00075
00076 static int dnxhd_init_vlc(DNXHDContext *ctx, int cid)
00077 {
00078 if (cid != ctx->cid) {
00079 int index;
00080
00081 if ((index = ff_dnxhd_get_cid_table(cid)) < 0) {
00082 av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %d\n", cid);
00083 return -1;
00084 }
00085 if (ff_dnxhd_cid_table[index].bit_depth != ctx->bit_depth) {
00086 av_log(ctx->avctx, AV_LOG_ERROR, "bit depth mismatches %d %d\n", ff_dnxhd_cid_table[index].bit_depth, ctx->bit_depth);
00087 return AVERROR_INVALIDDATA;
00088 }
00089 ctx->cid_table = &ff_dnxhd_cid_table[index];
00090
00091 ff_free_vlc(&ctx->ac_vlc);
00092 ff_free_vlc(&ctx->dc_vlc);
00093 ff_free_vlc(&ctx->run_vlc);
00094
00095 init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
00096 ctx->cid_table->ac_bits, 1, 1,
00097 ctx->cid_table->ac_codes, 2, 2, 0);
00098 init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, ctx->bit_depth + 4,
00099 ctx->cid_table->dc_bits, 1, 1,
00100 ctx->cid_table->dc_codes, 1, 1, 0);
00101 init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
00102 ctx->cid_table->run_bits, 1, 1,
00103 ctx->cid_table->run_codes, 2, 2, 0);
00104
00105 ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, ff_zigzag_direct);
00106 ctx->cid = cid;
00107 }
00108 return 0;
00109 }
00110
00111 static int dnxhd_decode_header(DNXHDContext *ctx, const uint8_t *buf, int buf_size, int first_field)
00112 {
00113 static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
00114 int i, cid;
00115
00116 if (buf_size < 0x280)
00117 return -1;
00118
00119 if (memcmp(buf, header_prefix, 5)) {
00120 av_log(ctx->avctx, AV_LOG_ERROR, "error in header\n");
00121 return -1;
00122 }
00123 if (buf[5] & 2) {
00124 ctx->cur_field = buf[5] & 1;
00125 ctx->picture.interlaced_frame = 1;
00126 ctx->picture.top_field_first = first_field ^ ctx->cur_field;
00127 av_log(ctx->avctx, AV_LOG_DEBUG, "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);
00128 }
00129
00130 ctx->height = AV_RB16(buf + 0x18);
00131 ctx->width = AV_RB16(buf + 0x1a);
00132
00133 av_dlog(ctx->avctx, "width %d, height %d\n", ctx->width, ctx->height);
00134
00135 if (buf[0x21] & 0x40) {
00136 ctx->avctx->pix_fmt = PIX_FMT_YUV422P10;
00137 ctx->avctx->bits_per_raw_sample = 10;
00138 if (ctx->bit_depth != 10) {
00139 ff_dsputil_init(&ctx->dsp, ctx->avctx);
00140 ctx->bit_depth = 10;
00141 ctx->decode_dct_block = dnxhd_decode_dct_block_10;
00142 }
00143 } else {
00144 ctx->avctx->pix_fmt = PIX_FMT_YUV422P;
00145 ctx->avctx->bits_per_raw_sample = 8;
00146 if (ctx->bit_depth != 8) {
00147 ff_dsputil_init(&ctx->dsp, ctx->avctx);
00148 ctx->bit_depth = 8;
00149 ctx->decode_dct_block = dnxhd_decode_dct_block_8;
00150 }
00151 }
00152
00153 cid = AV_RB32(buf + 0x28);
00154 av_dlog(ctx->avctx, "compression id %d\n", cid);
00155
00156 if (dnxhd_init_vlc(ctx, cid) < 0)
00157 return -1;
00158
00159 if (buf_size < ctx->cid_table->coding_unit_size) {
00160 av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size\n");
00161 return -1;
00162 }
00163
00164 ctx->mb_width = ctx->width>>4;
00165 ctx->mb_height = buf[0x16d];
00166
00167 av_dlog(ctx->avctx, "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);
00168
00169 if ((ctx->height+15)>>4 == ctx->mb_height && ctx->picture.interlaced_frame)
00170 ctx->height <<= 1;
00171
00172 if (ctx->mb_height > 68 ||
00173 (ctx->mb_height<<ctx->picture.interlaced_frame) > (ctx->height+15)>>4) {
00174 av_log(ctx->avctx, AV_LOG_ERROR, "mb height too big: %d\n", ctx->mb_height);
00175 return -1;
00176 }
00177
00178 for (i = 0; i < ctx->mb_height; i++) {
00179 ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i<<2));
00180 av_dlog(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]);
00181 if (buf_size < ctx->mb_scan_index[i] + 0x280) {
00182 av_log(ctx->avctx, AV_LOG_ERROR, "invalid mb scan index\n");
00183 return -1;
00184 }
00185 }
00186
00187 return 0;
00188 }
00189
00190 static av_always_inline void dnxhd_decode_dct_block(DNXHDContext *ctx,
00191 DCTELEM *block, int n,
00192 int qscale,
00193 int index_bits,
00194 int level_bias,
00195 int level_shift)
00196 {
00197 int i, j, index1, index2, len, flags;
00198 int level, component, sign;
00199 const int *scale;
00200 const uint8_t *weight_matrix;
00201 const uint8_t *ac_level = ctx->cid_table->ac_level;
00202 const uint8_t *ac_flags = ctx->cid_table->ac_flags;
00203 const int eob_index = ctx->cid_table->eob_index;
00204 OPEN_READER(bs, &ctx->gb);
00205
00206 if (n&2) {
00207 component = 1 + (n&1);
00208 scale = ctx->chroma_scale;
00209 weight_matrix = ctx->cid_table->chroma_weight;
00210 } else {
00211 component = 0;
00212 scale = ctx->luma_scale;
00213 weight_matrix = ctx->cid_table->luma_weight;
00214 }
00215
00216 UPDATE_CACHE(bs, &ctx->gb);
00217 GET_VLC(len, bs, &ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
00218 if (len) {
00219 level = GET_CACHE(bs, &ctx->gb);
00220 LAST_SKIP_BITS(bs, &ctx->gb, len);
00221 sign = ~level >> 31;
00222 level = (NEG_USR32(sign ^ level, len) ^ sign) - sign;
00223 ctx->last_dc[component] += level;
00224 }
00225 block[0] = ctx->last_dc[component];
00226
00227
00228 i = 0;
00229
00230 UPDATE_CACHE(bs, &ctx->gb);
00231 GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,
00232 DNXHD_VLC_BITS, 2);
00233
00234 while (index1 != eob_index) {
00235 level = ac_level[index1];
00236 flags = ac_flags[index1];
00237
00238 sign = SHOW_SBITS(bs, &ctx->gb, 1);
00239 SKIP_BITS(bs, &ctx->gb, 1);
00240
00241 if (flags & 1) {
00242 level += SHOW_UBITS(bs, &ctx->gb, index_bits) << 7;
00243 SKIP_BITS(bs, &ctx->gb, index_bits);
00244 }
00245
00246 if (flags & 2) {
00247 UPDATE_CACHE(bs, &ctx->gb);
00248 GET_VLC(index2, bs, &ctx->gb, ctx->run_vlc.table,
00249 DNXHD_VLC_BITS, 2);
00250 i += ctx->cid_table->run[index2];
00251 }
00252
00253 if (++i > 63) {
00254 av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
00255 break;
00256 }
00257
00258 j = ctx->scantable.permutated[i];
00259
00260
00261 level *= scale[i];
00262 if (level_bias < 32 || weight_matrix[i] != level_bias)
00263 level += level_bias;
00264 level >>= level_shift;
00265
00266
00267 block[j] = (level^sign) - sign;
00268
00269 UPDATE_CACHE(bs, &ctx->gb);
00270 GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,
00271 DNXHD_VLC_BITS, 2);
00272 }
00273
00274 CLOSE_READER(bs, &ctx->gb);
00275 }
00276
00277 static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, DCTELEM *block,
00278 int n, int qscale)
00279 {
00280 dnxhd_decode_dct_block(ctx, block, n, qscale, 4, 32, 6);
00281 }
00282
00283 static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, DCTELEM *block,
00284 int n, int qscale)
00285 {
00286 dnxhd_decode_dct_block(ctx, block, n, qscale, 6, 8, 4);
00287 }
00288
00289 static int dnxhd_decode_macroblock(DNXHDContext *ctx, int x, int y)
00290 {
00291 int shift1 = ctx->bit_depth == 10;
00292 int dct_linesize_luma = ctx->picture.linesize[0];
00293 int dct_linesize_chroma = ctx->picture.linesize[1];
00294 uint8_t *dest_y, *dest_u, *dest_v;
00295 int dct_y_offset, dct_x_offset;
00296 int qscale, i;
00297
00298 qscale = get_bits(&ctx->gb, 11);
00299 skip_bits1(&ctx->gb);
00300
00301
00302 if (qscale != ctx->last_qscale) {
00303 for (i = 0; i < 64; i++) {
00304 ctx->luma_scale[i] = qscale * ctx->cid_table->luma_weight[i];
00305 ctx->chroma_scale[i] = qscale * ctx->cid_table->chroma_weight[i];
00306 }
00307 ctx->last_qscale = qscale;
00308 }
00309
00310 for (i = 0; i < 8; i++) {
00311 ctx->dsp.clear_block(ctx->blocks[i]);
00312 ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
00313 }
00314
00315 if (ctx->picture.interlaced_frame) {
00316 dct_linesize_luma <<= 1;
00317 dct_linesize_chroma <<= 1;
00318 }
00319
00320 dest_y = ctx->picture.data[0] + ((y * dct_linesize_luma) << 4) + (x << (4 + shift1));
00321 dest_u = ctx->picture.data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1));
00322 dest_v = ctx->picture.data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1));
00323
00324 if (ctx->cur_field) {
00325 dest_y += ctx->picture.linesize[0];
00326 dest_u += ctx->picture.linesize[1];
00327 dest_v += ctx->picture.linesize[2];
00328 }
00329
00330 dct_y_offset = dct_linesize_luma << 3;
00331 dct_x_offset = 8 << shift1;
00332 ctx->dsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]);
00333 ctx->dsp.idct_put(dest_y + dct_x_offset, dct_linesize_luma, ctx->blocks[1]);
00334 ctx->dsp.idct_put(dest_y + dct_y_offset, dct_linesize_luma, ctx->blocks[4]);
00335 ctx->dsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, ctx->blocks[5]);
00336
00337 if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
00338 dct_y_offset = dct_linesize_chroma << 3;
00339 ctx->dsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]);
00340 ctx->dsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[3]);
00341 ctx->dsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, ctx->blocks[6]);
00342 ctx->dsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, ctx->blocks[7]);
00343 }
00344
00345 return 0;
00346 }
00347
00348 static int dnxhd_decode_macroblocks(DNXHDContext *ctx, const uint8_t *buf, int buf_size)
00349 {
00350 int x, y;
00351 for (y = 0; y < ctx->mb_height; y++) {
00352 ctx->last_dc[0] =
00353 ctx->last_dc[1] =
00354 ctx->last_dc[2] = 1 << (ctx->bit_depth + 2);
00355 init_get_bits(&ctx->gb, buf + ctx->mb_scan_index[y], (buf_size - ctx->mb_scan_index[y]) << 3);
00356 for (x = 0; x < ctx->mb_width; x++) {
00357
00358 dnxhd_decode_macroblock(ctx, x, y);
00359
00360 }
00361 }
00362 return 0;
00363 }
00364
00365 static int dnxhd_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00366 AVPacket *avpkt)
00367 {
00368 const uint8_t *buf = avpkt->data;
00369 int buf_size = avpkt->size;
00370 DNXHDContext *ctx = avctx->priv_data;
00371 AVFrame *picture = data;
00372 int first_field = 1;
00373 int ret;
00374
00375 av_dlog(avctx, "frame size %d\n", buf_size);
00376
00377 decode_coding_unit:
00378 if (dnxhd_decode_header(ctx, buf, buf_size, first_field) < 0)
00379 return -1;
00380
00381 if ((avctx->width || avctx->height) &&
00382 (ctx->width != avctx->width || ctx->height != avctx->height)) {
00383 av_log(avctx, AV_LOG_WARNING, "frame size changed: %dx%d -> %dx%d\n",
00384 avctx->width, avctx->height, ctx->width, ctx->height);
00385 first_field = 1;
00386 }
00387
00388 if (av_image_check_size(ctx->width, ctx->height, 0, avctx))
00389 return -1;
00390 avcodec_set_dimensions(avctx, ctx->width, ctx->height);
00391
00392 if (first_field) {
00393 if (ctx->picture.data[0])
00394 ff_thread_release_buffer(avctx, &ctx->picture);
00395 if ((ret = ff_thread_get_buffer(avctx, &ctx->picture)) < 0) {
00396 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00397 return ret;
00398 }
00399 }
00400
00401 dnxhd_decode_macroblocks(ctx, buf + 0x280, buf_size - 0x280);
00402
00403 if (first_field && ctx->picture.interlaced_frame) {
00404 buf += ctx->cid_table->coding_unit_size;
00405 buf_size -= ctx->cid_table->coding_unit_size;
00406 first_field = 0;
00407 goto decode_coding_unit;
00408 }
00409
00410 *picture = ctx->picture;
00411 *data_size = sizeof(AVPicture);
00412 return buf_size;
00413 }
00414
00415 static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
00416 {
00417 DNXHDContext *ctx = avctx->priv_data;
00418
00419 if (ctx->picture.data[0])
00420 ff_thread_release_buffer(avctx, &ctx->picture);
00421 ff_free_vlc(&ctx->ac_vlc);
00422 ff_free_vlc(&ctx->dc_vlc);
00423 ff_free_vlc(&ctx->run_vlc);
00424 return 0;
00425 }
00426
00427 AVCodec ff_dnxhd_decoder = {
00428 .name = "dnxhd",
00429 .type = AVMEDIA_TYPE_VIDEO,
00430 .id = CODEC_ID_DNXHD,
00431 .priv_data_size = sizeof(DNXHDContext),
00432 .init = dnxhd_decode_init,
00433 .close = dnxhd_decode_close,
00434 .decode = dnxhd_decode_frame,
00435 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
00436 .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
00437 };