[PATCH] massive vorbis speedup

2004-01-28
2013-04-11
  • Rich Tollerton
    Rich Tollerton
    2004-01-28

    Some profiling of oggvorbis.qtx indicated that the lion's share of CPU time was spent inside, of all places, QuickTime itself - but being called from BeginMediaEdits. The BeginMediaEdits/EndMediaEdits calls were wrapped around AddMediaSampleReference calls, but the Apple docs clearly state (http://developer.apple.com/documentation/QuickTime/APIREF/SOURCESI/addmediasamplereference.htm) that BeginMediaEdits doesn't need to be called in this case. This patch rips out said use of BeginMediaEdits and this speeds up Vorbis file loading by ~20x in iTunes. Enjoy :)

    Index: OggVDecompressor.c

    RCS file: /cvsroot/qtcomponents/qtoggvorbis/src/OggVDecompressor.c,v
    retrieving revision 1.11
    diff -u -r1.11 OggVDecompressor.c
    --- OggVDecompressor.c    16 Dec 2002 03:12:39 -0000    1.11
    +++ OggVDecompressor.c    28 Jan 2004 05:49:19 -0000
    @@ -1363,35 +1363,27 @@
                 )
                 {                               
                     vsi->lastSeenGranulePos = 0;    // flag as all caught up
    +
    +                result = AddMediaSampleReference(vsi->theMedia, vsi->prevPageOffset,
    +                            len, duration, vsi->sampleDesc, 1, 0, NULL); //@@@@ 64-bit enable

    -                result = BeginMediaEdits(vsi->theMedia);
                     if (result == noErr)
                     {
    -                    result = AddMediaSampleReference(vsi->theMedia, vsi->prevPageOffset,
    -                                len, duration, vsi->sampleDesc, 1, 0, NULL); //@@@@ 64-bit enable
    -               
    -                    result2 = EndMediaEdits(vsi->theMedia);
    -                    if (result2 != noErr)
    -                        DebugStr("\pEndMediaEdits returned an error");
    -
    -                    if (result == noErr)
    -                    {
    //printf("added media at %6d, len %5d duration %5d\n", (int)vsi->prevPageOffset, len, duration);
    -                        if (vsi->globals->usingIdle)
    -                        {
    -                            result = InsertMediaIntoTrack(vsi->theTrack, vsi->startTime, vsi->lastGranulePos,
    -                                        duration, fixed1);
    -                            /* now that the track is started at the right time, switch to -1 which
    -                               means add on the end.  Hopefully this will help QuickTime to coalese
    -                               it's mapping of track -> media references so they don't take up so
    -                               much space */
    -                            vsi->startTime = -1;
    -                            *timeLoaded = GetTrackOffset(vsi->theTrack) + GetTrackDuration(vsi->theTrack);
    -                        } // need to insert the new media into the track
    -           
    -                        vsi->prevPageOffset = endOffset;
    -                        vsi->lastGranulePos = pos;
    -                    }
    +                    if (vsi->globals->usingIdle)
    +                    {
    +                        result = InsertMediaIntoTrack(vsi->theTrack, vsi->startTime, vsi->lastGranulePos,
    +                                    duration, fixed1);
    +                        /* now that the track is started at the right time, switch to -1 which
    +                           means add on the end.  Hopefully this will help QuickTime to coalese
    +                           it's mapping of track -> media references so they don't take up so
    +                           much space */
    +                        vsi->startTime = -1;
    +                        *timeLoaded = GetTrackOffset(vsi->theTrack) + GetTrackDuration(vsi->theTrack);
    +                    } // need to insert the new media into the track
    +       
    +                    vsi->prevPageOffset = endOffset;
    +                    vsi->lastGranulePos = pos;
                     }

                     if (ogg_page_eos(og))
    @@ -1424,14 +1416,8 @@
             int   len = S64Subtract(vsi->lastSeenEndOffset, vsi->prevPageOffset);
             duration  = vsi->lastSeenGranulePos - vsi->lastGranulePos;

    -        result = BeginMediaEdits(vsi->theMedia);
    -        if (result == noErr)
    -        {
    -            result = AddMediaSampleReference(vsi->theMedia, vsi->prevPageOffset,
    -                        len, duration, vsi->sampleDesc, 1, 0, NULL); //@@@@ 64-bit enable
    -       
    -            EndMediaEdits(vsi->theMedia);
    -        }
    +        result = AddMediaSampleReference(vsi->theMedia, vsi->prevPageOffset,
    +                    len, duration, vsi->sampleDesc, 1, 0, NULL); //@@@@ 64-bit enable
         }
        
         if (!vsi->globals->usingIdle)

     
    • metrom
      metrom
      2004-01-30

      wow!
      if this really does what you say, it's a huge step for ogg vorbis into the macworld.

      but is there anyone who could complie a new binary with this hack and distribute it somehow?

      metrom

      www.vorbis.nu

       
    • metrom
      metrom
      2004-01-31

      Rich Tollerton sent me a binary with the new changes.

      Get the windows binary here: 
      http://www.vorbis.nu/pub/qtoggvorbis.zip

      Would be nice if someone could complie a osx binary :)

      metrom

       
    • Brian Schkerke
      Brian Schkerke
      2004-02-01

      Yea.  If you're tired of the load times on Ogg files that patch is the shit.

      Reduced my files by about 99%. :-)

      Thanks man.

       
    • I applied the patch and compiled the component for OS X. I also fixed the case insensitivity of the tags. The binary can be downloaded here:
      http://www.esat.kuleuven.ac.be/~athomas/OggVorbis_qtx.sit

      However, the speed increase is not dramatic. It now takes about 3 seconds to load a file which previously took 7 seconds. Files on slow media still take as much time as required to read the entire file. I tested this with a USB key and it takes 46 seconds to load a large ogg file! I really don't understand why the file has to be read entirely before it can be played. It's not because the file is loaded into memory, because it's still being read from disk while it's playing...

       
    • I put a Mac OS X binary up here also:

      http://doublecommand.sourceforge.net/qtoggvorbis.html

       

    • Anonymous
      2004-06-30

      If the ogg file has to be entirely in memory before it begins playing in iTunes, it would explain why I can't open an ogg stream with this.

       
    • Hmm, this patch doesn't help much in my iTunes.. I play my ogg's from an NFS share using 11mb/s Airport card, and the loading time is about 10-15 seconds. I haven't looked at the source, but could this problem be caused by the fact that the whole OGG file needs to be transfered before playing (or possibly iTunes i seeking for the OGG-tag at the end of the file?)

       
    • Schlingel
      Schlingel
      2004-09-26

      Hmm for Some reason, that Plugin doesnt solve my oggTag problem. Itunes still doesnt display the Track numbers of my oggs (on windows/linux/Rio Karma) everything works perfectly. Does anybody have a clue why this might happen?

       
    • Schlingel
      Schlingel
      2004-09-26

      Sorry I just noticed, that I missed the Track Numer Discussion a few threads below.

       
    • jon merriman
      jon merriman
      2005-03-10

      >> I really don't understand why the file has to be read entirely before it can be played. It's not because the file is loaded into memory, because it's still being read from disk while it's playing... <<

      The problem is that Quicktime is having to "import" the file each time it is loaded. A temporary solution is to open the file up in Quicktime, and click "Save as..." and save a small movie file, then import that into iTunes. It will play instantly.

       
    • Max Horn
      Max Horn
      2005-08-20

      How about putting your nice patch into a "Patch tracker item" of the project? That'll make it easier for other people to find it.

      Also, maybe some of the people who generously provide (sometimes improved) binaries of this project on their websites: Hey, maybe some of you are interested in trying to join this very project, and get your patches incorporate. A new official release of qtcomponent would be nicer than having a dozen "forks" scattered over the net.

       
    • Joshua Root
      Joshua Root
      2005-09-02

      I'm pretty sure this patch was applied shortly after being posted. As for joining this project, the current admins don't seem to be reachable. At least, I have tried to contact them about this project a couple of times and have received no reply.