You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
9.8KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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. 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. 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. };
  85. static inline int dc1394_read_common(AVFormatContext *c,
  86. struct dc1394_frame_format **select_fmt, struct dc1394_frame_rate **select_fps)
  87. {
  88. dc1394_data* dc1394 = c->priv_data;
  89. AVStream* vst;
  90. struct dc1394_frame_format *fmt;
  91. struct dc1394_frame_rate *fps;
  92. enum AVPixelFormat pix_fmt;
  93. int width, height;
  94. AVRational framerate;
  95. int ret = 0;
  96. if ((pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == AV_PIX_FMT_NONE) {
  97. av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format);
  98. ret = AVERROR(EINVAL);
  99. goto out;
  100. }
  101. if ((ret = av_parse_video_size(&width, &height, dc1394->video_size)) < 0) {
  102. av_log(c, AV_LOG_ERROR, "Could not parse video size '%s'.\n", dc1394->video_size);
  103. goto out;
  104. }
  105. if ((ret = av_parse_video_rate(&framerate, dc1394->framerate)) < 0) {
  106. av_log(c, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", dc1394->framerate);
  107. goto out;
  108. }
  109. dc1394->frame_rate = av_rescale(1000, framerate.num, framerate.den);
  110. for (fmt = dc1394_frame_formats; fmt->width; fmt++)
  111. if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
  112. break;
  113. for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
  114. if (fps->frame_rate == dc1394->frame_rate)
  115. break;
  116. if (!fps->frame_rate || !fmt->width) {
  117. 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),
  118. width, height, dc1394->frame_rate);
  119. ret = AVERROR(EINVAL);
  120. goto out;
  121. }
  122. /* create a video stream */
  123. vst = avformat_new_stream(c, NULL);
  124. if (!vst) {
  125. ret = AVERROR(ENOMEM);
  126. goto out;
  127. }
  128. avpriv_set_pts_info(vst, 64, 1, 1000);
  129. vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  130. vst->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  131. vst->codecpar->width = fmt->width;
  132. vst->codecpar->height = fmt->height;
  133. vst->codecpar->format = fmt->pix_fmt;
  134. vst->avg_frame_rate = framerate;
  135. dc1394->current_frame = 0;
  136. dc1394->stream_index = vst->index;
  137. dc1394->size = av_image_get_buffer_size(fmt->pix_fmt,
  138. fmt->width, fmt->height, 1);
  139. vst->codecpar->bit_rate = av_rescale(dc1394->size * 8,
  140. fps->frame_rate, 1000);
  141. *select_fps = fps;
  142. *select_fmt = fmt;
  143. out:
  144. return ret;
  145. }
  146. static int dc1394_read_header(AVFormatContext *c)
  147. {
  148. dc1394_data* dc1394 = c->priv_data;
  149. dc1394camera_list_t *list;
  150. int res, i;
  151. struct dc1394_frame_format *fmt = NULL;
  152. struct dc1394_frame_rate *fps = NULL;
  153. if (dc1394_read_common(c, &fmt, &fps) != 0)
  154. return -1;
  155. /* Now let us prep the hardware. */
  156. dc1394->d = dc1394_new();
  157. if (dc1394_camera_enumerate(dc1394->d, &list) != DC1394_SUCCESS || !list) {
  158. av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera.\n");
  159. goto out;
  160. }
  161. if (list->num == 0) {
  162. av_log(c, AV_LOG_ERROR, "No cameras found.\n");
  163. dc1394_camera_free_list(list);
  164. goto out;
  165. }
  166. /* FIXME: To select a specific camera I need to search in list its guid */
  167. dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
  168. if (list->num > 1) {
  169. av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
  170. }
  171. /* Freeing list of cameras */
  172. dc1394_camera_free_list (list);
  173. /* Select MAX Speed possible from the cam */
  174. if (dc1394->camera->bmode_capable>0) {
  175. dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
  176. i = DC1394_ISO_SPEED_800;
  177. } else {
  178. i = DC1394_ISO_SPEED_400;
  179. }
  180. for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
  181. res=dc1394_video_set_iso_speed(dc1394->camera, i);
  182. }
  183. if (res != DC1394_SUCCESS) {
  184. av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
  185. goto out_camera;
  186. }
  187. if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
  188. av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
  189. goto out_camera;
  190. }
  191. if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
  192. av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
  193. goto out_camera;
  194. }
  195. if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
  196. av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
  197. goto out_camera;
  198. }
  199. if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
  200. av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
  201. goto out_camera;
  202. }
  203. return 0;
  204. out_camera:
  205. dc1394_capture_stop(dc1394->camera);
  206. dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
  207. dc1394_camera_free (dc1394->camera);
  208. out:
  209. dc1394_free(dc1394->d);
  210. return -1;
  211. }
  212. static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt)
  213. {
  214. struct dc1394_data *dc1394 = c->priv_data;
  215. int res;
  216. /* discard stale frame */
  217. if (dc1394->current_frame++) {
  218. if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
  219. av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
  220. }
  221. res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
  222. if (res == DC1394_SUCCESS) {
  223. pkt->data = (uint8_t *)dc1394->frame->image;
  224. pkt->size = dc1394->frame->image_bytes;
  225. pkt->pts = dc1394->current_frame * 1000000 / dc1394->frame_rate;
  226. pkt->flags |= AV_PKT_FLAG_KEY;
  227. pkt->stream_index = dc1394->stream_index;
  228. } else {
  229. av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
  230. return AVERROR_INVALIDDATA;
  231. }
  232. return pkt->size;
  233. }
  234. static int dc1394_close(AVFormatContext * context)
  235. {
  236. struct dc1394_data *dc1394 = context->priv_data;
  237. dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
  238. dc1394_capture_stop(dc1394->camera);
  239. dc1394_camera_free(dc1394->camera);
  240. dc1394_free(dc1394->d);
  241. return 0;
  242. }
  243. AVInputFormat ff_libdc1394_demuxer = {
  244. .name = "libdc1394",
  245. .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
  246. .priv_data_size = sizeof(struct dc1394_data),
  247. .read_header = dc1394_read_header,
  248. .read_packet = dc1394_read_packet,
  249. .read_close = dc1394_close,
  250. .flags = AVFMT_NOFILE,
  251. .priv_class = &libdc1394_class,
  252. };