~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libavdevice/v4l2.c

  • Committer: Gauvain Pocentek
  • Date: 2012-03-06 11:59:12 UTC
  • mfrom: (66.1.15 precise)
  • Revision ID: gauvain@pocentek.net-20120306115912-h9d6kt9j0l532oo5
* Merge from Ubuntu:
  - put back faac support
  - recommends apport-hooks-medibuntu
  - change Maintainer, Uploaders & Vcs-* fields.
* New upstream snapshot
* upload to unstable
* Build against external libmpeg2
* drop 51_FTBFS_arm.patch again
* no longer build depend on libcdparanoia-dev on the Hurd
* Fix FTBFS on the hurd.
  Thanks to Samuel Thibault <sthibault@debian.org> (Closes: #654974)
* Fix FTBFS on arm
* New upstream snapshot, Closes: #650339, #643621, #481807
* Imported Upstream version 1.0~rc4+svn34492
* Bump standards version
* Bump dependency on libav >= 4:0.8~, Closes: #653887
* Fix build-indep
* Build mplayer-gui again, Closes: #568514
* Drop debian/all-lang-config-mak.sh, no longer needed
* include .dfsg1 in version number
* remove get-orig-source target
* no longer prune compiler flags from the environment
* No longer advertise nor build 3fdx, mga and dxr3 backends,
  Closes: #496106, #442181, #533546
* beautify mplayer version identification string
* Brown paperbag upload.
* Next try to fix build failure on sparce after recent binutils change.
* Brown paperbag upload.
* Really fix build failure on sparc after recent binutils change.
* Properly set Replaces/Conflicts on mplayer2{,-dbg} to avoid
  file overwrite errors.
* Adjust versioning of mplayer listed in the mplayer-dbg's Depends field.
* Fix build failure on sparc after recent binutils change.
* Urgency medium bumped because of RC-level bugfix
  and speeding up x264 transition.
* Update to my @debian.org email.
* Upload to unstable
* Enable joystick support on Linux only, Closes: #638408
* Rebuild fixes toolchain issue on arm, Closes: #637077
* New upstream snapshot
* following the discussion started by Diego Biurrun <diego@biurrun.de>
  in debian-devel, I have prepared a new packaging of 'mplayer'
  (with code that comes from CVS)
* the upstream tar.bz cannot be distributed by Debian, since it contains
   CSS code; so I am repackaging it 
* I have tried my best to address all known issues:
  - the package contains the detailed Copyright made by Diego Biurrun 
  - the package does not contain CSS code, or  AFAIK other code on which 
     there is active patent enforcement
  - there is a script  debian/cvs-changelog.sh  that shows all changes
     done to files included in this source.
    This should comply with GPLv2 sec 2.a  (in spirit if not in letter)
    For this reason, the source code contains CVS directories.
* needs   make (>= 3.80) for 'html-chunked-$(1)' in DOCS/xml/Makefile

* some corrections, as suggested Diego Biurrun
  - binary codecs should go into /usr/lib/codecs (upstream default)
  - better template 'mplayer/install_codecs'
  - an empty 'font=' in mplayer.conf breaks mplayer: postinst corrected
* correction in 'mplayer/cfgnote'
* better mplayer.postinst and mplayer.config

* New upstream release
* better debian/copyright file
* do not ship a skin
* New upstream release
* changed DEB_BUILD_OPTIONS to DEB_BUILD_CONFIGURE ,
  DEB_BUILD_OPTIONS is used as in debian policy
* use gcc-3.4
* changed xlibs-dev to a long list of dependencies, for Debian/etch
* try to adhere to  http://www.mplayerhq.hu/DOCS/tech/binary-packaging.txt
  (see README.Debian for details)
* removed dependency on xlibmesa-dev, disabled opengl
* New upstream release
* Simon McVittie <hacks@pseudorandom.co.uk> wonderful work:
- Work around Debian bug #267442 (glibc's sys/uio.h and gcc's altivec.h have
  conflicting uses for __vector) by re-ordering #includes
- Fix potential symlink attack in ./configure
- Disable support for binary codecs on platforms for which those codecs
  aren't available; also disable the corresponding Debconf note when it's
  inappropriate
- Changed Build-Depends: so it works in pbuilder
- Explicitly build-depend on libjpeg62-dev, libfontconfig1-dev,
  libungif4-dev 
- Tweak debian/rules to avoid certain errors being ignored
- Use --language=all
* provide a target  'debian/rules get-orig-source' 
  that recreates the orig.tar.gz ; then use the above orig.tar.gz
* rewrote some parts of debian/rules
* don't clean and recompile docs if upstream ships them
* mplayer-doc was shipping too much stuff
* translated man pages where not installed properly
* compile with libdv4-dev
* correct README.Debian
* Forgot build-dep on libtheora
* Must not depend on libxvidcore
* New upstream release
* new release.
* rc1 to become 0.90
* new pre-release
* new pre-release
* gtk bug fixed.
* new release.
* version bumped
* 0.60 pre2 release
* 0.60 pre-release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#undef __STRICT_ANSI__ //workaround due to broken kernel headers
31
31
#include "config.h"
32
32
#include "libavformat/avformat.h"
 
33
#include "libavformat/internal.h"
33
34
#include <unistd.h>
34
35
#include <fcntl.h>
35
36
#include <sys/ioctl.h>
36
37
#include <sys/mman.h>
37
38
#include <sys/time.h>
 
39
#include <poll.h>
38
40
#if HAVE_SYS_VIDEOIO_H
39
41
#include <sys/videoio.h>
40
42
#else
41
 
#include <asm/types.h>
42
43
#include <linux/videodev2.h>
43
44
#endif
44
45
#include <time.h>
45
 
#include <strings.h>
46
46
#include "libavutil/imgutils.h"
47
47
#include "libavutil/log.h"
48
48
#include "libavutil/opt.h"
49
49
#include "libavutil/parseutils.h"
50
50
#include "libavutil/pixdesc.h"
 
51
#include "libavutil/avstring.h"
 
52
#include "libavutil/mathematics.h"
51
53
 
52
54
static const int desired_video_buffers = 256;
53
55
 
54
 
enum io_method {
55
 
    io_read,
56
 
    io_mmap,
57
 
    io_userptr
58
 
};
 
56
#define V4L_ALLFORMATS  3
 
57
#define V4L_RAWFORMATS  1
 
58
#define V4L_COMPFORMATS 2
59
59
 
60
60
struct video_data {
61
61
    AVClass *class;
62
62
    int fd;
63
63
    int frame_format; /* V4L2_PIX_FMT_* */
64
 
    enum io_method io_method;
65
64
    int width, height;
66
65
    int frame_size;
 
66
    int timeout;
 
67
    int interlaced;
67
68
    int top_field_first;
68
69
 
69
70
    int buffers;
71
72
    unsigned int *buf_len;
72
73
    char *standard;
73
74
    int channel;
74
 
    char *video_size; /**< String describing video size, set by a private option. */
 
75
    char *video_size;   /**< String describing video size,
 
76
                             set by a private option. */
75
77
    char *pixel_format; /**< Set by a private option. */
 
78
    int list_format;    /**< Set by a private option. */
76
79
    char *framerate;    /**< Set by a private option. */
77
80
};
78
81
 
106
109
    { PIX_FMT_NONE,    CODEC_ID_MJPEG,    V4L2_PIX_FMT_JPEG    },
107
110
};
108
111
 
109
 
static int device_open(AVFormatContext *ctx, uint32_t *capabilities)
 
112
static int device_open(AVFormatContext *ctx)
110
113
{
111
114
    struct v4l2_capability cap;
112
115
    int fd;
116
119
    if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
117
120
        flags |= O_NONBLOCK;
118
121
    }
 
122
 
119
123
    fd = open(ctx->filename, flags, 0);
120
124
    if (fd < 0) {
 
125
        err = errno;
 
126
 
121
127
        av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
122
 
                 ctx->filename, strerror(errno));
 
128
               ctx->filename, strerror(err));
123
129
 
124
 
        return AVERROR(errno);
 
130
        return AVERROR(err);
125
131
    }
126
132
 
127
133
    res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
128
 
    // ENOIOCTLCMD definition only availble on __KERNEL__
129
 
    if (res < 0 && ((err = errno) == 515)) {
130
 
        av_log(ctx, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
131
 
        close(fd);
132
 
 
133
 
        return AVERROR(515);
134
 
    }
135
134
    if (res < 0) {
 
135
        err = errno;
136
136
        av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
137
 
                 strerror(errno));
138
 
        close(fd);
139
 
 
140
 
        return AVERROR(err);
141
 
    }
142
 
    if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
143
 
        av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n");
144
 
        close(fd);
145
 
 
146
 
        return AVERROR(ENODEV);
147
 
    }
148
 
    *capabilities = cap.capabilities;
 
137
               strerror(err));
 
138
 
 
139
        goto fail;
 
140
    }
 
141
 
 
142
    av_log(ctx, AV_LOG_VERBOSE, "[%d]Capabilities: %x\n",
 
143
           fd, cap.capabilities);
 
144
 
 
145
    if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
 
146
        av_log(ctx, AV_LOG_ERROR, "Not a video capture device.\n");
 
147
        err = ENODEV;
 
148
 
 
149
        goto fail;
 
150
    }
 
151
 
 
152
    if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
 
153
        av_log(ctx, AV_LOG_ERROR,
 
154
               "The device does not support the streaming I/O method.\n");
 
155
        err = ENOSYS;
 
156
 
 
157
        goto fail;
 
158
    }
149
159
 
150
160
    return fd;
 
161
 
 
162
fail:
 
163
    close(fd);
 
164
    return AVERROR(err);
151
165
}
152
166
 
153
 
static int device_init(AVFormatContext *ctx, int *width, int *height, uint32_t pix_fmt)
 
