iec61883.c 16 KB

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