[FFmpeg-devel] [PATCH] Add support for sndio to libavdevice

Brad brad
Sun Aug 8 05:17:24 CEST 2010


On Sat, Aug 07, 2010 at 11:11:23PM -0400, Brad wrote:
> On Mon, Aug 02, 2010 at 07:42:28PM -0400, Brad wrote:
> > sndio is a relatively new audio API utilized by OpenBSD.
> > 
> > Below is a patch to add sndio playback and record support
> > to FFmpeg.
> > 
> > I believe I have touched everything that needs updating
> > including documentation such as the recently added
> > indevs.texi and outdevs.texi, but if not please let
> > me know.
> > 
> > The sndio code was written by Jacob Meuser <jakemsr sdf lonestar org>
> > 
> > Please provide any feedback.
> 
> a third revision with the code splitting and some further clean up.

and the right diff.


Index: configure
===================================================================
--- configure	(revision 24743)
+++ configure	(working copy)
@@ -1033,6 +1033,7 @@
     sdl
     sdl_video_size
     setmode
+    sndio_h
     socklen_t
     soundcard_h
     poll_h
@@ -1363,6 +1364,8 @@
 libdc1394_indev_deps="libdc1394"
 oss_indev_deps_any="soundcard_h sys_soundcard_h"
 oss_outdev_deps_any="soundcard_h sys_soundcard_h"
+sndio_indev_deps="sndio_h"
+sndio_outdev_deps="sndio_h"
 v4l_indev_deps="linux_videodev_h"
 v4l2_indev_deps_any="linux_videodev2_h sys_videoio_h"
 vfwcap_indev_deps="capCreateCaptureWindow vfwcap_defines"
@@ -2775,11 +2778,14 @@
 
 check_header sys/soundcard.h
 check_header soundcard.h
+check_header sndio.h
 
 enabled_any alsa_indev alsa_outdev && check_lib2 alsa/asoundlib.h snd_pcm_htimestamp -lasound
 
 enabled jack_indev && check_lib2 jack/jack.h jack_client_open -ljack
 
+enabled_any sndio_indev sndio_outdev && check_lib2 sndio.h sio_open -lsndio
+
 enabled x11grab                         &&
 check_header X11/Xlib.h                 &&
 check_header X11/extensions/XShm.h      &&
Index: Changelog
===================================================================
--- Changelog	(revision 24743)
+++ Changelog	(working copy)
@@ -28,6 +28,7 @@
 - Chinese AVS encoding via libxavs
 - ffprobe -show_packets option added
 - RTP packetization of Theora and Vorbis
+- sndio support for playback and record
 
 
 
Index: doc/indevs.texi
===================================================================
--- doc/indevs.texi	(revision 24743)
+++ doc/indevs.texi	(working copy)
@@ -129,6 +129,23 @@
 For more information about OSS see:
 @url{http://manuals.opensound.com/usersguide/dsp.html}
 
+ at section sndio
+
+sndio input device.
+
+To enable this input device during configuration you need libsndio
+installed on your system.
+
+The filename to provide to the input device is the device node
+representing the sndio input device, and is usually set to
+ at file{/dev/audio0}.
+
+For example to grab from @file{/dev/audio0} using @file{ffmpeg} use the
+command:
+ at example
+ffmpeg -f sndio -i /dev/audio0 /tmp/oss.wav
+ at end example
+
 @section video4linux and video4linux2
 
 Video4Linux and Video4Linux2 input video devices.
Index: doc/outdevs.texi
===================================================================
--- doc/outdevs.texi	(revision 24743)
+++ doc/outdevs.texi	(working copy)
@@ -26,4 +26,8 @@
 
 OSS (Open Sound System) output device.
 
+ at section sndio
+
+sndio audio output device.
+
 @c man end OUTPUT DEVICES
Index: libavdevice/sndio-common.c
===================================================================
--- libavdevice/sndio-common.c	(revision 0)
+++ libavdevice/sndio-common.c	(revision 0)
@@ -0,0 +1,112 @@
+/*
+ * sndio play and grab interface
+ * Copyright (c) 2010 Jacob Meuser
+ *
+ * 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 <sndio.h>
+#include "libavformat/avformat.h"
+
+#include "sndio-common.h"
+
+static void movecb(void *addr, int delta)
+{
+    AudioData *s = addr;
+
+    s->hwpos += delta * s->channels * s->bps;
+}
+
+av_cold int ff_sndio_open(AVFormatContext *s1, int is_output,
+                       const char *audio_device)
+{
+    AudioData *s = s1->priv_data;
+    struct sio_hdl *hdl = NULL;
+    struct sio_par par;
+
+    hdl = sio_open(audio_device, is_output ? SIO_PLAY : SIO_REC, 0);
+    if (!hdl) {
+        av_log(s1, AV_LOG_ERROR, "could not open sndio device\n");
+        return AVERROR(EIO);
+    }
+
+    sio_initpar(&par);
+
+    par.bits = 16;
+    par.sig  = 1;
+    par.le   = SIO_LE_NATIVE;
+
+    if (is_output)
+        par.pchan = s->channels;
+    else
+        par.rchan = s->channels;
+    par.rate = s->sample_rate;
+
+    if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
+        av_log(s1, AV_LOG_ERROR, "impossible to set sndio parameters\n");
+        goto fail;
+    }
+
+    if (par.bits != 16 || par.sig != 1 || par.le != SIO_LE_NATIVE ||
+        (is_output  && (par.pchan != s->channels)) ||
+        (!is_output && (par.rchan != s->channels)) ||
+        (par.rate != s->sample_rate)) {
+        av_log(s1, AV_LOG_ERROR, "could not set appropriate sndio parameters\n");
+        goto fail;
+    }
+
+    s->buffer_size = par.round * par.bps *
+                     (is_output ? par.pchan : par.rchan);
+
+    s->buffer = av_malloc(s->buffer_size);
+    if (!s->buffer) {
+        av_log(s1, AV_LOG_ERROR, "could not allocate buffer\n");
+        goto fail;
+    }
+
+    s->codec_id    = par.le ? CODEC_ID_PCM_S16LE : CODEC_ID_PCM_S16BE;
+    s->channels    = is_output ? par.pchan : par.rchan;
+    s->sample_rate = par.rate;
+    s->bps         = par.bps;
+
+    sio_onmove(hdl, movecb, s);
+
+    if (!sio_start(hdl)) {
+        av_log(s1, AV_LOG_ERROR, "could not start sndio\n");
+        goto fail;
+    }
+
+    s->hdl = hdl;
+
+    return 0;
+
+fail:
+    if (s->buffer)
+        av_free(s->buffer);
+    if (hdl)
+        sio_close(hdl);
+    return AVERROR(EIO);
+}
+
+int ff_sndio_close(AudioData *s)
+{
+    if (s->buffer)
+        av_free(s->buffer);
+    if (s->hdl)
+        sio_close(s->hdl);
+    return 0;
+}
Index: libavdevice/sndio-dec.c
===================================================================
--- libavdevice/sndio-dec.c	(revision 0)
+++ libavdevice/sndio-dec.c	(revision 0)
@@ -0,0 +1,105 @@
+/*
+ * sndio play and grab interface
+ * Copyright (c) 2010 Jacob Meuser
+ *
+ * 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 <sndio.h>
+#include "libavformat/avformat.h"
+
+#include "sndio-common.h"
+
+static av_cold int audio_read_header(AVFormatContext *s1,
+                                     AVFormatParameters *ap)
+{
+    AudioData *s = s1->priv_data;
+    AVStream *st;
+    int ret;
+
+    if (ap->sample_rate <= 0 || ap->channels <= 0)
+        return AVERROR(EINVAL);
+
+    st = av_new_stream(s1, 0);
+    if (!st)
+        return AVERROR(ENOMEM);
+
+    s->sample_rate = ap->sample_rate;
+    s->channels    = ap->channels;
+
+    ret = ff_sndio_open(s1, 0, s1->filename);
+    if (ret < 0)
+        return AVERROR(EIO);
+
+    /* take real parameters */
+    st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
+    st->codec->codec_id    = s->codec_id;
+    st->codec->sample_rate = s->sample_rate;
+    st->codec->channels    = s->channels;
+
+    av_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
+    return 0;
+}
+
+static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
+{
+    AudioData *s = s1->priv_data;
+    int ret, bdelay;
+    int64_t cur_time;
+
+    if ((ret=av_new_packet(pkt, s->buffer_size)) < 0)
+        return ret;
+
+    ret = sio_read(s->hdl, pkt->data, pkt->size);
+    if (ret == 0 || sio_eof(s->hdl)) {
+        av_free_packet(pkt);
+        pkt->size = 0;
+        return AVERROR_EOF;
+    }
+
+    pkt->size   = ret;
+    s->softpos += ret;
+
+    /* compute pts of the start of the packet */
+    cur_time = av_gettime();
+
+    bdelay = ret + s->hwpos - s->softpos;
+
+    /* convert to wanted units */
+    pkt->pts = cur_time;
+
+    return 0;
+}
+
+static av_cold int audio_read_close(AVFormatContext *s1)
+{
+    AudioData *s = s1->priv_data;
+
+    ff_sndio_close(s);
+    return 0;
+}
+
+AVInputFormat sndio_demuxer = {
+    "sndio",
+    NULL_IF_CONFIG_SMALL("sndio audio capture"),
+    sizeof(AudioData),
+    NULL,
+    audio_read_header,
+    audio_read_packet,
+    audio_read_close,
+    .flags = AVFMT_NOFILE,
+};
Index: libavdevice/sndio-common.h
===================================================================
--- libavdevice/sndio-common.h	(revision 0)
+++ libavdevice/sndio-common.h	(revision 0)
@@ -0,0 +1,53 @@
+/*
+ * sndio play and grab interface
+ * Copyright (c) 2010 Jacob Meuser
+ *
+ * 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
+ */
+
+#ifndef AVDEVICE_SNDIO_COMMON_H
+#define AVDEVICE_SNDIO_COMMON_H
+
+#include <sndio.h>
+#include "config.h"
+#include "libavformat/avformat.h"
+
+/* XXX: we make the assumption that the soundcard accepts this format */
+/* XXX: find better solution with "preinit" method, needed also in
+        other formats */
+#if HAVE_BIGENDIAN
+#define DEFAULT_CODEC_ID CODEC_ID_PCM_S16BE
+#else
+#define DEFAULT_CODEC_ID CODEC_ID_PCM_S16LE
+#endif
+
+typedef struct {
+    struct sio_hdl *hdl;
+    int sample_rate;
+    int channels;
+    int bps;
+    enum CodecID codec_id;
+    int buffer_size;
+    uint8_t *buffer;
+    int buffer_ptr;
+    long long hwpos, softpos;
+} AudioData;
+
+int ff_sndio_open(AVFormatContext *, int, const char *);
+int ff_sndio_close(AudioData *);
+
+#endif /* AVDEVICE_SNDIO_COMMON_H */
Index: libavdevice/alldevices.c
===================================================================
--- libavdevice/alldevices.c	(revision 24743)
+++ libavdevice/alldevices.c	(working copy)
@@ -44,6 +44,7 @@
     REGISTER_INDEV    (DV1394, dv1394);
     REGISTER_INDEV    (JACK, jack);
     REGISTER_INOUTDEV (OSS, oss);
