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