~medibuntu-maintainers/mplayer/medibuntu.quantal

« back to all changes in this revision

Viewing changes to ffmpeg/libavdevice/libdc1394.c

  • Committer: Gauvain Pocentek
  • Date: 2011-08-21 07:22:23 UTC
  • mfrom: (66.1.11 oneiric)
  • Revision ID: gauvain@pocentek.net-20110821072223-ummeossdz7okpb3d
* Merge from Ubuntu:
  - put back faac support
  - recommends apport-hooks-medibuntu
  - change Maintainer, Uploaders & Vcs-* fields.
* New upstream snapshot
  - update 23mplayer-debug-printf.patch
  - fixes miscompilation with gcc 4.6, Closes: #623304
  - improved internal mkv demuxer, Closes: #595452
  - Fixed segfault due to missing sanitation on playlist files,
    Closes: #591525
  - Fixed byteorder on 16-bit displays, Closes: #594093
  - tighten build depends on libav
  - --enable-largefile switch has been dropped
  - add build dependency on yasm
* Fix build dependency on libjpeg-dev, Closes: #634277
* rewrite debian/copyright in DEP5 format
* fix clean target
* don't remove snapshot_version file
* enable XVID, MP3 and X264 encoders
* simply architecture specific dependencies, Closes: #634773
* make buildlogs verbose
* unbreak building mplayer-doc package
* don't fail debian package build if not all shlibdeps information could be retrieved
* update configure flags for static libav* libraries
* fix spelling in mplayer-dbg description, Closes: #617826
* enable blueray support, Closes: #577761
* Select oss as default audio output module on kFreeBSD, Closes: #598431
* Update documentation with regard to our modifications to the upstream tarball.
* really no longer build mplayer-gui, Closes: #612473
* simplify/remove instruction to get upstream sources
* normalize debian/{control,copyright,mplayer.install} with wrap-and-sort
* bump standards version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 Libav.
 
7
 *
 
8
 * Libav 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
 * Libav 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 Libav; if not, write to the Free Software
 
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
21
 */
 
22
 
 
23
#include "config.h"
 
24
#include "libavformat/avformat.h"
 
25
#include "libavutil/log.h"
 
26
#include "libavutil/opt.h"
 
27
#include "libavutil/parseutils.h"
 
28
#include "libavutil/pixdesc.h"
 
29
 
 
30
#if HAVE_LIBDC1394_2
 
31
#include <dc1394/dc1394.h>
 
32
#elif HAVE_LIBDC1394_1
 
33
#include <libraw1394/raw1394.h>
 
34
#include <libdc1394/dc1394_control.h>
 
35
 
 
36
#define DC1394_VIDEO_MODE_320x240_YUV422 MODE_320x240_YUV422
 
37
#define DC1394_VIDEO_MODE_640x480_YUV411 MODE_640x480_YUV411
 
38
#define DC1394_VIDEO_MODE_640x480_YUV422 MODE_640x480_YUV422
 
39
#define DC1394_FRAMERATE_1_875 FRAMERATE_1_875
 
40
#define DC1394_FRAMERATE_3_75  FRAMERATE_3_75
 
41
#define DC1394_FRAMERATE_7_5   FRAMERATE_7_5
 
42
#define DC1394_FRAMERATE_15    FRAMERATE_15
 
43
#define DC1394_FRAMERATE_30    FRAMERATE_30
 
44
#define DC1394_FRAMERATE_60    FRAMERATE_60
 
45
#define DC1394_FRAMERATE_120   FRAMERATE_120
 
46
#define DC1394_FRAMERATE_240   FRAMERATE_240
 
47
#endif
 
48
 
 
49
#undef free
 
