gst/ac3parse/gstac3parse.c: update to checklist 5
[platform/upstream/gst-plugins-good.git] / gst / auparse / gstauparse.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 /* Element-Checklist-Version: 5 */
20
21 /* 2001/04/03 - Updated parseau to use caps nego
22  *              Zaheer Merali <zaheer@grid9.net
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <gstauparse.h>
32 #include <gst/audio/audio.h>
33
34 /* elementfactory information */
35 static GstElementDetails gst_auparse_details = GST_ELEMENT_DETAILS (
36   ".au parser",
37   "Codec/Parser/Audio",
38   "Parse an .au file into raw audio",
39   "Erik Walthinsen <omega@cse.ogi.edu>"
40 );
41
42 static GstStaticPadTemplate gst_auparse_sink_template =
43 GST_STATIC_PAD_TEMPLATE (
44   "sink",
45   GST_PAD_SINK,
46   GST_PAD_ALWAYS,
47   GST_STATIC_CAPS ( "audio/x-au" )
48 );
49
50 static GstStaticPadTemplate gst_auparse_src_template =
51 GST_STATIC_PAD_TEMPLATE (
52   "src",
53   GST_PAD_SRC,
54   GST_PAD_ALWAYS,
55   GST_STATIC_CAPS (
56     GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
57     "audio/x-alaw, "
58       "rate = (int) [ 8000, 48000 ], "
59       "channels = (int) [ 1, 2 ]"
60   )
61 );
62
63 /* AuParse signals and args */
64 enum {
65   /* FILL ME */
66   LAST_SIGNAL
67 };
68
69 enum {
70   ARG_0,
71   /* FILL ME */
72 };
73
74 static void     gst_auparse_base_init           (gpointer g_class);
75 static void     gst_auparse_class_init          (GstAuParseClass *klass);
76 static void     gst_auparse_init                (GstAuParse *auparse);
77
78 static void     gst_auparse_chain               (GstPad *pad,GstData *_data);
79
80 static GstElementClass *parent_class = NULL;
81 /*static guint gst_auparse_signals[LAST_SIGNAL] = { 0 }; */
82
83 GType
84 gst_auparse_get_type (void) 
85 {
86   static GType auparse_type = 0;
87
88   if (!auparse_type) {
89     static const GTypeInfo auparse_info = {
90       sizeof(GstAuParseClass),
91       gst_auparse_base_init,
92       NULL,
93       (GClassInitFunc) gst_auparse_class_init,
94       NULL,
95       NULL,
96       sizeof(GstAuParse),
97       0,
98       (GInstanceInitFunc) gst_auparse_init,
99     };
100     auparse_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAuParse", &auparse_info, 0);
101   }
102   return auparse_type;
103 }
104
105 static void
106 gst_auparse_base_init (gpointer g_class)
107 {
108   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
109
110   gst_element_class_add_pad_template (element_class,
111       gst_static_pad_template_get (&gst_auparse_sink_template));
112   gst_element_class_add_pad_template (element_class,
113       gst_static_pad_template_get (&gst_auparse_src_template));
114   gst_element_class_set_details (element_class, &gst_auparse_details);
115
116 }
117
118 static void
119 gst_auparse_class_init (GstAuParseClass *klass) 
120 {
121   GstElementClass *gstelement_class;
122
123   gstelement_class = (GstElementClass*) klass;
124
125   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
126 }
127
128 static void 
129 gst_auparse_init (GstAuParse *auparse) 
130 {
131   auparse->sinkpad = gst_pad_new_from_template (
132       gst_static_pad_template_get (&gst_auparse_sink_template), "sink");
133   gst_element_add_pad (GST_ELEMENT (auparse), auparse->sinkpad);
134   gst_pad_set_chain_function (auparse->sinkpad, gst_auparse_chain);
135
136   auparse->srcpad = gst_pad_new_from_template (
137       gst_static_pad_template_get (&gst_auparse_src_template), "src");
138   gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad);
139   gst_pad_use_explicit_caps (auparse->srcpad);
140
141   auparse->offset = 0;
142   auparse->size = 0;
143   auparse->encoding = 0;
144   auparse->frequency = 0;
145   auparse->channels = 0;
146 }
147
148 static void 
149 gst_auparse_chain (GstPad *pad, GstData *_data) 
150 {
151   GstBuffer *buf = GST_BUFFER (_data);
152   GstAuParse *auparse;
153   gchar *data;
154   glong size;
155   GstCaps *tempcaps;
156   gint law, depth;
157   gboolean sign;
158
159   g_return_if_fail (pad != NULL);
160   g_return_if_fail (GST_IS_PAD (pad));
161   g_return_if_fail (buf != NULL);
162
163   auparse = GST_AUPARSE (gst_pad_get_parent (pad));
164   
165   GST_DEBUG ("gst_auparse_chain: got buffer in '%s'",
166           gst_element_get_name (GST_ELEMENT (auparse)));
167
168   data = GST_BUFFER_DATA (buf);
169   size = GST_BUFFER_SIZE (buf);
170
171   /* if we haven't seen any data yet... */
172   if (auparse->size == 0) {
173     GstBuffer *newbuf;
174     guint32 *head = (guint32 *)data;
175
176     /* normal format is big endian (au is a Sparc format) */
177     if (GUINT32_FROM_BE (*head) == 0x2e736e64) {
178       head++;
179       auparse->le = 0;
180       auparse->offset           = GUINT32_FROM_BE (*head);
181       head++;
182       auparse->size             = GUINT32_FROM_BE (*head);
183       head++;
184       auparse->encoding         = GUINT32_FROM_BE (*head);
185       head++;
186       auparse->frequency        = GUINT32_FROM_BE (*head);
187       head++;
188       auparse->channels         = GUINT32_FROM_BE (*head);
189       head++;
190
191     /* and of course, someone had to invent a little endian
192      * version.  Used by DEC systems. */
193     } else if (GUINT32_FROM_LE (*head) == 0x0064732E) {
194       auparse->le = 1;
195       head++;
196       auparse->le = 0;
197       auparse->offset           = GUINT32_FROM_LE (*head);
198       head++;
199       auparse->size             = GUINT32_FROM_LE (*head);
200       head++;
201       auparse->encoding         = GUINT32_FROM_LE (*head);
202       head++;
203       auparse->frequency        = GUINT32_FROM_LE (*head);
204       head++;
205       auparse->channels         = GUINT32_FROM_LE (*head);
206       head++;
207
208     } else {
209       g_warning ("help, dunno what I'm looking at!\n");
210       gst_buffer_unref(buf);
211       return;
212     }
213
214     g_print ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld\n",
215              auparse->offset,auparse->size,auparse->encoding,
216              auparse->frequency,auparse->channels);
217     GST_DEBUG ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld",
218              auparse->offset,auparse->size,auparse->encoding,
219              auparse->frequency,auparse->channels);
220     
221     switch (auparse->encoding) {
222       case 1:
223         law = 1;
224         depth = 8;
225         sign = FALSE;
226         break;
227       case 2:
228         law = 0;
229         depth = 8;
230         sign = FALSE;
231         break;
232       case 3:
233         law = 0;
234         depth = 16;
235         sign = TRUE;
236         break;
237       default:
238         g_warning ("help!, dont know how to deal with this format yet\n");
239         return;
240     }
241
242     if (law) {
243       tempcaps = gst_caps_new_simple ("audio/x-alaw",
244           "rate", G_TYPE_INT, auparse->frequency,
245           "channels", G_TYPE_INT, auparse->channels, NULL);
246     } else {
247       tempcaps = gst_caps_new_simple ("audio/x-raw-int",
248           "endianness", G_TYPE_INT, G_BIG_ENDIAN,
249           "rate",       G_TYPE_INT, auparse->frequency,
250           "channels",   G_TYPE_INT, auparse->channels,
251           "depth",      G_TYPE_INT, depth,
252           "width",      G_TYPE_INT, depth,
253           "signed",     G_TYPE_BOOLEAN, sign, NULL);
254     }
255
256     if (!gst_pad_set_explicit_caps (auparse->srcpad, tempcaps)) {
257       gst_buffer_unref (buf);
258       return;
259     }
260
261     newbuf = gst_buffer_new ();
262     GST_BUFFER_DATA (newbuf) = (gpointer) malloc (size-(auparse->offset));
263     memcpy (GST_BUFFER_DATA (newbuf), data+24, size-(auparse->offset));
264     GST_BUFFER_SIZE (newbuf) = size-(auparse->offset);
265
266     gst_buffer_unref (buf);
267
268     gst_pad_push (auparse->srcpad, GST_DATA (newbuf));
269     return;
270   }
271
272   gst_pad_push (auparse->srcpad, GST_DATA (buf));
273 }
274
275
276 static gboolean
277 plugin_init (GstPlugin *plugin)
278 {
279   if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
280         GST_TYPE_AUPARSE)) {
281     return FALSE;
282   }
283
284   return TRUE;
285 }
286
287 GST_PLUGIN_DEFINE (
288   GST_VERSION_MAJOR,
289   GST_VERSION_MINOR,
290   "auparse",
291   "parses au streams",
292   plugin_init,
293   VERSION,
294   "GPL",
295   GST_PACKAGE,
296   GST_ORIGIN
297 )
298