<?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>mFabrik - mobile sites, apps, HTML5 and CMS software development &#187; video</title>
	<atom:link href="http://blog.mfabrik.com/tag/video/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mfabrik.com</link>
	<description>Freedom delivered.</description>
	<lastBuildDate>Wed, 03 Aug 2011 09:47:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>BitReader &#8211; Python module for reading bits from bytes</title>
		<link>http://blog.mfabrik.com/2010/09/08/bitreader-python-module-for-reading-bits-from-bytes/</link>
		<comments>http://blog.mfabrik.com/2010/09/08/bitreader-python-module-for-reading-bits-from-bytes/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 15:12:25 +0000</pubDate>
		<dc:creator>Jussi Toivola</dc:creator>
				<category><![CDATA[development tools]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[technology]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[byte]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[dvb]]></category>
		<category><![CDATA[egp]]></category>
		<category><![CDATA[mpeg]]></category>
		<category><![CDATA[mpeg ts]]></category>
		<category><![CDATA[video]]></category>
		<category><![CDATA[word]]></category>

		<guid isPermaLink="false">http://blog.mfabrik.com/?p=773</guid>
		<description><![CDATA[I worked on a project that involved working with MPEG Transport Stream and Digi-TV(DVB). EPGReader to be exact. The MPEG TS is a binary format, where multiple fields can be defined within a single byte. I could not use Python&#8217;s struct module because it only works with bytes or larger and the fields I had [...]]]></description>
			<content:encoded><![CDATA[<p>I worked on a project that involved working with M<a href="http://en.wikipedia.org/wiki/MPEG_transport_stream">PEG Transport Stream</a> and Digi-TV(DVB). <a href="https://bitbucket.org/mfabrik/epgreader/wiki/Home">EPGReader</a> to be exact.</p>
<p>The MPEG TS is a binary format, where multiple fields can be defined within a single byte. I could not use Python&#8217;s struct module because it only works with bytes or larger and the fields I had were just a couple of bits. First I started with regular bitshifts and bitmasks, but I soon realised it was very error prone task for me. It was very easy to make mistakes and the code was not very readable either.</p>
<p>For example, first 3 bytes of MPEG TS header contains a SYNC_BYTE, which is 1 byte in size and always has the value 0&#215;47. This byte is used to detect packets from the stream. Each packet is 188 bytes long. The next bit is &#8220;transport error indicator&#8221;, which is set by receiver hardware to flag errors in demulation( analog signal to bits ), next bit is &#8220;payload unit start indicator&#8221; indicating that the current packet starts a new payload of data, then comes &#8220;transport priority&#8221; bit and finally &#8220;packet id&#8221;. Normally I&#8217;d write code something like following:</p>
<pre>data      = read(3)          # Get 3 bytes of data
sync_byte = data &gt;&gt; 16       # Get bits 24-16
tei       = data &gt;&gt; 15 &amp; 0x1 # 16. bit
payl_start= data &gt;&gt; 14 &amp; 0x1 # 15. bit
tp        = data &gt;&gt; 13 &amp; 0x1 # 14. bit
pid       = data &amp; 0x1FFF    # Get last 13 bits</pre>
<p>On top of that I needed to store the values in dictionary. As you can see this is not very readable nor convenient. So I figured there must be something easier.</p>
<h2>Meet BitReader</h2>
<pre>spec = (
    # Name of the data to read
    'sync_byte',
    # How many bits to read( 8 bits = 1 byte )
    8,
    'tei',
    1,
    'payl_start',
    1,
    'tp',
    1,
    'pid',
    13
)

reader = BitReader(spec)
data   = reader.read(read(3))
assert data.sync_byte == 0x47</pre>
<p>And if and when one needed to add one more byte and couple of variables, that&#8217;s when the code starts to break with bitshifts. Any change to the original data size requires you to change the bitshifts accordingly. Also adding new values to the middle requires changes to bitshifts, in case you missed it in the spec the first time etc.</p>
<pre>data      = read(4)          # Get 4 bytes of data
sync_byte = data &gt;&gt; 24       # Get bits 32-24
tei       = data &gt;&gt; 23 &amp; 0x1 # 24. bit
etc... not very interested on getting this right, but you'll get the idea</pre>
<p>When using BitReader, I just give the variable name and how many bits it takes. Simple as that. No need to touch the other variables.</p>
<pre>spec = (
    # Name of the data to read
    'sync_byte',
    # How many bits to read
    8,
    'tei',
    1,
    'payl_start',
    1,
    'tp',
    1,
    'pid',
    13,
    'scrambling',
    2,
    'has_adapt',
    1
    'has_payload',
    1,
    'continuity',
    4
)

reader = BitReader( spec )
data   = reader.read(read(4))</pre>
<p>And it doesn&#8217;t matter if the new values are added to the beginning, middle or at the end.</p>
<h2>About performance &amp; syntax</h2>
<p>BitReader is a bit slower than using bitshifts, but it was still easily fast enough for the task I worked on. And if compiled using Cython the performance nearly doubles without any code change.</p>
<p>Is it faster?<br />
- Performance, no, but you are faster. Have a cup of C if you want speed.<br />
Is it more readable?<br />
- Yes.<br />
Makes life easier?<br />
- You bet!</p>
<p>Somebody might look at the specification syntax and quickly note that I could have used dictionary instead. Unfortunately it is not possible because the order is needed and dictionary does not preserve it.</p>
<p>And what about using 2-tuples ( variable, bits )? Is it more readable and less error prone? Not sure, maybe, but I thought I&#8217;ll save myself from typing parenthesis <img src='http://blog.mfabrik.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The specification syntax was inspired by <a href="http://blog.mfabrik.com/2010/09/02/domgen-creating-html-with-javascript-without-dom-api/">domgen</a>&#8230; or the other way around. Can&#8217;t remember which came first.</p>
<p>You can also convert the data back into binary format. The read returns a BitData object, which implements &#8216;dump()&#8217; method, which returns an array.array(&#8216;B&#8217;) containing the bytes. You can change the attributes of the BitData and then dump the data back into array and easily write it to a file using array.tofile(f) or send it to network.</p>
<p>A Javascript port might be interesting for web apps&#8230; Especially mobile apps, which often have slow connections. And a hand made C module would probably be at least as fast as the bitshifts on Python.</p>
<h2>Project location</h2>
<p>Get the code <a href="https://bitbucket.org/jtoivola/bitreader">from bitbucket.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mfabrik.com/2010/09/08/bitreader-python-module-for-reading-bits-from-bytes/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>How to encode h264 video files for Nokia Series 60 standalone playback</title>
		<link>http://blog.mfabrik.com/2008/09/23/how-to-encode-h264-video-files-for-nokia-series-60-standalone-playback/</link>
		<comments>http://blog.mfabrik.com/2008/09/23/how-to-encode-h264-video-files-for-nokia-series-60-standalone-playback/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 23:27:32 +0000</pubDate>
		<dc:creator>Mikko Ohtamaa</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[series 60]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[aac]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[dvd]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[ffmpeg]]></category>
		<category><![CDATA[h264]]></category>
		<category><![CDATA[libfaac]]></category>
		<category><![CDATA[libx264]]></category>
		<category><![CDATA[movie]]></category>
		<category><![CDATA[n95]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[video]]></category>
		<category><![CDATA[x264]]></category>

		<guid isPermaLink="false">http://blog.redinnovation.com/?p=118</guid>
		<description><![CDATA[Bored with Spiderman 3 which came with your Nokia N95 8 GB? This guide shortly tells how to get movies into your N95 on Ubuntu Linux using ffmpeg video encoder. The aim is to encode video suitable for playback from Nokia N-series (N95, N78, others) mobile phone memory card. We use h264 + AAC codecs [...]]]></description>
			<content:encoded><![CDATA[<p>Bored with Spiderman 3 which came with your Nokia N95 8 GB? This guide shortly tells how to get movies into your N95 on Ubuntu Linux using <a href="http://ffmpeg.mplayerhq.hu/">ffmpeg</a> video encoder. The aim is to encode video suitable for playback from Nokia N-series (N95, N78, others) mobile phone memory card. We use <a href="http://en.wikipedia.org/wiki/H.264">h264</a> + AAC codecs which provides the best quality/compression rate for Nokia phones currently.</p>
<p>Ubuntu does not distribute proprietary codes. First thing you need to do is to <a href="https://wiki.ubuntu.com/ffmpeg">rebuild ffmpeg</a>.  Since Ubuntu 8.04 Hardy Heron ships with ffmpeg from 2007, which is aeons old in video codec years, <strong>you need to build libx264 and ffmpeg from SVN sources</strong>. <a href="http://ubuntuforums.org/showthread.php?t=786095&amp;highlight=libx264">Here are detailed, valid, instructions</a>. Note that FFMPEG trunk is not currently stable (September 2008), so <a href="http://ffmpeg.mplayerhq.hu/">you need to use revision 15261</a> which needs <a href="http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/2008-September/053527.html">this little patch</a>. Indeed, this is a very difficult month to start your career in the dark world of video encoders.</p>
<p>To make it legal and support open source codec development,  please <a href="http://blog.canonical.com/?p=37">pay for your codecs</a>.</p>
<p>Then we use this guide by <a href="http://rob.opendot.cl/index.php/useful-stuff/ffmpeg-x264-encoding-guide/">Robert Swain</a>. We have a tiny sub 2,4&#8243; screen, we do not care about the quality and do one pass encoding. By empirical research, I have found that the following MPEG-4 profile parameters are compatible with N95 8 GB and provide the optimal result. You can vary video and audio bitrate depending on your taste.</p>
<p>Here is a script which recursivelu encodes all detected video files suitable for mobile format:</p>
<pre>#!/bin/sh
#
# Optimal movie encoding for Nokia N-series mobile phones
#
# Copyright 2008 Red Innovation Ltd.
#
# Say hi if you find this useful.
# We do some professional mobile video publishing, so if you
# need a helping hand please call us.
#
# Usage: Run encode.sh in any folder and all video files are recursively converted to mobile phone suitable format
#
# Note: We expect all the source material be in 16:9 aspect ration
#
# Also see http://www.nseries.com/index.html#l=support,search,faq,general,video%20encoding,53848
#

VIDEO_BITRATE=300k

AUDIO_BITRATE=72k

# Assume locally build ffmpeg + x264 in /usr/local/bin
# http://ubuntuforums.org/showthread.php?t=786095
export LD_LIBRARY_PATH=/usr/local/lib

# Search all source AVI, MPG and WMV video files
# Place all encoded files to the same folder with the source, with added .mp4 extension
find . -iname "*.avi" -or -iname "*.wmv" -or -iname "*.mpg" | while read src ; do
        srcfile=`basename "$src"`
	srcfolder=`dirname "$src"`
	dstfile="$srcfolder"/"$srcfile".mp4

	# The magical string!
	# Size and cropping is for 16:9 source material, so that 320:240 display will have black bars.
	# Fex pixels off... note that h264 sizes must be multiplies of 16, use 256x144 for streaming
	# N95 RealMedia player does not seem to respect MPEG-4 embedded aspect ration info.
	/usr/local/bin/ffmpeg -y -i "$srcfile" -acodec libfaac -ab $AUDIO_BITRATE -s 320x176 -aspect 16:9 -vcodec libx264 -b $VIDEO_BITRATE -qcomp 0.6 -qmin 16 -qmax 51 -qdiff 4 -flags +loop -cmp +chroma -subq 7 -refs 6 -g 250 -keyint_min 25 -rc_eq 'blurCplx^(1-qComp)' -sc_threshold 40 -me_range 12 -i_qfactor 0.71 -directpred 3 "$dstfile"

done</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.mfabrik.com/2008/09/23/how-to-encode-h264-video-files-for-nokia-series-60-standalone-playback/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

