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