Statistics
| Branch: | Revision:

ffmpeg / libavdevice / dv1394.c @ b0067549

History | View | Annotate | Download (6.8 KB)

1
/*
2
 * Linux DV1394 interface
3
 * Copyright (c) 2003 Max Krasnyansky <maxk@qualcomm.com>
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#include "config.h"
23
#include <unistd.h>
24
#include <fcntl.h>
25
#include <errno.h>
26
#include <sys/ioctl.h>
27
#ifdef HAVE_SYS_MMAN_H
28
#include <sys/mman.h>
29
#endif
30
#include <sys/poll.h>
31
#include <sys/time.h>
32
#include <time.h>
33

    
34
#include "avformat.h"
35

    
36
#undef DV1394_DEBUG
37

    
38
#include "dv1394.h"
39
#include "dv.h"
40

    
41
struct dv1394_data {
42
    int fd;
43
    int channel;
44
    int format;
45

    
46
    uint8_t *ring; /* Ring buffer */
47
    int index;  /* Current frame index */
48
    int avail;  /* Number of frames available for reading */
49
    int done;   /* Number of completed frames */
50

    
51
    DVDemuxContext* dv_demux; /* Generic DV muxing/demuxing context */
52
};
53

    
54
/*
55
 * The trick here is to kludge around well known problem with kernel Ooopsing
56
 * when you try to capture PAL on a device node configure for NTSC. That's
57
 * why we have to configure the device node for PAL, and then read only NTSC
58
 * amount of data.
59
 */
60
static int dv1394_reset(struct dv1394_data *dv)
61
{
62
    struct dv1394_init init;
63

    
64
    init.channel     = dv->channel;
65
    init.api_version = DV1394_API_VERSION;
66
    init.n_frames    = DV1394_RING_FRAMES;
67
    init.format      = DV1394_PAL;
68

    
69
    if (ioctl(dv->fd, DV1394_INIT, &init) < 0)
70
        return -1;
71

    
72
    dv->avail  = dv->done = 0;
73
    return 0;
74
}
75

    
76
static int dv1394_start(struct dv1394_data *dv)
77
{
78
    /* Tell DV1394 driver to enable receiver */
79
    if (ioctl(dv->fd, DV1394_START_RECEIVE, 0) < 0) {
80
        av_log(NULL, AV_LOG_ERROR, "Failed to start receiver: %s\n", strerror(errno));
81
        return -1;
82
    }
83
    return 0;
84
}
85

    
86
static int dv1394_read_header(AVFormatContext * context, AVFormatParameters * ap)
87
{
88
    struct dv1394_data *dv = context->priv_data;
89

    
90
    dv->dv_demux = dv_init_demux(context);
91
    if (!dv->dv_demux)
92
        goto failed;
93

    
94
    if (ap->standard && !strcasecmp(ap->standard, "pal"))
95
        dv->format = DV1394_PAL;
96
    else
97
        dv->format = DV1394_NTSC;
98

    
99
    if (ap->channel)
100
        dv->channel = ap->channel;
101
    else
102
        dv->channel = DV1394_DEFAULT_CHANNEL;
103

    
104
    /* Open and initialize DV1394 device */
105
    dv->fd = open(context->filename, O_RDONLY);
106
    if (dv->fd < 0) {
107
        av_log(context, AV_LOG_ERROR, "Failed to open DV interface: %s\n", strerror(errno));
108
        goto failed;
109
    }
110

    
111
    if (dv1394_reset(dv) < 0) {
112
        av_log(context, AV_LOG_ERROR, "Failed to initialize DV interface: %s\n", strerror(errno));
113
        goto failed;
114
    }
115

    
116
    dv->ring = mmap(NULL, DV1394_PAL_FRAME_SIZE * DV1394_RING_FRAMES,
117
                    PROT_READ, MAP_PRIVATE, dv->fd, 0);
118
    if (dv->ring == MAP_FAILED) {
119
        av_log(context, AV_LOG_ERROR, "Failed to mmap DV ring buffer: %s\n", strerror(errno));
120
        goto failed;
121
    }
122

    
123
    if (dv1394_start(dv) < 0)
124
        goto failed;
125

    
126
    return 0;
127

    
128
failed:
129
    close(dv->fd);
130
    return AVERROR(EIO);
131
}
132

    
133
static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt)
134
{
135
    struct dv1394_data *dv = context->priv_data;
136
    int size;
137

    
138
    size = dv_get_packet(dv->dv_demux, pkt);
139
    if (size > 0)
140
        return size;
141

    
142
    if (!dv->avail) {
143
        struct dv1394_status s;
144
        struct pollfd p;
145

    
146
        if (dv->done) {
147
            /* Request more frames */
148
            if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) {
149
                /* This usually means that ring buffer overflowed.
150
                 * We have to reset :(.
151
                 */
152

    
153
                av_log(context, AV_LOG_ERROR, "DV1394: Ring buffer overflow. Reseting ..\n");
154

    
155
                dv1394_reset(dv);
156
                dv1394_start(dv);
157
            }
158
            dv->done = 0;
159
        }
160

    
161
        /* Wait until more frames are available */
162
restart_poll:
163
        p.fd = dv->fd;
164
        p.events = POLLIN | POLLERR | POLLHUP;
165
        if (poll(&p, 1, -1) < 0) {
166
            if (errno == EAGAIN || errno == EINTR)
167
                goto restart_poll;
168
            av_log(context, AV_LOG_ERROR, "Poll failed: %s\n", strerror(errno));
169
            return AVERROR(EIO);
170
        }
171

    
172
        if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
173
            av_log(context, AV_LOG_ERROR, "Failed to get status: %s\n", strerror(errno));
174
            return AVERROR(EIO);
175
        }
