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 
25 #if HAVE_LIBDC1394_2
26 #include <dc1394/dc1394.h>
27 #elif HAVE_LIBDC1394_1
28 #include <libraw1394/raw1394.h>
29 #include <libdc1394/dc1394_control.h>
30 
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
40 #define DC1394_FRAMERATE_120 FRAMERATE_120
41 #define DC1394_FRAMERATE_240 FRAMERATE_240
42 #endif
43 
44 #include "libavutil/imgutils.h"
45 #include "libavutil/internal.h"
46 #include "libavutil/log.h"
47 #include "libavutil/mathematics.h"
48 #include "libavutil/opt.h"
49 #include "libavutil/parseutils.h"
50 #include "libavutil/pixdesc.h"
51 
52 #include "libavformat/avformat.h"
53 #include "libavformat/internal.h"
54 
55 typedef struct dc1394_data {
56  AVClass *class;
57 #if HAVE_LIBDC1394_1
58  raw1394handle_t handle;
59  dc1394_cameracapture camera;
60  int channel;
61 #elif HAVE_LIBDC1394_2
62  dc1394_t *d;
63  dc1394camera_t *camera;
64  dc1394video_frame_t *frame;
65 #endif
67  int frame_rate; /**< frames per 1000 seconds (fps * 1000) */
68  char *video_size; /**< String describing video size, set by a private option. */
69  char *pixel_format; /**< Set by a private option. */
70  char *framerate; /**< Set by a private option. */
71 
73 } dc1394_data;
74 
75 static const struct dc1394_frame_format {
76  int width;
77  int height;
81  { 320, 240, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_320x240_YUV422 },
82  { 640, 480, AV_PIX_FMT_GRAY8, DC1394_VIDEO_MODE_640x480_MONO8 },
83  { 640, 480, AV_PIX_FMT_UYYVYY411, DC1394_VIDEO_MODE_640x480_YUV411 },
84  { 640, 480, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_640x480_YUV422 },
85  { 0, 0, 0, 0 } /* gotta be the last one */
86 };
87 
88 static const struct dc1394_frame_rate {
91 } dc1394_frame_rates[] = {
92  { 1875, DC1394_FRAMERATE_1_875 },
93  { 3750, DC1394_FRAMERATE_3_75 },
94  { 7500, DC1394_FRAMERATE_7_5 },
95  { 15000, DC1394_FRAMERATE_15 },
96  { 30000, DC1394_FRAMERATE_30 },
97  { 60000, DC1394_FRAMERATE_60 },
98  {120000, DC1394_FRAMERATE_120 },
99  {240000, DC1394_FRAMERATE_240 },
100  { 0, 0 } /* gotta be the last one */
101 };
102 
103 #define OFFSET(x) offsetof(dc1394_data, x)
104 #define DEC AV_OPT_FLAG_DECODING_PARAM
105 static const AVOption options[] = {
106 #if HAVE_LIBDC1394_1
107  { "channel", "", offsetof(dc1394_data, channel), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
108 #endif
109  { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "qvga"}, 0, 0, DEC },
110  { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "uyvy422"}, 0, 0, DEC },
111  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
112  { NULL },
113 };
114 
115 static const AVClass libdc1394_class = {
116  .class_name = "libdc1394 indev",
117  .item_name = av_default_item_name,
118  .option = options,
119  .version = LIBAVUTIL_VERSION_INT,
121 };
122 
123 
125  const struct dc1394_frame_format **select_fmt, const struct dc1394_frame_rate **select_fps)
126 {
127  dc1394_data* dc1394 = c->priv_data;
128  AVStream* vst;
129  const struct dc1394_frame_format *fmt;
130  const struct dc1394_frame_rate *fps;
131  enum AVPixelFormat pix_fmt;
132  int width, height;
133  AVRational framerate;
134  int ret = 0;
135 
136  if ((pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == AV_PIX_FMT_NONE) {
137  av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format);
138  ret = AVERROR(EINVAL);
139  goto out;
140  }
141 
142  if ((ret = av_parse_video_size(&width, &height, dc1394->video_size)) < 0) {
143  av_log(c, AV_LOG_ERROR, "Could not parse video size '%s'.\n", dc1394->video_size);
144  goto out;
145  }
146  if ((ret = av_parse_video_rate(&framerate, dc1394->framerate)) < 0) {
147  av_log(c, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", dc1394->framerate);
148  goto out;
149  }
150  dc1394->frame_rate = av_rescale(1000, framerate.num, framerate.den);
151 
152  for (fmt = dc1394_frame_formats; fmt->width; fmt++)
153  if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
154  break;
155 
156  for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
157  if (fps->frame_rate == dc1394->frame_rate)
158  break;
159 
160  if (!fps->frame_rate || !fmt->width) {
161  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),
162  width, height, dc1394->frame_rate);
163  ret = AVERROR(EINVAL);
164  goto out;
165  }
166 
167  /* create a video stream */
168  vst = avformat_new_stream(c, NULL);
169  if (!vst) {
170  ret = AVERROR(ENOMEM);
171  goto out;
172  }
173  avpriv_set_pts_info(vst, 64, 1, 1000);
176  vst->codec->time_base.den = framerate.num;
177  vst->codec->time_base.num = framerate.den;
178  vst->codec->width = fmt->width;
179  vst->codec->height = fmt->height;
180  vst->codec->pix_fmt = fmt->pix_fmt;
181 
182  /* packet init */
183  av_init_packet(&dc1394->packet);
185  fmt->width, fmt->height, 1);
186  dc1394->packet.stream_index = vst->index;
187  dc1394->packet.flags |= AV_PKT_FLAG_KEY;
188 
189  dc1394->current_frame = 0;
190 
191  vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000);
192  *select_fps = fps;
193  *select_fmt = fmt;
194 out:
195  return ret;
196 }
197 
198 #if HAVE_LIBDC1394_1
199 static int dc1394_v1_read_header(AVFormatContext *c)
200 {
201  dc1394_data* dc1394 = c->priv_data;
202  AVStream* vst;
203  nodeid_t* camera_nodes;
204  int res;
205  struct dc1394_frame_format *fmt = NULL;
206  struct dc1394_frame_rate *fps = NULL;
207 
208  if (dc1394_read_common(c, &fmt, &fps) != 0)
209  return -1;
210 
211  /* Now let us prep the hardware. */
212  dc1394->handle = dc1394_create_handle(0); /* FIXME: gotta have ap->port */
213  if (!dc1394->handle) {
214  av_log(c, AV_LOG_ERROR, "Can't acquire dc1394 handle on port %d\n", 0 /* ap->port */);
215  goto out;
216  }
217  camera_nodes = dc1394_get_camera_nodes(dc1394->handle, &res, 1);
218  if (!camera_nodes || camera_nodes[dc1394->channel] == DC1394_NO_CAMERA) {
219  av_log(c, AV_LOG_ERROR, "There's no IIDC camera on the channel %d\n", dc1394->channel);
220  goto out_handle;
221  }
222  res = dc1394_dma_setup_capture(dc1394->handle, camera_nodes[dc1394->channel],
223  0,
224  FORMAT_VGA_NONCOMPRESSED,
225  fmt->frame_size_id,
226  SPEED_400,
227  fps->frame_rate_id, 8, 1,
228  c->filename,
229  &dc1394->camera);
230  dc1394_free_camera_nodes(camera_nodes);
231  if (res != DC1394_SUCCESS) {
232  av_log(c, AV_LOG_ERROR, "Can't prepare camera for the DMA capture\n");
233  goto out_handle;
234  }
235 
236  res = dc1394_start_iso_transmission(dc1394->handle, dc1394->camera.node);
237  if (res != DC1394_SUCCESS) {
238  av_log(c, AV_LOG_ERROR, "Can't start isochronous transmission\n");
239  goto out_handle_dma;
240  }
241 
242  return 0;
243 
244 out_handle_dma:
245  dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
246  dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
247 out_handle:
248  dc1394_destroy_handle(dc1394->handle);
249 out:
250  return -1;
251 }
252 
253 static int dc1394_v1_read_packet(AVFormatContext *c, AVPacket *pkt)
254 {
255  struct dc1394_data *dc1394 = c->priv_data;
256  int res;
257 
258  /* discard stale frame */
259  if (dc1394->current_frame++) {
260  if (dc1394_dma_done_with_buffer(&dc1394->camera) != DC1394_SUCCESS)
261  av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
262  }
263 
264  res = dc1394_dma_single_capture(&dc1394->camera);
265 
266  if (res == DC1394_SUCCESS) {
267  dc1394->packet.data = (uint8_t *)(dc1394->camera.capture_buffer);
268  dc1394->packet.pts = (dc1394->current_frame * 1000000) / dc1394->frame_rate;
269  res = dc1394->packet.size;
270  } else {
271  av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
272  dc1394->packet.data = NULL;
273  res = -1;
274  }
275 
276  *pkt = dc1394->packet;
277  return res;
278 }
279 
280 static int dc1394_v1_close(AVFormatContext * context)
281 {
282  struct dc1394_data *dc1394 = context->priv_data;
283 
284  dc1394_stop_iso_transmission(dc1394->handle, dc1394->camera.node);
285  dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
286  dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
287  dc1394_destroy_handle(dc1394->handle);
288 
289  return 0;
290 }
291 
292 #elif HAVE_LIBDC1394_2
293 static int dc1394_v2_read_header(AVFormatContext *c)
294 {
295  dc1394_data* dc1394 = c->priv_data;
296  dc1394camera_list_t *list;
297  int res, i;
298  const struct dc1394_frame_format *fmt = NULL;
299  const struct dc1394_frame_rate *fps = NULL;
300 
301  if (dc1394_read_common(c, &fmt, &fps) != 0)
302  return -1;
303 
304  /* Now let us prep the hardware. */
305  dc1394->d = dc1394_new();
306  dc1394_camera_enumerate (dc1394->d, &list);
307  if ( !list || list->num == 0) {
308  av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n");
309  goto out;
310  }
311 
312  /* FIXME: To select a specific camera I need to search in list its guid */
313  dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
314  if (list->num > 1) {
315  av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
316  }
317 
318  /* Freeing list of cameras */
319  dc1394_camera_free_list (list);
320 
321  /* Select MAX Speed possible from the cam */
322  if (dc1394->camera->bmode_capable>0) {
323  dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
324  i = DC1394_ISO_SPEED_800;
325  } else {
326  i = DC1394_ISO_SPEED_400;
327  }
328 
329  for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
330  res=dc1394_video_set_iso_speed(dc1394->camera, i);
331  }
332  if (res != DC1394_SUCCESS) {
333  av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
334  goto out_camera;
335  }
336 
337  if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
338  av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
339  goto out_camera;
340  }
341 
342  if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
343  av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
344  goto out_camera;
345  }
346  if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
347  av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
348  goto out_camera;
349  }
350 
351  if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
352  av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
353  goto out_camera;
354  }
355  return 0;
356 
357 out_camera:
358  dc1394_capture_stop(dc1394->camera);
359  dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
360  dc1394_camera_free (dc1394->camera);
361 out:
362  dc1394_free(dc1394->d);
363  return -1;
364 }
365 
366 static int dc1394_v2_read_packet(AVFormatContext *c, AVPacket *pkt)
367 {
368  struct dc1394_data *dc1394 = c->priv_data;
369  int res;
370 
371  /* discard stale frame */
372  if (dc1394->current_frame++) {
373  if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
374  av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
375  }
376 
377  res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
378  if (res == DC1394_SUCCESS) {
379  dc1394->packet.data = (uint8_t *) dc1394->frame->image;
380  dc1394->packet.pts = dc1394->current_frame * 1000000 / dc1394->frame_rate;
381  res = dc1394->frame->image_bytes;
382  } else {
383  av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
384  dc1394->packet.data = NULL;
385  res = -1;
386  }
387 
388  *pkt = dc1394->packet;
389  return res;
390 }
391 
392 static int dc1394_v2_close(AVFormatContext * context)
393 {
394  struct dc1394_data *dc1394 = context->priv_data;
395 
396  dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
397  dc1394_capture_stop(dc1394->camera);
398  dc1394_camera_free(dc1394->camera);
399  dc1394_free(dc1394->d);
400 
401  return 0;
402 }
403 
404 AVInputFormat ff_libdc1394_demuxer = {
405  .name = "libdc1394",
406  .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
407  .priv_data_size = sizeof(struct dc1394_data),
408  .read_header = dc1394_v2_read_header,
409  .read_packet = dc1394_v2_read_packet,
410  .read_close = dc1394_v2_close,
411  .flags = AVFMT_NOFILE,
412  .priv_class = &libdc1394_class,
413 };
414 
415 #endif
416 #if HAVE_LIBDC1394_1
417 AVInputFormat ff_libdc1394_demuxer = {
418  .name = "libdc1394",
419  .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.1 A/V grab"),
420  .priv_data_size = sizeof(struct dc1394_data),
421  .read_header = dc1394_v1_read_header,
422  .read_packet = dc1394_v1_read_packet,
423  .read_close = dc1394_v1_close,
424  .flags = AVFMT_NOFILE,
425  .priv_class = &libdc1394_class,
426 };
427 #endif
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:83
#define NULL
Definition: coverity.c:32
static int dc1394_read_common(AVFormatContext *c, const struct dc1394_frame_format **select_fmt, const struct dc1394_frame_rate **select_fps)
Definition: libdc1394.c:124
static enum AVPixelFormat pix_fmt
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:174
AVOption.
Definition: opt.h:245
int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
Parse str and put in width_ptr and height_ptr the detected values.
Definition: parseutils.c:143
const char * fmt
Definition: avisynth_c.h:632
misc image utilities
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1597
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4149
int num
numerator
Definition: rational.h:44
int index
stream index in AVFormatContext
Definition: avformat.h:878
static const struct dc1394_frame_format dc1394_frame_formats[]
int size
Definition: avcodec.h:1468
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1752
static AVPacket pkt
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1661
Format I/O context.
Definition: avformat.h:1314
char * framerate
Set by a private option.
Definition: libdc1394.c:70
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
uint8_t
AVOptions.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3805
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1467
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1499
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters...
Definition: imgutils.c:361
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
static const struct dc1394_frame_rate dc1394_frame_rates[]
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1473
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:896
common internal API header
char filename[1024]
input or output filename
Definition: avformat.h:1390
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
#define OFFSET(x)
Definition: libdc1394.c:103
int width
picture width / height.
Definition: avcodec.h:1711
enum AVPixelFormat pix_fmt
Definition: libdc1394.c:78
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:638
FILE * out
Definition: movenc-test.c:54
Stream structure.
Definition: avformat.h:877
char * pixel_format
Set by a private option.
Definition: libdc1394.c:69
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
enum AVMediaType codec_type
Definition: avcodec.h:1540
enum AVCodecID codec_id
Definition: avcodec.h:1549
static const AVOption options[]
Definition: libdc1394.c:105
static const AVClass libdc1394_class
Definition: libdc1394.c:115
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
Describe the class of an AVClass context structure.
Definition: log.h:67
rational number numerator/denominator
Definition: rational.h:43
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:276
misc parsing utilities
Main libavformat public API header.
int frame_rate
frames per 1000 seconds (fps * 1000)
Definition: libdc1394.c:67
Y , 8bpp.
Definition: pixfmt.h:71
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:477
char * video_size
String describing video size, set by a private option.
Definition: libdc1394.c:68
static double c[64]
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
int den
denominator
Definition: rational.h:45
void * priv_data
Format private data.
Definition: avformat.h:1342
#define DEC
Definition: libdc1394.c:104
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:661
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2090
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2078
int stream_index
Definition: avcodec.h:1469
AVPacket packet
Definition: libdc1394.c:72
packed YUV 4:1:1, 12bpp, Cb Y0 Y1 Cr Y2 Y3
Definition: pixfmt.h:84
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1444
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1460
int current_frame
Definition: libdc1394.c:66
static int width