OSDN Git Service

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