1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

dnxhd decoder

Originally committed as revision 8445 to svn://svn.ffmpeg.org/ffmpeg/trunk
This commit is contained in:
Baptiste Coudurier 2007-03-18 23:25:00 +00:00
parent 4140a5234e
commit 52b6bad219
8 changed files with 483 additions and 2 deletions

View File

@ -74,6 +74,7 @@ version <next>
- GSM-MS encoder and decoder
- DCA decoder
- DXA demuxer and decoder
- DNxHD decoder
version 0.4.9-pre1:

View File

@ -113,6 +113,7 @@ Codecs:
cljr Alex Beregszaszi
cook.c, cookdata.h Benjamin Larsson
cscd.c Reimar Doeffinger
dnxhddec.c Baptiste Coudurier
dpcm.c Mike Melanson
dxa.c Kostya Shishkov
dv.c Roman Shaposhnik

View File

@ -1009,6 +1009,7 @@ following image formats are supported:
@item Cin Video @tab @tab X @tab Codec used in Delphine Software games.
@item Tiertex Seq Video @tab @tab X @tab Codec used in DOS CDROM FlashBack game.
@item DXA Video @tab @tab X @tab Codec originally used in Feeble Files game.
@item AVID DNxHD @tab @tab X @tab aka SMPTE VC3
@end multitable
@code{X} means that encoding (resp. decoding) is supported.

View File

@ -64,6 +64,7 @@ OBJS-$(CONFIG_COOK_DECODER) += cook.o
OBJS-$(CONFIG_CSCD_DECODER) += cscd.o
OBJS-$(CONFIG_CYUV_DECODER) += cyuv.o
OBJS-$(CONFIG_DCA_DECODER) += dca.o
OBJS-$(CONFIG_DNXHD_DECODER) += dnxhddec.o
OBJS-$(CONFIG_DSICINVIDEO_DECODER) += dsicinav.o
OBJS-$(CONFIG_DSICINAUDIO_DECODER) += dsicinav.o
OBJS-$(CONFIG_DVBSUB_DECODER) += dvbsubdec.o

View File

@ -65,6 +65,7 @@ void avcodec_register_all(void)
REGISTER_DECODER(CSCD, cscd);
REGISTER_DECODER(CYUV, cyuv);
REGISTER_DECODER(DCA, dca);
REGISTER_DECODER(DNXHD, dnxhd);
REGISTER_DECODER(DSICINVIDEO, dsicinvideo);
REGISTER_ENCDEC (DVVIDEO, dvvideo);
REGISTER_DECODER(DXA, dxa);

View File

