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();
59 switch(avctx->codec_id) {
61 avctx->frame_size = GSM_FRAME_SIZE;
62 avctx->block_align = GSM_BLOCK_SIZE;
64 case CODEC_ID_GSM_MS: {
66 gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
67 avctx->frame_size = 2*GSM_FRAME_SIZE;
68 avctx->block_align = GSM_MS_BLOCK_SIZE;
72 avctx->coded_frame= avcodec_alloc_frame();
73 avctx->coded_frame->key_frame= 1;
78 static av_cold int libgsm_encode_close(AVCodecContext *avctx) {
79 av_freep(&avctx->coded_frame);
80 gsm_destroy(avctx->priv_data);
81 avctx->priv_data = NULL;
85 static int libgsm_encode_frame(AVCodecContext *avctx,
86 unsigned char *frame, int buf_size, void *data) {
87 // we need a full block
88 if(buf_size < avctx->block_align) return 0;
90 switch(avctx->codec_id) {
92 gsm_encode(avctx->priv_data,data,frame);
95 gsm_encode(avctx->priv_data,data,frame);
96 gsm_encode(avctx->priv_data,((short*)data)+GSM_FRAME_SIZE,frame+32);
98 return avctx->block_align;
102 AVCodec ff_libgsm_encoder = {
104 .type = AVMEDIA_TYPE_AUDIO,
106 .init = libgsm_encode_init,
107 .encode = libgsm_encode_frame,
108 .close = libgsm_encode_close,
109 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
110 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
113 AVCodec ff_libgsm_ms_encoder = {
115 .type = AVMEDIA_TYPE_AUDIO,
116 .id = CODEC_ID_GSM_MS,
117 .init = libgsm_encode_init,
118 .encode = libgsm_encode_frame,
119 .close = libgsm_encode_close,
120 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
121 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
124 typedef struct LibGSMDecodeContext {
126 struct gsm_state *state;
127 } LibGSMDecodeContext;
129 static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
130 LibGSMDecodeContext *s = avctx->priv_data;
132 if (avctx->channels > 1) {
133 av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
138 if (!avctx->channels)
141 if (!avctx->sample_rate)
142 avctx->sample_rate = 8000;
144 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
146 s->state = gsm_create();
148 switch(avctx->codec_id) {
150 avctx->frame_size = GSM_FRAME_SIZE;
151 avctx->block_align = GSM_BLOCK_SIZE;
153 case CODEC_ID_GSM_MS: {
155 gsm_option(s->state, GSM_OPT_WAV49, &one);
156 avctx->frame_size = 2 * GSM_FRAME_SIZE;
157 avctx->block_align = GSM_MS_BLOCK_SIZE;
161 avcodec_get_frame_defaults(&s->frame);
162 avctx->coded_frame = &s->frame;
167 static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
168 LibGSMDecodeContext *s = avctx->priv_data;
170 gsm_destroy(s->state);
175 static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
176 int *got_frame_ptr, AVPacket *avpkt)
179 LibGSMDecodeContext *s = avctx->priv_data;
180 uint8_t *buf = avpkt->data;
181 int buf_size = avpkt->size;
184 if (buf_size < avctx->block_align) {
185 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
186 return AVERROR_INVALIDDATA;
189 /* get output buffer */
190 s->frame.nb_samples = avctx->frame_size;
191 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
192 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
195 samples = (int16_t *)s->frame.data[0];
197 for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
198 if ((ret = gsm_decode(s->state, buf, samples)) < 0)
200 buf += GSM_BLOCK_SIZE;
201 samples += GSM_FRAME_SIZE;
205 *(AVFrame *)data = s->frame;
207 return avctx->block_align;
210 static void libgsm_flush(AVCodecContext *avctx) {
211 LibGSMDecodeContext *s = avctx->priv_data;
214 gsm_destroy(s->state);
215 s->state = gsm_create();
216 if (avctx->codec_id == CODEC_ID_GSM_MS)
217 gsm_option(s->state, GSM_OPT_WAV49, &one);
220 AVCodec ff_libgsm_decoder = {
222 .type = AVMEDIA_TYPE_AUDIO,
224 .priv_data_size = sizeof(LibGSMDecodeContext),
225 .init = libgsm_decode_init,
226 .close = libgsm_decode_close,
227 .decode = libgsm_decode_frame,
228 .flush = libgsm_flush,
229 .capabilities = CODEC_CAP_DR1,
230 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
233 AVCodec ff_libgsm_ms_decoder = {
235 .type = AVMEDIA_TYPE_AUDIO,
236 .id = CODEC_ID_GSM_MS,
237 .priv_data_size = sizeof(LibGSMDecodeContext),
238 .init = libgsm_decode_init,
239 .close = libgsm_decode_close,
240 .decode = libgsm_decode_frame,
241 .flush = libgsm_flush,
242 .capabilities = CODEC_CAP_DR1,
243 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),