Mercurial > libavcodec.hg
annotate dnxhddec.c @ 5464:75940bbbecfc libavcodec
add coding unit size for interlaced decoding
| author | bcoudurier |
|---|---|
| date | Sat, 04 Aug 2007 12:59:49 +0000 |
| parents | 521465ec12e0 |
| children | 6478c562c80f |
| rev | line source |
|---|---|
| 4687 | 1 /* |
| 2 * VC3/DNxHD decoder. | |
| 3 * Copyright (c) 2007 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>. | |
| 4 * | |
| 5 * This file is part of FFmpeg. | |
| 6 * | |
| 7 * FFmpeg is free software; you can redistribute it and/or | |
| 8 * modify it under the terms of the GNU Lesser General Public | |
| 9 * License as published by the Free Software Foundation; either | |
| 10 * version 2.1 of the License, or (at your option) any later version. | |
| 11 * | |
| 12 * FFmpeg is distributed in the hope that it will be useful, | |
| 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 15 * Lesser General Public License for more details. | |
| 16 * | |
| 17 * You should have received a copy of the GNU Lesser General Public | |
| 18 * License along with FFmpeg; if not, write to the Free Software | |
| 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
| 20 */ | |
| 21 | |
| 22 //#define TRACE | |
| 23 //#define DEBUG | |
| 24 | |
| 25 #include "avcodec.h" | |
| 26 #include "bitstream.h" | |
| 27 #include "dnxhddata.h" | |
| 28 #include "dsputil.h" | |
| 29 #include "mpegvideo.h" | |
| 30 | |
| 31 typedef struct { | |
| 32 int cid; | |
| 33 unsigned int width, height; | |
| 34 int interlaced; | |
| 35 unsigned int frame_size; | |
| 5464 | 36 unsigned int coding_unit_size; |
| 4687 | 37 int index_bits; |
| 38 int bit_depth; | |
| 39 const uint8_t *luma_weigth, *chroma_weigth; | |
| 40 const uint8_t *dc_codes, *dc_bits; | |
| 41 const uint16_t *ac_codes; | |
| 42 const uint8_t *ac_bits, *ac_level; | |
| 43 const uint8_t *ac_run_flag, *ac_index_flag; | |
| 44 const uint16_t *run_codes; | |
| 45 const uint8_t *run_bits, *run; | |
| 46 } CIDEntry; | |
| 47 | |
| 48 typedef struct { | |
| 49 AVCodecContext *avctx; | |
| 50 AVFrame picture; | |
| 51 GetBitContext gb; | |
| 52 int cid; ///< compression id | |
| 53 unsigned int width, height; | |
| 54 unsigned int mb_width, mb_height; | |
| 55 uint32_t mb_scan_index[68]; /* max for 1080p */ | |
| 56 int cur_field; ///< current interlaced field | |
| 57 int index_bits; ///< length of index value | |
| 58 VLC ac_vlc, dc_vlc, run_vlc; | |
| 59 const uint8_t *ac_level, *run; | |
| 60 const uint8_t *ac_run_flag, *ac_index_flag; | |
| 61 const uint8_t *luma_weigth, *chroma_weigth; | |
| 62 int last_dc[3]; | |
| 63 DSPContext dsp; | |
| 64 DECLARE_ALIGNED_16(DCTELEM, blocks[8][64]); | |
| 65 DECLARE_ALIGNED_8(ScanTable, scantable); | |
| 66 const CIDEntry *cid_table; | |
| 67 } DNXHDContext; | |
| 68 | |
| 69 static const CIDEntry cid_table[] = { | |
| 5464 | 70 { 1238, 1920, 1080, 0, 917504, 917504, 4, 8, |
| 4687 | 71 dnxhd_1238_luma_weigth, dnxhd_1238_chroma_weigth, |
| 72 dnxhd_1238_dc_codes, dnxhd_1238_dc_bits, | |
| 73 dnxhd_1238_ac_codes, dnxhd_1238_ac_bits, dnxhd_1238_ac_level, | |
| 74 dnxhd_1238_ac_run_flag, dnxhd_1238_ac_index_flag, | |
| 75 dnxhd_1238_run_codes, dnxhd_1238_run_bits, dnxhd_1238_run }, | |
| 5464 | 76 /* { 1243, 1920, 1080, 1, 917504, 458752, 4, 8, */ |
| 4687 | 77 /* dnxhd_1243_luma_weigth, dnxhd_1243_chroma_weigth, */ |
| 78 /* dnxhd_1238_dc_codes, dnxhd_1238_dc_bits, */ | |
| 79 /* dnxhd_1238_ac_codes, dnxhd_1238_ac_bits, dnxhd_1238_ac_level, */ | |
| 80 /* dnxhd_1238_ac_run_flag, dnxhd_1238_ac_index_flag, */ | |
| 81 /* dnxhd_1238_run_codes, dnxhd_1238_run_bits, dnxhd_1238_run }, */ | |
| 82 }; | |
| 83 | |
| 84 static int dnxhd_get_cid_table(int cid) | |
| 85 { | |
| 86 int i; | |
| 87 for (i = 0; i < sizeof(cid_table)/sizeof(CIDEntry); i++) | |
| 88 if (cid_table[i].cid == cid) | |
| 89 return i; | |
| 90 return -1; | |
| 91 } | |
| 92 | |
| 93 #define DNXHD_VLC_BITS 9 | |
| 94 #define DNXHD_DC_VLC_BITS 6 | |
| 95 | |
| 96 static int dnxhd_decode_init(AVCodecContext *avctx) | |
| 97 { | |
| 98 DNXHDContext *ctx = avctx->priv_data; | |
| 99 | |
| 100 ctx->avctx = avctx; | |
| 101 dsputil_init(&ctx->dsp, avctx); | |
| 102 avctx->coded_frame = &ctx->picture; | |
| 103 ctx->picture.type = FF_I_TYPE; | |
| 104 return 0; | |
| 105 } | |
| 106 | |
| 107 static int dnxhd_init_vlc(DNXHDContext *ctx, int cid) | |
| 108 { | |
| 109 if (!ctx->cid_table) { | |
| 110 int index; | |
| 111 | |
| 112 if ((index = dnxhd_get_cid_table(cid)) < 0) { | |
| 113 av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %d\n", cid); | |
| 114 return -1; | |
| 115 } | |
| 116 ctx->cid_table = &cid_table[index]; | |
| 117 init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257, | |
| 118 cid_table->ac_bits, 1, 1, | |
| 119 cid_table->ac_codes, 2, 2, 0); | |
| 120 init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, 12, | |
| 121 cid_table->dc_bits, 1, 1, | |
| 122 cid_table->dc_codes, 1, 1, 0); | |
| 123 init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62, | |
| 124 cid_table->run_bits, 1, 1, | |
| 125 cid_table->run_codes, 2, 2, 0); | |
| 126 | |
| 127 ctx->run = cid_table->run; | |
| 128 ctx->ac_level = cid_table->ac_level; | |
| 129 ctx->ac_run_flag = cid_table->ac_run_flag; | |
| 130 ctx->ac_index_flag = cid_table->ac_index_flag; | |
| 131 ctx->luma_weigth = cid_table->luma_weigth; | |
| 132 ctx->chroma_weigth = cid_table->chroma_weigth; | |
| 133 | |
| 134 ctx->index_bits = cid_table->index_bits; | |
| 135 | |
| 136 ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, ff_zigzag_direct); | |
| 137 } | |
| 138 return 0; | |
| 139 } | |
| 140 | |
| 141 static int dnxhd_decode_header(DNXHDContext *ctx, uint8_t *buf, int buf_size) | |
| 142 { | |
| 143 static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 }; | |
| 144 int i; | |
| 145 | |
| 146 if (buf_size < 0x280) | |
| 147 return -1; | |
| 148 | |
| 149 if (memcmp(buf, header_prefix, 5)) { | |
| 150 av_log(ctx->avctx, AV_LOG_ERROR, "error in header\n"); | |
| 151 return -1; | |
| 152 } | |
| 153 if (buf[5] & 2) {/* interlaced FIXME top or bottom */ | |
| 154 ctx->picture.interlaced_frame = 1; | |
| 155 av_log(ctx->avctx, AV_LOG_DEBUG, "interlaced %d\n", buf[5] & 3); | |
| 156 } | |
| 157 | |
| 158 ctx->height = AV_RB16(buf + 0x18); | |
| 159 ctx->width = AV_RB16(buf + 0x1a); | |
| 160 | |
|
4688
1cefcd7da878
10l, fix debug, dprintf does not have log level
bcoudurier
parents:
4687
diff
changeset
|
161 dprintf(ctx->avctx, "width %d, heigth %d\n", ctx->width, ctx->height); |
| 4687 | 162 |
| 163 if (buf[0x21] & 0x80) { | |
| 164 av_log(ctx->avctx, AV_LOG_ERROR, "10 bit per component\n"); | |
| 165 return -1; | |
| 166 } | |
| 167 | |
| 168 ctx->cid = AV_RB32(buf + 0x28); | |
|
4688
1cefcd7da878
10l, fix debug, dprintf does not have log level
bcoudurier
parents:
4687
diff
changeset
|
169 dprintf(ctx->avctx, "compression id %d\n", ctx->cid); |
| 4687 | 170 |
| 171 if (dnxhd_init_vlc(ctx, ctx->cid) < 0) | |
| 172 return -1; | |
| 173 | |
| 5464 | 174 if (buf_size < ctx->cid_table->coding_unit_size) { |
| 4687 | 175 av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size\n"); |
| 176 return -1; | |
| 177 } | |
| 178 | |
| 179 ctx->mb_width = ctx->width>>4; | |
| 180 ctx->mb_height = buf[0x16d]; | |
| 181 | |
| 182 if (ctx->mb_height > 68) { | |
| 183 av_log(ctx->avctx, AV_LOG_ERROR, "mb height too big\n"); | |
| 184 return -1; | |
| 185 } | |
| 186 | |
|
4688
1cefcd7da878
10l, fix debug, dprintf does not have log level
bcoudurier
parents:
4687
diff
changeset
|
187 dprintf(ctx->avctx, "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height); |
| 4687 | 188 for (i = 0; i < ctx->mb_height; i++) { |
| 189 ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i<<2)); | |
|
4688
1cefcd7da878
10l, fix debug, dprintf does not have log level
bcoudurier
parents:
4687
diff
changeset
|
190 dprintf(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]); |
| 4687 | 191 if (buf_size < ctx->mb_scan_index[i] + 0x280) { |
| 192 av_log(ctx->avctx, AV_LOG_ERROR, "invalid mb scan index\n"); | |
| 193 return -1; | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 return 0; | |
| 198 } | |
| 199 | |
| 200 static int dnxhd_decode_dc(DNXHDContext *ctx) | |
| 201 { | |
| 202 int len; | |
| 203 | |
| 204 len = get_vlc2(&ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1); | |
| 205 return len ? get_xbits(&ctx->gb, len) : 0; | |
| 206 } | |
| 207 | |
| 208 static void dnxhd_decode_dct_block(DNXHDContext *ctx, DCTELEM *block, int n, int qscale) | |
| 209 { | |
| 210 int i, j, index, index2; | |
| 211 int level, component, sign; | |
| 212 const uint8_t *weigth_matrix; | |
| 213 | |
| 214 if (n&2) { | |
| 215 component = 1 + (n&1); | |
| 216 weigth_matrix = ctx->chroma_weigth; | |
| 217 } else { | |
| 218 component = 0; | |
| 219 weigth_matrix = ctx->luma_weigth; | |
| 220 } | |
| 221 | |
| 222 ctx->last_dc[component] += dnxhd_decode_dc(ctx); | |
| 223 block[0] = ctx->last_dc[component]; | |
| 224 //av_log(ctx->avctx, AV_LOG_DEBUG, "dc %d\n", block[0]); | |
| 225 for (i = 1; ; i++) { | |
| 226 index = get_vlc2(&ctx->gb, ctx->ac_vlc.table, DNXHD_VLC_BITS, 2); | |
| 227 //av_log(ctx->avctx, AV_LOG_DEBUG, "index %d\n", index); | |
| 228 level = ctx->ac_level[index]; | |
| 229 if (!level) { /* EOB */ | |
| 230 //av_log(ctx->avctx, AV_LOG_DEBUG, "EOB\n"); | |
| 231 return; | |
| 232 } | |
| 233 sign = get_sbits(&ctx->gb, 1); | |
| 234 | |
| 235 if (ctx->ac_index_flag[index]) { | |
| 236 level += get_bits(&ctx->gb, ctx->index_bits)<<6; | |
| 237 } | |
| 238 | |
| 239 if (ctx->ac_run_flag[index]) { | |
| 240 index2 = get_vlc2(&ctx->gb, ctx->run_vlc.table, DNXHD_VLC_BITS, 2); | |
| 241 i += ctx->run[index2]; | |
| 242 } | |
| 243 | |
| 244 j = ctx->scantable.permutated[i]; | |
| 245 //av_log(ctx->avctx, AV_LOG_DEBUG, "j %d\n", j); | |
| 246 //av_log(ctx->avctx, AV_LOG_DEBUG, "level %d, weigth %d\n", level, weigth_matrix[i]); | |
| 247 level = (2*level+1) * qscale * weigth_matrix[i]; | |
| 248 if (weigth_matrix[i] != 32) // FIXME 10bit | |
| 249 level += 32; | |
| 250 level >>= 6; | |
| 251 level = (level^sign) - sign; | |
| 252 | |
| 253 if (i > 63) { | |
| 254 av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i); | |
| 255 return; | |
| 256 } | |
| 257 | |
| 258 //av_log(NULL, AV_LOG_DEBUG, "i %d, j %d, end level %d\n", i, j, level); | |
| 259 block[j] = level; | |
| 260 } | |
| 261 } | |
| 262 | |
| 263 static int dnxhd_decode_macroblock(DNXHDContext *ctx, int x, int y) | |
| 264 { | |
| 265 int dct_linesize_luma = ctx->picture.linesize[0]; | |
| 266 int dct_linesize_chroma = ctx->picture.linesize[1]; | |
| 267 uint8_t *dest_y, *dest_u, *dest_v; | |
| 268 int dct_offset; | |
| 269 int qscale, i; | |
| 270 | |
| 271 ctx->dsp.clear_blocks(ctx->blocks[0]); | |
| 272 ctx->dsp.clear_blocks(ctx->blocks[2]); // FIXME change clear blocks to take block amount | |
| 273 | |
| 274 qscale = get_bits(&ctx->gb, 11); | |
| 275 skip_bits1(&ctx->gb); | |
| 276 //av_log(ctx->avctx, AV_LOG_DEBUG, "qscale %d\n", qscale); | |
| 277 | |
| 278 for (i = 0; i < 8; i++) { | |
| 279 dnxhd_decode_dct_block(ctx, ctx->blocks[i], i, qscale); | |
| 280 } | |
| 281 dest_y = ctx->picture.data[0] + ((y * dct_linesize_luma) << 4) + (x << 4); | |
| 282 dest_u = ctx->picture.data[1] + ((y * dct_linesize_chroma) << 4) + (x << 3); | |
| 283 dest_v = ctx->picture.data[2] + ((y * dct_linesize_chroma) << 4) + (x << 3); | |
| 284 | |
| 285 dct_offset = dct_linesize_luma << 3; | |
| 286 ctx->dsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]); | |
| 287 ctx->dsp.idct_put(dest_y + 8, dct_linesize_luma, ctx->blocks[1]); | |
| 288 ctx->dsp.idct_put(dest_y + dct_offset, dct_linesize_luma, ctx->blocks[4]); | |
| 289 ctx->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize_luma, ctx->blocks[5]); | |
| 290 | |
| 291 if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) { | |
| 292 dct_offset = dct_linesize_chroma << 3; | |
| 293 ctx->dsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]); | |
| 294 ctx->dsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[3]); | |
| 295 ctx->dsp.idct_put(dest_u + dct_offset, dct_linesize_chroma, ctx->blocks[6]); | |
| 296 ctx->dsp.idct_put(dest_v + dct_offset, dct_linesize_chroma, ctx->blocks[7]); | |
| 297 } | |
| 298 | |
| 299 return 0; | |
| 300 } | |
| 301 | |
| 302 static int dnxhd_decode_macroblocks(DNXHDContext *ctx, uint8_t *buf, int buf_size) | |
| 303 { | |
| 304 int x, y; | |
| 305 for (y = 0; y < ctx->mb_height; y++) { | |
| 5463 | 306 ctx->last_dc[0] = |
| 307 ctx->last_dc[1] = | |
| 308 ctx->last_dc[2] = 1024; // 1024 for levels +128 | |
| 4687 | 309 init_get_bits(&ctx->gb, buf + ctx->mb_scan_index[y], (buf_size - ctx->mb_scan_index[y]) << 3); |
| 310 for (x = 0; x < ctx->mb_width; x++) { | |
| 311 //START_TIMER; | |
| 312 dnxhd_decode_macroblock(ctx, x, y); | |
| 313 //STOP_TIMER("decode macroblock"); | |
| 314 } | |
| 315 } | |
| 316 return 0; | |
| 317 } | |
| 318 | |
| 319 static int dnxhd_decode_frame(AVCodecContext *avctx, void *data, int *data_size, | |
| 320 uint8_t *buf, int buf_size) | |
| 321 { | |
| 322 DNXHDContext *ctx = avctx->priv_data; | |
| 323 AVFrame *picture = data; | |
| 324 | |
|
4688
1cefcd7da878
10l, fix debug, dprintf does not have log level
bcoudurier
parents:
4687
diff
changeset
|
325 dprintf(avctx, "frame size %d\n", buf_size); |
| 4687 | 326 |
| 327 if (dnxhd_decode_header(ctx, buf, buf_size) < 0) | |
| 328 return -1; | |
| 329 | |
| 330 avctx->pix_fmt = PIX_FMT_YUV422P; | |
| 331 if (avcodec_check_dimensions(avctx, ctx->width, ctx->height)) | |
| 332 return -1; | |
| 333 avcodec_set_dimensions(avctx, ctx->width, ctx->height); | |
| 334 | |
| 335 if (ctx->picture.data[0]) | |
| 336 avctx->release_buffer(avctx, &ctx->picture); | |
| 337 if (avctx->get_buffer(avctx, &ctx->picture) < 0) { | |
| 338 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
| 339 return -1; | |
| 340 } | |
| 341 | |
| 342 dnxhd_decode_macroblocks(ctx, buf + 0x280, buf_size - 0x280); | |
| 343 | |
| 344 *picture = ctx->picture; | |
| 345 *data_size = sizeof(AVPicture); | |
| 4721 | 346 return buf_size; |
| 4687 | 347 } |
| 348 | |
| 349 static int dnxhd_decode_close(AVCodecContext *avctx) | |
| 350 { | |
| 351 DNXHDContext *ctx = avctx->priv_data; | |
| 352 | |
| 353 if(ctx->picture.data[0]) | |
| 354 avctx->release_buffer(avctx, &ctx->picture); | |
| 355 free_vlc(&ctx->ac_vlc); | |
| 356 free_vlc(&ctx->dc_vlc); | |
| 357 free_vlc(&ctx->run_vlc); | |
| 358 return 0; | |
| 359 } | |
| 360 | |
| 361 AVCodec dnxhd_decoder = { | |
| 362 "dnxhd", | |
| 363 CODEC_TYPE_VIDEO, | |
| 364 CODEC_ID_DNXHD, | |
| 365 sizeof(DNXHDContext), | |
| 366 dnxhd_decode_init, | |
| 367 NULL, | |
| 368 dnxhd_decode_close, | |
| 369 dnxhd_decode_frame, | |
| 370 CODEC_CAP_DR1, | |
| 371 }; |
