Libav 0.7.1
|
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 #include <strings.h> 00032 00033 #include "libavutil/log.h" 00034 #include "libavutil/opt.h" 00035 #include "libavformat/avformat.h" 00036 #include "libavformat/dv.h" 00037 #include "dv1394.h" 00038 00039 struct dv1394_data { 00040 AVClass *class; 00041 int fd; 00042 int channel; 00043 int format; 00044 00045 uint8_t *ring; /* Ring buffer */ 00046 int index; /* Current frame index */ 00047 int avail; /* Number of frames available for reading */ 00048 int done; /* Number of completed frames */ 00049 00050 DVDemuxContext* dv_demux; /* Generic DV muxing/demuxing context */ 00051 }; 00052 00053 /* 00054 * The trick here is to kludge around well known problem with kernel Ooopsing 00055 * when you try to capture PAL on a device node configure for NTSC. That's 00056 * why we have to configure the device node for PAL, and then read only NTSC 00057 * amount of data. 00058 */ 00059 static int dv1394_reset(struct dv1394_data *dv) 00060 { 00061 struct dv1394_init init; 00062 00063 init.channel = dv->channel; 00064 init.api_version = DV1394_API_VERSION; 00065 init.n_frames = DV1394_RING_FRAMES; 00066 init.format = DV1394_PAL; 00067 00068 if (ioctl(dv->fd, DV1394_INIT, &init) < 0) 00069 return -1; 00070 00071 dv->avail = dv->done = 0; 00072 return 0; 00073 } 00074 00075 static int dv1394_start(struct dv1394_data *dv) 00076 { 00077 /* Tell DV1394 driver to enable receiver */ 00078 if (ioctl(dv->fd, DV1394_START_RECEIVE, 0) < 0) { 00079 av_log(NULL, AV_LOG_ERROR, "Failed to start receiver: %s\n", strerror(errno)); 00080 return -1; 00081 } 00082 return 0; 00083 } 00084 00085 static int dv1394_read_header(AVFormatContext * context, AVFormatParameters * ap) 00086 { 00087 struct dv1394_data *dv = context->priv_data; 00088 00089 dv->dv_demux = dv_init_demux(context); 00090 if (!dv->dv_demux) 00091 goto failed; 00092 00093 #if FF_API_FORMAT_PARAMETERS 00094 if (ap->standard) { 00095 if (!strcasecmp(ap->standard, "pal")) 00096 dv->format = DV1394_PAL; 00097 else 00098 dv->format = DV1394_NTSC; 00099 } 00100 00101 if (ap->channel) 00102 dv->channel = ap->channel; 00103 #endif 00104 00105 /* Open and initialize DV1394 device */ 00106 dv->fd = open(context->filename, O_RDONLY); 00107 if (dv->fd < 0) { 00108 av_log(context, AV_LOG_ERROR, "Failed to open DV interface: %s\n", strerror(errno)); 00109 goto failed; 00110 } 00111 00112 if (dv1394_reset(dv) < 0) { 00113 av_log(context, AV_LOG_ERROR, "Failed to initialize DV interface: %s\n", strerror(errno)); 00114 goto failed; 00115 } 00116 00117 dv->ring = mmap(NULL, DV1394_PAL_FRAME_SIZE * DV1394_RING_FRAMES, 00118 PROT_READ, MAP_PRIVATE, dv->fd, 0); 00119 if (dv->ring == MAP_FAILED) { 00120 av_log(context, AV_LOG_ERROR, "Failed to mmap DV ring buffer: %s\n", strerror(errno)); 00121 goto failed; 00122 } 00123 00124 if (dv1394_start(dv) < 0) 00125 goto failed; 00126 00127 return 0; 00128 00129 failed: 00130 close(dv->fd); 00131 return AVERROR(EIO); 00132 } 00133 00134 static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt) 00135 { 00136 struct dv1394_data *dv = context->priv_data; 00137 int size; 00138 00139 size = dv_get_packet(dv->dv_demux, pkt); 00140 if (size > 0) 00141 return size; 00142 00143 if (!dv->avail) { 00144 struct dv1394_status s; 00145 struct pollfd p; 00146 00147 if (dv->done) { 00148 /* Request more frames */ 00149 if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) { 00150 /* This usually means that ring buffer overflowed. 00151 * We have to reset :(. 00152 */ 00153 00154 av_log(context, AV_LOG_ERROR, "DV1394: Ring buffer overflow. Reseting ..\n"); 00155 00156 dv1394_reset(dv); 00157 dv1394_start(dv); 00158 } 00159 dv->done = 0; 00160 } 00161 00162 /* Wait until more frames are available */ 00163 restart_poll: 00164 p.fd = dv->fd; 00165 p.events = POLLIN | POLLERR | POLLHUP; 00166 if (poll(&p, 1, -1) < 0) { 00167 if (errno == EAGAIN || errno == EINTR) 00168 goto restart_poll; 00169 av_log(context, AV_LOG_ERROR, "Poll failed: %s\n", strerror(errno)); 00170 return AVERROR(EIO); 00171 } 00172 00173 if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) { 00174 av_log(context, AV_LOG_ERROR, "Failed to get status: %s\n", strerror(errno)); 00175 return AVERROR(EIO); 00176 } 00177 av_dlog(context, "DV1394: status\n" 00178 "\tactive_frame\t%d\n" 00179 "\tfirst_clear_frame\t%d\n" 00180 "\tn_clear_frames\t%d\n" 00181 "\tdropped_frames\t%d\n", 00182 s.active_frame, s.first_clear_frame, 00183 s.n_clear_frames, s.dropped_frames); 00184 00185 dv->avail = s.n_clear_frames; 00186 dv->index = s.first_clear_frame; 00187 dv->done = 0; 00188 00189 if (s.dropped_frames) { 00190 av_log(context, AV_LOG_ERROR, "DV1394: Frame drop detected (%d). Reseting ..\n", 00191 s.dropped_frames); 00192 00193 dv1394_reset(dv); 00194 dv1394_start(dv); 00195 } 00196 } 00197 00198 av_dlog(context, "index %d, avail %d, done %d\n", dv->index, dv->avail, 00199 dv->done); 00200 00201 size = dv_produce_packet(dv->dv_demux, pkt, 00202 dv->ring + (dv->index * DV1394_PAL_FRAME_SIZE), 00203 DV1394_PAL_FRAME_SIZE); 00204 dv->index = (dv->index + 1) % DV1394_RING_FRAMES; 00205 dv->done++; dv->avail--; 00206 00207 return size; 00208 } 00209 00210 static int dv1394_close(AVFormatContext * context) 00211 { 00212 struct dv1394_data *dv = context->priv_data; 00213 00214 /* Shutdown DV1394 receiver */ 00215 if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0) 00216 av_log(context, AV_LOG_ERROR, "Failed to shutdown DV1394: %s\n", strerror(errno)); 00217 00218 /* Unmap ring buffer */ 00219 if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0) 00220 av_log(context, AV_LOG_ERROR, "Failed to munmap DV1394 ring buffer: %s\n", strerror(errno)); 00221 00222 close(dv->fd); 00223 av_free(dv->dv_demux); 00224 00225 return 0; 00226 } 00227 00228 static const AVOption options[] = { 00229 { "standard", "", offsetof(struct dv1394_data, format), FF_OPT_TYPE_INT, {.dbl = DV1394_NTSC}, DV1394_PAL, DV1394_NTSC, AV_OPT_FLAG_DECODING_PARAM, "standard" }, 00230 { "PAL", "", 0, FF_OPT_TYPE_CONST, {.dbl = DV1394_PAL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" }, 00231 { "NTSC", "", 0, FF_OPT_TYPE_CONST, {.dbl = DV1394_NTSC}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" }, 00232 { "channel", "", offsetof(struct dv1394_data, channel), FF_OPT_TYPE_INT, {.dbl = DV1394_DEFAULT_CHANNEL}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, 00233 { NULL }, 00234 }; 00235 00236 static const AVClass dv1394_class = { 00237 .class_name = "DV1394 indev", 00238 .item_name = av_default_item_name, 00239 .option = options, 00240 .version = LIBAVUTIL_VERSION_INT, 00241 }; 00242 00243 AVInputFormat ff_dv1394_demuxer = { 00244 .name = "dv1394", 00245 .long_name = NULL_IF_CONFIG_SMALL("DV1394 A/V grab"), 00246 .priv_data_size = sizeof(struct dv1394_data), 00247 .read_header = dv1394_read_header, 00248 .read_packet = dv1394_read_packet, 00249 .read_close = dv1394_close, 00250 .flags = AVFMT_NOFILE, 00251 .priv_class = &dv1394_class, 00252 };