diff mbox

[FFmpeg-devel,1/3] omadec: fix overflows during bit rate calculation

Message ID 18a6d792-ed50-8963-ccfd-8c585824682f@googlemail.com
State Superseded
Headers show

Commit Message

Andreas Cadhalpun Dec. 12, 2016, 11:48 p.m. UTC
Signed-off-by: Andreas Cadhalpun <Andreas.Cadhalpun@googlemail.com>
---
 libavformat/omadec.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Paul B Mahol Dec. 13, 2016, 7:11 a.m. UTC | #1
On 12/13/16, Andreas Cadhalpun <andreas.cadhalpun@googlemail.com> wrote:
> Signed-off-by: Andreas Cadhalpun <Andreas.Cadhalpun@googlemail.com>
> ---
>  libavformat/omadec.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/libavformat/omadec.c b/libavformat/omadec.c
> index 6e476db..e7751d0 100644
> --- a/libavformat/omadec.c
> +++ b/libavformat/omadec.c
> @@ -365,7 +365,7 @@ static int oma_read_header(AVFormatContext *s)
>          st->codecpar->channels    = 2;
>          st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
>          st->codecpar->sample_rate = samplerate;
> -        st->codecpar->bit_rate    = st->codecpar->sample_rate * framesize *
> 8 / 1024;
> +        st->codecpar->bit_rate    = st->codecpar->sample_rate * framesize /
> 128;
>
>          /* fake the ATRAC3 extradata
>           * (wav format, makes stream copy to wav work) */
> @@ -398,7 +398,7 @@ static int oma_read_header(AVFormatContext *s)
>              return AVERROR_INVALIDDATA;
>          }
>          st->codecpar->sample_rate = samplerate;
> -        st->codecpar->bit_rate    = samplerate * framesize * 8 / 2048;
> +        st->codecpar->bit_rate    = samplerate * framesize / 256;
>          avpriv_set_pts_info(st, 64, 1, samplerate);
>          break;
>      case OMA_CODECID_MP3:

Shouldn't using 8LL or similar be more future-proof?
Andreas Cadhalpun Dec. 14, 2016, 12:02 a.m. UTC | #2
On 13.12.2016 08:11, Paul B Mahol wrote:
> On 12/13/16, Andreas Cadhalpun <andreas.cadhalpun@googlemail.com> wrote:
>> Signed-off-by: Andreas Cadhalpun <Andreas.Cadhalpun@googlemail.com>
>> ---
>>  libavformat/omadec.c | 4 ++--
>>  1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/libavformat/omadec.c b/libavformat/omadec.c
>> index 6e476db..e7751d0 100644
>> --- a/libavformat/omadec.c
>> +++ b/libavformat/omadec.c
>> @@ -365,7 +365,7 @@ static int oma_read_header(AVFormatContext *s)
>>          st->codecpar->channels    = 2;
>>          st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
>>          st->codecpar->sample_rate = samplerate;
>> -        st->codecpar->bit_rate    = st->codecpar->sample_rate * framesize *
>> 8 / 1024;
>> +        st->codecpar->bit_rate    = st->codecpar->sample_rate * framesize /
>> 128;
>>
>>          /* fake the ATRAC3 extradata
>>           * (wav format, makes stream copy to wav work) */
>> @@ -398,7 +398,7 @@ static int oma_read_header(AVFormatContext *s)
>>              return AVERROR_INVALIDDATA;
>>          }
>>          st->codecpar->sample_rate = samplerate;
>> -        st->codecpar->bit_rate    = samplerate * framesize * 8 / 2048;
>> +        st->codecpar->bit_rate    = samplerate * framesize / 256;
>>          avpriv_set_pts_info(st, 64, 1, samplerate);
>>          break;
>>      case OMA_CODECID_MP3:
> 
> Shouldn't using 8LL or similar be more future-proof?

Why multiply with 8 when dividing by a multiple of 8 directly afterwards?
That's just a waste of computational resources.
If sample_rate and/or framesize get larger in the future, a cast can be added.

Best regards,
Andreas
Paul B Mahol Dec. 14, 2016, 7:29 a.m. UTC | #3
On 12/14/16, Andreas Cadhalpun <andreas.cadhalpun@googlemail.com> wrote:
> On 13.12.2016 08:11, Paul B Mahol wrote:
>> On 12/13/16, Andreas Cadhalpun <andreas.cadhalpun@googlemail.com> wrote:
>>> Signed-off-by: Andreas Cadhalpun <Andreas.Cadhalpun@googlemail.com>
>>> ---
>>>  libavformat/omadec.c | 4 ++--
>>>  1 file changed, 2 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/libavformat/omadec.c b/libavformat/omadec.c
>>> index 6e476db..e7751d0 100644
>>> --- a/libavformat/omadec.c
>>> +++ b/libavformat/omadec.c
>>> @@ -365,7 +365,7 @@ static int oma_read_header(AVFormatContext *s)
>>>          st->codecpar->channels    = 2;
>>>          st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
>>>          st->codecpar->sample_rate = samplerate;
>>> -        st->codecpar->bit_rate    = st->codecpar->sample_rate *
>>> framesize *
>>> 8 / 1024;
>>> +        st->codecpar->bit_rate    = st->codecpar->sample_rate *
>>> framesize /
>>> 128;
>>>
>>>          /* fake the ATRAC3 extradata
>>>           * (wav format, makes stream copy to wav work) */
>>> @@ -398,7 +398,7 @@ static int oma_read_header(AVFormatContext *s)
>>>              return AVERROR_INVALIDDATA;
>>>          }
>>>          st->codecpar->sample_rate = samplerate;
>>> -        st->codecpar->bit_rate    = samplerate * framesize * 8 / 2048;
>>> +        st->codecpar->bit_rate    = samplerate * framesize / 256;
>>>          avpriv_set_pts_info(st, 64, 1, samplerate);
>>>          break;
>>>      case OMA_CODECID_MP3:
>>
>> Shouldn't using 8LL or similar be more future-proof?
>
> Why multiply with 8 when dividing by a multiple of 8 directly afterwards?
> That's just a waste of computational resources.
> If sample_rate and/or framesize get larger in the future, a cast can be
> added.
>
> Best regards,
> Andreas
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>

