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