Browse code

lavd: add v4l2 outdev.

Clément Bœsch authored on 2013/05/20 06:36:10
Showing 7 changed files
... ...
@@ -51,6 +51,7 @@ version <next>:
51 51
 - Apple Intermediate Codec decoder
52 52
 - Escape 130 video decoder
53 53
 - FTP protocol support
54
+- V4L2 output device
54 55
 
55 56
 
56 57
 version 1.2:
... ...
@@ -2079,6 +2079,7 @@ sndio_indev_deps="sndio_h"
2079 2079
 sndio_outdev_deps="sndio_h"
2080 2080
 v4l_indev_deps="linux_videodev_h"
2081 2081
 v4l2_indev_deps_any="linux_videodev2_h sys_videoio_h"
2082
+v4l2_outdev_deps_any="linux_videodev2_h sys_videoio_h"
2082 2083
 vfwcap_indev_deps="capCreateCaptureWindow vfwcap_defines"
2083 2084
 vfwcap_indev_extralibs="-lavicap32"
2084 2085
 x11grab_indev_deps="x11grab"
... ...
@@ -1000,7 +1000,7 @@ performance on systems without hardware floating point support).
1000 1000
 @item OSS               @tab X      @tab X
1001 1001
 @item Pulseaudio        @tab X      @tab
1002 1002
 @item SDL               @tab        @tab X
1003
-@item Video4Linux2      @tab X      @tab
1003
+@item Video4Linux2      @tab X      @tab X
1004 1004
 @item VfW capture       @tab X      @tab
1005 1005
 @item X11 grabbing      @tab X      @tab
1006 1006
 @end multitable
... ...
@@ -33,6 +33,7 @@ OBJS-$(CONFIG_SDL_OUTDEV)                += sdl.o
33 33
 OBJS-$(CONFIG_SNDIO_INDEV)               += sndio_common.o sndio_dec.o
34 34
 OBJS-$(CONFIG_SNDIO_OUTDEV)              += sndio_common.o sndio_enc.o
35 35
 OBJS-$(CONFIG_V4L2_INDEV)                += v4l2.o v4l2-common.o timefilter.o
36
+OBJS-$(CONFIG_V4L2_OUTDEV)               += v4l2enc.o v4l2-common.o
36 37
 OBJS-$(CONFIG_V4L_INDEV)                 += v4l.o
37 38
 OBJS-$(CONFIG_VFWCAP_INDEV)              += vfwcap.o
38 39
 OBJS-$(CONFIG_X11GRAB_INDEV)             += x11grab.o
... ...
@@ -60,7 +60,7 @@ void avdevice_register_all(void)
60 60
     REGISTER_INDEV   (PULSE,            pulse);
61 61
     REGISTER_OUTDEV  (SDL,              sdl);
62 62
     REGISTER_INOUTDEV(SNDIO,            sndio);
63
-    REGISTER_INDEV   (V4L2,             v4l2);
63
+    REGISTER_INOUTDEV(V4L2,             v4l2);
64 64
 //    REGISTER_INDEV   (V4L,              v4l
65 65
     REGISTER_INDEV   (VFWCAP,           vfwcap);
66 66
     REGISTER_INDEV   (X11GRAB,          x11grab);
