How to capture hardware MJPEG stream using mencoder?

Hi,

I have a web cam that supports hardware MJPEG stream:

user@router:~> v4l2-ctl --list-formats
ioctl: VIDIOC_ENUM_FMT
        Index       : 0
        Type        : Video Capture
        Pixel Format: 'MJPG' (compressed)
        Name        : MJPEG

        Index       : 1
        Type        : Video Capture
        Pixel Format: 'YUYV'
        Name        : YUV 4:2:2 (YUYV)

user@router:~> v4l2-ctl -d /dev/video0 -v width=960,height=720,pixelformat=MJPEG

user@router:~> v4l2-ctl --all
Driver Info (not using libv4l2):
        Driver name   : uvcvideo
        Card type     : UVC Camera (046d:0991)
        Bus info      : usb-0000:00:1d.7-4
        Driver version: 3.4.6
        Capabilities  : 0x04000001
                Video Capture
                Streaming
Format Video Capture:
        Width/Height  : 960/720
        Pixel Format  : 'MJPG'
        Field         : None
        Bytes per Line: 0
        Size Image    : 409600
        Colorspace    : SRGB
Crop Capability Video Capture:
        Bounds      : Left 0, Top 0, Width 960, Height 720
        Default     : Left 0, Top 0, Width 960, Height 720
        Pixel Aspect: 1/1
Video input : 0 (Camera 1: ok)
Streaming Parameters Video Capture:
        Capabilities     : timeperframe
        Frames per second: 10.000 (10/1)
        Read buffers     : 0

I want to capture raw mjpeg stream without re-encode or transcode.

VLC does that:

user@router:~> vlc -I dummy v4l2:///dev/video0:width=960:height=720:fps=15 :v4l2-chroma=MJPG :v4l2-fps=15  --sout=file/asf:mjpeg-vlc.asf

VLC media player 2.0.3 Twoflower (revision 2.0.2-93-g77aa89e)
[0x88c51d8] main interface error: no suitable interface module
[0x882d3f0] main libvlc error: interface "globalhotkeys,none" initialization failed
[0x88c51d8] dummy interface: using the dummy interface module...
^C

user@router:~mediainfo mjpeg-vlc.asf 
General
Complete name                            : mjpeg-vlc.asf
Format                                   : Windows Media
File size                                : 5.28 MiB
Duration                                 : 8s 532ms
Overall bit rate                         : 5 189 Kbps
Maximum Overall bit rate                 : 1 bps

Video
ID                                       : 1
Format                                   : JPEG
Codec ID                                 : MJPG
Description of the codec                 : Unknown Video
Bit rate                                 : 4 977 Kbps
Width                                    : 960 pixels
Height                                   : 720 pixels
Display aspect ratio                     : 4:3
Color space                              : YUV
Bit depth                                : 8 bits
Compression mode                         : Lossy

ffmpeg does that:

user@router:~> ffmpeg -f video4linux2 -vcodec mjpeg -r 15 -i /dev/video0 -vcodec copy mjpeg-ffmpeg.avi

ffmpeg version 0.11.1 Copyright (c) 2000-2012 the FFmpeg developers
  built on Jun 10 2012 22:43:48 with gcc 4.7.1 20120723 [gcc-4_7-branch revision 189773]
   configuration: --shlibdir=/usr/lib --prefix=/usr  --mandir=/usr/share/man --libdir=/usr/lib --enable-shared  --disable-static --enable-debug --disable-stripping  --extra-cflags='-fomit-frame-pointer -fmessage-length=0 -O2 -Wall  -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables  -fasynchronous-unwind-tables -g -D_LARGEFILE_SOURCE  -D_FILE_OFFSET_BITS=64 -I/usr/include/gsm' --enable-gpl --enable-x11grab  --enable-version3 --enable-pthreads --enable-avfilter --enable-libpulse  --enable-libvpx --enable-libmp3lame --enable-libvorbis  --enable-libtheora --enable-libspeex --enable-libxvid --enable-libx264  --enable-libschroedinger --enable-libgsm --enable-libopencore-amrnb  --enable-libopencore-amrwb --enable-postproc --enable-libdc1394  --enable-librtmp --enable-libfreetype --enable-avresample
  WARNING: library configuration mismatch
   avutil      configuration: --enable-shared --disable-static  --disable-doc --prefix=/usr --libdir=/usr/lib --shlibdir=/usr/lib  --disable-stripping --extra-cflags='-fomit-frame-pointer  -fmessage-length=0 -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector  -funwind-tables -fasynchronous-unwind-tables -g'
  libavutil      51. 54.100 / 51. 54.100
  libavcodec     54. 23.100 / 54. 23.100
  libavformat    54.  6.100 / 54.  6.100
  libavdevice    54.  0.100 / 54.  0.100
  libavfilter     2. 77.100 /  2. 77.100
  libswscale      2.  1.100 /  2.  1.100
  libswresample   0. 15.100 /  0. 15.100
  libpostproc    52.  0.100 / 52.  0.100
[video4linux2,v4l2 @ 0x8f7d540] Estimating duration from bitrate, this may be inaccurate
Input #0, video4linux2,v4l2, from '/dev/video0':
  Duration: N/A, start: 11156.932290, bitrate: N/A
    Stream #0:0: Video: mjpeg, yuvj422p, 960x720, -2 kb/s, 15 tbr, 1000k tbn, 15 tbc
Output #0, avi, to 'mjpeg-ffmpeg.avi':
  Metadata:
    ISFT            : Lavf54.6.100
    Stream #0:0: Video: mjpeg (MJPG / 0x47504A4D), yuvj422p, 960x720, q=2-31, -2 kb/s, 30 tbn, 30 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, ?] for help
frame=   96 fps=9.1 q=-1.0 Lsize=    8045kB time=00:00:10.56 bitrate=6237.0kbits/s    
video:8032kB audio:0kB global headers:0kB muxing overhead 0.162122%
Received signal 2: terminating.

user@router:~> mediainfo mjpeg-ffmpeg.avi 
General
Complete name                            : mjpeg-ffmpeg.avi
Format                                   : AVI
Format/Info                              : Audio Video Interleave
File size                                : 7.86 MiB
Duration                                 : 10s 567ms
Overall bit rate                         : 6 237 Kbps
Writing application                      : Lavf54.6.100

Video
ID                                       : 0
Format                                   : JPEG
Codec ID                                 : MJPG
Duration                                 : 10s 567ms
Bit rate                                 : 6 227 Kbps
Width                                    : 960 pixels
Height                                   : 720 pixels
Display aspect ratio                     : 4:3
Frame rate                               : 30.000 fps
Color space                              : YUV
Chroma subsampling                       : 4:2:2
Bit depth                                : 8 bits
Compression mode                         : Lossy
Bits/(Pixel*Frame)                       : 0.300
Stream size                              : 7.84 MiB (100%)


But mencoder always captures YUV instead of mjpeg:

user@router:~> mencoder tv:// -tv driver=v4l2:width=960:height=720:fps=15:mjpeg -nosound -ovc copy -o mjpeg-mencoder.avi

MEncoder dev-SVN-r35127-4.7-openSUSE Linux 12.2 (i586)-Packman (C) 2000-2012 MPlayer Team
success: format: 9  data: 0x0 - 0x0
TV file format detected.
Selected driver: v4l2
 name: Video 4 Linux 2 input
 author: Martin Olschewski <olschewski@zpr.uni-koeln.de>
 comment: first try, more to come ;-)
v4l2: your device driver does not support VIDIOC_G_STD ioctl, VIDIOC_G_PARM was used instead.
Selected device: UVC Camera (046d:0991)
 Capabilities:  video capture  streaming
 supported norms:
 inputs: 0 = Camera 1;
 Current input: 0
 Current format: MJPEG
v4l2: ioctl set format failed: Invalid argument
v4l2: ioctl set format failed: Invalid argument
v4l2: ioctl set format failed: Invalid argument
tv.c: norm_from_string(pal): Bogus norm parameter, setting default.
v4l2: ioctl enum norm failed: Invalid argument
Error: Cannot set norm!
Selected input hasn't got a tuner!
v4l2: ioctl set mute failed: Invalid argument
v4l2: ioctl query control failed: Invalid argument
[V] filefmt:9  fourcc:0x32595559  size:960x720  fps:15.000  ftime:=0.0667
videocodec: framecopy (960x720 24bpp fourcc=32595559)
Forcing audio preload to 0, max pts correction to 0.
v4l2: select timeout
Writing header...
ODML: Aspect information not (yet?) available or unspecified, not writing vprp header.
Writing header...
ODML: Aspect information not (yet?) available or unspecified, not writing vprp header.
Pos:   0.0s      1f ( 0%)  0.65fps Trem:   0min   0mb  A-V:0.000 [0:0]
v4l2: select timeout

Skipping frame!
Pos:   0.1s      2f ( 0%)  0.84fps Trem:   0min   0mb  A-V:0.000 [0:0]

Skipping frame!
Pos:   0.1s      3f ( 0%)  0.98fps Trem:   0min   0mb  A-V:0.000 [0:0]
v4l2: select timeout

Skipping frame!
Pos:   0.1s      4f ( 0%)  1.01fps Trem:   0min   0mb  A-V:0.000 [0:0]
v4l2: select timeout

Skipping frame!
Pos:   0.1s      5f ( 0%)  1.04fps Trem:   0min   0mb  A-V:0.000 [0:0]

Skipping frame!
Pos:   0.2s      9f ( 0%)  1.63fps Trem:   0min   0mb  A-V:0.000 [0:0]

1 duplicate frame(s)!
Pos:   0.3s     10f ( 0%)  1.77fps Trem:   0min   0mb  A-V:0.000 [0:0]

1 duplicate frame(s)!
Pos:   0.5s     12f ( 0%)  2.04fps Trem:   0min   0mb  A-V:0.000 [0:0]

1 duplicate frame(s)!
Pos:   0.7s     14f ( 0%)  2.29fps Trem:   0min   0mb  A-V:0.000 [0:0]

1 duplicate frame(s)!
Pos:   0.9s     16f ( 0%)  2.47fps Trem:   0min   0mb  A-V:0.000 [0:0]

1 duplicate frame(s)!
Pos:   1.1s     18f ( 0%)  2.77fps Trem:   0min   0mb  A-V:0.000 [126855:0]

1 duplicate frame(s)!
Pos:   1.3s     20f ( 0%)  2.99fps Trem:   0min   0mb  A-V:0.000 [124416:0]

1 duplicate frame(s)!
Pos:   1.4s     21f ( 0%)  3.05fps Trem:   0min   0mb  A-V:0.000 [126390:0]

1 duplicate frame(s)!
Pos:   1.5s     22f ( 0%)  3.20fps Trem:   0min   0mb  A-V:0.000 [122612:0]

1 duplicate frame(s)!
^C
s:   1.7s     23f ( 0%)  3.25fps Trem:   0min   0mb  A-V:0.000 [119439:0]

1 duplicate frame(s)!
Pos:   1.8s     24f ( 0%)  3.31fps Trem:   0min   0mb  A-V:0.000 [116735:0]
Writing index...
Writing header...
ODML: Aspect information not (yet?) available or unspecified, not writing vprp header.

Video stream: 108685.242 kbit/s  (13585655 B/s)  size: 26265600 bytes  1.933 secs  24 frames
v4l2: ioctl set mute failed: Invalid argument
v4l2: 21 frames successfully processed, 13 frames dropped.

user@router:~> mediainfo mjpeg-mencoder.avi 
General
Complete name                            : mjpeg-mencoder.avi
Format                                   : AVI
Format/Info                              : Audio Video Interleave
File size                                : 25.1 MiB
Duration                                 : 1s 933ms
Overall bit rate                         : 109 Mbps
Writing application                      : MEncoder dev-SVN-r35127-4.7-openSUSE Linux 12.2 (i586)-Packman
Writing library                          : MPlayer

Video
ID                                       : 0
Format                                   : YUV
Codec ID                                 : YUY2
Codec ID/Info                            : YUV 4:2:2 as for UYVY but with different component ordering within the u_int32 macropixel
Duration                                 : 1s 933ms
Bit rate                                 : 109 Mbps
Width                                    : 960 pixels
Height                                   : 720 pixels
Display aspect ratio                     : 4:3
Frame rate                               : 15.000 fps
Color space                              : YUV
Chroma subsampling                       : 4:2:2
Compression mode                         : Lossless
Bits/(Pixel*Frame)                       : 10.483
Stream size                              : 25.0 MiB (100%)


How to force mencoder to read MJPEG stream?

Ok, figured that out:

mencoder tv:// -tv driver=v4l2:width=960:height=720:fps=15:outfmt=mjpeg -nosound -ovc copy -o test.avi

:wink: