Libav
|
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 <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 "libavformat/avformat.h" 00034 00035 #undef DV1394_DEBUG 00036 00037 #include "libavformat/dv.h" 00038 #include "dv1394.h" 00039 00040 struct dv1394_data { 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 (ap->standard && !strcasecmp(ap->standard, "pal")) 00094 dv->format = DV1394_PAL; 00095 else 00096 dv->format = DV1394_NTSC; 00097 00098 if (ap->channel) 00099 dv->channel = ap->channel; 00100 else 00101 dv->channel = DV1394_DEFAULT_CHANNEL; 00102 00103 /* Open and initialize DV1394 device */ 00104 dv->fd = open(context->filename, O_RDONLY); 00105 if (dv->fd < 0) { 00106 av_log(context, AV_LOG_ERROR, "Failed to open DV interface: %s\n", strerror(errno)); 00107 goto failed; 00108 } 00109 00110 if (dv1394_reset(dv) < 0) { 00111 av_log(context, AV_LOG_ERROR, "Failed to initialize DV interface: %s\n", strerror(errno)); 00112 goto failed; 00113 } 00114 00115 dv->ring = mmap(NULL, DV1394_PAL_FRAME_SIZE * DV1394_RING_FRAMES, 00116 PROT_READ, MAP_PRIVATE, dv->fd, 0); 00117 if (dv->ring == MAP_FAILED) { 00118 av_log(context, AV_LOG_ERROR, "Failed to mmap DV ring buffer: %s\n", strerror(errno)); 00119 goto failed; 00120 } 00121 00122 if (dv1394_start(dv) < 0) 00123 goto failed; 00124 00125 return 0; 00126 00127 failed: 00128 close(dv->fd); 00129 return AVERROR(EIO); 00130 } 00131 00132 static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt) 00133 { 00134 struct dv1394_data *dv = context->priv_data; 00135 int size; 00136 00137 size = dv_get_packet(dv->dv_demux, pkt); 00138 if (size > 0) 00139 return size; 00140 00141 if (!dv->avail) { 00142 struct dv1394_status s; 00143 struct pollfd p; 00144 00145 if (dv->done) { 00146 /* Request more frames */ 00147 if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) { 00148 /* This usually means that ring buffer overflowed. 00149 * We have to reset :(. 00150 */ 00151 00152 av_log(context, AV_LOG_ERROR, "DV1394: Ring buffer overflow. Reseting ..\n"); 00153 00154 dv1394_reset(dv); 00155 dv1394_start(dv); 00156 } 00157 dv->done = 0; 00158 } 00159 00160 /* Wait until more frames are available */ 00161 restart_poll: 00162 p.fd = dv->fd; 00163 p.events = POLLIN | POLLERR | POLLHUP; 00164 if (poll(&p, 1, -1) < 0) { 00165 if (errno == EAGAIN || errno == EINTR) 00166 goto restart_poll; 00167 av_log(context, AV_LOG_ERROR, "Poll failed: %s\n", strerror(errno)); 00168 return AVERROR(EIO); 00169 } 00170 00171 if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) { 00172 av_log(context, AV_LOG_ERROR, "Failed to get status: %s\n", strerror(errno)); 00173 return AVERROR(EIO); 00174 } 00175 #ifdef DV1394_DEBUG 00176 av_log(context, AV_LOG_DEBUG, "DV1394: status\n" 00177 "\tactive_frame\t%d\n" 00178 "\tfirst_clear_frame\t%d\n" 00179 "\tn_clear_frames\t%d\n" 00180 "\tdropped_frames\t%d\n", 00181 s.active_frame, s.first_clear_frame, 00182 s.n_clear_frames, s.dropped_frames); 00183 #endif 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 #ifdef DV1394_DEBUG 00199 av_log(context, AV_LOG_DEBUG, "index %d, avail %d, done %d\n", dv->index, dv->avail, 00200 dv->done); 00201 #endif 00202 00203 size = dv_produce_packet(dv->dv_demux, pkt, 00204 dv->ring + (dv->index * DV1394_PAL_FRAME_SIZE), 00205 DV1394_PAL_FRAME_SIZE); 00206 dv->index = (dv->index + 1) % DV1394_RING_FRAMES; 00207 dv->done++; dv->avail--; 00208 00209 return size; 00210 } 00211 00212 static int dv1394_close(AVFormatContext * context) 00213 { 00214 struct dv1394_data *dv = context->priv_data; 00215 00216 /* Shutdown DV1394 receiver */ 00217 if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0) 00218 av_log(context, AV_LOG_ERROR, "Failed to shutdown DV1394: %s\n", strerror(errno)); 00219 00220 /* Unmap ring buffer */ 00221 if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0) 00222 av_log(context, AV_LOG_ERROR, "Failed to munmap DV1394 ring buffer: %s\n", strerror(errno)); 00223 00224 close(dv->fd); 00225 av_free(dv->dv_demux); 00226 00227 return 0; 00228 } 00229 00230 AVInputFormat 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 };