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