iec61883.c 16 KB

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