]> git.sesse.net Git - ffmpeg/blob - libavcodec/libgsm.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / libgsm.c
1 /*
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>
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 encoding/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/gsm.h>
31
32 #include "avcodec.h"
33 #include "gsm.h"
34
35 static av_cold int libgsm_encode_close(AVCodecContext *avctx) {
36     av_freep(&avctx->coded_frame);
37     gsm_destroy(avctx->priv_data);
38     avctx->priv_data = NULL;
39     return 0;
40 }
41
42 static av_cold int libgsm_encode_init(AVCodecContext *avctx) {
43     if (avctx->channels > 1) {
44         av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
45                avctx->channels);
46         return -1;
47     }
48
49     if (avctx->sample_rate != 8000) {
50         av_log(avctx, AV_LOG_ERROR, "Sample rate 8000Hz required for GSM, got %dHz\n",
51                avctx->sample_rate);
52         if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
53             return -1;
54     }
55     if (avctx->bit_rate != 13000 /* Official */ &&
56         avctx->bit_rate != 13200 /* Very common */ &&
57         avctx->bit_rate != 0 /* Unknown; a.o. mov does not set bitrate when decoding */ ) {
58         av_log(avctx, AV_LOG_ERROR, "Bitrate 13000bps required for GSM, got %dbps\n",
59                avctx->bit_rate);
60         if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
61             return -1;
62     }
63
64     avctx->priv_data = gsm_create();
65     if (!avctx->priv_data)
66         goto error;
67
68     switch(avctx->codec_id) {
69     case CODEC_ID_GSM:
70         avctx->frame_size = GSM_FRAME_SIZE;
71         avctx->block_align = GSM_BLOCK_SIZE;
72         break;
73     case CODEC_ID_GSM_MS: {
74         int one = 1;
75         gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
76         avctx->frame_size = 2*GSM_FRAME_SIZE;
77         avctx->block_align = GSM_MS_BLOCK_SIZE;
78         }
79     }
80
81     avctx->coded_frame= avcodec_alloc_frame();
82     if (!avctx->coded_frame)
83         goto error;
84
85     return 0;
86 error:
87     libgsm_encode_close(avctx);
88     return -1;
89 }
90
91 static int libgsm_encode_frame(AVCodecContext *avctx,
92                                unsigned char *frame, int buf_size, void *data) {
93     // we need a full block
94     if(buf_size < avctx->block_align) return 0;
95
96     switch(avctx->codec_id) {
97     case CODEC_ID_GSM:
98         gsm_encode(avctx->priv_data,data,frame);
99         break;
100     case CODEC_ID_GSM_MS:
101         gsm_encode(avctx->priv_data,data,frame);
102         gsm_encode(avctx->priv_data,((short*)data)+GSM_FRAME_SIZE,frame+32);
103     }
104     return avctx->block_align;
105 }
106
107
108 AVCodec ff_libgsm_encoder = {
109     .name           = "libgsm",
110     .type           = AVMEDIA_TYPE_AUDIO,
111     .id             = CODEC_ID_GSM,
112     .init           = libgsm_encode_init,
113     .encode         = libgsm_encode_frame,
114     .close          = libgsm_encode_close,
115     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
116     .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
117 };
118
119 AVCodec ff_libgsm_ms_encoder = {
120     .name           = "libgsm_ms",
121     .type           = AVMEDIA_TYPE_AUDIO,
122     .id             = CODEC_ID_GSM_MS,
123     .init           = libgsm_encode_init,
124     .encode         = libgsm_encode_frame,
125     .close          = libgsm_encode_close,
126     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
127     .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
128 };
129
130 typedef struct LibGSMDecodeContext {
131     AVFrame frame;
132     struct gsm_state *state;
133 } LibGSMDecodeContext;
134
135 static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
136     LibGSMDecodeContext *s = avctx->priv_data;
137
138     if (avctx->channels > 1) {
139         av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
140                avctx->channels);
141         return -1;
142     }
143
144     if (!avctx->channels)
145         avctx->channels = 1;
146
147     if (!avctx->sample_rate)
148         avctx->sample_rate = 8000;
149
150     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
151
152     s->state = gsm_create();
153
154     switch(avctx->codec_id) {
155     case CODEC_ID_GSM:
156         avctx->frame_size  = GSM_FRAME_SIZE;
157         avctx->block_align = GSM_BLOCK_SIZE;
158         break;
159     case CODEC_ID_GSM_MS: {
160         int one = 1;
161         gsm_option(s->state, GSM_OPT_WAV49, &one);
162         avctx->frame_size  = 2 * GSM_FRAME_SIZE;
163         avctx->block_align = GSM_MS_BLOCK_SIZE;
164         }
165     }
166
167     avcodec_get_frame_defaults(&s->frame);
168     avctx->coded_frame = &s->frame;
169
170     return 0;
171 }
172
173 static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
174     LibGSMDecodeContext *s = avctx->priv_data;
175
176     gsm_destroy(s->state);
177     s->state = NULL;
178     return 0;
179 }
180
181 static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
182                                int *got_frame_ptr, AVPacket *avpkt)
183 {
184     int i, ret;
185     LibGSMDecodeContext *s = avctx->priv_data;
186     uint8_t *buf = avpkt->data;
187     int buf_size = avpkt->size;
188     int16_t *samples;
189
190     if (buf_size < avctx->block_align) {
191         av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
192         return AVERROR_INVALIDDATA;
193     }
194
195     /* get output buffer */
196     s->frame.nb_samples = avctx->frame_size;
197     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
198         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
199         return ret;
200     }
201     samples = (int16_t *)s->frame.data[0];
202
203     for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
204         if ((ret = gsm_decode(s->state, buf, samples)) < 0)
205             return -1;
206         buf     += GSM_BLOCK_SIZE;
207         samples += GSM_FRAME_SIZE;
208     }
209
210     *got_frame_ptr   = 1;
211     *(AVFrame *)data = s->frame;
212
213     return avctx->block_align;
214 }
215
216 static void libgsm_flush(AVCodecContext *avctx) {
217     LibGSMDecodeContext *s = avctx->priv_data;
218     int one = 1;
219
220     gsm_destroy(s->state);
221     s->state = gsm_create();
222     if (avctx->codec_id == CODEC_ID_GSM_MS)
223         gsm_option(s->state, GSM_OPT_WAV49, &one);
224 }
225
226 AVCodec ff_libgsm_decoder = {
227     .name           = "libgsm",
228     .type           = AVMEDIA_TYPE_AUDIO,
229     .id             = CODEC_ID_GSM,
230     .priv_data_size = sizeof(LibGSMDecodeContext),
231     .init           = libgsm_decode_init,
232     .close          = libgsm_decode_close,
233     .decode         = libgsm_decode_frame,
234     .flush          = libgsm_flush,
235     .capabilities   = CODEC_CAP_DR1,
236     .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
237 };
238
239 AVCodec ff_libgsm_ms_decoder = {
240     .name           = "libgsm_ms",
241     .type           = AVMEDIA_TYPE_AUDIO,
242     .id             = CODEC_ID_GSM_MS,
243     .priv_data_size = sizeof(LibGSMDecodeContext),
244     .init           = libgsm_decode_init,
245     .close          = libgsm_decode_close,
246     .decode         = libgsm_decode_frame,
247     .flush          = libgsm_flush,
248     .capabilities   = CODEC_CAP_DR1,
249     .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
250 };