--- In psychtoolbox@yahoogroups.com, Matthew Edmundson <medmunds@...> wrote:
>
> "This sounds more like just startup stutter. If you "clear all" and thereby
> "clear Screen" etc., you force a complete reinitialization of the playback
> engine for each movie. There can be delays every time a function is called
> the first time during execution of a script, especially after a clear.
> Similar for movie playback, e.g., decoder threads need to be started,
> libraries/codecs loaded, memory buffers must be allocated, etc. Just add a
> warmup trial at the beginning of your session, maybe playing some movie
> clip of a few seconds before the real session starts and see if the problem
> goes away."
>
> Thanks for this. I may give this warmup video workaround a shot if we get
> more situations with startup stutter occurring.
>
It's not really a workaround but best practice, not only for movie playback but in general. If Matlab runs through a script the first time, it will be slower, because stuff needs to be read from harddisc, which takes non-deterministic time, m-files need to be possibly partially jit-compiled during their first loop iterations, mex files need to get all their required support libraries loaded and linked, etc.
Warmup trials are not only for your subjects, but also for the equipment.
>
> "This last video queue is only there with async flag 4. Other queues are
> available before that final queue, but how many, where, and how exactly (if
> at all) the preloadSecs flag affects the capacity of that queue is
> dependent on the video and audio codecs, demultiplexers etc. - It depends
> both on the exact format and encoding of your movies and even a bit on the
> operating system and potentially hardware in use."
>
> Can you give some examples of codecs and/or container formats that you know
> from your work in the past have filled this queue, even if it was just on
> Linux or Mac OS X? Maybe you've used H.264 contained in MP4? What software
> would you have used to encode these videos?
>
I seldomly encode movies myself. I use test movies provided by users in the past, or generated by our own movie writing code, as shown in ImagingStereoDemo if the 'writemovie' flag is set, or movies recorded by our VideoRecordingDemo. I also test with movie trailers, or video created with Quicktime-Pro under osx.
When encoding with ptb's built in movie writer, default codec and muxer settings are chosen to facilitate use for psychophysics. The problem here is that the actual set of available codecs and muxers depends on your GStreamer installation and varies a lot across operating systems and GStreamer distributions, so the best settings may not work. On Linux you usually get a very complete set of codecs, OSX with the source build from Homebrew is ok iirc, as is the old OSSBuilds GStreamer for 32-Bit Matlab on Windows. The GStreamer SDK currently has the smallest set of supported encoders. Again, i mostly tested this on Linux, benchmarking such stuff is annoying enough on one OS, i didn't want to repeat this on the other OS'es and don't care too much about the other os'es for high performance applications anymore.
But mp4 container with H264 video and AAC audio is a common combination as a starting point. However, there are enough tunable parameters for that combo already to get you in trouble, or to spend time tinkering.
The last project i was involved in involved playback of stereoscopic HD 1920x1080 progressive scan video at sustained 60 fps, synchronized to the movements of a robotic motion simulator. The video material was in a ISO MP4 container, with MPEG-4 AAC audio and H264/AVC video. I wasn't involved in recording and encoding, but they used Handbrake ( http://http://handbrake.fr/ ) for encoding.
I think Tobias Wolf posted a few bis about encoding in the past. He uses ffmpeg directly iirc.
> If you aren't sure, I can do some tests myself, it just takes time to
> encode into all these different formats to test, so if I could start with a
> known quantity it would help.
>
LoadMovieIntoTexturesDemo has benchmark modes that allow to check how well different parts of the playback behave with different movies and settings.
> Like I said, I see no evidence of this queue filling up any significant
> amount of memory with Xvid AVIs on Windows. It could be that I'm just
> misusing the functions here, but I think it's more likely that this is
> something that's video format or OS specific.
>
I don't think i ever tested queueing behaviour a lot, beyond the very specific applications we needed. E.g., the application i mentioned used a preloadSecs of multiple seconds to buffer that amount, but it normally doesn't use the audio in the movie, so audio-video sync isn't a problem. Instead it loads the audio as a .wav file and plays it via PsychPortAudio, just making sure that audio and video playback start perfectly in sync. This causes audio and video to drift apart over time, but for trials of no more than maybe a minute or two, the error on our setup was still below ~ 40 msecs, good enough for the purpose.
As i said, the last queue is controlled via the async=4 flag. This is demonstrated e.g., in LoadMovieIntoTexturesDemo, but this is mostly unsuitable for audio+video playback. There are other queues in the playback pipeline, but their size depends on many factors. We essentially pass 'preloadSecs' as a high-level hint and then the specific pipeline somehow derives the sizes of the different queues from that and from other parameters. 'preloadSecs' for those latter queues is a misleading name, as the engine will not neccessarily prebuffer 'preloadSecs' seconds of data before playback starts. It is more like an upper limit of what can be buffered in certain situations, and it is also possible that certain codecs or older versions of GStreamer ignore this hint. E.g., the GStreamer from OSSBuilds that you have installed if you use 32-Bit Matlab on Windows is rather old and outdated, last updated over 2 years ago. Could be that it ignores the hint completely. Our source code says you need at least version v0.10.31 of the 'playbin2' plugin.
But i don't think that tweaking the buffering behaviour will gain you much for your approach. Where it might help is if you really play a movie in a loop for multiple interations, because then the 2nd iteration could use all the buffered data from the first iteration if it fits fully into memory. Or for random seeks in the material, once the whole video has been buffered. What you described in your other post doesn't sound as if the whole movie would get buffered, given your unconventional use of the loop flag. You'd only have the few seconds from your seek position to the end of the movie, not the following seconds from the start of the movie after it looped back to the beginning, so probably not much gain there.
-mario
> Best,
> Matthew
>
>
>
> On Thu, May 2, 2013 at 11:06 PM, Mario <mario.kleiner@...>wrote:
>
> > **
> >
> >
> >
> >
> > --- In psychtoolbox@yahoogroups.com, Matthew Edmundson <medmunds@>
> > wrote:
> > >
> > > Hi Mario,
> > >
> > > Not to hijack this thread, but I've also been trying to use background
> > > preloading of videos myself to see if it might help some performance
> > > problems we're having, but I can find no evidence that any preloading is
> > > actually occurring. I'm following the OpenMovie Docs page instructions,
> > > and according to it I'm supposed to open the movies with the async flag
> > set
> > > to 1 (or 2, or 4) and then wait a bit (say pause(10)), and then open them
> > > again with async flag set to 0 to get the pointers. I'm setting
> > preloadSecs
> > > to -1, but have also tried other values greater than 1.
> > >
> > > The problem is, I notice no increase in RAM usage on the system at all
> > > until the *second* OpenMovie is called with async flag set to 0, and even
> > > then it is only a very minor bump, say 6 MB per video. The videos I'm
> > > trying to preload are Xvids which are all larger than 20 MB, so this
> > > doesn't exactly make sense in terms of fully preloading the videos.
> > >
> >
> > What preloadSecs does on GStreamer is it sets the size of internal memory
> > buffers
> > meant for caching of data and also sets the maximum capacity of an
> > internal queue for decoded video frames if that async flag is set to
> > include the value 4 - an "or" of values.
> >
> > If your movies don't have sound then that async 4 setting will cause video
> > frames to get decoded and queued in an internal queue of up to
> > 'preloadSecs' seconds after 'PlayMovie' got called. Then each
> > 'GetMovieImage' call pulls the oldest decoded frame from that queue, so if
> > you gave it enough head start, it could pull frames from the queue and
> > survive stalls or slowdowns of the decoding process of up to preloadSecs
> > seconds. If you have sound and do the same it will work the same, but as
> > sound output would start almost immediately after 'PlayMovie', but you
> > would pause a bit before 'GetMovieImage' + 'DrawTexture' + 'Flip' you'd
> > have sound playback getting ahead of video playback.
> >
> > This last video queue is only there with async flag 4. Other queues are
> > available before that final queue, but how many, where, and how exactly (if
> > at all) the preloadSecs flag affects the capacity of that queue is
> > dependent on the video and audio codecs, demultiplexers etc. - It depends
> > both on the exact format and encoding of your movies and even a bit on the
> > operating system and potentially hardware in use.
> >
> >
> > > We're using Windows, btw.
> >
> > My condolence ;-).
> >
> > I mostly test and tune this stuff on Linux, and then some on OSX. Windows
> > gets only minimal effort, because trying to make pigs fly is so tiresome.
> >
> > The PlayMoviesDemo also has some optional flags 'pixelFormat' and
> > 'maxThreads' which allow optimizations if the video material is very taxing
> > for the system, e.g., very high resolution, high framerate, stereo etc. The
> > bottleneck always depends on the task, and clever choice of movie encoding
> > can boost performance a lot, but "clever" is not very well defined, a lot
> > of trial and error if you're not an expert in video formats.
> >
> > best,
> > -mario
> >
> > > Best,
> > > Matthew
> > >
> > >
> > > On Wed, May 1, 2013 at 12:16 PM, dkennedyiu <dkennedy80@> wrote:
> > >
> > > > **
> >
> > > >
> > > >
> > > > Hi Mario --
> > > >
> > > > Thanks very much for the reply.
> > > >
> > > > The videos I'm playing have sound.
> > > >
> > > > And, In terms of free memory, I have 32 GB of RAM, so that shouldn't be
> > > > the problem.
> > > >
> > > > I'm only playing 1 long-ish video at a time (~4 to 12 minutes, w/o
> > > > interruption), so there's no randomization. I'll investigate more to
> > see if
> > > > it's a problem specific to these specific video files. Also, I'll also
> > > > check whether I can reproduce the problem with your demos, and report
> > back.
> > > >
> > > > Here's one more interesting bit of info -- If I run my script in a
> > loop 50
> > > > times, I've noticed that the timing error only happens on the first
> > > > run-through of the video. Not in the next 49 runs. If I clear all,
> > clc, and
> > > > then rerun the loop, I might (but not always) again get the half-second
> > > > timing error on the first run and no where else. The weird thing is
> > that I
> > > > run the "clear all" command within the script at the end of each run.
> > So,
> > > > no idea what's happening here.
> > > >
> > > > And, the "Deficient Apple OS/X 10.7" warning is helpful, but I was just
> > > > confused since it didn't go away after installing the recommended
> > driver.
> > > > Thanks for clarifying!
> > > >
> > > > Dan
> > > >
> > > >
> > > >
> > >
> >
> >
> >
>