CFHDContext *s = avctx->priv_data;
int i;
- for (i = 0; i < 3; i++) {
+ for (i = 0; i < 4; i++) {
av_freep(&s->plane[i].idwt_buf);
av_freep(&s->plane[i].idwt_tmp);
}
static int alloc_buffers(AVCodecContext *avctx)
{
CFHDContext *s = avctx->priv_data;
- int i, j, k, ret;
+ int i, j, k, ret, planes;
if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
return ret;
+ avctx->pix_fmt = s->coded_format;
avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
+ planes = av_pix_fmt_count_planes(avctx->pix_fmt);
- for (i = 0; i < 3; i++) {
+ for (i = 0; i < planes; i++) {
int width = i ? avctx->width >> s->chroma_x_shift : avctx->width;
int height = i ? avctx->height >> s->chroma_y_shift : avctx->height;
int stride = FFALIGN(width / 8, 8) * 8;
s->a_height = s->coded_height;
s->a_width = s->coded_width;
+ s->a_format = s->coded_format;
return 0;
}
GetByteContext gb;
ThreadFrame frame = { .f = data };
AVFrame *pic = data;
- int ret = 0, i, j, plane, got_buffer = 0;
+ int ret = 0, i, j, planes, plane, got_buffer = 0;
int16_t *coeff_data;
- avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
+ s->coded_format = AV_PIX_FMT_YUV422P10;
init_frame_defaults(s);
+ planes = av_pix_fmt_count_planes(s->coded_format);
bytestream2_init(&gb, avpkt->data, avpkt->size);
} else if (tag == 12) {
av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
s->channel_cnt = data;
- if (data != 3) {
+ if (data > 4) {
av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
ret = AVERROR_PATCHWELCOME;
break;
} else if (tag == 62) {
s->channel_num = data;
av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
- if (s->channel_num > 2) {
+ if (s->channel_num >= planes) {
av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
ret = AVERROR(EINVAL);
break;
s->plane[s->channel_num].band[0][0].width = data;
s->plane[s->channel_num].band[0][0].stride = data;
av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
- if (data < 2 || (data & 1) || data > s->plane[s->channel_num].band[0][0].a_width) {
+ if (data < 2 || data > s->plane[s->channel_num].band[0][0].a_width) {
av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
ret = AVERROR(EINVAL);
break;
av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
else if (tag == 10) {
if (data != 0) {
- avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16" is unsupported\n", data);
+ avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
ret = AVERROR_PATCHWELCOME;
break;
}
bytestream2_skipu(&gb, data * 4);
} else if (tag == 23) {
av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
- avpriv_report_missing_feature(avctx, "Skip frame not supported\n");
+ avpriv_report_missing_feature(avctx, "Skip frame");
ret = AVERROR_PATCHWELCOME;
break;
} else if (tag == 2) {
av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
+ if (data > bytestream2_get_bytes_left(&gb) / 4) {
+ av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
+ ret = AVERROR_INVALIDDATA;
+ break;
+ }
for (i = 0; i < data; i++) {
uint16_t tag2 = bytestream2_get_be16(&gb);
uint16_t val2 = bytestream2_get_be16(&gb);
s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
av_log(avctx, AV_LOG_DEBUG, "Highpass width %i channel %i level %i subband %i\n", data, s->channel_num, s->level, s->subband_num);
- if (data < 2 || (data & 1)) {
+ if (data < 2) {
av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
ret = AVERROR(EINVAL);
break;
s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
- if (data < 2 || (data & 1)) {
+ if (data < 2) {
av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
ret = AVERROR(EINVAL);
break;
} else if (tag == 84) {
av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
if (data == 1)
- avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
+ s->coded_format = AV_PIX_FMT_YUV422P10;
else if (data == 3)
- avctx->pix_fmt = AV_PIX_FMT_GBRP12;
+ s->coded_format = AV_PIX_FMT_GBRP12;
+ else if (data == 4)
+ s->coded_format = AV_PIX_FMT_GBRAP12;
else {
- avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16" is unsupported\n", data);
+ avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
ret = AVERROR_PATCHWELCOME;
break;
}
+ planes = av_pix_fmt_count_planes(s->coded_format);
} else
av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
/* Some kind of end of header tag */
- if (tag == 4 && data == 0x1a4a && s->coded_width && s->coded_height && avctx->pix_fmt != AV_PIX_FMT_NONE) {
- if (s->a_width != s->coded_width || s->a_height != s->coded_height) {
+ if (tag == 4 && data == 0x1a4a && s->coded_width && s->coded_height &&
+ s->coded_format != AV_PIX_FMT_NONE) {
+ if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
+ s->a_format != s->coded_format) {
free_buffers(avctx);
if ((ret = alloc_buffers(avctx)) < 0) {
free_buffers(avctx);
s->coded_width = 0;
s->coded_height = 0;
+ s->coded_format = AV_PIX_FMT_NONE;
got_buffer = 1;
}
coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
coeff_data += lowpass_width;
}
+ /* Align to mod-4 position to continue reading tags */
+ bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
+
/* Copy last line of coefficients if odd height */
if (lowpass_height & 1) {
memcpy(&coeff_data[lowpass_height * lowpass_width],
}
}
- if (!s->a_width || !s->a_height || s->coded_width || s->coded_height) {
+ if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
+ s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
ret = AVERROR(EINVAL);
goto end;
goto end;
}
- for (plane = 0; plane < 3 && !ret; plane++) {
+ planes = av_pix_fmt_count_planes(avctx->pix_fmt);
+ for (plane = 0; plane < planes && !ret; plane++) {
/* level 1 */
int lowpass_height = s->plane[plane].band[0][0].height;
int lowpass_width = s->plane[plane].band[0][0].width;
int highpass_stride = s->plane[plane].band[0][1].stride;
- int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : 0;
+ int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
int16_t *low, *high, *output, *dst;
if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
high += lowpass_width;
output += lowpass_width * 2;
}
- if (avctx->pix_fmt == AV_PIX_FMT_GBRP12) {
+ if (s->bpc == 12) {
output = s->plane[plane].subband[0];
for (i = 0; i < lowpass_height * 2; i++) {
for (j = 0; j < lowpass_width * 2; j++)