]> git.sesse.net Git - ffmpeg/blob - libavcodec/libgsmdec.c
Merge commit '62d5b5a9d3b0181335072d6fa792f2d805bc27b6'
[ffmpeg] / libavcodec / libgsmdec.c
1 /*
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>
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 /**
24  * @file
25  * Interface to libgsm for GSM decoding
26  */
27
28 // The idiosyncrasies of GSM-in-WAV are explained at http://kbs.cs.tu-berlin.de/~jutta/toast.html
29
30 #include <gsm.h>
31
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/common.h"
34
35 #include "avcodec.h"
36 #include "internal.h"
37 #include "gsm.h"
38
39 typedef struct LibGSMDecodeContext {
40     struct gsm_state *state;
41 } LibGSMDecodeContext;
42
43 static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
44     LibGSMDecodeContext *s = avctx->priv_data;
45
46     avctx->channels       = 1;
47     avctx->channel_layout = AV_CH_LAYOUT_MONO;
48     if (!avctx->sample_rate)
49         avctx->sample_rate = 8000;
50     avctx->sample_fmt     = AV_SAMPLE_FMT_S16;
51
52     s->state = gsm_create();
53
54     switch(avctx->codec_id) {
55     case AV_CODEC_ID_GSM:
56         avctx->frame_size  = GSM_FRAME_SIZE;
57         avctx->block_align = GSM_BLOCK_SIZE;
58         break;
59     case AV_CODEC_ID_GSM_MS: {
60         int one = 1;
61         gsm_option(s->state, GSM_OPT_WAV49, &one);
62         avctx->frame_size  = 2 * GSM_FRAME_SIZE;
63         avctx->block_align = GSM_MS_BLOCK_SIZE;
64         }
65     }
66
67     return 0;
68 }
69
70 static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
71     LibGSMDecodeContext *s = avctx->priv_data;
72
73     gsm_destroy(s->state);
74     s->state = NULL;
75     return 0;
76 }
77
78 static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
79                                int *got_frame_ptr, AVPacket *avpkt)
80 {
81     int i, ret;
82     LibGSMDecodeContext *s = avctx->priv_data;
83     AVFrame *frame         = data;
84     uint8_t *buf = avpkt->data;
85     int buf_size = avpkt->size;
86     int16_t *samples;
87
88     if (buf_size < avctx->block_align) {
89         av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
90         return AVERROR_INVALIDDATA;
91     }
92
93     /* get output buffer */
94     frame->nb_samples = avctx->frame_size;
95     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
96         return ret;
97     samples = (int16_t *)frame->data[0];
98
99     for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
100         if ((ret = gsm_decode(s->state, buf, samples)) < 0)
101             return -1;
102         buf     += GSM_BLOCK_SIZE;
103         samples += GSM_FRAME_SIZE;
104     }
105
106     *got_frame_ptr = 1;
107
108     return avctx->block_align;
109 }
110
111 static void libgsm_flush(AVCodecContext *avctx) {
112     LibGSMDecodeContext *s = avctx->priv_data;
113     int one = 1;
114
115     gsm_destroy(s->state);
116     s->state = gsm_create();
117     if (avctx->codec_id == AV_CODEC_ID_GSM_MS)
118         gsm_option(s->state, GSM_OPT_WAV49, &one);
119 }
120
121 #if CONFIG_LIBGSM_DECODER
122 AVCodec ff_libgsm_decoder = {
123     .name           = "libgsm",
124     .long_name      = NULL_IF_CONFIG_SMALL("libgsm GSM"),
125     .type           = AVMEDIA_TYPE_AUDIO,
126     .id             = AV_CODEC_ID_GSM,
127     .priv_data_size = sizeof(LibGSMDecodeContext),
128     .init           = libgsm_decode_init,
129     .close          = libgsm_decode_close,
130     .decode         = libgsm_decode_frame,
131     .flush          = libgsm_flush,
132     .capabilities   = AV_CODEC_CAP_DR1,
133 };
134 #endif
135 #if CONFIG_LIBGSM_MS_DECODER
136 AVCodec ff_libgsm_ms_decoder = {
137     .name           = "libgsm_ms",
138     .long_name      = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
139     .type           = AVMEDIA_TYPE_AUDIO,
140     .id             = AV_CODEC_ID_GSM_MS,
141     .priv_data_size = sizeof(LibGSMDecodeContext),
142     .init           = libgsm_decode_init,
143     .close          = libgsm_decode_close,
144     .decode         = libgsm_decode_frame,
145     .flush          = libgsm_flush,
146     .capabilities   = AV_CODEC_CAP_DR1,
147 };
148 #endif