00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "config.h"
00024 #include "libavformat/avformat.h"
00025
00026 #if HAVE_LIBDC1394_2
00027 #include <dc1394/dc1394.h>
00028 #elif HAVE_LIBDC1394_1
00029 #include <libraw1394/raw1394.h>
00030 #include <libdc1394/dc1394_control.h>
00031
00032 #define DC1394_VIDEO_MODE_320x240_YUV422 MODE_320x240_YUV422
00033 #define DC1394_VIDEO_MODE_640x480_YUV411 MODE_640x480_YUV411
00034 #define DC1394_VIDEO_MODE_640x480_YUV422 MODE_640x480_YUV422
00035 #define DC1394_FRAMERATE_1_875 FRAMERATE_1_875
00036 #define DC1394_FRAMERATE_3_75 FRAMERATE_3_75
00037 #define DC1394_FRAMERATE_7_5 FRAMERATE_7_5
00038 #define DC1394_FRAMERATE_15 FRAMERATE_15
00039 #define DC1394_FRAMERATE_30 FRAMERATE_30
00040 #define DC1394_FRAMERATE_60 FRAMERATE_60
00041 #define DC1394_FRAMERATE_120 FRAMERATE_120
00042 #define DC1394_FRAMERATE_240 FRAMERATE_240
00043 #endif
00044
00045 #undef free
00046
00047 typedef struct dc1394_data {
00048 #if HAVE_LIBDC1394_1
00049 raw1394handle_t handle;
00050 dc1394_cameracapture camera;
00051 #elif HAVE_LIBDC1394_2
00052 dc1394_t *d;
00053 dc1394camera_t *camera;
00054 dc1394video_frame_t *frame;
00055 #endif
00056 int current_frame;
00057 int fps;
00058
00059 AVPacket packet;
00060 } dc1394_data;
00061
00062 struct dc1394_frame_format {
00063 int width;
00064 int height;
00065 enum PixelFormat pix_fmt;
00066 int frame_size_id;
00067 } dc1394_frame_formats[] = {
00068 { 320, 240, PIX_FMT_UYVY422, DC1394_VIDEO_MODE_320x240_YUV422 },
00069 { 640, 480, PIX_FMT_UYYVYY411, DC1394_VIDEO_MODE_640x480_YUV411 },
00070 { 640, 480, PIX_FMT_UYVY422, DC1394_VIDEO_MODE_640x480_YUV422 },
00071 { 0, 0, 0, 0 }
00072 };
00073
00074 struct dc1394_frame_rate {
00075 int frame_rate;
00076 int frame_rate_id;
00077 } dc1394_frame_rates[] = {
00078 { 1875, DC1394_FRAMERATE_1_875 },
00079 { 3750, DC1394_FRAMERATE_3_75 },
00080 { 7500, DC1394_FRAMERATE_7_5 },
00081 { 15000, DC1394_FRAMERATE_15 },
00082 { 30000, DC1394_FRAMERATE_30 },
00083 { 60000, DC1394_FRAMERATE_60 },
00084 {120000, DC1394_FRAMERATE_120 },
00085 {240000, DC1394_FRAMERATE_240 },
00086 { 0, 0 }
00087 };
00088
00089 static inline int dc1394_read_common(AVFormatContext *c, AVFormatParameters *ap,
00090 struct dc1394_frame_format **select_fmt, struct dc1394_frame_rate **select_fps)
00091 {
00092 dc1394_data* dc1394 = c->priv_data;
00093 AVStream* vst;
00094 struct dc1394_frame_format *fmt;
00095 struct dc1394_frame_rate *fps;
00096 enum PixelFormat pix_fmt = ap->pix_fmt == PIX_FMT_NONE ? PIX_FMT_UYVY422 : ap->pix_fmt;
00097 int width = !ap->width ? 320 : ap->width;
00098 int height = !ap->height ? 240 : ap->height;
00099 int frame_rate = !ap->time_base.num ? 30000 : av_rescale(1000, ap->time_base.den, ap->time_base.num);
00100
00101 for (fmt = dc1394_frame_formats; fmt->width; fmt++)
00102 if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
00103 break;
00104
00105 for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
00106 if (fps->frame_rate == frame_rate)
00107 break;
00108
00109 if (!fps->frame_rate || !fmt->width) {
00110 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),
00111 width, height, frame_rate);
00112 goto out;
00113 }
00114
00115
00116 vst = av_new_stream(c, 0);
00117 if (!vst)
00118 goto out;
00119 av_set_pts_info(vst, 64, 1, 1000);
00120 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00121 vst->codec->codec_id = CODEC_ID_RAWVIDEO;
00122 vst->codec->time_base.den = fps->frame_rate;
00123 vst->codec->time_base.num = 1000;
00124 vst->codec->width = fmt->width;
00125 vst->codec->height = fmt->height;
00126 vst->codec->pix_fmt = fmt->pix_fmt;
00127
00128
00129 av_init_packet(&dc1394->packet);
00130 dc1394->packet.size = avpicture_get_size(fmt->pix_fmt, fmt->width, fmt->height);
00131 dc1394->packet.stream_index = vst->index;
00132 dc1394->packet.flags |= AV_PKT_FLAG_KEY;
00133
00134 dc1394->current_frame = 0;
00135 dc1394->fps = fps->frame_rate;
00136
00137 vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000);
00138 *select_fps = fps;
00139 *select_fmt = fmt;
00140 return 0;
00141 out:
00142 return -1;
00143 }
00144
00145 #if HAVE_LIBDC1394_1
00146 static int dc1394_v1_read_header(AVFormatContext *c, AVFormatParameters * ap)
00147 {
00148 dc1394_data* dc1394 = c->priv_data;
00149 AVStream* vst;
00150 nodeid_t* camera_nodes;
00151 int res;
00152 struct dc1394_frame_format *fmt = NULL;
00153 struct dc1394_frame_rate *fps = NULL;
00154
00155 if (dc1394_read_common(c,ap,&fmt,&fps) != 0)
00156 return -1;
00157
00158
00159 dc1394->handle = dc1394_create_handle(0);
00160 if (!dc1394->handle) {
00161 av_log(c, AV_LOG_ERROR, "Can't acquire dc1394 handle on port %d\n", 0 );
00162 goto out;
00163 }
00164 camera_nodes = dc1394_get_camera_nodes(dc1394->handle, &res, 1);
00165 if (!camera_nodes || camera_nodes[ap->channel] == DC1394_NO_CAMERA) {
00166 av_log(c, AV_LOG_ERROR, "There's no IIDC camera on the channel %d\n", ap->channel);
00167 goto out_handle;
00168 }
00169 res = dc1394_dma_setup_capture(dc1394->handle, camera_nodes[ap->channel],
00170 0,
00171 FORMAT_VGA_NONCOMPRESSED,
00172 fmt->frame_size_id,
00173 SPEED_400,
00174 fps->frame_rate_id, 8, 1,
00175 c->filename,
00176 &dc1394->camera);
00177 dc1394_free_camera_nodes(camera_nodes);
00178 if (res != DC1394_SUCCESS) {
00179 av_log(c, AV_LOG_ERROR, "Can't prepare camera for the DMA capture\n");
00180 goto out_handle;
00181 }
00182
00183 res = dc1394_start_iso_transmission(dc1394->handle, dc1394->camera.node);
00184 if (res != DC1394_SUCCESS) {
00185 av_log(c, AV_LOG_ERROR, "Can't start isochronous transmission\n");
00186 goto out_handle_dma;
00187 }
00188
00189 return 0;
00190
00191 out_handle_dma:
00192 dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
00193 dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
00194 out_handle:
00195 dc1394_destroy_handle(dc1394->handle);
00196 out:
00197 return -1;
00198 }
00199
00200 static int dc1394_v1_read_packet(AVFormatContext *c, AVPacket *pkt)
00201 {
00202 struct dc1394_data *dc1394 = c->priv_data;
00203 int res;
00204
00205
00206 if (dc1394->current_frame++) {
00207 if (dc1394_dma_done_with_buffer(&dc1394->camera) != DC1394_SUCCESS)
00208 av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
00209 }
00210
00211 res = dc1394_dma_single_capture(&dc1394->camera);
00212
00213 if (res == DC1394_SUCCESS) {
00214 dc1394->packet.data = (uint8_t *)(dc1394->camera.capture_buffer);
00215 dc1394->packet.pts = (dc1394->current_frame * 1000000) / dc1394->fps;
00216 res = dc1394->packet.size;
00217 } else {
00218 av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
00219 dc1394->packet.data = NULL;
00220 res = -1;
00221 }
00222
00223 *pkt = dc1394->packet;
00224 return res;
00225 }
00226
00227 static int dc1394_v1_close(AVFormatContext * context)
00228 {
00229 struct dc1394_data *dc1394 = context->priv_data;
00230
00231 dc1394_stop_iso_transmission(dc1394->handle, dc1394->camera.node);
00232 dc1394_dma_unlisten(dc1394->handle, &dc1394->camera);
00233 dc1394_dma_release_camera(dc1394->handle, &dc1394->camera);
00234 dc1394_destroy_handle(dc1394->handle);
00235
00236 return 0;
00237 }
00238
00239 #elif HAVE_LIBDC1394_2
00240 static int dc1394_v2_read_header(AVFormatContext *c, AVFormatParameters * ap)
00241 {
00242 dc1394_data* dc1394 = c->priv_data;
00243 dc1394camera_list_t *list;
00244 int res, i;
00245 struct dc1394_frame_format *fmt = NULL;
00246 struct dc1394_frame_rate *fps = NULL;
00247
00248 if (dc1394_read_common(c,ap,&fmt,&fps) != 0)
00249 return -1;
00250
00251
00252 dc1394->d = dc1394_new();
00253 dc1394_camera_enumerate (dc1394->d, &list);
00254 if ( !list || list->num == 0) {
00255 av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n");
00256 goto out;
00257 }
00258
00259
00260 dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
00261 if (list->num > 1) {
00262 av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
00263 }
00264
00265
00266 dc1394_camera_free_list (list);
00267
00268
00269 if (dc1394->camera->bmode_capable>0) {
00270 dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
00271 i = DC1394_ISO_SPEED_800;
00272 } else {
00273 i = DC1394_ISO_SPEED_400;
00274 }
00275
00276 for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
00277 res=dc1394_video_set_iso_speed(dc1394->camera, i);
00278 }
00279 if (res != DC1394_SUCCESS) {
00280 av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
00281 goto out_camera;
00282 }
00283
00284 if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
00285 av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
00286 goto out_camera;
00287 }
00288
00289 if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
00290 av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
00291 goto out_camera;
00292 }
00293 if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
00294 av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
00295 goto out_camera;
00296 }
00297
00298 if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
00299 av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
00300 goto out_camera;
00301 }
00302 return 0;
00303
00304 out_camera:
00305 dc1394_capture_stop(dc1394->camera);
00306 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
00307 dc1394_camera_free (dc1394->camera);
00308 out:
00309 dc1394_free(dc1394->d);
00310 return -1;
00311 }
00312
00313 static int dc1394_v2_read_packet(AVFormatContext *c, AVPacket *pkt)
00314 {
00315 struct dc1394_data *dc1394 = c->priv_data;
00316 int res;
00317
00318
00319 if (dc1394->current_frame++) {
00320 if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
00321 av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
00322 }
00323
00324 res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
00325 if (res == DC1394_SUCCESS) {
00326 dc1394->packet.data = (uint8_t *)(dc1394->frame->image);
00327 dc1394->packet.pts = (dc1394->current_frame * 1000000) / (dc1394->fps);
00328 res = dc1394->frame->image_bytes;
00329 } else {
00330 av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
00331 dc1394->packet.data = NULL;
00332 res = -1;
00333 }
00334
00335 *pkt = dc1394->packet;
00336 return res;
00337 }
00338
00339 static int dc1394_v2_close(AVFormatContext * context)
00340 {
00341 struct dc1394_data *dc1394 = context->priv_data;
00342
00343 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
00344 dc1394_capture_stop(dc1394->camera);
00345 dc1394_camera_free(dc1394->camera);
00346 dc1394_free(dc1394->d);
00347
00348 return 0;
00349 }
00350
00351 AVInputFormat libdc1394_demuxer = {
00352 .name = "libdc1394",
00353 .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"),
00354 .priv_data_size = sizeof(struct dc1394_data),
00355 .read_header = dc1394_v2_read_header,
00356 .read_packet = dc1394_v2_read_packet,
00357 .read_close = dc1394_v2_close,
00358 .flags = AVFMT_NOFILE
00359 };
00360
00361 #endif
00362 #if HAVE_LIBDC1394_1
00363 AVInputFormat libdc1394_demuxer = {
00364 .name = "libdc1394",
00365 .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.1 A/V grab"),
00366 .priv_data_size = sizeof(struct dc1394_data),
00367 .read_header = dc1394_v1_read_header,
00368 .read_packet = dc1394_v1_read_packet,
00369 .read_close = dc1394_v1_close,
00370 .flags = AVFMT_NOFILE
00371 };
00372 #endif