summaryrefslogtreecommitdiff
path: root/libs/ffvpx/libavcodec/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'libs/ffvpx/libavcodec/utils.c')
-rw-r--r--libs/ffvpx/libavcodec/utils.c150
1 files changed, 123 insertions, 27 deletions
diff --git a/libs/ffvpx/libavcodec/utils.c b/libs/ffvpx/libavcodec/utils.c
index 59d41ccbb..bdd9a60c3 100644
--- a/libs/ffvpx/libavcodec/utils.c
+++ b/libs/ffvpx/libavcodec/utils.c
@@ -214,6 +214,8 @@ void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
case AV_PIX_FMT_YUVA422P9BE:
case AV_PIX_FMT_YUVA422P10LE:
case AV_PIX_FMT_YUVA422P10BE:
+ case AV_PIX_FMT_YUVA422P12LE:
+ case AV_PIX_FMT_YUVA422P12BE:
case AV_PIX_FMT_YUVA422P16LE:
case AV_PIX_FMT_YUVA422P16BE:
case AV_PIX_FMT_YUV440P10LE:
@@ -234,6 +236,8 @@ void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
case AV_PIX_FMT_YUVA444P9BE:
case AV_PIX_FMT_YUVA444P10LE:
case AV_PIX_FMT_YUVA444P10BE:
+ case AV_PIX_FMT_YUVA444P12LE:
+ case AV_PIX_FMT_YUVA444P12BE:
case AV_PIX_FMT_YUVA444P16LE:
case AV_PIX_FMT_YUVA444P16BE:
case AV_PIX_FMT_GBRP9LE:
@@ -288,6 +292,16 @@ void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
w_align = 8;
h_align = 8;
}
+ if (s->codec_id == AV_CODEC_ID_MJPEG ||
+ s->codec_id == AV_CODEC_ID_MJPEGB ||
+ s->codec_id == AV_CODEC_ID_LJPEG ||
+ s->codec_id == AV_CODEC_ID_SMVJPEG ||
+ s->codec_id == AV_CODEC_ID_AMV ||
+ s->codec_id == AV_CODEC_ID_SP5X ||
+ s->codec_id == AV_CODEC_ID_JPEGLS) {
+ w_align = 8;
+ h_align = 2*8;
+ }
break;
case AV_PIX_FMT_BGR24:
if ((s->codec_id == AV_CODEC_ID_MSZH) ||
@@ -408,7 +422,7 @@ int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
void ff_color_frame(AVFrame *frame, const int c[4])
{
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
- int p, y, x;
+ int p, y;
av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
@@ -417,13 +431,19 @@ void ff_color_frame(AVFrame *frame, const int c[4])
int is_chroma = p == 1 || p == 2;
int bytes = is_chroma ? AV_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
- for (y = 0; y < height; y++) {
- if (desc->comp[0].depth >= 9) {
- for (x = 0; x<bytes; x++)
- ((uint16_t*)dst)[x] = c[p];
- }else
- memset(dst, c[p], bytes);
+ if (desc->comp[0].depth >= 9) {
+ ((uint16_t*)dst)[0] = c[p];
+ av_memcpy_backptr(dst + 2, 2, bytes - 2);
dst += frame->linesize[p];
+ for (y = 1; y < height; y++) {
+ memcpy(dst, frame->data[p], 2*bytes);
+ dst += frame->linesize[p];
+ }
+ } else {
+ for (y = 0; y < height; y++) {
+ memset(dst, c[p], bytes);
+ dst += frame->linesize[p];
+ }
}
}
}
@@ -501,7 +521,14 @@ static int64_t get_bit_rate(AVCodecContext *ctx)
break;
case AVMEDIA_TYPE_AUDIO:
bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
- bit_rate = bits_per_sample ? ctx->sample_rate * (int64_t)ctx->channels * bits_per_sample : ctx->bit_rate;
+ if (bits_per_sample) {
+ bit_rate = ctx->sample_rate * (int64_t)ctx->channels;
+ if (bit_rate > INT64_MAX / bits_per_sample) {
+ bit_rate = 0;
+ } else
+ bit_rate *= bits_per_sample;
+ } else
+ bit_rate = ctx->bit_rate;
break;
default:
bit_rate = 0;
@@ -538,6 +565,7 @@ int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AV
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
{
int ret = 0;
+ int codec_init_ok = 0;
AVDictionary *tmp = NULL;
const AVPixFmtDescriptor *pixdesc;
@@ -673,7 +701,18 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
if (av_codec_is_decoder(codec))
av_freep(&avctx->subtitle_header);
- if (avctx->channels > FF_SANE_NB_CHANNELS) {
+ if (avctx->channels > FF_SANE_NB_CHANNELS || avctx->channels < 0) {
+ av_log(avctx, AV_LOG_ERROR, "Too many or invalid channels: %d\n", avctx->channels);
+ ret = AVERROR(EINVAL);
+ goto free_and_end;
+ }
+ if (avctx->sample_rate < 0) {
+ av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
+ ret = AVERROR(EINVAL);
+ goto free_and_end;
+ }
+ if (avctx->block_align < 0) {
+ av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
ret = AVERROR(EINVAL);
goto free_and_end;
}
@@ -726,6 +765,12 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
goto free_and_end;
}
+ if (av_codec_is_decoder(avctx->codec)) {
+ ret = ff_decode_bsfs_init(avctx);
+ if (ret < 0)
+ goto free_and_end;
+ }
+
if (HAVE_THREADS
&& !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
ret = ff_thread_init(avctx);
@@ -924,6 +969,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
if (ret < 0) {
goto free_and_end;
}
+ codec_init_ok = 1;
}
ret=0;
@@ -951,6 +997,10 @@ FF_ENABLE_DEPRECATION_WARNINGS
ret = AVERROR(EINVAL);
goto free_and_end;
}
+ if (avctx->bits_per_coded_sample < 0) {
+ ret = AVERROR(EINVAL);
+ goto free_and_end;
+ }
if (avctx->sub_charenc) {
if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
@@ -1007,10 +1057,14 @@ end:
return ret;
free_and_end:
- if (avctx->codec &&
- (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
+ if (avctx->codec && avctx->codec->close &&
+ (codec_init_ok ||
+ (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP)))
avctx->codec->close(avctx);
+ if (HAVE_THREADS && avctx->internal->thread_ctx)
+ ff_thread_free(avctx);
+
if (codec->priv_class && codec->priv_data_size)
av_opt_free(avctx->priv_data);
av_opt_free(avctx);
@@ -1023,6 +1077,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
av_dict_free(&tmp);
av_freep(&avctx->priv_data);
+ av_freep(&avctx->subtitle_header);
if (avctx->internal) {
av_frame_free(&avctx->internal->to_free);
av_frame_free(&avctx->internal->compat_decode_frame);
@@ -1031,6 +1086,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
av_packet_free(&avctx->internal->last_pkt_props);
av_packet_free(&avctx->internal->ds.in_pkt);
+ ff_decode_bsfs_uninit(avctx);
av_freep(&avctx->internal->pool);
}
@@ -1389,10 +1445,8 @@ const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
unsigned avcodec_version(void)
{
-// av_assert0(AV_CODEC_ID_V410==164);
av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
-// av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
av_assert0(AV_CODEC_ID_SRT==94216);
av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
@@ -1430,6 +1484,7 @@ int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
case AV_CODEC_ID_DSD_MSBF_PLANAR:
case AV_CODEC_ID_PCM_ALAW:
case AV_CODEC_ID_PCM_MULAW:
+ case AV_CODEC_ID_PCM_VIDC:
case AV_CODEC_ID_PCM_S8:
case AV_CODEC_ID_PCM_S8_PLANAR:
case AV_CODEC_ID_PCM_U8:
@@ -1536,7 +1591,11 @@ static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
case AV_CODEC_ID_GSM_MS: return 320;
case AV_CODEC_ID_MP1: return 384;
case AV_CODEC_ID_ATRAC1: return 512;
- case AV_CODEC_ID_ATRAC3: return 1024 * framecount;
+ case AV_CODEC_ID_ATRAC9:
+ case AV_CODEC_ID_ATRAC3:
+ if (framecount > INT_MAX/1024)
+ return 0;
+ return 1024 * framecount;
case AV_CODEC_ID_ATRAC3P: return 2048;
case AV_CODEC_ID_MP2:
case AV_CODEC_ID_MUSEPACK7: return 1152;
@@ -1552,8 +1611,11 @@ static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
if (ch > 0) {
/* calc from sample rate and channels */
- if (id == AV_CODEC_ID_BINKAUDIO_DCT)
+ if (id == AV_CODEC_ID_BINKAUDIO_DCT) {
+ if (sr / 22050 > 22)
+ return 0;
return (480 << (sr / 22050)) / ch;
+ }
}
if (id == AV_CODEC_ID_MP3)
@@ -1585,8 +1647,6 @@ static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
return 256 * (frame_bytes / 64);
if (id == AV_CODEC_ID_RA_144)
return 160 * (frame_bytes / 20);
- if (id == AV_CODEC_ID_G723_1)
- return 240 * (frame_bytes / 24);
if (bps > 0) {
/* calc from frame_bytes and bits_per_coded_sample */
@@ -1601,7 +1661,10 @@ static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
return frame_bytes / (9 * ch) * 16;
case AV_CODEC_ID_ADPCM_PSX:
case AV_CODEC_ID_ADPCM_DTK:
- return frame_bytes / (16 * ch) * 28;
+ frame_bytes /= 16 * ch;
+ if (frame_bytes > INT_MAX / 28)
+ return 0;
+ return frame_bytes * 28;
case AV_CODEC_ID_ADPCM_4XM:
case AV_CODEC_ID_ADPCM_IMA_DAT4:
case AV_CODEC_ID_ADPCM_IMA_ISS:
@@ -1613,7 +1676,7 @@ static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
case AV_CODEC_ID_ADPCM_THP:
case AV_CODEC_ID_ADPCM_THP_LE:
if (extradata)
- return frame_bytes * 14 / (8 * ch);
+ return frame_bytes * 14LL / (8 * ch);
break;
case AV_CODEC_ID_ADPCM_XA:
return (frame_bytes / 128) * 224 / ch;
@@ -1647,21 +1710,33 @@ static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
if (ba > 0) {
/* calc from frame_bytes, channels, and block_align */
int blocks = frame_bytes / ba;
+ int64_t tmp = 0;
switch (id) {
case AV_CODEC_ID_ADPCM_IMA_WAV:
if (bps < 2 || bps > 5)
return 0;
- return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
+ tmp = blocks * (1LL + (ba - 4 * ch) / (bps * ch) * 8);
+ break;
case AV_CODEC_ID_ADPCM_IMA_DK3:
- return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
+ tmp = blocks * (((ba - 16LL) * 2 / 3 * 4) / ch);
+ break;
case AV_CODEC_ID_ADPCM_IMA_DK4:
- return blocks * (1 + (ba - 4 * ch) * 2 / ch);
+ tmp = blocks * (1 + (ba - 4LL * ch) * 2 / ch);
+ break;
case AV_CODEC_ID_ADPCM_IMA_RAD:
- return blocks * ((ba - 4 * ch) * 2 / ch);
+ tmp = blocks * ((ba - 4LL * ch) * 2 / ch);
+ break;
case AV_CODEC_ID_ADPCM_MS:
- return blocks * (2 + (ba - 7 * ch) * 2 / ch);
+ tmp = blocks * (2 + (ba - 7LL * ch) * 2LL / ch);
+ break;
case AV_CODEC_ID_ADPCM_MTAF:
- return blocks * (ba - 16) * 2 / ch;
+ tmp = blocks * (ba - 16LL) * 2 / ch;
+ break;
+ }
+ if (tmp) {
+ if (tmp != (int)tmp)
+ return 0;
+ return tmp;
}
}
@@ -1699,20 +1774,22 @@ static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
{
- return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
+ int duration = get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
avctx->channels, avctx->block_align,
avctx->codec_tag, avctx->bits_per_coded_sample,
avctx->bit_rate, avctx->extradata, avctx->frame_size,
frame_bytes);
+ return FFMAX(0, duration);
}
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
{
- return get_audio_frame_duration(par->codec_id, par->sample_rate,
+ int duration = get_audio_frame_duration(par->codec_id, par->sample_rate,
par->channels, par->block_align,
par->codec_tag, par->bits_per_coded_sample,
par->bit_rate, par->extradata, par->frame_size,
frame_bytes);
+ return FFMAX(0, duration);
}
#if !HAVE_THREADS
@@ -2195,3 +2272,22 @@ int64_t ff_guess_coded_bitrate(AVCodecContext *avctx)
return bitrate;
}
+
+int ff_int_from_list_or_default(void *ctx, const char * val_name, int val,
+ const int * array_valid_values, int default_value)
+{
+ int i = 0, ref_val;
+
+ while (1) {
+ ref_val = array_valid_values[i];
+ if (ref_val == INT_MAX)
+ break;
+ if (val == ref_val)
+ return val;
+ i++;
+ }
+ /* val is not a valid value */
+ av_log(ctx, AV_LOG_DEBUG,
+ "%s %d are not supported. Set to default value : %d\n", val_name, val, default_value);
+ return default_value;
+}