diff mbox series

[FFmpeg-devel,v2,2/2] libavformat: Add DFPWM raw format

Message ID cd43c3b9-e77a-1f02-a957-00895eb1a2d4@gmail.com
State New
Headers show
Series [FFmpeg-devel,v2,1/2] libavcodec: Added DFPWM1a codec | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 fail Make fate failed
andriy/make_aarch64_jetson success Make finished
andriy/make_fate_aarch64_jetson fail Make fate failed
andriy/make_armv7_RPi4 success Make finished
andriy/make_fate_armv7_RPi4 fail Make fate failed

Commit Message

Jack Bruienne Feb. 25, 2022, 11:43 p.m. UTC
This patch builds on my previous DFPWM codec patch, adding a raw
audio format to be able to read/write the raw files that are most commonly
used (as no other container format supports it yet).

The muxers are mostly copied from the PCM demuxer and the raw muxers, as
DFPWM is typically stored as raw data.

Please see the previous patch for more information on DFPWM.

Signed-off-by: Jack Bruienne <jackbruienne@gmail.com>
---
  Changelog                 |   2 +-
  MAINTAINERS               |   1 +
  doc/general_contents.texi |   1 +
  libavformat/Makefile      |   2 +
  libavformat/allformats.c  |   2 +
  libavformat/dfpwmdec.c    | 107 ++++++++++++++++++++++++++++++++++++++
  libavformat/rawenc.c      |  13 +++++
  libavformat/version.h     |   4 +-
  8 files changed, 129 insertions(+), 3 deletions(-)
  create mode 100644 libavformat/dfpwmdec.c
diff mbox series

Patch

diff --git a/Changelog b/Changelog
index 5170a6a..ec688da 100644
--- a/Changelog
+++ b/Changelog
@@ -4,7 +4,7 @@  releases are sorted from youngest to oldest.
 version 5.1:
 - dialogue enhance audio filter
 - dropped obsolete XvMC hwaccel
-- DFPWM audio encoder/decoder
+- DFPWM audio encoder/decoder and raw muxer/demuxer
 
 
 version 5.0:
diff --git a/MAINTAINERS b/MAINTAINERS
index 57b6f33..931cf4b 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -416,6 +416,7 @@  Muxers/Demuxers:
   dashdec.c                             Steven Liu
   dashenc.c                             Karthick Jeyapal
   daud.c                                Reimar Doeffinger
+  dfpwmdec.c                            Jack Bruienne
   dss.c                                 Oleksij Rempel
   dtsdec.c                              foo86
   dtshddec.c                            Paul B Mahol
diff --git a/doc/general_contents.texi b/doc/general_contents.texi
index 14aeaed..fcd9da1 100644
--- a/doc/general_contents.texi
+++ b/doc/general_contents.texi
@@ -578,6 +578,7 @@  library:
 @item raw aptX                  @tab X @tab X
 @item raw aptX HD               @tab X @tab X
 @item raw Chinese AVS video     @tab X @tab X
+@item raw DFPWM                 @tab X @tab X
 @item raw Dirac                 @tab X @tab X
 @item raw DNxHD                 @tab X @tab X
 @item raw DTS                   @tab X @tab X
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 6566e40..b89073a 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -165,6 +165,8 @@  OBJS-$(CONFIG_DAUD_MUXER)                += daudenc.o
 OBJS-$(CONFIG_DCSTR_DEMUXER)             += dcstr.o
 OBJS-$(CONFIG_DERF_DEMUXER)              += derf.o pcm.o
 OBJS-$(CONFIG_DFA_DEMUXER)               += dfa.o
+OBJS-$(CONFIG_DFPWM_DEMUXER)             += dfpwmdec.o pcm.o
+OBJS-$(CONFIG_DFPWM_MUXER)               += rawenc.o
 OBJS-$(CONFIG_DHAV_DEMUXER)              += dhav.o
 OBJS-$(CONFIG_DIRAC_DEMUXER)             += diracdec.o rawdec.o
 OBJS-$(CONFIG_DIRAC_MUXER)               += rawenc.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index d066a77..587ad59 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -124,6 +124,8 @@  extern const AVOutputFormat ff_daud_muxer;
 extern const AVInputFormat  ff_dcstr_demuxer;
 extern const AVInputFormat  ff_derf_demuxer;
 extern const AVInputFormat  ff_dfa_demuxer;
+extern const AVInputFormat  ff_dfpwm_demuxer;
+extern const AVOutputFormat ff_dfpwm_muxer;
 extern const AVInputFormat  ff_dhav_demuxer;
 extern const AVInputFormat  ff_dirac_demuxer;
 extern const AVOutputFormat ff_dirac_muxer;
