]> 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 "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 CODEC_ID_GSM:
73         avctx->frame_size = GSM_FRAME_SIZE;
74         avctx->block_align = GSM_BLOCK_SIZE;
75         break;
76     case 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 CODEC_ID_GSM:
108         gsm_encode(state, samples, avpkt->data);
109         break;
110     case 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 AVCodec ff_libgsm_encoder = {
121     .name           = "libgsm",
122     .type           = AVMEDIA_TYPE_AUDIO,
123     .id             = CODEC_ID_GSM,
124     .init           = libgsm_encode_init,
125     .encode2        = libgsm_encode_frame,
126     .close          = libgsm_encode_close,
127     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
128     .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
129 };
130
131 AVCodec ff_libgsm_ms_encoder = {
132     .name           = "libgsm_ms",
133     .type           = AVMEDIA_TYPE_AUDIO,
134     .id             = CODEC_ID_GSM_MS,
135     .init           = libgsm_encode_init,
136     .encode2        = libgsm_encode_frame,
137     .close          = libgsm_encode_close,
138     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
139     .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
140 };
141
142 typedef struct LibGSMDecodeContext {
143     AVFrame frame;
144     struct gsm_state *state;
145 } LibGSMDecodeContext;
146
147 static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
148     LibGSMDecodeContext *s = avctx->priv_data;
149
150     if (avctx->channels > 1) {
151         av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
152                avctx->channels);
153         return -1;
154     }
155
156     if (!avctx->channels)
157         avctx->channels = 1;
158
159     if (!avctx->sample_rate)
160         avctx->sample_rate = 8000;
161
162     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
163
164     s->state = gsm_create();
165
166     switch(avctx->codec_id) {
167     case CODEC_ID_GSM:
168         avctx->frame_size  = GSM_FRAME_SIZE;
169         avctx->block_align = GSM_BLOCK_SIZE;
170         break;
171     case CODEC_ID_GSM_MS: {
172         int one = 1;
173         gsm_option(s->state, GSM_OPT_WAV49, &one);
174         avctx->frame_size  = 2 * GSM_FRAME_SIZE;
175         avctx->block_align = GSM_MS_BLOCK_SIZE;
176         }
177     }
178
179     avcodec_get_frame_defaults(&s->frame);
180     avctx->coded_frame = &s->frame;
181
182     return 0;
183 }
184
185 static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
186     LibGSMDecodeContext *s = avctx->priv_data;
187
188     gsm_destroy(s->state);
189     s->state = NULL;
190     return 0;
191 }
192
193 static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
194                                int *got_frame_ptr, AVPacket *avpkt)
195 {
196     int i, ret;
197     LibGSMDecodeContext *s = avctx->priv_data;
198     uint8_t *buf = avpkt->data;
199     int buf_size = avpkt->size;
200     int16_t *samples;
201
202     if (buf_size < avctx->block_align) {
203         av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
204         return AVERROR_INVALIDDATA;
205     }
206
207     /* get output buffer */
208     s->frame.nb_samples = avctx->frame_size;
209     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
210         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
211         return ret;
212     }
213     samples = (int16_t *)s->frame.data[0];
214
215     for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
216         if ((ret = gsm_decode(s->state, buf, samples)) < 0)
217             return -1;
218         buf     += GSM_BLOCK_SIZE;
219         samples += GSM_FRAME_SIZE;
220     }
221
222     *got_frame_ptr   = 1;
223     *(AVFrame *)data = s->frame;
224
225     return avctx->block_align;
226 }
227
228 static void libgsm_flush(AVCodecContext *avctx) {
229     LibGSMDecodeContext *s = avctx->priv_data;
230     int one = 1;
231
232     gsm_destroy(s->state);
233     s->state = gsm_create();
234     if (avctx->codec_id == CODEC_ID_GSM_MS)
235         gsm_option(s->state, GSM_OPT_WAV49, &one);
236 }
237
238 AVCodec ff_libgsm_decoder = {
239     .name           = "libgsm",
240     .type           = AVMEDIA_TYPE_AUDIO,
241     .id             = CODEC_ID_GSM,
242     .priv_data_size = sizeof(LibGSMDecodeContext),
243     .init           = libgsm_decode_init,
244     .close          = libgsm_decode_close,
245     .decode         = libgsm_decode_frame,
246     .flush          = libgsm_flush,
247     .capabilities   = CODEC_CAP_DR1,
248     .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
249 };
250
251 AVCodec ff_libgsm_ms_decoder = {
252     .name           = "libgsm_ms",
253     .type           = AVMEDIA_TYPE_AUDIO,
254     .id             = CODEC_ID_GSM_MS,
255     .priv_data_size = sizeof(LibGSMDecodeContext),
256     .init           = libgsm_decode_init,
257     .close          = libgsm_decode_close,
258     .decode         = libgsm_decode_frame,
259     .flush          = libgsm_flush,
260     .capabilities   = CODEC_CAP_DR1,
261     .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
262 };