]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/alp.c
avformat: Constify all muxer/demuxers
[ffmpeg] / libavformat / alp.c
index c0c7905380dbba21e58ebed08b8cb4e6a29a8b23..bc19f0208393075c0f4b83c609eba1cc842d179f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * LEGO Racers ALP (.tun & .pcm) demuxer
+ * LEGO Racers ALP (.tun & .pcm) (de)muxer
  *
  * Copyright (C) 2020 Zane van Iperen (zane@zanevaniperen.com)
  *
  */
 #include "avformat.h"
 #include "internal.h"
+#include "rawenc.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/internal.h"
+#include "libavutil/opt.h"
 
 #define ALP_TAG            MKTAG('A', 'L', 'P', ' ')
 #define ALP_MAX_READ_SIZE  4096
@@ -36,6 +38,18 @@ typedef struct ALPHeader {
     uint32_t    sample_rate;    /*< Sample rate, only if header_size >= 12. */
 } ALPHeader;
 
+typedef enum ALPType {
+    ALP_TYPE_AUTO = 0, /*< Autodetect based on file extension. */
+    ALP_TYPE_TUN  = 1, /*< Force a .TUN file. */
+    ALP_TYPE_PCM  = 2, /*< Force a .PCM file. */
+} ALPType;
+
+typedef struct ALPMuxContext {
+    const AVClass *class;
+    ALPType type;
+} ALPMuxContext;
+
+#if CONFIG_ALP_DEMUXER
 static int alp_probe(const AVProbeData *p)
 {
     uint32_t i;
@@ -51,45 +65,46 @@ static int alp_probe(const AVProbeData *p)
     if (strncmp("ADPCM", p->buf + 8, 6) != 0)
         return 0;
 
-    return AVPROBE_SCORE_EXTENSION + 1;
+    return AVPROBE_SCORE_MAX - 1;
 }
 
 static int alp_read_header(AVFormatContext *s)
 {
     int ret;
     AVStream *st;
-    ALPHeader hdr;
+    ALPHeader *hdr = s->priv_data;
     AVCodecParameters *par;
 
-    if ((hdr.magic = avio_rl32(s->pb)) != ALP_TAG)
+    if ((hdr->magic = avio_rl32(s->pb)) != ALP_TAG)
         return AVERROR_INVALIDDATA;
 
-    hdr.header_size = avio_rl32(s->pb);
+    hdr->header_size = avio_rl32(s->pb);
 
-    if (hdr.header_size != 8 && hdr.header_size != 12) {
+    if (hdr->header_size != 8 && hdr->header_size != 12) {
         return AVERROR_INVALIDDATA;
     }
 
-    if ((ret = avio_read(s->pb, hdr.adpcm, sizeof(hdr.adpcm))) < 0)
+    if ((ret = avio_read(s->pb, hdr->adpcm, sizeof(hdr->adpcm))) < 0)
         return ret;
-    else if (ret != sizeof(hdr.adpcm))
+    else if (ret != sizeof(hdr->adpcm))
         return AVERROR(EIO);
 
-    if (strncmp("ADPCM", hdr.adpcm, sizeof(hdr.adpcm)) != 0)
+    if (strncmp("ADPCM", hdr->adpcm, sizeof(hdr->adpcm)) != 0)
         return AVERROR_INVALIDDATA;
 
-    hdr.unk1                    = avio_r8(s->pb);
-    hdr.num_channels            = avio_r8(s->pb);
+    hdr->unk1                   = avio_r8(s->pb);
+    hdr->num_channels           = avio_r8(s->pb);
 
-    if (hdr.header_size == 8) {
+    if (hdr->header_size == 8) {
         /* .TUN music file */
-        hdr.sample_rate         = 11025 * hdr.num_channels;
+        hdr->sample_rate        = 22050;
+
     } else {
         /* .PCM sound file */
-        hdr.sample_rate         = avio_rl32(s->pb);
+        hdr->sample_rate        = avio_rl32(s->pb);
     }
 
-    if (hdr.sample_rate > 44100) {
+    if (hdr->sample_rate > 44100) {
         avpriv_request_sample(s, "Sample Rate > 44100");
         return AVERROR_PATCHWELCOME;
     }
@@ -101,12 +116,12 @@ static int alp_read_header(AVFormatContext *s)
     par->codec_type             = AVMEDIA_TYPE_AUDIO;
     par->codec_id               = AV_CODEC_ID_ADPCM_IMA_ALP;
     par->format                 = AV_SAMPLE_FMT_S16;
-    par->sample_rate            = hdr.sample_rate;
-    par->channels               = hdr.num_channels;
+    par->sample_rate            = hdr->sample_rate;
+    par->channels               = hdr->num_channels;
 
-    if (hdr.num_channels == 1)
+    if (hdr->num_channels == 1)
         par->channel_layout     = AV_CH_LAYOUT_MONO;
-    else if (hdr.num_channels == 2)
+    else if (hdr->num_channels == 2)
         par->channel_layout     = AV_CH_LAYOUT_STEREO;
     else
         return AVERROR_INVALIDDATA;
@@ -137,10 +152,155 @@ static int alp_read_packet(AVFormatContext *s, AVPacket *pkt)
     return 0;
 }
 
-AVInputFormat ff_alp_demuxer = {
+static int alp_seek(AVFormatContext *s, int stream_index,
+                     int64_t pts, int flags)
+{
+    const ALPHeader *hdr = s->priv_data;
+
+    if (pts != 0)
+        return AVERROR(EINVAL);
+
+    return avio_seek(s->pb, hdr->header_size + 8, SEEK_SET);
+}
+
+const AVInputFormat ff_alp_demuxer = {
     .name           = "alp",
     .long_name      = NULL_IF_CONFIG_SMALL("LEGO Racers ALP"),
+    .priv_data_size = sizeof(ALPHeader),
     .read_probe     = alp_probe,
     .read_header    = alp_read_header,
-    .read_packet    = alp_read_packet
+    .read_packet    = alp_read_packet,
+    .read_seek      = alp_seek,
+};
+#endif
+
+#if CONFIG_ALP_MUXER
+
+static int alp_write_init(AVFormatContext *s)
+{
+    ALPMuxContext *alp = s->priv_data;
+    AVCodecParameters *par;
+
+    if (alp->type == ALP_TYPE_AUTO) {
+        if (av_match_ext(s->url, "pcm"))
+            alp->type = ALP_TYPE_PCM;
+        else
+            alp->type = ALP_TYPE_TUN;
+    }
+
+    if (s->nb_streams != 1) {
+        av_log(s, AV_LOG_ERROR, "Too many streams\n");
+        return AVERROR(EINVAL);
+    }
+
+    par = s->streams[0]->codecpar;
+
+    if (par->codec_id != AV_CODEC_ID_ADPCM_IMA_ALP) {
+        av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
+               avcodec_get_name(par->codec_id));
+        return AVERROR(EINVAL);
+    }
+
+    if (par->channels > 2) {
+        av_log(s, AV_LOG_ERROR, "A maximum of 2 channels are supported\n");
+        return AVERROR(EINVAL);
+    }
+
+    if (par->sample_rate > 44100) {
+        av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
+        return AVERROR(EINVAL);
+    }
+
+    if (alp->type == ALP_TYPE_TUN && par->sample_rate != 22050) {
+        av_log(s, AV_LOG_ERROR, "Sample rate must be 22050 for TUN files\n");
+        return AVERROR(EINVAL);
+    }
+    return 0;
+}
+
+static int alp_write_header(AVFormatContext *s)
+{
+    ALPMuxContext *alp = s->priv_data;
+    AVCodecParameters *par = s->streams[0]->codecpar;
+
+    avio_wl32(s->pb,  ALP_TAG);
+    avio_wl32(s->pb,  alp->type == ALP_TYPE_PCM ? 12 : 8);
+    avio_write(s->pb, "ADPCM", 6);
+    avio_w8(s->pb,    0);
+    avio_w8(s->pb,    par->channels);
+    if (alp->type == ALP_TYPE_PCM)
+        avio_wl32(s->pb, par->sample_rate);
+
+    return 0;
+}
+
+enum { AE = AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM };
+
+static const AVOption alp_options[] = {
+    {
+        .name        = "type",
+        .help        = "set file type",
+        .offset      = offsetof(ALPMuxContext, type),
+        .type        = AV_OPT_TYPE_INT,
+        .default_val = {.i64 = ALP_TYPE_AUTO},
+        .min         = ALP_TYPE_AUTO,
+        .max         = ALP_TYPE_PCM,
+        .flags       = AE,
+        .unit        = "type",
+    },
+    {
+        .name        = "auto",
+        .help        = "autodetect based on file extension",
+        .offset      = 0,
+        .type        = AV_OPT_TYPE_CONST,
+        .default_val = {.i64 = ALP_TYPE_AUTO},
+        .min         = 0,
+        .max         = 0,
+        .flags       = AE,
+        .unit        = "type"
+    },
+    {
+        .name        = "tun",
+        .help        = "force .tun, used for music",
+        .offset      = 0,
+        .type        = AV_OPT_TYPE_CONST,
+        .default_val = {.i64 = ALP_TYPE_TUN},
+        .min         = 0,
+        .max         = 0,
+        .flags       = AE,
+        .unit        = "type"
+    },
+    {
+        .name        = "pcm",
+        .help        = "force .pcm, used for sfx",
+        .offset      = 0,
+        .type        = AV_OPT_TYPE_CONST,
+        .default_val = {.i64 = ALP_TYPE_PCM},
+        .min         = 0,
+        .max         = 0,
+        .flags       = AE,
+        .unit        = "type"
+    },
+    { NULL }
+};
+
+static const AVClass alp_muxer_class = {
+    .class_name = "alp",
+    .item_name  = av_default_item_name,
+    .option     = alp_options,
+    .version    = LIBAVUTIL_VERSION_INT
+};
+
+const AVOutputFormat ff_alp_muxer = {
+    .name           = "alp",
+    .long_name      = NULL_IF_CONFIG_SMALL("LEGO Racers ALP"),
+    .extensions     = "tun,pcm",
+    .audio_codec    = AV_CODEC_ID_ADPCM_IMA_ALP,
+    .video_codec    = AV_CODEC_ID_NONE,
+    .init           = alp_write_init,
+    .write_header   = alp_write_header,
+    .write_packet   = ff_raw_write_packet,
+    .priv_class     = &alp_muxer_class,
+    .priv_data_size = sizeof(ALPMuxContext)
 };
+#endif