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