1
by Hilaire Fernandes
Initial additions |
1 |
/* header for OggPlugin */
|
2 |
||
3 |
#include <vorbis/codec.h> |
|
4 |
#include <vorbis/vorbisenc.h> |
|
5 |
#include <speex/speex.h> |
|
6 |
#include <stdio.h> |
|
7 |
||
8 |
/* Result status */
|
|
9 |
#define SQ_OGG_SUCCESS 0
|
|
10 |
#define SQ_OGG_NEED_MORE -1
|
|
11 |
#define SQ_OGG_ERROR -2
|
|
12 |
#define SQ_OGG_ERROR_HEADER -3
|
|
13 |
#define SQ_OGG_ERROR_BODY -4
|
|
14 |
||
15 |
/* Hi level codec state */
|
|
16 |
#define SQ_OGG_INITIALIZED 1
|
|
17 |
#define SQ_VORBIS_GOT_INFO 2
|
|
18 |
#define SQ_SPEEX_GOT_INFO 2
|
|
19 |
#define SQ_VORBIS_GOT_COMMENT 3
|
|
20 |
#define SQ_OGG_RUNNING 4 /* Codec header is successfully processed */ |
|
21 |
||
22 |
/* Low level packet state */
|
|
23 |
#define SQ_STREAM_UNINITIALIZED 0
|
|
24 |
#define SQ_STREAM_BEGAN 1 /* b_o_f was written in encoding */ |
|
25 |
#define SQ_STREAM_INITIALIZED 2 /* serial number was gotten in decoding */ |
|
26 |
||
27 |
/* Open mode */
|
|
28 |
#define SQ_OGG_ENCODE 1
|
|
29 |
#define SQ_OGG_DECODE 2
|
|
30 |
#define SQ_VORBIS 4
|
|
31 |
#define SQ_SPEEX 8
|
|
32 |
||
33 |
typedef void * SpeexState; |
|
34 |
||
35 |
/* a buffer for output of the result */
|
|
36 |
typedef struct _SqOggResult { |
|
37 |
char * buffer; |
|
38 |
size_t size; /* size of the buffer */ |
|
39 |
struct _SqOggResult * next; /* next buffer, or NULL */ |
|
40 |
} SqOggResult; |
|
41 |
||
42 |
/* Speex codec object */
|
|
43 |
typedef struct _SqSpeex { |
|
44 |
SpeexBits bits; |
|
45 |
SpeexState state; |
|
46 |
ogg_int16_t remain[1024]; /* remain sound buffer */ |
|
47 |
int remainSize; /* remain sound buffer size in frames */ |
|
48 |
} SqSpeex; |
|
49 |
||
50 |
/* Vorbis codec object */
|
|
51 |
typedef struct _SqVorbis { |
|
52 |
vorbis_info vinfo; |
|
53 |
vorbis_comment comment; |
|
54 |
||
55 |
vorbis_dsp_state dsp_state; |
|
56 |
vorbis_block block; |
|
57 |
} SqVorbis; |
|
58 |
||
59 |
/* Encoder | Decoder status object */
|
|
60 |
typedef struct _SqOgg { |
|
61 |
int mode; /* ogg | vorbis + decode | encode */ |
|
62 |
||
63 |
ogg_sync_state sync_state; |
|
64 |
ogg_stream_state stream_state; |
|
65 |
ogg_page page; /* the current proccessing page */ |
|
66 |
void (* page_callback) (ogg_page *); |
|
67 |
||
68 |
int channels; |
|
69 |
int rate; |
|
70 |
float quality; |
|
71 |
||
72 |
int packetStatus; |
|
73 |
int state; |
|
74 |
||
75 |
SqVorbis * vorbis; |
|
76 |
SqSpeex * speex; |
|
77 |
||
78 |
SqOggResult * resultList; |
|
79 |
} SqOgg; |
|
80 |
||
81 |
/* SqOggPtr is used to pass SqOgg pointer to Squeak image. The
|
|
82 |
* structure is copied as a ByteArray object so that the ByteArray
|
|
83 |
* keeps a pair of {sessionID, ogg}. SqOggPtr is checked the
|
|
84 |
* sessionID whether if the pointer is valid in current session when
|
|
85 |
* it is called to avoid segfault error.
|
|
86 |
*/
|
|
87 |
typedef struct _SqOggPtr { |
|
88 |
int sessionID; |
|
89 |
SqOgg * ogg; |
|
90 |
} SqOggPtr; |
|
91 |
||
92 |
/* Basic buffer oparations for Ogg Vorbis | Speex */
|
|
93 |
void SqOggOpen(int mode, SqOggPtr * oggp); |
|
94 |
int SqOggWrite(SqOggPtr * ogg, const char * buffer, size_t bytes); |
|
95 |
size_t SqOggReadSize(SqOggPtr * ogg); |
|
96 |
size_t SqOggRead(SqOggPtr * ogg, char * dest, size_t bytes); |
|
97 |
void SqOggWriteEOS(SqOggPtr * ogg); |
|
98 |
void SqOggClose(SqOggPtr * oggp); |
|
99 |
||
100 |
/* Accessors */
|
|
101 |
int SqOggGetChannels(SqOggPtr * ogg); |
|
102 |
void SqOggSetChannels(SqOggPtr * ogg, int channels); |
|
103 |
int SqOggGetRate(SqOggPtr * ogg); |
|
104 |
void SqOggSetRate(SqOggPtr * ogg, int rate); |
|
105 |
||
106 |
int SqOggGetVendor(SqOggPtr * ogg, char * dest, size_t size); |
|
107 |
int SqOggGetCommentSize(SqOggPtr * ogg); |
|
108 |
int SqOggGetComment(SqOggPtr * ogg, char * dest, size_t size); |
|
109 |
void SqOggSetQuality(SqOggPtr * ogg, float quality); |
|
110 |
int SqOggGetState(SqOggPtr * ogg); |
|
111 |
||
112 |
/* Low level ogg interface */
|
|
113 |
void SqOggPacketWrite(SqOggPtr * ogg, ogg_packet * packet); |
|
114 |
void SqOggPacketWriteEOS(SqOggPtr * ogg); |
|
115 |
int SqOggPacketNext(SqOggPtr * ogg, ogg_packet * packet); |
|
116 |
void SqOggPacketFlush(SqOggPtr * ogg); |
|
117 |
int SqOggPacketRead(SqOggPtr * ogg, ogg_packet * packet); |
|
118 |
||
119 |
/* Result buffer (only for unit test) */
|
|
120 |
SqOggResult * SqOggResultNewBuffer(SqOggResult * head, int size); |
|
121 |
void SqOggResultDestroy(SqOggResult * head); |
|
122 |
int SqOggResultCopy(SqOggResult * head, char * dest, int size); |
|
123 |
void SqOggResultWrite(SqOggPtr * ogg, FILE * file); |
|
124 |
int SqOggResultSize(SqOggResult * head); |
|
125 |
||
126 |
/* Low level speex interface. These are available for future use of
|
|
127 |
* speex directly (speex is not depend on ogg container).
|
|
128 |
*/
|
|
129 |
SqSpeex * SqSpeexDecoder(ogg_packet * packet); |
|
130 |
int SqSpeexDecodeWrite(SqSpeex * speex, char * buffer, size_t size); |
|
131 |
void SqSpeexDecodeRead(SqSpeex * speex, char * buffer); |