167
static int device_init(AVFormatContext *ctx, int *width, int *height,
 
168
                       uint32_t pix_fmt)
154
169
{
155
170
    struct video_data *s = ctx->priv_data;
156
171
    int fd = s->fd;
157
 
    struct v4l2_format fmt;
 
172
    struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
 
173
    struct v4l2_pix_format *pix = &fmt.fmt.pix;
 
174
 
158
175
    int res;
159
176
 
160
 
    memset(&fmt, 0, sizeof(struct v4l2_format));
161
 
    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
162
 
    fmt.fmt.pix.width = *width;
163
 
    fmt.fmt.pix.height = *height;
164
 
    fmt.fmt.pix.pixelformat = pix_fmt;
165
 
    fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
 
177
    pix->width = *width;
 
178
    pix->height = *height;
 
179
    pix->pixelformat = pix_fmt;
 
180
    pix->field = V4L2_FIELD_ANY;
 
181
 
166
182
    res = ioctl(fd, VIDIOC_S_FMT, &fmt);
 
183
 
167
184
    if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
168
 
        av_log(ctx, AV_LOG_INFO, "The V4L2 driver changed the video from %dx%d to %dx%d\n", *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
 
185
        av_log(ctx, AV_LOG_INFO,
 
186
               "The V4L2 driver changed the video from %dx%d to %dx%d\n",
 
187
               *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
169
188
        *width = fmt.fmt.pix.width;
170
189
        *height = fmt.fmt.pix.height;
171
190
    }
172
191
 
173
192
    if (pix_fmt != fmt.fmt.pix.pixelformat) {
174
 
        av_log(ctx, AV_LOG_DEBUG, "The V4L2 driver changed the pixel format from 0x%08X to 0x%08X\n", pix_fmt, fmt.fmt.pix.pixelformat);
 
193
        av_log(ctx, AV_LOG_DEBUG,
 
194
               "The V4L2 driver changed the pixel format "
 
195
               "from 0x%08X to 0x%08X\n",
 
196
               pix_fmt, fmt.fmt.pix.pixelformat);
175
197
        res = -1;
176
198
    }
177
199
 
 
200
    if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
 
201
        av_log(ctx, AV_LOG_DEBUG, "The V4L2 driver using the interlaced mode");
 
202
        s->interlaced = 1;
 
203
    }
 
204
 
178
205
    return res;
179
206
}
180
207
 
237
264
    return CODEC_ID_NONE;
238
265
}
239
266
 
 
267
#if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
 
268
static void list_framesizes(AVFormatContext *ctx, int fd, uint32_t pixelformat)
 
269
{
 
270
    struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
 
271
 
 
272
    while(!ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
 
273
        switch (vfse.type) {
 
274
        case V4L2_FRMSIZE_TYPE_DISCRETE:
 
275
            av_log(ctx, AV_LOG_INFO, " %ux%u",
 
276
                   vfse.discrete.width, vfse.discrete.height);
 
277
        break;
 
278
        case V4L2_FRMSIZE_TYPE_CONTINUOUS:
 
279
        case V4L2_FRMSIZE_TYPE_STEPWISE:
 
280
            av_log(ctx, AV_LOG_INFO, " {%u-%u, %u}x{%u-%u, %u}",
 
281
                   vfse.stepwise.min_width,
 
282
                   vfse.stepwise.max_width,
 
283
                   vfse.stepwise.step_width,
 
284
                   vfse.stepwise.min_height,
 
285
                   vfse.stepwise.max_height,
 
286
                   vfse.stepwise.step_height);
 
287
        }
 
288
        vfse.index++;
 
289
    }
 
290
}
 
291
#endif
 
292
 
 
293
static void list_formats(AVFormatContext *ctx, int fd, int type)
 
294
{
 
295
    struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
 
296
 
 
297
    while(!ioctl(fd, VIDIOC_ENUM_FMT, &vfd)) {
 
298
        enum CodecID codec_id = fmt_v4l2codec(vfd.pixelformat);
 
299
        enum PixelFormat pix_fmt = fmt_v4l2ff(vfd.pixelformat, codec_id);
 
300
 
 
301
        vfd.index++;
 
302
 
 
303
        if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
 
304
            type & V4L_RAWFORMATS) {
 
305
            const char *fmt_name = av_get_pix_fmt_name(pix_fmt);
 
306
            av_log(ctx, AV_LOG_INFO, "R : %9s : %20s :",
 
307
                   fmt_name ? fmt_name : "Unsupported",
 
308
                   vfd.description);
 
309
        } else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
 
310
                   type & V4L_COMPFORMATS) {
 
311
            AVCodec *codec = avcodec_find_encoder(codec_id);
 
312
            av_log(ctx, AV_LOG_INFO, "C : %9s : %20s :",
 
313
                   codec ? codec->name : "Unsupported",
 
314
                   vfd.description);
 
315
        } else {
 
316
            continue;
 
317
        }
 
