00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "config.h"
00025 #include "libavutil/log.h"
00026 #include "libavutil/opt.h"
00027 #include "avdevice.h"
00028
00029 #include <stdlib.h>
00030 #include <string.h>
00031 #include "libavutil/parseutils.h"
00032 #include "libavutil/pixdesc.h"
00033
00034 #include <dc1394/dc1394.h>
00035
00036 #undef free
00037
00038 typedef struct dc1394_data {
00039 AVClass *class;
00040 dc1394_t *d;
00041 dc1394camera_t *camera;
00042 dc1394video_frame_t *frame;
00043 int current_frame;
00044 int frame_rate;
00045 char *video_size;
00046 char *pixel_format;
00047 char *framerate;
00049 AVPacket packet;
00050 } dc1394_data;
00051
00052
00053
00054
00055 struct dc1394_color_coding {
00056 int pix_fmt;
00057 int score;
00058 uint32_t coding;
00059 } dc1394_color_codings[] = {
00060 { PIX_FMT_GRAY16BE, 1000, DC1394_COLOR_CODING_MONO16 },
00061 { PIX_FMT_RGB48BE, 1100, DC1394_COLOR_CODING_RGB16 },
00062 { PIX_FMT_GRAY8, 1200, DC1394_COLOR_CODING_MONO8 },
00063 { PIX_FMT_RGB24, 1300, DC1394_COLOR_CODING_RGB8 },
00064 { PIX_FMT_UYYVYY411, 1400, DC1394_COLOR_CODING_YUV411 },
00065 { PIX_FMT_UYVY422, 1500, DC1394_COLOR_CODING_YUV422 },
00066 { PIX_FMT_NONE, 0, 0 }
00067 };
00068
00069 struct dc1394_frame_rate {
00070 int frame_rate;
00071 int frame_rate_id;
00072 } dc1394_frame_rates[] = {
00073 { 1875, DC1394_FRAMERATE_1_875 },
00074 { 3750, DC1394_FRAMERATE_3_75 },
00075 { 7500, DC1394_FRAMERATE_7_5 },
00076 { 15000, DC1394_FRAMERATE_15 },
00077 { 30000, DC1394_FRAMERATE_30 },
00078 { 60000, DC1394_FRAMERATE_60 },
00079 {120000, DC1394_FRAMERATE_120 },
00080 {240000, DC1394_FRAMERATE_240 },
00081 { 0, 0 }
00082 };
00083
00084 #define OFFSET(x) offsetof(dc1394_data, x)
00085 #define DEC AV_OPT_FLAG_DECODING_PARAM
00086 static const AVOption options[] = {
00087 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = "qvga"}, 0, 0, DEC },
00088 { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = "uyvy422"}, 0, 0, DEC },
00089 { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC },
00090 { NULL },
00091 };
00092
00093 static const AVClass libdc1394_class = {
00094 .class_name = "libdc1394 indev",
00095 .item_name = av_default_item_name,
00096 .option = options,
00097 .version = LIBAVUTIL_VERSION_INT,
00098 };
00099
00100 static int dc1394_read_header(AVFormatContext *c, AVFormatParameters * ap)
00101 {
00102 dc1394_data* dc1394 = c->priv_data;
00103 AVStream *vst;
00104 const struct dc1394_color_coding *cc;
00105 const struct dc1394_frame_rate *fr;
00106 dc1394camera_list_t *list;
00107 dc1394video_modes_t video_modes;
00108 dc1394video_mode_t video_mode;
00109 dc1394framerates_t frame_rates;
00110 dc1394framerate_t frame_rate;
00111 uint32_t dc1394_width, dc1394_height, dc1394_color_coding;
00112 int rate, best_rate;
00113 int score, max_score;
00114 int final_width, final_height, final_pix_fmt, final_frame_rate;
00115 int res, i, j;
00116 int ret=-1;
00117
00118
00119 dc1394->d = dc1394_new();
00120 dc1394_camera_enumerate (dc1394->d, &list);
00121 if ( !list || list->num == 0) {
00122 av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n");
00123 goto out;
00124 }
00125
00126
00127 dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
00128 if (list->num > 1) {
00129 av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
00130 }
00131
00132
00133 dc1394_camera_free_list (list);
00134
00135
00136 res = dc1394_video_get_supported_modes (dc1394->camera, &video_modes);
00137 if (res != DC1394_SUCCESS) {
00138 av_log(c, AV_LOG_ERROR, "Could not get video formats.\n");
00139 goto out_camera;
00140 }
00141
00142 if (dc1394->pixel_format) {
00143 if ((ap->pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == PIX_FMT_NONE) {
00144 av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format);
00145 ret = AVERROR(EINVAL);
00146 goto out;
00147 }
00148 }
00149
00150 if (dc1394->video_size) {
00151 if ((ret = av_parse_video_size(&ap->width, &ap->height, dc1394->video_size)) < 0) {
00152 av_log(c, AV_LOG_ERROR, "Couldn't parse video size.\n");
00153 goto out;
00154 }
00155 }
00156
00157
00158 rate = (ap->time_base.num ? av_rescale(1000, ap->time_base.den, ap->time_base.num) : -1);
00159 max_score = -1;
00160 for (i = 0; i < video_modes.num; i++) {
00161 if (video_modes.modes[i] == DC1394_VIDEO_MODE_EXIF
00162 || (video_modes.modes[i] >= DC1394_VIDEO_MODE_FORMAT7_MIN
00163 && video_modes.modes[i] <= DC1394_VIDEO_MODE_FORMAT7_MAX)) {
00164
00165
00166
00167
00168
00169 continue;
00170 }
00171 dc1394_get_color_coding_from_video_mode (NULL, video_modes.modes[i],
00172 &dc1394_color_coding);
00173 for (cc = dc1394_color_codings; cc->pix_fmt != PIX_FMT_NONE; cc++)
00174 if (cc->coding == dc1394_color_coding)
00175 break;
00176 if (cc->pix_fmt == PIX_FMT_NONE) {
00177
00178 continue;
00179 }
00180
00181
00182
00183 dc1394_get_image_size_from_video_mode (NULL, video_modes.modes[i],
00184 &dc1394_width, &dc1394_height);
00185 res = dc1394_video_get_supported_framerates (dc1394->camera, video_modes.modes[i],
00186 &frame_rates);
00187 if (res != DC1394_SUCCESS || frame_rates.num == 0) {
00188 av_log(c, AV_LOG_ERROR, "Cannot get frame rates for video mode.\n");
00189 goto out_camera;
00190 }
00191
00192 best_rate = -1;
00193 for (j = 0; j < frame_rates.num; j++) {
00194 for (fr = dc1394_frame_rates; fr->frame_rate; fr++) {
00195 if (fr->frame_rate_id == frame_rates.framerates[j]) {
00196 break;
00197 }
00198 }
00199 if (!fr->frame_rate) {
00200
00201 continue;
00202 }
00203 best_rate = fr->frame_rate;
00204 frame_rate = fr->frame_rate_id;
00205 if (ap->time_base.num && rate == fr->frame_rate) {
00206
00207 break;
00208 }
00209 }
00210 if (best_rate == -1) {
00211
00212 continue;
00213 }
00214
00215 if (ap->width && ap->height
00216 && (dc1394_width == ap->width && dc1394_height == ap->height)) {
00217 score = 110000;
00218 } else {
00219 score = dc1394_width * 10;
00220 }
00221 if (ap->pix_fmt == cc->pix_fmt) {
00222 score += 90000;
00223 } else {
00224 score += cc->score;
00225 }
00226 if (ap->time_base.num && rate == best_rate) {
00227 score += 70000;
00228 } else {
00229 score += best_rate / 1000;
00230 }
00231 if (score > max_score) {
00232 video_mode = video_modes.modes[i];
00233 final_width = dc1394_width;
00234 final_height = dc1394_height;
00235 final_pix_fmt = cc->pix_fmt;
00236 final_frame_rate = best_rate;
00237 max_score = score;
00238 }
00239 }
00240 if (max_score == -1) {
00241 av_log(c, AV_LOG_ERROR, "No suitable video mode / frame rate available.\n");
00242 goto out_camera;
00243 }
00244 if (ap->width && ap->height && !(ap->width == final_width && ap->height == final_height)) {
00245 av_log(c, AV_LOG_WARNING, "Requested frame size is not available, using fallback.\n");
00246 }
00247 if (ap->pix_fmt != PIX_FMT_NONE && ap->pix_fmt != final_pix_fmt) {
00248 av_log(c, AV_LOG_WARNING, "Requested pixel format is not supported, using fallback.\n");
00249 }
00250 if (ap->time_base.num && rate != final_frame_rate) {
00251 av_log(c, AV_LOG_WARNING, "Requested frame rate is not available, using fallback.\n");
00252 }
00253
00254
00255 vst = av_new_stream(c, 0);
00256 if (!vst)
00257 goto out_camera;
00258 av_set_pts_info(vst, 64, 1, 1000);
00259 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00260 vst->codec->codec_id = CODEC_ID_RAWVIDEO;
00261 vst->codec->time_base.den = final_frame_rate;
00262 vst->codec->time_base.num = 1000;
00263 vst->codec->width = final_width;
00264 vst->codec->height = final_height;
00265 vst->codec->pix_fmt = final_pix_fmt;
00266
00267
00268 av_init_packet(&dc1394->packet);
00269 dc1394->packet.size = avpicture_get_size(final_pix_fmt, final_width, final_height);
00270 dc1394->packet.stream_index = vst->index;
00271 dc1394->packet.flags |= AV_PKT_FLAG_KEY;
00272
00273 dc1394->current_frame = 0;
00274 dc1394->frame_rate = final_frame_rate;
00275
00276 vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, final_frame_rate, 1000);
00277
00278
00279 if (dc1394->camera->bmode_capable>0) {
00280 dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
00281 i = DC1394_ISO_SPEED_800;
00282 } else {
00283 i = DC1394_ISO_SPEED_400;
00284 }
00285
00286 for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
00287 res=dc1394_video_set_iso_speed(dc1394->camera, i);
00288 }
00289 if (res != DC1394_SUCCESS) {
00290 av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
00291 goto out_camera;
00292 }
00293
00294 if (dc1394_video_set_mode(dc1394->camera, video_mode) != DC1394_SUCCESS) {
00295 av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
00296 goto out_camera;
00297 }
00298
00299 if (dc1394_video_set_framerate(dc1394->camera, frame_rate) != DC1394_SUCCESS) {
00300 av_log(c, AV_LOG_ERROR, "Could not set framerate %d.\n", final_frame_rate);
00301 goto out_camera;
00302 }
00303 if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
00304 av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
00305 goto out_camera;
00306 }
00307
00308 if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
00309 av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
00310 goto out_camera;
00311 }
00312 return 0;
00313
00314 out_camera:
00315 dc1394_capture_stop(dc1394->camera);
00316 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
00317 dc1394_camera_free (dc1394->camera);
00318 out:
00319 dc1394_free(dc1394->d);
00320 return ret;
00321 }
00322
00323 static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt)
00324 {
00325 struct dc1394_data *dc1394 = c->priv_data;
00326 int res;
00327
00328
00329 if (dc1394->current_frame++) {
00330 if (dc1394_capture_enqueue(dc1394->camera, dc1394->frame) != DC1394_SUCCESS)
00331 av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame);
00332 }
00333
00334 res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame);
00335 if (res == DC1394_SUCCESS) {
00336 dc1394->packet.data = (uint8_t *)(dc1394->frame->image);
00337 dc1394->packet.pts = (dc1394->current_frame * 1000000) / (dc1394->frame_rate);
00338 res = dc1394->frame->image_bytes;
00339 } else {
00340 av_log(c, AV_LOG_ERROR, "DMA capture failed\n");
00341 dc1394->packet.data = NULL;
00342 res = -1;
00343 }
00344
00345 *pkt = dc1394->packet;
00346 return res;
00347 }
00348
00349 static int dc1394_close(AVFormatContext * context)
00350 {
00351 struct dc1394_data *dc1394 = context->priv_data;
00352
00353 dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
00354 dc1394_capture_stop(dc1394->camera);
00355 dc1394_camera_free(dc1394->camera);
00356 dc1394_free(dc1394->d);
00357
00358 return 0;
00359 }
00360
00361 AVInputFormat ff_libdc1394_demuxer = {
00362 .name = "libdc1394",
00363 .long_name = NULL_IF_CONFIG_SMALL("dc1394 A/V grab"),
00364 .priv_data_size = sizeof(struct dc1394_data),
00365 .read_header = dc1394_read_header,
00366 .read_packet = dc1394_read_packet,
00367 .read_close = dc1394_close,
00368 .flags = AVFMT_NOFILE,
00369 .priv_class = &libdc1394_class,
00370 };