Add log_level_offset to AVCodecContext.
[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 *
b78e7197
DB
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg 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 *
b78e7197 13 * FFmpeg 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
b78e7197 19 * License along with FFmpeg; 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 "config.h"
245976da 24#include "libavformat/avformat.h"
f02be79d 25
49fb20cb 26#if HAVE_LIBDC1394_2
0b4a1821 27#include <dc1394/dc1394.h>
49fb20cb 28#elif HAVE_LIBDC1394_1
f02be79d
RS
29#include <libraw1394/raw1394.h>
30#include <libdc1394/dc1394_control.h>
31
0b4a1821
AS
32#define DC1394_VIDEO_MODE_320x240_YUV422 MODE_320x240_YUV422
33#define DC1394_VIDEO_MODE_640x480_YUV411 MODE_640x480_YUV411
34#define DC1394_VIDEO_MODE_640x480_YUV422 MODE_640x480_YUV422
35#define DC1394_FRAMERATE_1_875 FRAMERATE_1_875
36#define DC1394_FRAMERATE_3_75 FRAMERATE_3_75
37#define DC1394_FRAMERATE_7_5 FRAMERATE_7_5
38#define DC1394_FRAMERATE_15 FRAMERATE_15
39#define DC1394_FRAMERATE_30 FRAMERATE_30
40#define DC1394_FRAMERATE_60 FRAMERATE_60
cbe3f0b8
AS
41#define DC1394_FRAMERATE_120 FRAMERATE_120
42#define DC1394_FRAMERATE_240 FRAMERATE_240
0b4a1821
AS
43#endif
44
f02be79d
RS
45#undef free
46
47typedef struct dc1394_data {
49fb20cb 48#if HAVE_LIBDC1394_1
f02be79d
RS
49 raw1394handle_t handle;
50 dc1394_cameracapture camera;
49fb20cb 51#elif HAVE_LIBDC1394_2
0b4a1821
AS
52 dc1394_t *d;
53 dc1394camera_t *camera;
54 dc1394video_frame_t *frame;
55#endif
f02be79d
RS
56 int current_frame;
57 int fps;
58
59 AVPacket packet;
60} dc1394_data;
61
62struct dc1394_frame_format {
63 int width;
64 int height;
65 enum PixelFormat pix_fmt;
66 int frame_size_id;
67} dc1394_frame_formats[] = {
0b4a1821
AS
68 { 320, 240, PIX_FMT_UYVY422, DC1394_VIDEO_MODE_320x240_YUV422 },
69 { 640, 480, PIX_FMT_UYYVYY411, DC1394_VIDEO_MODE_640x480_YUV411 },
70 { 640, 480, PIX_FMT_UYVY422, DC1394_VIDEO_MODE_640x480_YUV422 },
43d1a1c0 71 { 0, 0, 0, 0 } /* gotta be the last one */
f02be79d
RS
72};
73
74struct dc1394_frame_rate {
75 int frame_rate;
76 int frame_rate_id;
77} dc1394_frame_rates[] = {
0b4a1821
AS
78 { 1875, DC1394_FRAMERATE_1_875 },
79 { 3750, DC1394_FRAMERATE_3_75 },
80 { 7500, DC1394_FRAMERATE_7_5 },
81 { 15000, DC1394_FRAMERATE_15 },
82 { 30000, DC1394_FRAMERATE_30 },
83 { 60000, DC1394_FRAMERATE_60 },
cbe3f0b8
AS
84 {120000, DC1394_FRAMERATE_120 },
85 {240000, DC1394_FRAMERATE_240 },
43d1a1c0 86 { 0, 0 } /* gotta be the last one */
f02be79d
RS
87};
88
0b4a1821
AS
89static inline int dc1394_read_common(AVFormatContext *c, AVFormatParameters *ap,
90 struct dc1394_frame_format **select_fmt, struct dc1394_frame_rate **select_fps)
f02be79d
RS
91{
92 dc1394_data* dc1394 = c->priv_data;
93 AVStream* vst;
f02be79d
RS
94 struct dc1394_frame_format *fmt;
95 struct dc1394_frame_rate *fps;
43d1a1c0
RS
96 enum PixelFormat pix_fmt = ap->pix_fmt == PIX_FMT_NONE ? PIX_FMT_UYVY422 : ap->pix_fmt; /* defaults */
97 int width = !ap->width ? 320 : ap->width;
98 int height = !ap->height ? 240 : ap->height;
99 int frame_rate = !ap->time_base.num ? 30000 : av_rescale(1000, ap->time_base.den, ap->time_base.num);
f02be79d
RS
100
101 for (fmt = dc1394_frame_formats; fmt->width; fmt++)
43d1a1c0 102 if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
bb270c08 103 break;
115329f1 104
f02be79d 105 for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
43d1a1c0 106 if (fps->frame_rate == frame_rate)
bb270c08 107 break;
115329f1 108
43d1a1c0
RS
109 if (!fps->frame_rate || !fmt->width) {
110 av_log(c, AV_LOG_ERROR, "Can't find matching camera format for %s, %dx%d@%d:1000fps\n", avcodec_get_pix_fmt_name(pix_fmt),
111 width, height, frame_rate);
112 goto out;
113 }
114
f02be79d
RS
115 /* create a video stream */
116 vst = av_new_stream(c, 0);
117 if (!vst)
0b4a1821 118 goto out;
f02be79d 119 av_set_pts_info(vst, 64, 1, 1000);
72415b2a 120 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
9f747cc3
SH
121 vst->codec->codec_id = CODEC_ID_RAWVIDEO;
122 vst->codec->time_base.den = fps->frame_rate;
123 vst->codec->time_base.num = 1000;
124 vst->codec->width = fmt->width;
125 vst->codec->height = fmt->height;
126 vst->codec->pix_fmt = fmt->pix_fmt;
f02be79d
RS
127
128 /* packet init */
129 av_init_packet(&dc1394->packet);
130 dc1394->packet.size = avpicture_get_size(fmt->pix_fmt, fmt->width, fmt->height);
131 dc1394->packet.stream_index = vst->index;
cc947f04 132 dc1394->packet.flags |= AV_PKT_FLAG_KEY;
115329f1 133
f02be79d
RS
134 dc1394->current_frame = 0;
135 dc1394->fps = fps->frame_rate;
136
9f747cc3 137 vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000);
0b4a1821
AS
138 *select_fps = fps;
139 *select_fmt = fmt;
140 return 0;
141out:
142 return -1;
143}
144
49fb20cb 145#if HAVE_LIBDC1394_1
0b4a1821
AS
146static int dc1394_v1_read_header(AVFormatContext *c, AVFormatParameters * ap)
147{
148 dc1394_data* dc1394 = c->priv_data;
149 AVStream* vst;
150 nodeid_t* camera_nodes;
151 int res;
152 struct dc1394_frame_format *fmt = NULL;
153 struct dc1394_frame_rate *fps = NULL;
154
155 if (dc1394_read_common(c,ap,&fmt,&fps) != 0)
156 return -1;
115329f1 157
ca74c0a1 158 /* Now let us prep the hardware. */
f02be79d
RS
159 dc1394->handle = dc1394_create_handle(0); /* FIXME: gotta have ap->port */
160 if (!dc1394->handle) {
bb270c08 161 av_log(c, AV_LOG_ERROR, "Can't acquire dc1394 handle on port %d\n", 0 /* ap->port */);
f02be79d
RS
162 goto out;
163 }
164 camera_nodes = dc1394_get_camera_nodes(dc1394->handle, &res, 1);
165 if (!camera_nodes || camera_nodes[ap->channel] == DC1394_NO_CAMERA) {
166 av_log(c, AV_LOG_ERROR, "There's no IIDC camera on the channel %d\n", ap->channel);
115329f1 167 goto out_handle;
f02be79d 168 }
115329f1 169 res = dc1394_dma_setup_capture(dc1394->handle, camera_nodes[ap->channel],
bb270c08
DB
170 0,
171 FORMAT_VGA_NONCOMPRESSED,
172 fmt->frame_size_id,
173 SPEED_400,
174 fps->frame_rate_id, 8, 1,
cc58300e 175 c->filename,
bb270c08 176 &dc1394->camera);
f02be79d
RS
177 dc1394_free_camera_nodes(camera_nodes);
178 if (res != DC1394_SUCCESS) {
179 av_log(c, AV_LOG_ERROR, "Can't prepare camera for the DMA capture\n");
bb270c08 180 goto out_handle;
f02be79d
RS
181 }
182
183 res = dc1394_start_iso_transmission(dc1394->handle, dc1394->camera.node);
184 if (res != DC1394_SUCCESS) {
185 av_log(c, AV_LOG_ERROR, "Can't start isochronous transmission\n");
bb270c08 186 goto out_handle_dma;
f02be79d 187 }
115329f1 188
f02be79d
RS
189 return 0;
190
191out_handle_dma:
192 dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
193 dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
194out_handle:
195 dc1394_destroy_handle(dc1394->handle);
196out:
197 return -1;
198}
199
0b4a1821 200static int dc1394_v1_read_packet(AVFormatContext *c, AVPacket *pkt)
f02be79d
RS
201{
202 struct dc1394_data *dc1394 = c->priv_data;
203 int res;
115329f1 204
f02be79d
RS
205 /* discard stale frame */
206 if (dc1394->current_frame++) {
bb270c08 207 if (dc1394_dma_done_with_buffer(&dc1394->camera) != DC1394_SUCCESS)
f02be79d
RS
208 av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
209 }
115329f1 210
f02be79d
RS
211 res = dc1394_dma_single_capture(&dc1394->camera);
212
213 if (res == DC1394_SUCCESS) {
115329f1
DB
214 dc1394->packet.data = (uint8_t *)(dc1394->camera.capture_buffer);
215 dc1394->packet.pts = (dc1394->current_frame * 1000000) / dc1394->fps;
bb270c08 216 res = dc1394->packet.size;
f02be79d
RS
217 } else {
218 av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
219 dc1394->packet.data = NULL;
115329f1 220 res = -1;
f02be79d 221 }
115329f1 222
f02be79d
RS
223 *pkt = dc1394->packet;
224 return res;
225}
226
0b4a1821 227static int dc1394_v1_close(AVFormatContext * context)
f02be79d
RS
228{
229 struct dc1394_data *dc1394 = context->priv_data;
230
231 dc1394_stop_iso_transmission(dc1394->handle, dc1394->camera.node);
232 dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
233 dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
234 dc1394_destroy_handle(dc1394->handle);
115329f1 235
f02be79d
RS
236 return 0;
237}
238
49fb20cb 239#elif HAVE_LIBDC1394_2
0b4a1821
AS
240static int dc1394_v2_read_header(AVFormatContext *c, AVFormatParameters * ap)
241{
242 dc1394_data* dc1394 = c->priv_data;
243 dc1394camera_list_t *list;
244 int res, i;
245 struct dc1394_frame_format *fmt = NULL;
246 struct dc1394_frame_rate *fps = NULL;
247
248 if (dc1394_read_common(c,ap,&fmt,&fps) != 0)
249 return -1;
250
ca74c0a1 251 /* Now let us prep the hardware. */
0b4a1821
AS
252 dc1394->d = dc1394_new();
253 dc1394_camera_enumerate (dc1394->d, &list);
254 if ( !list || list->num == 0) {
255 av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n");
256 goto out;
257 }
258
259 /* FIXME: To select a specific camera I need to search in list its guid */
260 dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
261 if (list->num > 1) {
262 av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
263 }
264
265 /* Freeing list of cameras */
266 dc1394_camera_free_list (list);
267
268 /* Select MAX Speed possible from the cam */
269 if (dc1394->camera->bmode_capable>0) {
270 dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
271 i = DC1394_ISO_SPEED_800;
272 } else {
273 i = DC1394_ISO_SPEED_400;
274 }
275
276 for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
277 res=dc1394_video_set_iso_speed(dc1394->camera, i);
278 }
279 if (res != DC1394_SUCCESS) {
280 av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
281 goto out_camera;
282 }
283
284 if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
285 av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
286 goto out_camera;
287 }
288
289 if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
290 av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
291 goto out_camera;
292 }
293 if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
294 av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
295 goto out_camera;
296 }
297
298 if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
299 av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
300 goto out_camera;
301 }
302 return 0;
303
304out_camera:
305 dc1394_capture_stop(dc1394->camera);
306 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
307 dc1394_camera_free (dc1394->camera);
308out:
309 dc1394_free(dc1394->d);
310 return -1;
311}
312
313static int dc1394_v2_read_packet(AVFormatContext *c, AVPacket *pkt)
314{
315 struct dc1394_data *dc1394 = c->priv_data;
316 int res;
317
318 /* discard stale frame */
319 if (dc1394->current_frame++) {
320 if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
321 av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
322 }
323
324 res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
325 if (res == DC1394_SUCCESS) {
326 dc1394->packet.data = (uint8_t *)(dc1394->frame->image);
327 dc1394->packet.pts = (dc1394->current_frame * 1000000) / (dc1394->fps);
328 res = dc1394->frame->image_bytes;
329 } else {
330 av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
331 dc1394->packet.data = NULL;
332 res = -1;
333 }
334
335 *pkt = dc1394->packet;
336 return res;
337}
338
339static int dc1394_v2_close(AVFormatContext * context)
340{
341 struct dc1394_data *dc1394 = context->priv_data;
342
343 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
344 dc1394_capture_stop(dc1394->camera);
345 dc1394_camera_free(dc1394->camera);
346 dc1394_free(dc1394->d);
347
348 return 0;
349}
350
351AVInputFormat libdc1394_demuxer = {
352 .name = "libdc1394",
bde15e74 353 .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
0b4a1821
AS
354 .priv_data_size = sizeof(struct dc1394_data),
355 .read_header = dc1394_v2_read_header,
356 .read_packet = dc1394_v2_read_packet,
357 .read_close = dc1394_v2_close,
358 .flags = AVFMT_NOFILE
359};
360
361#endif
49fb20cb 362#if HAVE_LIBDC1394_1
f7b8bffe 363AVInputFormat libdc1394_demuxer = {
5fdb9cc5 364 .name = "libdc1394",
bde15e74 365 .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.1 A/V grab"),
f02be79d 366 .priv_data_size = sizeof(struct dc1394_data),
0b4a1821
AS
367 .read_header = dc1394_v1_read_header,
368 .read_packet = dc1394_v1_read_packet,
369 .read_close = dc1394_v1_close,
f02be79d
RS
370 .flags = AVFMT_NOFILE
371};
0b4a1821 372#endif