iec61883.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. pkt->destruct = av_destruct_packet;
  171. dv->queue_first = packet->next;
  172. av_free(packet);
  173. dv->packets--;
  174. if (size > 0)
  175. return size;
  176. return -1;
  177. }
  178. static int iec61883_parse_queue_hdv(struct iec61883_data *dv, AVPacket *pkt)
  179. {
  180. DVPacket *packet;
  181. int size;
  182. while (dv->queue_first) {
  183. packet = dv->queue_first;
  184. size = avpriv_mpegts_parse_packet(dv->mpeg_demux, pkt, packet->buf,
  185. packet->len);
  186. dv->queue_first = packet->next;
  187. av_freep(&packet->buf);
  188. av_freep(&packet);
  189. dv->packets--;
  190. if (size > 0)
  191. return size;
  192. }
  193. return -1;
  194. }
  195. static int iec61883_read_header(AVFormatContext *context)
  196. {
  197. struct iec61883_data *dv = context->priv_data;
  198. struct raw1394_portinfo pinf[16];
  199. rom1394_directory rom_dir;
  200. char *endptr;
  201. int inport;
  202. int nb_ports;
  203. int port = -1;
  204. int response;
  205. int i, j = 0;
  206. uint64_t guid = 0;
  207. dv->input_port = -1;
  208. dv->output_port = -1;
  209. dv->channel = -1;
  210. dv->raw1394 = raw1394_new_handle();
  211. if (!dv->raw1394) {
  212. av_log(context, AV_LOG_ERROR, "Failed to open IEEE1394 interface.\n");
  213. return AVERROR(EIO);
  214. }
  215. if ((nb_ports = raw1394_get_port_info(dv->raw1394, pinf, 16)) < 0) {
  216. av_log(context, AV_LOG_ERROR, "Failed to get number of IEEE1394 ports.\n");
  217. goto fail;
  218. }
  219. inport = strtol(context->filename, &endptr, 10);
  220. if (endptr != context->filename && *endptr == '\0') {
  221. av_log(context, AV_LOG_INFO, "Selecting IEEE1394 port: %d\n", inport);
  222. j = inport;
  223. nb_ports = inport + 1;
  224. } else if (strcmp(context->filename, "auto")) {
  225. av_log(context, AV_LOG_ERROR, "Invalid input \"%s\", you should specify "
  226. "\"auto\" for auto-detection, or the port number.\n", context->filename);
  227. goto fail;
  228. }
  229. if (dv->device_guid) {
  230. if (sscanf(dv->device_guid, "%llx", (long long unsigned int *)&guid) != 1) {
  231. av_log(context, AV_LOG_INFO, "Invalid dvguid parameter: %s\n",
  232. dv->device_guid);
  233. goto fail;
  234. }
  235. }
  236. for (; j < nb_ports && port==-1; ++j) {
  237. raw1394_destroy_handle(dv->raw1394);
  238. if (!(dv->raw1394 = raw1394_new_handle_on_port(j))) {
  239. av_log(context, AV_LOG_ERROR, "Failed setting IEEE1394 port.\n");
  240. goto fail;
  241. }
  242. for (i=0; i<raw1394_get_nodecount(dv->raw1394); ++i) {
  243. /* Select device explicitly by GUID */
  244. if (guid > 1) {
  245. if (guid == rom1394_get_guid(dv->raw1394, i)) {
  246. dv->node = i;
  247. port = j;
  248. break;
  249. }
  250. } else {
  251. /* Select first AV/C tape recorder player node */
  252. if (rom1394_get_directory(dv->raw1394, i, &rom_dir) < 0)
  253. continue;
  254. if (((rom1394_get_node_type(&rom_dir) == ROM1394_NODE_TYPE_AVC) &&
  255. avc1394_check_subunit_type(dv->raw1394, i, AVC1394_SUBUNIT_TYPE_VCR)) ||
  256. (rom_dir.unit_spec_id == MOTDCT_SPEC_ID)) {
  257. rom1394_free_directory(&rom_dir);
  258. dv->node = i;
  259. port = j;
  260. break;
  261. }
  262. rom1394_free_directory(&rom_dir);
  263. }
  264. }
  265. }
  266. if (port == -1) {
  267. av_log(context, AV_LOG_ERROR, "No AV/C devices found.\n");
  268. goto fail;
  269. }
  270. /* Provide bus sanity for multiple connections */
  271. iec61883_cmp_normalize_output(dv->raw1394, 0xffc0 | dv->node);
  272. /* Find out if device is DV or HDV */
  273. if (dv->type == IEC61883_AUTO) {
  274. response = avc1394_transaction(dv->raw1394, dv->node,
  275. AVC1394_CTYPE_STATUS |
  276. AVC1394_SUBUNIT_TYPE_TAPE_RECORDER |
  277. AVC1394_SUBUNIT_ID_0 |
  278. AVC1394_VCR_COMMAND_OUTPUT_SIGNAL_MODE |
  279. 0xFF, 2);
  280. response = AVC1394_GET_OPERAND0(response);
  281. dv->type = (response == 0x10 || response == 0x90 || response == 0x1A || response == 0x9A) ?
  282. IEC61883_HDV : IEC61883_DV;
  283. }
  284. /* Connect to device, and do initialization */
  285. dv->channel = iec61883_cmp_connect(dv->raw1394, dv->node, &dv->output_port,
  286. raw1394_get_local_id(dv->raw1394),
  287. &dv->input_port, &dv->bandwidth);
  288. if (dv->channel < 0)
  289. dv->channel = 63;
  290. if (!dv->max_packets)
  291. dv->max_packets = 100;
  292. if (CONFIG_MPEGTS_DEMUXER && dv->type == IEC61883_HDV) {
  293. /* Init HDV receive */
  294. avformat_new_stream(context, NULL);
  295. dv->mpeg_demux = avpriv_mpegts_parse_open(context);
  296. if (!dv->mpeg_demux)
  297. goto fail;
  298. dv->parse_queue = iec61883_parse_queue_hdv;
  299. dv->iec61883_mpeg2 = iec61883_mpeg2_recv_init(dv->raw1394,
  300. (iec61883_mpeg2_recv_t)iec61883_callback,
  301. dv);
  302. dv->max_packets *= 766;
  303. } else {
  304. /* Init DV receive */
  305. dv->dv_demux = avpriv_dv_init_demux(context);
  306. if (!dv->dv_demux)
  307. goto fail;
  308. dv->parse_queue = iec61883_parse_queue_dv;
  309. dv->iec61883_dv = iec61883_dv_fb_init(dv->raw1394, iec61883_callback, dv);
  310. }
  311. dv->raw1394_poll.fd = raw1394_get_fd(dv->raw1394);
  312. dv->raw1394_poll.events = POLLIN | POLLERR | POLLHUP | POLLPRI;
  313. /* Actually start receiving */
  314. if (dv->type == IEC61883_HDV)
  315. iec61883_mpeg2_recv_start(dv->iec61883_mpeg2, dv->channel);
  316. else
  317. iec61883_dv_fb_start(dv->iec61883_dv, dv->channel);
  318. #if THREADS
  319. dv->thread_loop = 1;
  320. pthread_mutex_init(&dv->mutex, NULL);
  321. pthread_cond_init(&dv->cond, NULL);
  322. pthread_create(&dv->receive_task_thread, NULL, iec61883_receive_task, dv);
  323. #endif
  324. return 0;
  325. fail:
  326. raw1394_destroy_handle(dv->raw1394);
  327. return AVERROR(EIO);
  328. }
  329. static int iec61883_read_packet(AVFormatContext *context, AVPacket *pkt)
  330. {
  331. struct iec61883_data *dv = context->priv_data;
  332. int size;
  333. /**
  334. * Try to parse frames from queue
  335. */
  336. #if THREADS
  337. pthread_mutex_lock(&dv->mutex);
  338. while ((size = dv->parse_queue(dv, pkt)) == -1)
  339. if (!dv->eof)
  340. pthread_cond_wait(&dv->cond, &dv->mutex);
  341. else
  342. break;
  343. pthread_mutex_unlock(&dv->mutex);
  344. #else
  345. int result;
  346. while ((size = dv->parse_queue(dv, pkt)) == -1) {
  347. iec61883_receive_task((void *)dv);
  348. if (dv->receive_error)
  349. return dv->receive_error;
  350. }
  351. #endif
  352. return size;
  353. }
  354. static int iec61883_close(AVFormatContext *context)
  355. {
  356. struct iec61883_data *dv = context->priv_data;
  357. #if THREADS
  358. dv->thread_loop = 0;
  359. pthread_join(dv->receive_task_thread, NULL);
  360. pthread_cond_destroy(&dv->cond);
  361. pthread_mutex_destroy(&dv->mutex);
  362. #endif
  363. if (CONFIG_MPEGTS_DEMUXER && dv->type == IEC61883_HDV) {
  364. iec61883_mpeg2_recv_stop(dv->iec61883_mpeg2);
  365. iec61883_mpeg2_close(dv->iec61883_mpeg2);
  366. avpriv_mpegts_parse_close(dv->mpeg_demux);
  367. } else {
  368. iec61883_dv_fb_stop(dv->iec61883_dv);
  369. iec61883_dv_fb_close(dv->iec61883_dv);
  370. }
  371. while (dv->queue_first) {
  372. DVPacket *packet = dv->queue_first;
  373. dv->queue_first = packet->next;
  374. av_freep(&packet->buf);
  375. av_freep(&packet);
  376. }
  377. iec61883_cmp_disconnect(dv->raw1394, dv->node, dv->output_port,
  378. raw1394_get_local_id(dv->raw1394),
  379. dv->input_port, dv->channel, dv->bandwidth);
  380. raw1394_destroy_handle(dv->raw1394);
  381. return 0;
  382. }
  383. static const AVOption options[] = {
  384. { "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" },
  385. { "auto", "auto detect DV/HDV", 0, AV_OPT_TYPE_CONST, {.i64 = IEC61883_AUTO}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "dvtype" },
  386. { "dv", "force device being treated as DV device", 0, AV_OPT_TYPE_CONST, {.i64 = IEC61883_DV}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "dvtype" },
  387. { "hdv" , "force device being treated as HDV device", 0, AV_OPT_TYPE_CONST, {.i64 = IEC61883_HDV}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "dvtype" },
  388. { "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 },
  389. { "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 },
  390. { NULL },
  391. };
  392. static const AVClass iec61883_class = {
  393. .class_name = "iec61883 indev",
  394. .item_name = av_default_item_name,
  395. .option = options,
  396. .version = LIBAVUTIL_VERSION_INT,
  397. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  398. };
  399. AVInputFormat ff_iec61883_demuxer = {
  400. .name = "iec61883",
  401. .long_name = NULL_IF_CONFIG_SMALL("libiec61883 (new DV1394) A/V input device"),
  402. .priv_data_size = sizeof(struct iec61883_data),
  403. .read_header = iec61883_read_header,
  404. .read_packet = iec61883_read_packet,
  405. .read_close = iec61883_close,
  406. .flags = AVFMT_NOFILE,
  407. .priv_class = &iec61883_class,
  408. };