libdc1394.c 13 KB

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