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 static av_cold int libgsm_encode_close(AVCodecContext *avctx) {
36 av_freep(&avctx->coded_frame);
37 gsm_destroy(avctx->priv_data);
38 avctx->priv_data = NULL;
42 static av_cold int libgsm_encode_init(AVCodecContext *avctx) {
43 if (avctx->channels > 1) {
44 av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
49 if (avctx->sample_rate != 8000) {
50 av_log(avctx, AV_LOG_ERROR, "Sample rate 8000Hz required for GSM, got %dHz\n",
52 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
55 if (avctx->bit_rate != 13000 /* Official */ &&
56 avctx->bit_rate != 13200 /* Very common */ &&
57 avctx->bit_rate != 0 /* Unknown; a.o. mov does not set bitrate when decoding */ ) {
58 av_log(avctx, AV_LOG_ERROR, "Bitrate 13000bps required for GSM, got %dbps\n",
60 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
64 avctx->priv_data = gsm_create();
65 if (!avctx->priv_data)
68 switch(avctx->codec_id) {
70 avctx->frame_size = GSM_FRAME_SIZE;
71 avctx->block_align = GSM_BLOCK_SIZE;
73 case CODEC_ID_GSM_MS: {
75 gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
76 avctx->frame_size = 2*GSM_FRAME_SIZE;
77 avctx->block_align = GSM_MS_BLOCK_SIZE;
81 avctx->coded_frame= avcodec_alloc_frame();
82 if (!avctx->coded_frame)
87 libgsm_encode_close(avctx);
91 static int libgsm_encode_frame(AVCodecContext *avctx,
92 unsigned char *frame, int buf_size, void *data) {
93 // we need a full block
94 if(buf_size < avctx->block_align) return 0;
96 switch(avctx->codec_id) {
98 gsm_encode(avctx->priv_data,data,frame);
100 case CODEC_ID_GSM_MS:
101 gsm_encode(avctx->priv_data,data,frame);
102 gsm_encode(avctx->priv_data,((short*)data)+GSM_FRAME_SIZE,frame+32);
104 return avctx->block_align;
108 AVCodec ff_libgsm_encoder = {
110 .type = AVMEDIA_TYPE_AUDIO,
112 .init = libgsm_encode_init,
113 .encode = libgsm_encode_frame,
114 .close = libgsm_encode_close,
115 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
116 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
119 AVCodec ff_libgsm_ms_encoder = {
121 .type = AVMEDIA_TYPE_AUDIO,
122 .id = CODEC_ID_GSM_MS,
123 .init = libgsm_encode_init,
124 .encode = libgsm_encode_frame,
125 .close = libgsm_encode_close,
126 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
127 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
130 typedef struct LibGSMDecodeContext {
132 struct gsm_state *state;
133 } LibGSMDecodeContext;
135 static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
136 LibGSMDecodeContext *s = avctx->priv_data;
138 if (avctx->channels > 1) {
139 av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
144 if (!avctx->channels)
147 if (!avctx->sample_rate)
148 avctx->sample_rate = 8000;
150 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
152 s->state = gsm_create();
154 switch(avctx->codec_id) {
156 avctx->frame_size = GSM_FRAME_SIZE;
157 avctx->block_align = GSM_BLOCK_SIZE;
159 case CODEC_ID_GSM_MS: {
161 gsm_option(s->state, GSM_OPT_WAV49, &one);
162 avctx->frame_size = 2 * GSM_FRAME_SIZE;
163 avctx->block_align = GSM_MS_BLOCK_SIZE;
167 avcodec_get_frame_defaults(&s->frame);
168 avctx->coded_frame = &s->frame;
173 static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
174 LibGSMDecodeContext *s = avctx->priv_data;
176 gsm_destroy(s->state);
181 static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
182 int *got_frame_ptr, AVPacket *avpkt)
185 LibGSMDecodeContext *s = avctx->priv_data;
186 uint8_t *buf = avpkt->data;
187 int buf_size = avpkt->size;
190 if (buf_size < avctx->block_align) {
191 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
192 return AVERROR_INVALIDDATA;
195 /* get output buffer */
196 s->frame.nb_samples = avctx->frame_size;
197 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
198 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
201 samples = (int16_t *)s->frame.data[0];
203 for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
204 if ((ret = gsm_decode(s->state, buf, samples)) < 0)
206 buf += GSM_BLOCK_SIZE;
207 samples += GSM_FRAME_SIZE;
211 *(AVFrame *)data = s->frame;
213 return avctx->block_align;
216 static void libgsm_flush(AVCodecContext *avctx) {
217 LibGSMDecodeContext *s = avctx->priv_data;
220 gsm_destroy(s->state);
221 s->state = gsm_create();
222 if (avctx->codec_id == CODEC_ID_GSM_MS)
223 gsm_option(s->state, GSM_OPT_WAV49, &one);
226 AVCodec ff_libgsm_decoder = {
228 .type = AVMEDIA_TYPE_AUDIO,
230 .priv_data_size = sizeof(LibGSMDecodeContext),
231 .init = libgsm_decode_init,
232 .close = libgsm_decode_close,
233 .decode = libgsm_decode_frame,
234 .flush = libgsm_flush,
235 .capabilities = CODEC_CAP_DR1,
236 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
239 AVCodec ff_libgsm_ms_decoder = {
241 .type = AVMEDIA_TYPE_AUDIO,
242 .id = CODEC_ID_GSM_MS,
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 Microsoft variant"),