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->codecpar->width = fmt->width;
177  vst->codecpar->height = fmt->height;
178  vst->codecpar->format = fmt->pix_fmt;
179  vst->avg_frame_rate = framerate;
180 
181  /* packet init */
182  av_init_packet(&dc1394->packet);
184  fmt->width, fmt->height, 1);
185  dc1394->packet.stream_index = vst->index;
186  dc1394->packet.flags |= AV_PKT_FLAG_KEY;
187 
188  dc1394->current_frame = 0;
189 
190  vst->codecpar->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000);
191  *select_fps = fps;
192  *select_fmt = fmt;
193 out:
194  return ret;
195 }
196 
197 #if HAVE_LIBDC1394_1
198 static int dc1394_v1_read_header(AVFormatContext *c)
199 {
200  dc1394_data* dc1394 = c->priv_data;
201  AVStream* vst;
202  nodeid_t* camera_nodes;
203  int res;
204  struct dc1394_frame_format *fmt = NULL;
205  struct dc1394_frame_rate *fps = NULL;
206 
207  if (dc1394_read_common(c, &fmt, &fps) != 0)
208  return -1;
209 
210  /* Now let us prep the hardware. */
211  dc1394->handle = dc1394_create_handle(0); /* FIXME: gotta have ap->port */
212  if (!dc1394->handle) {
213  av_log(c, AV_LOG_ERROR, "Can't acquire dc1394 handle on port %d\n", 0 /* ap->port */);
214  goto out;
215  }
216  camera_nodes = dc1394_get_camera_nodes(dc1394->handle, &res, 1);
217  if (!camera_nodes || camera_nodes[dc1394->channel] == DC1394_NO_CAMERA) {
218  av_log(c, AV_LOG_ERROR, "There's no IIDC camera on the channel %d\n", dc1394->channel);
219  goto out_handle;
220  }
221  res = dc1394_dma_setup_capture(dc1394->handle, camera_nodes[dc1394->channel],
222  0,
223  FORMAT_VGA_NONCOMPRESSED,
224  fmt->frame_size_id,
225  SPEED_400,
226  fps->frame_rate_id, 8, 1,
227  c->filename,
228  &dc1394->camera);
229  dc1394_free_camera_nodes(camera_nodes);
230  if (res != DC1394_SUCCESS) {
231  av_log(c, AV_LOG_ERROR, "Can't prepare camera for the DMA capture\n");
232  goto out_handle;
233  }
234 
235  res = dc1394_start_iso_transmission(dc1394->handle, dc1394->camera.node);
236  if (res != DC1394_SUCCESS) {
237  av_log(c, AV_LOG_ERROR, "Can't start isochronous transmission\n");
238  goto out_handle_dma;
239  }
240 
241  return 0;
242 
243 out_handle_dma:
244  dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
245  dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
246 out_handle:
247  dc1394_destroy_handle(dc1394->handle);
248 out:
249  return -1;
250 }
251 
252 static int dc1394_v1_read_packet(AVFormatContext *c, AVPacket *pkt)
253 {
254  struct dc1394_data *dc1394 = c->priv_data;
255  int res;
256 
257  /* discard stale frame */
258  if (dc1394->current_frame++) {
259  if (dc1394_dma_done_with_buffer(&dc1394->camera) != DC1394_SUCCESS)
260  av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
261  }
262 
263  res = dc1394_dma_single_capture(&dc1394->camera);
264 
265  if (res == DC1394_SUCCESS) {
266  dc1394->packet.data = (uint8_t *)(dc1394->camera.capture_buffer);
267  dc1394->packet.pts = (dc1394->current_frame * 1000000) / dc1394->frame_rate;
268  res = dc1394->packet.size;
269  } else {
270  av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
271  dc1394->packet.data = NULL;
272  res = -1;
273  }
274 
275  *pkt = dc1394->packet;
276  return res;
277 }
278 
279 static int dc1394_v1_close(AVFormatContext * context)
280 {
281  struct dc1394_data *dc1394 = context->priv_data;
282 
283  dc1394_stop_iso_transmission(dc1394->handle, dc1394->camera.node);
284  dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
285  dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
286  dc1394_destroy_handle(dc1394->handle);
287 
288  return 0;
289 }
290 
291 #elif HAVE_LIBDC1394_2
292 static int dc1394_v2_read_header(AVFormatContext *c)
293 {
294  dc1394_data* dc1394 = c->priv_data;
295  dc1394camera_list_t *list;
296  int res, i;
297  const struct dc1394_frame_format *fmt = NULL;
298  const struct dc1394_frame_rate *fps = NULL;
299 
300  if (dc1394_read_common(c, &fmt, &fps) != 0)
301  return -1;
302 
303  /* Now let us prep the hardware. */
304  dc1394->d = dc1394_new();
305  if (dc1394_camera_enumerate(dc1394->d, &list) != DC1394_SUCCESS || !list) {
306  av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera.\n");
307  goto out;
308  }
309 
310  if (list->num == 0) {
311  av_log(c, AV_LOG_ERROR, "No cameras found.\n");
312  dc1394_camera_free_list(list);
313  goto out;
314  }
315 
316  /* FIXME: To select a specific camera I need to search in list its guid */
317  dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
318  if (list->num > 1) {
319  av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
320  }
321 
322  /* Freeing list of cameras */
323  dc1394_camera_free_list (list);
324 
325  /* Select MAX Speed possible from the cam */
326  if (dc1394->camera->bmode_capable>0) {
327  dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
328  i = DC1394_ISO_SPEED_800;
329  } else {
330  i = DC1394_ISO_SPEED_400;
331  }
332 
333  for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
334  res=dc1394_video_set_iso_speed(dc1394->camera, i);
335  }
336  if (res != DC1394_SUCCESS) {
337  av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
338  goto out_camera;
339  }
340 
341  if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
342  av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
343  goto out_camera;
344  }
345 
346  if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
347  av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
348  goto out_camera;
349  }
350  if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
351  av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
352  goto out_camera;
353  }
354 
355  if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
356  av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
357  goto out_camera;
358  }
359  return 0;
360 
361 out_camera:
362  dc1394_capture_stop(dc1394->camera);
363  dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
364  dc1394_camera_free (dc1394->camera);
365 out:
366  dc1394_free(dc1394->d);
367  return -1;
368 }
369 
370 static int dc1394_v2_read_packet(AVFormatContext *c, AVPacket *pkt)
371 {
372  struct dc1394_data *dc1394 = c->priv_data;
373  int res;
374 
375  /* discard stale frame */
376  if (dc1394->current_frame++) {
377  if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
378  av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
379  }
380 
381  res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
382  if (res == DC1394_SUCCESS) {
383  dc1394->packet.data = (uint8_t *) dc1394->frame->image;
384  dc1394->packet.pts = dc1394->current_frame * 1000000 / dc1394->frame_rate;
385  res = dc1394->frame->image_bytes;
386  } else {
387  av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
388  dc1394->packet.data = NULL;
389  res = -1;
390  }
391 
392  *pkt = dc1394->packet;
393  return res;
394 }
395 
396 static int dc1394_v2_close(AVFormatContext * context)
397 {
398  struct dc1394_data *dc1394 = context->priv_data;
399 
400  dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
401  dc1394_capture_stop(dc1394->camera);
402  dc1394_camera_free(dc1394->camera);
403  dc1394_free(dc1394->d);
404 
405  return 0;
406 }
407 
408 AVInputFormat ff_libdc1394_demuxer = {
409  .name = "libdc1394",
410  .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
411  .priv_data_size = sizeof(struct dc1394_data),
412  .read_header = dc1394_v2_read_header,
413  .read_packet = dc1394_v2_read_packet,
414  .read_close = dc1394_v2_close,
415  .flags = AVFMT_NOFILE,
416  .priv_class = &libdc1394_class,
417 };
418 
419 #endif
420 #if HAVE_LIBDC1394_1
421 AVInputFormat ff_libdc1394_demuxer = {
422  .name = "libdc1394",
423  .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.1 A/V grab"),
424  .priv_data_size = sizeof(struct dc1394_data),
425  .read_header = dc1394_v1_read_header,
426  .read_packet = dc1394_v1_read_packet,
427  .read_close = dc1394_v1_close,
428  .flags = AVFMT_NOFILE,
429  .priv_class = &libdc1394_class,
430 };
431 #endif
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:82
#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:769
misc image utilities
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
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:4560
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3980
int num
Numerator.
Definition: rational.h:59
int index
stream index in AVFormatContext
Definition: avformat.h:890
static const struct dc1394_frame_format dc1394_frame_formats[]
int size
Definition: avcodec.h:1602
static AVPacket pkt
Format I/O context.
Definition: avformat.h:1338
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
int width
Video only.
Definition: avcodec.h:4046
AVOptions.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4193
static AVFrame * frame
#define height
uint8_t * data
Definition: avcodec.h:1601
#define av_log(a,...)
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:4009
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1633
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[]
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3976
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:967
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1607
common internal API header
char filename[1024]
input or output filename
Definition: avformat.h:1414
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
#define width
enum AVPixelFormat pix_fmt
Definition: libdc1394.c:78
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:514
Stream structure.
Definition: avformat.h:889
char * pixel_format
Set by a private option.
Definition: libdc1394.c:69
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static const AVOption options[]
Definition: libdc1394.c:105
static const AVClass libdc1394_class
Definition: libdc1394.c:115
Describe the class of an AVClass context structure.
Definition: log.h:67
Rational number (pair of numerator and denominator).
Definition: rational.h:58
#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:70
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:478
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:60
void * priv_data
Format private data.
Definition: avformat.h:1366
#define DEC
Definition: libdc1394.c:104
FILE * out
Definition: movenc.c:54
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
AVCodecParameters * codecpar
Definition: avformat.h:1241
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2194
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:2182
int stream_index
Definition: avcodec.h:1603
AVPacket packet
Definition: libdc1394.c:72
packed YUV 4:1:1, 12bpp, Cb Y0 Y1 Cr Y2 Y3
Definition: pixfmt.h:83
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1578
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1594
int current_frame
Definition: libdc1394.c:66