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>
6 * This file is part of Libav.
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.
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.
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
25 * Interface to libgsm for gsm encoding/decoding
28 // The idiosyncrasies of GSM-in-WAV are explained at http://kbs.cs.tu-berlin.de/~jutta/toast.html
36 static av_cold int libgsm_encode_init(AVCodecContext *avctx) {
37 if (avctx->channels > 1) {
38 av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
43 if (avctx->sample_rate != 8000) {
44 av_log(avctx, AV_LOG_ERROR, "Sample rate 8000Hz required for GSM, got %dHz\n",
46 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
49 if (avctx->bit_rate != 13000 /* Official */ &&
50 avctx->bit_rate != 13200 /* Very common */ &&
51 avctx->bit_rate != 0 /* Unknown; a.o. mov does not set bitrate when decoding */ ) {
52 av_log(avctx, AV_LOG_ERROR, "Bitrate 13000bps required for GSM, got %dbps\n",
54 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
58 avctx->priv_data = gsm_create();
60 switch(avctx->codec_id) {
62 avctx->frame_size = GSM_FRAME_SIZE;
63 avctx->block_align = GSM_BLOCK_SIZE;
65 case CODEC_ID_GSM_MS: {
67 gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
68 avctx->frame_size = 2*GSM_FRAME_SIZE;
69 avctx->block_align = GSM_MS_BLOCK_SIZE;
73 #if FF_API_OLD_ENCODE_AUDIO
74 avctx->coded_frame= avcodec_alloc_frame();
75 if (!avctx->coded_frame) {
76 gsm_destroy(avctx->priv_data);
77 return AVERROR(ENOMEM);
84 static av_cold int libgsm_encode_close(AVCodecContext *avctx) {
85 #if FF_API_OLD_ENCODE_AUDIO
86 av_freep(&avctx->coded_frame);
88 gsm_destroy(avctx->priv_data);
89 avctx->priv_data = NULL;
93 static int libgsm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
94 const AVFrame *frame, int *got_packet_ptr)
97 gsm_signal *samples = (gsm_signal *)frame->data[0];
98 struct gsm_state *state = avctx->priv_data;
100 if ((ret = ff_alloc_packet(avpkt, avctx->block_align))) {
101 av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
105 switch(avctx->codec_id) {
107 gsm_encode(state, samples, avpkt->data);
109 case CODEC_ID_GSM_MS:
110 gsm_encode(state, samples, avpkt->data);
111 gsm_encode(state, samples + GSM_FRAME_SIZE, avpkt->data + 32);
119 AVCodec ff_libgsm_encoder = {
121 .type = AVMEDIA_TYPE_AUDIO,
123 .init = libgsm_encode_init,
124 .encode2 = libgsm_encode_frame,
125 .close = libgsm_encode_close,
126 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
127 AV_SAMPLE_FMT_NONE },
128 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
131 AVCodec ff_libgsm_ms_encoder = {
133 .type = AVMEDIA_TYPE_AUDIO,
134 .id = CODEC_ID_GSM_MS,
135 .init = libgsm_encode_init,
136 .encode2 = libgsm_encode_frame,
137 .close = libgsm_encode_close,
138 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
139 AV_SAMPLE_FMT_NONE },
140 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
143 typedef struct LibGSMDecodeContext {
145 struct gsm_state *state;
146 } LibGSMDecodeContext;
148 static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
149 LibGSMDecodeContext *s = avctx->priv_data;
151 if (avctx->channels > 1) {
152 av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
157 if (!avctx->channels)
160 if (!avctx->sample_rate)
161 avctx->sample_rate = 8000;
163 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
165 s->state = gsm_create();
167 switch(avctx->codec_id) {
169 avctx->frame_size = GSM_FRAME_SIZE;
170 avctx->block_align = GSM_BLOCK_SIZE;
172 case CODEC_ID_GSM_MS: {
174 gsm_option(s->state, GSM_OPT_WAV49, &one);
175 avctx->frame_size = 2 * GSM_FRAME_SIZE;
176 avctx->block_align = GSM_MS_BLOCK_SIZE;
180 avcodec_get_frame_defaults(&s->frame);
181 avctx->coded_frame = &s->frame;
186 static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
187 LibGSMDecodeContext *s = avctx->priv_data;
189 gsm_destroy(s->state);
194 static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
195 int *got_frame_ptr, AVPacket *avpkt)
198 LibGSMDecodeContext *s = avctx->priv_data;
199 uint8_t *buf = avpkt->data;
200 int buf_size = avpkt->size;
203 if (buf_size < avctx->block_align) {
204 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
205 return AVERROR_INVALIDDATA;
208 /* get output buffer */
209 s->frame.nb_samples = avctx->frame_size;
210 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
211 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
214 samples = (int16_t *)s->frame.data[0];
216 for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
217 if ((ret = gsm_decode(s->state, buf, samples)) < 0)
219 buf += GSM_BLOCK_SIZE;
220 samples += GSM_FRAME_SIZE;
224 *(AVFrame *)data = s->frame;
226 return avctx->block_align;
229 static void libgsm_flush(AVCodecContext *avctx) {
230 LibGSMDecodeContext *s = avctx->priv_data;
233 gsm_destroy(s->state);
234 s->state = gsm_create();
235 if (avctx->codec_id == CODEC_ID_GSM_MS)
236 gsm_option(s->state, GSM_OPT_WAV49, &one);
239 AVCodec ff_libgsm_decoder = {
241 .type = AVMEDIA_TYPE_AUDIO,
243 .priv_data_size = sizeof(LibGSMDecodeContext),
244 .init = libgsm_decode_init,
245 .close = libgsm_decode_close,
246 .decode = libgsm_decode_frame,
247 .flush = libgsm_flush,
248 .capabilities = CODEC_CAP_DR1,
249 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
252 AVCodec ff_libgsm_ms_decoder = {
254 .type = AVMEDIA_TYPE_AUDIO,
255 .id = CODEC_ID_GSM_MS,
256 .priv_data_size = sizeof(LibGSMDecodeContext),
257 .init = libgsm_decode_init,
258 .close = libgsm_decode_close,
259 .decode = libgsm_decode_frame,
260 .flush = libgsm_flush,
261 .capabilities = CODEC_CAP_DR1,
262 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),