libdc1394.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 <dc1394/dc1394.h>
  23. #include "libavutil/imgutils.h"
  24. #include "libavutil/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. #include "libavformat/avformat.h"
  31. #include "libavformat/internal.h"
  32. typedef struct dc1394_data {
  33. AVClass *class;
  34. dc1394_t *d;
  35. dc1394camera_t *camera;
  36. dc1394video_frame_t *frame;
  37. int current_frame;
  38. int frame_rate; /**< frames per 1000 seconds (fps * 1000) */
  39. char *video_size; /**< String describing video size, set by a private option. */
  40. char *pixel_format; /**< Set by a private option. */
  41. char *framerate; /**< Set by a private option. */
  42. int size;
  43. int stream_index;
  44. } dc1394_data;
  45. static const struct dc1394_frame_format {
  46. int width;
  47. int height;
  48. enum AVPixelFormat pix_fmt;
  49. int frame_size_id;
  50. } dc1394_frame_formats[] = {
  51. { 320, 240, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_320x240_YUV422 },
  52. { 640, 480, AV_PIX_FMT_GRAY8, DC1394_VIDEO_MODE_640x480_MONO8 },
  53. { 640, 480, AV_PIX_FMT_UYYVYY411, DC1394_VIDEO_MODE_640x480_YUV411 },
  54. { 640, 480, AV_PIX_FMT_UYVY422, DC1394_VIDEO_MODE_640x480_YUV422 },
  55. { 0, 0, 0, 0 } /* gotta be the last one */
  56. };
  57. static const struct dc1394_frame_rate {
  58. int frame_rate;
  59. int frame_rate_id;
  60. } dc1394_frame_rates[] = {
  61. { 1875, DC1394_FRAMERATE_1_875 },
  62. { 3750, DC1394_FRAMERATE_3_75 },
  63. { 7500, DC1394_FRAMERATE_7_5 },
  64. { 15000, DC1394_FRAMERATE_15 },
  65. { 30000, DC1394_FRAMERATE_30 },
  66. { 60000, DC1394_FRAMERATE_60 },
  67. {120000, DC1394_FRAMERATE_120 },
  68. {240000, DC1394_FRAMERATE_240 },
  69. { 0, 0 } /* gotta be the last one */
  70. };
  71. #define OFFSET(x) offsetof(dc1394_data, x)
  72. #define DEC AV_OPT_FLAG_DECODING_PARAM
  73. static const AVOption options[] = {
  74. { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = "qvga"}, 0, 0, DEC },
  75. { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "uyvy422"}, 0, 0, DEC },
  76. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
  77. { NULL },
  78. };
  79. static const AVClass libdc1394_class = {
  80. .class_name = "libdc1394 indev",
  81. .item_name = av_default_item_name,
  82. .option = options,
  83. .version = LIBAVUTIL_VERSION_INT,
  84. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  85. };
  86. static inline int dc1394_read_common(AVFormatContext *c,
  87. const struct dc1394_frame_format **select_fmt, const struct dc1394_frame_rate **select_fps)
  88. {
  89. dc1394_data* dc1394 = c->priv_data;
  90. AVStream* vst;
  91. const struct dc1394_frame_format *fmt;
  92. const struct dc1394_frame_rate *fps;
  93. enum AVPixelFormat pix_fmt;
  94. int width, height;
  95. AVRational framerate;
  96. int ret = 0;
  97. if ((pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == AV_PIX_FMT_NONE) {
  98. av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format);
  99. ret = AVERROR(EINVAL);
  100. goto out;
  101. }
  102. if ((ret = av_parse_video_size(&width, &height, dc1394->video_size)) < 0) {
  103. av_log(c, AV_LOG_ERROR, "Could not parse video size '%s'.\n", dc1394->video_size);
  104. goto out;
  105. }
  106. if ((ret = av_parse_video_rate(&framerate, dc1394->framerate)) < 0) {
  107. av_log(c, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", dc1394->framerate);
  108. goto out;
  109. }
  110. dc1394->frame_rate = av_rescale(1000, framerate.num, framerate.den);
  111. for (fmt = dc1394_frame_formats; fmt->width; fmt++)
  112. if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
  113. break;
  114. for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
  115. if (fps->frame_rate == dc1394->frame_rate)
  116. break;
  117. if (!fps->frame_rate || !fmt->width) {
  118. 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),
  119. width, height, dc1394->frame_rate);
  120. ret = AVERROR(EINVAL);
  121. goto out;
  122. }
  123. /* create a video stream */
  124. vst = avformat_new_stream(c, NULL);
  125. if (!vst) {
  126. ret = AVERROR(ENOMEM);
  127. goto out;
  128. }
  129. avpriv_set_pts_info(vst, 64, 1, 1000);
  130. vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  131. vst->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  132. vst->codecpar->width = fmt->width;
  133. vst->codecpar->height = fmt->height;
  134. vst->codecpar->format = fmt->pix_fmt;
  135. vst->avg_frame_rate = framerate;
  136. dc1394->current_frame = 0;
  137. dc1394->stream_index = vst->index;
  138. dc1394->size = av_image_get_buffer_size(fmt->pix_fmt,
  139. fmt->width, fmt->height, 1);
  140. vst->codecpar->bit_rate = av_rescale(dc1394->size * 8,
  141. fps->frame_rate, 1000);
  142. *select_fps = fps;
  143. *select_fmt = fmt;
  144. out:
  145. return ret;
  146. }
  147. static int dc1394_read_header(AVFormatContext *c)
  148. {
  149. dc1394_data* dc1394 = c->priv_data;
  150. dc1394camera_list_t *list;
  151. int res, i;
  152. const struct dc1394_frame_format *fmt = NULL;
  153. const struct dc1394_frame_rate *fps = NULL;
  154. if (dc1394_read_common(c, &fmt, &fps) != 0)
  155. return -1;
  156. /* Now let us prep the hardware. */
  157. dc1394->d = dc1394_new();
  158. if (dc1394_camera_enumerate(dc1394->d, &list) != DC1394_SUCCESS || !list) {
  159. av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera.\n");
  160. goto out;
  161. }
  162. if (list->num == 0) {
  163. av_log(c, AV_LOG_ERROR, "No cameras found.\n");
  164. dc1394_camera_free_list(list);
  165. goto out;
  166. }
  167. /* FIXME: To select a specific camera I need to search in list its guid */
  168. dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
  169. if (!dc1394->camera) {
  170. av_log(c, AV_LOG_ERROR, "Unable to open camera with guid 0x%"PRIx64"\n",
  171. list->ids[0].guid);
  172. dc1394_camera_free_list(list);
  173. goto out;
  174. }
  175. if (list->num > 1) {
  176. av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
  177. }
  178. /* Freeing list of cameras */
  179. dc1394_camera_free_list (list);
  180. /* Select MAX Speed possible from the cam */
  181. if (dc1394->camera->bmode_capable>0) {
  182. dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
  183. i = DC1394_ISO_SPEED_800;
  184. } else {
  185. i = DC1394_ISO_SPEED_400;
  186. }
  187. for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
  188. res=dc1394_video_set_iso_speed(dc1394->camera, i);
  189. }
  190. if (res != DC1394_SUCCESS) {
  191. av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
  192. goto out_camera;
  193. }
  194. if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
  195. av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
  196. goto out_camera;
  197. }
  198. if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
  199. av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
  200. goto out_camera;
  201. }
  202. if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
  203. av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
  204. goto out_camera;
  205. }
  206. if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
  207. av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
  208. goto out_camera;
  209. }
  210. return 0;
  211. out_camera:
  212. dc1394_capture_stop(dc1394->camera);
  213. dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
  214. dc1394_camera_free (dc1394->camera);
  215. out:
  216. dc1394_free(dc1394->d);
  217. return -1;
  218. }
  219. static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt)
  220. {
  221. struct dc1394_data *dc1394 = c->priv_data;
  222. int res;
  223. /* discard stale frame */
  224. if (dc1394->current_frame++) {
  225. if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
  226. av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
  227. }
  228. res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
  229. if (res == DC1394_SUCCESS) {
  230. pkt->data = (uint8_t *)dc1394->frame->image;
  231. pkt->size = dc1394->frame->image_bytes;
  232. pkt->pts = dc1394->current_frame * 1000000 / dc1394->frame_rate;
  233. pkt->flags |= AV_PKT_FLAG_KEY;
  234. pkt->stream_index = dc1394->stream_index;
  235. } else {
  236. av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
  237. return AVERROR_INVALIDDATA;
  238. }
  239. return pkt->size;
  240. }
  241. static int dc1394_close(AVFormatContext * context)
  242. {
  243. struct dc1394_data *dc1394 = context->priv_data;
  244. dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
  245. dc1394_capture_stop(dc1394->camera);
  246. dc1394_camera_free(dc1394->camera);
  247. dc1394_free(dc1394->d);
  248. return 0;
  249. }
  250. const AVInputFormat ff_libdc1394_demuxer = {
  251. .name = "libdc1394",
  252. .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
  253. .priv_data_size = sizeof(struct dc1394_data),
  254. .read_header = dc1394_read_header,
  255. .read_packet = dc1394_read_packet,
  256. .read_close = dc1394_close,
  257. .flags = AVFMT_NOFILE,
  258. .priv_class = &libdc1394_class,
  259. };