<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Improve Flash MPEG-4 AVC Seeking With Binary Search</title>
	<atom:link href="http://www.brooksandrus.com/blog/2008/11/12/improve-flash-mpeg-4-avc-seeking-with-binary-search/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brooksandrus.com/blog/2008/11/12/improve-flash-mpeg-4-avc-seeking-with-binary-search/</link>
	<description>This is the blog of Brooks Andrus. Here, at irregular intervals, you may find digital noise centered around the activities of an early 21st century technologist. I work for TechSmith Corporation, but this web space and the views found on it are entirely my own.</description>
	<lastBuildDate>Wed, 08 Feb 2012 11:34:01 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Flex and Flash Developer &#8211; Jesse Warden dot Kizz-ohm &#187; Blog Archive &#187; Wish List for Flash Player 11</title>
		<link>http://www.brooksandrus.com/blog/2008/11/12/improve-flash-mpeg-4-avc-seeking-with-binary-search/comment-page-1/#comment-49951</link>
		<dc:creator>Flex and Flash Developer &#8211; Jesse Warden dot Kizz-ohm &#187; Blog Archive &#187; Wish List for Flash Player 11</dc:creator>
		<pubDate>Sun, 09 Aug 2009 04:29:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.brooksandrus.com/blog/?p=435#comment-49951</guid>
		<description>[...] I have many video projects where NetStream breaks, and I need to fix it. Â Additionally, I have many cool video scrubbing, pausing, and DVD like controls I&#8217;d like to [...]</description>
		<content:encoded><![CDATA[<p>[...] I have many video projects where NetStream breaks, and I need to fix it. Â Additionally, I have many cool video scrubbing, pausing, and DVD like controls I&#8217;d like to [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Sirgedas</title>
		<link>http://www.brooksandrus.com/blog/2008/11/12/improve-flash-mpeg-4-avc-seeking-with-binary-search/comment-page-1/#comment-49285</link>
		<dc:creator>Tom Sirgedas</dc:creator>
		<pubDate>Mon, 08 Dec 2008 16:35:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.brooksandrus.com/blog/?p=435#comment-49285</guid>
		<description>Your -1 return value for an exact match bugs me.  Just return the greatest index whose value doesn&#039;t exceed &quot;value&quot;.  If you really want to check for an exact match, the caller can do that easily.

Here&#039;s my implementation:

&lt;pre lang=&quot;actionscript&quot;&gt;
// x         -- the value to find
// arr       -- sorted array of Numbers
// [lo, hi)  -- the range of indices to search, lo is inclusive, hi is exclusive
//              so [5, 10) means consider 5,6,7,8,9
//
// The return value is the largest arr index whose value does not exceed x.
// e.g.  binarySearch_( 33.3, [0,10,20,30,40,50], 0, 6 ) returns 3
// e.g.  binarySearch_( 30.0, [0,10,20,30,40,50], 0, 6 ) returns 3
// e.g.  binarySearch_( 90  , [0,10,20,30,40,50], 0, 6 ) returns 5
// e.g.  binarySearch_( 0   , [0,10,20,30,40,50], 0, 6 ) returns 0
// e.g.  binarySearch_( -90 , [0,10,20,30,40,50], 0, 6 ) returns -1
function binarySearch_( x:Number, arr:Array, lo:Number, hi:Number ) : Number
{
	if ( hi - lo &lt; = 1 )
	      return x &lt; arr[lo] &#124;&#124; hi - lo &lt; 1 ? -1 : lo;
	var mid:Number = Math.floor((lo+hi)/2);
	return x&lt; arr[mid] ? binarySearch_( x, arr, lo, mid )
	                   : binarySearch_( x, arr, mid, hi );
}

// wrapper
function binarySearch( x:Number, arr:Array ) : Number
{
	return binarySearch_( x, arr, 0, arr.length );
}

trace( binarySearch( 33.3 , [0,10,20,30,40,50] ) ); // 3
trace( binarySearch( 30.0 , [0,10,20,30,40,50] ) ); // 3
trace( binarySearch( 90   , [0,10,20,30,40,50] ) ); // 5
trace( binarySearch( 0    , [0,10,20,30,40,50] ) ); // 0
trace( binarySearch( -90  , [0,10,20,30,40,50] ) ); // -1
stop();
&lt;/pre&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Your -1 return value for an exact match bugs me.  Just return the greatest index whose value doesn&#8217;t exceed &#8220;value&#8221;.  If you really want to check for an exact match, the caller can do that easily.</p>
<p>Here&#8217;s my implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// x         -- the value to find</span>
<span style="color: #808080; font-style: italic;">// arr       -- sorted array of Numbers</span>
<span style="color: #808080; font-style: italic;">// [lo, hi)  -- the range of indices to search, lo is inclusive, hi is exclusive</span>
<span style="color: #808080; font-style: italic;">//              so [5, 10) means consider 5,6,7,8,9</span>
<span style="color: #808080; font-style: italic;">//</span>
<span style="color: #808080; font-style: italic;">// The return value is the largest arr index whose value does not exceed x.</span>
<span style="color: #808080; font-style: italic;">// e.g.  binarySearch_( 33.3, [0,10,20,30,40,50], 0, 6 ) returns 3</span>
<span style="color: #808080; font-style: italic;">// e.g.  binarySearch_( 30.0, [0,10,20,30,40,50], 0, 6 ) returns 3</span>
<span style="color: #808080; font-style: italic;">// e.g.  binarySearch_( 90  , [0,10,20,30,40,50], 0, 6 ) returns 5</span>
<span style="color: #808080; font-style: italic;">// e.g.  binarySearch_( 0   , [0,10,20,30,40,50], 0, 6 ) returns 0</span>
<span style="color: #808080; font-style: italic;">// e.g.  binarySearch_( -90 , [0,10,20,30,40,50], 0, 6 ) returns -1</span>
<span style="color: #000000; font-weight: bold;">function</span> binarySearch_<span style="color: #66cc66;">&#40;</span> x:<span style="color: #0066CC;">Number</span>, arr:<span style="color: #0066CC;">Array</span>, lo:<span style="color: #0066CC;">Number</span>, hi:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">Number</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> hi - lo <span style="color: #66cc66;">&lt;</span> = <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>
	      <span style="color: #b1b100;">return</span> x <span style="color: #66cc66;">&lt;</span> arr<span style="color: #66cc66;">&#91;</span>lo<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">||</span> hi - lo <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">1</span> ? -<span style="color: #cc66cc;">1</span> : lo;
	<span style="color: #000000; font-weight: bold;">var</span> mid:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">floor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>lo+hi<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">return</span> x<span style="color: #66cc66;">&lt;</span> arr<span style="color: #66cc66;">&#91;</span>mid<span style="color: #66cc66;">&#93;</span> ? binarySearch_<span style="color: #66cc66;">&#40;</span> x, arr, lo, mid <span style="color: #66cc66;">&#41;</span>
	                   : binarySearch_<span style="color: #66cc66;">&#40;</span> x, arr, mid, hi <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// wrapper</span>
<span style="color: #000000; font-weight: bold;">function</span> binarySearch<span style="color: #66cc66;">&#40;</span> x:<span style="color: #0066CC;">Number</span>, arr:<span style="color: #0066CC;">Array</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">Number</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span> binarySearch_<span style="color: #66cc66;">&#40;</span> x, arr, <span style="color: #cc66cc;">0</span>, arr.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> binarySearch<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">33.3</span> , <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">30</span>,<span style="color: #cc66cc;">40</span>,<span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 3</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> binarySearch<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">30.0</span> , <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">30</span>,<span style="color: #cc66cc;">40</span>,<span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 3</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> binarySearch<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">90</span>   , <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">30</span>,<span style="color: #cc66cc;">40</span>,<span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 5</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> binarySearch<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">0</span>    , <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">30</span>,<span style="color: #cc66cc;">40</span>,<span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 0</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> binarySearch<span style="color: #66cc66;">&#40;</span> -<span style="color: #cc66cc;">90</span>  , <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">30</span>,<span style="color: #cc66cc;">40</span>,<span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// -1</span>
<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Zohar Babin</title>
		<link>http://www.brooksandrus.com/blog/2008/11/12/improve-flash-mpeg-4-avc-seeking-with-binary-search/comment-page-1/#comment-49106</link>
		<dc:creator>Zohar Babin</dc:creator>
		<pubDate>Sun, 23 Nov 2008 15:43:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.brooksandrus.com/blog/?p=435#comment-49106</guid>
		<description>:) maybe a personal preference, but it looks much cleaner.
running recursive calls is more heavy due to the need of keeping the stack and calling the function. I don&#039;t if it&#039;s still apply, in earlier versions of flash the recursion was also limited (though for binary search you shouldn&#039;t reach this limit anyway).</description>
		<content:encoded><![CDATA[<p>:) maybe a personal preference, but it looks much cleaner.<br />
running recursive calls is more heavy due to the need of keeping the stack and calling the function. I don&#8217;t if it&#8217;s still apply, in earlier versions of flash the recursion was also limited (though for binary search you shouldn&#8217;t reach this limit anyway).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brooks</title>
		<link>http://www.brooksandrus.com/blog/2008/11/12/improve-flash-mpeg-4-avc-seeking-with-binary-search/comment-page-1/#comment-49096</link>
		<dc:creator>Brooks</dc:creator>
		<pubDate>Sun, 23 Nov 2008 00:50:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.brooksandrus.com/blog/?p=435#comment-49096</guid>
		<description>@ Zohar - I felt like the recursive method was a bit more readable and there wasn&#039;t any noticeable performance hit. That said its a piece of cake to implement binary search via a loop rather than recursively. Here&#039;s an example:

&lt;pre lang=&quot;actionscript&quot;&gt;
/**
 * Loops through an array of seekpoints to find a requested time. 
 * If the value is found within the array of seekpoints the index at which 
 * the value is found is returned. If an exact match is not found, -1 is returned 
 * and the the upper and lower indices between which the value falls are set 
 * (so you can determine the closest indice, or seek to to the nearest indexed 
 * time behind or forward in the array of seekpoints.
 * 
 * Seekpoints array must be sorted.
 * 
 * @param value  the requested seek time to find
 * @param range the array of seekpoints collected from an MPEG-4 AVC NetStream metadata event
 * @param the low index within the range we&#039;d like to scan
 * @param the high index within the range we&#039;d like to scan
 * 
 */
public function search( value:Number, range:Array, low:int, high:int ):int
{
    var val:int = -1;
    var time:Number;
    while ( low &lt; = time  )
    {
       var median:int = ( low + high ) / 2;
       time = range[median].time;
       if ( value &gt; time  )
       {
          low = median + 1;
       }
       else if ( value &lt; time  )
       {
          high = median - 1;
       }
       else // found a match
       {
          low = median;
          high = median;
          val = median;
          break;
       }
   }
   _upperBounds = low;
   _lowerBounds = high;
   return val; // failed to find value. Look at the upper / lower bounds
}
&lt;/pre&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>@ Zohar &#8211; I felt like the recursive method was a bit more readable and there wasn&#8217;t any noticeable performance hit. That said its a piece of cake to implement binary search via a loop rather than recursively. Here&#8217;s an example:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
 * Loops through an array of seekpoints to find a requested time. 
 * If the value is found within the array of seekpoints the index at which 
 * the value is found is returned. If an exact match is not found, -1 is returned 
 * and the the upper and lower indices between which the value falls are set 
 * (so you can determine the closest indice, or seek to to the nearest indexed 
 * time behind or forward in the array of seekpoints.
 * 
 * Seekpoints array must be sorted.
 * 
 * @param value  the requested seek time to find
 * @param range the array of seekpoints collected from an MPEG-4 AVC NetStream metadata event
 * @param the low index within the range we'd like to scan
 * @param the high index within the range we'd like to scan
 * 
 */</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> search<span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">Number</span>, range:<span style="color: #0066CC;">Array</span>, low:<span style="color: #0066CC;">int</span>, high:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">int</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> val:<span style="color: #0066CC;">int</span> = -<span style="color: #cc66cc;">1</span>;
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">time</span>:<span style="color: #0066CC;">Number</span>;
    <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span> low <span style="color: #66cc66;">&lt;</span> = <span style="color: #0066CC;">time</span>  <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
       <span style="color: #000000; font-weight: bold;">var</span> median:<span style="color: #0066CC;">int</span> = <span style="color: #66cc66;">&#40;</span> low + high <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span>;
       <span style="color: #0066CC;">time</span> = range<span style="color: #66cc66;">&#91;</span>median<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">time</span>;
       <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> value <span style="color: #66cc66;">&gt;</span> <span style="color: #0066CC;">time</span>  <span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#123;</span>
          low = median + <span style="color: #cc66cc;">1</span>;
       <span style="color: #66cc66;">&#125;</span>
       <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> value <span style="color: #66cc66;">&lt;</span> <span style="color: #0066CC;">time</span>  <span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#123;</span>
          high = median - <span style="color: #cc66cc;">1</span>;
       <span style="color: #66cc66;">&#125;</span>
       <span style="color: #b1b100;">else</span> <span style="color: #808080; font-style: italic;">// found a match</span>
       <span style="color: #66cc66;">&#123;</span>
          low = median;
          high = median;
          val = median;
          <span style="color: #b1b100;">break</span>;
       <span style="color: #66cc66;">&#125;</span>
   <span style="color: #66cc66;">&#125;</span>
   _upperBounds = low;
   _lowerBounds = high;
   <span style="color: #b1b100;">return</span> val; <span style="color: #808080; font-style: italic;">// failed to find value. Look at the upper / lower bounds</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Zohar Babin</title>
		<link>http://www.brooksandrus.com/blog/2008/11/12/improve-flash-mpeg-4-avc-seeking-with-binary-search/comment-page-1/#comment-49070</link>
		<dc:creator>Zohar Babin</dc:creator>
		<pubDate>Fri, 21 Nov 2008 12:34:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.brooksandrus.com/blog/?p=435#comment-49070</guid>
		<description>nice work,

though I don&#039;t get why use recursive (which is known to be expensive in flash) instead of just looping through the Array.

by the way, in Kaltura we created a similar implementation for enhancing the seek experience for progressive download streams in the flv. Seeking was wired if you seek between keyframes where you&#039;ll wait for the &quot;not always working&quot; invalid seek time. When you seek exactly to a kayframe the response is better and the event shows the right time.</description>
		<content:encoded><![CDATA[<p>nice work,</p>
<p>though I don&#8217;t get why use recursive (which is known to be expensive in flash) instead of just looping through the Array.</p>
<p>by the way, in Kaltura we created a similar implementation for enhancing the seek experience for progressive download streams in the flv. Seeking was wired if you seek between keyframes where you&#8217;ll wait for the &#8220;not always working&#8221; invalid seek time. When you seek exactly to a kayframe the response is better and the event shows the right time.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

