From ec1ce86e7f7cf88dac5d4c00d7d5851e1202d6e2 Mon Sep 17 00:00:00 2001 From: Janne Grunau Date: Sun, 23 Oct 2011 00:33:44 +0200 Subject: [PATCH 01/22] resample: remove unused #define --- libavcodec/resample.c | 1 - 1 file changed, 1 deletion(-) diff --git a/libavcodec/resample.c b/libavcodec/resample.c index 04bbbf07e4..fce6272e78 100644 --- a/libavcodec/resample.c +++ b/libavcodec/resample.c @@ -213,7 +213,6 @@ ReSampleContext *av_audio_resample_init(int output_channels, int input_channels, } } -#define TAPS 16 s->resample_context = av_resample_init(output_rate, input_rate, filter_length, log2_phase_count, linear, cutoff); From b2e56e08c90a89c5566a1a391fec7c3136d49e3c Mon Sep 17 00:00:00 2001 From: Janne Grunau Date: Sun, 23 Oct 2011 00:31:16 +0200 Subject: [PATCH 02/22] resample: reject unhandled conversions audio_resample can not reduce the number of channels --- libavcodec/resample.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libavcodec/resample.c b/libavcodec/resample.c index fce6272e78..ba8ce8910c 100644 --- a/libavcodec/resample.c +++ b/libavcodec/resample.c @@ -162,9 +162,10 @@ ReSampleContext *av_audio_resample_init(int output_channels, int input_channels, MAX_CHANNELS); return NULL; } - if (output_channels > 2 && - !(output_channels == 6 && input_channels == 2) && - output_channels != input_channels) { + if (output_channels != input_channels && + (input_channels > 2 || + output_channels > 2 && + !(output_channels == 6 && input_channels == 2))) { av_log(NULL, AV_LOG_ERROR, "Resampling output channel count must be 1 or 2 for mono input; 1, 2 or 6 for stereo input; or N for N channel input.\n"); return NULL; From 80e36425fb25a53f89b735d00ff3e7c293a9472c Mon Sep 17 00:00:00 2001 From: Janne Grunau Date: Sun, 23 Oct 2011 01:11:44 +0200 Subject: [PATCH 03/22] aacdec: initialize sbr context only in new channel elements --- libavcodec/aacdec.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libavcodec/aacdec.c b/libavcodec/aacdec.c index e4e18d63a3..d1774a1f98 100644 --- a/libavcodec/aacdec.c +++ b/libavcodec/aacdec.c @@ -184,9 +184,11 @@ static av_cold int che_configure(AACContext *ac, int type, int id, int *channels) { if (che_pos[type][id]) { - if (!ac->che[type][id] && !(ac->che[type][id] = av_mallocz(sizeof(ChannelElement)))) - return AVERROR(ENOMEM); - ff_aac_sbr_ctx_init(ac, &ac->che[type][id]->sbr); + if (!ac->che[type][id]) { + if (!(ac->che[type][id] = av_mallocz(sizeof(ChannelElement)))) + return AVERROR(ENOMEM); + ff_aac_sbr_ctx_init(ac, &ac->che[type][id]->sbr); + } if (type != TYPE_CCE) { ac->output_data[(*channels)++] = ac->che[type][id]->ch[0].ret; if (type == TYPE_CPE || From 785f876cee7b55fe54007da00759a8f1cf81db36 Mon Sep 17 00:00:00 2001 From: Janne Grunau Date: Fri, 21 Oct 2011 17:13:03 +0200 Subject: [PATCH 04/22] latm: avoid unnecessary reinit of the aac decoder --- libavcodec/aacdec.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libavcodec/aacdec.c b/libavcodec/aacdec.c index d1774a1f98..6000fe190b 100644 --- a/libavcodec/aacdec.c +++ b/libavcodec/aacdec.c @@ -2509,8 +2509,9 @@ static int latm_decode_frame(AVCodecContext *avctx, void *out, int *out_size, *out_size = 0; return avpkt->size; } else { - aac_decode_close(avctx); - if ((err = aac_decode_init(avctx)) < 0) + if ((err = decode_audio_specific_config( + &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.m4ac, + avctx->extradata, avctx->extradata_size)) < 0) return err; latmctx->initialized = 1; } From 28287045ca3ee34e4ea980628ec8314fd2d819ae Mon Sep 17 00:00:00 2001 From: Janne Grunau Date: Fri, 21 Oct 2011 17:14:58 +0200 Subject: [PATCH 05/22] cosmetics: simplify latm_decode_init --- libavcodec/aacdec.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/libavcodec/aacdec.c b/libavcodec/aacdec.c index 6000fe190b..37ccd18ecb 100644 --- a/libavcodec/aacdec.c +++ b/libavcodec/aacdec.c @@ -2533,15 +2533,10 @@ static int latm_decode_frame(AVCodecContext *avctx, void *out, int *out_size, av_cold static int latm_decode_init(AVCodecContext *avctx) { struct LATMContext *latmctx = avctx->priv_data; - int ret; + int ret = aac_decode_init(avctx); - ret = aac_decode_init(avctx); - - if (avctx->extradata_size > 0) { + if (avctx->extradata_size > 0) latmctx->initialized = !ret; - } else { - latmctx->initialized = 0; - } return ret; } From f172132f825cec4e446a5bdde4ffa9065ba9061f Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sun, 9 Oct 2011 15:04:38 +0200 Subject: [PATCH 06/22] mpegenc: add preload private option. Deprecate AVFormatContext.preload. --- avconv.c | 7 +++++-- libavformat/avformat.h | 4 +++- libavformat/mpegenc.c | 10 +++++++++- libavformat/version.h | 3 +++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/avconv.c b/avconv.c index 8fc6fd1425..742695bc3f 100644 --- a/avconv.c +++ b/avconv.c @@ -375,7 +375,6 @@ static void reset_options(OptionsContext *o) memset(o, 0, sizeof(*o)); - o->mux_preload = 0.5; o->mux_max_delay = 0.7; o->recording_time = INT64_MAX; o->limit_filesize = UINT64_MAX; @@ -3572,7 +3571,11 @@ static void opt_output_file(void *optctx, const char *filename) } } - oc->preload = (int)(o->mux_preload * AV_TIME_BASE); + if (o->mux_preload) { + uint8_t buf[64]; + snprintf(buf, sizeof(buf), "%d", (int)(o->mux_preload*AV_TIME_BASE)); + av_dict_set(&output_files[nb_output_files - 1].opts, "preload", buf, 0); + } oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE); oc->flags |= AVFMT_FLAG_NONBLOCK; diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 65f66d84d3..74c7140e35 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -743,7 +743,9 @@ typedef struct AVFormatContext { attribute_deprecated int mux_rate; #endif unsigned int packet_size; - int preload; +#if FF_API_PRELOAD + attribute_deprecated int preload; +#endif int max_delay; #if FF_API_LOOP_OUTPUT diff --git a/libavformat/mpegenc.c b/libavformat/mpegenc.c index 5e8954c902..f824d6cb05 100644 --- a/libavformat/mpegenc.c +++ b/libavformat/mpegenc.c @@ -77,6 +77,7 @@ typedef struct { double vcd_padding_bitrate; //FIXME floats int64_t vcd_padding_bytes_written; + int preload; } MpegMuxContext; extern AVOutputFormat ff_mpeg1vcd_muxer; @@ -1158,9 +1159,15 @@ static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt) StreamInfo *stream = st->priv_data; int64_t pts, dts; PacketDesc *pkt_desc; - const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE); + int preload; const int is_iframe = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY); +#if FF_API_PRELOAD + if (ctx->preload) + s->preload = ctx->preload; +#endif + preload = av_rescale(s->preload, 90000, AV_TIME_BASE); + pts= pkt->pts; dts= pkt->dts; @@ -1237,6 +1244,7 @@ static int mpeg_mux_end(AVFormatContext *ctx) #define E AV_OPT_FLAG_ENCODING_PARAM static const AVOption options[] = { { "muxrate", NULL, OFFSET(mux_rate), AV_OPT_TYPE_INT, {0}, 0, INT_MAX, E }, + { "preload", "Initial demux-decode delay in microseconds.", OFFSET(preload), AV_OPT_TYPE_INT, {500000}, 0, INT_MAX, E}, { NULL }, }; diff --git a/libavformat/version.h b/libavformat/version.h index 9de30ea55b..3a6a4f611e 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -98,5 +98,8 @@ #ifndef FF_API_NEW_STREAM #define FF_API_NEW_STREAM (LIBAVFORMAT_VERSION_MAJOR < 54) #endif +#ifndef FF_API_PRELOAD +#define FF_API_PRELOAD (LIBAVFORMAT_VERSION_MAJOR < 54) +#endif #endif /* AVFORMAT_VERSION_H */ From a75034300fedba861454241abef0db6cfb2599cf Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 18 Oct 2011 09:17:12 +0200 Subject: [PATCH 07/22] lavf: simplify by using FFMAX/FFMIN. --- libavformat/utils.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/libavformat/utils.c b/libavformat/utils.c index d0a7fb90be..640fa70c3d 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -1848,27 +1848,22 @@ static void update_stream_timings(AVFormatContext *ic) st = ic->streams[i]; if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) { start_time1= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q); - if (start_time1 < start_time) - start_time = start_time1; + start_time = FFMIN(start_time, start_time1); if (st->duration != AV_NOPTS_VALUE) { end_time1 = start_time1 + av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q); - if (end_time1 > end_time) - end_time = end_time1; + end_time = FFMAX(end_time, end_time1); } } if (st->duration != AV_NOPTS_VALUE) { duration1 = av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q); - if (duration1 > duration) - duration = duration1; + duration = FFMAX(duration, duration1); } } if (start_time != INT64_MAX) { ic->start_time = start_time; - if (end_time != INT64_MIN) { - if (end_time - start_time > duration) - duration = end_time - start_time; - } + if (end_time != INT64_MIN) + duration = FFMAX(duration, end_time - start_time); } if (duration != INT64_MIN) { ic->duration = duration; @@ -2022,8 +2017,7 @@ static void estimate_timings(AVFormatContext *ic, int64_t old_offset) file_size = 0; } else { file_size = avio_size(ic->pb); - if (file_size < 0) - file_size = 0; + file_size = FFMAX(0, file_size); } if ((!strcmp(ic->iformat->name, "mpeg") || From 1b648c7cdbee335c642bd2c05fe624fc195b85e6 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sun, 23 Oct 2011 11:10:27 +0200 Subject: [PATCH 08/22] avconc: split choose_codec() to choose_decoder/choose_encoder. Prevents -c copy from working for input streams and allows to move stream_copy variable from AVStream to OutputStream. --- avconv.c | 54 +++++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/avconv.c b/avconv.c index 742695bc3f..ea33659bfc 100644 --- a/avconv.c +++ b/avconv.c @@ -2724,13 +2724,11 @@ static int opt_map_metadata(OptionsContext *o, const char *opt, const char *arg) return 0; } -static enum CodecID find_codec_or_die(const char *name, enum AVMediaType type, int encoder) +static AVCodec *find_codec_or_die(const char *name, enum AVMediaType type, int encoder) { const char *codec_string = encoder ? "encoder" : "decoder"; AVCodec *codec; - if(!name) - return CODEC_ID_NONE; codec = encoder ? avcodec_find_encoder_by_name(name) : avcodec_find_decoder_by_name(name); @@ -2742,29 +2740,20 @@ static enum CodecID find_codec_or_die(const char *name, enum AVMediaType type, i av_log(NULL, AV_LOG_FATAL, "Invalid %s type '%s'\n", codec_string, name); exit_program(1); } - return codec->id; + return codec; } -static AVCodec *choose_codec(OptionsContext *o, AVFormatContext *s, AVStream *st, enum AVMediaType type) +static AVCodec *choose_decoder(OptionsContext *o, AVFormatContext *s, AVStream *st) { char *codec_name = NULL; MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st); - - if (!codec_name) { - if (s->oformat) { - st->codec->codec_id = av_guess_codec(s->oformat, NULL, s->filename, NULL, type); - return avcodec_find_encoder(st->codec->codec_id); - } - } else if (!strcmp(codec_name, "copy")) - st->stream_copy = 1; - else { - st->codec->codec_id = find_codec_or_die(codec_name, type, s->iformat == NULL); - return s->oformat ? avcodec_find_encoder_by_name(codec_name) : - avcodec_find_decoder_by_name(codec_name); - } - - return NULL; + if (codec_name) { + AVCodec *codec = find_codec_or_die(codec_name, st->codec->codec_type, 0); + st->codec->codec_id = codec->id; + return codec; + } else + return avcodec_find_decoder(st->codec->codec_id); } /** @@ -2791,9 +2780,7 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic) MATCH_PER_STREAM_OPT(ts_scale, dbl, scale, ic, st); ist->ts_scale = scale; - ist->dec = choose_codec(o, ic, st, dec->codec_type); - if (!ist->dec) - ist->dec = avcodec_find_decoder(dec->codec_id); + ist->dec = choose_decoder(o, ic, st); switch (dec->codec_type) { case AVMEDIA_TYPE_AUDIO: @@ -2894,7 +2881,7 @@ static int opt_input_file(OptionsContext *o, const char *opt, const char *filena /* apply forced codec ids */ for (i = 0; i < ic->nb_streams; i++) - choose_codec(o, ic, ic->streams[i], ic->streams[i]->codec->codec_type); + choose_decoder(o, ic, ic->streams[i]); /* Set AVCodecContext options for avformat_find_stream_info */ opts = setup_find_stream_info_opts(ic, codec_opts); @@ -3012,6 +2999,23 @@ static int get_preset_file_2(const char *preset_name, const char *codec_name, AV return ret; } +static void choose_encoder(OptionsContext *o, AVFormatContext *s, OutputStream *ost) +{ + char *codec_name = NULL; + + MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, ost->st); + if (!codec_name) { + ost->st->codec->codec_id = av_guess_codec(s->oformat, NULL, s->filename, + NULL, ost->st->codec->codec_type); + ost->enc = avcodec_find_encoder(ost->st->codec->codec_id); + } else if (!strcmp(codec_name, "copy")) + ost->st->stream_copy = 1; + else { + ost->enc = find_codec_or_die(codec_name, ost->st->codec->codec_type, 1); + ost->st->codec->codec_id = ost->enc->id; + } +} + static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, enum AVMediaType type) { OutputStream *ost; @@ -3039,7 +3043,7 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e ost->index = idx; ost->st = st; st->codec->codec_type = type; - ost->enc = choose_codec(o, oc, st, type); + choose_encoder(o, oc, ost); if (ost->enc) { ost->opts = filter_codec_opts(codec_opts, ost->enc->id, oc, st); } From 3d813e4c548c99368876f985a324eac689385311 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sun, 23 Oct 2011 11:22:33 +0200 Subject: [PATCH 09/22] lavf: deprecate AVStream.stream_copy It's only used in avconv, so it properly belongs to OutputStream struct there. --- avconv.c | 25 ++++++++++++------------- libavformat/avformat.h | 4 +++- libavformat/version.h | 3 +++ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/avconv.c b/avconv.c index ea33659bfc..96f6059980 100644 --- a/avconv.c +++ b/avconv.c @@ -227,6 +227,7 @@ typedef struct OutputStream { int64_t sws_flags; AVDictionary *opts; int is_past_recording_time; + int stream_copy; } OutputStream; @@ -1346,7 +1347,7 @@ static void print_report(OutputFile *output_files, float q = -1; ost = &ost_table[i]; enc = ost->st->codec; - if (!ost->st->stream_copy && enc->coded_frame) + if (!ost->stream_copy && enc->coded_frame) q = enc->coded_frame->quality/(float)FF_QP2LAMBDA; if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) { snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ", q); @@ -1987,7 +1988,7 @@ static int transcode_init(OutputFile *output_files, codec->bits_per_raw_sample= icodec->bits_per_raw_sample; codec->chroma_sample_location = icodec->chroma_sample_location; - if (ost->st->stream_copy) { + if (ost->stream_copy) { uint64_t extra_size = (uint64_t)icodec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE; if (extra_size > INT_MAX) { @@ -2297,7 +2298,7 @@ static int transcode_init(OutputFile *output_files, av_log(NULL, AV_LOG_INFO, " [sync #%d.%d]", ost->sync_ist->file_index, ost->sync_ist->st->index); - if (ost->st->stream_copy) + if (ost->stream_copy) av_log(NULL, AV_LOG_INFO, " (copy)"); else av_log(NULL, AV_LOG_INFO, " (%s -> %s)", input_streams[ost->source_index].dec ? @@ -2525,7 +2526,7 @@ static int transcode(OutputFile *output_files, for (i = 0; i < nb_output_streams; i++) { ost = &output_streams[i]; if (ost) { - if (ost->st->stream_copy) + if (ost->stream_copy) av_freep(&ost->st->codec->extradata); if (ost->logfile) { fclose(ost->logfile); @@ -3009,7 +3010,7 @@ static void choose_encoder(OptionsContext *o, AVFormatContext *s, OutputStream * NULL, ost->st->codec->codec_type); ost->enc = avcodec_find_encoder(ost->st->codec->codec_id); } else if (!strcmp(codec_name, "copy")) - ost->st->stream_copy = 1; + ost->stream_copy = 1; else { ost->enc = find_codec_or_die(codec_name, ost->st->codec->codec_type, 1); ost->st->codec->codec_id = ost->enc->id; @@ -3144,7 +3145,7 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc) st = ost->st; video_enc = st->codec; - if (!st->stream_copy) { + if (!ost->stream_copy) { const char *p = NULL; char *forced_key_frames = NULL, *frame_rate = NULL, *frame_size = NULL; char *frame_aspect_ratio = NULL, *frame_pix_fmt = NULL; @@ -3261,7 +3262,7 @@ static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc) audio_enc = st->codec; audio_enc->codec_type = AVMEDIA_TYPE_AUDIO; - if (!st->stream_copy) { + if (!ost->stream_copy) { char *sample_fmt = NULL; MATCH_PER_STREAM_OPT(audio_channels, i, audio_enc->channels, oc, st); @@ -3281,12 +3282,10 @@ static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc) static OutputStream *new_data_stream(OptionsContext *o, AVFormatContext *oc) { - AVStream *st; OutputStream *ost; ost = new_output_stream(o, oc, AVMEDIA_TYPE_DATA); - st = ost->st; - if (!st->stream_copy) { + if (!ost->stream_copy) { av_log(NULL, AV_LOG_FATAL, "Data stream encoding not supported yet (only streamcopy)\n"); exit_program(1); } @@ -3297,7 +3296,7 @@ static OutputStream *new_data_stream(OptionsContext *o, AVFormatContext *oc) static OutputStream *new_attachment_stream(OptionsContext *o, AVFormatContext *oc) { OutputStream *ost = new_output_stream(o, oc, AVMEDIA_TYPE_ATTACHMENT); - ost->st->stream_copy = 1; + ost->stream_copy = 1; return ost; } @@ -3401,9 +3400,9 @@ static int read_avserver_streams(OptionsContext *o, AVFormatContext *s, const ch st->info = NULL; avcodec_copy_context(st->codec, ic->streams[i]->codec); - if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && !st->stream_copy) + if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && !ost->stream_copy) choose_sample_fmt(st, codec); - else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && !st->stream_copy) + else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && !ost->stream_copy) choose_pixel_fmt(st, codec); } diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 74c7140e35..1b67ee6bc2 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -530,8 +530,10 @@ typedef struct AVStream { */ AVRational time_base; int pts_wrap_bits; /**< number of bits in pts (used for wrapping control) */ +#if FF_API_STREAM_COPY /* ffmpeg.c private use */ - int stream_copy; /**< If set, just copy stream. */ + attribute_deprecated int stream_copy; /**< If set, just copy stream. */ +#endif enum AVDiscard discard; ///< Selects which packets can be discarded at will and do not need to be demuxed. #if FF_API_AVSTREAM_QUALITY diff --git a/libavformat/version.h b/libavformat/version.h index 3a6a4f611e..6041daebd6 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -101,5 +101,8 @@ #ifndef FF_API_PRELOAD #define FF_API_PRELOAD (LIBAVFORMAT_VERSION_MAJOR < 54) #endif +#ifndef FF_API_STREAM_COPY +#define FF_API_STREAM_COPY (LIBAVFORMAT_VERSION_MAJOR < 54) +#endif #endif /* AVFORMAT_VERSION_H */ From 35f9d8c20a26a7d383d3d36796e64a4b8987d743 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Wed, 21 Sep 2011 14:16:24 -0400 Subject: [PATCH 10/22] tta: check remaining bitstream size while reading unary value --- libavcodec/tta.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavcodec/tta.c b/libavcodec/tta.c index 5a11436ca9..9330e2d549 100644 --- a/libavcodec/tta.c +++ b/libavcodec/tta.c @@ -182,7 +182,7 @@ static int tta_get_unary(GetBitContext *gb) int ret = 0; // count ones - while(get_bits1(gb)) + while (get_bits_left(gb) > 0 && get_bits1(gb)) ret++; return ret; } From 4d3e7a75165c2724ab7866c1d644aaf4f11af60d Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Wed, 21 Sep 2011 14:17:56 -0400 Subject: [PATCH 11/22] tta: remove useless commented-out lines --- libavcodec/tta.c | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/libavcodec/tta.c b/libavcodec/tta.c index 9330e2d549..6eea6a0bd0 100644 --- a/libavcodec/tta.c +++ b/libavcodec/tta.c @@ -203,10 +203,6 @@ static av_cold int tta_decode_init(AVCodecContext * avctx) { /* signature */ skip_bits(&s->gb, 32); -// if (get_bits_long(&s->gb, 32) != av_bswap32(AV_RL32("TTA1"))) { -// av_log(s->avctx, AV_LOG_ERROR, "Missing magic\n"); -// return -1; -// } s->flags = get_bits(&s->gb, 16); if (s->flags != 1 && s->flags != 3) @@ -371,12 +367,6 @@ static int tta_decode_frame(AVCodecContext *avctx, } *predictor = *p; - /*if ((get_bits_count(&s->gb)+7)/8 > buf_size) - { - av_log(NULL, AV_LOG_INFO, "overread!!\n"); - break; - }*/ - // flip channels if (cur_chan < (s->channels-1)) cur_chan++; @@ -400,8 +390,6 @@ static int tta_decode_frame(AVCodecContext *avctx, case 2: { uint16_t *samples = data; for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) { -// *samples++ = (unsigned char)*p; -// *samples++ = (unsigned char)(*p >> 8); *samples++ = *p; } *data_size = (uint8_t *)samples - (uint8_t *)data; @@ -412,7 +400,6 @@ static int tta_decode_frame(AVCodecContext *avctx, } } -// return get_bits_count(&s->gb)+7)/8; return buf_size; } From b16960a8a5373de74027751a72f3b4f3e9c7fb77 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Thu, 22 Sep 2011 12:54:28 -0400 Subject: [PATCH 12/22] tta: fix reading of format in TTA header. TTA does not support float at all, and format 2 is encrypted TTA. --- libavcodec/tta.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/libavcodec/tta.c b/libavcodec/tta.c index 6eea6a0bd0..74d0d462d2 100644 --- a/libavcodec/tta.c +++ b/libavcodec/tta.c @@ -33,8 +33,8 @@ #include "avcodec.h" #include "get_bits.h" -#define FORMAT_INT 1 -#define FORMAT_FLOAT 3 +#define FORMAT_SIMPLE 1 +#define FORMAT_ENCRYPTED 2 #define MAX_ORDER 16 typedef struct TTAFilter { @@ -58,7 +58,7 @@ typedef struct TTAContext { AVCodecContext *avctx; GetBitContext gb; - int flags, channels, bps, is_float, data_length; + int format, channels, bps, data_length; int frame_length, last_frame_length, total_frames; int32_t *decode_buffer; @@ -204,13 +204,15 @@ static av_cold int tta_decode_init(AVCodecContext * avctx) /* signature */ skip_bits(&s->gb, 32); - s->flags = get_bits(&s->gb, 16); - if (s->flags != 1 && s->flags != 3) - { - av_log(s->avctx, AV_LOG_ERROR, "Invalid flags\n"); + s->format = get_bits(&s->gb, 16); + if (s->format > 2) { + av_log(s->avctx, AV_LOG_ERROR, "Invalid format\n"); return -1; } - s->is_float = (s->flags == FORMAT_FLOAT); + if (s->format == FORMAT_ENCRYPTED) { + av_log_missing_feature(s->avctx, "Encrypted TTA", 0); + return AVERROR(EINVAL); + } avctx->channels = s->channels = get_bits(&s->gb, 16); avctx->bits_per_coded_sample = get_bits(&s->gb, 16); s->bps = (avctx->bits_per_coded_sample + 7) / 8; @@ -222,13 +224,7 @@ static av_cold int tta_decode_init(AVCodecContext * avctx) s->data_length = get_bits_long(&s->gb, 32); skip_bits(&s->gb, 32); // CRC32 of header - if (s->is_float) - { - avctx->sample_fmt = AV_SAMPLE_FMT_FLT; - av_log_ask_for_sample(s->avctx, "Unsupported sample format.\n"); - return -1; - } - else switch(s->bps) { + switch(s->bps) { // case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break; case 2: avctx->sample_fmt = AV_SAMPLE_FMT_S16; break; // case 3: avctx->sample_fmt = AV_SAMPLE_FMT_S24; break; @@ -247,8 +243,8 @@ static av_cold int tta_decode_init(AVCodecContext * avctx) s->total_frames = s->data_length / s->frame_length + (s->last_frame_length ? 1 : 0); - av_log(s->avctx, AV_LOG_DEBUG, "flags: %x chans: %d bps: %d rate: %d block: %d\n", - s->flags, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate, + av_log(s->avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n", + s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate, avctx->block_align); av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n", s->data_length, s->frame_length, s->last_frame_length, s->total_frames); @@ -372,7 +368,7 @@ static int tta_decode_frame(AVCodecContext *avctx, cur_chan++; else { // decorrelate in case of stereo integer - if (!s->is_float && (s->channels > 1)) { + if (s->channels > 1) { int32_t *r = p - 1; for (*p += *r / 2; r > p - s->channels; r--) *r = *(r + 1) - *r; From e6923f683c506cbb581eb7f31288801f1a065fb0 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Thu, 22 Sep 2011 13:09:05 -0400 Subject: [PATCH 13/22] tta: check output buffer size after adjusting frame length for last frame --- libavcodec/tta.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/libavcodec/tta.c b/libavcodec/tta.c index 74d0d462d2..b77034cdf5 100644 --- a/libavcodec/tta.c +++ b/libavcodec/tta.c @@ -285,15 +285,16 @@ static int tta_decode_frame(AVCodecContext *avctx, int cur_chan = 0, framelen = s->frame_length; int32_t *p; - if (*data_size < (framelen * s->channels * 2)) { - av_log(avctx, AV_LOG_ERROR, "Output buffer size is too small.\n"); - return -1; - } // FIXME: seeking s->total_frames--; if (!s->total_frames && s->last_frame_length) framelen = s->last_frame_length; + if (*data_size < (framelen * s->channels * 2)) { + av_log(avctx, AV_LOG_ERROR, "Output buffer size is too small.\n"); + return -1; + } + // init per channel states for (i = 0; i < s->channels; i++) { s->ch_ctx[i].predictor = 0; From 7b7a74a1509348e5487a56c1cde8e48548a90ce3 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Thu, 22 Sep 2011 13:19:36 -0400 Subject: [PATCH 14/22] tta: remove pointless braces --- libavcodec/tta.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libavcodec/tta.c b/libavcodec/tta.c index b77034cdf5..eed5a08cd3 100644 --- a/libavcodec/tta.c +++ b/libavcodec/tta.c @@ -279,11 +279,10 @@ static int tta_decode_frame(AVCodecContext *avctx, int buf_size = avpkt->size; TTAContext *s = avctx->priv_data; int i; + int cur_chan = 0, framelen = s->frame_length; + int32_t *p; init_get_bits(&s->gb, buf, buf_size*8); - { - int cur_chan = 0, framelen = s->frame_length; - int32_t *p; // FIXME: seeking s->total_frames--; @@ -395,7 +394,6 @@ static int tta_decode_frame(AVCodecContext *avctx, default: av_log(s->avctx, AV_LOG_ERROR, "Error, only 16bit samples supported!\n"); } - } return buf_size; } From 8664682d0e6b6071ca7b3f6b9e350305d3fbcf76 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Thu, 22 Sep 2011 13:21:38 -0400 Subject: [PATCH 15/22] cosmetics: indentation --- libavcodec/tta.c | 192 +++++++++++++++++++++++------------------------ 1 file changed, 96 insertions(+), 96 deletions(-) diff --git a/libavcodec/tta.c b/libavcodec/tta.c index eed5a08cd3..fa599b8828 100644 --- a/libavcodec/tta.c +++ b/libavcodec/tta.c @@ -284,116 +284,116 @@ static int tta_decode_frame(AVCodecContext *avctx, init_get_bits(&s->gb, buf, buf_size*8); - // FIXME: seeking - s->total_frames--; - if (!s->total_frames && s->last_frame_length) - framelen = s->last_frame_length; + // FIXME: seeking + s->total_frames--; + if (!s->total_frames && s->last_frame_length) + framelen = s->last_frame_length; - if (*data_size < (framelen * s->channels * 2)) { - av_log(avctx, AV_LOG_ERROR, "Output buffer size is too small.\n"); + if (*data_size < (framelen * s->channels * 2)) { + av_log(avctx, AV_LOG_ERROR, "Output buffer size is too small.\n"); + return -1; + } + + // init per channel states + for (i = 0; i < s->channels; i++) { + s->ch_ctx[i].predictor = 0; + ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]); + rice_init(&s->ch_ctx[i].rice, 10, 10); + } + + for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) { + int32_t *predictor = &s->ch_ctx[cur_chan].predictor; + TTAFilter *filter = &s->ch_ctx[cur_chan].filter; + TTARice *rice = &s->ch_ctx[cur_chan].rice; + uint32_t unary, depth, k; + int32_t value; + + unary = tta_get_unary(&s->gb); + + if (unary == 0) { + depth = 0; + k = rice->k0; + } else { + depth = 1; + k = rice->k1; + unary--; + } + + if (get_bits_left(&s->gb) < k) return -1; - } - // init per channel states - for (i = 0; i < s->channels; i++) { - s->ch_ctx[i].predictor = 0; - ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]); - rice_init(&s->ch_ctx[i].rice, 10, 10); - } - - for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) { - int32_t *predictor = &s->ch_ctx[cur_chan].predictor; - TTAFilter *filter = &s->ch_ctx[cur_chan].filter; - TTARice *rice = &s->ch_ctx[cur_chan].rice; - uint32_t unary, depth, k; - int32_t value; - - unary = tta_get_unary(&s->gb); - - if (unary == 0) { - depth = 0; - k = rice->k0; - } else { - depth = 1; - k = rice->k1; - unary--; - } - - if (get_bits_left(&s->gb) < k) + if (k) { + if (k > MIN_CACHE_BITS) return -1; + value = (unary << k) + get_bits(&s->gb, k); + } else + value = unary; - if (k) { - if (k > MIN_CACHE_BITS) - return -1; - value = (unary << k) + get_bits(&s->gb, k); - } else - value = unary; + // FIXME: copy paste from original + switch (depth) { + case 1: + rice->sum1 += value - (rice->sum1 >> 4); + if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1]) + rice->k1--; + else if(rice->sum1 > shift_16[rice->k1 + 1]) + rice->k1++; + value += shift_1[rice->k0]; + default: + rice->sum0 += value - (rice->sum0 >> 4); + if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0]) + rice->k0--; + else if(rice->sum0 > shift_16[rice->k0 + 1]) + rice->k0++; + } - // FIXME: copy paste from original - switch (depth) { - case 1: - rice->sum1 += value - (rice->sum1 >> 4); - if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1]) - rice->k1--; - else if(rice->sum1 > shift_16[rice->k1 + 1]) - rice->k1++; - value += shift_1[rice->k0]; - default: - rice->sum0 += value - (rice->sum0 >> 4); - if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0]) - rice->k0--; - else if(rice->sum0 > shift_16[rice->k0 + 1]) - rice->k0++; - } - - // extract coded value + // extract coded value #define UNFOLD(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1)) - *p = UNFOLD(value); + *p = UNFOLD(value); - // run hybrid filter - ttafilter_process(filter, p, 0); + // run hybrid filter + ttafilter_process(filter, p, 0); - // fixed order prediction + // fixed order prediction #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k) - switch (s->bps) { - case 1: *p += PRED(*predictor, 4); break; - case 2: - case 3: *p += PRED(*predictor, 5); break; - case 4: *p += *predictor; break; - } - *predictor = *p; - - // flip channels - if (cur_chan < (s->channels-1)) - cur_chan++; - else { - // decorrelate in case of stereo integer - if (s->channels > 1) { - int32_t *r = p - 1; - for (*p += *r / 2; r > p - s->channels; r--) - *r = *(r + 1) - *r; - } - cur_chan = 0; - } + switch (s->bps) { + case 1: *p += PRED(*predictor, 4); break; + case 2: + case 3: *p += PRED(*predictor, 5); break; + case 4: *p += *predictor; break; } + *predictor = *p; - if (get_bits_left(&s->gb) < 32) - return -1; - skip_bits(&s->gb, 32); // frame crc - - // convert to output buffer - switch(s->bps) { - case 2: { - uint16_t *samples = data; - for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) { - *samples++ = *p; - } - *data_size = (uint8_t *)samples - (uint8_t *)data; - break; + // flip channels + if (cur_chan < (s->channels-1)) + cur_chan++; + else { + // decorrelate in case of stereo integer + if (s->channels > 1) { + int32_t *r = p - 1; + for (*p += *r / 2; r > p - s->channels; r--) + *r = *(r + 1) - *r; } - default: - av_log(s->avctx, AV_LOG_ERROR, "Error, only 16bit samples supported!\n"); + cur_chan = 0; } + } + + if (get_bits_left(&s->gb) < 32) + return -1; + skip_bits(&s->gb, 32); // frame crc + + // convert to output buffer + switch(s->bps) { + case 2: { + uint16_t *samples = data; + for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) { + *samples++ = *p; + } + *data_size = (uint8_t *)samples - (uint8_t *)data; + break; + } + default: + av_log(s->avctx, AV_LOG_ERROR, "Error, only 16bit samples supported!\n"); + } return buf_size; } From c6056d40049c2be6450982e9c6ce0b9571bd2403 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 23 Oct 2011 16:13:06 -0400 Subject: [PATCH 16/22] tta: add support for decoding 24-bit sample format Note that this will not work in most cases with avconv and avplay due to the AVCODEC_MAX_AUDIO_FRAME_SIZE limit, but it will decode correctly if given a large enough output buffer. --- libavcodec/tta.c | 56 +++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/libavcodec/tta.c b/libavcodec/tta.c index fa599b8828..b7b20e459b 100644 --- a/libavcodec/tta.c +++ b/libavcodec/tta.c @@ -225,14 +225,17 @@ static av_cold int tta_decode_init(AVCodecContext * avctx) skip_bits(&s->gb, 32); // CRC32 of header switch(s->bps) { -// case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break; - case 2: avctx->sample_fmt = AV_SAMPLE_FMT_S16; break; -// case 3: avctx->sample_fmt = AV_SAMPLE_FMT_S24; break; - case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break; - default: - av_log_ask_for_sample(s->avctx, - "Invalid/unsupported sample format.\n"); - return -1; + case 2: + avctx->sample_fmt = AV_SAMPLE_FMT_S16; + avctx->bits_per_raw_sample = 16; + break; + case 3: + avctx->sample_fmt = AV_SAMPLE_FMT_S32; + avctx->bits_per_raw_sample = 24; + break; + default: + av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n"); + return AVERROR_INVALIDDATA; } // FIXME: horribly broken, but directly from reference source @@ -259,7 +262,9 @@ static av_cold int tta_decode_init(AVCodecContext * avctx) return -1; } - s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels); + if (s->bps == 2) { + s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels); + } s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx)); if (!s->ch_ctx) return AVERROR(ENOMEM); @@ -278,7 +283,7 @@ static int tta_decode_frame(AVCodecContext *avctx, const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; TTAContext *s = avctx->priv_data; - int i; + int i, out_size; int cur_chan = 0, framelen = s->frame_length; int32_t *p; @@ -289,11 +294,16 @@ static int tta_decode_frame(AVCodecContext *avctx, if (!s->total_frames && s->last_frame_length) framelen = s->last_frame_length; - if (*data_size < (framelen * s->channels * 2)) { + out_size = framelen * s->channels * av_get_bytes_per_sample(avctx->sample_fmt); + if (*data_size < out_size) { av_log(avctx, AV_LOG_ERROR, "Output buffer size is too small.\n"); return -1; } + // decode directly to output buffer for 24-bit sample format + if (s->bps == 3) + s->decode_buffer = data; + // init per channel states for (i = 0; i < s->channels; i++) { s->ch_ctx[i].predictor = 0; @@ -382,19 +392,21 @@ static int tta_decode_frame(AVCodecContext *avctx, skip_bits(&s->gb, 32); // frame crc // convert to output buffer - switch(s->bps) { - case 2: { - uint16_t *samples = data; - for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) { - *samples++ = *p; - } - *data_size = (uint8_t *)samples - (uint8_t *)data; - break; - } - default: - av_log(s->avctx, AV_LOG_ERROR, "Error, only 16bit samples supported!\n"); + if (s->bps == 2) { + int16_t *samples = data; + for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) + *samples++ = *p; + } else { + // shift samples for 24-bit sample format + int32_t *samples = data; + for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) + *samples++ <<= 8; + // reset decode buffer + s->decode_buffer = NULL; } + *data_size = out_size; + return buf_size; } From b5050539c9e78186fc090a5c8b6b767590484c59 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 23 Oct 2011 16:25:02 -0400 Subject: [PATCH 17/22] tta: use correct frame_length calculation. using a floating-point calculation is not necessary. --- libavcodec/tta.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/libavcodec/tta.c b/libavcodec/tta.c index b7b20e459b..ddfcb2ebc1 100644 --- a/libavcodec/tta.c +++ b/libavcodec/tta.c @@ -217,10 +217,6 @@ static av_cold int tta_decode_init(AVCodecContext * avctx) avctx->bits_per_coded_sample = get_bits(&s->gb, 16); s->bps = (avctx->bits_per_coded_sample + 7) / 8; avctx->sample_rate = get_bits_long(&s->gb, 32); - if(avctx->sample_rate > 1000000){ //prevent FRAME_TIME * avctx->sample_rate from overflowing and sanity check - av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n"); - return -1; - } s->data_length = get_bits_long(&s->gb, 32); skip_bits(&s->gb, 32); // CRC32 of header @@ -238,9 +234,12 @@ static av_cold int tta_decode_init(AVCodecContext * avctx) return AVERROR_INVALIDDATA; } - // FIXME: horribly broken, but directly from reference source -#define FRAME_TIME 1.04489795918367346939 - s->frame_length = (int)(FRAME_TIME * avctx->sample_rate); + // prevent overflow + if (avctx->sample_rate > 0x7FFFFF) { + av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n"); + return AVERROR(EINVAL); + } + s->frame_length = 256 * avctx->sample_rate / 245; s->last_frame_length = s->data_length % s->frame_length; s->total_frames = s->data_length / s->frame_length + From 2f1d212fd0e62bd8cb2799e15661aa59e38f2a13 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Mon, 24 Oct 2011 09:20:07 -0400 Subject: [PATCH 18/22] tta: check for allocation failure of decode_buffer --- libavcodec/tta.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libavcodec/tta.c b/libavcodec/tta.c index ddfcb2ebc1..ed8d76bc1a 100644 --- a/libavcodec/tta.c +++ b/libavcodec/tta.c @@ -263,10 +263,14 @@ static av_cold int tta_decode_init(AVCodecContext * avctx) if (s->bps == 2) { s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels); + if (!s->decode_buffer) + return AVERROR(ENOMEM); } s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx)); - if (!s->ch_ctx) + if (!s->ch_ctx) { + av_freep(&s->decode_buffer); return AVERROR(ENOMEM); + } } else { av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n"); return -1; From f540ca22c5fb4504d959c295f55591a9ec2a8859 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Fri, 23 Sep 2011 19:22:06 -0400 Subject: [PATCH 19/22] tta: check for extradata allocation failure in tta demuxer --- libavformat/tta.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libavformat/tta.c b/libavformat/tta.c index 87174c1abd..9efcd1d499 100644 --- a/libavformat/tta.c +++ b/libavformat/tta.c @@ -107,6 +107,10 @@ static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap) return -1; } st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE); + if (!st->codec->extradata) { + st->codec->extradata_size = 0; + return AVERROR(ENOMEM); + } avio_seek(s->pb, start_offset, SEEK_SET); avio_read(s->pb, st->codec->extradata, st->codec->extradata_size); From d0a196962a82f26a06357efd843488ae62fabea7 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sat, 22 Oct 2011 18:29:14 -0400 Subject: [PATCH 20/22] g722dec: remove the use of lowres for half-rate decoding. It is broken because an AVCodecContext can be opened/closed multiple times, and sample_rate is getting divided by 2 each time that happens. This removes the only use of lowres for audio. --- avconv.c | 5 +---- libavcodec/g722dec.c | 18 +++++------------- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/avconv.c b/avconv.c index 96f6059980..f93c9fa8d5 100644 --- a/avconv.c +++ b/avconv.c @@ -2074,11 +2074,8 @@ static int transcode_init(OutputFile *output_files, return AVERROR(ENOMEM); } ost->reformat_pair = MAKE_SFMT_PAIR(AV_SAMPLE_FMT_NONE,AV_SAMPLE_FMT_NONE); - if (!codec->sample_rate) { + if (!codec->sample_rate) codec->sample_rate = icodec->sample_rate; - if (icodec->lowres) - codec->sample_rate >>= icodec->lowres; - } choose_sample_rate(ost->st, ost->enc); codec->time_base = (AVRational){1, codec->sample_rate}; if (codec->sample_fmt == AV_SAMPLE_FMT_NONE) diff --git a/libavcodec/g722dec.c b/libavcodec/g722dec.c index 51f87c9d3c..d2d2a48c34 100644 --- a/libavcodec/g722dec.c +++ b/libavcodec/g722dec.c @@ -66,9 +66,6 @@ static av_cold int g722_decode_init(AVCodecContext * avctx) c->band[1].scale_factor = 2; c->prev_samples_pos = 22; - if (avctx->lowres) - avctx->sample_rate /= 2; - return 0; } @@ -96,7 +93,8 @@ static int g722_decode_frame(AVCodecContext *avctx, void *data, init_get_bits(&gb, avpkt->data, avpkt->size * 8); for (j = 0; j < avpkt->size; j++) { - int ilow, ihigh, rlow; + int ilow, ihigh, rlow, rhigh, dhigh; + int xout1, xout2; ihigh = get_bits(&gb, 2); ilow = get_bits(&gb, 6 - skip); @@ -107,12 +105,9 @@ static int g722_decode_frame(AVCodecContext *avctx, void *data, ff_g722_update_low_predictor(&c->band[0], ilow >> (2 - skip)); - if (!avctx->lowres) { - const int dhigh = c->band[1].scale_factor * - ff_g722_high_inv_quant[ihigh] >> 10; - const int rhigh = av_clip(dhigh + c->band[1].s_predictor, - -16384, 16383); - int xout1, xout2; + dhigh = c->band[1].scale_factor * + ff_g722_high_inv_quant[ihigh] >> 10; + rhigh = av_clip(dhigh + c->band[1].s_predictor, -16384, 16383); ff_g722_update_high_predictor(&c->band[1], dhigh, ihigh); @@ -128,8 +123,6 @@ static int g722_decode_frame(AVCodecContext *avctx, void *data, 22 * sizeof(c->prev_samples[0])); c->prev_samples_pos = 22; } - } else - out_buf[out_len++] = rlow; } *data_size = out_len << 1; return avpkt->size; @@ -143,5 +136,4 @@ AVCodec ff_adpcm_g722_decoder = { .init = g722_decode_init, .decode = g722_decode_frame, .long_name = NULL_IF_CONFIG_SMALL("G.722 ADPCM"), - .max_lowres = 1, }; From 4e41973794c5fc5c3a045b00051e0c089774cf9b Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 23 Oct 2011 13:02:04 -0400 Subject: [PATCH 21/22] g722dec: cosmetics: reindent/linewrap --- libavcodec/g722dec.c | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/libavcodec/g722dec.c b/libavcodec/g722dec.c index d2d2a48c34..9330fea3ce 100644 --- a/libavcodec/g722dec.c +++ b/libavcodec/g722dec.c @@ -105,24 +105,22 @@ static int g722_decode_frame(AVCodecContext *avctx, void *data, ff_g722_update_low_predictor(&c->band[0], ilow >> (2 - skip)); - dhigh = c->band[1].scale_factor * - ff_g722_high_inv_quant[ihigh] >> 10; - rhigh = av_clip(dhigh + c->band[1].s_predictor, -16384, 16383); + dhigh = c->band[1].scale_factor * ff_g722_high_inv_quant[ihigh] >> 10; + rhigh = av_clip(dhigh + c->band[1].s_predictor, -16384, 16383); - ff_g722_update_high_predictor(&c->band[1], dhigh, ihigh); + ff_g722_update_high_predictor(&c->band[1], dhigh, ihigh); - c->prev_samples[c->prev_samples_pos++] = rlow + rhigh; - c->prev_samples[c->prev_samples_pos++] = rlow - rhigh; - ff_g722_apply_qmf(c->prev_samples + c->prev_samples_pos - 24, - &xout1, &xout2); - out_buf[out_len++] = av_clip_int16(xout1 >> 12); - out_buf[out_len++] = av_clip_int16(xout2 >> 12); - if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) { - memmove(c->prev_samples, - c->prev_samples + c->prev_samples_pos - 22, - 22 * sizeof(c->prev_samples[0])); - c->prev_samples_pos = 22; - } + c->prev_samples[c->prev_samples_pos++] = rlow + rhigh; + c->prev_samples[c->prev_samples_pos++] = rlow - rhigh; + ff_g722_apply_qmf(c->prev_samples + c->prev_samples_pos - 24, + &xout1, &xout2); + out_buf[out_len++] = av_clip_int16(xout1 >> 12); + out_buf[out_len++] = av_clip_int16(xout2 >> 12); + if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) { + memmove(c->prev_samples, c->prev_samples + c->prev_samples_pos - 22, + 22 * sizeof(c->prev_samples[0])); + c->prev_samples_pos = 22; + } } *data_size = out_len << 1; return avpkt->size; From a3a8572165ce636fb011b78764a2584777f81b95 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 23 Oct 2011 13:00:33 -0400 Subject: [PATCH 22/22] g722dec: check output buffer size before decoding --- libavcodec/g722dec.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/libavcodec/g722dec.c b/libavcodec/g722dec.c index 9330fea3ce..2be47159a4 100644 --- a/libavcodec/g722dec.c +++ b/libavcodec/g722dec.c @@ -85,11 +85,17 @@ static int g722_decode_frame(AVCodecContext *avctx, void *data, { G722Context *c = avctx->priv_data; int16_t *out_buf = data; - int j, out_len = 0; + int j, out_len; const int skip = 8 - avctx->bits_per_coded_sample; const int16_t *quantizer_table = low_inv_quants[skip]; GetBitContext gb; + out_len = avpkt->size * 2 * av_get_bytes_per_sample(avctx->sample_fmt); + if (*data_size < out_len) { + av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n"); + return AVERROR(EINVAL); + } + init_get_bits(&gb, avpkt->data, avpkt->size * 8); for (j = 0; j < avpkt->size; j++) { @@ -114,15 +120,15 @@ static int g722_decode_frame(AVCodecContext *avctx, void *data, c->prev_samples[c->prev_samples_pos++] = rlow - rhigh; ff_g722_apply_qmf(c->prev_samples + c->prev_samples_pos - 24, &xout1, &xout2); - out_buf[out_len++] = av_clip_int16(xout1 >> 12); - out_buf[out_len++] = av_clip_int16(xout2 >> 12); + *out_buf++ = av_clip_int16(xout1 >> 12); + *out_buf++ = av_clip_int16(xout2 >> 12); if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) { memmove(c->prev_samples, c->prev_samples + c->prev_samples_pos - 22, 22 * sizeof(c->prev_samples[0])); c->prev_samples_pos = 22; } } - *data_size = out_len << 1; + *data_size = out_len; return avpkt->size; }