2 * Copyright (c) 2012 Georg Lippitsch <georg.lippitsch@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * libiec61883 interface
27 #include <libraw1394/raw1394.h>
28 #include <libavc1394/avc1394.h>
29 #include <libavc1394/rom1394.h>
30 #include <libiec61883/iec61883.h>
31 #include "libavformat/dv.h"
32 #include "libavformat/mpegts.h"
33 #include "libavutil/opt.h"
36 #define THREADS HAVE_PTHREADS
42 #define MOTDCT_SPEC_ID 0x00005068
43 #define IEC61883_AUTO 0
45 #define IEC61883_HDV 2
48 * For DV, one packet corresponds exactly to one frame.
49 * For HDV, these are MPEG2 transport stream packets.
50 * The queue is implemented as linked list.
52 typedef struct DVPacket {
53 uint8_t *buf; ///< actual buffer data
54 int len; ///< size of buffer allocated
55 struct DVPacket *next; ///< next DVPacket
58 struct iec61883_data {
60 raw1394handle_t raw1394; ///< handle for libraw1394
61 iec61883_dv_fb_t iec61883_dv; ///< handle for libiec61883 when used with DV
62 iec61883_mpeg2_t iec61883_mpeg2; ///< handle for libiec61883 when used with HDV
64 DVDemuxContext *dv_demux; ///< generic DV muxing/demuxing context
65 MpegTSContext *mpeg_demux; ///< generic HDV muxing/demuxing context
67 DVPacket *queue_first; ///< first element of packet queue
68 DVPacket *queue_last; ///< last element of packet queue
70 int packets; ///< Number of packets queued
71 int max_packets; ///< Max. number of packets in queue
73 int bandwidth; ///< returned by libiec61883
74 int channel; ///< returned by libiec61883
75 int input_port; ///< returned by libiec61883
76 int type; ///< Stream type, to distinguish DV/HDV
77 int node; ///< returned by libiec61883
78 int output_port; ///< returned by libiec61883
79 int thread_loop; ///< Condition for thread while-loop
80 int receiving; ///< True as soon data from device available
81 int receive_error; ///< Set in receive task in case of error
82 int eof; ///< True as soon as no more data available
84 struct pollfd raw1394_poll; ///< to poll for new data from libraw1394
86 /** Parse function for DV/HDV differs, so this is set before packets arrive */
87 int (*parse_queue)(struct iec61883_data *dv, AVPacket *pkt);
90 pthread_t receive_task_thread;
91 pthread_mutex_t mutex;
96 static int iec61883_callback(unsigned char *data, int length,
97 int complete, void *callback_data)
99 struct iec61883_data *dv = callback_data;
104 pthread_mutex_lock(&dv->mutex);
107 if (dv->packets >= dv->max_packets) {
108 av_log(NULL, AV_LOG_ERROR, "DV packet queue overrun, dropping.\n");
113 packet = av_mallocz(sizeof(*packet));
119 packet->buf = av_malloc(length);
124 packet->len = length;
126 memcpy(packet->buf, data, length);
128 if (dv->queue_first) {
129 dv->queue_last->next = packet;
130 dv->queue_last = packet;
132 dv->queue_first = packet;
133 dv->queue_last = packet;
141 pthread_cond_signal(&dv->cond);
142 pthread_mutex_unlock(&dv->mutex);
147 static void *iec61883_receive_task(void *opaque)
149 struct iec61883_data *dv = (struct iec61883_data *)opaque;
153 while (dv->thread_loop)
156 while ((result = poll(&dv->raw1394_poll, 1, 200)) < 0) {
157 if (!(errno == EAGAIN || errno == EINTR)) {
158 av_log(NULL, AV_LOG_ERROR, "Raw1394 poll error occurred.\n");
159 dv->receive_error = AVERROR(EIO);
163 if (result > 0 && ((dv->raw1394_poll.revents & POLLIN)
164 || (dv->raw1394_poll.revents & POLLPRI))) {
166 raw1394_loop_iterate(dv->raw1394);
167 } else if (dv->receiving) {
168 av_log(NULL, AV_LOG_ERROR, "No more input data available\n");
170 pthread_mutex_lock(&dv->mutex);
172 pthread_cond_signal(&dv->cond);
173 pthread_mutex_unlock(&dv->mutex);
184 static int iec61883_parse_queue_dv(struct iec61883_data *dv, AVPacket *pkt)
189 size = avpriv_dv_get_packet(dv->dv_demux, pkt);
193 packet = dv->queue_first;
197 size = avpriv_dv_produce_packet(dv->dv_demux, pkt,
198 packet->buf, packet->len, -1);
199 pkt->destruct = av_destruct_packet;
200 dv->queue_first = packet->next;
210 static int iec61883_parse_queue_hdv(struct iec61883_data *dv, AVPacket *pkt)
215 while (dv->queue_first) {
216 packet = dv->queue_first;
217 size = ff_mpegts_parse_packet(dv->mpeg_demux, pkt, packet->buf,
219 dv->queue_first = packet->next;
220 av_free(packet->buf);
231 static int iec61883_read_header(AVFormatContext *context)
233 struct iec61883_data *dv = context->priv_data;
234 struct raw1394_portinfo pinf[16];
235 rom1394_directory rom_dir;
244 dv->output_port = -1;
247 dv->raw1394 = raw1394_new_handle();
250 av_log(context, AV_LOG_ERROR, "Failed to open IEEE1394 interface.\n");
254 if ((nb_ports = raw1394_get_port_info(dv->raw1394, pinf, 16)) < 0) {
255 av_log(context, AV_LOG_ERROR, "Failed to get number of IEEE1394 ports.\n");
259 inport = strtol(context->filename, &endptr, 10);
260 if (endptr != context->filename && *endptr == '\0') {
261 av_log(context, AV_LOG_INFO, "Selecting IEEE1394 port: %d\n", inport);
263 nb_ports = inport + 1;
264 } else if (strcmp(context->filename, "auto")) {
265 av_log(context, AV_LOG_ERROR, "Invalid input \"%s\", you should specify "
266 "\"auto\" for auto-detection, or the port number.\n", context->filename);
270 /* Select first AV/C tape recorder player node */
272 for (; j < nb_ports && port==-1; ++j) {
273 if (raw1394_set_port(dv->raw1394, j)) {
274 av_log(context, AV_LOG_ERROR, "Failed setting IEEE1394 port.\n");
277 for (i=0; i<raw1394_get_nodecount(dv->raw1394); ++i) {
278 if (rom1394_get_directory(dv->raw1394, i, &rom_dir) < 0)
280 if (((rom1394_get_node_type(&rom_dir) == ROM1394_NODE_TYPE_AVC) &&
281 avc1394_check_subunit_type(dv->raw1394, i, AVC1394_SUBUNIT_TYPE_VCR)) ||
282 (rom_dir.unit_spec_id == MOTDCT_SPEC_ID)) {
283 rom1394_free_directory(&rom_dir);
288 rom1394_free_directory(&rom_dir);
293 av_log(context, AV_LOG_ERROR, "No AV/C devices found.\n");
297 /* Find out if device is DV or HDV */
299 if (dv->type == IEC61883_AUTO) {
300 response = avc1394_transaction(dv->raw1394, dv->node,
301 AVC1394_CTYPE_STATUS |
302 AVC1394_SUBUNIT_TYPE_TAPE_RECORDER |
303 AVC1394_SUBUNIT_ID_0 |
304 AVC1394_VCR_COMMAND_OUTPUT_SIGNAL_MODE |
306 response = AVC1394_GET_OPERAND0(response);
307 dv->type = (response == 0x10 || response == 0x90 || response == 0x1A || response == 0x9A) ?
308 IEC61883_HDV : IEC61883_DV;
311 /* Connect to device, and do initialization */
313 dv->channel = iec61883_cmp_connect(dv->raw1394, dv->node, &dv->output_port,
314 raw1394_get_local_id(dv->raw1394),
315 &dv->input_port, &dv->bandwidth);
320 if (!dv->max_packets)
321 dv->max_packets = 100;
323 if (dv->type == IEC61883_HDV) {
325 /* Init HDV receive */
327 avformat_new_stream(context, NULL);
329 dv->mpeg_demux = ff_mpegts_parse_open(context);
333 dv->parse_queue = iec61883_parse_queue_hdv;
335 dv->iec61883_mpeg2 = iec61883_mpeg2_recv_init(dv->raw1394,
336 (iec61883_mpeg2_recv_t)iec61883_callback,
339 dv->max_packets *= 766;
342 /* Init DV receive */
344 dv->dv_demux = avpriv_dv_init_demux(context);
348 dv->parse_queue = iec61883_parse_queue_dv;
350 dv->iec61883_dv = iec61883_dv_fb_init(dv->raw1394, iec61883_callback, dv);
353 dv->raw1394_poll.fd = raw1394_get_fd(dv->raw1394);
354 dv->raw1394_poll.events = POLLIN | POLLERR | POLLHUP | POLLPRI;
356 /* Actually start receiving */
358 if (dv->type == IEC61883_HDV)
359 iec61883_mpeg2_recv_start(dv->iec61883_mpeg2, dv->channel);
361 iec61883_dv_fb_start(dv->iec61883_dv, dv->channel);
365 pthread_mutex_init(&dv->mutex, NULL);
366 pthread_cond_init(&dv->cond, NULL);
367 pthread_create(&dv->receive_task_thread, NULL, iec61883_receive_task, dv);
373 raw1394_destroy_handle(dv->raw1394);
377 static int iec61883_read_packet(AVFormatContext *context, AVPacket *pkt)
379 struct iec61883_data *dv = context->priv_data;
383 * Try to parse frames from queue
387 pthread_mutex_lock(&dv->mutex);
388 while ((size = dv->parse_queue(dv, pkt)) == -1)
390 pthread_cond_wait(&dv->cond, &dv->mutex);
393 pthread_mutex_unlock(&dv->mutex);
396 while ((size = dv->parse_queue(dv, pkt)) == -1) {
397 iec61883_receive_task((void *)dv);
398 if (dv->receive_error)
399 return dv->receive_error;
406 static int iec61883_close(AVFormatContext *context)
408 struct iec61883_data *dv = context->priv_data;
412 pthread_join(dv->receive_task_thread, NULL);
413 pthread_cond_destroy(&dv->cond);
414 pthread_mutex_destroy(&dv->mutex);
417 if (dv->type == IEC61883_HDV) {
418 iec61883_mpeg2_recv_stop(dv->iec61883_mpeg2);
419 iec61883_mpeg2_close(dv->iec61883_mpeg2);
420 ff_mpegts_parse_close(dv->mpeg_demux);
422 iec61883_dv_fb_stop(dv->iec61883_dv);
423 iec61883_dv_fb_close(dv->iec61883_dv);
425 while (dv->queue_first) {
426 DVPacket *packet = dv->queue_first;
427 dv->queue_first = packet->next;
428 av_free(packet->buf);
432 iec61883_cmp_disconnect(dv->raw1394, dv->node, dv->output_port,
433 raw1394_get_local_id(dv->raw1394),
434 dv->input_port, dv->channel, dv->bandwidth);
436 raw1394_destroy_handle(dv->raw1394);
441 static const AVOption options[] = {
442 { "dvtype", "override autodetection of DV/HDV", offsetof(struct iec61883_data, type), AV_OPT_TYPE_INT, {.i64 = IEC61883_AUTO}, IEC61883_AUTO, IEC61883_HDV, AV_OPT_FLAG_DECODING_PARAM, "dvtype" },
443 { "auto", "auto detect DV/HDV", 0, AV_OPT_TYPE_CONST, {.i64 = IEC61883_AUTO}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "dvtype" },
444 { "dv", "force device being treated as DV device", 0, AV_OPT_TYPE_CONST, {.i64 = IEC61883_DV}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "dvtype" },
445 { "hdv" , "force device being treated as HDV device", 0, AV_OPT_TYPE_CONST, {.i64 = IEC61883_HDV}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "dvtype" },
446 { "dvbuffer", "set queue buffer size (in packets)", offsetof(struct iec61883_data, max_packets), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
450 static const AVClass iec61883_class = {
451 .class_name = "iec61883 indev",
452 .item_name = av_default_item_name,
454 .version = LIBAVUTIL_VERSION_INT,
457 AVInputFormat ff_iec61883_demuxer = {
459 .long_name = NULL_IF_CONFIG_SMALL("libiec61883 (new DV1394) A/V input device"),
460 .priv_data_size = sizeof(struct iec61883_data),
461 .read_header = iec61883_read_header,
462 .read_packet = iec61883_read_packet,
463 .read_close = iec61883_close,
464 .flags = AVFMT_NOFILE,
465 .priv_class = &iec61883_class,