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,
128 AV_SAMPLE_FMT_NONE },
129 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
132 AVCodec ff_libgsm_ms_encoder = {
134 .type = AVMEDIA_TYPE_AUDIO,
135 .id = CODEC_ID_GSM_MS,
136 .init = libgsm_encode_init,
137 .encode2 = libgsm_encode_frame,
138 .close = libgsm_encode_close,
139 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
140 AV_SAMPLE_FMT_NONE },
141 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
144 typedef struct LibGSMDecodeContext {
146 struct gsm_state *state;
147 } LibGSMDecodeContext;
149 static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
150 LibGSMDecodeContext *s = avctx->priv_data;
152 if (avctx->channels > 1) {
153 av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
158 if (!avctx->channels)
161 if (!avctx->sample_rate)
162 avctx->sample_rate = 8000;
164 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
166 s->state = gsm_create();
168 switch(avctx->codec_id) {
170 avctx->frame_size = GSM_FRAME_SIZE;
171 avctx->block_align = GSM_BLOCK_SIZE;
173 case CODEC_ID_GSM_MS: {
175 gsm_option(s->state, GSM_OPT_WAV49, &one);
176 avctx->frame_size = 2 * GSM_FRAME_SIZE;
177 avctx->block_align = GSM_MS_BLOCK_SIZE;
181 avcodec_get_frame_defaults(&s->frame);
182 avctx->coded_frame = &s->frame;
187 static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
188 LibGSMDecodeContext *s = avctx->priv_data;
190 gsm_destroy(s->state);
195 static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
196 int *got_frame_ptr, AVPacket *avpkt)
199 LibGSMDecodeContext *s = avctx->priv_data;
200 uint8_t *buf = avpkt->data;
201 int buf_size = avpkt->size;
204 if (buf_size < avctx->block_align) {
205 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
206 return AVERROR_INVALIDDATA;
209 /* get output buffer */
210 s->frame.nb_samples = avctx->frame_size;
211 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
212 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
215 samples = (int16_t *)s->frame.data[0];
217 for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
218 if ((ret = gsm_decode(s->state, buf, samples)) < 0)
220 buf += GSM_BLOCK_SIZE;
221 samples += GSM_FRAME_SIZE;
225 *(AVFrame *)data = s->frame;
227 return avctx->block_align;
230 static void libgsm_flush(AVCodecContext *avctx) {
231 LibGSMDecodeContext *s = avctx->priv_data;
234 gsm_destroy(s->state);
235 s->state = gsm_create();
236 if (avctx->codec_id == CODEC_ID_GSM_MS)
237 gsm_option(s->state, GSM_OPT_WAV49, &one);
240 AVCodec ff_libgsm_decoder = {
242 .type = AVMEDIA_TYPE_AUDIO,
244 .priv_data_size = sizeof(LibGSMDecodeContext),
245 .init = libgsm_decode_init,
246 .close = libgsm_decode_close,
247 .decode = libgsm_decode_frame,
248 .flush = libgsm_flush,
249 .capabilities = CODEC_CAP_DR1,
250 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
253 AVCodec ff_libgsm_ms_decoder = {
255 .type = AVMEDIA_TYPE_AUDIO,
256 .id = CODEC_ID_GSM_MS,
257 .priv_data_size = sizeof(LibGSMDecodeContext),
258 .init = libgsm_decode_init,
259 .close = libgsm_decode_close,
260 .decode = libgsm_decode_frame,
261 .flush = libgsm_flush,
262 .capabilities = CODEC_CAP_DR1,
263 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),