FFmpeg
Loading...
Searching...
No Matches
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"
29#include "libavutil/opt.h"
31#include "libavutil/pixdesc.h"
32
34#include "libavformat/demux.h"
36
37typedef 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;
51
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
65static const struct dc1394_frame_rate {
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
82static 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
89static 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;
106 int width, height;
108 int ret = 0;
109
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;
154
155 dc1394->current_frame = 0;
156 dc1394->stream_index = vst->index;
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;
164out:
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
245out_camera:
246 dc1394_capture_stop(dc1394->camera);
247 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
248 dc1394_camera_free (dc1394->camera);
249out:
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;
270 pkt->flags |= AV_PKT_FLAG_KEY;
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
280static int dc1394_close(AVFormatContext * context)
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};
const FFInputFormat ff_libdc1394_demuxer
Definition libdc1394.c:292
static FILE * out
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:834
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition avformat.h:488
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define NULL
Definition coverity.c:32
static AVPacket * pkt
static enum AVPixelFormat pix_fmt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
@ AV_CODEC_ID_RAWVIDEO
Definition codec_id.h:63
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
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
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
misc image utilities
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt)
Definition libdc1394.c:254
static int dc1394_read_header(AVFormatContext *c)
Definition libdc1394.c:168
#define OFFSET(x)
Definition libdc1394.c:80
static int dc1394_close(AVFormatContext *context)
Definition libdc1394.c:280
static const AVClass libdc1394_class
Definition libdc1394.c:89
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
static const struct dc1394_frame_format dc1394_frame_formats[]
static const struct dc1394_frame_rate dc1394_frame_rates[]
#define DEC
Definition librsvgdec.c:149
@ AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT
Definition log.h:42
AVOptions.
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
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition parseutils.c:181
misc parsing utilities
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:3380
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition pixdesc.c:3392
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_UYVY422
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition pixfmt.h:88
@ AV_PIX_FMT_UYYVYY411
packed YUV 4:1:1, 12bpp, Cb Y0 Y1 Cr Y2 Y3
Definition pixfmt.h:89
Describe the class of an AVClass context structure.
Definition log.h:76
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int width
The width of the video frame in pixels.
Definition codec_par.h:143
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
void * priv_data
Format private data.
Definition avformat.h:1361
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
Rational number (pair of numerator and denominator).
Definition rational.h:58
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int index
stream index in AVFormatContext
Definition avformat.h:772
AVRational avg_frame_rate
Average framerate.
Definition avformat.h:855
char * video_size
String describing video size, set by a private option.
Definition libdc1394.c:44
int current_frame
Definition libdc1394.c:42
char * pixel_format
Set by a private option.
Definition libdc1394.c:45
int stream_index
Definition libdc1394.c:49
int frame_rate
frames per 1000 seconds (fps * 1000)
Definition libdc1394.c:43
dc1394video_frame_t * frame
Definition libdc1394.c:41
dc1394camera_t * camera
Definition libdc1394.c:40
char * framerate
Set by a private option.
Definition libdc1394.c:46
dc1394_t * d
Definition libdc1394.c:39
enum AVPixelFormat pix_fmt
Definition libdc1394.c:55
#define av_log(a,...)
float framerate
Definition av1_levels.c:29
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
static double c[64]