Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
initial checkin.
Browse files Browse the repository at this point in the history
svn path=/trunk/mooncodecs/; revision=133146
  • Loading branch information
atsushieno committed Apr 30, 2009
0 parents commit 6dc3021
Show file tree
Hide file tree
Showing 102 changed files with 9,174 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
@@ -0,0 +1,3 @@
2009-04-30 Atsushi Enomoto <atsushi@ximian.com>

Initial checkin.
9 changes: 9 additions & 0 deletions Makefile
@@ -0,0 +1,9 @@
all:
(cd csvorbis; make -f Makefile.moon) || exit
(cd csadpcm; make) || exit
(cd csdirac; make) || exit

clean:
(cd csvorbis; make -f Makefile.moon clean) || exit
(cd csadpcm; make clean) || exit
(cd csdirac; make clean) || exit
15 changes: 15 additions & 0 deletions README
@@ -0,0 +1,15 @@
This is a collection of managed custom media codecs for Moonlight 2.0
and Silverlight 3.0 (both not at final release in April 2009 yet.)

There are three codecs so far:

- Ogg Vorbis, based on csvorbis by Marc Crichton.
http://anonsvn.mono-project.com/viewvc/trunk/csvorbis/
- Dirac, based on csdirac by Olivier Dufour.
http://anonsvn.mono-project.com/viewvc/trunk/csdirac/
- IMA ADPCM, based on the work by n7shi
( http://d.hatena.ne.jp/n7shi/20090329/1238322010 )

For moonlight custom media support details, see:
http://veritas-vos-liberabit.com/monogatari/2009/03/moonvorbis.html

28 changes: 28 additions & 0 deletions csadpcm/Makefile
@@ -0,0 +1,28 @@

ADPCM_SRC = \
csadpcm/ImaAdpcm.cs \
MoonAdpcm/AdpcmMediaStreamSource.cs \
MoonAdpcm/WaveFormatExtensible.cs \
MoonAdpcm/StringExtensions.cs

ADPCM_TEST_SRC = \
MoonAdpcmTest/App.xaml \
MoonAdpcmTest/App.xaml.cs \
MoonAdpcmTest/Page.xaml \
MoonAdpcmTest/Page.xaml.cs

all: build/MoonAdpcmTest.xap

build/MoonAdpcmTest.xap: build/MoonAdpcm.dll $(ADPCM_TEST_SRC)
cp MoonAdpcmTest/*.xaml build
cp MoonAdpcmTest/*.xaml.cs build
cp MoonAdpcmTest/Properties/AppManifest.xml build/AppManifest.xaml
cd build; mxap --application-name=MoonAdpcmTest; cd ..

build/MoonAdpcm.dll: $(ADPCM_SRC)
smcs -t:library -out:MoonAdpcm.dll -debug $(ADPCM_SRC)
mv MoonAdpcm.dll build
mv MoonAdpcm.dll.mdb build

clean:
rm build/*
176 changes: 176 additions & 0 deletions csadpcm/MoonAdpcm/AdpcmMediaStreamSource.cs
@@ -0,0 +1,176 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Windows.Media;
using MediaParsers;
using csadpcm;

namespace MoonAdpcm
{
class DebugWriter : TextWriter
{
public override Encoding Encoding
{
get { return Encoding.UTF8; }
}

public override void WriteLine ()
{
Debug.WriteLine (String.Empty);
}

public override void WriteLine (string s)
{
Debug.WriteLine (s);
}
}

public class AdpcmMediaStreamSource : MediaStreamSource
{
ImaAdpcm source;

// int convsize=4096*2;
//byte[] convbuffer=new byte[convsize]; // take 8k out of the data segment, not the stack
// byte[] convbuffer;

TextWriter s_err = new DebugWriter(); // Console.Error;
// Stream input;

public AdpcmMediaStreamSource (Stream input)
{
/*
convbuffer=new byte[convsize];
vb = new Block (vd);
if (input == null)
throw new ArgumentNullException ("input");
this.input = input;
*/
source = new ImaAdpcm (input);
}

