[PATCH] OpenAL 1.1 Capture Support
I'm not sure if I'm doing it right, but I plan to make my first contribution to any open source project right now. Attached is a patch which adds an OpenAL 1.1 audio capture input device to libavdevice. This means that audio capture in ffmpeg should now be possible on any platform with a working OpenAL 1.1 implementation. Right now, there are three major OpenAL implementations: OpenAL Soft Portable software implementation with multiple backends, currently ALSA, OSS, PulseAudio, Solaris, DirectSound, PortAudio. LGPL. Creative Creative Labs' official implementation for Windows with hardware acceleration and software fallback. Proprietary. Apple Implementation included with recent versions of Mac OS X. So far, I've only tested it on Kubuntu Linux 11.04 using the OpenAL Soft implementation, and it works great. But it should work on Windows and Mac OS X too. I'll test it on Windows if I have to (I don't have a Mac). What this code does NOT do is provide an output device for playing audio. OpenAL is more or less overkill for this anyways, as it provides a 3D audio environment. Please tell me what I can do to improve the code, after all, I am only a high school student with too much time on my hands ;-)
Hi, Right now, there are three major OpenAL implementations
Their is also ASUS (or C-Media - not sure) proprietary implementation ;). --- a/configure
+++ b/configure @@ -1470,6 +1470,8 @@ dv1394_indev_deps="dv1394 dv_demuxer" fbdev_indev_deps="linux_fb_h" jack_indev_deps="jack_jack_h sem_timedwait" libdc1394_indev_deps="libdc1394" +openal_indev_deps="AL_al_h AL_alc_h" +openal_indev_extralibs="-lopenal" oss_indev_deps_any="soundcard_h sys_soundcard_h" oss_outdev_deps_any="soundcard_h sys_soundcard_h" sdl_outdev_deps="sdl"
On Windows openal library mostly named as "OpenAL32.lib" (nevertheless bitness). ----------------------------------------------- Kirill Gavrilov, Software designer. <kirill@sview.ru>
On date Saturday 2011-06-04 23:50:17 -0400, Jonathan Baldwin encoded:
I'm not sure if I'm doing it right, but I plan to make my first contribution to any open source project right now.
Attached is a patch which adds an OpenAL 1.1 audio capture input device to libavdevice. This means that audio capture in ffmpeg should now be possible on any platform with a working OpenAL 1.1 implementation. Right now, there are three major OpenAL implementations: OpenAL Soft Portable software implementation with multiple backends, currently ALSA, OSS, PulseAudio, Solaris, DirectSound, PortAudio. LGPL. Creative Creative Labs' official implementation for Windows with hardware acceleration and software fallback. Proprietary. Apple Implementation included with recent versions of Mac OS X.
So far, I've only tested it on Kubuntu Linux 11.04 using the OpenAL Soft implementation, and it works great. But it should work on Windows and Mac OS X too. I'll test it on Windows if I have to (I don't have a Mac).
What this code does NOT do is provide an output device for playing audio. OpenAL is more or less overkill for this anyways, as it provides a 3D audio environment.
Please tell me what I can do to improve the code, after all, I am only a high school student with too much time on my hands ;-)
diff --git a/Changelog b/Changelog index a2018dd..d809be5 100644 --- a/Changelog +++ b/Changelog @@ -21,6 +21,7 @@ version <next>: - split filter added - select filter added - sdl output device added +- openal input device added
version 0.7_beta1: diff --git a/configure b/configure index 62b7e2f..f8077cd 100755 --- a/configure +++ b/configure @@ -1470,6 +1470,8 @@ dv1394_indev_deps="dv1394 dv_demuxer" fbdev_indev_deps="linux_fb_h" jack_indev_deps="jack_jack_h sem_timedwait" libdc1394_indev_deps="libdc1394" +openal_indev_deps="AL_al_h AL_alc_h" +openal_indev_extralibs="-lopenal" oss_indev_deps_any="soundcard_h sys_soundcard_h" oss_outdev_deps_any="soundcard_h sys_soundcard_h" sdl_outdev_deps="sdl" @@ -2970,6 +2972,12 @@ check_header linux/videodev.h check_header linux/videodev2.h check_header sys/videoio.h
+enabled openal_indev && +check_cpp_condition "AL/al.h" "defined(AL_VERSION_1_1)" "" -lopenal && +check_lib2 "AL/al.h" alGetError -lopenal && +check_header "AL/al.h" && +check_header "AL/alc.h" +
I think this could be simplified by using: enabled libopenal && require AL/al.h alGetError -lopenal && { check_cpp_condition AL/al.h "defined(AL_VERSION_1_1)" || die "ERROR: libopenal version must be 1.1"; } ... openal_indev_deps="libopenal"
check_func_headers "windows.h vfw.h" capCreateCaptureWindow "$vfwcap_indev_extralibs" # check that WM_CAP_DRIVER_CONNECT is defined to the proper value # w32api 3.12 had it defined wrong diff --git a/doc/indevs.texi b/doc/indevs.texi index 0487108..c946867 100644 --- a/doc/indevs.texi +++ b/doc/indevs.texi @@ -137,6 +137,47 @@ For more information read:
IIDC1394 input device, based on libdc1394 and libraw1394.
+@section openal + +The OpenAL input device provides audio capture on all systems with a +working OpenAL 1.1 implementation. + +To enable this input device during configuration, you will need the +OpenAL headers and libraries. These are provided by the OpenAL Soft +implementation and as part of an SDK from Creative Labs. + +To capture from a specific sound device, provide the OpenAL name of +that device as the filename: +@example
+$> ffmpeg -f openal -i 'DR-BT101 via PulseAudio' /tmp/out.ogg
"$" alone should be more clear.
+@end example +Note that the device names used are different for each OpenAL +implementation. OpenAL Soft provides a tool called openal-info which
nit: @file{openal-info}
+can be used to list the devices on a system. + +Or, just pass a blank string as the filename to use the default +device: +@example +$> ffmpeg -f openal -i '' /tmp/out.ogg +@end example + +Some OpenAL implementations (such as OpenAL Soft) support capturing +from two devices simultaneously within a single process: +@example +$> ffmpeg -f openal -i 'DR-BT101 via PulseAudio' /tmp/out1.ogg -f openal -i 'ALSA Default' /tmp/out2.ogg +@end example
+If this doesn't work (crashes, duplicate audio, or worse) it is your +OpenAL implementation; try using the latest version of OpenAL Soft
it is your OpenAL implementation => it may be fault of your OpenAL implemention since we're no perfect.
+and see if that works. + +Links: +@itemize +@item +Creative Labs OpenAL Homepage @url{http://openal.org} +@item +OpenAL Soft @url{http://kcat.strangesoft.net/openal.html} +@end itemize + @section oss
Open Sound System input device. diff --git a/libavdevice/Makefile b/libavdevice/Makefile index 60103a4..4d3c1ae 100644 --- a/libavdevice/Makefile +++ b/libavdevice/Makefile @@ -19,6 +19,7 @@ OBJS-$(CONFIG_DSHOW_INDEV) += dshow.o dshow_enummediatypes.o \ OBJS-$(CONFIG_DV1394_INDEV) += dv1394.o OBJS-$(CONFIG_FBDEV_INDEV) += fbdev.o OBJS-$(CONFIG_JACK_INDEV) += jack_audio.o +OBJS-$(CONFIG_OPENAL_INDEV) += openal-dec.o OBJS-$(CONFIG_OSS_INDEV) += oss_audio.o OBJS-$(CONFIG_OSS_OUTDEV) += oss_audio.o OBJS-$(CONFIG_SDL_OUTDEV) += sdl.o diff --git a/libavdevice/alldevices.c b/libavdevice/alldevices.c index 7846704..ef302d7 100644 --- a/libavdevice/alldevices.c +++ b/libavdevice/alldevices.c @@ -44,6 +44,7 @@ void avdevice_register_all(void) REGISTER_INDEV (DV1394, dv1394); REGISTER_INDEV (FBDEV, fbdev); REGISTER_INDEV (JACK, jack); + REGISTER_INDEV (OPENAL, openal); REGISTER_INOUTDEV (OSS, oss); REGISTER_OUTDEV (SDL, sdl); REGISTER_INOUTDEV (SNDIO, sndio); diff --git a/libavdevice/avdevice.h b/libavdevice/avdevice.h index be56be4..7cb8f54 100644 --- a/libavdevice/avdevice.h +++ b/libavdevice/avdevice.h @@ -23,7 +23,7 @@ #include "libavformat/avformat.h"
#define LIBAVDEVICE_VERSION_MAJOR 53 -#define LIBAVDEVICE_VERSION_MINOR 1 +#define LIBAVDEVICE_VERSION_MINOR 2 #define LIBAVDEVICE_VERSION_MICRO 0
#define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \ diff --git a/libavdevice/openal-dec.c b/libavdevice/openal-dec.c new file mode 100644 index 0000000..5c3eeee --- /dev/null +++ b/libavdevice/openal-dec.c @@ -0,0 +1,269 @@ +/*
+ * OpenAL 1.1 capture device for libavdevice
Nit++: create a dedication /** @file ... */ doxy for this
+ * Copyright (c) 2011 Jonathan Baldwin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE.
OK, if you want this to be released as public domain (rather than the standard LGPL adopted by most FFmpeg codebase).
+ */ + +#include "libavformat/avformat.h" +#include "libavutil/opt.h" +#include <AL/al.h> +#include <AL/alc.h> + +#define SSIZE 2048 + +typedef struct +{
Style nits: * "typedef struct {" on the same line * if (foo) { on the same line, (foo) is preferred over ( foo ) * switch (foo) { on the same line
+ ALCdevice *device; + + ALCchar buffer[32768]; + ALCint buffer_capacity; + ALCint sample_size; + + int channels, sample_rate; + ALCenum sample_format; +} al_data; +
+/** + * Gets the corresponding codecid for an AL_FORMAT. + */ +static inline enum CodecID al_format_codecid ( ALCenum alf ) +{ + switch ( alf ) + { + case AL_FORMAT_MONO8: + case AL_FORMAT_STEREO8: + return CODEC_ID_PCM_S8; + case AL_FORMAT_MONO16: + case AL_FORMAT_STEREO16:
+#if HAVE_BIGENDIAN + return CODEC_ID_PCM_S16BE; +#else + return CODEC_ID_PCM_S16LE; +#endif
AV_NE(CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE),
+ } + return CODEC_ID_NONE; +} + +/** + * Gets the number of channels for an AL_FORMAT. + */ +static inline int al_format_channels ( ALCenum alf ) +{ + switch ( alf ) + { + case AL_FORMAT_MONO8: + case AL_FORMAT_MONO16: + return 1; + case AL_FORMAT_STEREO8: + case AL_FORMAT_STEREO16: + return 2; + } + return 0; +} + +/** + * Gets the sample size for an AL_FORMAT. + */ +static inline int al_format_sample_size ( ALCenum alf ) +{ + switch ( alf ) + { + case AL_FORMAT_MONO8: + return 1; + case AL_FORMAT_MONO16: + case AL_FORMAT_STEREO8: + return 2; + case AL_FORMAT_STEREO16: + return 4; + } + return 0; +}
This information can be stored in a table struct al_format_entry { ALCenum al_fmt, enum AVSampleFormat sample_fmt; int nb_channels; int sample_size; } al_format_entries[] = { ... } sample_size can be computed from nb_channels and sample_fmt by using av_get_bits_per_sample_fmt().
+ +/** + * Get the OpenAL error state.
+ * Returns true if the device has an error, false otherwise.
@return
+ */ +static inline int al_get_error ( ALCdevice *device, int *error_ret, const char **error_message_ret )
Maybe you can directly return the error code, and somehow simplify the interface.
+{ + ALCenum error = alcGetError ( device ); + switch ( error ) + { + case ALC_NO_ERROR: + return 0; + case ALC_INVALID_DEVICE: + *error_ret = AVERROR ( ENODEV ); + break; + case ALC_INVALID_CONTEXT: + case ALC_INVALID_ENUM: + case ALC_INVALID_VALUE: + *error_ret = AVERROR ( EINVAL ); + break; + case ALC_OUT_OF_MEMORY: + *error_ret = AVERROR ( ENOMEM ); + break; + default: + *error_ret = AVERROR ( ~0 ); + } + *error_message_ret = alcGetString ( device, error ); + return *error_ret; +} + +/** + * Sets up the audio device and related data. + */ +static int read_header ( AVFormatContext* ctx, AVFormatParameters *ap ) +{ + al_data *ad = ctx->priv_data; + int error=0; + const char *error_message=0;
+ AVStream *st = 0; + AVCodecContext *codec = 0;
NULL is preferred over 0 for struct pointers.
+ + ad->sample_format = ( ad->channels==1 ) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; +
+#if 0 && FF_API_FORMAT_PARAMETERS + if ( ap->sample_rate ) + ad->sample_rate = ap->sample_rate; + if ( ap->channels ) + ad->sample_format = ( ap->channels==1 ) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; +#endif
maybe this can be safely skipped
+ + /* Open device for capture */ + ad->device = alcCaptureOpenDevice ( + ctx->filename[0] ? ctx->filename : 0, + ad->sample_rate, + ad->sample_format, + SSIZE + ); + + /* Did we fail? */ + if ( al_get_error ( ad->device, &error, &error_message ) ) goto fail1; + + /* Create stream */ + if ( ! ( st=av_new_stream ( ctx, 0 ) ) ) + { + error=AVERROR ( ENOMEM ); + goto fail2; + } + + /* We work in microseconds */ + av_set_pts_info ( st, 64, 1, 1000000 ); + + /* Set codec parameters */ + codec = st->codec; + codec->codec_type = AVMEDIA_TYPE_AUDIO; + codec->sample_rate = ad->sample_rate; + codec->channels = al_format_channels ( ad->sample_format ); + codec->codec_id = al_format_codecid ( ad->sample_format ); + + /* These are needed to read the audio data */ + ad->sample_size = al_format_sample_size ( ad->sample_format ); + ad->buffer_capacity = sizeof ( ad->buffer ) /ad->sample_size; + + /* Finally, start the capture process */ + alcCaptureStart ( ad->device ); + + return 0; + + /* Handle faliure */ +fail2: + alcCaptureCloseDevice ( ad->device ); +fail1: + if ( error_message ) + av_log ( ctx, AV_LOG_ERROR, "OpenAL Error: %s\n", error_message ); + return error; +} + +/** + * Read audio data and store it in a packet. + */ +static int read_packet ( AVFormatContext* ctx, AVPacket *pkt ) +{ + al_data *ad = ctx->priv_data; + int error=0; + const char *error_message=0; + ALCint num_samples; + + /* Read samples from device... */ + alcGetIntegerv ( ad->device, ALC_CAPTURE_SAMPLES, ( ALCsizei ) sizeof ( ALCint ), &num_samples ); + if ( al_get_error ( ad->device, &error, &error_message ) ) goto fail; + + if ( num_samples > ad->buffer_capacity ) + num_samples = ad->buffer_capacity; + alcCaptureSamples ( ad->device, ad->buffer, num_samples ); + if ( al_get_error ( ad->device, &error, &error_message ) ) goto fail; + + /* ...and use them to fill a packet */ + av_init_packet ( pkt ); + pkt->data = ad->buffer; + pkt->size = num_samples*ad->sample_size; + pkt->pts = av_gettime(); + + return pkt->size; +fail: + /* Handle failure */ + if ( error_message ) + av_log ( ctx, AV_LOG_ERROR, "OpenAL Error: %s\n", error_message ); + return error; +} +
+/** + * Shuts down the audio device and related data. + */
Nit: third person, also no need to document standard callback imo.
+static int read_close ( AVFormatContext* ctx ) +{ + al_data *ad = ctx->priv_data; + + if ( ad->device ) + { + alcCaptureStop ( ad->device ); + alcCaptureCloseDevice ( ad->device ); + } + return 0; +} + +static const AVOption options[] = +{ + {"channels", "", offsetof ( al_data, channels ), FF_OPT_TYPE_INT, {.dbl=2}, 1, 2, AV_OPT_FLAG_DECODING_PARAM }, + {"sample_rate", "", offsetof ( al_data, sample_rate ), FF_OPT_TYPE_INT, {.dbl=44100}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, + {NULL}, +}; + +static const AVClass class = +{ + .class_name = "openal", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT +}; + +AVInputFormat ff_openal_demuxer = +{ + "openal", + .long_name = + NULL_IF_CONFIG_SMALL ( "Capture using OpenAL" ), + .priv_data_size = sizeof ( al_data ), + .read_probe = NULL, + .read_header = read_header, + .read_packet = read_packet, + .read_close = read_close, + .flags = AVFMT_NOFILE, + .priv_class = &class +};
Thanks for the patch. -- FFmpeg = Friendly & Fostering Most Ponderous Exciting Gargoyle
On Windows openal library mostly named as "OpenAL32.lib" The environment variable OPENAL_LIBS is now used for OpenAL library-related flags if it exists, otherwise -lopenal is used. Windows builders can set the environment variable appropriately.
I think this could be simplified by using: enabled libopenal && require AL/al.h alGetError -lopenal && { check_cpp_condition AL/al.h "defined(AL_VERSION_1_1)" || die "ERROR: libopenal version must be 1.1"; } ... openal_indev_deps="libopenal"
Added "openal" to CONFIG_LIST, added --enable-openal switch to show_help, added echo "openal enabled ${openal-no}" to where all the other ones are, changed openal_indev_deps so openal is the sole dep, removed "enabled openal_indev && ..." cruft, added something similar to above but with openal instead of libopenal.
"$" alone should be more clear.
Done.
nit: @file{openal-info}
Done.
it is your OpenAL implementation => it may be fault of your OpenAL implemention
since we're no perfect.
Added the word "probably" ;-)
Nit++: create a dedication /** @file ... */ doxy for this
Done at top of file.
OK, if you want this to be released as public domain (rather than the standard LGPL adopted by most FFmpeg codebase).
I prefer that the freedom of my code is enforced by moral rather than legal matters.
Style nits: * "typedef struct {" on the same line * if (foo) { on the same line, (foo) is preferred over ( foo ) * switch (foo) { on the same line
I prefer that style too, but my IDE disagreed with me. I changed its settings so now everything should be fine. Fixed.
AV_NE(CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE),
I knew AV_NE existed but forgot its name. I tried grepping for ENDIAN but couldn't find it. Fixed.
This information can be stored in a table
struct al_format_entry { ALCenum al_fmt, enum AVSampleFormat sample_fmt; int nb_channels; int sample_size; } al_format_entries[] = { ... }
sample_size can be computed from nb_channels and sample_fmt by using av_get_bits_per_sample_fmt().
Added a lookup table encapsulated in an inline get_ function, removed old al_*( alf ) crufty functions.
Maybe you can directly return the error code, and somehow simplify the interface.
Done. I hope.
NULL is preferred over 0 for struct pointers.
Done.
maybe this can be safely skipped
That block of code is now removed.
Nit: third person, also no need to document standard callback imo.
Documentation for standard read_* callbacks removed.
Thanks for the patch. --
My pleasure. An updated patch is attached.
On date Sunday 2011-06-05 17:05:28 -0400, Jonathan Baldwin encoded:
On Windows openal library mostly named as "OpenAL32.lib" The environment variable OPENAL_LIBS is now used for OpenAL library-related flags if it exists, otherwise -lopenal is used. Windows builders can set the environment variable appropriately. [...] My pleasure. An updated patch is attached.
diff --git a/Changelog b/Changelog index a2018dd..d809be5 100644 --- a/Changelog +++ b/Changelog @@ -21,6 +21,7 @@ version <next>: - split filter added - select filter added - sdl output device added +- openal input device added
version 0.7_beta1: diff --git a/configure b/configure index 62b7e2f..88458d1 100755 --- a/configure +++ b/configure @@ -188,6 +188,7 @@ External library support: --enable-libxavs enable AVS encoding via xavs [no] --enable-libxvid enable Xvid encoding via xvidcore, native MPEG-4/Xvid encoder exists [no] + --enable-openal enable OpenAL 1.1 capture support [no] --enable-mlib enable Sun medialib [no] --enable-zlib enable zlib [autodetect]
@@ -959,6 +960,7 @@ CONFIG_LIST=" mpegaudiodsp network nonfree + openal pic postproc rdft @@ -1470,6 +1472,8 @@ dv1394_indev_deps="dv1394 dv_demuxer" fbdev_indev_deps="linux_fb_h" jack_indev_deps="jack_jack_h sem_timedwait" libdc1394_indev_deps="libdc1394" +openal_indev_deps="openal"
+openal_indev_extralibs="${OPENAL_LIBS-'-lopenal'}"
is this required?...
oss_indev_deps_any="soundcard_h sys_soundcard_h" oss_outdev_deps_any="soundcard_h sys_soundcard_h" sdl_outdev_deps="sdl" @@ -2944,6 +2948,9 @@ enabled libx264 && require libx264 x264.h x264_encoder_encode -lx264 && die "ERROR: libx264 version must be >= 0.115."; } enabled libxavs && require libxavs xavs.h xavs_encoder_encode -lxavs enabled libxvid && require libxvid xvid.h xvid_global -lxvidcore
+enabled openal && require openal 'AL/al.h' alGetError ${OPENAL_LIBS-'-lopenal'} && ^^^^^^^^^^^^^^^^^^^^^^^^^
...isn't this enough?
+ { check_cpp_condition "AL/al.h" "defined(AL_VERSION_1_1)" || + die "ERROR: openal version must be 1.1 or compatible"; } enabled mlib && require mediaLib mlib_types.h mlib_VectorSub_S16_U8_Mod -lmlib
SDL_CONFIG="${cross_prefix}sdl-config" @@ -3233,6 +3240,7 @@ echo "libvpx enabled ${libvpx-no}" echo "libx264 enabled ${libx264-no}" echo "libxavs enabled ${libxavs-no}" echo "libxvid enabled ${libxvid-no}" +echo "openal enabled ${openal-no}" echo "zlib enabled ${zlib-no}" echo "bzlib enabled ${bzlib-no}" echo diff --git a/doc/indevs.texi b/doc/indevs.texi index 0487108..427c6da 100644 --- a/doc/indevs.texi +++ b/doc/indevs.texi @@ -137,6 +137,48 @@ For more information read:
IIDC1394 input device, based on libdc1394 and libraw1394.
+@section openal + +The OpenAL input device provides audio capture on all systems with a +working OpenAL 1.1 implementation. + +To enable this input device during configuration, you will need the +OpenAL headers and libraries. These are provided by the OpenAL Soft +implementation and as part of an SDK from Creative Labs. +
+To capture from a specific sound device, provide the OpenAL name of +that device as the filename: +@example +$ ffmpeg -f openal -i 'DR-BT101 via PulseAudio' /tmp/out.ogg +@end example +Note that the device names used are different for each OpenAL +implementation. OpenAL Soft provides a tool called +@file{openal-info} which can be used to list the devices on +a system. + +Or, just pass a blank string as the filename to use the default +device: +@example +$ ffmpeg -f openal -i '' /tmp/out.ogg +@end example + +Some OpenAL implementations (such as OpenAL Soft) support capturing +from two devices simultaneously within a single process: +@example +$ ffmpeg -f openal -i 'DR-BT101 via PulseAudio' /tmp/out1.ogg -f openal -i 'ALSA Default' /tmp/out2.ogg +@end example +If this doesn't work (crashes, duplicate audio, or worse) it is +probably your OpenAL implementation; try using the latest version +of OpenAL Soft and see if that works.
OK, maybe we could prefer plain filenames like out.ogg, /tmp/x.ogg looks very UNIX-ish, you may alienate some Windows user ;-).
+ +Links: +@itemize +@item +Creative Labs OpenAL Homepage @url{http://openal.org} +@item +OpenAL Soft @url{http://kcat.strangesoft.net/openal.html} +@end itemize + @section oss
Open Sound System input device. diff --git a/libavdevice/Makefile b/libavdevice/Makefile index 60103a4..4d3c1ae 100644 --- a/libavdevice/Makefile +++ b/libavdevice/Makefile @@ -19,6 +19,7 @@ OBJS-$(CONFIG_DSHOW_INDEV) += dshow.o dshow_enummediatypes.o \ OBJS-$(CONFIG_DV1394_INDEV) += dv1394.o OBJS-$(CONFIG_FBDEV_INDEV) += fbdev.o OBJS-$(CONFIG_JACK_INDEV) += jack_audio.o +OBJS-$(CONFIG_OPENAL_INDEV) += openal-dec.o OBJS-$(CONFIG_OSS_INDEV) += oss_audio.o OBJS-$(CONFIG_OSS_OUTDEV) += oss_audio.o OBJS-$(CONFIG_SDL_OUTDEV) += sdl.o diff --git a/libavdevice/alldevices.c b/libavdevice/alldevices.c index 7846704..ef302d7 100644 --- a/libavdevice/alldevices.c +++ b/libavdevice/alldevices.c @@ -44,6 +44,7 @@ void avdevice_register_all(void) REGISTER_INDEV (DV1394, dv1394); REGISTER_INDEV (FBDEV, fbdev); REGISTER_INDEV (JACK, jack); + REGISTER_INDEV (OPENAL, openal); REGISTER_INOUTDEV (OSS, oss); REGISTER_OUTDEV (SDL, sdl); REGISTER_INOUTDEV (SNDIO, sndio); diff --git a/libavdevice/avdevice.h b/libavdevice/avdevice.h index be56be4..7cb8f54 100644 --- a/libavdevice/avdevice.h +++ b/libavdevice/avdevice.h @@ -23,7 +23,7 @@ #include "libavformat/avformat.h"
#define LIBAVDEVICE_VERSION_MAJOR 53 -#define LIBAVDEVICE_VERSION_MINOR 1 +#define LIBAVDEVICE_VERSION_MINOR 2 #define LIBAVDEVICE_VERSION_MICRO 0
#define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \ diff --git a/libavdevice/openal-dec.c b/libavdevice/openal-dec.c new file mode 100644 index 0000000..1885c5c --- /dev/null +++ b/libavdevice/openal-dec.c @@ -0,0 +1,210 @@ +/** @file openal-dec.c OpenAL 1.1 capture device for libavdevice **/ +/* + * Copyright (c) 2011 Jonathan Baldwin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "libavformat/avformat.h" +#include "libavutil/opt.h" +#include <AL/al.h> +#include <AL/alc.h> + +#define SSIZE 2048 + +typedef struct { + ALCdevice *device; + + ALCchar buffer[32768]; + ALCint buffer_capacity; + ALCint sample_size; + + int channels, sample_rate; + ALCenum sample_format; +} al_data; + +typedef struct { + ALCenum al_fmt; + enum CodecID codec_id; + int channels; + int sample_size; +} al_format_info; + +//Preprocessor magic to find the lowest AL_FORMAT value
+#define FMIN_(a,b) ((a<b) ? a : b)
This is FFMIN defined in libavutil.
+#define FMIN_2(a,b) FMIN_(a,b)
Is this required?
+#define FMIN_4(a,b,c,d) FMIN_( FMIN_2(a,b), FMIN_2(c,d) ) +#define LOWEST_AL_FORMAT FMIN_4(AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, AL_FORMAT_STEREO16)
What about using FFMIN(FFMIN(a,b), FFMIN(c,d))?
+ +/** + * Get information about an AL_FORMAT value. + * @return A pointer to a structure containing information about the AL_FORMAT value. + */
+static inline al_format_info* get_al_format_info (ALCenum al_fmt) { + static al_format_info info_table[] = { + [AL_FORMAT_MONO8-LOWEST_AL_FORMAT] = + {AL_FORMAT_MONO8, CODEC_ID_PCM_U8, 1, 1}, + [AL_FORMAT_MONO16-LOWEST_AL_FORMAT] = + {AL_FORMAT_MONO16, AV_NE (CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE), 1, 2}, + [AL_FORMAT_STEREO8-LOWEST_AL_FORMAT] = + {AL_FORMAT_STEREO8, CODEC_ID_PCM_U8, 2, 2}, + [AL_FORMAT_STEREO16-LOWEST_AL_FORMAT] = + {AL_FORMAT_STEREO16, AV_NE (CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE), 2, 4}, + };
You may put each entry on a separate line, and vertically align fields.
+ + return &info_table[al_fmt-LOWEST_AL_FORMAT]; +} + +/** + * Get the OpenAL error code, translated into an av/errno error code. + * @return The error code, or 0 if there is no error. + */ +static inline int al_get_error (ALCdevice *device) { + ALCenum error = alcGetError (device); + switch (error) { + case ALC_NO_ERROR: + return 0; + case ALC_INVALID_DEVICE: + return AVERROR (ENODEV); + break; + case ALC_INVALID_CONTEXT: + case ALC_INVALID_ENUM: + case ALC_INVALID_VALUE: + return AVERROR (EINVAL); + break; + case ALC_OUT_OF_MEMORY: + return AVERROR (ENOMEM); + break;
+ default: + return AVERROR (INT_MAX);
return a generic AVERROR(EIO) or AVERROR(EINVAL), AVERROR(INT_MAX) semantics is undefined. Also providing the error string was a nice feature.
+ } +} + +static int read_header (AVFormatContext* ctx, AVFormatParameters *ap) { + al_data *ad = ctx->priv_data; + int error=0; + AVStream *st = NULL; + AVCodecContext *codec = NULL; + + ad->sample_format = (ad->channels==1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; + + /* Open device for capture */ + ad->device = alcCaptureOpenDevice ( + ctx->filename[0] ? ctx->filename : 0, + ad->sample_rate, + ad->sample_format, + SSIZE + ); + + /* Did we fail? */ + if (error=al_get_error(ad->device)) goto fail1; + + /* Create stream */ + if (! (st=av_new_stream (ctx, 0))) { + error=AVERROR (ENOMEM); + goto fail2; + } + + /* We work in microseconds */ + av_set_pts_info (st, 64, 1, 1000000); + + /* Set codec parameters */ + codec = st->codec; + codec->codec_type = AVMEDIA_TYPE_AUDIO; + codec->sample_rate = ad->sample_rate; + codec->channels = get_al_format_info (ad->sample_format)->channels; + codec->codec_id = get_al_format_info (ad->sample_format)->codec_id; + + /* These are needed to read the audio data */ + ad->sample_size = get_al_format_info (ad->sample_format)->sample_size; + ad->buffer_capacity = sizeof (ad->buffer) /ad->sample_size; + + /* Finally, start the capture process */ + alcCaptureStart (ad->device); + + return 0; +
+ /* Handle faliure */
typo
+fail2: + alcCaptureCloseDevice (ad->device); +fail1: + return error;
alcCaptureOpenDevice() return NULL in case of failure, so you can use just a single "fail" label and do: fail: if (ad->device) alcCaptureCloseDevice(ad->device); return error; Printing the error here as in the previous version is a good idea (improves feedback).
+fail1: + return error;
+} + +static int read_packet (AVFormatContext* ctx, AVPacket *pkt) { + al_data *ad = ctx->priv_data; + int error=0; + ALCint num_samples; + + /* Read samples from device... */ + alcGetIntegerv (ad->device, ALC_CAPTURE_SAMPLES, (ALCsizei) sizeof (ALCint), &num_samples); + if (error=al_get_error (ad->device)) goto fail; + + if (num_samples > ad->buffer_capacity) + num_samples = ad->buffer_capacity; + alcCaptureSamples (ad->device, ad->buffer, num_samples); + if (error=al_get_error (ad->device)) goto fail; + + /* ...and use them to fill a packet */ + av_init_packet (pkt); + pkt->data = ad->buffer; + pkt->size = num_samples*ad->sample_size; + pkt->pts = av_gettime(); + + return pkt->size; +fail: + /* Handle failure */ + return error; +} +
+static int read_close (AVFormatContext* ctx) { + al_data *ad = ctx->priv_data; + + if (ad->device) { + alcCaptureStop (ad->device); + alcCaptureCloseDevice (ad->device); + } + return 0; +}
More style nits: static int read_close(AVFormatContext* ctx) { al_data *ad = ctx->priv_data; if (ad->device) { alcCaptureStop(ad->device); alcCaptureCloseDevice(ad->device); } return 0; } that is: 4 chars of indent, no space between fun_name and params, and "{" on a starting line for functions. No need to address these nits if you don't care, I can do it myself with a few emacs keystrokes before committing.
+ +static const AVOption options[] = { + {"channels", "", offsetof (al_data, channels), FF_OPT_TYPE_INT, {.dbl=2}, 1, 2, AV_OPT_FLAG_DECODING_PARAM }, + {"sample_rate", "", offsetof (al_data, sample_rate), FF_OPT_TYPE_INT, {.dbl=44100}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, + {NULL},
Please mention these options in the manual. See how I did it in the sdl section of the output devices chapter. [...] -- FFmpeg = Fiendish and Fanciful Monstrous Powered Enchanting Gadget
Sorry for the delay, I was kind of busy. On Mon, Jun 6, 2011 at 5:02 AM, Stefano Sabatini <stefano.sabatini-lala@poste.it> wrote:
+openal_indev_extralibs="${OPENAL_LIBS-'-lopenal'}"
is this required?...
+enabled openal && require openal 'AL/al.h' alGetError ${OPENAL_LIBS-'-lopenal'} && ^^^^^^^^^^^^^^^^^^^^^^^^^ ...isn't this enough?
Fixed. I didn't know 'require' actually added the libs.
OK, maybe we could prefer plain filenames like out.ogg, /tmp/x.ogg looks very UNIX-ish, you may alienate some Windows user ;-).
Done.
What about using FFMIN(FFMIN(a,b), FFMIN(c,d))?
Done.
You may put each entry on a separate line, and vertically align fields.
Wasn't completely sure what you meant by 'entry', 'field', and 'vertically align' but I tried anyways.
return a generic AVERROR(EIO) or AVERROR(EINVAL), AVERROR(INT_MAX) semantics is undefined.
Also providing the error string was a nice feature.
Used EIO.
typo
Oops ;-)
alcCaptureOpenDevice() return NULL in case of failure, so you can use just a single "fail" label and do:
fail: if (ad->device) alcCaptureCloseDevice(ad->device); return error;
Done.
Printing the error here as in the previous version is a good idea (improves feedback).
Prints the error again. Also in the case of an EINVAL error in read_header, "Did you use a valid OpenAL capture device name?" is printed along with a list of OpenAL capture devices on the system, as that is the most likely cause of that error. This is also now documented.
More style nits:
static int read_close(AVFormatContext* ctx) { al_data *ad = ctx->priv_data;
if (ad->device) { alcCaptureStop(ad->device); alcCaptureCloseDevice(ad->device); } return 0; }
that is: 4 chars of indent, no space between fun_name and params, and "{" on a starting line for functions. No need to address these nits if you don't care, I can do it myself with a few emacs keystrokes before committing.
I tried to fix this, but my IDE apparently doesn't support having no space between function name and params, so I went through manually.
Please mention these options in the manual. See how I did it in the sdl section of the output devices chapter.
Revamped the documentation thanks to your suggestion. Now organized into subsections. Added a table listing the three most common generic AL implementations including links to their web sites. Added an options subsection. Cleaned up examples, now in their own section. Added an example where an invalid device name is used, and explained that it will fail. It seems quite extensive compared to the documentation for other indevs, but perhaps that's a good thing.
On date Friday 2011-06-10 22:41:43 -0400, Jonathan Baldwin encoded:
Sorry for the delay, I was kind of busy. [...] I tried to fix this, but my IDE apparently doesn't support having no space between function name and params, so I went through manually.
Please mention these options in the manual. See how I did it in the sdl section of the output devices chapter.
Revamped the documentation thanks to your suggestion. Now organized into subsections. Added a table listing the three most common generic AL implementations including links to their web sites. Added an options subsection. Cleaned up examples, now in their own section. Added an example where an invalid device name is used, and explained that it will fail.
It seems quite extensive compared to the documentation for other indevs, but perhaps that's a good thing.
diff --git a/Changelog b/Changelog index a2018dd..d809be5 100644 --- a/Changelog +++ b/Changelog @@ -21,6 +21,7 @@ version <next>: - split filter added - select filter added - sdl output device added +- openal input device added
version 0.7_beta1: diff --git a/configure b/configure index 62b7e2f..7a83cbe 100755 --- a/configure +++ b/configure @@ -188,6 +188,7 @@ External library support: --enable-libxavs enable AVS encoding via xavs [no] --enable-libxvid enable Xvid encoding via xvidcore, native MPEG-4/Xvid encoder exists [no] + --enable-openal enable OpenAL 1.1 capture support [no] --enable-mlib enable Sun medialib [no] --enable-zlib enable zlib [autodetect]
@@ -959,6 +960,7 @@ CONFIG_LIST=" mpegaudiodsp network nonfree + openal pic postproc rdft @@ -1470,6 +1472,7 @@ dv1394_indev_deps="dv1394 dv_demuxer" fbdev_indev_deps="linux_fb_h" jack_indev_deps="jack_jack_h sem_timedwait" libdc1394_indev_deps="libdc1394" +openal_indev_deps="openal" oss_indev_deps_any="soundcard_h sys_soundcard_h" oss_outdev_deps_any="soundcard_h sys_soundcard_h" sdl_outdev_deps="sdl" @@ -2944,6 +2947,9 @@ enabled libx264 && require libx264 x264.h x264_encoder_encode -lx264 && die "ERROR: libx264 version must be >= 0.115."; } enabled libxavs && require libxavs xavs.h xavs_encoder_encode -lxavs enabled libxvid && require libxvid xvid.h xvid_global -lxvidcore +enabled openal && require openal 'AL/al.h' alGetError ${OPENAL_LIBS-'-lopenal'} && + { check_cpp_condition "AL/al.h" "defined(AL_VERSION_1_1)" || + die "ERROR: openal version must be 1.1 or compatible"; } enabled mlib && require mediaLib mlib_types.h mlib_VectorSub_S16_U8_Mod -lmlib
SDL_CONFIG="${cross_prefix}sdl-config" @@ -3233,6 +3239,7 @@ echo "libvpx enabled ${libvpx-no}" echo "libx264 enabled ${libx264-no}" echo "libxavs enabled ${libxavs-no}" echo "libxvid enabled ${libxvid-no}" +echo "openal enabled ${openal-no}" echo "zlib enabled ${zlib-no}" echo "bzlib enabled ${bzlib-no}" echo diff --git a/doc/indevs.texi b/doc/indevs.texi index 0487108..49c9db9 100644 --- a/doc/indevs.texi +++ b/doc/indevs.texi @@ -137,6 +137,81 @@ For more information read:
IIDC1394 input device, based on libdc1394 and libraw1394.
+@section openal + +The OpenAL input device provides audio capture on all systems with a +working OpenAL 1.1 implementation. + +To enable this input device during configuration, you will need the +OpenAL headers and libraries. These should be provided as part of +your OpenAL implementation, or as an additional download (an SDK). + +Incomplete List of OpenAL Implementations: +@multitable @columnfractions .15 .20 .15 .50 + + @headitem Vendor @tab Platform @tab License @tab Links + + @item Creative + @tab Windows + @tab Proprietary + @tab @url{http://openal.org,Home Page} + @url{http://connect.creativelabs.com/openal/Downloads/Forms/AllItems.aspx,Downloads} + + @item OpenAL Soft + @tab Windows, Linux, Solaris, BSD + @tab LGPL + @tab @url{http://kcat.strangesoft.net/openal.html,Home Page} + + @item Apple + @tab Mac OS X + @tab + @tab OpenAL is part of + @url{http://developer.apple.com/library/mac/#documentation/MusicAudio/Conceptual/... Audio}, + the official OS X Audio interface. The SDK is included with XCode. + + @end multitable
Please verify that this is correctly rendered in the man page (the last time I checked, multi-tables are not correctly displayed), in this case the previous approach would be just fine.
+@subsection Options + +@table @option + +@item channels +The number of channels in the captured audio. Only the +values 1 (monaural) and 2 (stereo) are currently supported. +Defaults to stereo. + +@item sample_size +The sample size (in bits) of the captured audio. Only the +values 8 and 16 are currently supported. Defaults to 16-bit. + +@item sample_rate
+The sample rate (in Hz) of the captured audio. Defaults to 44100Hz.
+ +@end table + +@subsection Examples + +Capture from the OpenAL device 'DR-BT101 via PulseAudio': +@example +$ ffmpeg -f openal -i 'DR-BT101 via PulseAudio' out.ogg +@end example + +Capture from the default device (note the empty string '' as filename): +@example +$ ffmpeg -f openal -i '' out.ogg +@end example + +Capture from two devices simultaneously, writing to to different files, within same ffmpeg: +@example +$ ffmpeg -f openal -i 'DR-BT101 via PulseAudio' out1.ogg -f openal -i 'ALSA Default' out2.ogg +@end example +Note: Not all OpenAL implementations support multiple simultaneous capture, try the latest OpenAL Soft if the above doesn't work + +Using an invalid OpenAL device name will fail and a list of available devices will be printed: +@example +$ ffmpeg -f openal -i 'Not A Real OpenAL Device Name' +@end example
Note: it would be useful to list the available supported devices, e.g. with a list option which returns AVERROR_EXIT (no need to do it in this patch though).
+ @section oss Open Sound System input device. diff --git a/libavdevice/Makefile b/libavdevice/Makefile index 60103a4..4d3c1ae 100644 --- a/libavdevice/Makefile +++ b/libavdevice/Makefile @@ -19,6 +19,7 @@ OBJS-$(CONFIG_DSHOW_INDEV) += dshow.o dshow_enummediatypes.o \ OBJS-$(CONFIG_DV1394_INDEV) += dv1394.o OBJS-$(CONFIG_FBDEV_INDEV) += fbdev.o OBJS-$(CONFIG_JACK_INDEV) += jack_audio.o +OBJS-$(CONFIG_OPENAL_INDEV) += openal-dec.o OBJS-$(CONFIG_OSS_INDEV) += oss_audio.o OBJS-$(CONFIG_OSS_OUTDEV) += oss_audio.o OBJS-$(CONFIG_SDL_OUTDEV) += sdl.o diff --git a/libavdevice/alldevices.c b/libavdevice/alldevices.c index 7846704..ef302d7 100644 --- a/libavdevice/alldevices.c +++ b/libavdevice/alldevices.c @@ -44,6 +44,7 @@ void avdevice_register_all(void) REGISTER_INDEV (DV1394, dv1394); REGISTER_INDEV (FBDEV, fbdev); REGISTER_INDEV (JACK, jack); + REGISTER_INDEV (OPENAL, openal); REGISTER_INOUTDEV (OSS, oss); REGISTER_OUTDEV (SDL, sdl); REGISTER_INOUTDEV (SNDIO, sndio); diff --git a/libavdevice/avdevice.h b/libavdevice/avdevice.h index be56be4..7cb8f54 100644 --- a/libavdevice/avdevice.h +++ b/libavdevice/avdevice.h @@ -23,7 +23,7 @@ #include "libavformat/avformat.h"
#define LIBAVDEVICE_VERSION_MAJOR 53 -#define LIBAVDEVICE_VERSION_MINOR 1 +#define LIBAVDEVICE_VERSION_MINOR 2 #define LIBAVDEVICE_VERSION_MICRO 0
#define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \ diff --git a/libavdevice/openal-dec.c b/libavdevice/openal-dec.c new file mode 100644 index 0000000..e771e22 --- /dev/null +++ b/libavdevice/openal-dec.c @@ -0,0 +1,251 @@ +/** @file openal-dec.c OpenAL 1.1 capture device for libavdevice **/ +/* + * Copyright (c) 2011 Jonathan Baldwin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "libavformat/avformat.h" +#include "libavutil/opt.h" +#include <AL/al.h> +#include <AL/alc.h> +
+#define SSIZE 2048
SAMPLES_SIZE?
+ +typedef struct { + ALCdevice *device; +
+ ALCchar buffer[32768];
just curious, from where this magic number comes from?
+ ALCint buffer_capacity; + ALCint frame_size; + + int channels, sample_rate, sample_size; + ALCenum sample_format; +} al_data; + +typedef struct { + ALCenum al_fmt; + enum CodecID codec_id; + int channels;
+ int frame_size;
Nit: sample_step looks more explicative (number of bytes between two consecutives samples of the same channel/component).
+} al_format_info; + +#define LOWEST_AL_FORMAT FFMIN(FFMIN(AL_FORMAT_MONO8,AL_FORMAT_MONO16),FFMIN(AL_FORMAT_STEREO8,AL_FORMAT_STEREO16)) + +/** + * Get information about an AL_FORMAT value. + * @param al_fmt the AL_FORMAT value to find information about. + * @return A pointer to a structure containing information about the AL_FORMAT value. + */ +static inline al_format_info* get_al_format_info(ALCenum al_fmt) +{ + static al_format_info info_table[] = { + [AL_FORMAT_MONO8-LOWEST_AL_FORMAT] = {AL_FORMAT_MONO8, CODEC_ID_PCM_U8, 1, 1}, + [AL_FORMAT_MONO16-LOWEST_AL_FORMAT] = {AL_FORMAT_MONO16, AV_NE (CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE), 1, 2}, + [AL_FORMAT_STEREO8-LOWEST_AL_FORMAT] = {AL_FORMAT_STEREO8, CODEC_ID_PCM_U8, 2, 2}, + [AL_FORMAT_STEREO16-LOWEST_AL_FORMAT] = {AL_FORMAT_STEREO16, AV_NE (CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE), 2, 4}, + }; + + return &info_table[al_fmt-LOWEST_AL_FORMAT]; +} + +/** + * Get the OpenAL error code, translated into an av/errno error code. + * @param device The ALC device to check for errors. + * @param error_msg_ret A pointer to a char* in which to return the error message, or NULL if desired. + * @return The error code, or 0 if there is no error. + */ +static inline int al_get_error(ALCdevice *device, const char** error_msg_ret) +{ + ALCenum error = alcGetError (device); + if (error_msg_ret) + *error_msg_ret = (const char*) alcGetString (device, error); + switch (error) { + case ALC_NO_ERROR: + return 0; + case ALC_INVALID_DEVICE: + return AVERROR (ENODEV); + break; + case ALC_INVALID_CONTEXT: + case ALC_INVALID_ENUM: + case ALC_INVALID_VALUE: + return AVERROR (EINVAL); + break; + case ALC_OUT_OF_MEMORY: + return AVERROR (ENOMEM); + break; + default: + return AVERROR (EIO); + } +}
OK.
+ +/** + * Print out a list of OpenAL capture devices on this system. + */ +static inline void print_al_capture_devices() +{ + const char *devices; + + if (alcIsExtensionPresent (NULL, "ALC_ENUMERATION_EXT") != AL_TRUE) + return; + + if (! (devices = alcGetString (NULL, ALC_CAPTURE_DEVICE_SPECIFIER))) + return; + + av_log (NULL, AV_LOG_INFO, "List of OpenAL capture devices on this system:\n"); + + for (;*devices != '\0'; devices += strlen (devices) +1) + av_log (NULL, AV_LOG_INFO, "\t%s\n", devices); +} + +static int read_header(AVFormatContext* ctx, AVFormatParameters *ap) +{ + static const ALCenum sample_formats[2][2] = { + {AL_FORMAT_MONO8,AL_FORMAT_STEREO8}, + {AL_FORMAT_MONO16,AL_FORMAT_STEREO16} + }; + al_data *ad = ctx->priv_data; + int error=0; + const char *error_msg; + AVStream *st = NULL; + AVCodecContext *codec = NULL; + + ad->sample_format = sample_formats[ad->sample_size/8-1][ad->channels-1]; + + /* Open device for capture */ + ad->device = alcCaptureOpenDevice ( + ctx->filename[0] ? ctx->filename : 0, + ad->sample_rate, + ad->sample_format, + SSIZE + ); + + /* Did we fail? */ + if (error=al_get_error (ad->device, &error_msg)) goto fail; + + /* Create stream */ + if (! (st=av_new_stream (ctx, 0))) { + error=AVERROR (ENOMEM); + goto fail; + } + + /* We work in microseconds */ + av_set_pts_info (st, 64, 1, 1000000); + + /* Set codec parameters */ + codec = st->codec; + codec->codec_type = AVMEDIA_TYPE_AUDIO; + codec->sample_rate = ad->sample_rate; + codec->channels = get_al_format_info (ad->sample_format)->channels; + codec->codec_id = get_al_format_info (ad->sample_format)->codec_id; + + /* These are needed to read the audio data */ + ad->frame_size = get_al_format_info (ad->sample_format)->frame_size; + ad->buffer_capacity = sizeof (ad->buffer) /ad->frame_size; + + /* Finally, start the capture process */ + alcCaptureStart (ad->device); + + return 0; + + /* Handle failure */ +fail: + if (ad->device) + alcCaptureCloseDevice (ad->device); + + if (error_msg) + av_log (ctx, AV_LOG_ERROR, "Error: %s\n", error_msg); + + /* An EINVAL error here is probably due to an invalid OpenAL capture device name */ + if (error==AVERROR (EINVAL)) { + av_log (ctx, AV_LOG_ERROR, "Did you use a valid OpenAL capture device name?\n"); + print_al_capture_devices(); + } + + return error; +} + +static int read_packet(AVFormatContext* ctx, AVPacket *pkt) +{ + al_data *ad = ctx->priv_data; + int error=0; + const char *error_msg;
+ ALCint num_samples;
Nit+++: nb_samples -> more consistent with the rest of FFmpeg
+ + /* Read samples from device... */ + alcGetIntegerv (ad->device, ALC_CAPTURE_SAMPLES, (ALCsizei) sizeof (ALCint), &num_samples); + if (error=al_get_error (ad->device, &error_msg)) goto fail; + + if (num_samples > ad->buffer_capacity) + num_samples = ad->buffer_capacity; + alcCaptureSamples (ad->device, ad->buffer, num_samples); + if (error=al_get_error (ad->device, &error_msg)) goto fail; + + /* ...and use them to fill a packet */ + av_init_packet (pkt); + pkt->data = ad->buffer; + pkt->size = num_samples*ad->frame_size; + pkt->pts = av_gettime();
I believe here you need to put the data directly into the packet. You may get nb_samples, create a new packet with av_new_packet(), and finally fill it with alcCaptureSamples(). [...] Rest looks fine, nice work! -- FFmpeg = Furious & Fiendish Monstrous Picky Entertaining Genius
Sorry for the delay, I was kind of busy. ++;
Please verify that this is correctly rendered in the man page (the last time I checked, multi-tables are not correctly displayed), in this case the previous approach would be just fine.
Apparently upstream texi2pod.pl (gcc) does, but the output doesn't look to pleasant IMHO anyways. Wasted a few hours messing with it, and decided it was easier to avoid multi-tables. Changed to a plain old two-column @table.
Note: it would be useful to list the available supported devices, e.g. with a list option which returns AVERROR_EXIT (no need to do it in this patch though).
Regarding the list option, Can something like $ ffmpeg -f openal -list_devices be achieved, or is $ ffmpeg -f openal -list_devices 1 or even $ ffmpeg -f openal -list_devices 1 '' the best I can get? I found AV_OPT_TYPE_FLAGS, is this relevant, and how would I use it?
SAMPLES_SIZE?
No longer needed anymore.
just curious, from where this magic number comes from?
I needed an appropriate size for my buffer. Turns out this wasn't so appropriate - it was a bit inflated. Now it isn't needed anymore because the buffer was replaced with as neccesary packet allocation via av_new_packet, as suggested.
Nit: sample_step looks more explicative (number of bytes between two consecutives samples of the same channel/component).
Done.
Nit+++: nb_samples -> more consistent with the rest of FFmpeg
Done.
I believe here you need to put the data directly into the packet.
You may get nb_samples, create a new packet with av_new_packet(), and finally fill it with alcCaptureSamples().
Done. Static buffer[] removed, samples go into the packet allocated by av_new_packet().
On date Friday 2011-06-17 22:21:42 -0400, Jonathan Baldwin encoded:
Sorry for the delay, I was kind of busy. ++;
Please verify that this is correctly rendered in the man page (the last time I checked, multi-tables are not correctly displayed), in this case the previous approach would be just fine.
Apparently upstream texi2pod.pl (gcc) does, but the output doesn't look to pleasant IMHO anyways. Wasted a few hours messing with it, and decided it was easier to avoid multi-tables. Changed to a plain old two-column @table.
Note: it would be useful to list the available supported devices, e.g. with a list option which returns AVERROR_EXIT (no need to do it in this patch though).
Regarding the list option,
Can something like $ ffmpeg -f openal -list_devices be achieved, or is $ ffmpeg -f openal -list_devices 1 or even $ ffmpeg -f openal -list_devices 1 '' the best I can get?
I found AV_OPT_TYPE_FLAGS, is this relevant, and how would I use it?
Yes, I confirm there is no way to set a boolean, the only way is throug -list_devices 1 or by abusing the filename like it is done in vfwcap ("list" provided as filename will cause the device to list the available devices, but I don't like that solution). [...]
+static int read_header(AVFormatContext* ctx, AVFormatParameters *ap) +{ + static const ALCenum sample_formats[2][2] = { + {AL_FORMAT_MONO8,AL_FORMAT_STEREO8}, + {AL_FORMAT_MONO16,AL_FORMAT_STEREO16} + }; + al_data *ad = ctx->priv_data; + int error=0; + const char *error_msg; + AVStream *st = NULL; + AVCodecContext *codec = NULL; + + ad->sample_format = sample_formats[ad->sample_size/8-1][ad->channels-1]; + + /* Open device for capture */ + ad->device = alcCaptureOpenDevice( + ctx->filename[0] ? ctx->filename : 0, + ad->sample_rate, + ad->sample_format, + ad->sample_rate /* Maximum 1 second of sample data to be read at once */ + ); + + /* Did we fail? */ + if(error=al_get_error(ad->device, &error_msg)) goto fail; + + /* Create stream */ + if(! (st=av_new_stream(ctx, 0))) { + error=AVERROR (ENOMEM); + goto fail; + } + + /* We work in microseconds */ + av_set_pts_info(st, 64, 1, 1000000); + + /* Set codec parameters */ + codec = st->codec; + codec->codec_type = AVMEDIA_TYPE_AUDIO; + codec->sample_rate = ad->sample_rate; + codec->channels = get_al_format_info(ad->sample_format)->channels; + codec->codec_id = get_al_format_info(ad->sample_format)->codec_id; + + /* This is needed to read the audio data */ + ad->sample_step = (av_get_bits_per_sample(get_al_format_info(ad->sample_format)->codec_id) * + get_al_format_info(ad->sample_format)->channels) / 8; + + /* Finally, start the capture process */ + alcCaptureStart(ad->device); + + return 0; + + /* Handle failure */ +fail: + if(ad->device) + alcCaptureCloseDevice(ad->device); + + if(error_msg) + av_log(ctx, AV_LOG_ERROR, "Error: %s\n", error_msg); +
+ /* An EINVAL error here is probably due to an invalid OpenAL capture device name */ + if(error==AVERROR (EINVAL)) { + av_log(ctx, AV_LOG_ERROR, "Did you use a valid OpenAL capture device name?\n"); + print_al_capture_devices(); + }
I fear this message will get tedious if the "guess" is wrong, I'd prefer just the list_device option (explicit and perfectly predicible, no need for wild guesses or supposing that a device name can't be named "list" or like another arbitrary string). [...] No more comments from me. I suppose the device has been tested and works. -- FFmpeg = Fundamentalist and Fast Murdering Problematic Enigmatic Game
* Added the list_device option. * configure now checks for both 'openal' and 'OpenAL32.dll' library names. * Fixed a bug (al_data needed to begin with an AVClass pointer. Dunno why no segfaulting occured before.) * Did some (actually quite a bit of) testing. - I tested against both my distro's OpenAL Soft version as well as the latest version. - I tested on Windows cross-compiling from my Kubuntu box. I used OpenAL Soft's import library, libOpenAL32.dll.a (MinGW and MSVC use incompatible import library formats, no?). After rebooting into Windows 7, I tested the binary against both the Creative implementation, the OpenAL Soft implementation plus the Creative router dll, and OpenAL Soft solo. * The patch applies cleanly against git HEAD, as of writing this. I'm now out of school for the summer, so I won't be as slow responding now.
On date Thursday 2011-06-23 19:58:53 -0400, Jonathan Baldwin encoded:
* Added the list_device option. * configure now checks for both 'openal' and 'OpenAL32.dll' library names. * Fixed a bug (al_data needed to begin with an AVClass pointer. Dunno why no segfaulting occured before.) * Did some (actually quite a bit of) testing. - I tested against both my distro's OpenAL Soft version as well as the latest version. - I tested on Windows cross-compiling from my Kubuntu box. I used OpenAL Soft's import library, libOpenAL32.dll.a (MinGW and MSVC use incompatible import library formats, no?). After rebooting into Windows 7, I tested the binary against both the Creative implementation, the OpenAL Soft implementation plus the Creative router dll, and OpenAL Soft solo. * The patch applies cleanly against git HEAD, as of writing this.
I'm now out of school for the summer, so I won't be as slow responding now.
diff --git a/Changelog b/Changelog index 4362371..b8b4939 100644 --- a/Changelog +++ b/Changelog @@ -36,6 +36,7 @@ version 0.7_beta2: - split filter added - select filter added - sdl output device added +- openal input device added
version 0.7_beta1: diff --git a/configure b/configure index aa62831..1185310 100755 --- a/configure +++ b/configure @@ -188,6 +188,7 @@ External library support: --enable-libxavs enable AVS encoding via xavs [no] --enable-libxvid enable Xvid encoding via xvidcore, native MPEG-4/Xvid encoder exists [no] + --enable-openal enable OpenAL 1.1 capture support [no] --enable-mlib enable Sun medialib [no] --enable-zlib enable zlib [autodetect]
@@ -959,6 +960,7 @@ CONFIG_LIST=" mpegaudiodsp network nonfree + openal pic postproc rdft @@ -1470,6 +1472,7 @@ dv1394_indev_deps="dv1394 dv_demuxer" fbdev_indev_deps="linux_fb_h" jack_indev_deps="jack_jack_h sem_timedwait" libdc1394_indev_deps="libdc1394" +openal_indev_deps="openal" oss_indev_deps_any="soundcard_h sys_soundcard_h" oss_outdev_deps_any="soundcard_h sys_soundcard_h" sdl_outdev_deps="sdl" @@ -2946,6 +2949,11 @@ enabled libx264 && require libx264 x264.h x264_encoder_encode -lx264 && die "ERROR: libx264 version must be >= 0.115."; } enabled libxavs && require libxavs xavs.h xavs_encoder_encode -lxavs enabled libxvid && require libxvid xvid.h xvid_global -lxvidcore +enabled openal && { { for al_libs in "${OPENAL_LIBS}" "-lopenal" "-lOpenAL32.dll"; do + check_lib 'AL/al.h' alGetError "${al_libs}" && break; done } || + die "ERROR: openal not found"; } && + { check_cpp_condition "AL/al.h" "defined(AL_VERSION_1_1)" || + die "ERROR: openal version must be 1.1 or compatible"; }
Maybe this could be simplified, but I think it's safe to commit and see if someone has better ideas.
enabled mlib && require mediaLib mlib_types.h mlib_VectorSub_S16_U8_Mod -lmlib
SDL_CONFIG="${cross_prefix}sdl-config" @@ -3242,6 +3250,7 @@ echo "libvpx enabled ${libvpx-no}" echo "libx264 enabled ${libx264-no}" echo "libxavs enabled ${libxavs-no}" echo "libxvid enabled ${libxvid-no}" +echo "openal enabled ${openal-no}" echo "zlib enabled ${zlib-no}" echo "bzlib enabled ${bzlib-no}" echo diff --git a/doc/indevs.texi b/doc/indevs.texi index 0487108..0c0766b 100644 --- a/doc/indevs.texi +++ b/doc/indevs.texi @@ -137,6 +137,76 @@ For more information read:
IIDC1394 input device, based on libdc1394 and libraw1394.
+@section openal + +The OpenAL input device provides audio capture on all systems with a +working OpenAL 1.1 implementation. + +To enable this input device during configuration, you will need the +OpenAL headers and libraries. These should be provided as part of +your OpenAL implementation, or as an additional download (an SDK). + +Incomplete List of OpenAL Implementations: + +@table @strong +@item Creative +The official Windows implementation, providing hardware acceleration +with supported devices and software fallback. +http://openal.org/ +@item OpenAL Soft +Portable, open source (LGPL) software implementation. Includes +backends for the most common sound APIs on the Windows, Linux, +Solaris, and BSD operating systems. +http://kcat.strangesoft.net/openal.html +@item Apple +OpenAL is part of Core Audio, the official Mac OS X Audio interface. +http://developer.apple.com/technologies/mac/audio-and-video.html +@end table + +@subsection Options + +@table @option + +@item channels +The number of channels in the captured audio. Only the +values @option{1} (monaural) and @option{2} (stereo) are currently supported. +Defaults to @option{2}. + +@item sample_size +The sample size (in bits) of the captured audio. Only the +values @option{8} and @option{16} are currently supported. +Defaults to @option{16}. + +@item sample_rate +The sample rate (in Hz) of the captured audio. +Defaults to @option{44.1k}. + +@item list_devices +If set to @option{true}, print a list of devices and exit. +Defaults to @option{false}.
Note: maybe we could add proper support to "booleans" in avopt. [...] Looks fine to me, I'll test and commit in a few days if I see no comments from other devs (and if it works for me). -- FFmpeg = Fabulous and Fancy Muttering Philosofic Epic Gargoyle
On date Thursday 2011-06-23 19:58:53 -0400, Jonathan Baldwin encoded: [...]
+enabled openal && { { for al_libs in "${OPENAL_LIBS}" "-lopenal" "-lOpenAL32.dll"; do + check_lib 'AL/al.h' alGetError "${al_libs}" && break; done } || + die "ERROR: openal not found"; } &&
Tried to configure this natively on Mingw/Windows7, I had to replace "-lOpenAL32.dll" with "-lOpenAL32", as the OpenAL SDK from creative has not a .dll (it provides an OpenAL32.lib instead). [...] Tested on Linux and seems to work fine. I updated the patch with a few cosmetical fixes and docs additions. -- FFmpeg = Foolish Fiendish Mastodontic Practical Extroverse Gospel
Maybe this could be simplified, but I think it's safe to commit and see if someone has better ideas.
The simpler the better ;-)
Note: maybe we could add proper support to "booleans" in avopt.
That's probably a good idea, it could save a lot of boilerplate (such as the true/false option constants in this code) if there are enough things that require booleans. It could also make it easier on the user, depending on the interface.
Looks fine to me, I'll test and commit in a few days if I see no comments from other devs (and if it works for me).
Awesome. I look forward to contributing more.
Tried to configure this natively on Mingw/Windows7, I had to replace "-lOpenAL32.dll" with "-lOpenAL32", as the OpenAL SDK from creative has not a .dll (it provides an OpenAL32.lib instead).
I wasn't sure if MinGW could use the Creative import library, I thought that perhaps it was in a different archive format or something, because I couldn't unar it (I know that *nix and MinGW native import libraries are ar archives of object files.) I guess I could've tried linking against it anyways...
I updated the patch with a few cosmetical fixes and docs additions.
The updated patch works for me ;-)
On date Sunday 2011-06-26 22:44:47 -0400, Jonathan Baldwin encoded:
Maybe this could be simplified, but I think it's safe to commit and see if someone has better ideas.
The simpler the better ;-)
Note: maybe we could add proper support to "booleans" in avopt.
That's probably a good idea, it could save a lot of boilerplate (such as the true/false option constants in this code) if there are enough things that require booleans. It could also make it easier on the user, depending on the interface.
Looks fine to me, I'll test and commit in a few days if I see no comments from other devs (and if it works for me).
Awesome. I look forward to contributing more.
Tried to configure this natively on Mingw/Windows7, I had to replace "-lOpenAL32.dll" with "-lOpenAL32", as the OpenAL SDK from creative has not a .dll (it provides an OpenAL32.lib instead).
I wasn't sure if MinGW could use the Creative import library, I thought that perhaps it was in a different archive format or something, because I couldn't unar it (I know that *nix and MinGW native import libraries are ar archives of object files.) I guess I could've tried linking against it anyways...
I updated the patch with a few cosmetical fixes and docs additions.
The updated patch works for me ;-)
Tested on MinGW/Windows7 and pushed. I've been told that the ISC license is simpler, feel free to send a patch for changing licence if you feel like that. And thanks again for the nice job, this finally allows audio capture on Windows :-). -- FFmpeg = Fascinating and Fundamentalist Mega Powerful Enchanting Gadget
I've been told that the ISC license is simpler, feel free to send a patch for changing licence if you feel like that.
Here ya go. It is quite a bit simpler. I actually only chose the variant of the MIT license that I originally used because it was already in use by other parts of FFmpeg (specifically the libavformat output example.)
And thanks again for the nice job, this finally allows audio capture on Windows :-).
You're welcome :-). I'm very glad I could help. I plan to write an OpenAL output device someday, but I have something I'm writing first before that.
On date Thursday 2011-06-30 19:05:30 -0400, Jonathan Baldwin encoded:
I've been told that the ISC license is simpler, feel free to send a patch for changing licence if you feel like that.
Here ya go. It is quite a bit simpler. I actually only chose the variant of the MIT license that I originally used because it was already in use by other parts of FFmpeg (specifically the libavformat output example.)
Patch pushed.
And thanks again for the nice job, this finally allows audio capture on Windows :-).
You're welcome :-). I'm very glad I could help. I plan to write an OpenAL output device someday, but I have something I'm writing first before that.
Nice, looking forward for that :). -- FFmpeg = Fanciful Fanciful Most Puritan Embarassing Gem
participants (3)
-
Jonathan Baldwin -
Kirill Gavrilov -
Stefano Sabatini