libgsm.c 8.09 KB
Newer Older
1 2 3
/*
 * Interface to libgsm for gsm encoding/decoding
 * Copyright (c) 2005 Alban Bedel <albeu@free.fr>
4
 * Copyright (c) 2006, 2007 Michel Bardiaux <mbardiaux@mediaxim.be>
5
 *
6 7 8
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
9 10
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14 15 16 17 18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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
21
 */
22

23
/**
24
 * @file
25 26 27
 * Interface to libgsm for gsm encoding/decoding
 */

28 29
// The idiosyncrasies of GSM-in-WAV are explained at http://kbs.cs.tu-berlin.de/~jutta/toast.html

30
#include <gsm/gsm.h>
31

Justin Ruggles's avatar
Justin Ruggles committed
32
#include "avcodec.h"
33
#include "internal.h"
Justin Ruggles's avatar
Justin Ruggles committed
34
#include "gsm.h"
35
#include "libavutil/common.h"
36

37
static av_cold int libgsm_encode_close(AVCodecContext *avctx) {
38
#if FF_API_OLD_ENCODE_AUDIO
39
    av_freep(&avctx->coded_frame);
40
#endif
41 42 43 44 45
    gsm_destroy(avctx->priv_data);
    avctx->priv_data = NULL;
    return 0;
}

46
static av_cold int libgsm_encode_init(AVCodecContext *avctx) {
47 48 49
    if (avctx->channels > 1) {
        av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
               avctx->channels);
50
        return -1;
51
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
52

53 54 55 56 57 58 59 60 61 62 63 64 65
    if (avctx->sample_rate != 8000) {
        av_log(avctx, AV_LOG_ERROR, "Sample rate 8000Hz required for GSM, got %dHz\n",
               avctx->sample_rate);
        if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
            return -1;
    }
    if (avctx->bit_rate != 13000 /* Official */ &&
        avctx->bit_rate != 13200 /* Very common */ &&
        avctx->bit_rate != 0 /* Unknown; a.o. mov does not set bitrate when decoding */ ) {
        av_log(avctx, AV_LOG_ERROR, "Bitrate 13000bps required for GSM, got %dbps\n",
               avctx->bit_rate);
        if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
            return -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
66
    }
67 68

    avctx->priv_data = gsm_create();
69 70
    if (!avctx->priv_data)
        goto error;
71

72
    switch(avctx->codec_id) {
73
    case AV_CODEC_ID_GSM:
74 75 76
        avctx->frame_size = GSM_FRAME_SIZE;
        avctx->block_align = GSM_BLOCK_SIZE;
        break;
77
    case AV_CODEC_ID_GSM_MS: {
78 79 80 81 82 83 84
        int one = 1;
        gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
        avctx->frame_size = 2*GSM_FRAME_SIZE;
        avctx->block_align = GSM_MS_BLOCK_SIZE;
        }
    }

85
#if FF_API_OLD_ENCODE_AUDIO
86
    avctx->coded_frame= avcodec_alloc_frame();
87 88
    if (!avctx->coded_frame)
        goto error;
89
#endif
90

91
    return 0;
92
error:
93
    libgsm_encode_close(avctx);
94
    return -1;
95 96
}

97 98 99 100 101 102 103
static int libgsm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                               const AVFrame *frame, int *got_packet_ptr)
{
    int ret;
    gsm_signal *samples = (gsm_signal *)frame->data[0];
    struct gsm_state *state = avctx->priv_data;

104
    if ((ret = ff_alloc_packet2(avctx, avpkt, avctx->block_align)))
105
        return ret;
106 107

    switch(avctx->codec_id) {
108
    case AV_CODEC_ID_GSM:
109
        gsm_encode(state, samples, avpkt->data);
110
        break;
111
    case AV_CODEC_ID_GSM_MS:
112 113
        gsm_encode(state, samples,                  avpkt->data);
        gsm_encode(state, samples + GSM_FRAME_SIZE, avpkt->data + 32);
114
    }
115 116 117

    *got_packet_ptr = 1;
    return 0;
118 119 120
}


121
#if CONFIG_LIBGSM_ENCODER
122
AVCodec ff_libgsm_encoder = {
123 124
    .name           = "libgsm",
    .type           = AVMEDIA_TYPE_AUDIO,
125
    .id             = AV_CODEC_ID_GSM,
126
    .init           = libgsm_encode_init,
127
    .encode2        = libgsm_encode_frame,
128
    .close          = libgsm_encode_close,
129 130 131
    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
                                                     AV_SAMPLE_FMT_NONE },
    .long_name      = NULL_IF_CONFIG_SMALL("libgsm GSM"),
