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