{"id":4520,"date":"2017-01-05T22:13:54","date_gmt":"2017-01-06T06:13:54","guid":{"rendered":"https:\/\/multimedia.cx\/eggs\/?p=4520"},"modified":"2017-01-05T22:13:54","modified_gmt":"2017-01-06T06:13:54","slug":"writing-a-dreamcast-media-player","status":"publish","type":"post","link":"https:\/\/multimedia.cx\/eggs\/writing-a-dreamcast-media-player\/","title":{"rendered":"Writing A Dreamcast Media Player"},"content":{"rendered":"<p>I know I&#8217;m not the only person to have the idea to port a media player to the Sega Dreamcast video game console. But I did make significant progress on an implementation. I&#8217;m a little surprised to realize that I haven&#8217;t written anything about it on this blog yet, given my propensity for publishing my programming misadventures.<\/p>\n<p><center><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2010\/09\/3-dreamcast-consoles.jpg\" alt=\"3 Dreamcast consoles in a row\" width=\"450\" height=\"111\" class=\"aligncenter size-full wp-image-2870\" srcset=\"https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2010\/09\/3-dreamcast-consoles.jpg 450w, https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2010\/09\/3-dreamcast-consoles-300x74.jpg 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><br \/>\n<\/center><\/p>\n<p>This old effort had been on my mind lately due to its architectural similarities to something else I was recently brainstorming.<\/p>\n<p><strong>Early Days<\/strong><br \/>\nPorting a multimedia player was one of the earliest endeavors that I embarked upon in the multimedia domain. It&#8217;s a bit fuzzy for me now, but I&#8217;m pretty sure that my first exposure to <a href=\"http:\/\/www.mplayerhq.hu\/\">the MPlayer project<\/a> in 2001 arose from looking for a multimedia player to port. I fed it through the Dreamcast development toolchain but encountered roadblocks pretty quickly. However, this got me looking at the MPlayer source code and made me wonder how I could contribute, which is how I finally broke into practical open source multimedia hacking after studying the concepts and technology for more than a year at that point.<\/p>\n<p>Eventually, I jumped over to <a href=\"https:\/\/www.xine-project.org\/home\">the xine project<\/a>. After hacking on that for awhile, I remembered my DC media player efforts and endeavored to compile xine to the console. The first attempt was to simply compile the codebase using the Dreamcast hobbyist community&#8217;s toolchain. This is when I came to fear the multithreaded snake pit in xine&#8217;s core. Again, my memories are hazy on the specifics, but I remember the engine having a bunch of threading hacks with comments along the lines of &#8220;this code deadlocks sometimes, so on shutdown, monitor this lock and deliberately break it if it has been more than 3 seconds&#8221;.<\/p>\n<p><strong>Something Workable<\/strong><br \/>\nEventually, I settled on a combination of FFmpeg&#8217;s libavcodec library for audio and video decoders, xine&#8217;s demuxer library, and xine&#8217;s input API, combined with my own engine code to tie it all together along with video and output drivers provided by the <a href=\"http:\/\/gamedev.allusion.net\/softprj\/kos\/\">KallistiOS hobbyist OS<\/a> for Dreamcast. Here is a simple diagram of the data movement through this player:<\/p>\n<p><center><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2017\/01\/sega-dreamcast-media-player-architecture.png\" alt=\"Architecture diagram for a Sega Dreamcast media player\" width=\"711\" height=\"299\" class=\"aligncenter size-full wp-image-4522\" srcset=\"https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2017\/01\/sega-dreamcast-media-player-architecture.png 711w, https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2017\/01\/sega-dreamcast-media-player-architecture-300x126.png 300w\" sizes=\"auto, (max-width: 711px) 100vw, 711px\" \/><br \/>\n<\/center><\/p>\n<p><strong>Details and Challenges<\/strong><br \/>\nThis is a rare occasion when I actually got to write the core of a media player engine. I made some mistakes.<\/p>\n<p>xine&#8217;s internal clock ran at 90000 Hz. At least, its internal timestamps were all in reference to a 90 kHz clock. I got this brilliant idea to trigger timer interrupts at 6000 Hz to drive the engine. Whatever the timer facilities on the Dreamcast, I found that 6 kHz was the greatest common divisor with 90 kHz. This means that if I could have found an <em>even higher GCD frequency<\/em>, I would have used that instead.<\/p>\n<p>So the idea was that, for a 30 fps video, the engine would know to render a frame on every 200th timer interrupt. I eventually realized that servicing 6000 timer interrupts every second would incur a ridiculous amount of overhead. After that, my engine&#8217;s philosophy was to set a timer to fire for the next frame while beginning to process the current frame. I.e., when rendering a frame, set a timer to call back in 1\/30th of a second. That worked a lot better.<\/p>\n<p>As I was still keen on 8-bit paletted image codecs at the time (especially since they were simple and small for bootstrapping this project), I got to use output palette images directly thanks to the Dreamcast&#8217;s paletted textures. So that was exciting. The engine didn&#8217;t need to convert the paletted images to a different colorspace before rendering. However, I seem to recall that the Dreamcast&#8217;s PowerVR graphics hardware required that 8-bit textures be twiddled\/swizzled. Thus, it was still required to manipulate the 8-bit image before rendering.<\/p>\n<p>I made good progress on this player concept. However, a huge blocker for me was that I didn&#8217;t know how to make a proper user interface for the media player. Obviously, programming the Dreamcast occurred at a very low level (at least with the approach I was using), so there were no UI widgets easily available.<\/p>\n<p>This was circa 2003. I assumed there must have been some embedded UI widget libraries with amenable open source licenses that I could leverage. I remember searching and checking out a library named libSTK. I think STK stood for &#8220;set-top toolkit&#8221; and was positioned specifically for doing things like media player UIs on low-spec embedded computing devices. The domain hosting the project is no longer useful but <a href=\"https:\/\/github.com\/dvhart\/libstk\">this appears to be a backup of the core code<\/a>.<\/p>\n<p>It sounded promising, but the libSTK developers had a different definition of &#8220;low-spec embedded&#8221; device than I did. I seem to recall that they were targeting something along with likes of a Pentium III clocked at 800 MHz with 128 MB RAM. The Dreamcast, by contrast, has a 200 MHz SH-4 CPU and 16 MB RAM. LibSTK was also authored in C++ and leveraged <a href=\"http:\/\/www.boost.org\/\">the Boost library<\/a> (my first exposure to that code), and this all had the effect of making binaries quite large while I was trying to keep the player in lean C.<\/p>\n<p>Regrettably, I never made any serious progress on a proper user interface. I think that&#8217;s when the player effort ran out of steam.<\/p>\n<p><strong>The Code<\/strong><br \/>\nSo, that&#8217;s another project that I never got around to finishing or publishing. I was able to find the source code so I decided to <a href=\"https:\/\/github.com\/multimediamike\/dreamreel\">toss it up on github<\/a>, along with 2 old architecture outlines that I was able to dig up. It looks like I was starting small, just porting over a few of the demuxers and decoders that I knew well.<\/p>\n<p>I&#8217;m wondering if it would still be as straightforward to separate out such components now, more than 13 years later?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I know I&#8217;m not the only person to have the idea to port a media player to the Sega Dreamcast video game console. But I did make significant progress on an implementation. I&#8217;m a little surprised to realize that I haven&#8217;t written anything about it on this blog yet, given my propensity for publishing my [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[221],"tags":[],"class_list":["post-4520","post","type-post","status-publish","format-standard","hentry","category-sega-dreamcast"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/4520","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=4520"}],"version-history":[{"count":3,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/4520\/revisions"}],"predecessor-version":[{"id":4524,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/4520\/revisions\/4524"}],"wp:attachment":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/media?parent=4520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/categories?post=4520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/tags?post=4520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}