libdc1394.c 14 KB

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