132
};
133 134
#endif
#if CONFIG_LIBGSM_MS_ENCODER
135
AVCodec ff_libgsm_ms_encoder = {
136 137
    .name           = "libgsm_ms",
    .type           = AVMEDIA_TYPE_AUDIO,
138
    .id             = AV_CODEC_ID_GSM_MS,
139
    .init           = libgsm_encode_init,
140
    .encode2        = libgsm_encode_frame,
141
    .close          = libgsm_encode_close,
142 143 144
    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
                                                     AV_SAMPLE_FMT_NONE },
    .long_name      = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
145
};
146
#endif
147

148 149 150 151 152
typedef struct LibGSMDecodeContext {
    AVFrame frame;
    struct gsm_state *state;
} LibGSMDecodeContext;

153
static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
154 155
    LibGSMDecodeContext *s = avctx->priv_data;

156 157 158 159 160 161 162 163 164 165 166 167 168 169
    if (avctx->channels > 1) {
        av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
               avctx->channels);
        return -1;
    }

    if (!avctx->channels)
        avctx->channels = 1;

    if (!avctx->sample_rate)
        avctx->sample_rate = 8000;

    avctx->sample_fmt = AV_SAMPLE_FMT_S16;

170
    s->state = gsm_create();
171 172

    switch(avctx->codec_id) {
173
    case AV_CODEC_ID_GSM:
174 175 176
        avctx->frame_size  = GSM_FRAME_SIZE;
        avctx->block_align = GSM_BLOCK_SIZE;
        break;
177
    case AV_CODEC_ID_GSM_MS: {
178
        int one = 1;
179
        gsm_option(s->state, GSM_OPT_WAV49, &one);
180 181 182 183 184
        avctx->frame_size  = 2 * GSM_FRAME_SIZE;
        avctx->block_align = GSM_MS_BLOCK_SIZE;
        }
    }

185 186 187
    avcodec_get_frame_defaults(&s->frame);
    avctx->coded_frame = &s->frame;

188 189 190 191
    return 0;
}

static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
192 193 194 195
    LibGSMDecodeContext *s = avctx->priv_data;

    gsm_destroy(s->state);
    s->state = NULL;
196 197 198
    return 0;
}

199 200 201
static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
                               int *got_frame_ptr, AVPacket *avpkt)
{
202
    int i, ret;
203
    LibGSMDecodeContext *s = avctx->priv_data;
204
    uint8_t *buf = avpkt->data;
205
    int buf_size = avpkt->size;
206
    int16_t *samples;
207

208 209 210
    if (buf_size < avctx->block_align) {
        av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
        return AVERROR_INVALIDDATA;
211
    }
212

213 214 215 216 217 218 219 220
    /* get output buffer */
    s->frame.nb_samples = avctx->frame_size;
    if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
        return ret;
    }
    samples = (int16_t *)s->frame.data[0];

221
    for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
222
        if ((ret = gsm_decode(s->state, buf, samples)) < 0)
223 224 225
            return -1;
        buf     += GSM_BLOCK_SIZE;
        samples += GSM_FRAME_SIZE;
226
    }
227

228 229 230
    *got_frame_ptr   = 1;
    *(AVFrame *)data = s->frame;

231
    return avctx->block_align;
232 233
}

234
static void libgsm_flush(AVCodecContext *avctx) {
235
    LibGSMDecodeContext *s = avctx->priv_data;
236
    int one = 1;
237 238 239

    gsm_destroy(s->state);
    s->state = gsm_create();
240
    if (avctx->codec_id == AV_CODEC_ID_GSM_MS)
241
        gsm_option(s->state, GSM_OPT_WAV49, &one);
242 243
}

244
#if CONFIG_LIBGSM_DECODER
245
AVCodec ff_libgsm_decoder = {
246 247
    .name           = "libgsm",
    .type           = AVMEDIA_TYPE_AUDIO,
248
    .id             = AV_CODEC_ID_GSM,
249
    .priv_data_size = sizeof(LibGSMDecodeContext),
250 251
    .init           = libgsm_decode_init,
    .close          = libgsm_decode_close,
252
    .decode         = libgsm_decode_frame,
253
    .flush          = libgsm_flush,
254
    .capabilities   = CODEC_CAP_DR1,
255
    .long_name      = NULL_IF_CONFIG_SMALL("libgsm GSM"),
256
};
257 258
#endif
#if CONFIG_LIBGSM_MS_DECODER
259
AVCodec ff_libgsm_ms_decoder = {
260 261
    .name           = "libgsm_ms",
    .type           = AVMEDIA_TYPE_AUDIO,
262
    .id             = AV_CODEC_ID_GSM_MS,
263
    .priv_data_size = sizeof(LibGSMDecodeContext),
264 265
    .init           = libgsm_decode_init,
    .close          = libgsm_decode_close,
266
    .decode         = libgsm_decode_frame,
267
    .flush          = libgsm_flush,
268
    .capabilities   = CODEC_CAP_DR1,
269
    .long_name      = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
270
};
271
#endif