Skip to content

Instantly share code, notes, and snippets.

@ForeverZer0
Created January 25, 2020 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ForeverZer0/1d64bb73b8f6977e7ba99f8f1c0dcc7c to your computer and use it in GitHub Desktop.
Save ForeverZer0/1d64bb73b8f6977e7ba99f8f1c0dcc7c to your computer and use it in GitHub Desktop.
Minimal bindings of the libogg reference library.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Security;
// Referernce Source: https://xiph.org/vorbis/doc/libvorbis/reference.html
namespace OGG
{
[SuppressUnmanagedCodeSecurity]
[SuppressMessage("ReSharper", "IdentifierTypo")]
[SuppressMessage("ReSharper", "ConvertToAutoProperty")]
[SuppressMessage("ReSharper", "InconsistentNaming")]
[SuppressMessage("ReSharper", "FieldCanBeMadeReadOnly.Local")]
internal static unsafe class libogg
{
private const string LIBRARY = "ogg";
#region Data Structures
[StructLayout(LayoutKind.Sequential)]
public struct oggpack_buffer
{
private int endbyte;
private int endbit;
private byte *buffer;
private byte *ptr;
private int storage;
}
[StructLayout(LayoutKind.Sequential)]
public struct ogg_page
{
private byte *header;
private int header_len;
private byte *body;
private int body_len;
}
[StructLayout(LayoutKind.Sequential)]
public struct ogg_stream_state
{
private byte* body_data;
private int body_storage;
private int body_fill;
private int body_returned;
private int* lacing_vals;
private long* ganule_vals;
private int lacing_storage;
private int lacing_fill;
private int lacing_packet;
private int lacing_returned;
private fixed byte header[282];
private int header_fill;
private int e_o_s;
private int b_o_s;
private int serialno;
private int paheno;
private long packetno;
private long granulepos;
}
[StructLayout(LayoutKind.Sequential)]
public struct ogg_packet
{
private byte *packet;
private int bytes;
private int b_o_s;
private int e_o_s;
private long granulepos;
private long packetno;
}
[StructLayout(LayoutKind.Sequential)]
public struct ogg_sync_state
{
private byte *data;
private int storage;
private int fill;
private int returned;
private int unsynced;
private int headerbytes;
private int bodybytes;
}
[StructLayout(LayoutKind.Sequential)]
public struct ogg_iovec_t
{
void *iov_base;
UIntPtr iov_len;
}
#endregion
#region Bitpacking
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern void oggpack_writeinit(out oggpack_buffer b);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern void oggpack_readinit(out oggpack_buffer b, byte[] buf, int bytes);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int oggpack_writecheck(ref oggpack_buffer b);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern void oggpack_reset(ref oggpack_buffer b);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern void oggpack_writeclear(ref oggpack_buffer b);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern void oggpack_write(ref oggpack_buffer b, uint value, int bits);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int oggpack_look(ref oggpack_buffer b, int bits);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int oggpack_look1(ref oggpack_buffer b);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern void oggpack_adv(ref oggpack_buffer b, int bits);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern void oggpack_adv1(ref oggpack_buffer b);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int oggpack_read(ref oggpack_buffer b, int bits);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int oggpack_read1(ref oggpack_buffer b);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int oggpack_bytes(ref oggpack_buffer b);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int oggpack_bits(ref oggpack_buffer b);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern byte *oggpack_get_buffer(ref oggpack_buffer b);
#endregion
#region General
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_stream_init(out ogg_stream_state os, out int serialno);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_stream_destroy(ref ogg_stream_state os);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_stream_clear(ref ogg_stream_state os);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_stream_reset(ref ogg_stream_state os);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_stream_check(ref ogg_stream_state os);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_stream_eos(ref ogg_stream_state os);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_stream_reset_serialno(ref ogg_stream_state state, int serialno);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_page_version(ogg_page *og);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_page_continued(ogg_page *og);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_page_packets(ogg_page *og);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_page_bos(ogg_page *og);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_page_eos(ogg_page *og);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern long ogg_page_granulepos(ogg_page *og);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_page_serialno(ogg_page *og);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_page_pageno(ogg_page *og);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern void ogg_packet_clear(ogg_packet *op);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_page_checksum_set(ogg_page *og);
#endregion
#region Encoding
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_stream_packetin(ogg_stream_state *os,ogg_packet *op);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_stream_iovecin(ogg_stream_state *os, ogg_iovec_t *iov, int count, long e_o_s, long granulepos);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_stream_pageout(ogg_stream_state *os, ogg_page *og);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_stream_pageout_fill(ogg_stream_state *os, ogg_page *og, int fillbytes);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_stream_flush(ogg_stream_state *os, ogg_page *og);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_stream_flush_fill(ogg_stream_state *os, ogg_page *og, int fillbytes);
#endregion
#region Decoding
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_sync_init(ogg_sync_state *oy);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_sync_clear(ogg_sync_state *oy);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_sync_reset(ogg_sync_state *oy);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_sync_destroy(ogg_sync_state *oy);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_sync_check(ogg_sync_state *oy);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern byte *ogg_sync_buffer(ogg_sync_state *oy, int size);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_sync_wrote(ogg_sync_state *oy, int bytes);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_sync_pageseek(ogg_sync_state *oy, ogg_page *og);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_sync_pageout(ogg_sync_state *oy, ogg_page *og);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_stream_pagein(ogg_stream_state *os, ogg_page *og);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_stream_packetout(ogg_stream_state *os, ogg_packet *op);
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
public static extern int ogg_stream_packetpeek(ogg_stream_state *os, ogg_packet *op);
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment