<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>Dirac Video Codec</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body bgcolor="#ffffff"> <h1 align="center" color="#606080"> Dirac - A Video Codec </h1> <p align="center"> Created by the <a href="http://www.bbc.co.uk">British Broadcasting Corporation</a>. </p> <hr> <!-- Generated by Doxygen 1.5.4 --> <h1>arith_codec.h</h1><a href="a00158.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* ***** BEGIN LICENSE BLOCK *****</span> <a name="l00002"></a>00002 <span class="comment">*</span> <a name="l00003"></a>00003 <span class="comment">* $Id: arith_codec.h,v 1.40 2007/11/16 04:48:44 asuraparaju Exp $ $Name: Dirac_0_9_1 $</span> <a name="l00004"></a>00004 <span class="comment">*</span> <a name="l00005"></a>00005 <span class="comment">* Version: MPL 1.1/GPL 2.0/LGPL 2.1</span> <a name="l00006"></a>00006 <span class="comment">*</span> <a name="l00007"></a>00007 <span class="comment">* The contents of this file are subject to the Mozilla Public License</span> <a name="l00008"></a>00008 <span class="comment">* Version 1.1 (the "License"); you may not use this file except in compliance</span> <a name="l00009"></a>00009 <span class="comment">* with the License. You may obtain a copy of the License at</span> <a name="l00010"></a>00010 <span class="comment">* http://www.mozilla.org/MPL/</span> <a name="l00011"></a>00011 <span class="comment">*</span> <a name="l00012"></a>00012 <span class="comment">* Software distributed under the License is distributed on an "AS IS" basis,</span> <a name="l00013"></a>00013 <span class="comment">* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for</span> <a name="l00014"></a>00014 <span class="comment">* the specific language governing rights and limitations under the License.</span> <a name="l00015"></a>00015 <span class="comment">*</span> <a name="l00016"></a>00016 <span class="comment">* The Original Code is BBC Research and Development code.</span> <a name="l00017"></a>00017 <span class="comment">*</span> <a name="l00018"></a>00018 <span class="comment">* The Initial Developer of the Original Code is the British Broadcasting</span> <a name="l00019"></a>00019 <span class="comment">* Corporation.</span> <a name="l00020"></a>00020 <span class="comment">* Portions created by the Initial Developer are Copyright (C) 2004.</span> <a name="l00021"></a>00021 <span class="comment">* All Rights Reserved.</span> <a name="l00022"></a>00022 <span class="comment">*</span> <a name="l00023"></a>00023 <span class="comment">* Contributor(s): Richard Felton (Original Author),</span> <a name="l00024"></a>00024 <span class="comment"> Thomas Davies,</span> <a name="l00025"></a>00025 <span class="comment"> Scott R Ladd,</span> <a name="l00026"></a>00026 <span class="comment"> Peter Bleackley,</span> <a name="l00027"></a>00027 <span class="comment"> Steve Bearcroft,</span> <a name="l00028"></a>00028 <span class="comment"> Anuradha Suraparaju,</span> <a name="l00029"></a>00029 <span class="comment"> Tim Borer (major refactor February 2006)</span> <a name="l00030"></a>00030 <span class="comment"> Andrew Kennedy</span> <a name="l00031"></a>00031 <span class="comment">*</span> <a name="l00032"></a>00032 <span class="comment">* Alternatively, the contents of this file may be used under the terms of</span> <a name="l00033"></a>00033 <span class="comment">* the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser</span> <a name="l00034"></a>00034 <span class="comment">* Public License Version 2.1 (the "LGPL"), in which case the provisions of</span> <a name="l00035"></a>00035 <span class="comment">* the GPL or the LGPL are applicable instead of those above. If you wish to</span> <a name="l00036"></a>00036 <span class="comment">* allow use of your version of this file only under the terms of the either</span> <a name="l00037"></a>00037 <span class="comment">* the GPL or LGPL and not to allow others to use your version of this file</span> <a name="l00038"></a>00038 <span class="comment">* under the MPL, indicate your decision by deleting the provisions above</span> <a name="l00039"></a>00039 <span class="comment">* and replace them with the notice and other provisions required by the GPL</span> <a name="l00040"></a>00040 <span class="comment">* or LGPL. If you do not delete the provisions above, a recipient may use</span> <a name="l00041"></a>00041 <span class="comment">* your version of this file under the terms of any one of the MPL, the GPL</span> <a name="l00042"></a>00042 <span class="comment">* or the LGPL.</span> <a name="l00043"></a>00043 <span class="comment">* ***** END LICENSE BLOCK ***** */</span> <a name="l00044"></a>00044 <a name="l00045"></a>00045 <a name="l00046"></a>00046 <span class="preprocessor">#ifndef _ARITH_CODEC_H_</span> <a name="l00047"></a>00047 <span class="preprocessor"></span><span class="preprocessor">#define _ARITH_CODEC_H_</span> <a name="l00048"></a>00048 <span class="preprocessor"></span> <a name="l00057"></a>00057 <a name="l00058"></a>00058 <span class="preprocessor">#include <<a class="code" href="a00167.html">libdirac_common/common.h</a>></span> <a name="l00059"></a>00059 <span class="preprocessor">#include <<a class="code" href="a00164.html">libdirac_byteio/byteio.h</a>></span> <a name="l00060"></a>00060 <span class="preprocessor">#include <vector></span> <a name="l00061"></a>00061 <a name="l00062"></a>00062 <span class="keyword">namespace </span>dirac <a name="l00063"></a>00063 { <a name="l00064"></a>00064 <a name="l00065"></a><a class="code" href="a00029.html">00065</a> <span class="keyword">class </span><a class="code" href="a00029.html">Context</a> { <a name="l00066"></a>00066 <span class="keyword">public</span>: <a name="l00067"></a>00067 <a name="l00069"></a>00069 <a name="l00072"></a>00072 <span class="keyword">inline</span> <a class="code" href="a00029.html#7700773e0fc8a5cc6d52f762cf6f6bed" title="Default Constructor.">Context</a>(); <a name="l00073"></a>00073 <a name="l00074"></a>00074 <span class="comment">//Class is POD</span> <a name="l00075"></a>00075 <span class="comment">//Use built in copy constructor, assignment and destructor.</span> <a name="l00076"></a>00076 <a name="l00078"></a>00078 <a name="l00079"></a><a class="code" href="a00029.html#2e169fe721a59bddbd28a906b9dc17ea">00079</a> <span class="keyword">inline</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="a00029.html#2e169fe721a59bddbd28a906b9dc17ea" title="Returns estimate of probability of 0 (false) scaled to 2**16.">GetScaledProb0</a>( )<span class="keyword"> const</span>{ <span class="keywordflow">return</span> <a class="code" href="a00029.html#811817929c5649f4122217e7a439224e">m_prob0</a>;} <a name="l00080"></a>00080 <a name="l00082"></a><a class="code" href="a00029.html#4c7c472be34ced9c8fe69655c634bf0e">00082</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="a00029.html#4c7c472be34ced9c8fe69655c634bf0e" title="Updates context counts.">Update</a>( <span class="keywordtype">bool</span> symbol ) { <a name="l00083"></a>00083 <span class="keywordflow">if</span> (symbol) <a class="code" href="a00029.html#811817929c5649f4122217e7a439224e">m_prob0</a> -= <a class="code" href="a00029.html#890205dc2ebcec3471edfd3d946e49b2">lut</a>[<a class="code" href="a00029.html#811817929c5649f4122217e7a439224e">m_prob0</a>>>8]; <a name="l00084"></a>00084 <span class="keywordflow">else</span> <a class="code" href="a00029.html#811817929c5649f4122217e7a439224e">m_prob0</a> += <a class="code" href="a00029.html#890205dc2ebcec3471edfd3d946e49b2">lut</a>[255-(<a class="code" href="a00029.html#811817929c5649f4122217e7a439224e">m_prob0</a>>>8)]; <a name="l00085"></a>00085 } <a name="l00086"></a>00086 <a name="l00087"></a>00087 <span class="keyword">private</span>: <a name="l00088"></a>00088 <a name="l00089"></a><a class="code" href="a00029.html#811817929c5649f4122217e7a439224e">00089</a> <span class="keywordtype">int</span> <a class="code" href="a00029.html#811817929c5649f4122217e7a439224e">m_prob0</a>; <a name="l00090"></a><a class="code" href="a00029.html#890205dc2ebcec3471edfd3d946e49b2">00090</a> <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="a00029.html#890205dc2ebcec3471edfd3d946e49b2">lut</a>[256]; <span class="comment">//Probability update table</span> <a name="l00091"></a>00091 }; <a name="l00092"></a>00092 <a name="l00093"></a><a class="code" href="a00029.html#7700773e0fc8a5cc6d52f762cf6f6bed">00093</a> <a class="code" href="a00029.html#7700773e0fc8a5cc6d52f762cf6f6bed" title="Default Constructor.">Context::Context</a>(): m_prob0( 0x8000 ) {} <a name="l00094"></a>00094 <a name="l00095"></a><a class="code" href="a00003.html">00095</a> <span class="keyword">class </span><a class="code" href="a00003.html">ArithCodecBase</a> { <a name="l00096"></a>00096 <a name="l00097"></a>00097 <span class="keyword">public</span>: <a name="l00098"></a>00098 <a name="l00100"></a>00100 <a name="l00106"></a>00106 <a class="code" href="a00003.html">ArithCodecBase</a>(<a class="code" href="a00018.html" title="Class ByteIO - top-level class for reading/writing bytes to a stream.">ByteIO</a>* p_byteio, <span class="keywordtype">size_t</span> number_of_contexts); <a name="l00107"></a>00107 <a name="l00109"></a>00109 <a name="l00112"></a>00112 <span class="keyword">virtual</span> ~<a class="code" href="a00003.html">ArithCodecBase</a>(); <a name="l00113"></a>00113 <a name="l00114"></a>00114 <span class="keyword">protected</span>: <a name="l00115"></a>00115 <a name="l00116"></a>00116 <span class="comment">//core encode functions</span> <a name="l00118"></a>00118 <span class="comment"></span> <a name="l00120"></a>00120 <span class="keywordtype">void</span> InitEncoder(); <a name="l00121"></a>00121 <a name="l00123"></a>00123 <span class="keywordtype">void</span> EncodeSymbol(<span class="keyword">const</span> <span class="keywordtype">bool</span> symbol, <span class="keyword">const</span> <span class="keywordtype">int</span> context_num); <a name="l00124"></a>00124 <a name="l00125"></a>00125 <span class="keywordtype">void</span> EncodeUInt(<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> value, <span class="keyword">const</span> <span class="keywordtype">int</span> bin1, <span class="keyword">const</span> <span class="keywordtype">int</span> max_bin); <a name="l00126"></a>00126 <a name="l00127"></a>00127 <span class="keywordtype">void</span> EncodeSInt(<span class="keyword">const</span> <span class="keywordtype">int</span> value, <span class="keyword">const</span> <span class="keywordtype">int</span> bin1, <span class="keyword">const</span> <span class="keywordtype">int</span> max_bin); <a name="l00128"></a>00128 <a name="l00130"></a>00130 <span class="keywordtype">void</span> FlushEncoder(); <a name="l00131"></a>00131 <a name="l00132"></a>00132 <span class="keywordtype">int</span> ByteCount() <span class="keyword">const</span>; <a name="l00133"></a>00133 <a name="l00134"></a>00134 <span class="comment">// core decode functions</span> <a name="l00136"></a>00136 <span class="comment"></span> <a name="l00138"></a>00138 <span class="keywordtype">void</span> InitDecoder(<span class="keywordtype">int</span> num_bytes); <a name="l00139"></a>00139 <a name="l00141"></a>00141 <span class="keywordtype">bool</span> DecodeSymbol( <span class="keywordtype">int</span> context_num ); <a name="l00142"></a>00142 <a name="l00143"></a>00143 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> DecodeUInt(<span class="keyword">const</span> <span class="keywordtype">int</span> bin1, <span class="keyword">const</span> <span class="keywordtype">int</span> max_bin); <a name="l00144"></a>00144 <a name="l00145"></a>00145 <span class="keywordtype">int</span> DecodeSInt(<span class="keyword">const</span> <span class="keywordtype">int</span> bin1, <span class="keyword">const</span> <span class="keywordtype">int</span> max_bin); <a name="l00146"></a>00146 <a name="l00148"></a><a class="code" href="a00003.html#11e987e1a4372b9727dd38a8915561af">00148</a> std::vector<Context> m_context_list; <a name="l00149"></a>00149 <a name="l00150"></a>00150 <span class="keyword">private</span>: <a name="l00151"></a>00151 <a name="l00153"></a>00153 <a class="code" href="a00003.html">ArithCodecBase</a>(<span class="keyword">const</span> <a class="code" href="a00003.html">ArithCodecBase</a> & cpy); <a name="l00154"></a>00154 <a name="l00156"></a>00156 <a class="code" href="a00003.html">ArithCodecBase</a> & operator = (<span class="keyword">const</span> <a class="code" href="a00003.html">ArithCodecBase</a> & rhs); <a name="l00157"></a>00157 <a name="l00158"></a>00158 <a name="l00159"></a>00159 <span class="comment">// Decode functions</span> <a name="l00161"></a>00161 <span class="comment"></span> <a name="l00163"></a>00163 <span class="keywordtype">void</span> ReadAllData(<span class="keywordtype">int</span> num_bytes); <a name="l00164"></a>00164 <a name="l00166"></a>00166 <span class="keyword">inline</span> <span class="keywordtype">bool</span> InputBit(); <a name="l00167"></a>00167 <a name="l00168"></a>00168 <span class="comment">// Codec data</span> <a name="l00170"></a>00170 <span class="comment"></span> <a name="l00171"></a><a class="code" href="a00003.html#4a9ea66b50e954747218cf85a1325f6e">00171</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> m_scount; <a name="l00172"></a>00172 <a name="l00174"></a><a class="code" href="a00003.html#1996e5d84afcf97fd077e48fe2002283">00174</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> m_low_code; <a name="l00175"></a>00175 <a name="l00177"></a><a class="code" href="a00003.html#11259e2ec2728c7cdfb215f24b72b37b">00177</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> m_range; <a name="l00178"></a>00178 <a name="l00180"></a><a class="code" href="a00003.html#28a2490809e366c92144f75a48a703e3">00180</a> <a class="code" href="a00018.html" title="Class ByteIO - top-level class for reading/writing bytes to a stream.">ByteIO</a> *m_byteio; <a name="l00181"></a>00181 <a name="l00182"></a>00182 <span class="comment">// For encoder only</span> <a name="l00183"></a>00183 <a name="l00185"></a><a class="code" href="a00003.html#896fb231527c4094182c534a13de1297">00185</a> <span class="keywordtype">int</span> m_underflow; <a name="l00186"></a>00186 <a name="l00188"></a><a class="code" href="a00003.html#2b79911be35754ae75517acb1908337a">00188</a> <span class="keywordtype">char</span>* m_decode_data_ptr; <a name="l00189"></a>00189 <a name="l00191"></a><a class="code" href="a00003.html#09ee9ca6966c44973a864eb751dcfd71">00191</a> <span class="keywordtype">char</span>* m_data_ptr; <a name="l00192"></a>00192 <a name="l00194"></a><a class="code" href="a00003.html#36c3d594e47cd34db38762d1bba7ef53">00194</a> <span class="keywordtype">int</span> m_input_bits_left; <a name="l00195"></a>00195 <a name="l00197"></a><a class="code" href="a00003.html#c2c85e023cb7a01d94e46c26fb2e4d86">00197</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> m_code; <a name="l00198"></a>00198 <a name="l00199"></a>00199 }; <a name="l00200"></a>00200 <a name="l00201"></a>00201 <a name="l00202"></a><a class="code" href="a00003.html#fd3402ab56731f6b9281d0efef72e4fd">00202</a> <span class="keyword">inline</span> <span class="keywordtype">bool</span> ArithCodecBase::DecodeSymbol( <span class="keywordtype">int</span> context_num ) <a name="l00203"></a>00203 { <a name="l00204"></a>00204 <a name="l00205"></a>00205 <span class="comment">// Determine the next symbol value by placing code within</span> <a name="l00206"></a>00206 <span class="comment">// the [low,high] interval.</span> <a name="l00207"></a>00207 <a name="l00208"></a>00208 <span class="comment">// Fetch the statistical context to be used</span> <a name="l00209"></a>00209 <a class="code" href="a00029.html">Context</a>& ctx = <a class="code" href="a00003.html#11e987e1a4372b9727dd38a8915561af" title="List of contexts.">m_context_list</a>[context_num]; <a name="l00210"></a>00210 <a name="l00211"></a>00211 <span class="comment">// Decode as per updated specification</span> <a name="l00212"></a>00212 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> count = <a class="code" href="a00003.html#c2c85e023cb7a01d94e46c26fb2e4d86" title="The present input code.">m_code</a> - <a class="code" href="a00003.html#1996e5d84afcf97fd077e48fe2002283" title="Start of the current code range.">m_low_code</a> ; <a name="l00213"></a>00213 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> range_x_prob = ( <a class="code" href="a00003.html#11259e2ec2728c7cdfb215f24b72b37b" title="Length of the current code range.">m_range</a>* ctx.<a class="code" href="a00029.html#2e169fe721a59bddbd28a906b9dc17ea" title="Returns estimate of probability of 0 (false) scaled to 2**16.">GetScaledProb0</a>())>>16; <a name="l00214"></a>00214 <span class="keyword">const</span> <span class="keywordtype">bool</span> symbol = ( count >= range_x_prob ); <a name="l00215"></a>00215 <a name="l00216"></a>00216 <span class="comment">// Rescale the interval</span> <a name="l00217"></a>00217 <span class="keywordflow">if</span>( symbol ) <span class="comment">//symbol is 1</span> <a name="l00218"></a>00218 { <a name="l00219"></a>00219 m_low_code += range_x_prob; <a name="l00220"></a>00220 <a class="code" href="a00003.html#11259e2ec2728c7cdfb215f24b72b37b" title="Length of the current code range.">m_range</a> -= range_x_prob; <a name="l00221"></a>00221 } <a name="l00222"></a>00222 <span class="keywordflow">else</span> <span class="comment">//symbol is 0, so m_low_code unchanged</span> <a name="l00223"></a>00223 { <a name="l00224"></a>00224 <a class="code" href="a00003.html#11259e2ec2728c7cdfb215f24b72b37b" title="Length of the current code range.">m_range</a> = range_x_prob; <a name="l00225"></a>00225 } <a name="l00226"></a>00226 <a name="l00227"></a>00227 <span class="comment">// Update the statistical context</span> <a name="l00228"></a>00228 ctx.<a class="code" href="a00029.html#4c7c472be34ced9c8fe69655c634bf0e" title="Updates context counts.">Update</a>( symbol ); <a name="l00229"></a>00229 <a name="l00230"></a>00230 <span class="keywordflow">while</span> ( <a class="code" href="a00003.html#11259e2ec2728c7cdfb215f24b72b37b" title="Length of the current code range.">m_range</a><=0x4000 ) <a name="l00231"></a>00231 { <a name="l00232"></a>00232 <span class="keywordflow">if</span>( ( (m_low_code+<a class="code" href="a00003.html#11259e2ec2728c7cdfb215f24b72b37b" title="Length of the current code range.">m_range</a>-1)^m_low_code)>=0x8000 ) <a name="l00233"></a>00233 { <a name="l00234"></a>00234 <span class="comment">// Straddle condition</span> <a name="l00235"></a>00235 <span class="comment">// We must have an underflow situation with</span> <a name="l00236"></a>00236 <span class="comment">// low = 0x01... and high = 0x10...</span> <a name="l00237"></a>00237 <span class="comment">// Flip 2nd bit prior to rescaling</span> <a name="l00238"></a>00238 <a class="code" href="a00003.html#c2c85e023cb7a01d94e46c26fb2e4d86" title="The present input code.">m_code</a> ^= 0x4000; <a name="l00239"></a>00239 m_low_code ^= 0x4000; <a name="l00240"></a>00240 } <a name="l00241"></a>00241 <a name="l00242"></a>00242 <span class="comment">// Double low and range, throw away top bit of low</span> <a name="l00243"></a>00243 m_low_code <<= 1; <a name="l00244"></a>00244 <a class="code" href="a00003.html#11259e2ec2728c7cdfb215f24b72b37b" title="Length of the current code range.">m_range</a> <<= 1; <a name="l00245"></a>00245 m_low_code &= 0xFFFF; <a name="l00246"></a>00246 <a name="l00247"></a>00247 <span class="comment">// Shift in another bit of code</span> <a name="l00248"></a>00248 <a class="code" href="a00003.html#c2c85e023cb7a01d94e46c26fb2e4d86" title="The present input code.">m_code</a> <<= 1; <a name="l00249"></a>00249 <a class="code" href="a00003.html#c2c85e023cb7a01d94e46c26fb2e4d86" title="The present input code.">m_code</a> += <a class="code" href="a00003.html#1395c87448e4fb04e6879c8e30ebf5bd" title="Read in a bit of data.">InputBit</a>(); <a name="l00250"></a>00250 <a class="code" href="a00003.html#c2c85e023cb7a01d94e46c26fb2e4d86" title="The present input code.">m_code</a> &= 0xFFFF; <a name="l00251"></a>00251 <a name="l00252"></a>00252 } <a name="l00253"></a>00253 <a name="l00254"></a>00254 <span class="keywordflow">return</span> symbol; <a name="l00255"></a>00255 } <a name="l00256"></a>00256 <a name="l00257"></a><a class="code" href="a00003.html#cbfef773024344089487f3c8eb3b81e5">00257</a> <span class="keyword">inline</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="a00003.html#cbfef773024344089487f3c8eb3b81e5">ArithCodecBase::DecodeUInt</a>(<span class="keyword">const</span> <span class="keywordtype">int</span> bin1, <span class="keyword">const</span> <span class="keywordtype">int</span> max_bin) { <a name="l00258"></a>00258 <span class="keyword">const</span> <span class="keywordtype">int</span> info_ctx = (max_bin+1); <a name="l00259"></a>00259 <span class="keywordtype">int</span> bin = bin1; <a name="l00260"></a>00260 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> value = 1; <a name="l00261"></a>00261 <span class="keywordflow">while</span> (!<a class="code" href="a00003.html#fd3402ab56731f6b9281d0efef72e4fd" title="Decodes a symbol given a context number.">DecodeSymbol</a>(bin)) { <a name="l00262"></a>00262 value <<= 1; <a name="l00263"></a>00263 <span class="keywordflow">if</span> (<a class="code" href="a00003.html#fd3402ab56731f6b9281d0efef72e4fd" title="Decodes a symbol given a context number.">DecodeSymbol</a>(info_ctx)) value+=1; <a name="l00264"></a>00264 <span class="keywordflow">if</span> (bin<max_bin) bin+=1; <a name="l00265"></a>00265 } <a name="l00266"></a>00266 value -= 1; <a name="l00267"></a>00267 <span class="keywordflow">return</span> value; <a name="l00268"></a>00268 } <a name="l00269"></a>00269 <a name="l00270"></a><a class="code" href="a00003.html#8927f9ed4aa66a3bf1a5a54475f9766c">00270</a> <span class="keyword">inline</span> <span class="keywordtype">int</span> <a class="code" href="a00003.html#8927f9ed4aa66a3bf1a5a54475f9766c">ArithCodecBase::DecodeSInt</a>(<span class="keyword">const</span> <span class="keywordtype">int</span> bin1, <span class="keyword">const</span> <span class="keywordtype">int</span> max_bin) { <a name="l00271"></a>00271 <span class="keywordtype">int</span> value = 0; <a name="l00272"></a>00272 <span class="keyword">const</span> <span class="keywordtype">int</span> magnitude = <a class="code" href="a00003.html#cbfef773024344089487f3c8eb3b81e5">DecodeUInt</a>(bin1, max_bin); <a name="l00273"></a>00273 <span class="keywordflow">if</span> (magnitude!=0) { <a name="l00274"></a>00274 <span class="keywordflow">if</span> (<a class="code" href="a00003.html#fd3402ab56731f6b9281d0efef72e4fd" title="Decodes a symbol given a context number.">DecodeSymbol</a>(max_bin+2)) value=-magnitude; <a name="l00275"></a>00275 <span class="keywordflow">else</span> value=magnitude; <a name="l00276"></a>00276 } <a name="l00277"></a>00277 <span class="keywordflow">return</span> value; <a name="l00278"></a>00278 } <a name="l00279"></a>00279 <a name="l00280"></a><a class="code" href="a00003.html#c388b85aff0398ba10d656089249a272">00280</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="a00003.html#c388b85aff0398ba10d656089249a272" title="encodes a symbol and writes to output">ArithCodecBase::EncodeSymbol</a>(<span class="keyword">const</span> <span class="keywordtype">bool</span> symbol, <span class="keyword">const</span> <span class="keywordtype">int</span> context_num) <a name="l00281"></a>00281 { <a name="l00282"></a>00282 <a name="l00283"></a>00283 <span class="comment">// Adjust high and low (rescale interval) based on the symbol we are encoding</span> <a name="l00284"></a>00284 <a name="l00285"></a>00285 <a class="code" href="a00029.html">Context</a>& ctx = <a class="code" href="a00003.html#11e987e1a4372b9727dd38a8915561af" title="List of contexts.">m_context_list</a>[context_num]; <a name="l00286"></a>00286 <a name="l00287"></a>00287 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> range_x_prob = ( <a class="code" href="a00003.html#11259e2ec2728c7cdfb215f24b72b37b" title="Length of the current code range.">m_range</a>* ctx.<a class="code" href="a00029.html#2e169fe721a59bddbd28a906b9dc17ea" title="Returns estimate of probability of 0 (false) scaled to 2**16.">GetScaledProb0</a>())>>16; <a name="l00288"></a>00288 <a name="l00289"></a>00289 <span class="keywordflow">if</span> ( symbol ) <span class="comment">//symbol is 1</span> <a name="l00290"></a>00290 { <a name="l00291"></a>00291 <a class="code" href="a00003.html#1996e5d84afcf97fd077e48fe2002283" title="Start of the current code range.">m_low_code</a> += range_x_prob; <a name="l00292"></a>00292 <a class="code" href="a00003.html#11259e2ec2728c7cdfb215f24b72b37b" title="Length of the current code range.">m_range</a> -= range_x_prob; <a name="l00293"></a>00293 } <a name="l00294"></a>00294 <span class="keywordflow">else</span> <span class="comment">// symbol is 0, so m_low_code unchanged</span> <a name="l00295"></a>00295 { <a name="l00296"></a>00296 <a class="code" href="a00003.html#11259e2ec2728c7cdfb215f24b72b37b" title="Length of the current code range.">m_range</a> = range_x_prob; <a name="l00297"></a>00297 } <a name="l00298"></a>00298 <a name="l00299"></a>00299 <span class="comment">// Update the statistical context</span> <a name="l00300"></a>00300 ctx.<a class="code" href="a00029.html#4c7c472be34ced9c8fe69655c634bf0e" title="Updates context counts.">Update</a>( symbol ); <a name="l00301"></a>00301 <a name="l00302"></a>00302 <span class="keywordflow">while</span> ( <a class="code" href="a00003.html#11259e2ec2728c7cdfb215f24b72b37b" title="Length of the current code range.">m_range</a> <= 0x4000 ) <a name="l00303"></a>00303 { <a name="l00304"></a>00304 <span class="keywordflow">if</span> ( ( (<a class="code" href="a00003.html#1996e5d84afcf97fd077e48fe2002283" title="Start of the current code range.">m_low_code</a>+<a class="code" href="a00003.html#11259e2ec2728c7cdfb215f24b72b37b" title="Length of the current code range.">m_range</a>-1)^<a class="code" href="a00003.html#1996e5d84afcf97fd077e48fe2002283" title="Start of the current code range.">m_low_code</a>)>=0x8000 ) <a name="l00305"></a>00305 { <a name="l00306"></a>00306 <span class="comment">// Straddle condition</span> <a name="l00307"></a>00307 <span class="comment">// We must have an underflow situation with</span> <a name="l00308"></a>00308 <span class="comment">// low = 0x01... and high = 0x10...</span> <a name="l00309"></a>00309 <a name="l00310"></a>00310 <a class="code" href="a00003.html#1996e5d84afcf97fd077e48fe2002283" title="Start of the current code range.">m_low_code</a> ^= 0x4000; <a name="l00311"></a>00311 <a class="code" href="a00003.html#896fb231527c4094182c534a13de1297" title="Number of underflow bits.">m_underflow</a>++; <a name="l00312"></a>00312 <a name="l00313"></a>00313 } <a name="l00314"></a>00314 <span class="keywordflow">else</span> <a name="l00315"></a>00315 { <a name="l00316"></a>00316 <span class="comment">// Bits agree - output them</span> <a name="l00317"></a>00317 <a class="code" href="a00003.html#28a2490809e366c92144f75a48a703e3" title="Input/output stream of Dirac-format bytes.">m_byteio</a>-><a class="code" href="a00018.html#0ac5a730dab2ed232eff97b49055151e" title="Outputs a bit.">WriteBit</a>( <a class="code" href="a00003.html#1996e5d84afcf97fd077e48fe2002283" title="Start of the current code range.">m_low_code</a> & 0x8000); <a name="l00318"></a>00318 <span class="keywordflow">for</span> (; <a class="code" href="a00003.html#896fb231527c4094182c534a13de1297" title="Number of underflow bits.">m_underflow</a> > 0; <a class="code" href="a00003.html#896fb231527c4094182c534a13de1297" title="Number of underflow bits.">m_underflow</a>-- ) <a name="l00319"></a>00319 <a class="code" href="a00003.html#28a2490809e366c92144f75a48a703e3" title="Input/output stream of Dirac-format bytes.">m_byteio</a>-><a class="code" href="a00018.html#0ac5a730dab2ed232eff97b49055151e" title="Outputs a bit.">WriteBit</a>(~<a class="code" href="a00003.html#1996e5d84afcf97fd077e48fe2002283" title="Start of the current code range.">m_low_code</a> & 0x8000); <a name="l00320"></a>00320 } <a name="l00321"></a>00321 <a name="l00322"></a>00322 <span class="comment">// Double low value and range</span> <a name="l00323"></a>00323 <a class="code" href="a00003.html#1996e5d84afcf97fd077e48fe2002283" title="Start of the current code range.">m_low_code</a> <<= 1; <a name="l00324"></a>00324 <a class="code" href="a00003.html#11259e2ec2728c7cdfb215f24b72b37b" title="Length of the current code range.">m_range</a> <<= 1; <a name="l00325"></a>00325 <a name="l00326"></a>00326 <span class="comment">// keep low to 16 bits - throw out top bit</span> <a name="l00327"></a>00327 <a class="code" href="a00003.html#1996e5d84afcf97fd077e48fe2002283" title="Start of the current code range.">m_low_code</a> &= 0xFFFF; <a name="l00328"></a>00328 <a name="l00329"></a>00329 } <a name="l00330"></a>00330 } <a name="l00331"></a>00331 <a name="l00332"></a><a class="code" href="a00003.html#f355dc235e6bf8524631060e898e4f70">00332</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="a00003.html#f355dc235e6bf8524631060e898e4f70">ArithCodecBase::EncodeUInt</a>(<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> the_int, <a name="l00333"></a>00333 <span class="keyword">const</span> <span class="keywordtype">int</span> bin1, <span class="keyword">const</span> <span class="keywordtype">int</span> max_bin) { <a name="l00334"></a>00334 <span class="keyword">const</span> <span class="keywordtype">int</span> value = (the_int+1); <a name="l00335"></a>00335 <span class="keyword">const</span> <span class="keywordtype">int</span> info_ctx = (max_bin+1); <a name="l00336"></a>00336 <span class="keywordtype">int</span> bin = bin1; <a name="l00337"></a>00337 <span class="keywordtype">int</span> top_bit = 1; <a name="l00338"></a>00338 { <a name="l00339"></a>00339 <span class="keywordtype">int</span> max_value = 1; <a name="l00340"></a>00340 <span class="keywordflow">while</span> (value>max_value) { <a name="l00341"></a>00341 top_bit <<= 1; <a name="l00342"></a>00342 max_value <<= 1; <a name="l00343"></a>00343 max_value += 1; <a name="l00344"></a>00344 } <a name="l00345"></a>00345 } <a name="l00346"></a>00346 <span class="keywordtype">bool</span> stop = (top_bit==1); <a name="l00347"></a>00347 <a class="code" href="a00003.html#c388b85aff0398ba10d656089249a272" title="encodes a symbol and writes to output">EncodeSymbol</a>(stop, bin); <a name="l00348"></a>00348 <span class="keywordflow">while</span> (!stop) { <a name="l00349"></a>00349 top_bit >>= 1; <a name="l00350"></a>00350 <a class="code" href="a00003.html#c388b85aff0398ba10d656089249a272" title="encodes a symbol and writes to output">EncodeSymbol</a>( (value&top_bit), info_ctx); <a name="l00351"></a>00351 <span class="keywordflow">if</span> ( bin < max_bin) bin+=1; <a name="l00352"></a>00352 stop = (top_bit==1); <a name="l00353"></a>00353 <a class="code" href="a00003.html#c388b85aff0398ba10d656089249a272" title="encodes a symbol and writes to output">EncodeSymbol</a>(stop, bin); <a name="l00354"></a>00354 } <a name="l00355"></a>00355 } <a name="l00356"></a>00356 <a name="l00357"></a><a class="code" href="a00003.html#8a58ca61428bc0f21ac05482c08b268f">00357</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="a00003.html#8a58ca61428bc0f21ac05482c08b268f">ArithCodecBase::EncodeSInt</a>(<span class="keyword">const</span> <span class="keywordtype">int</span> value, <a name="l00358"></a>00358 <span class="keyword">const</span> <span class="keywordtype">int</span> bin1, <span class="keyword">const</span> <span class="keywordtype">int</span> max_bin) { <a name="l00359"></a>00359 <a class="code" href="a00003.html#f355dc235e6bf8524631060e898e4f70">EncodeUInt</a>(std::abs(value), bin1, max_bin); <a name="l00360"></a>00360 <span class="keywordflow">if</span> (value != 0) { <a name="l00361"></a>00361 <a class="code" href="a00003.html#c388b85aff0398ba10d656089249a272" title="encodes a symbol and writes to output">EncodeSymbol</a>( (value < 0), max_bin+2 ); <a name="l00362"></a>00362 } <a name="l00363"></a>00363 } <a name="l00364"></a>00364 <a name="l00365"></a>00365 <a name="l00367"></a>00367 <a name="l00373"></a>00373 <span class="keyword">template</span><<span class="keyword">class</span> T> <span class="comment">//T is container/array type</span> <a name="l00374"></a><a class="code" href="a00002.html">00374</a> <span class="keyword">class </span><a class="code" href="a00002.html" title="Abstract binary arithmetic coding class.">ArithCodec</a> <a name="l00375"></a>00375 : <span class="keyword">public</span> <a class="code" href="a00003.html">ArithCodecBase</a> <a name="l00376"></a>00376 { <a name="l00377"></a>00377 <span class="keyword">public</span>: <a name="l00378"></a>00378 <a name="l00380"></a>00380 <a name="l00386"></a>00386 <a class="code" href="a00002.html" title="Abstract binary arithmetic coding class.">ArithCodec</a>(<a class="code" href="a00018.html" title="Class ByteIO - top-level class for reading/writing bytes to a stream.">ByteIO</a>* p_byteio, <span class="keywordtype">size_t</span> number_of_contexts); <a name="l00387"></a>00387 <a name="l00388"></a>00388 <a name="l00390"></a>00390 <a name="l00393"></a><a class="code" href="a00002.html#7554b390827496f6de00fdd6a8ede9d3">00393</a> <span class="keyword">virtual</span> ~<a class="code" href="a00002.html" title="Abstract binary arithmetic coding class.">ArithCodec</a>() {} <a name="l00394"></a>00394 <a name="l00396"></a>00396 <a name="l00404"></a>00404 <span class="keywordtype">int</span> Compress(T & in_data); <a name="l00405"></a>00405 <a name="l00407"></a>00407 <a name="l00415"></a>00415 <span class="keywordtype">void</span> Decompress(T & out_data, <span class="keyword">const</span> <span class="keywordtype">int</span> num_bytes); <a name="l00416"></a>00416 <a name="l00417"></a>00417 <span class="keyword">protected</span>: <a name="l00418"></a>00418 <a name="l00419"></a>00419 <span class="comment">//virtual encode-only functions</span> <a name="l00421"></a>00421 <span class="comment"></span> <a name="l00423"></a>00423 <span class="keyword">virtual</span> <span class="keywordtype">void</span> DoWorkCode(T & in_data) = 0; <a name="l00424"></a>00424 <a name="l00427"></a>00427 <span class="keyword">virtual</span> <span class="keywordtype">void</span> DoWorkDecode(T & out_data)=0; <a name="l00428"></a>00428 }; <a name="l00429"></a>00429 <a name="l00430"></a>00430 <span class="comment">//Implementation - core functions</span> <a name="l00432"></a>00432 <span class="comment"></span> <a name="l00433"></a>00433 <span class="keyword">template</span><<span class="keyword">class</span> T> <a name="l00434"></a><a class="code" href="a00002.html#feff75556eeb4759ece937e440f0b4b0">00434</a> <a class="code" href="a00002.html" title="Abstract binary arithmetic coding class.">ArithCodec<T>::ArithCodec</a>(<a class="code" href="a00018.html" title="Class ByteIO - top-level class for reading/writing bytes to a stream.">ByteIO</a>* p_byteio, <span class="keywordtype">size_t</span> number_of_contexts): <a name="l00435"></a>00435 <a class="code" href="a00003.html">ArithCodecBase</a>(p_byteio, number_of_contexts) {} <a name="l00436"></a>00436 <a name="l00437"></a>00437 <a name="l00438"></a>00438 <a name="l00439"></a>00439 <span class="keyword">template</span><<span class="keyword">class</span> T> <a name="l00440"></a><a class="code" href="a00002.html#13dca796e9759ef713c4d9efcd14ba19">00440</a> <span class="keywordtype">int</span> <a class="code" href="a00002.html" title="Abstract binary arithmetic coding class.">ArithCodec<T>::Compress</a>(T &in_data) <a name="l00441"></a>00441 { <a name="l00442"></a>00442 <a class="code" href="a00003.html#13417bf951850754890a945d1fff5901" title="Initialises the Encoder.">InitEncoder</a>(); <a name="l00443"></a>00443 <a class="code" href="a00002.html#494cce54ef4b1e29dbfa501172a4c5c5" title="Does the work of actually coding the data.">DoWorkCode</a>(in_data); <a name="l00444"></a>00444 <a class="code" href="a00003.html#dd48e1c11a630d1465cba01e5e7524a6" title="flushes the output of the encoder.">FlushEncoder</a>(); <a name="l00445"></a>00445 <span class="keywordflow">return</span> <a class="code" href="a00003.html#612441c07cdee30c7246539f7fe3199e">ByteCount</a>(); <a name="l00446"></a>00446 } <a name="l00447"></a>00447 <a name="l00448"></a>00448 <span class="keyword">template</span><<span class="keyword">class</span> T> <a name="l00449"></a><a class="code" href="a00002.html#c3b0d97af3420190de64db61d064697f">00449</a> <span class="keywordtype">void</span> <a class="code" href="a00002.html" title="Abstract binary arithmetic coding class.">ArithCodec<T>::Decompress</a>( T &out_data, <span class="keyword">const</span> <span class="keywordtype">int</span> num_bytes ) <a name="l00450"></a>00450 { <a name="l00451"></a>00451 <a class="code" href="a00003.html#c45185be0b16e6e105708e1c790c23d3" title="Initialise the Decoder.">InitDecoder</a>(num_bytes); <a name="l00452"></a>00452 <a class="code" href="a00002.html#a2391df876c8b5aa1007830371f551f7" title="virtual decode-only functions ////////////////////////////">DoWorkDecode</a>( out_data ); <a name="l00453"></a>00453 } <a name="l00454"></a>00454 <a name="l00455"></a><a class="code" href="a00003.html#1395c87448e4fb04e6879c8e30ebf5bd">00455</a> <span class="keyword">inline</span> <span class="keywordtype">bool</span> <a class="code" href="a00003.html#1395c87448e4fb04e6879c8e30ebf5bd" title="Read in a bit of data.">ArithCodecBase::InputBit</a>() <a name="l00456"></a>00456 { <a name="l00457"></a>00457 <span class="keywordflow">if</span> (<a class="code" href="a00003.html#36c3d594e47cd34db38762d1bba7ef53" title="The index of the bit of the byte being read.">m_input_bits_left</a> == 0) <a name="l00458"></a>00458 { <a name="l00459"></a>00459 <a class="code" href="a00003.html#09ee9ca6966c44973a864eb751dcfd71" title="A point to the byte currently being read.">m_data_ptr</a>++; <a name="l00460"></a>00460 <a class="code" href="a00003.html#36c3d594e47cd34db38762d1bba7ef53" title="The index of the bit of the byte being read.">m_input_bits_left</a> = 8; <a name="l00461"></a>00461 } <a name="l00462"></a>00462 <a class="code" href="a00003.html#36c3d594e47cd34db38762d1bba7ef53" title="The index of the bit of the byte being read.">m_input_bits_left</a>--; <a name="l00463"></a>00463 <span class="comment">// MSB to LSB</span> <a name="l00464"></a>00464 <span class="keywordflow">return</span> bool( ( (*<a class="code" href="a00003.html#09ee9ca6966c44973a864eb751dcfd71" title="A point to the byte currently being read.">m_data_ptr</a>) >> <a class="code" href="a00003.html#36c3d594e47cd34db38762d1bba7ef53" title="The index of the bit of the byte being read.">m_input_bits_left</a> ) & 1 ); <a name="l00465"></a>00465 } <a name="l00466"></a>00466 <a name="l00467"></a>00467 }<span class="comment">// namespace dirac</span> <a name="l00468"></a>00468 <span class="preprocessor">#endif</span> <a name="l00469"></a>00469 <span class="preprocessor"></span> </pre></div><hr> <p align="center"><small> © 2004 British Broadcasting Corporation. Dirac code licensed under the <a href="http://www.mozilla.org/MPL/">Mozilla Public License (MPL) Version 1.1</a>.<br> HTML documentation generated by Dimitri van Heesch's excellent <a href="http://www.doxygen.org">Doxygen</a> tool. </small></p> </body> </html>