diff mbox series

[FFmpeg-devel,v2,2/2] avformat/mvdec: handle audio sample size

Message ID 20211127214551.22949-2-jpstewart@personalprojects.net
State Accepted
Commit 6c76b6392348460472f0b6deac4d0a161109d498
Headers show
Series [FFmpeg-devel,v2,1/2] avformat/mvdec: fix reading number of audio channels | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

John-Paul Stewart Nov. 27, 2021, 9:45 p.m. UTC
Adds support for reading audio sample size from the data instead of
assuming all audio is 16 bits per sample.
---
 libavformat/mvdec.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

Comments

Peter Ross Dec. 1, 2021, 5:18 a.m. UTC | #1
On Sat, Nov 27, 2021 at 04:45:51PM -0500, John-Paul Stewart wrote:
> Adds support for reading audio sample size from the data instead of
> assuming all audio is 16 bits per sample.
> ---
>  libavformat/mvdec.c | 18 +++++++++++++++---
>  1 file changed, 15 insertions(+), 3 deletions(-)
> 
> diff --git a/libavformat/mvdec.c b/libavformat/mvdec.c
> index 8492928820..8b54a9ab04 100644
> --- a/libavformat/mvdec.c
> +++ b/libavformat/mvdec.c
> @@ -299,6 +299,8 @@ static int mv_read_header(AVFormatContext *avctx)
>      if (version == 2) {
>          uint64_t timestamp;
>          int v;
> +        uint32_t bytes_per_sample;
> +
>          avio_skip(pb, 22);
>  
>          /* allocate audio track first to prevent unnecessary seeking
> @@ -341,11 +343,21 @@ static int mv_read_header(AVFormatContext *avctx)
>          }
>          avpriv_set_pts_info(ast, 33, 1, ast->codecpar->sample_rate);
>  
> -        avio_skip(pb, 4);
> +        bytes_per_sample = avio_rb32(pb);
>  
>          v = avio_rb32(pb);
>          if (v == AUDIO_FORMAT_SIGNED) {
> -            ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
> +            switch (bytes_per_sample) {
> +            case 1:
> +                ast->codecpar->codec_id = AV_CODEC_ID_PCM_S8;
> +                break;
> +            case 2:
> +                ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
> +                break;
> +            default:
> +                avpriv_request_sample(avctx, "Audio sample size %i bytes", bytes_per_sample);
> +                break;
> +            }
>          } else {
>              avpriv_request_sample(avctx, "Audio compression (format %i)", v);
>          }
> @@ -369,7 +381,7 @@ static int mv_read_header(AVFormatContext *avctx)
>              avio_skip(pb, 8);
>              av_add_index_entry(ast, pos, timestamp, asize, 0, AVINDEX_KEYFRAME);
>              av_add_index_entry(vst, pos + asize, i, vsize, 0, AVINDEX_KEYFRAME);
> -            timestamp += asize / (ast->codecpar->channels * 2LL);
> +            timestamp += asize / (ast->codecpar->channels * bytes_per_sample);
>          }
>      } else if (!version && avio_rb16(pb) == 3) {
>          avio_skip(pb, 4);
> -- 
> 2.33.0

thanks for doing this.
both patches look good. i will apply in a few days.

-- Peter
(A907 E02F A6E5 0CD2 34CD 20D2 6760 79C5 AC40 DD6B)
Peter Ross Dec. 3, 2021, 8:57 a.m. UTC | #2
On Wed, Dec 01, 2021 at 04:18:42PM +1100, Peter Ross wrote:
> On Sat, Nov 27, 2021 at 04:45:51PM -0500, John-Paul Stewart wrote:
> > Adds support for reading audio sample size from the data instead of
> > assuming all audio is 16 bits per sample.
> > ---
> >  libavformat/mvdec.c | 18 +++++++++++++++---
> >  1 file changed, 15 insertions(+), 3 deletions(-)
> > 
> > diff --git a/libavformat/mvdec.c b/libavformat/mvdec.c
> > index 8492928820..8b54a9ab04 100644
> > --- a/libavformat/mvdec.c
> > +++ b/libavformat/mvdec.c
> > @@ -299,6 +299,8 @@ static int mv_read_header(AVFormatContext *avctx)
> >      if (version == 2) {
> >          uint64_t timestamp;
> >          int v;
> > +        uint32_t bytes_per_sample;
> > +
> >          avio_skip(pb, 22);
> >  
> >          /* allocate audio track first to prevent unnecessary seeking
> > @@ -341,11 +343,21 @@ static int mv_read_header(AVFormatContext *avctx)
> >          }
> >          avpriv_set_pts_info(ast, 33, 1, ast->codecpar->sample_rate);
> >  
> > -        avio_skip(pb, 4);
> > +        bytes_per_sample = avio_rb32(pb);
> >  
> >          v = avio_rb32(pb);
> >          if (v == AUDIO_FORMAT_SIGNED) {
> > -            ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
> > +            switch (bytes_per_sample) {
> > +            case 1:
> > +                ast->codecpar->codec_id = AV_CODEC_ID_PCM_S8;
> > +                break;
> > +            case 2:
> > +                ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
> > +                break;
> > +            default:
> > +                avpriv_request_sample(avctx, "Audio sample size %i bytes", bytes_per_sample);
> > +                break;
> > +            }
> >          } else {
> >              avpriv_request_sample(avctx, "Audio compression (format %i)", v);
> >          }
> > @@ -369,7 +381,7 @@ static int mv_read_header(AVFormatContext *avctx)
> >              avio_skip(pb, 8);
> >              av_add_index_entry(ast, pos, timestamp, asize, 0, AVINDEX_KEYFRAME);
> >              av_add_index_entry(vst, pos + asize, i, vsize, 0, AVINDEX_KEYFRAME);
> > -            timestamp += asize / (ast->codecpar->channels * 2LL);
> > +            timestamp += asize / (ast->codecpar->channels * bytes_per_sample);
> >          }
> >      } else if (!version && avio_rb16(pb) == 3) {
> >          avio_skip(pb, 4);
> > -- 
> > 2.33.0
> 
> thanks for doing this.
> both patches look good. i will apply in a few days.

applied.

-- Peter
(A907 E02F A6E5 0CD2 34CD 20D2 6760 79C5 AC40 DD6B)
diff mbox series

Patch

diff --git a/libavformat/mvdec.c b/libavformat/mvdec.c
index 8492928820..8b54a9ab04 100644
--- a/libavformat/mvdec.c
+++ b/libavformat/mvdec.c
@@ -299,6 +299,8 @@  static int mv_read_header(AVFormatContext *avctx)
     if (version == 2) {
         uint64_t timestamp;
         int v;
+        uint32_t bytes_per_sample;
+
         avio_skip(pb, 22);
 
         /* allocate audio track first to prevent unnecessary seeking
@@ -341,11 +343,21 @@  static int mv_read_header(AVFormatContext *avctx)
         }
         avpriv_set_pts_info(ast, 33, 1, ast->codecpar->sample_rate);
 
-        avio_skip(pb, 4);
+        bytes_per_sample = avio_rb32(pb);
 
         v = avio_rb32(pb);
         if (v == AUDIO_FORMAT_SIGNED) {
-            ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
+            switch (bytes_per_sample) {
+            case 1:
+                ast->codecpar->codec_id = AV_CODEC_ID_PCM_S8;
+                break;
+            case 2:
+                ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
+                break;
+            default:
+                avpriv_request_sample(avctx, "Audio sample size %i bytes", bytes_per_sample);
+                break;
+            }
         } else {
             avpriv_request_sample(avctx, "Audio compression (format %i)", v);
         }
@@ -369,7 +381,7 @@  static int mv_read_header(AVFormatContext *avctx)
             avio_skip(pb, 8);
             av_add_index_entry(ast, pos, timestamp, asize, 0, AVINDEX_KEYFRAME);
             av_add_index_entry(vst, pos + asize, i, vsize, 0, AVINDEX_KEYFRAME);
-            timestamp += asize / (ast->codecpar->channels * 2LL);
+            timestamp += asize / (ast->codecpar->channels * bytes_per_sample);
         }
     } else if (!version && avio_rb16(pb) == 3) {
         avio_skip(pb, 4);