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