rtpvp8: port some more to new memory API
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpg726pay.c
1 /* GStreamer
2  * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) 2005 Edgard Lima <edgard.lima@indt.org.br>
4  * Copyright (C) 2005 Nokia Corporation <kai.vehmanen@nokia.com>
5  * Copyright (C) 2007,2008 Axis Communications <dev-gstreamer@axis.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <gst/rtp/gstrtpbuffer.h>
30
31 #include "gstrtpg726pay.h"
32
33 GST_DEBUG_CATEGORY_STATIC (rtpg726pay_debug);
34 #define GST_CAT_DEFAULT (rtpg726pay_debug)
35
36 #define DEFAULT_FORCE_AAL2      TRUE
37
38 enum
39 {
40   PROP_0,
41   PROP_FORCE_AAL2,
42   PROP_LAST
43 };
44
45 static GstStaticPadTemplate gst_rtp_g726_pay_sink_template =
46 GST_STATIC_PAD_TEMPLATE ("sink",
47     GST_PAD_SINK,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS ("audio/x-adpcm, "
50         "channels = (int) 1, "
51         "rate = (int) 8000, "
52         "bitrate = (int) { 16000, 24000, 32000, 40000 }, "
53         "layout = (string) \"g726\"")
54     );
55
56 static GstStaticPadTemplate gst_rtp_g726_pay_src_template =
57 GST_STATIC_PAD_TEMPLATE ("src",
58     GST_PAD_SRC,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS ("application/x-rtp, "
61         "media = (string) \"audio\", "
62         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
63         "clock-rate = (int) 8000, "
64         "encoding-name = (string) { \"G726-16\", \"G726-24\", \"G726-32\", \"G726-40\", "
65         "    \"AAL2-G726-16\", \"AAL2-G726-24\", \"AAL2-G726-32\", \"AAL2-G726-40\" } ")
66     );
67
68 static void gst_rtp_g726_pay_get_property (GObject * object, guint prop_id,
69     GValue * value, GParamSpec * pspec);
70 static void gst_rtp_g726_pay_set_property (GObject * object, guint prop_id,
71     const GValue * value, GParamSpec * pspec);
72
73 static gboolean gst_rtp_g726_pay_setcaps (GstRTPBasePayload * payload,
74     GstCaps * caps);
75 static GstFlowReturn gst_rtp_g726_pay_handle_buffer (GstRTPBasePayload *
76     payload, GstBuffer * buffer);
77
78 #define gst_rtp_g726_pay_parent_class parent_class
79 G_DEFINE_TYPE (GstRtpG726Pay, gst_rtp_g726_pay,
80     GST_TYPE_RTP_BASE_AUDIO_PAYLOAD);
81
82 static void
83 gst_rtp_g726_pay_class_init (GstRtpG726PayClass * klass)
84 {
85   GObjectClass *gobject_class;
86   GstElementClass *gstelement_class;
87   GstRTPBasePayloadClass *gstrtpbasepayload_class;
88
89   gobject_class = (GObjectClass *) klass;
90   gstelement_class = (GstElementClass *) klass;
91   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
92
93   gobject_class->set_property = gst_rtp_g726_pay_set_property;
94   gobject_class->get_property = gst_rtp_g726_pay_get_property;
95
96   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FORCE_AAL2,
97       g_param_spec_boolean ("force-aal2", "Force AAL2",
98           "Force AAL2 encoding for compatibility with bad depayloaders",
99           DEFAULT_FORCE_AAL2, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
100
101   gst_element_class_add_pad_template (gstelement_class,
102       gst_static_pad_template_get (&gst_rtp_g726_pay_sink_template));
103   gst_element_class_add_pad_template (gstelement_class,
104       gst_static_pad_template_get (&gst_rtp_g726_pay_src_template));
105
106   gst_element_class_set_static_metadata (gstelement_class,
107       "RTP G.726 payloader", "Codec/Payloader/Network/RTP",
108       "Payload-encodes G.726 audio into a RTP packet",
109       "Axis Communications <dev-gstreamer@axis.com>");
110
111   gstrtpbasepayload_class->set_caps = gst_rtp_g726_pay_setcaps;
112   gstrtpbasepayload_class->handle_buffer = gst_rtp_g726_pay_handle_buffer;
113
114   GST_DEBUG_CATEGORY_INIT (rtpg726pay_debug, "rtpg726pay", 0,
115       "G.726 RTP Payloader");
116 }
117
118 static void
119 gst_rtp_g726_pay_init (GstRtpG726Pay * rtpg726pay)
120 {
121   GstRTPBaseAudioPayload *rtpbaseaudiopayload;
122
123   rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpg726pay);
124
125   GST_RTP_BASE_PAYLOAD (rtpg726pay)->clock_rate = 8000;
126
127   rtpg726pay->force_aal2 = DEFAULT_FORCE_AAL2;
128
129   /* sample based codec */
130   gst_rtp_base_audio_payload_set_sample_based (rtpbaseaudiopayload);
131 }
132
133 static gboolean
134 gst_rtp_g726_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
135 {
136   gchar *encoding_name;
137   GstStructure *structure;
138   GstRTPBaseAudioPayload *rtpbaseaudiopayload;
139   GstRtpG726Pay *pay;
140   GstCaps *peercaps;
141   gboolean res;
142
143   rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (payload);
144   pay = GST_RTP_G726_PAY (payload);
145
146   structure = gst_caps_get_structure (caps, 0);
147
148   if (!gst_structure_get_int (structure, "bitrate", &pay->bitrate))
149     pay->bitrate = 32000;
150
151   GST_DEBUG_OBJECT (payload, "using bitrate %d", pay->bitrate);
152
153   pay->aal2 = FALSE;
154
155   /* first see what we can do with the bitrate */
156   switch (pay->bitrate) {
157     case 16000:
158       encoding_name = g_strdup ("G726-16");
159       gst_rtp_base_audio_payload_set_samplebits_options (rtpbaseaudiopayload,
160           2);
161       break;
162     case 24000:
163       encoding_name = g_strdup ("G726-24");
164       gst_rtp_base_audio_payload_set_samplebits_options (rtpbaseaudiopayload,
165           3);
166       break;
167     case 32000:
168       encoding_name = g_strdup ("G726-32");
169       gst_rtp_base_audio_payload_set_samplebits_options (rtpbaseaudiopayload,
170           4);
171       break;
172     case 40000:
173       encoding_name = g_strdup ("G726-40");
174       gst_rtp_base_audio_payload_set_samplebits_options (rtpbaseaudiopayload,
175           5);
176       break;
177     default:
178       goto invalid_bitrate;
179   }
180
181   GST_DEBUG_OBJECT (payload, "selected base encoding %s", encoding_name);
182
183   /* now see if we need to produce AAL2 or not */
184   peercaps = gst_pad_peer_query_caps (payload->srcpad, NULL);
185   if (peercaps) {
186     GstCaps *filter, *intersect;
187     gchar *capsstr;
188
189     GST_DEBUG_OBJECT (payload, "have peercaps %" GST_PTR_FORMAT, peercaps);
190
191     capsstr = g_strdup_printf ("application/x-rtp, "
192         "media = (string) \"audio\", "
193         "clock-rate = (int) 8000, "
194         "encoding-name = (string) %s; "
195         "application/x-rtp, "
196         "media = (string) \"audio\", "
197         "clock-rate = (int) 8000, "
198         "encoding-name = (string) AAL2-%s", encoding_name, encoding_name);
199     filter = gst_caps_from_string (capsstr);
200     g_free (capsstr);
201
202     /* intersect to filter */
203     intersect = gst_caps_intersect (peercaps, filter);
204     gst_caps_unref (peercaps);
205
206     GST_DEBUG_OBJECT (payload, "intersected to %" GST_PTR_FORMAT, intersect);
207
208     if (!intersect)
209       goto no_format;
210     if (gst_caps_is_empty (intersect)) {
211       gst_caps_unref (intersect);
212       goto no_format;
213     }
214
215     structure = gst_caps_get_structure (intersect, 0);
216
217     /* now see what encoding name we settled on, we need to dup because the
218      * string goes away when we unref the intersection below. */
219     encoding_name =
220         g_strdup (gst_structure_get_string (structure, "encoding-name"));
221
222     /* if we managed to negotiate to AAL2, we definatly are going to do AAL2
223      * encoding. Else we only encode AAL2 when explicitly set by the
224      * property. */
225     if (g_str_has_prefix (encoding_name, "AAL2-"))
226       pay->aal2 = TRUE;
227     else
228       pay->aal2 = pay->force_aal2;
229
230     GST_DEBUG_OBJECT (payload, "final encoding %s, AAL2 %d", encoding_name,
231         pay->aal2);
232
233     gst_caps_unref (intersect);
234   } else {
235     /* downstream can do anything but we prefer the better supported non-AAL2 */
236     pay->aal2 = pay->force_aal2;
237     GST_DEBUG_OBJECT (payload, "no peer caps, AAL2 %d", pay->aal2);
238   }
239
240   gst_rtp_base_payload_set_options (payload, "audio", TRUE, encoding_name,
241       8000);
242   res = gst_rtp_base_payload_set_outcaps (payload, NULL);
243
244   g_free (encoding_name);
245
246   return res;
247
248   /* ERRORS */
249 invalid_bitrate:
250   {
251     GST_ERROR_OBJECT (payload, "invalid bitrate %d specified", pay->bitrate);
252     return FALSE;
253   }
254 no_format:
255   {
256     GST_ERROR_OBJECT (payload, "could not negotiate format");
257     return FALSE;
258   }
259 }
260
261 static GstFlowReturn
262 gst_rtp_g726_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
263 {
264   GstFlowReturn res;
265   GstRtpG726Pay *pay;
266
267   pay = GST_RTP_G726_PAY (payload);
268
269   if (!pay->aal2) {
270     GstMapInfo map;
271     guint8 *data, tmp;
272     gsize size;
273
274     /* for non AAL2, we need to reshuffle the bytes, we can do this in-place
275      * when the buffer is writable. */
276     buffer = gst_buffer_make_writable (buffer);
277
278     gst_buffer_map (buffer, &map, GST_MAP_READWRITE);
279     data = map.data;
280     size = map.size;
281
282     GST_LOG_OBJECT (pay, "packing %" G_GSIZE_FORMAT " bytes of data", map.size);
283
284     /* we need to reshuffle the bytes, output is of the form:
285      * A B C D .. with the number of bits depending on the bitrate. */
286     switch (pay->bitrate) {
287       case 16000:
288       {
289         /*  0               
290          *  0 1 2 3 4 5 6 7 
291          * +-+-+-+-+-+-+-+-+-
292          * |D D|C C|B B|A A| ...
293          * |0 1|0 1|0 1|0 1|
294          * +-+-+-+-+-+-+-+-+-
295          */
296         while (size > 0) {
297           tmp = *data;
298           *data++ = ((tmp & 0xc0) >> 6) |
299               ((tmp & 0x30) >> 2) | ((tmp & 0x0c) << 2) | ((tmp & 0x03) << 6);
300           size--;
301         }
302         break;
303       }
304       case 24000:
305       {
306         /*  0                   1                   2
307          *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
308          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
309          * |C C|B B B|A A A|F|E E E|D D D|C|H H H|G G G|F F| ...
310          * |1 2|0 1 2|0 1 2|2|0 1 2|0 1 2|0|0 1 2|0 1 2|0 1|
311          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
312          */
313         while (size > 2) {
314           tmp = *data;
315           *data++ = ((tmp & 0xc0) >> 6) |
316               ((tmp & 0x38) >> 1) | ((tmp & 0x07) << 5);
317           tmp = *data;
318           *data++ = ((tmp & 0x80) >> 7) |
319               ((tmp & 0x70) >> 3) | ((tmp & 0x0e) << 4) | ((tmp & 0x01) << 7);
320           tmp = *data;
321           *data++ = ((tmp & 0xe0) >> 5) |
322               ((tmp & 0x1c) >> 2) | ((tmp & 0x03) << 6);
323           size -= 3;
324         }
325         break;
326       }
327       case 32000:
328       {
329         /*  0                   1
330          *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
331          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
332          * |B B B B|A A A A|D D D D|C C C C| ...
333          * |0 1 2 3|0 1 2 3|0 1 2 3|0 1 2 3|
334          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
335          */
336         while (size > 0) {
337           tmp = *data;
338           *data++ = ((tmp & 0xf0) >> 4) | ((tmp & 0x0f) << 4);
339           size--;
340         }
341         break;
342       }
343       case 40000:
344       {
345         /*  0                   1                   2                   3                   4
346          *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
347          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
348          * |B B B|A A A A A|D|C C C C C|B B|E E E E|D D D D|G G|F F F F F|E|H H H H H|G G G|
349          * |2 3 4|0 1 2 3 4|4|0 1 2 3 4|0 1|1 2 3 4|0 1 2 3|3 4|0 1 2 3 4|0|0 1 2 3 4|0 1 2|   
350          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
351          */
352         while (size > 4) {
353           tmp = *data;
354           *data++ = ((tmp & 0xe0) >> 5) | ((tmp & 0x1f) << 3);
355           tmp = *data;
356           *data++ = ((tmp & 0x80) >> 7) |
357               ((tmp & 0x7c) >> 2) | ((tmp & 0x03) << 6);
358           tmp = *data;
359           *data++ = ((tmp & 0xf0) >> 4) | ((tmp & 0x0f) << 4);
360           tmp = *data;
361           *data++ = ((tmp & 0xc0) >> 6) |
362               ((tmp & 0x3e) << 2) | ((tmp & 0x01) << 7);
363           tmp = *data;
364           *data++ = ((tmp & 0xf8) >> 3) | ((tmp & 0x07) << 5);
365           size -= 5;
366         }
367         break;
368       }
369     }
370     gst_buffer_unmap (buffer, &map);
371   }
372
373   res =
374       GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->handle_buffer (payload,
375       buffer);
376
377   return res;
378 }
379
380 static void
381 gst_rtp_g726_pay_set_property (GObject * object, guint prop_id,
382     const GValue * value, GParamSpec * pspec)
383 {
384   GstRtpG726Pay *rtpg726pay;
385
386   rtpg726pay = GST_RTP_G726_PAY (object);
387
388   switch (prop_id) {
389     case PROP_FORCE_AAL2:
390       rtpg726pay->force_aal2 = g_value_get_boolean (value);
391       break;
392     default:
393       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
394       break;
395   }
396 }
397
398 static void
399 gst_rtp_g726_pay_get_property (GObject * object, guint prop_id,
400     GValue * value, GParamSpec * pspec)
401 {
402   GstRtpG726Pay *rtpg726pay;
403
404   rtpg726pay = GST_RTP_G726_PAY (object);
405
406   switch (prop_id) {
407     case PROP_FORCE_AAL2:
408       g_value_set_boolean (value, rtpg726pay->force_aal2);
409       break;
410     default:
411       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
412       break;
413   }
414 }
415
416 gboolean
417 gst_rtp_g726_pay_plugin_init (GstPlugin * plugin)
418 {
419   return gst_element_register (plugin, "rtpg726pay",
420       GST_RANK_SECONDARY, GST_TYPE_RTP_G726_PAY);
421 }