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.

204 lines
6.8KB

  1. /*
  2. * IIDC1394 grab interface (uses libdc1394 and libraw1394)
  3. * Copyright (c) 2004 Roman Shaposhnik
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include <libraw1394/raw1394.h>
  23. #include <libdc1394/dc1394_control.h>
  24. #undef free
  25. typedef struct dc1394_data {
  26. raw1394handle_t handle;
  27. dc1394_cameracapture camera;
  28. int current_frame;
  29. int fps;
  30. AVPacket packet;
  31. } dc1394_data;
  32. struct dc1394_frame_format {
  33. int width;
  34. int height;
  35. enum PixelFormat pix_fmt;
  36. int frame_size_id;
  37. } dc1394_frame_formats[] = {
  38. { 320, 240, PIX_FMT_UYVY422, MODE_320x240_YUV422 },
  39. { 640, 480, PIX_FMT_UYYVYY411, MODE_640x480_YUV411 },
  40. { 640, 480, PIX_FMT_UYVY422, MODE_640x480_YUV422 },
  41. { 0, 0, 0, 0 } /* gotta be the last one */
  42. };
  43. struct dc1394_frame_rate {
  44. int frame_rate;
  45. int frame_rate_id;
  46. } dc1394_frame_rates[] = {
  47. { 1875, FRAMERATE_1_875 },
  48. { 3750, FRAMERATE_3_75 },
  49. { 7500, FRAMERATE_7_5 },
  50. { 15000, FRAMERATE_15 },
  51. { 30000, FRAMERATE_30 },
  52. { 60000, FRAMERATE_60 },
  53. { 0, 0 } /* gotta be the last one */
  54. };
  55. static int dc1394_read_header(AVFormatContext *c, AVFormatParameters * ap)
  56. {
  57. dc1394_data* dc1394 = c->priv_data;
  58. AVStream* vst;
  59. nodeid_t* camera_nodes;
  60. int res;
  61. struct dc1394_frame_format *fmt;
  62. struct dc1394_frame_rate *fps;
  63. enum PixelFormat pix_fmt = ap->pix_fmt == PIX_FMT_NONE ? PIX_FMT_UYVY422 : ap->pix_fmt; /* defaults */
  64. int width = !ap->width ? 320 : ap->width;
  65. int height = !ap->height ? 240 : ap->height;
  66. int frame_rate = !ap->time_base.num ? 30000 : av_rescale(1000, ap->time_base.den, ap->time_base.num);
  67. for (fmt = dc1394_frame_formats; fmt->width; fmt++)
  68. if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
  69. break;
  70. for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
  71. if (fps->frame_rate == frame_rate)
  72. break;
  73. if (!fps->frame_rate || !fmt->width) {
  74. av_log(c, AV_LOG_ERROR, "Can't find matching camera format for %s, %dx%d@%d:1000fps\n", avcodec_get_pix_fmt_name(pix_fmt),
  75. width, height, frame_rate);
  76. goto out;
  77. }
  78. /* create a video stream */
  79. vst = av_new_stream(c, 0);
  80. if (!vst)
  81. return -1;
  82. av_set_pts_info(vst, 64, 1, 1000);
  83. vst->codec->codec_type = CODEC_TYPE_VIDEO;
  84. vst->codec->codec_id = CODEC_ID_RAWVIDEO;
  85. vst->codec->time_base.den = fps->frame_rate;
  86. vst->codec->time_base.num = 1000;
  87. vst->codec->width = fmt->width;
  88. vst->codec->height = fmt->height;
  89. vst->codec->pix_fmt = fmt->pix_fmt;
  90. /* packet init */
  91. av_init_packet(&dc1394->packet);
  92. dc1394->packet.size = avpicture_get_size(fmt->pix_fmt, fmt->width, fmt->height);
  93. dc1394->packet.stream_index = vst->index;
  94. dc1394->packet.flags |= PKT_FLAG_KEY;
  95. dc1394->current_frame = 0;
  96. dc1394->fps = fps->frame_rate;
  97. vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000);
  98. /* Now lets prep the hardware */
  99. dc1394->handle = dc1394_create_handle(0); /* FIXME: gotta have ap->port */
  100. if (!dc1394->handle) {
  101. av_log(c, AV_LOG_ERROR, "Can't acquire dc1394 handle on port %d\n", 0 /* ap->port */);
  102. goto out;
  103. }
  104. camera_nodes = dc1394_get_camera_nodes(dc1394->handle, &res, 1);
  105. if (!camera_nodes || camera_nodes[ap->channel] == DC1394_NO_CAMERA) {
  106. av_log(c, AV_LOG_ERROR, "There's no IIDC camera on the channel %d\n", ap->channel);
  107. goto out_handle;
  108. }
  109. res = dc1394_dma_setup_capture(dc1394->handle, camera_nodes[ap->channel],
  110. 0,
  111. FORMAT_VGA_NONCOMPRESSED,
  112. fmt->frame_size_id,
  113. SPEED_400,
  114. fps->frame_rate_id, 8, 1,
  115. c->filename,
  116. &dc1394->camera);
  117. dc1394_free_camera_nodes(camera_nodes);
  118. if (res != DC1394_SUCCESS) {
  119. av_log(c, AV_LOG_ERROR, "Can't prepare camera for the DMA capture\n");
  120. goto out_handle;
  121. }
  122. res = dc1394_start_iso_transmission(dc1394->handle, dc1394->camera.node);
  123. if (res != DC1394_SUCCESS) {
  124. av_log(c, AV_LOG_ERROR, "Can't start isochronous transmission\n");
  125. goto out_handle_dma;
  126. }
  127. return 0;
  128. out_handle_dma:
  129. dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
  130. dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
  131. out_handle:
  132. dc1394_destroy_handle(dc1394->handle);
  133. out:
  134. return -1;
  135. }
  136. static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt)
  137. {
  138. struct dc1394_data *dc1394 = c->priv_data;
  139. int res;
  140. /* discard stale frame */
  141. if (dc1394->current_frame++) {
  142. if (dc1394_dma_done_with_buffer(&dc1394->camera) != DC1394_SUCCESS)
  143. av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
  144. }
  145. res = dc1394_dma_single_capture(&dc1394->camera);
  146. if (res == DC1394_SUCCESS) {
  147. dc1394->packet.data = (uint8_t *)(dc1394->camera.capture_buffer);
  148. dc1394->packet.pts = (dc1394->current_frame * 1000000) / dc1394->fps;
  149. res = dc1394->packet.size;
  150. } else {
  151. av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
  152. dc1394->packet.data = NULL;
  153. res = -1;
  154. }
  155. *pkt = dc1394->packet;
  156. return res;
  157. }
  158. static int dc1394_close(AVFormatContext * context)
  159. {
  160. struct dc1394_data *dc1394 = context->priv_data;
  161. dc1394_stop_iso_transmission(dc1394->handle, dc1394->camera.node);
  162. dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
  163. dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
  164. dc1394_destroy_handle(dc1394->handle);
  165. return 0;
  166. }
  167. AVInputFormat libdc1394_demuxer = {
  168. .name = "libdc1394",
  169. .long_name = "dc1394 A/V grab",
  170. .priv_data_size = sizeof(struct dc1394_data),
  171. .read_header = dc1394_read_header,
  172. .read_packet = dc1394_read_packet,
  173. .read_close = dc1394_close,
  174. .flags = AVFMT_NOFILE
  175. };