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