Can Of Code

Hello World in Spectrum assembly

I started this post originally in November 2013 but it ended up in the drafts pile. The idea then came back to me when I heard of the Spectrum ZA Vega IndieGoGo.

After a voyage into the roof I discovered a box of old electronics. Getting completely distracted from what I was supposed to be finding I took a peek inside the box to find a collection of Spectrum consoles and related accessories.

After digging around in the box I found this book:

Spectrum & Spectrum+ Assembly Language Course

Spectrum & Spectrum+ Assembly Language Course

 

I should explain that seeing such a book didn’t instil some sort of nostalgia. If i’m honest, it was before my time. The book did however spike my interest, perhaps I could see what life was like without the comfy luxuries of IDEs and managed languages.

 

Getting Started

It makes sense that the first step is to get the hardware setup. This gifted me with my first two issues:

– I have been reliable informed that none of the Spectrums in the roof actually work. Made in England!

– Even if they did work, I don’t have a TV that has a coax connector.

So my best bet was to try and find a Spectrum emulator. The search was both long and tiresome but I finally found an emulator that both worked and included a code editor.

Enter ZX Spin

ZX Spin

ZX Spin

You can get ZX Spin here. Unlike most emulators I found, this one works on Windows 8 and it runs straight out of its folder, no need to install anything.

 

Time for Hello World

So to print “Hello World” to the screen is a pretty simple command:

PRINT “Hello World”

The first gotcha here is that you would be inclined to type out “PRINT”  however the Spectrum keyboard worked in such a way that the “p” key would write “PRINT”

 

Spectrum Keyboard

Spectrum Keyboard

 

To type a string you need to surrond the string in quotation marks, nothing new there.  So to print out “Hello World” we would have to type:

P key  –  (Right Alt + P)  –   hello world – (Right Alt + P)

Hit enter and you should see:

 

hello-spectrum

 

Next time…

Next time I hope to create a reusable program using the tape emulation. I’m also intrigued by why they chose to use single key presses for commands? I assume it was a technical limitation?  Let me know in the comments if you have an insight.

Posted in Books, Software | Tagged , , , | 15 Comments

ASP Entity Framework Many to Many not working

i’ve been working on a simple book website using ASP MVC.  I have a Book class and an Author class. Each book can have one or more Authors and each Author can have one or more books.

Many to Many incorrect in the database

The first issue I came across was that the database was being created without a lookup table as should be the case with a many to many relationship. instead it added a bookId to the Author table. This would mean that duplicate author records would exist for authors with more than one book.

To fix this (thanks to @TheRealDuckboy) I just needed to add a List of books to the Author object! I only had a Author list in the Book object which confused Entity Framework.

Correct database layout, hurrah?

After solving my borked many to many relationship I stumbled across my next issue. It was time to list the authors of the Book object in my view. The Book’s Authors property was null. After checking the database I saw that the data was there and the relationship was valid.

The problem was with how I represented the collection in the Model.  I naively bashed out the following:

 

public List<Book> Authors { get; set;}

 

After some googling i saw that people were using a ICollection for their lists. Its always a good idea to use a generic interface so more fool me.

 

public ICollection<Book> Authors { get; set; }

 

So I ran “update-database” to do the code first migration and started running. The Authors property was still null!

What I then found was that I was missing the “virtual” keyword.

 

public virtual ICollection<Book> Authors { get; set; }

 

It worked! This Stackoverflow answer suggests that the collection can’t be lazy loaded (loaded when first accessed) unless its virtual.

Posted in Web | Tagged , , | Leave a comment

The History of Video Decoding on Android

In this post I aim to give a brief overview of the differences with video decoding on Android. This overview is mainly focused on whats available if MediaPlayer or VideoView doesn’t fit your requirements.

 

From the beginning (Android 1.0)

The first media framework on Android came from PacketVideo and was called OpenCore.

 

Donut time (Android 1.6)

Android-Donut

Image source: http://www.huntlogo.com/android-donut-logo/

 

A updated version of OpenCore was released with the catchy title of “OpenCore 2.0”.  The new version of OpenCore included support for the OpenMax IL API which enabled apps to make use of dedicated hardware encoder/decoders.

The inclusion of OpenMax support was a massive step in bringing high resolution video to mobile devices. A dedicated Hardware decoding/encoding chip would take the stress away from the CPU,  essential in the days when a device like the HTC Hero had a CPU speed of 528Mhz.

That is all I know of OpenCore and considering that it’s only available for 0.1% of the distribution it’s probably not worth implementing.

 

Eclair has Stagefright (Android 2.0 – 2.1)

Android-Eclair

Image source: http://www.huntlogo.com/android-eclair-logo/

 

The Stagefright library was introduced as a replacement for the previously mentioned OpenCore.

From the Java side you only get a handful of classes which should use hardware decoding when available. If you want to use media formats that are not support by Eclair you will need to go down the painful route of using private Android libraries.

When going down this route you will read of people warning you against it, the main reason being that there is no guarantee that the libraries will be the same on every device.

The OMXCodec would be your best bet at decoding via hardware. A good demo of this can be found here. What that demo doesn’t mention is how to include the private libraries and headers so you can actually use OMXCodec. One way would be to copy the libstagefright.so (among others) from an emulator into your project.

 

Gingerbread also has Stagefright (Android 2.3)

Android-Gingerbread

Image source: http://www.huntlogo.com/android-gingerbread-logo/

 

Not much relating to video was changed in Gingerbread. One point of interest is the addition of being able to use the Surface from native code via the NDK.

 

Honeycomb HLS (Android 3.0)

Android-Honeycomb-2

Image source: http://www.huntlogo.com/android-honeycomb-logo-2/

 

Although nobody uses Honeycomb it did bring along support for HTTP Live Streaming (HLS). However Android’s implementation is a little flaky especially when seeking.

 

Ice Cream Sandwich (Android 4.0)

Android-Ice-Cream-Sandwich

Image source: http://www.huntlogo.com/android-ice-cream-sandwich-logo/
 

Ice Cream Sandwich introduced OpenMax AL Support via the NDK. One significant downfall with Android’s implementation of OpenMax AL is that it only supports the decoding of “MPEG-2 transport stream via a buffer queue“. This rules out passing raw h264 NALUs or other media formats for that matter.

 

Jellybean brings MediaCodec (Android 4.1 – 4.3)

Android-Jelly-Bean-logo

 

 

Image source: http://www.huntlogo.com/android-jelly-bean-logo/
 

In jellybean the MediaCodec was introduced which provides a Java API for Encoding / Decoding using both software and hardware decoders.

Although the introduction of the MediaCodec gives the benefit of staying within the comfort of a Java environment. One frustration with MediaCodec is the poor documentation, especially if you want to use your own demuxer instead of the MediaExtractor.

A good example of the lack of documentation revolves around how codec private data should be handled. For example with a H264 Stream you should put the SPS in a ByteBuffer with the key “csd-0” and the PPS with a key of “csd-1”. The best approach i found was to study the MediaFormat that is created by the MediaExtractor and then emulate the results in your project.

A really helpful working example of the MediaCodec can be found here, its worth keeping the site in your favorites as they also has a few other useful related posts.

Update: Android 4.3 has a new “modular DRM framework” for handling streams that make use of DRM.

 

Finally

That’s my overview of video decoding on Android. You may notice i have missed a few of the smaller release mainly because nothing interesting had changed. I am relatively new to both Android and Video decoding so if i have missed something important or made any incorrect statements please let me know in the comments.

Posted in Android | Tagged , , , , | 4 Comments