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