• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012 Georg Lippitsch <georg.lippitsch@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 /**
22  * @file
23  * libiec61883 interface
24  */
25 
26 #include "config_components.h"
27 
28 #include <poll.h>
29 #include <libraw1394/raw1394.h>
30 #include <libavc1394/avc1394.h>
31 #include <libavc1394/rom1394.h>
32 #include <libiec61883/iec61883.h>
33 #include "libavformat/dv.h"
34 #include "libavformat/mpegts.h"
35 #include "libavutil/opt.h"
36 #include "avdevice.h"
37 
38 #define THREADS HAVE_PTHREADS
39 
40 #if THREADS
41 #include <pthread.h>
42 #endif
43 
44 #define MOTDCT_SPEC_ID      0x00005068
45 #define IEC61883_AUTO       0
46 #define IEC61883_DV         1
47 #define IEC61883_HDV        2
48 
49 /**
50  * For DV, one packet corresponds exactly to one frame.
51  * For HDV, these are MPEG2 transport stream packets.
52  * The queue is implemented as linked list.
53  */
54 typedef struct DVPacket {
55     uint8_t *buf;                       ///< actual buffer data
56     int len;                            ///< size of buffer allocated
57     struct DVPacket *next;              ///< next DVPacket
58 } DVPacket;
59 
60 struct iec61883_data {
61     AVClass *class;
62     raw1394handle_t raw1394;            ///< handle for libraw1394
63     iec61883_dv_fb_t iec61883_dv;       ///< handle for libiec61883 when used with DV
64     iec61883_mpeg2_t iec61883_mpeg2;    ///< handle for libiec61883 when used with HDV
65 
66     DVDemuxContext *dv_demux;           ///< generic DV muxing/demuxing context
67     MpegTSContext *mpeg_demux;          ///< generic HDV muxing/demuxing context
68 
69     DVPacket *queue_first;              ///< first element of packet queue
70     DVPacket *queue_last;               ///< last element of packet queue
71 
72     char *device_guid;                  ///< to select one of multiple DV devices
73 
74     int packets;                        ///< Number of packets queued
75     int max_packets;                    ///< Max. number of packets in queue
76 
77     int bandwidth;                      ///< returned by libiec61883
78     int channel;                        ///< returned by libiec61883
79     int input_port;                     ///< returned by libiec61883
80     int type;                           ///< Stream type, to distinguish DV/HDV
81     int node;                           ///< returned by libiec61883
82     int output_port;                    ///< returned by libiec61883
83     int thread_loop;                    ///< Condition for thread while-loop
84     int receiving;                      ///< True as soon data from device available
85     int receive_error;                  ///< Set in receive task in case of error
86     int eof;                            ///< True as soon as no more data available
87 
88     struct pollfd raw1394_poll;         ///< to poll for new data from libraw1394
89 
90     /** Parse function for DV/HDV differs, so this is set before packets arrive */
91     int (*parse_queue)(struct iec61883_data *dv, AVPacket *pkt);
92 
93 #if THREADS
94     pthread_t receive_task_thread;
95     pthread_mutex_t mutex;
96     pthread_cond_t cond;
97 #endif
98 };
99 
iec61883_callback(unsigned char * data,int length,int complete,void * callback_data)100 static int iec61883_callback(unsigned char *data, int length,
101                              int complete, void *callback_data)
102 {
103     struct iec61883_data *dv = callback_data;
104     DVPacket *packet;
105     int ret;
106 
107 #if THREADS
108     pthread_mutex_lock(&dv->mutex);
109 #endif
110 
111     if (dv->packets >= dv->max_packets) {
112         av_log(NULL, AV_LOG_ERROR, "DV packet queue overrun, dropping.\n");
113         ret = 0;
114         goto exit;
115     }
116 
117     packet = av_mallocz(sizeof(*packet));
118     if (!packet) {
119         ret = -1;
120         goto exit;
121     }
122 
123     packet->buf = av_malloc(length + AV_INPUT_BUFFER_PADDING_SIZE);
124     if (!packet->buf) {
125         av_free(packet);
126         ret = -1;
127         goto exit;
128     }
129     packet->len = length;
130 
131     memcpy(packet->buf, data, length);
132     memset(packet->buf + length, 0, AV_INPUT_BUFFER_PADDING_SIZE);
133 
134     if (dv->queue_first) {
135         dv->queue_last->next = packet;
136         dv->queue_last = packet;
137     } else {
138         dv->queue_first = packet;
139         dv->queue_last = packet;
140     }
141     dv->packets++;
142 
143     ret = 0;
144 
145 exit:
146 #if THREADS
147     pthread_cond_broadcast(&dv->cond);
148     pthread_mutex_unlock(&dv->mutex);
149 #endif
150     return ret;
151 }
152 
iec61883_receive_task(void * opaque)153 static void *iec61883_receive_task(void *opaque)
154 {
155     struct iec61883_data *dv = (struct iec61883_data *)opaque;
156     int result;
157 
158 #if THREADS
159     while (dv->thread_loop)
160 #endif
161     {
162         while ((result = poll(&dv->raw1394_poll, 1, 200)) < 0) {
163             if (!(errno == EAGAIN || errno == EINTR)) {
164                 av_log(NULL, AV_LOG_ERROR, "Raw1394 poll error occurred.\n");
165                 dv->receive_error = AVERROR(EIO);
166                 return NULL;
167             }
168         }
169         if (result > 0 && ((dv->raw1394_poll.revents & POLLIN)
170                            || (dv->raw1394_poll.revents & POLLPRI))) {
171             dv->receiving = 1;
172             raw1394_loop_iterate(dv->raw1394);
173         } else if (dv->receiving) {
174             av_log(NULL, AV_LOG_ERROR, "No more input data available\n");
175 #if THREADS
176             pthread_mutex_lock(&dv->mutex);
177             dv->eof = 1;
178             pthread_cond_broadcast(&dv->cond);
179             pthread_mutex_unlock(&dv->mutex);
180 #else
181             dv->eof = 1;
182 #endif
183             return NULL;
184         }
185     }
186 
187     return NULL;
188 }
189 
iec61883_parse_queue_dv(struct iec61883_data * dv,AVPacket * pkt)190 static int iec61883_parse_queue_dv(struct iec61883_data *dv, AVPacket *pkt)
191 {
192     DVPacket *packet;
193     int size;
194 
195     size = avpriv_dv_get_packet(dv->dv_demux, pkt);
196     if (size > 0)
197         return size;
198 
199     packet = dv->queue_first;
200     if (!packet)
201         return -1;
202 
203     size = avpriv_dv_produce_packet(dv->dv_demux, pkt,
204                                     packet->buf, packet->len, -1);
205     dv->queue_first = packet->next;
206     if (size < 0)
207         av_free(packet->buf);
208     av_free(packet);
209     dv->packets--;
210 
211     if (size < 0)
212         return -1;
213 
214     if (av_packet_from_data(pkt, pkt->data, pkt->size) < 0) {
215         av_freep(&pkt->data);
216         av_packet_unref(pkt);
217         return -1;
218     }
219 
220     return size;
221 }
222 
iec61883_parse_queue_hdv(struct iec61883_data * dv,AVPacket * pkt)223 static int iec61883_parse_queue_hdv(struct iec61883_data *dv, AVPacket *pkt)
224 {
225 #if CONFIG_MPEGTS_DEMUXER
226     DVPacket *packet;
227     int size;
228 
229     while (dv->queue_first) {
230         packet = dv->queue_first;
231         size = avpriv_mpegts_parse_packet(dv->mpeg_demux, pkt, packet->buf,
232                                           packet->len);
233         dv->queue_first = packet->next;
234         av_freep(&packet->buf);
235         av_freep(&packet);
236         dv->packets--;
237 
238         if (size > 0)
239             return size;
240     }
241 #endif
242     return -1;
243 }
244 
iec61883_read_header(AVFormatContext * context)245 static int iec61883_read_header(AVFormatContext *context)
246 {
247     struct iec61883_data *dv = context->priv_data;
248     struct raw1394_portinfo pinf[16];
249     rom1394_directory rom_dir;
250     char *endptr;
251     int inport;
252     int nb_ports;
253     int port = -1;
254     int response;
255     int i, j = 0;
256     uint64_t guid = 0;
257 
258     dv->input_port = -1;
259     dv->output_port = -1;
260     dv->channel = -1;
261 
262     dv->raw1394 = raw1394_new_handle();
263 
264     if (!dv->raw1394) {
265         av_log(context, AV_LOG_ERROR, "Failed to open IEEE1394 interface.\n");
266         return AVERROR(EIO);
267     }
268 
269     if ((nb_ports = raw1394_get_port_info(dv->raw1394, pinf, 16)) < 0) {
270         av_log(context, AV_LOG_ERROR, "Failed to get number of IEEE1394 ports.\n");
271         goto fail;
272     }
273 
274     inport = strtol(context->url, &endptr, 10);
275     if (endptr != context->url && *endptr == '\0') {
276         av_log(context, AV_LOG_INFO, "Selecting IEEE1394 port: %d\n", inport);
277         j = inport;
278         nb_ports = inport + 1;
279     } else if (strcmp(context->url, "auto")) {
280         av_log(context, AV_LOG_ERROR, "Invalid input \"%s\", you should specify "
281                "\"auto\" for auto-detection, or the port number.\n", context->url);
282         goto fail;
283     }
284 
285     if (dv->device_guid) {
286         if (sscanf(dv->device_guid, "%"SCNu64, &guid) != 1) {
287             av_log(context, AV_LOG_INFO, "Invalid dvguid parameter: %s\n",
288                    dv->device_guid);
289             goto fail;
290         }
291     }
292 
293     for (; j < nb_ports && port==-1; ++j) {
294         raw1394_destroy_handle(dv->raw1394);
295 
296         if (!(dv->raw1394 = raw1394_new_handle_on_port(j))) {
297             av_log(context, AV_LOG_ERROR, "Failed setting IEEE1394 port.\n");
298             goto fail;
299         }
300 
301         for (i=0; i<raw1394_get_nodecount(dv->raw1394); ++i) {
302 
303             /* Select device explicitly by GUID */
304 
305             if (guid > 1) {
306                 if (guid == rom1394_get_guid(dv->raw1394, i)) {
307                     dv->node = i;
308                     port = j;
309                     break;
310                 }
311             } else {
312 
313                 /* Select first AV/C tape recorder player node */
314 
315                 if (rom1394_get_directory(dv->raw1394, i, &rom_dir) < 0)
316                     continue;
317                 if (((rom1394_get_node_type(&rom_dir) == ROM1394_NODE_TYPE_AVC) &&
318                      avc1394_check_subunit_type(dv->raw1394, i, AVC1394_SUBUNIT_TYPE_VCR)) ||
319                     (rom_dir.unit_spec_id == MOTDCT_SPEC_ID)) {
320                     rom1394_free_directory(&rom_dir);
321                     dv->node = i;
322                     port = j;
323                     break;
324                 }
325                 rom1394_free_directory(&rom_dir);
326             }
327         }
328     }
329 
330     if (port == -1) {
331         av_log(context, AV_LOG_ERROR, "No AV/C devices found.\n");
332         goto fail;
333     }
334 
335     /* Provide bus sanity for multiple connections */
336 
337     iec61883_cmp_normalize_output(dv->raw1394, 0xffc0 | dv->node);
338 
339     /* Find out if device is DV or HDV */
340 
341     if (dv->type == IEC61883_AUTO) {
342         response = avc1394_transaction(dv->raw1394, dv->node,
343                                        AVC1394_CTYPE_STATUS |
344                                        AVC1394_SUBUNIT_TYPE_TAPE_RECORDER |
345                                        AVC1394_SUBUNIT_ID_0 |
346                                        AVC1394_VCR_COMMAND_OUTPUT_SIGNAL_MODE |
347                                        0xFF, 2);
348         response = AVC1394_GET_OPERAND0(response);
349         dv->type = (response == 0x10 || response == 0x90 || response == 0x1A || response == 0x9A) ?
350             IEC61883_HDV : IEC61883_DV;
351     }
352 
353     /* Connect to device, and do initialization */
354 
355     dv->channel = iec61883_cmp_connect(dv->raw1394, dv->node, &dv->output_port,
356                                        raw1394_get_local_id(dv->raw1394),
357                                        &dv->input_port, &dv->bandwidth);
358 
359     if (dv->channel < 0)
360         dv->channel = 63;
361 
362     if (!dv->max_packets)
363         dv->max_packets = 100;
364 
365     if (CONFIG_MPEGTS_DEMUXER && dv->type == IEC61883_HDV) {
366 
367         /* Init HDV receive */
368 
369         avformat_new_stream(context, NULL);
370 
371         dv->mpeg_demux = avpriv_mpegts_parse_open(context);
372         if (!dv->mpeg_demux)
373             goto fail;
374 
375         dv->parse_queue = iec61883_parse_queue_hdv;
376 
377         dv->iec61883_mpeg2 = iec61883_mpeg2_recv_init(dv->raw1394,
378                                                       (iec61883_mpeg2_recv_t)iec61883_callback,
379                                                       dv);
380 
381         dv->max_packets *= 766;
382     } else {
383 
384         /* Init DV receive */
385 
386         dv->dv_demux = avpriv_dv_init_demux(context);
387         if (!dv->dv_demux)
388             goto fail;
389 
390         dv->parse_queue = iec61883_parse_queue_dv;
391 
392         dv->iec61883_dv = iec61883_dv_fb_init(dv->raw1394, iec61883_callback, dv);
393     }
394 
395     dv->raw1394_poll.fd = raw1394_get_fd(dv->raw1394);
396     dv->raw1394_poll.events = POLLIN | POLLERR | POLLHUP | POLLPRI;
397 
398     /* Actually start receiving */
399 
400     if (dv->type == IEC61883_HDV)
401         iec61883_mpeg2_recv_start(dv->iec61883_mpeg2, dv->channel);
402     else
403         iec61883_dv_fb_start(dv->iec61883_dv, dv->channel);
404 
405 #if THREADS
406     dv->thread_loop = 1;
407     if (pthread_mutex_init(&dv->mutex, NULL))
408         goto fail;
409     if (pthread_cond_init(&dv->cond, NULL))
410         goto fail;
411     if (pthread_create(&dv->receive_task_thread, NULL, iec61883_receive_task, dv))
412         goto fail;
413 #endif
414 
415     return 0;
416 
417 fail:
418     raw1394_destroy_handle(dv->raw1394);
419     return AVERROR(EIO);
420 }
421 
iec61883_read_packet(AVFormatContext * context,AVPacket * pkt)422 static int iec61883_read_packet(AVFormatContext *context, AVPacket *pkt)
423 {
424     struct iec61883_data *dv = context->priv_data;
425     int size;
426 
427     /**
428      * Try to parse frames from queue
429      */
430 
431 #if THREADS
432     pthread_mutex_lock(&dv->mutex);
433     while ((size = dv->parse_queue(dv, pkt)) == -1)
434         if (!dv->eof)
435             pthread_cond_wait(&dv->cond, &dv->mutex);
436         else
437             break;
438     pthread_mutex_unlock(&dv->mutex);
439 #else
440     int result;
441     while ((size = dv->parse_queue(dv, pkt)) == -1) {
442         iec61883_receive_task((void *)dv);
443         if (dv->receive_error)
444             return dv->receive_error;
445     }
446 #endif
447 
448     return size;
449 }
450 
iec61883_close(AVFormatContext * context)451 static int iec61883_close(AVFormatContext *context)
452 {
453     struct iec61883_data *dv = context->priv_data;
454 
455 #if THREADS
456     dv->thread_loop = 0;
457     pthread_join(dv->receive_task_thread, NULL);
458     pthread_cond_destroy(&dv->cond);
459     pthread_mutex_destroy(&dv->mutex);
460 #endif
461 
462     if (CONFIG_MPEGTS_DEMUXER && dv->type == IEC61883_HDV) {
463         iec61883_mpeg2_recv_stop(dv->iec61883_mpeg2);
464         iec61883_mpeg2_close(dv->iec61883_mpeg2);
465         avpriv_mpegts_parse_close(dv->mpeg_demux);
466     } else {
467         iec61883_dv_fb_stop(dv->iec61883_dv);
468         iec61883_dv_fb_close(dv->iec61883_dv);
469         av_freep(&dv->dv_demux);
470     }
471     while (dv->queue_first) {
472         DVPacket *packet = dv->queue_first;
473         dv->queue_first = packet->next;
474         av_freep(&packet->buf);
475         av_freep(&packet);
476     }
477 
478     iec61883_cmp_disconnect(dv->raw1394, dv->node, dv->output_port,
479                             raw1394_get_local_id(dv->raw1394),
480                             dv->input_port, dv->channel, dv->bandwidth);
481 
482     raw1394_destroy_handle(dv->raw1394);
483 
484     return 0;
485 }
486 
487 static const AVOption options[] = {
488     { "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" },
489     { "auto",   "auto detect DV/HDV", 0, AV_OPT_TYPE_CONST, {.i64 = IEC61883_AUTO}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "dvtype" },
490     { "dv",     "force device being treated as DV device", 0, AV_OPT_TYPE_CONST, {.i64 = IEC61883_DV},   0, 0, AV_OPT_FLAG_DECODING_PARAM, "dvtype" },
491     { "hdv" ,   "force device being treated as HDV device", 0, AV_OPT_TYPE_CONST, {.i64 = IEC61883_HDV},  0, 0, AV_OPT_FLAG_DECODING_PARAM, "dvtype" },
492     { "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 },
493     { "dvguid", "select one of multiple DV devices by its GUID", offsetof(struct iec61883_data, device_guid), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
494     { NULL },
495 };
496 
497 static const AVClass iec61883_class = {
498     .class_name = "iec61883 indev",
499     .item_name  = av_default_item_name,
500     .option     = options,
501     .version    = LIBAVUTIL_VERSION_INT,
502     .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
503 };
504 
505 const AVInputFormat ff_iec61883_demuxer = {
506     .name           = "iec61883",
507     .long_name      = NULL_IF_CONFIG_SMALL("libiec61883 (new DV1394) A/V input device"),
508     .priv_data_size = sizeof(struct iec61883_data),
509     .read_header    = iec61883_read_header,
510     .read_packet    = iec61883_read_packet,
511     .read_close     = iec61883_close,
512     .flags          = AVFMT_NOFILE,
513     .priv_class     = &iec61883_class,
514 };
515