_peer_get_caps() -> _peer_query_caps()
[platform/upstream/gst-plugins-good.git] / gst / law / mulaw-encode.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 /**
20  * SECTION:element-mulawenc
21  *
22  * This element encode mulaw audio. Mulaw coding is also known as G.711.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 #include <gst/gst.h>
29 #include "mulaw-encode.h"
30 #include "mulaw-conversion.h"
31
32 extern GstStaticPadTemplate mulaw_enc_src_factory;
33 extern GstStaticPadTemplate mulaw_enc_sink_factory;
34
35 /* Stereo signals and args */
36 enum
37 {
38   /* FILL ME */
39   LAST_SIGNAL
40 };
41
42 enum
43 {
44   ARG_0
45 };
46
47 static gboolean gst_mulawenc_event (GstPad * pad, GstEvent * event);
48 static GstFlowReturn gst_mulawenc_chain (GstPad * pad, GstBuffer * buffer);
49
50 #define gst_mulawenc_parent_class parent_class
51 G_DEFINE_TYPE (GstMuLawEnc, gst_mulawenc, GST_TYPE_ELEMENT);
52
53 /*static guint gst_stereo_signals[LAST_SIGNAL] = { 0 }; */
54
55 static GstCaps *
56 mulawenc_getcaps (GstPad * pad, GstCaps * filter)
57 {
58   GstMuLawEnc *mulawenc;
59   GstPad *otherpad;
60   GstCaps *othercaps, *result;
61   const GstCaps *templ;
62   const gchar *name;
63   gint i;
64
65   mulawenc = GST_MULAWENC (GST_PAD_PARENT (pad));
66
67   /* figure out the name of the caps we are going to return */
68   if (pad == mulawenc->srcpad) {
69     name = "audio/x-mulaw";
70     otherpad = mulawenc->sinkpad;
71   } else {
72     name = "audio/x-raw-int";
73     otherpad = mulawenc->srcpad;
74   }
75   /* get caps from the peer, this can return NULL when there is no peer */
76   othercaps = gst_pad_peer_query_caps (otherpad, filter);
77
78   /* get the template caps to make sure we return something acceptable */
79   templ = gst_pad_get_pad_template_caps (pad);
80
81   if (othercaps) {
82     /* there was a peer */
83     othercaps = gst_caps_make_writable (othercaps);
84
85     /* go through the caps and remove the fields we don't want */
86     for (i = 0; i < gst_caps_get_size (othercaps); i++) {
87       GstStructure *structure;
88
89       structure = gst_caps_get_structure (othercaps, i);
90
91       /* adjust the name */
92       gst_structure_set_name (structure, name);
93
94       if (pad == mulawenc->srcpad) {
95         /* remove the fields we don't want */
96         gst_structure_remove_fields (structure, "width", "depth", "endianness",
97             "signed", NULL);
98       } else {
99         /* add fixed fields */
100         gst_structure_set (structure, "width", G_TYPE_INT, 16,
101             "depth", G_TYPE_INT, 16,
102             "endianness", G_TYPE_INT, G_BYTE_ORDER,
103             "signed", G_TYPE_BOOLEAN, TRUE, NULL);
104       }
105     }
106     /* filter against the allowed caps of the pad to return our result */
107     result = gst_caps_intersect (othercaps, templ);
108     gst_caps_unref (othercaps);
109   } else {
110     /* there was no peer, return the template caps */
111     result = gst_caps_copy (templ);
112   }
113   return result;
114 }
115
116 static gboolean
117 gst_mulawenc_query (GstPad * pad, GstQuery * query)
118 {
119   gboolean res;
120
121   switch (GST_QUERY_TYPE (query)) {
122     case GST_QUERY_CAPS:
123     {
124       GstCaps *filter, *caps;
125
126       gst_query_parse_caps (query, &filter);
127       caps = mulawenc_getcaps (pad, filter);
128       gst_query_set_caps_result (query, caps);
129       gst_caps_unref (caps);
130
131       res = TRUE;
132       break;
133     }
134     default:
135       res = gst_pad_query_default (pad, query);
136       break;
137   }
138   return res;
139 }
140
141
142 static gboolean
143 mulawenc_setcaps (GstMuLawEnc * mulawenc, GstCaps * caps)
144 {
145   GstStructure *structure;
146   GstCaps *base_caps;
147
148   structure = gst_caps_get_structure (caps, 0);
149   gst_structure_get_int (structure, "channels", &mulawenc->channels);
150   gst_structure_get_int (structure, "rate", &mulawenc->rate);
151
152   base_caps = gst_caps_copy (gst_pad_get_pad_template_caps (mulawenc->srcpad));
153
154   structure = gst_caps_get_structure (base_caps, 0);
155   gst_structure_set (structure, "rate", G_TYPE_INT, mulawenc->rate, NULL);
156   gst_structure_set (structure, "channels", G_TYPE_INT, mulawenc->channels,
157       NULL);
158
159   gst_pad_set_caps (mulawenc->srcpad, base_caps);
160
161   gst_caps_unref (base_caps);
162
163   return TRUE;
164 }
165
166 static void
167 gst_mulawenc_class_init (GstMuLawEncClass * klass)
168 {
169   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
170
171   gst_element_class_add_pad_template (element_class,
172       gst_static_pad_template_get (&mulaw_enc_src_factory));
173   gst_element_class_add_pad_template (element_class,
174       gst_static_pad_template_get (&mulaw_enc_sink_factory));
175
176   gst_element_class_set_details_simple (element_class, "Mu Law audio encoder",
177       "Codec/Encoder/Audio",
178       "Convert 16bit PCM to 8bit mu law",
179       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
180 }
181
182 static void
183 gst_mulawenc_init (GstMuLawEnc * mulawenc)
184 {
185   mulawenc->sinkpad =
186       gst_pad_new_from_static_template (&mulaw_enc_sink_factory, "sink");
187   gst_pad_set_query_function (mulawenc->sinkpad, gst_mulawenc_query);
188   gst_pad_set_event_function (mulawenc->sinkpad, gst_mulawenc_event);
189   gst_pad_set_chain_function (mulawenc->sinkpad, gst_mulawenc_chain);
190   gst_element_add_pad (GST_ELEMENT (mulawenc), mulawenc->sinkpad);
191
192   mulawenc->srcpad =
193       gst_pad_new_from_static_template (&mulaw_enc_src_factory, "src");
194   gst_pad_set_query_function (mulawenc->srcpad, gst_mulawenc_query);
195   gst_element_add_pad (GST_ELEMENT (mulawenc), mulawenc->srcpad);
196
197   /* init rest */
198   mulawenc->channels = 0;
199   mulawenc->rate = 0;
200 }
201
202 static gboolean
203 gst_mulawenc_event (GstPad * pad, GstEvent * event)
204 {
205   GstMuLawEnc *mulawenc;
206   gboolean res;
207
208   mulawenc = GST_MULAWENC (GST_PAD_PARENT (pad));
209
210   switch (GST_EVENT_TYPE (event)) {
211     case GST_EVENT_CAPS:
212     {
213       GstCaps *caps;
214
215       gst_event_parse_caps (event, &caps);
216       mulawenc_setcaps (mulawenc, caps);
217       gst_event_unref (event);
218
219       res = TRUE;
220       break;
221     }
222     default:
223       res = gst_pad_event_default (pad, event);
224       break;
225   }
226   return res;
227 }
228
229 static GstFlowReturn
230 gst_mulawenc_chain (GstPad * pad, GstBuffer * buffer)
231 {
232   GstMuLawEnc *mulawenc;
233   gint16 *linear_data;
234   gsize linear_size;
235   guint8 *mulaw_data;
236   guint mulaw_size;
237   GstBuffer *outbuf;
238   GstFlowReturn ret;
239   GstClockTime timestamp, duration;
240
241   mulawenc = GST_MULAWENC (gst_pad_get_parent (pad));
242
243   if (!mulawenc->rate || !mulawenc->channels)
244     goto not_negotiated;
245
246   linear_data = gst_buffer_map (buffer, &linear_size, NULL, GST_MAP_READ);
247
248   mulaw_size = linear_size / 2;
249
250   timestamp = GST_BUFFER_TIMESTAMP (buffer);
251   duration = GST_BUFFER_DURATION (buffer);
252
253   outbuf = gst_buffer_new_allocate (NULL, mulaw_size, 0);
254
255   if (duration == -1) {
256     duration = gst_util_uint64_scale_int (mulaw_size,
257         GST_SECOND, mulawenc->rate * mulawenc->channels);
258   }
259
260   mulaw_data = gst_buffer_map (outbuf, NULL, NULL, GST_MAP_WRITE);
261
262   /* copy discont flag */
263   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))
264     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
265
266   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
267   GST_BUFFER_DURATION (outbuf) = duration;
268
269   mulaw_encode (linear_data, mulaw_data, mulaw_size);
270
271   gst_buffer_unmap (outbuf, mulaw_data, -1);
272   gst_buffer_unmap (buffer, linear_data, -1);
273   gst_buffer_unref (buffer);
274
275   ret = gst_pad_push (mulawenc->srcpad, outbuf);
276
277 done:
278   gst_object_unref (mulawenc);
279
280   return ret;
281
282 not_negotiated:
283   {
284     GST_DEBUG_OBJECT (mulawenc, "no format negotiated");
285     ret = GST_FLOW_NOT_NEGOTIATED;
286     gst_buffer_unref (buffer);
287     goto done;
288   }
289 }