2 * IIDC1394 grab interface (uses libdc1394 and libraw1394)
3 * Copyright (c) 2004 Roman Shaposhnik
4 * Copyright (c) 2008 Alessandro Sappia
5 * Copyright (c) 2011 Martin Lambers
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/log.h"
26 #include "libavutil/opt.h"
31 #include "libavutil/parseutils.h"
32 #include "libavutil/pixdesc.h"
34 #include <dc1394/dc1394.h>
38 typedef struct dc1394_data {
41 dc1394camera_t *camera;
42 dc1394video_frame_t *frame;
44 int frame_rate; /**< frames per 1000 seconds (fps * 1000) */
45 char *video_size; /**< String describing video size, set by a private option. */
46 char *pixel_format; /**< Set by a private option. */
47 char *framerate; /**< Set by a private option. */
52 /* The list of color codings that we support.
53 * We assume big endian for the dc1394 16bit modes: libdc1394 never sets the
54 * flag little_endian in dc1394video_frame_t. */
55 struct dc1394_color_coding {
59 } dc1394_color_codings[] = {
60 { PIX_FMT_GRAY16BE, 1000, DC1394_COLOR_CODING_MONO16 },
61 { PIX_FMT_RGB48BE, 1100, DC1394_COLOR_CODING_RGB16 },
62 { PIX_FMT_GRAY8, 1200, DC1394_COLOR_CODING_MONO8 },
63 { PIX_FMT_RGB24, 1300, DC1394_COLOR_CODING_RGB8 },
64 { PIX_FMT_UYYVYY411, 1400, DC1394_COLOR_CODING_YUV411 },
65 { PIX_FMT_UYVY422, 1500, DC1394_COLOR_CODING_YUV422 },
66 { PIX_FMT_NONE, 0, 0 } /* gotta be the last one */
69 struct dc1394_frame_rate {
72 } dc1394_frame_rates[] = {
73 { 1875, DC1394_FRAMERATE_1_875 },
74 { 3750, DC1394_FRAMERATE_3_75 },
75 { 7500, DC1394_FRAMERATE_7_5 },
76 { 15000, DC1394_FRAMERATE_15 },
77 { 30000, DC1394_FRAMERATE_30 },
78 { 60000, DC1394_FRAMERATE_60 },
79 {120000, DC1394_FRAMERATE_120 },
80 {240000, DC1394_FRAMERATE_240 },
81 { 0, 0 } /* gotta be the last one */
84 #define OFFSET(x) offsetof(dc1394_data, x)
85 #define DEC AV_OPT_FLAG_DECODING_PARAM
86 static const AVOption options[] = {
87 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = "qvga"}, 0, 0, DEC },
88 { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = "uyvy422"}, 0, 0, DEC },
89 { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
93 static const AVClass libdc1394_class = {
94 .class_name = "libdc1394 indev",
95 .item_name = av_default_item_name,
97 .version = LIBAVUTIL_VERSION_INT,
100 static int dc1394_read_header(AVFormatContext *c, AVFormatParameters * ap)
102 dc1394_data* dc1394 = c->priv_data;
104 const struct dc1394_color_coding *cc;
105 const struct dc1394_frame_rate *fr;
106 dc1394camera_list_t *list;
107 dc1394video_modes_t video_modes;
108 dc1394video_mode_t video_mode;
109 dc1394framerates_t frame_rates;
110 dc1394framerate_t frame_rate;
111 uint32_t dc1394_width, dc1394_height, dc1394_color_coding;
113 int score, max_score;
114 int final_width, final_height, final_pix_fmt, final_frame_rate;
118 /* Now let us prep the hardware. */
119 dc1394->d = dc1394_new();
120 dc1394_camera_enumerate (dc1394->d, &list);
121 if ( !list || list->num == 0) {
122 av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n");
126 /* FIXME: To select a specific camera I need to search in list its guid */
127 dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
129 av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
132 /* Freeing list of cameras */
133 dc1394_camera_free_list (list);
135 /* Get the list of video modes supported by the camera. */
136 res = dc1394_video_get_supported_modes (dc1394->camera, &video_modes);
137 if (res != DC1394_SUCCESS) {
138 av_log(c, AV_LOG_ERROR, "Could not get video formats.\n");
142 if (dc1394->pixel_format) {
143 if ((ap->pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == PIX_FMT_NONE) {
144 av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format);
145 ret = AVERROR(EINVAL);
150 if (dc1394->video_size) {
151 if ((ret = av_parse_video_size(&ap->width, &ap->height, dc1394->video_size)) < 0) {
152 av_log(c, AV_LOG_ERROR, "Couldn't parse video size.\n");
157 /* Choose the best mode. */
158 rate = (ap->time_base.num ? av_rescale(1000, ap->time_base.den, ap->time_base.num) : -1);
160 for (i = 0; i < video_modes.num; i++) {
161 if (video_modes.modes[i] == DC1394_VIDEO_MODE_EXIF
162 || (video_modes.modes[i] >= DC1394_VIDEO_MODE_FORMAT7_MIN
163 && video_modes.modes[i] <= DC1394_VIDEO_MODE_FORMAT7_MAX)) {
164 /* These modes are currently not supported as they would require
165 * much more work. For the remaining modes, the functions
166 * dc1394_get_image_size_from_video_mode and
167 * dc1394_get_color_coding_from_video_mode do not need to query the
168 * camera, and thus cannot fail. */
171 dc1394_get_color_coding_from_video_mode (NULL, video_modes.modes[i],
172 &dc1394_color_coding);
173 for (cc = dc1394_color_codings; cc->pix_fmt != PIX_FMT_NONE; cc++)
174 if (cc->coding == dc1394_color_coding)
176 if (cc->pix_fmt == PIX_FMT_NONE) {
177 /* We currently cannot handle this color coding. */
180 /* Here we know that the mode is supported. Get its frame size and the list
181 * of frame rates supported by the camera for this mode. This list is sorted
182 * in ascending order according to libdc1394 example programs. */
183 dc1394_get_image_size_from_video_mode (NULL, video_modes.modes[i],
184 &dc1394_width, &dc1394_height);
185 res = dc1394_video_get_supported_framerates (dc1394->camera, video_modes.modes[i],
187 if (res != DC1394_SUCCESS || frame_rates.num == 0) {
188 av_log(c, AV_LOG_ERROR, "Cannot get frame rates for video mode.\n");
191 /* Choose the best frame rate. */
193 for (j = 0; j < frame_rates.num; j++) {
194 for (fr = dc1394_frame_rates; fr->frame_rate; fr++) {
195 if (fr->frame_rate_id == frame_rates.framerates[j]) {
199 if (!fr->frame_rate) {
200 /* This frame rate is not supported. */
203 best_rate = fr->frame_rate;
204 frame_rate = fr->frame_rate_id;
205 if (ap->time_base.num && rate == fr->frame_rate) {
206 /* This is the requested frame rate. */
210 if (best_rate == -1) {
211 /* No supported rate found. */
214 /* Here we know that both the mode and the rate are supported. Compute score. */
215 if (ap->width && ap->height
216 && (dc1394_width == ap->width && dc1394_height == ap->height)) {
219 score = dc1394_width * 10; // 1600 - 16000
221 if (ap->pix_fmt == cc->pix_fmt) {
224 score += cc->score; // 1000 - 1500
226 if (ap->time_base.num && rate == best_rate) {
229 score += best_rate / 1000; // 1 - 240
231 if (score > max_score) {
232 video_mode = video_modes.modes[i];
233 final_width = dc1394_width;
234 final_height = dc1394_height;
235 final_pix_fmt = cc->pix_fmt;
236 final_frame_rate = best_rate;
240 if (max_score == -1) {
241 av_log(c, AV_LOG_ERROR, "No suitable video mode / frame rate available.\n");
244 if (ap->width && ap->height && !(ap->width == final_width && ap->height == final_height)) {
245 av_log(c, AV_LOG_WARNING, "Requested frame size is not available, using fallback.\n");
247 if (ap->pix_fmt != PIX_FMT_NONE && ap->pix_fmt != final_pix_fmt) {
248 av_log(c, AV_LOG_WARNING, "Requested pixel format is not supported, using fallback.\n");
250 if (ap->time_base.num && rate != final_frame_rate) {
251 av_log(c, AV_LOG_WARNING, "Requested frame rate is not available, using fallback.\n");
254 /* create a video stream */
255 vst = av_new_stream(c, 0);
258 av_set_pts_info(vst, 64, 1, 1000);
259 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
260 vst->codec->codec_id = CODEC_ID_RAWVIDEO;
261 vst->codec->time_base.den = final_frame_rate;
262 vst->codec->time_base.num = 1000;
263 vst->codec->width = final_width;
264 vst->codec->height = final_height;
265 vst->codec->pix_fmt = final_pix_fmt;
268 av_init_packet(&dc1394->packet);
269 dc1394->packet.size = avpicture_get_size(final_pix_fmt, final_width, final_height);
270 dc1394->packet.stream_index = vst->index;
271 dc1394->packet.flags |= AV_PKT_FLAG_KEY;
273 dc1394->current_frame = 0;
274 dc1394->frame_rate = final_frame_rate;
276 vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, final_frame_rate, 1000);
278 /* Select MAX Speed possible from the cam */
279 if (dc1394->camera->bmode_capable>0) {
280 dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
281 i = DC1394_ISO_SPEED_800;
283 i = DC1394_ISO_SPEED_400;
286 for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
287 res=dc1394_video_set_iso_speed(dc1394->camera, i);
289 if (res != DC1394_SUCCESS) {
290 av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
294 if (dc1394_video_set_mode(dc1394->camera, video_mode) != DC1394_SUCCESS) {
295 av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
299 if (dc1394_video_set_framerate(dc1394->camera, frame_rate) != DC1394_SUCCESS) {
300 av_log(c, AV_LOG_ERROR, "Could not set framerate %d.\n", final_frame_rate);
303 if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
304 av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
308 if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
309 av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
315 dc1394_capture_stop(dc1394->camera);
316 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
317 dc1394_camera_free (dc1394->camera);
319 av_freep(&dc1394->video_size);
320 av_freep(&dc1394->pixel_format);
321 dc1394_free(dc1394->d);
325 static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt)
327 struct dc1394_data *dc1394 = c->priv_data;
330 /* discard stale frame */
331 if (dc1394->current_frame++) {
332 if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
333 av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
336 res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
337 if (res == DC1394_SUCCESS) {
338 dc1394->packet.data = (uint8_t *)(dc1394->frame->image);
339 dc1394->packet.pts = (dc1394->current_frame * 1000000) / (dc1394->frame_rate);
340 res = dc1394->frame->image_bytes;
342 av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
343 dc1394->packet.data = NULL;
347 *pkt = dc1394->packet;
351 static int dc1394_close(AVFormatContext * context)
353 struct dc1394_data *dc1394 = context->priv_data;
355 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
356 dc1394_capture_stop(dc1394->camera);
357 dc1394_camera_free(dc1394->camera);
358 dc1394_free(dc1394->d);
363 AVInputFormat ff_libdc1394_demuxer = {
365 .long_name = NULL_IF_CONFIG_SMALL("dc1394 A/V grab"),
366 .priv_data_size = sizeof(struct dc1394_data),
367 .read_header = dc1394_read_header,
368 .read_packet = dc1394_read_packet,
369 .read_close = dc1394_close,
370 .flags = AVFMT_NOFILE,
371 .priv_class = &libdc1394_class,