# ffmpeg + video4linux2 problem



## pashap (Oct 30, 2014)

Hi,

I try to capture video from a USB camera but it won't work. I installed webcamd, ffmpeg and pwcview. pwcview successfully got a picture from the USB camera but ffmpeg can't do it.


```
# env | grep LD_PRELOAD
LD_PRELOAD=/usr/local/lib/libv4l/v4l1compat.so
# ffmpeg -f video4linux2 -i /dev/video0 -vcodec mpeg4 -f avi test.avi

ffmpeg version 2.3.3 Copyright (c) 2000-2014 the FFmpeg developers
  built on Oct 28 2014 23:57:50 with FreeBSD clang version 3.3 (tags/RELEASE_33/final 183502) 20130610
  configuration: --disable-libaacplus --disable-indev=alsa --disable-outdev=alsa --disable-libopencore-amrnb --disable-libopencore-amrwb --disable-libass --disable-libcdio --disable-libcelt --enable-libfaac --disable-libfdk-aac --disable-ffserver --enable-fontconfig --enable-libfreetype --enable-frei0r --disable-gnutls --disable-libgsm --enable-iconv --disable-indev=jack --enable-libmp3lame --disable-libbluray --enable-libv4l2 --disable-libmodplug --disable-openal --disable-indev=openal --enable-libopencv --disable-libopenjpeg --disable-libopus --disable-libpulse --disable-indev=pulse --disable-outdev=pulse --disable-librtmp --enable-libschroedinger --disable-libspeex --enable-libtheora --disable-vaapi --disable-vdpau --enable-libvorbis --disable-libvo-aacenc --disable-libvo-amrwbenc --enable-libvpx --enable-libx264 --disable-libx265 --enable-libxvid --prefix=/usr/local --mandir=/usr/local/man --datadir=/usr/local/share/ffmpeg --enable-shared --enable-gpl --enable-postproc --enable-avfilter --enable-avresample --enable-pthreads --enable-memalign-hack --disable-libstagefright-h264 --disable-libutvideo --disable-libsoxr --cc=cc --extra-cflags='-msse -I/usr/local/include/vorbis -I/usr/local/include' --extra-ldflags='-L/usr/local/lib ' --extra-libs=-pthread --disable-debug --disable-ffplay --disable-outdev=sdl --enable-nonfree
  libavutil      52. 92.100 / 52. 92.100
  libavcodec     55. 69.100 / 55. 69.100
  libavformat    55. 48.100 / 55. 48.100
  libavdevice    55. 13.102 / 55. 13.102
  libavfilter     4. 11.100 /  4. 11.100
  libavresample   1.  3.  0 /  1.  3.  0
  libswscale      2.  6.100 /  2.  6.100
  libswresample   0. 19.100 /  0. 19.100
  libpostproc    52.  3.100 / 52.  3.100
libv4l2: error mmapping buffer 16: Invalid argument
[video4linux2,v4l2 @ 0x808050420] ioctl(VIDIOC_QBUF): Invalid argument
libv4l2: warning v4l2 mmap buffers still mapped on close()
/dev/video0: Invalid argument
```
truss:

```
<cut>
libv4l2: error mmapping buffer 16: Invalid argument
write(2,"libv4l2: error mmapping buffer 1"...,52) = 52 (0x34)
[video4linux2,v4l2 @ 0x808050420] write(2,"\^[[0;35m[video4linux2,v4l2 @ 0x"...,45) = 45 (0x2d)
ioctl(VIDIOC_QBUF): Invalid argument
write(2,"\^[[1;31mioctl(VIDIOC_QBUF): Inv"...,48) = 48 (0x30)
munmap(0x800685000,460800)                       = 0 (0x0)
munmap(0x8006f6000,460800)                       = 0 (0x0)
munmap(0x800767000,460800)                       = 0 (0x0)
munmap(0x80798e000,460800)                       = 0 (0x0)
munmap(0x8079ff000,460800)                       = 0 (0x0)
munmap(0x807a70000,460800)                       = 0 (0x0)
munmap(0x807ae1000,460800)                       = 0 (0x0)
munmap(0x807b52000,460800)                       = 0 (0x0)
munmap(0x828400000,460800)                       = 0 (0x0)
munmap(0x828471000,460800)                       = 0 (0x0)
munmap(0x8284e2000,460800)                       = 0 (0x0)
munmap(0x828553000,460800)                       = 0 (0x0)
munmap(0x8285c4000,460800)                       = 0 (0x0)
munmap(0x828635000,460800)                       = 0 (0x0)
munmap(0x8286a6000,460800)                       = 0 (0x0)
munmap(0x828717000,460800)                       = 0 (0x0)
libv4l2: warning v4l2 mmap buffers still mapped on close()
write(2,"libv4l2: warning v4l2 mmap buffe"...,59) = 59 (0x3b)
close(3)                                         = 0 (0x0)
madvise(0x80802c000,0x1000,0x5,0xaaaaaaaaaaaaaaab,0x808000b58,0x80374cb10) = 0 (0x0)
madvise(0x808031000,0x2000,0x5,0xaaaaaaaaaaaaaaab,0x808000b58,0x80374cb10) = 0 (0x0)
madvise(0x808064000,0x1000,0x5,0xaaaaaaaaaaaaaaab,0x808000b58,0x80374cb10) = 0 (0x0)
madvise(0x80806d000,0x1000,0x5,0xaaaaaaaaaaaaaaab,0x808000b58,0x80374cb10) = 0 (0x0)
madvise(0x80807d000,0x4000,0x5,0xaaaaaaaaaaaaaaab,0x808000b58,0x80374cb10) = 0 (0x0)
madvise(0x808046000,0x1000,0x5,0xaaaaaaaaaaaaaaab,0x808000708,0x80374cb10) = 0 (0x0)
madvise(0x80804f000,0x1000,0x5,0xaaaaaaaaaaaaaaab,0x808000708,0x80374cb10) = 0 (0x0)
/dev/video0: Invalid argument
write(2,"\^[[1;31m/dev/video0: Invalid ar"...,41) = 41 (0x29)
madvise(0x808047000,0x8000,0x5,0xaaaaaaaaaaaaaaab,0x8080008a0,0x80374cb10) = 0 (0x0)
madvise(0x808050000,0x1000,0x5,0xaaaaaaaaaaaaaaab,0x8080008a0,0x80374cb10) = 0 (0x0)
madvise(0x808060000,0x1000,0x5,0xaaaaaaaaaaaaaaab,0x8080008a0,0x80374cb10) = 0 (0x0)
ioctl(0,TIOCSETA,0x62c748)                       = 0 (0x0)
process exit, rval = 1
```

On a Linux host this works fine. How can I resolve this issue?

Thanks.


----------



## justwantask (Oct 31, 2014)

I have the same error. It seems like a bug and needs a PR.


----------



## hselasky@ (Nov 1, 2014)

Hi,

Just make a PR and assign it to me. I've seen a similar issue myself, but didn't hunt it down yet.

--HPS


----------



## justwantask (Nov 1, 2014)

Thanks for the response, I wrote a PR,  PR 194734, how do I assign it to you?


----------



## hselasky@ (Nov 1, 2014)

Hi,

The problem is that the following MMAP constant inside webcamd is too small:


```
kernel/linux_struct.h:#define   LINUX_VMA_MAX 16
```

Change it to 128, and ffmpeg will work. It simply runs out of MMAP slots.

I'm planning a webcamd update.

--HPS


----------



## hselasky@ (Nov 1, 2014)

I've updated PR 194734. Please check and verify my solution.

--HPS


----------



## justwantask (Nov 2, 2014)

Thank you, now ffmpeg works fine!


----------



## pashap (Nov 5, 2014)

Thank you, now ffmpeg works with a USB camera.


----------



## pashap (Nov 5, 2014)

But I have problem with dropped frames. Why does this happen?

`# ffmpeg -f video4linux2 -s 1920x1080 -pix_fmt uyvy422 -i /dev/video0 -c:v libx264 -an -pix_fmt yuv420p -f avi test2.avi`

```
ffmpeg version 2.3.5 Copyright (c) 2000-2014 the FFmpeg developers
  built on Nov  5 2014 23:41:35 with FreeBSD clang version 3.3 (tags/RELEASE_33/final 183502) 20130610
  configuration: --disable-libaacplus --disable-indev=alsa --disable-outdev=alsa --disable-libopencore-amrnb --disable-libopencore-amrwb --disable-libass --disable-libcdio --disable-libcelt --enable-libfaac --disable-libfdk-aac --disable-ffserver --enable-fontconfig --enable-libfreetype --enable-frei0r --disable-gnutls --disable-libgsm --enable-iconv --disable-indev=jack --enable-libmp3lame --disable-libbluray --enable-libv4l2 --disable-libmodplug --disable-openal --disable-indev=openal --enable-libopencv --disable-libopenjpeg --disable-libopus --disable-libpulse --disable-indev=pulse --disable-outdev=pulse --disable-librtmp --enable-libschroedinger --disable-libspeex --enable-libtheora --disable-vaapi --disable-vdpau --enable-libvorbis --disable-libvo-aacenc --disable-libvo-amrwbenc --enable-libvpx --enable-libx264 --disable-libx265 --enable-libxvid --prefix=/usr/local --mandir=/usr/local/man --datadir=/usr/local/share/ffmpeg --enable-shared --enable-gpl --enable-postproc --enable-avfilter --enable-avresample --enable-pthreads --enable-memalign-hack --disable-libstagefright-h264 --disable-libutvideo --disable-libsoxr --cc=cc --extra-cflags='-msse -I/usr/local/include/vorbis -I/usr/local/include' --extra-ldflags='-L/usr/local/lib ' --extra-libs=-pthread --disable-debug --disable-ffplay --disable-outdev=sdl --enable-nonfree
  libavutil      52. 92.100 / 52. 92.100
  libavcodec     55. 69.100 / 55. 69.100
  libavformat    55. 48.100 / 55. 48.100
  libavdevice    55. 13.102 / 55. 13.102
  libavfilter     4. 11.100 /  4. 11.100
  libavresample   1.  3.  0 /  1.  3.  0
  libswscale      2.  6.100 /  2.  6.100
  libswresample   0. 19.100 /  0. 19.100
  libpostproc    52.  3.100 / 52.  3.100
Input #0, video4linux2,v4l2, from '/dev/video0':
  Duration: N/A, start: 49594.020175, bitrate: 165888 kb/s
    Stream #0:0: Video: rawvideo (YUY2 / 0x32595559), yuyv422, 1920x1080, 165888 kb/s, 5 fps, 5 tbr, 1000k tbn, 1000k tbc
[libx264 @ 0x807c6e020] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x807c6e020] profile High, level 4.0
Output #0, avi, to 'test2.avi':
  Metadata:
    ISFT            : Lavf55.48.100
    Stream #0:0: Video: h264 (libx264) (H264 / 0x34363248), yuv420p, 1920x1080, q=-1--1, 5 fps, 5 tbn, 5 tbc
    Metadata:
      encoder         : Lavc55.69.100 libx264
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo (native) -> h264 (libx264))
[avi @ 0x807c50a20] H.264 bitstream error, startcode missingbitrate=24003.4kbits/s dup=0 drop=322   
    Last message repeated 37 times
[avi @ 0x807c50a20] H.264 bitstream error, startcode missingbitrate=1602.3kbits/s dup=0 drop=322   
    Last message repeated 30 times
[avi @ 0x807c50a20] H.264 bitstream error, startcode missingbitrate=1857.2kbits/s dup=0 drop=396   
    Last message repeated 37 times
[avi @ 0x807c50a20] H.264 bitstream error, startcode missingbitrate=1467.4kbits/s dup=0 drop=468   
[avi @ 0x807c50a20] H.264 bitstream error, startcode missingbitrate=1539.7kbits/s dup=0 drop=470   
    Last message repeated 7 times
[avi @ 0x807c50a20] H.264 bitstream error, startcode missingbitrate=1608.2kbits/s dup=0 drop=470   
    Last message repeated 36 times
[avi @ 0x807c50a20] H.264 bitstream error, startcode missingbitrate=1907.3kbits/s dup=0 drop=490   
    Last message repeated 57 times
[avi @ 0x807c50a20] H.264 bitstream error, startcode missingbitrate=1609.7kbits/s dup=0 drop=490   
    Last message repeated 112 times
[avi @ 0x807c50a20] H.264 bitstream error, startcode missingbitrate=1174.6kbits/s dup=0 drop=566   
    Last message repeated 169 times
frame=  106 fps=0.7 q=-1.0 Lsize=   19266kB time=00:01:59.60 bitrate=1319.6kbits/s dup=0 drop=568   
video:19246kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.102140%
[libx264 @ 0x807c6e020] frame I:1     Avg QP:18.81  size:212174
[libx264 @ 0x807c6e020] frame P:54    Avg QP:17.79  size:206249
[libx264 @ 0x807c6e020] frame B:51    Avg QP:19.12  size:163895
[libx264 @ 0x807c6e020] consecutive B-frames:  3.8% 96.2%  0.0%  0.0%
[libx264 @ 0x807c6e020] mb I  I16..4:  3.7% 88.1%  8.1%
[libx264 @ 0x807c6e020] mb P  I16..4:  0.7% 35.2%  1.7%  P16..4: 23.0% 26.1% 13.1%  0.0%  0.0%    skip: 0.0%
[libx264 @ 0x807c6e020] mb B  I16..4:  0.2% 19.2%  1.1%  B16..8: 31.0% 14.4%  4.0%  direct:26.1%  skip: 4.1%  L0:51.0% L1:28.1% BI:21.0%
[libx264 @ 0x807c6e020] 8x8 transform intra:93.4% inter:71.9%
[libx264 @ 0x807c6e020] coded y,uvDC,uvAC intra: 94.6% 99.9% 99.6% inter: 68.2% 96.1% 93.0%
[libx264 @ 0x807c6e020] i16 v,h,dc,p:  3%  2%  0% 95%
[libx264 @ 0x807c6e020] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 16% 14% 20%  6%  9%  8%  9%  8% 10%
[libx264 @ 0x807c6e020] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 17% 15% 12%  8% 13% 10% 10%  7%  8%
[libx264 @ 0x807c6e020] i8c dc,h,v,p: 75% 11%  6%  9%
[libx264 @ 0x807c6e020] Weighted P-Frames: Y:5.6% UV:3.7%
[libx264 @ 0x807c6e020] ref P L0: 34.2%  9.9% 30.6% 23.8%  1.5%
[libx264 @ 0x807c6e020] ref B L0: 53.6% 46.4%
[libx264 @ 0x807c6e020] kb/s:956.71
```


----------

