gstgsmenc.c 4.99 KB
Newer Older
1 2 3 4
/*
 * Farsight
 * GStreamer GSM encoder
 * Copyright (C) 2005 Philippe Khalaf <burger@speedy.org>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


23 24 25
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
26 27 28 29
#include <string.h>

#include "gstgsmenc.h"

30
GST_DEBUG_CATEGORY_STATIC (gsmenc_debug);
31 32
#define GST_CAT_DEFAULT (gsmenc_debug)

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
33
/* GSMEnc signals and args */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
34 35
enum
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
36 37 38 39
  /* FILL ME */
  LAST_SIGNAL
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
40 41
enum
{
42
  /* FILL ME */
43
  ARG_0
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
44 45
};

46 47 48 49 50 51
static gboolean gst_gsmenc_start (GstAudioEncoder * enc);
static gboolean gst_gsmenc_stop (GstAudioEncoder * enc);
static gboolean gst_gsmenc_set_format (GstAudioEncoder * enc,
    GstAudioInfo * info);
static GstFlowReturn gst_gsmenc_handle_frame (GstAudioEncoder * enc,
    GstBuffer * in_buf);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
52

David Schleef's avatar
David Schleef committed
53
static GstStaticPadTemplate gsmenc_src_template =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
54 55 56
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
57
    GST_STATIC_CAPS ("audio/x-gsm, " "rate = (int) 8000, " "channels = (int) 1")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
58
    );
David Schleef's avatar
David Schleef committed
59 60

static GstStaticPadTemplate gsmenc_sink_template =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
61 62 63 64
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("audio/x-raw-int, "
65 66 67
        "endianness = (int) BYTE_ORDER, "
        "signed = (boolean) true, "
        "width = (int) 16, "
68
        "depth = (int) 16, " "rate = (int) 8000, " "channels = (int) 1")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
69
    );
Iain Holmes's avatar
Iain Holmes committed
70

71 72 73
GST_BOILERPLATE (GstGSMEnc, gst_gsmenc, GstAudioEncoder,
    GST_TYPE_AUDIO_ENCODER);

Iain Holmes's avatar
Iain Holmes committed
74 75 76 77 78
static void
gst_gsmenc_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

David Schleef's avatar
David Schleef committed
79 80 81 82
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gsmenc_sink_template));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gsmenc_src_template));
83 84 85
  gst_element_class_set_details_simple (element_class, "GSM audio encoder",
      "Codec/Encoder/Audio",
      "Encodes GSM audio", "Philippe Khalaf <burger@speedy.org>");
Iain Holmes's avatar
Iain Holmes committed
86
}
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
87

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
88
static void
89
gst_gsmenc_class_init (GstGSMEncClass * klass)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
90
{
91
  GstAudioEncoderClass *base_class;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
92

93
  base_class = (GstAudioEncoderClass *) klass;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
94

95 96 97 98
  base_class->start = GST_DEBUG_FUNCPTR (gst_gsmenc_start);
  base_class->stop = GST_DEBUG_FUNCPTR (gst_gsmenc_stop);
  base_class->set_format = GST_DEBUG_FUNCPTR (gst_gsmenc_set_format);
  base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_gsmenc_handle_frame);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
99

100
  GST_DEBUG_CATEGORY_INIT (gsmenc_debug, "gsmenc", 0, "GSM Encoder");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
101 102 103
}

static void
104
gst_gsmenc_init (GstGSMEnc * gsmenc, GstGSMEncClass * klass)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
105
{
106
}
Wim Taymans's avatar
Wim Taymans committed
107

108 109 110 111 112
static gboolean
gst_gsmenc_start (GstAudioEncoder * enc)
{
  GstGSMEnc *gsmenc = GST_GSMENC (enc);
  gint use_wav49;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
113

114
  GST_DEBUG_OBJECT (enc, "start");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
115 116

  gsmenc->state = gsm_create ();
117

118
  /* turn off WAV49 handling */
Wim Taymans's avatar
Wim Taymans committed
119
  use_wav49 = 0;
120
  gsm_option (gsmenc->state, GSM_OPT_WAV49, &use_wav49);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
121

122
  return TRUE;
123 124
}

125 126
static gboolean
gst_gsmenc_stop (GstAudioEncoder * enc)
Wim Taymans's avatar
Wim Taymans committed
127
{
128
  GstGSMEnc *gsmenc = GST_GSMENC (enc);
Wim Taymans's avatar
Wim Taymans committed
129

130
  GST_DEBUG_OBJECT (enc, "stop");
Wim Taymans's avatar
Wim Taymans committed
131 132
  gsm_destroy (gsmenc->state);

133
  return TRUE;
Wim Taymans's avatar
Wim Taymans committed
134 135
}

Wim Taymans's avatar
Wim Taymans committed
136
static gboolean
137
gst_gsmenc_set_format (GstAudioEncoder * benc, GstAudioInfo * info)
Wim Taymans's avatar
Wim Taymans committed
138 139 140 141
{
  GstCaps *srccaps;

  srccaps = gst_static_pad_template_get_caps (&gsmenc_src_template);
142
  gst_pad_set_caps (GST_AUDIO_ENCODER_SRC_PAD (benc), srccaps);
Wim Taymans's avatar
Wim Taymans committed
143

144 145 146 147
  /* report needs to base class */
  gst_audio_encoder_set_frame_samples_min (benc, 160);
  gst_audio_encoder_set_frame_samples_max (benc, 160);
  gst_audio_encoder_set_frame_max (benc, 1);
Wim Taymans's avatar
Wim Taymans committed
148 149 150 151

  return TRUE;
}

152
static GstFlowReturn
153
gst_gsmenc_handle_frame (GstAudioEncoder * benc, GstBuffer * buffer)
154 155 156
{
  GstGSMEnc *gsmenc;
  gsm_signal *data;
157
  GstFlowReturn ret = GST_FLOW_OK;
158
  GstBuffer *outbuf;
159

160
  gsmenc = GST_GSMENC (benc);
Wim Taymans's avatar
Wim Taymans committed
161

162 163 164 165
  /* we don't deal with squeezing remnants, so simply discard those */
  if (G_UNLIKELY (buffer == NULL)) {
    GST_DEBUG_OBJECT (gsmenc, "no data");
    goto done;
Wim Taymans's avatar
Wim Taymans committed
166 167
  }

168 169 170 171 172 173
  if (G_UNLIKELY (GST_BUFFER_SIZE (buffer) < 320)) {
    GST_DEBUG_OBJECT (gsmenc, "discarding trailing data %d",
        GST_BUFFER_SIZE (buffer));
    ret = gst_audio_encoder_finish_frame (benc, NULL, -1);
    goto done;
  }
174

175
  outbuf = gst_buffer_new_and_alloc (33 * sizeof (gsm_byte));
Wim Taymans's avatar
Wim Taymans committed
176

177 178 179
  /* encode 160 16-bit samples into 33 bytes */
  data = (gsm_signal *) GST_BUFFER_DATA (buffer);
  gsm_encode (gsmenc->state, data, (gsm_byte *) GST_BUFFER_DATA (outbuf));
Wim Taymans's avatar
Wim Taymans committed
180

181
  GST_LOG_OBJECT (gsmenc, "encoded to %d bytes", GST_BUFFER_SIZE (outbuf));
182

183
  ret = gst_audio_encoder_finish_frame (benc, outbuf, 160);
184

185
done:
186
  return ret;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
187
}