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