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.

194 lines
6.3KB

  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, MODE_320x240_YUV422 } /* default -- 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, FRAMERATE_30 } /* default -- 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. for (fmt = dc1394_frame_formats; fmt->width; fmt++)
  64. if (fmt->pix_fmt == ap->pix_fmt && fmt->width == ap->width && fmt->height == ap->height)
  65. break;
  66. for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
  67. if (fps->frame_rate == av_rescale(1000, ap->time_base.den, ap->time_base.num))
  68. break;
  69. /* create a video stream */
  70. vst = av_new_stream(c, 0);
  71. if (!vst)
  72. return -1;
  73. av_set_pts_info(vst, 64, 1, 1000);
  74. vst->codec->codec_type = CODEC_TYPE_VIDEO;
  75. vst->codec->codec_id = CODEC_ID_RAWVIDEO;
  76. vst->codec->time_base.den = fps->frame_rate;
  77. vst->codec->time_base.num = 1000;
  78. vst->codec->width = fmt->width;
  79. vst->codec->height = fmt->height;
  80. vst->codec->pix_fmt = fmt->pix_fmt;
  81. /* packet init */
  82. av_init_packet(&dc1394->packet);
  83. dc1394->packet.size = avpicture_get_size(fmt->pix_fmt, fmt->width, fmt->height);
  84. dc1394->packet.stream_index = vst->index;
  85. dc1394->packet.flags |= PKT_FLAG_KEY;
  86. dc1394->current_frame = 0;
  87. dc1394->fps = fps->frame_rate;
  88. vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000);
  89. /* Now lets prep the hardware */
  90. dc1394->handle = dc1394_create_handle(0); /* FIXME: gotta have ap->port */
  91. if (!dc1394->handle) {
  92. av_log(c, AV_LOG_ERROR, "Can't acquire dc1394 handle on port %d\n", 0 /* ap->port */);
  93. goto out;
  94. }
  95. camera_nodes = dc1394_get_camera_nodes(dc1394->handle, &res, 1);
  96. if (!camera_nodes || camera_nodes[ap->channel] == DC1394_NO_CAMERA) {
  97. av_log(c, AV_LOG_ERROR, "There's no IIDC camera on the channel %d\n", ap->channel);
  98. goto out_handle;
  99. }
  100. res = dc1394_dma_setup_capture(dc1394->handle, camera_nodes[ap->channel],
  101. 0,
  102. FORMAT_VGA_NONCOMPRESSED,
  103. fmt->frame_size_id,
  104. SPEED_400,
  105. fps->frame_rate_id, 8, 1,
  106. c->filename,
  107. &dc1394->camera);
  108. dc1394_free_camera_nodes(camera_nodes);
  109. if (res != DC1394_SUCCESS) {
  110. av_log(c, AV_LOG_ERROR, "Can't prepare camera for the DMA capture\n");
  111. goto out_handle;
  112. }
  113. res = dc1394_start_iso_transmission(dc1394->handle, dc1394->camera.node);
  114. if (res != DC1394_SUCCESS) {
  115. av_log(c, AV_LOG_ERROR, "Can't start isochronous transmission\n");
  116. goto out_handle_dma;
  117. }
  118. return 0;
  119. out_handle_dma:
  120. dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
  121. dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
  122. out_handle:
  123. dc1394_destroy_handle(dc1394->handle);
  124. out:
  125. return -1;
  126. }
  127. static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt)
  128. {
  129. struct dc1394_data *dc1394 = c->priv_data;
  130. int res;
  131. /* discard stale frame */
  132. if (dc1394->current_frame++) {
  133. if (dc1394_dma_done_with_buffer(&dc1394->camera) != DC1394_SUCCESS)
  134. av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
  135. }
  136. res = dc1394_dma_single_capture(&dc1394->camera);
  137. if (res == DC1394_SUCCESS) {
  138. dc1394->packet.data = (uint8_t *)(dc1394->camera.capture_buffer);
  139. dc1394->packet.pts = (dc1394->current_frame * 1000000) / dc1394->fps;
  140. res = dc1394->packet.size;
  141. } else {
  142. av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
  143. dc1394->packet.data = NULL;
  144. res = -1;
  145. }
  146. *pkt = dc1394->packet;
  147. return res;
  148. }
  149. static int dc1394_close(AVFormatContext * context)
  150. {
  151. struct dc1394_data *dc1394 = context->priv_data;
  152. dc1394_stop_iso_transmission(dc1394->handle, dc1394->camera.node);
  153. dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
  154. dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
  155. dc1394_destroy_handle(dc1394->handle);
  156. return 0;
  157. }
  158. AVInputFormat dc1394_demuxer = {
  159. .name = "dc1394",
  160. .long_name = "dc1394 A/V grab",
  161. .priv_data_size = sizeof(struct dc1394_data),
  162. .read_header = dc1394_read_header,
  163. .read_packet = dc1394_read_packet,
  164. .read_close = dc1394_close,
  165. .flags = AVFMT_NOFILE
  166. };