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