50
 
 
51
typedef struct dc1394_data {
 
52
    AVClass *class;
 
53
#if HAVE_LIBDC1394_1
 
54
    raw1394handle_t handle;
 
55
    dc1394_cameracapture camera;
 
56
    int channel;
 
57
#elif HAVE_LIBDC1394_2
 
58
    dc1394_t *d;
 
59
    dc1394camera_t *camera;
 
60
    dc1394video_frame_t *frame;
 
61
#endif
 
62
    int current_frame;
 
63
    int  frame_rate;        /**< frames per 1000 seconds (fps * 1000) */
 
64
    char *video_size;       /**< String describing video size, set by a private option. */
 
65
    char *pixel_format;     /**< Set by a private option. */
 
66
    char *framerate;        /**< Set by a private option. */
 
67
 
 
68
    AVPacket packet;
 
69
} dc1394_data;
 
70
 
 
71
struct dc1394_frame_format {
 
72
    int width;
 
73
    int height;
 
74
    enum PixelFormat pix_fmt;
 
75
    int frame_size_id;
 
76
} dc1394_frame_formats[] = {
 
77
    { 320, 240, PIX_FMT_UYVY422, DC1394_VIDEO_MODE_320x240_YUV422 },
 
78
    { 640, 480, PIX_FMT_UYYVYY411, DC1394_VIDEO_MODE_640x480_YUV411 },
 
79
    { 640, 480, PIX_FMT_UYVY422, DC1394_VIDEO_MODE_640x480_YUV422 },
 
80
    { 0, 0, 0, 0 } /* gotta be the last one */
 
81
};
 
82
 
 
83
struct dc1394_frame_rate {
 
84
    int frame_rate;
 
85
    int frame_rate_id;
 
86
} dc1394_frame_rates[] = {
 
87
    {  1875, DC1394_FRAMERATE_1_875 },
 
88
    {  3750, DC1394_FRAMERATE_3_75  },
 
89
    {  7500, DC1394_FRAMERATE_7_5   },
 
90
    { 15000, DC1394_FRAMERATE_15    },
 
91
    { 30000, DC1394_FRAMERATE_30    },
 
92
    { 60000, DC1394_FRAMERATE_60    },
 
93
    {120000, DC1394_FRAMERATE_120   },
 
94
    {240000, DC1394_FRAMERATE_240    },
 
95
    { 0, 0 } /* gotta be the last one */
 
96
};
 
97
 
 
98
#define OFFSET(x) offsetof(dc1394_data, x)
 
99
#define DEC AV_OPT_FLAG_DECODING_PARAM
 
100
static const AVOption options[] = {
 
101
#if HAVE_LIBDC1394_1
 
102
    { "channel", "", offsetof(dc1394_data, channel), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
 
103
#endif
 
104
    { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = "qvga"}, 0, 0, DEC },
 
105
    { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = "uyvy422"}, 0, 0, DEC },
 
106
    { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
 
107
    { NULL },
 
108
};
 
109
 
 
110
static const AVClass libdc1394_class = {
 
111
    .class_name = "libdc1394 indev",
 
112
    .item_name  = av_default_item_name,
 
113
    .option     = options,
 
114
    .version    = LIBAVUTIL_VERSION_INT,
 
115
};
 
116
 
 
117
 
 
118
static inline int dc1394_read_common(AVFormatContext *c, AVFormatParameters *ap,
 
119
                                     struct dc1394_frame_format **select_fmt, struct dc1394_frame_rate **select_fps)
 