318
 
 
319
#ifdef V4L2_FMT_FLAG_EMULATED
 
320
        if (vfd.flags & V4L2_FMT_FLAG_EMULATED) {
 
321
            av_log(ctx, AV_LOG_WARNING, "%s", "Emulated");
 
322
            continue;
 
323
        }
 
324
#endif
 
325
#if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
 
326
        list_framesizes(ctx, fd, vfd.pixelformat);
 
327
#endif
 
328
        av_log(ctx, AV_LOG_INFO, "\n");
 
329
    }
 
330
}
 
331
 
240
332
static int mmap_init(AVFormatContext *ctx)
241
333
{
 
334
    int i, res;
242
335
    struct video_data *s = ctx->priv_data;
243
 
    struct v4l2_requestbuffers req;
244
 
    int i, res;
 
336
    struct v4l2_requestbuffers req = {
 
337
        .type   = V4L2_BUF_TYPE_VIDEO_CAPTURE,
 
338
        .count  = desired_video_buffers,
 
339
        .memory = V4L2_MEMORY_MMAP
 
340
    };
245
341
 
246
 
    memset(&req, 0, sizeof(struct v4l2_requestbuffers));
247
 
    req.count = desired_video_buffers;
248
 
    req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
249
 
    req.memory = V4L2_MEMORY_MMAP;
250
342
    res = ioctl(s->fd, VIDIOC_REQBUFS, &req);
251
343
    if (res < 0) {
252
344
        if (errno == EINVAL) {
279
371
    }
280
372
 
281
373
    for (i = 0; i < req.count; i++) {
282
 
        struct v4l2_buffer buf;
 
374
        struct v4l2_buffer buf = {
 
375
            .type   = V4L2_BUF_TYPE_VIDEO_CAPTURE,
 
376
            .index  = i,
 
377
            .memory = V4L2_MEMORY_MMAP
 
378
        };
283
379
 
284
 
        memset(&buf, 0, sizeof(struct v4l2_buffer));
285
 
        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
286
 
        buf.memory = V4L2_MEMORY_MMAP;
287
 
        buf.index = i;
288
380
        res = ioctl(s->fd, VIDIOC_QUERYBUF, &buf);
289
381
        if (res < 0) {
290
382
            av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
294
386
 
295
387
        s->buf_len[i] = buf.length;
296
388
        if (s->frame_size > 0 && s->buf_len[i] < s->frame_size) {
297
 
            av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
 
389
            av_log(ctx, AV_LOG_ERROR,
 
390
                   "Buffer len [%d] = %d != %d\n",
 
391
                   i, s->buf_len[i], s->frame_size);
298
392
 
299
393
            return -1;
300
394
        }
301
 
        s->buf_start[i] = mmap (NULL, buf.length,
302
 
                        PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
 
395
        s->buf_start[i] = mmap(NULL, buf.length,
 
396
                               PROT_READ | PROT_WRITE, MAP_SHARED,
 
397
                               s->fd, buf.m.offset);
 
398
 
303
399
        if (s->buf_start[i] == MAP_FAILED) {
304
400
            av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
305
401
 
310
406
    return 0;
311
407
}
312
408
 
313
 
static int read_init(AVFormatContext *ctx)
314
 
{
315
 
    return -1;
316
 
}
317
 
 
318
409
static void mmap_release_buffer(AVPacket *pkt)
319
410
{
320
 
    struct v4l2_buffer buf;
 
411
    struct v4l2_buffer buf = { 0 };
321
412
    int res, fd;
322
413
    struct buff_data *buf_descriptor = pkt->priv;
323
414
 
324
 
    if (pkt->data == NULL) {
325
 
         return;
326
 
    }
 
415
    if (pkt->data == NULL)
 
416
        return;
327
417
 
328
 
    memset(&buf, 0, sizeof(struct v4l2_buffer));
329
418
    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
330
419
    buf.memory = V4L2_MEMORY_MMAP;
331
420
    buf.index = buf_descriptor->index;
333
422
    av_free(buf_descriptor);
334
423
 
335
424
    res = ioctl(fd, VIDIOC_QBUF, &buf);
336
 
    if (res < 0) {
337
 
        av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
338
 
    }
 
425
    if (res < 0)
 
426
        av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n",
 
427
               strerror(errno));
 
428
 
339
429
    pkt->data = NULL;
340
430
    pkt->size = 0;
341
431
}
343
433
static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
344
434
{
345
435
    struct video_data *s = ctx->priv_data;
346
 
    struct v4l2_buffer buf;
 
436
    struct v4l2_buffer buf = {
 
437
        .type   = V4L2_BUF_TYPE_VIDEO_CAPTURE,
 
438
        .memory = V4L2_MEMORY_MMAP
 
439
    };
347
440
    struct buff_data *buf_descriptor;
 
441
    struct pollfd p = { .fd = s->fd, .events = POLLIN };
348
442
    int res;
349
443
 
350
 
    memset(&buf, 0, sizeof(struct v4l2_buffer));
351
 
    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
352
 
    buf.memory = V4L2_MEMORY_MMAP;
 
444
    res = poll(&p, 1, s->timeout);
 
445
    if (res < 0)
 
446
        return AVERROR(errno);
 
447
 
 
448
    if (!(p.revents & (POLLIN | POLLERR | POLLHUP)))
 
449
        return AVERROR(EAGAIN);
353
450
 
354
451
    /* FIXME: Some special treatment might be needed in case of loss of signal... */
355
452
    while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
359
456
 
360
457
            return AVERROR(EAGAIN);
361
458
        }
362
 
        av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
 
459
        av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n",
 
460
               strerror(errno));
363
461
 
364
462
        return AVERROR(errno);
365
463
    }
366
464
    assert (buf.index < s->buffers);
367
465
    if (s->frame_size > 0 && buf.bytesused != s->frame_size) {
368
 
        av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
 
466
        av_log(ctx, AV_LOG_ERROR,
 
467
               "The v4l2 frame is %d bytes, but %d bytes are expected\n",
 
468
               buf.bytesused, s->frame_size);
369
469
 
370
470
        return AVERROR_INVALIDDATA;
371
471
    }
392
492
    return s->buf_len[buf.index];
393
493
}
394
494
 
395
 
static int read_frame(AVFormatContext *ctx, AVPacket *pkt)
396
 
{
397
 
    return -1;
398
 
}
399
 
 
400
495
static int mmap_start(AVFormatContext *ctx)
401
496
{
402
497
    struct video_data *s = ctx->priv_data;
404
499
    int i, res;
405
500
 
406
501
    for (i = 0; i < s->buffers; i++) {
407
 
        struct v4l2_buffer buf;
408
 
 
409
 
        memset(&buf, 0, sizeof(struct v4l2_buffer));
410
 
        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
411
 
        buf.memory = V4L2_MEMORY_MMAP;
412
 
        buf.index  = i;
 
502
        struct v4l2_buffer buf = {
 
503
            .type   = V4L2_BUF_TYPE_VIDEO_CAPTURE,
 
504
            .index  = i,
 
505
            .memory = V4L2_MEMORY_MMAP
 
506
        };
413
507
 
414
508
        res = ioctl(s->fd, VIDIOC_QBUF, &buf);
415
509
        if (res < 0) {
416
 
            av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
 
510
            av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n",
 
511
                   strerror(errno));
417
512
 
418
513
            return AVERROR(errno);
419
514
        }
422
517
    type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
423
518
    res = ioctl(s->fd, VIDIOC_STREAMON, &type);
424
519
    if (res < 0) {
425
 
        av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
 
520
        av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n",
 
521
               strerror(errno));
426
522
 
427
523
        return AVERROR(errno);
428
524
    }
450
546
static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap)
451
547
{
452
548
    struct video_data *s = s1->priv_data;
453
 
    struct v4l2_input input;
454
 
    struct v4l2_standard standard;
 
549
    struct v4l2_input input = { 0 };
 
550
    struct v4l2_standard standard = { 0 };
455
551
    struct v4l2_streamparm streamparm = { 0 };
456
552
    struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe;
 
553
    AVRational framerate_q = { 0 };
457
554
    int i, ret;
458
 
    AVRational fps;
459
555
 
460
556
    streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
461
557
 
462
 
    if (s->framerate && (ret = av_parse_video_rate(&fps, s->framerate)) < 0) {
463
 
        av_log(s1, AV_LOG_ERROR, "Couldn't parse framerate.\n");
 
558
    if (s->framerate &&
 
559
        (ret = av_parse_video_rate(&framerate_q, s->framerate)) < 0) {
 
560
        av_log(s1, AV_LOG_ERROR, "Could not parse framerate '%s'.\n",
 
561
               s->framerate);
464
562
        return ret;
465
563
    }
466
 
#if FF_API_FORMAT_PARAMETERS
467
 
    if (ap->channel > 0)
468
 
        s->channel = ap->channel;
469
 
    if (ap->time_base.num)
470
 
        fps = (AVRational){ap->time_base.den, ap->time_base.num};
471
 
#endif
472
564
 
473
565
    /* set tv video input */
474
 
    memset (&input, 0, sizeof (input));
475
566
    input.index = s->channel;
476
567
    if (ioctl(s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
477
568
        av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
481
572
    av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
482
573
            s->channel, input.name);
483
574
    if (ioctl(s->fd, VIDIOC_S_INPUT, &input.index) < 0) {
484
 
        av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set input(%d) failed\n",
 
575
        av_log(s1, AV_LOG_ERROR,
 
576
               "The V4L2 driver ioctl set input(%d) failed\n",
485
577
                s->channel);
486
578
        return AVERROR(EIO);
487
579
    }
488
580
 
489
 
#if FF_API_FORMAT_PARAMETERS
490
 
    if (ap->standard) {
491
 
        av_freep(&s->standard);
492
 
        s->standard = av_strdup(ap->standard);
493
 
    }
494
 
#endif
495
 
 
496
581
    if (s->standard) {
497
582
        av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
498
583
               s->standard);
499
584
        /* set tv standard */
500
 
        memset (&standard, 0, sizeof (standard));
501
585
        for(i=0;;i++) {
502
586
            standard.index = i;
503
587
            if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
504
 
                av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
 
588
                av_log(s1, AV_LOG_ERROR,
 
589
                       "The V4L2 driver ioctl set standard(%s) failed\n",
505
590
                       s->standard);
506
591
                return AVERROR(EIO);
507
592
            }
508
593
 
509
 
            if (!strcasecmp(standard.name, s->standard)) {
 
594
            if (!av_strcasecmp(standard.name, s->standard)) {
510
595
                break;
511
596
            }
512
597
        }
513
598
 
514
 
        av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
 
599
        av_log(s1, AV_LOG_DEBUG,
 
600
               "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
515
601
               s->standard, (uint64_t)standard.id);
516
602
        if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
517
 
            av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
 
603
            av_log(s1, AV_LOG_ERROR,
 
604
                   "The V4L2 driver ioctl set standard(%s) failed\n",
518
605
                   s->standard);
519
606
            return AVERROR(EIO);
520
607
        }
521
608
    }
522
609
 
523
 
    if (fps.num && fps.den) {
 
610
    if (framerate_q.num && framerate_q.den) {
524
611
        av_log(s1, AV_LOG_DEBUG, "Setting time per frame to %d/%d\n",
525
 
               fps.den, fps.num);
526
 
        tpf->numerator   = fps.den;
527
 
        tpf->denominator = fps.num;
 
612
               framerate_q.den, framerate_q.num);
 
613
        tpf->numerator   = framerate_q.den;
 
614
        tpf->denominator = framerate_q.num;
 
615
 
528
616
        if (ioctl(s->fd, VIDIOC_S_PARM, &streamparm) != 0) {
529
617
            av_log(s1, AV_LOG_ERROR,
530
618
                   "ioctl set time per frame(%d/%d) failed\n",
531
 
                   fps.den, fps.num);
 
619
                   framerate_q.den, framerate_q.num);
532
620
            return AVERROR(EIO);
533
621
        }
534
622
 
535
 
        if (fps.num != tpf->denominator ||
536
 
            fps.den != tpf->numerator) {
 
623
        if (framerate_q.num != tpf->denominator ||
 
624
            framerate_q.den != tpf->numerator) {
537
625
            av_log(s1, AV_LOG_INFO,
538
 
                   "The driver changed the time per frame from %d/%d to %d/%d\n",
539
 
                   fps.den, fps.num,
 
626
                   "The driver changed the time per frame from "
 
627
                   "%d/%d to %d/%d\n",
 
628
                   framerate_q.den, framerate_q.num,
540
629
                   tpf->numerator, tpf->denominator);
541
630
        }
542
631
    } else {
543
 
        /* if timebase value is not set, read the timebase value from the driver */
544
632
        if (ioctl(s->fd, VIDIOC_G_PARM, &streamparm) != 0) {
545
 
            av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_PARM): %s\n", strerror(errno));
 
633
            av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_PARM): %s\n",
 
634
                   strerror(errno));
546
635
            return AVERROR(errno);
547
636
        }
548
637
    }
549
638
    s1->streams[0]->codec->time_base.den = tpf->denominator;
550
639
    s1->streams[0]->codec->time_base.num = tpf->numerator;
551
640
 
 
641
    s->timeout = 100 +
 
642
        av_rescale_q(1, s1->streams[0]->codec->time_base,
 
643
                        (AVRational){1, 1000});
 
644
 
552
645
    return 0;
553
646
}
554
647
 
576
669
            }
577
670
        }
578
671
    }
 
672
 
579
673
    if (desired_format != 0) {
580
674
        *codec_id = fmt_v4l2codec(desired_format);
581
675
        assert(*codec_id != CODEC_ID_NONE);
589
683
    struct video_data *s = s1->priv_data;
590
684
    AVStream *st;
591
685
    int res = 0;
592
 
    uint32_t desired_format, capabilities;
 
686
    uint32_t desired_format;
593
687
    enum CodecID codec_id;
594
688
    enum PixelFormat pix_fmt = PIX_FMT_NONE;
595
689
 
596
 
    st = av_new_stream(s1, 0);
 
690
    st = avformat_new_stream(s1, NULL);
597
691
    if (!st) {
598
692
        res = AVERROR(ENOMEM);
599
693
        goto out;
600
694
    }
601
 
    av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
602
 
 
603
 
    if (s->video_size && (res = av_parse_video_size(&s->width, &s->height, s->video_size)) < 0) {
604
 
        av_log(s1, AV_LOG_ERROR, "Couldn't parse video size.\n");
605
 
        goto out;
606
 
    }
607
 
    if (s->pixel_format && (pix_fmt = av_get_pix_fmt(s->pixel_format)) == PIX_FMT_NONE) {
608
 
        av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n", s->pixel_format);
609
 
        res = AVERROR(EINVAL);
610
 
        goto out;
611
 
    }
612
 
#if FF_API_FORMAT_PARAMETERS
613
 
    if (ap->width > 0)
614
 
        s->width  = ap->width;
615
 
    if (ap->height > 0)
616
 
        s->height = ap->height;
617
 
    if (ap->pix_fmt)
618
 
        pix_fmt = ap->pix_fmt;
619
 
#endif
620
 
 
621
 
    capabilities = 0;
622
 
    s->fd = device_open(s1, &capabilities);
 
695
 
 
696
    s->fd = device_open(s1);
623
697
    if (s->fd < 0) {
624
 
        res = AVERROR(EIO);
625
 
        goto out;
626
 
    }
627
 
    av_log(s1, AV_LOG_VERBOSE, "[%d]Capabilities: %x\n", s->fd, capabilities);
 
698
        res = s->fd;
 
699
        goto out;
 
700
    }
 
701
 
 
702
    if (s->list_format) {
 
703
        list_formats(s1, s->fd, s->list_format);
 
704
        res = AVERROR_EXIT;
 
705
        goto out;
 
706
    }
 
707
 
 
708
    avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
 
709
 
 
710
    if (s->video_size &&
 
711
        (res = av_parse_video_size(&s->width, &s->height, s->video_size)) < 0) {
 
712
        av_log(s1, AV_LOG_ERROR, "Could not parse video size '%s'.\n",
 
713
               s->video_size);
 
714
        goto out;
 
715
    }
 
716
 
 
717
    if (s->pixel_format) {
 
718
        AVCodec *codec = avcodec_find_decoder_by_name(s->pixel_format);
 
719
 
 
720
        if (codec)
 
721
            s1->video_codec_id = codec->id;
 
722
 
 
723
        pix_fmt = av_get_pix_fmt(s->pixel_format);
 
724
 
 
725
        if (pix_fmt == PIX_FMT_NONE && !codec) {
 
726
            av_log(s1, AV_LOG_ERROR, "No such input format: %s.\n",
 
727
                   s->pixel_format);
 
728
 
 
729
            res = AVERROR(EINVAL);
 
730
            goto out;
 
731
        }
 
732
    }
628
733
 
629
734
    if (!s->width && !s->height) {
630
735
        struct v4l2_format fmt;
631
736
 
632
 
        av_log(s1, AV_LOG_VERBOSE, "Querying the device for the current frame size\n");
 
737
        av_log(s1, AV_LOG_VERBOSE,
 
738
               "Querying the device for the current frame size\n");
633
739
        fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
634
740
        if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
635
 
            av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n", strerror(errno));
 
741
            av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n",
 
742
                   strerror(errno));
636
743
            res = AVERROR(errno);
637
744
            goto out;
638
745
        }
 
746
 
639
747
        s->width  = fmt.fmt.pix.width;
640
748
        s->height = fmt.fmt.pix.height;
641
 
        av_log(s1, AV_LOG_VERBOSE, "Setting frame size to %dx%d\n", s->width, s->height);
 
749
        av_log(s1, AV_LOG_VERBOSE,
 
750
               "Setting frame size to %dx%d\n", s->width, s->height);
642
751
    }
643
752
 
644
 
    desired_format = device_try_init(s1, pix_fmt, &s->width, &s->height, &codec_id);
 
753
    desired_format = device_try_init(s1, pix_fmt, &s->width, &s->height,
 
754
                                     &codec_id);
645
755
    if (desired_format == 0) {
646
756
        av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for "
647
757
               "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, pix_fmt);
650
760
        res = AVERROR(EIO);
651
761
        goto out;
652
762
    }
 
763
 
653
764
    if ((res = av_image_check_size(s->width, s->height, 0, s1) < 0))
654
765
        goto out;
 
766
 
655
767
    s->frame_format = desired_format;
656
768
 
657
769
    if ((res = v4l2_set_parameters(s1, ap) < 0))
658
770
        goto out;
659
771
 
660
772
    st->codec->pix_fmt = fmt_v4l2ff(desired_format, codec_id);
661
 
    s->frame_size = avpicture_get_size(st->codec->pix_fmt, s->width, s->height);
662
 
    if (capabilities & V4L2_CAP_STREAMING) {
663
 
        s->io_method = io_mmap;
664
 
        res = mmap_init(s1);
665
 
        if (res == 0) {
666
 
            res = mmap_start(s1);
667
 
        }
668
 
    } else {
669
 
        s->io_method = io_read;
670
 
        res = read_init(s1);
671
 
    }
672
 
    if (res < 0) {
 
773
    s->frame_size =
 
774
        avpicture_get_size(st->codec->pix_fmt, s->width, s->height);
 
775
 
 
776
    if ((res = mmap_init(s1)) ||
 
777
        (res = mmap_start(s1)) < 0) {
673
778
        close(s->fd);
674
 
 
675
 
        res = AVERROR(EIO);
676
779
        goto out;
677
780
    }
 
781
 
678
782
    s->top_field_first = first_field(s->fd);
679
783
 
680
784
    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
681
785
    st->codec->codec_id = codec_id;
 
786
    if (codec_id == CODEC_ID_RAWVIDEO)
 
787
        st->codec->codec_tag =
 
788
            avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt);
682
789
    st->codec->width = s->width;
683
790
    st->codec->height = s->height;
684
791
    st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
690
797
static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
691
798
{
692
799
    struct video_data *s = s1->priv_data;
 
800
    AVFrame *frame = s1->streams[0]->codec->coded_frame;
693
801
    int res;
694
802
 
695
 
    if (s->io_method == io_mmap) {
696
 
        av_init_packet(pkt);
697
 
        res = mmap_read_frame(s1, pkt);
698
 
    } else if (s->io_method == io_read) {
699
 
        if (av_new_packet(pkt, s->frame_size) < 0)
700
 
            return AVERROR(EIO);
701
 
 
702
 
        res = read_frame(s1, pkt);
703
 
    } else {
704
 
        return AVERROR(EIO);
705
 
    }
706
 
    if (res < 0) {
 
803
    av_init_packet(pkt);
 
804
    if ((res = mmap_read_frame(s1, pkt)) < 0) {
707
805
        return res;
708
806
    }
709
807
 
710
 
    if (s1->streams[0]->codec->coded_frame) {
711
 
        s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
712
 
        s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
 
808
    if (frame && s->interlaced) {
 
809
        frame->interlaced_frame = 1;
 
810
        frame->top_field_first = s->top_field_first;
713
811
    }
714
812
 
715
813
    return pkt->size;
719
817
{
720
818
    struct video_data *s = s1->priv_data;
721
819
 
722
 
    if (s->io_method == io_mmap) {
723
 
        mmap_close(s);
724
 
    }
 
820
    mmap_close(s);
725
821
 
726
822
    close(s->fd);
727
823
    return 0;
730
826
#define OFFSET(x) offsetof(struct video_data, x)
731
827
#define DEC AV_OPT_FLAG_DECODING_PARAM
732
828
static const AVOption options[] = {
733
 
    { "standard", "", offsetof(struct video_data, standard), FF_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
734
 
    { "channel",  "", offsetof(struct video_data, channel),  FF_OPT_TYPE_INT,    {.dbl = 0 }, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
735
 
    { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
736
 
    { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
737
 
    { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
 
829
    { "standard",     "TV standard, used only by analog frame grabber",            OFFSET(standard),     AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0,       DEC },
 
830
    { "channel",      "TV channel, used only by frame grabber",                    OFFSET(channel),      AV_OPT_TYPE_INT,    {.dbl = 0 },    0, INT_MAX, DEC },
 
831
    { "video_size",   "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size),   AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       DEC },
 
832
    { "pixel_format", "Preferred pixel format",                                    OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       DEC },
 
833
    { "input_format", "Preferred pixel format (for raw video) or codec name",      OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       DEC },
 
834
    { "framerate",    "",                                                          OFFSET(framerate),    AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       DEC },
 
835
    { "list_formats", "List available formats and exit",                           OFFSET(list_format),  AV_OPT_TYPE_INT,    {.dbl = 0 },  0, INT_MAX, DEC, "list_formats" },
 
836
    { "all",          "Show all available formats",                                OFFSET(list_format),  AV_OPT_TYPE_CONST,  {.dbl = V4L_ALLFORMATS  },    0, INT_MAX, DEC, "list_formats" },
 
837
    { "raw",          "Show only non-compressed formats",                          OFFSET(list_format),  AV_OPT_TYPE_CONST,  {.dbl = V4L_RAWFORMATS  },    0, INT_MAX, DEC, "list_formats" },
 
838
    { "compressed",   "Show only compressed formats",                              OFFSET(list_format),  AV_OPT_TYPE_CONST,  {.dbl = V4L_COMPFORMATS },    0, INT_MAX, DEC, "list_formats" },
738
839
    { NULL },
739
840
};
740
841
 
746
847
};
747
848
 
748
849
AVInputFormat ff_v4l2_demuxer = {
749
 
    "video4linux2",
750
 
    NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
751
 
    sizeof(struct video_data),
752
 
    NULL,
753
 
    v4l2_read_header,
754
 
    v4l2_read_packet,
755
 
    v4l2_read_close,
756
 
    .flags = AVFMT_NOFILE,
757
 
    .priv_class = &v4l2_class,
 
850
    .name           = "video4linux2",
 
851
    .long_name      = NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
 
852
    .priv_data_size = sizeof(struct video_data),
 
853
    .read_header    = v4l2_read_header,
 
854
    .read_packet    = v4l2_read_packet,
 
855
    .read_close     = v4l2_read_close,
 
856
    .flags          = AVFMT_NOFILE,
 
857
    .priv_class     = &v4l2_class,
758
858
};