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/demux.h"
35 #include "libavformat/internal.h"
36 
37 typedef struct dc1394_data {
38  AVClass *class;
39  dc1394_t *d;
40  dc1394camera_t *camera;
41  dc1394video_frame_t *frame;
43  int frame_rate; /**< frames per 1000 seconds (fps * 1000) */
44  char *video_size; /**< String describing video size, set by a private option. */
45  char *pixel_format; /**< Set by a private option. */
46  char *framerate; /**< Set by a private option. */
47 
48  int size;
50 } dc1394_data;
51 
52 static const struct dc1394_frame_format {
53  int width;
54  int height;
58  { 320, 240, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_320x240_YUV422 },
59  { 640, 480, AV_PIX_FMT_GRAY8, DC1394_VIDEO_MODE_640x480_MONO8 },
60  { 640, 480, AV_PIX_FMT_UYYVYY411, DC1394_VIDEO_MODE_640x480_YUV411 },
61  { 640, 480, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_640x480_YUV422 },
62  { 0, 0, 0, 0 } /* gotta be the last one */
63 };
64 
65 static const struct dc1394_frame_rate {
68 } dc1394_frame_rates[] = {
69  { 1875, DC1394_FRAMERATE_1_875 },
70  { 3750, DC1394_FRAMERATE_3_75 },
71  { 7500, DC1394_FRAMERATE_7_5 },
72  { 15000, DC1394_FRAMERATE_15 },
73  { 30000, DC1394_FRAMERATE_30 },
74  { 60000, DC1394_FRAMERATE_60 },
75  {120000, DC1394_FRAMERATE_120 },
76  {240000, DC1394_FRAMERATE_240 },
77  { 0, 0 } /* gotta be the last one */
78 };
79 
80 #define OFFSET(x) offsetof(dc1394_data, x)
81 #define DEC AV_OPT_FLAG_DECODING_PARAM
82 static const AVOption options[] = {
83  { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "qvga"}, 0, 0, DEC },
84  { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "uyvy422"}, 0, 0, DEC },
85  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
86  { NULL },
87 };
88 
89 static const AVClass libdc1394_class = {
90  .class_name = "libdc1394 indev",
91  .item_name = av_default_item_name,
92  .option = options,
93  .version = LIBAVUTIL_VERSION_INT,
95 };
96 
97 
99  const struct dc1394_frame_format **select_fmt, const struct dc1394_frame_rate **select_fps)
100 {
101  dc1394_data* dc1394 = c->priv_data;
102  AVStream* vst;
103  const struct dc1394_frame_format *fmt;
104  const struct dc1394_frame_rate *fps;
105  enum AVPixelFormat pix_fmt;
106  int width, height;
108  int ret = 0;
109 
110  if ((pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == AV_PIX_FMT_NONE) {
111  av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format);
112  ret = AVERROR(EINVAL);
113  goto out;
114  }
115 
116  if ((ret = av_parse_video_size(&width, &height, dc1394->video_size)) < 0) {
117  av_log(c, AV_LOG_ERROR, "Could not parse video size '%s'.\n", dc1394->video_size);
118  goto out;
119  }
120  if ((ret = av_parse_video_rate(&framerate, dc1394->framerate)) < 0) {
121  av_log(c, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", dc1394->framerate);
122  goto out;
123  }
124  dc1394->frame_rate = av_rescale(1000, framerate.num, framerate.den);
125 
126  for (fmt = dc1394_frame_formats; fmt->width; fmt++)
127  if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
128  break;
129 
130  for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
131  if (fps->frame_rate == dc1394->frame_rate)
132  break;
133 
134  if (!fps->frame_rate || !fmt->width) {
135  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),
136  width, height, dc1394->frame_rate);
137  ret = AVERROR(EINVAL);
138  goto out;
139  }
140 
141  /* create a video stream */
142  vst = avformat_new_stream(c, NULL);
143  if (!vst) {
144  ret = AVERROR(ENOMEM);
145  goto out;
146  }
147  avpriv_set_pts_info(vst, 64, 1, 1000);
150  vst->codecpar->width = fmt->width;
151  vst->codecpar->height = fmt->height;
152  vst->codecpar->format = fmt->pix_fmt;
153  vst->avg_frame_rate = framerate;
154 
155  dc1394->current_frame = 0;
156  dc1394->stream_index = vst->index;
157  dc1394->size = av_image_get_buffer_size(fmt->pix_fmt,
158  fmt->width, fmt->height, 1);
159 
160  vst->codecpar->bit_rate = av_rescale(dc1394->size * 8,
161  fps->frame_rate, 1000);
162  *select_fps = fps;
163  *select_fmt = fmt;
164 out:
165  return ret;
166 }
167 
169 {
170  dc1394_data* dc1394 = c->priv_data;
171  dc1394camera_list_t *list;
172  int res, i;
173  const struct dc1394_frame_format *fmt = NULL;
174  const struct dc1394_frame_rate *fps = NULL;
175 
176  if (dc1394_read_common(c, &fmt, &fps) != 0)
177  return -1;
178 
179  /* Now let us prep the hardware. */
180  dc1394->d = dc1394_new();
181  if (dc1394_camera_enumerate(dc1394->d, &list) != DC1394_SUCCESS || !list) {
182  av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera.\n");
183  goto out;
184  }
185 
186  if (list->num == 0) {
187  av_log(c, AV_LOG_ERROR, "No cameras found.\n");
188  dc1394_camera_free_list(list);
189  goto out;
190  }
191 
192  /* FIXME: To select a specific camera I need to search in list its guid */
193  dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
194 
195  if (!dc1394->camera) {
196  av_log(c, AV_LOG_ERROR, "Unable to open camera with guid 0x%"PRIx64"\n",
197  list->ids[0].guid);
198  dc1394_camera_free_list(list);
199  goto out;
200  }
201 
202  if (list->num > 1) {
203  av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
204  }
205 
206  /* Freeing list of cameras */
207  dc1394_camera_free_list (list);
208 
209  /* Select MAX Speed possible from the cam */
210  if (dc1394->camera->bmode_capable>0) {
211  dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
212  i = DC1394_ISO_SPEED_800;
213  } else {
214  i = DC1394_ISO_SPEED_400;
215  }
216 
217  for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
218  res=dc1394_video_set_iso_speed(dc1394->camera, i);
219  }
220  if (res != DC1394_SUCCESS) {
221  av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
222  goto out_camera;
223  }
224 
225  if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
226  av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
227  goto out_camera;
228  }
229 
230  if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
231  av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
232  goto out_camera;
233  }
234  if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
235  av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
236  goto out_camera;
237  }
238 
239  if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
240  av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
241  goto out_camera;
242  }
243  return 0;
244 
245 out_camera:
246  dc1394_capture_stop(dc1394->camera);
247  dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
248  dc1394_camera_free (dc1394->camera);
249 out:
250  dc1394_free(dc1394->d);
251  return -1;
252 }
253 
255 {
256  struct dc1394_data *dc1394 = c->priv_data;
257  int res;
258 
259  /* discard stale frame */
260  if (dc1394->current_frame++) {
261  if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
262  av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
263  }
264 
265  res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
266  if (res == DC1394_SUCCESS) {
267  pkt->data = (uint8_t *)dc1394->frame->image;
268  pkt->size = dc1394->frame->image_bytes;
269  pkt->pts = dc1394->current_frame * 1000000 / dc1394->frame_rate;
271  pkt->stream_index = dc1394->stream_index;
272  } else {
273  av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
274  return AVERROR_INVALIDDATA;
275  }
276 
277  return pkt->size;
278 }
279 
281 {
282  struct dc1394_data *dc1394 = context->priv_data;
283 
284  dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
285  dc1394_capture_stop(dc1394->camera);
286  dc1394_camera_free(dc1394->camera);
287  dc1394_free(dc1394->d);
288 
289  return 0;
290 }
291 
293  .p.name = "libdc1394",
294  .p.long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
295  .p.flags = AVFMT_NOFILE,
296  .p.priv_class = &libdc1394_class,
297  .priv_data_size = sizeof(struct dc1394_data),
299  .read_packet = dc1394_read_packet,
300  .read_close = dc1394_close,
301 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
dc1394_data::video_size
char * video_size
String describing video size, set by a private option.
Definition: libdc1394.c:44
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
out
FILE * out
Definition: movenc.c:54
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AV_CODEC_ID_RAWVIDEO
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:65
pixdesc.h
AVPacket::data
uint8_t * data
Definition: packet.h:522
AVOption
AVOption.
Definition: opt.h:346
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:832
DEC
#define DEC
Definition: libdc1394.c:81
mathematics.h
dc1394_data::camera
dc1394camera_t * camera
Definition: libdc1394.c:40
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:577
dc1394_data::current_frame
int current_frame
Definition: libdc1394.c:42
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:98
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: avformat.c:853
dc1394_data::d
dc1394_t * d
Definition: libdc1394.c:39
dc1394_frame_format::width
int width
Definition: libdc1394.c:53
dc1394_frame_format
Definition: libdc1394.c:52
dc1394_frame_format::frame_size_id
int frame_size_id
Definition: libdc1394.c:56
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
dc1394_data
Definition: libdc1394.c:37
width
#define width
dc1394_frame_format::pix_fmt
enum AVPixelFormat pix_fmt
Definition: libdc1394.c:55
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demux_decode.c:41
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
dc1394_data::frame_rate
int frame_rate
frames per 1000 seconds (fps * 1000)
Definition: libdc1394.c:43
dc1394_frame_rate
Definition: libdc1394.c:65
dc1394_frame_format::height
int height
Definition: libdc1394.c:54
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:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
framerate
float framerate
Definition: av1_levels.c:29
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
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:237
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:81
dc1394_data::size
int size
Definition: libdc1394.c:48
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:168
AVPacket::size
int size
Definition: packet.h:523
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:106
dc1394_frame_rate::frame_rate_id
int frame_rate_id
Definition: libdc1394.c:67
dc1394_close
static int dc1394_close(AVFormatContext *context)
Definition: libdc1394.c:280
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:468
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
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:528
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:45
ff_libdc1394_demuxer
const FFInputFormat ff_libdc1394_demuxer
Definition: libdc1394.c:292
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:255
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
internal.h
AVCodecParameters::height
int height
Definition: codec_par.h:135
dc1394_frame_formats
static const struct dc1394_frame_format dc1394_frame_formats[]
demux.h
dc1394_frame_rate::frame_rate
int frame_rate
Definition: libdc1394.c:66
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:129
libdc1394_class
static const AVClass libdc1394_class
Definition: libdc1394.c:89
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
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:88
av_get_pix_fmt
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2894
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:749
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
dc1394_frame_rates
static const struct dc1394_frame_rate dc1394_frame_rates[]
AVPacket::stream_index
int stream_index
Definition: packet.h:524
dc1394_read_packet
static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt)
Definition: libdc1394.c:254
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::format
int format
Definition: codec_par.h:92
options
static const AVOption options[]
Definition: libdc1394.c:82
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:499
FFInputFormat
Definition: demux.h:37
imgutils.h
OFFSET
#define OFFSET(x)
Definition: libdc1394.c:80
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
dc1394_data::frame
dc1394video_frame_t * frame
Definition: libdc1394.c:41
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:239
dc1394_data::framerate
char * framerate
Set by a private option.
Definition: libdc1394.c:46
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:2882
dc1394_data::stream_index
int stream_index
Definition: libdc1394.c:49
AV_PIX_FMT_UYYVYY411
@ AV_PIX_FMT_UYYVYY411
packed YUV 4:1:1, 12bpp, Cb Y0 Y1 Cr Y2 Y3
Definition: pixfmt.h:89