]> git.alsa-project.org Git - tinycompress.git/commitdiff
fcplay: Decode the AAC header to set correct codec->format value
authorPeter Ujfalusi <peter.ujfalusi@linux.intel.com>
Thu, 12 Mar 2026 11:31:43 +0000 (13:31 +0200)
committerJaroslav Kysela <perex@perex.cz>
Fri, 29 May 2026 11:20:27 +0000 (13:20 +0200)
Set the codec->format for AAC based on the information from the header.

Closes: https://github.com/alsa-project/tinycompress/pull/34
Signed-off-by: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
src/utils-lgpl/fcplay.c

index eac0712b907c91444cd052bb31e41489e2da30b5..1b89c2bec62672593fceb959c2e8806524903721 100644 (file)
@@ -199,6 +199,34 @@ static int get_codec_id(int codec_id)
        }
 }
 
+static unsigned int get_aac_format(AVFormatContext *ctx)
+{
+       unsigned char buf[4];
+       int n;
+
+       avio_seek(ctx->pb, 0, SEEK_SET);
+       n = avio_read(ctx->pb, buf, sizeof(buf));
+       avio_seek(ctx->pb, 0, SEEK_SET);
+
+       if (n < 2)
+               return 0;
+
+       /* ADIF magic: "ADIF" */
+       if (n == 4 && buf[0] == 0x41 && buf[1] == 0x44 && buf[2] == 0x49 && buf[3] == 0x46)
+               return SND_AUDIOSTREAMFORMAT_ADIF;
+
+       /* ADTS sync word: 0xFFF... */
+       if (buf[0] == 0xff && (buf[1] & 0xf0) == 0xf0) {
+               /* MPEG ID bit (bit 3 of buf[1]): 0 = MPEG-4, 1 = MPEG-2 */
+               if ((buf[1] >> 3) & 0x1)
+                       return SND_AUDIOSTREAMFORMAT_MP2ADTS;
+               else
+                       return SND_AUDIOSTREAMFORMAT_MP4ADTS;
+       }
+
+       return 0;
+}
+
 static int parse_file(char *file, struct snd_codec *codec)
 {
        AVFormatContext *ctx = NULL;
@@ -245,7 +273,10 @@ static int parse_file(char *file, struct snd_codec *codec)
                                codec->sample_rate = stream->codecpar->sample_rate;
                                codec->bit_rate = ctx->bit_rate;
                                codec->profile = stream->codecpar->profile;
-                               codec->format = 0; /* need codec format type */
+                               if (codec->id == SND_AUDIOCODEC_AAC)
+                                       codec->format = get_aac_format(ctx);
+                               else
+                                       codec->format = 0;
                                codec->align = stream->codecpar->block_align;
                                codec->level = 0;
                                codec->rate_control = 0;