120
{
 
121
    dc1394_data* dc1394 = c->priv_data;
 
122
    AVStream* vst;
 
123
    struct dc1394_frame_format *fmt;
 
124
    struct dc1394_frame_rate *fps;
 
125
    enum PixelFormat pix_fmt;
 
126
    int width, height;
 
127
    AVRational framerate;
 
128
    int ret = 0;
 
129
 
 
130
    if ((pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == PIX_FMT_NONE) {
 
131
        av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format);
 
132
        ret = AVERROR(EINVAL);
 
133
        goto out;
 
134
    }
 
135
 
 
136
    if ((ret = av_parse_video_size(&width, &height, dc1394->video_size)) < 0) {
 
137
        av_log(c, AV_LOG_ERROR, "Couldn't parse video size.\n");
 
138
        goto out;
 
139
    }
 
140
    if ((ret = av_parse_video_rate(&framerate, dc1394->framerate)) < 0) {
 
141
        av_log(c, AV_LOG_ERROR, "Couldn't parse framerate.\n");
 
142
        goto out;
 
143
    }
 
144
#if FF_API_FORMAT_PARAMETERS
 
145
    if (ap->width > 0)
 
146
        width = ap->width;
 
147
    if (ap->height > 0)
 
148
        height = ap->height;
 
149
    if (ap->pix_fmt)
 
150
        pix_fmt = ap->pix_fmt;
 
151
    if (ap->time_base.num)
 
152
        framerate = (AVRational){ap->time_base.den, ap->time_base.num};
 
153
#endif
 
154
    dc1394->frame_rate = av_rescale(1000, framerate.num, framerate.den);
 
155
 
 
156
    for (fmt = dc1394_frame_formats; fmt->width; fmt++)
 
157
         if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
 
158
             break;
 
159
 
 
160
    for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
 
161
         if (fps->frame_rate == dc1394->frame_rate)
 
162
             break;
 
163
 
 
164
    if (!fps->frame_rate || !fmt->width) {
 
165
        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),
 
166
                                                                                                width, height, dc1394->frame_rate);
 
167
        ret = AVERROR(EINVAL);
 
168
        goto out;
 
169
    }
 
170
 
 
171
    /* create a video stream */
 
172
    vst = av_new_stream(c, 0);
 
173
    if (!vst) {
 
174
        ret = AVERROR(ENOMEM);
 
175
        goto out;
 
176
    }
 
177
    av_set_pts_info(vst, 64, 1, 1000);
 
178
    vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
 
179
    vst->codec->codec_id = CODEC_ID_RAWVIDEO;
 
180
    vst->codec->time_base.den = framerate.num;
 
181
    vst->codec->time_base.num = framerate.den;
 
182
    vst->codec->width = fmt->width;
 
183
    vst->codec->height = fmt->height;
 
184
    vst->codec->pix_fmt = fmt->pix_fmt;
 
185
 
 
186
    /* packet init */
 
187
    av_init_packet(&dc1394->packet);
 
188
    dc1394->packet.size = avpicture_get_size(fmt->pix_fmt, fmt->width, fmt->height);
 
189
    dc1394->packet.stream_index = vst->index;
 
190
    dc1394->packet.flags |= AV_PKT_FLAG_KEY;
 
191
 
 
192
    dc1394->current_frame = 0;
 
193
 
 
194
    vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000);
 
195
    *select_fps = fps;
 
196
    *select_fmt = fmt;
 
197
out:
 
198
    return ret;
 
199
}
 
200
 
 
201
#if HAVE_LIBDC1394_1
 
202
static int dc1394_v1_read_header(AVFormatContext *c, AVFormatParameters * ap)
 
203
{
 
204
    dc1394_data* dc1394 = c->priv_data;
 
205
    AVStream* vst;
 
206
    nodeid_t* camera_nodes;
 
207
    int res;
 
208
    struct dc1394_frame_format *fmt = NULL;
 
209
    struct dc1394_frame_rate *fps = NULL;
 
210
 
 
211
    if (dc1394_read_common(c,ap,&fmt,&fps) != 0)
 
212
        return -1;
 
213
 
 
214
#if FF_API_FORMAT_PARAMETERS
 
215
    if (ap->channel)
 
216
        dc1394->channel = ap->channel;
 
217
#endif
 
218
 
 
219
    /* Now let us prep the hardware. */
 
220
    dc1394->handle = dc1394_create_handle(0); /* FIXME: gotta have ap->port */
 
221
    if (!dc1394->handle) {
 
222
        av_log(c, AV_LOG_ERROR, "Can't acquire dc1394 handle on port %d\n", 0 /* ap->port */);
 
223
        goto out;
 
224
    }
 
225
    camera_nodes = dc1394_get_camera_nodes(dc1394->handle, &res, 1);
 
226
    if (!camera_nodes || camera_nodes[dc1394->channel] == DC1394_NO_CAMERA) {
 
227
        av_log(c, AV_LOG_ERROR, "There's no IIDC camera on the channel %d\n", dc1394->channel);
 
228
        goto out_handle;
 
229
    }
 
230
    res = dc1394_dma_setup_capture(dc1394->handle, camera_nodes[dc1394->channel],
 
231
                                   0,
 
232
                                   FORMAT_VGA_NONCOMPRESSED,
 
233
                                   fmt->frame_size_id,
 
234
                                   SPEED_400,
 
235
                                   fps->frame_rate_id, 8, 1,
 
236
                                   c->filename,
 
237
                                   &dc1394->camera);
 
238
    dc1394_free_camera_nodes(camera_nodes);
 
239
    if (res != DC1394_SUCCESS) {
 
240
        av_log(c, AV_LOG_ERROR, "Can't prepare camera for the DMA capture\n");
 
241
        goto out_handle;
 
242
    }
 
243
 
 
244
    res = dc1394_start_iso_transmission(dc1394->handle, dc1394->camera.node);
 
245
    if (res != DC1394_SUCCESS) {
 
246
        av_log(c, AV_LOG_ERROR, "Can't start isochronous transmission\n");
 
247
        goto out_handle_dma;
 
248
    }
 
249
 
 
250
    return 0;
 
251
 
 
252
out_handle_dma:
 
253
    dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
 
254
    dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
 
255
out_handle:
 
256
    dc1394_destroy_handle(dc1394->handle);
 
257
out:
 
258
    return -1;
 
259
}
 
