diff options
author | Michael Niedermayer <michaelni@gmx.at> | 2011-06-03 05:19:30 +0200 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2011-06-03 05:19:30 +0200 |
commit | 99eb31e263a24bc6c5a7a3f455a2bcb04a60e70e (patch) | |
tree | 83aab2f083fd3d6923d4e76fe0cc0c41ef7aef35 /libavdevice | |
parent | 9034001b17077e9da5205c4344eb1b88b9882f03 (diff) | |
parent | f190f676bc93a7e80344f2feeb3b9b44604d4717 (diff) | |
download | ffmpeg-99eb31e263a24bc6c5a7a3f455a2bcb04a60e70e.tar.gz |
Merge remote-tracking branch 'qatar/master'
* qatar/master: (25 commits)
Replace custom DEBUG preprocessor trickery by the standard one.
vorbis: Remove non-compiling debug statement.
vorbis: Remove pointless DEBUG #ifdef around debug output macros.
cook: Remove non-compiling debug output.
Remove pointless #ifdefs around function declarations in a header.
Replace #ifdef + av_log() combinations by av_dlog().
Replace custom debug output functions by av_dlog().
cook: Remove unused debug functions.
Remove stray extra arguments from av_dlog() invocations.
targa: fix big-endian build
v4l2: remove one forgotten use of AVFormatParameters.pix_fmt.
vfwcap: add a framerate private option.
v4l2: add a framerate private option.
libdc1394: add a framerate private option.
fbdev: add a framerate private option.
bktr: add a framerate private option.
oma: check avio_read() return value
nutdec: remove unused variable
Remove unused variables
swscale: allocate larger buffer to handle altivec overreads.
...
Conflicts:
ffmpeg.c
libavcodec/dca.c
libavcodec/dirac.c
libavcodec/error_resilience.c
libavcodec/h264.c
libavcodec/mpeg12.c
libavcodec/mpeg4videodec.c
libavcodec/mpegvideo.c
libavcodec/mpegvideo_enc.c
libavcodec/pthread.c
libavcodec/rv10.c
libavcodec/s302m.c
libavcodec/shorten.c
libavcodec/truemotion2.c
libavcodec/utils.c
libavdevice/dv1394.c
libavdevice/fbdev.c
libavdevice/libdc1394.c
libavdevice/v4l2.c
libavformat/4xm.c
libavformat/apetag.c
libavformat/asfdec.c
libavformat/avidec.c
libavformat/mmf.c
libavformat/mpeg.c
libavformat/mpegenc.c
libavformat/mpegts.c
libavformat/oggdec.c
libavformat/oggparseogm.c
libavformat/rl2.c
libavformat/rmdec.c
libavformat/rpl.c
libavformat/rtpdec_latm.c
libavformat/sauce.c
libavformat/sol.c
libswscale/utils.c
tests/ref/vsynth1/error
tests/ref/vsynth2/error
Merged-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavdevice')
-rw-r--r-- | libavdevice/bktr.c | 24 | ||||
-rw-r--r-- | libavdevice/dv1394.c | 11 | ||||
-rw-r--r-- | libavdevice/fbdev.c | 48 | ||||
-rw-r--r-- | libavdevice/libdc1394.c | 446 | ||||
-rw-r--r-- | libavdevice/v4l2.c | 32 | ||||
-rw-r--r-- | libavdevice/vfwcap.c | 20 |
6 files changed, 339 insertions, 242 deletions
diff --git a/libavdevice/bktr.c b/libavdevice/bktr.c index f04f29c3e2..dd2a2c1e7c 100644 --- a/libavdevice/bktr.c +++ b/libavdevice/bktr.c @@ -54,11 +54,10 @@ typedef struct { int video_fd; int tuner_fd; int width, height; - int frame_rate; - int frame_rate_base; uint64_t per_frame; int standard; char *video_size; /**< String describing video size, set by a private option. */ + char *framerate; /**< Set by a private option. */ } VideoData; @@ -249,8 +248,7 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap) VideoData *s = s1->priv_data; AVStream *st; int width, height; - int frame_rate; - int frame_rate_base; + AVRational fps; int ret = 0; if (ap->time_base.den <= 0) { @@ -262,14 +260,18 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap) av_log(s1, AV_LOG_ERROR, "Couldn't parse video size.\n"); goto out; } + if ((ret = av_parse_video_rate(&fps, s->framerate)) < 0) { + av_log(s1, AV_LOG_ERROR, "Couldn't parse framerate.\n"); + goto out; + } #if FF_API_FORMAT_PARAMETERS if (ap->width > 0) width = ap->width; if (ap->height > 0) height = ap->height; + if (ap->time_base.num) + fps = (AVRational){ap->time_base.den, ap->time_base.num}; #endif - frame_rate = ap->time_base.den; - frame_rate_base = ap->time_base.num; st = av_new_stream(s1, 0); if (!st) { @@ -280,17 +282,15 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap) s->width = width; s->height = height; - s->frame_rate = frame_rate; - s->frame_rate_base = frame_rate_base; - s->per_frame = ((uint64_t)1000000 * s->frame_rate_base) / s->frame_rate; + s->per_frame = ((uint64_t)1000000 * fps.den) / fps.num; st->codec->codec_type = AVMEDIA_TYPE_VIDEO; st->codec->pix_fmt = PIX_FMT_YUV420P; st->codec->codec_id = CODEC_ID_RAWVIDEO; st->codec->width = width; st->codec->height = height; - st->codec->time_base.den = frame_rate; - st->codec->time_base.num = frame_rate_base; + st->codec->time_base.den = fps.num; + st->codec->time_base.num = fps.den; #if FF_API_FORMAT_PARAMETERS if (ap->standard) { @@ -314,6 +314,7 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap) out: av_freep(&s->video_size); + av_freep(&s->framerate); return ret; } @@ -346,6 +347,7 @@ static const AVOption options[] = { { "PALM", "", 0, FF_OPT_TYPE_CONST, {.dbl = PALM}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" }, { "NTSCJ", "", 0, FF_OPT_TYPE_CONST, {.dbl = NTSCJ}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" }, { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = "vga"}, 0, 0, DEC }, + { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC }, { NULL }, }; diff --git a/libavdevice/dv1394.c b/libavdevice/dv1394.c index 4a84383264..63eff2aec8 100644 --- a/libavdevice/dv1394.c +++ b/libavdevice/dv1394.c @@ -33,9 +33,6 @@ #include "libavutil/log.h" #include "libavutil/opt.h" #include "avdevice.h" - -#undef DV1394_DEBUG - #include "libavformat/dv.h" #include "dv1394.h" @@ -177,15 +174,13 @@ restart_poll: av_log(context, AV_LOG_ERROR, "Failed to get status: %s\n", strerror(errno)); return AVERROR(EIO); } -#ifdef DV1394_DEBUG - av_log(context, AV_LOG_DEBUG, "DV1394: status\n" + av_dlog(context, "DV1394: status\n" "\tactive_frame\t%d\n" "\tfirst_clear_frame\t%d\n" "\tn_clear_frames\t%d\n" "\tdropped_frames\t%d\n", s.active_frame, s.first_clear_frame, s.n_clear_frames, s.dropped_frames); -#endif dv->avail = s.n_clear_frames; dv->index = s.first_clear_frame; @@ -200,10 +195,8 @@ restart_poll: } } -#ifdef DV1394_DEBUG - av_log(context, AV_LOG_DEBUG, "index %d, avail %d, done %d\n", dv->index, dv->avail, + av_dlog(context, "index %d, avail %d, done %d\n", dv->index, dv->avail, dv->done); -#endif size = dv_produce_packet(dv->dv_demux, pkt, dv->ring + (dv->index * DV1394_PAL_FRAME_SIZE), diff --git a/libavdevice/fbdev.c b/libavdevice/fbdev.c index 19bf5ad5ef..0d41b5d6ba 100644 --- a/libavdevice/fbdev.c +++ b/libavdevice/fbdev.c @@ -37,7 +37,10 @@ #include <time.h> #include <linux/fb.h> +#include "libavutil/log.h" #include "libavutil/mem.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" #include "libavutil/pixdesc.h" #include "avdevice.h" @@ -74,8 +77,10 @@ static enum PixelFormat get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *var } typedef struct { + AVClass *class; ///< class for private options int frame_size; ///< size in bytes of a grabbed frame - AVRational time_base; ///< time base + AVRational fps; ///< framerate + char *framerate; ///< framerate string set by a private option int64_t time_frame; ///< time for the next frame to output (in 1/1000000 units) int fd; ///< framebuffer device file descriptor @@ -97,16 +102,21 @@ av_cold static int fbdev_read_header(AVFormatContext *avctx, enum PixelFormat pix_fmt; int ret, flags = O_RDONLY; + ret = av_parse_video_rate(&fbdev->fps, fbdev->framerate); + av_freep(&fbdev->framerate); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Couldn't parse framerate.\n"); + return ret; + } +#if FF_API_FORMAT_PARAMETERS + if (ap->time_base.num) + fbdev->fps = (AVRational){ap->time_base.den, ap->time_base.num}; +#endif + if (!(st = av_new_stream(avctx, 0))) return AVERROR(ENOMEM); av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in microseconds */ - if (ap->time_base.den <= 0) { - av_log(avctx, AV_LOG_ERROR, "Invalid time base %d/%d\n", - ap->time_base.num, ap->time_base.den); - return AVERROR(EINVAL); - } - /* NONBLOCK is ignored by the fbdev driver, only set for consistency */ if (avctx->flags & AVFMT_FLAG_NONBLOCK) flags |= O_NONBLOCK; @@ -146,7 +156,6 @@ av_cold static int fbdev_read_header(AVFormatContext *avctx, fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3; fbdev->frame_linesize = fbdev->width * fbdev->bytes_per_pixel; fbdev->frame_size = fbdev->frame_linesize * fbdev->heigth; - fbdev->time_base = ap->time_base; fbdev->time_frame = AV_NOPTS_VALUE; fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0); if (fbdev->data == MAP_FAILED) { @@ -162,13 +171,13 @@ av_cold static int fbdev_read_header(AVFormatContext *avctx, st->codec->pix_fmt = pix_fmt; st->codec->time_base = ap->time_base; st->codec->bit_rate = - fbdev->width * fbdev->heigth * fbdev->bytes_per_pixel / av_q2d(ap->time_base) * 8; + fbdev->width * fbdev->heigth * fbdev->bytes_per_pixel * av_q2d(fbdev->fps) * 8; av_log(avctx, AV_LOG_INFO, - "w:%d h:%d bpp:%d pixfmt:%s tb:%d/%d bit_rate:%d\n", + "w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%d\n", fbdev->width, fbdev->heigth, fbdev->varinfo.bits_per_pixel, av_pix_fmt_descriptors[pix_fmt].name, - ap->time_base.num, ap->time_base.den, + fbdev->fps.num, fbdev->fps.den, st->codec->bit_rate); return 0; @@ -196,7 +205,7 @@ static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt) "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n", fbdev->time_frame, curtime, delay); if (delay <= 0) { - fbdev->time_frame += INT64_C(1000000) * av_q2d(fbdev->time_base); + fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->fps); break; } if (avctx->flags & AVFMT_FLAG_NONBLOCK) @@ -240,6 +249,20 @@ av_cold static int fbdev_read_close(AVFormatContext *avctx) return 0; } +#define OFFSET(x) offsetof(FBDevContext, x) +#define DEC AV_OPT_FLAG_DECODING_PARAM +static const AVOption options[] = { + { "framerate","", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC }, + { NULL }, +}; + +static const AVClass fbdev_class = { + .class_name = "fbdev indev", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + AVInputFormat ff_fbdev_demuxer = { .name = "fbdev", .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"), @@ -248,4 +271,5 @@ AVInputFormat ff_fbdev_demuxer = { .read_packet = fbdev_read_packet, .read_close = fbdev_read_close, .flags = AVFMT_NOFILE, + .priv_class = &fbdev_class, }; diff --git a/libavdevice/libdc1394.c b/libavdevice/libdc1394.c index 5a9043bc24..be934ea6df 100644 --- a/libavdevice/libdc1394.c +++ b/libavdevice/libdc1394.c @@ -2,7 +2,6 @@ * IIDC1394 grab interface (uses libdc1394 and libraw1394) * Copyright (c) 2004 Roman Shaposhnik * Copyright (c) 2008 Alessandro Sappia - * Copyright (c) 2011 Martin Lambers * * This file is part of FFmpeg. * @@ -22,47 +21,63 @@ */ #include "config.h" +#include "libavformat/avformat.h" #include "libavutil/log.h" #include "libavutil/opt.h" -#include "avdevice.h" - -#include <stdlib.h> -#include <string.h> #include "libavutil/parseutils.h" #include "libavutil/pixdesc.h" +#if HAVE_LIBDC1394_2 #include <dc1394/dc1394.h> +#elif HAVE_LIBDC1394_1 +#include <libraw1394/raw1394.h> +#include <libdc1394/dc1394_control.h> + +#define DC1394_VIDEO_MODE_320x240_YUV422 MODE_320x240_YUV422 +#define DC1394_VIDEO_MODE_640x480_YUV411 MODE_640x480_YUV411 +#define DC1394_VIDEO_MODE_640x480_YUV422 MODE_640x480_YUV422 +#define DC1394_FRAMERATE_1_875 FRAMERATE_1_875 +#define DC1394_FRAMERATE_3_75 FRAMERATE_3_75 +#define DC1394_FRAMERATE_7_5 FRAMERATE_7_5 +#define DC1394_FRAMERATE_15 FRAMERATE_15 +#define DC1394_FRAMERATE_30 FRAMERATE_30 +#define DC1394_FRAMERATE_60 FRAMERATE_60 +#define DC1394_FRAMERATE_120 FRAMERATE_120 +#define DC1394_FRAMERATE_240 FRAMERATE_240 +#endif #undef free typedef struct dc1394_data { AVClass *class; +#if HAVE_LIBDC1394_1 + raw1394handle_t handle; + dc1394_cameracapture camera; + int channel; +#elif HAVE_LIBDC1394_2 dc1394_t *d; dc1394camera_t *camera; dc1394video_frame_t *frame; +#endif int current_frame; - int fps; + int frame_rate; /**< frames per 1000 seconds (fps * 1000) */ char *video_size; /**< String describing video size, set by a private option. */ char *pixel_format; /**< Set by a private option. */ + char *framerate; /**< Set by a private option. */ AVPacket packet; } dc1394_data; -/* The list of color codings that we support. - * We assume big endian for the dc1394 16bit modes: libdc1394 never sets the - * flag little_endian in dc1394video_frame_t. */ -struct dc1394_color_coding { - int pix_fmt; - int score; - uint32_t coding; -} dc1394_color_codings[] = { - { PIX_FMT_GRAY16BE, 1000, DC1394_COLOR_CODING_MONO16 }, - { PIX_FMT_RGB48BE, 1100, DC1394_COLOR_CODING_RGB16 }, - { PIX_FMT_GRAY8, 1200, DC1394_COLOR_CODING_MONO8 }, - { PIX_FMT_RGB24, 1300, DC1394_COLOR_CODING_RGB8 }, - { PIX_FMT_UYYVYY411, 1400, DC1394_COLOR_CODING_YUV411 }, - { PIX_FMT_UYVY422, 1500, DC1394_COLOR_CODING_YUV422 }, - { PIX_FMT_NONE, 0, 0 } /* gotta be the last one */ +struct dc1394_frame_format { + int width; + int height; + enum PixelFormat pix_fmt; + int frame_size_id; +} dc1394_frame_formats[] = { + { 320, 240, PIX_FMT_UYVY422, DC1394_VIDEO_MODE_320x240_YUV422 }, + { 640, 480, PIX_FMT_UYYVYY411, DC1394_VIDEO_MODE_640x480_YUV411 }, + { 640, 480, PIX_FMT_UYVY422, DC1394_VIDEO_MODE_640x480_YUV422 }, + { 0, 0, 0, 0 } /* gotta be the last one */ }; struct dc1394_frame_rate { @@ -83,8 +98,12 @@ struct dc1394_frame_rate { #define OFFSET(x) offsetof(dc1394_data, x) #define DEC AV_OPT_FLAG_DECODING_PARAM static const AVOption options[] = { +#if HAVE_LIBDC1394_1 + { "channel", "", offsetof(dc1394_data, channel), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, +#endif { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = "qvga"}, 0, 0, DEC }, { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = "uyvy422"}, 0, 0, DEC }, + { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC }, { NULL }, }; @@ -95,183 +114,220 @@ static const AVClass libdc1394_class = { .version = LIBAVUTIL_VERSION_INT, }; -static int dc1394_read_header(AVFormatContext *c, AVFormatParameters * ap) + +static inline int dc1394_read_common(AVFormatContext *c, AVFormatParameters *ap, + struct dc1394_frame_format **select_fmt, struct dc1394_frame_rate **select_fps) { dc1394_data* dc1394 = c->priv_data; - AVStream *vst; - const struct dc1394_color_coding *cc; - const struct dc1394_frame_rate *fr; - dc1394camera_list_t *list; - dc1394video_modes_t video_modes; - dc1394video_mode_t video_mode; - dc1394framerates_t frame_rates; - dc1394framerate_t frame_rate; - uint32_t dc1394_width, dc1394_height, dc1394_color_coding; - int rate, best_rate; - int score, max_score; - int final_width, final_height, final_pix_fmt, final_frame_rate; - int res, i, j; - int ret=-1; - - /* Now let us prep the hardware. */ - dc1394->d = dc1394_new(); - dc1394_camera_enumerate (dc1394->d, &list); - if ( !list || list->num == 0) { - av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n"); + AVStream* vst; + struct dc1394_frame_format *fmt; + struct dc1394_frame_rate *fps; + enum PixelFormat pix_fmt; + int width, height; + AVRational framerate; + int ret = 0; + + if ((pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == PIX_FMT_NONE) { + av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format); + ret = AVERROR(EINVAL); goto out; } - /* FIXME: To select a specific camera I need to search in list its guid */ - dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid); - if (list->num > 1) { - av_log(c, AV_LOG_INFO, "Working with the first camera found\n"); - } - - /* Freeing list of cameras */ - dc1394_camera_free_list (list); - - /* Get the list of video modes supported by the camera. */ - res = dc1394_video_get_supported_modes (dc1394->camera, &video_modes); - if (res != DC1394_SUCCESS) { - av_log(c, AV_LOG_ERROR, "Could not get video formats.\n"); - goto out_camera; - } - - if (dc1394->pixel_format) { - if ((ap->pix_fmt = av_get_pix_fmt(dc1394->pixel_format)) == PIX_FMT_NONE) { - av_log(c, AV_LOG_ERROR, "No such pixel format: %s.\n", dc1394->pixel_format); - ret = AVERROR(EINVAL); - goto out; - } - } - - if (dc1394->video_size) { - if ((ret = av_parse_video_size(&ap->width, &ap->height, dc1394->video_size)) < 0) { - av_log(c, AV_LOG_ERROR, "Couldn't parse video size.\n"); - goto out; - } - } - - /* Choose the best mode. */ - rate = (ap->time_base.num ? av_rescale(1000, ap->time_base.den, ap->time_base.num) : -1); - max_score = -1; - for (i = 0; i < video_modes.num; i++) { - if (video_modes.modes[i] == DC1394_VIDEO_MODE_EXIF - || (video_modes.modes[i] >= DC1394_VIDEO_MODE_FORMAT7_MIN - && video_modes.modes[i] <= DC1394_VIDEO_MODE_FORMAT7_MAX)) { - /* These modes are currently not supported as they would require - * much more work. For the remaining modes, the functions - * dc1394_get_image_size_from_video_mode and - * dc1394_get_color_coding_from_video_mode do not need to query the - * camera, and thus cannot fail. */ - continue; - } - dc1394_get_color_coding_from_video_mode (NULL, video_modes.modes[i], - &dc1394_color_coding); - for (cc = dc1394_color_codings; cc->pix_fmt != PIX_FMT_NONE; cc++) - if (cc->coding == dc1394_color_coding) - break; - if (cc->pix_fmt == PIX_FMT_NONE) { - /* We currently cannot handle this color coding. */ - continue; - } - /* Here we know that the mode is supported. Get its frame size and the list - * of frame rates supported by the camera for this mode. This list is sorted - * in ascending order according to libdc1394 example programs. */ - dc1394_get_image_size_from_video_mode (NULL, video_modes.modes[i], - &dc1394_width, &dc1394_height); - res = dc1394_video_get_supported_framerates (dc1394->camera, video_modes.modes[i], - &frame_rates); - if (res != DC1394_SUCCESS || frame_rates.num == 0) { - av_log(c, AV_LOG_ERROR, "Cannot get frame rates for video mode.\n"); - goto out_camera; - } - /* Choose the best frame rate. */ - best_rate = -1; - for (j = 0; j < frame_rates.num; j++) { - for (fr = dc1394_frame_rates; fr->frame_rate; fr++) { - if (fr->frame_rate_id == frame_rates.framerates[j]) { - break; - } - } - if (!fr->frame_rate) { - /* This frame rate is not supported. */ - continue; - } - best_rate = fr->frame_rate; - frame_rate = fr->frame_rate_id; - if (ap->time_base.num && rate == fr->frame_rate) { - /* This is the requested frame rate. */ - break; - } - } - if (best_rate == -1) { - /* No supported rate found. */ - continue; - } - /* Here we know that both the mode and the rate are supported. Compute score. */ - if (ap->width && ap->height - && (dc1394_width == ap->width && dc1394_height == ap->height)) { - score = 110000; - } else { - score = dc1394_width * 10; // 1600 - 16000 - } - if (ap->pix_fmt == cc->pix_fmt) { - score += 90000; - } else { - score += cc->score; // 1000 - 1500 - } - if (ap->time_base.num && rate == best_rate) { - score += 70000; - } else { - score += best_rate / 1000; // 1 - 240 - } - if (score > max_score) { - video_mode = video_modes.modes[i]; - final_width = dc1394_width; - final_height = dc1394_height; - final_pix_fmt = cc->pix_fmt; - final_frame_rate = best_rate; - max_score = score; - } - } - if (max_score == -1) { - av_log(c, AV_LOG_ERROR, "No suitable video mode / frame rate available.\n"); - goto out_camera; - } - if (ap->width && ap->height && !(ap->width == final_width && ap->height == final_height)) { - av_log(c, AV_LOG_WARNING, "Requested frame size is not available, using fallback.\n"); + if ((ret = av_parse_video_size(&width, &height, dc1394->video_size)) < 0) { + av_log(c, AV_LOG_ERROR, "Couldn't parse video size.\n"); + goto out; } - if (ap->pix_fmt != PIX_FMT_NONE && ap->pix_fmt != final_pix_fmt) { - av_log(c, AV_LOG_WARNING, "Requested pixel format is not supported, using fallback.\n"); + if ((ret = av_parse_video_rate(&framerate, dc1394->framerate)) < 0) { + av_log(c, AV_LOG_ERROR, "Couldn't parse framerate.\n"); + goto out; } - if (ap->time_base.num && rate != final_frame_rate) { - av_log(c, AV_LOG_WARNING, "Requested frame rate is not available, using fallback.\n"); +#if FF_API_FORMAT_PARAMETERS + if (ap->width > 0) + width = ap->width; + if (ap->height > 0) + height = ap->height; + if (ap->pix_fmt) + pix_fmt = ap->pix_fmt; + if (ap->time_base.num) + framerate = (AVRational){ap->time_base.den, ap->time_base.num}; +#endif + dc1394->frame_rate = av_rescale(1000, framerate.num, framerate.den); + + for (fmt = dc1394_frame_formats; fmt->width; fmt++) + if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height) + break; + + for (fps = dc1394_frame_rates; fps->frame_rate; fps++) + if (fps->frame_rate == dc1394->frame_rate) + break; + + if (!fps->frame_rate || !fmt->width) { + 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), + width, height, dc1394->frame_rate); + ret = AVERROR(EINVAL); + goto out; } /* create a video stream */ vst = av_new_stream(c, 0); - if (!vst) - goto out_camera; + if (!vst) { + ret = AVERROR(ENOMEM); + goto out; + } av_set_pts_info(vst, 64, 1, 1000); vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; vst->codec->codec_id = CODEC_ID_RAWVIDEO; - vst->codec->time_base.den = final_frame_rate; - vst->codec->time_base.num = 1000; - vst->codec->width = final_width; - vst->codec->height = final_height; - vst->codec->pix_fmt = final_pix_fmt; + vst->codec->time_base.den = framerate.num; + vst->codec->time_base.num = framerate.den; + vst->codec->width = fmt->width; + vst->codec->height = fmt->height; + vst->codec->pix_fmt = fmt->pix_fmt; /* packet init */ av_init_packet(&dc1394->packet); - dc1394->packet.size = avpicture_get_size(final_pix_fmt, final_width, final_height); + dc1394->packet.size = avpicture_get_size(fmt->pix_fmt, fmt->width, fmt->height); dc1394->packet.stream_index = vst->index; dc1394->packet.flags |= AV_PKT_FLAG_KEY; dc1394->current_frame = 0; - dc1394->fps = final_frame_rate; - vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, final_frame_rate, 1000); + vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000); + *select_fps = fps; + *select_fmt = fmt; +out: + av_freep(&dc1394->video_size); + av_freep(&dc1394->pixel_format); + av_freep(&dc1394->framerate); + return ret; +} + +#if HAVE_LIBDC1394_1 +static int dc1394_v1_read_header(AVFormatContext *c, AVFormatParameters * ap) +{ + dc1394_data* dc1394 = c->priv_data; + AVStream* vst; + nodeid_t* camera_nodes; + int res; + struct dc1394_frame_format *fmt = NULL; + struct dc1394_frame_rate *fps = NULL; + + if (dc1394_read_common(c,ap,&fmt,&fps) != 0) + return -1; + +#if FF_API_FORMAT_PARAMETERS + if (ap->channel) + dc1394->channel = ap->channel; +#endif + + /* Now let us prep the hardware. */ + dc1394->handle = dc1394_create_handle(0); /* FIXME: gotta have ap->port */ + if (!dc1394->handle) { + av_log(c, AV_LOG_ERROR, "Can't acquire dc1394 handle on port %d\n", 0 /* ap->port */); + goto out; + } + camera_nodes = dc1394_get_camera_nodes(dc1394->handle, &res, 1); + if (!camera_nodes || camera_nodes[dc1394->channel] == DC1394_NO_CAMERA) { + av_log(c, AV_LOG_ERROR, "There's no IIDC camera on the channel %d\n", dc1394->channel); + goto out_handle; + } + res = dc1394_dma_setup_capture(dc1394->handle, camera_nodes[dc1394->channel], + 0, + FORMAT_VGA_NONCOMPRESSED, + fmt->frame_size_id, + SPEED_400, + fps->frame_rate_id, 8, 1, + c->filename, + &dc1394->camera); + dc1394_free_camera_nodes(camera_nodes); + if (res != DC1394_SUCCESS) { + av_log(c, AV_LOG_ERROR, "Can't prepare camera for the DMA capture\n"); + goto out_handle; + } + + res = dc1394_start_iso_transmission(dc1394->handle, dc1394->camera.node); + if (res != DC1394_SUCCESS) { + av_log(c, AV_LOG_ERROR, "Can't start isochronous transmission\n"); + goto out_handle_dma; + } + + return 0; + +out_handle_dma: + dc1394_dma_unlisten(dc1394->handle, &dc1394->camera); + dc1394_dma_release_camera(dc1394->handle, &dc1394->camera); +out_handle: + dc1394_destroy_handle(dc1394->handle); +out: + return -1; +} + +static int dc1394_v1_read_packet(AVFormatContext *c, AVPacket *pkt) +{ + struct dc1394_data *dc1394 = c->priv_data; + int res; + + /* discard stale frame */ + if (dc1394->current_frame++) { + if (dc1394_dma_done_with_buffer(&dc1394->camera) != DC1394_SUCCESS) + av_log(c, AV_LOG_ERROR, "failed to release %d frame\n", dc1394->current_frame); + } + + res = dc1394_dma_single_capture(&dc1394->camera); + + if (res == DC1394_SUCCESS) { + dc1394->packet.data = (uint8_t *)(dc1394->camera.capture_buffer); + dc1394->packet.pts = (dc1394->current_frame * 1000000) / dc1394->frame_rate; + res = dc1394->packet.size; + } else { + av_log(c, AV_LOG_ERROR, "DMA capture failed\n"); + dc1394->packet.data = NULL; + res = -1; + } + + *pkt = dc1394->packet; + return res; +} + +static int dc1394_v1_close(AVFormatContext * context) +{ + struct dc1394_data *dc1394 = context->priv_data; + + dc1394_stop_iso_transmission(dc1394->handle, dc1394->camera.node); + dc1394_dma_unlisten(dc1394->handle, &dc1394->camera); + dc1394_dma_release_camera(dc1394->handle, &dc1394->camera); + dc1394_destroy_handle(dc1394->handle); + + return 0; +} + +#elif HAVE_LIBDC1394_2 +static int dc1394_v2_read_header(AVFormatContext *c, AVFormatParameters * ap) +{ + dc1394_data* dc1394 = c->priv_data; + dc1394camera_list_t *list; + int res, i; + struct dc1394_frame_format *fmt = NULL; + struct dc1394_frame_rate *fps = NULL; + + if (dc1394_read_common(c,ap,&fmt,&fps) != 0) + return -1; + + /* Now let us prep the hardware. */ + dc1394->d = dc1394_new(); + dc1394_camera_enumerate (dc1394->d, &list); + if ( !list || list->num == 0) { + av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n"); + goto out; + } + + /* FIXME: To select a specific camera I need to search in list its guid */ + dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid); + if (list->num > 1) { + av_log(c, AV_LOG_INFO, "Working with the first camera found\n"); + } + + /* Freeing list of cameras */ + dc1394_camera_free_list (list); /* Select MAX Speed possible from the cam */ if (dc1394->camera->bmode_capable>0) { @@ -289,13 +345,13 @@ static int dc1394_read_header(AVFormatContext *c, AVFormatParameters * ap) goto out_camera; } - if (dc1394_video_set_mode(dc1394->camera, video_mode) != DC1394_SUCCESS) { + if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) { av_log(c, AV_LOG_ERROR, "Couldn't set video format\n"); goto out_camera; } - if (dc1394_video_set_framerate(dc1394->camera, frame_rate) != DC1394_SUCCESS) { - av_log(c, AV_LOG_ERROR, "Could not set framerate %d.\n", final_frame_rate); + if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) { + av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate); goto out_camera; } if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) { @@ -314,13 +370,11 @@ out_camera: dc1394_video_set_transmission(dc1394->camera, DC1394_OFF); dc1394_camera_free (dc1394->camera); out: - av_freep(&dc1394->video_size); - av_freep(&dc1394->pixel_format); dc1394_free(dc1394->d); - return ret; + return -1; } -static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt) +static int dc1394_v2_read_packet(AVFormatContext *c, AVPacket *pkt) { struct dc1394_data *dc1394 = c->priv_data; int res; @@ -334,7 +388,7 @@ static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt) res = dc1394_capture_dequeue(dc1394->camera, DC1394_CAPTURE_POLICY_WAIT, &dc1394->frame); if (res == DC1394_SUCCESS) { dc1394->packet.data = (uint8_t *)(dc1394->frame->image); - dc1394->packet.pts = (dc1394->current_frame * 1000000) / (dc1394->fps); + dc1394->packet.pts = (dc1394->current_frame * 1000000) / (dc1394->frame_rate); res = dc1394->frame->image_bytes; } else { av_log(c, AV_LOG_ERROR, "DMA capture failed\n"); @@ -346,7 +400,7 @@ static int dc1394_read_packet(AVFormatContext *c, AVPacket *pkt) return res; } -static int dc1394_close(AVFormatContext * context) +static int dc1394_v2_close(AVFormatContext * context) { struct dc1394_data *dc1394 = context->priv_data; @@ -360,11 +414,25 @@ static int dc1394_close(AVFormatContext * context) AVInputFormat ff_libdc1394_demuxer = { .name = "libdc1394", - .long_name = NULL_IF_CONFIG_SMALL("dc1394 A/V grab"), + .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.2 A/V grab"), + .priv_data_size = sizeof(struct dc1394_data), + .read_header = dc1394_v2_read_header, + .read_packet = dc1394_v2_read_packet, + .read_close = dc1394_v2_close, + .flags = AVFMT_NOFILE, + .priv_class = &libdc1394_class, +}; + +#endif +#if HAVE_LIBDC1394_1 +AVInputFormat ff_libdc1394_demuxer = { + .name = "libdc1394", + .long_name = NULL_IF_CONFIG_SMALL("dc1394 v.1 A/V grab"), .priv_data_size = sizeof(struct dc1394_data), - .read_header = dc1394_read_header, - .read_packet = dc1394_read_packet, - .read_close = dc1394_close, + .read_header = dc1394_v1_read_header, + .read_packet = dc1394_v1_read_packet, + .read_close = dc1394_v1_close, .flags = AVFMT_NOFILE, .priv_class = &libdc1394_class, }; +#endif diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c index 605b7b748e..08cf0096f6 100644 --- a/libavdevice/v4l2.c +++ b/libavdevice/v4l2.c @@ -76,6 +76,7 @@ struct video_data { int channel; char *video_size; /**< String describing video size, set by a private option. */ char *pixel_format; /**< Set by a private option. */ + char *framerate; /**< Set by a private option. */ }; struct buff_data { @@ -438,12 +439,19 @@ static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap) struct v4l2_streamparm streamparm = {0}; struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe; int i, ret; + AVRational fps; streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (s->framerate && (ret = av_parse_video_rate(&fps, s->framerate)) < 0) { + av_log(s1, AV_LOG_ERROR, "Couldn't parse framerate.\n"); + return ret; + } #if FF_API_FORMAT_PARAMETERS if (ap->channel > 0) s->channel = ap->channel; + if (ap->time_base.num) + fps = (AVRational){ap->time_base.den, ap->time_base.num}; #endif /* set tv video input */ @@ -492,34 +500,32 @@ static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap) } } - if (ap->time_base.num && ap->time_base.den) { + if (fps.num && fps.den) { av_log(s1, AV_LOG_DEBUG, "Setting time per frame to %d/%d\n", - ap->time_base.num, ap->time_base.den); - tpf->numerator = ap->time_base.num; - tpf->denominator = ap->time_base.den; + fps.den, fps.num); + tpf->numerator = fps.den; + tpf->denominator = fps.num; if (ioctl(s->fd, VIDIOC_S_PARM, &streamparm) != 0) { av_log(s1, AV_LOG_ERROR, "ioctl set time per frame(%d/%d) failed\n", - ap->time_base.num, ap->time_base.den); + fps.den, fps.num); return AVERROR(EIO); } - if (ap->time_base.den != tpf->denominator || - ap->time_base.num != tpf->numerator) { + if (fps.num != tpf->denominator || + fps.den != tpf->numerator) { av_log(s1, AV_LOG_INFO, "The driver changed the time per frame from %d/%d to %d/%d\n", - ap->time_base.num, ap->time_base.den, + fps.den, fps.num, tpf->numerator, tpf->denominator); } } else { - /* if timebase value is not set in ap, read the timebase value from the driver */ + /* if timebase value is not set, read the timebase value from the driver */ if (ioctl(s->fd, VIDIOC_G_PARM, &streamparm) != 0) { av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_PARM): %s\n", strerror(errno)); return AVERROR(errno); } } - ap->time_base.num = tpf->numerator; - ap->time_base.den = tpf->denominator; return 0; } @@ -616,7 +622,7 @@ static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap) desired_format = device_try_init(s1, pix_fmt, &s->width, &s->height, &codec_id); if (desired_format == 0) { av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for " - "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, ap->pix_fmt); + "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, pix_fmt); close(s->fd); res = AVERROR(EIO); @@ -660,6 +666,7 @@ out: av_freep(&s->video_size); av_freep(&s->pixel_format); av_freep(&s->standard); + av_freep(&s->framerate); return res; } @@ -711,6 +718,7 @@ static const AVOption options[] = { { "channel", "", OFFSET(channel), FF_OPT_TYPE_INT, {.dbl = 0 }, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, + { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, { NULL }, }; diff --git a/libavdevice/vfwcap.c b/libavdevice/vfwcap.c index 75841d8acb..49fa078dd8 100644 --- a/libavdevice/vfwcap.c +++ b/libavdevice/vfwcap.c @@ -26,8 +26,6 @@ #include <vfw.h> #include "avdevice.h" -//#define DEBUG_VFW - /* Defines for VFW missing from MinGW. * Remove this when MinGW incorporates them. */ #define HWND_MESSAGE ((HWND)-3) @@ -43,6 +41,7 @@ struct vfw_ctx { unsigned int curbufsize; unsigned int frame_num; char *video_size; /**< A string describing video size, set by a private option. */ + char *framerate; /**< Set by a private option. */ }; static enum PixelFormat vfw_pixfmt(DWORD biCompression, WORD biBitCount) @@ -119,7 +118,7 @@ static void dump_captureparms(AVFormatContext *s, CAPTUREPARMS *cparms) static void dump_videohdr(AVFormatContext *s, VIDEOHDR *vhdr) { -#ifdef DEBUG_VFW +#ifdef DEBUG av_log(s, AV_LOG_DEBUG, "VIDEOHDR\n"); dstruct(s, vhdr, lpData, "p"); dstruct(s, vhdr, dwBufferLength, "lu"); @@ -234,6 +233,7 @@ static int vfw_read_close(AVFormatContext *s) } av_freep(&ctx->video_size); + av_freep(&ctx->framerate); return 0; } @@ -250,6 +250,7 @@ static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap) DWORD biCompression; WORD biBitCount; int ret; + AVRational fps; if (!strcmp(s->filename, "list")) { for (devnum = 0; devnum <= 9; devnum++) { @@ -267,10 +268,10 @@ static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap) return AVERROR(EIO); } - if(!ap->time_base.den) { - av_log(s, AV_LOG_ERROR, "A time base must be specified.\n"); - return AVERROR(EIO); - } +#if FF_API_FORMAT_PARAMETERS + if (ap->time_base.num) + fps = (AVRational){ap->time_base.den, ap->time_base.num}; +#endif ctx->hwnd = capCreateCaptureWindow(NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0); if(!ctx->hwnd) { @@ -369,7 +370,7 @@ static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap) cparms.fYield = 1; // Spawn a background thread cparms.dwRequestMicroSecPerFrame = - (ap->time_base.num*1000000) / ap->time_base.den; + (fps.den*1000000) / fps.num; cparms.fAbortLeftMouse = 0; cparms.fAbortRightMouse = 0; cparms.fCaptureAudio = 0; @@ -381,7 +382,7 @@ static int vfw_read_header(AVFormatContext *s, AVFormatParameters *ap) goto fail_io; codec = st->codec; - codec->time_base = ap->time_base; + codec->time_base = (AVRational){fps.den, fps.num}; codec->codec_type = AVMEDIA_TYPE_VIDEO; codec->width = bi->bmiHeader.biWidth; codec->height = bi->bmiHeader.biHeight; @@ -469,6 +470,7 @@ static int vfw_read_packet(AVFormatContext *s, AVPacket *pkt) #define DEC AV_OPT_FLAG_DECODING_PARAM static const AVOption options[] = { { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, + { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "ntsc"}, 0, 0, DEC }, { NULL }, }; |