2 * IIDC1394 grab interface (uses libdc1394 and libraw1394)
3 * Copyright (c) 2004 Roman Shaposhnik
4 * Copyright (c) 2008 Alessandro Sappia
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <dc1394/dc1394.h>
27 #elif HAVE_LIBDC1394_1
28 #include <libraw1394/raw1394.h>
29 #include <libdc1394/dc1394_control.h>
31 #define DC1394_VIDEO_MODE_320x240_YUV422 MODE_320x240_YUV422
32 #define DC1394_VIDEO_MODE_640x480_YUV411 MODE_640x480_YUV411
33 #define DC1394_VIDEO_MODE_640x480_YUV422 MODE_640x480_YUV422
34 #define DC1394_FRAMERATE_1_875 FRAMERATE_1_875
35 #define DC1394_FRAMERATE_3_75 FRAMERATE_3_75
36 #define DC1394_FRAMERATE_7_5 FRAMERATE_7_5
37 #define DC1394_FRAMERATE_15 FRAMERATE_15
38 #define DC1394_FRAMERATE_30 FRAMERATE_30
39 #define DC1394_FRAMERATE_60 FRAMERATE_60
40 #define DC1394_FRAMERATE_120 FRAMERATE_120
41 #define DC1394_FRAMERATE_240 FRAMERATE_240
44 #include "libavutil/imgutils.h"
45 #include "libavutil/internal.h"
46 #include "libavutil/log.h"
47 #include "libavutil/mathematics.h"
48 #include "libavutil/opt.h"
49 #include "libavutil/parseutils.h"
50 #include "libavutil/pixdesc.h"
52 #include "libavformat/avformat.h"
53 #include "libavformat/internal.h"
55 typedef struct dc1394_data {
58 raw1394handle_t handle;
59 dc1394_cameracapture camera;
61 #elif HAVE_LIBDC1394_2
63 dc1394camera_t *camera;
64 dc1394video_frame_t *frame;
67 int frame_rate; /**< frames per 1000 seconds (fps * 1000) */
68 char *video_size; /**< String describing video size, set by a private option. */
69 char *pixel_format; /**< Set by a private option. */
70 char *framerate; /**< Set by a private option. */
75 static const struct dc1394_frame_format {
78 enum AVPixelFormat pix_fmt;
80 } dc1394_frame_formats[] = {
81 { 320, 240, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_320x240_YUV422 },
82 { 640, 480, AV_PIX_FMT_GRAY8, DC1394_VIDEO_MODE_640x480_MONO8 },
83 { 640, 480, AV_PIX_FMT_UYYVYY411, DC1394_VIDEO_MODE_640x480_YUV411 },
84 { 640, 480, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_640x480_YUV422 },
85 { 0, 0, 0, 0 } /* gotta be the last one */
88 static const struct dc1394_frame_rate {
91 } dc1394_frame_rates[] = {
92 { 1875, DC1394_FRAMERATE_1_875 },
93 { 3750, DC1394_FRAMERATE_3_75 },
94 { 7500, DC1394_FRAMERATE_7_5 },
95 { 15000, DC1394_FRAMERATE_15 },
96 { 30000, DC1394_FRAMERATE_30 },
97 { 60000, DC1394_FRAMERATE_60 },
98 {120000, DC1394_FRAMERATE_120 },
99 {240000, DC1394_FRAMERATE_240 },
100 { 0, 0 } /* gotta be the last one */
103 #define OFFSET(x) offsetof(dc1394_data, x)
104 #define DEC AV_OPT_FLAG_DECODING_PARAM
105 static const AVOption options[] = {
107 { "channel", "", offsetof(dc1394_data, channel), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
109 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "qvga"}, 0, 0, DEC },
110 { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "uyvy422"}, 0, 0, DEC },
111 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
115 static const AVClass libdc1394_class = {
116 .class_name = "libdc1394 indev",
117 .item_name = av_default_item_name,
119 .version = LIBAVUTIL_VERSION_INT,
120 .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
124 static inline int dc1394_read_common(AVFormatContext *c,
125 const struct dc1394_frame_format **select_fmt, const struct dc1394_frame_rate **select_fps)
127 dc1394_data* dc1394 = c->priv_data;
129 const struct dc1394_frame_format *fmt;
130 const struct dc1394_frame_rate *fps;
131 enum AVPixelFormat pix_fmt;
133 AVRational framerate;
136 if ((pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == AV_PIX_FMT_NONE) {
137 av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format);
138 ret = AVERROR(EINVAL);
142 if ((ret = av_parse_video_size(&width, &height, dc1394->video_size)) < 0) {
143 av_log(c, AV_LOG_ERROR, "Could not parse video size '%s'.\n", dc1394->video_size);
146 if ((ret = av_parse_video_rate(&framerate, dc1394->framerate)) < 0) {
147 av_log(c, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", dc1394->framerate);
150 dc1394->frame_rate = av_rescale(1000, framerate.num, framerate.den);
152 for (fmt = dc1394_frame_formats; fmt->width; fmt++)
153 if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
156 for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
157 if (fps->frame_rate == dc1394->frame_rate)
160 if (!fps->frame_rate || !fmt->width) {
161 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),
162 width, height, dc1394->frame_rate);
163 ret = AVERROR(EINVAL);
167 /* create a video stream */
168 vst = avformat_new_stream(c, NULL);
170 ret = AVERROR(ENOMEM);
173 avpriv_set_pts_info(vst, 64, 1, 1000);
174 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
175 vst->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
176 vst->codecpar->width = fmt->width;
177 vst->codecpar->height = fmt->height;
178 vst->codecpar->format = fmt->pix_fmt;
179 vst->avg_frame_rate = framerate;
182 av_init_packet(&dc1394->packet);
183 dc1394->packet.size = av_image_get_buffer_size(fmt->pix_fmt,
184 fmt->width, fmt->height, 1);
185 dc1394->packet.stream_index = vst->index;
186 dc1394->packet.flags |= AV_PKT_FLAG_KEY;
188 dc1394->current_frame = 0;
190 vst->codecpar->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000);
198 static int dc1394_v1_read_header(AVFormatContext *c)
200 dc1394_data* dc1394 = c->priv_data;
202 nodeid_t* camera_nodes;
204 struct dc1394_frame_format *fmt = NULL;
205 struct dc1394_frame_rate *fps = NULL;
207 if (dc1394_read_common(c, &fmt, &fps) != 0)
210 /* Now let us prep the hardware. */
211 dc1394->handle = dc1394_create_handle(0); /* FIXME: gotta have ap->port */
212 if (!dc1394->handle) {
213 av_log(c, AV_LOG_ERROR, "Can't acquire dc1394 handle on port %d\n", 0 /* ap->port */);
216 camera_nodes = dc1394_get_camera_nodes(dc1394->handle, &res, 1);
217 if (!camera_nodes || camera_nodes[dc1394->channel] == DC1394_NO_CAMERA) {
218 av_log(c, AV_LOG_ERROR, "There's no IIDC camera on the channel %d\n", dc1394->channel);
221 res = dc1394_dma_setup_capture(dc1394->handle, camera_nodes[dc1394->channel],
223 FORMAT_VGA_NONCOMPRESSED,
226 fps->frame_rate_id, 8, 1,
229 dc1394_free_camera_nodes(camera_nodes);
230 if (res != DC1394_SUCCESS) {
231 av_log(c, AV_LOG_ERROR, "Can't prepare camera for the DMA capture\n");
235 res = dc1394_start_iso_transmission(dc1394->handle, dc1394->camera.node);
236 if (res != DC1394_SUCCESS) {
237 av_log(c, AV_LOG_ERROR, "Can't start isochronous transmission\n");
244 dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
245 dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
247 dc1394_destroy_handle(dc1394->handle);
252 static int dc1394_v1_read_packet(AVFormatContext *c, AVPacket *pkt)
254 struct dc1394_data *dc1394 = c->priv_data;
257 /* discard stale frame */
258 if (dc1394->current_frame++) {
259 if (dc1394_dma_done_with_buffer(&dc1394->camera) != DC1394_SUCCESS)
260 av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
263 res = dc1394_dma_single_capture(&dc1394->camera);
265 if (res == DC1394_SUCCESS) {
266 dc1394->packet.data = (uint8_t *)(dc1394->camera.capture_buffer);
267 dc1394->packet.pts = (dc1394->current_frame * 1000000) / dc1394->frame_rate;
268 res = dc1394->packet.size;
270 av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
271 dc1394->packet.data = NULL;
275 *pkt = dc1394->packet;
279 static int dc1394_v1_close(AVFormatContext * context)
281 struct dc1394_data *dc1394 = context->priv_data;
283 dc1394_stop_iso_transmission(dc1394->handle, dc1394->camera.node);
284 dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
285 dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
286 dc1394_destroy_handle(dc1394->handle);
291 #elif HAVE_LIBDC1394_2
292 static int dc1394_v2_read_header(AVFormatContext *c)
294 dc1394_data* dc1394 = c->priv_data;
295 dc1394camera_list_t *list;
297 const struct dc1394_frame_format *fmt = NULL;
298 const struct dc1394_frame_rate *fps = NULL;
300 if (dc1394_read_common(c, &fmt, &fps) != 0)
303 /* Now let us prep the hardware. */
304 dc1394->d = dc1394_new();
305 dc1394_camera_enumerate (dc1394->d, &list);
306 if ( !list || list->num == 0) {
307 av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n");
311 /* FIXME: To select a specific camera I need to search in list its guid */
312 dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
314 av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
317 /* Freeing list of cameras */
318 dc1394_camera_free_list (list);
320 /* Select MAX Speed possible from the cam */
321 if (dc1394->camera->bmode_capable>0) {
322 dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
323 i = DC1394_ISO_SPEED_800;
325 i = DC1394_ISO_SPEED_400;
328 for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
329 res=dc1394_video_set_iso_speed(dc1394->camera, i);
331 if (res != DC1394_SUCCESS) {
332 av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
336 if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
337 av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
341 if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
342 av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
345 if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
346 av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
350 if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
351 av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
357 dc1394_capture_stop(dc1394->camera);
358 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
359 dc1394_camera_free (dc1394->camera);
361 dc1394_free(dc1394->d);
365 static int dc1394_v2_read_packet(AVFormatContext *c, AVPacket *pkt)
367 struct dc1394_data *dc1394 = c->priv_data;
370 /* discard stale frame */
371 if (dc1394->current_frame++) {
372 if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
373 av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
376 res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
377 if (res == DC1394_SUCCESS) {
378 dc1394->packet.data = (uint8_t *) dc1394->frame->image;
379 dc1394->packet.pts = dc1394->current_frame * 1000000 / dc1394->frame_rate;
380 res = dc1394->frame->image_bytes;
382 av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
383 dc1394->packet.data = NULL;
387 *pkt = dc1394->packet;
391 static int dc1394_v2_close(AVFormatContext * context)
393 struct dc1394_data *dc1394 = context->priv_data;
395 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
396 dc1394_capture_stop(dc1394->camera);
397 dc1394_camera_free(dc1394->camera);
398 dc1394_free(dc1394->d);
403 AVInputFormat ff_libdc1394_demuxer = {
405 .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
406 .priv_data_size = sizeof(struct dc1394_data),
407 .read_header = dc1394_v2_read_header,
408 .read_packet = dc1394_v2_read_packet,
409 .read_close = dc1394_v2_close,
410 .flags = AVFMT_NOFILE,
411 .priv_class = &libdc1394_class,
416 AVInputFormat ff_libdc1394_demuxer = {
418 .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.1 A/V grab"),
419 .priv_data_size = sizeof(struct dc1394_data),
420 .read_header = dc1394_v1_read_header,
421 .read_packet = dc1394_v1_read_packet,
422 .read_close = dc1394_v1_close,
423 .flags = AVFMT_NOFILE,
424 .priv_class = &libdc1394_class,