260
 
 
261
static int dc1394_v1_read_packet(AVFormatContext *c, AVPacket *pkt)
 
262
{
 
263
    struct dc1394_data *dc1394 = c->priv_data;
 
264
    int res;
 
265
 
 
266
    /* discard stale frame */
 
267
    if (dc1394->current_frame++) {
 
268
        if (dc1394_dma_done_with_buffer(&dc1394->camera) != DC1394_SUCCESS)
 
269
            av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
 
270
    }
 
271
 
 
272
    res = dc1394_dma_single_capture(&dc1394->camera);
 
273
 
 
274
    if (res == DC1394_SUCCESS) {
 
275
        dc1394->packet.data = (uint8_t *)(dc1394->camera.capture_buffer);
 
276
        dc1394->packet.pts = (dc1394->current_frame * 1000000) / dc1394->frame_rate;
 
277
        res = dc1394->packet.size;
 
278
    } else {
 
279
        av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
 
280
        dc1394->packet.data = NULL;
 
281
        res = -1;
 
282
    }
 
283
 
 
284
    *pkt = dc1394->packet;
 
285
    return res;
 
286
}
 
287
 
 
288
static int dc1394_v1_close(AVFormatContext * context)
 
289
{
 
290
    struct dc1394_data *dc1394 = context->priv_data;
 
291
 
 
292
    dc1394_stop_iso_transmission(dc1394->handle, dc1394->camera.node);
 
293
    dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
 
294
    dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
 
295
    dc1394_destroy_handle(dc1394->handle);
 
296
 
 
297
    return 0;
 
298
}
 
299
 
 
300
#elif HAVE_LIBDC1394_2
 
301
static int dc1394_v2_read_header(AVFormatContext *c, AVFormatParameters * ap)
 
302
{
 
303
    dc1394_data* dc1394 = c->priv_data;
 
304
    dc1394camera_list_t *list;
 
305
    int res, i;
 
306
    struct dc1394_frame_format *fmt = NULL;
 
307
    struct dc1394_frame_rate *fps = NULL;
 
308
 
 
309
    if (dc1394_read_common(c,ap,&fmt,&fps) != 0)
 
310
       return -1;
 
311
 
 
312
    /* Now let us prep the hardware. */
 
313
    dc1394->d = dc1394_new();
 
314
    dc1394_camera_enumerate (dc1394->d, &list);
 
315
    if ( !list || list->num == 0) {
 
316
        av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n");
 
317
        goto out;
 
318
    }
 
319
 
 
320
    /* FIXME: To select a specific camera I need to search in list its guid */
 
321
    dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
 
322
    if (list->num > 1) {
 
323
        av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
 
324
    }
 
325
 
 
326
    /* Freeing list of cameras */
 
327
    dc1394_camera_free_list (list);
 
328
 
 
329
    /* Select MAX Speed possible from the cam */
 
330
    if (dc1394->camera->bmode_capable>0) {
 
331
       dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
 
332
       i = DC1394_ISO_SPEED_800;
 
333
    } else {
 
334
       i = DC1394_ISO_SPEED_400;
 
335
    }
 
336
 
 
337
    for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
 
338
            res=dc1394_video_set_iso_speed(dc1394->camera, i);
 
339
    }
 
