libavcodec: Don't crash in avcodec_encode_audio if time_base isn't set

Message ID 1327615606-28677-1-git-send-email-martin@martin.st
State Superseded
Headers show

Commit Message

Martin Storsjö Jan. 26, 2012, 10:06 p.m.
Earlier, calling avcodec_encode_audio worked fine even if time_base
wasn't set. Now it crashes due to trying to scale the output pts to
the codec context time base. This affects e.g. VLC.

If no time_base is set for audio codecs, set it to the sample
rate.
---
 libavcodec/utils.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)

Comments

Justin Ruggles Jan. 26, 2012, 11:22 p.m. | #1
On 01/26/2012 05:06 PM, Martin Storsjö wrote:

> Earlier, calling avcodec_encode_audio worked fine even if time_base
> wasn't set. Now it crashes due to trying to scale the output pts to
> the codec context time base. This affects e.g. VLC.
> 
> If no time_base is set for audio codecs, set it to the sample
> rate.
> ---
>  libavcodec/utils.c |    5 +++++
>  1 files changed, 5 insertions(+), 0 deletions(-)
> 
> diff --git a/libavcodec/utils.c b/libavcodec/utils.c
> index 184f67e..bcf9611 100644
> --- a/libavcodec/utils.c
> +++ b/libavcodec/utils.c
> @@ -734,6 +734,11 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVD
>             avctx->error_recognition, avctx->err_recognition);
>  #endif
>  
> +    if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && !avctx->time_base.den) {
> +        avctx->time_base.num = 1;
> +        avctx->time_base.den = avctx->sample_rate;
> +    }
> +
>      if (HAVE_THREADS && !avctx->thread_opaque) {
>          ret = ff_thread_init(avctx);
>          if (ret < 0) {


default time_base is 0/1, which will still crash.

-Justin

Patch

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 184f67e..bcf9611 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -734,6 +734,11 @@  int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVD
            avctx->error_recognition, avctx->err_recognition);
 #endif
 
+    if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && !avctx->time_base.den) {
+        avctx->time_base.num = 1;
+        avctx->time_base.den = avctx->sample_rate;
+    }
+
     if (HAVE_THREADS && !avctx->thread_opaque) {
         ret = ff_thread_init(avctx);
         if (ret < 0) {