cc348d4e84090eece2ea9d9914b966e49f942142
[libav.git] / libavdevice / libdc1394.c
1 /*
2 * IIDC1394 grab interface (uses libdc1394 and libraw1394)
3 * Copyright (c) 2004 Roman Shaposhnik
4 * Copyright (c) 2008 Alessandro Sappia
5 *
6 * This file is part of Libav.
7 *
8 * Libav 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.
12 *
13 * Libav 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.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "config.h"
24 #include "libavformat/avformat.h"
25 #include "libavformat/internal.h"
26 #include "libavutil/log.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/parseutils.h"
30 #include "libavutil/pixdesc.h"
31
32 #if HAVE_LIBDC1394_2
33 #include <dc1394/dc1394.h>
34 #elif HAVE_LIBDC1394_1
35 #include <libraw1394/raw1394.h>
36 #include <libdc1394/dc1394_control.h>
37
38 #define DC1394_VIDEO_MODE_320x240_YUV422 MODE_320x240_YUV422
39 #define DC1394_VIDEO_MODE_640x480_YUV411 MODE_640x480_YUV411
40 #define DC1394_VIDEO_MODE_640x480_YUV422 MODE_640x480_YUV422
41 #define DC1394_FRAMERATE_1_875 FRAMERATE_1_875
42 #define DC1394_FRAMERATE_3_75 FRAMERATE_3_75
43 #define DC1394_FRAMERATE_7_5 FRAMERATE_7_5
44 #define DC1394_FRAMERATE_15 FRAMERATE_15
45 #define DC1394_FRAMERATE_30 FRAMERATE_30
46 #define DC1394_FRAMERATE_60 FRAMERATE_60
47 #define DC1394_FRAMERATE_120 FRAMERATE_120
48 #define DC1394_FRAMERATE_240 FRAMERATE_240
49 #endif
50
51 typedef struct dc1394_data {
52 AVClass *class;
53 #if HAVE_LIBDC1394_1
54 raw1394handle_t handle;
55 dc1394_cameracapture camera;
56 int channel;
57 #elif HAVE_LIBDC1394_2
58 dc1394_t *d;
59 dc1394camera_t *camera;
60 dc1394video_frame_t *frame;
61 #endif
62 int current_frame;
63 int frame_rate; /**< frames per 1000 seconds (fps * 1000) */
64 char *video_size; /**< String describing video size, set by a private option. */
65 char *pixel_format; /**< Set by a private option. */
66 char *framerate; /**< Set by a private option. */
67
68 AVPacket packet;
69 } dc1394_data;
70
71 struct dc1394_frame_format {
72 int width;
73 int height;
74 enum AVPixelFormat pix_fmt;
75 int frame_size_id;
76 } dc1394_frame_formats[] = {
77 { 320, 240, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_320x240_YUV422 },
78 { 640, 480, AV_PIX_FMT_GRAY8, DC1394_VIDEO_MODE_640x480_MONO8 },
79 { 640, 480, AV_PIX_FMT_UYYVYY411, DC1394_VIDEO_MODE_640x480_YUV411 },
80 { 640, 480, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_640x480_YUV422 },
81 { 0, 0, 0, 0 } /* gotta be the last one */
82 };
83
84 struct dc1394_frame_rate {
85 int frame_rate;
86 int frame_rate_id;
87 } dc1394_frame_rates[] = {
88 { 1875, DC1394_FRAMERATE_1_875 },
89 { 3750, DC1394_FRAMERATE_3_75 },
90 { 7500, DC1394_FRAMERATE_7_5 },
91 { 15000, DC1394_FRAMERATE_15 },
92 { 30000, DC1394_FRAMERATE_30 },
93 { 60000, DC1394_FRAMERATE_60 },
94 {120000, DC1394_FRAMERATE_120 },
95 {240000, DC1394_FRAMERATE_240 },
96 { 0, 0 } /* gotta be the last one */
97 };
98
99 #define OFFSET(x) offsetof(dc1394_data, x)
100 #define DEC AV_OPT_FLAG_DECODING_PARAM
101 static const AVOption options[] = {
102 #if HAVE_LIBDC1394_1
103 { "channel", "", offsetof(dc1394_data, channel), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
104 #endif
105 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "qvga"}, 0, 0, DEC },
106 { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "uyvy422"}, 0, 0, DEC },
107 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
108 { NULL },
109 };
110
111 static const AVClass libdc1394_class = {
112 .class_name = "libdc1394 indev",
113 .item_name = av_default_item_name,
114 .option = options,
115 .version = LIBAVUTIL_VERSION_INT,
116 };
117
118
119 static inline int dc1394_read_common(AVFormatContext *c,
120 struct dc1394_frame_format **select_fmt, struct dc1394_frame_rate **select_fps)
121 {
122 dc1394_data* dc1394 = c->priv_data;
123 AVStream* vst;
124 struct dc1394_frame_format *fmt;
125 struct dc1394_frame_rate *fps;
126 enum AVPixelFormat pix_fmt;
127 int width, height;
128 AVRational framerate;
129 int ret = 0;
130
131 if ((pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == AV_PIX_FMT_NONE) {
132 av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format);
133 ret = AVERROR(EINVAL);
134 goto out;
135 }
136
137 if ((ret = av_parse_video_size(&width, &height, dc1394->video_size)) < 0) {
138 av_log(c, AV_LOG_ERROR, "Could not parse video size '%s'.\n", dc1394->video_size);
139 goto out;
140 }
141 if ((ret = av_parse_video_rate(&framerate, dc1394->framerate)) < 0) {
142 av_log(c, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", dc1394->framerate);
143 goto out;
144 }
145 dc1394->frame_rate = av_rescale(1000, framerate.num, framerate.den);
146
147 for (fmt = dc1394_frame_formats; fmt->width; fmt++)
148 if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
149 break;
150
151 for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
152 if (fps->frame_rate == dc1394->frame_rate)
153 break;
154
155 if (!fps->frame_rate || !fmt->width) {
156 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),
157 width, height, dc1394->frame_rate);
158 ret = AVERROR(EINVAL);
159 goto out;
160 }
161
162 /* create a video stream */
163 vst = avformat_new_stream(c, NULL);
164 if (!vst) {
165 ret = AVERROR(ENOMEM);
166 goto out;
167 }
168 avpriv_set_pts_info(vst, 64, 1, 1000);
169 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
170 vst->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
171 vst->codec->time_base.den = framerate.num;
172 vst->codec->time_base.num = framerate.den;
173 vst->codec->width = fmt->width;
174 vst->codec->height = fmt->height;
175 vst->codec->pix_fmt = fmt->pix_fmt;
176
177 /* packet init */
178 av_init_packet(&dc1394->packet);
179 dc1394->packet.size = avpicture_get_size(fmt->pix_fmt, fmt->width, fmt->height);
180 dc1394->packet.stream_index = vst->index;
181 dc1394->packet.flags |= AV_PKT_FLAG_KEY;
182
183 dc1394->current_frame = 0;
184
185 vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000);
186 *select_fps = fps;
187 *select_fmt = fmt;
188 out:
189 return ret;
190 }
191
192 #if HAVE_LIBDC1394_1
193 static int dc1394_v1_read_header(AVFormatContext *c)
194 {
195 dc1394_data* dc1394 = c->priv_data;
196 AVStream* vst;
197 nodeid_t* camera_nodes;
198 int res;
199 struct dc1394_frame_format *fmt = NULL;
200 struct dc1394_frame_rate *fps = NULL;
201
202 if (dc1394_read_common(c, &fmt, &fps) != 0)
203 return -1;
204
205 /* Now let us prep the hardware. */
206 dc1394->handle = dc1394_create_handle(0); /* FIXME: gotta have ap->port */
207 if (!dc1394->handle) {
208 av_log(c, AV_LOG_ERROR, "Can't acquire dc1394 handle on port %d\n", 0 /* ap->port */);
209 goto out;
210 }
211 camera_nodes = dc1394_get_camera_nodes(dc1394->handle, &res, 1);
212 if (!camera_nodes || camera_nodes[dc1394->channel] == DC1394_NO_CAMERA) {
213 av_log(c, AV_LOG_ERROR, "There's no IIDC camera on the channel %d\n", dc1394->channel);
214 goto out_handle;
215 }
216 res = dc1394_dma_setup_capture(dc1394->handle, camera_nodes[dc1394->channel],
217 0,
218 FORMAT_VGA_NONCOMPRESSED,
219 fmt->frame_size_id,
220 SPEED_400,
221 fps->frame_rate_id, 8, 1,
222 c->filename,
223 &dc1394->camera);
224 dc1394_free_camera_nodes(camera_nodes);
225 if (res != DC1394_SUCCESS) {
226 av_log(c, AV_LOG_ERROR, "Can't prepare camera for the DMA capture\n");
227 goto out_handle;
228 }
229
230 res = dc1394_start_iso_transmission(dc1394->handle, dc1394->camera.node);
231 if (res != DC1394_SUCCESS) {
232 av_log(c, AV_LOG_ERROR, "Can't start isochronous transmission\n");
233 goto out_handle_dma;
234 }
235
236 return 0;
237
238 out_handle_dma:
239 dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
240 dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
241 out_handle:
242 dc1394_destroy_handle(dc1394->handle);
243 out:
244 return -1;
245 }
246
247 static int dc1394_v1_read_packet(AVFormatContext *c, AVPacket *pkt)
248 {
249 struct dc1394_data *dc1394 = c->priv_data;
250 int res;
251
252 /* discard stale frame */
253 if (dc1394->current_frame++) {
254 if (dc1394_dma_done_with_buffer(&dc1394->camera) != DC1394_SUCCESS)
255 av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
256 }
257
258 res = dc1394_dma_single_capture(&dc1394->camera);
259
260 if (res == DC1394_SUCCESS) {
261 dc1394->packet.data = (uint8_t *)(dc1394->camera.capture_buffer);
262 dc1394->packet.pts = (dc1394->current_frame * 1000000) / dc1394->frame_rate;
263 res = dc1394->packet.size;
264 } else {
265 av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
266 dc1394->packet.data = NULL;
267 res = -1;
268 }
269
270 *pkt = dc1394->packet;
271 return res;
272 }
273
274 static int dc1394_v1_close(AVFormatContext * context)
275 {
276 struct dc1394_data *dc1394 = context->priv_data;
277
278 dc1394_stop_iso_transmission(dc1394->handle, dc1394->camera.node);
279 dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
280 dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
281 dc1394_destroy_handle(dc1394->handle);
282
283 return 0;
284 }
285
286 #elif HAVE_LIBDC1394_2
287 static int dc1394_v2_read_header(AVFormatContext *c)
288 {
289 dc1394_data* dc1394 = c->priv_data;
290 dc1394camera_list_t *list;
291 int res, i;
292 struct dc1394_frame_format *fmt = NULL;
293 struct dc1394_frame_rate *fps = NULL;
294
295 if (dc1394_read_common(c, &fmt, &fps) != 0)
296 return -1;
297
298 /* Now let us prep the hardware. */
299 dc1394->d = dc1394_new();
300 dc1394_camera_enumerate (dc1394->d, &list);
301 if ( !list || list->num == 0) {
302 av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n");
303 goto out;
304 }
305
306 /* FIXME: To select a specific camera I need to search in list its guid */
307 dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
308 if (list->num > 1) {
309 av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
310 }
311
312 /* Freeing list of cameras */
313 dc1394_camera_free_list (list);
314
315 /* Select MAX Speed possible from the cam */
316 if (dc1394->camera->bmode_capable>0) {
317 dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
318 i = DC1394_ISO_SPEED_800;
319 } else {
320 i = DC1394_ISO_SPEED_400;
321 }
322
323 for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
324 res=dc1394_video_set_iso_speed(dc1394->camera, i);
325 }
326 if (res != DC1394_SUCCESS) {
327 av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
328 goto out_camera;
329 }
330
331 if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
332 av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
333 goto out_camera;
334 }
335
336 if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
337 av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
338 goto out_camera;
339 }
340 if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
341 av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
342 goto out_camera;
343 }
344
345 if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
346 av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
347 goto out_camera;
348 }
349 return 0;
350
351 out_camera:
352 dc1394_capture_stop(dc1394->camera);
353 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
354 dc1394_camera_free (dc1394->camera);
355 out:
356 dc1394_free(dc1394->d);
357 return -1;
358 }
359
360 static int dc1394_v2_read_packet(AVFormatContext *c, AVPacket *pkt)
361 {
362 struct dc1394_data *dc1394 = c->priv_data;
363 int res;
364
365 /* discard stale frame */
366 if (dc1394->current_frame++) {
367 if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
368 av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
369 }
370
371 res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
372 if (res == DC1394_SUCCESS) {
373 dc1394->packet.data = (uint8_t *) dc1394->frame->image;
374 dc1394->packet.pts = dc1394->current_frame * 1000000 / dc1394->frame_rate;
375 res = dc1394->frame->image_bytes;
376 } else {
377 av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
378 dc1394->packet.data = NULL;
379 res = -1;
380 }
381
382 *pkt = dc1394->packet;
383 return res;
384 }
385
386 static int dc1394_v2_close(AVFormatContext * context)
387 {
388 struct dc1394_data *dc1394 = context->priv_data;
389
390 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
391 dc1394_capture_stop(dc1394->camera);
392 dc1394_camera_free(dc1394->camera);
393 dc1394_free(dc1394->d);
394
395 return 0;
396 }
397
398 AVInputFormat ff_libdc1394_demuxer = {
399 .name = "libdc1394",
400 .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
401 .priv_data_size = sizeof(struct dc1394_data),
402 .read_header = dc1394_v2_read_header,
403 .read_packet = dc1394_v2_read_packet,
404 .read_close = dc1394_v2_close,
405 .flags = AVFMT_NOFILE,
406 .priv_class = &libdc1394_class,
407 };
408
409 #endif
410 #if HAVE_LIBDC1394_1
411 AVInputFormat ff_libdc1394_demuxer = {
412 .name = "libdc1394",
413 .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.1 A/V grab"),
414 .priv_data_size = sizeof(struct dc1394_data),
415 .read_header = dc1394_v1_read_header,
416 .read_packet = dc1394_v1_read_packet,
417 .read_close = dc1394_v1_close,
418 .flags = AVFMT_NOFILE,
419 .priv_class = &libdc1394_class,
420 };
421 #endif