dv1394.c

Go to the documentation of this file.
00001 /*
00002  * Linux DV1394 interface
00003  * Copyright (c) 2003 Max Krasnyansky <maxk@qualcomm.com>
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "config.h"
00023 #include <unistd.h>
00024 #include <fcntl.h>
00025 #include <errno.h>
00026 #include <sys/ioctl.h>
00027 #include <sys/mman.h>
00028 #include <sys/poll.h>
00029 #include <sys/time.h>
00030 #include <time.h>
00031 
00032 #include "libavformat/avformat.h"
00033 
00034 #undef DV1394_DEBUG
00035 
00036 #include "libavformat/dv.h"
00037 #include "dv1394.h"
00038 
00039 struct dv1394_data {
00040     int fd;
00041     int channel;
00042     int format;
00043 
00044     uint8_t *ring; /* Ring buffer */
00045     int index;  /* Current frame index */
00046     int avail;  /* Number of frames available for reading */
00047     int done;   /* Number of completed frames */
00048 
00049     DVDemuxContext* dv_demux; /* Generic DV muxing/demuxing context */
00050 };
00051 
00052 /*
00053  * The trick here is to kludge around well known problem with kernel Ooopsing
00054  * when you try to capture PAL on a device node configure for NTSC. That's
00055  * why we have to configure the device node for PAL, and then read only NTSC
00056  * amount of data.
00057  */
00058 static int dv1394_reset(struct dv1394_data *dv)
00059 {
00060     struct dv1394_init init;
00061 
00062     init.channel     = dv->channel;
00063     init.api_version = DV1394_API_VERSION;
00064     init.n_frames    = DV1394_RING_FRAMES;
00065     init.format      = DV1394_PAL;
00066 
00067     if (ioctl(dv->fd, DV1394_INIT, &init) < 0)
00068         return -1;
00069 
00070     dv->avail  = dv->done = 0;
00071     return 0;
00072 }
00073 
00074 static int dv1394_start(struct dv1394_data *dv)
00075 {
00076     /* Tell DV1394 driver to enable receiver */
00077     if (ioctl(dv->fd, DV1394_START_RECEIVE, 0) < 0) {
00078         av_log(NULL, AV_LOG_ERROR, "Failed to start receiver: %s\n", strerror(errno));
00079         return -1;
00080     }
00081     return 0;
00082 }
00083 
00084 static int dv1394_read_header(AVFormatContext * context, AVFormatParameters * ap)
00085 {
00086     struct dv1394_data *dv = context->priv_data;
00087 
00088     dv->dv_demux = dv_init_demux(context);
00089     if (!dv->dv_demux)
00090         goto failed;
00091 
00092     if (ap->standard && !strcasecmp(ap->standard, "pal"))
00093         dv->format = DV1394_PAL;
00094     else
00095         dv->format = DV1394_NTSC;
00096 
00097     if (ap->channel)
00098         dv->channel = ap->channel;
00099     else
00100         dv->channel = DV1394_DEFAULT_CHANNEL;
00101 
00102     /* Open and initialize DV1394 device */
00103     dv->fd = open(context->filename, O_RDONLY);
00104     if (dv->fd < 0) {
00105         av_log(context, AV_LOG_ERROR, "Failed to open DV interface: %s\n", strerror(errno));
00106         goto failed;
00107     }
00108 
00109     if (dv1394_reset(dv) < 0) {
00110         av_log(context, AV_LOG_ERROR, "Failed to initialize DV interface: %s\n", strerror(errno));
00111         goto failed;
00112     }
00113 
00114     dv->ring = mmap(NULL, DV1394_PAL_FRAME_SIZE * DV1394_RING_FRAMES,
00115                     PROT_READ, MAP_PRIVATE, dv->fd, 0);
00116     if (dv->ring == MAP_FAILED) {
00117         av_log(context, AV_LOG_ERROR, "Failed to mmap DV ring buffer: %s\n", strerror(errno));
00118         goto failed;
00119     }
00120 
00121     if (dv1394_start(dv) < 0)
00122         goto failed;
00123 
00124     return 0;
00125 
00126 failed:
00127     close(dv->fd);
00128     return AVERROR(EIO);
00129 }
00130 
00131 static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt)
00132 {
00133     struct dv1394_data *dv = context->priv_data;
00134     int size;
00135 
00136     size = dv_get_packet(dv->dv_demux, pkt);
00137     if (size > 0)
00138         return size;
00139 
00140     if (!dv->avail) {
00141         struct dv1394_status s;
00142         struct pollfd p;
00143 
00144         if (dv->done) {
00145             /* Request more frames */
00146             if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) {
00147                 /* This usually means that ring buffer overflowed.
00148                  * We have to reset :(.
00149                  */
00150 
00151                 av_log(context, AV_LOG_ERROR, "DV1394: Ring buffer overflow. Reseting ..\n");
00152 
00153                 dv1394_reset(dv);
00154                 dv1394_start(dv);
00155             }
00156             dv->done = 0;
00157         }
00158 
00159         /* Wait until more frames are available */
00160 restart_poll:
00161         p.fd = dv->fd;
00162         p.events = POLLIN | POLLERR | POLLHUP;
00163         if (poll(&p, 1, -1) < 0) {
00164             if (errno == EAGAIN || errno == EINTR)
00165                 goto restart_poll;
00166             av_log(context, AV_LOG_ERROR, "Poll failed: %s\n", strerror(errno));
00167             return AVERROR(EIO);
00168         }
00169 
00170         if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
00171             av_log(context, AV_LOG_ERROR, "Failed to get status: %s\n", strerror(errno));
00172             return AVERROR(EIO);
00173         }
00174 #ifdef DV1394_DEBUG
00175         av_log(context, AV_LOG_DEBUG, "DV1394: status\n"
00176                 "\tactive_frame\t%d\n"
00177                 "\tfirst_clear_frame\t%d\n"
00178                 "\tn_clear_frames\t%d\n"
00179                 "\tdropped_frames\t%d\n",
00180                 s.active_frame, s.first_clear_frame,
00181                 s.n_clear_frames, s.dropped_frames);
00182 #endif
00183 
00184         dv->avail = s.n_clear_frames;
00185         dv->index = s.first_clear_frame;
00186         dv->done  = 0;
00187 
00188         if (s.dropped_frames) {
00189             av_log(context, AV_LOG_ERROR, "DV1394: Frame drop detected (%d). Reseting ..\n",
00190                     s.dropped_frames);
00191 
00192             dv1394_reset(dv);
00193             dv1394_start(dv);
00194         }
00195     }
00196 
00197 #ifdef DV1394_DEBUG
00198     av_log(context, AV_LOG_DEBUG, "index %d, avail %d, done %d\n", dv->index, dv->avail,
00199             dv->done);
00200 #endif
00201 
00202     size = dv_produce_packet(dv->dv_demux, pkt,
00203                              dv->ring + (dv->index * DV1394_PAL_FRAME_SIZE),
00204                              DV1394_PAL_FRAME_SIZE);
00205     dv->index = (dv->index + 1) % DV1394_RING_FRAMES;
00206     dv->done++; dv->avail--;
00207 
00208     return size;
00209 }
00210 
00211 static int dv1394_close(AVFormatContext * context)
00212 {
00213     struct dv1394_data *dv = context->priv_data;
00214 
00215     /* Shutdown DV1394 receiver */
00216     if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
00217         av_log(context, AV_LOG_ERROR, "Failed to shutdown DV1394: %s\n", strerror(errno));
00218 
00219     /* Unmap ring buffer */
00220     if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0)
00221         av_log(context, AV_LOG_ERROR, "Failed to munmap DV1394 ring buffer: %s\n", strerror(errno));
00222 
00223     close(dv->fd);
00224     av_free(dv->dv_demux);
00225 
00226     return 0;
00227 }
00228 
00229 AVInputFormat dv1394_demuxer = {
00230     .name           = "dv1394",
00231     .long_name      = NULL_IF_CONFIG_SMALL("DV1394 A/V grab"),
00232     .priv_data_size = sizeof(struct dv1394_data),
00233     .read_header    = dv1394_read_header,
00234     .read_packet    = dv1394_read_packet,
00235     .read_close     = dv1394_close,
00236     .flags          = AVFMT_NOFILE
00237 };

Generated on Fri Jan 9 16:44:27 2009 for libextractor by  doxygen 1.5.1