libdc1394.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * IIDC1394 grab interface (uses libdc1394 and libraw1394)
  3. * Copyright (c) 2004 Roman Shaposhnik
  4. * Copyright (c) 2008 Alessandro Sappia
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "config.h"
  23. #include "libavformat/avformat.h"
  24. #include "libavformat/internal.h"
  25. #include "libavutil/log.h"
  26. #include "libavutil/mathematics.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/parseutils.h"
  29. #include "libavutil/pixdesc.h"
  30. #if HAVE_LIBDC1394_2
  31. #include <dc1394/dc1394.h>
  32. #elif HAVE_LIBDC1394_1
  33. #include <libraw1394/raw1394.h>
  34. #include <libdc1394/dc1394_control.h>
  35. #define DC1394_VIDEO_MODE_320x240_YUV422 MODE_320x240_YUV422
  36. #define DC1394_VIDEO_MODE_640x480_YUV411 MODE_640x480_YUV411
  37. #define DC1394_VIDEO_MODE_640x480_YUV422 MODE_640x480_YUV422
  38. #define DC1394_FRAMERATE_1_875 FRAMERATE_1_875
  39. #define DC1394_FRAMERATE_3_75 FRAMERATE_3_75
  40. #define DC1394_FRAMERATE_7_5 FRAMERATE_7_5
  41. #define DC1394_FRAMERATE_15 FRAMERATE_15
  42. #define DC1394_FRAMERATE_30 FRAMERATE_30
  43. #define DC1394_FRAMERATE_60 FRAMERATE_60
  44. #define DC1394_FRAMERATE_120 FRAMERATE_120
  45. #define DC1394_FRAMERATE_240 FRAMERATE_240
  46. #endif
  47. #undef free
  48. typedef struct dc1394_data {
  49. AVClass *class;
  50. #if HAVE_LIBDC1394_1
  51. raw1394handle_t handle;
  52. dc1394_cameracapture camera;
  53. int channel;
  54. #elif HAVE_LIBDC1394_2
  55. dc1394_t *d;
  56. dc1394camera_t *camera;
  57. dc1394video_frame_t *frame;
  58. #endif
  59. int current_frame;
  60. int frame_rate; /**< frames per 1000 seconds (fps * 1000) */
  61. char *video_size; /**< String describing video size, set by a private option. */
  62. char *pixel_format; /**< Set by a private option. */
  63. char *framerate; /**< Set by a private option. */
  64. AVPacket packet;
  65. } dc1394_data;
  66. struct dc1394_frame_format {
  67. int width;
  68. int height;
  69. enum PixelFormat pix_fmt;
  70. int frame_size_id;
  71. } dc1394_frame_formats[] = {
  72. { 320, 240, PIX_FMT_UYVY422, DC1394_VIDEO_MODE_320x240_YUV422 },
  73. { 640, 480, PIX_FMT_UYYVYY411, DC1394_VIDEO_MODE_640x480_YUV411 },
  74. { 640, 480, PIX_FMT_UYVY422, DC1394_VIDEO_MODE_640x480_YUV422 },
  75. { 0, 0, 0, 0 } /* gotta be the last one */
  76. };
  77. struct dc1394_frame_rate {
  78. int frame_rate;
  79. int frame_rate_id;
  80. } dc1394_frame_rates[] = {
  81. { 1875, DC1394_FRAMERATE_1_875 },
  82. { 3750, DC1394_FRAMERATE_3_75 },
  83. { 7500, DC1394_FRAMERATE_7_5 },
  84. { 15000, DC1394_FRAMERATE_15 },
  85. { 30000, DC1394_FRAMERATE_30 },
  86. { 60000, DC1394_FRAMERATE_60 },
  87. {120000, DC1394_FRAMERATE_120 },
  88. {240000, DC1394_FRAMERATE_240 },
  89. { 0, 0 } /* gotta be the last one */
  90. };
  91. #define OFFSET(x) offsetof(dc1394_data, x)
  92. #define DEC AV_OPT_FLAG_DECODING_PARAM
  93. static const AVOption options[] = {
  94. #if HAVE_LIBDC1394_1
  95. { "channel", "", offsetof(dc1394_data, channel), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  96. #endif
  97. { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "qvga"}, 0, 0, DEC },
  98. { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "uyvy422"}, 0, 0, DEC },
  99. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
  100. { NULL },
  101. };
  102. static const AVClass libdc1394_class = {
  103. .class_name = "libdc1394 indev",
  104. .item_name = av_default_item_name,
  105. .option = options,
  106. .version = LIBAVUTIL_VERSION_INT,
  107. };
  108. static inline int dc1394_read_common(AVFormatContext *c,
  109. struct dc1394_frame_format **select_fmt, struct dc1394_frame_rate **select_fps)
  110. {
  111. dc1394_data* dc1394 = c->priv_data;
  112. AVStream* vst;
  113. struct dc1394_frame_format *fmt;
  114. struct dc1394_frame_rate *fps;
  115. enum PixelFormat pix_fmt;
  116. int width, height;
  117. AVRational framerate;
  118. int ret = 0;
  119. if ((pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == PIX_FMT_NONE) {
  120. av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format);
  121. ret = AVERROR(EINVAL);
  122. goto out;
  123. }
  124. if ((ret = av_parse_video_size(&width, &height, dc1394->video_size)) < 0) {
  125. av_log(c, AV_LOG_ERROR, "Could not parse video size '%s'.\n", dc1394->video_size);
  126. goto out;
  127. }
  128. if ((ret = av_parse_video_rate(&framerate, dc1394->framerate)) < 0) {
  129. av_log(c, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", dc1394->framerate);
  130. goto out;
  131. }
  132. dc1394->frame_rate = av_rescale(1000, framerate.num, framerate.den);
  133. for (fmt = dc1394_frame_formats; fmt->width; fmt++)
  134. if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
  135. break;
  136. for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
  137. if (fps->frame_rate == dc1394->frame_rate)
  138. break;
  139. if (!fps->frame_rate || !fmt->width) {
  140. av_log(c, AV_LOG_ERROR, "Can't find matching camera format for %s, %dx%d@%d:1000fps\n", av_get_pix_fmt_name(pix_fmt),
  141. width, height, dc1394->frame_rate);
  142. ret = AVERROR(EINVAL);
  143. goto out;
  144. }
  145. /* create a video stream */
  146. vst = avformat_new_stream(c, NULL);
  147. if (!vst) {
  148. ret = AVERROR(ENOMEM);
  149. goto out;
  150. }
  151. avpriv_set_pts_info(vst, 64, 1, 1000);
  152. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  153. vst->codec->codec_id = CODEC_ID_RAWVIDEO;
  154. vst->codec->time_base.den = framerate.num;
  155. vst->codec->time_base.num = framerate.den;
  156. vst->codec->width = fmt->width;
  157. vst->codec->height = fmt->height;
  158. vst->codec->pix_fmt = fmt->pix_fmt;
  159. /* packet init */
  160. av_init_packet(&dc1394->packet);
  161. dc1394->packet.size = avpicture_get_size(fmt->pix_fmt, fmt->width, fmt->height);
  162. dc1394->packet.stream_index = vst->index;
  163. dc1394->packet.flags |= AV_PKT_FLAG_KEY;
  164. dc1394->current_frame = 0;
  165. vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000);
  166. *select_fps = fps;
  167. *select_fmt = fmt;
  168. out:
  169. return ret;
  170. }
  171. #if HAVE_LIBDC1394_1
  172. static int dc1394_v1_read_header(AVFormatContext *c)
  173. {
  174. dc1394_data* dc1394 = c->priv_data;
  175. AVStream* vst;
  176. nodeid_t* camera_nodes;
  177. int res;
  178. struct dc1394_frame_format *fmt = NULL;
  179. struct dc1394_frame_rate *fps = NULL;
  180. if (dc1394_read_common(c, &fmt, &fps) != 0)
  181. return -1;
  182. /* Now let us prep the hardware. */
  183. dc1394->handle = dc1394_create_handle(0); /* FIXME: gotta have ap->port */
  184. if (!dc1394->handle) {
  185. av_log(c, AV_LOG_ERROR, "Can't acquire dc1394 handle on port %d\n", 0 /* ap->port */);
  186. goto out;
  187. }
  188. camera_nodes = dc1394_get_camera_nodes(dc1394->handle, &res, 1);
  189. if (!camera_nodes || camera_nodes[dc1394->channel] == DC1394_NO_CAMERA) {
  190. av_log(c, AV_LOG_ERROR, "There's no IIDC camera on the channel %d\n", dc1394->channel);
  191. goto out_handle;
  192. }
  193. res = dc1394_dma_setup_capture(dc1394->handle, camera_nodes[dc1394->channel],
  194. 0,
  195. FORMAT_VGA_NONCOMPRESSED,
  196. fmt->frame_size_id,
  197. SPEED_400,
  198. fps->frame_rate_id, 8, 1,
  199. c->filename,
  200. &dc1394->camera);
  201. dc1394_free_camera_nodes(camera_nodes);
  202. if (res != DC1394_SUCCESS) {
  203. av_log(c, AV_LOG_ERROR, "Can't prepare camera for the DMA capture\n");
  204. goto out_handle;
  205. }
  206. res = dc1394_start_iso_transmission(dc1394->handle, dc1394->camera.node);
  207. if (res != DC1394_SUCCESS) {
  208. av_log(c, AV_LOG_ERROR, "Can't start isochronous transmission\n");
  209. goto out_handle_dma;
  210. }
  211. return 0;
  212. out_handle_dma:
  213. dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
  214. dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
  215. out_handle:
  216. dc1394_destroy_handle(dc1394->handle);
  217. out:
  218. return -1;
  219. }
  220. static int dc1394_v1_read_packet(AVFormatContext *c, AVPacket *pkt)
  221. {
  222. struct dc1394_data *dc1394 = c->priv_data;
  223. int res;
  224. /* discard stale frame */
  225. if (dc1394->current_frame++) {
  226. if (dc1394_dma_done_with_buffer(&dc1394->camera) != DC1394_SUCCESS)
  227. av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
  228. }
  229. res = dc1394_dma_single_capture(&dc1394->camera);
  230. if (res == DC1394_SUCCESS) {
  231. dc1394->packet.data = (uint8_t *)(dc1394->camera.capture_buffer);
  232. dc1394->packet.pts = (dc1394->current_frame * 1000000) / dc1394->frame_rate;
  233. res = dc1394->packet.size;
  234. } else {
  235. av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
  236. dc1394->packet.data = NULL;
  237. res = -1;
  238. }
  239. *pkt = dc1394->packet;
  240. return res;
  241. }
  242. static int dc1394_v1_close(AVFormatContext * context)
  243. {
  244. struct dc1394_data *dc1394 = context->priv_data;
  245. dc1394_stop_iso_transmission(dc1394->handle, dc1394->camera.node);
  246. dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
  247. dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
  248. dc1394_destroy_handle(dc1394->handle);
  249. return 0;
  250. }
  251. #elif HAVE_LIBDC1394_2
  252. static int dc1394_v2_read_header(AVFormatContext *c)
  253. {
  254. dc1394_data* dc1394 = c->priv_data;
  255. dc1394camera_list_t *list;
  256. int res, i;
  257. struct dc1394_frame_format *fmt = NULL;
  258. struct dc1394_frame_rate *fps = NULL;
  259. if (dc1394_read_common(c, &fmt, &fps) != 0)
  260. return -1;
  261. /* Now let us prep the hardware. */
  262. dc1394->d = dc1394_new();
  263. dc1394_camera_enumerate (dc1394->d, &list);
  264. if ( !list || list->num == 0) {
  265. av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n");
  266. goto out;
  267. }
  268. /* FIXME: To select a specific camera I need to search in list its guid */
  269. dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
  270. if (list->num > 1) {
  271. av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
  272. }
  273. /* Freeing list of cameras */
  274. dc1394_camera_free_list (list);
  275. /* Select MAX Speed possible from the cam */
  276. if (dc1394->camera->bmode_capable>0) {
  277. dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
  278. i = DC1394_ISO_SPEED_800;
  279. } else {
  280. i = DC1394_ISO_SPEED_400;
  281. }
  282. for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
  283. res=dc1394_video_set_iso_speed(dc1394->camera, i);
  284. }
  285. if (res != DC1394_SUCCESS) {
  286. av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
  287. goto out_camera;
  288. }
  289. if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
  290. av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
  291. goto out_camera;
  292. }
  293. if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
  294. av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
  295. goto out_camera;
  296. }
  297. if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
  298. av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
  299. goto out_camera;
  300. }
  301. if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
  302. av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
  303. goto out_camera;
  304. }
  305. return 0;
  306. out_camera:
  307. dc1394_capture_stop(dc1394->camera);
  308. dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
  309. dc1394_camera_free (dc1394->camera);
  310. out:
  311. dc1394_free(dc1394->d);
  312. return -1;
  313. }
  314. static int dc1394_v2_read_packet(AVFormatContext *c, AVPacket *pkt)
  315. {
  316. struct dc1394_data *dc1394 = c->priv_data;
  317. int res;
  318. /* discard stale frame */
  319. if (dc1394->current_frame++) {
  320. if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
  321. av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
  322. }
  323. res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
  324. if (res == DC1394_SUCCESS) {
  325. dc1394->packet.data = (uint8_t *) dc1394->frame->image;
  326. dc1394->packet.pts = dc1394->current_frame * 1000000 / dc1394->frame_rate;
  327. res = dc1394->frame->image_bytes;
  328. } else {
  329. av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
  330. dc1394->packet.data = NULL;
  331. res = -1;
  332. }
  333. *pkt = dc1394->packet;
  334. return res;
  335. }
  336. static int dc1394_v2_close(AVFormatContext * context)
  337. {
  338. struct dc1394_data *dc1394 = context->priv_data;
  339. dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
  340. dc1394_capture_stop(dc1394->camera);
  341. dc1394_camera_free(dc1394->camera);
  342. dc1394_free(dc1394->d);
  343. return 0;
  344. }
  345. AVInputFormat ff_libdc1394_demuxer = {
  346. .name = "libdc1394",
  347. .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
  348. .priv_data_size = sizeof(struct dc1394_data),
  349. .read_header = dc1394_v2_read_header,
  350. .read_packet = dc1394_v2_read_packet,
  351. .read_close = dc1394_v2_close,
  352. .flags = AVFMT_NOFILE,
  353. .priv_class = &libdc1394_class,
  354. };
  355. #endif
  356. #if HAVE_LIBDC1394_1
  357. AVInputFormat ff_libdc1394_demuxer = {
  358. .name = "libdc1394",
  359. .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.1 A/V grab"),
  360. .priv_data_size = sizeof(struct dc1394_data),
  361. .read_header = dc1394_v1_read_header,
  362. .read_packet = dc1394_v1_read_packet,
  363. .read_close = dc1394_v1_close,
  364. .flags = AVFMT_NOFILE,
  365. .priv_class = &libdc1394_class,
  366. };
  367. #endif