Add AVFoundation input device.
authorThilo Borgmann <borgmann@nue170.nue.tu-berlin.de>
Fri, 11 Apr 2014 15:29:07 +0000 (17:29 +0200)
committerMichael Niedermayer <michaelni@gmx.at>
Fri, 11 Apr 2014 15:51:33 +0000 (17:51 +0200)
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
Changelog
MAINTAINERS
configure
doc/indevs.texi
libavdevice/Makefile
libavdevice/alldevices.c
libavdevice/avfoundation.m [new file with mode: 0644]

index 6923200..2771684 100644 (file)
--- a/Changelog
+++ b/Changelog
@@ -16,6 +16,7 @@ version <next>:
 - support decoding 16-bit RLE SGI images
 - GDI screen grabbing for Windows
 - alternative rendition support for HTTP Live Streaming
+- AVFoundation input device
 
 
 version 2.2:
index f0ed81e..32e2732 100644 (file)
@@ -303,6 +303,7 @@ libavdevice
     libavdevice/avdevice.h
 
 
+  avfoundation.m                        Thilo Borgmann
   dshow.c                               Roger Pack (CC rogerdpack@gmail.com)
   fbdev_enc.c                           Lukasz Marek
   gdigrab.c                             Roger Pack (CC rogerdpack@gmail.com)
index 507a0e9..c962d41 100755 (executable)
--- a/configure
+++ b/configure
@@ -2366,6 +2366,8 @@ xwma_demuxer_select="riffdec"
 # indevs / outdevs
 alsa_indev_deps="alsa_asoundlib_h snd_pcm_htimestamp"
 alsa_outdev_deps="alsa_asoundlib_h"
+avfoundation_indev_extralibs="-framework AVFoundation -framework CoreMedia"
+avfoundation_indev_select="avfoundation"
 bktr_indev_deps_any="dev_bktr_ioctl_bt848_h machine_ioctl_bt848_h dev_video_bktr_ioctl_bt848_h dev_ic_bt8xx_h"
 caca_outdev_deps="libcaca"
 decklink_outdev_deps="decklink pthreads"
@@ -4579,6 +4581,7 @@ for func in $MATH_FUNCS; do
 done
 
 # these are off by default, so fail if requested and not available
+enabled avfoundation_indev && { check_header_oc AVFoundation/AVFoundation.h || disable avfoundation_indev; }
 enabled avisynth          && { { check_lib2 "windows.h" LoadLibrary; } ||
                                { check_lib2 "dlfcn.h" dlopen -ldl; } ||
                                die "ERROR: LoadLibrary/dlopen not found for avisynth"; }