IEnumerable<SampleBuffer> DecodeSamples ()
{
foreach (byte [] buf in source.DecodeSamples ())
yield return new SampleBuffer (buf, 0, buf.Length);
}

struct SampleBuffer
{
byte [] buf;
int index, count;
public SampleBuffer (byte [] buf, int index, int count)
{
this.buf = buf;
this.index = index;
this.count = count;
}

public byte [] Data { get { return buf; } }
public int Index { get { return index; } }
public int Count { get { return count; } }
}

protected override void CloseMedia ()
{
this.sample_enumerator = null;
s_err.WriteLine("Done.");
}

// FIXME: should be implemented, but can be done later.
protected override void SwitchMediaStreamAsync (MediaStreamDescription mediaStreamDescription)
{
throw new System.NotImplementedException();
// ReportSwitchMediaStreamCompleted (mediaStreamDescription);
}

// FIXME: should be implemented, but looks like it can be left as is
// just to run decoder so far.
protected override void SeekAsync (long seekToTime)
{
ReportSeekCompleted (seekToTime);
}

MediaStreamDescription audioStreamDescription;

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>();

// Initialize the Mp3 data structures used by the Media pipeline with state from the first frame.
WaveFormatExtensible wfx = new WaveFormatExtensible();
wfx.FormatTag = 1; // PCM
wfx.Channels = (short) this.source.Channels;
wfx.SamplesPerSec = this.source.SamplesPerSec;
wfx.BlockAlign = (short) (this.source.Channels * 2);
wfx.BitsPerSample = 16;
wfx.AverageBytesPerSecond = wfx.SamplesPerSec * wfx.Channels * 2;
wfx.Size = 0;

mediaStreamAttributes[MediaStreamAttributeKeys.CodecPrivateData] = wfx.ToHexString();
this.audioStreamDescription = new MediaStreamDescription(MediaStreamType.Audio, mediaStreamAttributes);

mediaStreamDescriptions.Add(this.audioStreamDescription);

// <note>This part is mere copy of Mp3MediaStreamSource:
// 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(0).Ticks.ToString (CultureInfo.InvariantCulture);
mediaSourceAttributes[MediaSourceAttributesKeys.CanSeek] = false.ToString ();
// </note>

sample_enumerator = this.DecodeSamples ().GetEnumerator ();
this.ReportOpenMediaCompleted(mediaSourceAttributes, mediaStreamDescriptions);
}

Dictionary<MediaSampleAttributeKeys, string> emptyDict = new Dictionary<MediaSampleAttributeKeys, string>();
IEnumerator<SampleBuffer> sample_enumerator;

protected override void GetSampleAsync (MediaStreamType mediaStreamType)
{
MediaStreamSample audioSample = null;

if (!sample_enumerator.MoveNext ())
{
// If you are near the end of the file, return a null stream, which
// tells the MediaStreamSource and MediaElement to close down.
audioSample = new MediaStreamSample(
this.audioStreamDescription,
null,
0,
0,
0,
emptyDict);
this.ReportGetSampleCompleted(audioSample);
}
else
{
// FIXME: Stream should not be created every time.
SampleBuffer buf = (SampleBuffer) sample_enumerator.Current;
audioSample = new MediaStreamSample(
this.audioStreamDescription,
new MemoryStream (buf.Data, buf.Index, buf.Count, false),
buf.Index,
buf.Count,
timePosition,
emptyDict);
timePosition += buf.Count * 10000000 / (44100 * 2 * 2);
this.ReportGetSampleCompleted(audioSample);
}
}
long timePosition;

