{"id":3168,"date":"2011-02-19T22:23:17","date_gmt":"2011-02-20T06:23:17","guid":{"rendered":"http:\/\/multimedia.cx\/eggs\/?p=3168"},"modified":"2011-03-08T12:06:29","modified_gmt":"2011-03-08T20:06:29","slug":"decoding-vp8-on-a-sega-dreamcast","status":"publish","type":"post","link":"https:\/\/multimedia.cx\/eggs\/decoding-vp8-on-a-sega-dreamcast\/","title":{"rendered":"Decoding VP8 On A Sega Dreamcast"},"content":{"rendered":"<p>I got Google&#8217;s <a href=\"http:\/\/www.webmproject.org\/code\/\">libvpx VP8 codec library<\/a> to compile and run on the Sega Dreamcast with its Hitachi\/Renesas SH-4 200 MHz CPU. So give Google\/On2 their due credit for writing portable software. I&#8217;m not sure how best to illustrate this so please accept this still photo depicting my testbench Dreamcast console driving video to my monitor:<\/p>\n<p><center><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2011\/02\/vp8-on-sega-dreamcast.jpg\" alt=\"\" title=\"Google&#039;s libvpx VP8 decoder running on a Sega Dreamcast\" width=\"400\" height=\"231\" class=\"aligncenter size-full wp-image-3169\" srcset=\"https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2011\/02\/vp8-on-sega-dreamcast.jpg 400w, https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2011\/02\/vp8-on-sega-dreamcast-300x173.jpg 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><br \/>\n<\/center><\/p>\n<p>Why? Because I wanted to try my hand at porting some existing software to this console and because I tend to be most comfortable working with assorted multimedia software components. This seemed like it would be a good exercise.<\/p>\n<p>You may have observed that <strong>the video is blue.<\/strong> Shortest, simplest answer: <em>Pure laziness.<\/em> Short, technical answer: Path of least resistance for getting through this exercise. Longer answer follows.<\/p>\n<blockquote><p><strong>Update:<\/strong> I did eventually realize that the Dreamcast can work with YUV textures. <a href=\"http:\/\/multimedia.cx\/eggs\/notes-on-linux-for-dreamcast\/\">Read more in my followup post<\/a>.<\/p><\/blockquote>\n<p><strong>Process and Pitfalls<\/strong><br \/>\nlibvpx comes with a number of little utilities including <code>decode_to_md5.c<\/code>. The first order of business was porting over enough source files to make the VP8 decoder compile along with the MD5 testbench utility.<\/p>\n<p>Again, I used the <a href=\"http:\/\/gamedev.allusion.net\/softprj\/kos\/\">KallistiOS (KOS) console RTOS<\/a> <em>(aside: I&#8217;m still working to get modern Linux kernels compiled for the Dreamcast)<\/em>. I started by configuring and compiling libvpx on a regular desktop Linux system. From there, I was able to modify a number of configuration options to make the build more amenable to the embedded RTOS.<br \/>\n<!--more--><br \/>\nI had to create a few shim header files that mapped various functions related to threading and synchronization to their KOS equivalents. For example, KOS has a threading library cleverly named kthreads which is mostly compatible with the more common pthread library functions. KOS apparently also predates stdint.h, so I had to contrive a file with those basic types.<\/p>\n<p>So I got everything compiled and then uploaded the binary along with a small VP8 IVF test vector. Imagine my surprise when an MD5 sum came out of the serial console. Further, visualize my utter speechlessness when I noticed that the MD5 sum matched what my desktop platform produced. It worked!<\/p>\n<p>Almost. When I tried to decode all frames in a test vector, the program would invariably crash. The problem was that the file that manages motion compensation (reconinter.c) needs to define MUST_BE_ALIGNED which compiles byte-wise block copy functions. This is necessary for CPUs like the SH-4 which can&#8217;t load unaligned data. Apparently, even ARM CPUs these days can handle unaligned memory accesses which is why this isn&#8217;t a configure-time option.<\/p>\n<p><strong>Showing The Work<\/strong><br \/>\nI completed the first testbench application which ran the MD5 test on all 17 official IVF test vectors. The SH-4\/Dreamcast version aces the whole suite.<\/p>\n<p>However, this is a video game console, so I had better be able to show the decoded video. The Dreamcast is strictly RGB&#8211; forget about displaying YUV data directly. I could take the performance hit to convert YUV -&gt; RGB. Or, I could just display the intensity information (Y plane) rendered on a random color scale (I chose blue) on an RGB565 texture (the DC&#8217;s graphics hardware can also do paletted textures but those need to be rearranged\/twiddled\/swizzled).<\/p>\n<p><strong>Results<\/strong><br \/>\nSo, can the Dreamcast decode VP8 video in realtime? <strong>Sure!<\/strong> Well, I really need to qualify. In the test depicted in the picture, it seems to be realtime (though I wasn&#8217;t enforcing proper frame timings, just decoding and displaying as quickly as possible). Obviously, I wasn&#8217;t bothering to properly convert YUV -&gt; RGB. Plus, that Big Buck Bunny test vector clip is only 176&#215;144. Obviously, no audio decoding either.<\/p>\n<p>So, realtime playback, with a little fine print.<\/p>\n<p>On the plus side, it&#8217;s trivial to get the Dreamcast video hardware to upscale that little blue image to fullscreen.<\/p>\n<p>I was able to tally the total milliseconds&#8217; worth of wall clock time required to decode the 17 VP8 test vectors. As you can probably work out from this list, when I try to play a 320&#215;240 video, things start to break down.<\/p>\n<ol>\n<li>Processed 29 176&#215;144 frames in 987 milliseconds.<\/li>\n<li>Processed 49 176&#215;144 frames in 1809 milliseconds.<\/li>\n<li>Processed 49 176&#215;144 frames in 704 milliseconds.<\/li>\n<li>Processed 29 176&#215;144 frames in 255 milliseconds.<\/li>\n<li>Processed 49 176&#215;144 frames in 339 milliseconds.<\/li>\n<li>Processed 48 175&#215;143 frames in 2446 milliseconds.<\/li>\n<li>Processed 29 176&#215;144 frames in 432 milliseconds.<\/li>\n<li>Processed 2 1432&#215;888 frames in 2060 milliseconds.<\/li>\n<li>Processed 49 176&#215;144 frames in 1884 milliseconds.<\/li>\n<li>Processed 57 320&#215;240 frames in 5792 milliseconds.<\/li>\n<li>Processed 29 176&#215;144 frames in 989 milliseconds.<\/li>\n<li>Processed 29 176&#215;144 frames in 740 milliseconds.<\/li>\n<li>Processed 29 176&#215;144 frames in 839 milliseconds.<\/li>\n<li>Processed 49 175&#215;143 frames in 2849 milliseconds.<\/li>\n<li>Processed 260 320&#215;240 frames in 29719 milliseconds.<\/li>\n<li>Processed 29 176&#215;144 frames in 962 milliseconds.<\/li>\n<li>Processed 29 176&#215;144 frames in 933 milliseconds.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Compiling Google&#8217;s libvpx (video component of WebM) for the old Sega Dreamcast video game console<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[221,219],"tags":[],"class_list":["post-3168","post","type-post","status-publish","format-standard","hentry","category-sega-dreamcast","category-vp8"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/3168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/comments?post=3168"}],"version-history":[{"count":8,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/3168\/revisions"}],"predecessor-version":[{"id":3201,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/3168\/revisions\/3201"}],"wp:attachment":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/media?parent=3168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/categories?post=3168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/tags?post=3168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}