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