]> git.sesse.net Git - ffmpeg/blob - libavdevice/libdc1394.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavdevice / libdc1394.c
1 /*
2  * IIDC1394 grab interface (uses libdc1394 and libraw1394)
3  * Copyright (c) 2004 Roman Shaposhnik
4  * Copyright (c) 2008 Alessandro Sappia
5  * Copyright (c) 2011 Martin Lambers
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include "config.h"
25 #include "libavutil/log.h"
26 #include "libavutil/opt.h"
27 #include "avdevice.h"
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include "libavutil/parseutils.h"
32
33 #include <dc1394/dc1394.h>
34
35 #undef free
36
37 typedef struct dc1394_data {
38     AVClass *class;
39     dc1394_t *d;
40     dc1394camera_t *camera;
41     dc1394video_frame_t *frame;
42     int current_frame;
43     int fps;
44     char *video_size;       /**< String describing video size, set by a private option. */
45
46     AVPacket packet;
47 } dc1394_data;
48
49 /* The list of color codings that we support.
50  * We assume big endian for the dc1394 16bit modes: libdc1394 never sets the
51  * flag little_endian in dc1394video_frame_t. */
52 struct dc1394_color_coding {
53     int pix_fmt;
54     int score;
55     uint32_t coding;
56 } dc1394_color_codings[] = {
57     { PIX_FMT_GRAY16BE,  1000, DC1394_COLOR_CODING_MONO16 },
58     { PIX_FMT_RGB48BE,   1100, DC1394_COLOR_CODING_RGB16  },
59     { PIX_FMT_GRAY8,     1200, DC1394_COLOR_CODING_MONO8  },
60     { PIX_FMT_RGB24,     1300, DC1394_COLOR_CODING_RGB8   },
61     { PIX_FMT_UYYVYY411, 1400, DC1394_COLOR_CODING_YUV411 },
62     { PIX_FMT_UYVY422,   1500, DC1394_COLOR_CODING_YUV422 },
63     { PIX_FMT_NONE, 0, 0 } /* gotta be the last one */
64 };
65
66 struct dc1394_frame_rate {
67     int frame_rate;
68     int frame_rate_id;
69 } dc1394_frame_rates[] = {
70     {  1875, DC1394_FRAMERATE_1_875 },
71     {  3750, DC1394_FRAMERATE_3_75  },
72     {  7500, DC1394_FRAMERATE_7_5   },
73     { 15000, DC1394_FRAMERATE_15    },
74     { 30000, DC1394_FRAMERATE_30    },
75     { 60000, DC1394_FRAMERATE_60    },
76     {120000, DC1394_FRAMERATE_120   },
77     {240000, DC1394_FRAMERATE_240    },
78     { 0, 0 } /* gotta be the last one */
79 };
80
81 #define OFFSET(x) offsetof(dc1394_data, x)
82 #define DEC AV_OPT_FLAG_DECODING_PARAM
83 static const AVOption options[] = {
84     { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = "qvga"}, 0, 0, DEC },
85     { NULL },
86 };
87
88 static 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 static int dc1394_read_header(AVFormatContext *c, AVFormatParameters * ap)
96 {
97     dc1394_data* dc1394 = c->priv_data;
98     AVStream *vst;
99     const struct dc1394_color_coding *cc;
100     const struct dc1394_frame_rate *fr;
101     dc1394camera_list_t *list;
102     dc1394video_modes_t video_modes;
103     dc1394video_mode_t video_mode;
104     dc1394framerates_t frame_rates;
105     dc1394framerate_t frame_rate;
106     uint32_t dc1394_width, dc1394_height, dc1394_color_coding;
107     int rate, best_rate;
108     int score, max_score;
109     int final_width, final_height, final_pix_fmt, final_frame_rate;
110     int res, i, j;
111     int ret=-1;
112
113     /* Now let us prep the hardware. */
114     dc1394->d = dc1394_new();
115     dc1394_camera_enumerate (dc1394->d, &list);
116     if ( !list || list->num == 0) {
117         av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n");
118         goto out;
119     }
120
121     /* FIXME: To select a specific camera I need to search in list its guid */
122     dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
123     if (list->num > 1) {
124         av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
125     }
126
127     /* Freeing list of cameras */
128     dc1394_camera_free_list (list);
129
130     /* Get the list of video modes supported by the camera. */
131     res = dc1394_video_get_supported_modes (dc1394->camera, &video_modes);
132     if (res != DC1394_SUCCESS) {
133         av_log(c, AV_LOG_ERROR, "Could not get video formats.\n");
134         goto out_camera;
135     }
136
137     if (dc1394->video_size) {
138         if ((ret = av_parse_video_size(&ap->width, &ap->height, dc1394->video_size)) < 0) {
139             av_log(c, AV_LOG_ERROR, "Couldn't parse video size.\n");
140             goto out;
141         }
142     }
143     
144     /* Choose the best mode. */
145     rate = (ap->time_base.num ? av_rescale(1000, ap->time_base.den, ap->time_base.num) : -1);
146     max_score = -1;
147     for (i = 0; i < video_modes.num; i++) {
148         if (video_modes.modes[i] == DC1394_VIDEO_MODE_EXIF
149                 || (video_modes.modes[i] >= DC1394_VIDEO_MODE_FORMAT7_MIN
150                     && video_modes.modes[i] <= DC1394_VIDEO_MODE_FORMAT7_MAX)) {
151             /* These modes are currently not supported as they would require
152              * much more work. For the remaining modes, the functions
153              * dc1394_get_image_size_from_video_mode and
154              * dc1394_get_color_coding_from_video_mode do not need to query the
155              * camera, and thus cannot fail. */
156             continue;
157         }
158         dc1394_get_color_coding_from_video_mode (NULL, video_modes.modes[i],
159                 &dc1394_color_coding);
160         for (cc = dc1394_color_codings; cc->pix_fmt != PIX_FMT_NONE; cc++)
161             if (cc->coding == dc1394_color_coding)
162                 break;
163         if (cc->pix_fmt == PIX_FMT_NONE) {
164             /* We currently cannot handle this color coding. */
165             continue;
166         }
167         /* Here we know that the mode is supported. Get its frame size and the list
168          * of frame rates supported by the camera for this mode. This list is sorted
169          * in ascending order according to libdc1394 example programs. */
170         dc1394_get_image_size_from_video_mode (NULL, video_modes.modes[i],
171                 &dc1394_width, &dc1394_height);
172         res = dc1394_video_get_supported_framerates (dc1394->camera, video_modes.modes[i],
173                 &frame_rates);
174         if (res != DC1394_SUCCESS || frame_rates.num == 0) {
175             av_log(c, AV_LOG_ERROR, "Cannot get frame rates for video mode.\n");
176             goto out_camera;
177         }
178         /* Choose the best frame rate. */
179         best_rate = -1;
180         for (j = 0; j < frame_rates.num; j++) {
181             for (fr = dc1394_frame_rates; fr->frame_rate; fr++) {
182                 if (fr->frame_rate_id == frame_rates.framerates[j]) {
183                     break;
184                 }
185             }
186             if (!fr->frame_rate) {
187                 /* This frame rate is not supported. */
188                 continue;
189             }
190             best_rate = fr->frame_rate;
191             frame_rate = fr->frame_rate_id;
192             if (ap->time_base.num && rate == fr->frame_rate) {
193                 /* This is the requested frame rate. */
194                 break;
195             }
196         }
197         if (best_rate == -1) {
198             /* No supported rate found. */
199             continue;
200         }
201         /* Here we know that both the mode and the rate are supported. Compute score. */
202         if (ap->width && ap->height
203                 && (dc1394_width == ap->width && dc1394_height == ap->height)) {
204             score = 110000;
205         } else {
206             score = dc1394_width * 10;  // 1600 - 16000
207         }
208         if (ap->pix_fmt == cc->pix_fmt) {
209             score += 90000;
210         } else {
211             score += cc->score;         // 1000 - 1500
212         }
213         if (ap->time_base.num && rate == best_rate) {
214             score += 70000;
215         } else {
216             score += best_rate / 1000;  // 1 - 240
217         }
218         if (score > max_score) {
219             video_mode = video_modes.modes[i];
220             final_width = dc1394_width;
221             final_height = dc1394_height;
222             final_pix_fmt = cc->pix_fmt;
223             final_frame_rate = best_rate;
224             max_score = score;
225         }
226     }
227     if (max_score == -1) {
228         av_log(c, AV_LOG_ERROR, "No suitable video mode / frame rate available.\n");
229         goto out_camera;
230     }
231     if (ap->width && ap->height && !(ap->width == final_width && ap->height == final_height)) {
232         av_log(c, AV_LOG_WARNING, "Requested frame size is not available, using fallback.\n");
233     }
234     if (ap->pix_fmt != PIX_FMT_NONE && ap->pix_fmt != final_pix_fmt) {
235         av_log(c, AV_LOG_WARNING, "Requested pixel format is not supported, using fallback.\n");
236     }
237     if (ap->time_base.num && rate != final_frame_rate) {
238         av_log(c, AV_LOG_WARNING, "Requested frame rate is not available, using fallback.\n");
239     }
240
241     /* create a video stream */
242     vst = av_new_stream(c, 0);
243     if (!vst)
244         goto out_camera;
245     av_set_pts_info(vst, 64, 1, 1000);
246     vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
247     vst->codec->codec_id = CODEC_ID_RAWVIDEO;
248     vst->codec->time_base.den = final_frame_rate;
249     vst->codec->time_base.num = 1000;
250     vst->codec->width = final_width;
251     vst->codec->height = final_height;
252     vst->codec->pix_fmt = final_pix_fmt;
253
254     /* packet init */
255     av_init_packet(&dc1394->packet);
256     dc1394->packet.size = avpicture_get_size(final_pix_fmt, final_width, final_height);
257     dc1394->packet.stream_index = vst->index;
258     dc1394->packet.flags |= AV_PKT_FLAG_KEY;
259
260     dc1394->current_frame = 0;
261     dc1394->fps = final_frame_rate;
262
263     vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, final_frame_rate, 1000);
264
265     /* Select MAX Speed possible from the cam */
266     if (dc1394->camera->bmode_capable>0) {
267        dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
268        i = DC1394_ISO_SPEED_800;
269     } else {
270        i = DC1394_ISO_SPEED_400;
271     }
272
273     for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
274             res=dc1394_video_set_iso_speed(dc1394->camera, i);
275     }
276     if (res != DC1394_SUCCESS) {
277         av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
278         goto out_camera;
279     }
280
281     if (dc1394_video_set_mode(dc1394->camera, video_mode) != DC1394_SUCCESS) {
282         av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
283         goto out_camera;
284     }
285
286     if (dc1394_video_set_framerate(dc1394->camera, frame_rate) != DC1394_SUCCESS) {
287         av_log(c, AV_LOG_ERROR, "Could not set framerate %d.\n", final_frame_rate);
288         goto out_camera;
289     }
290     if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
291         av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
292         goto out_camera;
293     }
294
295     if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
296         av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
297         goto out_camera;
298     }
299     return 0;
300
301 out_camera:
302     dc1394_capture_stop(dc1394->camera);
303     dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
304     dc1394_camera_free (dc1394->camera);
305 out:
306     dc1394_free(dc1394->d);
307     return ret;
308 }
309
310 static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt)
311 {
312     struct dc1394_data *dc1394 = c->priv_data;
313     int res;
314
315     /* discard stale frame */
316     if (dc1394->current_frame++) {
317         if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
318             av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
319     }
320
321     res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
322     if (res == DC1394_SUCCESS) {
323         dc1394->packet.data = (uint8_t *)(dc1394->frame->image);
324         dc1394->packet.pts = (dc1394->current_frame  * 1000000) / (dc1394->fps);
325         res = dc1394->frame->image_bytes;
326     } else {
327         av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
328         dc1394->packet.data = NULL;
329         res = -1;
330     }
331
332     *pkt = dc1394->packet;
333     return res;
334 }
335
336 static int dc1394_close(AVFormatContext * context)
337 {
338     struct dc1394_data *dc1394 = context->priv_data;
339
340     dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
341     dc1394_capture_stop(dc1394->camera);
342     dc1394_camera_free(dc1394->camera);
343     dc1394_free(dc1394->d);
344
345     return 0;
346 }
347
348 AVInputFormat ff_libdc1394_demuxer = {
349     .name           = "libdc1394",
350     .long_name      = NULL_IF_CONFIG_SMALL("dc1394 A/V grab"),
351     .priv_data_size = sizeof(struct dc1394_data),
352     .read_header    = dc1394_read_header,
353     .read_packet    = dc1394_read_packet,
354     .read_close     = dc1394_close,
355     .flags          = AVFMT_NOFILE,
356     .priv_class     = &libdc1394_class,
357 };