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 
72  int size;
74 } dc1394_data;
75 
76 static const struct dc1394_frame_format {
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 
89 static const struct dc1394_frame_rate {
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,
122 };
123 
124 
126  const struct dc1394_frame_format **select_fmt, const struct dc1394_frame_rate **select_fps)
127 {
128  dc1394_data* dc1394 = c->priv_data;
129  AVStream* vst;
130  const struct dc1394_frame_format *fmt;
131  const struct dc1394_frame_rate *fps;
132  enum AVPixelFormat pix_fmt;
133  int width, height;
134  AVRational framerate;
135  int ret = 0;
136 
137  if ((pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == AV_PIX_FMT_NONE) {
138  av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format);
139  ret = AVERROR(EINVAL);
140  goto out;
141  }
142 
143  if ((ret = av_parse_video_size(&width, &height, dc1394->video_size)) < 0) {
144  av_log(c, AV_LOG_ERROR, "Could not parse video size '%s'.\n", dc1394->video_size);
145  goto out;
146  }
147  if ((ret = av_parse_video_rate(&framerate, dc1394->framerate)) < 0) {
148  av_log(c, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", dc1394->framerate);
149  goto out;
150  }
151  dc1394->frame_rate = av_rescale(1000, framerate.num, framerate.den);
152 
153  for (fmt = dc1394_frame_formats; fmt->width; fmt++)
154  if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
155  break;
156 
157  for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
158  if (fps->frame_rate == dc1394->frame_rate)
159  break;
160 
161  if (!fps->frame_rate || !fmt->width) {
162  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),
163  width, height, dc1394->frame_rate);
164  ret = AVERROR(EINVAL);
165  goto out;
166  }
167 
168  /* create a video stream */
169  vst = avformat_new_stream(c, NULL);
170  if (!vst) {
171  ret = AVERROR(ENOMEM);
172  goto out;
173  }
174  avpriv_set_pts_info(vst, 64, 1, 1000);
177  vst->codecpar->width = fmt->width;
178  vst->codecpar->height = fmt->height;
179  vst->codecpar->format = fmt->pix_fmt;
180  vst->avg_frame_rate = framerate;
181 
182  dc1394->current_frame = 0;
183  dc1394->stream_index = vst->index;
184  dc1394->size = av_image_get_buffer_size(fmt->pix_fmt,
185  fmt->width, fmt->height, 1);
186 
187  vst->codecpar->bit_rate = av_rescale(dc1394->size * 8,
188  fps->frame_rate, 1000);
189  *select_fps = fps;
190  *select_fmt = fmt;
191 out:
192  return ret;
193 }
194 
195 #if HAVE_LIBDC1394_1
196 static int dc1394_v1_read_header(AVFormatContext *c)
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 
205  if (dc1394_read_common(c, &fmt, &fps) != 0)
206  return -1;
207 
208  /* Now let us prep the hardware. */
209  dc1394->handle = dc1394_create_handle(0); /* FIXME: gotta have ap->port */
210  if (!dc1394->handle) {
211  av_log(c, AV_LOG_ERROR, "Can't acquire dc1394 handle on port %d\n", 0 /* ap->port */);
212  goto out;
213  }
214  camera_nodes = dc1394_get_camera_nodes(dc1394->handle, &res, 1);
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);
217  goto out_handle;
218  }
219  res = dc1394_dma_setup_capture(dc1394->handle, camera_nodes[dc1394->channel],
220  0,
221  FORMAT_VGA_NONCOMPRESSED,
222  fmt->frame_size_id,
223  SPEED_400,
224  fps->frame_rate_id, 8, 1,
225  c->filename,
226  &dc1394->camera);
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");
230  goto out_handle;
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");
236  goto out_handle_dma;
237  }
238 
239  return 0;
240 
241 out_handle_dma:
242  dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
243  dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
244 out_handle:
245  dc1394_destroy_handle(dc1394->handle);
246 out:
247  return -1;
248 }
249 
250 static int dc1394_v1_read_packet(AVFormatContext *c, AVPacket *pkt)
251 {
252  struct dc1394_data *dc1394 = c->priv_data;
253  int res;
254 
255  /* discard stale frame */
256  if (dc1394->current_frame++) {
257  if (dc1394_dma_done_with_buffer(&dc1394->camera) != DC1394_SUCCESS)
258  av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
259  }
260 
261  res = dc1394_dma_single_capture(&dc1394->camera);
262 
263  if (res == DC1394_SUCCESS) {
264  pkt->data = (uint8_t *)dc1394->camera.capture_buffer;
265  pkt->size = dc1394->size;
266  pkt->pts = (dc1394->current_frame * 1000000) / dc1394->frame_rate;
267  pkt->flags |= AV_PKT_FLAG_KEY;
268  pkt->stream_index = dc1394->stream_index;
269  } else {
270  av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
271  return AVERROR_INVALIDDATA;
272  }
273 
274  return pkt->size;
275 }
276 
277 static int dc1394_v1_close(AVFormatContext * context)
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);
285 
286  return 0;
287 }
288 
289 #elif HAVE_LIBDC1394_2
290 static int dc1394_v2_read_header(AVFormatContext *c)
291 {
292  dc1394_data* dc1394 = c->priv_data;
293  dc1394camera_list_t *list;
294  int res, i;
295  const struct dc1394_frame_format *fmt = NULL;
296  const struct dc1394_frame_rate *fps = NULL;
297 
298  if (dc1394_read_common(c, &fmt, &fps) != 0)
299  return -1;
300 
301  /* Now let us prep the hardware. */
302  dc1394->d = dc1394_new();
303  if (dc1394_camera_enumerate(dc1394->d, &list) != DC1394_SUCCESS || !list) {
304  av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera.\n");
305  goto out;
306  }
307 
308  if (list->num == 0) {
309  av_log(c, AV_LOG_ERROR, "No cameras found.\n");
310  dc1394_camera_free_list(list);
311  goto out;
312  }
313 
314  /* FIXME: To select a specific camera I need to search in list its guid */
315  dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
316  if (list->num > 1) {
317  av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
318  }
319 
320  /* Freeing list of cameras */
321  dc1394_camera_free_list (list);
322 
323  /* Select MAX Speed possible from the cam */
324  if (dc1394->camera->bmode_capable>0) {
325  dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
326  i = DC1394_ISO_SPEED_800;
327  } else {
328  i = DC1394_ISO_SPEED_400;
329  }
330 
331  for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
332  res=dc1394_video_set_iso_speed(dc1394->camera, i);
333  }
334  if (res != DC1394_SUCCESS) {
335  av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
336  goto out_camera;
337  }
338 
339  if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
340  av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
341  goto out_camera;
342  }
343 
344  if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
345  av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
346  goto out_camera;
347  }
348  if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
349  av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
350  goto out_camera;
351  }
352 
353  if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
354  av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
355  goto out_camera;
356  }
357  return 0;
358 
359 out_camera:
360  dc1394_capture_stop(dc1394->camera);
361  dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
362  dc1394_camera_free (dc1394->camera);
363 out:
364  dc1394_free(dc1394->d);
365  return -1;
366 }
367 
368 static int dc1394_v2_read_packet(AVFormatContext *c, AVPacket *pkt)
369 {
370  struct dc1394_data *dc1394 = c->priv_data;
371  int res;
372 
373  /* discard stale frame */
374  if (dc1394->current_frame++) {
375  if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
376  av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
377  }
378 
379  res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
380  if (res == DC1394_SUCCESS) {
381  pkt->data = (uint8_t *)dc1394->frame->image;
382  pkt->size = dc1394->frame->image_bytes;
383  pkt->pts = dc1394->current_frame * 1000000 / dc1394->frame_rate;
384  pkt->flags |= AV_PKT_FLAG_KEY;
385  pkt->stream_index = dc1394->stream_index;
386  } else {
387  av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
388  return AVERROR_INVALIDDATA;
389  }
390 
391  return pkt->size;
392 }
393 
394 static int dc1394_v2_close(AVFormatContext * context)
395 {
396  struct dc1394_data *dc1394 = context->priv_data;
397 
398  dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
399  dc1394_capture_stop(dc1394->camera);
400  dc1394_camera_free(dc1394->camera);
401  dc1394_free(dc1394->d);
402 
403  return 0;
404 }
405 
406 AVInputFormat ff_libdc1394_demuxer = {
407  .name = "libdc1394",
408  .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
409  .priv_data_size = sizeof(struct dc1394_data),
410  .read_header = dc1394_v2_read_header,
411  .read_packet = dc1394_v2_read_packet,
412  .read_close = dc1394_v2_close,
413  .flags = AVFMT_NOFILE,
414  .priv_class = &libdc1394_class,
415 };
416 
417 #endif
418 #if HAVE_LIBDC1394_1
419 AVInputFormat ff_libdc1394_demuxer = {
420  .name = "libdc1394",
421  .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.1 A/V grab"),
422  .priv_data_size = sizeof(struct dc1394_data),
423  .read_header = dc1394_v1_read_header,
424  .read_packet = dc1394_v1_read_packet,
425  .read_close = dc1394_v1_close,
426  .flags = AVFMT_NOFILE,
427  .priv_class = &libdc1394_class,
428 };
429 #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:125
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
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:179
AVOption.
Definition: opt.h:246
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:148
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:4603
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4026
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:1617
static AVPacket pkt
Format I/O context.
Definition: avformat.h:1341
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:4092
AVOptions.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4233
static AVFrame * frame
#define height
uint8_t * data
Definition: avcodec.h:1616
#define av_log(a,...)
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:4055
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1648
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:380
#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:180
static const struct dc1394_frame_rate dc1394_frame_rates[]
int stream_index
Definition: libdc1394.c:73
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4022
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:970
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1622
common internal API header
char filename[1024]
input or output filename
Definition: avformat.h:1417
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:104
#define width
enum AVPixelFormat pix_fmt
Definition: libdc1394.c:79
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:106
static const AVClass libdc1394_class
Definition: libdc1394.c:116
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:277
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]
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
int den
Denominator.
Definition: rational.h:60
void * priv_data
Format private data.
Definition: avformat.h:1369
#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:664
AVCodecParameters * codecpar
Definition: avformat.h:1244
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2260
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:2248
int stream_index
Definition: avcodec.h:1618
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:1593
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1609
int current_frame
Definition: libdc1394.c:66