Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial import of csdirac

svn path=/trunk/csdirac/; revision=130937
  • Loading branch information...
commit 745ca298b5529bb29e33dfec50f6f0300a39edc1 0 parents
Olivier Dufour authored
Showing with 15,420 additions and 0 deletions.
  1. +82 −0 DiracStreamSource.cs
  2. +59 −0 DiractTest/ArithTest.cs
  3. +134 −0 DiractTest/DecoderTest.cs
  4. +61 −0 DiractTest/DiractTest.csproj
  5. +89 −0 DiractTest/GeneralTest.cs
  6. +36 −0 DiractTest/Properties/AssemblyInfo.cs
  7. +114 −0 DiractTest/UnpackTest.cs
  8. +73 −0 DiractTest/Wavelet2dTest.cs
  9. BIN  DiractTest/bin/Debug/DiractTest.dll
  10. BIN  DiractTest/bin/Debug/DiractTest.pdb
  11. BIN  DiractTest/bin/Debug/csdirac.dll
  12. BIN  DiractTest/bin/Debug/csdirac.pdb
  13. BIN  DiractTest/bin/Debug/nunit.framework.dll
  14. +5,622 −0 DiractTest/bin/Debug/nunit.framework.xml
  15. BIN  DiractTest/bin/Release/DiractTest.dll
  16. BIN  DiractTest/bin/Release/DiractTest.pdb
  17. BIN  DiractTest/bin/Release/System.Core.dll
  18. BIN  DiractTest/bin/Release/System.Net.dll
  19. BIN  DiractTest/bin/Release/System.Runtime.Serialization.dll
  20. BIN  DiractTest/bin/Release/System.Windows.dll
  21. BIN  DiractTest/bin/Release/csdirac.dll
  22. BIN  DiractTest/bin/Release/csdirac.pdb
  23. BIN  DiractTest/bin/Release/nunit.framework.dll
  24. +5,622 −0 DiractTest/bin/Release/nunit.framework.xml
  25. +9 −0 DiractTest/obj/Debug/DiractTest.csproj.FileListAbsolute.txt
  26. BIN  DiractTest/obj/Debug/DiractTest.dll
  27. BIN  DiractTest/obj/Debug/DiractTest.pdb
  28. BIN  DiractTest/obj/Debug/ResolveAssemblyReference.cache
  29. +13 −0 DiractTest/obj/Release/DiractTest.csproj.FileListAbsolute.txt
  30. BIN  DiractTest/obj/Release/DiractTest.dll
  31. BIN  DiractTest/obj/Release/DiractTest.pdb
  32. BIN  DiractTest/obj/Release/ResolveAssemblyReference.cache
  33. +19 −0 Math/Dimension.cs
  34. +20 −0 Math/Point.cs
  35. +36 −0 Properties/AssemblyInfo.cs
  36. +39 −0 TestResult.xml
  37. BIN  bin/Debug/csdirac.dll
  38. BIN  bin/Debug/csdirac.pdb
  39. BIN  bin/Release/System.Core.dll
  40. BIN  bin/Release/System.Net.dll
  41. BIN  bin/Release/System.Runtime.Serialization.dll
  42. BIN  bin/Release/System.Windows.dll
  43. BIN  bin/Release/csdirac.dll
  44. BIN  bin/Release/csdirac.pdb
  45. +76 −0 csdirac.csproj
  46. +1 −0  csdirac.csproj.user
  47. +26 −0 csdirac.sln
  48. BIN  csdirac.suo
  49. +260 −0 csdirac/Arithmetic.cs
  50. +197 −0 csdirac/Block.cs
  51. +61 −0 csdirac/Buffer.cs
  52. +64 −0 csdirac/Cache.cs
  53. +75 −0 csdirac/ColourSpace.cs
  54. +114 −0 csdirac/Decoder.cs
  55. +22 −0 csdirac/Global.cs
  56. +514 −0 csdirac/Motion.cs
  57. +121 −0 csdirac/Parameters.cs
  58. +369 −0 csdirac/Picture.cs
  59. +137 −0 csdirac/Queue.cs
  60. +11 −0 csdirac/Slice.cs
  61. +51 −0 csdirac/Stream.cs
  62. +208 −0 csdirac/SubBand.cs
  63. +234 −0 csdirac/Unpack.cs
  64. +58 −0 csdirac/Util.cs
  65. +70 −0 csdirac/Vector.cs
  66. +280 −0 csdirac/VideoFormat.cs
  67. +297 −0 csdirac/Wavelet.cs
  68. +120 −0 csiracTest.VisualState.xml
  69. +7 −0 csiracTest.nunit
  70. +5 −0 obj/Debug/csdirac.csproj.FileListAbsolute.txt
  71. BIN  obj/Debug/csdirac.dll
  72. BIN  obj/Debug/csdirac.pdb
  73. BIN  obj/Release/Refactor/csdirac.dll
  74. BIN  obj/Release/ResolveAssemblyReference.cache
  75. +14 −0 obj/Release/csdirac.csproj.FileListAbsolute.txt
  76. BIN  obj/Release/csdirac.dll
  77. BIN  obj/Release/csdirac.pdb
