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