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 "libavformat/internal.h"
26 #include "libavutil/log.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/opt.h"
33 #include "libavutil/parseutils.h"
34 #include "libavutil/pixdesc.h"
36 #include <dc1394/dc1394.h>
40 typedef struct dc1394_data {
43 dc1394camera_t *camera;
44 dc1394video_frame_t *frame;
46 int frame_rate; /**< frames per 1000 seconds (fps * 1000) */
47 char *video_size; /**< String describing video size, set by a private option. */
48 char *pixel_format; /**< Set by a private option. */
49 char *framerate; /**< Set by a private option. */
54 /* The list of color codings that we support.
55 * We assume big endian for the dc1394 16bit modes: libdc1394 never sets the
56 * flag little_endian in dc1394video_frame_t. */
57 struct dc1394_color_coding {
61 } dc1394_color_codings[] = {
62 { PIX_FMT_GRAY16BE, 1000, DC1394_COLOR_CODING_MONO16 },
63 { PIX_FMT_RGB48BE, 1100, DC1394_COLOR_CODING_RGB16 },
64 { PIX_FMT_GRAY8, 1200, DC1394_COLOR_CODING_MONO8 },
65 { PIX_FMT_RGB24, 1300, DC1394_COLOR_CODING_RGB8 },
66 { PIX_FMT_UYYVYY411, 1400, DC1394_COLOR_CODING_YUV411 },
67 { PIX_FMT_UYVY422, 1500, DC1394_COLOR_CODING_YUV422 },
68 { PIX_FMT_NONE, 0, 0 } /* gotta be the last one */
71 struct dc1394_frame_rate {
74 } dc1394_frame_rates[] = {
75 { 1875, DC1394_FRAMERATE_1_875 },
76 { 3750, DC1394_FRAMERATE_3_75 },
77 { 7500, DC1394_FRAMERATE_7_5 },
78 { 15000, DC1394_FRAMERATE_15 },
79 { 30000, DC1394_FRAMERATE_30 },
80 { 60000, DC1394_FRAMERATE_60 },
81 {120000, DC1394_FRAMERATE_120 },
82 {240000, DC1394_FRAMERATE_240 },
83 { 0, 0 } /* gotta be the last one */
86 #define OFFSET(x) offsetof(dc1394_data, x)
87 #define DEC AV_OPT_FLAG_DECODING_PARAM
88 static const AVOption options[] = {
89 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "qvga"}, 0, 0, DEC },
90 { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "uyvy422"}, 0, 0, DEC },
91 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
95 static const AVClass libdc1394_class = {
96 .class_name = "libdc1394 indev",
97 .item_name = av_default_item_name,
99 .version = LIBAVUTIL_VERSION_INT,
102 static int dc1394_read_header(AVFormatContext *c, AVFormatParameters * ap)
104 dc1394_data* dc1394 = c->priv_data;
106 const struct dc1394_color_coding *cc;
107 const struct dc1394_frame_rate *fr;
108 dc1394camera_list_t *list;
109 dc1394video_modes_t video_modes;
110 dc1394video_mode_t video_mode;
111 dc1394framerates_t frame_rates;
112 dc1394framerate_t frame_rate;
113 uint32_t dc1394_width, dc1394_height, dc1394_color_coding;
115 int score, max_score;
116 int final_width, final_height, final_pix_fmt, final_frame_rate;
120 /* Now let us prep the hardware. */
121 dc1394->d = dc1394_new();
122 dc1394_camera_enumerate (dc1394->d, &list);
123 if ( !list || list->num == 0) {
124 av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n");
128 /* FIXME: To select a specific camera I need to search in list its guid */
129 dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
131 av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
134 /* Freeing list of cameras */
135 dc1394_camera_free_list (list);
137 /* Get the list of video modes supported by the camera. */
138 res = dc1394_video_get_supported_modes (dc1394->camera, &video_modes);
139 if (res != DC1394_SUCCESS) {
140 av_log(c, AV_LOG_ERROR, "Could not get video formats.\n");
144 if (dc1394->pixel_format) {
145 if ((ap->pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == PIX_FMT_NONE) {
146 av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format);
147 ret = AVERROR(EINVAL);
152 if (dc1394->video_size) {
153 if ((ret = av_parse_video_size(&ap->width, &ap->height, dc1394->video_size)) < 0) {
154 av_log(c, AV_LOG_ERROR, "Couldn't parse video size.\n");
159 /* Choose the best mode. */
160 rate = (ap->time_base.num ? av_rescale(1000, ap->time_base.den, ap->time_base.num) : -1);
162 for (i = 0; i < video_modes.num; i++) {
163 if (video_modes.modes[i] == DC1394_VIDEO_MODE_EXIF
164 || (video_modes.modes[i] >= DC1394_VIDEO_MODE_FORMAT7_MIN
165 && video_modes.modes[i] <= DC1394_VIDEO_MODE_FORMAT7_MAX)) {
166 /* These modes are currently not supported as they would require
167 * much more work. For the remaining modes, the functions
168 * dc1394_get_image_size_from_video_mode and
169 * dc1394_get_color_coding_from_video_mode do not need to query the
170 * camera, and thus cannot fail. */
173 dc1394_get_color_coding_from_video_mode (NULL, video_modes.modes[i],
174 &dc1394_color_coding);
175 for (cc = dc1394_color_codings; cc->pix_fmt != PIX_FMT_NONE; cc++)
176 if (cc->coding == dc1394_color_coding)
178 if (cc->pix_fmt == PIX_FMT_NONE) {
179 /* We currently cannot handle this color coding. */
182 /* Here we know that the mode is supported. Get its frame size and the list
183 * of frame rates supported by the camera for this mode. This list is sorted
184 * in ascending order according to libdc1394 example programs. */
185 dc1394_get_image_size_from_video_mode (NULL, video_modes.modes[i],
186 &dc1394_width, &dc1394_height);
187 res = dc1394_video_get_supported_framerates (dc1394->camera, video_modes.modes[i],
189 if (res != DC1394_SUCCESS || frame_rates.num == 0) {
190 av_log(c, AV_LOG_ERROR, "Cannot get frame rates for video mode.\n");
193 /* Choose the best frame rate. */
195 for (j = 0; j < frame_rates.num; j++) {
196 for (fr = dc1394_frame_rates; fr->frame_rate; fr++) {
197 if (fr->frame_rate_id == frame_rates.framerates[j]) {
201 if (!fr->frame_rate) {
202 /* This frame rate is not supported. */
205 best_rate = fr->frame_rate;
206 frame_rate = fr->frame_rate_id;
207 if (ap->time_base.num && rate == fr->frame_rate) {
208 /* This is the requested frame rate. */
212 if (best_rate == -1) {
213 /* No supported rate found. */
216 /* Here we know that both the mode and the rate are supported. Compute score. */
217 if (ap->width && ap->height
218 && (dc1394_width == ap->width && dc1394_height == ap->height)) {
221 score = dc1394_width * 10; // 1600 - 16000
223 if (ap->pix_fmt == cc->pix_fmt) {
226 score += cc->score; // 1000 - 1500
228 if (ap->time_base.num && rate == best_rate) {
231 score += best_rate / 1000; // 1 - 240
233 if (score > max_score) {
234 video_mode = video_modes.modes[i];
235 final_width = dc1394_width;
236 final_height = dc1394_height;
237 final_pix_fmt = cc->pix_fmt;
238 final_frame_rate = best_rate;
242 if (max_score == -1) {
243 av_log(c, AV_LOG_ERROR, "No suitable video mode / frame rate available.\n");
246 if (ap->width && ap->height && !(ap->width == final_width && ap->height == final_height)) {
247 av_log(c, AV_LOG_WARNING, "Requested frame size is not available, using fallback.\n");
249 if (ap->pix_fmt != PIX_FMT_NONE && ap->pix_fmt != final_pix_fmt) {
250 av_log(c, AV_LOG_WARNING, "Requested pixel format is not supported, using fallback.\n");
252 if (ap->time_base.num && rate != final_frame_rate) {
253 av_log(c, AV_LOG_WARNING, "Requested frame rate is not available, using fallback.\n");
256 /* create a video stream */
257 vst = avformat_new_stream(c, NULL);
260 avpriv_set_pts_info(vst, 64, 1, 1000);
261 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
262 vst->codec->codec_id = CODEC_ID_RAWVIDEO;
263 vst->codec->time_base.den = final_frame_rate;
264 vst->codec->time_base.num = 1000;
265 vst->codec->width = final_width;
266 vst->codec->height = final_height;
267 vst->codec->pix_fmt = final_pix_fmt;
270 av_init_packet(&dc1394->packet);
271 dc1394->packet.size = avpicture_get_size(final_pix_fmt, final_width, final_height);
272 dc1394->packet.stream_index = vst->index;
273 dc1394->packet.flags |= AV_PKT_FLAG_KEY;
275 dc1394->current_frame = 0;
276 dc1394->frame_rate = final_frame_rate;
278 vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, final_frame_rate, 1000);
280 /* Select MAX Speed possible from the cam */
281 if (dc1394->camera->bmode_capable>0) {
282 dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
283 i = DC1394_ISO_SPEED_800;
285 i = DC1394_ISO_SPEED_400;
288 for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
289 res=dc1394_video_set_iso_speed(dc1394->camera, i);
291 if (res != DC1394_SUCCESS) {
292 av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
296 if (dc1394_video_set_mode(dc1394->camera, video_mode) != DC1394_SUCCESS) {
297 av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
301 if (dc1394_video_set_framerate(dc1394->camera, frame_rate) != DC1394_SUCCESS) {
302 av_log(c, AV_LOG_ERROR, "Could not set framerate %d.\n", final_frame_rate);
305 if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
306 av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
310 if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
311 av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
317 dc1394_capture_stop(dc1394->camera);
318 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
319 dc1394_camera_free (dc1394->camera);
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,