diff options
Diffstat (limited to 'libavdevice')
33 files changed, 3424 insertions, 467 deletions
diff --git a/libavdevice/Makefile b/libavdevice/Makefile index d8a5945..879e933 100644 --- a/libavdevice/Makefile +++ b/libavdevice/Makefile @@ -1,5 +1,8 @@ +include $(SUBDIR)../config.mak + NAME = avdevice FFLIBS = avformat avcodec avutil +FFLIBS-$(CONFIG_LAVFI_INDEV) += avfilter HEADERS = avdevice.h @@ -11,11 +14,17 @@ OBJS-$(CONFIG_ALSA_INDEV) += alsa-audio-common.o \ OBJS-$(CONFIG_ALSA_OUTDEV) += alsa-audio-common.o \ alsa-audio-enc.o OBJS-$(CONFIG_BKTR_INDEV) += bktr.o +OBJS-$(CONFIG_DSHOW_INDEV) += dshow.o dshow_enummediatypes.o \ + dshow_enumpins.o dshow_filter.o \ + dshow_pin.o dshow_common.o OBJS-$(CONFIG_DV1394_INDEV) += dv1394.o OBJS-$(CONFIG_FBDEV_INDEV) += fbdev.o OBJS-$(CONFIG_JACK_INDEV) += jack_audio.o +OBJS-$(CONFIG_LAVFI_INDEV) += lavfi.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 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 diff --git a/libavdevice/alldevices.c b/libavdevice/alldevices.c index a1a333f..ce2ea02 100644 --- a/libavdevice/alldevices.c +++ b/libavdevice/alldevices.c @@ -1,25 +1,24 @@ /* * Register all the grabbing devices. * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "config.h" -#include "libavformat/avformat.h" #include "avdevice.h" #define REGISTER_OUTDEV(X,x) { \ @@ -41,10 +40,14 @@ void avdevice_register_all(void) /* devices */ REGISTER_INOUTDEV (ALSA, alsa); REGISTER_INDEV (BKTR, bktr); + REGISTER_INDEV (DSHOW, dshow); REGISTER_INDEV (DV1394, dv1394); REGISTER_INDEV (FBDEV, fbdev); REGISTER_INDEV (JACK, jack); + REGISTER_INDEV (LAVFI, lavfi); + REGISTER_INDEV (OPENAL, openal); REGISTER_INOUTDEV (OSS, oss); + REGISTER_OUTDEV (SDL, sdl); REGISTER_INOUTDEV (SNDIO, sndio); REGISTER_INDEV (V4L2, v4l2); #if FF_API_V4L diff --git a/libavdevice/alsa-audio-common.c b/libavdevice/alsa-audio-common.c index a0662f6..1ed7b6e 100644 --- a/libavdevice/alsa-audio-common.c +++ b/libavdevice/alsa-audio-common.c @@ -3,20 +3,20 @@ * Copyright (c) 2007 Luca Abeni ( lucabe72 email it ) * Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr ) * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -29,7 +29,7 @@ */ #include <alsa/asoundlib.h> -#include "libavformat/avformat.h" +#include "avdevice.h" #include "libavutil/avassert.h" #include "libavutil/audioconvert.h" @@ -320,6 +320,8 @@ av_cold int ff_alsa_close(AVFormatContext *s1) AlsaData *s = s1->priv_data; av_freep(&s->reorder_buf); + if (CONFIG_ALSA_INDEV) + ff_timefilter_destroy(s->timefilter); snd_pcm_close(s->h); return 0; } diff --git a/libavdevice/alsa-audio-dec.c b/libavdevice/alsa-audio-dec.c index 05f68d3..11c6e92 100644 --- a/libavdevice/alsa-audio-dec.c +++ b/libavdevice/alsa-audio-dec.c @@ -3,20 +3,20 @@ * Copyright (c) 2007 Luca Abeni ( lucabe72 email it ) * Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr ) * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -46,9 +46,10 @@ */ #include <alsa/asoundlib.h> -#include "libavformat/avformat.h" #include "libavutil/opt.h" +#include "libavutil/mathematics.h" +#include "avdevice.h" #include "alsa-audio.h" static av_cold int audio_read_header(AVFormatContext *s1, @@ -58,7 +59,7 @@ static av_cold int audio_read_header(AVFormatContext *s1, AVStream *st; int ret; enum CodecID codec_id; - snd_pcm_sw_params_t *sw_params; + double o; st = av_new_stream(s1, 0); if (!st) { @@ -74,35 +75,17 @@ static av_cold int audio_read_header(AVFormatContext *s1, return AVERROR(EIO); } - if (snd_pcm_type(s->h) != SND_PCM_TYPE_HW) - av_log(s1, AV_LOG_WARNING, - "capture with some ALSA plugins, especially dsnoop, " - "may hang.\n"); - - ret = snd_pcm_sw_params_malloc(&sw_params); - if (ret < 0) { - av_log(s1, AV_LOG_ERROR, "cannot allocate software parameters structure (%s)\n", - snd_strerror(ret)); - goto fail; - } - - snd_pcm_sw_params_current(s->h, sw_params); - snd_pcm_sw_params_set_tstamp_mode(s->h, sw_params, SND_PCM_TSTAMP_ENABLE); - - ret = snd_pcm_sw_params(s->h, sw_params); - snd_pcm_sw_params_free(sw_params); - if (ret < 0) { - av_log(s1, AV_LOG_ERROR, "cannot install ALSA software parameters (%s)\n", - snd_strerror(ret)); - goto fail; - } - /* take real parameters */ st->codec->codec_type = AVMEDIA_TYPE_AUDIO; st->codec->codec_id = 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 */ + o = 2 * M_PI * s->period_size / s->sample_rate * 1.5; // bandwidth: 1.5Hz + s->timefilter = ff_timefilter_new(1000000.0 / s->sample_rate, + sqrt(2 * o), o * o); + if (!s->timefilter) + goto fail; return 0; @@ -114,16 +97,15 @@ fail: static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt) { AlsaData *s = s1->priv_data; - AVStream *st = s1->streams[0]; int res; - snd_htimestamp_t timestamp; - snd_pcm_uframes_t ts_delay; + int64_t dts; + snd_pcm_sframes_t delay = 0; - if (av_new_packet(pkt, s->period_size) < 0) { + if (av_new_packet(pkt, s->period_size * s->frame_size) < 0) { return AVERROR(EIO); } - while ((res = snd_pcm_readi(s->h, pkt->data, pkt->size / s->frame_size)) < 0) { + while ((res = snd_pcm_readi(s->h, pkt->data, s->period_size)) < 0) { if (res == -EAGAIN) { av_free_packet(pkt); @@ -136,14 +118,13 @@ static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt) return AVERROR(EIO); } + ff_timefilter_reset(s->timefilter); } - snd_pcm_htimestamp(s->h, &ts_delay, ×tamp); - ts_delay += res; - pkt->pts = timestamp.tv_sec * 1000000LL - + (timestamp.tv_nsec * st->codec->sample_rate - - ts_delay * 1000000000LL + st->codec->sample_rate * 500LL) - / (st->codec->sample_rate * 1000LL); + dts = av_gettime(); + snd_pcm_delay(s->h, &delay); + dts -= av_rescale(delay + res, 1000000, s->sample_rate); + pkt->pts = ff_timefilter_update(s->timefilter, dts, res); pkt->size = res * s->frame_size; diff --git a/libavdevice/alsa-audio-enc.c b/libavdevice/alsa-audio-enc.c index f3782c5..0da22bb 100644 --- a/libavdevice/alsa-audio-enc.c +++ b/libavdevice/alsa-audio-enc.c @@ -3,20 +3,20 @@ * Copyright (c) 2007 Luca Abeni ( lucabe72 email it ) * Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr ) * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -38,8 +38,8 @@ */ #include <alsa/asoundlib.h> -#include "libavformat/avformat.h" +#include "avdevice.h" #include "alsa-audio.h" static av_cold int audio_write_header(AVFormatContext *s1) @@ -61,6 +61,7 @@ static av_cold int audio_write_header(AVFormatContext *s1) st->codec->sample_rate, sample_rate); goto fail; } + av_set_pts_info(st, 64, 1, sample_rate); return res; @@ -101,6 +102,17 @@ static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt) return 0; } +static void +audio_get_output_timestamp(AVFormatContext *s1, int stream, + int64_t *dts, int64_t *wall) +{ + AlsaData *s = s1->priv_data; + snd_pcm_sframes_t delay = 0; + *wall = av_gettime(); + snd_pcm_delay(s->h, &delay); + *dts = s1->streams[0]->cur_dts - delay; +} + AVOutputFormat ff_alsa_muxer = { "alsa", NULL_IF_CONFIG_SMALL("ALSA audio output"), @@ -112,5 +124,6 @@ AVOutputFormat ff_alsa_muxer = { audio_write_header, audio_write_packet, ff_alsa_close, + .get_output_timestamp = audio_get_output_timestamp, .flags = AVFMT_NOFILE, }; diff --git a/libavdevice/alsa-audio.h b/libavdevice/alsa-audio.h index ee43463..83af464 100644 --- a/libavdevice/alsa-audio.h +++ b/libavdevice/alsa-audio.h @@ -3,20 +3,20 @@ * Copyright (c) 2007 Luca Abeni ( lucabe72 email it ) * Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr ) * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -32,23 +32,27 @@ #include <alsa/asoundlib.h> #include "config.h" -#include "libavformat/avformat.h" #include "libavutil/log.h" +#include "libavformat/timefilter.h" +#include "avdevice.h" /* XXX: we make the assumption that the soundcard accepts this format */ /* XXX: find better solution with "preinit" method, needed also in other formats */ #define DEFAULT_CODEC_ID AV_NE(CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE) -#define ALSA_BUFFER_SIZE_MAX 32768 +typedef void (*ff_reorder_func)(const void *, void *, int); + +#define ALSA_BUFFER_SIZE_MAX 65536 typedef struct { AVClass *class; snd_pcm_t *h; - int frame_size; ///< preferred size for reads and writes - int period_size; ///< bytes per sample * channels + int frame_size; ///< bytes per sample * channels + int period_size; ///< preferred size for reads and writes, in frames int sample_rate; ///< sample rate set by user int channels; ///< number of channels set by user + TimeFilter *timefilter; void (*reorder_func)(const void *, void *, int); void *reorder_buf; int reorder_buf_size; ///< in frames diff --git a/libavdevice/avdevice.c b/libavdevice/avdevice.c index 4813a3d..3d67b4b 100644 --- a/libavdevice/avdevice.c +++ b/libavdevice/avdevice.c @@ -1,18 +1,18 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -25,11 +25,11 @@ unsigned avdevice_version(void) const char * avdevice_configuration(void) { - return LIBAV_CONFIGURATION; + return FFMPEG_CONFIGURATION; } const char * avdevice_license(void) { #define LICENSE_PREFIX "libavdevice license: " - return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1; + return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1; } diff --git a/libavdevice/avdevice.h b/libavdevice/avdevice.h index c0d392d..68e3a46 100644 --- a/libavdevice/avdevice.h +++ b/libavdevice/avdevice.h @@ -1,18 +1,18 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -20,9 +20,10 @@ #define AVDEVICE_AVDEVICE_H #include "libavutil/avutil.h" +#include "libavformat/avformat.h" #define LIBAVDEVICE_VERSION_MAJOR 53 -#define LIBAVDEVICE_VERSION_MINOR 1 +#define LIBAVDEVICE_VERSION_MINOR 4 #define LIBAVDEVICE_VERSION_MICRO 0 #define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \ diff --git a/libavdevice/bktr.c b/libavdevice/bktr.c index 7ff46cf..2792fcf 100644 --- a/libavdevice/bktr.c +++ b/libavdevice/bktr.c @@ -7,24 +7,23 @@ * and * simple_grab.c Copyright (c) 1999 Roger Hardiman * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "libavformat/avformat.h" #include "libavutil/log.h" #include "libavutil/opt.h" #include "libavutil/parseutils.h" @@ -48,6 +47,7 @@ #include <signal.h> #include <stdint.h> #include <strings.h> +#include "avdevice.h" typedef struct { AVClass *class; diff --git a/libavdevice/dshow.c b/libavdevice/dshow.c new file mode 100644 index 0000000..a9d400c --- /dev/null +++ b/libavdevice/dshow.c @@ -0,0 +1,960 @@ +/* + * Directshow capture interface + * Copyright (c) 2010 Ramiro Polla + * + * 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/parseutils.h" +#include "libavutil/opt.h" + +#include "avdevice.h" +#include "dshow.h" + +struct dshow_ctx { + const AVClass *class; + + IGraphBuilder *graph; + + char *device_name[2]; + + int list_options; + int list_devices; + + IBaseFilter *device_filter[2]; + IPin *device_pin[2]; + libAVFilter *capture_filter[2]; + libAVPin *capture_pin[2]; + + HANDLE mutex; + HANDLE event; + AVPacketList *pktl; + + unsigned int curbufsize; + unsigned int video_frame_num; + + IMediaControl *control; + + char *video_size; + char *framerate; + + int requested_width; + int requested_height; + AVRational requested_framerate; + + int sample_rate; + int sample_size; + int channels; +}; + +static enum PixelFormat dshow_pixfmt(DWORD biCompression, WORD biBitCount) +{ + switch(biCompression) { + case MKTAG('U', 'Y', 'V', 'Y'): + return PIX_FMT_UYVY422; + case MKTAG('Y', 'U', 'Y', '2'): + return PIX_FMT_YUYV422; + case MKTAG('I', '4', '2', '0'): + return PIX_FMT_YUV420P; + case BI_RGB: + switch(biBitCount) { /* 1-8 are untested */ + case 1: + return PIX_FMT_MONOWHITE; + case 4: + return PIX_FMT_RGB4; + case 8: + return PIX_FMT_RGB8; + case 16: + return PIX_FMT_RGB555; + case 24: + return PIX_FMT_BGR24; + case 32: + return PIX_FMT_RGB32; + } + } + return PIX_FMT_NONE; +} + +static enum CodecID dshow_codecid(DWORD biCompression) +{ + switch(biCompression) { + case MKTAG('d', 'v', 's', 'd'): + return CODEC_ID_DVVIDEO; + case MKTAG('M', 'J', 'P', 'G'): + case MKTAG('m', 'j', 'p', 'g'): + return CODEC_ID_MJPEG; + } + return CODEC_ID_NONE; +} + +static int +dshow_read_close(AVFormatContext *s) +{ + struct dshow_ctx *ctx = s->priv_data; + AVPacketList *pktl; + + if (ctx->control) { + IMediaControl_Stop(ctx->control); + IMediaControl_Release(ctx->control); + } + + if (ctx->graph) { + IEnumFilters *fenum; + int r; + r = IGraphBuilder_EnumFilters(ctx->graph, &fenum); + if (r == S_OK) { + IBaseFilter *f; + IEnumFilters_Reset(fenum); + while (IEnumFilters_Next(fenum, 1, &f, NULL) == S_OK) + if (IGraphBuilder_RemoveFilter(ctx->graph, f) == S_OK) + IEnumFilters_Reset(fenum); /* When a filter is removed, + * the list must be reset. */ + IEnumFilters_Release(fenum); + } + IGraphBuilder_Release(ctx->graph); + } + + if (ctx->capture_pin[VideoDevice]) + libAVPin_Release(ctx->capture_pin[VideoDevice]); + if (ctx->capture_pin[AudioDevice]) + libAVPin_Release(ctx->capture_pin[AudioDevice]); + if (ctx->capture_filter[VideoDevice]) + libAVFilter_Release(ctx->capture_filter[VideoDevice]); + if (ctx->capture_filter[AudioDevice]) + libAVFilter_Release(ctx->capture_filter[AudioDevice]); + + if (ctx->device_pin[VideoDevice]) + IPin_Release(ctx->device_pin[VideoDevice]); + if (ctx->device_pin[AudioDevice]) + IPin_Release(ctx->device_pin[AudioDevice]); + if (ctx->device_filter[VideoDevice]) + IBaseFilter_Release(ctx->device_filter[VideoDevice]); + if (ctx->device_filter[AudioDevice]) + IBaseFilter_Release(ctx->device_filter[AudioDevice]); + + if (ctx->device_name[0]) + av_free(ctx->device_name[0]); + if (ctx->device_name[1]) + av_free(ctx->device_name[1]); + + if(ctx->mutex) + CloseHandle(ctx->mutex); + if(ctx->event) + CloseHandle(ctx->event); + + pktl = ctx->pktl; + while (pktl) { + AVPacketList *next = pktl->next; + av_destruct_packet(&pktl->pkt); + av_free(pktl); + pktl = next; + } + + return 0; +} + +static char *dup_wchar_to_utf8(wchar_t *w) +{ + char *s = NULL; + int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0); + s = av_malloc(l); + if (s) + WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0); + return s; +} + +static int shall_we_drop(AVFormatContext *s) +{ + struct dshow_ctx *ctx = s->priv_data; + const uint8_t dropscore[] = {62, 75, 87, 100}; + const int ndropscores = FF_ARRAY_ELEMS(dropscore); + unsigned int buffer_fullness = (ctx->curbufsize*100)/s->max_picture_buffer; + + if(dropscore[++ctx->video_frame_num%ndropscores] <= buffer_fullness) { + av_log(s, AV_LOG_ERROR, + "real-time buffer %d%% full! frame dropped!\n", buffer_fullness); + return 1; + } + + return 0; +} + +static void +callback(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time) +{ + AVFormatContext *s = priv_data; + struct dshow_ctx *ctx = s->priv_data; + AVPacketList **ppktl, *pktl_next; + +// dump_videohdr(s, vdhdr); + + if(shall_we_drop(s)) + return; + + WaitForSingleObject(ctx->mutex, INFINITE); + + pktl_next = av_mallocz(sizeof(AVPacketList)); + if(!pktl_next) + goto fail; + + if(av_new_packet(&pktl_next->pkt, buf_size) < 0) { + av_free(pktl_next); + goto fail; + } + + pktl_next->pkt.stream_index = index; + pktl_next->pkt.pts = time; + memcpy(pktl_next->pkt.data, buf, buf_size); + + for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next); + *ppktl = pktl_next; + + ctx->curbufsize += buf_size; + + SetEvent(ctx->event); + ReleaseMutex(ctx->mutex); + + return; +fail: + ReleaseMutex(ctx->mutex); + return; +} + +/** + * Cycle through available devices using the device enumerator devenum, + * retrieve the device with type specified by devtype and return the + * pointer to the object found in *pfilter. + * If pfilter is NULL, list all device names. + */ +static int +dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum, + enum dshowDeviceType devtype, IBaseFilter **pfilter) +{ + struct dshow_ctx *ctx = avctx->priv_data; + IBaseFilter *device_filter = NULL; + IEnumMoniker *classenum = NULL; + IMoniker *m = NULL; + const char *device_name = ctx->device_name[devtype]; + int r; + + const GUID *device_guid[2] = { &CLSID_VideoInputDeviceCategory, + &CLSID_AudioInputDeviceCategory }; + const char *devtypename = (devtype == VideoDevice) ? "video" : "audio"; + + r = ICreateDevEnum_CreateClassEnumerator(devenum, device_guid[devtype], + (IEnumMoniker **) &classenum, 0); + if (r != S_OK) { + av_log(avctx, AV_LOG_ERROR, "Could not enumerate %s devices.\n", + devtypename); + return AVERROR(EIO); + } + + while (!device_filter && IEnumMoniker_Next(classenum, 1, &m, NULL) == S_OK) { + IPropertyBag *bag = NULL; + char *buf = NULL; + VARIANT var; + + r = IMoniker_BindToStorage(m, 0, 0, &IID_IPropertyBag, (void *) &bag); + if (r != S_OK) + goto fail1; + + var.vt = VT_BSTR; + r = IPropertyBag_Read(bag, L"FriendlyName", &var, NULL); + if (r != S_OK) + goto fail1; + + buf = dup_wchar_to_utf8(var.bstrVal); + + if (pfilter) { + if (strcmp(device_name, buf)) + goto fail1; + + IMoniker_BindToObject(m, 0, 0, &IID_IBaseFilter, (void *) &device_filter); + } else { + av_log(avctx, AV_LOG_INFO, " \"%s\"\n", buf); + } + +fail1: + if (buf) + av_free(buf); + if (bag) + IPropertyBag_Release(bag); + IMoniker_Release(m); + } + + IEnumMoniker_Release(classenum); + + if (pfilter) { + if (!device_filter) { + av_log(avctx, AV_LOG_ERROR, "Could not find %s device.\n", + devtypename); + return AVERROR(EIO); + } + *pfilter = device_filter; + } + + return 0; +} + +/** + * Cycle through available formats using the specified pin, + * try to set parameters specified through AVOptions and if successful + * return 1 in *pformat_set. + * If pformat_set is NULL, list all pin capabilities. + */ +static void +dshow_cycle_formats(AVFormatContext *avctx, enum dshowDeviceType devtype, + IPin *pin, int *pformat_set) +{ + struct dshow_ctx *ctx = avctx->priv_data; + IAMStreamConfig *config = NULL; + AM_MEDIA_TYPE *type = NULL; + int format_set = 0; + void *caps = NULL; + int i, n, size; + + if (IPin_QueryInterface(pin, &IID_IAMStreamConfig, (void **) &config) != S_OK) + return; + if (IAMStreamConfig_GetNumberOfCapabilities(config, &n, &size) != S_OK) + goto end; + + caps = av_malloc(size); + if (!caps) + goto end; + + for (i = 0; i < n && !format_set; i++) { + IAMStreamConfig_GetStreamCaps(config, i, &type, (void *) caps); + +#if DSHOWDEBUG + ff_print_AM_MEDIA_TYPE(type); +#endif + + if (devtype == VideoDevice) { + VIDEO_STREAM_CONFIG_CAPS *vcaps = caps; + BITMAPINFOHEADER *bih; + int64_t *fr; +#if DSHOWDEBUG + ff_print_VIDEO_STREAM_CONFIG_CAPS(vcaps); +#endif + if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo)) { + VIDEOINFOHEADER *v = (void *) type->pbFormat; + fr = &v->AvgTimePerFrame; + bih = &v->bmiHeader; + } else if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo2)) { + VIDEOINFOHEADER2 *v = (void *) type->pbFormat; + fr = &v->AvgTimePerFrame; + bih = &v->bmiHeader; + } else { + goto next; + } + if (!pformat_set) { + av_log(avctx, AV_LOG_INFO, " min s=%ldx%ld fps=%g max s=%ldx%ld fps=%g\n", + vcaps->MinOutputSize.cx, vcaps->MinOutputSize.cy, + 1e7 / vcaps->MinFrameInterval, + vcaps->MaxOutputSize.cx, vcaps->MaxOutputSize.cy, + 1e7 / vcaps->MaxFrameInterval); + continue; + } + if (ctx->framerate) { + int64_t framerate = ((int64_t) ctx->requested_framerate.den*10000000) + / ctx->requested_framerate.num; + if (framerate > vcaps->MaxFrameInterval || + framerate < vcaps->MinFrameInterval) + goto next; + *fr = framerate; + } + if (ctx->video_size) { + if (ctx->requested_width > vcaps->MaxOutputSize.cx || + ctx->requested_width < vcaps->MinOutputSize.cx || + ctx->requested_height > vcaps->MaxOutputSize.cy || + ctx->requested_height < vcaps->MinOutputSize.cy) + goto next; + bih->biWidth = ctx->requested_width; + bih->biHeight = ctx->requested_height; + } + } else { + AUDIO_STREAM_CONFIG_CAPS *acaps = caps; + WAVEFORMATEX *fx; +#if DSHOWDEBUG + ff_print_AUDIO_STREAM_CONFIG_CAPS(acaps); +#endif + if (IsEqualGUID(&type->formattype, &FORMAT_WaveFormatEx)) { + fx = (void *) type->pbFormat; + } else { + goto next; + } + if (!pformat_set) { + av_log(avctx, AV_LOG_INFO, " min ch=%lu bits=%lu rate=%6lu max ch=%lu bits=%lu rate=%6lu\n", + acaps->MinimumChannels, acaps->MinimumBitsPerSample, acaps->MinimumSampleFrequency, + acaps->MaximumChannels, acaps->MaximumBitsPerSample, acaps->MaximumSampleFrequency); + continue; + } + if (ctx->sample_rate) { + if (ctx->sample_rate > acaps->MaximumSampleFrequency || + ctx->sample_rate < acaps->MinimumSampleFrequency) + goto next; + fx->nSamplesPerSec = ctx->sample_rate; + } + if (ctx->sample_size) { + if (ctx->sample_size > acaps->MaximumBitsPerSample || + ctx->sample_size < acaps->MinimumBitsPerSample) + goto next; + fx->wBitsPerSample = ctx->sample_size; + } + if (ctx->channels) { + if (ctx->channels > acaps->MaximumChannels || + ctx->channels < acaps->MinimumChannels) + goto next; + fx->nChannels = ctx->channels; + } + } + if (IAMStreamConfig_SetFormat(config, type) != S_OK) + goto next; + format_set = 1; +next: + if (type->pbFormat) + CoTaskMemFree(type->pbFormat); + CoTaskMemFree(type); + } +end: + IAMStreamConfig_Release(config); + if (caps) + av_free(caps); + if (pformat_set) + *pformat_set = format_set; +} + +/** + * Cycle through available pins using the device_filter device, of type + * devtype, retrieve the first output pin and return the pointer to the + * object found in *ppin. + * If ppin is NULL, cycle through all pins listing audio/video capabilities. + */ +static int +dshow_cycle_pins(AVFormatContext *avctx, enum dshowDeviceType devtype, + IBaseFilter *device_filter, IPin **ppin) +{ + struct dshow_ctx *ctx = avctx->priv_data; + IEnumPins *pins = 0; + IPin *device_pin = NULL; + IPin *pin; + int r; + + const GUID *mediatype[2] = { &MEDIATYPE_Video, &MEDIATYPE_Audio }; + const char *devtypename = (devtype == VideoDevice) ? "video" : "audio"; + + int set_format = (devtype == VideoDevice && (ctx->video_size || ctx->framerate)) + || (devtype == AudioDevice && (ctx->channels || ctx->sample_rate)); + int format_set = 0; + + r = IBaseFilter_EnumPins(device_filter, &pins); + if (r != S_OK) { + av_log(avctx, AV_LOG_ERROR, "Could not enumerate pins.\n"); + return AVERROR(EIO); + } + + if (!ppin) { + av_log(avctx, AV_LOG_INFO, "DirectShow %s device options\n", + devtypename); + } + while (!device_pin && IEnumPins_Next(pins, 1, &pin, NULL) == S_OK) { + IKsPropertySet *p = NULL; + IEnumMediaTypes *types = NULL; + PIN_INFO info = {0}; + AM_MEDIA_TYPE *type; + GUID category; + DWORD r2; + + IPin_QueryPinInfo(pin, &info); + IBaseFilter_Release(info.pFilter); + + if (info.dir != PINDIR_OUTPUT) + goto next; + if (IPin_QueryInterface(pin, &IID_IKsPropertySet, (void **) &p) != S_OK) + goto next; + if (IKsPropertySet_Get(p, &ROPSETID_Pin, AMPROPERTY_PIN_CATEGORY, + NULL, 0, &category, sizeof(GUID), &r2) != S_OK) + goto next; + if (!IsEqualGUID(&category, &PIN_CATEGORY_CAPTURE)) + goto next; + + if (!ppin) { + char *buf = dup_wchar_to_utf8(info.achName); + av_log(avctx, AV_LOG_INFO, " Pin \"%s\"\n", buf); + av_free(buf); + dshow_cycle_formats(avctx, devtype, pin, NULL); + goto next; + } + if (set_format) { + dshow_cycle_formats(avctx, devtype, pin, &format_set); + if (!format_set) { + goto next; + } + } + + if (IPin_EnumMediaTypes(pin, &types) != S_OK) + goto next; + + IEnumMediaTypes_Reset(types); + while (!device_pin && IEnumMediaTypes_Next(types, 1, &type, NULL) == S_OK) { + if (IsEqualGUID(&type->majortype, mediatype[devtype])) { + device_pin = pin; + goto next; + } + CoTaskMemFree(type); + } + +next: + if (types) + IEnumMediaTypes_Release(types); + if (p) + IKsPropertySet_Release(p); + if (device_pin != pin) + IPin_Release(pin); + } + + IEnumPins_Release(pins); + + if (ppin) { + if (set_format && !format_set) { + av_log(avctx, AV_LOG_ERROR, "Could not set %s options\n", devtypename); + return AVERROR(EIO); + } + if (!device_pin) { + av_log(avctx, AV_LOG_ERROR, + "Could not find output pin from %s capture device.\n", devtypename); + return AVERROR(EIO); + } + *ppin = device_pin; + } + + return 0; +} + +/** + * List options for device with type devtype. + * + * @param devenum device enumerator used for accessing the device + */ +static int +dshow_list_device_options(AVFormatContext *avctx, ICreateDevEnum *devenum, + enum dshowDeviceType devtype) +{ + IBaseFilter *device_filter = NULL; + int r; + + if ((r = dshow_cycle_devices(avctx, devenum, devtype, &device_filter)) < 0) + return r; + if ((r = dshow_cycle_pins(avctx, devtype, device_filter, NULL)) < 0) + return r; + + return 0; +} + +static int +dshow_open_device(AVFormatContext *avctx, ICreateDevEnum *devenum, + enum dshowDeviceType devtype) +{ + struct dshow_ctx *ctx = avctx->priv_data; + IBaseFilter *device_filter = NULL; + IGraphBuilder *graph = ctx->graph; + IPin *device_pin = NULL; + libAVPin *capture_pin = NULL; + libAVFilter *capture_filter = NULL; + int ret = AVERROR(EIO); + int r; + + const wchar_t *filter_name[2] = { L"Audio capture filter", L"Video capture filter" }; + + if ((r = dshow_cycle_devices(avctx, devenum, devtype, &device_filter)) < 0) { + ret = r; + goto error; + } + + ctx->device_filter [devtype] = device_filter; + + r = IGraphBuilder_AddFilter(graph, device_filter, NULL); + if (r != S_OK) { + av_log(avctx, AV_LOG_ERROR, "Could not add device filter to graph.\n"); + goto error; + } + + if ((r = dshow_cycle_pins(avctx, devtype, device_filter, &device_pin)) < 0) { + ret = r; + goto error; + } + ctx->device_pin[devtype] = device_pin; + + capture_filter = libAVFilter_Create(avctx, callback, devtype); + if (!capture_filter) { + av_log(avctx, AV_LOG_ERROR, "Could not create grabber filter.\n"); + goto error; + } + ctx->capture_filter[devtype] = capture_filter; + + r = IGraphBuilder_AddFilter(graph, (IBaseFilter *) capture_filter, + filter_name[devtype]); + if (r != S_OK) { + av_log(avctx, AV_LOG_ERROR, "Could not add capture filter to graph\n"); + goto error; + } + + libAVPin_AddRef(capture_filter->pin); + capture_pin = capture_filter->pin; + ctx->capture_pin[devtype] = capture_pin; + + r = IGraphBuilder_ConnectDirect(graph, device_pin, (IPin *) capture_pin, NULL); + if (r != S_OK) { + av_log(avctx, AV_LOG_ERROR, "Could not connect pins\n"); + goto error; + } + + ret = 0; + +error: + return ret; +} + +static enum CodecID waveform_codec_id(enum AVSampleFormat sample_fmt) +{ + switch (sample_fmt) { + case AV_SAMPLE_FMT_U8: return CODEC_ID_PCM_U8; + case AV_SAMPLE_FMT_S16: return CODEC_ID_PCM_S16LE; + case AV_SAMPLE_FMT_S32: return CODEC_ID_PCM_S32LE; + default: return CODEC_ID_NONE; /* Should never happen. */ + } +} + +static enum SampleFormat sample_fmt_bits_per_sample(int bits) +{ + switch (bits) { + case 8: return AV_SAMPLE_FMT_U8; + case 16: return AV_SAMPLE_FMT_S16; + case 32: return AV_SAMPLE_FMT_S32; + default: return AV_SAMPLE_FMT_NONE; /* Should never happen. */ + } +} + +static int +dshow_add_device(AVFormatContext *avctx, AVFormatParameters *ap, + enum dshowDeviceType devtype) +{ + struct dshow_ctx *ctx = avctx->priv_data; + AM_MEDIA_TYPE type; + AVCodecContext *codec; + AVStream *st; + int ret = AVERROR(EIO); + + st = av_new_stream(avctx, devtype); + if (!st) { + ret = AVERROR(ENOMEM); + goto error; + } + + ctx->capture_filter[devtype]->stream_index = st->index; + + libAVPin_ConnectionMediaType(ctx->capture_pin[devtype], &type); + + codec = st->codec; + if (devtype == VideoDevice) { + BITMAPINFOHEADER *bih = NULL; + + if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo)) { + VIDEOINFOHEADER *v = (void *) type.pbFormat; + bih = &v->bmiHeader; + } else if (IsEqualGUID(&type.formattype, &FORMAT_VideoInfo2)) { + VIDEOINFOHEADER2 *v = (void *) type.pbFormat; + bih = &v->bmiHeader; + } + if (!bih) { + av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n"); + goto error; + } + + codec->time_base = ap->time_base; + codec->codec_type = AVMEDIA_TYPE_VIDEO; + codec->width = bih->biWidth; + codec->height = bih->biHeight; + codec->pix_fmt = dshow_pixfmt(bih->biCompression, bih->biBitCount); + if (codec->pix_fmt == PIX_FMT_NONE) { + codec->codec_id = dshow_codecid(bih->biCompression); + if (codec->codec_id == CODEC_ID_NONE) { + av_log(avctx, AV_LOG_ERROR, "Unknown compression type. " + "Please report verbose (-v 9) debug information.\n"); + dshow_read_close(avctx); + return AVERROR_PATCHWELCOME; + } + codec->bits_per_coded_sample = bih->biBitCount; + } else { + codec->codec_id = CODEC_ID_RAWVIDEO; + if (bih->biCompression == BI_RGB) { + codec->bits_per_coded_sample = bih->biBitCount; + codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE); + if (codec->extradata) { + codec->extradata_size = 9; + memcpy(codec->extradata, "BottomUp", 9); + } + } + } + } else { + WAVEFORMATEX *fx = NULL; + + if (IsEqualGUID(&type.formattype, &FORMAT_WaveFormatEx)) { + fx = (void *) type.pbFormat; + } + if (!fx) { + av_log(avctx, AV_LOG_ERROR, "Could not get media type.\n"); + goto error; + } + + codec->codec_type = AVMEDIA_TYPE_AUDIO; + codec->sample_fmt = sample_fmt_bits_per_sample(fx->wBitsPerSample); + codec->codec_id = waveform_codec_id(codec->sample_fmt); + codec->sample_rate = fx->nSamplesPerSec; + codec->channels = fx->nChannels; + } + + av_set_pts_info(st, 64, 1, 10000000); + + ret = 0; + +error: + return ret; +} + +static int parse_device_name(AVFormatContext *avctx) +{ + struct dshow_ctx *ctx = avctx->priv_data; + char **device_name = ctx->device_name; + char *name = av_strdup(avctx->filename); + char *tmp = name; + int ret = 1; + char *type; + + while ((type = strtok(tmp, "="))) { + char *token = strtok(NULL, ":"); + tmp = NULL; + + if (!strcmp(type, "video")) { + device_name[0] = token; + } else if (!strcmp(type, "audio")) { + device_name[1] = token; + } else { + device_name[0] = NULL; + device_name[1] = NULL; + break; + } + } + + if (!device_name[0] && !device_name[1]) { + ret = 0; + } else { + if (device_name[0]) + device_name[0] = av_strdup(device_name[0]); + if (device_name[1]) + device_name[1] = av_strdup(device_name[1]); + } + + av_free(name); + return ret; +} + +static int dshow_read_header(AVFormatContext *avctx, AVFormatParameters *ap) +{ + struct dshow_ctx *ctx = avctx->priv_data; + IGraphBuilder *graph = NULL; + ICreateDevEnum *devenum = NULL; + IMediaControl *control = NULL; + int ret = AVERROR(EIO); + int r; + + if (!ctx->list_devices && !parse_device_name(avctx)) { + av_log(avctx, AV_LOG_ERROR, "Malformed dshow input string.\n"); + goto error; + } + + if (ctx->video_size) { + r = av_parse_video_size(&ctx->requested_width, &ctx->requested_height, ctx->video_size); + if (r < 0) { + av_log(avctx, AV_LOG_ERROR, "Could not parse video size '%s'.\n", ctx->video_size); + goto error; + } + } + if (ctx->framerate) { + r = av_parse_video_rate(&ctx->requested_framerate, ctx->framerate); + if (r < 0) { + av_log(avctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", ctx->framerate); + goto error; + } + } + + CoInitialize(0); + + r = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, + &IID_IGraphBuilder, (void **) &graph); + if (r != S_OK) { + av_log(avctx, AV_LOG_ERROR, "Could not create capture graph.\n"); + goto error; + } + ctx->graph = graph; + + r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, + &IID_ICreateDevEnum, (void **) &devenum); + if (r != S_OK) { + av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n"); + goto error; + } + + if (ctx->list_devices) { + av_log(avctx, AV_LOG_INFO, "DirectShow video devices\n"); + dshow_cycle_devices(avctx, devenum, VideoDevice, NULL); + av_log(avctx, AV_LOG_INFO, "DirectShow audio devices\n"); + dshow_cycle_devices(avctx, devenum, AudioDevice, NULL); + ret = AVERROR_EXIT; + goto error; + } + if (ctx->list_options) { + if (ctx->device_name[VideoDevice]) + dshow_list_device_options(avctx, devenum, VideoDevice); + if (ctx->device_name[AudioDevice]) + dshow_list_device_options(avctx, devenum, AudioDevice); + ret = AVERROR_EXIT; + goto error; + } + + if (ctx->device_name[VideoDevice]) { + ret = dshow_open_device(avctx, devenum, VideoDevice); + if (ret < 0) + goto error; + ret = dshow_add_device(avctx, ap, VideoDevice); + if (ret < 0) + goto error; + } + if (ctx->device_name[AudioDevice]) { + ret = dshow_open_device(avctx, devenum, AudioDevice); + if (ret < 0) + goto error; + ret = dshow_add_device(avctx, ap, AudioDevice); + if (ret < 0) + goto error; + } + + ctx->mutex = CreateMutex(NULL, 0, NULL); + if (!ctx->mutex) { + av_log(avctx, AV_LOG_ERROR, "Could not create Mutex\n"); + goto error; + } + ctx->event = CreateEvent(NULL, 1, 0, NULL); + if (!ctx->event) { + av_log(avctx, AV_LOG_ERROR, "Could not create Event\n"); + goto error; + } + + r = IGraphBuilder_QueryInterface(graph, &IID_IMediaControl, (void **) &control); + if (r != S_OK) { + av_log(avctx, AV_LOG_ERROR, "Could not get media control.\n"); + goto error; + } + ctx->control = control; + + r = IMediaControl_Run(control); + if (r == S_FALSE) { + OAFilterState pfs; + r = IMediaControl_GetState(control, 0, &pfs); + } + if (r != S_OK) { + av_log(avctx, AV_LOG_ERROR, "Could not run filter\n"); + goto error; + } + + ret = 0; + +error: + + if (ret < 0) + dshow_read_close(avctx); + + if (devenum) + ICreateDevEnum_Release(devenum); + + return ret; +} + +static int dshow_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + struct dshow_ctx *ctx = s->priv_data; + AVPacketList *pktl = NULL; + + while (!pktl) { + WaitForSingleObject(ctx->mutex, INFINITE); + pktl = ctx->pktl; + if (ctx->pktl) { + *pkt = ctx->pktl->pkt; + ctx->pktl = ctx->pktl->next; + av_free(pktl); + } + ResetEvent(ctx->event); + ReleaseMutex(ctx->mutex); + if (!pktl) { + if (s->flags & AVFMT_FLAG_NONBLOCK) { + return AVERROR(EAGAIN); + } else { + WaitForSingleObject(ctx->event, INFINITE); + } + } + } + + ctx->curbufsize -= pkt->size; + + return pkt->size; +} + +#define OFFSET(x) offsetof(struct dshow_ctx, x) +#define DEC AV_OPT_FLAG_DECODING_PARAM +static const AVOption options[] = { + { "video_size", "set video size given a string such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, + { "framerate", "set video frame rate", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, + { "sample_rate", "set audio sample rate", OFFSET(sample_rate), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, DEC }, + { "sample_size", "set audio sample size", OFFSET(sample_size), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 16, DEC }, + { "channels", "set number of audio channels, such as 1 or 2", OFFSET(channels), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, DEC }, + { "list_devices", "list available devices", OFFSET(list_devices), FF_OPT_TYPE_INT, {.dbl=0}, 0, 1, DEC, "list_devices" }, + { "true", "", 0, FF_OPT_TYPE_CONST, {.dbl=1}, 0, 0, DEC, "list_devices" }, + { "false", "", 0, FF_OPT_TYPE_CONST, {.dbl=0}, 0, 0, DEC, "list_devices" }, + { "list_options", "list available options for specified device", OFFSET(list_options), FF_OPT_TYPE_INT, {.dbl=0}, 0, 1, DEC, "list_options" }, + { "true", "", 0, FF_OPT_TYPE_CONST, {.dbl=1}, 0, 0, DEC, "list_options" }, + { "false", "", 0, FF_OPT_TYPE_CONST, {.dbl=0}, 0, 0, DEC, "list_options" }, + { NULL }, +}; + +static const AVClass dshow_class = { + .class_name = "DirectShow indev", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVInputFormat ff_dshow_demuxer = { + "dshow", + NULL_IF_CONFIG_SMALL("DirectShow capture"), + sizeof(struct dshow_ctx), + NULL, + dshow_read_header, + dshow_read_packet, + dshow_read_close, + .flags = AVFMT_NOFILE, + .priv_class = &dshow_class, +}; diff --git a/libavdevice/dshow.h b/libavdevice/dshow.h new file mode 100644 index 0000000..83c71c4 --- /dev/null +++ b/libavdevice/dshow.h @@ -0,0 +1,268 @@ +/* + * DirectShow capture interface + * Copyright (c) 2010 Ramiro Polla + * + * 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 + */ + +#define DSHOWDEBUG 0 + +#include "avdevice.h" + +#define COBJMACROS +#include <windows.h> +#include <dshow.h> +#include <dvdmedia.h> + +long ff_copy_dshow_media_type(AM_MEDIA_TYPE *dst, const AM_MEDIA_TYPE *src); +void ff_print_VIDEO_STREAM_CONFIG_CAPS(const VIDEO_STREAM_CONFIG_CAPS *caps); +void ff_print_AUDIO_STREAM_CONFIG_CAPS(const AUDIO_STREAM_CONFIG_CAPS *caps); +void ff_print_AM_MEDIA_TYPE(const AM_MEDIA_TYPE *type); +void ff_printGUID(const GUID *g); + +#if DSHOWDEBUG +extern const AVClass *ff_dshow_context_class_ptr; +#define dshowdebug(...) av_log(&ff_dshow_context_class_ptr, AV_LOG_DEBUG, __VA_ARGS__) +#else +#define dshowdebug(...) +#endif + +static inline void nothing(void *foo) +{ +} + +struct GUIDoffset { + const GUID *iid; + int offset; +}; + +enum dshowDeviceType { + VideoDevice = 0, + AudioDevice = 1, +}; + +#define DECLARE_QUERYINTERFACE(class, ...) \ +long WINAPI \ +class##_QueryInterface(class *this, const GUID *riid, void **ppvObject) \ +{ \ + struct GUIDoffset ifaces[] = __VA_ARGS__; \ + int i; \ + dshowdebug(AV_STRINGIFY(class)"_QueryInterface(%p, %p, %p)\n", this, riid, ppvObject); \ + ff_printGUID(riid); \ + if (!ppvObject) \ + return E_POINTER; \ + for (i = 0; i < sizeof(ifaces)/sizeof(ifaces[0]); i++) { \ + if (IsEqualGUID(riid, ifaces[i].iid)) { \ + void *obj = (void *) ((uint8_t *) this + ifaces[i].offset); \ + class##_AddRef(this); \ + dshowdebug("\tfound %d with offset %d\n", i, ifaces[i].offset); \ + *ppvObject = (void *) obj; \ + return S_OK; \ + } \ + } \ + dshowdebug("\tE_NOINTERFACE\n"); \ + *ppvObject = NULL; \ + return E_NOINTERFACE; \ +} +#define DECLARE_ADDREF(class) \ +unsigned long WINAPI \ +class##_AddRef(class *this) \ +{ \ + dshowdebug(AV_STRINGIFY(class)"_AddRef(%p)\t%ld\n", this, this->ref+1); \ + return InterlockedIncrement(&this->ref); \ +} +#define DECLARE_RELEASE(class) \ +unsigned long WINAPI \ +class##_Release(class *this) \ +{ \ + long ref = InterlockedDecrement(&this->ref); \ + dshowdebug(AV_STRINGIFY(class)"_Release(%p)\t%ld\n", this, ref); \ + if (!ref) \ + class##_Destroy(this); \ + return ref; \ +} + +#define DECLARE_DESTROY(class, func) \ +void class##_Destroy(class *this) \ +{ \ + dshowdebug(AV_STRINGIFY(class)"_Destroy(%p)\n", this); \ + func(this); \ + if (this) { \ + if (this->vtbl) \ + CoTaskMemFree(this->vtbl); \ + CoTaskMemFree(this); \ + } \ +} +#define DECLARE_CREATE(class, setup, ...) \ +class *class##_Create(__VA_ARGS__) \ +{ \ + class *this = CoTaskMemAlloc(sizeof(class)); \ + void *vtbl = CoTaskMemAlloc(sizeof(*this->vtbl)); \ + dshowdebug(AV_STRINGIFY(class)"_Create(%p)\n", this); \ + if (!this || !vtbl) \ + goto fail; \ + ZeroMemory(this, sizeof(class)); \ + ZeroMemory(vtbl, sizeof(*this->vtbl)); \ + this->ref = 1; \ + this->vtbl = vtbl; \ + if (!setup) \ + goto fail; \ + dshowdebug("created "AV_STRINGIFY(class)" %p\n", this); \ + return this; \ +fail: \ + class##_Destroy(this); \ + dshowdebug("could not create "AV_STRINGIFY(class)"\n"); \ + return NULL; \ +} + +#define SETVTBL(vtbl, class, fn) \ + do { (vtbl)->fn = (void *) class##_##fn; } while(0) + +/***************************************************************************** + * Forward Declarations + ****************************************************************************/ +typedef struct libAVPin libAVPin; +typedef struct libAVMemInputPin libAVMemInputPin; +typedef struct libAVEnumPins libAVEnumPins; +typedef struct libAVEnumMediaTypes libAVEnumMediaTypes; +typedef struct libAVFilter libAVFilter; + +/***************************************************************************** + * libAVPin + ****************************************************************************/ +struct libAVPin { + IPinVtbl *vtbl; + long ref; + libAVFilter *filter; + IPin *connectedto; + AM_MEDIA_TYPE type; + IMemInputPinVtbl *imemvtbl; +}; + +long WINAPI libAVPin_QueryInterface (libAVPin *, const GUID *, void **); +unsigned long WINAPI libAVPin_AddRef (libAVPin *); +unsigned long WINAPI libAVPin_Release (libAVPin *); +long WINAPI libAVPin_Connect (libAVPin *, IPin *, const AM_MEDIA_TYPE *); +long WINAPI libAVPin_ReceiveConnection (libAVPin *, IPin *, const AM_MEDIA_TYPE *); +long WINAPI libAVPin_Disconnect (libAVPin *); +long WINAPI libAVPin_ConnectedTo (libAVPin *, IPin **); +long WINAPI libAVPin_ConnectionMediaType (libAVPin *, AM_MEDIA_TYPE *); +long WINAPI libAVPin_QueryPinInfo (libAVPin *, PIN_INFO *); +long WINAPI libAVPin_QueryDirection (libAVPin *, PIN_DIRECTION *); +long WINAPI libAVPin_QueryId (libAVPin *, wchar_t **); +long WINAPI libAVPin_QueryAccept (libAVPin *, const AM_MEDIA_TYPE *); +long WINAPI libAVPin_EnumMediaTypes (libAVPin *, IEnumMediaTypes **); +long WINAPI libAVPin_QueryInternalConnections(libAVPin *, IPin **, unsigned long *); +long WINAPI libAVPin_EndOfStream (libAVPin *); +long WINAPI libAVPin_BeginFlush (libAVPin *); +long WINAPI libAVPin_EndFlush (libAVPin *); +long WINAPI libAVPin_NewSegment (libAVPin *, REFERENCE_TIME, REFERENCE_TIME, double); + +long WINAPI libAVMemInputPin_QueryInterface (libAVMemInputPin *, const GUID *, void **); +unsigned long WINAPI libAVMemInputPin_AddRef (libAVMemInputPin *); +unsigned long WINAPI libAVMemInputPin_Release (libAVMemInputPin *); +long WINAPI libAVMemInputPin_GetAllocator (libAVMemInputPin *, IMemAllocator **); +long WINAPI libAVMemInputPin_NotifyAllocator (libAVMemInputPin *, IMemAllocator *, WINBOOL); +long WINAPI libAVMemInputPin_GetAllocatorRequirements(libAVMemInputPin *, ALLOCATOR_PROPERTIES *); +long WINAPI libAVMemInputPin_Receive (libAVMemInputPin *, IMediaSample *); +long WINAPI libAVMemInputPin_ReceiveMultiple (libAVMemInputPin *, IMediaSample **, long, long *); +long WINAPI libAVMemInputPin_ReceiveCanBlock (libAVMemInputPin *); + +void libAVPin_Destroy(libAVPin *); +libAVPin *libAVPin_Create (libAVFilter *filter); + +void libAVMemInputPin_Destroy(libAVMemInputPin *); + +/***************************************************************************** + * libAVEnumPins + ****************************************************************************/ +struct libAVEnumPins { + IEnumPinsVtbl *vtbl; + long ref; + int pos; + libAVPin *pin; + libAVFilter *filter; +}; + +long WINAPI libAVEnumPins_QueryInterface(libAVEnumPins *, const GUID *, void **); +unsigned long WINAPI libAVEnumPins_AddRef (libAVEnumPins *); +unsigned long WINAPI libAVEnumPins_Release (libAVEnumPins *); +long WINAPI libAVEnumPins_Next (libAVEnumPins *, unsigned long, IPin **, unsigned long *); +long WINAPI libAVEnumPins_Skip (libAVEnumPins *, unsigned long); +long WINAPI libAVEnumPins_Reset (libAVEnumPins *); +long WINAPI libAVEnumPins_Clone (libAVEnumPins *, libAVEnumPins **); + +void libAVEnumPins_Destroy(libAVEnumPins *); +libAVEnumPins *libAVEnumPins_Create (libAVPin *pin, libAVFilter *filter); + +/***************************************************************************** + * libAVEnumMediaTypes + ****************************************************************************/ +struct libAVEnumMediaTypes { + IEnumPinsVtbl *vtbl; + long ref; + int pos; + AM_MEDIA_TYPE type; +}; + +long WINAPI libAVEnumMediaTypes_QueryInterface(libAVEnumMediaTypes *, const GUID *, void **); +unsigned long WINAPI libAVEnumMediaTypes_AddRef (libAVEnumMediaTypes *); +unsigned long WINAPI libAVEnumMediaTypes_Release (libAVEnumMediaTypes *); +long WINAPI libAVEnumMediaTypes_Next (libAVEnumMediaTypes *, unsigned long, AM_MEDIA_TYPE **, unsigned long *); +long WINAPI libAVEnumMediaTypes_Skip (libAVEnumMediaTypes *, unsigned long); +long WINAPI libAVEnumMediaTypes_Reset (libAVEnumMediaTypes *); +long WINAPI libAVEnumMediaTypes_Clone (libAVEnumMediaTypes *, libAVEnumMediaTypes **); + +void libAVEnumMediaTypes_Destroy(libAVEnumMediaTypes *); +libAVEnumMediaTypes *libAVEnumMediaTypes_Create(const AM_MEDIA_TYPE *type); + +/***************************************************************************** + * libAVFilter + ****************************************************************************/ +struct libAVFilter { + IBaseFilterVtbl *vtbl; + long ref; + const wchar_t *name; + libAVPin *pin; + FILTER_INFO info; + FILTER_STATE state; + IReferenceClock *clock; + enum dshowDeviceType type; + void *priv_data; + int stream_index; + int64_t start_time; + void (*callback)(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time); +}; + +long WINAPI libAVFilter_QueryInterface (libAVFilter *, const GUID *, void **); +unsigned long WINAPI libAVFilter_AddRef (libAVFilter *); +unsigned long WINAPI libAVFilter_Release (libAVFilter *); +long WINAPI libAVFilter_GetClassID (libAVFilter *, CLSID *); +long WINAPI libAVFilter_Stop (libAVFilter *); +long WINAPI libAVFilter_Pause (libAVFilter *); +long WINAPI libAVFilter_Run (libAVFilter *, REFERENCE_TIME); +long WINAPI libAVFilter_GetState (libAVFilter *, DWORD, FILTER_STATE *); +long WINAPI libAVFilter_SetSyncSource (libAVFilter *, IReferenceClock *); +long WINAPI libAVFilter_GetSyncSource (libAVFilter *, IReferenceClock **); +long WINAPI libAVFilter_EnumPins (libAVFilter *, IEnumPins **); +long WINAPI libAVFilter_FindPin (libAVFilter *, const wchar_t *, IPin **); +long WINAPI libAVFilter_QueryFilterInfo(libAVFilter *, FILTER_INFO *); +long WINAPI libAVFilter_JoinFilterGraph(libAVFilter *, IFilterGraph *, const wchar_t *); +long WINAPI libAVFilter_QueryVendorInfo(libAVFilter *, wchar_t **); + +void libAVFilter_Destroy(libAVFilter *); +libAVFilter *libAVFilter_Create (void *, void *, enum dshowDeviceType); diff --git a/libavdevice/dshow_common.c b/libavdevice/dshow_common.c new file mode 100644 index 0000000..8fe2f77 --- /dev/null +++ b/libavdevice/dshow_common.c @@ -0,0 +1,190 @@ +/* + * Directshow capture interface + * Copyright (c) 2010 Ramiro Polla + * + * 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 "dshow.h" + +long ff_copy_dshow_media_type(AM_MEDIA_TYPE *dst, const AM_MEDIA_TYPE *src) +{ + uint8_t *pbFormat = NULL; + + if (src->cbFormat) { + pbFormat = CoTaskMemAlloc(src->cbFormat); + if (!pbFormat) + return E_OUTOFMEMORY; + memcpy(pbFormat, src->pbFormat, src->cbFormat); + } + + *dst = *src; + dst->pUnk = NULL; + dst->pbFormat = pbFormat; + + return S_OK; +} + +void ff_printGUID(const GUID *g) +{ +#if DSHOWDEBUG + const uint32_t *d = (const uint32_t *) &g->Data1; + const uint16_t *w = (const uint16_t *) &g->Data2; + const uint8_t *c = (const uint8_t *) &g->Data4; + + dshowdebug("0x%08x 0x%04x 0x%04x %02x%02x%02x%02x%02x%02x%02x%02x", + d[0], w[0], w[1], + c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7]); +#endif +} + +static const char *dshow_context_to_name(void *ptr) +{ + return "dshow"; +} +static const AVClass ff_dshow_context_class = { "DirectShow", dshow_context_to_name }; +const AVClass *ff_dshow_context_class_ptr = &ff_dshow_context_class; + +#define dstruct(pctx, sname, var, type) \ + dshowdebug(" "#var":\t%"type"\n", sname->var) + +#if DSHOWDEBUG +static void dump_bih(void *s, BITMAPINFOHEADER *bih) +{ + dshowdebug(" BITMAPINFOHEADER\n"); + dstruct(s, bih, biSize, "lu"); + dstruct(s, bih, biWidth, "ld"); + dstruct(s, bih, biHeight, "ld"); + dstruct(s, bih, biPlanes, "d"); + dstruct(s, bih, biBitCount, "d"); + dstruct(s, bih, biCompression, "lu"); + dshowdebug(" biCompression:\t\"%.4s\"\n", + (char*) &bih->biCompression); + dstruct(s, bih, biSizeImage, "lu"); + dstruct(s, bih, biXPelsPerMeter, "lu"); + dstruct(s, bih, biYPelsPerMeter, "lu"); + dstruct(s, bih, biClrUsed, "lu"); + dstruct(s, bih, biClrImportant, "lu"); +} +#endif + +void ff_print_VIDEO_STREAM_CONFIG_CAPS(const VIDEO_STREAM_CONFIG_CAPS *caps) +{ +#if DSHOWDEBUG + dshowdebug(" VIDEO_STREAM_CONFIG_CAPS\n"); + dshowdebug(" guid\t"); + ff_printGUID(&caps->guid); + dshowdebug("\n"); + dshowdebug(" VideoStandard\t%lu\n", caps->VideoStandard); + dshowdebug(" InputSize %ld\t%ld\n", caps->InputSize.cx, caps->InputSize.cy); + dshowdebug(" MinCroppingSize %ld\t%ld\n", caps->MinCroppingSize.cx, caps->MinCroppingSize.cy); + dshowdebug(" MaxCroppingSize %ld\t%ld\n", caps->MaxCroppingSize.cx, caps->MaxCroppingSize.cy); + dshowdebug(" CropGranularityX\t%d\n", caps->CropGranularityX); + dshowdebug(" CropGranularityY\t%d\n", caps->CropGranularityY); + dshowdebug(" CropAlignX\t%d\n", caps->CropAlignX); + dshowdebug(" CropAlignY\t%d\n", caps->CropAlignY); + dshowdebug(" MinOutputSize %ld\t%ld\n", caps->MinOutputSize.cx, caps->MinOutputSize.cy); + dshowdebug(" MaxOutputSize %ld\t%ld\n", caps->MaxOutputSize.cx, caps->MaxOutputSize.cy); + dshowdebug(" OutputGranularityX\t%d\n", caps->OutputGranularityX); + dshowdebug(" OutputGranularityY\t%d\n", caps->OutputGranularityY); + dshowdebug(" StretchTapsX\t%d\n", caps->StretchTapsX); + dshowdebug(" StretchTapsY\t%d\n", caps->StretchTapsY); + dshowdebug(" ShrinkTapsX\t%d\n", caps->ShrinkTapsX); + dshowdebug(" ShrinkTapsY\t%d\n", caps->ShrinkTapsY); + dshowdebug(" MinFrameInterval\t%"PRId64"\n", caps->MinFrameInterval); + dshowdebug(" MaxFrameInterval\t%"PRId64"\n", caps->MaxFrameInterval); + dshowdebug(" MinBitsPerSecond\t%ld\n", caps->MinBitsPerSecond); + dshowdebug(" MaxBitsPerSecond\t%ld\n", caps->MaxBitsPerSecond); +#endif +} + +void ff_print_AUDIO_STREAM_CONFIG_CAPS(const AUDIO_STREAM_CONFIG_CAPS *caps) +{ +#if DSHOWDEBUG + dshowdebug(" AUDIO_STREAM_CONFIG_CAPS\n"); + dshowdebug(" guid\t"); + ff_printGUID(&caps->guid); + dshowdebug("\n"); + dshowdebug(" MinimumChannels\t%lu\n", caps->MinimumChannels); + dshowdebug(" MaximumChannels\t%lu\n", caps->MaximumChannels); + dshowdebug(" ChannelsGranularity\t%lu\n", caps->ChannelsGranularity); + dshowdebug(" MinimumBitsPerSample\t%lu\n", caps->MinimumBitsPerSample); + dshowdebug(" MaximumBitsPerSample\t%lu\n", caps->MaximumBitsPerSample); + dshowdebug(" BitsPerSampleGranularity\t%lu\n", caps->BitsPerSampleGranularity); + dshowdebug(" MinimumSampleFrequency\t%lu\n", caps->MinimumSampleFrequency); + dshowdebug(" MaximumSampleFrequency\t%lu\n", caps->MaximumSampleFrequency); + dshowdebug(" SampleFrequencyGranularity\t%lu\n", caps->SampleFrequencyGranularity); +#endif +} + +void ff_print_AM_MEDIA_TYPE(const AM_MEDIA_TYPE *type) +{ +#if DSHOWDEBUG + dshowdebug(" majortype\t"); + ff_printGUID(&type->majortype); + dshowdebug("\n"); + dshowdebug(" subtype\t"); + ff_printGUID(&type->subtype); + dshowdebug("\n"); + dshowdebug(" bFixedSizeSamples\t%d\n", type->bFixedSizeSamples); + dshowdebug(" bTemporalCompression\t%d\n", type->bTemporalCompression); + dshowdebug(" lSampleSize\t%lu\n", type->lSampleSize); + dshowdebug(" formattype\t"); + ff_printGUID(&type->formattype); + dshowdebug("\n"); + dshowdebug(" pUnk\t%p\n", type->pUnk); + dshowdebug(" cbFormat\t%lu\n", type->cbFormat); + dshowdebug(" pbFormat\t%p\n", type->pbFormat); + + if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo)) { + VIDEOINFOHEADER *v = (void *) type->pbFormat; + dshowdebug(" rcSource: left %ld top %ld right %ld bottom %ld\n", + v->rcSource.left, v->rcSource.top, v->rcSource.right, v->rcSource.bottom); + dshowdebug(" rcTarget: left %ld top %ld right %ld bottom %ld\n", + v->rcTarget.left, v->rcTarget.top, v->rcTarget.right, v->rcTarget.bottom); + dshowdebug(" dwBitRate: %lu\n", v->dwBitRate); + dshowdebug(" dwBitErrorRate: %lu\n", v->dwBitErrorRate); + dshowdebug(" AvgTimePerFrame: %"PRId64"\n", v->AvgTimePerFrame); + dump_bih(NULL, &v->bmiHeader); + } else if (IsEqualGUID(&type->formattype, &FORMAT_VideoInfo2)) { + VIDEOINFOHEADER2 *v = (void *) type->pbFormat; + dshowdebug(" rcSource: left %ld top %ld right %ld bottom %ld\n", + v->rcSource.left, v->rcSource.top, v->rcSource.right, v->rcSource.bottom); + dshowdebug(" rcTarget: left %ld top %ld right %ld bottom %ld\n", + v->rcTarget.left, v->rcTarget.top, v->rcTarget.right, v->rcTarget.bottom); + dshowdebug(" dwBitRate: %lu\n", v->dwBitRate); + dshowdebug(" dwBitErrorRate: %lu\n", v->dwBitErrorRate); + dshowdebug(" AvgTimePerFrame: %"PRId64"\n", v->AvgTimePerFrame); + dshowdebug(" dwInterlaceFlags: %lu\n", v->dwInterlaceFlags); + dshowdebug(" dwCopyProtectFlags: %lu\n", v->dwCopyProtectFlags); + dshowdebug(" dwPictAspectRatioX: %lu\n", v->dwPictAspectRatioX); + dshowdebug(" dwPictAspectRatioY: %lu\n", v->dwPictAspectRatioY); +// dshowdebug(" dwReserved1: %lu\n", v->u.dwReserved1); /* mingw-w64 is buggy and doesn't name unnamed unions */ + dshowdebug(" dwReserved2: %lu\n", v->dwReserved2); + dump_bih(NULL, &v->bmiHeader); + } else if (IsEqualGUID(&type->formattype, &FORMAT_WaveFormatEx)) { + WAVEFORMATEX *fx = (void *) type->pbFormat; + dshowdebug(" wFormatTag: %u\n", fx->wFormatTag); + dshowdebug(" nChannels: %u\n", fx->nChannels); + dshowdebug(" nSamplesPerSec: %lu\n", fx->nSamplesPerSec); + dshowdebug(" nAvgBytesPerSec: %lu\n", fx->nAvgBytesPerSec); + dshowdebug(" nBlockAlign: %u\n", fx->nBlockAlign); + dshowdebug(" wBitsPerSample: %u\n", fx->wBitsPerSample); + dshowdebug(" cbSize: %u\n", fx->cbSize); + } +#endif +} diff --git a/libavdevice/dshow_enummediatypes.c b/libavdevice/dshow_enummediatypes.c new file mode 100644 index 0000000..a700133 --- /dev/null +++ b/libavdevice/dshow_enummediatypes.c @@ -0,0 +1,103 @@ +/* + * DirectShow capture interface + * Copyright (c) 2010 Ramiro Polla + * + * 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 "dshow.h" + +DECLARE_QUERYINTERFACE(libAVEnumMediaTypes, + { {&IID_IUnknown,0}, {&IID_IEnumPins,0} }) +DECLARE_ADDREF(libAVEnumMediaTypes) +DECLARE_RELEASE(libAVEnumMediaTypes) + +long WINAPI +libAVEnumMediaTypes_Next(libAVEnumMediaTypes *this, unsigned long n, + AM_MEDIA_TYPE **types, unsigned long *fetched) +{ + int count = 0; + dshowdebug("libAVEnumMediaTypes_Next(%p)\n", this); + if (!types) + return E_POINTER; + if (!this->pos && n == 1) { + if (!IsEqualGUID(&this->type.majortype, &GUID_NULL)) { + AM_MEDIA_TYPE *type = av_malloc(sizeof(AM_MEDIA_TYPE)); + ff_copy_dshow_media_type(type, &this->type); + *types = type; + count = 1; + } + this->pos = 1; + } + if (fetched) + *fetched = count; + if (!count) + return S_FALSE; + return S_OK; +} +long WINAPI +libAVEnumMediaTypes_Skip(libAVEnumMediaTypes *this, unsigned long n) +{ + dshowdebug("libAVEnumMediaTypes_Skip(%p)\n", this); + if (n) /* Any skip will always fall outside of the only valid type. */ + return S_FALSE; + return S_OK; +} +long WINAPI +libAVEnumMediaTypes_Reset(libAVEnumMediaTypes *this) +{ + dshowdebug("libAVEnumMediaTypes_Reset(%p)\n", this); + this->pos = 0; + return S_OK; +} +long WINAPI +libAVEnumMediaTypes_Clone(libAVEnumMediaTypes *this, libAVEnumMediaTypes **enums) +{ + libAVEnumMediaTypes *new; + dshowdebug("libAVEnumMediaTypes_Clone(%p)\n", this); + if (!enums) + return E_POINTER; + new = libAVEnumMediaTypes_Create(&this->type); + if (!new) + return E_OUTOFMEMORY; + new->pos = this->pos; + *enums = new; + return S_OK; +} + +static int +libAVEnumMediaTypes_Setup(libAVEnumMediaTypes *this, const AM_MEDIA_TYPE *type) +{ + IEnumPinsVtbl *vtbl = this->vtbl; + SETVTBL(vtbl, libAVEnumMediaTypes, QueryInterface); + SETVTBL(vtbl, libAVEnumMediaTypes, AddRef); + SETVTBL(vtbl, libAVEnumMediaTypes, Release); + SETVTBL(vtbl, libAVEnumMediaTypes, Next); + SETVTBL(vtbl, libAVEnumMediaTypes, Skip); + SETVTBL(vtbl, libAVEnumMediaTypes, Reset); + SETVTBL(vtbl, libAVEnumMediaTypes, Clone); + + if (!type) { + this->type.majortype = GUID_NULL; + } else { + ff_copy_dshow_media_type(&this->type, type); + } + + return 1; +} +DECLARE_CREATE(libAVEnumMediaTypes, libAVEnumMediaTypes_Setup(this, type), const AM_MEDIA_TYPE *type) +DECLARE_DESTROY(libAVEnumMediaTypes, nothing) diff --git a/libavdevice/dshow_enumpins.c b/libavdevice/dshow_enumpins.c new file mode 100644 index 0000000..02e967a --- /dev/null +++ b/libavdevice/dshow_enumpins.c @@ -0,0 +1,105 @@ +/* + * DirectShow capture interface + * Copyright (c) 2010 Ramiro Polla + * + * 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 "dshow.h" + +DECLARE_QUERYINTERFACE(libAVEnumPins, + { {&IID_IUnknown,0}, {&IID_IEnumPins,0} }) +DECLARE_ADDREF(libAVEnumPins) +DECLARE_RELEASE(libAVEnumPins) + +long WINAPI +libAVEnumPins_Next(libAVEnumPins *this, unsigned long n, IPin **pins, + unsigned long *fetched) +{ + int count = 0; + dshowdebug("libAVEnumPins_Next(%p)\n", this); + if (!pins) + return E_POINTER; + if (!this->pos && n == 1) { + libAVPin_AddRef(this->pin); + *pins = (IPin *) this->pin; + count = 1; + this->pos = 1; + } + if (fetched) + *fetched = count; + if (!count) + return S_FALSE; + return S_OK; +} +long WINAPI +libAVEnumPins_Skip(libAVEnumPins *this, unsigned long n) +{ + dshowdebug("libAVEnumPins_Skip(%p)\n", this); + if (n) /* Any skip will always fall outside of the only valid pin. */ + return S_FALSE; + return S_OK; +} +long WINAPI +libAVEnumPins_Reset(libAVEnumPins *this) +{ + dshowdebug("libAVEnumPins_Reset(%p)\n", this); + this->pos = 0; + return S_OK; +} +long WINAPI +libAVEnumPins_Clone(libAVEnumPins *this, libAVEnumPins **pins) +{ + libAVEnumPins *new; + dshowdebug("libAVEnumPins_Clone(%p)\n", this); + if (!pins) + return E_POINTER; + new = libAVEnumPins_Create(this->pin, this->filter); + if (!new) + return E_OUTOFMEMORY; + new->pos = this->pos; + *pins = new; + return S_OK; +} + +static int +libAVEnumPins_Setup(libAVEnumPins *this, libAVPin *pin, libAVFilter *filter) +{ + IEnumPinsVtbl *vtbl = this->vtbl; + SETVTBL(vtbl, libAVEnumPins, QueryInterface); + SETVTBL(vtbl, libAVEnumPins, AddRef); + SETVTBL(vtbl, libAVEnumPins, Release); + SETVTBL(vtbl, libAVEnumPins, Next); + SETVTBL(vtbl, libAVEnumPins, Skip); + SETVTBL(vtbl, libAVEnumPins, Reset); + SETVTBL(vtbl, libAVEnumPins, Clone); + + this->pin = pin; + this->filter = filter; + libAVFilter_AddRef(this->filter); + + return 1; +} +static int +libAVEnumPins_Cleanup(libAVEnumPins *this) +{ + libAVFilter_Release(this->filter); + return 1; +} +DECLARE_CREATE(libAVEnumPins, libAVEnumPins_Setup(this, pin, filter), + libAVPin *pin, libAVFilter *filter) +DECLARE_DESTROY(libAVEnumPins, libAVEnumPins_Cleanup) diff --git a/libavdevice/dshow_filter.c b/libavdevice/dshow_filter.c new file mode 100644 index 0000000..64e8306 --- /dev/null +++ b/libavdevice/dshow_filter.c @@ -0,0 +1,202 @@ +/* + * DirectShow capture interface + * Copyright (c) 2010 Ramiro Polla + * + * 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 "dshow.h" + +DECLARE_QUERYINTERFACE(libAVFilter, + { {&IID_IUnknown,0}, {&IID_IBaseFilter,0} }) +DECLARE_ADDREF(libAVFilter) +DECLARE_RELEASE(libAVFilter) + +long WINAPI +libAVFilter_GetClassID(libAVFilter *this, CLSID *id) +{ + dshowdebug("libAVFilter_GetClassID(%p)\n", this); + /* I'm not creating a ClassID just for this. */ + return E_FAIL; +} +long WINAPI +libAVFilter_Stop(libAVFilter *this) +{ + dshowdebug("libAVFilter_Stop(%p)\n", this); + this->state = State_Stopped; + return S_OK; +} +long WINAPI +libAVFilter_Pause(libAVFilter *this) +{ + dshowdebug("libAVFilter_Pause(%p)\n", this); + this->state = State_Paused; + return S_OK; +} +long WINAPI +libAVFilter_Run(libAVFilter *this, REFERENCE_TIME start) +{ + dshowdebug("libAVFilter_Run(%p) %"PRId64"\n", this, start); + this->state = State_Running; + this->start_time = start; + return S_OK; +} +long WINAPI +libAVFilter_GetState(libAVFilter *this, DWORD ms, FILTER_STATE *state) +{ + dshowdebug("libAVFilter_GetState(%p)\n", this); + if (!state) + return E_POINTER; + *state = this->state; + return S_OK; +} +long WINAPI +libAVFilter_SetSyncSource(libAVFilter *this, IReferenceClock *clock) +{ + dshowdebug("libAVFilter_SetSyncSource(%p)\n", this); + + if (this->clock != clock) { + if (this->clock) + IReferenceClock_Release(this->clock); + this->clock = clock; + if (clock) + IReferenceClock_AddRef(clock); + } + + return S_OK; +} +long WINAPI +libAVFilter_GetSyncSource(libAVFilter *this, IReferenceClock **clock) +{ + dshowdebug("libAVFilter_GetSyncSource(%p)\n", this); + + if (!clock) + return E_POINTER; + if (this->clock) + IReferenceClock_AddRef(this->clock); + *clock = this->clock; + + return S_OK; +} +long WINAPI +libAVFilter_EnumPins(libAVFilter *this, IEnumPins **enumpin) +{ + libAVEnumPins *new; + dshowdebug("libAVFilter_EnumPins(%p)\n", this); + + if (!enumpin) + return E_POINTER; + new = libAVEnumPins_Create(this->pin, this); + if (!new) + return E_OUTOFMEMORY; + + *enumpin = (IEnumPins *) new; + return S_OK; +} +long WINAPI +libAVFilter_FindPin(libAVFilter *this, const wchar_t *id, IPin **pin) +{ + libAVPin *found = NULL; + dshowdebug("libAVFilter_FindPin(%p)\n", this); + + if (!id || !pin) + return E_POINTER; + if (!wcscmp(id, L"In")) { + found = this->pin; + libAVPin_AddRef(found); + } + *pin = (IPin *) found; + if (!found) + return VFW_E_NOT_FOUND; + + return S_OK; +} +long WINAPI +libAVFilter_QueryFilterInfo(libAVFilter *this, FILTER_INFO *info) +{ + dshowdebug("libAVFilter_QueryFilterInfo(%p)\n", this); + + if (!info) + return E_POINTER; + if (this->info.pGraph) + IFilterGraph_AddRef(this->info.pGraph); + *info = this->info; + + return S_OK; +} +long WINAPI +libAVFilter_JoinFilterGraph(libAVFilter *this, IFilterGraph *graph, + const wchar_t *name) +{ + dshowdebug("libAVFilter_JoinFilterGraph(%p)\n", this); + + this->info.pGraph = graph; + if (name) + wcscpy(this->info.achName, name); + + return S_OK; +} +long WINAPI +libAVFilter_QueryVendorInfo(libAVFilter *this, wchar_t **info) +{ + dshowdebug("libAVFilter_QueryVendorInfo(%p)\n", this); + + if (!info) + return E_POINTER; + *info = wcsdup(L"libAV"); + + return S_OK; +} + +static int +libAVFilter_Setup(libAVFilter *this, void *priv_data, void *callback, + enum dshowDeviceType type) +{ + IBaseFilterVtbl *vtbl = this->vtbl; + SETVTBL(vtbl, libAVFilter, QueryInterface); + SETVTBL(vtbl, libAVFilter, AddRef); + SETVTBL(vtbl, libAVFilter, Release); + SETVTBL(vtbl, libAVFilter, GetClassID); + SETVTBL(vtbl, libAVFilter, Stop); + SETVTBL(vtbl, libAVFilter, Pause); + SETVTBL(vtbl, libAVFilter, Run); + SETVTBL(vtbl, libAVFilter, GetState); + SETVTBL(vtbl, libAVFilter, SetSyncSource); + SETVTBL(vtbl, libAVFilter, GetSyncSource); + SETVTBL(vtbl, libAVFilter, EnumPins); + SETVTBL(vtbl, libAVFilter, FindPin); + SETVTBL(vtbl, libAVFilter, QueryFilterInfo); + SETVTBL(vtbl, libAVFilter, JoinFilterGraph); + SETVTBL(vtbl, libAVFilter, QueryVendorInfo); + + this->pin = libAVPin_Create(this); + + this->priv_data = priv_data; + this->callback = callback; + this->type = type; + + return 1; +} +static int +libAVFilter_Cleanup(libAVFilter *this) +{ + libAVPin_Release(this->pin); + return 1; +} +DECLARE_CREATE(libAVFilter, libAVFilter_Setup(this, priv_data, callback, type), + void *priv_data, void *callback, enum dshowDeviceType type) +DECLARE_DESTROY(libAVFilter, libAVFilter_Cleanup) diff --git a/libavdevice/dshow_pin.c b/libavdevice/dshow_pin.c new file mode 100644 index 0000000..5e14108 --- /dev/null +++ b/libavdevice/dshow_pin.c @@ -0,0 +1,362 @@ +/* + * DirectShow capture interface + * Copyright (c) 2010 Ramiro Polla + * + * 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 "dshow.h" + +#include <stddef.h> +#define imemoffset offsetof(libAVPin, imemvtbl) + +DECLARE_QUERYINTERFACE(libAVPin, + { {&IID_IUnknown,0}, {&IID_IPin,0}, {&IID_IMemInputPin,imemoffset} }) +DECLARE_ADDREF(libAVPin) +DECLARE_RELEASE(libAVPin) + +long WINAPI +libAVPin_Connect(libAVPin *this, IPin *pin, const AM_MEDIA_TYPE *type) +{ + dshowdebug("libAVPin_Connect(%p, %p, %p)\n", this, pin, type); + /* Input pins receive connections. */ + return S_FALSE; +} +long WINAPI +libAVPin_ReceiveConnection(libAVPin *this, IPin *pin, + const AM_MEDIA_TYPE *type) +{ + enum dshowDeviceType devtype = this->filter->type; + dshowdebug("libAVPin_ReceiveConnection(%p)\n", this); + + if (!pin) + return E_POINTER; + if (this->connectedto) + return VFW_E_ALREADY_CONNECTED; + + ff_print_AM_MEDIA_TYPE(type); + if (devtype == VideoDevice) { + if (!IsEqualGUID(&type->majortype, &MEDIATYPE_Video)) + return VFW_E_TYPE_NOT_ACCEPTED; + } else { + if (!IsEqualGUID(&type->majortype, &MEDIATYPE_Audio)) + return VFW_E_TYPE_NOT_ACCEPTED; + } + + IPin_AddRef(pin); + this->connectedto = pin; + + ff_copy_dshow_media_type(&this->type, type); + + return S_OK; +} +long WINAPI +libAVPin_Disconnect(libAVPin *this) +{ + dshowdebug("libAVPin_Disconnect(%p)\n", this); + + if (this->filter->state != State_Stopped) + return VFW_E_NOT_STOPPED; + if (!this->connectedto) + return S_FALSE; + IPin_Release(this->connectedto); + this->connectedto = NULL; + + return S_OK; +} +long WINAPI +libAVPin_ConnectedTo(libAVPin *this, IPin **pin) +{ + dshowdebug("libAVPin_ConnectedTo(%p)\n", this); + + if (!pin) + return E_POINTER; + if (!this->connectedto) + return VFW_E_NOT_CONNECTED; + IPin_AddRef(this->connectedto); + *pin = this->connectedto; + + return S_OK; +} +long WINAPI +libAVPin_ConnectionMediaType(libAVPin *this, AM_MEDIA_TYPE *type) +{ + dshowdebug("libAVPin_ConnectionMediaType(%p)\n", this); + + if (!type) + return E_POINTER; + if (!this->connectedto) + return VFW_E_NOT_CONNECTED; + + return ff_copy_dshow_media_type(type, &this->type); +} +long WINAPI +libAVPin_QueryPinInfo(libAVPin *this, PIN_INFO *info) +{ + dshowdebug("libAVPin_QueryPinInfo(%p)\n", this); + + if (!info) + return E_POINTER; + + if (this->filter) + libAVFilter_AddRef(this->filter); + + info->pFilter = (IBaseFilter *) this->filter; + info->dir = PINDIR_INPUT; + wcscpy(info->achName, L"Capture"); + + return S_OK; +} +long WINAPI +libAVPin_QueryDirection(libAVPin *this, PIN_DIRECTION *dir) +{ + dshowdebug("libAVPin_QueryDirection(%p)\n", this); + if (!dir) + return E_POINTER; + *dir = PINDIR_INPUT; + return S_OK; +} +long WINAPI +libAVPin_QueryId(libAVPin *this, wchar_t **id) +{ + dshowdebug("libAVPin_QueryId(%p)\n", this); + + if (!id) + return E_POINTER; + + *id = wcsdup(L"libAV Pin"); + + return S_OK; +} +long WINAPI +libAVPin_QueryAccept(libAVPin *this, const AM_MEDIA_TYPE *type) +{ + dshowdebug("libAVPin_QueryAccept(%p)\n", this); + return S_FALSE; +} +long WINAPI +libAVPin_EnumMediaTypes(libAVPin *this, IEnumMediaTypes **enumtypes) +{ + const AM_MEDIA_TYPE *type = NULL; + libAVEnumMediaTypes *new; + dshowdebug("libAVPin_EnumMediaTypes(%p)\n", this); + + if (!enumtypes) + return E_POINTER; + new = libAVEnumMediaTypes_Create(type); + if (!new) + return E_OUTOFMEMORY; + + *enumtypes = (IEnumMediaTypes *) new; + return S_OK; +} +long WINAPI +libAVPin_QueryInternalConnections(libAVPin *this, IPin **pin, + unsigned long *npin) +{ + dshowdebug("libAVPin_QueryInternalConnections(%p)\n", this); + return E_NOTIMPL; +} +long WINAPI +libAVPin_EndOfStream(libAVPin *this) +{ + dshowdebug("libAVPin_EndOfStream(%p)\n", this); + /* I don't care. */ + return S_OK; +} +long WINAPI +libAVPin_BeginFlush(libAVPin *this) +{ + dshowdebug("libAVPin_BeginFlush(%p)\n", this); + /* I don't care. */ + return S_OK; +} +long WINAPI +libAVPin_EndFlush(libAVPin *this) +{ + dshowdebug("libAVPin_EndFlush(%p)\n", this); + /* I don't care. */ + return S_OK; +} +long WINAPI +libAVPin_NewSegment(libAVPin *this, REFERENCE_TIME start, REFERENCE_TIME stop, + double rate) +{ + dshowdebug("libAVPin_NewSegment(%p)\n", this); + /* I don't care. */ + return S_OK; +} + +static int +libAVPin_Setup(libAVPin *this, libAVFilter *filter) +{ + IPinVtbl *vtbl = this->vtbl; + IMemInputPinVtbl *imemvtbl; + + if (!filter) + return 0; + + imemvtbl = av_malloc(sizeof(IMemInputPinVtbl)); + if (!imemvtbl) + return 0; + + SETVTBL(imemvtbl, libAVMemInputPin, QueryInterface); + SETVTBL(imemvtbl, libAVMemInputPin, AddRef); + SETVTBL(imemvtbl, libAVMemInputPin, Release); + SETVTBL(imemvtbl, libAVMemInputPin, GetAllocator); + SETVTBL(imemvtbl, libAVMemInputPin, NotifyAllocator); + SETVTBL(imemvtbl, libAVMemInputPin, GetAllocatorRequirements); + SETVTBL(imemvtbl, libAVMemInputPin, Receive); + SETVTBL(imemvtbl, libAVMemInputPin, ReceiveMultiple); + SETVTBL(imemvtbl, libAVMemInputPin, ReceiveCanBlock); + + this->imemvtbl = imemvtbl; + + SETVTBL(vtbl, libAVPin, QueryInterface); + SETVTBL(vtbl, libAVPin, AddRef); + SETVTBL(vtbl, libAVPin, Release); + SETVTBL(vtbl, libAVPin, Connect); + SETVTBL(vtbl, libAVPin, ReceiveConnection); + SETVTBL(vtbl, libAVPin, Disconnect); + SETVTBL(vtbl, libAVPin, ConnectedTo); + SETVTBL(vtbl, libAVPin, ConnectionMediaType); + SETVTBL(vtbl, libAVPin, QueryPinInfo); + SETVTBL(vtbl, libAVPin, QueryDirection); + SETVTBL(vtbl, libAVPin, QueryId); + SETVTBL(vtbl, libAVPin, QueryAccept); + SETVTBL(vtbl, libAVPin, EnumMediaTypes); + SETVTBL(vtbl, libAVPin, QueryInternalConnections); + SETVTBL(vtbl, libAVPin, EndOfStream); + SETVTBL(vtbl, libAVPin, BeginFlush); + SETVTBL(vtbl, libAVPin, EndFlush); + SETVTBL(vtbl, libAVPin, NewSegment); + + this->filter = filter; + + return 1; +} +DECLARE_CREATE(libAVPin, libAVPin_Setup(this, filter), libAVFilter *filter) +DECLARE_DESTROY(libAVPin, nothing) + +/***************************************************************************** + * libAVMemInputPin + ****************************************************************************/ +long WINAPI +libAVMemInputPin_QueryInterface(libAVMemInputPin *this, const GUID *riid, + void **ppvObject) +{ + libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset); + dshowdebug("libAVMemInputPin_QueryInterface(%p)\n", this); + return libAVPin_QueryInterface(pin, riid, ppvObject); +} +unsigned long WINAPI +libAVMemInputPin_AddRef(libAVMemInputPin *this) +{ + libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset); + dshowdebug("libAVMemInputPin_AddRef(%p)\n", this); + return libAVPin_AddRef(pin); +} +unsigned long WINAPI +libAVMemInputPin_Release(libAVMemInputPin *this) +{ + libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset); + dshowdebug("libAVMemInputPin_Release(%p)\n", this); + return libAVPin_Release(pin); +} +long WINAPI +libAVMemInputPin_GetAllocator(libAVMemInputPin *this, IMemAllocator **alloc) +{ + dshowdebug("libAVMemInputPin_GetAllocator(%p)\n", this); + return VFW_E_NO_ALLOCATOR; +} +long WINAPI +libAVMemInputPin_NotifyAllocator(libAVMemInputPin *this, IMemAllocator *alloc, + WINBOOL rdwr) +{ + dshowdebug("libAVMemInputPin_NotifyAllocator(%p)\n", this); + return S_OK; +} +long WINAPI +libAVMemInputPin_GetAllocatorRequirements(libAVMemInputPin *this, + ALLOCATOR_PROPERTIES *props) +{ + dshowdebug("libAVMemInputPin_GetAllocatorRequirements(%p)\n", this); + return E_NOTIMPL; +} +long WINAPI +libAVMemInputPin_Receive(libAVMemInputPin *this, IMediaSample *sample) +{ + libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset); + enum dshowDeviceType devtype = pin->filter->type; + void *priv_data; + uint8_t *buf; + int buf_size; + int index; + int64_t curtime; + + dshowdebug("libAVMemInputPin_Receive(%p)\n", this); + + if (!sample) + return E_POINTER; + + if (devtype == VideoDevice) { + /* PTS from video devices is unreliable. */ + IReferenceClock *clock = pin->filter->clock; + IReferenceClock_GetTime(clock, &curtime); + } else { + int64_t dummy; + IMediaSample_GetTime(sample, &curtime, &dummy); + curtime += pin->filter->start_time; + } + + buf_size = IMediaSample_GetActualDataLength(sample); + IMediaSample_GetPointer(sample, &buf); + priv_data = pin->filter->priv_data; + index = pin->filter->stream_index; + + pin->filter->callback(priv_data, index, buf, buf_size, curtime); + + return S_OK; +} +long WINAPI +libAVMemInputPin_ReceiveMultiple(libAVMemInputPin *this, + IMediaSample **samples, long n, long *nproc) +{ + int i; + dshowdebug("libAVMemInputPin_ReceiveMultiple(%p)\n", this); + + for (i = 0; i < n; i++) + libAVMemInputPin_Receive(this, samples[i]); + + *nproc = n; + return S_OK; +} +long WINAPI +libAVMemInputPin_ReceiveCanBlock(libAVMemInputPin *this) +{ + dshowdebug("libAVMemInputPin_ReceiveCanBlock(%p)\n", this); + /* I swear I will not block. */ + return S_FALSE; +} + +void +libAVMemInputPin_Destroy(libAVMemInputPin *this) +{ + libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset); + dshowdebug("libAVMemInputPin_Destroy(%p)\n", this); + return libAVPin_Destroy(pin); +} diff --git a/libavdevice/dv1394.c b/libavdevice/dv1394.c index 5502fa2..779117a 100644 --- a/libavdevice/dv1394.c +++ b/libavdevice/dv1394.c @@ -2,20 +2,20 @@ * Linux DV1394 interface * Copyright (c) 2003 Max Krasnyansky <maxk@qualcomm.com> * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -32,7 +32,7 @@ #include "libavutil/log.h" #include "libavutil/opt.h" -#include "libavformat/avformat.h" +#include "avdevice.h" #include "libavformat/dv.h" #include "dv1394.h" @@ -188,7 +188,7 @@ restart_poll: size = dv_produce_packet(dv->dv_demux, pkt, dv->ring + (dv->index * DV1394_PAL_FRAME_SIZE), - DV1394_PAL_FRAME_SIZE); + DV1394_PAL_FRAME_SIZE, -1); dv->index = (dv->index + 1) % DV1394_RING_FRAMES; dv->done++; dv->avail--; diff --git a/libavdevice/dv1394.h b/libavdevice/dv1394.h index 5ccc68a..00706f7 100644 --- a/libavdevice/dv1394.h +++ b/libavdevice/dv1394.h @@ -8,20 +8,20 @@ * Copyright (C)1999,2000 Sebastien Rougeaux <sebastien.rougeaux@anu.edu.au> * Peter Schlaile <udbz@rz.uni-karlsruhe.de> * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ diff --git a/libavdevice/fbdev.c b/libavdevice/fbdev.c index 57c257f..70a904c 100644 --- a/libavdevice/fbdev.c +++ b/libavdevice/fbdev.c @@ -3,20 +3,20 @@ * Copyright (c) 2009 Giliard B. de Freitas <giliarde@gmail.com> * Copyright (C) 2002 Gunnar Monell <gmo@linux.nu> * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -42,7 +42,7 @@ #include "libavutil/opt.h" #include "libavutil/parseutils.h" #include "libavutil/pixdesc.h" -#include "libavformat/avformat.h" +#include "avdevice.h" struct rgb_pixfmt_map_entry { int bits_per_pixel; @@ -193,20 +193,22 @@ static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt) fbdev->time_frame = av_gettime(); /* wait based on the frame rate */ - curtime = av_gettime(); - delay = fbdev->time_frame - curtime; - av_dlog(avctx, - "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n", - fbdev->time_frame, curtime, delay); - if (delay > 0) { + while (1) { + curtime = av_gettime(); + delay = fbdev->time_frame - curtime; + av_dlog(avctx, + "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n", + fbdev->time_frame, curtime, delay); + if (delay <= 0) { + fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->framerate_q); + break; + } if (avctx->flags & AVFMT_FLAG_NONBLOCK) return AVERROR(EAGAIN); ts.tv_sec = delay / 1000000; ts.tv_nsec = (delay % 1000000) * 1000; while (nanosleep(&ts, &ts) < 0 && errno == EINTR); } - /* compute the time of the next frame */ - fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->framerate_q); if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0) return ret; @@ -223,7 +225,6 @@ static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt) fbdev->varinfo.yoffset * fbdev->fixinfo.line_length; pout = pkt->data; - // TODO it'd be nice if the lines were aligned for (i = 0; i < fbdev->heigth; i++) { memcpy(pout, pin, fbdev->frame_linesize); pin += fbdev->fixinfo.line_length; diff --git a/libavdevice/jack_audio.c b/libavdevice/jack_audio.c index 029ace6..bdf5eb8 100644 --- a/libavdevice/jack_audio.c +++ b/libavdevice/jack_audio.c @@ -3,20 +3,20 @@ * Copyright (c) 2009 Samalyse * Author: Olivier Guilyardi <olivier samalyse com> * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -28,8 +28,8 @@ #include "libavutil/fifo.h" #include "libavutil/opt.h" #include "libavcodec/avcodec.h" -#include "libavformat/avformat.h" #include "libavformat/timefilter.h" +#include "avdevice.h" /** * Size of the internal FIFO buffers as a number of audio packets diff --git a/libavdevice/lavfi.c b/libavdevice/lavfi.c new file mode 100644 index 0000000..4d7297b --- /dev/null +++ b/libavdevice/lavfi.c @@ -0,0 +1,342 @@ +/* + * Copyright (c) 2011 Stefano Sabatini + * + * 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 + */ + +/** + * @file + * libavfilter virtual input device + */ + +/* #define DEBUG */ + +#include "float.h" /* DBL_MIN, DBL_MAX */ + +#include "libavutil/log.h" +#include "libavutil/mem.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/pixdesc.h" +#include "libavfilter/avfilter.h" +#include "libavfilter/avfiltergraph.h" +#include "libavfilter/buffersink.h" +#include "avdevice.h" + +typedef struct { + AVClass *class; ///< class for private options + char *graph_str; + AVFilterGraph *graph; + AVFilterContext **sinks; + int *sink_stream_map; + int *stream_sink_map; +} LavfiContext; + +static int *create_all_formats(int n) +{ + int i, j, *fmts, count = 0; + + for (i = 0; i < n; i++) + if (!(av_pix_fmt_descriptors[i].flags & PIX_FMT_HWACCEL)) + count++; + + if (!(fmts = av_malloc((count+1) * sizeof(int)))) + return NULL; + for (j = 0, i = 0; i < n; i++) { + if (!(av_pix_fmt_descriptors[i].flags & PIX_FMT_HWACCEL)) + fmts[j++] = i; + } + fmts[j] = -1; + return fmts; +} + +av_cold static int lavfi_read_close(AVFormatContext *avctx) +{ + LavfiContext *lavfi = avctx->priv_data; + + av_freep(&lavfi->sink_stream_map); + av_freep(&lavfi->stream_sink_map); + avfilter_graph_free(&lavfi->graph); + + return 0; +} + +av_cold static int lavfi_read_header(AVFormatContext *avctx, + AVFormatParameters *ap) +{ + LavfiContext *lavfi = avctx->priv_data; + AVFilterInOut *input_links = NULL, *output_links = NULL, *inout; + AVFilter *buffersink, *abuffersink; + int *pix_fmts = create_all_formats(PIX_FMT_NB); + enum AVMediaType type; + int ret = 0, i, n; + +#define FAIL(ERR) { ret = ERR; goto end; } + + avfilter_register_all(); + + buffersink = avfilter_get_by_name("buffersink"); + abuffersink = avfilter_get_by_name("abuffersink"); + + if (!lavfi->graph_str) + lavfi->graph_str = av_strdup(avctx->filename); + + /* parse the graph, create a stream for each open output */ + if (!(lavfi->graph = avfilter_graph_alloc())) + FAIL(AVERROR(ENOMEM)); + + if ((ret = avfilter_graph_parse(lavfi->graph, lavfi->graph_str, + &input_links, &output_links, avctx)) < 0) + FAIL(ret); + + if (input_links) { + av_log(avctx, AV_LOG_ERROR, + "Open inputs in the filtergraph are not acceptable\n"); + FAIL(AVERROR(EINVAL)); + } + + /* count the outputs */ + for (n = 0, inout = output_links; inout; n++, inout = inout->next); + + if (!(lavfi->sink_stream_map = av_malloc(sizeof(int) * n))) + FAIL(AVERROR(ENOMEM)); + if (!(lavfi->stream_sink_map = av_malloc(sizeof(int) * n))) + FAIL(AVERROR(ENOMEM)); + + for (i = 0; i < n; i++) + lavfi->stream_sink_map[i] = -1; + + /* parse the output link names - they need to be of the form out0, out1, ... + * create a mapping between them and the streams */ + for (i = 0, inout = output_links; inout; i++, inout = inout->next) { + int stream_idx; + if (!strcmp(inout->name, "out")) + stream_idx = 0; + else if (sscanf(inout->name, "out%d\n", &stream_idx) != 1) { + av_log(avctx, AV_LOG_ERROR, + "Invalid outpad name '%s'\n", inout->name); + FAIL(AVERROR(EINVAL)); + } + + if ((unsigned)stream_idx >= n) { + av_log(avctx, AV_LOG_ERROR, + "Invalid index was specified in output '%s', " + "must be a non-negative value < %d\n", + inout->name, n); + FAIL(AVERROR(EINVAL)); + } + + /* is a video output? */ + type = inout->filter_ctx->output_pads[inout->pad_idx].type; + if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) { + av_log(avctx, AV_LOG_ERROR, + "Output '%s' is not a video or audio output, not yet supported\n", inout->name); + FAIL(AVERROR(EINVAL)); + } + + if (lavfi->stream_sink_map[stream_idx] != -1) { + av_log(avctx, AV_LOG_ERROR, + "An with stream index %d was already specified\n", + stream_idx); + FAIL(AVERROR(EINVAL)); + } + lavfi->sink_stream_map[i] = stream_idx; + lavfi->stream_sink_map[stream_idx] = i; + } + + /* for each open output create a corresponding stream */ + for (i = 0, inout = output_links; inout; i++, inout = inout->next) { + AVStream *st; + if (!(st = av_new_stream(avctx, i))) + FAIL(AVERROR(ENOMEM)); + } + + /* create a sink for each output and connect them to the graph */ + lavfi->sinks = av_malloc(sizeof(AVFilterContext *) * avctx->nb_streams); + if (!lavfi->sinks) + FAIL(AVERROR(ENOMEM)); + + for (i = 0, inout = output_links; inout; i++, inout = inout->next) { + AVFilterContext *sink; + + type = inout->filter_ctx->output_pads[inout->pad_idx].type; + + if (type == AVMEDIA_TYPE_VIDEO && ! buffersink || + type == AVMEDIA_TYPE_AUDIO && ! abuffersink) { + av_log(avctx, AV_LOG_ERROR, "Missing required buffersink filter, aborting.\n"); + FAIL(AVERROR_FILTER_NOT_FOUND); + } + + if (type == AVMEDIA_TYPE_VIDEO) { + AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc(); + buffersink_params->pixel_fmts = pix_fmts; + +#if FF_API_OLD_VSINK_API + ret = avfilter_graph_create_filter(&sink, buffersink, + inout->name, NULL, + pix_fmts, lavfi->graph); +#else + buffersink_params->pixel_fmts = pix_fmts; + ret = avfilter_graph_create_filter(&sink, buffersink, + inout->name, NULL, + buffersink_params, lavfi->graph); +#endif + av_freep(&buffersink_params); + + if (ret < 0) + goto end; + } else if (type == AVMEDIA_TYPE_AUDIO) { + enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, -1 }; + const int packing_fmts[] = { AVFILTER_PACKED, -1 }; + const int64_t *chlayouts = avfilter_all_channel_layouts; + AVABufferSinkParams *abuffersink_params = av_abuffersink_params_alloc(); + abuffersink_params->sample_fmts = sample_fmts; + abuffersink_params->packing_fmts = packing_fmts; + abuffersink_params->channel_layouts = chlayouts; + + ret = avfilter_graph_create_filter(&sink, abuffersink, + inout->name, NULL, + abuffersink_params, lavfi->graph); + av_free(abuffersink_params); + if (ret < 0) + goto end; + } + + lavfi->sinks[i] = sink; + if ((ret = avfilter_link(inout->filter_ctx, inout->pad_idx, sink, 0)) < 0) + FAIL(ret); + } + + /* configure the graph */ + if ((ret = avfilter_graph_config(lavfi->graph, avctx)) < 0) + FAIL(ret); + + /* fill each stream with the information in the corresponding sink */ + for (i = 0; i < avctx->nb_streams; i++) { + AVFilterLink *link = lavfi->sinks[lavfi->stream_sink_map[i]]->inputs[0]; + AVStream *st = avctx->streams[i]; + st->codec->codec_type = link->type; + av_set_pts_info(st, 64, link->time_base.num, link->time_base.den); + if (link->type == AVMEDIA_TYPE_VIDEO) { + st->codec->codec_id = CODEC_ID_RAWVIDEO; + st->codec->pix_fmt = link->format; + st->codec->time_base = link->time_base; + st->codec->width = link->w; + st->codec->height = link->h; + st ->sample_aspect_ratio = + st->codec->sample_aspect_ratio = link->sample_aspect_ratio; + } else if (link->type == AVMEDIA_TYPE_AUDIO) { + st->codec->codec_id = CODEC_ID_PCM_S16LE; + st->codec->channels = av_get_channel_layout_nb_channels(link->channel_layout); + st->codec->sample_fmt = link->format; + st->codec->sample_rate = link->sample_rate; + st->codec->time_base = link->time_base; + st->codec->channel_layout = link->channel_layout; + } + } + +end: + avfilter_inout_free(&input_links); + avfilter_inout_free(&output_links); + if (ret < 0) + lavfi_read_close(avctx); + return ret; +} + +static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt) +{ + LavfiContext *lavfi = avctx->priv_data; + double min_pts = DBL_MAX; + int stream_idx, min_pts_sink_idx = 0; + AVFilterBufferRef *ref; + AVPicture pict; + int ret, i, size; + + /* iterate through all the graph sinks. Select the sink with the + * minimum PTS */ + for (i = 0; i < avctx->nb_streams; i++) { + AVRational tb = lavfi->sinks[i]->inputs[0]->time_base; + double d; + int ret = av_buffersink_get_buffer_ref(lavfi->sinks[i], + &ref, AV_BUFFERSINK_FLAG_PEEK); + if (ret < 0) + return ret; + d = av_rescale_q(ref->pts, tb, AV_TIME_BASE_Q); + av_dlog(avctx, "sink_idx:%d time:%f\n", i, d); + + if (d < min_pts) { + min_pts = d; + min_pts_sink_idx = i; + } + } + av_dlog(avctx, "min_pts_sink_idx:%i\n", min_pts_sink_idx); + + av_buffersink_get_buffer_ref(lavfi->sinks[min_pts_sink_idx], &ref, 0); + stream_idx = lavfi->sink_stream_map[min_pts_sink_idx]; + + if (ref->video) { + size = avpicture_get_size(ref->format, ref->video->w, ref->video->h); + if ((ret = av_new_packet(pkt, size)) < 0) + return ret; + + memcpy(pict.data, ref->data, 4*sizeof(ref->data[0])); + memcpy(pict.linesize, ref->linesize, 4*sizeof(ref->linesize[0])); + + avpicture_layout(&pict, ref->format, ref->video->w, + ref->video->h, pkt->data, size); + } else if (ref->audio) { + size = ref->linesize[0]; + if ((ret = av_new_packet(pkt, size)) < 0) + return ret; + memcpy(pkt->data, ref->data[0], size); + } + + pkt->stream_index = stream_idx; + pkt->pts = ref->pts; + pkt->pos = ref->pos; + pkt->size = size; + avfilter_unref_buffer(ref); + + return size; +} + +#define OFFSET(x) offsetof(LavfiContext, x) + +#define DEC AV_OPT_FLAG_DECODING_PARAM + +static const AVOption options[] = { + { "graph", "Libavfilter graph", OFFSET(graph_str), FF_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC }, + { NULL }, +}; + +static const AVClass lavfi_class = { + .class_name = "lavfi indev", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVInputFormat ff_lavfi_demuxer = { + .name = "lavfi", + .long_name = NULL_IF_CONFIG_SMALL("Libavfilter virtual input device"), + .priv_data_size = sizeof(LavfiContext), + .read_header = lavfi_read_header, + .read_packet = lavfi_read_packet, + .read_close = lavfi_read_close, + .flags = AVFMT_NOFILE, + .priv_class = &lavfi_class, +}; diff --git a/libavdevice/libdc1394.c b/libavdevice/libdc1394.c index b6f5345..dffb06c 100644 --- a/libavdevice/libdc1394.c +++ b/libavdevice/libdc1394.c @@ -2,64 +2,45 @@ * IIDC1394 grab interface (uses libdc1394 and libraw1394) * Copyright (c) 2004 Roman Shaposhnik * Copyright (c) 2008 Alessandro Sappia + * Copyright (c) 2011 Martin Lambers * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "config.h" -#include "libavformat/avformat.h" #include "libavutil/log.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" +#include "avdevice.h" + +#include <stdlib.h> +#include <string.h> #include "libavutil/parseutils.h" #include "libavutil/pixdesc.h" -#if HAVE_LIBDC1394_2 #include <dc1394/dc1394.h> -#elif HAVE_LIBDC1394_1 -#include <libraw1394/raw1394.h> -#include <libdc1394/dc1394_control.h> - -#define DC1394_VIDEO_MODE_320x240_YUV422 MODE_320x240_YUV422 -#define DC1394_VIDEO_MODE_640x480_YUV411 MODE_640x480_YUV411 -#define DC1394_VIDEO_MODE_640x480_YUV422 MODE_640x480_YUV422 -#define DC1394_FRAMERATE_1_875 FRAMERATE_1_875 -#define DC1394_FRAMERATE_3_75 FRAMERATE_3_75 -#define DC1394_FRAMERATE_7_5 FRAMERATE_7_5 -#define DC1394_FRAMERATE_15 FRAMERATE_15 -#define DC1394_FRAMERATE_30 FRAMERATE_30 -#define DC1394_FRAMERATE_60 FRAMERATE_60 -#define DC1394_FRAMERATE_120 FRAMERATE_120 -#define DC1394_FRAMERATE_240 FRAMERATE_240 -#endif #undef free typedef struct dc1394_data { AVClass *class; -#if HAVE_LIBDC1394_1 - raw1394handle_t handle; - dc1394_cameracapture camera; - int channel; -#elif HAVE_LIBDC1394_2 dc1394_t *d; dc1394camera_t *camera; dc1394video_frame_t *frame; -#endif int current_frame; int frame_rate; /**< frames per 1000 seconds (fps * 1000) */ char *video_size; /**< String describing video size, set by a private option. */ @@ -69,16 +50,21 @@ typedef struct dc1394_data { AVPacket packet; } dc1394_data; -struct dc1394_frame_format { - int width; - int height; - enum PixelFormat pix_fmt; - int frame_size_id; -} dc1394_frame_formats[] = { - { 320, 240, PIX_FMT_UYVY422, DC1394_VIDEO_MODE_320x240_YUV422 }, - { 640, 480, PIX_FMT_UYYVYY411, DC1394_VIDEO_MODE_640x480_YUV411 }, - { 640, 480, PIX_FMT_UYVY422, DC1394_VIDEO_MODE_640x480_YUV422 }, - { 0, 0, 0, 0 } /* gotta be the last one */ +/* The list of color codings that we support. + * We assume big endian for the dc1394 16bit modes: libdc1394 never sets the + * flag little_endian in dc1394video_frame_t. */ +struct dc1394_color_coding { + int pix_fmt; + int score; + uint32_t coding; +} dc1394_color_codings[] = { + { PIX_FMT_GRAY16BE, 1000, DC1394_COLOR_CODING_MONO16 }, + { PIX_FMT_RGB48BE, 1100, DC1394_COLOR_CODING_RGB16 }, + { PIX_FMT_GRAY8, 1200, DC1394_COLOR_CODING_MONO8 }, + { PIX_FMT_RGB24, 1300, DC1394_COLOR_CODING_RGB8 }, + { PIX_FMT_UYYVYY411, 1400, DC1394_COLOR_CODING_YUV411 }, + { PIX_FMT_UYVY422, 1500, DC1394_COLOR_CODING_YUV422 }, + { PIX_FMT_NONE, 0, 0 } /* gotta be the last one */ }; struct dc1394_frame_rate { @@ -99,9 +85,6 @@ struct dc1394_frame_rate { #define OFFSET(x) offsetof(dc1394_data, x) #define DEC AV_OPT_FLAG_DECODING_PARAM static const AVOption options[] = { -#if HAVE_LIBDC1394_1 - { "channel", "", offsetof(dc1394_data, channel), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, -#endif { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = "qvga"}, 0, 0, DEC }, { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = "uyvy422"}, 0, 0, DEC }, { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC }, @@ -115,202 +98,183 @@ static const AVClass libdc1394_class = { .version = LIBAVUTIL_VERSION_INT, }; - -static inline int dc1394_read_common(AVFormatContext *c, AVFormatParameters *ap, - struct dc1394_frame_format **select_fmt, struct dc1394_frame_rate **select_fps) +static int dc1394_read_header(AVFormatContext *c, AVFormatParameters * ap) { dc1394_data* dc1394 = c->priv_data; - AVStream* vst; - struct dc1394_frame_format *fmt; - struct dc1394_frame_rate *fps; - enum PixelFormat pix_fmt; - int width, height; - AVRational framerate; - int ret = 0; - - if ((pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == PIX_FMT_NONE) { - av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format); - ret = AVERROR(EINVAL); - goto out; - } + AVStream *vst; + const struct dc1394_color_coding *cc; + const struct dc1394_frame_rate *fr; + dc1394camera_list_t *list; + dc1394video_modes_t video_modes; + dc1394video_mode_t video_mode; + dc1394framerates_t frame_rates; + dc1394framerate_t frame_rate; + uint32_t dc1394_width, dc1394_height, dc1394_color_coding; + int rate, best_rate; + int score, max_score; + int final_width, final_height, final_pix_fmt, final_frame_rate; + int res, i, j; + int ret=-1; - if ((ret = av_parse_video_size(&width, &height, dc1394->video_size)) < 0) { - av_log(c, AV_LOG_ERROR, "Could not parse video size '%s'.\n", dc1394->video_size); + /* Now let us prep the hardware. */ + dc1394->d = dc1394_new(); + dc1394_camera_enumerate (dc1394->d, &list); + if ( !list || list->num == 0) { + av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n"); goto out; } - if ((ret = av_parse_video_rate(&framerate, dc1394->framerate)) < 0) { - av_log(c, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", dc1394->framerate); - goto out; + + /* FIXME: To select a specific camera I need to search in list its guid */ + dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid); + if (list->num > 1) { + av_log(c, AV_LOG_INFO, "Working with the first camera found\n"); } - dc1394->frame_rate = av_rescale(1000, framerate.num, framerate.den); - for (fmt = dc1394_frame_formats; fmt->width; fmt++) - if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height) - break; + /* Freeing list of cameras */ + dc1394_camera_free_list (list); - for (fps = dc1394_frame_rates; fps->frame_rate; fps++) - if (fps->frame_rate == dc1394->frame_rate) - break; + /* Get the list of video modes supported by the camera. */ + res = dc1394_video_get_supported_modes (dc1394->camera, &video_modes); + if (res != DC1394_SUCCESS) { + av_log(c, AV_LOG_ERROR, "Could not get video formats.\n"); + goto out_camera; + } - if (!fps->frame_rate || !fmt->width) { - av_log(c, AV_LOG_ERROR, "Can't find matching camera format for %s, %dx%d@%d:1000fps\n", av_get_pix_fmt_name(pix_fmt), - width, height, dc1394->frame_rate); - ret = AVERROR(EINVAL); - goto out; + if (dc1394->pixel_format) { + if ((ap->pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == PIX_FMT_NONE) { + av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format); + ret = AVERROR(EINVAL); + goto out; + } + } + + if (dc1394->video_size) { + if ((ret = av_parse_video_size(&ap->width, &ap->height, dc1394->video_size)) < 0) { + av_log(c, AV_LOG_ERROR, "Couldn't parse video size.\n"); + goto out; + } + } + + /* Choose the best mode. */ + rate = (ap->time_base.num ? av_rescale(1000, ap->time_base.den, ap->time_base.num) : -1); + max_score = -1; + for (i = 0; i < video_modes.num; i++) { + if (video_modes.modes[i] == DC1394_VIDEO_MODE_EXIF + || (video_modes.modes[i] >= DC1394_VIDEO_MODE_FORMAT7_MIN + && video_modes.modes[i] <= DC1394_VIDEO_MODE_FORMAT7_MAX)) { + /* These modes are currently not supported as they would require + * much more work. For the remaining modes, the functions + * dc1394_get_image_size_from_video_mode and + * dc1394_get_color_coding_from_video_mode do not need to query the + * camera, and thus cannot fail. */ + continue; + } + dc1394_get_color_coding_from_video_mode (NULL, video_modes.modes[i], + &dc1394_color_coding); + for (cc = dc1394_color_codings; cc->pix_fmt != PIX_FMT_NONE; cc++) + if (cc->coding == dc1394_color_coding) + break; + if (cc->pix_fmt == PIX_FMT_NONE) { + /* We currently cannot handle this color coding. */ + continue; + } + /* Here we know that the mode is supported. Get its frame size and the list + * of frame rates supported by the camera for this mode. This list is sorted + * in ascending order according to libdc1394 example programs. */ + dc1394_get_image_size_from_video_mode (NULL, video_modes.modes[i], + &dc1394_width, &dc1394_height); + res = dc1394_video_get_supported_framerates (dc1394->camera, video_modes.modes[i], + &frame_rates); + if (res != DC1394_SUCCESS || frame_rates.num == 0) { + av_log(c, AV_LOG_ERROR, "Cannot get frame rates for video mode.\n"); + goto out_camera; + } + /* Choose the best frame rate. */ + best_rate = -1; + for (j = 0; j < frame_rates.num; j++) { + for (fr = dc1394_frame_rates; fr->frame_rate; fr++) { + if (fr->frame_rate_id == frame_rates.framerates[j]) { + break; + } + } + if (!fr->frame_rate) { + /* This frame rate is not supported. */ + continue; + } + best_rate = fr->frame_rate; + frame_rate = fr->frame_rate_id; + if (ap->time_base.num && rate == fr->frame_rate) { + /* This is the requested frame rate. */ + break; + } + } + if (best_rate == -1) { + /* No supported rate found. */ + continue; + } + /* Here we know that both the mode and the rate are supported. Compute score. */ + if (ap->width && ap->height + && (dc1394_width == ap->width && dc1394_height == ap->height)) { + score = 110000; + } else { + score = dc1394_width * 10; // 1600 - 16000 + } + if (ap->pix_fmt == cc->pix_fmt) { + score += 90000; + } else { + score += cc->score; // 1000 - 1500 + } + if (ap->time_base.num && rate == best_rate) { + score += 70000; + } else { + score += best_rate / 1000; // 1 - 240 + } + if (score > max_score) { + video_mode = video_modes.modes[i]; + final_width = dc1394_width; + final_height = dc1394_height; + final_pix_fmt = cc->pix_fmt; + final_frame_rate = best_rate; + max_score = score; + } + } + if (max_score == -1) { + av_log(c, AV_LOG_ERROR, "No suitable video mode / frame rate available.\n"); + goto out_camera; + } + if (ap->width && ap->height && !(ap->width == final_width && ap->height == final_height)) { + av_log(c, AV_LOG_WARNING, "Requested frame size is not available, using fallback.\n"); + } + if (ap->pix_fmt != PIX_FMT_NONE && ap->pix_fmt != final_pix_fmt) { + av_log(c, AV_LOG_WARNING, "Requested pixel format is not supported, using fallback.\n"); + } + if (ap->time_base.num && rate != final_frame_rate) { + av_log(c, AV_LOG_WARNING, "Requested frame rate is not available, using fallback.\n"); } /* create a video stream */ vst = av_new_stream(c, 0); - if (!vst) { - ret = AVERROR(ENOMEM); - goto out; - } + if (!vst) + goto out_camera; av_set_pts_info(vst, 64, 1, 1000); vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; vst->codec->codec_id = CODEC_ID_RAWVIDEO; - vst->codec->time_base.den = framerate.num; - vst->codec->time_base.num = framerate.den; - vst->codec->width = fmt->width; - vst->codec->height = fmt->height; - vst->codec->pix_fmt = fmt->pix_fmt; + vst->codec->time_base.den = final_frame_rate; + vst->codec->time_base.num = 1000; + vst->codec->width = final_width; + vst->codec->height = final_height; + vst->codec->pix_fmt = final_pix_fmt; /* packet init */ av_init_packet(&dc1394->packet); - dc1394->packet.size = avpicture_get_size(fmt->pix_fmt, fmt->width, fmt->height); + dc1394->packet.size = avpicture_get_size(final_pix_fmt, final_width, final_height); dc1394->packet.stream_index = vst->index; dc1394->packet.flags |= AV_PKT_FLAG_KEY; dc1394->current_frame = 0; + dc1394->frame_rate = final_frame_rate; - vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000); - *select_fps = fps; - *select_fmt = fmt; -out: - return ret; -} - -#if HAVE_LIBDC1394_1 -static int dc1394_v1_read_header(AVFormatContext *c, AVFormatParameters * ap) -{ - dc1394_data* dc1394 = c->priv_data; - AVStream* vst; - nodeid_t* camera_nodes; - int res; - struct dc1394_frame_format *fmt = NULL; - struct dc1394_frame_rate *fps = NULL; - - if (dc1394_read_common(c,ap,&fmt,&fps) != 0) - return -1; - - /* Now let us prep the hardware. */ - dc1394->handle = dc1394_create_handle(0); /* FIXME: gotta have ap->port */ - if (!dc1394->handle) { - av_log(c, AV_LOG_ERROR, "Can't acquire dc1394 handle on port %d\n", 0 /* ap->port */); - goto out; - } - camera_nodes = dc1394_get_camera_nodes(dc1394->handle, &res, 1); - if (!camera_nodes || camera_nodes[dc1394->channel] == DC1394_NO_CAMERA) { - av_log(c, AV_LOG_ERROR, "There's no IIDC camera on the channel %d\n", dc1394->channel); - goto out_handle; - } - res = dc1394_dma_setup_capture(dc1394->handle, camera_nodes[dc1394->channel], - 0, - FORMAT_VGA_NONCOMPRESSED, - fmt->frame_size_id, - SPEED_400, - fps->frame_rate_id, 8, 1, - c->filename, - &dc1394->camera); - dc1394_free_camera_nodes(camera_nodes); - if (res != DC1394_SUCCESS) { - av_log(c, AV_LOG_ERROR, "Can't prepare camera for the DMA capture\n"); - goto out_handle; - } - - res = dc1394_start_iso_transmission(dc1394->handle, dc1394->camera.node); - if (res != DC1394_SUCCESS) { - av_log(c, AV_LOG_ERROR, "Can't start isochronous transmission\n"); - goto out_handle_dma; - } - - return 0; - -out_handle_dma: - dc1394_dma_unlisten(dc1394->handle, &dc1394->camera); - dc1394_dma_release_camera(dc1394->handle, &dc1394->camera); -out_handle: - dc1394_destroy_handle(dc1394->handle); -out: - return -1; -} - -static int dc1394_v1_read_packet(AVFormatContext *c, AVPacket *pkt) -{ - struct dc1394_data *dc1394 = c->priv_data; - int res; - - /* discard stale frame */ - if (dc1394->current_frame++) { - if (dc1394_dma_done_with_buffer(&dc1394->camera) != DC1394_SUCCESS) - av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame); - } - - res = dc1394_dma_single_capture(&dc1394->camera); - - if (res == DC1394_SUCCESS) { - dc1394->packet.data = (uint8_t *)(dc1394->camera.capture_buffer); - dc1394->packet.pts = (dc1394->current_frame * 1000000) / dc1394->frame_rate; - res = dc1394->packet.size; - } else { - av_log(c, AV_LOG_ERROR, "DMA capture failed\n"); - dc1394->packet.data = NULL; - res = -1; - } - - *pkt = dc1394->packet; - return res; -} - -static int dc1394_v1_close(AVFormatContext * context) -{ - struct dc1394_data *dc1394 = context->priv_data; - - dc1394_stop_iso_transmission(dc1394->handle, dc1394->camera.node); - dc1394_dma_unlisten(dc1394->handle, &dc1394->camera); - dc1394_dma_release_camera(dc1394->handle, &dc1394->camera); - dc1394_destroy_handle(dc1394->handle); - - return 0; -} - -#elif HAVE_LIBDC1394_2 -static int dc1394_v2_read_header(AVFormatContext *c, AVFormatParameters * ap) -{ - dc1394_data* dc1394 = c->priv_data; - dc1394camera_list_t *list; - int res, i; - struct dc1394_frame_format *fmt = NULL; - struct dc1394_frame_rate *fps = NULL; - - if (dc1394_read_common(c,ap,&fmt,&fps) != 0) - return -1; - - /* Now let us prep the hardware. */ - dc1394->d = dc1394_new(); - dc1394_camera_enumerate (dc1394->d, &list); - if ( !list || list->num == 0) { - av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n"); - goto out; - } - - /* FIXME: To select a specific camera I need to search in list its guid */ - dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid); - if (list->num > 1) { - av_log(c, AV_LOG_INFO, "Working with the first camera found\n"); - } - - /* Freeing list of cameras */ - dc1394_camera_free_list (list); + vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, final_frame_rate, 1000); /* Select MAX Speed possible from the cam */ if (dc1394->camera->bmode_capable>0) { @@ -328,13 +292,13 @@ static int dc1394_v2_read_header(AVFormatContext *c, AVFormatParameters * ap) goto out_camera; } - if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) { + if (dc1394_video_set_mode(dc1394->camera, video_mode) != DC1394_SUCCESS) { av_log(c, AV_LOG_ERROR, "Couldn't set video format\n"); goto out_camera; } - if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) { - av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate); + if (dc1394_video_set_framerate(dc1394->camera, frame_rate) != DC1394_SUCCESS) { + av_log(c, AV_LOG_ERROR, "Could not set framerate %d.\n", final_frame_rate); goto out_camera; } if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) { @@ -354,10 +318,10 @@ out_camera: dc1394_camera_free (dc1394->camera); out: dc1394_free(dc1394->d); - return -1; + return ret; } -static int dc1394_v2_read_packet(AVFormatContext *c, AVPacket *pkt) +static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt) { struct dc1394_data *dc1394 = c->priv_data; int res; @@ -383,7 +347,7 @@ static int dc1394_v2_read_packet(AVFormatContext *c, AVPacket *pkt) return res; } -static int dc1394_v2_close(AVFormatContext * context) +static int dc1394_close(AVFormatContext * context) { struct dc1394_data *dc1394 = context->priv_data; @@ -397,25 +361,11 @@ static int dc1394_v2_close(AVFormatContext * context) AVInputFormat ff_libdc1394_demuxer = { .name = "libdc1394", - .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"), - .priv_data_size = sizeof(struct dc1394_data), - .read_header = dc1394_v2_read_header, - .read_packet = dc1394_v2_read_packet, - .read_close = dc1394_v2_close, - .flags = AVFMT_NOFILE, - .priv_class = &libdc1394_class, -}; - -#endif -#if HAVE_LIBDC1394_1 -AVInputFormat ff_libdc1394_demuxer = { - .name = "libdc1394", - .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.1 A/V grab"), + .long_name = NULL_IF_CONFIG_SMALL("dc1394 A/V grab"), .priv_data_size = sizeof(struct dc1394_data), - .read_header = dc1394_v1_read_header, - .read_packet = dc1394_v1_read_packet, - .read_close = dc1394_v1_close, + .read_header = dc1394_read_header, + .read_packet = dc1394_read_packet, + .read_close = dc1394_close, .flags = AVFMT_NOFILE, .priv_class = &libdc1394_class, }; -#endif diff --git a/libavdevice/openal-dec.c b/libavdevice/openal-dec.c new file mode 100644 index 0000000..efe40f6 --- /dev/null +++ b/libavdevice/openal-dec.c @@ -0,0 +1,250 @@ +/* + * Copyright (c) 2011 Jonathan Baldwin + * + * This file is part of FFmpeg. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH + * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, + * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM + * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ + +/** + * @file + * OpenAL 1.1 capture device for libavdevice + **/ + +#include <AL/al.h> +#include <AL/alc.h> + +#include "libavutil/opt.h" +#include "avdevice.h" + +typedef struct { + AVClass *class; + /** OpenAL capture device context. **/ + ALCdevice *device; + /** The number of channels in the captured audio. **/ + int channels; + /** The sample rate (in Hz) of the captured audio. **/ + int sample_rate; + /** The sample size (in bits) of the captured audio. **/ + int sample_size; + /** The OpenAL sample format of the captured audio. **/ + ALCenum sample_format; + /** The number of bytes between two consecutive samples of the same channel/component. **/ + ALCint sample_step; + /** If true, print a list of capture devices on this system and exit. **/ + int list_devices; +} al_data; + +typedef struct { + ALCenum al_fmt; + enum CodecID codec_id; + int channels; +} 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}, + [AL_FORMAT_MONO16-LOWEST_AL_FORMAT] = {AL_FORMAT_MONO16, AV_NE (CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE), 1}, + [AL_FORMAT_STEREO8-LOWEST_AL_FORMAT] = {AL_FORMAT_STEREO8, CODEC_ID_PCM_U8, 2}, + [AL_FORMAT_STEREO16-LOWEST_AL_FORMAT] = {AL_FORMAT_STEREO16, AV_NE (CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE), 2}, + }; + + 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); + } +} + +/** + * Print out a list of OpenAL capture devices on this system. + */ +static inline void print_al_capture_devices(void *log_ctx) +{ + const char *devices; + + if (!(devices = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER))) + return; + + av_log(log_ctx, AV_LOG_INFO, "List of OpenAL capture devices on this system:\n"); + + for (; *devices != '\0'; devices += strlen(devices) + 1) + av_log(log_ctx, AV_LOG_INFO, " %s\n", devices); +} + +static int read_header(AVFormatContext *ctx, AVFormatParameters *ap) +{ + al_data *ad = ctx->priv_data; + static const ALCenum sample_formats[2][2] = { + { AL_FORMAT_MONO8, AL_FORMAT_STEREO8 }, + { AL_FORMAT_MONO16, AL_FORMAT_STEREO16 } + }; + int error = 0; + const char *error_msg; + AVStream *st = NULL; + AVCodecContext *codec = NULL; + + if (ad->list_devices) { + print_al_capture_devices(ctx); + return AVERROR_EXIT; + } + + 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 : NULL, + ad->sample_rate, + ad->sample_format, + ad->sample_rate); /* Maximum 1 second of sample data to be read at once */ + + 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; + +fail: + /* Handle failure */ + if (ad->device) + alcCaptureCloseDevice(ad->device); + if (error_msg) + av_log(ctx, AV_LOG_ERROR, "Cannot open device: %s\n", error_msg); + return error; +} + +static int read_packet(AVFormatContext* ctx, AVPacket *pkt) +{ + al_data *ad = ctx->priv_data; + int error=0; + const char *error_msg; + ALCint nb_samples; + + /* Get number of samples available */ + alcGetIntegerv(ad->device, ALC_CAPTURE_SAMPLES, (ALCsizei) sizeof(ALCint), &nb_samples); + if (error = al_get_error(ad->device, &error_msg)) goto fail; + + /* Create a packet of appropriate size */ + av_new_packet(pkt, nb_samples*ad->sample_step); + pkt->pts = av_gettime(); + + /* Fill the packet with the available samples */ + alcCaptureSamples(ad->device, pkt->data, nb_samples); + if (error = al_get_error(ad->device, &error_msg)) goto fail; + + return pkt->size; +fail: + /* Handle failure */ + if (pkt->data) + av_destruct_packet(pkt); + if (error_msg) + av_log(ctx, AV_LOG_ERROR, "Error: %s\n", error_msg); + 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; +} + +#define OFFSET(x) offsetof(al_data, x) + +static const AVOption options[] = { + {"channels", "set number of channels", OFFSET(channels), FF_OPT_TYPE_INT, {.dbl=2}, 1, 2, AV_OPT_FLAG_DECODING_PARAM }, + {"sample_rate", "set sample rate", OFFSET(sample_rate), FF_OPT_TYPE_INT, {.dbl=44100}, 1, 192000, AV_OPT_FLAG_DECODING_PARAM }, + {"sample_size", "set sample size", OFFSET(sample_size), FF_OPT_TYPE_INT, {.dbl=16}, 8, 16, AV_OPT_FLAG_DECODING_PARAM }, + {"list_devices", "list available devices", OFFSET(list_devices), FF_OPT_TYPE_INT, {.dbl=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM, "list_devices" }, + {"true", "", 0, FF_OPT_TYPE_CONST, {.dbl=1}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "list_devices" }, + {"false", "", 0, FF_OPT_TYPE_CONST, {.dbl=0}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "list_devices" }, + {NULL}, +}; + +static const AVClass class = { + .class_name = "openal", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT +}; + +AVInputFormat ff_openal_demuxer = { + .name = "openal", + .long_name = NULL_IF_CONFIG_SMALL("OpenAL audio capture device"), + .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 +}; diff --git a/libavdevice/oss_audio.c b/libavdevice/oss_audio.c index 6df7f34..73cb575 100644 --- a/libavdevice/oss_audio.c +++ b/libavdevice/oss_audio.c @@ -2,20 +2,20 @@ * Linux audio play and grab interface * Copyright (c) 2000, 2001 Fabrice Bellard * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -39,7 +39,7 @@ #include "libavutil/log.h" #include "libavutil/opt.h" #include "libavcodec/avcodec.h" -#include "libavformat/avformat.h" +#include "avdevice.h" #define AUDIO_BLOCK_SIZE 4096 diff --git a/libavdevice/sdl.c b/libavdevice/sdl.c new file mode 100644 index 0000000..4b2566f --- /dev/null +++ b/libavdevice/sdl.c @@ -0,0 +1,230 @@ +/* + * Copyright (c) 2011 Stefano Sabatini + * + * 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 + */ + +/** + * @file + * libSDL output device + */ + +#include <SDL.h> +#include "libavutil/avstring.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/pixdesc.h" +#include "avdevice.h" + +typedef struct { + AVClass *class; + SDL_Surface *surface; + SDL_Overlay *overlay; + char *window_title; + char *icon_title; + char *window_size; + int window_width, window_height; + int overlay_width, overlay_height; + int overlay_fmt; + int sdl_was_already_inited; +} SDLContext; + +struct sdl_overlay_pix_fmt_entry { + enum PixelFormat pix_fmt; int overlay_fmt; +} sdl_overlay_pix_fmt_map[] = { + { PIX_FMT_YUV420P, SDL_IYUV_OVERLAY }, + { PIX_FMT_YUYV422, SDL_YUY2_OVERLAY }, + { PIX_FMT_UYVY422, SDL_UYVY_OVERLAY }, + { PIX_FMT_NONE, 0 }, +}; + +static int sdl_write_trailer(AVFormatContext *s) +{ + SDLContext *sdl = s->priv_data; + + av_freep(&sdl->window_title); + av_freep(&sdl->icon_title); + av_freep(&sdl->window_size); + + if (sdl->overlay) { + SDL_FreeYUVOverlay(sdl->overlay); + sdl->overlay = NULL; + } + if (!sdl->sdl_was_already_inited) + SDL_Quit(); + + return 0; +} + +static int sdl_write_header(AVFormatContext *s) +{ + SDLContext *sdl = s->priv_data; + AVStream *st = s->streams[0]; + AVCodecContext *encctx = st->codec; + float sar, dar; /* sample and display aspect ratios */ + int i, ret; + + if (!sdl->window_title) + sdl->window_title = av_strdup(s->filename); + if (!sdl->icon_title) + sdl->icon_title = av_strdup(sdl->window_title); + + if (SDL_WasInit(SDL_INIT_VIDEO)) { + av_log(s, AV_LOG_ERROR, + "SDL video subsystem was already inited, aborting.\n"); + sdl->sdl_was_already_inited = 1; + ret = AVERROR(EINVAL); + goto fail; + } + + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + av_log(s, AV_LOG_ERROR, "Unable to initialize SDL: %s\n", SDL_GetError()); + ret = AVERROR(EINVAL); + goto fail; + } + + if ( s->nb_streams > 1 + || encctx->codec_type != AVMEDIA_TYPE_VIDEO + || encctx->codec_id != CODEC_ID_RAWVIDEO) { + av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n"); + ret = AVERROR(EINVAL); + goto fail; + } + + for (i = 0; sdl_overlay_pix_fmt_map[i].pix_fmt != PIX_FMT_NONE; i++) { + if (sdl_overlay_pix_fmt_map[i].pix_fmt == encctx->pix_fmt) { + sdl->overlay_fmt = sdl_overlay_pix_fmt_map[i].overlay_fmt; + break; + } + } + + if (!sdl->overlay_fmt) { + av_log(s, AV_LOG_ERROR, + "Unsupported pixel format '%s', choose one of yuv420p, yuyv422, or uyvy422.\n", + av_get_pix_fmt_name(encctx->pix_fmt)); + ret = AVERROR(EINVAL); + goto fail; + } + + if (sdl->window_size) { + if (av_parse_video_size(&sdl->window_width, &sdl->window_height, + sdl->window_size) < 0) { + av_log(s, AV_LOG_ERROR, "Invalid window size '%s'\n", sdl->window_size); + ret = AVERROR(EINVAL); + goto fail; + } + } + + /* compute overlay width and height from the codec context information */ + sar = st->sample_aspect_ratio.num ? av_q2d(st->sample_aspect_ratio) : 1; + dar = sar * (float)encctx->width / (float)encctx->height; + + /* we suppose the screen has a 1/1 sample aspect ratio */ + sdl->overlay_height = encctx->height; + sdl->overlay_width = ((int)rint(sdl->overlay_height * dar)); + if (sdl->overlay_width > encctx->width) { + sdl->overlay_width = encctx->width; + sdl->overlay_height = ((int)rint(sdl->overlay_width / dar)); + } + + if (!sdl->window_width || !sdl->window_height) { + sdl->window_width = sdl->overlay_width; + sdl->window_height = sdl->overlay_height; + } + + SDL_WM_SetCaption(sdl->window_title, sdl->icon_title); + sdl->surface = SDL_SetVideoMode(sdl->window_width, sdl->window_height, + 24, SDL_SWSURFACE); + if (!sdl->surface) { + av_log(s, AV_LOG_ERROR, "Unable to set video mode: %s\n", SDL_GetError()); + ret = AVERROR(EINVAL); + goto fail; + } + + sdl->overlay = SDL_CreateYUVOverlay(sdl->overlay_width, sdl->overlay_height, + sdl->overlay_fmt, sdl->surface); + if (!sdl->overlay || sdl->overlay->pitches[0] < sdl->overlay_width) { + av_log(s, AV_LOG_ERROR, + "SDL does not support an overlay with size of %dx%d pixels.\n", + sdl->overlay_width, sdl->overlay_height); + ret = AVERROR(EINVAL); + goto fail; + } + + av_log(s, AV_LOG_INFO, "w:%d h:%d fmt:%s sar:%f -> w:%d h:%d\n", + encctx->width, encctx->height, av_get_pix_fmt_name(encctx->pix_fmt), sar, + sdl->window_width, sdl->window_height); + return 0; + +fail: + sdl_write_trailer(s); + return ret; +} + +static int sdl_write_packet(AVFormatContext *s, AVPacket *pkt) +{ + SDLContext *sdl = s->priv_data; + AVCodecContext *encctx = s->streams[0]->codec; + SDL_Rect rect = { 0, 0, sdl->window_width, sdl->window_height }; + AVPicture pict; + int i; + + avpicture_fill(&pict, pkt->data, encctx->pix_fmt, encctx->width, encctx->height); + + SDL_FillRect(sdl->surface, &sdl->surface->clip_rect, + SDL_MapRGB(sdl->surface->format, 0, 0, 0)); + SDL_LockYUVOverlay(sdl->overlay); + for (i = 0; i < 3; i++) { + sdl->overlay->pixels [i] = pict.data [i]; + sdl->overlay->pitches[i] = pict.linesize[i]; + } + SDL_DisplayYUVOverlay(sdl->overlay, &rect); + SDL_UnlockYUVOverlay(sdl->overlay); + + SDL_UpdateRect(sdl->surface, 0, 0, sdl->overlay_width, sdl->overlay_height); + + return 0; +} + +#define OFFSET(x) offsetof(SDLContext,x) + +static const AVOption options[] = { + { "window_title", "SDL window title", OFFSET(window_title), FF_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, + { "icon_title", "SDL iconified window title", OFFSET(icon_title) , FF_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, + { "window_size", "SDL window forced size", OFFSET(window_size) , FF_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, + { NULL }, +}; + +static const AVClass sdl_class = { + .class_name = "sdl outdev", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVOutputFormat ff_sdl_muxer = { + .name = "sdl", + .long_name = NULL_IF_CONFIG_SMALL("SDL output device"), + .priv_data_size = sizeof(SDLContext), + .audio_codec = CODEC_ID_NONE, + .video_codec = CODEC_ID_RAWVIDEO, + .write_header = sdl_write_header, + .write_packet = sdl_write_packet, + .write_trailer = sdl_write_trailer, + .flags = AVFMT_NOFILE, + .priv_class = &sdl_class, +}; diff --git a/libavdevice/sndio_common.c b/libavdevice/sndio_common.c index 56c37c7..048e72e 100644 --- a/libavdevice/sndio_common.c +++ b/libavdevice/sndio_common.c @@ -2,27 +2,27 @@ * sndio play and grab interface * Copyright (c) 2010 Jacob Meuser * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include <stdint.h> #include <sndio.h> -#include "libavformat/avformat.h" +#include "avdevice.h" #include "sndio_common.h" diff --git a/libavdevice/sndio_common.h b/libavdevice/sndio_common.h index e23b96d..12218b4 100644 --- a/libavdevice/sndio_common.h +++ b/libavdevice/sndio_common.h @@ -2,20 +2,20 @@ * sndio play and grab interface * Copyright (c) 2010 Jacob Meuser * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -25,8 +25,8 @@ #include <stdint.h> #include <sndio.h> -#include "libavformat/avformat.h" #include "libavutil/log.h" +#include "avdevice.h" typedef struct { AVClass *class; diff --git a/libavdevice/sndio_dec.c b/libavdevice/sndio_dec.c index 40e9236..6e386c6 100644 --- a/libavdevice/sndio_dec.c +++ b/libavdevice/sndio_dec.c @@ -2,20 +2,20 @@ * sndio play and grab interface * Copyright (c) 2010 Jacob Meuser * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ diff --git a/libavdevice/sndio_enc.c b/libavdevice/sndio_enc.c index 49a52b3..9ad5cad 100644 --- a/libavdevice/sndio_enc.c +++ b/libavdevice/sndio_enc.c @@ -2,28 +2,27 @@ * sndio play and grab interface * Copyright (c) 2010 Jacob Meuser * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include <stdint.h> #include <sndio.h> -#include "libavformat/avformat.h" - +#include "avdevice.h" #include "sndio_common.h" static av_cold int audio_write_header(AVFormatContext *s1) diff --git a/libavdevice/v4l.c b/libavdevice/v4l.c index b0f0452..3950474 100644 --- a/libavdevice/v4l.c +++ b/libavdevice/v4l.c @@ -2,20 +2,20 @@ * Linux video grab interface * Copyright (c) 2000,2001 Fabrice Bellard * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -29,7 +29,6 @@ #include "libavutil/imgutils.h" #include "libavutil/log.h" #include "libavutil/opt.h" -#include "libavformat/avformat.h" #include "libavcodec/dsputil.h" #include <unistd.h> #include <fcntl.h> @@ -40,6 +39,7 @@ #include <linux/videodev.h> #include <time.h> #include <strings.h> +#include "avdevice.h" typedef struct { AVClass *class; diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c index 7a501a9..9682d21 100644 --- a/libavdevice/v4l2.c +++ b/libavdevice/v4l2.c @@ -1,35 +1,37 @@ /* - * Video4Linux2 grab interface * Copyright (c) 2000,2001 Fabrice Bellard * Copyright (c) 2006 Luca Abeni * - * Part of this file is based on the V4L2 video capture example - * (http://v4l2spec.bytesex.org/v4l2spec/capture.c) + * This file is part of FFmpeg. * - * Thanks to Michael Niedermayer for providing the mapping between - * V4L2_PIX_FMT_* and PIX_FMT_* - * - * - * This file is part of Libav. - * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +/** + * @file + * Video4Linux2 grab interface + * + * Part of this file is based on the V4L2 video capture example + * (http://v4l2spec.bytesex.org/v4l2spec/capture.c) + * + * Thanks to Michael Niedermayer for providing the mapping between + * V4L2_PIX_FMT_* and PIX_FMT_* + */ + #undef __STRICT_ANSI__ //workaround due to broken kernel headers #include "config.h" -#include "libavformat/avformat.h" #include <unistd.h> #include <fcntl.h> #include <sys/ioctl.h> @@ -46,6 +48,7 @@ #include "libavutil/imgutils.h" #include "libavutil/log.h" #include "libavutil/opt.h" +#include "avdevice.h" #include "libavutil/parseutils.h" #include "libavutil/pixdesc.h" @@ -120,7 +123,6 @@ static int device_open(AVFormatContext *ctx, uint32_t *capabilities) if (fd < 0) { av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n", ctx->filename, strerror(errno)); - return AVERROR(errno); } @@ -136,13 +138,11 @@ static int device_open(AVFormatContext *ctx, uint32_t *capabilities) av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n", strerror(errno)); close(fd); - return AVERROR(err); } if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) { av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n"); close(fd); - return AVERROR(ENODEV); } *capabilities = cap.capabilities; @@ -154,10 +154,9 @@ static int device_init(AVFormatContext *ctx, int *width, int *height, uint32_t p { struct video_data *s = ctx->priv_data; int fd = s->fd; - struct v4l2_format fmt; + struct v4l2_format fmt = {0}; int res; - memset(&fmt, 0, sizeof(struct v4l2_format)); fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; fmt.fmt.pix.width = *width; fmt.fmt.pix.height = *height; @@ -240,10 +239,9 @@ static enum CodecID fmt_v4l2codec(uint32_t v4l2_fmt) static int mmap_init(AVFormatContext *ctx) { struct video_data *s = ctx->priv_data; - struct v4l2_requestbuffers req; + struct v4l2_requestbuffers req = {0}; int i, res; - memset(&req, 0, sizeof(struct v4l2_requestbuffers)); req.count = desired_video_buffers; req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; req.memory = V4L2_MEMORY_MMAP; @@ -254,41 +252,35 @@ static int mmap_init(AVFormatContext *ctx) } else { av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n"); } - return AVERROR(errno); } if (req.count < 2) { av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n"); - return AVERROR(ENOMEM); } s->buffers = req.count; s->buf_start = av_malloc(sizeof(void *) * s->buffers); if (s->buf_start == NULL) { av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n"); - return AVERROR(ENOMEM); } s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers); if (s->buf_len == NULL) { av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n"); av_free(s->buf_start); - return AVERROR(ENOMEM); } for (i = 0; i < req.count; i++) { - struct v4l2_buffer buf; + struct v4l2_buffer buf = {0}; - memset(&buf, 0, sizeof(struct v4l2_buffer)); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; buf.index = i; res = ioctl(s->fd, VIDIOC_QUERYBUF, &buf); if (res < 0) { av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n"); - return AVERROR(errno); } @@ -302,7 +294,6 @@ static int mmap_init(AVFormatContext *ctx) PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset); if (s->buf_start[i] == MAP_FAILED) { av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno)); - return AVERROR(errno); } } @@ -317,7 +308,7 @@ static int read_init(AVFormatContext *ctx) static void mmap_release_buffer(AVPacket *pkt) { - struct v4l2_buffer buf; + struct v4l2_buffer buf = {0}; int res, fd; struct buff_data *buf_descriptor = pkt->priv; @@ -325,7 +316,6 @@ static void mmap_release_buffer(AVPacket *pkt) return; } - memset(&buf, 0, sizeof(struct v4l2_buffer)); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; buf.index = buf_descriptor->index; @@ -343,11 +333,10 @@ static void mmap_release_buffer(AVPacket *pkt) static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt) { struct video_data *s = ctx->priv_data; - struct v4l2_buffer buf; + struct v4l2_buffer buf = {0}; struct buff_data *buf_descriptor; int res; - memset(&buf, 0, sizeof(struct v4l2_buffer)); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; @@ -356,17 +345,15 @@ static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt) if (res < 0) { if (errno == EAGAIN) { pkt->size = 0; - return AVERROR(EAGAIN); } av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno)); return AVERROR(errno); } - assert (buf.index < s->buffers); + assert(buf.index < s->buffers); if (s->frame_size > 0 && buf.bytesused != s->frame_size) { av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size); - return AVERROR_INVALIDDATA; } @@ -404,9 +391,8 @@ static int mmap_start(AVFormatContext *ctx) int i, res; for (i = 0; i < s->buffers; i++) { - struct v4l2_buffer buf; + struct v4l2_buffer buf = {0}; - memset(&buf, 0, sizeof(struct v4l2_buffer)); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; buf.index = i; @@ -414,7 +400,6 @@ static int mmap_start(AVFormatContext *ctx) res = ioctl(s->fd, VIDIOC_QBUF, &buf); if (res < 0) { av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno)); - return AVERROR(errno); } } @@ -423,7 +408,6 @@ static int mmap_start(AVFormatContext *ctx) res = ioctl(s->fd, VIDIOC_STREAMON, &type); if (res < 0) { av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno)); - return AVERROR(errno); } @@ -450,9 +434,9 @@ static void mmap_close(struct video_data *s) static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap) { struct video_data *s = s1->priv_data; - struct v4l2_input input; - struct v4l2_standard standard; - struct v4l2_streamparm streamparm = { 0 }; + struct v4l2_input input = {0}; + struct v4l2_standard standard = {0}; + struct v4l2_streamparm streamparm = {0}; struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe; int i, ret; AVRational framerate_q; @@ -465,7 +449,6 @@ static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap) } /* set tv video input */ - memset (&input, 0, sizeof (input)); input.index = s->channel; if (ioctl(s->fd, VIDIOC_ENUMINPUT, &input) < 0) { av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n"); @@ -484,18 +467,15 @@ static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap) av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n", s->standard); /* set tv standard */ - memset (&standard, 0, sizeof (standard)); - for(i=0;;i++) { + for (i = 0;; i++) { standard.index = i; - if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) { - av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n", - s->standard); - return AVERROR(EIO); - } - - if (!strcasecmp(standard.name, s->standard)) { + ret = ioctl(s->fd, VIDIOC_ENUMSTD, &standard); + if (ret < 0 || !strcasecmp(standard.name, s->standard)) break; - } + } + if (ret < 0) { + av_log(s1, AV_LOG_ERROR, "Unknown standard '%s'\n", s->standard); + return ret; } av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n", @@ -650,7 +630,6 @@ static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap) } if (res < 0) { close(s->fd); - res = AVERROR(EIO); goto out; } @@ -708,9 +687,10 @@ static int v4l2_read_close(AVFormatContext *s1) #define OFFSET(x) offsetof(struct video_data, x) #define DEC AV_OPT_FLAG_DECODING_PARAM + static const AVOption options[] = { - { "standard", "", offsetof(struct video_data, standard), FF_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_DECODING_PARAM }, - { "channel", "", offsetof(struct video_data, channel), FF_OPT_TYPE_INT, {.dbl = 0 }, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, + { "standard", "", OFFSET(standard), FF_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_DECODING_PARAM }, + { "channel", "", OFFSET(channel), FF_OPT_TYPE_INT, {.dbl = 0 }, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, diff --git a/libavdevice/vfwcap.c b/libavdevice/vfwcap.c index 47a9ebe..e5ecf84 100644 --- a/libavdevice/vfwcap.c +++ b/libavdevice/vfwcap.c @@ -2,36 +2,34 @@ * VFW capture interface * Copyright (c) 2006-2008 Ramiro Polla * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "libavformat/avformat.h" #include "libavutil/log.h" #include "libavutil/opt.h" #include "libavutil/parseutils.h" #include <windows.h> #include <vfw.h> +#include "avdevice.h" /* Defines for VFW missing from MinGW. * Remove this when MinGW incorporates them. */ #define HWND_MESSAGE ((HWND)-3) -#define BI_RGB 0 - /* End of missing MinGW defines */ struct vfw_ctx { @@ -244,7 +242,7 @@ static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap) AVStream *st; int devnum; int bisize; - BITMAPINFO *bi; + BITMAPINFO *bi = NULL; CAPTUREPARMS cparms; DWORD biCompression; WORD biBitCount; @@ -290,7 +288,7 @@ static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap) (LPARAM) videostream_cb); if(!ret) { av_log(s, AV_LOG_ERROR, "Could not set video stream callback.\n"); - goto fail_io; + goto fail; } SetWindowLongPtr(ctx->hwnd, GWLP_USERDATA, (LONG_PTR) s); @@ -304,7 +302,7 @@ static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap) /* Set video format */ bisize = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, 0, 0); if(!bisize) - goto fail_io; + goto fail; bi = av_malloc(bisize); if(!bi) { vfw_read_close(s); @@ -312,16 +310,21 @@ static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap) } ret = SendMessage(ctx->hwnd, WM_CAP_GET_VIDEOFORMAT, bisize, (LPARAM) bi); if(!ret) - goto fail_bi; + goto fail; dump_bih(s, &bi->bmiHeader); + ret = av_parse_video_rate(&framerate_q, ctx->framerate); + if (ret < 0) { + av_log(s, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", ctx->framerate); + goto fail; + } if (ctx->video_size) { ret = av_parse_video_size(&bi->bmiHeader.biWidth, &bi->bmiHeader.biHeight, ctx->video_size); if (ret < 0) { av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n"); - goto fail_bi; + goto fail; } } @@ -340,19 +343,17 @@ static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap) ret = SendMessage(ctx->hwnd, WM_CAP_SET_VIDEOFORMAT, bisize, (LPARAM) bi); if(!ret) { av_log(s, AV_LOG_ERROR, "Could not set Video Format.\n"); - goto fail_bi; + goto fail; } biCompression = bi->bmiHeader.biCompression; biBitCount = bi->bmiHeader.biBitCount; - av_free(bi); - /* Set sequence setup */ ret = SendMessage(ctx->hwnd, WM_CAP_GET_SEQUENCE_SETUP, sizeof(cparms), (LPARAM) &cparms); if(!ret) - goto fail_io; + goto fail; dump_captureparms(s, &cparms); @@ -367,7 +368,7 @@ static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap) ret = SendMessage(ctx->hwnd, WM_CAP_SET_SEQUENCE_SETUP, sizeof(cparms), (LPARAM) &cparms); if(!ret) - goto fail_io; + goto fail; codec = st->codec; codec->time_base = (AVRational){framerate_q.den, framerate_q.num}; @@ -396,31 +397,31 @@ static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap) } } + av_freep(&bi); + av_set_pts_info(st, 32, 1, 1000); ctx->mutex = CreateMutex(NULL, 0, NULL); if(!ctx->mutex) { av_log(s, AV_LOG_ERROR, "Could not create Mutex.\n" ); - goto fail_io; + goto fail; } ctx->event = CreateEvent(NULL, 1, 0, NULL); if(!ctx->event) { av_log(s, AV_LOG_ERROR, "Could not create Event.\n" ); - goto fail_io; + goto fail; } ret = SendMessage(ctx->hwnd, WM_CAP_SEQUENCE_NOFILE, 0, 0); if(!ret) { av_log(s, AV_LOG_ERROR, "Could not start capture sequence.\n" ); - goto fail_io; + goto fail; } return 0; -fail_bi: - av_free(bi); - -fail_io: +fail: + av_freep(&bi); vfw_read_close(s); return AVERROR(EIO); } diff --git a/libavdevice/x11grab.c b/libavdevice/x11grab.c index 5cc32dc..7debe9d 100644 --- a/libavdevice/x11grab.c +++ b/libavdevice/x11grab.c @@ -1,9 +1,9 @@ /* * X11 video grab interface * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav integration: + * FFmpeg integration: * Copyright (C) 2006 Clemens Fruhwirth <clemens@endorphin.org> * Edouard Gomez <ed.gomez@free.fr> * @@ -14,18 +14,18 @@ * Copyright (C) 1997-1998 Rasca, Berlin * 2003-2004 Karl H. Beckers, Frankfurt * - * Libav is free software; you can redistribute it and/or modify + * FFmpeg is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * 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 General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with Libav; if not, write to the Free Software + * along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -36,7 +36,6 @@ */ #include "config.h" -#include "libavformat/avformat.h" #include "libavutil/log.h" #include "libavutil/opt.h" #include "libavutil/parseutils.h" @@ -50,6 +49,7 @@ #include <X11/extensions/shape.h> #include <X11/extensions/XShm.h> #include <X11/extensions/Xfixes.h> +#include "avdevice.h" /** * X11 Device Demuxer context @@ -163,12 +163,12 @@ x11grab_read_header(AVFormatContext *s1, AVFormatParameters *ap) int y_off = 0; int screen; int use_shm; - char *param, *offset; + char *dpyname, *offset; int ret = 0; AVRational framerate; - param = av_strdup(s1->filename); - offset = strchr(param, '+'); + dpyname = av_strdup(s1->filename); + offset = strchr(dpyname, '+'); if (offset) { sscanf(offset, "%d,%d", &x_off, &y_off); x11grab->draw_mouse = !strstr(offset, "nomouse"); @@ -184,9 +184,10 @@ x11grab_read_header(AVFormatContext *s1, AVFormatParameters *ap) goto out; } av_log(s1, AV_LOG_INFO, "device: %s -> display: %s x: %d y: %d width: %d height: %d\n", - s1->filename, param, x_off, y_off, x11grab->width, x11grab->height); + s1->filename, dpyname, x_off, y_off, x11grab->width, x11grab->height); - dpy = XOpenDisplay(param); + dpy = XOpenDisplay(dpyname); + av_freep(&dpyname); if(!dpy) { av_log(s1, AV_LOG_ERROR, "Could not open X display.\n"); ret = AVERROR(EIO); @@ -217,7 +218,7 @@ x11grab_read_header(AVFormatContext *s1, AVFormatParameters *ap) } use_shm = XShmQueryExtension(dpy); - av_log(s1, AV_LOG_INFO, "shared memory extension %s found\n", use_shm ? "" : "not"); + av_log(s1, AV_LOG_INFO, "shared memory extension%s found\n", use_shm ? "" : " not"); if(use_shm) { int scr = XDefaultScreen(dpy); |