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
36 static av_cold int libgsm_encode_close(AVCodecContext *avctx) {
37 #if FF_API_OLD_ENCODE_AUDIO
38 av_freep(&avctx->coded_frame);
40 gsm_destroy(avctx->priv_data);
41 avctx->priv_data = NULL;
45 static av_cold int libgsm_encode_init(AVCodecContext *avctx) {
46 if (avctx->channels > 1) {
47 av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
52 if (avctx->sample_rate != 8000) {
53 av_log(avctx, AV_LOG_ERROR, "Sample rate 8000Hz required for GSM, got %dHz\n",
55 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
58 if (avctx->bit_rate != 13000 /* Official */ &&
59 avctx->bit_rate != 13200 /* Very common */ &&
60 avctx->bit_rate != 0 /* Unknown; a.o. mov does not set bitrate when decoding */ ) {
61 av_log(avctx, AV_LOG_ERROR, "Bitrate 13000bps required for GSM, got %dbps\n",
63 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
67 avctx->priv_data = gsm_create();
68 if (!avctx->priv_data)
71 switch(avctx->codec_id) {
73 avctx->frame_size = GSM_FRAME_SIZE;
74 avctx->block_align = GSM_BLOCK_SIZE;
76 case CODEC_ID_GSM_MS: {
78 gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
79 avctx->frame_size = 2*GSM_FRAME_SIZE;
80 avctx->block_align = GSM_MS_BLOCK_SIZE;
84 #if FF_API_OLD_ENCODE_AUDIO
85 avctx->coded_frame= avcodec_alloc_frame();
86 if (!avctx->coded_frame)
92 libgsm_encode_close(avctx);
96 static int libgsm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
97 const AVFrame *frame, int *got_packet_ptr)
100 gsm_signal *samples = (gsm_signal *)frame->data[0];
101 struct gsm_state *state = avctx->priv_data;
103 if ((ret = ff_alloc_packet2(avctx, avpkt, avctx->block_align)))
106 switch(avctx->codec_id) {
108 gsm_encode(state, samples, avpkt->data);
110 case CODEC_ID_GSM_MS:
111 gsm_encode(state, samples, avpkt->data);
112 gsm_encode(state, samples + GSM_FRAME_SIZE, avpkt->data + 32);
120 AVCodec ff_libgsm_encoder = {
122 .type = AVMEDIA_TYPE_AUDIO,
124 .init = libgsm_encode_init,
125 .encode2 = libgsm_encode_frame,
126 .close = libgsm_encode_close,
127 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,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,AV_SAMPLE_FMT_NONE},
139 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
142 typedef struct LibGSMDecodeContext {
144 struct gsm_state *state;
145 } LibGSMDecodeContext;
147 static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
148 LibGSMDecodeContext *s = avctx->priv_data;
150 if (avctx->channels > 1) {
151 av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
156 if (!avctx->channels)
159 if (!avctx->sample_rate)
160 avctx->sample_rate = 8000;
162 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
164 s->state = gsm_create();
166 switch(avctx->codec_id) {
168 avctx->frame_size = GSM_FRAME_SIZE;
169 avctx->block_align = GSM_BLOCK_SIZE;
171 case CODEC_ID_GSM_MS: {
173 gsm_option(s->state, GSM_OPT_WAV49, &one);
174 avctx->frame_size = 2 * GSM_FRAME_SIZE;
175 avctx->block_align = GSM_MS_BLOCK_SIZE;
179 avcodec_get_frame_defaults(&s->frame);
180 avctx->coded_frame = &s->frame;
185 static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
186 LibGSMDecodeContext *s = avctx->priv_data;
188 gsm_destroy(s->state);
193 static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
194 int *got_frame_ptr, AVPacket *avpkt)
197 LibGSMDecodeContext *s = avctx->priv_data;
198 uint8_t *buf = avpkt->data;
199 int buf_size = avpkt->size;
202 if (buf_size < avctx->block_align) {
203 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
204 return AVERROR_INVALIDDATA;
207 /* get output buffer */
208 s->frame.nb_samples = avctx->frame_size;
209 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
210 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
213 samples = (int16_t *)s->frame.data[0];
215 for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
216 if ((ret = gsm_decode(s->state, buf, samples)) < 0)
218 buf += GSM_BLOCK_SIZE;
219 samples += GSM_FRAME_SIZE;
223 *(AVFrame *)data = s->frame;
225 return avctx->block_align;
228 static void libgsm_flush(AVCodecContext *avctx) {
229 LibGSMDecodeContext *s = avctx->priv_data;
232 gsm_destroy(s->state);
233 s->state = gsm_create();
234 if (avctx->codec_id == CODEC_ID_GSM_MS)
235 gsm_option(s->state, GSM_OPT_WAV49, &one);
238 AVCodec ff_libgsm_decoder = {
240 .type = AVMEDIA_TYPE_AUDIO,
242 .priv_data_size = sizeof(LibGSMDecodeContext),
243 .init = libgsm_decode_init,
244 .close = libgsm_decode_close,
245 .decode = libgsm_decode_frame,
246 .flush = libgsm_flush,
247 .capabilities = CODEC_CAP_DR1,
248 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
251 AVCodec ff_libgsm_ms_decoder = {
253 .type = AVMEDIA_TYPE_AUDIO,
254 .id = CODEC_ID_GSM_MS,
255 .priv_data_size = sizeof(LibGSMDecodeContext),
256 .init = libgsm_decode_init,
257 .close = libgsm_decode_close,
258 .decode = libgsm_decode_frame,
259 .flush = libgsm_flush,
260 .capabilities = CODEC_CAP_DR1,
261 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),