gst-indent
[platform/upstream/gst-plugins-good.git] / gst / wavenc / gstwavenc.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2 -*- */
2 /* GStreamer
3  * Copyright (C) <2002> Iain Holmes <iain@prettypeople.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  * 
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <string.h>
27 #include <gstwavenc.h>
28 #include <riff.h>
29
30 static void gst_wavenc_base_init (gpointer g_class);
31 static void gst_wavenc_class_init (GstWavEncClass * klass);
32 static void gst_wavenc_init (GstWavEnc * wavenc);
33 static void gst_wavenc_chain (GstPad * pad, GstData * _data);
34
35 #define WAVE_FORMAT_PCM 0x0001
36
37 #define WRITE_U32(buf, x) *(buf) = (unsigned char) (x&0xff);\
38 *((buf)+1) = (unsigned char)((x>>8)&0xff);\
39 *((buf)+2) = (unsigned char)((x>>16)&0xff);\
40 *((buf)+3) = (unsigned char)((x>>24)&0xff);
41
42 #define WRITE_U16(buf, x) *(buf) = (unsigned char) (x&0xff);\
43 *((buf)+1) = (unsigned char)((x>>8)&0xff);
44
45 struct riff_struct
46 {
47   guint8 id[4];                 /* RIFF */
48   guint32 len;
49   guint8 wav_id[4];             /* WAVE */
50 };
51
52 struct chunk_struct
53 {
54   guint8 id[4];
55   guint32 len;
56 };
57
58 struct common_struct
59 {
60   guint16 wFormatTag;
61   guint16 wChannels;
62   guint32 dwSamplesPerSec;
63   guint32 dwAvgBytesPerSec;
64   guint16 wBlockAlign;
65   guint16 wBitsPerSample;       /* Only for PCM */
66 };
67
68 struct wave_header
69 {
70   struct riff_struct riff;
71   struct chunk_struct format;
72   struct common_struct common;
73   struct chunk_struct data;
74 };
75
76 static GstElementDetails gst_wavenc_details =
77 GST_ELEMENT_DETAILS ("WAV encoder",
78     "Codec/Encoder/Audio",
79     "Encode raw audio into WAV",
80     "Iain Holmes <iain@prettypeople.org>");
81
82 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
83     GST_PAD_SINK,
84     GST_PAD_ALWAYS,
85     GST_STATIC_CAPS ("audio/x-raw-int, "
86         "rate = (int) [ 1, MAX ], "
87         "channels = (int) [ 1, MAX ], "
88         "endianness = (int) LITTLE_ENDIAN, "
89         "width = (int) { 8, 16 }, "
90         "depth = (int) { 8, 16 }, " "signed = (boolean) true")
91     );
92
93 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
94     GST_PAD_SRC,
95     GST_PAD_ALWAYS,
96     GST_STATIC_CAPS ("audio/x-wav")
97     );
98
99 enum
100 {
101   PROP_0,
102 };
103
104 static GstElementClass *parent_class = NULL;
105
106 static GType
107 gst_wavenc_get_type (void)
108 {
109   static GType type = 0;
110
111   if (type == 0) {
112     static const GTypeInfo info = {
113       sizeof (GstWavEncClass),
114       gst_wavenc_base_init,
115       NULL,
116       (GClassInitFunc) gst_wavenc_class_init,
117       NULL,
118       NULL,
119       sizeof (GstWavEnc),
120       0,
121       (GInstanceInitFunc) gst_wavenc_init
122     };
123
124     type = g_type_register_static (GST_TYPE_ELEMENT, "GstWavEnc", &info, 0);
125   }
126
127   return type;
128 }
129
130 static GstElementStateReturn
131 gst_wavenc_change_state (GstElement * element)
132 {
133   GstWavEnc *wavenc = GST_WAVENC (element);
134
135   switch (GST_STATE_TRANSITION (element)) {
136     case GST_STATE_PAUSED_TO_READY:
137       wavenc->setup = FALSE;
138       wavenc->flush_header = TRUE;
139       break;
140     default:
141       break;
142   }
143
144   if (parent_class->change_state) {
145     return parent_class->change_state (element);
146   }
147
148   return GST_STATE_SUCCESS;
149 }
150
151 static void
152 set_property (GObject * object,
153     guint prop_id, const GValue * value, GParamSpec * pspec)
154 {
155   GstWavEnc *enc;
156
157   enc = GST_WAVENC (object);
158
159   switch (prop_id) {
160     default:
161       break;
162   }
163 }
164
165 static void
166 gst_wavenc_base_init (gpointer g_class)
167 {
168   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
169
170   gst_element_class_set_details (element_class, &gst_wavenc_details);
171
172   gst_element_class_add_pad_template (element_class,
173       gst_static_pad_template_get (&src_factory));
174   gst_element_class_add_pad_template (element_class,
175       gst_static_pad_template_get (&sink_factory));
176 }
177 static void
178 gst_wavenc_class_init (GstWavEncClass * klass)
179 {
180   GstElementClass *element_class;
181   GObjectClass *object_class;
182
183   element_class = (GstElementClass *) klass;
184   object_class = (GObjectClass *) klass;
185   object_class->set_property = set_property;
186
187   element_class->change_state = gst_wavenc_change_state;
188
189   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
190 }
191
192 static gboolean
193 gst_wavenc_setup (GstWavEnc * wavenc)
194 {
195   struct wave_header wave;
196   gint size = 0x7fffffff;       /* Use a bogus size initially */
197
198   wave.common.wChannels = wavenc->channels;
199   wave.common.wBitsPerSample = wavenc->bits;
200   wave.common.dwSamplesPerSec = wavenc->rate;
201
202   memset (wavenc->header, 0, WAV_HEADER_LEN);
203
204   /* Fill out our wav-header with some information */
205   strncpy (wave.riff.id, "RIFF", 4);
206   wave.riff.len = size - 8;
207   strncpy (wave.riff.wav_id, "WAVE", 4);
208
209   strncpy (wave.format.id, "fmt ", 4);
210   wave.format.len = 16;
211
212   wave.common.wFormatTag = WAVE_FORMAT_PCM;
213   wave.common.dwAvgBytesPerSec =
214       wave.common.wChannels * wave.common.dwSamplesPerSec *
215       (wave.common.wBitsPerSample >> 3);
216   wave.common.wBlockAlign =
217       wave.common.wChannels * (wave.common.wBitsPerSample >> 3);
218
219   strncpy (wave.data.id, "data", 4);
220   wave.data.len = size - 44;
221
222   strncpy (wavenc->header, wave.riff.id, 4);
223   WRITE_U32 (wavenc->header + 4, wave.riff.len);
224   strncpy (wavenc->header + 8, wave.riff.wav_id, 4);
225   strncpy (wavenc->header + 12, wave.format.id, 4);
226   WRITE_U32 (wavenc->header + 16, wave.format.len);
227   WRITE_U16 (wavenc->header + 20, wave.common.wFormatTag);
228   WRITE_U16 (wavenc->header + 22, wave.common.wChannels);
229   WRITE_U32 (wavenc->header + 24, wave.common.dwSamplesPerSec);
230   WRITE_U32 (wavenc->header + 28, wave.common.dwAvgBytesPerSec);
231   WRITE_U16 (wavenc->header + 32, wave.common.wBlockAlign);
232   WRITE_U16 (wavenc->header + 34, wave.common.wBitsPerSample);
233   strncpy (wavenc->header + 36, wave.data.id, 4);
234   WRITE_U32 (wavenc->header + 40, wave.data.len);
235
236   wavenc->setup = TRUE;
237   return TRUE;
238 }
239
240 static GstPadLinkReturn
241 gst_wavenc_sinkconnect (GstPad * pad, const GstCaps * caps)
242 {
243   GstWavEnc *wavenc;
244   GstStructure *structure;
245
246   wavenc = GST_WAVENC (gst_pad_get_parent (pad));
247
248   structure = gst_caps_get_structure (caps, 0);
249
250   gst_structure_get_int (structure, "channels", &wavenc->channels);
251   gst_structure_get_int (structure, "rate", &wavenc->rate);
252   gst_structure_get_int (structure, "depth", &wavenc->bits);
253
254   gst_wavenc_setup (wavenc);
255
256   if (wavenc->setup) {
257     return GST_PAD_LINK_OK;
258   }
259
260   return GST_PAD_LINK_REFUSED;
261 }
262
263 static void
264 gst_wavenc_stop_file (GstWavEnc * wavenc)
265 {
266   GstEvent *event;
267   GstBuffer *outbuf;
268
269   event = gst_event_new_seek (GST_FORMAT_BYTES | GST_SEEK_METHOD_SET, 0);
270   gst_pad_send_event (GST_PAD_PEER (wavenc->srcpad), event);
271
272   outbuf = gst_buffer_new_and_alloc (WAV_HEADER_LEN);
273   WRITE_U32 (wavenc->header + 4, wavenc->length);
274   memcpy (GST_BUFFER_DATA (outbuf), wavenc->header, WAV_HEADER_LEN);
275
276   gst_pad_push (wavenc->srcpad, GST_DATA (outbuf));
277 }
278
279 static void
280 gst_wavenc_init (GstWavEnc * wavenc)
281 {
282   GstElementClass *klass = GST_ELEMENT_GET_CLASS (wavenc);
283
284   wavenc->sinkpad =
285       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
286           "sink"), "sink");
287   gst_element_add_pad (GST_ELEMENT (wavenc), wavenc->sinkpad);
288   gst_pad_set_chain_function (wavenc->sinkpad, gst_wavenc_chain);
289   gst_pad_set_link_function (wavenc->sinkpad, gst_wavenc_sinkconnect);
290
291   wavenc->srcpad =
292       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
293           "src"), "src");
294   gst_element_add_pad (GST_ELEMENT (wavenc), wavenc->srcpad);
295
296   wavenc->setup = FALSE;
297   wavenc->flush_header = TRUE;
298
299   GST_FLAG_SET (wavenc, GST_ELEMENT_EVENT_AWARE);
300 }
301
302 struct _maps
303 {
304   guint32 id;
305   char *name;
306 } maps[] = {
307   {
308   GST_RIFF_INFO_IARL, "Location"}, {
309   GST_RIFF_INFO_IART, "Artist"}, {
310   GST_RIFF_INFO_ICMS, "Commissioner"}, {
311   GST_RIFF_INFO_ICMT, "Comment"}, {
312   GST_RIFF_INFO_ICOP, "Copyright"}, {
313   GST_RIFF_INFO_ICRD, "Creation Date"}, {
314   GST_RIFF_INFO_IENG, "Engineer"}, {
315   GST_RIFF_INFO_IGNR, "Genre"}, {
316   GST_RIFF_INFO_IKEY, "Keywords"}, {
317   GST_RIFF_INFO_INAM, "Title"}, /* Name */
318   {
319   GST_RIFF_INFO_IPRD, "Product"}, {
320   GST_RIFF_INFO_ISBJ, "Subject"}, {
321   GST_RIFF_INFO_ISFT, "Software"}, {
322   GST_RIFF_INFO_ITCH, "Technician"}, {
323   0, NULL}
324 };
325
326 #if 0
327 static guint32
328 get_id_from_name (const char *name)
329 {
330   int i;
331
332   for (i = 0; maps[i].name; i++) {
333     if (strcasecmp (maps[i].name, name) == 0) {
334       return maps[i].id;
335     }
336   }
337
338   return 0;
339 }
340
341 static void
342 write_metadata (GstWavEnc * wavenc)
343 {
344   GString *info_str;
345   GList *props;
346   int total = 4;
347   gboolean need_to_write = FALSE;
348
349   info_str = g_string_new ("LIST    INFO");
350
351   for (props = wavenc->metadata->properties->properties; props;
352       props = props->next) {
353     GstPropsEntry *entry = props->data;
354     const char *name;
355     guint32 id;
356
357     name = gst_props_entry_get_name (entry);
358     id = get_id_from_name (name);
359     if (id != 0) {
360       const char *text;
361       char *tmp;
362       int len, req, i;
363
364       need_to_write = TRUE;     /* We've got at least one entry */
365
366       gst_props_entry_get_string (entry, &text);
367       len = strlen (text) + 1;  /* The length in the file includes the \0 */
368
369       tmp = g_strdup_printf (GST_FOURCC_FORMAT "%d%s", GST_FOURCC_ARGS (id),
370           GUINT32_TO_LE (len), text);
371       g_string_append (info_str, tmp);
372       g_free (tmp);
373
374       /* Check that we end on an even boundary */
375       req = ((len + 8) + 1) & ~1;
376       for (i = 0; i < req - len; i++) {
377         g_string_append_printf (info_str, "%c", 0);
378       }
379
380       total += req;
381     }
382   }
383
384   if (need_to_write) {
385     GstBuffer *buf;
386
387     /* Now we've got all the strings together, we can write our length in */
388     info_str->str[4] = GUINT32_TO_LE (total);
389
390     buf = gst_buffer_new ();
391     gst_buffer_set_data (buf, info_str->str, info_str->len);
392
393     gst_pad_push (wavenc->srcpad, GST_DATA (buf));
394     g_string_free (info_str, FALSE);
395   }
396 }
397
398 static void
399 write_cues (GstWavEnc * wavenc)
400 {
401   GString *cue_string, *point_string;
402   GstBuffer *buf;
403   GList *cue_list, *c;
404   int num_cues, total = 4;
405
406   if (gst_props_get (wavenc->metadata->properties,
407           "cues", &cue_list, NULL) == FALSE) {
408     /* No cues, move along please, nothing to see here */
409     return;
410   }
411
412   /* Space for 'cue ', chunk size and number of cuepoints */
413   cue_string = g_string_new ("cue         ");
414 #define CUEPOINT_SIZE 24
415   point_string = g_string_sized_new (CUEPOINT_SIZE);
416
417   for (c = cue_list, num_cues = 0; c; c = c->next, num_cues++) {
418     GstCaps *cue_caps = c->data;
419     guint32 pos;
420
421     gst_props_get (cue_caps->properties, "position", &pos, NULL);
422
423     point_string->str[0] = GUINT32_TO_LE (num_cues + 1);
424     point_string->str[4] = GUINT32_TO_LE (0);
425     /* Fixme: There is probably a macro for this */
426     point_string->str[8] = 'd';
427     point_string->str[9] = 'a';
428     point_string->str[10] = 't';
429     point_string->str[11] = 'a';
430     point_string->str[12] = GUINT32_TO_LE (0);
431     point_string->str[16] = GUINT32_TO_LE (0);
432     point_string->str[20] = GUINT32_TO_LE (pos);
433
434     total += CUEPOINT_SIZE;
435   }
436
437   /* Set the length and chunk size */
438   cue_string->str[4] = GUINT32_TO_LE (total);
439   cue_string->str[8] = GUINT32_TO_LE (num_cues);
440   /* Stick the cue points on the end */
441   g_string_append (cue_string, point_string->str);
442   g_string_free (point_string, TRUE);
443
444   buf = gst_buffer_new ();
445   gst_buffer_set_data (buf, cue_string->str, cue_string->len);
446
447   gst_pad_push (wavenc->srcpad, GST_DATA (buf));
448   g_string_free (cue_string, FALSE);
449 }
450
451 static void
452 write_labels (GstWavEnc * wavenc)
453 {
454   GstBuffer *buf;
455   GString *info_str;
456   int total = 4;
457   GList *caps;
458
459   info_str = g_string_new ("LIST    adtl");
460   if (gst_props_get (wavenc->metadata->properties, "ltxts", &caps, NULL)) {
461     GList *p;
462     int i;
463
464     for (p = caps, i = 1; p; p = p->next, i++) {
465       GstCaps *ltxt_caps = p->data;
466       GString *ltxt;
467       char *label = NULL;
468       int len, req, j;
469
470       gst_props_get (ltxt_caps->properties, "name", &label, NULL);
471       len = strlen (label);
472
473 #define LTXT_SIZE 28
474       ltxt = g_string_new ("ltxt                        ");
475       ltxt->str[8] = GUINT32_TO_LE (i); /* Identifier */
476       ltxt->str[12] = GUINT32_TO_LE (0);        /* Sample Length */
477       ltxt->str[16] = GUINT32_TO_LE (0);        /* FIXME: Don't save the purpose yet */
478       ltxt->str[20] = GUINT16_TO_LE (0);        /* Country */
479       ltxt->str[22] = GUINT16_TO_LE (0);        /* Language */
480       ltxt->str[24] = GUINT16_TO_LE (0);        /* Dialect */
481       ltxt->str[26] = GUINT16_TO_LE (0);        /* Code Page */
482       g_string_append (ltxt, label);
483       g_free (label);
484
485       len += LTXT_SIZE;
486
487       ltxt->str[4] = GUINT32_TO_LE (len);
488
489       /* Check that we end on an even boundary */
490       req = ((len + 8) + 1) & ~1;
491       for (j = 0; j < req - len; j++) {
492         g_string_append_printf (ltxt, "%c", 0);
493       }
494
495       total += req;
496
497       g_string_append (info_str, ltxt->str);
498       g_string_free (ltxt, TRUE);
499     }
500   }
501
502   if (gst_props_get (wavenc->metadata->properties, "labels", &caps, NULL)) {
503     GList *p;
504     int i;
505
506     for (p = caps, i = 1; p; p = p->next, i++) {
507       GstCaps *labl_caps = p->data;
508       GString *labl;
509       char *label = NULL;
510       int len, req, j;
511
512       gst_props_get (labl_caps->properties, "name", &label, NULL);
513       len = strlen (label);
514
515 #define LABL_SIZE 4
516       labl = g_string_new ("labl        ");
517       labl->str[8] = GUINT32_TO_LE (i);
518       g_string_append (labl, label);
519       g_free (label);
520
521       len += LABL_SIZE;
522
523       labl->str[4] = GUINT32_TO_LE (len);
524
525       /* Check our size */
526       req = ((len + 8) + 1) & ~1;
527       for (j = 0; j < req - len; j++) {
528         g_string_append_printf (labl, "%c", 0);
529       }
530
531       total += req;
532
533       g_string_append (info_str, labl->str);
534       g_string_free (labl, TRUE);
535     }
536   }
537
538   if (gst_props_get (wavenc->metadata->properties, "notes", &caps, NULL)) {
539     GList *p;
540     int i;
541
542     for (p = caps, i = 1; p; p = p->next, i++) {
543       GstCaps *note_caps = p->data;
544       GString *note;
545       char *label = NULL;
546       int len, req, j;
547
548       gst_props_get (note_caps->properties, "name", &label, NULL);
549       len = strlen (label);
550
551 #define NOTE_SIZE 4
552       note = g_string_new ("note        ");
553       note->str[8] = GUINT32_TO_LE (i);
554       g_string_append (note, label);
555       g_free (label);
556
557       len += NOTE_SIZE;
558
559       note->str[4] = GUINT32_TO_LE (len);
560
561       /* Size check */
562       req = ((len + 8) + 1) & ~1;
563       for (j = 0; j < req - len; j++) {
564         g_string_append_printf (note, "%c", 0);
565       }
566
567       total += req;
568
569       g_string_append (info_str, note->str);
570       g_string_free (note, TRUE);
571     }
572   }
573
574   info_str->str[4] = GUINT32_TO_LE (total);
575
576   buf = gst_buffer_new ();
577   gst_buffer_set_data (buf, info_str->str, info_str->len);
578
579   gst_pad_push (wavenc->srcpad, GST_DATA (buf));
580   g_string_free (info_str, FALSE);
581 }
582 #endif
583
584 static void
585 gst_wavenc_chain (GstPad * pad, GstData * _data)
586 {
587   GstBuffer *buf = GST_BUFFER (_data);
588   GstWavEnc *wavenc;
589
590   wavenc = GST_WAVENC (gst_pad_get_parent (pad));
591
592   if (!wavenc->setup) {
593     gst_buffer_unref (buf);
594     GST_ELEMENT_ERROR (wavenc, CORE, NEGOTIATION, (NULL),
595         ("encoder not initialised (input is not audio?)"));
596     return;
597   }
598
599   if (GST_IS_EVENT (buf)) {
600     if (GST_EVENT_TYPE (buf) == GST_EVENT_EOS) {
601       wavenc->pad_eos = TRUE;
602
603 #if 0
604       /* Write our metadata if we have any */
605       if (wavenc->metadata) {
606         write_metadata (wavenc);
607         write_cues (wavenc);
608         write_labels (wavenc);
609       }
610 #endif
611
612       gst_wavenc_stop_file (wavenc);
613       gst_pad_push (wavenc->srcpad, GST_DATA (gst_event_new (GST_EVENT_EOS)));
614       gst_element_set_eos (GST_ELEMENT (wavenc));
615     } else {
616       gst_pad_event_default (wavenc->srcpad, GST_EVENT (buf));
617     }
618     return;
619   }
620
621   if (GST_PAD_IS_USABLE (wavenc->srcpad)) {
622     if (wavenc->flush_header) {
623       GstBuffer *outbuf;
624
625       outbuf = gst_buffer_new_and_alloc (WAV_HEADER_LEN);
626       memcpy (GST_BUFFER_DATA (outbuf), wavenc->header, WAV_HEADER_LEN);
627       GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
628
629       gst_pad_push (wavenc->srcpad, GST_DATA (outbuf));
630       wavenc->flush_header = FALSE;
631     }
632
633     wavenc->length += GST_BUFFER_SIZE (buf);
634     gst_pad_push (wavenc->srcpad, GST_DATA (buf));
635   }
636 }
637
638 static gboolean
639 plugin_init (GstPlugin * plugin)
640 {
641   return gst_element_register (plugin, "wavenc", GST_RANK_NONE,
642       GST_TYPE_WAVENC);
643 }
644
645 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
646     GST_VERSION_MINOR,
647     "wavenc",
648     "Encode raw audio into WAV",
649     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)