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