// FIXME: should be implemented, but can be done later.
protected override void GetDiagnosticAsync (MediaStreamSourceDiagnosticKind diagnosticKind)
{
throw new System.NotImplementedException();
// ReportGetDiagnosticCompleted(diagnosticKind, diagnosticValue);
}
}
}
59 changes: 59 additions & 0 deletions csadpcm/MoonAdpcm/MoonAdpcm.csproj
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{A4D1AB0C-25BA-4E7D-9510-76BB4BA1D78B}</ProjectGuid>
<ProjectTypeGuids>{A1591282-1198-4647-A2B1-27E5FF5F6F3B};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<SilverlightApplication>false</SilverlightApplication>
<ValidateXaml>true</ValidateXaml>
<ThrowErrorsInValidation>false</ThrowErrorsInValidation>
<AssemblyName>AdpcmMediaStreamSource</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System.Windows" />
<Reference Include="mscorlib" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml" />
<Reference Include="System.Net" />
<Reference Include="System.Windows.Browser" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight\v2.0\Microsoft.Silverlight.CSharp.targets" />
<ProjectExtensions>
<MonoDevelop>
<Properties InternalTargetFrameworkVersion="2.1" />
</MonoDevelop>
</ProjectExtensions>
<ItemGroup>
<Compile Include="AdpcmMediaStreamSource.cs" />
<Compile Include="StringExtensions.cs" />
<Compile Include="WaveFormatExtensible.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\csadpcm\csadpcm.moonlight.csproj">
<Project>{1F3AC0CD-1994-4D37-BF94-C2359C3B5E68}</Project>
<Name>csadpcm.moonlight</Name>
</ProjectReference>
</ItemGroup>
</Project>
101 changes: 101 additions & 0 deletions csadpcm/MoonAdpcm/StringExtensions.cs
@@ -0,0 +1,101 @@
//-----------------------------------------------------------------------
// <copyright file="StringExtensions.cs" company="Larry Olson">
// (c) Copyright Larry Olson.
// This source is subject to the Microsoft Public License (Ms-PL)
// See http://code.msdn.microsoft.com/ManagedMediaHelpers/Project/License.aspx
// All other rights reserved.
// </copyright>
//-----------------------------------------------------------------------

// Supressing Code Analysis rule(s)
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes",
Scope = "namespace",
Target = "ExtensionMethods",
Justification = "This is appropriate as a separate namespace because it logically is separate from the ManagedMediaParsers namespace.")]

[module: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes",
Scope = "member",
Target = "ExtensionMethods.StringExtensions.#ToLittleEndian(System.String)",
Justification = "This is appropriate to make this method look like a first class member of string.")]

namespace ExtensionMethods
{
using System;

/// <summary>
/// Extensions for the standard string class.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// <para>
/// Converts a string of characters from Big Endian byte order to
/// Little Endian byte order.
/// </para>
/// <para>
/// Assumptions this makes about the string. Every two characters
/// make up the smallest data unit (analogous to byte). The entire
/// string is the size of the systems natural unit of data (analogous
/// to a word).
/// </para>
/// </summary>
/// <param name="value">
/// A string in Big Endian Byte order.
/// </param>
/// <returns>
/// A string in Little Endian Byte order.
/// </returns>
/// <remarks>
/// This function was designed to take in a Big Endian string of
/// hexadecimal digits.
/// <example>
/// input:
/// DEADBEEF
/// output:
/// EFBEADDE
/// </example>
/// </remarks>
public static string ToLittleEndian(this string value)
{
// Guard
if (value == null)
{
throw new NullReferenceException();
}

char[] bigEndianChars = value.ToCharArray();

// Guard
if (bigEndianChars.Length % 2 != 0)
{
return string.Empty;
}

int i, ai, bi, ci, di;
char a, b, c, d;

for (i = 0; i < bigEndianChars.Length / 2; i += 2)
{
// front byte ( in hex )
ai = i;
bi = i + 1;

// back byte ( in hex )
ci = bigEndianChars.Length - 2 - i;
di = bigEndianChars.Length - 1 - i;

a = bigEndianChars[ai];
b = bigEndianChars[bi];
c = bigEndianChars[ci];
d = bigEndianChars[di];

bigEndianChars[ci] = a;
bigEndianChars[di] = b;
bigEndianChars[ai] = c;
bigEndianChars[bi] = d;
}

return new string(bigEndianChars);
}
}
}

0 comments on commit 6dc3021

Please sign in to comment.