iec61883.c 16 KB

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