+    REGISTER_INOUTDEV (SNDIO, sndio);
     REGISTER_INDEV    (V4L2, v4l2);
     REGISTER_INDEV    (V4L, v4l);
     REGISTER_INDEV    (VFWCAP, vfwcap);
Index: libavdevice/sndio-enc.c
===================================================================
--- libavdevice/sndio-enc.c	(revision 0)
+++ libavdevice/sndio-enc.c	(revision 0)
@@ -0,0 +1,90 @@
+/*
+ * sndio play and grab interface
+ * Copyright (c) 2010 Jacob Meuser
+ *
+ * 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 <sndio.h>
+#include "libavformat/avformat.h"
+
+#include "sndio-common.h"
+
+static av_cold int audio_write_header(AVFormatContext *s1)
+{
+    AudioData *s = s1->priv_data;
+    AVStream *st;
+    int ret;
+
+    st             = s1->streams[0];
+    s->sample_rate = st->codec->sample_rate;
+    s->channels    = st->codec->channels;
+
+    ret = ff_sndio_open(s1, 1, s1->filename);
+    if (ret < 0)
+        return AVERROR(EIO);
+    else
+        return 0;
+}
+
+static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
+{
+    AudioData *s = s1->priv_data;
+    int len, ret;
+    int size = pkt->size;
+    uint8_t *buf= pkt->data;
+
+    while (size > 0) {
+        len = s->buffer_size - s->buffer_ptr;
+        if (len > size)
+            len = size;
+        memcpy(s->buffer + s->buffer_ptr, buf, len);
+        buf += len;
+        size -= len;
+        s->buffer_ptr += len;
+        if (s->buffer_ptr >= s->buffer_size) {
+            ret = sio_write(s->hdl, s->buffer, s->buffer_size);
+            if (ret == 0 || sio_eof(s->hdl))
+                return AVERROR(EIO);
+            s->softpos += ret;
+            s->buffer_ptr = 0;
+        }
+    }
+    return 0;
+}
+
+static int audio_write_trailer(AVFormatContext *s1)
+{
+    AudioData *s = s1->priv_data;
+
+    ff_sndio_close(s);
+    return 0;
+}
+
+AVOutputFormat sndio_muxer = {
+    "sndio",
+    NULL_IF_CONFIG_SMALL("sndio audio playback"),
+    "",
+    "",
+    sizeof(AudioData),
+    DEFAULT_CODEC_ID,
+    CODEC_ID_NONE,
+    audio_write_header,
+    audio_write_packet,
+    audio_write_trailer,
+    .flags = AVFMT_NOFILE,
+};
Index: libavdevice/avdevice.h
===================================================================
--- libavdevice/avdevice.h	(revision 24743)
+++ libavdevice/avdevice.h	(working copy)
@@ -22,7 +22,7 @@
 #include "libavutil/avutil.h"
 
 #define LIBAVDEVICE_VERSION_MAJOR 52
-#define LIBAVDEVICE_VERSION_MINOR  2
+#define LIBAVDEVICE_VERSION_MINOR  3
 #define LIBAVDEVICE_VERSION_MICRO  1
 
 #define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \
Index: libavdevice/Makefile
===================================================================
--- libavdevice/Makefile	(revision 24743)
+++ libavdevice/Makefile	(working copy)
@@ -17,6 +17,10 @@
 OBJS-$(CONFIG_JACK_INDEV)                += jack_audio.o
 OBJS-$(CONFIG_OSS_INDEV)                 += oss_audio.o
 OBJS-$(CONFIG_OSS_OUTDEV)                += oss_audio.o
+OBJS-$(CONFIG_SNDIO_INDEV)               += sndio-common.o \
+                                            sndio-dec.o
+OBJS-$(CONFIG_SNDIO_OUTDEV)              += sndio-common.o \
+                                            sndio-enc.o
 OBJS-$(CONFIG_V4L2_INDEV)                += v4l2.o
 OBJS-$(CONFIG_V4L_INDEV)                 += v4l.o
 OBJS-$(CONFIG_VFWCAP_INDEV)              += vfwcap.o
@@ -26,5 +30,6 @@
 OBJS-$(CONFIG_LIBDC1394_INDEV)           += libdc1394.o
 
 SKIPHEADERS-$(HAVE_ALSA_ASOUNDLIB_H)     += alsa-audio.h
+SKIPHEADERS-$(HAVE_SNDIO_H)              += sndio-common.h
 
 include $(SUBDIR)../subdir.mak

-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.




More information about the ffmpeg-devel mailing list