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