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
37 #include "libavutil/channel_layout.h"
38 #include "libavutil/common.h"
43 static av_cold int libgsm_encode_close(AVCodecContext *avctx) {
44 #if FF_API_OLD_ENCODE_AUDIO
45 av_freep(&avctx->coded_frame);
47 gsm_destroy(avctx->priv_data);
48 avctx->priv_data = NULL;
52 static av_cold int libgsm_encode_init(AVCodecContext *avctx) {
53 if (avctx->channels > 1) {
54 av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
59 if (avctx->sample_rate != 8000) {
60 av_log(avctx, AV_LOG_ERROR, "Sample rate 8000Hz required for GSM, got %dHz\n",
62 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
65 if (avctx->bit_rate != 13000 /* Official */ &&
66 avctx->bit_rate != 13200 /* Very common */ &&
67 avctx->bit_rate != 0 /* Unknown; a.o. mov does not set bitrate when decoding */ ) {
68 av_log(avctx, AV_LOG_ERROR, "Bitrate 13000bps required for GSM, got %dbps\n",
70 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
74 avctx->priv_data = gsm_create();
75 if (!avctx->priv_data)
78 switch(avctx->codec_id) {
80 avctx->frame_size = GSM_FRAME_SIZE;
81 avctx->block_align = GSM_BLOCK_SIZE;
83 case AV_CODEC_ID_GSM_MS: {
85 gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
86 avctx->frame_size = 2*GSM_FRAME_SIZE;
87 avctx->block_align = GSM_MS_BLOCK_SIZE;
91 #if FF_API_OLD_ENCODE_AUDIO
92 avctx->coded_frame= avcodec_alloc_frame();
93 if (!avctx->coded_frame)
99 libgsm_encode_close(avctx);
103 static int libgsm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
104 const AVFrame *frame, int *got_packet_ptr)
107 gsm_signal *samples = (gsm_signal *)frame->data[0];
108 struct gsm_state *state = avctx->priv_data;
110 if ((ret = ff_alloc_packet2(avctx, avpkt, avctx->block_align)))
113 switch(avctx->codec_id) {
114 case AV_CODEC_ID_GSM:
115 gsm_encode(state, samples, avpkt->data);
117 case AV_CODEC_ID_GSM_MS:
118 gsm_encode(state, samples, avpkt->data);
119 gsm_encode(state, samples + GSM_FRAME_SIZE, avpkt->data + 32);
127 #if CONFIG_LIBGSM_ENCODER
128 AVCodec ff_libgsm_encoder = {
130 .type = AVMEDIA_TYPE_AUDIO,
131 .id = AV_CODEC_ID_GSM,
132 .init = libgsm_encode_init,
133 .encode2 = libgsm_encode_frame,
134 .close = libgsm_encode_close,
135 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
136 AV_SAMPLE_FMT_NONE },
137 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
140 #if CONFIG_LIBGSM_MS_ENCODER
141 AVCodec ff_libgsm_ms_encoder = {
143 .type = AVMEDIA_TYPE_AUDIO,
144 .id = AV_CODEC_ID_GSM_MS,
145 .init = libgsm_encode_init,
146 .encode2 = libgsm_encode_frame,
147 .close = libgsm_encode_close,
148 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
149 AV_SAMPLE_FMT_NONE },
150 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
154 typedef struct LibGSMDecodeContext {
156 struct gsm_state *state;
157 } LibGSMDecodeContext;
159 static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
160 LibGSMDecodeContext *s = avctx->priv_data;
163 avctx->channel_layout = AV_CH_LAYOUT_MONO;
164 if (!avctx->sample_rate)
165 avctx->sample_rate = 8000;
166 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
168 s->state = gsm_create();
170 switch(avctx->codec_id) {
171 case AV_CODEC_ID_GSM:
172 avctx->frame_size = GSM_FRAME_SIZE;
173 avctx->block_align = GSM_BLOCK_SIZE;
175 case AV_CODEC_ID_GSM_MS: {
177 gsm_option(s->state, GSM_OPT_WAV49, &one);
178 avctx->frame_size = 2 * GSM_FRAME_SIZE;
179 avctx->block_align = GSM_MS_BLOCK_SIZE;
183 avcodec_get_frame_defaults(&s->frame);
184 avctx->coded_frame = &s->frame;
189 static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
190 LibGSMDecodeContext *s = avctx->priv_data;
192 gsm_destroy(s->state);
197 static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
198 int *got_frame_ptr, AVPacket *avpkt)
201 LibGSMDecodeContext *s = avctx->priv_data;
202 uint8_t *buf = avpkt->data;
203 int buf_size = avpkt->size;
206 if (buf_size < avctx->block_align) {
207 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
208 return AVERROR_INVALIDDATA;
211 /* get output buffer */
212 s->frame.nb_samples = avctx->frame_size;
213 if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
214 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
217 samples = (int16_t *)s->frame.data[0];
219 for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
220 if ((ret = gsm_decode(s->state, buf, samples)) < 0)
222 buf += GSM_BLOCK_SIZE;
223 samples += GSM_FRAME_SIZE;
227 *(AVFrame *)data = s->frame;
229 return avctx->block_align;
232 static void libgsm_flush(AVCodecContext *avctx) {
233 LibGSMDecodeContext *s = avctx->priv_data;
236 gsm_destroy(s->state);
237 s->state = gsm_create();
238 if (avctx->codec_id == AV_CODEC_ID_GSM_MS)
239 gsm_option(s->state, GSM_OPT_WAV49, &one);
242 #if CONFIG_LIBGSM_DECODER
243 AVCodec ff_libgsm_decoder = {
245 .type = AVMEDIA_TYPE_AUDIO,
246 .id = AV_CODEC_ID_GSM,
247 .priv_data_size = sizeof(LibGSMDecodeContext),
248 .init = libgsm_decode_init,
249 .close = libgsm_decode_close,
250 .decode = libgsm_decode_frame,
251 .flush = libgsm_flush,
252 .capabilities = CODEC_CAP_DR1,
253 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
256 #if CONFIG_LIBGSM_MS_DECODER
257 AVCodec ff_libgsm_ms_decoder = {
259 .type = AVMEDIA_TYPE_AUDIO,
260 .id = AV_CODEC_ID_GSM_MS,
261 .priv_data_size = sizeof(LibGSMDecodeContext),
262 .init = libgsm_decode_init,
263 .close = libgsm_decode_close,
264 .decode = libgsm_decode_frame,
265 .flush = libgsm_flush,
266 .capabilities = CODEC_CAP_DR1,
267 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),