libdc1394.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. #if HAVE_LIBDC1394_2
  25. #include <dc1394/dc1394.h>
  26. #elif HAVE_LIBDC1394_1
  27. #include <libraw1394/raw1394.h>
  28. #include <libdc1394/dc1394_control.h>
  29. #define DC1394_VIDEO_MODE_320x240_YUV422 MODE_320x240_YUV422
  30. #define DC1394_VIDEO_MODE_640x480_YUV411 MODE_640x480_YUV411
  31. #define DC1394_VIDEO_MODE_640x480_YUV422 MODE_640x480_YUV422
  32. #define DC1394_FRAMERATE_1_875 FRAMERATE_1_875
  33. #define DC1394_FRAMERATE_3_75 FRAMERATE_3_75
  34. #define DC1394_FRAMERATE_7_5 FRAMERATE_7_5
  35. #define DC1394_FRAMERATE_15 FRAMERATE_15
  36. #define DC1394_FRAMERATE_30 FRAMERATE_30
  37. #define DC1394_FRAMERATE_60 FRAMERATE_60
  38. #define DC1394_FRAMERATE_120 FRAMERATE_120
  39. #define DC1394_FRAMERATE_240 FRAMERATE_240
  40. #endif
  41. #undef free
  42. typedef struct dc1394_data {
  43. #if HAVE_LIBDC1394_1
  44. raw1394handle_t handle;
  45. dc1394_cameracapture camera;
  46. #elif HAVE_LIBDC1394_2
  47. dc1394_t *d;
  48. dc1394camera_t *camera;
  49. dc1394video_frame_t *frame;
  50. #endif
  51. int current_frame;
  52. int fps;
  53. AVPacket packet;
  54. } dc1394_data;
  55. struct dc1394_frame_format {
  56. int width;
  57. int height;
  58. enum PixelFormat pix_fmt;
  59. int frame_size_id;
  60. } dc1394_frame_formats[] = {
  61. { 320, 240, PIX_FMT_UYVY422, DC1394_VIDEO_MODE_320x240_YUV422 },
  62. { 640, 480, PIX_FMT_UYYVYY411, DC1394_VIDEO_MODE_640x480_YUV411 },
  63. { 640, 480, PIX_FMT_UYVY422, DC1394_VIDEO_MODE_640x480_YUV422 },
  64. { 0, 0, 0, 0 } /* gotta be the last one */
  65. };
  66. struct dc1394_frame_rate {
  67. int frame_rate;
  68. int frame_rate_id;
  69. } dc1394_frame_rates[] = {
  70. { 1875, DC1394_FRAMERATE_1_875 },
  71. { 3750, DC1394_FRAMERATE_3_75 },
  72. { 7500, DC1394_FRAMERATE_7_5 },
  73. { 15000, DC1394_FRAMERATE_15 },
  74. { 30000, DC1394_FRAMERATE_30 },
  75. { 60000, DC1394_FRAMERATE_60 },
  76. {120000, DC1394_FRAMERATE_120 },
  77. {240000, DC1394_FRAMERATE_240 },
  78. { 0, 0 } /* gotta be the last one */
  79. };
  80. static inline int dc1394_read_common(AVFormatContext *c, AVFormatParameters *ap,
  81. struct dc1394_frame_format **select_fmt, struct dc1394_frame_rate **select_fps)
  82. {
  83. dc1394_data* dc1394 = c->priv_data;
  84. AVStream* vst;
  85. struct dc1394_frame_format *fmt;
  86. struct dc1394_frame_rate *fps;
  87. enum PixelFormat pix_fmt = ap->pix_fmt == PIX_FMT_NONE ? PIX_FMT_UYVY422 : ap->pix_fmt; /* defaults */
  88. int width = !ap->width ? 320 : ap->width;
  89. int height = !ap->height ? 240 : ap->height;
  90. int frame_rate = !ap->time_base.num ? 30000 : av_rescale(1000, ap->time_base.den, ap->time_base.num);
  91. for (fmt = dc1394_frame_formats; fmt->width; fmt++)
  92. if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
  93. break;
  94. for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
  95. if (fps->frame_rate == frame_rate)
  96. break;
  97. if (!fps->frame_rate || !fmt->width) {
  98. av_log(c, AV_LOG_ERROR, "Can't find matching camera format for %s, %dx%d@%d:1000fps\n", avcodec_get_pix_fmt_name(pix_fmt),
  99. width, height, frame_rate);
  100. goto out;
  101. }
  102. /* create a video stream */
  103. vst = av_new_stream(c, 0);
  104. if (!vst)
  105. goto out;
  106. av_set_pts_info(vst, 64, 1, 1000);
  107. vst->codec->codec_type = CODEC_TYPE_VIDEO;
  108. vst->codec->codec_id = CODEC_ID_RAWVIDEO;
  109. vst->codec->time_base.den = fps->frame_rate;
  110. vst->codec->time_base.num = 1000;
  111. vst->codec->width = fmt->width;
  112. vst->codec->height = fmt->height;
  113. vst->codec->pix_fmt = fmt->pix_fmt;
  114. /* packet init */
  115. av_init_packet(&dc1394->packet);
  116. dc1394->packet.size = avpicture_get_size(fmt->pix_fmt, fmt->width, fmt->height);
  117. dc1394->packet.stream_index = vst->index;
  118. dc1394->packet.flags |= PKT_FLAG_KEY;
  119. dc1394->current_frame = 0;
  120. dc1394->fps = fps->frame_rate;
  121. vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000);
  122. *select_fps = fps;
  123. *select_fmt = fmt;
  124. return 0;
  125. out:
  126. return -1;
  127. }
  128. #if HAVE_LIBDC1394_1
  129. static int dc1394_v1_read_header(AVFormatContext *c, AVFormatParameters * ap)
  130. {
  131. dc1394_data* dc1394 = c->priv_data;
  132. AVStream* vst;
  133. nodeid_t* camera_nodes;
  134. int res;
  135. struct dc1394_frame_format *fmt = NULL;
  136. struct dc1394_frame_rate *fps = NULL;
  137. if (dc1394_read_common(c,ap,&fmt,&fps) != 0)
  138. return -1;
  139. /* Now let us prep the hardware. */
  140. dc1394->handle = dc1394_create_handle(0); /* FIXME: gotta have ap->port */
  141. if (!dc1394->handle) {
  142. av_log(c, AV_LOG_ERROR, "Can't acquire dc1394 handle on port %d\n", 0 /* ap->port */);
  143. goto out;
  144. }
  145. camera_nodes = dc1394_get_camera_nodes(dc1394->handle, &res, 1);
  146. if (!camera_nodes || camera_nodes[ap->channel] == DC1394_NO_CAMERA) {
  147. av_log(c, AV_LOG_ERROR, "There's no IIDC camera on the channel %d\n", ap->channel);
  148. goto out_handle;
  149. }
  150. res = dc1394_dma_setup_capture(dc1394->handle, camera_nodes[ap->channel],
  151. 0,
  152. FORMAT_VGA_NONCOMPRESSED,
  153. fmt->frame_size_id,
  154. SPEED_400,
  155. fps->frame_rate_id, 8, 1,
  156. c->filename,
  157. &dc1394->camera);
  158. dc1394_free_camera_nodes(camera_nodes);
  159. if (res != DC1394_SUCCESS) {
  160. av_log(c, AV_LOG_ERROR, "Can't prepare camera for the DMA capture\n");
  161. goto out_handle;
  162. }
  163. res = dc1394_start_iso_transmission(dc1394->handle, dc1394->camera.node);
  164. if (res != DC1394_SUCCESS) {
  165. av_log(c, AV_LOG_ERROR, "Can't start isochronous transmission\n");
  166. goto out_handle_dma;
  167. }
  168. return 0;
  169. out_handle_dma:
  170. dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
  171. dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
  172. out_handle:
  173. dc1394_destroy_handle(dc1394->handle);
  174. out:
  175. return -1;
  176. }
  177. static int dc1394_v1_read_packet(AVFormatContext *c, AVPacket *pkt)
  178. {
  179. struct dc1394_data *dc1394 = c->priv_data;
  180. int res;
  181. /* discard stale frame */
  182. if (dc1394->current_frame++) {
  183. if (dc1394_dma_done_with_buffer(&dc1394->camera) != DC1394_SUCCESS)
  184. av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
  185. }
  186. res = dc1394_dma_single_capture(&dc1394->camera);
  187. if (res == DC1394_SUCCESS) {
  188. dc1394->packet.data = (uint8_t *)(dc1394->camera.capture_buffer);
  189. dc1394->packet.pts = (dc1394->current_frame * 1000000) / dc1394->fps;
  190. res = dc1394->packet.size;
  191. } else {
  192. av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
  193. dc1394->packet.data = NULL;
  194. res = -1;
  195. }
  196. *pkt = dc1394->packet;
  197. return res;
  198. }
  199. static int dc1394_v1_close(AVFormatContext * context)
  200. {
  201. struct dc1394_data *dc1394 = context->priv_data;
  202. dc1394_stop_iso_transmission(dc1394->handle, dc1394->camera.node);
  203. dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
  204. dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
  205. dc1394_destroy_handle(dc1394->handle);
  206. return 0;
  207. }
  208. #elif HAVE_LIBDC1394_2
  209. static int dc1394_v2_read_header(AVFormatContext *c, AVFormatParameters * ap)
  210. {
  211. dc1394_data* dc1394 = c->priv_data;
  212. dc1394camera_list_t *list;
  213. int res, i;
  214. struct dc1394_frame_format *fmt = NULL;
  215. struct dc1394_frame_rate *fps = NULL;
  216. if (dc1394_read_common(c,ap,&fmt,&fps) != 0)
  217. return -1;
  218. /* Now let us prep the hardware. */
  219. dc1394->d = dc1394_new();
  220. dc1394_camera_enumerate (dc1394->d, &list);
  221. if ( !list || list->num == 0) {
  222. av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n");
  223. goto out;
  224. }
  225. /* FIXME: To select a specific camera I need to search in list its guid */
  226. dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
  227. if (list->num > 1) {
  228. av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
  229. }
  230. /* Freeing list of cameras */
  231. dc1394_camera_free_list (list);
  232. /* Select MAX Speed possible from the cam */
  233. if (dc1394->camera->bmode_capable>0) {
  234. dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
  235. i = DC1394_ISO_SPEED_800;
  236. } else {
  237. i = DC1394_ISO_SPEED_400;
  238. }
  239. for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
  240. res=dc1394_video_set_iso_speed(dc1394->camera, i);
  241. }
  242. if (res != DC1394_SUCCESS) {
  243. av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
  244. goto out_camera;
  245. }
  246. if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
  247. av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
  248. goto out_camera;
  249. }
  250. if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
  251. av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
  252. goto out_camera;
  253. }
  254. if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
  255. av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
  256. goto out_camera;
  257. }
  258. if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
  259. av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
  260. goto out_camera;
  261. }
  262. return 0;
  263. out_camera:
  264. dc1394_capture_stop(dc1394->camera);
  265. dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
  266. dc1394_camera_free (dc1394->camera);
  267. out:
  268. dc1394_free(dc1394->d);
  269. return -1;
  270. }
  271. static int dc1394_v2_read_packet(AVFormatContext *c, AVPacket *pkt)
  272. {
  273. struct dc1394_data *dc1394 = c->priv_data;
  274. int res;
  275. /* discard stale frame */
  276. if (dc1394->current_frame++) {
  277. if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
  278. av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
  279. }
  280. res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
  281. if (res == DC1394_SUCCESS) {
  282. dc1394->packet.data = (uint8_t *)(dc1394->frame->image);
  283. dc1394->packet.pts = (dc1394->current_frame * 1000000) / (dc1394->fps);
  284. res = dc1394->frame->image_bytes;
  285. } else {
  286. av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
  287. dc1394->packet.data = NULL;
  288. res = -1;
  289. }
  290. *pkt = dc1394->packet;
  291. return res;
  292. }
  293. static int dc1394_v2_close(AVFormatContext * context)
  294. {
  295. struct dc1394_data *dc1394 = context->priv_data;
  296. dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
  297. dc1394_capture_stop(dc1394->camera);
  298. dc1394_camera_free(dc1394->camera);
  299. dc1394_free(dc1394->d);
  300. return 0;
  301. }
  302. AVInputFormat libdc1394_demuxer = {
  303. .name = "libdc1394",
  304. .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
  305. .priv_data_size = sizeof(struct dc1394_data),
  306. .read_header = dc1394_v2_read_header,
  307. .read_packet = dc1394_v2_read_packet,
  308. .read_close = dc1394_v2_close,
  309. .flags = AVFMT_NOFILE
  310. };
  311. #endif
  312. #if HAVE_LIBDC1394_1
  313. AVInputFormat libdc1394_demuxer = {
  314. .name = "libdc1394",
  315. .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.1 A/V grab"),
  316. .priv_data_size = sizeof(struct dc1394_data),
  317. .read_header = dc1394_v1_read_header,
  318. .read_packet = dc1394_v1_read_packet,
  319. .read_close = dc1394_v1_close,
  320. .flags = AVFMT_NOFILE
  321. };
  322. #endif