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"
33 #include <dc1394/dc1394.h>
37 typedef struct dc1394_data {
40 dc1394camera_t *camera;
41 dc1394video_frame_t *frame;
44 char *video_size; /**< String describing video size, set by a private option. */
49 /* The list of color codings that we support.
50 * We assume big endian for the dc1394 16bit modes: libdc1394 never sets the
51 * flag little_endian in dc1394video_frame_t. */
52 struct dc1394_color_coding {
56 } dc1394_color_codings[] = {
57 { PIX_FMT_GRAY16BE, 1000, DC1394_COLOR_CODING_MONO16 },
58 { PIX_FMT_RGB48BE, 1100, DC1394_COLOR_CODING_RGB16 },
59 { PIX_FMT_GRAY8, 1200, DC1394_COLOR_CODING_MONO8 },
60 { PIX_FMT_RGB24, 1300, DC1394_COLOR_CODING_RGB8 },
61 { PIX_FMT_UYYVYY411, 1400, DC1394_COLOR_CODING_YUV411 },
62 { PIX_FMT_UYVY422, 1500, DC1394_COLOR_CODING_YUV422 },
63 { PIX_FMT_NONE, 0, 0 } /* gotta be the last one */
66 struct dc1394_frame_rate {
69 } dc1394_frame_rates[] = {
70 { 1875, DC1394_FRAMERATE_1_875 },
71 { 3750, DC1394_FRAMERATE_3_75 },
72 { 7500, DC1394_FRAMERATE_7_5 },
73 { 15000, DC1394_FRAMERATE_15 },
74 { 30000, DC1394_FRAMERATE_30 },
75 { 60000, DC1394_FRAMERATE_60 },
76 {120000, DC1394_FRAMERATE_120 },
77 {240000, DC1394_FRAMERATE_240 },
78 { 0, 0 } /* gotta be the last one */
81 #define OFFSET(x) offsetof(dc1394_data, x)
82 #define DEC AV_OPT_FLAG_DECODING_PARAM
83 static const AVOption options[] = {
84 { "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 static const AVClass libdc1394_class = {
89 .class_name = "libdc1394 indev",
90 .item_name = av_default_item_name,
92 .version = LIBAVUTIL_VERSION_INT,
95 static int dc1394_read_header(AVFormatContext *c, AVFormatParameters * ap)
97 dc1394_data* dc1394 = c->priv_data;
99 const struct dc1394_color_coding *cc;
100 const struct dc1394_frame_rate *fr;
101 dc1394camera_list_t *list;
102 dc1394video_modes_t video_modes;
103 dc1394video_mode_t video_mode;
104 dc1394framerates_t frame_rates;
105 dc1394framerate_t frame_rate;
106 uint32_t dc1394_width, dc1394_height, dc1394_color_coding;
108 int score, max_score;
109 int final_width, final_height, final_pix_fmt, final_frame_rate;
113 /* Now let us prep the hardware. */
114 dc1394->d = dc1394_new();
115 dc1394_camera_enumerate (dc1394->d, &list);
116 if ( !list || list->num == 0) {
117 av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n");
121 /* FIXME: To select a specific camera I need to search in list its guid */
122 dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
124 av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
127 /* Freeing list of cameras */
128 dc1394_camera_free_list (list);
130 /* Get the list of video modes supported by the camera. */
131 res = dc1394_video_get_supported_modes (dc1394->camera, &video_modes);
132 if (res != DC1394_SUCCESS) {
133 av_log(c, AV_LOG_ERROR, "Could not get video formats.\n");
137 if (dc1394->video_size) {
138 if ((ret = av_parse_video_size(&ap->width, &ap->height, dc1394->video_size)) < 0) {
139 av_log(c, AV_LOG_ERROR, "Couldn't parse video size.\n");
144 /* Choose the best mode. */
145 rate = (ap->time_base.num ? av_rescale(1000, ap->time_base.den, ap->time_base.num) : -1);
147 for (i = 0; i < video_modes.num; i++) {
148 if (video_modes.modes[i] == DC1394_VIDEO_MODE_EXIF
149 || (video_modes.modes[i] >= DC1394_VIDEO_MODE_FORMAT7_MIN
150 && video_modes.modes[i] <= DC1394_VIDEO_MODE_FORMAT7_MAX)) {
151 /* These modes are currently not supported as they would require
152 * much more work. For the remaining modes, the functions
153 * dc1394_get_image_size_from_video_mode and
154 * dc1394_get_color_coding_from_video_mode do not need to query the
155 * camera, and thus cannot fail. */
158 dc1394_get_color_coding_from_video_mode (NULL, video_modes.modes[i],
159 &dc1394_color_coding);
160 for (cc = dc1394_color_codings; cc->pix_fmt != PIX_FMT_NONE; cc++)
161 if (cc->coding == dc1394_color_coding)
163 if (cc->pix_fmt == PIX_FMT_NONE) {
164 /* We currently cannot handle this color coding. */
167 /* Here we know that the mode is supported. Get its frame size and the list
168 * of frame rates supported by the camera for this mode. This list is sorted
169 * in ascending order according to libdc1394 example programs. */
170 dc1394_get_image_size_from_video_mode (NULL, video_modes.modes[i],
171 &dc1394_width, &dc1394_height);
172 res = dc1394_video_get_supported_framerates (dc1394->camera, video_modes.modes[i],
174 if (res != DC1394_SUCCESS || frame_rates.num == 0) {
175 av_log(c, AV_LOG_ERROR, "Cannot get frame rates for video mode.\n");
178 /* Choose the best frame rate. */
180 for (j = 0; j < frame_rates.num; j++) {
181 for (fr = dc1394_frame_rates; fr->frame_rate; fr++) {
182 if (fr->frame_rate_id == frame_rates.framerates[j]) {
186 if (!fr->frame_rate) {
187 /* This frame rate is not supported. */
190 best_rate = fr->frame_rate;
191 frame_rate = fr->frame_rate_id;
192 if (ap->time_base.num && rate == fr->frame_rate) {
193 /* This is the requested frame rate. */
197 if (best_rate == -1) {
198 /* No supported rate found. */
201 /* Here we know that both the mode and the rate are supported. Compute score. */
202 if (ap->width && ap->height
203 && (dc1394_width == ap->width && dc1394_height == ap->height)) {
206 score = dc1394_width * 10; // 1600 - 16000
208 if (ap->pix_fmt == cc->pix_fmt) {
211 score += cc->score; // 1000 - 1500
213 if (ap->time_base.num && rate == best_rate) {
216 score += best_rate / 1000; // 1 - 240
218 if (score > max_score) {
219 video_mode = video_modes.modes[i];
220 final_width = dc1394_width;
221 final_height = dc1394_height;
222 final_pix_fmt = cc->pix_fmt;
223 final_frame_rate = best_rate;
227 if (max_score == -1) {
228 av_log(c, AV_LOG_ERROR, "No suitable video mode / frame rate available.\n");
231 if (ap->width && ap->height && !(ap->width == final_width && ap->height == final_height)) {
232 av_log(c, AV_LOG_WARNING, "Requested frame size is not available, using fallback.\n");
234 if (ap->pix_fmt != PIX_FMT_NONE && ap->pix_fmt != final_pix_fmt) {
235 av_log(c, AV_LOG_WARNING, "Requested pixel format is not supported, using fallback.\n");
237 if (ap->time_base.num && rate != final_frame_rate) {
238 av_log(c, AV_LOG_WARNING, "Requested frame rate is not available, using fallback.\n");
241 /* create a video stream */
242 vst = av_new_stream(c, 0);
245 av_set_pts_info(vst, 64, 1, 1000);
246 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
247 vst->codec->codec_id = CODEC_ID_RAWVIDEO;
248 vst->codec->time_base.den = final_frame_rate;
249 vst->codec->time_base.num = 1000;
250 vst->codec->width = final_width;
251 vst->codec->height = final_height;
252 vst->codec->pix_fmt = final_pix_fmt;
255 av_init_packet(&dc1394->packet);
256 dc1394->packet.size = avpicture_get_size(final_pix_fmt, final_width, final_height);
257 dc1394->packet.stream_index = vst->index;
258 dc1394->packet.flags |= AV_PKT_FLAG_KEY;
260 dc1394->current_frame = 0;
261 dc1394->fps = final_frame_rate;
263 vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, final_frame_rate, 1000);
265 /* Select MAX Speed possible from the cam */
266 if (dc1394->camera->bmode_capable>0) {
267 dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
268 i = DC1394_ISO_SPEED_800;
270 i = DC1394_ISO_SPEED_400;
273 for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
274 res=dc1394_video_set_iso_speed(dc1394->camera, i);
276 if (res != DC1394_SUCCESS) {
277 av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
281 if (dc1394_video_set_mode(dc1394->camera, video_mode) != DC1394_SUCCESS) {
282 av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
286 if (dc1394_video_set_framerate(dc1394->camera, frame_rate) != DC1394_SUCCESS) {
287 av_log(c, AV_LOG_ERROR, "Could not set framerate %d.\n", final_frame_rate);
290 if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
291 av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
295 if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
296 av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
302 dc1394_capture_stop(dc1394->camera);
303 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
304 dc1394_camera_free (dc1394->camera);
306 dc1394_free(dc1394->d);
310 static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt)
312 struct dc1394_data *dc1394 = c->priv_data;
315 /* discard stale frame */
316 if (dc1394->current_frame++) {
317 if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
318 av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
321 res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
322 if (res == DC1394_SUCCESS) {
323 dc1394->packet.data = (uint8_t *)(dc1394->frame->image);
324 dc1394->packet.pts = (dc1394->current_frame * 1000000) / (dc1394->fps);
325 res = dc1394->frame->image_bytes;
327 av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
328 dc1394->packet.data = NULL;
332 *pkt = dc1394->packet;
336 static int dc1394_close(AVFormatContext * context)
338 struct dc1394_data *dc1394 = context->priv_data;
340 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
341 dc1394_capture_stop(dc1394->camera);
342 dc1394_camera_free(dc1394->camera);
343 dc1394_free(dc1394->d);
348 AVInputFormat ff_libdc1394_demuxer = {
350 .long_name = NULL_IF_CONFIG_SMALL("dc1394 A/V grab"),
351 .priv_data_size = sizeof(struct dc1394_data),
352 .read_header = dc1394_read_header,
353 .read_packet = dc1394_read_packet,
354 .read_close = dc1394_close,
355 .flags = AVFMT_NOFILE,
356 .priv_class = &libdc1394_class,