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.

371 lines
13KB

  1. /*
  2. * IIDC1394 grab interface (uses libdc1394 and libraw1394)
  3. * Copyright (c) 2004 Roman Shaposhnik
  4. * Copyright (c) 2008 Alessandro Sappia
  5. * Copyright (c) 2011 Martin Lambers
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "config.h"
  24. #include "libavutil/log.h"
  25. #include "libavutil/opt.h"
  26. #include "avdevice.h"
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "libavutil/parseutils.h"
  30. #include "libavutil/pixdesc.h"
  31. #include <dc1394/dc1394.h>
  32. #undef free
  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 fps;
  40. char *video_size; /**< String describing video size, set by a private option. */
  41. char *pixel_format; /**< Set by a private option. */
  42. AVPacket packet;
  43. } dc1394_data;
  44. /* The list of color codings that we support.
  45. * We assume big endian for the dc1394 16bit modes: libdc1394 never sets the
  46. * flag little_endian in dc1394video_frame_t. */
  47. struct dc1394_color_coding {
  48. int pix_fmt;
  49. int score;
  50. uint32_t coding;
  51. } dc1394_color_codings[] = {
  52. { PIX_FMT_GRAY16BE, 1000, DC1394_COLOR_CODING_MONO16 },
  53. { PIX_FMT_RGB48BE, 1100, DC1394_COLOR_CODING_RGB16 },
  54. { PIX_FMT_GRAY8, 1200, DC1394_COLOR_CODING_MONO8 },
  55. { PIX_FMT_RGB24, 1300, DC1394_COLOR_CODING_RGB8 },
  56. { PIX_FMT_UYYVYY411, 1400, DC1394_COLOR_CODING_YUV411 },
  57. { PIX_FMT_UYVY422, 1500, DC1394_COLOR_CODING_YUV422 },
  58. { PIX_FMT_NONE, 0, 0 } /* gotta be the last one */
  59. };
  60. struct dc1394_frame_rate {
  61. int frame_rate;
  62. int frame_rate_id;
  63. } dc1394_frame_rates[] = {
  64. { 1875, DC1394_FRAMERATE_1_875 },
  65. { 3750, DC1394_FRAMERATE_3_75 },
  66. { 7500, DC1394_FRAMERATE_7_5 },
  67. { 15000, DC1394_FRAMERATE_15 },
  68. { 30000, DC1394_FRAMERATE_30 },
  69. { 60000, DC1394_FRAMERATE_60 },
  70. {120000, DC1394_FRAMERATE_120 },
  71. {240000, DC1394_FRAMERATE_240 },
  72. { 0, 0 } /* gotta be the last one */
  73. };
  74. #define OFFSET(x) offsetof(dc1394_data, x)
  75. #define DEC AV_OPT_FLAG_DECODING_PARAM
  76. static const AVOption options[] = {
  77. { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = "qvga"}, 0, 0, DEC },
  78. { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = "uyvy422"}, 0, 0, DEC },
  79. { NULL },
  80. };
  81. static const AVClass libdc1394_class = {
  82. .class_name = "libdc1394 indev",
  83. .item_name = av_default_item_name,
  84. .option = options,
  85. .version = LIBAVUTIL_VERSION_INT,
  86. };
  87. static int dc1394_read_header(AVFormatContext *c, AVFormatParameters * ap)
  88. {
  89. dc1394_data* dc1394 = c->priv_data;
  90. AVStream *vst;
  91. const struct dc1394_color_coding *cc;
  92. const struct dc1394_frame_rate *fr;
  93. dc1394camera_list_t *list;
  94. dc1394video_modes_t video_modes;
  95. dc1394video_mode_t video_mode;
  96. dc1394framerates_t frame_rates;
  97. dc1394framerate_t frame_rate;
  98. uint32_t dc1394_width, dc1394_height, dc1394_color_coding;
  99. int rate, best_rate;
  100. int score, max_score;
  101. int final_width, final_height, final_pix_fmt, final_frame_rate;
  102. int res, i, j;
  103. int ret=-1;
  104. /* Now let us prep the hardware. */
  105. dc1394->d = dc1394_new();
  106. dc1394_camera_enumerate (dc1394->d, &list);
  107. if ( !list || list->num == 0) {
  108. av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n");
  109. goto out;
  110. }
  111. /* FIXME: To select a specific camera I need to search in list its guid */
  112. dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
  113. if (list->num > 1) {
  114. av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
  115. }
  116. /* Freeing list of cameras */
  117. dc1394_camera_free_list (list);
  118. /* Get the list of video modes supported by the camera. */
  119. res = dc1394_video_get_supported_modes (dc1394->camera, &video_modes);
  120. if (res != DC1394_SUCCESS) {
  121. av_log(c, AV_LOG_ERROR, "Could not get video formats.\n");
  122. goto out_camera;
  123. }
  124. if (dc1394->pixel_format) {
  125. if ((ap->pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == PIX_FMT_NONE) {
  126. av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format);
  127. ret = AVERROR(EINVAL);
  128. goto out;
  129. }
  130. }
  131. if (dc1394->video_size) {
  132. if ((ret = av_parse_video_size(&ap->width, &ap->height, dc1394->video_size)) < 0) {
  133. av_log(c, AV_LOG_ERROR, "Couldn't parse video size.\n");
  134. goto out;
  135. }
  136. }
  137. /* Choose the best mode. */
  138. rate = (ap->time_base.num ? av_rescale(1000, ap->time_base.den, ap->time_base.num) : -1);
  139. max_score = -1;
  140. for (i = 0; i < video_modes.num; i++) {
  141. if (video_modes.modes[i] == DC1394_VIDEO_MODE_EXIF
  142. || (video_modes.modes[i] >= DC1394_VIDEO_MODE_FORMAT7_MIN
  143. && video_modes.modes[i] <= DC1394_VIDEO_MODE_FORMAT7_MAX)) {
  144. /* These modes are currently not supported as they would require
  145. * much more work. For the remaining modes, the functions
  146. * dc1394_get_image_size_from_video_mode and
  147. * dc1394_get_color_coding_from_video_mode do not need to query the
  148. * camera, and thus cannot fail. */
  149. continue;
  150. }
  151. dc1394_get_color_coding_from_video_mode (NULL, video_modes.modes[i],
  152. &dc1394_color_coding);
  153. for (cc = dc1394_color_codings; cc->pix_fmt != PIX_FMT_NONE; cc++)
  154. if (cc->coding == dc1394_color_coding)
  155. break;
  156. if (cc->pix_fmt == PIX_FMT_NONE) {
  157. /* We currently cannot handle this color coding. */
  158. continue;
  159. }
  160. /* Here we know that the mode is supported. Get its frame size and the list
  161. * of frame rates supported by the camera for this mode. This list is sorted
  162. * in ascending order according to libdc1394 example programs. */
  163. dc1394_get_image_size_from_video_mode (NULL, video_modes.modes[i],
  164. &dc1394_width, &dc1394_height);
  165. res = dc1394_video_get_supported_framerates (dc1394->camera, video_modes.modes[i],
  166. &frame_rates);
  167. if (res != DC1394_SUCCESS || frame_rates.num == 0) {
  168. av_log(c, AV_LOG_ERROR, "Cannot get frame rates for video mode.\n");
  169. goto out_camera;
  170. }
  171. /* Choose the best frame rate. */
  172. best_rate = -1;
  173. for (j = 0; j < frame_rates.num; j++) {
  174. for (fr = dc1394_frame_rates; fr->frame_rate; fr++) {
  175. if (fr->frame_rate_id == frame_rates.framerates[j]) {
  176. break;
  177. }
  178. }
  179. if (!fr->frame_rate) {
  180. /* This frame rate is not supported. */
  181. continue;
  182. }
  183. best_rate = fr->frame_rate;
  184. frame_rate = fr->frame_rate_id;
  185. if (ap->time_base.num && rate == fr->frame_rate) {
  186. /* This is the requested frame rate. */
  187. break;
  188. }
  189. }
  190. if (best_rate == -1) {
  191. /* No supported rate found. */
  192. continue;
  193. }
  194. /* Here we know that both the mode and the rate are supported. Compute score. */
  195. if (ap->width && ap->height
  196. && (dc1394_width == ap->width && dc1394_height == ap->height)) {
  197. score = 110000;
  198. } else {
  199. score = dc1394_width * 10; // 1600 - 16000
  200. }
  201. if (ap->pix_fmt == cc->pix_fmt) {
  202. score += 90000;
  203. } else {
  204. score += cc->score; // 1000 - 1500
  205. }
  206. if (ap->time_base.num && rate == best_rate) {
  207. score += 70000;
  208. } else {
  209. score += best_rate / 1000; // 1 - 240
  210. }
  211. if (score > max_score) {
  212. video_mode = video_modes.modes[i];
  213. final_width = dc1394_width;
  214. final_height = dc1394_height;
  215. final_pix_fmt = cc->pix_fmt;
  216. final_frame_rate = best_rate;
  217. max_score = score;
  218. }
  219. }
  220. if (max_score == -1) {
  221. av_log(c, AV_LOG_ERROR, "No suitable video mode / frame rate available.\n");
  222. goto out_camera;
  223. }
  224. if (ap->width && ap->height && !(ap->width == final_width && ap->height == final_height)) {
  225. av_log(c, AV_LOG_WARNING, "Requested frame size is not available, using fallback.\n");
  226. }
  227. if (ap->pix_fmt != PIX_FMT_NONE && ap->pix_fmt != final_pix_fmt) {
  228. av_log(c, AV_LOG_WARNING, "Requested pixel format is not supported, using fallback.\n");
  229. }
  230. if (ap->time_base.num && rate != final_frame_rate) {
  231. av_log(c, AV_LOG_WARNING, "Requested frame rate is not available, using fallback.\n");
  232. }
  233. /* create a video stream */
  234. vst = av_new_stream(c, 0);
  235. if (!vst)
  236. goto out_camera;
  237. av_set_pts_info(vst, 64, 1, 1000);
  238. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  239. vst->codec->codec_id = CODEC_ID_RAWVIDEO;
  240. vst->codec->time_base.den = final_frame_rate;
  241. vst->codec->time_base.num = 1000;
  242. vst->codec->width = final_width;
  243. vst->codec->height = final_height;
  244. vst->codec->pix_fmt = final_pix_fmt;
  245. /* packet init */
  246. av_init_packet(&dc1394->packet);
  247. dc1394->packet.size = avpicture_get_size(final_pix_fmt, final_width, final_height);
  248. dc1394->packet.stream_index = vst->index;
  249. dc1394->packet.flags |= AV_PKT_FLAG_KEY;
  250. dc1394->current_frame = 0;
  251. dc1394->fps = final_frame_rate;
  252. vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, final_frame_rate, 1000);
  253. /* Select MAX Speed possible from the cam */
  254. if (dc1394->camera->bmode_capable>0) {
  255. dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
  256. i = DC1394_ISO_SPEED_800;
  257. } else {
  258. i = DC1394_ISO_SPEED_400;
  259. }
  260. for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
  261. res=dc1394_video_set_iso_speed(dc1394->camera, i);
  262. }
  263. if (res != DC1394_SUCCESS) {
  264. av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
  265. goto out_camera;
  266. }
  267. if (dc1394_video_set_mode(dc1394->camera, video_mode) != DC1394_SUCCESS) {
  268. av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
  269. goto out_camera;
  270. }
  271. if (dc1394_video_set_framerate(dc1394->camera, frame_rate) != DC1394_SUCCESS) {
  272. av_log(c, AV_LOG_ERROR, "Could not set framerate %d.\n", final_frame_rate);
  273. goto out_camera;
  274. }
  275. if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
  276. av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
  277. goto out_camera;
  278. }
  279. if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
  280. av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
  281. goto out_camera;
  282. }
  283. return 0;
  284. out_camera:
  285. dc1394_capture_stop(dc1394->camera);
  286. dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
  287. dc1394_camera_free (dc1394->camera);
  288. out:
  289. av_freep(&dc1394->video_size);
  290. av_freep(&dc1394->pixel_format);
  291. dc1394_free(dc1394->d);
  292. return ret;
  293. }
  294. static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt)
  295. {
  296. struct dc1394_data *dc1394 = c->priv_data;
  297. int res;
  298. /* discard stale frame */
  299. if (dc1394->current_frame++) {
  300. if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
  301. av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
  302. }
  303. res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
  304. if (res == DC1394_SUCCESS) {
  305. dc1394->packet.data = (uint8_t *)(dc1394->frame->image);
  306. dc1394->packet.pts = (dc1394->current_frame * 1000000) / (dc1394->fps);
  307. res = dc1394->frame->image_bytes;
  308. } else {
  309. av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
  310. dc1394->packet.data = NULL;
  311. res = -1;
  312. }
  313. *pkt = dc1394->packet;
  314. return res;
  315. }
  316. static int dc1394_close(AVFormatContext * context)
  317. {
  318. struct dc1394_data *dc1394 = context->priv_data;
  319. dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
  320. dc1394_capture_stop(dc1394->camera);
  321. dc1394_camera_free(dc1394->camera);
  322. dc1394_free(dc1394->d);
  323. return 0;
  324. }
  325. AVInputFormat ff_libdc1394_demuxer = {
  326. .name = "libdc1394",
  327. .long_name = NULL_IF_CONFIG_SMALL("dc1394 A/V grab"),
  328. .priv_data_size = sizeof(struct dc1394_data),
  329. .read_header = dc1394_read_header,
  330. .read_packet = dc1394_read_packet,
  331. .read_close = dc1394_close,
  332. .flags = AVFMT_NOFILE,
  333. .priv_class = &libdc1394_class,
  334. };