Modify the Ogg/Speex demuxer and the libspeex decoder so that they always treat
[libav.git] / libavcodec / libspeexdec.c
1 /*
2 * Copyright (C) 2008 David Conrad
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "avcodec.h"
22 #include <speex/speex.h>
23 #include <speex/speex_header.h>
24 #include <speex/speex_stereo.h>
25 #include <speex/speex_callbacks.h>
26
27 typedef struct {
28 SpeexBits bits;
29 SpeexStereoState stereo;
30 void *dec_state;
31 SpeexHeader *header;
32 } LibSpeexContext;
33
34
35 static av_cold int libspeex_decode_init(AVCodecContext *avctx)
36 {
37 LibSpeexContext *s = avctx->priv_data;
38 const SpeexMode *mode;
39
40 // defaults in the case of a missing header
41 if (avctx->sample_rate <= 8000)
42 mode = &speex_nb_mode;
43 else if (avctx->sample_rate <= 16000)
44 mode = &speex_wb_mode;
45 else
46 mode = &speex_uwb_mode;
47
48 if (avctx->extradata_size >= 80)
49 s->header = speex_packet_to_header(avctx->extradata, avctx->extradata_size);
50
51 avctx->sample_fmt = SAMPLE_FMT_S16;
52 if (s->header) {
53 avctx->sample_rate = s->header->rate;
54 avctx->channels = s->header->nb_channels;
55 avctx->frame_size = s->header->frame_size;
56 if (s->header->frames_per_packet)
57 avctx->frame_size *= s->header->frames_per_packet;
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 -1;
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 -1;
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, &avctx->frame_size);
81
82 if (avctx->channels == 2) {
83 SpeexCallback callback;
84 callback.callback_id = SPEEX_INBAND_STEREO;
85 callback.func = speex_std_stereo_request_handler;
86 callback.data = &s->stereo;
87 s->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
88 speex_decoder_ctl(s->dec_state, SPEEX_SET_HANDLER, &callback);
89 }
90 return 0;
91 }
92
93 static int libspeex_decode_frame(AVCodecContext *avctx,
94 void *data, int *data_size,
95 AVPacket *avpkt)
96 {
97 const uint8_t *buf = avpkt->data;
98 int buf_size = avpkt->size;
99 LibSpeexContext *s = avctx->priv_data;
100 int16_t *output = data, *end;
101 int i, num_samples;
102
103 num_samples = s->header->frame_size * avctx->channels;
104 end = output + *data_size/2;
105
106 speex_bits_read_from(&s->bits, buf, buf_size);
107
108 for (i = 0; speex_bits_remaining(&s->bits) && output + num_samples < end; i++) {
109 int ret = speex_decode_int(s->dec_state, &s->bits, output);
110 if (ret <= -2) {
111 av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n");
112 return -1;
113 } else if (ret == -1)
114 // end of stream
115 break;
116
117 if (avctx->channels == 2)
118 speex_decode_stereo_int(output, s->header->frame_size, &s->stereo);
119
120 output += num_samples;
121 }
122
123 avctx->frame_size = s->header->frame_size * i;
124 *data_size = avctx->channels * avctx->frame_size * sizeof(*output);
125 return buf_size;
126 }
127
128 static av_cold int libspeex_decode_close(AVCodecContext *avctx)
129 {
130 LibSpeexContext *s = avctx->priv_data;
131
132 speex_header_free(s->header);
133 speex_bits_destroy(&s->bits);
134 speex_decoder_destroy(s->dec_state);
135
136 return 0;
137 }
138
139 AVCodec libspeex_decoder = {
140 "libspeex",
141 CODEC_TYPE_AUDIO,
142 CODEC_ID_SPEEX,
143 sizeof(LibSpeexContext),
144 libspeex_decode_init,
145 NULL,
146 libspeex_decode_close,
147 libspeex_decode_frame,
148 .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),
149 };