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_init(AVCodecContext *avctx) {
36 if (avctx->channels > 1) {
37 av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
42 if (avctx->sample_rate != 8000) {
43 av_log(avctx, AV_LOG_ERROR, "Sample rate 8000Hz required for GSM, got %dHz\n",
45 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
48 if (avctx->bit_rate != 13000 /* Official */ &&
49 avctx->bit_rate != 13200 /* Very common */ &&
50 avctx->bit_rate != 0 /* Unknown; a.o. mov does not set bitrate when decoding */ ) {
51 av_log(avctx, AV_LOG_ERROR, "Bitrate 13000bps required for GSM, got %dbps\n",
53 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
57 avctx->priv_data = gsm_create();
58 if (!avctx->priv_data)
61 switch(avctx->codec_id) {
63 avctx->frame_size = GSM_FRAME_SIZE;
64 avctx->block_align = GSM_BLOCK_SIZE;
66 case CODEC_ID_GSM_MS: {
68 gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
69 avctx->frame_size = 2*GSM_FRAME_SIZE;
70 avctx->block_align = GSM_MS_BLOCK_SIZE;
74 avctx->coded_frame= avcodec_alloc_frame();
75 avctx->coded_frame->key_frame= 1;
76 if (!avctx->coded_frame)
81 libgsm_encode_close();
85 static av_cold int libgsm_encode_close(AVCodecContext *avctx) {
86 av_freep(&avctx->coded_frame);
87 gsm_destroy(avctx->priv_data);
88 avctx->priv_data = NULL;
92 static int libgsm_encode_frame(AVCodecContext *avctx,
93 unsigned char *frame, int buf_size, void *data) {
94 // we need a full block
95 if(buf_size < avctx->block_align) return 0;
97 switch(avctx->codec_id) {
99 gsm_encode(avctx->priv_data,data,frame);
101 case CODEC_ID_GSM_MS:
102 gsm_encode(avctx->priv_data,data,frame);
103 gsm_encode(avctx->priv_data,((short*)data)+GSM_FRAME_SIZE,frame+32);
105 return avctx->block_align;
109 AVCodec ff_libgsm_encoder = {
111 .type = AVMEDIA_TYPE_AUDIO,
113 .init = libgsm_encode_init,
114 .encode = libgsm_encode_frame,
115 .close = libgsm_encode_close,
116 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
117 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
120 AVCodec ff_libgsm_ms_encoder = {
122 .type = AVMEDIA_TYPE_AUDIO,
123 .id = CODEC_ID_GSM_MS,
124 .init = libgsm_encode_init,
125 .encode = 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 Microsoft variant"),
131 typedef struct LibGSMDecodeContext {
133 struct gsm_state *state;
134 } LibGSMDecodeContext;
136 static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
137 LibGSMDecodeContext *s = avctx->priv_data;
139 if (avctx->channels > 1) {
140 av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
145 if (!avctx->channels)
148 if (!avctx->sample_rate)
149 avctx->sample_rate = 8000;
151 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
153 s->state = gsm_create();
155 switch(avctx->codec_id) {
157 avctx->frame_size = GSM_FRAME_SIZE;
158 avctx->block_align = GSM_BLOCK_SIZE;
160 case CODEC_ID_GSM_MS: {
162 gsm_option(s->state, GSM_OPT_WAV49, &one);
163 avctx->frame_size = 2 * GSM_FRAME_SIZE;
164 avctx->block_align = GSM_MS_BLOCK_SIZE;
168 avcodec_get_frame_defaults(&s->frame);
169 avctx->coded_frame = &s->frame;
174 static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
175 LibGSMDecodeContext *s = avctx->priv_data;
177 gsm_destroy(s->state);
182 static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
183 int *got_frame_ptr, AVPacket *avpkt)
186 LibGSMDecodeContext *s = avctx->priv_data;
187 uint8_t *buf = avpkt->data;
188 int buf_size = avpkt->size;
191 if (buf_size < avctx->block_align) {
192 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
193 return AVERROR_INVALIDDATA;
196 /* get output buffer */
197 s->frame.nb_samples = avctx->frame_size;
198 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
199 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
202 samples = (int16_t *)s->frame.data[0];
204 for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
205 if ((ret = gsm_decode(s->state, buf, samples)) < 0)
207 buf += GSM_BLOCK_SIZE;
208 samples += GSM_FRAME_SIZE;
212 *(AVFrame *)data = s->frame;
214 return avctx->block_align;
217 static void libgsm_flush(AVCodecContext *avctx) {
218 LibGSMDecodeContext *s = avctx->priv_data;
221 gsm_destroy(s->state);
222 s->state = gsm_create();
223 if (avctx->codec_id == CODEC_ID_GSM_MS)
224 gsm_option(s->state, GSM_OPT_WAV49, &one);
227 AVCodec ff_libgsm_decoder = {
229 .type = AVMEDIA_TYPE_AUDIO,
231 .priv_data_size = sizeof(LibGSMDecodeContext),
232 .init = libgsm_decode_init,
233 .close = libgsm_decode_close,
234 .decode = libgsm_decode_frame,
235 .flush = libgsm_flush,
236 .capabilities = CODEC_CAP_DR1,
237 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
240 AVCodec ff_libgsm_ms_decoder = {
242 .type = AVMEDIA_TYPE_AUDIO,
243 .id = CODEC_ID_GSM_MS,
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 Microsoft variant"),