2 * Interface to libgsm for GSM 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 Libav.
8 * Libav 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 * Libav 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 Libav; 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 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"
44 typedef struct LibGSMDecodeContext {
45 struct gsm_state *state;
46 } LibGSMDecodeContext;
48 static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
49 LibGSMDecodeContext *s = avctx->priv_data;
52 avctx->channel_layout = AV_CH_LAYOUT_MONO;
53 avctx->sample_rate = 8000;
54 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
56 s->state = gsm_create();
58 switch(avctx->codec_id) {
60 avctx->frame_size = GSM_FRAME_SIZE;
61 avctx->block_align = GSM_BLOCK_SIZE;
63 case AV_CODEC_ID_GSM_MS: {
65 gsm_option(s->state, GSM_OPT_WAV49, &one);
66 avctx->frame_size = 2 * GSM_FRAME_SIZE;
67 avctx->block_align = GSM_MS_BLOCK_SIZE;
74 static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
75 LibGSMDecodeContext *s = avctx->priv_data;
77 gsm_destroy(s->state);
82 static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
83 int *got_frame_ptr, AVPacket *avpkt)
86 LibGSMDecodeContext *s = avctx->priv_data;
87 AVFrame *frame = data;
88 uint8_t *buf = avpkt->data;
89 int buf_size = avpkt->size;
92 if (buf_size < avctx->block_align) {
93 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
94 return AVERROR_INVALIDDATA;
97 /* get output buffer */
98 frame->nb_samples = avctx->frame_size;
99 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
100 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
103 samples = (int16_t *)frame->data[0];
105 for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
106 if ((ret = gsm_decode(s->state, buf, samples)) < 0)
108 buf += GSM_BLOCK_SIZE;
109 samples += GSM_FRAME_SIZE;
114 return avctx->block_align;
117 static void libgsm_flush(AVCodecContext *avctx) {
118 LibGSMDecodeContext *s = avctx->priv_data;
121 gsm_destroy(s->state);
122 s->state = gsm_create();
123 if (avctx->codec_id == AV_CODEC_ID_GSM_MS)
124 gsm_option(s->state, GSM_OPT_WAV49, &one);
127 AVCodec ff_libgsm_decoder = {
129 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
130 .type = AVMEDIA_TYPE_AUDIO,
131 .id = AV_CODEC_ID_GSM,
132 .priv_data_size = sizeof(LibGSMDecodeContext),
133 .init = libgsm_decode_init,
134 .close = libgsm_decode_close,
135 .decode = libgsm_decode_frame,
136 .flush = libgsm_flush,
137 .capabilities = AV_CODEC_CAP_DR1,
140 AVCodec ff_libgsm_ms_decoder = {
142 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
143 .type = AVMEDIA_TYPE_AUDIO,
144 .id = AV_CODEC_ID_GSM_MS,
145 .priv_data_size = sizeof(LibGSMDecodeContext),
146 .init = libgsm_decode_init,
147 .close = libgsm_decode_close,
148 .decode = libgsm_decode_frame,
149 .flush = libgsm_flush,
150 .capabilities = AV_CODEC_CAP_DR1,