libdc1394.c 10 KB

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