@ -37,8 +37,8 @@ extern "C" {
#define AV_STRINGIFY(s) AV_TOSTRING(s)
#define AV_TOSTRING(s) #s
#define LIBAVCODEC_VERSION_INT ((51<<16)+(39<<8)+0)
#define LIBAVCODEC_VERSION 51.39.0
#define LIBAVCODEC_VERSION_INT ((51<<16)+(40<<8)+0)
#define LIBAVCODEC_VERSION 51.40.0
#define LIBAVCODEC_BUILD LIBAVCODEC_VERSION_INT
#define LIBAVCODEC_IDENT "Lavc" AV_STRINGIFY(LIBAVCODEC_VERSION)
@ -151,6 +151,7 @@ enum CodecID {
CODEC_ID_GIF,
CODEC_ID_FFH264,
CODEC_ID_DXA,
CODEC_ID_DNXHD,
/* various pcm "codecs" */
CODEC_ID_PCM_S16LE= 0x10000,
@ -2231,6 +2232,7 @@ extern AVCodec cook_decoder;
extern AVCodec cscd_decoder;
extern AVCodec cyuv_decoder;
extern AVCodec dca_decoder;
extern AVCodec dnxhd_decoder;
extern AVCodec dsicinaudio_decoder;
extern AVCodec dsicinvideo_decoder;
extern AVCodec dvvideo_decoder;

106
libavcodec/dnxhddata.h Normal file
View File

@ -0,0 +1,106 @@
/*
* VC3/DNxHD decoder.
* Copyright (c) 2007 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>.
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
static const uint8_t dnxhd_1238_luma_weigth[] = {
0, 32, 32, 33, 34, 33, 33, 33,
33, 33, 33, 33, 33, 35, 37, 37,
36, 36, 35, 36, 38, 38, 36, 35,
36, 37, 38, 41, 42, 41, 39, 38,
38, 38, 39, 41, 42, 41, 39, 39,
40, 41, 43, 44, 44, 44, 44, 44,
45, 47, 47, 47, 49, 50, 51, 51,
51, 53, 55, 57, 58, 59, 57, 57,
};
static const uint8_t dnxhd_1238_chroma_weigth[] = {
0, 32, 35, 35, 35, 34, 34, 35,
39, 43, 45, 45, 41, 39, 40, 41,
42, 44, 48, 55, 59, 63, 65, 59,
53, 52, 52, 55, 61, 62, 58, 58,
63, 66, 66, 65, 70, 74, 70, 66,
65, 68, 75, 77, 74, 74, 77, 76,
73, 73, 73, 73, 76, 80, 89, 90,
82, 77, 80, 86, 84, 82, 82, 82,
};
/* FIXME permute */
static const uint8_t dnxhd_1243_luma_weigth[] = {
0,32,35,35,38,40,44,45,
32,33,35,35,39,42,44,45,
33,35,34,37,41,42,45,45,
35,35,37,40,41,44,45,48,
35,37,38,40,43,45,47,48,
36,36,38,40,45,47,48,47,
35,36,38,41,45,46,47,48,
36,37,39,41,44,45,47,47,
};
/* FIXME permute */
static const uint8_t dnxhd_1243_chroma_weigth[] = {
0,32,37,39,41,42,45,45,
36,36,39,41,43,45,46,45,
37,41,41,43,45,44,45,46,
43,42,43,46,44,45,46,48,
43,44,47,45,44,46,47,49,
44,46,44,45,45,47,48,47,
44,42,46,44,45,46,47,48,
41,43,45,44,45,46,47,47,
};
static const uint8_t dnxhd_1238_dc_codes[12] = {
0, 12, 13, 1, 2, 3, 4, 5, 14, 30, 62, 63,
};
static const uint8_t dnxhd_1238_dc_bits[12] = {
3, 4, 4, 3, 3, 3, 3, 3, 4, 5, 6, 6,
};
static const uint16_t dnxhd_1238_ac_codes[257] = {
0, 1, 4, 10, 11, 24, 25, 26, 54, 55, 56, 57, 116, 117, 118, 119, 240, 241, 242, 243, 244, 245, 492, 493, 494, 495, 496, 497, 498, 499, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 4056, 4057, 4058, 4059, 4060, 4061, 4062, 4063, 4064, 4065, 4066, 4067, 4068, 4069, 8140, 8141, 8142, 8143, 8144, 8145, 8146, 8147, 8148, 8149, 8150, 8151, 8152, 8153, 8154, 8155, 8156, 16314, 16315, 16316, 16317, 16318, 16319, 16320, 16321, 16322, 16323, 16324, 16325, 16326, 16327, 16328, 16329, 16330, 16331, 16332, 16333, 16334, 16335, 16336, 16337, 16338, 32678, 32679, 32680, 32681, 32682, 32683, 32684, 32685, 32686, 32687, 32688, 32689, 32690, 32691, 32692, 32693, 32694, 32695, 32696, 32697, 32698, 32699, 32700, 32701, 32702, 32703, 32704, 32705, 65412, 65413, 65414, 65415, 65416, 65417, 65418, 65419, 65420, 65421, 65422, 65423, 65424, 65425, 65426, 65427, 65428, 65429, 65430, 65431, 65432, 65433, 65434, 65435, 65436, 65437, 65438, 65439, 65440, 65441, 65442, 65443, 65444, 65445, 65446, 65447, 65448, 65449, 65450, 65451, 65452, 65453, 65454, 65455, 65456, 65457, 65458, 65459, 65460, 65461, 65462, 65463, 65464, 65465, 65466, 65467, 65468, 65469, 65470, 65471, 65472, 65473, 65474, 65475, 65476, 65477, 65478, 65479, 65480, 65481, 65482, 65483, 65484, 65485, 65486, 65487, 65488, 65489, 65490, 65491, 65492, 65493, 65494, 65495, 65496, 65497, 65498, 65499, 65500, 65501, 65502, 65503, 65504, 65505, 65506, 65507, 65508, 65509, 65510, 65511, 65512, 65513, 65514, 65515, 65516, 65517, 65518, 65519, 65520, 65521, 65522, 65523, 65524, 65525, 65526, 65527, 65528, 65529, 65530, 65531, 65532, 65533, 65534, 65535,
};
static const uint8_t dnxhd_1238_ac_bits[257] = {
2, 2, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
};
static const uint8_t dnxhd_1238_ac_level[257] = {
1, 1, 2, 3, 0, 4, 5, 2, 6, 7, 8, 3, 9, 10, 11, 4, 12, 13, 14, 15, 16, 5, 17, 18, 19, 20, 21, 22, 6, 7, 23, 24, 25, 26, 27, 28, 29, 8, 9, 30, 31, 32, 33, 34, 35, 36, 37, 10, 11, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 12, 13, 14, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 15, 16, 17, 18, 62, 63, 64, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19, 20, 21, 22, 23, 24, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 40, 25, 26, 27, 28, 29, 30, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
}; /* 0 is EOB */
static const uint8_t dnxhd_1238_ac_run_flag[257] = {
0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
static const uint8_t dnxhd_1238_ac_index_flag[257] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
static const uint16_t dnxhd_1238_run_codes[62] = {
0, 4, 10, 11, 24, 25, 26, 27, 56, 57, 58, 59, 120, 242, 486, 487, 488, 489, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023,
};
static const uint8_t dnxhd_1238_run_bits[62] = {
1, 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
};
static const uint8_t dnxhd_1238_run[62] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 21, 17, 18, 19, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
};

368
libavcodec/dnxhddec.c Normal file
View File

@ -0,0 +1,368 @@
/*
* VC3/DNxHD decoder.
* Copyright (c) 2007 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>.
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
//#define TRACE
//#define DEBUG
#include "avcodec.h"
#include "bitstream.h"
#include "dnxhddata.h"
#include "dsputil.h"
#include "mpegvideo.h"
typedef struct {
int cid;
unsigned int width, height;
int interlaced;
unsigned int frame_size;
int index_bits;
int bit_depth;
const uint8_t *luma_weigth, *chroma_weigth;
const uint8_t *dc_codes, *dc_bits;
const uint16_t *ac_codes;
const uint8_t *ac_bits, *ac_level;
const uint8_t *ac_run_flag, *ac_index_flag;
const uint16_t *run_codes;
const uint8_t *run_bits, *run;
} CIDEntry;
typedef struct {
AVCodecContext *avctx;
AVFrame picture;
GetBitContext gb;
int cid; ///< compression id
unsigned int width, height;
unsigned int mb_width, mb_height;
uint32_t mb_scan_index[68]; /* max for 1080p */
int cur_field; ///< current interlaced field
int index_bits; ///< length of index value
VLC ac_vlc, dc_vlc, run_vlc;
const uint8_t *ac_level, *run;
const uint8_t *ac_run_flag, *ac_index_flag;
const uint8_t *luma_weigth, *chroma_weigth;
int last_dc[3];
DSPContext dsp;
DECLARE_ALIGNED_16(DCTELEM, blocks[8][64]);
DECLARE_ALIGNED_8(ScanTable, scantable);
const CIDEntry *cid_table;
} DNXHDContext;
static const CIDEntry cid_table[] = {
{ 1238, 1920, 1080, 0, 917504, 4, 8,
dnxhd_1238_luma_weigth, dnxhd_1238_chroma_weigth,
dnxhd_1238_dc_codes, dnxhd_1238_dc_bits,
dnxhd_1238_ac_codes, dnxhd_1238_ac_bits, dnxhd_1238_ac_level,
dnxhd_1238_ac_run_flag, dnxhd_1238_ac_index_flag,
dnxhd_1238_run_codes, dnxhd_1238_run_bits, dnxhd_1238_run },
/* { 1243, 1920, 1080, 1, 917504, 4, 8, */
/* dnxhd_1243_luma_weigth, dnxhd_1243_chroma_weigth, */
/* dnxhd_1238_dc_codes, dnxhd_1238_dc_bits, */
/* dnxhd_1238_ac_codes, dnxhd_1238_ac_bits, dnxhd_1238_ac_level, */
/* dnxhd_1238_ac_run_flag, dnxhd_1238_ac_index_flag, */
/* dnxhd_1238_run_codes, dnxhd_1238_run_bits, dnxhd_1238_run }, */
};
static int dnxhd_get_cid_table(int cid)
{
int i;
for (i = 0; i < sizeof(cid_table)/sizeof(CIDEntry); i++)
if (cid_table[i].cid == cid)
return i;
return -1;
}
#define DNXHD_VLC_BITS 9
#define DNXHD_DC_VLC_BITS 6
static int dnxhd_decode_init(AVCodecContext *avctx)
{
DNXHDContext *ctx = avctx->priv_data;
ctx->avctx = avctx;
dsputil_init(&ctx->dsp, avctx);
avctx->coded_frame = &ctx->picture;
ctx->picture.type = FF_I_TYPE;
return 0;
}
static int dnxhd_init_vlc(DNXHDContext *ctx, int cid)
{
if (!ctx->cid_table) {
int index;
if ((index = dnxhd_get_cid_table(cid)) < 0) {
av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %d\n", cid);
return -1;
}
ctx->cid_table = &cid_table[index];
init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
cid_table->ac_bits, 1, 1,
cid_table->ac_codes, 2, 2, 0);
init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, 12,
cid_table->dc_bits, 1, 1,
cid_table->dc_codes, 1, 1, 0);
init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
cid_table->run_bits, 1, 1,
cid_table->run_codes, 2, 2, 0);
ctx->run = cid_table->run;
ctx->ac_level = cid_table->ac_level;
ctx->ac_run_flag = cid_table->ac_run_flag;
ctx->ac_index_flag = cid_table->ac_index_flag;
ctx->luma_weigth = cid_table->luma_weigth;
ctx->chroma_weigth = cid_table->chroma_weigth;
ctx->index_bits = cid_table->index_bits;
ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, ff_zigzag_direct);
}
return 0;
}
static int dnxhd_decode_header(DNXHDContext *ctx, uint8_t *buf, int buf_size)
{
static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
int i;
if (buf_size < 0x280)
return -1;
if (memcmp(buf, header_prefix, 5)) {
av_log(ctx->avctx, AV_LOG_ERROR, "error in header\n");
return -1;
}
if (buf[5] & 2) {/* interlaced FIXME top or bottom */
ctx->picture.interlaced_frame = 1;
av_log(ctx->avctx, AV_LOG_DEBUG, "interlaced %d\n", buf[5] & 3);
}
ctx->height = AV_RB16(buf + 0x18);
ctx->width = AV_RB16(buf + 0x1a);
dprintf(ctx->avctx, AV_LOG_DEBUG, "width %d, heigth %d\n", ctx->width, ctx->height);
if (buf[0x21] & 0x80) {
av_log(ctx->avctx, AV_LOG_ERROR, "10 bit per component\n");
return -1;
}
ctx->cid = AV_RB32(buf + 0x28);
dprintf(ctx->avctx, AV_LOG_DEBUG, "compression id %d\n", ctx->cid);
if (dnxhd_init_vlc(ctx, ctx->cid) < 0)
return -1;
if (buf_size < ctx->cid_table->frame_size) {
av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size\n");
return -1;
}
ctx->mb_width = ctx->width>>4;
ctx->mb_height = buf[0x16d];
if (ctx->mb_height > 68) {
av_log(ctx->avctx, AV_LOG_ERROR, "mb height too big\n");
return -1;
}
dprintf(ctx->avctx, AV_LOG_DEBUG, "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);
for (i = 0; i < ctx->mb_height; i++) {
ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i<<2));
dprintf(ctx->avctx, AV_LOG_DEBUG, "mb scan index %d\n", ctx->mb_scan_index[i]);
if (buf_size < ctx->mb_scan_index[i] + 0x280) {
av_log(ctx->avctx, AV_LOG_ERROR, "invalid mb scan index\n");
return -1;
}
}
return 0;
}
static int dnxhd_decode_dc(DNXHDContext *ctx)
{
int len;
len = get_vlc2(&ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
return len ? get_xbits(&ctx->gb, len) : 0;
}
static void dnxhd_decode_dct_block(DNXHDContext *ctx, DCTELEM *block, int n, int qscale)
{
int i, j, index, index2;
int level, component, sign;
const uint8_t *weigth_matrix;
if (n&2) {
component = 1 + (n&1);
weigth_matrix = ctx->chroma_weigth;
} else {
component = 0;
weigth_matrix = ctx->luma_weigth;
}
ctx->last_dc[component] += dnxhd_decode_dc(ctx);
block[0] = ctx->last_dc[component];
//av_log(ctx->avctx, AV_LOG_DEBUG, "dc %d\n", block[0]);
for (i = 1; ; i++) {
index = get_vlc2(&ctx->gb, ctx->ac_vlc.table, DNXHD_VLC_BITS, 2);
//av_log(ctx->avctx, AV_LOG_DEBUG, "index %d\n", index);
level = ctx->ac_level[index];
if (!level) { /* EOB */
//av_log(ctx->avctx, AV_LOG_DEBUG, "EOB\n");
return;
}
sign = get_sbits(&ctx->gb, 1);
if (ctx->ac_index_flag[index]) {
level += get_bits(&ctx->gb, ctx->index_bits)<<6;
}
if (ctx->ac_run_flag[index]) {
index2 = get_vlc2(&ctx->gb, ctx->run_vlc.table, DNXHD_VLC_BITS, 2);
i += ctx->run[index2];
}
j = ctx->scantable.permutated[i];
//av_log(ctx->avctx, AV_LOG_DEBUG, "j %d\n", j);
//av_log(ctx->avctx, AV_LOG_DEBUG, "level %d, weigth %d\n", level, weigth_matrix[i]);
level = (2*level+1) * qscale * weigth_matrix[i];
if (weigth_matrix[i] != 32) // FIXME 10bit
level += 32;
level >>= 6;
level = (level^sign) - sign;
if (i > 63) {
av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
return;
}
//av_log(NULL, AV_LOG_DEBUG, "i %d, j %d, end level %d\n", i, j, level);
block[j] = level;
}
}
static int dnxhd_decode_macroblock(DNXHDContext *ctx, int x, int y)
{
int dct_linesize_luma = ctx->picture.linesize[0];
int dct_linesize_chroma = ctx->picture.linesize[1];
uint8_t *dest_y, *dest_u, *dest_v;
int dct_offset;
int qscale, i;
ctx->dsp.clear_blocks(ctx->blocks[0]);
ctx->dsp.clear_blocks(ctx->blocks[2]); // FIXME change clear blocks to take block amount
qscale = get_bits(&ctx->gb, 11);
skip_bits1(&ctx->gb);
//av_log(ctx->avctx, AV_LOG_DEBUG, "qscale %d\n", qscale);
for (i = 0; i < 8; i++) {
dnxhd_decode_dct_block(ctx, ctx->blocks[i], i, qscale);
}
dest_y = ctx->picture.data[0] + ((y * dct_linesize_luma) << 4) + (x << 4);
dest_u = ctx->picture.data[1] + ((y * dct_linesize_chroma) << 4) + (x << 3);
dest_v = ctx->picture.data[2] + ((y * dct_linesize_chroma) << 4) + (x << 3);
dct_offset = dct_linesize_luma << 3;
ctx->dsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]);
ctx->dsp.idct_put(dest_y + 8, dct_linesize_luma, ctx->blocks[1]);
ctx->dsp.idct_put(dest_y + dct_offset, dct_linesize_luma, ctx->blocks[4]);
ctx->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize_luma, ctx->blocks[5]);
if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
dct_offset = dct_linesize_chroma << 3;
ctx->dsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]);
ctx->dsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[3]);
ctx->dsp.idct_put(dest_u + dct_offset, dct_linesize_chroma, ctx->blocks[6]);
ctx->dsp.idct_put(dest_v + dct_offset, dct_linesize_chroma, ctx->blocks[7]);
}
return 0;
}
static int dnxhd_decode_macroblocks(DNXHDContext *ctx, uint8_t *buf, int buf_size)
{
int x, y;
for (y = 0; y < ctx->mb_height; y++) {
memset(ctx->last_dc, 4, sizeof(ctx->last_dc)); // 4 for levels +128
init_get_bits(&ctx->gb, buf + ctx->mb_scan_index[y], (buf_size - ctx->mb_scan_index[y]) << 3);
for (x = 0; x < ctx->mb_width; x++) {
//START_TIMER;
dnxhd_decode_macroblock(ctx, x, y);
//STOP_TIMER("decode macroblock");
}
}
return 0;
}
static int dnxhd_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
uint8_t *buf, int buf_size)
{
DNXHDContext *ctx = avctx->priv_data;
AVFrame *picture = data;
dprintf(avctx, AV_LOG_DEBUG, "frame size %d\n", buf_size);
if (dnxhd_decode_header(ctx, buf, buf_size) < 0)
return -1;
avctx->pix_fmt = PIX_FMT_YUV422P;
if (avcodec_check_dimensions(avctx, ctx->width, ctx->height))
return -1;
avcodec_set_dimensions(avctx, ctx->width, ctx->height);
if (ctx->picture.data[0])
avctx->release_buffer(avctx, &ctx->picture);
if (avctx->get_buffer(avctx, &ctx->picture) < 0) {
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return -1;
}
dnxhd_decode_macroblocks(ctx, buf + 0x280, buf_size - 0x280);
*picture = ctx->picture;
*data_size = sizeof(AVPicture);
return 0;
}
static int dnxhd_decode_close(AVCodecContext *avctx)
{
DNXHDContext *ctx = avctx->priv_data;
if(ctx->picture.data[0])
avctx->release_buffer(avctx, &ctx->picture);
free_vlc(&ctx->ac_vlc);
free_vlc(&ctx->dc_vlc);
free_vlc(&ctx->run_vlc);
return 0;
}
AVCodec dnxhd_decoder = {
"dnxhd",
CODEC_TYPE_VIDEO,
CODEC_ID_DNXHD,
sizeof(DNXHDContext),
dnxhd_decode_init,
NULL,
dnxhd_decode_close,
dnxhd_decode_frame,
CODEC_CAP_DR1,
};