]> git.sesse.net Git - ffmpeg/blob - libavcodec/libgsm.c
Merge commit '58b619c8a226cc4564ad5af291bc99a04f89ee56'
[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 "internal.h"
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     gsm_destroy(avctx->priv_data);
42     avctx->priv_data = NULL;
43     return 0;
44 }
45
46 static av_cold int libgsm_encode_init(AVCodecContext *avctx) {
47     if (avctx->channels > 1) {
48         av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
49                avctx->channels);
50         return -1;
51     }
52
53     if (avctx->sample_rate != 8000) {
54         av_log(avctx, AV_LOG_ERROR, "Sample rate 8000Hz required for GSM, got %dHz\n",
55                avctx->sample_rate);
56         if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
57             return -1;
58     }
59     if (avctx->bit_rate != 13000 /* Official */ &&
60         avctx->bit_rate != 13200 /* Very common */ &&
61         avctx->bit_rate != 0 /* Unknown; a.o. mov does not set bitrate when decoding */ ) {
62         av_log(avctx, AV_LOG_ERROR, "Bitrate 13000bps required for GSM, got %dbps\n",
63                avctx->bit_rate);
64         if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
65             return -1;
66     }
67
68     avctx->priv_data = gsm_create();
69     if (!avctx->priv_data)
70         goto error;
71
72     switch(avctx->codec_id) {
73     case AV_CODEC_ID_GSM:
74         avctx->frame_size = GSM_FRAME_SIZE;
75         avctx->block_align = GSM_BLOCK_SIZE;
76         break;
77     case AV_CODEC_ID_GSM_MS: {
78         int one = 1;
79         gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
80         avctx->frame_size = 2*GSM_FRAME_SIZE;
81         avctx->block_align = GSM_MS_BLOCK_SIZE;
82         }
83     }
84
85 #if FF_API_OLD_ENCODE_AUDIO
86     avctx->coded_frame= avcodec_alloc_frame();
87     if (!avctx->coded_frame)
88         goto error;
89 #endif
90
91     return 0;
92 error:
93     libgsm_encode_close(avctx);
94     return -1;
95 }
96
97 static int libgsm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
98                                const AVFrame *frame, int *got_packet_ptr)
99 {
100     int ret;
101     gsm_signal *samples = (gsm_signal *)frame->data[0];
102     struct gsm_state *state = avctx->priv_data;
103
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         gsm_encode(state, samples,                  avpkt->data);
113         gsm_encode(state, samples + GSM_FRAME_SIZE, avpkt->data + 32);
114     }
115
116     *got_packet_ptr = 1;
117     return 0;
118 }
119
120
121 #if CONFIG_LIBGSM_ENCODER
122 AVCodec ff_libgsm_encoder = {
123     .name           = "libgsm",
124     .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     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
130                                                      AV_SAMPLE_FMT_NONE },
131     .long_name      = NULL_IF_CONFIG_SMALL("libgsm GSM"),
132 };
133 #endif
134 #if CONFIG_LIBGSM_MS_ENCODER
135 AVCodec ff_libgsm_ms_encoder = {
136     .name           = "libgsm_ms",
137     .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     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
143                                                      AV_SAMPLE_FMT_NONE },
144     .long_name      = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
145 };
146 #endif
147
148 typedef struct LibGSMDecodeContext {
149     AVFrame frame;
150     struct gsm_state *state;
151 } LibGSMDecodeContext;
152
153 static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
154     LibGSMDecodeContext *s = avctx->priv_data;
155
156     if (avctx->channels > 1) {
157         av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
158                avctx->channels);
159         return -1;
160     }
161
162     if (!avctx->channels)
163         avctx->channels = 1;
164
165     if (!avctx->sample_rate)
166         avctx->sample_rate = 8000;
167
168     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
169
170     s->state = gsm_create();
171
172     switch(avctx->codec_id) {
173     case AV_CODEC_ID_GSM:
174         avctx->frame_size  = GSM_FRAME_SIZE;
175         avctx->block_align = GSM_BLOCK_SIZE;
176         break;
177     case AV_CODEC_ID_GSM_MS: {
178         int one = 1;
179         gsm_option(s->state, GSM_OPT_WAV49, &one);
180         avctx->frame_size  = 2 * GSM_FRAME_SIZE;
181         avctx->block_align = GSM_MS_BLOCK_SIZE;
182         }
183     }
184
185     avcodec_get_frame_defaults(&s->frame);
186     avctx->coded_frame = &s->frame;
187
188     return 0;
189 }
190
191 static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
192     LibGSMDecodeContext *s = avctx->priv_data;
193
194     gsm_destroy(s->state);
195     s->state = NULL;
196     return 0;
197 }
198
199 static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
200                                int *got_frame_ptr, AVPacket *avpkt)
201 {
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     if (buf_size < avctx->block_align) {
209         av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
210         return AVERROR_INVALIDDATA;
211     }
212
213     /* get output buffer */
214     s->frame.nb_samples = avctx->frame_size;
215     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
216         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
217         return ret;
218     }
219     samples = (int16_t *)s->frame.data[0];
220
221     for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
222         if ((ret = gsm_decode(s->state, buf, samples)) < 0)
223             return -1;
224         buf     += GSM_BLOCK_SIZE;
225         samples += GSM_FRAME_SIZE;
226     }
227
228     *got_frame_ptr   = 1;
229     *(AVFrame *)data = s->frame;
230
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     gsm_destroy(s->state);
239     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     .name           = "libgsm",
247     .type           = AVMEDIA_TYPE_AUDIO,
248     .id             = AV_CODEC_ID_GSM,
249     .priv_data_size = sizeof(LibGSMDecodeContext),
250     .init           = libgsm_decode_init,
251     .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 #endif
258 #if CONFIG_LIBGSM_MS_DECODER
259 AVCodec ff_libgsm_ms_decoder = {
260     .name           = "libgsm_ms",
261     .type           = AVMEDIA_TYPE_AUDIO,
262     .id             = AV_CODEC_ID_GSM_MS,
263     .priv_data_size = sizeof(LibGSMDecodeContext),
264     .init           = libgsm_decode_init,
265     .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