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