libdc1394.c 13 KB

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