libdc1394.c 13 KB

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