]> git.sesse.net Git - ffmpeg/blobdiff - libavdevice/v4l2.c
lavc: update doxy to use nondeprecated API.
[ffmpeg] / libavdevice / v4l2.c
index f5baf08b42fe3890d933c1a1cd0ed3795eb2c11c..372d0901ccf9772e932d914e03f9a0f3caef4891 100644 (file)
 #include <linux/videodev2.h>
 #endif
 #include <time.h>
-#include <strings.h>
 #include "libavutil/imgutils.h"
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/avstring.h"
 
 static const int desired_video_buffers = 256;
 
@@ -71,6 +72,8 @@ struct video_data {
     char *standard;
     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 {
@@ -451,14 +454,15 @@ static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap)
     struct v4l2_standard standard;
     struct v4l2_streamparm streamparm = { 0 };
     struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe;
-    int i;
+    int i, ret;
+    AVRational framerate_q;
 
     streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 
-#if FF_API_FORMAT_PARAMETERS
-    if (ap->channel > 0)
-        s->channel = ap->channel;
-#endif
+    if (s->framerate && (ret = av_parse_video_rate(&framerate_q, s->framerate)) < 0) {
+        av_log(s1, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", s->framerate);
+        return ret;
+    }
 
     /* set tv video input */
     memset (&input, 0, sizeof (input));
@@ -476,13 +480,6 @@ static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap)
         return AVERROR(EIO);
     }
 
-#if FF_API_FORMAT_PARAMETERS
-    if (ap->standard) {
-        av_freep(&s->standard);
-        s->standard = av_strdup(ap->standard);
-    }
-#endif
-
     if (s->standard) {
         av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
                s->standard);
@@ -496,7 +493,7 @@ static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap)
                 return AVERROR(EIO);
             }
 
-            if (!strcasecmp(standard.name, s->standard)) {
+            if (!av_strcasecmp(standard.name, s->standard)) {
                 break;
             }
         }
@@ -509,47 +506,46 @@ static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap)
             return AVERROR(EIO);
         }
     }
-    av_freep(&s->standard);
 
-    if (ap->time_base.num && ap->time_base.den) {
+    if (framerate_q.num && framerate_q.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;
+               framerate_q.den, framerate_q.num);
+        tpf->numerator   = framerate_q.den;
+        tpf->denominator = framerate_q.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);
+                   framerate_q.den, framerate_q.num);
             return AVERROR(EIO);
         }
 
-        if (ap->time_base.den != tpf->denominator ||
-            ap->time_base.num != tpf->numerator) {
+        if (framerate_q.num != tpf->denominator ||
+            framerate_q.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,
+                   framerate_q.den, framerate_q.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;
+    s1->streams[0]->codec->time_base.den = tpf->denominator;
+    s1->streams[0]->codec->time_base.num = tpf->numerator;
 
     return 0;
 }
 
 static uint32_t device_try_init(AVFormatContext *s1,
-                                const AVFormatParameters *ap,
+                                enum PixelFormat pix_fmt,
                                 int *width,
                                 int *height,
                                 enum CodecID *codec_id)
 {
-    uint32_t desired_format = fmt_ff2v4l(ap->pix_fmt, s1->video_codec_id);
+    uint32_t desired_format = fmt_ff2v4l(pix_fmt, s1->video_codec_id);
 
     if (desired_format == 0 ||
         device_init(s1, width, height, desired_format) < 0) {
@@ -582,8 +578,9 @@ static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     int res = 0;
     uint32_t desired_format, capabilities;
     enum CodecID codec_id;
+    enum PixelFormat pix_fmt = PIX_FMT_NONE;
 
-    st = av_new_stream(s1, 0);
+    st = avformat_new_stream(s1, NULL);
     if (!st) {
         res = AVERROR(ENOMEM);
         goto out;
@@ -591,15 +588,14 @@ static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
 
     if (s->video_size && (res = av_parse_video_size(&s->width, &s->height, s->video_size)) < 0) {
-        av_log(s1, AV_LOG_ERROR, "Couldn't parse video size.\n");
+        av_log(s1, AV_LOG_ERROR, "Could not parse video size '%s'.\n", s->video_size);
+        goto out;
+    }
+    if (s->pixel_format && (pix_fmt = av_get_pix_fmt(s->pixel_format)) == PIX_FMT_NONE) {
+        av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n", s->pixel_format);
+        res = AVERROR(EINVAL);
         goto out;
     }
-#if FF_API_FORMAT_PARAMETERS
-    if (ap->width > 0)
-        s->width  = ap->width;
-    if (ap->height > 0)
-        s->height = ap->height;
-#endif
 
     capabilities = 0;
     s->fd = device_open(s1, &capabilities);
@@ -624,10 +620,10 @@ static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
         av_log(s1, AV_LOG_VERBOSE, "Setting frame size to %dx%d\n", s->width, s->height);
     }
 
-    desired_format = device_try_init(s1, ap, &s->width, &s->height, &codec_id);
+    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);
@@ -664,12 +660,9 @@ static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     st->codec->codec_id = codec_id;
     st->codec->width = s->width;
     st->codec->height = s->height;
-    st->codec->time_base.den = ap->time_base.den;
-    st->codec->time_base.num = ap->time_base.num;
     st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
 
 out:
-    av_freep(&s->video_size);
     return res;
 }
 
@@ -716,9 +709,11 @@ static int v4l2_read_close(AVFormatContext *s1)
 #define OFFSET(x) offsetof(struct video_data, x)
 #define DEC AV_OPT_FLAG_DECODING_PARAM
 static const AVOption options[] = {
-    { "standard", "", offsetof(struct video_data, standard), FF_OPT_TYPE_STRING, {.str = "NTSC" }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
-    { "channel",  "", offsetof(struct video_data, 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 },
+    { "standard", "", offsetof(struct video_data, standard), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
+    { "channel",  "", offsetof(struct video_data, channel),  AV_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), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
+    { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
+    { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
     { NULL },
 };
 
@@ -730,13 +725,12 @@ static const AVClass v4l2_class = {
 };
 
 AVInputFormat ff_v4l2_demuxer = {
-    "video4linux2",
-    NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
-    sizeof(struct video_data),
-    NULL,
-    v4l2_read_header,
-    v4l2_read_packet,
-    v4l2_read_close,
-    .flags = AVFMT_NOFILE,
-    .priv_class = &v4l2_class,
+    .name           = "video4linux2",
+    .long_name      = NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
+    .priv_data_size = sizeof(struct video_data),
+    .read_header    = v4l2_read_header,
+    .read_packet    = v4l2_read_packet,
+    .read_close     = v4l2_read_close,
+    .flags          = AVFMT_NOFILE,
+    .priv_class     = &v4l2_class,
 };