00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00030 #include "libavutil/log.h"
00031 #include "libavutil/opt.h"
00032 #include "avdevice.h"
00033 #include "libavformat/dv.h"
00034 #include "dv1394.h"
00035
00036 struct dv1394_data {
00037 AVClass *class;
00038 int fd;
00039 int channel;
00040 int format;
00041
00042 uint8_t *ring;
00043 int index;
00044 int avail;
00045 int done;
00046
00047 DVDemuxContext* dv_demux;
00048 };
00049
00050
00051
00052
00053
00054
00055
00056 static int dv1394_reset(struct dv1394_data *dv)
00057 {
00058 struct dv1394_init init;
00059
00060 init.channel = dv->channel;
00061 init.api_version = DV1394_API_VERSION;
00062 init.n_frames = DV1394_RING_FRAMES;
00063 init.format = DV1394_PAL;
00064
00065 if (ioctl(dv->fd, DV1394_INIT, &init) < 0)
00066 return -1;
00067
00068 dv->avail = dv->done = 0;
00069 return 0;
00070 }
00071
00072 static int dv1394_start(struct dv1394_data *dv)
00073 {
00074
00075 if (ioctl(dv->fd, DV1394_START_RECEIVE, 0) < 0) {
00076 av_log(NULL, AV_LOG_ERROR, "Failed to start receiver: %s\n", strerror(errno));
00077 return -1;
00078 }
00079 return 0;
00080 }
00081
00082 static int dv1394_read_header(AVFormatContext * context)
00083 {
00084 struct dv1394_data *dv = context->priv_data;
00085
00086 dv->dv_demux = avpriv_dv_init_demux(context);
00087 if (!dv->dv_demux)
00088 goto failed;
00089
00090
00091 dv->fd = open(context->filename, O_RDONLY);
00092 if (dv->fd < 0) {
00093 av_log(context, AV_LOG_ERROR, "Failed to open DV interface: %s\n", strerror(errno));
00094 goto failed;
00095 }
00096
00097 if (dv1394_reset(dv) < 0) {
00098 av_log(context, AV_LOG_ERROR, "Failed to initialize DV interface: %s\n", strerror(errno));
00099 goto failed;
00100 }
00101
00102 dv->ring = mmap(NULL, DV1394_PAL_FRAME_SIZE * DV1394_RING_FRAMES,
00103 PROT_READ, MAP_PRIVATE, dv->fd, 0);
00104 if (dv->ring == MAP_FAILED) {
00105 av_log(context, AV_LOG_ERROR, "Failed to mmap DV ring buffer: %s\n", strerror(errno));
00106 goto failed;
00107 }
00108
00109 if (dv1394_start(dv) < 0)
00110 goto failed;
00111
00112 return 0;
00113
00114 failed:
00115 close(dv->fd);
00116 return AVERROR(EIO);
00117 }
00118
00119 static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt)
00120 {
00121 struct dv1394_data *dv = context->priv_data;
00122 int size;
00123
00124 size = avpriv_dv_get_packet(dv->dv_demux, pkt);
00125 if (size > 0)
00126 return size;
00127
00128 if (!dv->avail) {
00129 struct dv1394_status s;
00130 struct pollfd p;
00131
00132 if (dv->done) {
00133
00134 if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) {
00135
00136
00137
00138
00139 av_log(context, AV_LOG_ERROR, "DV1394: Ring buffer overflow. Reseting ..\n");
00140
00141 dv1394_reset(dv);
00142 dv1394_start(dv);
00143 }
00144 dv->done = 0;
00145 }
00146
00147
00148 restart_poll:
00149 p.fd = dv->fd;
00150 p.events = POLLIN | POLLERR | POLLHUP;
00151 if (poll(&p, 1, -1) < 0) {
00152 if (errno == EAGAIN || errno == EINTR)
00153 goto restart_poll;
00154 av_log(context, AV_LOG_ERROR, "Poll failed: %s\n", strerror(errno));
00155 return AVERROR(EIO);
00156 }
00157
00158 if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
00159 av_log(context, AV_LOG_ERROR, "Failed to get status: %s\n", strerror(errno));
00160 return AVERROR(EIO);
00161 }
00162 av_dlog(context, "DV1394: status\n"
00163 "\tactive_frame\t%d\n"
00164 "\tfirst_clear_frame\t%d\n"
00165 "\tn_clear_frames\t%d\n"
00166 "\tdropped_frames\t%d\n",
00167 s.active_frame, s.first_clear_frame,
00168 s.n_clear_frames, s.dropped_frames);
00169
00170 dv->avail = s.n_clear_frames;
00171 dv->index = s.first_clear_frame;
00172 dv->done = 0;
00173
00174 if (s.dropped_frames) {
00175 av_log(context, AV_LOG_ERROR, "DV1394: Frame drop detected (%d). Reseting ..\n",
00176 s.dropped_frames);
00177
00178 dv1394_reset(dv);
00179 dv1394_start(dv);
00180 }
00181 }
00182
00183 av_dlog(context, "index %d, avail %d, done %d\n", dv->index, dv->avail,
00184 dv->done);
00185
00186 size = avpriv_dv_produce_packet(dv->dv_demux, pkt,
00187 dv->ring + (dv->index * DV1394_PAL_FRAME_SIZE),
00188 DV1394_PAL_FRAME_SIZE, -1);
00189 dv->index = (dv->index + 1) % DV1394_RING_FRAMES;
00190 dv->done++; dv->avail--;
00191
00192 return size;
00193 }
00194
00195 static int dv1394_close(AVFormatContext * context)
00196 {
00197 struct dv1394_data *dv = context->priv_data;
00198
00199
00200 if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
00201 av_log(context, AV_LOG_ERROR, "Failed to shutdown DV1394: %s\n", strerror(errno));
00202
00203
00204 if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0)
00205 av_log(context, AV_LOG_ERROR, "Failed to munmap DV1394 ring buffer: %s\n", strerror(errno));
00206
00207 close(dv->fd);
00208 av_free(dv->dv_demux);
00209
00210 return 0;
00211 }
00212
00213 static const AVOption options[] = {
00214 { "standard", "", offsetof(struct dv1394_data, format), AV_OPT_TYPE_INT, {.i64 = DV1394_NTSC}, DV1394_PAL, DV1394_NTSC, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00215 { "PAL", "", 0, AV_OPT_TYPE_CONST, {.i64 = DV1394_PAL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00216 { "NTSC", "", 0, AV_OPT_TYPE_CONST, {.i64 = DV1394_NTSC}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
00217 { "channel", "", offsetof(struct dv1394_data, channel), AV_OPT_TYPE_INT, {.i64 = DV1394_DEFAULT_CHANNEL}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
00218 { NULL },
00219 };
00220
00221 static const AVClass dv1394_class = {
00222 .class_name = "DV1394 indev",
00223 .item_name = av_default_item_name,
00224 .option = options,
00225 .version = LIBAVUTIL_VERSION_INT,
00226 };
00227
00228 AVInputFormat ff_dv1394_demuxer = {
00229 .name = "dv1394",
00230 .long_name = NULL_IF_CONFIG_SMALL("DV1394 A/V grab"),
00231 .priv_data_size = sizeof(struct dv1394_data),
00232 .read_header = dv1394_read_header,
00233 .read_packet = dv1394_read_packet,
00234 .read_close = dv1394_close,
00235 .flags = AVFMT_NOFILE,
00236 .priv_class = &dv1394_class,
00237 };