67 67
new file mode 100644
... ...
@@ -0,0 +1,109 @@
0
+/*
1
+ * Copyright (c) 2013 Clément Bœsch
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "v4l2-common.h"
21
+#include "avdevice.h"
22
+
23
+typedef struct {
24
+    int fd;
25
+} V4L2Context;
26
+
27
+static av_cold int write_header(AVFormatContext *s1)
28
+{
29
+    int res = 0, flags = O_RDWR;
30
+    struct v4l2_format fmt = {
31
+        .type = V4L2_BUF_TYPE_VIDEO_OUTPUT
32
+    };
33
+    V4L2Context *s = s1->priv_data;
34
+    AVCodecContext *enc_ctx;
35
+    uint32_t v4l2_pixfmt;
36
+
37
+    if (s1->flags & AVFMT_FLAG_NONBLOCK)
38
+        flags |= O_NONBLOCK;
39
+
40
+    s->fd = open(s1->filename, flags);
41
+    if (s->fd < 0) {
42
+        res = AVERROR(errno);
43
+        av_log(s1, AV_LOG_ERROR, "Unable to open V4L2 device '%s'\n", s1->filename);
44
+        return res;
45
+    }
46
+
47
+    if (s1->nb_streams != 1 ||
48
+        s1->streams[0]->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
49
+        s1->streams[0]->codec->codec_id   != AV_CODEC_ID_RAWVIDEO) {
50
+        av_log(s1, AV_LOG_ERROR,
51
+               "V4L2 output device supports only a single raw video stream\n");
52
+        return AVERROR(EINVAL);
53
+    }
54
+
55
+    enc_ctx = s1->streams[0]->codec;
56
+
57
+    v4l2_pixfmt = avpriv_fmt_ff2v4l(enc_ctx->pix_fmt, AV_CODEC_ID_RAWVIDEO);
58
+    if (!v4l2_pixfmt) { // XXX: try to force them one by one?
59
+        av_log(s1, AV_LOG_ERROR, "Unknown V4L2 pixel format equivalent for %s\n",
60
+               av_get_pix_fmt_name(enc_ctx->pix_fmt));
61
+        return AVERROR(EINVAL);
62
+    }
63
+
64
+    if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
65
+        res = AVERROR(errno);
66
+        av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n", av_err2str(res));
67
+        return res;
68
+    }
69
+
70
+    fmt.fmt.pix.width       = enc_ctx->width;
71
+    fmt.fmt.pix.height      = enc_ctx->height;
72
+    fmt.fmt.pix.pixelformat = v4l2_pixfmt;
73
+    fmt.fmt.pix.sizeimage   = av_image_get_buffer_size(enc_ctx->pix_fmt, enc_ctx->width, enc_ctx->height, 1);
74
+
75
+    if (ioctl(s->fd, VIDIOC_S_FMT, &fmt) < 0) {
76
+        res = AVERROR(errno);
77
+        av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_S_FMT): %s\n", av_err2str(res));
78
+        return res;
79
+    }
80
+
81
+    return res;
82
+}
83
+
84
+static int write_packet(AVFormatContext *s1, AVPacket *pkt)
85
+{
86
+    const V4L2Context *s = s1->priv_data;
87
+    write(s->fd, pkt->data, pkt->size);
88
+    return 0;
89
+}
90
+
91
+static int write_trailer(AVFormatContext *s1)
92
+{
93
+    const V4L2Context *s = s1->priv_data;
94
+    close(s->fd);
95
+    return 0;
96
+}
97
+
98
+AVOutputFormat ff_v4l2_muxer = {
99
+    .name           = "v4l2",
100
+    .long_name      = NULL_IF_CONFIG_SMALL("Video4Linux2 output device"),
101
+    .priv_data_size = sizeof(V4L2Context),
102
+    .audio_codec    = AV_CODEC_ID_NONE,
103
+    .video_codec    = AV_CODEC_ID_RAWVIDEO,
104
+    .write_header   = write_header,
105
+    .write_packet   = write_packet,
106
+    .write_trailer  = write_trailer,
107
+    .flags          = AVFMT_NOFILE,
108
+};
... ...
@@ -28,7 +28,7 @@
28 28
 #include "libavutil/avutil.h"
29 29
 
30 30
 #define LIBAVDEVICE_VERSION_MAJOR  55
31
-#define LIBAVDEVICE_VERSION_MINOR   0
31
+#define LIBAVDEVICE_VERSION_MINOR   1
32 32
 #define LIBAVDEVICE_VERSION_MICRO 100
33 33
 
34 34
 #define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \