[FFmpeg-devel] iSight camera support

Thilo Borgmann thilo.borgmann at mail.de
Mon Mar 24 10:55:35 CET 2014


Am 24.03.14 02:40, schrieb Michael Niedermayer:
> On Sun, Mar 16, 2014 at 01:08:28PM +0100, Thilo Borgmann wrote:
> Am 16.03.14 00:19, schrieb Peter Ross:
>>>> On Sat, Mar 15, 2014 at 07:34:47PM +0100, Thilo Borgmann wrote:
>>>>> Hi,
>>>>>
>>>>> Am 31.10.13 09:02, schrieb Stefano Sabatini:
>>>>>> On date Tuesday 2013-10-29 22:53:11 -0400, Vadim Kalinsky encoded:
>>>>>>>
>>>>>
>>>>> tested and improved with Stefano's latest remarks, except...
>>>>>
>>>>
>>>> isight is the name given to the camera bundled with most mac computers. can
>>>> the qtkit framework access other capture devices? if this is the case, i
>>>> suggest using qtkit for AVInputFormat.name and --enable-qtkit in
>>>> configure.
> 
> Indeed it can. Renamed accordingly.
> 
>>>> here is some more review inline.
> 
> Thanks, updated patch attached.
> 
> -Thilo
> 
> 
>>  MAINTAINERS              |    1 
>>  common.mak               |    3 
>>  configure                |    4 
>>  doc/indevs.texi          |   10 +
>>  libavdevice/Makefile     |    1 
>>  libavdevice/alldevices.c |    1 
>>  libavdevice/qtkit.m      |  306 +++++++++++++++++++++++++++++++++++++++++++++++
>>  7 files changed, 326 insertions(+)
>> 1130ac778b975c6ffae28acfc01265ceac8c6f10  0001-lavd-Add-QTKit-input-device.patch
>> From fb73d0f372551f8433c8b0390a6c914e2a520196 Mon Sep 17 00:00:00 2001
>> From: Vadim Kalinsky <vadim at kalinsky.ru>
>> Date: Sun, 16 Mar 2014 13:04:06 +0100
>> Subject: [PATCH] lavd: Add QTKit input device.
>>
>> Signed-off-by: Thilo Borgmann <thilo.borgmann at mail.de>
>> ---
>>  MAINTAINERS              |   1 +
>>  common.mak               |   3 +
>>  configure                |   4 +
>>  doc/indevs.texi          |  10 ++
>>  libavdevice/Makefile     |   1 +
>>  libavdevice/alldevices.c |   1 +
>>  libavdevice/qtkit.m      | 306 +++++++++++++++++++++++++++++++++++++++++++++++
>>  7 files changed, 326 insertions(+)
>>  create mode 100644 libavdevice/qtkit.m
> 
> missing changelog entry otherwise
> 
> should be ok

Updated patch attached. Thanks.

-Thilo
-------------- next part --------------
>From 4209e5b39f7db27f212d6df30951d0d1c21ebd3d Mon Sep 17 00:00:00 2001
From: Vadim Kalinsky <vadim at kalinsky.ru>
Date: Mon, 24 Mar 2014 10:54:14 +0100
Subject: [PATCH] lavd: Add QTKit input device.

Signed-off-by: Thilo Borgmann <thilo.borgmann at mail.de>
---
 Changelog                |   1 +
 MAINTAINERS              |   1 +
 common.mak               |   3 +
 configure                |   4 +
 doc/indevs.texi          |  10 ++
 libavdevice/Makefile     |   1 +
 libavdevice/alldevices.c |   1 +
 libavdevice/qtkit.m      | 306 +++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 327 insertions(+)
 create mode 100644 libavdevice/qtkit.m

diff --git a/Changelog b/Changelog
index 7ccd638..a2a0fb3 100644
--- a/Changelog
+++ b/Changelog
@@ -5,6 +5,7 @@ version <next>:
 - shuffleplanes filter
 - subfile protocol
 - Phantom Cine demuxer
+- QTKit input device
 
 
 version 2.2:
diff --git a/MAINTAINERS b/MAINTAINERS
index cd4a242..1284a1f 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -310,6 +310,7 @@ libavdevice
   libdc1394.c                           Roman Shaposhnik
   opengl_enc.c                          Lukasz Marek
   pulse_audio_enc.c                     Lukasz Marek
