FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libdc1394.c
Go to the documentation of this file.
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 FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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
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 
69 } dc1394_data;
70 
72  int width;
73  int height;
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 
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,
117 };
118 
119 
121  struct dc1394_frame_format **select_fmt, struct dc1394_frame_rate **select_fps)
122 {
123  dc1394_data* dc1394 = c->priv_data;
124  AVStream* vst;
125  struct dc1394_frame_format *fmt;
126  struct dc1394_frame_rate *fps;
127  enum AVPixelFormat pix_fmt;
128  int width, height;
129  AVRational framerate;
130  int ret = 0;
131 
132  if ((pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == AV_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 
138  if ((ret = av_parse_video_size(&width, &height, dc1394->video_size)) < 0) {
139  av_log(c, AV_LOG_ERROR, "Could not parse video size '%s'.\n", dc1394->video_size);
140  goto out;
141  }
142  if ((ret = av_parse_video_rate(&framerate, dc1394->framerate)) < 0) {
143  av_log(c, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", dc1394->framerate);
144  goto out;
145  }
146  dc1394->frame_rate = av_rescale(1000, framerate.num, framerate.den);
147 
148  for (fmt = dc1394_frame_formats; fmt->width; fmt++)
149  if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
150  break;
151 
152  for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
153  if (fps->frame_rate == dc1394->frame_rate)
154  break;
155 
156  if (!fps->frame_rate || !fmt->width) {
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),
158  width, height, dc1394->frame_rate);
159  ret = AVERROR(EINVAL);
160  goto out;
161  }
162 
163  /* create a video stream */
164  vst = avformat_new_stream(c, NULL);
165  if (!vst) {
166  ret = AVERROR(ENOMEM);
167  goto out;
168  }
169  avpriv_set_pts_info(vst, 64, 1, 1000);
172  vst->codec->time_base.den = framerate.num;
173  vst->codec->time_base.num = framerate.den;
174  vst->codec->width = fmt->width;
175  vst->codec->height = fmt->height;
176  vst->codec->pix_fmt = fmt->pix_fmt;
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;
182  dc1394->packet.flags |= AV_PKT_FLAG_KEY;
183 
184  dc1394->current_frame = 0;
185 
186  vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000);
187  *select_fps = fps;
188  *select_fmt = fmt;
189 out:
190  return ret;
191 }
192 
193 #if HAVE_LIBDC1394_1
194 static int dc1394_v1_read_header(AVFormatContext *c)
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 
203  if (dc1394_read_common(c, &fmt, &fps) != 0)
204  return -1;
205 
206  /* Now let us prep the hardware. */
207  dc1394->handle = dc1394_create_handle(0); /* FIXME: gotta have ap->port */
208  if (!dc1394->handle) {
209  av_log(c, AV_LOG_ERROR, "Can't acquire dc1394 handle on port %d\n", 0 /* ap->port */);
210  goto out;
211  }
212  camera_nodes = dc1394_get_camera_nodes(dc1394->handle, &res, 1);
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);
215  goto out_handle;
216  }
217  res = dc1394_dma_setup_capture(dc1394->handle, camera_nodes[dc1394->channel],
218  0,
219  FORMAT_VGA_NONCOMPRESSED,
220  fmt->frame_size_id,
221  SPEED_400,
222  fps->frame_rate_id, 8, 1,
223  c->filename,
224  &dc1394->camera);
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");
228  goto out_handle;
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");
234  goto out_handle_dma;
235  }
236 
237  return 0;
238 
239 out_handle_dma:
240  dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
241  dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
242 out_handle:
243  dc1394_destroy_handle(dc1394->handle);
244 out:
245  return -1;
246 }
247 
248 static int dc1394_v1_read_packet(AVFormatContext *c, AVPacket *pkt)
249 {
250  struct dc1394_data *dc1394 = c->priv_data;
251  int res;
252 
253  /* discard stale frame */
254  if (dc1394->current_frame++) {
255  if (dc1394_dma_done_with_buffer(&dc1394->camera) != DC1394_SUCCESS)
256  av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
257  }
258 
259  res = dc1394_dma_single_capture(&dc1394->camera);
260 
261  if (res == DC1394_SUCCESS) {
262  dc1394->packet.data = (uint8_t *)(dc1394->camera.capture_buffer);
263  dc1394->packet.pts = (dc1394->current_frame * 1000000) / dc1394->frame_rate;
264  res = dc1394->packet.size;
265  } else {
266  av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
267  dc1394->packet.data = NULL;
268  res = -1;
269  }
270 
271  *pkt = dc1394->packet;
272  return res;
273 }
274 
275 static int dc1394_v1_close(AVFormatContext * context)
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);
283 
284  return 0;
285 }
286 
287 #elif HAVE_LIBDC1394_2
288 static int dc1394_v2_read_header(AVFormatContext *c)
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 
296  if (dc1394_read_common(c, &fmt, &fps) != 0)
297  return -1;
298 
299  /* Now let us prep the hardware. */
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 
352 out_camera:
353  dc1394_capture_stop(dc1394->camera);
354  dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
355  dc1394_camera_free (dc1394->camera);
356 out:
357  dc1394_free(dc1394->d);
358  return -1;
359 }
360 
361 static 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) {
374  dc1394->packet.data = (uint8_t *) dc1394->frame->image;
375  dc1394->packet.pts = dc1394->current_frame * 1000000 / dc1394->frame_rate;
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 
387 static 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 
399 AVInputFormat ff_libdc1394_demuxer = {
400  .name = "libdc1394",
401  .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
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,
406  .flags = AVFMT_NOFILE,
407  .priv_class = &libdc1394_class,
408 };
409 
410 #endif
411 #if HAVE_LIBDC1394_1
412 AVInputFormat ff_libdc1394_demuxer = {
413  .name = "libdc1394",
414  .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.1 A/V grab"),
415  .priv_data_size = sizeof(struct dc1394_data),
416  .read_header = dc1394_v1_read_header,
417  .read_packet = dc1394_v1_read_packet,
418  .read_close = dc1394_v1_close,
419  .flags = AVFMT_NOFILE,
420  .priv_class = &libdc1394_class,
421 };
422 #endif