• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • 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  * Copyright (c) 2011 MirriAd Ltd
00005  *
00006  * 10 bit support added by MirriAd Ltd, Joseph Artsimovich <joseph@mirriad.com>
00007  *
00008  * This file is part of Libav.
00009  *
00010  * Libav is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * Libav is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with Libav; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00023  */
00024 
00025 //#define TRACE
00026 //#define DEBUG
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];         /* max for 1080p */
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; // 8, 10 or 0 if not initialized at all.
00050     void (*decode_dct_block)(struct DNXHDContext *ctx, DCTELEM *block,
00051                              int n, int qscale);
00052 } DNXHDContext;
00053 
00054 #define DNXHD_VLC_BITS 9
00055 #define DNXHD_DC_VLC_BITS 7
00056 
00057 static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, DCTELEM *block, int n, int qscale);
00058 static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, DCTELEM *block, int n, int qscale);
00059 
00060 static av_cold int dnxhd_decode_init(AVCodecContext *avctx)
00061 {
00062     DNXHDContext *ctx = avctx->priv_data;
00063 
00064     ctx->avctx = avctx;
00065     avctx->coded_frame = &ctx->picture;
00066     ctx->picture.type = AV_PICTURE_TYPE_I;
00067     ctx->picture.key_frame = 1;
00068     return 0;
00069 }
00070 
00071 static int dnxhd_init_vlc(DNXHDContext *ctx, int cid)
00072 {
00073     if (cid != ctx->cid) {
00074         int index;
00075 
00076         if ((index = ff_dnxhd_get_cid_table(cid)) < 0) {
00077             av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %d\n", cid);
00078             return -1;
00079         }
00080         ctx->cid_table = &ff_dnxhd_cid_table[index];
00081 
00082         free_vlc(&ctx->ac_vlc);
00083         free_vlc(&ctx->dc_vlc);
00084         free_vlc(&ctx->run_vlc);
00085 
00086         init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
00087                  ctx->cid_table->ac_bits, 1, 1,
00088                  ctx->cid_table->ac_codes, 2, 2, 0);
00089         init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, ctx->bit_depth + 4,
00090                  ctx->cid_table->dc_bits, 1, 1,
00091                  ctx->cid_table->dc_codes, 1, 1, 0);
00092         init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
00093                  ctx->cid_table->run_bits, 1, 1,
00094                  ctx->cid_table->run_codes, 2, 2, 0);
00095 
00096         ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, ff_zigzag_direct);
00097         ctx->cid = cid;
00098     }
00099     return 0;
00100 }
00101 
00102 static int dnxhd_decode_header(DNXHDContext *ctx, const uint8_t *buf, int buf_size, int first_field)
00103 {
00104     static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
00105     int i, cid;
00106 
00107     if (buf_size < 0x280)
00108         return -1;
00109 
00110     if (memcmp(buf, header_prefix, 5)) {
00111         av_log(ctx->avctx, AV_LOG_ERROR, "error in header\n");
00112         return -1;
00113     }
00114     if (buf[5] & 2) { /* interlaced */
00115         ctx->cur_field = buf[5] & 1;
00116         ctx->picture.interlaced_frame = 1;
00117         ctx->picture.top_field_first = first_field ^ ctx->cur_field;
00118         av_log(ctx->avctx, AV_LOG_DEBUG, "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);
00119     }
00120 
00121     ctx->height = AV_RB16(buf + 0x18);
00122     ctx->width  = AV_RB16(buf + 0x1a);
00123 
00124     av_dlog(ctx->avctx, "width %d, height %d\n", ctx->width, ctx->height);
00125 
00126     if (buf[0x21] & 0x40) {
00127         ctx->avctx->pix_fmt = PIX_FMT_YUV422P10;
00128         ctx->avctx->bits_per_raw_sample = 10;
00129         if (ctx->bit_depth != 10) {
00130             dsputil_init(&ctx->dsp, ctx->avctx);
00131             ctx->bit_depth = 10;
00132             ctx->decode_dct_block = dnxhd_decode_dct_block_10;
00133         }
00134     } else {
00135         ctx->avctx->pix_fmt = PIX_FMT_YUV422P;
00136         ctx->avctx->bits_per_raw_sample = 8;
00137         if (ctx->bit_depth != 8) {
00138             dsputil_init(&ctx->dsp, ctx->avctx);
00139             ctx->bit_depth = 8;
00140             ctx->decode_dct_block = dnxhd_decode_dct_block_8;
00141         }
00142     }
00143 
00144     cid = AV_RB32(buf + 0x28);
00145     av_dlog(ctx->avctx, "compression id %d\n", cid);
00146 
00147     if (dnxhd_init_vlc(ctx, cid) < 0)
00148         return -1;
00149 
00150     if (buf_size < ctx->cid_table->coding_unit_size) {
00151         av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size\n");
00152         return -1;
00153     }
00154 
00155     ctx->mb_width = ctx->width>>4;
00156     ctx->mb_height = buf[0x16d];
00157 
00158     av_dlog(ctx->avctx, "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);
00159 
00160     if ((ctx->height+15)>>4 == ctx->mb_height && ctx->picture.interlaced_frame)
00161         ctx->height <<= 1;
00162 
00163     if (ctx->mb_height > 68 ||
00164         (ctx->mb_height<<ctx->picture.interlaced_frame) > (ctx->height+15)>>4) {
00165         av_log(ctx->avctx, AV_LOG_ERROR, "mb height too big: %d\n", ctx->mb_height);
00166         return -1;
00167     }
00168 
00169     for (i = 0; i < ctx->mb_height; i++) {
00170         ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i<<2));
00171         av_dlog(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]);
00172         if (buf_size < ctx->mb_scan_index[i] + 0x280) {
00173             av_log(ctx->avctx, AV_LOG_ERROR, "invalid mb scan index\n");
00174             return -1;
00175         }
00176     }
00177 
00178     return 0;
00179 }
00180 
00181 static av_always_inline void dnxhd_decode_dct_block(DNXHDContext *ctx,
00182                                                     DCTELEM *block, int n,
00183                                                     int qscale,
00184                                                     int index_bits,
00185                                                     int level_bias,
00186                                                     int level_shift)
00187 {
00188     int i, j, index1, index2, len;
00189     int level, component, sign;
00190     const uint8_t *weight_matrix;
00191     OPEN_READER(bs, &ctx->gb);
00192 
00193     if (n&2) {
00194         component = 1 + (n&1);
00195         weight_matrix = ctx->cid_table->chroma_weight;
00196     } else {
00197         component = 0;
00198         weight_matrix = ctx->cid_table->luma_weight;
00199     }
00200 
00201     UPDATE_CACHE(bs, &ctx->gb);
00202     GET_VLC(len, bs, &ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
00203     if (len) {
00204         level = GET_CACHE(bs, &ctx->gb);
00205         LAST_SKIP_BITS(bs, &ctx->gb, len);
00206         sign  = ~level >> 31;
00207         level = (NEG_USR32(sign ^ level, len) ^ sign) - sign;
00208         ctx->last_dc[component] += level;
00209     }
00210     block[0] = ctx->last_dc[component];
00211     //av_log(ctx->avctx, AV_LOG_DEBUG, "dc %d\n", block[0]);
00212 
00213     for (i = 1; ; i++) {
00214         UPDATE_CACHE(bs, &ctx->gb);
00215         GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,
00216                 DNXHD_VLC_BITS, 2);
00217         //av_log(ctx->avctx, AV_LOG_DEBUG, "index %d\n", index1);
00218         level = ctx->cid_table->ac_level[index1];
00219         if (!level) { /* EOB */
00220             //av_log(ctx->avctx, AV_LOG_DEBUG, "EOB\n");
00221             break;
00222         }
00223 
00224         sign = SHOW_SBITS(bs, &ctx->gb, 1);
00225         SKIP_BITS(bs, &ctx->gb, 1);
00226 
00227         if (ctx->cid_table->ac_index_flag[index1]) {
00228             level += SHOW_UBITS(bs, &ctx->gb, index_bits) << 6;
00229             SKIP_BITS(bs, &ctx->gb, index_bits);
00230         }
00231 
00232         if (ctx->cid_table->ac_run_flag[index1]) {
00233             UPDATE_CACHE(bs, &ctx->gb);
00234             GET_VLC(index2, bs, &ctx->gb, ctx->run_vlc.table,
00235                     DNXHD_VLC_BITS, 2);
00236             i += ctx->cid_table->run[index2];
00237         }
00238 
00239         if (i > 63) {
00240             av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
00241             break;
00242         }
00243 
00244         j = ctx->scantable.permutated[i];
00245         //av_log(ctx->avctx, AV_LOG_DEBUG, "j %d\n", j);
00246         //av_log(ctx->avctx, AV_LOG_DEBUG, "level %d, weight %d\n", level, weight_matrix[i]);
00247         level = (2*level+1) * qscale * weight_matrix[i];
00248         if (level_bias < 32 || weight_matrix[i] != level_bias)
00249             level += level_bias;
00250         level >>= level_shift;
00251 
00252         //av_log(NULL, AV_LOG_DEBUG, "i %d, j %d, end level %d\n", i, j, level);
00253         block[j] = (level^sign) - sign;
00254     }
00255 
00256     CLOSE_READER(bs, &ctx->gb);
00257 }
00258 
00259 static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, DCTELEM *block,
00260                                      int n, int qscale)
00261 {
00262     dnxhd_decode_dct_block(ctx, block, n, qscale, 4, 32, 6);
00263 }
00264 
00265 static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, DCTELEM *block,
00266                                       int n, int qscale)
00267 {
00268     dnxhd_decode_dct_block(ctx, block, n, qscale, 6, 8, 4);
00269 }
00270 
00271 static int dnxhd_decode_macroblock(DNXHDContext *ctx, int x, int y)
00272 {
00273     int shift1 = ctx->bit_depth == 10;
00274     int dct_linesize_luma   = ctx->picture.linesize[0];
00275     int dct_linesize_chroma = ctx->picture.linesize[1];
00276     uint8_t *dest_y, *dest_u, *dest_v;
00277     int dct_y_offset, dct_x_offset;
00278     int qscale, i;
00279 
00280     qscale = get_bits(&ctx->gb, 11);
00281     skip_bits1(&ctx->gb);
00282     //av_log(ctx->avctx, AV_LOG_DEBUG, "qscale %d\n", qscale);
00283 
00284     for (i = 0; i < 8; i++) {
00285         ctx->dsp.clear_block(ctx->blocks[i]);
00286         ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
00287     }
00288 
00289     if (ctx->picture.interlaced_frame) {
00290         dct_linesize_luma   <<= 1;
00291         dct_linesize_chroma <<= 1;
00292     }
00293 
00294     dest_y = ctx->picture.data[0] + ((y * dct_linesize_luma)   << 4) + (x << (4 + shift1));
00295     dest_u = ctx->picture.data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1));
00296     dest_v = ctx->picture.data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1));
00297 
00298     if (ctx->cur_field) {
00299         dest_y += ctx->picture.linesize[0];
00300         dest_u += ctx->picture.linesize[1];
00301         dest_v += ctx->picture.linesize[2];
00302     }
00303 
00304     dct_y_offset = dct_linesize_luma << 3;
00305     dct_x_offset = 8 << shift1;
00306     ctx->dsp.idct_put(dest_y,                               dct_linesize_luma, ctx->blocks[0]);
00307     ctx->dsp.idct_put(dest_y + dct_x_offset,                dct_linesize_luma, ctx->blocks[1]);
00308     ctx->dsp.idct_put(dest_y + dct_y_offset,                dct_linesize_luma, ctx->blocks[4]);
00309     ctx->dsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, ctx->blocks[5]);
00310 
00311     if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
00312         dct_y_offset = dct_linesize_chroma << 3;
00313         ctx->dsp.idct_put(dest_u,                dct_linesize_chroma, ctx->blocks[2]);
00314         ctx->dsp.idct_put(dest_v,                dct_linesize_chroma, ctx->blocks[3]);
00315         ctx->dsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, ctx->blocks[6]);
00316         ctx->dsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, ctx->blocks[7]);
00317     }
00318 
00319     return 0;
00320 }
00321 
00322 static int dnxhd_decode_macroblocks(DNXHDContext *ctx, const uint8_t *buf, int buf_size)
00323 {
00324     int x, y;
00325     for (y = 0; y < ctx->mb_height; y++) {
00326         ctx->last_dc[0] =
00327         ctx->last_dc[1] =
00328         ctx->last_dc[2] = 1 << (ctx->bit_depth + 2); // for levels +2^(bitdepth-1)
00329         init_get_bits(&ctx->gb, buf + ctx->mb_scan_index[y], (buf_size - ctx->mb_scan_index[y]) << 3);
00330         for (x = 0; x < ctx->mb_width; x++) {
00331             //START_TIMER;
00332             dnxhd_decode_macroblock(ctx, x, y);
00333             //STOP_TIMER("decode macroblock");
00334         }
00335     }
00336     return 0;
00337 }
00338 
00339 static int dnxhd_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00340                               AVPacket *avpkt)
00341 {
00342     const uint8_t *buf = avpkt->data;
00343     int buf_size = avpkt->size;
00344     DNXHDContext *ctx = avctx->priv_data;
00345     AVFrame *picture = data;
00346     int first_field = 1;
00347 
00348     av_dlog(avctx, "frame size %d\n", buf_size);
00349 
00350  decode_coding_unit:
00351     if (dnxhd_decode_header(ctx, buf, buf_size, first_field) < 0)
00352         return -1;
00353 
00354     if ((avctx->width || avctx->height) &&
00355         (ctx->width != avctx->width || ctx->height != avctx->height)) {
00356         av_log(avctx, AV_LOG_WARNING, "frame size changed: %dx%d -> %dx%d\n",
00357                avctx->width, avctx->height, ctx->width, ctx->height);
00358         first_field = 1;
00359     }
00360 
00361     if (av_image_check_size(ctx->width, ctx->height, 0, avctx))
00362         return -1;
00363     avcodec_set_dimensions(avctx, ctx->width, ctx->height);
00364 
00365     if (first_field) {
00366         if (ctx->picture.data[0])
00367             avctx->release_buffer(avctx, &ctx->picture);
00368         if (avctx->get_buffer(avctx, &ctx->picture) < 0) {
00369             av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00370             return -1;
00371         }
00372     }
00373 
00374     dnxhd_decode_macroblocks(ctx, buf + 0x280, buf_size - 0x280);
00375 
00376     if (first_field && ctx->picture.interlaced_frame) {
00377         buf      += ctx->cid_table->coding_unit_size;
00378         buf_size -= ctx->cid_table->coding_unit_size;
00379         first_field = 0;
00380         goto decode_coding_unit;
00381     }
00382 
00383     *picture = ctx->picture;
00384     *data_size = sizeof(AVPicture);
00385     return buf_size;
00386 }
00387 
00388 static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
00389 {
00390     DNXHDContext *ctx = avctx->priv_data;
00391 
00392     if (ctx->picture.data[0])
00393         avctx->release_buffer(avctx, &ctx->picture);
00394     free_vlc(&ctx->ac_vlc);
00395     free_vlc(&ctx->dc_vlc);
00396     free_vlc(&ctx->run_vlc);
00397     return 0;
00398 }
00399 
00400 AVCodec ff_dnxhd_decoder = {
00401     .name           = "dnxhd",
00402     .type           = AVMEDIA_TYPE_VIDEO,
00403     .id             = CODEC_ID_DNXHD,
00404     .priv_data_size = sizeof(DNXHDContext),
00405     .init           = dnxhd_decode_init,
00406     .close          = dnxhd_decode_close,
00407     .decode         = dnxhd_decode_frame,
00408     .capabilities   = CODEC_CAP_DR1,
00409     .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
00410 };
Generated on Sun Apr 22 2012 21:54:00 for Libav by doxygen 1.7.1