00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avformat.h"
00023
00024 #include <libraw1394/raw1394.h>
00025 #include <libdc1394/dc1394_control.h>
00026
00027 #undef free
00028
00029 typedef struct dc1394_data {
00030 raw1394handle_t handle;
00031 dc1394_cameracapture camera;
00032 int current_frame;
00033 int fps;
00034
00035 AVPacket packet;
00036 } dc1394_data;
00037
00038 struct dc1394_frame_format {
00039 int width;
00040 int height;
00041 enum PixelFormat pix_fmt;
00042 int frame_size_id;
00043 } dc1394_frame_formats[] = {
00044 { 320, 240, PIX_FMT_UYVY422, MODE_320x240_YUV422 },
00045 { 640, 480, PIX_FMT_UYYVYY411, MODE_640x480_YUV411 },
00046 { 640, 480, PIX_FMT_UYVY422, MODE_640x480_YUV422 },
00047 { 0, 0, 0, MODE_320x240_YUV422 }
00048 };
00049
00050 struct dc1394_frame_rate {
00051 int frame_rate;
00052 int frame_rate_id;
00053 } dc1394_frame_rates[] = {
00054 { 1875, FRAMERATE_1_875 },
00055 { 3750, FRAMERATE_3_75 },
00056 { 7500, FRAMERATE_7_5 },
00057 { 15000, FRAMERATE_15 },
00058 { 30000, FRAMERATE_30 },
00059 { 60000, FRAMERATE_60 },
00060 { 0, FRAMERATE_30 }
00061 };
00062
00063 static int dc1394_read_header(AVFormatContext *c, AVFormatParameters * ap)
00064 {
00065 dc1394_data* dc1394 = c->priv_data;
00066 AVStream* vst;
00067 nodeid_t* camera_nodes;
00068 int res;
00069 struct dc1394_frame_format *fmt;
00070 struct dc1394_frame_rate *fps;
00071
00072 for (fmt = dc1394_frame_formats; fmt->width; fmt++)
00073 if (fmt->pix_fmt == ap->pix_fmt && fmt->width == ap->width && fmt->height == ap->height)
00074 break;
00075
00076 for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
00077 if (fps->frame_rate == av_rescale(1000, ap->time_base.den, ap->time_base.num))
00078 break;
00079
00080
00081 vst = av_new_stream(c, 0);
00082 if (!vst)
00083 return -1;
00084 av_set_pts_info(vst, 64, 1, 1000);
00085 vst->codec->codec_type = CODEC_TYPE_VIDEO;
00086 vst->codec->codec_id = CODEC_ID_RAWVIDEO;
00087 vst->codec->time_base.den = fps->frame_rate;
00088 vst->codec->time_base.num = 1000;
00089 vst->codec->width = fmt->width;
00090 vst->codec->height = fmt->height;
00091 vst->codec->pix_fmt = fmt->pix_fmt;
00092
00093
00094 av_init_packet(&dc1394->packet);
00095 dc1394->packet.size = avpicture_get_size(fmt->pix_fmt, fmt->width, fmt->height);
00096 dc1394->packet.stream_index = vst->index;
00097 dc1394->packet.flags |= PKT_FLAG_KEY;
00098
00099 dc1394->current_frame = 0;
00100 dc1394->fps = fps->frame_rate;
00101
00102 vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000);
00103
00104
00105 dc1394->handle = dc1394_create_handle(0);
00106 if (!dc1394->handle) {
00107 av_log(c, AV_LOG_ERROR, "Can't acquire dc1394 handle on port %d\n", 0 );
00108 goto out;
00109 }
00110 camera_nodes = dc1394_get_camera_nodes(dc1394->handle, &res, 1);
00111 if (!camera_nodes || camera_nodes[ap->channel] == DC1394_NO_CAMERA) {
00112 av_log(c, AV_LOG_ERROR, "There's no IIDC camera on the channel %d\n", ap->channel);
00113 goto out_handle;
00114 }
00115 res = dc1394_dma_setup_capture(dc1394->handle, camera_nodes[ap->channel],
00116 0,
00117 FORMAT_VGA_NONCOMPRESSED,
00118 fmt->frame_size_id,
00119 SPEED_400,
00120 fps->frame_rate_id, 8, 1,
00121 c->filename,
00122 &dc1394->camera);
00123 dc1394_free_camera_nodes(camera_nodes);
00124 if (res != DC1394_SUCCESS) {
00125 av_log(c, AV_LOG_ERROR, "Can't prepare camera for the DMA capture\n");
00126 goto out_handle;
00127 }
00128
00129 res = dc1394_start_iso_transmission(dc1394->handle, dc1394->camera.node);
00130 if (res != DC1394_SUCCESS) {
00131 av_log(c, AV_LOG_ERROR, "Can't start isochronous transmission\n");
00132 goto out_handle_dma;
00133 }
00134
00135 return 0;
00136
00137 out_handle_dma:
00138 dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
00139 dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
00140 out_handle:
00141 dc1394_destroy_handle(dc1394->handle);
00142 out:
00143 return -1;
00144 }
00145
00146 static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt)
00147 {
00148 struct dc1394_data *dc1394 = c->priv_data;
00149 int res;
00150
00151
00152 if (dc1394->current_frame++) {
00153 if (dc1394_dma_done_with_buffer(&dc1394->camera) != DC1394_SUCCESS)
00154 av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
00155 }
00156
00157 res = dc1394_dma_single_capture(&dc1394->camera);
00158
00159 if (res == DC1394_SUCCESS) {
00160 dc1394->packet.data = (uint8_t *)(dc1394->camera.capture_buffer);
00161 dc1394->packet.pts = (dc1394->current_frame * 1000000) / dc1394->fps;
00162 res = dc1394->packet.size;
00163 } else {
00164 av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
00165 dc1394->packet.data = NULL;
00166 res = -1;
00167 }
00168
00169 *pkt = dc1394->packet;
00170 return res;
00171 }
00172
00173 static int dc1394_close(AVFormatContext * context)
00174 {
00175 struct dc1394_data *dc1394 = context->priv_data;
00176
00177 dc1394_stop_iso_transmission(dc1394->handle, dc1394->camera.node);
00178 dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
00179 dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
00180 dc1394_destroy_handle(dc1394->handle);
00181
00182 return 0;
00183 }
00184
00185 AVInputFormat libdc1394_demuxer = {
00186 .name = "libdc1394",
00187 .long_name = "dc1394 A/V grab",
00188 .priv_data_size = sizeof(struct dc1394_data),
00189 .read_header = dc1394_read_header,
00190 .read_packet = dc1394_read_packet,
00191 .read_close = dc1394_close,
00192 .flags = AVFMT_NOFILE
00193 };