Libav
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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;
68  char *video_size;
69  char *pixel_format;
70  char *framerate;
72  int size;
74 } dc1394_data;
75 
77  int width;
78  int height;
82  { 320, 240, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_320x240_YUV422 },
83  { 640, 480, AV_PIX_FMT_GRAY8, DC1394_VIDEO_MODE_640x480_MONO8 },
84  { 640, 480, AV_PIX_FMT_UYYVYY411, DC1394_VIDEO_MODE_640x480_YUV411 },
85  { 640, 480, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_640x480_YUV422 },
86  { 0, 0, 0, 0 } /* gotta be the last one */
87 };
88 
92 } dc1394_frame_rates[] = {
93  { 1875, DC1394_FRAMERATE_1_875 },
94  { 3750, DC1394_FRAMERATE_3_75 },
95  { 7500, DC1394_FRAMERATE_7_5 },
96  { 15000, DC1394_FRAMERATE_15 },
97  { 30000, DC1394_FRAMERATE_30 },
98  { 60000, DC1394_FRAMERATE_60 },
99  {120000, DC1394_FRAMERATE_120 },
100  {240000, DC1394_FRAMERATE_240 },
101  { 0, 0 } /* gotta be the last one */
102 };
103 
104 #define OFFSET(x) offsetof(dc1394_data, x)
105 #define DEC AV_OPT_FLAG_DECODING_PARAM
106 static const AVOption options[] = {
107 #if HAVE_LIBDC1394_1
108  { "channel", "", offsetof(dc1394_data, channel), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
109 #endif
110  { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "qvga"}, 0, 0, DEC },
111  { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "uyvy422"}, 0, 0, DEC },
112  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
113  { NULL },
114 };
115 
116 static const AVClass libdc1394_class = {
117  .class_name = "libdc1394 indev",
118  .item_name = av_default_item_name,
119  .option = options,
120  .version = LIBAVUTIL_VERSION_INT,
121 };
122 
123 
124 static inline int dc1394_read_common(AVFormatContext *c,
125  struct dc1394_frame_format **select_fmt, struct dc1394_frame_rate **select_fps)
126 {
127  dc1394_data* dc1394 = c->priv_data;
128  AVStream* vst;
129  struct dc1394_frame_format *fmt;
130  struct dc1394_frame_rate *fps;
131  enum AVPixelFormat pix_fmt;
132  int width, height;
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  dc1394->current_frame = 0;
182  dc1394->stream_index = vst->index;
183  dc1394->size = av_image_get_buffer_size(fmt->pix_fmt,
184  fmt->width, fmt->height, 1);
185 
186  vst->codecpar->bit_rate = av_rescale(dc1394->size * 8,
187  fps->frame_rate, 1000);
188  *select_fps = fps;
189  *select_fmt = fmt;
190 out:
191  return ret;
192 }
193 
194 #if HAVE_LIBDC1394_1
195 static int dc1394_v1_read_header(AVFormatContext *c)
196 {
197  dc1394_data* dc1394 = c->priv_data;
198  AVStream* vst;
199  nodeid_t* camera_nodes;
200  int res;
201  struct dc1394_frame_format *fmt = NULL;
202  struct dc1394_frame_rate *fps = NULL;
203 
204  if (dc1394_read_common(c, &fmt, &fps) != 0)
205  return -1;
206 
207  /* Now let us prep the hardware. */
208  dc1394->handle = dc1394_create_handle(0); /* FIXME: gotta have ap->port */
209  if (!dc1394->handle) {
210  av_log(c, AV_LOG_ERROR, "Can't acquire dc1394 handle on port %d\n", 0 /* ap->port */);
211  goto out;
212  }
213  camera_nodes = dc1394_get_camera_nodes(dc1394->handle, &res, 1);
214  if (!camera_nodes || camera_nodes[dc1394->channel] == DC1394_NO_CAMERA) {
215  av_log(c, AV_LOG_ERROR, "There's no IIDC camera on the channel %d\n", dc1394->channel);
216  goto out_handle;
217  }
218  res = dc1394_dma_setup_capture(dc1394->handle, camera_nodes[dc1394->channel],
219  0,
220  FORMAT_VGA_NONCOMPRESSED,
221  fmt->frame_size_id,
222  SPEED_400,
223  fps->frame_rate_id, 8, 1,
224  c->filename,
225  &dc1394->camera);
226  dc1394_free_camera_nodes(camera_nodes);
227  if (res != DC1394_SUCCESS) {
228  av_log(c, AV_LOG_ERROR, "Can't prepare camera for the DMA capture\n");
229  goto out_handle;
230  }
231 
232  res = dc1394_start_iso_transmission(dc1394->handle, dc1394->camera.node);
233  if (res != DC1394_SUCCESS) {
234  av_log(c, AV_LOG_ERROR, "Can't start isochronous transmission\n");
235  goto out_handle_dma;
236  }
237 
238  return 0;
239 
240 out_handle_dma:
241  dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
242  dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
243 out_handle:
244  dc1394_destroy_handle(dc1394->handle);
245 out:
246  return -1;
247 }
248 
249 static int dc1394_v1_read_packet(AVFormatContext *c, AVPacket *pkt)
250 {
251  struct dc1394_data *dc1394 = c->priv_data;
252  int res;
253 
254  /* discard stale frame */
255  if (dc1394->current_frame++) {
256  if (dc1394_dma_done_with_buffer(&dc1394->camera) != DC1394_SUCCESS)
257  av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
258  }
259 
260  res = dc1394_dma_single_capture(&dc1394->camera);
261 
262  if (res == DC1394_SUCCESS) {
263  pkt->data = (uint8_t *)dc1394->camera.capture_buffer;
264  pkt->size = dc1394->size;
265  pkt->pts = (dc1394->current_frame * 1000000) / dc1394->frame_rate;
266  pkt->flags |= AV_PKT_FLAG_KEY;
267  pkt->stream_index = dc1394->stream_index;
268  } else {
269  av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
270  return AVERROR_INVALIDDATA;
271  }
272 
273  return pkt->size;
274 }
275 
276 static int dc1394_v1_close(AVFormatContext * context)
277 {
278  struct dc1394_data *dc1394 = context->priv_data;
279 
280  dc1394_stop_iso_transmission(dc1394->handle, dc1394->camera.node);
281  dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
282  dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
283  dc1394_destroy_handle(dc1394->handle);
284 
285  return 0;
286 }
287 
288 #elif HAVE_LIBDC1394_2
289 static int dc1394_v2_read_header(AVFormatContext *c)
290 {
291  dc1394_data* dc1394 = c->priv_data;
292  dc1394camera_list_t *list;
293  int res, i;
294  struct dc1394_frame_format *fmt = NULL;
295  struct dc1394_frame_rate *fps = NULL;
296 
297  if (dc1394_read_common(c, &fmt, &fps) != 0)
298  return -1;
299 
300  /* Now let us prep the hardware. */
301  dc1394->d = dc1394_new();
302  dc1394_camera_enumerate (dc1394->d, &list);
303  if ( !list || list->num == 0) {
304  av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n");
305  goto out;
306  }
307 
308  /* FIXME: To select a specific camera I need to search in list its guid */
309  dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
310  if (list->num > 1) {
311  av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
312  }
313 
314  /* Freeing list of cameras */
315  dc1394_camera_free_list (list);
316 
317  /* Select MAX Speed possible from the cam */
318  if (dc1394->camera->bmode_capable>0) {
319  dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
320  i = DC1394_ISO_SPEED_800;
321  } else {
322  i = DC1394_ISO_SPEED_400;
323  }
324 
325  for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
326  res=dc1394_video_set_iso_speed(dc1394->camera, i);
327  }
328  if (res != DC1394_SUCCESS) {
329  av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
330  goto out_camera;
331  }
332 
333  if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
334  av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
335  goto out_camera;
336  }
337 
338  if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
339  av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
340  goto out_camera;
341  }
342  if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
343  av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
344  goto out_camera;
345  }
346 
347  if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
348  av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
349  goto out_camera;
350  }
351  return 0;
352 
353 out_camera:
354  dc1394_capture_stop(dc1394->camera);
355  dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
356  dc1394_camera_free (dc1394->camera);
357 out:
358  dc1394_free(dc1394->d);
359  return -1;
360 }
361 
362 static int dc1394_v2_read_packet(AVFormatContext *c, AVPacket *pkt)
363 {
364  struct dc1394_data *dc1394 = c->priv_data;
365  int res;
366 
367  /* discard stale frame */
368  if (dc1394->current_frame++) {
369  if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
370  av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
371  }
372 
373  res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
374  if (res == DC1394_SUCCESS) {
375  pkt->data = (uint8_t *)dc1394->frame->image;
376  pkt->size = dc1394->frame->image_bytes;
377  pkt->pts = dc1394->current_frame * 1000000 / dc1394->frame_rate;
378  pkt->flags |= AV_PKT_FLAG_KEY;
379  pkt->stream_index = dc1394->stream_index;
380  } else {
381  av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
382  return AVERROR_INVALIDDATA;
383  }
384 
385  return pkt->size;
386 }
387 
388 static int dc1394_v2_close(AVFormatContext * context)
389 {
390  struct dc1394_data *dc1394 = context->priv_data;
391 
392  dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
393  dc1394_capture_stop(dc1394->camera);
394  dc1394_camera_free(dc1394->camera);
395  dc1394_free(dc1394->d);
396 
397  return 0;
398 }
399 
400 AVInputFormat ff_libdc1394_demuxer = {
401  .name = "libdc1394",
402  .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
403  .priv_data_size = sizeof(struct dc1394_data),
404  .read_header = dc1394_v2_read_header,
405  .read_packet = dc1394_v2_read_packet,
406  .read_close = dc1394_v2_close,
407  .flags = AVFMT_NOFILE,
408  .priv_class = &libdc1394_class,
409 };
410 
411 #endif
412 #if HAVE_LIBDC1394_1
413 AVInputFormat ff_libdc1394_demuxer = {
414  .name = "libdc1394",
415  .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.1 A/V grab"),
416  .priv_data_size = sizeof(struct dc1394_data),
417  .read_header = dc1394_v1_read_header,
418  .read_packet = dc1394_v1_read_packet,
419  .read_close = dc1394_v1_close,
420  .flags = AVFMT_NOFILE,
421  .priv_class = &libdc1394_class,
422 };
423 #endif
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:78
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:127
AVOption.
Definition: opt.h:234
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:100
misc image utilities
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:2986
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
int num
numerator
Definition: rational.h:44
int index
stream index in AVFormatContext
Definition: avformat.h:706
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
struct dc1394_frame_rate dc1394_frame_rates[]
Format I/O context.
Definition: avformat.h:940
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:39
uint8_t
int width
Video only.
Definition: avcodec.h:3525
AVOptions.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
uint8_t * data
Definition: avcodec.h:1346
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
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:323
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#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:148
int stream_index
Definition: libdc1394.c:73
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:781
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
int bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3512
common internal API header
char filename[1024]
input or output filename
Definition: avformat.h:1016
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:86
#define OFFSET(x)
Definition: libdc1394.c:104
enum AVPixelFormat pix_fmt
Definition: libdc1394.c:79
enum AVPixelFormat pix_fmt
Definition: movenc.c:853
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
Stream structure.
Definition: avformat.h:705
NULL
Definition: eval.c:55
char * pixel_format
Set by a private option.
Definition: libdc1394.c:69
static int width
Definition: utils.c:156
#define AV_LOG_INFO
Standard information.
Definition: log.h:135
av_default_item_name
Definition: dnxhdenc.c:55
static const AVOption options[]
Definition: libdc1394.c:106
static int dc1394_read_common(AVFormatContext *c, struct dc1394_frame_format **select_fmt, struct dc1394_frame_rate **select_fps)
Definition: libdc1394.c:124
static const AVClass libdc1394_class
Definition: libdc1394.c:116
Describe the class of an AVClass context structure.
Definition: log.h:34
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:265
misc parsing utilities
int height
Definition: gxfenc.c:72
Main libavformat public API header.
int frame_rate
frames per 1000 seconds (fps * 1000)
Definition: libdc1394.c:67
Y , 8bpp.
Definition: pixfmt.h:67
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:412
char * video_size
String describing video size, set by a private option.
Definition: libdc1394.c:68
int den
denominator
Definition: rational.h:45
struct dc1394_frame_format dc1394_frame_formats[]
void * priv_data
Format private data.
Definition: avformat.h:968
#define DEC
Definition: libdc1394.c:105
FILE * out
Definition: movenc.c:54
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:529
AVCodecParameters * codecpar
Definition: avformat.h:831
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:1716
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:1704
int stream_index
Definition: avcodec.h:1348
packed YUV 4:1:1, 12bpp, Cb Y0 Y1 Cr Y2 Y3
Definition: pixfmt.h:79
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
This structure stores compressed data.
Definition: avcodec.h:1323
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339
int current_frame
Definition: libdc1394.c:66