"Fossies" - the Fresh Open Source Software Archive

Member "gst-plugins-good-1.20.3/gst/law/alaw-decode.c" (15 Jun 2022, 7944 Bytes) of package /linux/misc/gst-plugins-good-1.20.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "alaw-decode.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.18.6_vs_1.20.0.

    1 /* GStreamer A-Law to PCM conversion
    2  * Copyright (C) 2000 by Abramo Bagnara <abramo@alsa-project.org>
    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.1 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., 51 Franklin St, Fifth Floor,
   17  * Boston, MA 02110-1301, USA.
   18  */
   19 /**
   20  * SECTION:element-alawdec
   21  * @title: alawdec
   22  *
   23  * This element decodes alaw audio. Alaw coding is also known as G.711.
   24  */
   25 
   26 #ifdef HAVE_CONFIG_H
   27 #include "config.h"
   28 #endif
   29 
   30 #include "alaw-decode.h"
   31 
   32 extern GstStaticPadTemplate alaw_dec_src_factory;
   33 extern GstStaticPadTemplate alaw_dec_sink_factory;
   34 
   35 GST_DEBUG_CATEGORY_STATIC (alaw_dec_debug);
   36 #define GST_CAT_DEFAULT alaw_dec_debug
   37 
   38 static gboolean gst_alaw_dec_set_format (GstAudioDecoder * dec, GstCaps * caps);
   39 static GstFlowReturn gst_alaw_dec_handle_frame (GstAudioDecoder * dec,
   40     GstBuffer * buffer);
   41 
   42 #define gst_alaw_dec_parent_class parent_class
   43 G_DEFINE_TYPE (GstALawDec, gst_alaw_dec, GST_TYPE_AUDIO_DECODER);
   44 GST_ELEMENT_REGISTER_DEFINE (alawdec, "alawdec", GST_RANK_PRIMARY,
   45     GST_TYPE_ALAW_DEC);
   46 
   47 /* some day we might have defines in gstconfig.h that tell us about the
   48  * desired cpu/memory/binary size trade-offs */
   49 #define GST_ALAW_DEC_USE_TABLE
   50 
   51 #ifdef GST_ALAW_DEC_USE_TABLE
   52 
   53 static const gint alaw_to_s16_table[256] = {
   54   -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
   55   -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
   56   -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
   57   -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
   58   -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
   59   -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
   60   -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
   61   -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
   62   -344, -328, -376, -360, -280, -264, -312, -296,
   63   -472, -456, -504, -488, -408, -392, -440, -424,
   64   -88, -72, -120, -104, -24, -8, -56, -40,
   65   -216, -200, -248, -232, -152, -136, -184, -168,
   66   -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
   67   -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
   68   -688, -656, -752, -720, -560, -528, -624, -592,
   69   -944, -912, -1008, -976, -816, -784, -880, -848,
   70   5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
   71   7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
   72   2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
   73   3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
   74   22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
   75   30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
   76   11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
   77   15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
   78   344, 328, 376, 360, 280, 264, 312, 296,
   79   472, 456, 504, 488, 408, 392, 440, 424,
   80   88, 72, 120, 104, 24, 8, 56, 40,
   81   216, 200, 248, 232, 152, 136, 184, 168,
   82   1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
   83   1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
   84   688, 656, 752, 720, 560, 528, 624, 592,
   85   944, 912, 1008, 976, 816, 784, 880, 848
   86 };
   87 
   88 static inline gint
   89 alaw_to_s16 (guint8 a_val)
   90 {
   91   return alaw_to_s16_table[a_val];
   92 }
   93 
   94 #else /* GST_ALAW_DEC_USE_TABLE */
   95 
   96 static inline gint
   97 alaw_to_s16 (guint8 a_val)
   98 {
   99   gint t;
  100   gint seg;
  101 
  102   a_val ^= 0x55;
  103   t = a_val & 0x7f;
  104   if (t < 16)
  105     t = (t << 4) + 8;
  106   else {
  107     seg = (t >> 4) & 0x07;
  108     t = ((t & 0x0f) << 4) + 0x108;
  109     t <<= seg - 1;
  110   }
  111   return ((a_val & 0x80) ? t : -t);
  112 }
  113 
  114 #endif /* GST_ALAW_DEC_USE_TABLE */
  115 
  116 GstStaticPadTemplate alaw_dec_src_factory = GST_STATIC_PAD_TEMPLATE ("src",
  117     GST_PAD_SRC,
  118     GST_PAD_ALWAYS,
  119     GST_STATIC_CAPS ("audio/x-raw, "
  120         "format = (string) " GST_AUDIO_NE (S16) ", "
  121         "layout = (string) interleaved, "
  122         "rate = (int) [ 8000, 192000 ], " "channels = (int) [ 1, 2 ]")
  123     );
  124 
  125 GstStaticPadTemplate alaw_dec_sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
  126     GST_PAD_SINK,
  127     GST_PAD_ALWAYS,
  128     GST_STATIC_CAPS ("audio/x-alaw, "
  129         "rate = [ 8000 , 192000 ], " "channels = [ 1 , 2 ]")
  130     );
  131 
  132 static gboolean
  133 gst_alaw_dec_set_format (GstAudioDecoder * dec, GstCaps * caps)
  134 {
  135   GstALawDec *alawdec = GST_ALAW_DEC (dec);
  136   GstStructure *structure;
  137   int rate, channels;
  138   GstAudioInfo info;
  139 
  140   structure = gst_caps_get_structure (caps, 0);
  141   if (!structure) {
  142     GST_ERROR_OBJECT (dec, "failed to get structure from caps");
  143     return FALSE;
  144   }
  145 
  146   if (!gst_structure_get_int (structure, "rate", &rate)) {
  147     GST_ERROR_OBJECT (dec, "failed to find field rate in input caps");
  148     return FALSE;
  149   }
  150 
  151   if (!gst_structure_get_int (structure, "channels", &channels)) {
  152     GST_ERROR_OBJECT (dec, "failed to find field channels in input caps");
  153     return FALSE;
  154   }
  155 
  156   gst_audio_info_init (&info);
  157   gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16, rate, channels, NULL);
  158 
  159   GST_DEBUG_OBJECT (alawdec, "rate=%d, channels=%d", rate, channels);
  160 
  161   return gst_audio_decoder_set_output_format (dec, &info);
  162 }
  163 
  164 static GstFlowReturn
  165 gst_alaw_dec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
  166 {
  167   GstMapInfo inmap, outmap;
  168   gint16 *linear_data;
  169   guint8 *alaw_data;
  170   gsize alaw_size, linear_size;
  171   GstBuffer *outbuf;
  172   gint i;
  173 
  174   if (!buffer) {
  175     return GST_FLOW_OK;
  176   }
  177 
  178   if (!gst_buffer_map (buffer, &inmap, GST_MAP_READ)) {
  179     GST_ERROR_OBJECT (dec, "failed to map input buffer");
  180     goto error_failed_map_input_buffer;
  181   }
  182 
  183   alaw_data = inmap.data;
  184   alaw_size = inmap.size;
  185 
  186   linear_size = alaw_size * 2;
  187 
  188   outbuf = gst_audio_decoder_allocate_output_buffer (dec, linear_size);
  189   if (!gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE)) {
  190     GST_ERROR_OBJECT (dec, "failed to map input buffer");
  191     goto error_failed_map_output_buffer;
  192   }
  193 
  194   linear_data = (gint16 *) outmap.data;
  195   for (i = 0; i < alaw_size; i++) {
  196     linear_data[i] = alaw_to_s16 (alaw_data[i]);
  197   }
  198 
  199   gst_buffer_unmap (outbuf, &outmap);
  200   gst_buffer_unmap (buffer, &inmap);
  201 
  202   return gst_audio_decoder_finish_frame (dec, outbuf, -1);
  203 
  204 error_failed_map_output_buffer:
  205   gst_buffer_unref (outbuf);
  206   gst_buffer_unmap (buffer, &inmap);
  207 
  208 error_failed_map_input_buffer:
  209   return GST_FLOW_ERROR;
  210 }
  211 
  212 static gboolean
  213 gst_alaw_dec_start (GstAudioDecoder * dec)
  214 {
  215   gst_audio_decoder_set_estimate_rate (dec, TRUE);
  216 
  217   return TRUE;
  218 }
  219 
  220 static void
  221 gst_alaw_dec_class_init (GstALawDecClass * klass)
  222 {
  223   GstElementClass *element_class = (GstElementClass *) klass;
  224   GstAudioDecoderClass *audiodec_class = GST_AUDIO_DECODER_CLASS (klass);
  225 
  226   gst_element_class_add_static_pad_template (element_class,
  227       &alaw_dec_src_factory);
  228   gst_element_class_add_static_pad_template (element_class,
  229       &alaw_dec_sink_factory);
  230 
  231   audiodec_class->start = GST_DEBUG_FUNCPTR (gst_alaw_dec_start);
  232   audiodec_class->set_format = GST_DEBUG_FUNCPTR (gst_alaw_dec_set_format);
  233   audiodec_class->handle_frame = GST_DEBUG_FUNCPTR (gst_alaw_dec_handle_frame);
  234 
  235   gst_element_class_set_static_metadata (element_class, "A Law audio decoder",
  236       "Codec/Decoder/Audio",
  237       "Convert 8bit A law to 16bit PCM",
  238       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
  239 
  240   GST_DEBUG_CATEGORY_INIT (alaw_dec_debug, "alawdec", 0, "A Law audio decoder");
  241 }
  242 
  243 static void
  244 gst_alaw_dec_init (GstALawDec * alawdec)
  245 {
  246   gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (alawdec), TRUE);
  247   gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
  248       (alawdec), TRUE);
  249   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (alawdec));
  250 }