82 DiracStreamSource.cs
@@ -0,0 +1,82 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Windows.Media;
+using System.IO;
+using System.Globalization;
+
+
+namespace csdirac
+{
+ public class DiracStreamSource : MediaStreamSource
+ {
+ public DiracStreamSource(Stream videoStream)
+ {
+ this.videoStream = videoStream;
+ }
+
+ protected override void CloseMedia()
+ {
+ throw new NotImplementedException();
+ }
+
+ protected override void GetDiagnosticAsync(MediaStreamSourceDiagnosticKind diagnosticKind)
+ {
+ throw new NotImplementedException();
+ }
+
+ protected override void GetSampleAsync(MediaStreamType mediaStreamType)
+ {
+ throw new NotImplementedException();
+ }
+ private MediaStreamDescription streamDescription;
+ private Stream videoStream;
+
+ protected override void OpenMediaAsync()
+ {
+ // Initialize data structures to pass to the Media pipeline via the MediaStreamSource
+ Dictionary<MediaSourceAttributesKeys, string> mediaSourceAttributes = new Dictionary<MediaSourceAttributesKeys, string>();
+ Dictionary<MediaStreamAttributeKeys, string> mediaStreamAttributes = new Dictionary<MediaStreamAttributeKeys, string>();
+ List<MediaStreamDescription> mediaStreamDescriptions = new List<MediaStreamDescription>();
+
+ // Pull in the entire Audio stream.
+ byte[] videoData = new byte[this.videoStream.Length];
+ if (videoData.Length != this.videoStream.Read(videoData, 0, videoData.Length))
+ {
+ throw new IOException("Could not read in the VideoStream");
+ }
+ //TODO parse until first frame
+ //todo find what is the offset of first frame and put it in push len param
+ org.diracvideo.Jirac.Decoder dec = new org.diracvideo.Jirac.Decoder();
+ dec.Push(videoData, 0, videoData.Length);
+ dec.Decode();
+
+ mediaStreamAttributes[MediaStreamAttributeKeys.CodecPrivateData] = dec.format.ToString();
+ this.streamDescription = new MediaStreamDescription(MediaStreamType.Video, mediaStreamAttributes);
+
+ mediaStreamDescriptions.Add(streamDescription);
+
+ // Setting a 0 duration to avoid the math to calcualte the Mp3 file length in minutes and seconds.
+ // This was done just to simplify this initial version of the code for other people reading it.
+ mediaSourceAttributes[MediaSourceAttributesKeys.Duration] = TimeSpan.FromMinutes(5).Ticks.ToString(CultureInfo.InvariantCulture);
+ mediaSourceAttributes[MediaSourceAttributesKeys.CanSeek] = "0";
+
+ // Report that the DiracMediaStreamSource has finished initializing its internal state and can now
+ // pass in Dirac Samples.
+ this.ReportOpenMediaCompleted(mediaSourceAttributes, mediaStreamDescriptions);
+
+ //this.currentFrameStartPosition = result;
+ //this.currentFrameSize = mpegLayer3Frame.FrameSize;
+ }
+
+ protected override void SeekAsync(long seekToTime)
+ {
+ throw new NotImplementedException();
+ }
+
+ protected override void SwitchMediaStreamAsync(MediaStreamDescription mediaStreamDescription)
+ {
+ throw new NotImplementedException();
+ }
+ }
+}
59 DiractTest/ArithTest.cs
@@ -0,0 +1,59 @@
+using org.diracvideo.Jirac;
+using System;
+using System.IO;
+using NUnit.Framework;
+using System.Collections.Generic;
+namespace org.diracvideo.Jirac.Test
+{
+ /** A test for the arithmetic decoder.
+ *
+ * Decodes a arithmetic encoded file from arith_file */
+
+ [TestFixture]
+ public class ArithTest {
+ [Test]
+ public void main() {
+ //TODO dialogue to ask string
+ string[] a = new string[0];
+ FileStream input = TryOpen(a);
+ byte[] d = new byte[input.Length];
+ input.Read(d, 0, (int)input.Length);
+ input.Close();
+ input.Dispose();
+ Arithmetic ar = new Arithmetic(d);
+ TestArithmetic(ar);
+ }
+
+ private FileStream TryOpen(String[] a)
+ {
+ for(int i = 0; i < a.Length; i++) {
+ if (File.Exists(a[i]))
+ {
+ return File.Open(a[i], FileMode.Open);
+ }
+ }
+ List<string> files = new List<string>();
+
+ foreach(string f in Directory.GetFiles(".")) {
+ if(f.Length == f.LastIndexOf(".sundae") + 7 && File.Exists(f))
+ return new FileStream(f, FileMode.Open);
+ }
+ Console.WriteLine("No arith file was found");
+ Environment.Exit(0);
+ return null;
+ }
+
+ private void TestArithmetic(Arithmetic a) {
+ byte[] d = new byte[a.BytesLeft()];
+ for(int i = 0; i < d.Length; i++) {
+ byte c = 0;
+ for(int j = 0; j < 8; j++) {
+ c = (byte)((c << 1) | a.DecodeBit(j));
+ }
+ d[i] = c;
+ }
+ String s = System.Text.Encoding.Default.GetString(d);
+ Console.WriteLine(s);
+ }
+ }
+}
134 DiractTest/DecoderTest.cs
@@ -0,0 +1,134 @@
+using org.diracvideo.Jirac;
+using NUnit.Framework;
+using System.IO;
+using System;
+using System.Drawing;
+using System.Windows.Forms;
+using System.Threading;
+
+namespace org.diracvideo.Jirac.Test
+{
+ [TestFixture]
+ public class DecoderTest {
+ [Test]
+ public void main() {
+ //TODO dialogue to ask string
+ string[] a = new string[0];
+
+ Decoder dec = new Decoder();
+ int ev = 0;
+ FileStream input = null;
+ Form win;
+ try {
+ input = tryOpen(a);
+ byte[] packet;
+ while(dec.format == null) {
+ packet = readPacket(input);
+ dec.Push(packet, 0, packet.Length);
+ }
+ ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object o) { dec.Decode(); }));
+
+ win = createWindow(dec);
+ while(input.Length > 0 && !dec.Done()) {
+ packet = readPacket(input);
+ dec.Push(packet, 0, packet.Length);
+ }
+ dec.status = Decoder.Status.DONE;
+ input.Close();
+ win.Visible = false;
+ win.Dispose();
+ } catch(Exception e) {
+ Console.WriteLine(e);
+ ev = 1;
+ } finally {
+ Environment.Exit(ev);
+ }
+ }
+
+ private byte[] readPacket(FileStream input) {
+ if(true) {
+ int read = (int)System.Math.Min(input.Length, 100);
+ byte[] packet = new byte[read];
+ input.Read(packet, 0, read);
+ return packet;
+ } else {
+ byte[] header = new byte[13];
+ input.Read(header, 0, 13);
+ Unpack u = new Unpack(header);
+ if(u.DecodeLit32() != 0x42424344) {
+ throw new IOException("Cannot parse dirac stream");
+ }
+ if(u.Bits(8) == 0x10) {
+ return header;
+ }
+ int size = u.DecodeLit32();
+ byte[] packet = new byte[size];
+ Array.Copy(header, 0, packet, 0, 13);
+ input.Read(packet, 13, size - 13);
+ return packet;
+ }
+ }
+
+
+ private FileStream tryOpen(string[] a) {
+ foreach (string f in a)
+ {
+ if (File.Exists(f))
+ {
+ try
+ {
+ return new FileStream(f, FileMode.Open);
+ }
+ catch (IOException e)
+ {
+ Console.WriteLine(e);
+ }
+ }
+ }
+
+ foreach (string f in Directory.GetFiles("."))
+ {
+ if (f.Length == f.LastIndexOf(".drc") + 4 && File.Exists(f))
+ {
+ try
+ {
+ return new FileStream(f, FileMode.Open);
+ }
+ catch (IOException e)
+ {
+ Console.WriteLine(e);
+ }
+ }
+ }
+ Console.WriteLine("No dirac file was found");
+ Environment.Exit(0);
+ return null;
+ }
+
+ private Form createWindow(Decoder dec)
+ {
+ Form fr = new Form();
+ fr.FormClosed += new FormClosedEventHandler(delegate(object sender, FormClosedEventArgs args) { Environment.Exit(0); });
+ PictureBox b = new PictureBox();
+ b.Size = new Size(dec.format.width, dec.format.height);
+ b.Paint += new PaintEventHandler(delegate(object sender, PaintEventArgs args) {
+ //Need b.Invoke or not?
+ int wait = (1000 * dec.format.frame_rate_denominator) / dec.format.frame_rate_numerator;
+
+ Picture pic = dec.Pull();
+ while (pic.status != Decoder.Status.DONE)
+ {
+ if (pic != null &&
+ pic.error == null)
+ {
+ b.Image = pic.GetImage();
+ }
+ Thread.Sleep(wait);
+ }
+ });
+ fr.Controls.Add(b);
+ fr.Show();
+ return fr;
+ }
+ }
+}
61 DiractTest/DiractTest.csproj
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>9.0.21022</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{0D7DF30A-8A9B-4559-9A9F-D34DB6E8FBF2}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>DiractTest</RootNamespace>
+ <AssemblyName>DiractTest</AssemblyName>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="nunit.framework, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
+ <Reference Include="System" />
+ <Reference Include="System.Drawing" />
+ <Reference Include="System.Windows.Forms" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="ArithTest.cs" />
+ <Compile Include="DecoderTest.cs" />
+ <Compile Include="GeneralTest.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="UnpackTest.cs" />
+ <Compile Include="Wavelet2dTest.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\csdirac.csproj">
+ <Project>{9BE09FC4-05EF-450C-9852-708EB91DE326}</Project>
+ <Name>csdirac</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
89 DiractTest/GeneralTest.cs
@@ -0,0 +1,89 @@
+using org.diracvideo.Jirac;
+using NUnit.Framework;
+using System;
+using org.diracvideo.Math;
+namespace org.diracvideo.Jirac.Test
+{
+ [TestFixture]
+ public class GeneralTest {
+
+ [Test]
+ private void testColourSpace() {
+ ColourSpace col = new ColourSpace(0,null);
+ Console.WriteLine(col);
+ }
+ [Test]
+ public void testShift()
+ {
+ int i = 16;
+ Console.WriteLine("%d %d %d\n", i >> 0, i >> 1, i >> 2);
+ }
+ [Test]
+ public void testShort()
+ {
+ //OD: removed
+ /*int s = (short)(0x8000);
+ Console.WriteLine("%d %d %d\n", s, s >> 2, s >> 2);
+ */
+ int i = 0x8000;
+ Console.WriteLine("%d %d %d\n", i, i >> 2, i >> 2);
+ }
+ [Test]
+ public void testLevel()
+ {
+ for(int n = 1; n < 8; n++) {
+ Console.WriteLine("Testing with TransformDepth = %d\n", n);
+ for(int i = 0; i < 3*n+1; i++) {
+ Console.WriteLine("Number: %d\tLevel: %d\tOrient: %d\n",
+ i, (i-1)/3, (i-1) % 3 + 1);
+ }
+ }
+ }
+ [Test]
+ public void testDivision()
+ {
+ for(int i = 0; i < 1000; i++) {
+ for(int j = 1; j < 1000; j++) {
+ int d = i/j;
+ if(d*j > i) {
+ Console.WriteLine("Divison error");
+ } else if(d*j != i) {
+ Console.WriteLine("Inexact division");
+ }
+ }
+ }
+ }
+ [Test]
+ public void testBlockDimensions()
+ {
+ Dimension frame = new Dimension(320,240);
+ for(int numY = 1; numY < 10; numY ++) {
+ for(int numX = 1; numX < 10; numX++) {
+ Console.WriteLine("numX: %d\tnumY: %d\n", numX, numY);
+ Dimension block = new Dimension(frame.Width / numX,
+ frame.Height / numY);
+ for(int i = 0; i < numY; i++)
+ for(int j = 0; j < numX; j++) {
+ int testStart = (block.Width*j) +
+ (frame.Width*block.Height*i);
+ int testEnd = testStart + block.Width +
+ (frame.Width*(block.Height-1));
+ int specX = (frame.Width * j)/numX;
+ int specY = (frame.Height *i)/numY;
+ int specStart = (frame.Width*specY) + specX;
+ int specEndX = (frame.Width * (j+1))/numX;
+ int specEndY = (frame.Height *(i+1))/numY;
+ int specEnd = (frame.Width*(specEndY - 1)) + specEndX;
+ if(specEnd != testEnd ||
+ specStart != testStart) {
+ Console.WriteLine("Spec:\t\tTest\n%d\t\t%d\n",
+ specEnd, testEnd);
+ Console.WriteLine("%d\t\t%d\n", specStart,
+ testStart);
+ }
+ }
+ }
+ }
+ }
+ }
+}
36 DiractTest/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// Les informations générales relatives à un assembly dépendent de
+// l'ensemble d'attributs suivant. Changez les valeurs de ces attributs pour modifier les informations
+// associées à un assembly.
+[assembly: AssemblyTitle("DiractTest")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Windows-Trust")]
+[assembly: AssemblyProduct("DiractTest")]
+[assembly: AssemblyCopyright("Copyright © Windows-Trust 2009")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// L'affectation de la valeur false à ComVisible rend les types invisibles dans cet assembly
+// aux composants COM. Si vous devez accéder à un type dans cet assembly à partir de
+// COM, affectez la valeur true à l'attribut ComVisible sur ce type.
+[assembly: ComVisible(false)]
+
+// Le GUID suivant est pour l'ID de la typelib si ce projet est exposé à COM
+[assembly: Guid("c6bf7c35-a5c6-49f3-8cd6-d56332672ae1")]
+
+// Les informations de version pour un assembly se composent des quatre valeurs suivantes :
+//
+// Version principale
+// Version secondaire
+// Numéro de build
+// Révision
+//
+// Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
+// en utilisant '*', comme indiqué ci-dessous :
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
114 DiractTest/UnpackTest.cs
@@ -0,0 +1,114 @@
+using org.diracvideo.Jirac;
+using NUnit.Framework;
+using System;
+namespace org.diracvideo.Jirac.Test
+{
+ public static class Utils
+ {
+ public static byte[] ToBytes(string s)
+ {
+ return System.Text.Encoding.Default.GetBytes(s);
+ }
+ }
+
+ [TestFixture]
+ public class UnpackTest {
+
+ [Test]
+ private void bitsLoopTest()
+ {
+ for (int i = 0; i < 5000; i++)
+ {
+ bitsReadTest();
+ skipTest();
+ }
+ }
+
+ private byte[] ToBytes(string s)
+ {
+ return System.Text.Encoding.Default.GetBytes(s);
+ }
+
+ private void skipTest() {
+ /* There used to be an extensive test here.
+ It failed, continued to fail, and then failed even more.
+ Apparantly an Unpack object can be inconsistent
+ after skipping. I have absolutely no clue why.
+ Especially as it all /seems/ to work so smoothly.
+ Anyone that is interested can try to fix it. */
+ Unpack u,o;
+ string s = String.Format("Hello World! \n%s\n%s\n%s",
+ "How are you today? I'm fine,",
+ "thank you for asking. It is",
+ "such lovely weather today");
+ u = new Unpack(Utils.ToBytes(s));
+ Random r = new Random();
+ while(u.BitsLeft() > 160) {
+ u.Bits(r.Next(31));
+ }
+ int i = r.Next(u.BitsLeft());
+ o = u.Clone();
+ /*
+ o.skip(i);
+ for(; i > 31; i -= 31) {
+ u.bits(31);
+ }
+ u.bits(i);
+ */
+ if(u.Equals(o)) {
+ while(u.BitsLeft() > 8) {
+ i = r.Next(System.Math.Min(u.BitsLeft(), 31));
+ if(u.Bits(i) != o.Bits(i)) {
+ throw new Exception("Skip Error (Inconsistency)");
+ }
+ }
+ } else {
+ throw new Exception("Skip Error (Unequality)");
+ }
+ }
+
+ [Test]
+ private void decodeTest() {
+ byte[] r = { (byte)0x96, (byte)0x11, (byte)0xA5, (byte)0x7F};
+ Unpack u = new Unpack(r);
+ for(int i = 0; i < 6; i++) {
+ int v = u.DecodeUint();
+ // Console.WriteLine(v);
+ if(i != v) {
+ throw new Exception("Error in decodeUint()");
+ }
+ }
+ }
+
+ [Test]
+ private void bitsTest() {
+ string s = "BBCD is the code for Dirac bitstreams\n" +
+ "This string should be just a little bit longer\n" ;
+ Unpack u = new Unpack(Utils.ToBytes(s));
+ char[] r = new char[s.Length];
+ char[] o = new char[s.Length];
+ for(int i = 0; u.BitsLeft() > 8; i++) {
+ r[i] = Convert.ToChar((byte)u.Bits(8));
+ o[i] = Convert.ToChar((byte)s[9*i]);
+ u.Skip(37);
+ u.Skip(27);
+ }
+ if(new String(o).CompareTo(new String(r)) != 0) {
+ throw new Exception("Bits error");
+ }
+ }
+
+ private void bitsReadTest() {
+ Unpack u = new Unpack(Utils.ToBytes("hallo sanne je bent mooi en lief"));
+ Random r = new Random();
+ int t = 0;
+ for(int c = 0; u.BitsLeft() > 32; c += t) {
+ if(u.BitsRead() != c) {
+ throw new Exception("bitsRead() Error");
+ }
+ t = r.Next(System.Math.Min(u.BitsLeft(),32));
+ u.Bits(t);
+ }
+ }
+ }
+}
73 DiractTest/Wavelet2dTest.cs
@@ -0,0 +1,73 @@
+using org.diracvideo.Jirac;
+using System;
+using NUnit.Framework;
+using System.Text;
+namespace org.diracvideo.Jirac.Test
+{
+ [TestFixture]
+ public class Wavelet2dTest {
+ private Wavelet wav;
+
+ [SetUp]
+ public void main() {
+ wav = new Wavelet();
+ }
+
+ [Test]
+ public void synthesizeTest()
+ {
+ short[] d = new short[16];
+ d[8] = 9;
+ d[12] = 5;
+ d[14] = 2;
+ d[15] = 1;
+ wav.Synthesize(d,4,0,d.Length);
+ wav.Synthesize(d,2,0,d.Length);
+ wav.Synthesize(d,1,0,d.Length);
+ Console.WriteLine(d);
+ }
+
+ [Test]
+ public void interLeaveTest()
+ {
+ short[] ll = new short[36];
+ short[] lh = new short[36];
+ short[] hl = new short[36];
+ short[] hh = new short[36];
+ fill(ll,1);
+ fill(lh,2);
+ fill(hl,3);
+ fill(hh,4);
+ short[] frame = wav.Interleave(ll,lh,hl,hh,6);
+ // Console.WriteLine(printable(frame,12));
+ }
+
+ [Test]
+ public void twoDimensionTest()
+ {
+ short[] ll = new short[36];
+ short[] other = new short[36];
+ fill(ll,1);
+ short[] frame = wav.Interleave(ll, other, other, other, 6);
+ wav.Inverse(frame, 12, 1);
+ Console.WriteLine(printable(frame,12));
+ }
+
+ private void fill(short[] arr, int v) {
+ short s = (short)v;
+ for(int i = 0; i < arr.Length; i++) {
+ arr[i] = s;
+ }
+ }
+
+ private String printable(short[] arr, int w) {
+ StringBuilder sb = new StringBuilder();
+ for(int i = 0; i < arr.Length; i++) {
+ sb.Append(String.Format("%c%d", (((i % w) == 0) ? '\n' : ' '),
+ arr[i]));
+ }
+ return sb.ToString();
+ }
+
+ }
+}
BIN  DiractTest/bin/Debug/DiractTest.dll
Binary file not shown
BIN  DiractTest/bin/Debug/DiractTest.pdb
Binary file not shown
BIN  DiractTest/bin/Debug/csdirac.dll
Binary file not shown
BIN  DiractTest/bin/Debug/csdirac.pdb
Binary file not shown
BIN  DiractTest/bin/Debug/nunit.framework.dll
Binary file not shown
5,622 DiractTest/bin/Debug/nunit.framework.xml
5,622 additions, 0 deletions not shown
BIN  DiractTest/bin/Release/DiractTest.dll
Binary file not shown
BIN  DiractTest/bin/Release/DiractTest.pdb
Binary file not shown
BIN  DiractTest/bin/Release/System.Core.dll
Binary file not shown
BIN  DiractTest/bin/Release/System.Net.dll
Binary file not shown
BIN  DiractTest/bin/Release/System.Runtime.Serialization.dll
Binary file not shown
BIN  DiractTest/bin/Release/System.Windows.dll
Binary file not shown
BIN  DiractTest/bin/Release/csdirac.dll
Binary file not shown
BIN  DiractTest/bin/Release/csdirac.pdb
Binary file not shown
BIN  DiractTest/bin/Release/nunit.framework.dll
Binary file not shown
5,622 DiractTest/bin/Release/nunit.framework.xml
5,622 additions, 0 deletions not shown
9 DiractTest/obj/Debug/DiractTest.csproj.FileListAbsolute.txt
@@ -0,0 +1,9 @@
+D:\src\csdirac\csirac\DiractTest\bin\Debug\DiractTest.dll
+D:\src\csdirac\csirac\DiractTest\bin\Debug\DiractTest.pdb
+D:\src\csdirac\csirac\DiractTest\bin\Debug\csdirac.dll
+D:\src\csdirac\csirac\DiractTest\bin\Debug\nunit.framework.dll
+D:\src\csdirac\csirac\DiractTest\bin\Debug\csdirac.pdb
+D:\src\csdirac\csirac\DiractTest\bin\Debug\nunit.framework.xml
+D:\src\csdirac\csirac\DiractTest\obj\Debug\ResolveAssemblyReference.cache
+D:\src\csdirac\csirac\DiractTest\obj\Debug\DiractTest.dll
+D:\src\csdirac\csirac\DiractTest\obj\Debug\DiractTest.pdb
BIN  DiractTest/obj/Debug/DiractTest.dll
Binary file not shown
BIN  DiractTest/obj/Debug/DiractTest.pdb
Binary file not shown
BIN  DiractTest/obj/Debug/ResolveAssemblyReference.cache
Binary file not shown
13 DiractTest/obj/Release/DiractTest.csproj.FileListAbsolute.txt
@@ -0,0 +1,13 @@
+D:\src\csdirac\csirac\DiractTest\obj\Release\ResolveAssemblyReference.cache
+D:\src\csdirac\csirac\DiractTest\bin\Release\DiractTest.dll
+D:\src\csdirac\csirac\DiractTest\bin\Release\DiractTest.pdb
+D:\src\csdirac\csirac\DiractTest\bin\Release\csdirac.dll
+D:\src\csdirac\csirac\DiractTest\bin\Release\nunit.framework.dll
+D:\src\csdirac\csirac\DiractTest\bin\Release\csdirac.pdb
+D:\src\csdirac\csirac\DiractTest\bin\Release\nunit.framework.xml
+D:\src\csdirac\csirac\DiractTest\obj\Release\DiractTest.dll
+D:\src\csdirac\csirac\DiractTest\obj\Release\DiractTest.pdb
+D:\src\csdirac\csirac\DiractTest\bin\Release\System.Core.dll
+D:\src\csdirac\csirac\DiractTest\bin\Release\System.Net.dll
+D:\src\csdirac\csirac\DiractTest\bin\Release\System.Runtime.Serialization.dll
+D:\src\csdirac\csirac\DiractTest\bin\Release\System.Windows.dll
BIN  DiractTest/obj/Release/DiractTest.dll
Binary file not shown
BIN  DiractTest/obj/Release/DiractTest.pdb
Binary file not shown
BIN  DiractTest/obj/Release/ResolveAssemblyReference.cache
Binary file not shown
19 Math/Dimension.cs
@@ -0,0 +1,19 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace org.diracvideo.Math
+{
+ public class Dimension
+ {
+ private int width;
+ private int height;
+ public Dimension(int width, int height)
+ {
+ this.width = width;
+ this.height = height;
+ }
+ public int Width { get { return width; } set { width = value; } }
+ public int Height { get { return height; } set { height = value; } }
+ }
+}
20 Math/Point.cs
@@ -0,0 +1,20 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace org.diracvideo.Math
+{
+ public class Point
+ {
+ private int x;
+ private int y;
+
+ public Point(int x, int y)
+ {
+ this.x = x;
+ this.y = y;
+ }
+ public int X { get { return x; } set { x = value; } }
+ public int Y { get { return y; } set { y = value; } }
+ }
+}
36 Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// Les informations générales relatives à un assembly dépendent de
+// l'ensemble d'attributs suivant. Changez les valeurs de ces attributs pour modifier les informations
+// associées à un assembly.
+[assembly: AssemblyTitle("csdirac")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Windows-Trust")]
+[assembly: AssemblyProduct("csdirac")]
+[assembly: AssemblyCopyright("Copyright © Windows-Trust 2009")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// L'affectation de la valeur false à ComVisible rend les types invisibles dans cet assembly
+// aux composants COM. Si vous devez accéder à un type dans cet assembly à partir de
+// COM, affectez la valeur true à l'attribut ComVisible sur ce type.
+[assembly: ComVisible(false)]
+
+// Le GUID suivant est pour l'ID de la typelib si ce projet est exposé à COM
+[assembly: Guid("f5576238-b1e3-486e-98e1-8a153fdd764b")]
+
+// Les informations de version pour un assembly se composent des quatre valeurs suivantes :
+//
+// Version principale
+// Version secondaire
+// Numéro de build
+// Révision
+//
+// Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
+// en utilisant '*', comme indiqué ci-dessous :
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
39 TestResult.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="utf-8" standalone="no"?>
+<!--This file represents the results of running a test suite-->
+<test-results name="D:\src\csdirac\csirac\csiracTest.nunit" total="0" failures="0" not-run="1" date="2009-03-31" time="22:17:00">
+ <environment nunit-version="2.4.8.0" clr-version="2.0.50727.1433" os-version="Microsoft Windows NT 5.1.2600 Service Pack 3, v.5512" platform="Win32NT" cwd="D:\src\csdirac\csirac\DiractTest\bin\Release" machine-name="WINDOWS-EDEA89B" user="Administrateur" user-domain="WINDOWS-EDEA89B" />
+ <culture-info current-culture="fr-FR" current-uiculture="fr-FR" />
+ <test-suite name="D:\src\csdirac\csirac\csiracTest.nunit" success="True" time="0.016" asserts="0">
+ <results>
+ <test-suite name="D:\src\csdirac\csirac\DiractTest\bin\Release\DiractTest.dll" success="True" time="0.016" asserts="0">
+ <results>
+ <test-suite name="org" success="True" time="0.016" asserts="0">
+ <results>
+ <test-suite name="diracvideo" success="True" time="0.016" asserts="0">
+ <results>
+ <test-suite name="Jirac" success="True" time="0.016" asserts="0">
+ <results>
+ <test-suite name="Test" success="True" time="0.016" asserts="0">
+ <results>
+ <test-suite name="ArithTest" success="True" time="0.016" asserts="0">
+ <results>
+ <test-case name="org.diracvideo.Jirac.Test.ArithTest.main" executed="False">
+ <reason>
+ <message><![CDATA[Method main's signature is not correct: it must be an instance method.]]></message>
+ </reason>
+ </test-case>
+ </results>
+ </test-suite>
+ </results>
+ </test-suite>
+ </results>
+ </test-suite>
+ </results>
+ </test-suite>
+ </results>
+ </test-suite>
+ </results>
+ </test-suite>
+ </results>
+ </test-suite>
+</test-results>
BIN  bin/Debug/csdirac.dll
Binary file not shown
BIN  bin/Debug/csdirac.pdb
Binary file not shown
BIN  bin/Release/System.Core.dll
Binary file not shown
BIN  bin/Release/System.Net.dll
Binary file not shown
BIN  bin/Release/System.Runtime.Serialization.dll
Binary file not shown
BIN  bin/Release/System.Windows.dll
Binary file not shown
BIN  bin/Release/csdirac.dll
Binary file not shown
BIN  bin/Release/csdirac.pdb
Binary file not shown
76 csdirac.csproj
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>9.0.21022</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{9BE09FC4-05EF-450C-9852-708EB91DE326}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>csdirac</RootNamespace>
+ <AssemblyName>csdirac</AssemblyName>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Drawing" />
+ <Reference Include="System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>C:\Program Files\Microsoft Silverlight\3.0.40307.0\System.Windows.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="csdirac\Arithmetic.cs" />
+ <Compile Include="csdirac\Block.cs" />
+ <Compile Include="csdirac\Buffer.cs" />
+ <Compile Include="csdirac\Cache.cs" />
+ <Compile Include="csdirac\ColourSpace.cs" />
+ <Compile Include="csdirac\Decoder.cs" />
+ <Compile Include="csdirac\Global.cs" />
+ <Compile Include="csdirac\Motion.cs" />
+ <Compile Include="csdirac\Parameters.cs" />
+ <Compile Include="csdirac\Picture.cs" />
+ <Compile Include="csdirac\Queue.cs" />
+ <Compile Include="csdirac\Slice.cs" />
+ <Compile Include="csdirac\Stream.cs" />
+ <Compile Include="csdirac\SubBand.cs" />
+ <Compile Include="csdirac\Unpack.cs" />
+ <Compile Include="csdirac\Util.cs" />
+ <Compile Include="csdirac\Vector.cs" />
+ <Compile Include="csdirac\VideoFormat.cs" />
+ <Compile Include="csdirac\Wavelet.cs" />
+ <Compile Include="DiracStreamSource.cs" />
+ <Compile Include="Math\Dimension.cs" />
+ <Compile Include="Math\Point.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
1  csdirac.csproj.user
@@ -0,0 +1 @@
+<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />
26 csdirac.sln
@@ -0,0 +1,26 @@
+
+Microsoft Visual Studio Solution File, Format Version 10.00
+# Visual C# Express 2008
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "csdirac", "csdirac.csproj", "{9BE09FC4-05EF-450C-9852-708EB91DE326}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiractTest", "DiractTest\DiractTest.csproj", "{0D7DF30A-8A9B-4559-9A9F-D34DB6E8FBF2}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {9BE09FC4-05EF-450C-9852-708EB91DE326}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {9BE09FC4-05EF-450C-9852-708EB91DE326}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {9BE09FC4-05EF-450C-9852-708EB91DE326}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {9BE09FC4-05EF-450C-9852-708EB91DE326}.Release|Any CPU.Build.0 = Release|Any CPU
+ {0D7DF30A-8A9B-4559-9A9F-D34DB6E8FBF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {0D7DF30A-8A9B-4559-9A9F-D34DB6E8FBF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {0D7DF30A-8A9B-4559-9A9F-D34DB6E8FBF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {0D7DF30A-8A9B-4559-9A9F-D34DB6E8FBF2}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
BIN  csdirac.suo
Binary file not shown
260 csdirac/Arithmetic.cs
@@ -0,0 +1,260 @@
+namespace org.diracvideo.Jirac
+{
+
+
+ internal class Context {
+
+ public const short
+ ZERO_CODEBLOCK = 0,
+ QUANTISER_CONT = 1,
+ QUANTISER_VALUE = 2,
+ QUANTISER_SIGN = 3,
+ ZPZN_F1 = 4,
+ ZPNN_F1 = 5,
+ ZP_F2 = 6,
+ ZP_F3 = 7,
+ ZP_F4 = 8,
+ ZP_F5 = 9,
+ ZP_F6p = 10,
+ NPZN_F1 = 11,
+ NPNN_F1 = 12,
+ NP_F2 = 13,
+ NP_F3 = 14,
+ NP_F4 = 15,
+ NP_F5 = 16,
+ NP_F6p = 17,
+ SIGN_POS = 18,
+ SIGN_NEG = 19,
+ SIGN_ZERO = 20,
+ COEFF_DATA = 21,
+ SB_F1 = 22,
+ SB_F2 = 23,
+ SB_DATA = 24,
+ BLOCK_MODE_REF1 = 25,
+ BLOCK_MODE_REF2 = 26,
+ GLOBAL_BLOCK = 27,
+ LUMA_DC_CONT_BIN1 = 28,
+ LUMA_DC_CONT_BIN2 = 29,
+ LUMA_DC_VALUE = 30,
+ LUMA_DC_SIGN = 31,
+ CHROMA1_DC_CONT_BIN1 = 32,
+ CHROMA1_DC_CONT_BIN2 = 33,
+ CHROMA1_DC_VALUE = 34,
+ CHROMA1_DC_SIGN = 35,
+ CHROMA2_DC_CONT_BIN1 = 36,
+ CHROMA2_DC_CONT_BIN2 = 37,
+ CHROMA2_DC_VALUE = 38,
+ CHROMA2_DC_SIGN = 39,
+ MV_REF1_H_CONT_BIN1 = 40,
+ MV_REF1_H_CONT_BIN2 = 41,
+ MV_REF1_H_CONT_BIN3 = 42,
+ MV_REF1_H_CONT_BIN4 = 43,
+ MV_REF1_H_CONT_BIN5 = 44,
+ MV_REF1_H_VALUE = 45,
+ MV_REF1_H_SIGN = 46,
+ MV_REF1_V_CONT_BIN1 = 47,
+ MV_REF1_V_CONT_BIN2 = 48,
+ MV_REF1_V_CONT_BIN3 = 49,
+ MV_REF1_V_CONT_BIN4 = 50,
+ MV_REF1_V_CONT_BIN5 = 51,
+ MV_REF1_V_VALUE = 52,
+ MV_REF1_V_SIGN = 53,
+ MV_REF2_H_CONT_BIN1 = 54,
+ MV_REF2_H_CONT_BIN2 = 55,
+ MV_REF2_H_CONT_BIN3 = 56,
+ MV_REF2_H_CONT_BIN4 = 57,
+ MV_REF2_H_CONT_BIN5 = 58,
+ MV_REF2_H_VALUE = 59,
+ MV_REF2_H_SIGN = 60,
+ MV_REF2_V_CONT_BIN1 = 61,
+ MV_REF2_V_CONT_BIN2 = 62,
+ MV_REF2_V_CONT_BIN3 = 63,
+ MV_REF2_V_CONT_BIN4 = 64,
+ MV_REF2_V_CONT_BIN5 = 65,
+ MV_REF2_V_VALUE = 66,
+ MV_REF2_V_SIGN = 67,
+ LAST = 68;
+ }
+
+
+ public class Arithmetic {
+ private int offset, size, code, low, range, cntr;
+ private byte[] data;
+ private byte shift;
+ private int[] probabilities;
+ private short[] lut;
+ /* static lookup tables */
+ private readonly static short[] LUT = new short[] {
+ 0, 2, 5, 8, 11, 15, 20, 24,
+ 29, 35, 41, 47, 53, 60, 67, 74,
+ 82, 89, 97, 106, 114, 123, 132, 141,
+ 150, 160, 170, 180, 190, 201, 211, 222,
+ 233, 244, 256, 267, 279, 291, 303, 315,
+ 327, 340, 353, 366, 379, 392, 405, 419,
+ 433, 447, 461, 475, 489, 504, 518, 533,
+ 548, 563, 578, 593, 609, 624, 640, 656,
+ 672, 688, 705, 721, 738, 754, 771, 788,
+ 805, 822, 840, 857, 875, 892, 910, 928,
+ 946, 964, 983, 1001, 1020, 1038, 1057, 1076,
+ 1095, 1114, 1133, 1153, 1172, 1192, 1211, 1231,
+ 1251, 1271, 1291, 1311, 1332, 1352, 1373, 1393,
+ 1414, 1435, 1456, 1477, 1498, 1520, 1541, 1562,
+ 1584, 1606, 1628, 1649, 1671, 1694, 1716, 1738,
+ 1760, 1783, 1806, 1828, 1851, 1874, 1897, 1920,
+ 1935, 1942, 1949, 1955, 1961, 1968, 1974, 1980,
+ 1985, 1991, 1996, 2001, 2006, 2011, 2016, 2021,
+ 2025, 2029, 2033, 2037, 2040, 2044, 2047, 2050,
+ 2053, 2056, 2058, 2061, 2063, 2065, 2066, 2068,
+ 2069, 2070, 2071, 2072, 2072, 2072, 2072, 2072,
+ 2072, 2071, 2070, 2069, 2068, 2066, 2065, 2063,
+ 2060, 2058, 2055, 2052, 2049, 2045, 2042, 2038,
+ 2033, 2029, 2024, 2019, 2013, 2008, 2002, 1996,
+ 1989, 1982, 1975, 1968, 1960, 1952, 1943, 1934,
+ 1925, 1916, 1906, 1896, 1885, 1874, 1863, 1851,
+ 1839, 1827, 1814, 1800, 1786, 1772, 1757, 1742,
+ 1727, 1710, 1694, 1676, 1659, 1640, 1622, 1602,
+ 1582, 1561, 1540, 1518, 1495, 1471, 1447, 1422,
+ 1396, 1369, 1341, 1312, 1282, 1251, 1219, 1186,
+ 1151, 1114, 1077, 1037, 995, 952, 906, 857,
+ 805, 750, 690, 625, 553, 471, 376, 255
+ };
+
+ private readonly static int[] next_list = new int[] {
+ 0, Context.QUANTISER_CONT,0,0,
+ Context.ZP_F2, Context.ZP_F2,
+ Context.ZP_F3, Context.ZP_F4,
+ Context.ZP_F5, Context.ZP_F6p,
+ Context.ZP_F6p, Context.NP_F2,
+ Context.NP_F2, Context.NP_F3,
+ Context.NP_F4, Context.NP_F5,
+ Context.NP_F6p, Context.NP_F6p,
+ 0, 0, 0, 0,Context.SB_F2,
+ Context.SB_F2, 0, 0, 0, 0,
+ Context.LUMA_DC_CONT_BIN2,
+ Context.LUMA_DC_CONT_BIN2,
+ 0, 0, Context.CHROMA1_DC_CONT_BIN2,
+ Context.CHROMA1_DC_CONT_BIN2,
+ 0, 0, Context.CHROMA2_DC_CONT_BIN2,
+ Context.CHROMA2_DC_CONT_BIN2,
+ 0, 0, Context.MV_REF1_H_CONT_BIN2,
+ Context.MV_REF1_H_CONT_BIN3,
+ Context.MV_REF1_H_CONT_BIN4,
+ Context.MV_REF1_H_CONT_BIN5,
+ Context.MV_REF1_H_CONT_BIN5,
+ 0, 0, Context.MV_REF1_V_CONT_BIN2,
+ Context.MV_REF1_V_CONT_BIN3,
+ Context.MV_REF1_V_CONT_BIN4,
+ Context.MV_REF1_V_CONT_BIN5,
+ Context.MV_REF1_V_CONT_BIN5,
+ 0, 0, Context.MV_REF2_H_CONT_BIN2,
+ Context.MV_REF2_H_CONT_BIN3,
+ Context.MV_REF2_H_CONT_BIN4,
+ Context.MV_REF2_H_CONT_BIN5,
+ Context.MV_REF2_H_CONT_BIN5,
+ 0, 0, Context.MV_REF2_V_CONT_BIN2,
+ Context.MV_REF2_V_CONT_BIN3,
+ Context.MV_REF2_V_CONT_BIN4,
+ Context.MV_REF2_V_CONT_BIN5,
+ Context.MV_REF2_V_CONT_BIN5,
+ 0, 0, 0
+ };
+
+ public Arithmetic(Buffer b) {
+ data = b.d;
+ offset = b.s;
+ size = b.e;
+ DecodeInit();
+ }
+
+ public Arithmetic(byte[] d)
+ : this(new Buffer(d))
+ {
+ }
+
+ private void DecodeInit() {
+ cntr = 0;
+ low = 0;
+ range = 0xffff;
+ if(size - offset > 1) {
+ code = ((data[offset]&0xff)<<8) | (data[offset+1]&0xff);
+ } else if(size - offset > 0) {
+ code = ((data[offset]&0xff)<<8) | 0xff;
+ } else {
+ code = 0xffff;
+ }
+ offset += 2;
+ shift = (size - offset > 0 ? data[offset] : (byte)0xff);
+ probabilities = new int[Context.LAST];
+ lut = new short[512];
+ for(int i = 0; i < Context.LAST; i++) {
+ probabilities[i] = 0x8000;
+ }
+ for(int i = 0; i < 256; i++) {
+ lut[i] = Arithmetic.LUT[255-i];
+ lut[256+i] = (short)(-Arithmetic.LUT[i]);
+ }
+ }
+
+ public int DecodeUint(int cont, int val) {
+ int v = 1;
+ while(!DecodeBool(cont)) {
+ cont = Arithmetic.next_list[cont];
+ v = (v << 1) | DecodeBit(val);
+ }
+ return v-1;
+ }
+
+ public int DecodeSint(int cont, int val, int sign) {
+ int v = DecodeUint(cont, val);
+ return (v == 0 || DecodeBool(sign)) ? -v : v;
+ }
+
+ public int DecodeBit(int context) {
+ return (DecodeBool(context) ? 1 : 0);
+ }
+
+ public bool DecodeBool(int context) {
+ bool v;
+ int range_times_prob, lut_index;
+ range_times_prob =
+ (range * probabilities[context]) >> 16;
+ v = (code - low >= range_times_prob);
+ lut_index = probabilities[context] >> 8 | (v ? 256 : 0);
+ probabilities[context] += lut[lut_index];
+ if(v) {
+ low += range_times_prob;
+ range -= range_times_prob;
+ } else {
+ range = range_times_prob;
+ }
+
+ while(range <= 0x4000) {
+ low <<= 1;
+ range <<= 1;
+ code <<= 1;
+ code |= (shift >> (7-cntr))&1;
+ cntr++;
+ if(cntr == 8) {
+ offset++;
+ if(offset < size) {
+ shift = data[offset];
+ } else {
+ shift = (byte)0xff;
+ }
+ low &= 0xffff;
+ code &= 0xffff;
+ if(code < low) {
+ code |= (1<<16);
+ }
+ cntr = 0;
+ }
+ }
+ return v;
+ }
+
+ /* an estimate of how many bits there are left */
+ public int BytesLeft() {
+ return (size - offset);
+ }
+ }
+}
197 csdirac/Block.cs
@@ -0,0 +1,197 @@
+//import java.awt.Point;
+//import java.awt.Dimension;
+using org.diracvideo.Math;
+using System;
+
+namespace org.diracvideo.Jirac
+{
+
+
+ /** Block
+ *
+ * Has methods for getting the correct positions
+ * of elements in the data array. I would call it
+ * Frame but that conflicts with java.awt.Frame. */
+
+ public class Block : IEquatable<Block> {
+ public short[] d;
+ public Point p;
+ public Dimension s, o;
+ /** Default Block constructor
+ *
+ * @param d is the data of the frame
+ * @param p is the place where the frame should start
+ * @param s is the dimension of the frame
+ * @param o is the dimension of the outer frame **/
+
+ public Block(short[] d, Point p, Dimension s, Dimension o) {
+ this.s = s;
+ this.d = d;
+ this.p = p;
+ this.o = o;
+ }
+ /**
+ * Creates a Block consisting of the entire frame.
+ *
+ * @param width is the width of the frame
+ * @param d is the data of the frame **/
+
+ public Block(short[] d, int width) {
+ this.d = d;
+ this.p = new Point(0,0);
+ this.s = this.o = new Dimension(width, d.Length / width);
+ }
+
+ /**
+ * Creates a Block with dimension d namespace a
+ * newly allocated array for the frame **/
+ public Block(Dimension d) {
+ this.d = new short[d.Width * d.Height];
+ this.o = this.s = d;
+ this.p = new Point(0,0);
+ }
+
+ public Block Sub(Point off, Dimension sub) {
+ Point pnt = new Point(p.X + off.X, p.Y + off.Y);
+ return new Block(d, pnt, sub, o);
+ }
+
+ /** @return the index to the start of the frame **/
+ public int Start() {
+ return (p.Y*o.Width) + p.X;
+ }
+
+ /** @return the index to the end of the frame (last element + 1) **/
+ public int End() {
+ return Line(s.Height - 1) + s.Width;
+ }
+
+ /** @return the line of the frame with index n **/
+ public int Line(int n) {
+ return (n+p.Y)*o.Width + p.X;
+ }
+
+ /** The index of a point
+ * @return the index for a general point in the frame **/
+ public int Index(int x, int y) {
+ return Line(y) + x;
+ }
+
+ /** Pixel at a given point, unchecked */
+ public short Pixel(int x, int y) {
+ return d[(y + p.Y)*o.Width + (p.X + x)];
+ }
+
+ /** Pixel at a given point, checked */
+ public short Real(int x, int y) {
+ return Pixel(Util.Clamp(x, 0, s.Width - 1),
+ Util.Clamp(y, 0, s.Height - 1));
+ }
+
+ public void Set(int x, int y, short v) {
+ d[(y+p.Y)*o.Width + (p.X + x)] = v;
+ }
+
+ public void Set(int x, int y, int v) {
+ Set(x, y, (short)v);
+ }
+
+ public void AddTo(Block o) {
+ int height = System.Math.Min(s.Height, o.s.Height);
+ int width = System.Math.Min(s.Width, o.s.Width);
+ for(int y = 0; y < height; y++) {
+ int a = Line(y);
+ int b = o.Line(y);
+ for(int x = 0; x < width; x++) {
+ o.d[b+x] += d[a+x];
+ }
+ }
+ }
+
+ /** upsample a block
+ * block should be `real'
+ * @return the upsampled block
+ * @see the dirac specification section 15.8.11 */
+ public Block UpSample() {
+ Block r = new Block(new Dimension(2*s.Width, 2*s.Height));
+ short[] taps = new short[] {21, -7, 3, -1};
+ for(int y = 0; y < s.Height - 1; y++) { /* vertical upsampling */
+ for(int x = 0; x < s.Width - 1; x++) {
+ r.Set(x*2, y*2, Pixel(x,y)); /* the copying part */
+ }
+ for(int x = 0; x < s.Width - 1; x++) {
+ short val = 16;
+ for(int i = 0; i < 4; i++) {
+ val += (short)(taps[i] * Pixel(x, System.Math.Max(0, y - i)));
+ val += (short)(taps[i] * Pixel(x, System.Math.Min(s.Height - 1, y + i)));
+ }
+ r.Set(x*2, y*2 + 1, (short)(val >> 5));
+ }
+ }
+ for(int y = 0; y < s.Height - 1; y++) {
+ for(int x = 0; x < s.Width - 1; x++) {
+ short val = 16;
+ for(int i = 0; i < 4; i++) {
+ val += (short)(taps[i] * Pixel(System.Math.Max(0, x - i), y));
+ val += (short)(taps[i] * Pixel(System.Math.Min(x + i, s.Width - 1), y));
+ }
+ r.Set(x*2 + 1, y*2, (short) (val >> 5));
+ val = 16;
+ for(int i = 0; i < 4; i++) {
+ int xdown = System.Math.Max(0, (x - i) * 2);
+ int xup = System.Math.Min(2 * s.Width - 2, (x + i) * 2);
+ val += (short)(taps[i]*r.Pixel(xdown, y*2+1));
+ val += (short)(taps[i]*r.Pixel(xup, y*2+1));
+ }
+ r.Set(x*2 + 1, y*2 + 1, (short) (val >> 5));
+ }
+ }
+ return r;
+ }
+
+ public void ShiftOut(int b, int a) {
+ for(int y = 0; y < s.Height; y++) {
+ int line = Line(y);
+ for(int x = 0; x < s.Width; x++) {
+ d[line + x] += (short)a;
+ d[line + x] >>= b;
+ }
+ }
+ }
+
+ public void Clip(int b) {
+ int l = -(1 << b), h = (1 << b) - 1;
+ for(int y = 0; y < s.Height; y++) {
+ int line = Line(y);
+ for(int x = 0; x < s.Width; x++)
+ d[line+x] = (short)Util.Clamp(d[line+x], l, h);
+ }
+ }
+
+ /** A test method which fills the block with a checkers pattern
+ * @param m the size of the blocks */
+ public void Checkers(int m) {
+ m = (1 << m);
+ for(int i = 0; i < s.Height; i++) {
+ for(int j = 0; j < s.Width; j++) {
+ Set(i,j,(short)(((i&m)^(j&m))*255));
+ }
+ }
+ }
+
+ /** a method to test for the equality of two blocks
+ * Two blocks are found to be equal if each of their
+ * points are equal */
+ public bool Equals(Block o) {
+ if(s.Width != o.s.Width)
+ return false;
+ if(s.Height != o.s.Height)
+ return false;
+ for(int i = 0; i < s.Height; i++)
+ for(int j = 0; j < s.Width; j++)
+ if(Pixel(i,j) != o.Pixel(i,j))
+ return false;
+ return true;
+ }
+ }
+}
61 csdirac/Buffer.cs
@@ -0,0 +1,61 @@
+namespace org.diracvideo.Jirac
+{
+
+ /** Buffer
+ *
+ * Buffer represents a one-dimensional array,
+ */
+ public class Buffer {
+ public int s,e;
+ public byte[] d;
+
+ public Buffer(byte[] d, int s, int e) {
+ this.s = Util.Clamp(s,0,d.Length);
+ this.e = Util.Clamp(e,s,d.Length);
+ this.d = d;
+ }
+
+ public Buffer(byte[] d, int b) : this(d, b, d.Length) { }
+
+ public Buffer(byte[] d) : this(d, 0, d.Length) { }
+
+
+ /** Get a subbuffer
+ *
+ * Parameters are relative to the beginning of the block
+ *
+ * @param b begin of block
+ * @param e end of block */
+
+ public Buffer Sub(int b, int e) {
+ return new Buffer(this.d, this.s + b, this.s + e);
+ }
+
+ public Buffer Sub(int b) {
+ return new Buffer(this.d, this.s + b, this.e);
+ }
+
+ public byte GetByte(int i) {
+ return d[i + s];
+ }
+
+ public int GetInt(int i) {
+ int r = 0;
+ for(int j = i + s; j < i + s + 4; j++)
+ r = (r << 8) | (d[j]&0xff);
+ return r;
+ }
+
+ public int Size() {
+ return e - s;
+ }
+
+ public Buffer Cat(Buffer o) {
+ byte[] n = new byte[o.Size() + Size()];
+ System.Array.Copy(this.d, this.s, n, 0, e - s);
+ System.Array.Copy(o.d, o.s, n, e - s, o.e - o.s);
+ return new Buffer(n);
+ }
+ }
+
+}
64 csdirac/Cache.cs
@@ -0,0 +1,64 @@
+namespace org.diracvideo.Jirac
+{
+
+ public class Cache {
+ private int end;
+ private int[] nums;
+ private Block[][] blocks, scaled;
+
+ public Cache(int n) {
+ end = 0;
+ nums = new int[n];
+ blocks = new Block[n][];
+ scaled = new Block[n][];
+ }
+
+ public void Add(int n, Block[] refs) {
+ if(end == nums.Length)
+ ShiftFrom(0);
+ nums[end] = n;
+ blocks[end] = refs;
+ end++;
+ }
+
+ public Block[] Get(int n, bool upscaled) {
+ int i = GetIndex(n);
+ if(i < 0) return null;
+ if(upscaled) {
+ if(scaled[i] == null) {
+ scaled[i] = new Block[blocks[i].Length];
+ for(int j = 0; j < scaled[i].Length; j++)
+ scaled[i][j] = blocks[i][j].UpSample();
+ }
+ return scaled[i];
+ }
+ return blocks[i];
+ }
+
+ public void Remove(int n) {
+ int i = GetIndex(n);
+ if(i >= 0) ShiftFrom(i);
+ }
+
+ public bool Has(int n) {
+ return GetIndex(n) >= 0;
+ }
+
+ private void ShiftFrom(int i) {
+ while(++i < end) {
+ nums[i-1] = nums[i];
+ blocks[i-1] = blocks[i];
+ scaled[i-1] = scaled[i];
+ }
+ end--;
+ nums[end] = -1;
+ blocks[end] = scaled[end] = null;
+ }
+
+ private int GetIndex(int n) {
+ for(int i = 0; i < end; i++)
+ if(nums[i] == n) return i;
+ return -1;
+ }
+ }
+}
75 csdirac/ColourSpace.cs
@@ -0,0 +1,75 @@
+using System.Text;
+using System;
+using org.diracvideo.Math;
+//import java.awt.Dimension;
+namespace org.diracvideo.Jirac
+{
+
+
+ public class ColourSpace {
+ private VideoFormat format;
+ int[][] rgb_table, matrix;
+ public ColourSpace(int colourmode, VideoFormat fmt) {
+ format = fmt;
+ SetupTables();
+ }
+
+ private void SetupTables() {
+ matrix = new int[3][];
+
+ for (int i = 0; i < 3; i++)
+ matrix[i] = new int[3];
+
+ rgb_table = new int[9][];
+ for (int j = 0; j < 9; j++)
+ rgb_table[j] = new int[255];
+ SetupMatrix(0.2990, 0.1140);
+ for(int c = 0; c < 3; c++) {
+ for(int i = 0; i < 255; i++) {
+ rgb_table[3*c][i] = (matrix[0][c]*i)>>16;
+ rgb_table[3*c+1][i] = (matrix[1][c]*(i-128))>>16;
+ rgb_table[3*c+2][i] = (matrix[2][c]*(i-128))>>16;
+ }
+ }
+ }
+
+ private void SetupMatrix(double kr, double kb) {
+ double kg = 1.0 - kr - kb;
+ int factor = 1 << 16;
+ matrix[0][0] = matrix[1][0] = matrix[2][0] = factor;
+ matrix[0][2] = (int)(2*(1-kr)*factor);
+ matrix[1][1] = (int)((-2*kb*(1-kb)/kg)*factor);
+ matrix[1][2] = (int)((-2*kr*(1-kr)/kg)*factor);
+ matrix[2][1] = (int)(2*(1-kb)*factor);
+ }
+
+
+
+ public int Clamp(int y) {
+ return Util.Clamp(y+128,0,255);
+ }
+
+ public void Convert(Block[] yuv, ref int[] rgb) {
+ short[] Y = yuv[0].d, U = yuv[1].d, V = yuv[2].d;
+ int xShift = format.ChromaHShift(), yShift = format.ChromaVShift();
+ Dimension lum = yuv[0].s;
+ for(int y = 0; y < lum.Height; y++) {
+ int yLine = yuv[0].Line(y);
+ int uvLine = yuv[1].Line(y >> yShift);
+ int rgbLine = y*lum.Width;
+ for(int x = 0; x < lum.Width; x++)
+ rgb[rgbLine+x] = Clamp(Y[yLine+x])*0x010101;
+ }
+ }
+
+ public override string ToString() {
+ StringBuilder sb = new StringBuilder();
+ sb.Append("org.diracvideo.Jirac.ColourSpace");
+ for(int i = 0; i < 3; i++) {
+ sb.Append(String.Format("\n%d\t%d\t%d", matrix[i][0],
+ matrix[i][1], matrix[i][2]));
+ }
+ return sb.ToString();
+ }
+ }
+}
114 csdirac/Decoder.cs
@@ -0,0 +1,114 @@
+using System;
+using System.Threading;
+namespace org.diracvideo.Jirac
+{
+
+ /**
+ * Decoder
+ *
+ * An interface to decoding a dirac stream.
+ * Most (all) of the actual work is done by the
+ * Picture class, however Decoder can do general
+ * dispatching, scheduling and bookkeeping.
+ * That is the reason we keep it arround */
+
+ public class Decoder {
+ private Stream stream;
+ public VideoFormat format;
+ public Status status = Status.NULL;
+ public Exception e;
+ public Queue inQueue, outQueue;
+ public Cache refs;
+ public enum Status {NULL, OK, WAIT, DONE, ERROR}
+ public static AutoResetEvent ev = new AutoResetEvent(false);
+
+ public Decoder() {
+ stream = new Stream();
+ refs = new Cache(4);
+ inQueue = new InputQueue();
+ outQueue = new OutputQueue();
+ }
+
+ /** Push:
+ * @param d byte array containing stream data
+ * @param o offset in the byte array
+ *
+ * The stated goal of this function is that
+ * it should work even in the case of a so-called
+ * DumbMuxingFormat which splits the dirac stream
+ k * into x-byte segments, and that the driver program
+ * should only ever have to push such segments to
+ * the decoder. */
+
+ public void Push(byte[] d, int s, int e) {
+ Push(new Buffer(d,s,e));
+ }
+
+
+ private void Push(Buffer buf) {
+ stream.Add(buf);
+ for(Buffer packet = stream.Next();
+ packet != null;
+ packet = stream.Next())
+ Dispatch(packet);
+
+ if(!inQueue.Empty() && status == Status.WAIT)
+ lock (this) { ev.Set(); }
+ }
+
+
+ /* at this point, the buffer must be a complete dirac packet */
+ private void Dispatch(Buffer b) {
+ if (b.GetInt(5) != b.Size())
+ throw new Exception("Incorrect buffer sizes");
+ byte c = b.GetByte(4);
+ switch(c) {
+ case 0x00:
+ VideoFormat tmp = new VideoFormat(b);
+ if(format == null) {
+ format = tmp;
+ status = Status.OK;
+ } else if(!tmp.Equals(format)) {
+ throw new Exception("Stream Error: Inequal Video Formats");
+ }
+ break;
+ case 0x10:
+ status = Status.DONE;
+ break;
+ case 0x20:
+ case 0x30:
+ break;
+ default:
+ if(format == null)
+ throw new Exception("Stream Error: Picture Before Header");
+ Picture pic = new Picture(b, this);
+ pic.Parse();
+ inQueue.Push(pic);
+ break;
+ }
+ }
+
+ public Picture Pull() {
+ return outQueue.Pull();
+ }
+
+ public void Decode() {
+ lock (inQueue)
+ {
+ while(!inQueue.Empty()) {
+ Picture pic = inQueue.Pull();
+ pic.Decode();
+ if(pic.error != null)
+ Console.WriteLine(pic.error.ToString());
+ else
+ outQueue.Push(pic);
+ }
+ }
+ }
+
+ public bool Done() {
+ return status == Status.DONE && inQueue.Empty();
+ }
+ }
+
+}
22 csdirac/Global.cs
@@ -0,0 +1,22 @@
+namespace org.diracvideo.Jirac
+{
+
+ /** Global
+ *
+ * The class for global motion estimation */
+ internal class Global
+ {
+ public int b0, b1;
+ public int a_exp, a00, a01, a10, a11;
+ public int c_exp, c0, c1;
+
+ public void GetVector(Vector mv, int x, int y, int n)
+ {
+ int scale = (1 << c_exp) - (c0 * x + c1 * y);
+ int dx = scale * (a00 * x + a01 * y + (1 << a_exp) * b0);
+ int dy = scale * (a10 * x + a11 * y + (1 << a_exp) * b1);
+ mv.dx[n] = dx >> (a_exp + c_exp);
+ mv.dy[n] = dy >> (a_exp + c_exp);
+ }
+ }
+}
514 csdirac/Motion.cs
<
@@ -0,0 +1,514 @@
+//import java.awt.Dimension;
+//import java.awt.Point;
+
+using System;
+using org.diracvideo.Math;
+namespace org.diracvideo.Jirac
+{
+
+
+ /** Motion
+ *
+ * An ill-named class representing an object
+ * which does motion compensation prediction
+ * on a picture. **/
+ class Motion {
+ Parameters par;
+ Vector[] vecs;
+ Block[][] refs;
+ Arithmetic[] ar;
+ int xbsep, ybsep, xblen, yblen, xoffset, yoffset;
+ int chroma_h_shift, chroma_v_shift;
+ short[] weight_x, weight_y, obmc;
+ Block[] tmp_ref;
+ Block block;
+
+ static int ARITH_SUPERBLOCK = 0;
+ static int ARITH_PRED_MODE = 1;
+ static int ARITH_REF1_X = 2;
+ static int ARITH_REF1_Y = 3;
+ static int ARITH_REF2_X = 4;
+ static int ARITH_REF2_Y = 5;
+ static int ARITH_DC_0 = 6;
+ static int ARITH_DC_1 = 7;
+ static int ARITH_DC_2 = 8;
+
+ public Motion(Parameters p, Buffer[] bufs, Block[][] frames) {
+ par = p;
+ refs = frames;
+ vecs = new Vector[par.x_num_blocks * par.y_num_blocks];
+ tmp_ref = new Block[refs.Length];
+ ar = new Arithmetic[9];
+ for(int i = 0; i < 9; i++)
+ if(bufs[i] != null) ar[i] = new Arithmetic(bufs[i]);
+ }
+
+
+ public void Decode() {
+ for(int y = 0; y < par.y_num_blocks; y += 4)
+ for(int x = 0; x < par.x_num_blocks; x += 4)
+ DecodeMacroBlock(x,y);
+ }
+
+ private void DecodeMacroBlock(int x, int y) {
+ int split = SplitPrediction(x,y);
+ Vector mv = GetVector(x,y);
+ mv.split = (split + ar[ARITH_SUPERBLOCK].DecodeUint(Context.SB_F1, Context.SB_DATA))%3;
+ switch(mv.split) {
+ case 0:
+ DecodePredictionUnit(mv, x, y);
+ for(int i = 0; i < 4; i++)
+ for(int j = 0; j < 4; j++)
+ SetVector(mv, x + j, y + i);
+ break;
+ case 1:
+ for(int i = 0; i < 4; i += 2)
+ for(int j = 0; j < 4; j += 2) {
+ mv = GetVector(x + j, y + i);
+ mv.split = 1;
+ DecodePredictionUnit(mv, x + j, y + i);
+ SetVector(mv, x + j + 1, y + i);
+ SetVector(mv, x + j, y + i + 1);
+ SetVector(mv, x + j + 1, y + i + 1);
+ }
+ break;
+ case 2:
+ for(int i = 0; i < 4; i++)
+ for(int j = 0; j < 4; j++) {
+ mv = GetVector(x + j, y + i);
+ mv.split = 2;
+ DecodePredictionUnit(mv, x + j, y + i);
+ }
+ break;
+ default:
+ throw new Exception("Unsupported splitting mode");
+ }
+ }
+
+ private void DecodePredictionUnit(Vector mv, int x, int y) {
+ mv.pred_mode = ModePrediction(x,y);
+ mv.pred_mode ^= ar[ARITH_PRED_MODE].DecodeBit(Context.BLOCK_MODE_REF1);
+ if(par.num_refs > 1) {
+ mv.pred_mode ^= (ar[ARITH_PRED_MODE].DecodeBit(Context.BLOCK_MODE_REF2) << 1);
+ }
+ if(mv.pred_mode == 0) {
+ int[] pred = new int[3];
+ DcPrediction(x,y,pred);
+ mv.dc[0] = pred[0] +
+ ar[ARITH_DC_0].DecodeSint(Context.LUMA_DC_CONT_BIN1,
+ Context.LUMA_DC_VALUE,
+ Context.LUMA_DC_SIGN);
+ mv.dc[1] = pred[1] +
+ ar[ARITH_DC_1].DecodeSint(Context.CHROMA1_DC_CONT_BIN1,
+ Context.CHROMA1_DC_VALUE,
+ Context.CHROMA1_DC_SIGN);
+ mv.dc[2] = pred[2] +
+ ar[ARITH_DC_2].DecodeSint(Context.CHROMA2_DC_CONT_BIN1,
+ Context.CHROMA2_DC_VALUE,
+ Context.CHROMA2_DC_SIGN);
+ } else {
+ int pred_x, pred_y;
+ if(par.have_global_motion) {
+ int pred = GlobalPrediction(x,y);
+ pred ^= ar[ARITH_SUPERBLOCK].DecodeBit(Context.GLOBAL_BLOCK);
+ mv.namespace_global = (pred == 0 ? false : true);
+ } else {
+ mv.namespace_global = false;
+ }
+ if(!mv.namespace_global) {
+ if((mv.pred_mode & 1) != 0) {
+ VectorPrediction(mv,x,y,1);
+ mv.dx[0] +=
+ ar[ARITH_REF1_X].DecodeSint(Context.MV_REF1_H_CONT_BIN1,
+ Context.MV_REF1_H_VALUE,
+ Context.MV_REF1_H_SIGN);
+ mv.dy[0] +=
+ ar[ARITH_REF1_Y].DecodeSint(Context.MV_REF1_V_CONT_BIN1,
+ Context.MV_REF1_V_VALUE,
+ Context.MV_REF1_V_SIGN);
+ }
+ if((mv.pred_mode & 2) != 0) {
+ VectorPrediction(mv, x, y, 2);
+ mv.dx[1] += ar[ARITH_REF2_X].DecodeSint(Context.MV_REF2_H_CONT_BIN1,
+ Context.MV_REF2_H_VALUE,
+ Context.MV_REF2_H_SIGN);
+ mv.dy[1] += ar[ARITH_REF2_Y].DecodeSint(Context.MV_REF2_V_CONT_BIN1,
+ Context.MV_REF2_V_VALUE,
+ Context.MV_REF2_V_SIGN);
+
+ }
+ }
+ }
+ mv.namespace_global = false;
+ mv.dx[0] = 0;
+ mv.dy[0] = 0;
+ mv.dx[1] = 0;
+ mv.dy[1] = 0;
+ }
+
+ public void Render(Block[] outBlocks, VideoFormat f) {
+ for(int k = 0; k < outBlocks.Length; k++) {
+ InitializeRender(k,f);
+ block = new Block(new Dimension(xblen, yblen));
+ for(int i = 0; i < par.num_refs; i++)
+ tmp_ref[i] = refs[i][k];
+ for(int j = 0; j < par.y_num_blocks; j++)
+ for(int i = 0; i < par.x_num_blocks; i++) {
+ PredictBlock(outBlocks[k], i, j, k);
+ AccumulateBlock(outBlocks[k], i*xbsep - xoffset,
+ j*ybsep - yoffset);
+ }
+ outBlocks[k].ShiftOut(6,0);
+ outBlocks[k].Clip(7);
+ }
+ }
+
+ private void InitializeRender(int k, VideoFormat f) {
+ chroma_h_shift = f.ChromaHShift();
+ chroma_v_shift = f.ChromaVShift();
+ yblen = par.yblen_luma;
+ xblen = par.xblen_luma;
+ ybsep = par.ybsep_luma;
+ xbsep = par.xbsep_luma;
+ if(k != 0) {
+ yblen >>= chroma_v_shift;
+ ybsep >>= chroma_v_shift;
+ xbsep >>= chroma_h_shift;
+ xblen >>= chroma_h_shift;
+ }
+ yoffset = (yblen - ybsep) >> 1;
+ xoffset = (xblen - xbsep) >> 1;
+ /* initialize obmc weight */
+ weight_y = new short[yblen];
+ weight_x = new short[xblen];
+ obmc = new short[xblen*yblen];
+ for(int i = 0; i < xblen; i++) {
+ short wx;
+ if(xoffset == 0) {
+ wx = 8;
+ } else if( i < 2*xoffset) {
+ wx = Util.GetRamp(i, xoffset);
+ } else if(xblen - 1 - i < 2*xoffset) {
+ wx = Util.GetRamp(xblen - 1 - i, xoffset);
+ } else {
+ wx = 8;
+ }
+ weight_x[i] = wx;
+ }
+ for(int j = 0; j < yblen; j++) {
+ short wy;
+ if(yoffset == 0) {
+ wy = 8;
+ } else if(j < 2*yoffset) {
+ wy = Util.GetRamp(j, yoffset);
+ } else if(yblen - 1 - j < 2*yoffset) {
+ wy = Util.GetRamp(yblen - 1 - j, yoffset);
+ } else {
+ wy = 8;
+ }
+ weight_y[j] = wy;
+ }
+ }
+
+ private void DumpWeights() {
+ System.Console.WriteLine("weight_x");
+ for(int i = 0; i < xblen; i++)
+ Console.WriteLine("%d ", weight_x[i]);
+ System.Console.WriteLine("\nweight_y");
+ for(int i = 0; i < yblen; i++)
+ Console.WriteLine("%d ", weight_y[i]);
+ System.Console.WriteLine("");
+ }
+
+ private void PredictBlock(Block b, int i, int j, int k) {
+ int xstart = (i*xbsep) - xoffset,
+ ystart = (j*ybsep) - yoffset;
+ Vector mv = GetVector(i,j);
+ if(mv.pred_mode == 0) {
+ for(int q = 0; j < yblen; j++)
+ for(int p = 0; i < xblen; i++)
+ block.Set(p, q, (mv.dc[k]));
+ }
+ if(k != 0 && !mv.namespace_global)
+ mv = mv.Scale(chroma_h_shift, chroma_v_shift);
+ for(int q = 0; q < yblen; q++) {
+ int y = ystart + q;
+ if(y < 0 || y > b.s.Height - 1) continue;
+ for(int p = 0; p < xblen; p++) {
+ int x = xstart + p;
+ if(x < 0 || x > b.s.Width - 1) continue;
+ block.Set(p,q, PredictPixel(mv, x, y, k));
+ }
+ }
+ }
+
+ private short PredictPixel(Vector mv, int x, int y, int k) {
+ if(mv.namespace_global) {
+ for(int i = 0; i < par.num_refs; i++) {
+ par.global[i].GetVector(mv, x, y, i);
+ }
+ if(k != 0)
+ mv = mv.Scale(chroma_h_shift, chroma_v_shift);
+ }
+ short weight = (short)(par.picture_weight_1 + par.picture_weight_2);
+ short val = 0;
+ int px, py;
+ switch(mv.pred_mode) {
+ case 1:
+ px = (x << par.mv_precision) + mv.dx[0];
+ py = (y << par.mv_precision) + mv.dy[0];
+ val = (short)(weight*PredictSubPixel(0, px, py));
+ break;
+ case 2:
+ px = (x << par.mv_precision) + mv.dx[1];
+ py = (y << par.mv_precision) + mv.dy[1];
+ val = (short)(weight*PredictSubPixel(1, px, py));
+ break;
+ case 3:
+ px = (x << par.mv_precision) + mv.dx[0];
+ py = (y << par.mv_precision) + mv.dy[0];
+ val = (short)(par.picture_weight_1*PredictSubPixel(0, px, py));
+ px = (x << par.mv_precision) + mv.dx[1];
+ py = (x << par.mv_precision) + mv.dy[1];
+ val += (short)(par.picture_weight_2*PredictSubPixel(1, px, py));
+ break;
+ default:
+ break;
+ }
+ return (short)Util.RoundShift(val, par.picture_weight_bits);
+ }
+
+ private short PredictSubPixel(int reference, int px, int py) {
+ if(par.mv_precision < 2) {
+ return tmp_ref[reference].Real(px, py);
+ }
+ int prec = par.mv_precision;
+ int add = 1 << (prec - 1);
+ int hx = px >> (prec-1);
+ int hy = py >> (prec-1);
+ int rx = px - (hx << (prec-1));
+ int ry = py - (hy << (prec-1));
+ int w00,w01, w10, w11;
+ w00 = (add - rx)*(add - ry);
+ w01 = (add - rx)*ry;
+ w10 = rx*(add - ry);
+ w11 = rx*ry;
+ int val = w00*tmp_ref[reference].Real(hx, hy) +
+ w01*tmp_ref[reference].Real(hx + 1, hy) +
+ w10*tmp_ref[reference].Real(hx, hy + 1) +
+ w11*tmp_ref[reference].Real(hx + 1, hy + 1);
+ return (short)((val + (1 << (2*prec-3))) >> (2*prec - 2));
+ }
+
+
+ private void AccumulateBlock(Block b, int x, int y) {
+ if(!Edge(x,y)) {
+ for(int q = 0; q < yblen; q++) {
+ if(q + y < 0 || q + y >= b.s.Height) continue;
+ int outLine = b.Index(x, y + q);
+ int inLine = block.Line(q);
+ for(int p = 0; p < xblen; p++) {
+ if(p + x < 0 || p + x >= b.s.Width) continue;
+ b.d[outLine + p] +=
+ (short)(weight_x[p]*weight_y[q]*block.d[inLine+p]);
+ }
+ }
+ } else {
+ int w_x, w_y;
+ for(int q = 0; q < yblen; q++) {
+ if(q + y < 0 || q + y >= b.s.Height) continue;
+ if((y < 0 && q < 2*yoffset) ||
+ (y >= par.y_num_blocks*ybsep - yoffset &&
+ yblen - 1 - q < 2*yoffset))
+ w_y = 8;
+ else
+ w_y = weight_y[q];
+ int outLine = b.Index(x, y + q);
+ int inLine = block.Line(q);
+ for(int p = 0; p < xblen; p++) {
+ if(p + x < 0 || p + x >= b.s.Width) continue;
+ if((x < 0 && p < 2*xoffset) ||
+ (x >= par.x_num_blocks*xbsep - xoffset &&
+ xblen - 1 - p < 2*xoffset))
+ w_x = 8;
+ else
+ w_x = weight_x[p];
+ b.d[outLine + p] +=
+ (short)(w_x*w_y*block.d[inLine+p]);
+ }
+ }
+
+ }
+
+ }
+
+ private bool Edge(int x, int y) {
+ return (x < 0 || x >= par.x_num_blocks*xbsep - xoffset)
+ || (y < 0 || y >= par.y_num_blocks*ybsep - yoffset);
+
+ }
+
+ private int SplitPrediction(int x, int y) {
+ if(y == 0) {
+ if(x == 0) {
+ return 0;
+ } else {
+ return vecs[x-4].split;
+ }
+ } else {
+ if(x == 0) {
+ return GetVector(0, y - 4).split;
+ } else {
+ int sum = 0;
+ sum += GetVector(x, y - 4).split;
+ sum += GetVector(x - 4, y).split;
+ sum += GetVector(x - 4, y - 4).split;
+ return (sum+1)/3;
+ }
+ }
+ }
+
+ private int ModePrediction(int x, int y) {
+ if(y == 0) {
+ if(x == 0) {
+ return 0;
+ } else {
+ return vecs[x - 1].pred_mode;
+ }
+ } else {
+ if(x == 0) {
+ return GetVector(0, y - 1).pred_mode;
+ } else {
+ int a,b,c;
+ a = GetVector(x - 1, y).pred_mode;
+ b = GetVector(x, y - 1).pred_mode;
+ c = GetVector(x - 1, y - 1).pred_mode;
+ return (a&b)|(b&c)|(c&a);
+ }
+ }
+ }
+
+ private int GlobalPrediction(int x, int y) {
+ if(x == 0 && y == 0) {
+ return 0;
+ }
+ if(y == 0) {
+ return vecs[x-1].namespace_global ? 1 : 0;
+ }
+ if(x == 0) {
+ return GetVector(0, y-1).namespace_global ? 1 : 0;
+ }
+ int sum = 0;
+ sum += GetVector(x - 1, y).namespace_global ? 1 : 0;
+ sum += GetVector(x, y - 1).namespace_global ? 1 : 0;
+ sum += GetVector(x - 1, y - 1).namespace_global ? 1 : 0;
+ return (sum >= 2) ? 1 : 0;
+ }
+
+ private void VectorPrediction(Vector mv, int x, int y, int mode) {
+ int n = 0;
+ int[] vx = new int[3];
+ int[] vy = new int[3];
+ if(x > 0) {
+ Vector ov = GetVector(x-1, y);
+ if(!ov.namespace_global &&
+ (ov.pred_mode & mode) != 0) {
+ vx[n] = ov.dx[mode-1];
+ vy[n] = ov.dx[mode-1];
+ n++;
+ }
+ }
+ if(y > 0) {
+ Vector ov = GetVector(x, y-1);
+ if(!ov.namespace_global &&
+ (ov.pred_mode & mode) != 0) {
+ vx[n] = ov.dx[mode-1];
+ vy[n] = ov.dx[mode-1];
+ n++;
+ }
+ }
+ if(x > 0 && y > 0) {
+ Vector ov = GetVector(x - 1, y - 1);
+ if(!ov.namespace_global &&
+ (ov.pred_mode & mode) != 0) {
+ vx[n] = ov.dx[mode-1];
+ vy[n] = ov.dy[mode-1];
+ n++;
+ }
+ }
+ switch(n) {
+ case 0:
+ mv.dx[mode-1] = 0;
+ mv.dy[mode-1] = 0;
+ break;
+ case 1:
+ mv.dx[mode-1] = vx[0];
+ mv.dy[mode-1] = vy[0];
+ break;
+ case 2:
+ mv.dx[mode-1] = (vx[0] + vx[1] + 1) >> 1;
+ mv.dy[mode-1] = (vy[0] + vy[1] + 1) >> 1;
+ break;
+ case 3:
+ mv.dx[mode-1] = Util.Median(vx);
+ mv.dy[mode-1] = Util.Median(vy);
+ break;
+ }
+ }
+
+ private void DcPrediction(int x, int y, int[] pred) {
+ for(int i = 0; i < 3; i++) {
+ int sum = 0, n = 0;
+ if(x > 0) {
+ Vector ov = GetVector(x - 1, y);
+ if(ov.pred_mode == 0) {
+ sum += ov.dc[i];
+ n++;
+ }
+ }
+ if(y > 0) {
+ Vector ov = GetVector(x, y - 1);
+ if(ov.pred_mode == 0) {
+ sum += ov.dc[i];
+ n++;
+ }
+ }
+ if(x > 0 && y > 0) {
+ Vector ov = GetVector(x - 1, y - 1);
+ if(ov.pred_mode == 0) {
+ sum += ov.dc[i];
+ n++;
+ }
+ }
+ switch(n) {
+ case 0:
+ pred[i] = 0;
+ break;
+ case 1:
+ pred[i] = sum;
+ break;
+ case 2:
+ pred[i] = (sum+1)>>1;
+ break;
+ case 3:
+ pred[i] = (sum+1)/3;
+ break;
+ }
+ }
+ }
+
+ private Vector GetVector(int x, int y) {
+ int pos = x + y*par.x_num_blocks;
+ if(vecs[pos] == null) {
+ vecs[pos] = new Vector();
+ }
+ return vecs[pos];
+ }
+
+ private void SetVector(Vector mv, int x, int y) {
+ vecs[x + y*par.x_num_blocks] = mv;
+ }
+
+ }
+
+}