1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-28 20:53:54 +02:00

dnxhd: add better support for CIDs 1270 to 1274

These are DNxHR profiles with the following properties:
- Variable size in a profile (property added in a previous commit),
  requiring variable-sized macroblock table;
- Variable bitdepth, up to 12 bits.
- Better validation of buffer sizes and positions

Signed-off-by: Christophe Gisquet <christophe.gisquet@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
This commit is contained in:
Jeremy James 2015-09-28 16:28:12 +01:00 committed by Michael Niedermayer
parent 66db504f03
commit 6f1ccca4ae
2 changed files with 118 additions and 38 deletions

View File

@ -49,7 +49,7 @@ static const uint8_t dnxhd_1235_chroma_weight[] = {
90, 90, 85, 79, 73, 73, 73, 73,
};
/* Used in CID 1237, 1253, 1259 */
/* Used in CID 1237, 1253, 1259, 1273, 1274 */
static const uint8_t dnxhd_1237_luma_weight[] = {
0, 32, 33, 34, 34, 36, 37, 36,
36, 37, 38, 38, 38, 39, 41, 44,
@ -61,7 +61,7 @@ static const uint8_t dnxhd_1237_luma_weight[] = {
97, 100, 104, 102, 98, 98, 99, 99,
};
/* Used in CID 1237, 1253, 1259 */
/* Used in CID 1237, 1253, 1259, 1273, 1274 */
static const uint8_t dnxhd_1237_chroma_weight[] = {
0, 32, 36, 39, 39, 38, 39, 41,
45, 51, 57, 58, 53, 48, 47, 51,
@ -73,6 +73,7 @@ static const uint8_t dnxhd_1237_chroma_weight[] = {
97, 100, 104, 102, 98, 98, 99, 99,
};
/* Used in CID 1238, 1272 */
static const uint8_t dnxhd_1238_luma_weight[] = {
0, 32, 32, 33, 34, 33, 33, 33,
33, 33, 33, 33, 33, 35, 37, 37,
@ -84,6 +85,7 @@ static const uint8_t dnxhd_1238_luma_weight[] = {
51, 53, 55, 57, 58, 59, 57, 57,
};
/* Used in CID 1238, 1272 */
static const uint8_t dnxhd_1238_chroma_weight[] = {
0, 32, 35, 35, 35, 34, 34, 35,
39, 43, 45, 45, 41, 39, 40, 41,
@ -95,6 +97,7 @@ static const uint8_t dnxhd_1238_chroma_weight[] = {
82, 77, 80, 86, 84, 82, 82, 82,
};
/* Used in CID 1241, 1271 */
static const uint8_t dnxhd_1241_luma_weight[] = {
0, 32, 33, 34, 34, 35, 36, 37,
36, 37, 38, 38, 38, 39, 39, 40,
@ -106,6 +109,7 @@ static const uint8_t dnxhd_1241_luma_weight[] = {
48, 46, 47, 48, 48, 49, 49, 49,
};
/* Used in CID 1241, 1271 */
static const uint8_t dnxhd_1241_chroma_weight[] = {
0, 32, 36, 38, 37, 37, 40, 41,
40, 40, 42, 42, 41, 41, 41, 41,
@ -261,17 +265,17 @@ static const uint8_t dnxhd_1235_dc_bits[14] = {
4, 6, 4, 4, 4, 3, 3, 3, 3, 3, 4, 5, 7, 7,
};
/* Used in CID 1237, 1238, 1242, 1243, 1251, 1252, 1253, 1258, 1259, 1260 */
/* Used in CID 1237, 1238, 1242, 1243, 1251, 1252, 1253, 1258, 1259, 1260, 1272, 1273, 1274 */
static const uint8_t dnxhd_1237_dc_codes[12] = {
0, 12, 13, 1, 2, 3, 4, 5, 14, 30, 62, 63,
};
/* Used in CID 1237, 1238, 1242, 1243, 1251, 1252, 1253, 1258, 1259, 1260 */
/* Used in CID 1237, 1238, 1242, 1243, 1251, 1252, 1253, 1258, 1259, 1260, 1272, 1273, 1274 */
static const uint8_t dnxhd_1237_dc_bits[12] = {
3, 4, 4, 3, 3, 3, 3, 3, 4, 5, 6, 6,
};
/* Used in CID 1237, 1242, 1253, 1259, 1260 */
/* Used in CID 1237, 1242, 1253, 1259, 1260, 1273, 1274 */
static const uint16_t dnxhd_1237_ac_codes[257] = {
0, 1, 4, 5, 12, 26, 27, 56,
57, 58, 59, 120, 121, 244, 245, 246,
@ -308,7 +312,7 @@ static const uint16_t dnxhd_1237_ac_codes[257] = {
65535,
};
/* Used in CID 1237, 1242, 1253, 1259, 1260 */
/* Used in CID 1237, 1242, 1253, 1259, 1260, 1273, 1274 */
static const uint8_t dnxhd_1237_ac_bits[257] = {
2, 2, 3, 3, 4, 5, 5, 6, 6, 6, 6, 7, 7, 8, 8, 8,
8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 11, 11, 11,
@ -329,7 +333,7 @@ static const uint8_t dnxhd_1237_ac_bits[257] = {
16,
};
/* Used in CID 1237, 1242, 1253, 1259, 1260 */
/* Used in CID 1237, 1242, 1253, 1259, 1260, 1273, 1274 */
static const uint8_t dnxhd_1237_ac_level[257] = {
3, 3, 5, 0, 7, 9, 5, 11, 13, 15, 7, 17, 19, 21, 23, 25,
9, 11, 27, 29, 31, 33, 13, 35, 37, 39, 41, 43, 15, 45, 47, 49,
@ -350,7 +354,7 @@ static const uint8_t dnxhd_1237_ac_level[257] = {
129,
};
/* Used in CID 1237, 1242, 1253, 1259, 1260 */
/* Used in CID 1237, 1242, 1253, 1259, 1260, 1273, 1274 */
static const uint8_t dnxhd_1237_ac_flags[257] = {
0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0,
2, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0,
@ -450,6 +454,7 @@ static const uint8_t dnxhd_1238_ac_level[257] = {
129,
}; /* 0 is EOB */
/* Used in CID 1238, 1243, 1272 */
static const uint8_t dnxhd_1238_ac_flags[257] = {
0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2,
0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0,
@ -528,7 +533,7 @@ static const uint8_t dnxhd_1235_ac_bits[257] = {
16,
};
/* Used in CID 1235, 1241, 1256 */
/* Used in CID 1235, 1241, 1256, 1270, 1271 */
static const uint8_t dnxhd_1235_ac_level[257] = {
3, 3, 5, 7, 0, 9, 11, 5, 13, 15, 17, 7, 19, 21, 23, 9,
25, 27, 29, 31, 33, 11, 35, 37, 39, 41, 43, 13, 15, 45, 47, 49,
@ -549,7 +554,7 @@ static const uint8_t dnxhd_1235_ac_level[257] = {
129,
};
/* Used in CID 1235, 1241, 1256 */
/* Used in CID 1235, 1241, 1256, 1270, 1271 */
static const uint8_t dnxhd_1235_ac_flags[257] = {
0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2,
0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0,
@ -858,7 +863,7 @@ static const uint8_t dnxhd_1252_ac_flags[257] = {
3,
};
/* Used in CID 1235, 1238, 1241, 1256 */
/* Used in CID 1235, 1238, 1241, 1243, 1256, 1270, 1271, 1272 */
static const uint16_t dnxhd_1235_run_codes[62] = {
0, 4, 10, 11, 24, 25, 26, 27,
56, 57, 58, 59, 120, 242, 486, 487,
@ -870,7 +875,7 @@ static const uint16_t dnxhd_1235_run_codes[62] = {
1018, 1019, 1020, 1021, 1022, 1023,
};
/* Used in CID 1235, 1238, 1241, 1243, 1256 */
/* Used in CID 1235, 1238, 1241, 1243, 1256, 1270, 1271, 1272 */
static const uint8_t dnxhd_1235_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,
@ -878,7 +883,7 @@ static const uint8_t dnxhd_1235_run_bits[62] = {
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
};
/* Used in CID 1235, 1241, 1256 */
/* Used in CID 1235, 1241, 1256, 1270, 1271 */
static const uint8_t dnxhd_1235_run[62] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
18, 20, 17, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
@ -886,7 +891,7 @@ static const uint8_t dnxhd_1235_run[62] = {
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
};
/* Used in CID 1237, 1242, 1253, 1259, 1260 */
/* Used in CID 1237, 1242, 1253, 1259, 1260, 1273, 1274 */
static const uint16_t dnxhd_1237_run_codes[62] = {
0, 4, 10, 11, 24, 25, 26, 54,
55, 56, 57, 58, 118, 119, 240, 482,
@ -898,7 +903,7 @@ static const uint16_t dnxhd_1237_run_codes[62] = {
1018, 1019, 1020, 1021, 1022, 1023,
};
/* Used in CID 1237, 1242, 1253, 1259, 1260 */
/* Used in CID 1237, 1242, 1253, 1259, 1260, 1273, 1274 */
static const uint8_t dnxhd_1237_run_bits[62] = {
1, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 8, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
@ -906,7 +911,7 @@ static const uint8_t dnxhd_1237_run_bits[62] = {
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
};
/* Used in CID 1237, 1242, 1253, 1259, 1260 */
/* Used in CID 1237, 1242, 1253, 1259, 1260, 1273, 1274 */
static const uint8_t dnxhd_1237_run[62] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 53, 57, 58, 59, 60, 61, 62, 22, 23, 24, 25,
@ -914,6 +919,7 @@ static const uint8_t dnxhd_1237_run[62] = {
42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 54, 55, 56,
};
/* Used in CID 1238, 1243, 1272 */
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,
@ -1074,13 +1080,37 @@ const CIDEntry ff_dnxhd_cid_table[] = {
dnxhd_1237_run_codes, dnxhd_1237_run_bits, dnxhd_1237_run,
{ 80, 90, 100, 110 } },
{ 1270, DNXHD_VARIABLE, DNXHD_VARIABLE, DNXHD_VARIABLE, DNXHD_VARIABLE,
DNXHD_444, 6, 10, 4,
DNXHD_444, 6, DNXHD_VARIABLE, 4,
dnxhd_1235_luma_weight, dnxhd_1235_luma_weight,
dnxhd_1235_dc_codes, dnxhd_1235_dc_bits,
dnxhd_1235_ac_codes, dnxhd_1235_ac_bits, dnxhd_1235_ac_level,
dnxhd_1235_ac_flags,
dnxhd_1235_run_codes, dnxhd_1235_run_bits, dnxhd_1235_run,
{ 0 } },
{ 1271, DNXHD_VARIABLE, DNXHD_VARIABLE, DNXHD_VARIABLE, DNXHD_VARIABLE,
0, 6, DNXHD_VARIABLE, 4,
dnxhd_1241_luma_weight, dnxhd_1241_chroma_weight,
dnxhd_1235_dc_codes, dnxhd_1235_dc_bits,
dnxhd_1235_ac_codes, dnxhd_1235_ac_bits, dnxhd_1235_ac_level,
dnxhd_1235_ac_flags,
dnxhd_1235_run_codes, dnxhd_1235_run_bits, dnxhd_1235_run,
{ 0 } },
{ 1272, DNXHD_VARIABLE, DNXHD_VARIABLE, DNXHD_VARIABLE, DNXHD_VARIABLE,
0, 4, 8, 4,
dnxhd_1238_luma_weight, dnxhd_1238_chroma_weight,
dnxhd_1237_dc_codes, dnxhd_1237_dc_bits,
dnxhd_1238_ac_codes, dnxhd_1238_ac_bits, dnxhd_1238_ac_level,
dnxhd_1238_ac_flags,
dnxhd_1235_run_codes, dnxhd_1235_run_bits, dnxhd_1238_run,
{ 0 } },
{ 1273, DNXHD_VARIABLE, DNXHD_VARIABLE, DNXHD_VARIABLE, DNXHD_VARIABLE,
0, 4, 8, 3,
dnxhd_1237_luma_weight, dnxhd_1237_chroma_weight,
dnxhd_1237_dc_codes, dnxhd_1237_dc_bits,
dnxhd_1237_ac_codes, dnxhd_1237_ac_bits, dnxhd_1237_ac_level,
dnxhd_1237_ac_flags,
dnxhd_1237_run_codes, dnxhd_1237_run_bits, dnxhd_1237_run,
{ 0 } },
{ 1274, DNXHD_VARIABLE, DNXHD_VARIABLE, DNXHD_VARIABLE, DNXHD_VARIABLE,
0, 4, 8, 3,
dnxhd_1237_luma_weight, dnxhd_1237_chroma_weight,

View File

@ -55,13 +55,14 @@ typedef struct DNXHDContext {
unsigned int width, height;
enum AVPixelFormat pix_fmt;
unsigned int mb_width, mb_height;
uint32_t mb_scan_index[68]; /* max for 1080p */
uint32_t *mb_scan_index;
int data_offset; // End of mb_scan_index, where macroblocks start
int cur_field; ///< current interlaced field
VLC ac_vlc, dc_vlc, run_vlc;
IDCTDSPContext idsp;
ScanTable scantable;
const CIDEntry *cid_table;
int bit_depth; // 8, 10 or 0 if not initialized at all.
int bit_depth; // 8, 10, 12 or 0 if not initialized at all.
int is_444;
int mbaff;
int act;
@ -78,6 +79,10 @@ static int dnxhd_decode_dct_block_10(const DNXHDContext *ctx,
RowContext *row, int n);
static int dnxhd_decode_dct_block_10_444(const DNXHDContext *ctx,
RowContext *row, int n);
static int dnxhd_decode_dct_block_12(const DNXHDContext *ctx,
RowContext *row, int n);
static int dnxhd_decode_dct_block_12_444(const DNXHDContext *ctx,
RowContext *row, int n);
static av_cold int dnxhd_decode_init(AVCodecContext *avctx)
{
@ -153,9 +158,11 @@ static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
{
static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
static const uint8_t header_prefix444[] = { 0x00, 0x00, 0x02, 0x80, 0x02 };
static const uint8_t header_prefixhr[] = { 0x00, 0x00, 0x02, 0x80, 0x03 };
static const uint8_t header_prefixhr1[] = { 0x00, 0x00, 0x02, 0x80, 0x03 };
static const uint8_t header_prefixhr2[] = { 0x00, 0x00, 0x03, 0x8C, 0x03 };
int i, cid, ret;
int old_bit_depth = ctx->bit_depth;
int old_mb_height = ctx->mb_height;
if (buf_size < 0x280) {
av_log(ctx->avctx, AV_LOG_ERROR,
@ -163,7 +170,8 @@ static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
return AVERROR_INVALIDDATA;
}
if (memcmp(buf, header_prefix, 5) && memcmp(buf, header_prefix444, 5) && memcmp(buf, header_prefixhr, 5)) {
if (memcmp(buf, header_prefix, 5) && memcmp(buf, header_prefix444, 5) &&
memcmp(buf, header_prefixhr1, 5) && memcmp(buf, header_prefixhr2, 5)) {
av_log(ctx->avctx, AV_LOG_ERROR,
"unknown header 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n",
buf[0], buf[1], buf[2], buf[3], buf[4]);
@ -186,6 +194,7 @@ static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
switch(buf[0x21] >> 5) {
case 1: ctx->bit_depth = 8; break;
case 2: ctx->bit_depth = 10; break;
case 3: ctx->bit_depth = 12; break;
default:
av_log(ctx->avctx, AV_LOG_ERROR,
"Unknown bitdepth indicator (%d)\n", buf[0x21] >> 5);
@ -198,9 +207,16 @@ static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
if (ctx->bit_depth == 8) {
avpriv_request_sample(ctx->avctx, "4:4:4 8 bits\n");
return AVERROR_INVALIDDATA;
}
} else if (ctx->bit_depth == 10) {
ctx->decode_dct_block = dnxhd_decode_dct_block_10_444;
ctx->pix_fmt = AV_PIX_FMT_YUV444P10;
} else {
ctx->decode_dct_block = dnxhd_decode_dct_block_12_444;
ctx->pix_fmt = AV_PIX_FMT_YUV444P12;
}
} else if (ctx->bit_depth == 12) {
ctx->decode_dct_block = dnxhd_decode_dct_block_12;
ctx->pix_fmt = AV_PIX_FMT_YUV422P12;
} else if (ctx->bit_depth == 10) {
ctx->decode_dct_block = dnxhd_decode_dct_block_10;
ctx->pix_fmt = AV_PIX_FMT_YUV422P10;
@ -254,20 +270,40 @@ static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
ctx->width, ctx->height, ctx->is_444 ? "4:4" : "2:2",
ctx->bit_depth, ctx->mbaff, ctx->act);
// Newer format supports variable mb_scan_index sizes
if (!memcmp(buf, header_prefixhr2, 5)) {
ctx->data_offset = 0x170 + (ctx->mb_height << 2);
} else {
if (ctx->mb_height > 68 ||
(ctx->mb_height << frame->interlaced_frame) > (ctx->height + 15) >> 4) {
av_log(ctx->avctx, AV_LOG_ERROR,
"mb height too big: %d\n", ctx->mb_height);
return AVERROR_INVALIDDATA;
}
ctx->data_offset = 0x280;
}
if (buf_size < ctx->data_offset) {
av_log(ctx->avctx, AV_LOG_ERROR,
"buffer too small (%d < %d).\n", buf_size, ctx->data_offset);
return AVERROR_INVALIDDATA;
}
if (ctx->mb_height != old_mb_height) {
av_freep(&ctx->mb_scan_index);
ctx->mb_scan_index = av_mallocz_array(ctx->mb_height, sizeof(uint32_t));
if (!ctx->mb_scan_index)
return AVERROR(ENOMEM);
}
for (i = 0; i < ctx->mb_height; i++) {
ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i << 2));
ff_dlog(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]);
if (buf_size < ctx->mb_scan_index[i] + 0x280LL) {
ff_dlog(ctx->avctx, "mb scan index %d, pos %d: %u\n", i, 0x170 + (i << 2), ctx->mb_scan_index[i]);
if (buf_size - ctx->data_offset < ctx->mb_scan_index[i]) {
av_log(ctx->avctx, AV_LOG_ERROR,
"invalid mb scan index (%d < %d).\n",
buf_size, ctx->mb_scan_index[i] + 0x280);
"invalid mb scan index (%u vs %u).\n",
ctx->mb_scan_index[i], buf_size - ctx->data_offset);
return AVERROR_INVALIDDATA;
}
}
@ -280,7 +316,8 @@ static av_always_inline int dnxhd_decode_dct_block(const DNXHDContext *ctx,
int n,
int index_bits,
int level_bias,
int level_shift)
int level_shift,
int dc_shift)
{
int i, j, index1, index2, len, flags;
int level, component, sign;
@ -323,7 +360,7 @@ static av_always_inline int dnxhd_decode_dct_block(const DNXHDContext *ctx,
LAST_SKIP_BITS(bs, &row->gb, len);
sign = ~level >> 31;
level = (NEG_USR32(sign ^ level, len) ^ sign) - sign;
row->last_dc[component] += level;
row->last_dc[component] += level << dc_shift;
}
block[0] = row->last_dc[component];
@ -379,25 +416,37 @@ static av_always_inline int dnxhd_decode_dct_block(const DNXHDContext *ctx,
static int dnxhd_decode_dct_block_8(const DNXHDContext *ctx,
RowContext *row, int n)
{
return dnxhd_decode_dct_block(ctx, row, n, 4, 32, 6);
return dnxhd_decode_dct_block(ctx, row, n, 4, 32, 6, 0);
}
static int dnxhd_decode_dct_block_10(const DNXHDContext *ctx,
RowContext *row, int n)
{
return dnxhd_decode_dct_block(ctx, row, n, 6, 8, 4);
return dnxhd_decode_dct_block(ctx, row, n, 6, 8, 4, 0);
}
static int dnxhd_decode_dct_block_10_444(const DNXHDContext *ctx,
RowContext *row, int n)
{
return dnxhd_decode_dct_block(ctx, row, n, 6, 32, 6);
return dnxhd_decode_dct_block(ctx, row, n, 6, 32, 6, 0);
}
static int dnxhd_decode_dct_block_12(const DNXHDContext *ctx,
RowContext *row, int n)
{
return dnxhd_decode_dct_block(ctx, row, n, 6, 8, 4, 2);
}
static int dnxhd_decode_dct_block_12_444(const DNXHDContext *ctx,
RowContext *row, int n)
{
return dnxhd_decode_dct_block(ctx, row, n, 6, 32, 4, 2);
}
static int dnxhd_decode_macroblock(const DNXHDContext *ctx, RowContext *row,
AVFrame *frame, int x, int y)
{
int shift1 = ctx->bit_depth == 10;
int shift1 = ctx->bit_depth >= 10;
int dct_linesize_luma = frame->linesize[0];
int dct_linesize_chroma = frame->linesize[1];
uint8_t *dest_y, *dest_u, *dest_v;
@ -556,8 +605,8 @@ decode_coding_unit:
picture->key_frame = 1;
}
ctx->buf_size = buf_size - 0x280;
ctx->buf = buf + 0x280;
ctx->buf_size = buf_size - ctx->data_offset;
ctx->buf = buf + ctx->data_offset;
avctx->execute2(avctx, dnxhd_decode_row, picture, NULL, ctx->mb_height);
if (first_field && picture->interlaced_frame) {
@ -590,6 +639,7 @@ static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
ff_free_vlc(&ctx->dc_vlc);
ff_free_vlc(&ctx->run_vlc);
av_freep(&ctx->mb_scan_index);
av_freep(&ctx->rows);
return 0;