FFmpeg
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 <dc1394/dc1394.h>
24 
25 #include "libavutil/imgutils.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/log.h"
28 #include "libavutil/mathematics.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/parseutils.h"
31 #include "libavutil/pixdesc.h"
32 
33 #include "libavformat/avformat.h"
34 #include "libavformat/internal.h"
35 
36 typedef struct dc1394_data {
37  AVClass *class;
38  dc1394_t *d;
39  dc1394camera_t *camera;
40  dc1394video_frame_t *frame;
42  int frame_rate; /**< frames per 1000 seconds (fps * 1000) */
43  char *video_size; /**< String describing video size, set by a private option. */
44  char *pixel_format; /**< Set by a private option. */
45  char *framerate; /**< Set by a private option. */
46 
47  int size;
49 } dc1394_data;
50 
51 static const struct dc1394_frame_format {
52  int width;
53  int height;
57  { 320, 240, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_320x240_YUV422 },
58  { 640, 480, AV_PIX_FMT_GRAY8, DC1394_VIDEO_MODE_640x480_MONO8 },
59  { 640, 480, AV_PIX_FMT_UYYVYY411, DC1394_VIDEO_MODE_640x480_YUV411 },
60  { 640, 480, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_640x480_YUV422 },
61  { 0, 0, 0, 0 } /* gotta be the last one */
62 };
63 
64 static const struct dc1394_frame_rate {
67 } dc1394_frame_rates[] = {
68  { 1875, DC1394_FRAMERATE_1_875 },
69  { 3750, DC1394_FRAMERATE_3_75 },
70  { 7500, DC1394_FRAMERATE_7_5 },
71  { 15000, DC1394_FRAMERATE_15 },
72  { 30000, DC1394_FRAMERATE_30 },
73  { 60000, DC1394_FRAMERATE_60 },
74  {120000, DC1394_FRAMERATE_120 },
75  {240000, DC1394_FRAMERATE_240 },
76  { 0, 0 } /* gotta be the last one */
77 };
78 
79 #define OFFSET(x) offsetof(dc1394_data, x)
80 #define DEC AV_OPT_FLAG_DECODING_PARAM
81 static const AVOption options[] = {
82  { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "qvga"}, 0, 0, DEC },
83  { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "uyvy422"}, 0, 0, DEC },
84  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
85  { NULL },
86 };
87 
88 static const AVClass libdc1394_class = {
89  .class_name = "libdc1394 indev",
90  .item_name = av_default_item_name,
91  .option = options,
92  .version = LIBAVUTIL_VERSION_INT,
94 };
95 
96 
98  const struct dc1394_frame_format **select_fmt, const struct dc1394_frame_rate **select_fps)
99 {
100  dc1394_data* dc1394 = c->priv_data;
101  AVStream* vst;
102  const struct dc1394_frame_format *fmt;
103  const struct dc1394_frame_rate *fps;
104  enum AVPixelFormat pix_fmt;
105  int width, height;
107  int ret = 0;
108 
109  if ((pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == AV_PIX_FMT_NONE) {
110  av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format);
111  ret = AVERROR(EINVAL);
112  goto out;
113  }
114 
115  if ((ret = av_parse_video_size(&width, &height, dc1394->video_size)) < 0) {
116  av_log(c, AV_LOG_ERROR, "Could not parse video size '%s'.\n", dc1394->video_size);
117  goto out;
118  }
119  if ((ret = av_parse_video_rate(&framerate, dc1394->framerate)) < 0) {
120  av_log(c, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", dc1394->framerate);
121  goto out;
122  }
123  dc1394->frame_rate = av_rescale(1000, framerate.num, framerate.den);
124 
125  for (fmt = dc1394_frame_formats; fmt->width; fmt++)
126  if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
127  break;
128 
129  for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
130  if (fps->frame_rate == dc1394->frame_rate)
131  break;
132 
133  if (!fps->frame_rate || !fmt->width) {
134  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),
135  width, height, dc1394->frame_rate);
136  ret = AVERROR(EINVAL);
137  goto out;
138  }
139 
140  /* create a video stream */
141  vst = avformat_new_stream(c, NULL);
142  if (!vst) {
143  ret = AVERROR(ENOMEM);
144  goto out;
145  }
146  avpriv_set_pts_info(vst, 64, 1, 1000);
149  vst->codecpar->width = fmt->width;
150  vst->codecpar->height = fmt->height;
151  vst->codecpar->format = fmt->pix_fmt;
152  vst->avg_frame_rate = framerate;
153 
154  dc1394->current_frame = 0;
155  dc1394->stream_index = vst->index;
156  dc1394->size = av_image_get_buffer_size(fmt->pix_fmt,
157  fmt->width, fmt->height, 1);
158 
159  vst->codecpar->bit_rate = av_rescale(dc1394->size * 8,
160  fps->frame_rate, 1000);
161  *select_fps = fps;
162  *select_fmt = fmt;
163 out:
164  return ret;
165 }
166 
168 {
169  dc1394_data* dc1394 = c->priv_data;
170  dc1394camera_list_t *list;
171  int res, i;
172  const struct dc1394_frame_format *fmt = NULL;
173  const struct dc1394_frame_rate *fps = NULL;
174 
175  if (dc1394_read_common(c, &fmt, &fps) != 0)
176  return -1;
177 
178  /* Now let us prep the hardware. */
179  dc1394->d = dc1394_new();
180  if (dc1394_camera_enumerate(dc1394->d, &list) != DC1394_SUCCESS || !list) {
181  av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera.\n");
182  goto out;
183  }
184 
185  if (list->num == 0) {
186  av_log(c, AV_LOG_ERROR, "No cameras found.\n");
187  dc1394_camera_free_list(list);
188  goto out;
189  }
190 
191  /* FIXME: To select a specific camera I need to search in list its guid */
192  dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
193 
194  if (!dc1394->camera) {
195  av_log(c, AV_LOG_ERROR, "Unable to open camera with guid 0x%"PRIx64"\n",
196  list->ids[0].guid);
197  dc1394_camera_free_list(list);
198  goto out;
199  }
200 
201  if (list->num > 1) {
202  av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
203  }
204 
205  /* Freeing list of cameras */
206  dc1394_camera_free_list (list);
207 
208  /* Select MAX Speed possible from the cam */
209  if (dc1394->camera->bmode_capable>0) {
210  dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
211  i = DC1394_ISO_SPEED_800;
212  } else {
213  i = DC1394_ISO_SPEED_400;
214  }
215 
216  for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
217  res=dc1394_video_set_iso_speed(dc1394->camera, i);
218  }
219  if (res != DC1394_SUCCESS) {
220  av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
221  goto out_camera;
222  }
223 
224  if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
225  av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
226  goto out_camera;
227  }
228 
229  if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
230  av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
231  goto out_camera;
232  }
233  if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
234  av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
235  goto out_camera;
236  }
237 
238  if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
239  av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
240  goto out_camera;
241  }
242  return 0;
243 
244 out_camera:
245  dc1394_capture_stop(dc1394->camera);
246  dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
247  dc1394_camera_free (dc1394->camera);
248 out:
249  dc1394_free(dc1394->d);
250  return -1;
251 }
252 
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_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
261  av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
262  }
263 
264  res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
265  if (res == DC1394_SUCCESS) {
266  pkt->data = (uint8_t *)dc1394->frame->image;
267  pkt->size = dc1394->frame->image_bytes;
268  pkt->pts = dc1394->current_frame * 1000000 / dc1394->frame_rate;
270  pkt->stream_index = dc1394->stream_index;
271  } else {
272  av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
273  return AVERROR_INVALIDDATA;
274  }
275 
276  return pkt->size;
277 }
278 
280 {
281  struct dc1394_data *dc1394 = context->priv_data;
282 
283  dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
284  dc1394_capture_stop(dc1394->camera);
285  dc1394_camera_free(dc1394->camera);
286  dc1394_free(dc1394->d);
287 
288  return 0;
289 }
290 
292  .name = "libdc1394",
293  .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
294  .priv_data_size = sizeof(struct dc1394_data),
296  .read_packet = dc1394_read_packet,
297  .read_close = dc1394_close,
298  .flags = AVFMT_NOFILE,
299  .priv_class = &libdc1394_class,
300 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
dc1394_data::video_size
char * video_size
String describing video size, set by a private option.
Definition: libdc1394.c:43
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:768
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
out
FILE * out
Definition: movenc.c:54
AV_CODEC_ID_RAWVIDEO
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:63
pixdesc.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVOption
AVOption.
Definition: opt.h:247
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:1015
DEC
#define DEC
Definition: libdc1394.c:80
mathematics.h
dc1394_data::camera
dc1394camera_t * camera
Definition: libdc1394.c:39
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:428
dc1394_data::current_frame
int current_frame
Definition: libdc1394.c:41
dc1394_read_common
static int dc1394_read_common(AVFormatContext *c, const struct dc1394_frame_format **select_fmt, const struct dc1394_frame_rate **select_fps)
Definition: libdc1394.c:97
framerate
int framerate
Definition: h264_levels.c:65
dc1394_data::d
dc1394_t * d
Definition: libdc1394.c:38
dc1394_frame_format::width
int width
Definition: libdc1394.c:52
dc1394_frame_format
Definition: libdc1394.c:51
dc1394_frame_format::frame_size_id
int frame_size_id
Definition: libdc1394.c:55
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVInputFormat
Definition: avformat.h:650
dc1394_data
Definition: libdc1394.c:36
width
#define width
dc1394_frame_format::pix_fmt
enum AVPixelFormat pix_fmt
Definition: libdc1394.c:54
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:655
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
dc1394_data::frame_rate
int frame_rate
frames per 1000 seconds (fps * 1000)
Definition: libdc1394.c:42
dc1394_frame_rate
Definition: libdc1394.c:64
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demuxing_decoding.c:41
dc1394_frame_format::height
int height
Definition: libdc1394.c:53
context
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your context
Definition: writing_filters.txt:91
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
parseutils.h
list
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining list
Definition: filter_design.txt:25
AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT
@ AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT
Definition: log.h:41
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
dc1394_data::size
int size
Definition: libdc1394.c:47
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
dc1394_read_header
static int dc1394_read_header(AVFormatContext *c)
Definition: libdc1394.c:167
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
dc1394_frame_rate::frame_rate_id
int frame_rate_id
Definition: libdc1394.c:66
dc1394_close
static int dc1394_close(AVFormatContext *context)
Definition: libdc1394.c:279
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:464
height
#define height
av_image_get_buffer_size
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:466
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:379
av_parse_video_size
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:150
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
dc1394_data::pixel_format
char * pixel_format
Set by a private option.
Definition: libdc1394.c:44
av_parse_video_rate
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:181
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:366
internal.h
AVCodecParameters::height
int height
Definition: codec_par.h:127
dc1394_frame_formats
static const struct dc1394_frame_format dc1394_frame_formats[]
dc1394_frame_rate::frame_rate
int frame_rate
Definition: libdc1394.c:65
av_rescale
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:128
libdc1394_class
static const AVClass libdc1394_class
Definition: libdc1394.c:88
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:935
AVClass::class_name
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:71
avformat.h
AV_PIX_FMT_UYVY422
@ AV_PIX_FMT_UYVY422
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:81
av_get_pix_fmt
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2592
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:943
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, 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:1196
dc1394_frame_rates
static const struct dc1394_frame_rate dc1394_frame_rates[]
AVPacket::stream_index
int stream_index
Definition: packet.h:375
dc1394_read_packet
static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt)
Definition: libdc1394.c:253
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ff_libdc1394_demuxer
const AVInputFormat ff_libdc1394_demuxer
Definition: libdc1394.c:291
AVCodecParameters::format
int format
Definition: codec_par.h:84
options
static const AVOption options[]
Definition: libdc1394.c:81
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
imgutils.h
OFFSET
#define OFFSET(x)
Definition: libdc1394.c:79
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
dc1394_data::frame
dc1394video_frame_t * frame
Definition: libdc1394.c:40
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:228
dc1394_data::framerate
char * framerate
Set by a private option.
Definition: libdc1394.c:45
av_get_pix_fmt_name
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:2580
dc1394_data::stream_index
int stream_index
Definition: libdc1394.c:48
AV_PIX_FMT_UYYVYY411
@ AV_PIX_FMT_UYYVYY411
packed YUV 4:1:1, 12bpp, Cb Y0 Y1 Cr Y2 Y3
Definition: pixfmt.h:82