index 552ba3a..7b94155 100644 (file)
@@ -51,6 +51,36 @@ ffmpeg -f alsa -i hw:0 alsaout.wav
 For more information see:
 @url{http://www.alsa-project.org/alsa-doc/alsa-lib/pcm.html}
 
+@section avfoundation
+
+AVFoundation input device.
+
+AVFoundation is the currently recommended framework by Apple for streamgrabbing on OSX >= 10.7 as well as on iOS.
+The older QTKit framework has been marked deprecated since OSX version 10.7.
+
+The filename passed as input is parsed to contain either a device name or index.
+The device index can also be given by using -video_device_index.
+A given device index will override any given device name.
+If the desired device consists of numbers only, use -video_device_index to identify it.
+The default device will be chosen if an empty string  or the device name "default" is given.
+The available devices can be enumerated by using -list_devices.
+
+@example
+ffmpeg -f avfoundation -i "0" out.mpg
+@end example
+
+@example
+ffmpeg -f avfoundation -video_device_index 0 -i "" out.mpg
+@end example
+
+@example
+ffmpeg -f avfoundation -i "default" out.mpg
+@end example
+
+@example
+ffmpeg -f avfoundation -list_devices true -i ""
+@end example
+
 @section bktr
 
 BSD video input device.
index 68b65cc..d4e6911 100644 (file)
@@ -15,6 +15,7 @@ OBJS-$(CONFIG_ALSA_INDEV)                += alsa-audio-common.o \
                                             alsa-audio-dec.o timefilter.o
 OBJS-$(CONFIG_ALSA_OUTDEV)               += alsa-audio-common.o \
                                             alsa-audio-enc.o
+OBJS-$(CONFIG_AVFOUNDATION_INDEV)        += avfoundation.o
 OBJS-$(CONFIG_BKTR_INDEV)                += bktr.o
 OBJS-$(CONFIG_CACA_OUTDEV)               += caca.o
 OBJS-$(CONFIG_DECKLINK_OUTDEV)           += decklink_enc.o decklink_enc_c.o
index 9164d68..ca34959 100644 (file)
@@ -47,6 +47,7 @@ void avdevice_register_all(void)
 
     /* devices */
     REGISTER_INOUTDEV(ALSA,             alsa);
+    REGISTER_INDEV   (AVFOUNDATION,     avfoundation);
     REGISTER_INDEV   (BKTR,             bktr);
     REGISTER_OUTDEV  (CACA,             caca);
     REGISTER_OUTDEV  (DECKLINK,         decklink);
diff --git a/libavdevice/avfoundation.m b/libavdevice/avfoundation.m
new file mode 100644 (file)
index 0000000..3622be0
--- /dev/null
@@ -0,0 +1,374 @@
+/*
+ * AVFoundation input device
+ * Copyright (c) 2014 Thilo Borgmann <thilo.borgmann@mail.de>
+ *
+ * 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
+ * AVFoundation input device
+ * @author Thilo Borgmann <thilo.borgmann@mail.de>
+ */
+
+#import <AVFoundation/AVFoundation.h>
+#include <pthread.h>
+
+#include "libavutil/pixdesc.h"
+#include "libavutil/opt.h"
+#include "libavformat/internal.h"
+#include "libavutil/internal.h"
+#include "libavutil/time.h"
+#include "avdevice.h"
+
+static const int avf_time_base = 100;
+
+static const AVRational avf_time_base_q = {
+    .num = 1,
+    .den = avf_time_base
+};
+
+typedef struct
+{
+    AVClass*        class;
+
+    float           frame_rate;
+    int             frames_captured;
+    int64_t         first_pts;
+    pthread_mutex_t frame_lock;
+    pthread_cond_t  frame_wait_cond;
+    id              avf_delegate;
+
+    int             list_devices;
+    int             video_device_index;
+
+    AVCaptureSession         *capture_session;
+    AVCaptureVideoDataOutput *video_output;
+    CMSampleBufferRef         current_frame;
+} AVFContext;
+
+static void lock_frames(AVFContext* ctx)
+{
+    pthread_mutex_lock(&ctx->frame_lock);
+}
+
+static void unlock_frames(AVFContext* ctx)
+{
+    pthread_mutex_unlock(&ctx->frame_lock);
+}
+
+/** FrameReciever class - delegate for AVCaptureSession
+ */
+@interface AVFFrameReceiver : NSObject
+{
+    AVFContext* _context;
+}
+
+- (id)initWithContext:(AVFContext*)context;
+
+- (void)  captureOutput:(AVCaptureOutput *)captureOutput
+  didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
+         fromConnection:(AVCaptureConnection *)connection;
+
+@end
+
+@implementation AVFFrameReceiver
+
+- (id)initWithContext:(AVFContext*)context
+{
+    if (self = [super init]) {
+        _context = context;
+    }
+    return self;
+}
+
+- (void)  captureOutput:(AVCaptureOutput *)captureOutput
+  didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
+         fromConnection:(AVCaptureConnection *)connection
+{
+    lock_frames(_context);
+
+    if (_context->current_frame != nil) {
+        CFRelease(_context->current_frame);
+    }
+
+    _context->current_frame = (CMSampleBufferRef)CFRetain(videoFrame);
+
+    pthread_cond_signal(&_context->frame_wait_cond);
+
+    unlock_frames(_context);
+
+    ++_context->frames_captured;
+}
+
+@end
+
+static void destroy_context(AVFContext* ctx)
+{
+    [ctx->capture_session stopRunning];
+
+    [ctx->capture_session release];
+    [ctx->video_output    release];
+    [ctx->avf_delegate    release];
+
+    ctx->capture_session = NULL;
+    ctx->video_output    = NULL;
+    ctx->avf_delegate    = NULL;
+
+    pthread_mutex_destroy(&ctx->frame_lock);
+    pthread_cond_destroy(&ctx->frame_wait_cond);
+
+    if (ctx->current_frame) {
+        CFRelease(ctx->current_frame);
+    }
+}
+
+static int avf_read_header(AVFormatContext *s)
+{
+    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+    AVFContext *ctx         = (AVFContext*)s->priv_data;
+    ctx->first_pts          = av_gettime();
+
+    pthread_mutex_init(&ctx->frame_lock, NULL);
+    pthread_cond_init(&ctx->frame_wait_cond, NULL);
+
+    // List devices if requested
+    if (ctx->list_devices) {
+        av_log(ctx, AV_LOG_INFO, "AVFoundation video devices:\n");
+        NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
+        for (AVCaptureDevice *device in devices) {
+            const char *name = [[device localizedName] UTF8String];
+            int index  = [devices indexOfObject:device];
+            av_log(ctx, AV_LOG_INFO, "[%d] %s\n", index, name);
+        }
+        goto fail;
+    }
+
+    // Find capture device
+    AVCaptureDevice *video_device = nil;
+
+    // check for device index given in filename
+    if (ctx->video_device_index == -1) {
+        sscanf(s->filename, "%d", &ctx->video_device_index);
+    }
+
+    if (ctx->video_device_index >= 0) {
+        NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
+
+        if (ctx->video_device_index >= [devices count]) {
+            av_log(ctx, AV_LOG_ERROR, "Invalid device index\n");
+            goto fail;
+        }
+
+        video_device = [devices objectAtIndex:ctx->video_device_index];
+    } else if (strncmp(s->filename, "",        1) &&
+               strncmp(s->filename, "default", 7)) {
+        NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
+
+        for (AVCaptureDevice *device in devices) {
+            if (!strncmp(s->filename, [[device localizedName] UTF8String], strlen(s->filename))) {
+                video_device = device;
+                break;
+            }
+        }
+
+        if (!video_device) {
+            av_log(ctx, AV_LOG_ERROR, "Video device not found\n");
+            goto fail;
+        }
+    } else {
+        video_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeMuxed];
+    }
+
+    // Video capture device not found, looking for AVMediaTypeVideo
+    if (!video_device) {
+        video_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
+
+        if (!video_device) {
+            av_log(s, AV_LOG_ERROR, "No AV capture device found\n");
+            goto fail;
+        }
+    }
+
+    NSString* dev_display_name = [video_device localizedName];
+    av_log(s, AV_LOG_DEBUG, "'%s' opened\n", [dev_display_name UTF8String]);
+
+    // Initialize capture session
+    ctx->capture_session = [[AVCaptureSession alloc] init];
+
+    NSError *error = nil;
+    AVCaptureDeviceInput* capture_dev_input = [[[AVCaptureDeviceInput alloc] initWithDevice:video_device error:&error] autorelease];
+
+    if (!capture_dev_input) {
+        av_log(s, AV_LOG_ERROR, "Failed to create AV capture input device: %s\n",
+               [[error localizedDescription] UTF8String]);
+        goto fail;
+    }
+
+    if (!capture_dev_input) {
+        av_log(s, AV_LOG_ERROR, "Failed to add AV capture input device to session: %s\n",
+               [[error localizedDescription] UTF8String]);
+        goto fail;
+    }
+
+    if ([ctx->capture_session canAddInput:capture_dev_input]) {
+        [ctx->capture_session addInput:capture_dev_input];
+    } else {
+        av_log(s, AV_LOG_ERROR, "can't add video input to capture session\n");
+        goto fail;
+    }
+
+    // Attaching output
+    // FIXME: Allow for a user defined pixel format
+    ctx->video_output = [[AVCaptureVideoDataOutput alloc] init];
+
+    if (!ctx->video_output) {
+        av_log(s, AV_LOG_ERROR, "Failed to init AV video output\n");
+        goto fail;
+    }
+
+    NSNumber     *pixel_format = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_24RGB];
+    NSDictionary *capture_dict = [NSDictionary dictionaryWithObject:pixel_format
+                                               forKey:(id)kCVPixelBufferPixelFormatTypeKey];
+
+    [ctx->video_output setVideoSettings:capture_dict];
+    [ctx->video_output setAlwaysDiscardsLateVideoFrames:YES];
+
+    ctx->avf_delegate = [[AVFFrameReceiver alloc] initWithContext:ctx];
+
+    dispatch_queue_t queue = dispatch_queue_create("avf_queue", NULL);
+    [ctx->video_output setSampleBufferDelegate:ctx->avf_delegate queue:queue];
+    dispatch_release(queue);
+
+    if ([ctx->capture_session canAddOutput:ctx->video_output]) {
+        [ctx->capture_session addOutput:ctx->video_output];
+    } else {
+        av_log(s, AV_LOG_ERROR, "can't add video output to capture session\n");
+        goto fail;
+    }
+
+    [ctx->capture_session startRunning];
+
+    // Take stream info from the first frame.
+    while (ctx->frames_captured < 1) {
+        CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
+    }
+
+    lock_frames(ctx);
+
+    AVStream* stream = avformat_new_stream(s, NULL);
+
+    if (!stream) {
+        goto fail;
+    }
+
+    avpriv_set_pts_info(stream, 64, 1, avf_time_base);
+
+    CVImageBufferRef image_buffer = CMSampleBufferGetImageBuffer(ctx->current_frame);
+    CGSize image_buffer_size      = CVImageBufferGetEncodedSize(image_buffer);
+
+    stream->codec->codec_id   = AV_CODEC_ID_RAWVIDEO;
+    stream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
+    stream->codec->width      = (int)image_buffer_size.width;
+    stream->codec->height     = (int)image_buffer_size.height;
+    stream->codec->pix_fmt    = AV_PIX_FMT_RGB24;
+
+    CFRelease(ctx->current_frame);
+    ctx->current_frame = nil;
+
+    unlock_frames(ctx);
+    [pool release];
+    return 0;
+
+fail:
+    [pool release];
+    destroy_context(ctx);
+    return AVERROR(EIO);
+}
+
+static int avf_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    AVFContext* ctx = (AVFContext*)s->priv_data;
+
+    do {
+        lock_frames(ctx);
+
+        CVImageBufferRef image_buffer = CMSampleBufferGetImageBuffer(ctx->current_frame);
+
+        if (ctx->current_frame != nil) {
+            if (av_new_packet(pkt, (int)CVPixelBufferGetDataSize(image_buffer)) < 0) {
+                return AVERROR(EIO);
+            }
+
+            pkt->pts = pkt->dts = av_rescale_q(av_gettime() - ctx->first_pts,
+                                               AV_TIME_BASE_Q,
+                                               avf_time_base_q);
+            pkt->stream_index  = 0;
+            pkt->flags        |= AV_PKT_FLAG_KEY;
+
+            CVPixelBufferLockBaseAddress(image_buffer, 0);
+
+            void* data = CVPixelBufferGetBaseAddress(image_buffer);
+            memcpy(pkt->data, data, pkt->size);
+
+            CVPixelBufferUnlockBaseAddress(image_buffer, 0);
+            CFRelease(ctx->current_frame);
+            ctx->current_frame = nil;
+        } else {
+            pkt->data = NULL;
+            pthread_cond_wait(&ctx->frame_wait_cond, &ctx->frame_lock);
+        }
+
+        unlock_frames(ctx);
+    } while (!pkt->data);
+
+    return 0;
+}
+
+static int avf_close(AVFormatContext *s)
+{
+    AVFContext* ctx = (AVFContext*)s->priv_data;
+    destroy_context(ctx);
+    return 0;
+}
+
+static const AVOption options[] = {
+    { "frame_rate", "set frame rate", offsetof(AVFContext, frame_rate), AV_OPT_TYPE_FLOAT, { .dbl = 30.0 }, 0.1, 30.0, AV_OPT_TYPE_VIDEO_RATE, NULL },
+    { "list_devices", "list available devices", offsetof(AVFContext, list_devices), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM, "list_devices" },
+    { "true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "list_devices" },
+    { "false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "list_devices" },
+    { "video_device_index", "select video device by index for devices with same name (starts at 0)", offsetof(AVFContext, video_device_index), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
+    { NULL },
+};
+
+static const AVClass avf_class = {
+    .class_name = "AVFoundation input device",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+AVInputFormat ff_avfoundation_demuxer = {
+    .name           = "avfoundation",
+    .long_name      = NULL_IF_CONFIG_SMALL("AVFoundation input device"),
+    .priv_data_size = sizeof(AVFContext),
+    .read_header    = avf_read_header,
+    .read_packet    = avf_read_packet,
+    .read_close     = avf_close,
+    .flags          = AVFMT_NOFILE,
+    .priv_class     = &avf_class,
+};