<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hardwarebug &#187; Multimedia</title>
	<atom:link href="http://hardwarebug.org/category/multimedia/feed/" rel="self" type="application/rss+xml" />
	<link>http://hardwarebug.org</link>
	<description>Everything is broken</description>
	<lastBuildDate>Tue, 18 Oct 2011 00:41:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Ogg objections</title>
		<link>http://hardwarebug.org/2010/03/03/ogg-objections/</link>
		<comments>http://hardwarebug.org/2010/03/03/ogg-objections/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 14:17:07 +0000</pubDate>
		<dc:creator>Mans</dc:creator>
				<category><![CDATA[Multimedia]]></category>

		<guid isPermaLink="false">http://hardwarebug.org/?p=374</guid>
		<description><![CDATA[The Ogg container format is being promoted by the Xiph Foundation for use with its Vorbis and Theora codecs. Unfortunately, a number of technical shortcomings in the format render it ill-suited to most, if not all, use cases. This article examines the most severe of these flaws. Overview of Ogg The basic unit in an [...]]]></description>
			<content:encoded><![CDATA[<p>The Ogg container format is being promoted by the Xiph Foundation for use with its Vorbis and Theora codecs. Unfortunately, a number of technical shortcomings in the format render it ill-suited to most, if not all, use cases.  This article examines the most severe of these flaws.<br />
<span id="more-374"></span></p>
<h1>Overview of Ogg</h1>
<p>The basic unit in an Ogg stream is the <em>page</em> consisting of a header followed by one or more packets from a single elementary stream. A page can contain up to 255 packets, and a packet can span any number of pages. The following table describes the page header.</p>
<div class="frame-outer small">
<div style="text-align: left;">
<table>
<tr>
<th>Field</th>
<th>Size (bits)</th>
<th>Description</th>
</tr>
<tr>
<td>capture_pattern</td>
<td>32</td>
<td>magic number &#8220;OggS&#8221;</td>
</tr>
<tr>
<td>version</td>
<td>8</td>
<td>always zero</td>
</tr>
<tr>
<td>flags</td>
<td>8</td>
</tr>
<tr>
<td>granule_position</td>
<td>64</td>
<td>abstract timestamp</td>
</tr>
<tr>
<td>bitstream_serial_number</td>
<td>32</td>
<td>elementary stream number</td>
</tr>
<tr>
<td>page_sequence_number</td>
<td>32</td>
<td>incremented by 1 each page</td>
</tr>
<tr>
<td>checksum</td>
<td>32</td>
<td>CRC of entire page</td>
</tr>
<tr>
<td>page_segments</td>
<td>8</td>
<td>length of segment_table</td>
</tr>
<tr>
<td>segment_table</td>
<td>variable</td>
<td>list of packet sizes</td>
</tr>
</table>
</div>
</div>
<p>Elementary stream types are identified by looking at the payload of the first few pages, which contain any setup data required by the decoders. For full details, see the official <a href="http://xiph.org/ogg/">format specification</a>.</p>
<h1>Generality</h1>
<p>Ogg, legend tells, was designed to be a general-purpose container format. To most multimedia developers, a general-purpose format is one in which encoded data of any type can be encapsulated with a minimum of effort.</p>
<p>The Ogg format defined by the specification does not fit this description. For every format one wishes to use with Ogg, a complex <em>mapping</em> must first be defined. This mapping defines how to identify a codec, how to extract setup data, and even how timestamps are to be interpreted. All this is done differently for every codec. To correctly parse an Ogg stream, every such mapping ever defined must be known.</p>
<p>Under this premise, a centralised repository of codec mappings would seem like a sensible idea, but alas, no such thing exists. It is simply impossible to obtain a exhaustive list of defined mappings, which makes the task of creating a complete implementation somewhat daunting.</p>
<p>One brave soul, Tobias Waldvogel, created a mapping, OGM, capable of storing any Microsoft AVI compatible codec data in Ogg files. This format saw some use in the wild, but was <a href="http://www.xiph.org/container/ogm.html">frowned upon</a> by Xiph, and it was eventually displaced by other formats.</p>
<p>True generality is evidently not to be found with the Ogg format.</p>
<p>A good example of a general-purpose format is <a href="http://matroska.org/">Matroska</a>. This container can trivially accommodate any codec, all it requires is a unique string to identify the codec. For codecs requiring setup data, a standard location for this is provided in the container. Furthermore, an official list of codec identifiers is maintained, meaning all information required to fully support Matroska files is available from one place.</p>
<p>Matroska also has probably the greatest advantage of all: it is in active, wide-spread use. Historically, standards derived from existing practice have proven more successful than those created by a design committee.</p>
<h1>Overhead</h1>
<p>When designing a container format, one important consideration is that of overhead, i.e. the extra space required in addition to the elementary stream data being combined. For any given container, the overhead can be divided into a fixed part, independent of the total file size, and a variable part growing with increasing file size.  The fixed overhead is not of much concern, its relative contribution being negligible for typical file sizes.</p>
<p>The variable overhead in the Ogg format comes from the page headers, mostly from the <code>segment_table</code> field.  This field uses a most peculiar encoding, somewhat reminiscent of Roman numerals. In Roman times, numbers were written as a sequence of symbols, each representing a value, the combined value being the sum of the constituent values.</p>
<p>The <code>segment_table</code> field lists the sizes of all packets in the page. Each value in the list is coded as a number of bytes equal to 255 followed by a final byte with a smaller value. The packet size is simply the sum of all these bytes. Any strictly additive encoding, such as this, has the distinct drawback of coded length being linearly proportional to the encoded value.  A value of 5000, a reasonable packet size for video of moderate bitrate, requires no less than 20 bytes to encode.</p>
<p>On top of this we have the 27-byte page header which, although paling in comparison to the packet size encoding, is still much larger than necessary. Starting at the top of the list:</p>
<ul>
<li>The <code>version</code> field could be disposed of, a single-bit marker being adequate to separate this first version from hypothetical future versions. One of the unused positions in the <code>flags</code> field could be used for this purpose</li>
<li>A 64-bit <code>granule_position</code> is completely overkill. 32 bits would be more than enough for the vast majority of use cases. In extreme cases, a one-bit flag could be used to signal an extended timestamp field.</li>
<li>32-bit elementary stream number? Are they anticipating files with four billion elementary streams? An eight-bit field, if not smaller, would seem more appropriate here.</li>
<li>The 32-bit <code>page_sequence_number</code> is inexplicable. The intent is to allow detection of page loss due to transmission errors. ISO MPEG-TS uses a 4-bit counter per 188-byte packet for this purpose, and that format is used where packet loss actually happens, unlike any use of Ogg to date.</li>
<li>A mandatory 32-bit checksum is nothing but a waste of space when using a reliable storage/transmission medium. Again, a flag could be used to signal the presence of an optional checksum field.</li>
</ul>
<p>With the changes suggested above, the page header would shrink from 27 bytes to 12 bytes in size.</p>
<p>We thus see that in an Ogg file, the packet size fields alone contribute an overhead of 1/255 or approximately 0.4%. This is a hard lower bound on the overhead, not attainable even in theory. In reality the overhead tends to be closer to 1%.</p>
<p>Contrast this with the ISO MP4 file format, which can easily achieve an overhead of less than 0.05% with a 1 Mbps elementary stream.</p>
<h1>Latency</h1>
<p>In many applications end-to-end latency is an important factor. Examples include video conferencing, telephony, live sports events, interactive gaming, etc. With the codec layer contributing as little as <a href="http://x264dev.multimedia.cx/?p=249">10 milliseconds</a> of latency, the amount imposed by the container becomes an important factor.</p>
<p>Latency in an Ogg-based system is introduced at both the sender and the receiver. Since the page header depends on the entire contents of the page (packet sizes and checksum), a full page of packets must be buffered by the sender before a single bit can be transmitted. This sets a lower bound for the sending latency at the duration of a page.</p>
<p>On the receiving side, playback cannot commence until packets from all elementary streams are available. Hence, with two streams (audio and video) interleaved at the page level, playback is delayed by at least one page duration (two if checksums are verified).</p>
<p>Taking both send and receive latencies into account, the minimum end-to-end latency for Ogg is thus twice the duration of a page, triple if strict checksum verification is required. If page durations are variable, the maximum value must be used in order to avoid buffer underflows.</p>
<p>Minimum latency is clearly achieved by minimising the page duration, which in turn implies sending only one packet per page. This is where the size of the page header becomes important. The header for a single-packet page is <code>27 + packet_size/255</code> bytes in size. For a 1 Mbps video stream at 25 fps this gives an overhead of approximately 1%.  With a typical audio packet size of 400 bytes, the overhead becomes a staggering 7%. The average overhead for a multiplex of these two streams is 1.4%.</p>
<p>As it stands, the Ogg format is clearly not a good choice for a low-latency application. The key to low latency is small packets and fine-grained interleaving of streams, and although Ogg can provide both of these, by sending a single packet per page, the price in overhead is simply too high.</p>
<p>ISO MPEG-PS has an overhead of 9 bytes on most packets (a 5-byte timestamp is added a few times per second), and Microsoft&#8217;s ASF has a 12-byte packet header. My suggestions for compacting the Ogg page header would bring it in line with these formats.</p>
<h1>Random access</h1>
<p>Any general-purpose container format needs to allow random access for direct seeking to any given position in the file. Despite this goal being explicitly mentioned in the Ogg specification, the format only allows the most crude of random access methods.</p>
<p>While many container formats include an index allowing a time to be directly translated into an offset into the file, Ogg has nothing of this kind, the stated rationale for the omission being that this would require a two-pass multiplexing, the second pass creating the index. This is obviously not true; the index could simply be written at the end of the file. Those objecting that this index would be unavailable in a streaming scenario are forgetting that seeking is impossible there regardless.</p>
<p>The method for seeking suggested by the Ogg documentation is to perform a binary search on the file, after each file-level seek operation scanning for a page header, extracting the timestamp, and comparing it to the desired position. When the elementary stream encoding allows only certain packets as random access points (video key frames), a second search will have to be performed to locate the entry point closest to the desired time. In a large file (sizes upwards of 10 GB are common), 50 seeks might be required to find the correct position.</p>
<p>A typical hard drive has an average seek time of roughly 10 ms, giving a total time for the seek operation of around 500 ms, an annoyingly long time. On a slow medium, such as an optical disc or files served over a network, the times are orders of magnitude longer.</p>
<p>A factor further complicating the seeking process is the possibility of header emulation within the elementary stream data. To safeguard against this, one has to read the entire page and verify the checksum. If the storage medium cannot provide data much faster than during normal playback, this provides yet another substantial delay towards finishing the seeking operation. This too applies to both network delivery and optical discs.</p>
<p>Although optical disc usage is perhaps in decline today, one should bear in mind that the Ogg format was designed at a time when CDs and DVDs were rapidly gaining ground, and network-based storage is most certainly on the rise.</p>
<p>The final nail in the coffin of seeking is the codec-dependent timestamp format. At each step in the seeking process, the timestamp parsing specified by the codec mapping corresponding the current page must be invoked. If the mapping is not known, the best one can do is skip pages until one with a known mapping is found. This delays the seeking and complicates the implementation, both bad things.</p>
<h1>Timestamps</h1>
<p>A problem old as multimedia itself is that of synchronising multiple elementary streams (e.g. audio and video) during playback; badly synchronised A/V is highly unpleasant to view. By the time Ogg was invented, solutions to this problem were long since explored and well-understood. The key to proper synchronisation lies in tagging elementary stream packets with timestamps, packets carrying the same timestamp intended for simultaneous presentation. The concept is as simple as it seems, so it is astonishing to see the amount of complexity with which the Ogg designers managed to imbue it. So bizarre is it, that I have devoted an <a href="http://hardwarebug.org/2008/11/17/ogg-timestamps-explored/">entire article</a> to the topic, and will not cover it further here.</p>
<h1>Complexity</h1>
<p>Video and audio decoding are time-consuming tasks, so containers should be designed to minimise extra processing required. With the data volumes involved, even an act as simple as copying a packet of compressed data can have a significant impact. Once again, however, Ogg lets us down. Despite the brevity of the specification, the format is remarkably complicated to parse properly.</p>
<p>The unusual and inefficient encoding of the packet sizes limits the page size to somewhat less than 64 kB. To still allow individual packets larger than this limit, it was decided to allow packets spanning multiple pages, a decision with unfortunate implications. A page-spanning packet as it arrives in the Ogg stream will be discontiguous in memory, a situation most decoders are unable to handle, and reassembly, i.e. copying, is required.</p>
<p>The knowledgeable reader may at this point remark that the MPEG-TS format also splits packets into pieces requiring reassembly before decoding. There is, however, a significant difference there. MPEG-TS was designed for hardware demultiplexing feeding directly into hardware decoders. In such an implementation the fragmentation is not a problem. Rather, the fine-grained interleaving is a feature allowing smaller on-chip buffers.</p>
<p>Buffering is also an area in which Ogg suffers. To keep the overhead down, pages must be made as large as practically possible, and page size translates directly into demultiplexer buffer size. Playback of a file with two elementary streams thus requires 128 kB of buffer space. On a modern PC this is perhaps nothing to be concerned about, but in a small embedded system, e.g. a portable media player, it can be relevant.</p>
<p>In addition to the above, a number of other issues, some of them minor, others more severe, make Ogg processing a painful experience. A selection follows:</p>
<ul>
<li>32-bit random elementary stream identifiers mean a simple table-lookup cannot be used. Instead the list of streams must be searched for a match. While trivial to do in software, it is still annoying, and a hardware demultiplexer would be significantly more complicated than with a smaller identifier.</li>
<li>Semantically ambiguous streams are possible. For example, the continuation flag (bit 1) may conflict with continuation (or lack thereof) implied by the segment table on the preceding page. Such invalid files have been spotted in the wild.</li>
<li>Concatenating independent Ogg streams forms a valid stream. While finding a use case for this strange feature is difficult, an implementation must of course be prepared to encounter such streams. Detecting and dealing with these adds pointless complexity.</li>
<li>Unusual terminology: inventing new terms for well-known concepts is confusing for the developer trying to understand the format in relation to others. A few examples:<br />
<table style="text-align: left; width: 100%;">
<tr>
<th>Ogg name</th>
<th>Usual name</th>
</tr>
<tr>
<td>logical bitstream</td>
<td>elementary stream</td>
</tr>
<tr>
<td>grouping</td>
<td>multiplexing</td>
</tr>
<tr>
<td>lacing value</td>
<td>packet size (approximately)</td>
</tr>
<tr>
<td>segment</td>
<td>imaginary element serving no real purpose</td>
</tr>
<tr>
<td>granule position</td>
<td>timestamp</td>
</tr>
</table>
</li>
</ul>
<h1>Final words</h1>
<p>We have found the Ogg format to be a dubious choice in just about every situation. Why then do certain organisations and individuals persist in promoting it with such ferocity?</p>
<p>When challenged, three types of reaction are characteristic of the Ogg campaigners.</p>
<p>On occasion, these people will assume an apologetic tone, explaining how Ogg was only ever designed for simple audio-only streams (ignoring it is as bad for these as for anything), and this is no doubt true. Why then, I ask again, do they continue to tout Ogg as the one-size-fits-all solution they already admitted it is not?</p>
<p>More commonly, the Ogg proponents will respond with hand-waving arguments best summarised as <em id="notbad" onmouseover="document.getElementById('notbad').textContent='Ogg isn\'t dead, it\'s just resting'" onmouseout="document.getElementById('notbad').textContent='Ogg isn\'t bad, it\'s just different'">Ogg isn&#8217;t bad, it&#8217;s just different</em>. My reply to this assertion is twofold:</p>
<ul>
<li>Being too different <em>is</em> bad. We live in a world where multimedia files come in many varieties, and a decent media player will need to handle the majority of them. Fortunately, most multimedia file formats share some basic traits, and they can easily be processed in the same general framework, the specifics being taken care of at the input stage. A format deviating too far from the standard model becomes problematic.</li>
<li>Ogg <em>is</em> bad. When every angle of examination reveals serious flaws, bad is the only fitting description.</li>
</ul>
<p>The third reaction bypasses all technical analysis: <em>Ogg is patent-free</em>, a claim I am not qualified to directly discuss. Assuming it is true, it still does not alter the fact that <u>Ogg is a bad format</u>. Being free from patents does not magically make Ogg a good choice as file format. If all the standard formats are indeed covered by patents, the only proper solution is to design a new, good format which is not, this time hopefully avoiding the old mistakes.</p>
]]></content:encoded>
			<wfw:commentRss>http://hardwarebug.org/2010/03/03/ogg-objections/feed/</wfw:commentRss>
		<slash:comments>93</slash:comments>
		</item>
		<item>
		<title>1080p video on Beagle</title>
		<link>http://hardwarebug.org/2010/02/10/1080p-video-on-beagle/</link>
		<comments>http://hardwarebug.org/2010/02/10/1080p-video-on-beagle/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 01:22:35 +0000</pubDate>
		<dc:creator>Mans</dc:creator>
				<category><![CDATA[Multimedia]]></category>

		<guid isPermaLink="false">http://hardwarebug.org/?p=299</guid>
		<description><![CDATA[FOSDEM 2010 is over, and I&#8217;d like to call it a success for FFmpeg. The 11-man strong delegation showed the stunned audience a smashing demo featuring a Beagle-powered video wall. It looked like this: The people who pulled this off look like this:]]></description>
			<content:encoded><![CDATA[<p>FOSDEM 2010 is over, and I&#8217;d like to call it a success for <a href="http://ffmpeg.org/">FFmpeg</a>.  The 11-man strong delegation showed the stunned audience a smashing demo featuring a <a href="http://beagleboard.org/">Beagle</a>-powered video wall. It looked like this:</p>
<p><object class="frame-outer  size-full aligncenter" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="384" height="313" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/9pwUdRKllo0&amp;hl=en_US&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="384" height="313" src="http://www.youtube.com/v/9pwUdRKllo0&amp;hl=en_US&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>The people who pulled this off look like this:</p>
<p style="text-align: center;"><a href="http://hardwarebug.org/wp-content/uploads/2010/02/img_3133.jpg"><img class="size-full wp-image-321 aligncenter" title="The FFmpeg team at FOSDEM 2010" src="http://hardwarebug.org/wp-content/uploads/2010/02/img_3133_small.jpg" alt="The FFmpeg team" width="384" height="256" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://hardwarebug.org/2010/02/10/1080p-video-on-beagle/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>IJG swings again, and misses</title>
		<link>http://hardwarebug.org/2010/02/01/ijg-swings-again-and-misses/</link>
		<comments>http://hardwarebug.org/2010/02/01/ijg-swings-again-and-misses/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 16:18:24 +0000</pubDate>
		<dc:creator>Mans</dc:creator>
				<category><![CDATA[Multimedia]]></category>

		<guid isPermaLink="false">http://hardwarebug.org/?p=234</guid>
		<description><![CDATA[Earlier this month the IJG unleashed version 8 of its ubiquitous libjpeg library on the world. Eager to try out the &#8220;major breakthrough in image coding technology&#8221; promised in the README file accompanying v7, I downloaded the release. A glance at the README file suggests something major indeed is afoot: Version 8.0 is the first [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier this month the <a href="http://ijg.org/">IJG</a> unleashed version 8 of its ubiquitous libjpeg library on the world. Eager to try out the &#8220;major breakthrough in image coding technology&#8221; <a href="http://hardwarebug.org/2009/08/04/ijg-is-back/">promised</a> in the README file accompanying v7, I downloaded the release. A glance at the README file suggests something major indeed is afoot:</p>
<blockquote><p>Version 8.0 is the first release of a new generation JPEG standard to overcome the limitations of the original JPEG specification.</p></blockquote>
<p>The text also hints at the existence of a <a href="http://jpegclub.org/temp/ITU-T-JPEG-Plus-Proposal_R3.doc">document</a> detailing these marvellous new features, and a Google search later a copy has found its way onto my monitor. As I read, however, my state of mind shifts from an initial excited curiosity, through bewilderment and disbelief, finally arriving at pure merriment.<br />
<span id="more-234"></span><br />
Already on the first page it becomes clear no new JPEG standard in fact exists. All we have is an unsolicited proposal sent to the ITU-T by members of the IJG. Realising that even the most brilliant of inventions must start off as mere proposals, I carry on reading. The summary informs me that I am about to witness the introduction of three extensions to the T.81 JPEG format:</p>
<ol>
<li>An alternative coefficient scan sequence for DCT coefficient serialization</li>
<li>A SmartScale extension in the Start-Of-Scan (SOS) marker segment</li>
<li>A Frame Offset definition in or in addition to the Start-Of-Frame (SOF) marker segment</li>
</ol>
<p>Together these three extensions will, it is promised, &#8220;bring DCT based JPEG back to the forefront of state-of-the-art image coding technologies.&#8221;</p>
<h3>Alternative scan</h3>
<p>The first of the proposed extensions introduces an alternative DCT coefficient scan sequence to be used in place of the zigzag scan employed in most block transform based codecs.</p>
<div id="attachment_239" class="wp-caption aligncenter" style="width: 335px"><img class="size-full wp-image-239" title="jpeg8-alt-scan" src="http://hardwarebug.org/wp-content/uploads/2010/02/jpeg8-alt-scan.png" alt="Alternative scan sequence" width="325" height="256" /><p class="wp-caption-text">Alternative scan sequence</p></div>
<p>The advantage of this scan would be that combined with the existing progressive mode, it simplifies decoding of an initial low-resolution image which is enhanced through subsequent passes. The author of the document calls this scheme  &#8220;image-pyramid/hierarchical multi-resolution coding.&#8221; It is not immediately obvious to me how this constitutes even a small advance in image coding technology.</p>
<p>At this point I am beginning to suspect that our friend from the IJG has been trapped in a half-world between interlaced GIF images transmitted down noisy phone lines and today&#8217;s inferno of SVC, MVC, and other buzzwords.</p>
<h3>(Not so) SmartScale</h3>
<p>Disguised behind this camel-cased moniker we encounter a method which, we are told, will provide better image quality at high compression ratios. The author has combined two well-known (to us) properties in a (to him) clever way.</p>
<p>The first property concerns the perceived impact of different types of distortion in an image. When encoding with JPEG, as the quantiser is increased, the decoded image becomes ever more blocky. At a certain point, a better subjective visual quality can be achieved by down-sampling the image before encoding it, thus allowing a lower quantiser to be used. If the decoded image is scaled back up to the original size, the unpleasant, blocky appearance is replaced with a smooth blur.</p>
<p>The second property belongs to the DCT where, as we all know, the top-left (DC) coefficient is the average of the entire block, its neighbours represent the lowest frequency components etc. A top-left-aligned subset of the coefficient block thus represents a low-resolution version of the full block in the spatial domain.</p>
<p>In his flash of genius, our hero came up with the idea of using the DCT for down-scaling the image. Unfortunately, he appears to possess precious little knowledge of sampling theory and human visual perception. Any block-based resampling will inevitably produce sharp artefacts along the block edges. The human visual system is particularly sensitive to sharp edges, so this is one of the most unwanted types of distortion in an encoded image.</p>
<p>Despite the obvious flaws in this approach, I decided to give it a try. After all, the software is already written, allowing downscaling by factors of 8/8..16.</p>
<p>Using a 1280&#215;720 <a href="http://hardwarebug.org/wp-content/uploads/2010/02/parkrun.png">test image</a>, I encoded it with each of the nine scaling options, from unity to half size, each time adjusting the quality parameter for a final encoded file size of no more than 200000 bytes. The following table presents the encoded file size, the libjpeg quality parameter used, and the <a href="http://en.wikipedia.org/wiki/Structural_similarity">SSIM</a> metric for each of the images.</p>
<table width="50%" align="center">
<tbody>
<tr style="text-align: left">
<th>Scale</th>
<th>Size</th>
<th>Quality</th>
<th>SSIM</th>
</tr>
<tr>
<td>8/8</td>
<td>198462</td>
<td>59</td>
<td>0.940</td>
</tr>
<tr>
<td>8/9</td>
<td>196337</td>
<td>70</td>
<td>0.936</td>
</tr>
<tr>
<td>8/10</td>
<td>196133</td>
<td>79</td>
<td>0.934</td>
</tr>
<tr>
<td>8/11</td>
<td>197179</td>
<td>84</td>
<td>0.927</td>
</tr>
<tr>
<td>8/12</td>
<td>193872</td>
<td>89</td>
<td>0.915</td>
</tr>
<tr>
<td>8/13</td>
<td>197153</td>
<td>92</td>
<td>0.914</td>
</tr>
<tr>
<td>8/14</td>
<td>188334</td>
<td>94</td>
<td>0.899</td>
</tr>
<tr>
<td>8/15</td>
<td>198911</td>
<td>96</td>
<td>0.886</td>
</tr>
<tr>
<td>8/16</td>
<td>197190</td>
<td>97</td>
<td>0.869</td>
</tr>
</tbody>
</table>
<p>Although the smaller images allowed a higher quality setting to be used, the SSIM value drops significantly. Numbers may of course be misleading, but the images below speak for themselves. These are cut-outs from the full image, the original on the left, unscaled JPEG-compressed in the middle, and JPEG with 8/16 scaling to the right.</p>
<div style="text-align: center;"><a href="http://hardwarebug.org/wp-content/uploads/2010/02/parkrun-cut1.png"><img class="alignnone size-full wp-image-246" title="Original" src="http://hardwarebug.org/wp-content/uploads/2010/02/parkrun-cut1.png" alt="" width="128" height="140" /></a><a href="http://hardwarebug.org/wp-content/uploads/2010/02/jpeg8-88-cut1.png"><img class="alignnone size-full wp-image-247" title="No scaling" src="http://hardwarebug.org/wp-content/uploads/2010/02/jpeg8-88-cut1.png" alt="" width="128" height="140" /></a><a href="http://hardwarebug.org/wp-content/uploads/2010/02/jpeg8-816-cut1.png"><img class="alignnone size-full wp-image-244" title="Scale 8/16" src="http://hardwarebug.org/wp-content/uploads/2010/02/jpeg8-816-cut1.png" alt="" width="128" height="140" /></a></div>
<div style="text-align: center;"><a href="http://hardwarebug.org/wp-content/uploads/2010/02/parkrun-cut2.png"><img class="alignnone size-full wp-image-255" title="Original" src="http://hardwarebug.org/wp-content/uploads/2010/02/parkrun-cut2.png" alt="" width="128" height="140" /></a><a href="http://hardwarebug.org/wp-content/uploads/2010/02/jpeg8-88-cut2.png"><img class="alignnone size-full wp-image-256" title="No scaling" src="http://hardwarebug.org/wp-content/uploads/2010/02/jpeg8-88-cut2.png" alt="" width="128" height="140" /></a><a href="http://hardwarebug.org/wp-content/uploads/2010/02/jpeg8-816-cut2.png"><img class="alignnone size-full wp-image-257" title="Scale 8/16" src="http://hardwarebug.org/wp-content/uploads/2010/02/jpeg8-816-cut2.png" alt="" width="128" height="140" /></a></div>
<p>Looking at these images, I do not need to hesitate before picking the JPEG variant I prefer.</p>
<h3>Frame offset</h3>
<p>The third and final extension proposed is quite simple and also quite pointless: a top-left cropping to be applied to the decoded image. The alleged utility of this feature would be to enable lossless cropping of a JPEG image. In a typical image workflow, however, JPEG is only used for the final published version, so the need for this feature appears quite far-fetched.</p>
<h3>The grand finale</h3>
<p>Throughout the text, the author makes references to &#8220;the fundamental DCT property for image representation.&#8221; In his own words:</p>
<blockquote><p>This property was found by the author during implementation of the new DCT scaling features and is after his belief one of the most important discoveries in digital image coding after releasing the JPEG standard in 1992.</p></blockquote>
<p>The secret is to be revealed in an annex to the main text. This annex quotes in full a post by the author to the comp.dsp Usenet group in a thread with the subject <a href="http://groups.google.com/group/comp.dsp/browse_frm/thread/4eb02c1d668daa7d">why DCT</a>. Reading the entire thread proves quite amusing. A few excerpts follow.</p>
<blockquote>
<div class="frame-outer small">
<div style="text-align: left;">The actual reason is much simpler, and therefore apparently very difficult to recognize by complicated-thinking people.<br/><br />
Here is the explanation:<br/><br />
What are people doing when they have a bunch of images and want a quick preview?  They use thumbnails!  What are thumbnails? Thumbnails are small downscaled versions of the original image! If you want more details of the image, you can zoom in stepwise by enlarging (upscaling) the image.</div>
</div>
</blockquote>
<blockquote>
<div class="frame-outer small">
<div style="text-align: left;">So with proper understanding of the fundamental DCT property, the MPEG folks could make their videos more scalable, but, as in the case of JPEG, they are unable to recognize this simple but basic property, unfortunately, and pursue rather inferior approaches in actual developments.</div>
</div>
</blockquote>
<blockquote>
<div class="frame-outer small">
<div style="text-align: left;">These are just phrases, and they don&#8217;t explain anything. But this is typical for the current state in this field: The relevant people ignore and deny the true reasons, and thus they turn in a circle and no progress is being made.</div>
</div>
</blockquote>
<blockquote>
<div class="frame-outer small">
<div style="text-align: left;">However, there are dark forces in action today which ignore and deny any fruitful advances in this field. That is the reason that we didn&#8217;t see any progress in JPEG for more than a decade, and as long as those forces dominate, we will see more confusion and less enlightenment. The truth is always simple, and the DCT *is* simple, but this fact is suppressed by established people who don&#8217;t want to lose their dubious position.</div>
</div>
</blockquote>
<p>I believe a trip to the <a href="http://en.wikipedia.org/wiki/Technology_in_The_Hitchhiker%27s_Guide_to_the_Galaxy#Total_Perspective_Vortex">Total Perspective Vortex</a> may be in order. Perhaps his tin-foil hat will save him.</p>
]]></content:encoded>
			<wfw:commentRss>http://hardwarebug.org/2010/02/01/ijg-swings-again-and-misses/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>IJG is back</title>
		<link>http://hardwarebug.org/2009/08/04/ijg-is-back/</link>
		<comments>http://hardwarebug.org/2009/08/04/ijg-is-back/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 02:17:48 +0000</pubDate>
		<dc:creator>Mans</dc:creator>
				<category><![CDATA[Multimedia]]></category>

		<guid isPermaLink="false">http://hardwarebug.org/?p=140</guid>
		<description><![CDATA[When FFmpeg released version 0.5 earlier this year, nearly five years had passed since the previous release, during which time the project had attracted frequent criticism for the lack of regular releases. There exists, however, a project whose release interval dwarves that of FFmpeg. I speak of the Independent JPEG Group&#8217;s libjpeg, version 7 of [...]]]></description>
			<content:encoded><![CDATA[<p>When <a href="http://ffmpeg.org/">FFmpeg</a> released version 0.5 earlier this year, nearly five years had passed since the previous release, during which time the project had attracted frequent criticism for the lack of regular releases. There exists, however, a project whose release interval dwarves that of FFmpeg. I speak of the <a href="http://ijg.org/">Independent JPEG Group&#8217;s</a> libjpeg, version 7 of which was recently released after 11 years of silence.</p>
<p>So what have they been doing during the last 11 years? Not a lot, it seems. The only change log entry I find noteworthy is the addition of arithmetic entropy coding, previously omitted due to patent concerns. Contrast this with the <em>TO DO</em> note from the previous release:</p>
<blockquote><p>The major thrust for v7 will probably be improvement of visual quality. The current method for scaling the quantization tables is known not to be very good at low Q values.  We also intend to investigate block boundary smoothing, &#8220;poor man&#8217;s variable quantization&#8221;, and other means of improving quality-vs-file-size performance without sacrificing compatibility.</p>
<p>In future versions, we are considering supporting some of the upcoming JPEG Part 3 extensions &#8212; principally, variable quantization and the SPIFF file format.</p>
<p>As always, speeding things up is of great interest.</p></blockquote>
<p>Eleven years is of course plenty of time for the developers to change their minds, or perhaps even lose them. The <em>TO DO</em> note in version 7 reads thus:<br />
<span id="more-140"></span></p>
<blockquote><p>v7 is basically just a necessary interim release, paving the way for a major breakthrough in image coding technology with the next v8 package which is scheduled for release in the year 2010.</p></blockquote>
<p>Immediately preceding this note is a rant against rival image compression formats:</p>
<blockquote><p>The ISO JPEG standards committee actually promotes different formats like JPEG-2000 or JPEG-XR which are incompatible with original DCT-based JPEG and which are based on faulty technologies.  IJG therefore does not and will not support such momentary mistakes (see REFERENCES). We have little or no sympathy for the promotion of these formats.  Indeed, one of the original reasons for developing this free software was to help force convergence on common, interoperable format standards for JPEG files. Don&#8217;t use an incompatible file format!</p></blockquote>
<p>Evidently, the authors are of the opinion that JPEG is the one, true image coding format. Under this premise, what could the &#8220;major breakthrough&#8221; possibly be all about? The <em>REFERENCES</em> section provides a hint:</p>
<blockquote><p>The best currently available description of JPEG is the textbook &#8220;JPEG Still Image Data Compression Standard&#8221; by William B. Pennebaker and Joan L. Mitchell, published by Van Nostrand Reinhold, 1993, ISBN 0-442-01272-1. Price US$59.95, 638 pp.  The book includes the complete text of the ISO JPEG standards (DIS 10918-1 and draft DIS 10918-2).</p>
<p>Although this is by far the most detailed and comprehensive exposition of JPEG publicly available, we point out that it is still missing an explanation of the most essential properties and algorithms of the underlying DCT technology.</p>
<p>If you think that you know about DCT-based JPEG after reading this book, then you are in delusion. The real fundamentals and corresponding potential of DCT-based JPEG are not publicly known so far, and that is the reason for all the mistaken developments taking place in the image coding domain.</p></blockquote>
<p>Again, a comparison with the corresponding paragraph from the previous release is revealing:</p>
<blockquote><p>The best full description of JPEG is the textbook &#8220;JPEG Still Image Data Compression Standard&#8221; by William B. Pennebaker and Joan L. Mitchell, published by Van Nostrand Reinhold, 1993, ISBN 0-442-01272-1.  Price US$59.95, 638 pp. The book includes the complete text of the ISO JPEG standards (DIS 10918-1 and draft DIS 10918-2).  This is by far the most complete exposition of JPEG in existence, and we highly recommend it.</p></blockquote>
<p>Even if the IJG were to be in possession of the powerful secret they allude to, how they would make use of it while staying compatible with the JPEG specification is a mystery to me. Short of using a time machine to travel back in time and alter the original text, nothing springs to mind, although that would of course qualify as a &#8220;major breakthrough.&#8221; Next year we will know. Until then, I call vapour-ware.</p>
]]></content:encoded>
			<wfw:commentRss>http://hardwarebug.org/2009/08/04/ijg-is-back/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ogg timestamps explored</title>
		<link>http://hardwarebug.org/2008/11/17/ogg-timestamps-explored/</link>
		<comments>http://hardwarebug.org/2008/11/17/ogg-timestamps-explored/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 23:36:45 +0000</pubDate>
		<dc:creator>Mans</dc:creator>
				<category><![CDATA[Multimedia]]></category>

		<guid isPermaLink="false">http://hardwarebug.org/?p=46</guid>
		<description><![CDATA[Ogg is the name of a multimedia container format invented by the Xiph Foundation. Moreover, it is a deeply flawed format. One of its many flaws relates to timestamps, an aspect of Ogg I shall explore in this article. Ogg structure The Ogg format splits elementary stream data into a sequence of packets which are [...]]]></description>
			<content:encoded><![CDATA[<p>Ogg is the name of a multimedia container format invented by the <a href="http://xiph.org">Xiph Foundation</a>. Moreover, it is a deeply flawed format. One of its many flaws relates to timestamps, an aspect of Ogg I shall explore in this article.</p>
<h1>Ogg structure</h1>
<p>The Ogg format splits elementary stream data into a sequence of packets which are then distributed arbitrarily across <em>pages</em>. A page can contain any number of packets, and a packet can span any number of pages. This two-level packetisation scheme is used since the packet headers would otherwise, due to design shortfalls elsewhere, become prohibitively large.</p>
<h1>Timestamps in Ogg</h1>
<p>Each Ogg page (not packet) header contains a timestamp, or <em>granule position</em> in Ogg terms, encoded as a 64-bit number. The precise interpretation of this number is not defined by the Ogg specification; it depends on the codec used for each elementary stream. The specification does, however, tell us one thing:</p>
<blockquote><p>The position specified is the total samples encoded after including all packets finished on this page (packets begun on this page but continuing on to the next page do not count).</p></blockquote>
<p>The meaning of <em>samples</em> is, again, left unspecified. It is merely suggested that it could refer to video frames or audio PCM samples.</p>
<p>Timestamping the end of packets, instead of the start, is impractical for a number of reasons including, but not limited to, the following:</p>
<ul>
<li>Scheduling decoded samples for playback is more easily done based on the desired start time than on the end time.</li>
<li>Virtually every other container format ties timestamps to the start of the first following sample. Doing it differently only complicates players and other tools supporting multiple formats without providing any advantage.</li>
<li>Inferring the timestamp of the first sample of the stream is impossible without first decoding, at least partially, every packet in the first page.</li>
</ul>
<h1>Timestamp interpretation</h1>
<p>As mentioned previously, the meaning of the 64-bit timestamps associated with an elementary stream depends on the codec of the stream. I conducted a survey of codecs with defined Ogg mappings looking specifically at their timestamp definitions.<span id="more-46"></span></p>
<h3>Vorbis</h3>
<p>The <a href="http://xiph.org/vorbis/doc/Vorbis_I_spec.html">Vorbis  specification</a> includes a convoluted definition of the <em>granule position</em>.  In summary, it defines the timestamp as the number of PCM samples obtained by decoding the stream from the beginning, up through the last packet completed on the page.</p>
<h3>FLAC</h3>
<p>The <a href="http://flac.sourceforge.net/ogg_mapping.html">FLAC-in-Ogg specification</a> defines its Ogg timestamp as being &#8220;same as Vorbis&#8221;, and for clarification offers a broken link to the Vorbis spec.</p>
<h3>Speex</h3>
<p>The <a href="http://speex.org/docs/manual/speex-manual/node8.html">Ogg mapping for Speex</a> states only that &#8220;the <tt>granulepos</tt> is the number of the last sample encoded in that packet.&#8221; There is not even an explanation of which packet this refers to. Speex being a Xiph-made codec, a reasonable assumption is that semantics equal to those for Vorbis and FLAC are intended.</p>
<h3>Theora</h3>
<p><a href="http://theora.org/">Theora</a>, the much-hyped video codec abandoned by On2 and adopted by Xiph, naturally needs its very own timestamp format. In usual Ogg manner, the timestamp refers to end of the display interval of the last frame obtained after decoding the last packet of the page, but not without a twist. The 64-bit timestamp is split into two fields. The first of these fields encodes the frame number, starting from one, of the key frame most recently preceding the frame to which the timestamp applies. The second timestamp field encodes the number of frames, this time starting from zero, since the most recent key frame. The bit position of the field split is specified by the Theora stream header.</p>
<h3>Dirac</h3>
<p>Of the handful of codecs including a specification for use in the Ogg container, <a href="http://diracvideo.org/">Dirac</a> stands out as the only one not invented/branded by Xiph. Rather, it is a product of the BBC.</p>
<p>The <a href="http://diracvideo.org/wiki/DiracInOgg">Dirac-in-Ogg specification</a> presents a confusing read. First, we are informed that</p>
<blockquote><p>The unit of encapsulation for Ogg is the packet; a packet of Dirac shall contain:<br />
— zero or more non-picture Dirac data units<br />
followed by a single:<br />
— Dirac picture data unit, or<br />
— Dirac end-of-sequence data unit<br />
NOTE It follows that no Dirac data unit shall span multiple packets.</p></blockquote>
<p>Next, we are treated to this rule:</p>
<blockquote><p>In a logical stream of Dirac, an Ogg page should not terminate multiple Ogg packets.</p></blockquote>
<p>So far, so good. Proceeding to the next sub-clause, still on the same page, things get interesting:</p>
<blockquote><p>The granule position applies to the picture contained within the first packet that terminates in an Ogg page; any subsequent packets that terminate within the same page do not have a granule position.</p></blockquote>
<p>Stop right there. Only moments ago, we were told that at most one packet may terminate in any page. There can be no &#8220;subsequent packets that terminate within the same page.&#8221; That sentence provides nothing but confusion. Perhaps an earlier version of the specification allowed multiple packets per page, and this statement was accidentally left behind.</p>
<p>Finally, we arrive at the definition of the timestamp value itself. Like for Theora, the 64-bit value is made up from several sub-fields. The Dirac spec is agreeably terse in its description:</p>
<blockquote><p>The granule position is composed of the following:</p>
<dl>
<dt>pt</dt>
<dd>Picture number (in display order). When picture_coding_mode = 0 (progressive), <code>pt</code> increments by two for each picture in display order. When picture_coding_mode = 1 (interlace), <code>pt</code> increments by one for each field in display order.</dd>
<dt>dist</dt>
<dd>Number of pictures in coded order (equal to number of packets) since an appropriate sync point that allows for correct decoding of <code>pt</code>. This field is split into two parts, <code>dist_h</code> and <code>dist_l</code>. <code>dist = (dist_h &lt;&lt; 8)|dist_l</code></dd>
<dt>delay</dt>
<dd>Delay (in pictures) between decoding time and presentation time of pt.</dd>
</dl>
<p>These values shall be packed into granule position as follows:</p>
<pre>  6                              3 3 2      2 2
  3                              1 0 9      2 1           9 8 7      0
+---------------------------------+-+--------+-------------+-+--------+
|           pt - delay            |0| dist_h |    delay    |0| dist_l |
+---------------------------------+-+--------+-------------+-+--------+
|&lt;---------------- high_word ---------------&gt;|&lt;------ low_word ------&gt;|</pre>
</blockquote>
<p>In other words, the stored value encodes the DTS of the frame and the difference between DTS and PTS. What the text fails to relay is whether this time refers to the start or end of the display interval of the frame.</p>
<h3>OGM</h3>
<p>Once <a href="http://www.xiph.org/container/ogm.html">despised</a>, now <a href="http://svn.xiph.org/trunk/oggds/">embraced</a>, by Xiph, OGM is a scheme for storing any Microsoft Directshow-compatible data in an Ogg container. A formal specification does not exist, so the only means of obtaining information about this format is to read the <a href="https://trac.xiph.org/browser/trunk/oggds">oggds source code</a>.</p>
<p>The oggds code incorrectly relates Vorbis timestamps to the beginning of packets, not the end, and non-Vorbis packets are handled in the same fashion. The practical implication of this is that software must treat OGM streams specially, even when they contain Vorbis audio.</p>
]]></content:encoded>
			<wfw:commentRss>http://hardwarebug.org/2008/11/17/ogg-timestamps-explored/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

