16916
|
1 /**
|
|
2 * Speex decoder by Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>
|
|
3 * License: GPL
|
|
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 #include "config.h"
|
|
8 #include <speex/speex.h>
|
|
9 #include <speex/speex_stereo.h>
|
|
10 #include <speex/speex_header.h>
|
|
11 #include "ad_internal.h"
|
|
12
|
|
13 static ad_info_t info = {
|
|
14 "Speex audio decoder",
|
|
15 "speex",
|
|
16 "Reimar Döffinger",
|
|
17 "",
|
|
18 ""
|
|
19 };
|
|
20
|
|
21 LIBAD_EXTERN(speex)
|
|
22
|
|
23 typedef struct {
|
|
24 SpeexBits bits;
|
|
25 void *dec_context;
|
|
26 SpeexStereoState stereo;
|
|
27 SpeexHeader *hdr;
|
|
28 } context_t;
|
|
29
|
|
30 static int preinit(sh_audio_t *sh) {
|
|
31 return 1;
|
|
32 }
|
|
33
|
|
34 static int init(sh_audio_t *sh) {
|
|
35 context_t *ctx = (context_t *)calloc(1, sizeof(context_t));
|
|
36 const SpeexMode *spx_mode;
|
|
37 const SpeexStereoState st_st = SPEEX_STEREO_STATE_INIT; // hack
|
|
38 int mode;
|
|
39 if (!sh->wf || sh->wf->cbSize < 80) {
|
|
40 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Missing extradata!\n");
|
|
41 return 0;
|
|
42 }
|
|
43 ctx->hdr = speex_packet_to_header((char *)&sh->wf[1], sh->wf->cbSize);
|
|
44 if (ctx->hdr->nb_channels != 1 && ctx->hdr->nb_channels != 2) {
|
|
45 mp_msg(MSGT_DECAUDIO, MSGL_WARN, "Invalid number of channels (%i), "
|
|
46 "assuming mono\n", ctx->hdr->nb_channels);
|
|
47 ctx->hdr->nb_channels = 1;
|
|
48 }
|
|
49 switch (ctx->hdr->mode) {
|
|
50 case 0:
|
|
51 spx_mode = &speex_nb_mode; break;
|
|
52 case 1:
|
|
53 spx_mode = &speex_wb_mode; break;
|
|
54 case 2:
|
|
55 spx_mode = &speex_uwb_mode; break;
|
|
56 default:
|
|
57 mp_msg(MSGT_DECAUDIO, MSGL_WARN, "Unknown speex mode (%i)\n", mode);
|
|
58 spx_mode = &speex_nb_mode;
|
|
59 }
|
|
60 ctx->dec_context = speex_decoder_init(spx_mode);
|
|
61 speex_bits_init(&ctx->bits);
|
|
62 memcpy(&ctx->stereo, &st_st, sizeof(ctx->stereo)); // hack part 2
|
|
63 sh->channels = ctx->hdr->nb_channels;
|
|
64 sh->samplerate = ctx->hdr->rate;
|
|
65 sh->samplesize = 2;
|
|
66 sh->sample_format = AF_FORMAT_S16_NE;
|
|
67 sh->context = ctx;
|
|
68 return 1;
|
|
69 }
|
|
70
|
|
71 static void uninit(sh_audio_t *sh) {
|
|
72 context_t *ctx = sh->context;
|
|
73 if (ctx) {
|
|
74 speex_bits_destroy(&ctx->bits);
|
|
75 speex_decoder_destroy(ctx->dec_context);
|
|
76 if (ctx->hdr)
|
|
77 speex_free(ctx->hdr);
|
|
78 free(ctx);
|
|
79 }
|
|
80 ctx = NULL;
|
|
81 }
|
|
82
|
|
83 static int decode_audio(sh_audio_t *sh, unsigned char *buf,
|
|
84 int minlen, int maxlen) {
|
|
85 context_t *ctx = sh->context;
|
|
86 int len, framelen, framesamples;
|
|
87 char *packet;
|
|
88 int i, err;
|
|
89 speex_decoder_ctl(ctx->dec_context, SPEEX_GET_FRAME_SIZE, &framesamples);
|
|
90 framelen = framesamples * ctx->hdr->nb_channels * sizeof(short);
|
|
91 if (maxlen < ctx->hdr->frames_per_packet * framelen) {
|
|
92 mp_msg(MSGT_DECAUDIO, MSGL_V, "maxlen too small in decode_audio\n");
|
|
93 return -1;
|
|
94 }
|
|
95 len = ds_get_packet(sh->ds, (unsigned char **)&packet);
|
|
96 if (len <= 0) return -1;
|
|
97 speex_bits_read_from(&ctx->bits, packet, len);
|
|
98 i = ctx->hdr->frames_per_packet;
|
|
99 do {
|
|
100 err = speex_decode_int(ctx->dec_context, &ctx->bits, (short *)buf);
|
|
101 if (err == -2)
|
|
102 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Error decoding file.\n");
|
|
103 if (ctx->hdr->nb_channels == 2)
|
|
104 speex_decode_stereo_int((short *)buf, framesamples, &ctx->stereo);
|
|
105 buf = &buf[framelen];
|
|
106 } while (--i > 0);
|
|
107 return ctx->hdr->frames_per_packet * framelen;
|
|
108 }
|
|
109
|
|
110 static int control(sh_audio_t *sh, int cmd, void *arg, ...) {
|
|
111 return CONTROL_UNKNOWN;
|
|
112 }
|
|
113
|