340
    if (res != DC1394_SUCCESS) {
 
341
        av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
 
342
        goto out_camera;
 
343
    }
 
344
 
 
345
    if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
 
346
        av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
 
347
        goto out_camera;
 
348
    }
 
349
 
 
350
    if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
 
351
        av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
 
352
        goto out_camera;
 
353
    }
 
354
    if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
 
355
        av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
 
356
        goto out_camera;
 
357
    }
 
358
 
 
359
    if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
 
360
        av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
 
361
        goto out_camera;
 
362
    }
 
363
    return 0;
 
364
 
 
365
out_camera:
 
366
    dc1394_capture_stop(dc1394->camera);
 
367
    dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
 
368
    dc1394_camera_free (dc1394->camera);
 
369
out:
 
370
    dc1394_free(dc1394->d);
 
371
    return -1;
 
372
}
 
373
 
 
374
static int dc1394_v2_read_packet(AVFormatContext *c, AVPacket *pkt)
 
375
{
 
376
    struct dc1394_data *dc1394 = c->priv_data;
 
377
    int res;
 
378
 
 
379
    /* discard stale frame */
 
380
    if (dc1394->current_frame++) {
 
381
        if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
 
382
            av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
 
383
    }
 
384
 
 
385
    res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
 
386
    if (res == DC1394_SUCCESS) {
 
387
        dc1394->packet.data = (uint8_t *)(dc1394->frame->image);
 
388
        dc1394->packet.pts = (dc1394->current_frame  * 1000000) / (dc1394->frame_rate);
 
389
        res = dc1394->frame->image_bytes;
 
390
    } else {
 
391
        av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
 
392
        dc1394->packet.data = NULL;
 
393
        res = -1;
 
394
    }
 
395
 
 
396
    *pkt = dc1394->packet;
 
397
    return res;
 
398
}
 
399
 
 
400
static int dc1394_v2_close(AVFormatContext * context)
 
401
{
 
402
    struct dc1394_data *dc1394 = context->priv_data;
 
403
 
 
404
    dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
 
405
    dc1394_capture_stop(dc1394->camera);
 
406
    dc1394_camera_free(dc1394->camera);
 
407
    dc1394_free(dc1394->d);
 
408
 
 
409
    return 0;
 
410
}
 
411
 
 
412
AVInputFormat ff_libdc1394_demuxer = {
 
413
    .name           = "libdc1394",
 
414
    .long_name      = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
 
415
    .priv_data_size = sizeof(struct dc1394_data),
 
416
    .read_header    = dc1394_v2_read_header,
 
417
    .read_packet    = dc1394_v2_read_packet,
 
418
    .read_close     = dc1394_v2_close,
 
419
    .flags          = AVFMT_NOFILE,
 
420
    .priv_class     = &libdc1394_class,
 
421
};
 
422
 
 
423
#endif
 
424
#if HAVE_LIBDC1394_1
 
425
AVInputFormat ff_libdc1394_demuxer = {
 
426
    .name           = "libdc1394",
 
427
    .long_name      = NULL_IF_CONFIG_SMALL("dc1394 v.1 A/V grab"),
 
428
    .priv_data_size = sizeof(struct dc1394_data),
 
429
    .read_header    = dc1394_v1_read_header,
 
430
    .read_packet    = dc1394_v1_read_packet,
 
431
    .read_close     = dc1394_v1_close,
 
432
    .flags          = AVFMT_NOFILE,
 
433
    .priv_class     = &libdc1394_class,
 
434
};
 
435
#endif