iec61883.c 15 KB

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