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.

293 lines
9.9KB

  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 (list->num > 1) {
  170. av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
  171. }
  172. /* Freeing list of cameras */
  173. dc1394_camera_free_list (list);
  174. /* Select MAX Speed possible from the cam */
  175. if (dc1394->camera->bmode_capable>0) {
  176. dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
  177. i = DC1394_ISO_SPEED_800;
  178. } else {
  179. i = DC1394_ISO_SPEED_400;
  180. }
  181. for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
  182. res=dc1394_video_set_iso_speed(dc1394->camera, i);
  183. }
  184. if (res != DC1394_SUCCESS) {
  185. av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
  186. goto out_camera;
  187. }
  188. if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
  189. av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
  190. goto out_camera;
  191. }
  192. if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
  193. av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
  194. goto out_camera;
  195. }
  196. if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
  197. av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
  198. goto out_camera;
  199. }
  200. if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
  201. av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
  202. goto out_camera;
  203. }
  204. return 0;
  205. out_camera:
  206. dc1394_capture_stop(dc1394->camera);
  207. dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
  208. dc1394_camera_free (dc1394->camera);
  209. out:
  210. dc1394_free(dc1394->d);
  211. return -1;
  212. }
  213. static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt)
  214. {
  215. struct dc1394_data *dc1394 = c->priv_data;
  216. int res;
  217. /* discard stale frame */
  218. if (dc1394->current_frame++) {
  219. if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
  220. av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
  221. }
  222. res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
  223. if (res == DC1394_SUCCESS) {
  224. pkt->data = (uint8_t *)dc1394->frame->image;
  225. pkt->size = dc1394->frame->image_bytes;
  226. pkt->pts = dc1394->current_frame * 1000000 / dc1394->frame_rate;
  227. pkt->flags |= AV_PKT_FLAG_KEY;
  228. pkt->stream_index = dc1394->stream_index;
  229. } else {
  230. av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
  231. return AVERROR_INVALIDDATA;
  232. }
  233. return pkt->size;
  234. }
  235. static int dc1394_close(AVFormatContext * context)
  236. {
  237. struct dc1394_data *dc1394 = context->priv_data;
  238. dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
  239. dc1394_capture_stop(dc1394->camera);
  240. dc1394_camera_free(dc1394->camera);
  241. dc1394_free(dc1394->d);
  242. return 0;
  243. }
  244. AVInputFormat ff_libdc1394_demuxer = {
  245. .name = "libdc1394",
  246. .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
  247. .priv_data_size = sizeof(struct dc1394_data),
  248. .read_header = dc1394_read_header,
  249. .read_packet = dc1394_read_packet,
  250. .read_close = dc1394_close,
  251. .flags = AVFMT_NOFILE,
  252. .priv_class = &libdc1394_class,
  253. };