ok
Hendrik Leppkes Dec. 14, 2016, 9:47 a.m. UTC | #4
On Wed, Dec 14, 2016 at 1:02 AM, Andreas Cadhalpun
<andreas.cadhalpun@googlemail.com> wrote:
> On 13.12.2016 08:11, Paul B Mahol wrote:
>> On 12/13/16, Andreas Cadhalpun <andreas.cadhalpun@googlemail.com> wrote:
>>> Signed-off-by: Andreas Cadhalpun <Andreas.Cadhalpun@googlemail.com>
>>> ---
>>>  libavformat/omadec.c | 4 ++--
>>>  1 file changed, 2 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/libavformat/omadec.c b/libavformat/omadec.c
>>> index 6e476db..e7751d0 100644
>>> --- a/libavformat/omadec.c
>>> +++ b/libavformat/omadec.c
>>> @@ -365,7 +365,7 @@ static int oma_read_header(AVFormatContext *s)
>>>          st->codecpar->channels    = 2;
>>>          st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
>>>          st->codecpar->sample_rate = samplerate;
>>> -        st->codecpar->bit_rate    = st->codecpar->sample_rate * framesize *
>>> 8 / 1024;
>>> +        st->codecpar->bit_rate    = st->codecpar->sample_rate * framesize /
>>> 128;
>>>
>>>          /* fake the ATRAC3 extradata
>>>           * (wav format, makes stream copy to wav work) */
>>> @@ -398,7 +398,7 @@ static int oma_read_header(AVFormatContext *s)
>>>              return AVERROR_INVALIDDATA;
>>>          }
>>>          st->codecpar->sample_rate = samplerate;
>>> -        st->codecpar->bit_rate    = samplerate * framesize * 8 / 2048;
>>> +        st->codecpar->bit_rate    = samplerate * framesize / 256;
>>>          avpriv_set_pts_info(st, 64, 1, samplerate);
>>>          break;
>>>      case OMA_CODECID_MP3:
>>
>> Shouldn't using 8LL or similar be more future-proof?
>
> Why multiply with 8 when dividing by a multiple of 8 directly afterwards?
> That's just a waste of computational resources.
> If sample_rate and/or framesize get larger in the future, a cast can be added.
>

The compiler should be able to optimize such things out and it may add
clarity to what the code is doing.

- Hendrik
Moritz Barsnick Dec. 14, 2016, 10:16 a.m. UTC | #5
On Wed, Dec 14, 2016 at 01:02:41 +0100, Andreas Cadhalpun wrote:
> On 13.12.2016 08:11, Paul B Mahol wrote:
> >> -        st->codecpar->bit_rate    = samplerate * framesize * 8 / 2048;
> >> +        st->codecpar->bit_rate    = samplerate * framesize / 256;
> 
> Why multiply with 8 when dividing by a multiple of 8 directly afterwards?
> That's just a waste of computational resources.

I can only explain the term with "readability" (e.g. "number of bytes
times 8 is number of bits, divided by 2048 is the rate"). If you
bracket the (8 / 2048), it would avoid the overflow, and the compiler
should evaluate the term to that constant 256 anyway, right? (Just if
anyone cares about the presumed readability.)

Moritz
diff mbox

Patch

diff --git a/libavformat/omadec.c b/libavformat/omadec.c
index 6e476db..e7751d0 100644
--- a/libavformat/omadec.c
+++ b/libavformat/omadec.c
@@ -365,7 +365,7 @@  static int oma_read_header(AVFormatContext *s)
         st->codecpar->channels    = 2;
         st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
         st->codecpar->sample_rate = samplerate;
-        st->codecpar->bit_rate    = st->codecpar->sample_rate * framesize * 8 / 1024;
+        st->codecpar->bit_rate    = st->codecpar->sample_rate * framesize / 128;
 
         /* fake the ATRAC3 extradata
          * (wav format, makes stream copy to wav work) */
@@ -398,7 +398,7 @@  static int oma_read_header(AVFormatContext *s)
             return AVERROR_INVALIDDATA;
         }
         st->codecpar->sample_rate = samplerate;
-        st->codecpar->bit_rate    = samplerate * framesize * 8 / 2048;
+        st->codecpar->bit_rate    = samplerate * framesize / 256;
         avpriv_set_pts_info(st, 64, 1, samplerate);
         break;
     case OMA_CODECID_MP3: