video_io.q

Video input/output library for Quasar.  This library is a wrapper around FFMPEG encoding / decoding routines, and provides a small number of functions that make video processing really easy.  Nevertheless, it is possible to configure many of the encoding settings.

Summary
video_io.qVideo input/output library for Quasar.
video I/O
vidplayOpens and plays the specified video file using TNGVideoPlayer.
vidopenOpens the specified video file for reading (and decoding)
vidsaveOpens the specified video file for writing (by encoding)
vidreadframeReads and decodes one frame of the video sequence
vidwriteframeEncodes and writes one frame to an output video stream
Video camera inputsTo connect to a video camera, it suffices to use special filenames that start with “cam:” (called camera connection string).
Supported video codecsThis video library supports all video codecs of FFMPEG.

video I/O

vidplay

Opens and plays the specified video file using TNGVideoPlayer.  The function also allows for real-time video filtering through the “filter” argument.

function [] = vidplay(filename : string)
function [] = vidplay(filename : string, filter : [?? -> ()])

Parameters

filenamethe filename of the video to play.  Note that webcams can also be specified as filename (see Video camera inputs).
filtera real-time filtering function to apply to the video data.
function [] = filter(x)

Parameters

x.framea matrix (“cube”) of dimensions [x.frame_height x x.frame_width x 3] containing RGB color values for the frame that needs to be processed.
x.frame_widththe width of the frame
x.frame_heightthe height of the frame
x.spat_ups_factorTNGVideoPlayer’s spatial upsampling factor
x.temp_ups_factorTNGVideoPlayer’s temporal upsampling factor
x.mb_size_xthe width of the macroblocks (for MPEG type codecs)
x.mb_size_ythe height of the macroblocks (for MPEG type codes)
x.av_sync_sourcethe audio/video synchronization source
0 (AV_SYNC_AUDIO_MASTER) - synchronizes to the audio signal
1 (AV_SYNC_VIDEO_MASTER) - synchronizes to the video signal
2 (AV_SYNC_EXTERNAL_CLOCK) - synchronizes to the external clock
x.ptsthe presentation timestamp of the frame (in seconds)
x.temp_subframe_poswhen upsampling temporally, gives the index (from 0..x.temp_ups_factor-1) of the current sub-frame
x.video_clockthe video clock (in seconds)
x.master_clockthe master clock (in seconds)

Notes

  • The function vidplay is blocking.  This means that no other actions can be performed rather then processing the video.  Also only one video can be processed at the time.  If this is not desired, have a look at the functions vidopen, vidreadframe.
  • Below is a simple video filter to illustrate the real-time video processing capabilities of this library.

Example

function [] = filter(x)
    function [] = __kernel__ kernel (y : cube, pos : ivec2)
        x = y[pos[0],pos[1],0..2]

        if pos[0] > 100 && pos[0] < size(y,0)-100 && _
           pos[1] > 100 && pos[1] < size(y,1)-100

            % switch R and B
            tmp = x[0]
            x[2] = tmp
            x[0] = x[2]
            y[pos[0],pos[1],0..2] = x
        endif
    end
    im = x.frame
    parallel_do(size(im,0..1),im,kernel)
end

file_name = "cam:video=Integrated Webcam"
vidplay(file_name, filter)

vidopen

Opens the specified video file for reading (and decoding)

function stream = vidopen(file_name : string)

Parameters

file_namethe filename of the video to play.  Note that webcams can also be specified as filename (see Video camera inputs).
streamthe input video stream
stream.rgb_datacontains RGB color data of the last decoded frame (the type is ‘cube[uint8]’)
stream.ptsthe presentation timestamp of the last decoded frame, in seconds.
stream.cam_inputindicates whether the stream corresponds to a camera input stream
stream.frame_widththe frame width of the video
stream.frame_heightthe frame height of the video
stream.frame_bytesthe number of (uncompressed) bytes needed to store one frame (for buffering purposes)
stream.duration_secthe duration of the video stream (in seconds)
stream.avg_frame_ratethe average frame rate
stream.video_bit_ratethe bit rate of the video stream (in bits/second)
stream.pixel_aspect_ratiothe aspect ratio of an individual pixel
stream.codecthe name of the codec (see Supported video codecs)

Notes

  • The function vidopen allows multiple video streams to be opened and processed simultaneously.

vidsave

Opens the specified video file for writing (by encoding)

function stream = vidsave(file_name : string, codec_settings)

Parameters

file_namethe filename of the video to write to.
codec_settingsan untyped object (object) containing encoder settings
codec_settings.video_bit_ratethe bit rate of the video stream (in bits/second)
codec_settings.encoderthe name of the codec (see Supported video codecs) Specify an empty string “” to automatically detect the codec from the file extension.
codec_settings.frame_widththe frame width of the video
codec_settings.frame_heightthe frame height of the video
codec_settings.avg_frame_ratethe average frame rate of the video (e.g.  25)
codec_settings.gop_sizethe size of one video group of pictures (GOP) - contains the maximal periodicity of I frames.
streamthe output video stream
stream.frame_widththe frame width of the video
stream.frame_heightthe frame height of the video
stream.frame_bytesthe number of (uncompressed) bytes needed to store one frame (for buffering purposes)
stream.duration_secthe duration of the video stream (in seconds)
stream.avg_frame_ratethe average frame rate
stream.video_bit_ratethe bit rate of the video stream (in bits/second)
stream.pixel_aspect_ratiothe aspect ratio of an individual pixel
stream.codecthe name of the selected codec (see Supported video codecs)

Notes

  • The function vidsave allows multiple video streams to be saved simultaneously.
  • When codec_settings.frame_width and codec_settings.frame_height do not match the input data, automatic frame rescaling will be performed, using bicubic interpolation.

vidreadframe

Reads and decodes one frame of the video sequence

function RGB_data : cube[uint8] = vidreadframe(stream)

Parameters

streama video stream, opened with vidopen
RGB_datathe RGB color data for the frame (type cube[uint8])

vidwriteframe

Encodes and writes one frame to an output video stream

function [] = vidwriteframe(stream, pts : scalar, RGB_data : cube[uint8])

Parameters

streama video stream, opened with vidsave
ptsthe presentation timestamp of the frame, in seconds.
RGB_datathe RGB color data for the frame (type cube[uint8])

Video camera inputs

To connect to a video camera, it suffices to use special filenames that start with “cam:” (called camera connection string).  On Windows platforms, the video camera is accessed using the DirectShow API.  The user specifies which camera to use with the filename:

cam:video=[<camera_name>],video_size=[XXxYY],frame_rate=XX

For example

file_name = "cam:video=Integrated Webcam"
file_name = "cam:video=QuickCam Orbit/Sphere MP,video_size=640x480,frame_rate=30"

Remark that it is also possible to specify the frame size and frame rate, through the camera connection string.  You can list the installed cameras using the FFMPEG tool:

ffmpeg -f dshow -list_devices true -i list

On Linux platforms, we support two types of devices: VideoForLinux and DV1394.  Again, the user specifies which camera to use with the filename:

cam:[device_type][:standard]

where device_type - ‘v4l’ for VideoForLinux, ‘dv1394’ for DV1394.  By default ‘v4l’ standard - ‘pal’, ‘secam’ or ‘ntsc’.  By default ‘ntsc’ The driver name is constructed automatically from the device type:

v4l   : /dev/video<camIdx>
dv1394: /dev/dv1394/<camIdx>

If you have different driver name, you can specify the driver name explicitly instead of device type.  Examples of valid camera connection strings:

cam:/dev/v4l/video0:pal
cam:/dev/ieee1394/1:ntsc
cam:dv1394:secam
cam:v4l:pal

Supported video codecs

This video library supports all video codecs of FFMPEG.  You can obtain a list through the command

ffmpeg -decoders
ffmpeg -encoders

The supported video formats are listed below.

V..... = Video
A..... = Audio
S..... = Subtitle
.F.... = Frame-level multithreading
..S... = Slice-level multithreading
...X.. = Codec is experimental
....B. = Supports draw_horiz_band
.....D = Supports direct rendering method 1
V.S.BD mpeg1video           MPEG-1 video
V.S.BD mpeg2video           MPEG-2 video
V.S.BD mpegvideo            MPEG-1 video (codec mpeg2video)
V....D h261                 H.261
V...BD h263                 H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2
V....D rv10                 RealVideo 1.0
V....D rv20                 RealVideo 2.0
V....D mjpeg                MJPEG (Motion JPEG)
V....D mjpegb               Apple MJPEG-B
V....D sp5x                 Sunplus JPEG (SP5X)
V....D jpegls               JPEG-LS
VF..BD mpeg4                MPEG-4 part 2
V..... rawvideo             raw video
V...BD msmpeg4v1            MPEG-4 part 2 Microsoft variant version 1
V...BD msmpeg4v2            MPEG-4 part 2 Microsoft variant version 2
V...BD msmpeg4              MPEG-4 part 2 Microsoft variant version 3 (codec msmpeg4v3)
V...BD wmv1                 Windows Media Video 7
V...BD wmv2                 Windows Media Video 8
V...BD h263p                H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2
V...BD h263i                Intel H.263
V...BD flv                  FLV / Sorenson Spark / Sorenson H.263 (Flash Video) (codec flv1)
V....D svq1                 Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1
V...BD svq3                 Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3
V.S..D dvvideo              DV (Digital Video)
VF..BD huffyuv              Huffyuv / HuffYUV
V....D cyuv                 Creative YUV (CYUV)
VFS..D h264                 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
V..... indeo3               Intel Indeo 3
VF..BD vp3                  On2 VP3
VF..BD theora               Theora
V....D asv1                 ASUS V1
V....D asv2                 ASUS V2
V.S..D ffv1                 FFmpeg video codec #1
V....D 4xm                  4X Movie
V....D vcr1                 ATI VCR1
V....D cljr                 Cirrus Logic AccuPak
VF...D mdec                 Sony PlayStation MDEC (Motion DECoder)
V....D roqvideo             id RoQ video (codec roq)
V....D interplayvideo       Interplay MVE video
V....D xan_wc3              Wing Commander III / Xan
V....D xan_wc4              Wing Commander IV / Xxan
V....D rpza                 QuickTime video (RPZA)
V....D cinepak              Cinepak
V....D vqavideo             Westwood Studios VQA (Vector Quantized Animation) video (codec ws_vqa)
V....D msrle                Microsoft RLE
V....D msvideo1             Microsoft Video 1
V....D idcinvideo           id Quake II CIN video (codec idcin)
V....D 8bps                 QuickTime 8BPS video
V....D smc                  QuickTime Graphics (SMC)
V....D flic                 Autodesk Animator Flic video
V....D truemotion1          Duck TrueMotion 1.0
V....D vmdvideo             Sierra VMD video
V....D mszh                 LCL (LossLess Codec Library) MSZH
V....D zlib                 LCL (LossLess Codec Library) ZLIB
V....D qtrle                QuickTime Animation (RLE) video
V....D snow                 Snow
V....D camtasia             TechSmith Screen Capture Codec (codec tscc)
V....D ultimotion           IBM UltiMotion (codec ulti)
V....D qdraw                Apple QuickDraw
V....D xl                   Miro VideoXL (codec vixl)
V....D qpeg                 Q-team QPEG
V....D png                  PNG (Portable Network Graphics) image
V....D ppm                  PPM (Portable PixelMap) image
V....D pbm                  PBM (Portable BitMap) image
V....D pgm                  PGM (Portable GrayMap) image
V....D pgmyuv               PGMYUV (Portable GrayMap YUV) image
V....D pam                  PAM (Portable AnyMap) image
VF..BD ffvhuff              Huffyuv FFmpeg variant
VF...D rv30                 RealVideo 3.0
VF...D rv40                 RealVideo 4.0
V....D vc1                  SMPTE VC-1
V....D wmv3                 Windows Media Video 9
V....D loco                 LOCO
V....D wnv1                 Winnov WNV1
V....D aasc                 Autodesk RLE
V....D indeo2               Intel Indeo 2
VF...D fraps                Fraps
V....D truemotion2          Duck TrueMotion 2.0
V....D bmp                  BMP (Windows and OS/2 bitmap)
V....D camstudio            CamStudio (codec cscd)
V....D mmvideo              American Laser Games MM Video
V....D zmbv                 Zip Motion Blocks Video
V....D avs                  AVS (Audio Video Standard) video
V....D smackvid             Smacker video (codec smackvideo)
V....D nuv                  NuppelVideo/RTJPEG
V....D kmvc                 Karl Morton's video codec
V....D flashsv              Flash Screen Video v1
V....D cavs                 Chinese AVS (Audio Video Standard) (AVS1-P2, JiZhun profile)
V..X.. j2k                  JPEG 2000 (codec jpeg2000)
VF...D libopenjpeg          OpenJPEG JPEG 2000 (codec jpeg2000)
V....D vmnc                 VMware Screen Codec / VMware Video
V....D vp5                  On2 VP5
V....D vp6                  On2 VP6
V....D vp6f                 On2 VP6 (Flash version)
V....D targa                Truevision Targa image
V....D dsicinvideo          Delphine Software International CIN video
V....D tiertexseqvideo      Tiertex Limited SEQ video
V....D tiff                 TIFF image
V....D gif                  GIF (Graphics Interchange Format)
V....D dxa                  Feeble Files/ScummVM DXA
VF...D dnxhd                VC3/DNxHD
V....D thp                  Nintendo Gamecube THP video
V....D sgi                  SGI image
V....D c93                  Interplay C93
V....D bethsoftvid          Bethesda VID video
V....D ptx                  V.Flash PTX image
V....D txd                  Renderware TXD (TeXture Dictionary) image
V....D vp6a                 On2 VP6 (Flash version, with alpha channel)
V..... amv                  AMV Video
V..... vb                   Beam Software VB
V....D pcx                  PC Paintbrush PCX image
V....D sunrast              Sun Rasterfile image
V..... indeo4               Intel Indeo Video Interactive 4
V..... indeo5               Intel Indeo Video Interactive 5
VF...D mimic                Mimic
V....D rl2                  RL2 video
V....D escape124            Escape 124
V..... dirac                BBC Dirac VC-2
V..... libschroedinger      libschroedinger Dirac 2.2 (codec dirac)
V....D bfi                  Brute Force & Ignorance
V....D eacmv                Electronic Arts CMV video (codec cmv)
V....D motionpixels         Motion Pixels video
V..... eatgv                Electronic Arts TGV video (codec tgv)
V....D eatgq                Electronic Arts TGQ video (codec tgq)
V....D eatqi                Electronic Arts TQI Video (codec tqi)
V....D aura                 Auravision AURA
V....D aura2                Auravision Aura 2
V....D v210x                Uncompressed 4:2:2 10-bit
V....D tmv                  8088flex TMV
V....D v210                 Uncompressed 4:2:2 10-bit
V....D dpx                  DPX image
V....D eamad                Electronic Arts Madcow Video (codec mad)
V....D frwu                 Forward Uncompressed
V....D flashsv2             Flash Screen Video v2
V....D cdgraphics           CD Graphics video
V....D r210                 Uncompressed RGB 10-bit
V....D anm                  Deluxe Paint Animation
V..... binkvideo            Bink video
V....D iff_ilbm             IFF ILBM
V....D iff_byterun1         IFF ByteRun1
V..... kgv1                 Kega Game Video
V..... yop                  Psygnosis YOP Video
VFS..D vp8                  On2 VP8
V..... libvpx               libvpx VP8 (codec vp8)
V....D pictor               Pictor/PC Paint
V....D ansi                 ASCII/ANSI art
V....D r10k                 AJA Kona 10-bit RGB Codec
V....D mxpeg                Mobotix MxPEG video
VF...D lagarith             Lagarith lossless
V.S..D prores               ProRes
V.S..D prores_lgpl          Apple ProRes (iCodec Pro) (codec prores)
V....D jv                   Bitmap Brothers JV video
V....D dfa                  Chronomaster DFA
V....D wmv3image            Windows Media Video 9 Image
V....D vc1image             Windows Media Video 9 Image v2
VF...D utvideo              Ut Video
V..... libutvideo           Ut Video (codec utvideo)
V....D bmv_video            Discworld II BMV video
V....D vble                 VBLE Lossless Codec
V....D dxtory               Dxtory
V....D v410                 Uncompressed 4:4:4 10-bit
V....D xwd                  XWD (X Window Dump) image
V....D cdxl                 Commodore CDXL video
V....D xbm                  XBM (X BitMap) image
V....D zerocodec            ZeroCodec Lossless Video
V....D mss1                 MS Screen 1
V....D msa1                 MS ATC Screen
V....D tscc2                TechSmith Screen Codec 2
V....D mts2                 MS Expression Encoder Screen
V....D cllc                 Canopus Lossless Codec
V....D mss2                 MS Windows Media Video V9 Screen
V....D y41p                 Uncompressed YUV 4:1:1 12-bit
V....D escape130            Escape 130
VF...D exr                  OpenEXR image
V....D avrp                 Avid 1:1 10-bit RGB Packer
V....D avui                 AVID Meridien
V....D ayuv                 Uncompressed packed MS 4:4:4:4
V....D v308                 Uncompressed packed 4:4:4
V....D v408                 Uncompressed packed QT 4:4:4:4
V....D yuv4                 Uncompressed packed 4:2:0
V....D sanm                 LucasArts SMUSH video
V....D paf_video            Amazing Studio Packed Animation File Video
V....D avrn                 Avid AVI Codec
V....D cpia                 CPiA video format
To connect to a video camera, it suffices to use special filenames that start with “cam:” (called camera connection string).
Opens the specified video file for reading (and decoding)
Reads and decodes one frame of the video sequence
This video library supports all video codecs of FFMPEG.
Creates an empty untyped object, which can be used to store data.
Opens the specified video file for writing (by encoding)
Close