1 /*
2 * Speex decoder by Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>
3 *
4 * This code may be be relicensed under the terms of the GNU LGPL when it
5 * becomes part of the FFmpeg project (ffmpeg.org)
6 *
7 * This file is part of MPlayer.
8 *
9 * MPlayer is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MPlayer is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #include <stdlib.h>
25 #include <speex/speex.h>
26 #include <speex/speex_stereo.h>
27 #include <speex/speex_header.h>
28
29 #include "config.h"
30 #include "mp_msg.h"
31 #include "ad_internal.h"
32
33 static const ad_info_t info = {
34 "Speex audio decoder",
35 "speex",
36 "Reimar Döffinger",
37 "",
38 ""
39 };
40
41 LIBAD_EXTERN(speex)
42
43 typedef struct {
44 SpeexBits bits;
45 void *dec_context;
46 SpeexStereoState stereo;
47 SpeexHeader *hdr;
48 } context_t;
49
50 #define MAX_FRAMES_PER_PACKET 100
51
preinit(sh_audio_t * sh)52 static int preinit(sh_audio_t *sh) {
53 sh->audio_out_minsize = 2 * 320 * MAX_FRAMES_PER_PACKET * 2 * sizeof(short);
54 return 1;
55 }
56
read_le32(const uint8_t ** src)57 static int read_le32(const uint8_t **src) {
58 const uint8_t *p = *src;
59 *src += 4;
60 return p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24);
61 }
62
init(sh_audio_t * sh)63 static int init(sh_audio_t *sh) {
64 context_t *ctx = calloc(1, sizeof(context_t));
65 const uint8_t *hdr = (const uint8_t *)(sh->wf + 1);
66 const SpeexMode *spx_mode;
67 const SpeexStereoState st_st = SPEEX_STEREO_STATE_INIT; // hack
68 if (sh->wf && sh->wf->cbSize >= 80)
69 ctx->hdr = speex_packet_to_header((char *)&sh->wf[1], sh->wf->cbSize);
70 if (!ctx->hdr && sh->wf->cbSize == 0x72 && hdr[0] == 1 && hdr[1] == 0) {
71 // speex.acm format: raw SpeexHeader dump
72 ctx->hdr = calloc(1, sizeof(*ctx->hdr));
73 hdr += 2;
74 hdr += 8; // identifier string
75 hdr += 20; // version string
76 ctx->hdr->speex_version_id = read_le32(&hdr);
77 ctx->hdr->header_size = read_le32(&hdr);
78 ctx->hdr->rate = read_le32(&hdr);
79 ctx->hdr->mode = read_le32(&hdr);
80 ctx->hdr->mode_bitstream_version = read_le32(&hdr);
81 ctx->hdr->nb_channels = read_le32(&hdr);
82 ctx->hdr->bitrate = read_le32(&hdr);
83 ctx->hdr->frame_size = read_le32(&hdr);
84 ctx->hdr->vbr = read_le32(&hdr);
85 ctx->hdr->frames_per_packet = read_le32(&hdr);
86 }
87 if (!ctx->hdr) {
88 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Invalid or missing extradata! Assuming defaults.\n");
89 ctx->hdr = calloc(1, sizeof(*ctx->hdr));
90 ctx->hdr->frames_per_packet = 1;
91 ctx->hdr->mode = 0;
92 if (sh->wf) {
93 ctx->hdr->nb_channels = sh->wf->nChannels;
94 ctx->hdr->rate = sh->wf->nSamplesPerSec;
95 if (ctx->hdr->rate > 16000)
96 ctx->hdr->mode = 2;
97 else if (ctx->hdr->rate > 8000)
98 ctx->hdr->mode = 1;
99 }
100 }
101 if (ctx->hdr->nb_channels != 1 && ctx->hdr->nb_channels != 2) {
102 mp_msg(MSGT_DECAUDIO, MSGL_WARN, "Invalid number of channels (%i), "
103 "assuming mono\n", ctx->hdr->nb_channels);
104 ctx->hdr->nb_channels = 1;
105 }
106 if (ctx->hdr->frames_per_packet > MAX_FRAMES_PER_PACKET) {
107 mp_msg(MSGT_DECAUDIO, MSGL_WARN, "Invalid number of frames per packet (%i), "
108 "assuming 1\n", ctx->hdr->frames_per_packet);
109 ctx->hdr->frames_per_packet = 1;
110 }
111 switch (ctx->hdr->mode) {
112 case 0:
113 spx_mode = &speex_nb_mode; break;
114 case 1:
115 spx_mode = &speex_wb_mode; break;
116 case 2:
117 spx_mode = &speex_uwb_mode; break;
118 default:
119 mp_msg(MSGT_DECAUDIO, MSGL_WARN, "Unknown speex mode (%i)\n", ctx->hdr->mode);
120 spx_mode = &speex_nb_mode;
121 }
122 ctx->dec_context = speex_decoder_init(spx_mode);
123 speex_bits_init(&ctx->bits);
124 memcpy(&ctx->stereo, &st_st, sizeof(ctx->stereo)); // hack part 2
125 sh->channels = ctx->hdr->nb_channels;
126 sh->samplerate = ctx->hdr->rate;
127 sh->samplesize = 2;
128 sh->sample_format = AF_FORMAT_S16_NE;
129 sh->context = ctx;
130 return 1;
131 }
132
uninit(sh_audio_t * sh)133 static void uninit(sh_audio_t *sh) {
134 context_t *ctx = sh->context;
135 if (ctx) {
136 speex_bits_destroy(&ctx->bits);
137 speex_decoder_destroy(ctx->dec_context);
138 free(ctx->hdr);
139 free(ctx);
140 }
141 ctx = NULL;
142 }
143
decode_audio(sh_audio_t * sh,unsigned char * buf,int minlen,int maxlen)144 static int decode_audio(sh_audio_t *sh, unsigned char *buf,
145 int minlen, int maxlen) {
146 double pts;
147 context_t *ctx = sh->context;
148 int len, framelen, framesamples;
149 char *packet;
150 int i, err;
151 speex_decoder_ctl(ctx->dec_context, SPEEX_GET_FRAME_SIZE, &framesamples);
152 framelen = framesamples * ctx->hdr->nb_channels * sizeof(short);
153 if (maxlen < ctx->hdr->frames_per_packet * framelen) {
154 mp_msg(MSGT_DECAUDIO, MSGL_V, "maxlen too small in decode_audio\n");
155 return -1;
156 }
157 len = ds_get_packet_pts(sh->ds, (unsigned char **)&packet, &pts);
158 if (len <= 0) return -1;
159 if (sh->pts == MP_NOPTS_VALUE)
160 sh->pts = 0;
161 if (pts != MP_NOPTS_VALUE) {
162 sh->pts = pts;
163 sh->pts_bytes = 0;
164 }
165 speex_bits_read_from(&ctx->bits, packet, len);
166 i = ctx->hdr->frames_per_packet;
167 do {
168 err = speex_decode_int(ctx->dec_context, &ctx->bits, (short *)buf);
169 if (err == -2)
170 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Error decoding file.\n");
171 if (ctx->hdr->nb_channels == 2)
172 speex_decode_stereo_int((short *)buf, framesamples, &ctx->stereo);
173 buf = &buf[framelen];
174 } while (--i > 0);
175 sh->pts_bytes += ctx->hdr->frames_per_packet * framelen;
176 return ctx->hdr->frames_per_packet * framelen;
177 }
178
control(sh_audio_t * sh,int cmd,void * arg,...)179 static int control(sh_audio_t *sh, int cmd, void *arg, ...) {
180 return CONTROL_UNKNOWN;
181 }
182