Added a WAV file encoder
[platform/upstream/gst-plugins-good.git] / gst / wavenc / gstwavenc.c
1 /* GStreamer
2  * Copyright (C) <2002> Iain Holmes <iain@prettypeople.org>
3  */
4
5 #include <string.h>
6 #include <gstwavenc.h>
7
8 static void gst_wavenc_class_init (GstWavEncClass *klass);
9 static void gst_wavenc_init (GstWavEnc *wavenc);
10 static void gst_wavenc_chain (GstPad *pad, GstBuffer *buf);
11
12 #define WAVE_FORMAT_PCM 0x0001
13
14 #define WRITE_U32(buf, x) *(buf) = (unsigned char) (x&0xff);\
15 *((buf)+1) = (unsigned char)((x>>8)&0xff);\
16 *((buf)+2) = (unsigned char)((x>>16)&0xff);\
17 *((buf)+3) = (unsigned char)((x>>24)&0xff);
18
19 #define WRITE_U16(buf, x) *(buf) = (unsigned char) (x&0xff);\
20 *((buf)+1) = (unsigned char)((x>>8)&0xff);
21
22 struct riff_struct {
23   unsigned char id[4]; /* RIFF */
24   unsigned int len;
25   unsigned char wav_id[4]; /* WAVE */
26 };
27
28 struct chunk_struct {
29   unsigned char id[4];
30   unsigned int len;
31 };
32
33 struct common_struct {
34   unsigned short wFormatTag;
35   unsigned short wChannels;
36   unsigned int dwSamplesPerSec;
37   unsigned int dwAvgBytesPerSec;
38   unsigned short wBlockAlign;
39   unsigned short wBitsPerSample; /* Only for PCM */
40 };
41
42 struct wave_header {
43   struct riff_struct riff;
44   struct chunk_struct format;
45   struct common_struct common;
46   struct chunk_struct data;
47 };
48
49 static GstElementDetails gst_wavenc_details = {
50   "WAV encoder",
51   "Codec/Encoder",
52   "GPL",
53   "Encode raw audio into WAV",
54   VERSION,
55   "Iain Holmes <iain@prettypeople.org>",
56   "(C) 2002",
57 };
58
59 static GstPadTemplate *srctemplate, *sinktemplate;
60
61 static GstPadTemplate *
62 sink_factory (void)
63 {
64   return gst_pad_template_new ("wavenc_sink",
65                                GST_PAD_SINK,
66                                GST_PAD_ALWAYS,
67                                gst_caps_new ("wavenc_raw",
68                                              "audio/raw",
69                                              gst_props_new ("format", GST_PROPS_STRING ("int"),
70                                                             "law", GST_PROPS_INT (0),
71                                                             "endianness", GST_PROPS_INT (G_BYTE_ORDER),
72                                                             "signed", GST_PROPS_BOOLEAN (TRUE),
73                                                             "width", GST_PROPS_INT (16),
74                                                             "depth", GST_PROPS_INT (16),
75                                                             "rate", GST_PROPS_INT_RANGE (8000, 48000),
76                                                             "channels", GST_PROPS_INT_RANGE (1, 2),
77                                                             NULL)),
78                                NULL);
79 }
80
81 static GstPadTemplate *
82 src_factory (void)
83 {
84   return gst_pad_template_new ("wavenc_src",
85                                GST_PAD_SRC,
86                                GST_PAD_ALWAYS,
87                                gst_caps_new ("wavenc_wav",
88                                              "audio/x-wav",
89                                              NULL),
90                                NULL);
91 }
92
93 static GstElementClass *parent_class = NULL;
94
95 GType
96 gst_wavenc_get_type (void)
97 {
98   static GType type = 0;
99
100   if (type == 0) {
101     static const GTypeInfo info = {
102       sizeof (GstWavEncClass), NULL, NULL,
103       (GClassInitFunc) gst_wavenc_class_init, NULL, NULL,
104       sizeof (GstWavEnc), 0, (GInstanceInitFunc) gst_wavenc_init
105     };
106
107     type = g_type_register_static (GST_TYPE_ELEMENT, "GstWavEnc", &info, 0);
108   }
109
110   return type;
111 }
112
113 static GstElementStateReturn
114 gst_wavenc_change_state (GstElement *element)
115 {
116   GstWavEnc *wavenc = GST_WAVENC (element);
117   
118   switch (GST_STATE_TRANSITION (element)) {
119   case GST_STATE_NULL_TO_READY:
120   case GST_STATE_READY_TO_PAUSED:
121     wavenc->setup = FALSE;
122     break;
123
124   default:
125     break;
126   }
127
128   if (parent_class->change_state) {
129     return parent_class->change_state (element);
130   }
131
132   return GST_STATE_SUCCESS;
133 }
134
135 static void
136 gst_wavenc_class_init (GstWavEncClass *klass)
137 {
138   GstElementClass *element_class;
139
140   element_class = (GstElementClass *) klass;
141   element_class->change_state = gst_wavenc_change_state;
142
143   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
144 }
145
146 static gboolean
147 gst_wavenc_setup (GstWavEnc *wavenc)
148 {
149   struct wave_header wave;
150   int size = 0x7fffffff; /* Use a bogus size initially */
151
152   wave.common.wChannels = wavenc->channels;
153   wave.common.wBitsPerSample = wavenc->bits;
154   wave.common.dwSamplesPerSec = wavenc->rate;
155
156   memset (wavenc->header, 0, WAV_HEADER_LEN);
157
158   /* Fill out our wav-header with some information */
159   strncpy (wave.riff.id, "RIFF", 4);
160   wave.riff.len = size - 8;
161   strncpy (wave.riff.wav_id, "WAVE", 4);
162
163   strncpy (wave.format.id, "fmt ", 4);
164   wave.format.len = 16;
165
166   wave.common.wFormatTag = WAVE_FORMAT_PCM;
167   wave.common.dwAvgBytesPerSec = wave.common.wChannels * wave.common.dwSamplesPerSec * (wave.common.wBitsPerSample >> 3);
168   wave.common.wBlockAlign = wave.common.wChannels * (wave.common.wBitsPerSample >> 3);
169
170   strncpy (wave.data.id, "data", 4);
171   wave.data.len = size - 44;
172
173   strncpy (wavenc->header, wave.riff.id, 4);
174   WRITE_U32 (wavenc->header + 4, wave.riff.len);
175   strncpy (wavenc->header + 8, wave.riff.wav_id, 4);
176   strncpy (wavenc->header + 12, wave.format.id, 4);
177   WRITE_U32 (wavenc->header + 16, wave.format.len);
178   WRITE_U16 (wavenc->header + 20, wave.common.wFormatTag);
179   WRITE_U16 (wavenc->header + 22, wave.common.wChannels);
180   WRITE_U32 (wavenc->header + 24, wave.common.dwSamplesPerSec);
181   WRITE_U32 (wavenc->header + 28, wave.common.dwAvgBytesPerSec);
182   WRITE_U16 (wavenc->header + 32, wave.common.wBlockAlign);
183   WRITE_U16 (wavenc->header + 34, wave.common.wBitsPerSample);
184   strncpy (wavenc->header + 36, wave.data.id, 4);
185   WRITE_U32 (wavenc->header + 40, wave.data.len);
186
187   wavenc->setup = TRUE;
188   return TRUE;
189 }
190
191 static GstPadConnectReturn
192 gst_wavenc_sinkconnect (GstPad *pad,
193                         GstCaps *caps)
194 {
195   GstWavEnc *wavenc;
196
197   wavenc = GST_WAVENC (gst_pad_get_parent (pad));
198
199   if (!GST_CAPS_IS_FIXED (caps)) {
200     return GST_PAD_CONNECT_DELAYED;
201   }
202
203   gst_caps_get_int (caps, "channels", &wavenc->channels);
204   gst_caps_get_int (caps, "rate", &wavenc->rate);
205   gst_caps_get_int (caps, "depth", &wavenc->bits);
206
207   gst_wavenc_setup (wavenc);
208
209   if (wavenc->setup) {
210     return GST_PAD_CONNECT_OK;
211   }
212
213   return GST_PAD_CONNECT_REFUSED;
214 }
215
216 static void
217 gst_wavenc_init (GstWavEnc *wavenc)
218 {
219   wavenc->sinkpad = gst_pad_new_from_template (sinktemplate, "sink");
220   gst_element_add_pad (GST_ELEMENT (wavenc), wavenc->sinkpad);
221   gst_pad_set_chain_function (wavenc->sinkpad, gst_wavenc_chain);
222   gst_pad_set_connect_function (wavenc->sinkpad, gst_wavenc_sinkconnect);
223
224   wavenc->srcpad = gst_pad_new_from_template (srctemplate, "src");
225   gst_element_add_pad (GST_ELEMENT (wavenc), wavenc->srcpad);
226
227   wavenc->setup = FALSE;
228   wavenc->flush_header = TRUE;
229 }
230
231 static void
232 gst_wavenc_chain (GstPad *pad,
233                   GstBuffer *buf)
234 {
235   GstWavEnc *wavenc;
236
237   wavenc = GST_WAVENC (gst_pad_get_parent (pad));
238
239   if (GST_IS_EVENT (buf)) {
240     GstEvent *event = GST_EVENT (buf);
241
242     switch (GST_EVENT_TYPE (event)) {
243     case GST_EVENT_EOS:
244       /* Should do something... */
245       gst_event_unref (event);
246
247       gst_pad_push (wavenc->srcpad, GST_BUFFER (gst_event_new (GST_EVENT_EOS)));
248       gst_element_set_eos (GST_ELEMENT (wavenc));
249       break;
250
251     default:
252       gst_pad_event_default (pad, event);
253       return;
254     }
255   } else {
256     if (!wavenc->setup) {
257       gst_buffer_unref (buf);
258       gst_element_error (GST_ELEMENT (wavenc), "encoder not initialised (input is not audio?)");
259       return;
260     }
261
262     if (wavenc->flush_header) {
263       GstBuffer *outbuf;
264
265       outbuf = gst_buffer_new_and_alloc (WAV_HEADER_LEN);
266       memcpy (GST_BUFFER_DATA (outbuf), wavenc->header, WAV_HEADER_LEN);
267
268       if (GST_PAD_IS_USABLE (wavenc->srcpad)) {
269         gst_pad_push (wavenc->srcpad, outbuf);
270       } else {
271         gst_buffer_unref (outbuf);
272       }
273
274       wavenc->flush_header = FALSE;
275     }
276     
277     gst_pad_push (wavenc->srcpad, buf);
278   }
279 }
280
281 static gboolean
282 plugin_init (GModule *module,
283              GstPlugin *plugin)
284 {
285   GstElementFactory *factory;
286
287   factory = gst_element_factory_new ("wavenc", GST_TYPE_WAVENC,
288                                      &gst_wavenc_details);
289   
290   srctemplate = src_factory ();
291   gst_element_factory_add_pad_template (factory, srctemplate);
292
293   sinktemplate = sink_factory ();
294   gst_element_factory_add_pad_template (factory, sinktemplate);
295
296   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
297
298   return TRUE;
299 }
300
301 GstPluginDesc plugin_desc = {
302   GST_VERSION_MAJOR,
303   GST_VERSION_MINOR,
304   "wavenc",
305   plugin_init
306 };
307