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 #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 "avdevice.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;
00046 int index;
00047 int avail;
00048 int done;
00049
00050 DVDemuxContext* dv_demux;
00051 };
00052
00053
00054
00055
00056
00057
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
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
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
00149 if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) {
00150
00151
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
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, -1);
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
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
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 };