+  qtkit.m                               Thilo Borgmann
   sdl                                   Stefano Sabatini
   v4l2.c                                Luca Abeni
   vfwcap.c                              Ramiro Polla
diff --git a/common.mak b/common.mak
index b557dff..cdad4f3 100644
--- a/common.mak
+++ b/common.mak
@@ -51,6 +51,9 @@ COMPILE_HOSTC = $(call COMPILE,HOSTCC)
 %.o: %.cpp
 	$(COMPILE_CXX)
 
+%.o: %.m
+	$(COMPILE_C)
+
 %.s: %.c
 	$(CC) $(CPPFLAGS) $(CFLAGS) -S -o $@ $<
 
diff --git a/configure b/configure
index 2857f39..c31f5a0 100755
--- a/configure
+++ b/configure
@@ -255,6 +255,7 @@ External library support:
   --enable-opencl          enable OpenCL code
   --enable-opengl          enable OpenGL rendering [no]
   --enable-openssl         enable openssl [no]
+  --disable-qtkit          disable QTKit input device support [autodetect]
   --enable-x11grab         enable X11 grabbing [no]
   --disable-xlib           disable xlib [autodetect]
   --disable-zlib           disable zlib [autodetect]
@@ -2358,6 +2359,8 @@ oss_indev_deps_any="soundcard_h sys_soundcard_h"
 oss_outdev_deps_any="soundcard_h sys_soundcard_h"
 pulse_indev_deps="libpulse"
 pulse_outdev_deps="libpulse"
+qtkit_indev_extralibs="-framework QTKit -framework Foundation -framework QuartzCore"
+qtkit_indev_select="qtkit"
 sdl_outdev_deps="sdl"
 sndio_indev_deps="sndio_h"
 sndio_outdev_deps="sndio_h"
@@ -4641,6 +4644,7 @@ enabled openssl           && { check_lib openssl/ssl.h SSL_library_init -lssl -l
                                check_lib openssl/ssl.h SSL_library_init -lssl32 -leay32 ||
                                check_lib openssl/ssl.h SSL_library_init -lssl -lcrypto -lws2_32 -lgdi32 ||
                                die "ERROR: openssl not found"; }
+enabled qtkit_indev      && { check_header QTKit/QTKit.h || disable qtkit_indev; }
 
 if enabled gnutls; then
     { check_lib nettle/bignum.h nettle_mpz_get_str_256 -lnettle -lhogweed -lgmp && enable nettle; } ||
diff --git a/doc/indevs.texi b/doc/indevs.texi
index 93fbbe8..e1ff231 100644
--- a/doc/indevs.texi
+++ b/doc/indevs.texi
@@ -531,6 +531,16 @@ Record a stream from default device:
 ffmpeg -f pulse -i default /tmp/pulse.wav
 @end example
 
+ at section qtkit
+
+QTKit input device.
+
+The filename passed as input is unused. The default device will be chosen.
+
+ at example
+ffmpeg -f isight -i "" out.mpg
+ at end example
+
 @section sndio
 
 sndio input device.
diff --git a/libavdevice/Makefile b/libavdevice/Makefile
index 7d07216..7e5f9bf 100644
--- a/libavdevice/Makefile
+++ b/libavdevice/Makefile
@@ -37,6 +37,7 @@ OBJS-$(CONFIG_PULSE_INDEV)               += pulse_audio_dec.o \
                                             pulse_audio_common.o
 OBJS-$(CONFIG_PULSE_OUTDEV)              += pulse_audio_enc.o \
                                             pulse_audio_common.o
+OBJS-$(CONFIG_QTKIT_INDEV)               += qtkit.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
diff --git a/libavdevice/alldevices.c b/libavdevice/alldevices.c
index 005bbc8..282c50e 100644
--- a/libavdevice/alldevices.c
+++ b/libavdevice/alldevices.c
@@ -60,6 +60,7 @@ void avdevice_register_all(void)
     REGISTER_OUTDEV  (OPENGL,           opengl);
     REGISTER_INOUTDEV(OSS,              oss);
     REGISTER_INOUTDEV(PULSE,            pulse);