diff --git a/libavformat/dfpwmdec.c b/libavformat/dfpwmdec.c
new file mode 100644
index 0000000..ad5bfa5
--- /dev/null
+++ b/libavformat/dfpwmdec.c
@@ -0,0 +1,107 @@ 
+/*
+ * RAW PCM demuxers
+ * Copyright (c) 2002 Fabrice Bellard
+ * Copyright (c) 2022 Jack Bruienne
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/avstring.h"
+#include "avformat.h"
+#include "internal.h"
+#include "pcm.h"
+#include "libavutil/log.h"
+#include "libavutil/opt.h"
+#include "libavutil/avassert.h"
+
+typedef struct DFPWMAudioDemuxerContext {
+    AVClass *class;
+    int sample_rate;
+} DFPWMAudioDemuxerContext;
+
+static int dfpwm_read_header(AVFormatContext *s)
+{
+    DFPWMAudioDemuxerContext *s1 = s->priv_data;
+    AVCodecParameters *par;
+    AVStream *st;
+    uint8_t *mime_type = NULL;
+
+    st = avformat_new_stream(s, NULL);
+    if (!st)
+        return AVERROR(ENOMEM);
+    par = st->codecpar;
+
+    par->codec_type  = AVMEDIA_TYPE_AUDIO;
+    par->codec_id    = s->iformat->raw_codec_id;
+    par->sample_rate = s1->sample_rate;
+    par->channels    = 1;
+
+    av_opt_get(s->pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type);
+    if (mime_type && s->iformat->mime_type) {
+        int rate = 0;
+        const char *options;
+        if (av_stristart(mime_type, s->iformat->mime_type, &options)) { /* audio/L16 */
+            while (options = strchr(options, ';')) {
+                options++;
+                if (!rate)
+                    sscanf(options, " rate=%d",     &rate);
+            }
+            if (rate <= 0) {
+                av_log(s, AV_LOG_ERROR,
+                       "Invalid sample_rate found in mime_type \"%s\"\n",
+                       mime_type);
+                av_freep(&mime_type);
+                return AVERROR_INVALIDDATA;
+            }
+            par->sample_rate = rate;
+        }
+    }
+    av_freep(&mime_type);
+
+    par->bits_per_coded_sample = av_get_bits_per_sample(par->codec_id);
+
+    av_assert0(par->bits_per_coded_sample > 0);
+
+    par->block_align = 1;
+
+    avpriv_set_pts_info(st, 64, 1, par->sample_rate);
+    return 0;
+}
+
+static const AVOption dfpwm_options[] = {
+    { "sample_rate", "", offsetof(DFPWMAudioDemuxerContext, sample_rate), AV_OPT_TYPE_INT, {.i64 = 44100}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
+    { NULL },
+};
+static const AVClass dfpwm_demuxer_class = {
+    .class_name = "dfpwm demuxer",
+    .item_name  = av_default_item_name,
+    .option     = dfpwm_options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+const AVInputFormat ff_dfpwm_demuxer = {
+    .name           = "dfpwm",
+    .long_name      = NULL_IF_CONFIG_SMALL("raw DFPWM1a data"),
+    .priv_data_size = sizeof(DFPWMAudioDemuxerContext),
+    .read_header    = dfpwm_read_header,
+    .read_packet    = ff_pcm_read_packet,
+    .read_seek      = ff_pcm_read_seek,
+    .flags          = AVFMT_GENERIC_INDEX,
+    .extensions     = "dfpwm",
+    .raw_codec_id   = AV_CODEC_ID_DFPWM,
+    .priv_class     = &dfpwm_demuxer_class,
+};
\ No newline at end of file
diff --git a/libavformat/rawenc.c b/libavformat/rawenc.c
index 4bbae77..17b627b 100644
--- a/libavformat/rawenc.c
+++ b/libavformat/rawenc.c
@@ -192,6 +192,19 @@  const AVOutputFormat ff_data_muxer = {
 };
 #endif
 
+#if CONFIG_DFPWM_MUXER
+const AVOutputFormat ff_dfpwm_muxer = {
+    .name              = "dfpwm",
+    .long_name         = NULL_IF_CONFIG_SMALL("raw DFPWM1a audio"),
+    .extensions        = "dfpwm",
+    .audio_codec       = AV_CODEC_ID_DFPWM,
+    .video_codec       = AV_CODEC_ID_NONE,
+    .init              = force_one_stream,
+    .write_packet      = ff_raw_write_packet,
+    .flags             = AVFMT_NOTIMESTAMPS,
+};
+#endif
+
 #if CONFIG_DIRAC_MUXER
 const AVOutputFormat ff_dirac_muxer = {
     .name              = "dirac",
diff --git a/libavformat/version.h b/libavformat/version.h
index 2623457..0f89af4 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,8 +32,8 @@ 
 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  59
-#define LIBAVFORMAT_VERSION_MINOR  17
-#define LIBAVFORMAT_VERSION_MICRO 102
+#define LIBAVFORMAT_VERSION_MINOR  18
+#define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \