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.

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