+    REGISTER_INDEV   (QTKIT,            qtkit);
     REGISTER_OUTDEV  (SDL,              sdl);
     REGISTER_INOUTDEV(SNDIO,            sndio);
     REGISTER_INOUTDEV(V4L2,             v4l2);
diff --git a/libavdevice/qtkit.m b/libavdevice/qtkit.m
new file mode 100644
index 0000000..038ce02
--- /dev/null
+++ b/libavdevice/qtkit.m
@@ -0,0 +1,306 @@
+/*
+ * QTKit input device
+ * Copyright (c) 2013 Vadim Kalinsky <vadim at kalinsky.ru>
+ *
+ * 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
+ * QTKit input device
+ * @author Vadim Kalinsky <vadim at kalinsky.ru>
+ */
+
+#import <QTKit/QTkit.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 kQTKitTimeBase = 100;
+
+static const AVRational kQTKitTimeBase_q = {
+    .num = 1,
+    .den = kQTKitTimeBase
+};
+
+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              qt_delegate;
+
+    QTCaptureSession*                 capture_session;
+    QTCaptureDecompressedVideoOutput* video_output;
+    CVImageBufferRef                  current_frame;
+} CaptureContext;
+
+static void lock_frames(CaptureContext* ctx)
+{
+    pthread_mutex_lock(&ctx->frame_lock);
+}
+
+static void unlock_frames(CaptureContext* ctx)
+{
+    pthread_mutex_unlock(&ctx->frame_lock);
+}
+
+/** FrameReciever class - delegate for QTCaptureSession
+ */
+ at interface FFMPEG_FrameReceiver : NSObject
+{
+    CaptureContext* _context;
+}
+
+- (id)initWithContext:(CaptureContext*)context;
+
+- (void)captureOutput:(QTCaptureOutput *)captureOutput
+  didOutputVideoFrame:(CVImageBufferRef)videoFrame
+     withSampleBuffer:(QTSampleBuffer *)sampleBuffer
+       fromConnection:(QTCaptureConnection *)connection;
+
+ at end
+
+ at implementation FFMPEG_FrameReceiver
+
+- (id)initWithContext:(CaptureContext*)context
+{
+    if (self = [super init]) {
+        _context = context;
+    }
+    return self;
+}
+
+- (void)captureOutput:(QTCaptureOutput *)captureOutput
+  didOutputVideoFrame:(CVImageBufferRef)videoFrame
+     withSampleBuffer:(QTSampleBuffer *)sampleBuffer
+       fromConnection:(QTCaptureConnection *)connection
+{
+    lock_frames(_context);
+    if (_context->current_frame != nil) {
+        CVBufferRelease(_context->current_frame);
+    }
+
+    _context->current_frame = CVBufferRetain(videoFrame);
+
+    pthread_cond_signal(&_context->frame_wait_cond);
+
+    unlock_frames(_context);
+
+    ++_context->frames_captured;
+}
+
+ at end
+
+static void destroy_context(CaptureContext* ctx)
+{
+    [ctx->capture_session stopRunning];
+
+    [ctx->capture_session release];
+    [ctx->video_output    release];
+    [ctx->qt_delegate     release];
+
+    ctx->capture_session = NULL;
+    ctx->video_output    = NULL;
+    ctx->qt_delegate     = NULL;
+
+    pthread_mutex_destroy(&ctx->frame_lock);
+    pthread_cond_destroy(&ctx->frame_wait_cond);
+
+    if (ctx->current_frame)
+        CVBufferRelease(ctx->current_frame);
+}
+
+static int qtkit_read_header(AVFormatContext *s)
+{
+    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
+
+    CaptureContext* ctx = (CaptureContext*)s->priv_data;
+
+    ctx->first_pts = av_gettime();
+
+    pthread_mutex_init(&ctx->frame_lock, NULL);
+    pthread_cond_init(&ctx->frame_wait_cond, NULL);
+
+    // Find default capture device
+    QTCaptureDevice *video_device = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeMuxed];
+
+    BOOL success = [video_device open:nil];
+
+    // Video capture device not found, looking for QTMediaTypeVideo
+    if (!success) {
+        video_device = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeVideo];
+        success      = [video_device open:nil];
+
+        if (!success) {
+            av_log(s, AV_LOG_ERROR, "No QT capture device found\n");
+            goto fail;
+        }
+    }
+
+    NSString* dev_display_name = [video_device localizedDisplayName];
+    av_log (s, AV_LOG_DEBUG, "'%s' opened\n", [dev_display_name UTF8String]);
+
+    // Initialize capture session
+    ctx->capture_session = [[QTCaptureSession alloc] init];
+
+    QTCaptureDeviceInput* capture_dev_input = [[[QTCaptureDeviceInput alloc] initWithDevice:video_device] autorelease];
+    success = [ctx->capture_session addInput:capture_dev_input error:nil];
+
+    if (!success) {
+        av_log (s, AV_LOG_ERROR, "Failed to add QT capture device to session\n");
+        goto fail;
+    }
+
+    // Attaching output
+    // FIXME: Allow for a user defined pixel format
+    ctx->video_output = [[QTCaptureDecompressedVideoOutput alloc] init];
+
+    NSDictionary *captureDictionary = [NSDictionary dictionaryWithObject:
+                                       [NSNumber numberWithUnsignedInt:kCVPixelFormatType_24RGB]
+                                       forKey:(id)kCVPixelBufferPixelFormatTypeKey];
+
+    [ctx->video_output setPixelBufferAttributes:captureDictionary];
+
+    ctx->qt_delegate = [[FFMPEG_FrameReceiver alloc] initWithContext:ctx];
+
+    [ctx->video_output setDelegate:ctx->qt_delegate];
+    [ctx->video_output setAutomaticallyDropsLateVideoFrames:YES];
+    [ctx->video_output setMinimumVideoFrameInterval:1.0/ctx->frame_rate];
+
+    success = [ctx->capture_session addOutput:ctx->video_output error:nil];
+
+    if (!success) {
+        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, kQTKitTimeBase);
+
+    stream->codec->codec_id   = AV_CODEC_ID_RAWVIDEO;
+    stream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
+    stream->codec->width      = (int)CVPixelBufferGetWidth (ctx->current_frame);
+    stream->codec->height     = (int)CVPixelBufferGetHeight(ctx->current_frame);
+    stream->codec->pix_fmt    = AV_PIX_FMT_RGB24;
+
+    CVBufferRelease(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 qtkit_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    CaptureContext* ctx = (CaptureContext*)s->priv_data;
+
+    do {
+        lock_frames(ctx);
+
+        if (ctx->current_frame != nil) {
+            if (av_new_packet(pkt, (int)CVPixelBufferGetDataSize(ctx->current_frame)) < 0) {
+                return AVERROR(EIO);
+            }
+
+            pkt->pts = pkt->dts = av_rescale_q(av_gettime() - ctx->first_pts, AV_TIME_BASE_Q, kQTKitTimeBase_q);
+            pkt->stream_index = 0;
+            pkt->flags |= AV_PKT_FLAG_KEY;
+
+            CVPixelBufferLockBaseAddress(ctx->current_frame, 0);
+
+            void* data = CVPixelBufferGetBaseAddress(ctx->current_frame);
+            memcpy(pkt->data, data, pkt->size);
+
+            CVPixelBufferUnlockBaseAddress(ctx->current_frame, 0);
+            CVBufferRelease(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 qtkit_close(AVFormatContext *s)
+{
+    CaptureContext* ctx = (CaptureContext*)s->priv_data;
+
+    destroy_context(ctx);
+
+    return 0;
+}
+
+static const AVOption options[] = {
+    { "frame_rate", "set frame rate", offsetof(CaptureContext, frame_rate), AV_OPT_TYPE_FLOAT, { .dbl = 30.0 }, 0.1, 30.0, AV_OPT_TYPE_VIDEO_RATE, NULL },
+    { NULL },
+};
+
+static const AVClass qtkit_class = {
+    .class_name = "QTKit input device",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+AVInputFormat ff_qtkit_demuxer = {
+    .name           = "qtkit",
+    .long_name      = NULL_IF_CONFIG_SMALL("QTKit input device"),
+    .priv_data_size = sizeof(CaptureContext),
+    .read_header    = qtkit_read_header,
+    .read_packet    = qtkit_read_packet,
+    .read_close     = qtkit_close,
+    .flags          = AVFMT_NOFILE,
+    .priv_class     = &qtkit_class,
+};
-- 
1.8.3.2



More information about the ffmpeg-devel mailing list