gstgsmenc.c 5.03 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
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
64 65 66 67
    GST_STATIC_CAPS ("audio/x-raw, "
        "format = (string) " GST_AUDIO_NE (S16) ", "
        "layout = (string) interleaved, "
        "rate = (int) 8000, channels = (int) 1")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
68
    );
Iain Holmes's avatar
Iain Holmes committed
69

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
70
G_DEFINE_TYPE (GstGSMEnc, gst_gsmenc, GST_TYPE_AUDIO_ENCODER);
71

Iain Holmes's avatar
Iain Holmes committed
72
static void
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
73
gst_gsmenc_class_init (GstGSMEncClass * klass)
Iain Holmes's avatar
Iain Holmes committed
74
{
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
75 76 77 78 79
  GstElementClass *element_class;
  GstAudioEncoderClass *base_class;

  element_class = (GstElementClass *) klass;
  base_class = (GstAudioEncoderClass *) klass;
Iain Holmes's avatar
Iain Holmes committed
80

David Schleef's avatar
David Schleef committed
81 82 83 84
  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));
85 86 87
  gst_element_class_set_details_simple (element_class, "GSM audio encoder",
      "Codec/Encoder/Audio",
      "Encodes GSM audio", "Philippe Khalaf <burger@speedy.org>");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
88

89 90 91 92
  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
93

94
  GST_DEBUG_CATEGORY_INIT (gsmenc_debug, "gsmenc", 0, "GSM Encoder");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
95 96 97
}

static void
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
98
gst_gsmenc_init (GstGSMEnc * gsmenc)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
99
{
100
}
Wim Taymans's avatar
Wim Taymans committed
101

102 103 104 105 106
static gboolean
gst_gsmenc_start (GstAudioEncoder * enc)
{
  GstGSMEnc *gsmenc = GST_GSMENC (enc);
  gint use_wav49;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
107

108
  GST_DEBUG_OBJECT (enc, "start");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
109 110

  gsmenc->state = gsm_create ();
111

112
  /* turn off WAV49 handling */
Wim Taymans's avatar
Wim Taymans committed
113
  use_wav49 = 0;
114
  gsm_option (gsmenc->state, GSM_OPT_WAV49, &use_wav49);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
115

116
  return TRUE;
117 118
}

119 120
static gboolean
gst_gsmenc_stop (GstAudioEncoder * enc)
Wim Taymans's avatar
Wim Taymans committed
121
{
122
  GstGSMEnc *gsmenc = GST_GSMENC (enc);
Wim Taymans's avatar
Wim Taymans committed
123

124
  GST_DEBUG_OBJECT (enc, "stop");
Wim Taymans's avatar
Wim Taymans committed
125 126
  gsm_destroy (gsmenc->state);

127
  return TRUE;
Wim Taymans's avatar
Wim Taymans committed
128 129
}

Wim Taymans's avatar
Wim Taymans committed
130
static gboolean
131
gst_gsmenc_set_format (GstAudioEncoder * benc, GstAudioInfo * info)
Wim Taymans's avatar
Wim Taymans committed
132 133 134 135
{
  GstCaps *srccaps;

  srccaps = gst_static_pad_template_get_caps (&gsmenc_src_template);
136
  gst_audio_encoder_set_output_format (GST_AUDIO_ENCODER (benc), srccaps);
Wim Taymans's avatar
Wim Taymans committed
137

138 139 140 141
  /* 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
142 143 144 145

  return TRUE;
}

146
static GstFlowReturn
147
gst_gsmenc_handle_frame (GstAudioEncoder * benc, GstBuffer * buffer)
148 149 150
{
  GstGSMEnc *gsmenc;
  gsm_signal *data;
151
  GstFlowReturn ret = GST_FLOW_OK;
152
  GstBuffer *outbuf;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
153
  GstMapInfo map, omap;
154

155
  gsmenc = GST_GSMENC (benc);
Wim Taymans's avatar
Wim Taymans committed
156

157 158 159 160
  /* 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
161 162
  }

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
163 164 165 166
  gst_buffer_map (buffer, &map, GST_MAP_READ);
  if (G_UNLIKELY (map.size < 320)) {
    GST_DEBUG_OBJECT (gsmenc, "discarding trailing data %d", (gint) map.size);
    gst_buffer_unmap (buffer, &map);
167 168 169
    ret = gst_audio_encoder_finish_frame (benc, NULL, -1);
    goto done;
  }
170

171
  outbuf = gst_buffer_new_and_alloc (33 * sizeof (gsm_byte));
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
172
  gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
Wim Taymans's avatar
Wim Taymans committed
173

174
  /* encode 160 16-bit samples into 33 bytes */
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
175 176
  data = (gsm_signal *) map.data;
  gsm_encode (gsmenc->state, data, (gsm_byte *) omap.data);
Wim Taymans's avatar
Wim Taymans committed
177

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
178 179 180
  GST_LOG_OBJECT (gsmenc, "encoded to %d bytes", (gint) omap.size);
  gst_buffer_unmap (buffer, &map);
  gst_buffer_unmap (buffer, &omap);
181

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

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