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