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.

192 lines
6.2KB

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