• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/dnxhddec.c

Go to the documentation of this file.
00001 /*
00002  * VC3/DNxHD decoder.
00003  * Copyright (c) 2007 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 //#define TRACE
00023 //#define DEBUG
00024 
00025 #include "avcodec.h"
00026 #include "get_bits.h"
00027 #include "dnxhddata.h"
00028 #include "dsputil.h"
00029 
00030 typedef struct {
00031     AVCodecContext *avctx;
00032     AVFrame picture;
00033     GetBitContext gb;
00034     int cid;                            
00035     unsigned int width, height;
00036     unsigned int mb_width, mb_height;
00037     uint32_t mb_scan_index[68];         /* max for 1080p */
00038     int cur_field;                      
00039     VLC ac_vlc, dc_vlc, run_vlc;
00040     int last_dc[3];
00041     DSPContext dsp;
00042     DECLARE_ALIGNED(16, DCTELEM, blocks)[8][64];
00043     ScanTable scantable;
00044     const CIDEntry *cid_table;
00045 } DNXHDContext;
00046 
00047 #define DNXHD_VLC_BITS 9
00048 #define DNXHD_DC_VLC_BITS 7
00049 
00050 static av_cold int dnxhd_decode_init(AVCodecContext *avctx)
00051 {
00052     DNXHDContext *ctx = avctx->priv_data;
00053 
00054     ctx->avctx = avctx;
00055     dsputil_init(&ctx->dsp, avctx);
00056     avctx->coded_frame = &ctx->picture;
00057     ctx->picture.type = FF_I_TYPE;
00058     return 0;
00059 }
00060 
00061 static int dnxhd_init_vlc(DNXHDContext *ctx, int cid)
00062 {
00063     if (!ctx->cid_table) {
00064         int index;
00065 
00066         if ((index = ff_dnxhd_get_cid_table(cid)) < 0) {
00067             av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %d\n", cid);
00068             return -1;
00069         }
00070         ctx->cid_table = &ff_dnxhd_cid_table[index];
00071         init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
00072                  ctx->cid_table->ac_bits, 1, 1,
00073                  ctx->cid_table->ac_codes, 2, 2, 0);
00074         init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, ctx->cid_table->bit_depth+4,
00075                  ctx->cid_table->dc_bits, 1, 1,
00076                  ctx->cid_table->dc_codes, 1, 1, 0);
00077         init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
00078                  ctx->cid_table->run_bits, 1, 1,
00079                  ctx->cid_table->run_codes, 2, 2, 0);
00080 
00081         ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, ff_zigzag_direct);
00082     }
00083     return 0;
00084 }
00085 
00086 static int dnxhd_decode_header(DNXHDContext *ctx, const uint8_t *buf, int buf_size, int first_field)
00087 {
00088     static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
00089     int i;
00090 
00091     if (buf_size < 0x280)
00092         return -1;
00093 
00094     if (memcmp(buf, header_prefix, 5)) {
00095         av_log(ctx->avctx, AV_LOG_ERROR, "error in header\n");
00096         return -1;
00097     }
00098     if (buf[5] & 2) { /* interlaced */
00099         ctx->cur_field = buf[5] & 1;
00100         ctx->picture.interlaced_frame = 1;
00101         ctx->picture.top_field_first = first_field ^ ctx->cur_field;
00102         av_log(ctx->avctx, AV_LOG_DEBUG, "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);
00103     }
00104 
00105     ctx->height = AV_RB16(buf + 0x18);
00106     ctx->width  = AV_RB16(buf + 0x1a);
00107 
00108     dprintf(ctx->avctx, "width %d, heigth %d\n", ctx->width, ctx->height);
00109 
00110     if (buf[0x21] & 0x40) {
00111         av_log(ctx->avctx, AV_LOG_ERROR, "10 bit per component\n");
00112         return -1;
00113     }
00114 
00115     ctx->cid = AV_RB32(buf + 0x28);
00116     dprintf(ctx->avctx, "compression id %d\n", ctx->cid);
00117 
00118     if (dnxhd_init_vlc(ctx, ctx->cid) < 0)
00119         return -1;
00120 
00121     if (buf_size < ctx->cid_table->coding_unit_size) {
00122         av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size\n");
00123         return -1;
00124     }
00125 
00126     ctx->mb_width = ctx->width>>4;
00127     ctx->mb_height = buf[0x16d];
00128 
00129     dprintf(ctx->avctx, "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);
00130 
00131     if ((ctx->height+15)>>4 == ctx->mb_height && ctx->picture.interlaced_frame)
00132         ctx->height <<= 1;
00133 
00134     if (ctx->mb_height > 68 ||
00135         (ctx->mb_height<<ctx->picture.interlaced_frame) > (ctx->height+15)>>4) {
00136         av_log(ctx->avctx, AV_LOG_ERROR, "mb height too big: %d\n", ctx->mb_height);
00137         return -1;
00138     }
00139 
00140     for (i = 0; i < ctx->mb_height; i++) {
00141         ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i<<2));
00142         dprintf(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]);
00143         if (buf_size < ctx->mb_scan_index[i] + 0x280) {
00144             av_log(ctx->avctx, AV_LOG_ERROR, "invalid mb scan index\n");
00145             return -1;
00146         }
00147     }
00148 
00149     return 0;
00150 }
00151 
00152 static int dnxhd_decode_dc(DNXHDContext *ctx)
00153 {
00154     int len;
00155 
00156     len = get_vlc2(&ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
00157     return len ? get_xbits(&ctx->gb, len) : 0;
00158 }
00159 
00160 static void dnxhd_decode_dct_block(DNXHDContext *ctx, DCTELEM *block, int n, int qscale)
00161 {
00162     int i, j, index, index2;
00163     int level, component, sign;
00164     const uint8_t *weigth_matrix;
00165 
00166     if (n&2) {
00167         component = 1 + (n&1);
00168         weigth_matrix = ctx->cid_table->chroma_weight;
00169     } else {
00170         component = 0;
00171         weigth_matrix = ctx->cid_table->luma_weight;
00172     }
00173 
00174     ctx->last_dc[component] += dnxhd_decode_dc(ctx);
00175     block[0] = ctx->last_dc[component];
00176     //av_log(ctx->avctx, AV_LOG_DEBUG, "dc %d\n", block[0]);
00177     for (i = 1; ; i++) {
00178         index = get_vlc2(&ctx->gb, ctx->ac_vlc.table, DNXHD_VLC_BITS, 2);
00179         //av_log(ctx->avctx, AV_LOG_DEBUG, "index %d\n", index);
00180         level = ctx->cid_table->ac_level[index];
00181         if (!level) { /* EOB */
00182             //av_log(ctx->avctx, AV_LOG_DEBUG, "EOB\n");
00183             return;
00184         }
00185         sign = get_sbits(&ctx->gb, 1);
00186 
00187         if (ctx->cid_table->ac_index_flag[index]) {
00188             level += get_bits(&ctx->gb, ctx->cid_table->index_bits)<<6;
00189         }
00190 
00191         if (ctx->cid_table->ac_run_flag[index]) {
00192             index2 = get_vlc2(&ctx->gb, ctx->run_vlc.table, DNXHD_VLC_BITS, 2);
00193             i += ctx->cid_table->run[index2];
00194         }
00195 
00196         if (i > 63) {
00197             av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
00198             return;
00199         }
00200 
00201         j = ctx->scantable.permutated[i];
00202         //av_log(ctx->avctx, AV_LOG_DEBUG, "j %d\n", j);
00203         //av_log(ctx->avctx, AV_LOG_DEBUG, "level %d, weigth %d\n", level, weigth_matrix[i]);
00204         level = (2*level+1) * qscale * weigth_matrix[i];
00205         if (ctx->cid_table->bit_depth == 10) {
00206             if (weigth_matrix[i] != 8)
00207                 level += 8;
00208             level >>= 4;
00209         } else {
00210             if (weigth_matrix[i] != 32)
00211                 level += 32;
00212             level >>= 6;
00213         }
00214         //av_log(NULL, AV_LOG_DEBUG, "i %d, j %d, end level %d\n", i, j, level);
00215         block[j] = (level^sign) - sign;
00216     }
00217 }
00218 
00219 static int dnxhd_decode_macroblock(DNXHDContext *ctx, int x, int y)
00220 {
00221     int dct_linesize_luma   = ctx->picture.linesize[0];
00222     int dct_linesize_chroma = ctx->picture.linesize[1];
00223     uint8_t *dest_y, *dest_u, *dest_v;
00224     int dct_offset;
00225     int qscale, i;
00226 
00227     qscale = get_bits(&ctx->gb, 11);
00228     skip_bits1(&ctx->gb);
00229     //av_log(ctx->avctx, AV_LOG_DEBUG, "qscale %d\n", qscale);
00230 
00231     for (i = 0; i < 8; i++) {
00232         ctx->dsp.clear_block(ctx->blocks[i]);
00233         dnxhd_decode_dct_block(ctx, ctx->blocks[i], i, qscale);
00234     }
00235 
00236     if (ctx->picture.interlaced_frame) {
00237         dct_linesize_luma   <<= 1;
00238         dct_linesize_chroma <<= 1;
00239     }
00240 
00241     dest_y = ctx->picture.data[0] + ((y * dct_linesize_luma)   << 4) + (x << 4);
00242     dest_u = ctx->picture.data[1] + ((y * dct_linesize_chroma) << 4) + (x << 3);
00243     dest_v = ctx->picture.data[2] + ((y * dct_linesize_chroma) << 4) + (x << 3);
00244 
00245     if (ctx->cur_field) {
00246         dest_y += ctx->picture.linesize[0];
00247         dest_u += ctx->picture.linesize[1];
00248         dest_v += ctx->picture.linesize[2];
00249     }
00250 
00251     dct_offset = dct_linesize_luma << 3;
00252     ctx->dsp.idct_put(dest_y,                  dct_linesize_luma, ctx->blocks[0]);
00253     ctx->dsp.idct_put(dest_y + 8,              dct_linesize_luma, ctx->blocks[1]);
00254     ctx->dsp.idct_put(dest_y + dct_offset,     dct_linesize_luma, ctx->blocks[4]);
00255     ctx->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize_luma, ctx->blocks[5]);
00256 
00257     if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
00258         dct_offset = dct_linesize_chroma << 3;
00259         ctx->dsp.idct_put(dest_u,              dct_linesize_chroma, ctx->blocks[2]);
00260         ctx->dsp.idct_put(dest_v,              dct_linesize_chroma, ctx->blocks[3]);
00261         ctx->dsp.idct_put(dest_u + dct_offset, dct_linesize_chroma, ctx->blocks[6]);
00262         ctx->dsp.idct_put(dest_v + dct_offset, dct_linesize_chroma, ctx->blocks[7]);
00263     }
00264 
00265     return 0;
00266 }
00267 
00268 static int dnxhd_decode_macroblocks(DNXHDContext *ctx, const uint8_t *buf, int buf_size)
00269 {
00270     int x, y;
00271     for (y = 0; y < ctx->mb_height; y++) {
00272         ctx->last_dc[0] =
00273         ctx->last_dc[1] =
00274         ctx->last_dc[2] = 1<<(ctx->cid_table->bit_depth+2); // for levels +2^(bitdepth-1)
00275         init_get_bits(&ctx->gb, buf + ctx->mb_scan_index[y], (buf_size - ctx->mb_scan_index[y]) << 3);
00276         for (x = 0; x < ctx->mb_width; x++) {
00277             //START_TIMER;
00278             dnxhd_decode_macroblock(ctx, x, y);
00279             //STOP_TIMER("decode macroblock");
00280         }
00281     }
00282     return 0;
00283 }
00284 
00285 static int dnxhd_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00286                               AVPacket *avpkt)
00287 {
00288     const uint8_t *buf = avpkt->data;
00289     int buf_size = avpkt->size;
00290     DNXHDContext *ctx = avctx->priv_data;
00291     AVFrame *picture = data;
00292     int first_field = 1;
00293 
00294     dprintf(avctx, "frame size %d\n", buf_size);
00295 
00296  decode_coding_unit:
00297     if (dnxhd_decode_header(ctx, buf, buf_size, first_field) < 0)
00298         return -1;
00299 
00300     if ((avctx->width || avctx->height) &&
00301         (ctx->width != avctx->width || ctx->height != avctx->height)) {
00302         av_log(avctx, AV_LOG_WARNING, "frame size changed: %dx%d -> %dx%d\n",
00303                avctx->width, avctx->height, ctx->width, ctx->height);
00304         first_field = 1;
00305     }
00306 
00307     avctx->pix_fmt = PIX_FMT_YUV422P;
00308     if (avcodec_check_dimensions(avctx, ctx->width, ctx->height))
00309         return -1;
00310     avcodec_set_dimensions(avctx, ctx->width, ctx->height);
00311 
00312     if (first_field) {
00313         if (ctx->picture.data[0])
00314             avctx->release_buffer(avctx, &ctx->picture);
00315         if (avctx->get_buffer(avctx, &ctx->picture) < 0) {
00316             av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00317             return -1;
00318         }
00319     }
00320 
00321     dnxhd_decode_macroblocks(ctx, buf + 0x280, buf_size - 0x280);
00322 
00323     if (first_field && ctx->picture.interlaced_frame) {
00324         buf      += ctx->cid_table->coding_unit_size;
00325         buf_size -= ctx->cid_table->coding_unit_size;
00326         first_field = 0;
00327         goto decode_coding_unit;
00328     }
00329 
00330     *picture = ctx->picture;
00331     *data_size = sizeof(AVPicture);
00332     return buf_size;
00333 }
00334 
00335 static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
00336 {
00337     DNXHDContext *ctx = avctx->priv_data;
00338 
00339     if (ctx->picture.data[0])
00340         avctx->release_buffer(avctx, &ctx->picture);
00341     free_vlc(&ctx->ac_vlc);
00342     free_vlc(&ctx->dc_vlc);
00343     free_vlc(&ctx->run_vlc);
00344     return 0;
00345 }
00346 
00347 AVCodec dnxhd_decoder = {
00348     "dnxhd",
00349     AVMEDIA_TYPE_VIDEO,
00350     CODEC_ID_DNXHD,
00351     sizeof(DNXHDContext),
00352     dnxhd_decode_init,
00353     NULL,
00354     dnxhd_decode_close,
00355     dnxhd_decode_frame,
00356     CODEC_CAP_DR1,
00357     .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
00358 };

Generated on Fri Sep 16 2011 17:17:35 for FFmpeg by  doxygen 1.7.1