libgsm.c
Go to the documentation of this file.
1 /*
2  * Interface to libgsm for gsm encoding/decoding
3  * Copyright (c) 2005 Alban Bedel <albeu@free.fr>
4  * Copyright (c) 2006, 2007 Michel Bardiaux <mbardiaux@mediaxim.be>
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
28 // The idiosyncrasies of GSM-in-WAV are explained at http://kbs.cs.tu-berlin.de/~jutta/toast.html
29 
30 #include <gsm/gsm.h>
31 
33 #include "libavutil/common.h"
34 #include "avcodec.h"
35 #include "internal.h"
36 #include "gsm.h"
37 
39  if (avctx->channels > 1) {
40  av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
41  avctx->channels);
42  return -1;
43  }
44 
45  if (avctx->sample_rate != 8000) {
46  av_log(avctx, AV_LOG_ERROR, "Sample rate 8000Hz required for GSM, got %dHz\n",
47  avctx->sample_rate);
49  return -1;
50  }
51  if (avctx->bit_rate != 13000 /* Official */ &&
52  avctx->bit_rate != 13200 /* Very common */ &&
53  avctx->bit_rate != 0 /* Unknown; a.o. mov does not set bitrate when decoding */ ) {
54  av_log(avctx, AV_LOG_ERROR, "Bitrate 13000bps required for GSM, got %dbps\n",
55  avctx->bit_rate);
57  return -1;
58  }
59 
60  avctx->priv_data = gsm_create();
61 
62  switch(avctx->codec_id) {
63  case AV_CODEC_ID_GSM:
64  avctx->frame_size = GSM_FRAME_SIZE;
65  avctx->block_align = GSM_BLOCK_SIZE;
66  break;
67  case AV_CODEC_ID_GSM_MS: {
68  int one = 1;
69  gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
70  avctx->frame_size = 2*GSM_FRAME_SIZE;
72  }
73  }
74 
75 #if FF_API_OLD_ENCODE_AUDIO
77  if (!avctx->coded_frame) {
78  gsm_destroy(avctx->priv_data);
79  return AVERROR(ENOMEM);
80  }
81 #endif
82 
83  return 0;
84 }
85 
87 #if FF_API_OLD_ENCODE_AUDIO
88  av_freep(&avctx->coded_frame);
89 #endif
90  gsm_destroy(avctx->priv_data);
91  avctx->priv_data = NULL;
92  return 0;
93 }
94 
95 static int libgsm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
96  const AVFrame *frame, int *got_packet_ptr)
97 {
98  int ret;
99  gsm_signal *samples = (gsm_signal *)frame->data[0];
100  struct gsm_state *state = avctx->priv_data;
101 
102  if ((ret = ff_alloc_packet(avpkt, avctx->block_align))) {
103  av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
104  return ret;
105  }
106 
107  switch(avctx->codec_id) {
108  case AV_CODEC_ID_GSM:
109  gsm_encode(state, samples, avpkt->data);
110  break;
111  case AV_CODEC_ID_GSM_MS:
112  gsm_encode(state, samples, avpkt->data);
113  gsm_encode(state, samples + GSM_FRAME_SIZE, avpkt->data + 32);
114  }
115 
116  *got_packet_ptr = 1;
117  return 0;
118 }
119 
120 
122  .name = "libgsm",
123  .type = AVMEDIA_TYPE_AUDIO,
124  .id = AV_CODEC_ID_GSM,
125  .init = libgsm_encode_init,
126  .encode2 = libgsm_encode_frame,
127  .close = libgsm_encode_close,
128  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
130  .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
131 };
132 
134  .name = "libgsm_ms",
135  .type = AVMEDIA_TYPE_AUDIO,
136  .id = AV_CODEC_ID_GSM_MS,
137  .init = libgsm_encode_init,
138  .encode2 = libgsm_encode_frame,
139  .close = libgsm_encode_close,
140  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
142  .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
143 };
144 
145 typedef struct LibGSMDecodeContext {
147  struct gsm_state *state;
149 
151  LibGSMDecodeContext *s = avctx->priv_data;
152 
153  avctx->channels = 1;
155  avctx->sample_rate = 8000;
156  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
157 
158  s->state = gsm_create();
159 
160  switch(avctx->codec_id) {
161  case AV_CODEC_ID_GSM:
162  avctx->frame_size = GSM_FRAME_SIZE;
163  avctx->block_align = GSM_BLOCK_SIZE;
164  break;
165  case AV_CODEC_ID_GSM_MS: {
166  int one = 1;
167  gsm_option(s->state, GSM_OPT_WAV49, &one);
168  avctx->frame_size = 2 * GSM_FRAME_SIZE;
170  }
171  }
172 
174  avctx->coded_frame = &s->frame;
175 
176  return 0;
177 }
178 
180  LibGSMDecodeContext *s = avctx->priv_data;
181 
182  gsm_destroy(s->state);
183  s->state = NULL;
184  return 0;
185 }
186 
187 static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
188  int *got_frame_ptr, AVPacket *avpkt)
189 {
190  int i, ret;
191  LibGSMDecodeContext *s = avctx->priv_data;
192  uint8_t *buf = avpkt->data;
193  int buf_size = avpkt->size;
194  int16_t *samples;
195 
196  if (buf_size < avctx->block_align) {
197  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
198  return AVERROR_INVALIDDATA;
199  }
200 
201  /* get output buffer */
202  s->frame.nb_samples = avctx->frame_size;
203  if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
204  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
205  return ret;
206  }
207  samples = (int16_t *)s->frame.data[0];
208 
209  for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
210  if ((ret = gsm_decode(s->state, buf, samples)) < 0)
211  return -1;
212  buf += GSM_BLOCK_SIZE;
213  samples += GSM_FRAME_SIZE;
214  }
215 
216  *got_frame_ptr = 1;
217  *(AVFrame *)data = s->frame;
218 
219  return avctx->block_align;
220 }
221 
222 static void libgsm_flush(AVCodecContext *avctx) {
223  LibGSMDecodeContext *s = avctx->priv_data;
224  int one = 1;
225 
226  gsm_destroy(s->state);
227  s->state = gsm_create();
228  if (avctx->codec_id == AV_CODEC_ID_GSM_MS)
229  gsm_option(s->state, GSM_OPT_WAV49, &one);
230 }
231 
233  .name = "libgsm",
234  .type = AVMEDIA_TYPE_AUDIO,
235  .id = AV_CODEC_ID_GSM,
236  .priv_data_size = sizeof(LibGSMDecodeContext),
240  .flush = libgsm_flush,
241  .capabilities = CODEC_CAP_DR1,
242  .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
243 };
244 
246  .name = "libgsm_ms",
247  .type = AVMEDIA_TYPE_AUDIO,
248  .id = AV_CODEC_ID_GSM_MS,
249  .priv_data_size = sizeof(LibGSMDecodeContext),
253  .flush = libgsm_flush,
254  .capabilities = CODEC_CAP_DR1,
255  .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
256 };
static int16_t * samples
#define GSM_FRAME_SIZE
Definition: gsm.h:29
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
int size
Definition: avcodec.h:916
signed 16 bits
Definition: samplefmt.h:52
AVCodec.
Definition: avcodec.h:2960
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2141
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2112
uint8_t
#define GSM_MS_BLOCK_SIZE
Definition: gsm.h:26
#define GSM_BLOCK_SIZE
Definition: gsm.h:25
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
AVCodec ff_libgsm_ms_encoder
Definition: libgsm.c:133
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
AVCodec ff_libgsm_encoder
Definition: libgsm.c:121
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2165
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:618
static av_cold int libgsm_encode_close(AVCodecContext *avctx)
Definition: libgsm.c:86
int bit_rate
the average bitrate
Definition: avcodec.h:1404
audio channel layout utility functions
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
AVCodec ff_libgsm_ms_decoder
Definition: libgsm.c:245
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:880
static av_cold int libgsm_decode_close(AVCodecContext *avctx)
Definition: libgsm.c:179
struct gsm_state * state
Definition: libgsm.c:147
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2124
NULL
Definition: eval.c:52
external API header
enum AVCodecID codec_id
Definition: avcodec.h:1350
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:63
int sample_rate
samples per second
Definition: avcodec.h:2104
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:604
static uint32_t state
Definition: trasher.c:27
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
static void libgsm_flush(AVCodecContext *avctx)
Definition: libgsm.c:222
common internal api header.
common internal and external API header
static av_cold int libgsm_encode_init(AVCodecContext *avctx)
Definition: libgsm.c:38
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
Definition: alsdec.c:1772
static int libgsm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libgsm.c:95
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
void * priv_data
Definition: avcodec.h:1382
static av_cold int libgsm_decode_init(AVCodecContext *avctx)
Definition: libgsm.c:150
static int libgsm_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: libgsm.c:187
as in Berlin toast format
Definition: avcodec.h:365
int channels
number of audio channels
Definition: avcodec.h:2105
struct LibGSMDecodeContext LibGSMDecodeContext
#define AV_CH_LAYOUT_MONO
AVCodec ff_libgsm_decoder
Definition: libgsm.c:232
This structure stores compressed data.
Definition: avcodec.h:898
int nb_samples
number of audio samples (per channel) described by this frame
Definition: avcodec.h:1042
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2547
for(j=16;j >0;--j)
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)