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 FFmpeg.
8 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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
35 #include "libavutil/common.h"
37 static av_cold int libgsm_encode_close(AVCodecContext *avctx) {
38 #if FF_API_OLD_ENCODE_AUDIO
39 av_freep(&avctx->coded_frame);
41 gsm_destroy(avctx->priv_data);
42 avctx->priv_data = NULL;
46 static av_cold int libgsm_encode_init(AVCodecContext *avctx) {
47 if (avctx->channels > 1) {
48 av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
53 if (avctx->sample_rate != 8000) {
54 av_log(avctx, AV_LOG_ERROR, "Sample rate 8000Hz required for GSM, got %dHz\n",
56 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
59 if (avctx->bit_rate != 13000 /* Official */ &&
60 avctx->bit_rate != 13200 /* Very common */ &&
61 avctx->bit_rate != 0 /* Unknown; a.o. mov does not set bitrate when decoding */ ) {
62 av_log(avctx, AV_LOG_ERROR, "Bitrate 13000bps required for GSM, got %dbps\n",
64 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
68 avctx->priv_data = gsm_create();
69 if (!avctx->priv_data)
72 switch(avctx->codec_id) {
74 avctx->frame_size = GSM_FRAME_SIZE;
75 avctx->block_align = GSM_BLOCK_SIZE;
77 case AV_CODEC_ID_GSM_MS: {
79 gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
80 avctx->frame_size = 2*GSM_FRAME_SIZE;
81 avctx->block_align = GSM_MS_BLOCK_SIZE;
85 #if FF_API_OLD_ENCODE_AUDIO
86 avctx->coded_frame= avcodec_alloc_frame();
87 if (!avctx->coded_frame)
93 libgsm_encode_close(avctx);
97 static int libgsm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
98 const AVFrame *frame, int *got_packet_ptr)
101 gsm_signal *samples = (gsm_signal *)frame->data[0];
102 struct gsm_state *state = avctx->priv_data;
104 if ((ret = ff_alloc_packet2(avctx, avpkt, avctx->block_align)))
107 switch(avctx->codec_id) {
108 case AV_CODEC_ID_GSM:
109 gsm_encode(state, samples, avpkt->data);
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);
121 #if CONFIG_LIBGSM_ENCODER
122 AVCodec ff_libgsm_encoder = {
124 .type = AVMEDIA_TYPE_AUDIO,
125 .id = AV_CODEC_ID_GSM,
126 .init = libgsm_encode_init,
127 .encode2 = libgsm_encode_frame,
128 .close = libgsm_encode_close,
129 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
130 AV_SAMPLE_FMT_NONE },
131 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
134 #if CONFIG_LIBGSM_MS_ENCODER
135 AVCodec ff_libgsm_ms_encoder = {
137 .type = AVMEDIA_TYPE_AUDIO,
138 .id = AV_CODEC_ID_GSM_MS,
139 .init = libgsm_encode_init,
140 .encode2 = libgsm_encode_frame,
141 .close = libgsm_encode_close,
142 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
143 AV_SAMPLE_FMT_NONE },
144 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
148 typedef struct LibGSMDecodeContext {
150 struct gsm_state *state;
151 } LibGSMDecodeContext;
153 static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
154 LibGSMDecodeContext *s = avctx->priv_data;
156 if (avctx->channels > 1) {
157 av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
162 if (!avctx->channels)
165 if (!avctx->sample_rate)
166 avctx->sample_rate = 8000;
168 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
170 s->state = gsm_create();
172 switch(avctx->codec_id) {
173 case AV_CODEC_ID_GSM:
174 avctx->frame_size = GSM_FRAME_SIZE;
175 avctx->block_align = GSM_BLOCK_SIZE;
177 case AV_CODEC_ID_GSM_MS: {
179 gsm_option(s->state, GSM_OPT_WAV49, &one);
180 avctx->frame_size = 2 * GSM_FRAME_SIZE;
181 avctx->block_align = GSM_MS_BLOCK_SIZE;
185 avcodec_get_frame_defaults(&s->frame);
186 avctx->coded_frame = &s->frame;
191 static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
192 LibGSMDecodeContext *s = avctx->priv_data;
194 gsm_destroy(s->state);
199 static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
200 int *got_frame_ptr, AVPacket *avpkt)
203 LibGSMDecodeContext *s = avctx->priv_data;
204 uint8_t *buf = avpkt->data;
205 int buf_size = avpkt->size;
208 if (buf_size < avctx->block_align) {
209 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
210 return AVERROR_INVALIDDATA;
213 /* get output buffer */
214 s->frame.nb_samples = avctx->frame_size;
215 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
216 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
219 samples = (int16_t *)s->frame.data[0];
221 for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
222 if ((ret = gsm_decode(s->state, buf, samples)) < 0)
224 buf += GSM_BLOCK_SIZE;
225 samples += GSM_FRAME_SIZE;
229 *(AVFrame *)data = s->frame;
231 return avctx->block_align;
234 static void libgsm_flush(AVCodecContext *avctx) {
235 LibGSMDecodeContext *s = avctx->priv_data;
238 gsm_destroy(s->state);
239 s->state = gsm_create();
240 if (avctx->codec_id == AV_CODEC_ID_GSM_MS)
241 gsm_option(s->state, GSM_OPT_WAV49, &one);
244 #if CONFIG_LIBGSM_DECODER
245 AVCodec ff_libgsm_decoder = {
247 .type = AVMEDIA_TYPE_AUDIO,
248 .id = AV_CODEC_ID_GSM,
249 .priv_data_size = sizeof(LibGSMDecodeContext),
250 .init = libgsm_decode_init,
251 .close = libgsm_decode_close,
252 .decode = libgsm_decode_frame,
253 .flush = libgsm_flush,
254 .capabilities = CODEC_CAP_DR1,
255 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
258 #if CONFIG_LIBGSM_MS_DECODER
259 AVCodec ff_libgsm_ms_decoder = {
261 .type = AVMEDIA_TYPE_AUDIO,
262 .id = AV_CODEC_ID_GSM_MS,
263 .priv_data_size = sizeof(LibGSMDecodeContext),
264 .init = libgsm_decode_init,
265 .close = libgsm_decode_close,
266 .decode = libgsm_decode_frame,
267 .flush = libgsm_flush,
268 .capabilities = CODEC_CAP_DR1,
269 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),