176
#ifdef DV1394_DEBUG
177
        av_log(context, AV_LOG_DEBUG, "DV1394: status\n"
178
                "\tactive_frame\t%d\n"
179
                "\tfirst_clear_frame\t%d\n"
180
                "\tn_clear_frames\t%d\n"
181
                "\tdropped_frames\t%d\n",
182
                s.active_frame, s.first_clear_frame,
183
                s.n_clear_frames, s.dropped_frames);
184
#endif
185

    
186
        dv->avail = s.n_clear_frames;
187
        dv->index = s.first_clear_frame;
188
        dv->done  = 0;
189

    
190
        if (s.dropped_frames) {
191
            av_log(context, AV_LOG_ERROR, "DV1394: Frame drop detected (%d). Reseting ..\n",
192
                    s.dropped_frames);
193

    
194
            dv1394_reset(dv);
195
            dv1394_start(dv);
196
        }
197
    }
198

    
199
#ifdef DV1394_DEBUG
200
    av_log(context, AV_LOG_DEBUG, "index %d, avail %d, done %d\n", dv->index, dv->avail,
201
            dv->done);
202
#endif
203

    
204
    size = dv_produce_packet(dv->dv_demux, pkt,
205
                             dv->ring + (dv->index * DV1394_PAL_FRAME_SIZE),
206
                             DV1394_PAL_FRAME_SIZE);
207
    dv->index = (dv->index + 1) % DV1394_RING_FRAMES;
208
    dv->done++; dv->avail--;
209

    
210
    return size;
211
}
212

    
213
static int dv1394_close(AVFormatContext * context)
214
{
215
    struct dv1394_data *dv = context->priv_data;
216

    
217
    /* Shutdown DV1394 receiver */
218
    if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
219
        av_log(context, AV_LOG_ERROR, "Failed to shutdown DV1394: %s\n", strerror(errno));
220

    
221
    /* Unmap ring buffer */
222
    if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0)
223
        av_log(context, AV_LOG_ERROR, "Failed to munmap DV1394 ring buffer: %s\n", strerror(errno));
224

    
225
    close(dv->fd);
226
    av_free(dv->dv_demux);
227

    
228
    return 0;
229
}
230

    
231
AVInputFormat dv1394_demuxer = {
232
    .name           = "dv1394",
233
    .long_name      = "dv1394 A/V grab",
234
    .priv_data_size = sizeof(struct dv1394_data),
235
    .read_header    = dv1394_read_header,
236
    .read_packet    = dv1394_read_packet,
237
    .read_close     = dv1394_close,
238
    .flags          = AVFMT_NOFILE
239
};