]> git.sesse.net Git - ffmpeg/blob - libavcodec/libspeexdec.c
Don't include common.h from avutil.h
[ffmpeg] / libavcodec / libspeexdec.c
1 /*
2  * Copyright (C) 2008 David Conrad
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <speex/speex.h>
22 #include <speex/speex_header.h>
23 #include <speex/speex_stereo.h>
24 #include <speex/speex_callbacks.h>
25 #include "avcodec.h"
26 #include "libavutil/common.h"
27
28 typedef struct {
29     AVFrame frame;
30     SpeexBits bits;
31     SpeexStereoState stereo;
32     void *dec_state;
33     SpeexHeader *header;
34     int frame_size;
35 } LibSpeexContext;
36
37
38 static av_cold int libspeex_decode_init(AVCodecContext *avctx)
39 {
40     LibSpeexContext *s = avctx->priv_data;
41     const SpeexMode *mode;
42
43     // defaults in the case of a missing header
44     if (avctx->sample_rate <= 8000)
45         mode = &speex_nb_mode;
46     else if (avctx->sample_rate <= 16000)
47         mode = &speex_wb_mode;
48     else
49         mode = &speex_uwb_mode;
50
51     if (avctx->extradata_size >= 80)
52         s->header = speex_packet_to_header(avctx->extradata, avctx->extradata_size);
53
54     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
55     if (s->header) {
56         avctx->sample_rate = s->header->rate;
57         avctx->channels    = s->header->nb_channels;
58         s->frame_size      = s->header->frame_size;
59
60         mode = speex_lib_get_mode(s->header->mode);
61         if (!mode) {
62             av_log(avctx, AV_LOG_ERROR, "Unknown Speex mode %d", s->header->mode);
63             return AVERROR_INVALIDDATA;
64         }
65     } else
66         av_log(avctx, AV_LOG_INFO, "Missing Speex header, assuming defaults.\n");
67
68     if (avctx->channels > 2) {
69         av_log(avctx, AV_LOG_ERROR, "Only stereo and mono are supported.\n");
70         return AVERROR(EINVAL);
71     }
72
73     speex_bits_init(&s->bits);
74     s->dec_state = speex_decoder_init(mode);
75     if (!s->dec_state) {
76         av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex decoder.\n");
77         return -1;
78     }
79
80     if (!s->header) {
81         speex_decoder_ctl(s->dec_state, SPEEX_GET_FRAME_SIZE, &s->frame_size);
82     }
83
84     if (avctx->channels == 2) {
85         SpeexCallback callback;
86         callback.callback_id = SPEEX_INBAND_STEREO;
87         callback.func = speex_std_stereo_request_handler;
88         callback.data = &s->stereo;
89         s->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
90         speex_decoder_ctl(s->dec_state, SPEEX_SET_HANDLER, &callback);
91     }
92
93     avcodec_get_frame_defaults(&s->frame);
94     avctx->coded_frame = &s->frame;
95
96     return 0;
97 }
98
99 static int libspeex_decode_frame(AVCodecContext *avctx, void *data,
100                                  int *got_frame_ptr, AVPacket *avpkt)
101 {
102     uint8_t *buf = avpkt->data;
103     int buf_size = avpkt->size;
104     LibSpeexContext *s = avctx->priv_data;
105     int16_t *output;
106     int ret, consumed = 0;
107
108     /* get output buffer */
109     s->frame.nb_samples = s->frame_size;
110     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
111         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
112         return ret;
113     }
114     output = (int16_t *)s->frame.data[0];
115
116     /* if there is not enough data left for the smallest possible frame,
117        reset the libspeex buffer using the current packet, otherwise ignore
118        the current packet and keep decoding frames from the libspeex buffer. */
119     if (speex_bits_remaining(&s->bits) < 43) {
120         /* check for flush packet */
121         if (!buf || !buf_size) {
122             *got_frame_ptr = 0;
123             return buf_size;
124         }
125         /* set new buffer */
126         speex_bits_read_from(&s->bits, buf, buf_size);
127         consumed = buf_size;
128     }
129
130     /* decode a single frame */
131     ret = speex_decode_int(s->dec_state, &s->bits, output);
132     if (ret <= -2) {
133         av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n");
134         return AVERROR_INVALIDDATA;
135     }
136     if (avctx->channels == 2)
137         speex_decode_stereo_int(output, s->frame_size, &s->stereo);
138
139     *got_frame_ptr   = 1;
140     *(AVFrame *)data = s->frame;
141
142     return consumed;
143 }
144
145 static av_cold int libspeex_decode_close(AVCodecContext *avctx)
146 {
147     LibSpeexContext *s = avctx->priv_data;
148
149     speex_header_free(s->header);
150     speex_bits_destroy(&s->bits);
151     speex_decoder_destroy(s->dec_state);
152
153     return 0;
154 }
155
156 static av_cold void libspeex_decode_flush(AVCodecContext *avctx)
157 {
158     LibSpeexContext *s = avctx->priv_data;
159     speex_bits_reset(&s->bits);
160 }
161
162 AVCodec ff_libspeex_decoder = {
163     .name           = "libspeex",
164     .type           = AVMEDIA_TYPE_AUDIO,
165     .id             = AV_CODEC_ID_SPEEX,
166     .priv_data_size = sizeof(LibSpeexContext),
167     .init           = libspeex_decode_init,
168     .close          = libspeex_decode_close,
169     .decode         = libspeex_decode_frame,
170     .flush          = libspeex_decode_flush,
171     .capabilities   = CODEC_CAP_SUBFRAMES | CODEC_CAP_DELAY | CODEC_CAP_DR1,
172     .long_name      = NULL_IF_CONFIG_SMALL("libspeex Speex"),
173 };