h264_metadata: Add option to delete filler data
[libav.git] / libavdevice / libdc1394.c
CommitLineData
f02be79d
RS
1/*
2 * IIDC1394 grab interface (uses libdc1394 and libraw1394)
3 * Copyright (c) 2004 Roman Shaposhnik
0b4a1821 4 * Copyright (c) 2008 Alessandro Sappia
f02be79d 5 *
2912e87a 6 * This file is part of Libav.
b78e7197 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
f02be79d
RS
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
f02be79d 12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
f02be79d
RS
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
2912e87a 19 * License along with Libav; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
f02be79d
RS
21 */
22
0b4a1821 23#include <dc1394/dc1394.h>
0b4a1821 24
f890677d 25#include "libavutil/imgutils.h"
7a9b764c
MK
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
f02be79d 36typedef struct dc1394_data {
79405e57 37 AVClass *class;
0b4a1821
AS
38 dc1394_t *d;
39 dc1394camera_t *camera;
40 dc1394video_frame_t *frame;
f02be79d 41 int current_frame;
121ef2e2 42 int frame_rate; /**< frames per 1000 seconds (fps * 1000) */
fd48620e 43 char *video_size; /**< String describing video size, set by a private option. */
2e0e1e71 44 char *pixel_format; /**< Set by a private option. */
121ef2e2 45 char *framerate; /**< Set by a private option. */
f02be79d 46
5d0f85f1
VG
47 int size;
48 int stream_index;
f02be79d
RS
49} dc1394_data;
50
51struct dc1394_frame_format {
52 int width;
53 int height;
716d413c 54 enum AVPixelFormat pix_fmt;
f02be79d
RS
55 int frame_size_id;
56} dc1394_frame_formats[] = {
716d413c 57 { 320, 240, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_320x240_YUV422 },
6a808f5a 58 { 640, 480, AV_PIX_FMT_GRAY8, DC1394_VIDEO_MODE_640x480_MONO8 },
716d413c
AK
59 { 640, 480, AV_PIX_FMT_UYYVYY411, DC1394_VIDEO_MODE_640x480_YUV411 },
60 { 640, 480, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_640x480_YUV422 },
43d1a1c0 61 { 0, 0, 0, 0 } /* gotta be the last one */
f02be79d
RS
62};
63
64struct dc1394_frame_rate {
65 int frame_rate;
66 int frame_rate_id;
67} dc1394_frame_rates[] = {
0b4a1821
AS
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 },
cbe3f0b8
AS
74 {120000, DC1394_FRAMERATE_120 },
75 {240000, DC1394_FRAMERATE_240 },
43d1a1c0 76 { 0, 0 } /* gotta be the last one */
f02be79d
RS
77};
78
fd48620e
AK
79#define OFFSET(x) offsetof(dc1394_data, x)
80#define DEC AV_OPT_FLAG_DECODING_PARAM
2341c97e 81static const AVOption options[] = {
145f741e
AK
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 },
2341c97e
AK
85 { NULL },
86};
87
88static const AVClass libdc1394_class = {
89 .class_name = "libdc1394 indev",
90 .item_name = av_default_item_name,
91 .option = options,
92 .version = LIBAVUTIL_VERSION_INT,
93};
94
95
6e9651d1 96static inline int dc1394_read_common(AVFormatContext *c,
0b4a1821 97 struct dc1394_frame_format **select_fmt, struct dc1394_frame_rate **select_fps)
f02be79d
RS
98{
99 dc1394_data* dc1394 = c->priv_data;
100 AVStream* vst;
f02be79d
RS
101 struct dc1394_frame_format *fmt;
102 struct dc1394_frame_rate *fps;
716d413c 103 enum AVPixelFormat pix_fmt;
fd48620e 104 int width, height;
121ef2e2 105 AVRational framerate;
284bac2e 106 int ret = 0;
f02be79d 107
716d413c 108 if ((pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == AV_PIX_FMT_NONE) {
2e0e1e71
AK
109 av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format);
110 ret = AVERROR(EINVAL);
111 goto out;
112 }
113
fd48620e 114 if ((ret = av_parse_video_size(&width, &height, dc1394->video_size)) < 0) {
41b68dce 115 av_log(c, AV_LOG_ERROR, "Could not parse video size '%s'.\n", dc1394->video_size);
fd48620e
AK
116 goto out;
117 }
121ef2e2 118 if ((ret = av_parse_video_rate(&framerate, dc1394->framerate)) < 0) {
41b68dce 119 av_log(c, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", dc1394->framerate);
121ef2e2
AK
120 goto out;
121 }
121ef2e2 122 dc1394->frame_rate = av_rescale(1000, framerate.num, framerate.den);
fd48620e 123
f02be79d 124 for (fmt = dc1394_frame_formats; fmt->width; fmt++)
43d1a1c0 125 if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
bb270c08 126 break;
115329f1 127
f02be79d 128 for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
121ef2e2 129 if (fps->frame_rate == dc1394->frame_rate)
bb270c08 130 break;
115329f1 131
43d1a1c0 132 if (!fps->frame_rate || !fmt->width) {
94bed8e5 133 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),
121ef2e2 134 width, height, dc1394->frame_rate);
284bac2e 135 ret = AVERROR(EINVAL);
43d1a1c0
RS
136 goto out;
137 }
138
f02be79d 139 /* create a video stream */
3b3bbdd3 140 vst = avformat_new_stream(c, NULL);
284bac2e
AK
141 if (!vst) {
142 ret = AVERROR(ENOMEM);
0b4a1821 143 goto out;
284bac2e 144 }
c3f9ebf7 145 avpriv_set_pts_info(vst, 64, 1, 1000);
9200514a
AK
146 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
147 vst->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
148 vst->codecpar->width = fmt->width;
149 vst->codecpar->height = fmt->height;
150 vst->codecpar->format = fmt->pix_fmt;
151 vst->avg_frame_rate = framerate;
f02be79d 152
f02be79d 153 dc1394->current_frame = 0;
5d0f85f1
VG
154 dc1394->stream_index = vst->index;
155 dc1394->size = av_image_get_buffer_size(fmt->pix_fmt,
156 fmt->width, fmt->height, 1);
f02be79d 157
5d0f85f1
VG
158 vst->codecpar->bit_rate = av_rescale(dc1394->size * 8,
159 fps->frame_rate, 1000);
0b4a1821
AS
160 *select_fps = fps;
161 *select_fmt = fmt;
0b4a1821 162out:
284bac2e 163 return ret;
0b4a1821
AS
164}
165
4a1ef543 166static int dc1394_read_header(AVFormatContext *c)
0b4a1821
AS
167{
168 dc1394_data* dc1394 = c->priv_data;
169 dc1394camera_list_t *list;
170 int res, i;
171 struct dc1394_frame_format *fmt = NULL;
172 struct dc1394_frame_rate *fps = NULL;
173
6e9651d1 174 if (dc1394_read_common(c, &fmt, &fps) != 0)
0b4a1821
AS
175 return -1;
176
ca74c0a1 177 /* Now let us prep the hardware. */
0b4a1821 178 dc1394->d = dc1394_new();
bc739993
JK
179 if (dc1394_camera_enumerate(dc1394->d, &list) != DC1394_SUCCESS || !list) {
180 av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera.\n");
181 goto out;
182 }
183
184 if (list->num == 0) {
185 av_log(c, AV_LOG_ERROR, "No cameras found.\n");
186 dc1394_camera_free_list(list);
0b4a1821
AS
187 goto out;
188 }
189
190 /* FIXME: To select a specific camera I need to search in list its guid */
191 dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
192 if (list->num > 1) {
193 av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
194 }
195
196 /* Freeing list of cameras */
197 dc1394_camera_free_list (list);
198
199 /* Select MAX Speed possible from the cam */
200 if (dc1394->camera->bmode_capable>0) {
201 dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
202 i = DC1394_ISO_SPEED_800;
203 } else {
204 i = DC1394_ISO_SPEED_400;
205 }
206
207 for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
208 res=dc1394_video_set_iso_speed(dc1394->camera, i);
209 }
210 if (res != DC1394_SUCCESS) {
211 av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
212 goto out_camera;
213 }
214
215 if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
216 av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
217 goto out_camera;
218 }
219
220 if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
221 av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
222 goto out_camera;
223 }
224 if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
225 av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
226 goto out_camera;
227 }
228
229 if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
230 av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
231 goto out_camera;
232 }
233 return 0;
234
235out_camera:
236 dc1394_capture_stop(dc1394->camera);
237 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
238 dc1394_camera_free (dc1394->camera);
239out:
240 dc1394_free(dc1394->d);
241 return -1;
242}
243
4a1ef543 244static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt)
0b4a1821
AS
245{
246 struct dc1394_data *dc1394 = c->priv_data;
247 int res;
248
249 /* discard stale frame */
250 if (dc1394->current_frame++) {
251 if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
252 av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
253 }
254
255 res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
256 if (res == DC1394_SUCCESS) {
5d0f85f1
VG
257 pkt->data = (uint8_t *)dc1394->frame->image;
258 pkt->size = dc1394->frame->image_bytes;
259 pkt->pts = dc1394->current_frame * 1000000 / dc1394->frame_rate;
260 pkt->flags |= AV_PKT_FLAG_KEY;
261 pkt->stream_index = dc1394->stream_index;
0b4a1821
AS
262 } else {
263 av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
5d0f85f1 264 return AVERROR_INVALIDDATA;
0b4a1821
AS
265 }
266
5d0f85f1 267 return pkt->size;
0b4a1821
AS
268}
269
4a1ef543 270static int dc1394_close(AVFormatContext * context)
0b4a1821
AS
271{
272 struct dc1394_data *dc1394 = context->priv_data;
273
274 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
275 dc1394_capture_stop(dc1394->camera);
276 dc1394_camera_free(dc1394->camera);
277 dc1394_free(dc1394->d);
278
279 return 0;
280}
281
c6610a21 282AVInputFormat ff_libdc1394_demuxer = {
0b4a1821 283 .name = "libdc1394",
bde15e74 284 .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
0b4a1821 285 .priv_data_size = sizeof(struct dc1394_data),
4a1ef543
DB
286 .read_header = dc1394_read_header,
287 .read_packet = dc1394_read_packet,
288 .read_close = dc1394_close,
79405e57
AK
289 .flags = AVFMT_NOFILE,
290 .priv_class = &libdc1394_class,
f02be79d 291};