annotate libmpcodecs/ad_speex.c @ 17960:ec93436dc52d

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