]> git.sesse.net Git - ffmpeg/blob - libavcodec/libspeexdec.c
lavfi: add video buffer sink, and use it in avtools
[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
27 typedef struct {
28     AVFrame frame;
29     SpeexBits bits;
30     SpeexStereoState stereo;
31     void *dec_state;
32     SpeexHeader *header;
33     int frame_size;
34 } LibSpeexContext;
35
36
37 static av_cold int libspeex_decode_init(AVCodecContext *avctx)
38 {
39     LibSpeexContext *s = avctx->priv_data;
40     const SpeexMode *mode;
41
42     // defaults in the case of a missing header
43     if (avctx->sample_rate <= 8000)
44         mode = &speex_nb_mode;
45     else if (avctx->sample_rate <= 16000)
46         mode = &speex_wb_mode;
47     else
48         mode = &speex_uwb_mode;
49
50     if (avctx->extradata_size >= 80)
51         s->header = speex_packet_to_header(avctx->extradata, avctx->extradata_size);
52
53     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
54     if (s->header) {
55         avctx->sample_rate = s->header->rate;
56         avctx->channels    = s->header->nb_channels;
57         s->frame_size      = s->header->frame_size;
58
59         mode = speex_lib_get_mode(s->header->mode);
60         if (!mode) {
61             av_log(avctx, AV_LOG_ERROR, "Unknown Speex mode %d", s->header->mode);
62             return AVERROR_INVALIDDATA;
63         }
64     } else
65         av_log(avctx, AV_LOG_INFO, "Missing Speex header, assuming defaults.\n");
66
67     if (avctx->channels > 2) {
68         av_log(avctx, AV_LOG_ERROR, "Only stereo and mono are supported.\n");
69         return AVERROR(EINVAL);
70     }
71
72     speex_bits_init(&s->bits);
73     s->dec_state = speex_decoder_init(mode);
74     if (!s->dec_state) {
75         av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex decoder.\n");
76         return -1;
77     }
78
79     if (!s->header) {
80         speex_decoder_ctl(s->dec_state, SPEEX_GET_FRAME_SIZE, &s->frame_size);
81     }
82
83     if (avctx->channels == 2) {
84         SpeexCallback callback;
85         callback.callback_id = SPEEX_INBAND_STEREO;
86         callback.func = speex_std_stereo_request_handler;
87         callback.data = &s->stereo;
88         s->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
89         speex_decoder_ctl(s->dec_state, SPEEX_SET_HANDLER, &callback);
90     }
91
92     avcodec_get_frame_defaults(&s->frame);
93     avctx->coded_frame = &s->frame;
94
95     return 0;
96 }
97
98 static int libspeex_decode_frame(AVCodecContext *avctx, void *data,
99                                  int *got_frame_ptr, AVPacket *avpkt)
100 {
101     uint8_t *buf = avpkt->data;
102     int buf_size = avpkt->size;
103     LibSpeexContext *s = avctx->priv_data;
104     int16_t *output;
105     int ret, consumed = 0;
106
107     /* get output buffer */
108     s->frame.nb_samples = s->frame_size;
109     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
110         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
111         return ret;
112     }
113     output = (int16_t *)s->frame.data[0];
114
115     /* if there is not enough data left for the smallest possible frame,
116        reset the libspeex buffer using the current packet, otherwise ignore
117        the current packet and keep decoding frames from the libspeex buffer. */
118     if (speex_bits_remaining(&s->bits) < 43) {
119         /* check for flush packet */
120         if (!buf || !buf_size) {
121             *got_frame_ptr = 0;
122             return buf_size;
123         }
124         /* set new buffer */
125         speex_bits_read_from(&s->bits, buf, buf_size);
126         consumed = buf_size;
127     }
128
129     /* decode a single frame */
130     ret = speex_decode_int(s->dec_state, &s->bits, output);
131     if (ret <= -2) {
132         av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n");
133         return AVERROR_INVALIDDATA;
134     }
135     if (avctx->channels == 2)
136         speex_decode_stereo_int(output, s->frame_size, &s->stereo);
137
138     *got_frame_ptr   = 1;
139     *(AVFrame *)data = s->frame;
140
141     return consumed;
142 }
143
144 static av_cold int libspeex_decode_close(AVCodecContext *avctx)
145 {
146     LibSpeexContext *s = avctx->priv_data;
147
148     speex_header_free(s->header);
149     speex_bits_destroy(&s->bits);
150     speex_decoder_destroy(s->dec_state);
151
152     return 0;
153 }
154
155 static av_cold void libspeex_decode_flush(AVCodecContext *avctx)
156 {
157     LibSpeexContext *s = avctx->priv_data;
158     speex_bits_reset(&s->bits);
159 }
160
161 AVCodec ff_libspeex_decoder = {
162     .name           = "libspeex",
163     .type           = AVMEDIA_TYPE_AUDIO,
164     .id             = CODEC_ID_SPEEX,
165     .priv_data_size = sizeof(LibSpeexContext),
166     .init           = libspeex_decode_init,
167     .close          = libspeex_decode_close,
168     .decode         = libspeex_decode_frame,
169     .flush          = libspeex_decode_flush,
170     .capabilities   = CODEC_CAP_SUBFRAMES | CODEC_CAP_DELAY | CODEC_CAP_DR1,
171     .long_name      = NULL_IF_CONFIG_SMALL("libspeex Speex"),
172 };