3 * Copyright (c) 2013 Vadim Kalinsky <vadim@kalinsky.ru>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @author Vadim Kalinsky <vadim@kalinsky.ru>
28 #if defined(__clang__)
29 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
32 #import <QTKit/QTKit.h>
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/opt.h"
37 #include "libavformat/internal.h"
38 #include "libavutil/internal.h"
39 #include "libavutil/time.h"
42 #define QTKIT_TIMEBASE 100
44 static const AVRational kQTKitTimeBase_q = {
56 pthread_mutex_t frame_lock;
57 pthread_cond_t frame_wait_cond;
61 int video_device_index;
63 QTCaptureSession* capture_session;
64 QTCaptureDecompressedVideoOutput* video_output;
65 CVImageBufferRef current_frame;
68 static void lock_frames(CaptureContext* ctx)
70 pthread_mutex_lock(&ctx->frame_lock);
73 static void unlock_frames(CaptureContext* ctx)
75 pthread_mutex_unlock(&ctx->frame_lock);
78 /** FrameReciever class - delegate for QTCaptureSession
80 @interface FFMPEG_FrameReceiver : NSObject
82 CaptureContext* _context;
85 - (id)initWithContext:(CaptureContext*)context;
87 - (void)captureOutput:(QTCaptureOutput *)captureOutput
88 didOutputVideoFrame:(CVImageBufferRef)videoFrame
89 withSampleBuffer:(QTSampleBuffer *)sampleBuffer
90 fromConnection:(QTCaptureConnection *)connection;
94 @implementation FFMPEG_FrameReceiver
96 - (id)initWithContext:(CaptureContext*)context
98 if (self = [super init]) {
104 - (void)captureOutput:(QTCaptureOutput *)captureOutput
105 didOutputVideoFrame:(CVImageBufferRef)videoFrame
106 withSampleBuffer:(QTSampleBuffer *)sampleBuffer
107 fromConnection:(QTCaptureConnection *)connection
109 lock_frames(_context);
110 if (_context->current_frame != nil) {
111 CVBufferRelease(_context->current_frame);
114 _context->current_frame = CVBufferRetain(videoFrame);
116 pthread_cond_signal(&_context->frame_wait_cond);
118 unlock_frames(_context);
120 ++_context->frames_captured;
125 static void destroy_context(CaptureContext* ctx)
127 [ctx->capture_session stopRunning];
129 [ctx->capture_session release];
130 [ctx->video_output release];
131 [ctx->qt_delegate release];
133 ctx->capture_session = NULL;
134 ctx->video_output = NULL;
135 ctx->qt_delegate = NULL;
137 pthread_mutex_destroy(&ctx->frame_lock);
138 pthread_cond_destroy(&ctx->frame_wait_cond);
140 if (ctx->current_frame)
141 CVBufferRelease(ctx->current_frame);
144 static int qtkit_read_header(AVFormatContext *s)
146 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
148 CaptureContext* ctx = (CaptureContext*)s->priv_data;
150 ctx->first_pts = av_gettime();
152 pthread_mutex_init(&ctx->frame_lock, NULL);
153 pthread_cond_init(&ctx->frame_wait_cond, NULL);
155 // List devices if requested
156 if (ctx->list_devices) {
157 av_log(ctx, AV_LOG_INFO, "QTKit video devices:\n");
158 NSArray *devices = [QTCaptureDevice inputDevicesWithMediaType:QTMediaTypeVideo];
159 for (QTCaptureDevice *device in devices) {
160 const char *name = [[device localizedDisplayName] UTF8String];
161 int index = [devices indexOfObject:device];
162 av_log(ctx, AV_LOG_INFO, "[%d] %s\n", index, name);
167 // Find capture device
168 QTCaptureDevice *video_device = nil;
170 // check for device index given in filename
171 if (ctx->video_device_index == -1) {
172 sscanf(s->filename, "%d", &ctx->video_device_index);
175 if (ctx->video_device_index >= 0) {
176 NSArray *devices = [QTCaptureDevice inputDevicesWithMediaType:QTMediaTypeVideo];
178 if (ctx->video_device_index >= [devices count]) {
179 av_log(ctx, AV_LOG_ERROR, "Invalid device index\n");
183 video_device = [devices objectAtIndex:ctx->video_device_index];
184 } else if (strncmp(s->filename, "", 1) &&
185 strncmp(s->filename, "default", 7)) {
186 NSArray *devices = [QTCaptureDevice inputDevicesWithMediaType:QTMediaTypeVideo];
188 for (QTCaptureDevice *device in devices) {
189 if (!strncmp(s->filename, [[device localizedDisplayName] UTF8String], strlen(s->filename))) {
190 video_device = device;
195 av_log(ctx, AV_LOG_ERROR, "Video device not found\n");
199 video_device = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeMuxed];
202 BOOL success = [video_device open:nil];
204 // Video capture device not found, looking for QTMediaTypeVideo
206 video_device = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeVideo];
207 success = [video_device open:nil];
210 av_log(s, AV_LOG_ERROR, "No QT capture device found\n");
215 NSString* dev_display_name = [video_device localizedDisplayName];
216 av_log (s, AV_LOG_DEBUG, "'%s' opened\n", [dev_display_name UTF8String]);
218 // Initialize capture session
219 ctx->capture_session = [[QTCaptureSession alloc] init];
221 QTCaptureDeviceInput* capture_dev_input = [[[QTCaptureDeviceInput alloc] initWithDevice:video_device] autorelease];
222 success = [ctx->capture_session addInput:capture_dev_input error:nil];
225 av_log (s, AV_LOG_ERROR, "Failed to add QT capture device to session\n");
230 // FIXME: Allow for a user defined pixel format
231 ctx->video_output = [[QTCaptureDecompressedVideoOutput alloc] init];
233 NSDictionary *captureDictionary = [NSDictionary dictionaryWithObject:
234 [NSNumber numberWithUnsignedInt:kCVPixelFormatType_24RGB]
235 forKey:(id)kCVPixelBufferPixelFormatTypeKey];
237 [ctx->video_output setPixelBufferAttributes:captureDictionary];
239 ctx->qt_delegate = [[FFMPEG_FrameReceiver alloc] initWithContext:ctx];
241 [ctx->video_output setDelegate:ctx->qt_delegate];
242 [ctx->video_output setAutomaticallyDropsLateVideoFrames:YES];
243 [ctx->video_output setMinimumVideoFrameInterval:1.0/ctx->frame_rate];
245 success = [ctx->capture_session addOutput:ctx->video_output error:nil];
248 av_log (s, AV_LOG_ERROR, "can't add video output to capture session\n");
252 [ctx->capture_session startRunning];
254 // Take stream info from the first frame.
255 while (ctx->frames_captured < 1) {
256 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
261 AVStream* stream = avformat_new_stream(s, NULL);
267 avpriv_set_pts_info(stream, 64, 1, QTKIT_TIMEBASE);
269 stream->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
270 stream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
271 stream->codec->width = (int)CVPixelBufferGetWidth (ctx->current_frame);
272 stream->codec->height = (int)CVPixelBufferGetHeight(ctx->current_frame);
273 stream->codec->pix_fmt = AV_PIX_FMT_RGB24;
275 CVBufferRelease(ctx->current_frame);
276 ctx->current_frame = nil;
287 destroy_context(ctx);
292 static int qtkit_read_packet(AVFormatContext *s, AVPacket *pkt)
294 CaptureContext* ctx = (CaptureContext*)s->priv_data;
299 if (ctx->current_frame != nil) {
300 if (av_new_packet(pkt, (int)CVPixelBufferGetDataSize(ctx->current_frame)) < 0) {
304 pkt->pts = pkt->dts = av_rescale_q(av_gettime() - ctx->first_pts, AV_TIME_BASE_Q, kQTKitTimeBase_q);
305 pkt->stream_index = 0;
306 pkt->flags |= AV_PKT_FLAG_KEY;
308 CVPixelBufferLockBaseAddress(ctx->current_frame, 0);
310 void* data = CVPixelBufferGetBaseAddress(ctx->current_frame);
311 memcpy(pkt->data, data, pkt->size);
313 CVPixelBufferUnlockBaseAddress(ctx->current_frame, 0);
314 CVBufferRelease(ctx->current_frame);
315 ctx->current_frame = nil;
318 pthread_cond_wait(&ctx->frame_wait_cond, &ctx->frame_lock);
322 } while (!pkt->data);
327 static int qtkit_close(AVFormatContext *s)
329 CaptureContext* ctx = (CaptureContext*)s->priv_data;
331 destroy_context(ctx);
336 static const AVOption options[] = {
337 { "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 },
338 { "list_devices", "list available devices", offsetof(CaptureContext, list_devices), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM, "list_devices" },
339 { "true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "list_devices" },
340 { "false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "list_devices" },
341 { "video_device_index", "select video device by index for devices with same name (starts at 0)", offsetof(CaptureContext, video_device_index), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
345 static const AVClass qtkit_class = {
346 .class_name = "QTKit input device",
347 .item_name = av_default_item_name,
349 .version = LIBAVUTIL_VERSION_INT,
350 .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
353 AVInputFormat ff_qtkit_demuxer = {
355 .long_name = NULL_IF_CONFIG_SMALL("QTKit input device"),
356 .priv_data_size = sizeof(CaptureContext),
357 .read_header = qtkit_read_header,
358 .read_packet = qtkit_read_packet,
359 .read_close = qtkit_close,
360 .flags = AVFMT_NOFILE,
361 .priv_class = &qtkit_class,