2004-10-22 05:04:30 +03:00
|
|
|
/*
|
|
|
|
* IIDC1394 grab interface (uses libdc1394 and libraw1394)
|
|
|
|
* Copyright (c) 2004 Roman Shaposhnik
|
2008-01-11 03:59:05 +02:00
|
|
|
* Copyright (c) 2008 Alessandro Sappia
|
2004-10-22 05:04:30 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2004-10-22 05:04:30 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2004-10-22 05:04:30 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2004-10-22 05:04:30 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2004-10-22 05:04:30 +03:00
|
|
|
*/
|
|
|
|
|
2008-01-11 03:59:05 +02:00
|
|
|
#include <dc1394/dc1394.h>
|
|
|
|
|
2015-10-14 11:33:24 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
2015-04-09 21:03:39 +02:00
|
|
|
#include "libavutil/internal.h"
|
|
|
|
#include "libavutil/log.h"
|
|
|
|
#include "libavutil/mathematics.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/parseutils.h"
|
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
|
|
|
|
#include "libavformat/avformat.h"
|
2024-02-10 16:50:43 +02:00
|
|
|
#include "libavformat/demux.h"
|
2015-04-09 21:03:39 +02:00
|
|
|
#include "libavformat/internal.h"
|
|
|
|
|
2004-10-22 05:04:30 +03:00
|
|
|
typedef struct dc1394_data {
|
2011-05-23 23:01:36 +03:00
|
|
|
AVClass *class;
|
2008-01-11 03:59:05 +02:00
|
|
|
dc1394_t *d;
|
|
|
|
dc1394camera_t *camera;
|
|
|
|
dc1394video_frame_t *frame;
|
2004-10-22 05:04:30 +03:00
|
|
|
int current_frame;
|
2011-05-24 22:16:47 +03:00
|
|
|
int frame_rate; /**< frames per 1000 seconds (fps * 1000) */
|
2011-05-24 08:43:01 +03:00
|
|
|
char *video_size; /**< String describing video size, set by a private option. */
|
2011-05-24 10:02:11 +03:00
|
|
|
char *pixel_format; /**< Set by a private option. */
|
2011-05-24 22:16:47 +03:00
|
|
|
char *framerate; /**< Set by a private option. */
|
2004-10-22 05:04:30 +03:00
|
|
|
|
2016-07-13 18:56:51 +02:00
|
|
|
int size;
|
|
|
|
int stream_index;
|
2004-10-22 05:04:30 +03:00
|
|
|
} dc1394_data;
|
|
|
|
|
2015-08-30 12:42:42 +02:00
|
|
|
static const struct dc1394_frame_format {
|
2004-10-22 05:04:30 +03:00
|
|
|
int width;
|
|
|
|
int height;
|
2012-10-06 13:10:34 +03:00
|
|
|
enum AVPixelFormat pix_fmt;
|
2004-10-22 05:04:30 +03:00
|
|
|
int frame_size_id;
|
|
|
|
} dc1394_frame_formats[] = {
|
2012-10-06 13:10:34 +03:00
|
|
|
{ 320, 240, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_320x240_YUV422 },
|
2015-01-04 09:30:44 +02:00
|
|
|
{ 640, 480, AV_PIX_FMT_GRAY8, DC1394_VIDEO_MODE_640x480_MONO8 },
|
2012-10-06 13:10:34 +03:00
|
|
|
{ 640, 480, AV_PIX_FMT_UYYVYY411, DC1394_VIDEO_MODE_640x480_YUV411 },
|
|
|
|
{ 640, 480, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_640x480_YUV422 },
|
2008-01-08 04:52:05 +02:00
|
|
|
{ 0, 0, 0, 0 } /* gotta be the last one */
|
2004-10-22 05:04:30 +03:00
|
|
|
};
|
|
|
|
|
2015-08-30 12:42:42 +02:00
|
|
|
static const struct dc1394_frame_rate {
|
2004-10-22 05:04:30 +03:00
|
|
|
int frame_rate;
|
|
|
|
int frame_rate_id;
|
|
|
|
} dc1394_frame_rates[] = {
|
2008-01-11 03:59:05 +02:00
|
|
|
{ 1875, DC1394_FRAMERATE_1_875 },
|
|
|
|
{ 3750, DC1394_FRAMERATE_3_75 },
|
|
|
|
{ 7500, DC1394_FRAMERATE_7_5 },
|
|
|
|
{ 15000, DC1394_FRAMERATE_15 },
|
|
|
|
{ 30000, DC1394_FRAMERATE_30 },
|
|
|
|
{ 60000, DC1394_FRAMERATE_60 },
|
2008-01-11 04:13:32 +02:00
|
|
|
{120000, DC1394_FRAMERATE_120 },
|
|
|
|
{240000, DC1394_FRAMERATE_240 },
|
2008-01-08 04:52:05 +02:00
|
|
|
{ 0, 0 } /* gotta be the last one */
|
2004-10-22 05:04:30 +03:00
|
|
|
};
|
|
|
|
|
2011-05-24 08:43:01 +03:00
|
|
|
#define OFFSET(x) offsetof(dc1394_data, x)
|
|
|
|
#define DEC AV_OPT_FLAG_DECODING_PARAM
|
2011-05-26 08:25:41 +03:00
|
|
|
static const AVOption options[] = {
|
2011-10-04 08:38:01 +03:00
|
|
|
{ "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "qvga"}, 0, 0, DEC },
|
|
|
|
{ "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "uyvy422"}, 0, 0, DEC },
|
|
|
|
{ "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
|
2011-05-26 08:25:41 +03:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass libdc1394_class = {
|
|
|
|
.class_name = "libdc1394 indev",
|
2024-01-19 14:33:28 +02:00
|
|
|
.item_name = av_default_item_name,
|
2011-05-26 08:25:41 +03:00
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
2014-02-23 01:32:51 +03:00
|
|
|
.category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
|
2011-05-26 08:25:41 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
static inline int dc1394_read_common(AVFormatContext *c,
|
2015-09-20 22:11:59 +02:00
|
|
|
const struct dc1394_frame_format **select_fmt, const struct dc1394_frame_rate **select_fps)
|
2008-01-11 03:59:05 +02:00
|
|
|
{
|
|
|
|
dc1394_data* dc1394 = c->priv_data;
|
2004-10-22 05:04:30 +03:00
|
|
|
AVStream* vst;
|
2015-09-20 22:11:59 +02:00
|
|
|
const struct dc1394_frame_format *fmt;
|
|
|
|
const struct dc1394_frame_rate *fps;
|
2012-10-06 13:10:34 +03:00
|
|
|
enum AVPixelFormat pix_fmt;
|
2011-05-24 08:43:01 +03:00
|
|
|
int width, height;
|
2011-05-24 22:16:47 +03:00
|
|
|
AVRational framerate;
|
2011-05-26 18:13:09 +03:00
|
|
|
int ret = 0;
|
2004-10-22 05:04:30 +03:00
|
|
|
|
2012-10-06 13:10:34 +03:00
|
|
|
if ((pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == AV_PIX_FMT_NONE) {
|
2011-05-24 10:02:11 +03:00
|
|
|
av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format);
|
|
|
|
ret = AVERROR(EINVAL);
|
2008-01-11 03:59:05 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-05-24 08:43:01 +03:00
|
|
|
if ((ret = av_parse_video_size(&width, &height, dc1394->video_size)) < 0) {
|
2011-06-03 11:50:28 +03:00
|
|
|
av_log(c, AV_LOG_ERROR, "Could not parse video size '%s'.\n", dc1394->video_size);
|
2011-05-24 08:43:01 +03:00
|
|
|
goto out;
|
2011-05-26 09:16:58 +03:00
|
|
|
}
|
2011-05-24 22:16:47 +03:00
|
|
|
if ((ret = av_parse_video_rate(&framerate, dc1394->framerate)) < 0) {
|
2011-06-03 11:50:28 +03:00
|
|
|
av_log(c, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", dc1394->framerate);
|
2011-05-24 22:16:47 +03:00
|
|
|
goto out;
|
2011-05-26 09:16:58 +03:00
|
|
|
}
|
2011-05-24 22:16:47 +03:00
|
|
|
dc1394->frame_rate = av_rescale(1000, framerate.num, framerate.den);
|
2011-06-05 03:33:59 +03:00
|
|
|
|
2004-10-22 05:04:30 +03:00
|
|
|
for (fmt = dc1394_frame_formats; fmt->width; fmt++)
|
2008-01-08 04:52:05 +02:00
|
|
|
if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
|
2005-12-22 03:10:11 +02:00
|
|
|
break;
|
2011-06-05 03:33:59 +03:00
|
|
|
|
2004-10-22 05:04:30 +03:00
|
|
|
for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
|
2011-05-24 22:16:47 +03:00
|
|
|
if (fps->frame_rate == dc1394->frame_rate)
|
2005-12-22 03:10:11 +02:00
|
|
|
break;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2008-01-08 04:52:05 +02:00
|
|
|
if (!fps->frame_rate || !fmt->width) {
|
2011-06-03 12:42:07 +03:00
|
|
|
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),
|
2011-05-24 22:16:47 +03:00
|
|
|
width, height, dc1394->frame_rate);
|
2011-05-26 18:13:09 +03:00
|
|
|
ret = AVERROR(EINVAL);
|
2008-01-08 04:52:05 +02:00
|
|
|
goto out;
|
2011-05-26 09:16:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create a video stream */
|
2011-06-18 12:43:24 +03:00
|
|
|
vst = avformat_new_stream(c, NULL);
|
2011-05-26 18:13:09 +03:00
|
|
|
if (!vst) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
2008-01-11 03:59:05 +02:00
|
|
|
goto out;
|
2011-05-26 18:13:09 +03:00
|
|
|
}
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(vst, 64, 1, 1000);
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
vst->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
|
|
|
|
vst->codecpar->width = fmt->width;
|
|
|
|
vst->codecpar->height = fmt->height;
|
|
|
|
vst->codecpar->format = fmt->pix_fmt;
|
|
|
|
vst->avg_frame_rate = framerate;
|
2011-05-26 09:16:58 +03:00
|
|
|
|
|
|
|
dc1394->current_frame = 0;
|
2016-07-13 18:56:51 +02:00
|
|
|
dc1394->stream_index = vst->index;
|
|
|
|
dc1394->size = av_image_get_buffer_size(fmt->pix_fmt,
|
|
|
|
fmt->width, fmt->height, 1);
|
2011-05-26 09:16:58 +03:00
|
|
|
|
2016-07-13 18:56:51 +02:00
|
|
|
vst->codecpar->bit_rate = av_rescale(dc1394->size * 8,
|
|
|
|
fps->frame_rate, 1000);
|
2008-01-11 03:59:05 +02:00
|
|
|
*select_fps = fps;
|
|
|
|
*select_fmt = fmt;
|
|
|
|
out:
|
2011-05-26 18:13:09 +03:00
|
|
|
return ret;
|
2008-01-11 03:59:05 +02:00
|
|
|
}
|
|
|
|
|
2016-11-29 13:44:23 +02:00
|
|
|
static int dc1394_read_header(AVFormatContext *c)
|
2008-01-11 03:59:05 +02:00
|
|
|
{
|
|
|
|
dc1394_data* dc1394 = c->priv_data;
|
|
|
|
dc1394camera_list_t *list;
|
|
|
|
int res, i;
|
2015-09-20 22:11:59 +02:00
|
|
|
const struct dc1394_frame_format *fmt = NULL;
|
|
|
|
const struct dc1394_frame_rate *fps = NULL;
|
2008-01-11 03:59:05 +02:00
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
if (dc1394_read_common(c, &fmt, &fps) != 0)
|
2008-01-11 03:59:05 +02:00
|
|
|
return -1;
|
|
|
|
|
2008-07-24 20:09:28 +03:00
|
|
|
/* Now let us prep the hardware. */
|
2008-01-11 03:59:05 +02:00
|
|
|
dc1394->d = dc1394_new();
|
2016-07-24 20:49:40 +02:00
|
|
|
if (dc1394_camera_enumerate(dc1394->d, &list) != DC1394_SUCCESS || !list) {
|
|
|
|
av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera.\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (list->num == 0) {
|
|
|
|
av_log(c, AV_LOG_ERROR, "No cameras found.\n");
|
|
|
|
dc1394_camera_free_list(list);
|
2008-01-11 03:59:05 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: To select a specific camera I need to search in list its guid */
|
|
|
|
dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
|
2017-08-18 19:20:15 +02:00
|
|
|
|
|
|
|
if (!dc1394->camera) {
|
|
|
|
av_log(c, AV_LOG_ERROR, "Unable to open camera with guid 0x%"PRIx64"\n",
|
|
|
|
list->ids[0].guid);
|
|
|
|
dc1394_camera_free_list(list);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-01-11 03:59:05 +02:00
|
|
|
if (list->num > 1) {
|
|
|
|
av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Freeing list of cameras */
|
|
|
|
dc1394_camera_free_list (list);
|
2011-05-26 09:16:58 +03:00
|
|
|
|
2008-01-11 03:59:05 +02:00
|
|
|
/* Select MAX Speed possible from the cam */
|
|
|
|
if (dc1394->camera->bmode_capable>0) {
|
|
|
|
dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
|
|
|
|
i = DC1394_ISO_SPEED_800;
|
|
|
|
} else {
|
|
|
|
i = DC1394_ISO_SPEED_400;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
|
|
|
|
res=dc1394_video_set_iso_speed(dc1394->camera, i);
|
|
|
|
}
|
|
|
|
if (res != DC1394_SUCCESS) {
|
|
|
|
av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
|
|
|
|
goto out_camera;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
|
|
|
|
av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
|
|
|
|
goto out_camera;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
|
|
|
|
av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
|
|
|
|
goto out_camera;
|
|
|
|
}
|
|
|
|
if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
|
|
|
|
av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
|
|
|
|
goto out_camera;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
|
|
|
|
av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
|
|
|
|
goto out_camera;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_camera:
|
|
|
|
dc1394_capture_stop(dc1394->camera);
|
|
|
|
dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
|
|
|
|
dc1394_camera_free (dc1394->camera);
|
|
|
|
out:
|
|
|
|
dc1394_free(dc1394->d);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-29 13:44:23 +02:00
|
|
|
static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt)
|
2008-01-11 03:59:05 +02:00
|
|
|
{
|
|
|
|
struct dc1394_data *dc1394 = c->priv_data;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
/* discard stale frame */
|
|
|
|
if (dc1394->current_frame++) {
|
|
|
|
if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
|
|
|
|
av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
|
|
|
|
if (res == DC1394_SUCCESS) {
|
2016-07-13 18:56:51 +02:00
|
|
|
pkt->data = (uint8_t *)dc1394->frame->image;
|
|
|
|
pkt->size = dc1394->frame->image_bytes;
|
|
|
|
pkt->pts = dc1394->current_frame * 1000000 / dc1394->frame_rate;
|
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
pkt->stream_index = dc1394->stream_index;
|
2008-01-11 03:59:05 +02:00
|
|
|
} else {
|
|
|
|
av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
|
2016-07-13 18:56:51 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2008-01-11 03:59:05 +02:00
|
|
|
}
|
|
|
|
|
2016-07-13 18:56:51 +02:00
|
|
|
return pkt->size;
|
2008-01-11 03:59:05 +02:00
|
|
|
}
|
|
|
|
|
2016-11-29 13:44:23 +02:00
|
|
|
static int dc1394_close(AVFormatContext * context)
|
2008-01-11 03:59:05 +02:00
|
|
|
{
|
|
|
|
struct dc1394_data *dc1394 = context->priv_data;
|
|
|
|
|
|
|
|
dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
|
|
|
|
dc1394_capture_stop(dc1394->camera);
|
|
|
|
dc1394_camera_free(dc1394->camera);
|
|
|
|
dc1394_free(dc1394->d);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-02-10 16:50:43 +02:00
|
|
|
const FFInputFormat ff_libdc1394_demuxer = {
|
|
|
|
.p.name = "libdc1394",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
|
|
|
|
.p.flags = AVFMT_NOFILE,
|
|
|
|
.p.priv_class = &libdc1394_class,
|
2008-01-11 03:59:05 +02:00
|
|
|
.priv_data_size = sizeof(struct dc1394_data),
|
2016-11-29 13:44:23 +02:00
|
|
|
.read_header = dc1394_read_header,
|
|
|
|
.read_packet = dc1394_read_packet,
|
|
|
|
.read_close = dc1394_close,
|
2008-01-11 03:59:05 +02:00
|
|
|
};
|