2 * Speex decoder by Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>
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)
7 * This file is part of MPlayer.
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.
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.
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.
26 #include <speex/speex.h>
27 #include <speex/speex_stereo.h>
28 #include <speex/speex_header.h>
29 #include "ad_internal.h"
31 static const ad_info_t info
= {
32 "Speex audio decoder",
44 SpeexStereoState stereo
;
48 #define MAX_FRAMES_PER_PACKET 100
50 static int preinit(sh_audio_t
*sh
) {
51 sh
->audio_out_minsize
= 2 * 320 * MAX_FRAMES_PER_PACKET
* 2 * sizeof(short);
55 static int read_le32(const uint8_t **src
) {
56 const uint8_t *p
= *src
;
58 return p
[0] + (p
[1] << 8) + (p
[2] << 16) + (p
[3] << 24);
61 static int init(sh_audio_t
*sh
) {
62 context_t
*ctx
= calloc(1, sizeof(context_t
));
63 const uint8_t *hdr
= (const uint8_t *)(sh
->wf
+ 1);
64 const SpeexMode
*spx_mode
;
65 const SpeexStereoState st_st
= SPEEX_STEREO_STATE_INIT
; // hack
66 if (sh
->wf
&& sh
->wf
->cbSize
>= 80)
67 ctx
->hdr
= speex_packet_to_header((char *)&sh
->wf
[1], sh
->wf
->cbSize
);
68 if (!ctx
->hdr
&& sh
->wf
->cbSize
== 0x72 && hdr
[0] == 1 && hdr
[1] == 0) {
69 // speex.acm format: raw SpeexHeader dump
70 ctx
->hdr
= calloc(1, sizeof(*ctx
->hdr
));
72 hdr
+= 8; // identifier string
73 hdr
+= 20; // version string
74 ctx
->hdr
->speex_version_id
= read_le32(&hdr
);
75 ctx
->hdr
->header_size
= read_le32(&hdr
);
76 ctx
->hdr
->rate
= read_le32(&hdr
);
77 ctx
->hdr
->mode
= read_le32(&hdr
);
78 ctx
->hdr
->mode_bitstream_version
= read_le32(&hdr
);
79 ctx
->hdr
->nb_channels
= read_le32(&hdr
);
80 ctx
->hdr
->bitrate
= read_le32(&hdr
);
81 ctx
->hdr
->frame_size
= read_le32(&hdr
);
82 ctx
->hdr
->vbr
= read_le32(&hdr
);
83 ctx
->hdr
->frames_per_packet
= read_le32(&hdr
);
86 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "Invalid or missing extradata! Assuming defaults.\n");
87 ctx
->hdr
= calloc(1, sizeof(*ctx
->hdr
));
88 ctx
->hdr
->frames_per_packet
= 1;
91 ctx
->hdr
->nb_channels
= sh
->wf
->nChannels
;
92 ctx
->hdr
->rate
= sh
->wf
->nSamplesPerSec
;
93 if (ctx
->hdr
->rate
> 16000)
95 else if (ctx
->hdr
->rate
> 8000)
99 if (ctx
->hdr
->nb_channels
!= 1 && ctx
->hdr
->nb_channels
!= 2) {
100 mp_msg(MSGT_DECAUDIO
, MSGL_WARN
, "Invalid number of channels (%i), "
101 "assuming mono\n", ctx
->hdr
->nb_channels
);
102 ctx
->hdr
->nb_channels
= 1;
104 if (ctx
->hdr
->frames_per_packet
> MAX_FRAMES_PER_PACKET
) {
105 mp_msg(MSGT_DECAUDIO
, MSGL_WARN
, "Invalid number of frames per packet (%i), "
106 "assuming 1\n", ctx
->hdr
->frames_per_packet
);
107 ctx
->hdr
->frames_per_packet
= 1;
109 switch (ctx
->hdr
->mode
) {
111 spx_mode
= &speex_nb_mode
; break;
113 spx_mode
= &speex_wb_mode
; break;
115 spx_mode
= &speex_uwb_mode
; break;
117 mp_msg(MSGT_DECAUDIO
, MSGL_WARN
, "Unknown speex mode (%i)\n", ctx
->hdr
->mode
);
118 spx_mode
= &speex_nb_mode
;
120 ctx
->dec_context
= speex_decoder_init(spx_mode
);
121 speex_bits_init(&ctx
->bits
);
122 memcpy(&ctx
->stereo
, &st_st
, sizeof(ctx
->stereo
)); // hack part 2
123 sh
->channels
= ctx
->hdr
->nb_channels
;
124 sh
->samplerate
= ctx
->hdr
->rate
;
126 sh
->sample_format
= AF_FORMAT_S16_NE
;
131 static void uninit(sh_audio_t
*sh
) {
132 context_t
*ctx
= sh
->context
;
134 speex_bits_destroy(&ctx
->bits
);
135 speex_decoder_destroy(ctx
->dec_context
);
142 static int decode_audio(sh_audio_t
*sh
, unsigned char *buf
,
143 int minlen
, int maxlen
) {
145 context_t
*ctx
= sh
->context
;
146 int len
, framelen
, framesamples
;
149 speex_decoder_ctl(ctx
->dec_context
, SPEEX_GET_FRAME_SIZE
, &framesamples
);
150 framelen
= framesamples
* ctx
->hdr
->nb_channels
* sizeof(short);
151 if (maxlen
< ctx
->hdr
->frames_per_packet
* framelen
) {
152 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "maxlen too small in decode_audio\n");
155 len
= ds_get_packet_pts(sh
->ds
, (unsigned char **)&packet
, &pts
);
156 if (len
<= 0) return -1;
157 if (sh
->pts
== MP_NOPTS_VALUE
)
159 if (pts
!= MP_NOPTS_VALUE
) {
163 speex_bits_read_from(&ctx
->bits
, packet
, len
);
164 i
= ctx
->hdr
->frames_per_packet
;
166 err
= speex_decode_int(ctx
->dec_context
, &ctx
->bits
, (short *)buf
);
168 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "Error decoding file.\n");
169 if (ctx
->hdr
->nb_channels
== 2)
170 speex_decode_stereo_int((short *)buf
, framesamples
, &ctx
->stereo
);
171 buf
= &buf
[framelen
];
173 sh
->pts_bytes
+= ctx
->hdr
->frames_per_packet
* framelen
;
174 return ctx
->hdr
->frames_per_packet
* framelen
;
177 static int control(sh_audio_t
*sh
, int cmd
, void *arg
, ...) {
178 return CONTROL_UNKNOWN
;