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