iec61883.c 16 KB

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