• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavdevice/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 Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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 <poll.h>
00027 #include <sys/ioctl.h>
00028 #include <sys/mman.h>
00029 #include <sys/time.h>
00030 #include <time.h>
00031 
00032 #include "libavutil/log.h"
00033 #include "libavutil/opt.h"
00034 #include "libavformat/avformat.h"
00035 #include "libavformat/dv.h"
00036 #include "dv1394.h"
00037 
00038 struct dv1394_data {
00039     AVClass *class;
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 = avpriv_dv_init_demux(context);
00089     if (!dv->dv_demux)
00090         goto failed;
00091 
00092     /* Open and initialize DV1394 device */
00093     dv->fd = open(context->filename, O_RDONLY);
00094     if (dv->fd < 0) {
00095         av_log(context, AV_LOG_ERROR, "Failed to open DV interface: %s\n", strerror(errno));
00096         goto failed;
00097     }
00098 
00099     if (dv1394_reset(dv) < 0) {
00100         av_log(context, AV_LOG_ERROR, "Failed to initialize DV interface: %s\n", strerror(errno));
00101         goto failed;
00102     }
00103 
00104     dv->ring = mmap(NULL, DV1394_PAL_FRAME_SIZE * DV1394_RING_FRAMES,
00105                     PROT_READ, MAP_PRIVATE, dv->fd, 0);
00106     if (dv->ring == MAP_FAILED) {
00107         av_log(context, AV_LOG_ERROR, "Failed to mmap DV ring buffer: %s\n", strerror(errno));
00108         goto failed;
00109     }
00110 
00111     if (dv1394_start(dv) < 0)
00112         goto failed;
00113 
00114     return 0;
00115 
00116 failed:
00117     close(dv->fd);
00118     return AVERROR(EIO);
00119 }
00120 
00121 static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt)
00122 {
00123     struct dv1394_data *dv = context->priv_data;
00124     int size;
00125 
00126     size = avpriv_dv_get_packet(dv->dv_demux, pkt);
00127     if (size > 0)
00128         return size;
00129 
00130     if (!dv->avail) {
00131         struct dv1394_status s;
00132         struct pollfd p;
00133 
00134         if (dv->done) {
00135             /* Request more frames */
00136             if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) {
00137                 /* This usually means that ring buffer overflowed.
00138                  * We have to reset :(.
00139                  */
00140 
00141                 av_log(context, AV_LOG_ERROR, "DV1394: Ring buffer overflow. Reseting ..\n");
00142 
00143                 dv1394_reset(dv);
00144                 dv1394_start(dv);
00145             }
00146             dv->done = 0;
00147         }
00148 
00149         /* Wait until more frames are available */
00150 restart_poll:
00151         p.fd = dv->fd;
00152         p.events = POLLIN | POLLERR | POLLHUP;
00153         if (poll(&p, 1, -1) < 0) {
00154             if (errno == EAGAIN || errno == EINTR)
00155                 goto restart_poll;
00156             av_log(context, AV_LOG_ERROR, "Poll failed: %s\n", strerror(errno));
00157             return AVERROR(EIO);
00158         }
00159 
00160         if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
00161             av_log(context, AV_LOG_ERROR, "Failed to get status: %s\n", strerror(errno));
00162             return AVERROR(EIO);
00163         }
00164         av_dlog(context, "DV1394: status\n"
00165                 "\tactive_frame\t%d\n"
00166                 "\tfirst_clear_frame\t%d\n"
00167                 "\tn_clear_frames\t%d\n"
00168                 "\tdropped_frames\t%d\n",
00169                 s.active_frame, s.first_clear_frame,
00170                 s.n_clear_frames, s.dropped_frames);
00171 
00172         dv->avail = s.n_clear_frames;
00173         dv->index = s.first_clear_frame;
00174         dv->done  = 0;
00175 
00176         if (s.dropped_frames) {
00177             av_log(context, AV_LOG_ERROR, "DV1394: Frame drop detected (%d). Reseting ..\n",
00178                     s.dropped_frames);
00179 
00180             dv1394_reset(dv);
00181             dv1394_start(dv);
00182         }
00183     }
00184 
00185     av_dlog(context, "index %d, avail %d, done %d\n", dv->index, dv->avail,
00186             dv->done);
00187 
00188     size = avpriv_dv_produce_packet(dv->dv_demux, pkt,
00189                              dv->ring + (dv->index * DV1394_PAL_FRAME_SIZE),
00190                              DV1394_PAL_FRAME_SIZE);
00191     dv->index = (dv->index + 1) % DV1394_RING_FRAMES;
00192     dv->done++; dv->avail--;
00193 
00194     return size;
00195 }
00196 
00197 static int dv1394_close(AVFormatContext * context)
00198 {
00199     struct dv1394_data *dv = context->priv_data;
00200 
00201     /* Shutdown DV1394 receiver */
00202     if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
00203         av_log(context, AV_LOG_ERROR, "Failed to shutdown DV1394: %s\n", strerror(errno));
00204 
00205     /* Unmap ring buffer */
00206     if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0)
00207         av_log(context, AV_LOG_ERROR, "Failed to munmap DV1394 ring buffer: %s\n", strerror(errno));
00208 
00209     close(dv->fd);
00210     av_free(dv->dv_demux);
00211 
00212     return 0;
00213 }
00214 
00215 static const AVOption options[] = {
00216     { "standard", "", offsetof(struct dv1394_data, format), AV_OPT_TYPE_INT, {.dbl = DV1394_NTSC}, DV1394_PAL, DV1394_NTSC, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00217     { "PAL",      "", 0, AV_OPT_TYPE_CONST, {.dbl = DV1394_PAL},   0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00218     { "NTSC",     "", 0, AV_OPT_TYPE_CONST, {.dbl = DV1394_NTSC},  0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00219     { "channel",  "", offsetof(struct dv1394_data, channel), AV_OPT_TYPE_INT, {.dbl = DV1394_DEFAULT_CHANNEL}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
00220     { NULL },
00221 };
00222 
00223 static const AVClass dv1394_class = {
00224     .class_name = "DV1394 indev",
00225     .item_name  = av_default_item_name,
00226     .option     = options,
00227     .version    = LIBAVUTIL_VERSION_INT,
00228 };
00229 
00230 AVInputFormat ff_dv1394_demuxer = {
00231     .name           = "dv1394",
00232     .long_name      = NULL_IF_CONFIG_SMALL("DV1394 A/V grab"),
00233     .priv_data_size = sizeof(struct dv1394_data),
00234     .read_header    = dv1394_read_header,
00235     .read_packet    = dv1394_read_packet,
00236     .read_close     = dv1394_close,
00237     .flags          = AVFMT_NOFILE,
00238     .priv_class     = &dv1394_class,
00239 };
Generated on Thu Jan 24 2013 17:08:54 for Libav by doxygen 1.7.1