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