iec61883.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. int packets; ///< Number of packets queued
  61. int max_packets; ///< Max. number of packets in queue
  62. int bandwidth; ///< returned by libiec61883
  63. int channel; ///< returned by libiec61883
  64. int input_port; ///< returned by libiec61883
  65. int type; ///< Stream type, to distinguish DV/HDV
  66. int node; ///< returned by libiec61883
  67. int output_port; ///< returned by libiec61883
  68. int thread_loop; ///< Condition for thread while-loop
  69. int receiving; ///< True as soon data from device available
  70. int receive_error; ///< Set in receive task in case of error
  71. int eof; ///< True as soon as no more data available
  72. struct pollfd raw1394_poll; ///< to poll for new data from libraw1394
  73. /** Parse function for DV/HDV differs, so this is set before packets arrive */
  74. int (*parse_queue)(struct iec61883_data *dv, AVPacket *pkt);
  75. #if THREADS
  76. pthread_t receive_task_thread;
  77. pthread_mutex_t mutex;
  78. pthread_cond_t cond;
  79. #endif
  80. };
  81. static int iec61883_callback(unsigned char *data, int length,
  82. int complete, void *callback_data)
  83. {
  84. struct iec61883_data *dv = callback_data;
  85. DVPacket *packet;
  86. int ret;
  87. #ifdef THREADS
  88. pthread_mutex_lock(&dv->mutex);
  89. #endif
  90. if (dv->packets >= dv->max_packets) {
  91. av_log(NULL, AV_LOG_ERROR, "DV packet queue overrun, dropping.\n");
  92. ret = 0;
  93. goto exit;
  94. }
  95. packet = av_mallocz(sizeof(*packet));
  96. if (!packet) {
  97. ret = -1;
  98. goto exit;
  99. }
  100. packet->buf = av_malloc(length);
  101. if (!packet->buf) {
  102. ret = -1;
  103. goto exit;
  104. }
  105. packet->len = length;
  106. memcpy(packet->buf, data, length);
  107. if (dv->queue_first) {
  108. dv->queue_last->next = packet;
  109. dv->queue_last = packet;
  110. } else {
  111. dv->queue_first = packet;
  112. dv->queue_last = packet;
  113. }
  114. dv->packets++;
  115. ret = 0;
  116. exit:
  117. #ifdef THREADS
  118. pthread_cond_signal(&dv->cond);
  119. pthread_mutex_unlock(&dv->mutex);
  120. #endif
  121. return ret;
  122. }
  123. static void *iec61883_receive_task(void *opaque)
  124. {
  125. struct iec61883_data *dv = (struct iec61883_data *)opaque;
  126. int result;
  127. #ifdef THREADS
  128. while (dv->thread_loop)
  129. #endif
  130. {
  131. while ((result = poll(&dv->raw1394_poll, 1, 200)) < 0) {
  132. if (!(errno == EAGAIN || errno == EINTR)) {
  133. av_log(NULL, AV_LOG_ERROR, "Raw1394 poll error occurred.\n");
  134. dv->receive_error = AVERROR(EIO);
  135. return NULL;
  136. }
  137. }
  138. if (result > 0 && ((dv->raw1394_poll.revents & POLLIN)
  139. || (dv->raw1394_poll.revents & POLLPRI))) {
  140. dv->receiving = 1;
  141. raw1394_loop_iterate(dv->raw1394);
  142. } else if (dv->receiving) {
  143. av_log(NULL, AV_LOG_ERROR, "No more input data available\n");
  144. #ifdef THREADS
  145. pthread_mutex_lock(&dv->mutex);
  146. dv->eof = 1;
  147. pthread_cond_signal(&dv->cond);
  148. pthread_mutex_unlock(&dv->mutex);
  149. #else
  150. dv->eof = 1;
  151. #endif
  152. return NULL;
  153. }
  154. }
  155. return NULL;
  156. }
  157. static int iec61883_parse_queue_dv(struct iec61883_data *dv, AVPacket *pkt)
  158. {
  159. DVPacket *packet;
  160. int size;
  161. size = avpriv_dv_get_packet(dv->dv_demux, pkt);
  162. if (size > 0)
  163. return size;
  164. packet = dv->queue_first;
  165. if (!packet)
  166. return -1;
  167. size = avpriv_dv_produce_packet(dv->dv_demux, pkt,
  168. packet->buf, packet->len, -1);
  169. pkt->destruct = av_destruct_packet;
  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 = ff_mpegts_parse_packet(dv->mpeg_demux, pkt, packet->buf,
  184. packet->len);
  185. dv->queue_first = packet->next;
  186. av_free(packet->buf);
  187. av_free(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. dv->input_port = -1;
  206. dv->output_port = -1;
  207. dv->channel = -1;
  208. dv->raw1394 = raw1394_new_handle();
  209. if (!dv->raw1394) {
  210. av_log(context, AV_LOG_ERROR, "Failed to open IEEE1394 interface.\n");
  211. return AVERROR(EIO);
  212. }
  213. if ((nb_ports = raw1394_get_port_info(dv->raw1394, pinf, 16)) < 0) {
  214. av_log(context, AV_LOG_ERROR, "Failed to get number of IEEE1394 ports.\n");
  215. goto fail;
  216. }
  217. inport = strtol(context->filename, &endptr, 10);
  218. if (endptr != context->filename && *endptr == '\0') {
  219. av_log(context, AV_LOG_INFO, "Selecting IEEE1394 port: %d\n", inport);
  220. j = inport;
  221. nb_ports = inport + 1;
  222. } else if (strcmp(context->filename, "auto")) {
  223. av_log(context, AV_LOG_ERROR, "Invalid input \"%s\", you should specify "
  224. "\"auto\" for auto-detection, or the port number.\n", context->filename);
  225. goto fail;
  226. }
  227. /* Select first AV/C tape recorder player node */
  228. for (; j < nb_ports && port==-1; ++j) {
  229. if (raw1394_set_port(dv->raw1394, j)) {
  230. av_log(context, AV_LOG_ERROR, "Failed setting IEEE1394 port.\n");
  231. goto fail;
  232. }
  233. for (i=0; i<raw1394_get_nodecount(dv->raw1394); ++i) {
  234. if (rom1394_get_directory(dv->raw1394, i, &rom_dir) < 0)
  235. continue;
  236. if (((rom1394_get_node_type(&rom_dir) == ROM1394_NODE_TYPE_AVC) &&
  237. avc1394_check_subunit_type(dv->raw1394, i, AVC1394_SUBUNIT_TYPE_VCR)) ||
  238. (rom_dir.unit_spec_id == MOTDCT_SPEC_ID)) {
  239. rom1394_free_directory(&rom_dir);
  240. dv->node = i;
  241. port = j;
  242. break;
  243. }
  244. rom1394_free_directory(&rom_dir);
  245. }
  246. }
  247. if (port == -1) {
  248. av_log(context, AV_LOG_ERROR, "No AV/C devices found.\n");
  249. goto fail;
  250. }
  251. /* Find out if device is DV or HDV */
  252. if (dv->type == IEC61883_AUTO) {
  253. response = avc1394_transaction(dv->raw1394, dv->node,
  254. AVC1394_CTYPE_STATUS |
  255. AVC1394_SUBUNIT_TYPE_TAPE_RECORDER |
  256. AVC1394_SUBUNIT_ID_0 |
  257. AVC1394_VCR_COMMAND_OUTPUT_SIGNAL_MODE |
  258. 0xFF, 2);
  259. response = AVC1394_GET_OPERAND0(response);
  260. dv->type = (response == 0x10 || response == 0x90 || response == 0x1A || response == 0x9A) ?
  261. IEC61883_HDV : IEC61883_DV;
  262. }
  263. /* Connect to device, and do initialization */
  264. dv->channel = iec61883_cmp_connect(dv->raw1394, dv->node, &dv->output_port,
  265. raw1394_get_local_id(dv->raw1394),
  266. &dv->input_port, &dv->bandwidth);
  267. if (dv->channel < 0)
  268. dv->channel = 63;
  269. if (!dv->max_packets)
  270. dv->max_packets = 100;
  271. if (dv->type == IEC61883_HDV) {
  272. /* Init HDV receive */
  273. avformat_new_stream(context, NULL);
  274. dv->mpeg_demux = ff_mpegts_parse_open(context);
  275. if (!dv->mpeg_demux)
  276. goto fail;
  277. dv->parse_queue = iec61883_parse_queue_hdv;
  278. dv->iec61883_mpeg2 = iec61883_mpeg2_recv_init(dv->raw1394,
  279. (iec61883_mpeg2_recv_t)iec61883_callback,
  280. dv);
  281. dv->max_packets *= 766;
  282. } else {
  283. /* Init DV receive */
  284. dv->dv_demux = avpriv_dv_init_demux(context);
  285. if (!dv->dv_demux)
  286. goto fail;
  287. dv->parse_queue = iec61883_parse_queue_dv;
  288. dv->iec61883_dv = iec61883_dv_fb_init(dv->raw1394, iec61883_callback, dv);
  289. }
  290. dv->raw1394_poll.fd = raw1394_get_fd(dv->raw1394);
  291. dv->raw1394_poll.events = POLLIN | POLLERR | POLLHUP | POLLPRI;
  292. /* Actually start receiving */
  293. if (dv->type == IEC61883_HDV)
  294. iec61883_mpeg2_recv_start(dv->iec61883_mpeg2, dv->channel);
  295. else
  296. iec61883_dv_fb_start(dv->iec61883_dv, dv->channel);
  297. #if THREADS
  298. dv->thread_loop = 1;
  299. pthread_mutex_init(&dv->mutex, NULL);
  300. pthread_cond_init(&dv->cond, NULL);
  301. pthread_create(&dv->receive_task_thread, NULL, iec61883_receive_task, dv);
  302. #endif
  303. return 0;
  304. fail:
  305. raw1394_destroy_handle(dv->raw1394);
  306. return AVERROR(EIO);
  307. }
  308. static int iec61883_read_packet(AVFormatContext *context, AVPacket *pkt)
  309. {
  310. struct iec61883_data *dv = context->priv_data;
  311. int size;
  312. /**
  313. * Try to parse frames from queue
  314. */
  315. #ifdef THREADS
  316. pthread_mutex_lock(&dv->mutex);
  317. while ((size = dv->parse_queue(dv, pkt)) == -1)
  318. if (!dv->eof)
  319. pthread_cond_wait(&dv->cond, &dv->mutex);
  320. else
  321. break;
  322. pthread_mutex_unlock(&dv->mutex);
  323. #else
  324. int result;
  325. while ((size = dv->parse_queue(dv, pkt)) == -1) {
  326. iec61883_receive_task((void *)dv);
  327. if (dv->receive_error)
  328. return dv->receive_error;
  329. }
  330. #endif
  331. return size;
  332. }
  333. static int iec61883_close(AVFormatContext *context)
  334. {
  335. struct iec61883_data *dv = context->priv_data;
  336. #if THREADS
  337. dv->thread_loop = 0;
  338. pthread_join(dv->receive_task_thread, NULL);
  339. pthread_cond_destroy(&dv->cond);
  340. pthread_mutex_destroy(&dv->mutex);
  341. #endif
  342. if (dv->type == IEC61883_HDV) {
  343. iec61883_mpeg2_recv_stop(dv->iec61883_mpeg2);
  344. iec61883_mpeg2_close(dv->iec61883_mpeg2);
  345. ff_mpegts_parse_close(dv->mpeg_demux);
  346. } else {
  347. iec61883_dv_fb_stop(dv->iec61883_dv);
  348. iec61883_dv_fb_close(dv->iec61883_dv);
  349. }
  350. while (dv->queue_first) {
  351. DVPacket *packet = dv->queue_first;
  352. dv->queue_first = packet->next;
  353. av_free(packet->buf);
  354. av_free(packet);
  355. }
  356. iec61883_cmp_disconnect(dv->raw1394, dv->node, dv->output_port,
  357. raw1394_get_local_id(dv->raw1394),
  358. dv->input_port, dv->channel, dv->bandwidth);
  359. raw1394_destroy_handle(dv->raw1394);
  360. return 0;
  361. }
  362. static const AVOption options[] = {
  363. { "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" },
  364. { "auto", "auto detect DV/HDV", 0, AV_OPT_TYPE_CONST, {.i64 = IEC61883_AUTO}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "dvtype" },
  365. { "dv", "force device being treated as DV device", 0, AV_OPT_TYPE_CONST, {.i64 = IEC61883_DV}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "dvtype" },
  366. { "hdv" , "force device being treated as HDV device", 0, AV_OPT_TYPE_CONST, {.i64 = IEC61883_HDV}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "dvtype" },
  367. { "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 },
  368. { NULL },
  369. };
  370. static const AVClass iec61883_class = {
  371. .class_name = "iec61883 indev",
  372. .item_name = av_default_item_name,
  373. .option = options,
  374. .version = LIBAVUTIL_VERSION_INT,
  375. };
  376. AVInputFormat ff_iec61883_demuxer = {
  377. .name = "iec61883",
  378. .long_name = NULL_IF_CONFIG_SMALL("libiec61883 (new DV1394) A/V input device"),
  379. .priv_data_size = sizeof(struct iec61883_data),
  380. .read_header = iec61883_read_header,
  381. .read_packet = iec61883_read_packet,
  382. .read_close = iec61883_close,
  383. .flags = AVFMT_NOFILE,
  384. .priv_class = &iec61883_class,
  385. };