5479
|
1 // SAMPLE audio decoder - you can use this file as template when creating new codec!
|
|
2
|
|
3 #include <stdio.h>
|
|
4 #include <stdlib.h>
|
|
5 #include <unistd.h>
|
|
6
|
|
7 #include "config.h"
|
|
8
|
|
9 #ifdef USE_LIBMAD
|
|
10
|
|
11 #include "ad_internal.h"
|
|
12
|
|
13 static ad_info_t info = {
|
|
14 "libmad mpeg audio decoder",
|
|
15 "libmad",
|
|
16 "A'rpi",
|
|
17 "libmad...",
|
|
18 "based on Xine's libmad/xine_decoder.c"
|
|
19 };
|
|
20
|
|
21 LIBAD_EXTERN(libmad)
|
|
22
|
|
23 #include <mad.h>
|
|
24
|
|
25 typedef struct mad_decoder_s {
|
|
26
|
|
27 struct mad_synth synth;
|
|
28 struct mad_stream stream;
|
|
29 struct mad_frame frame;
|
|
30
|
|
31 int have_frame;
|
|
32
|
|
33 int output_sampling_rate;
|
|
34 int output_open;
|
|
35 int output_mode;
|
|
36
|
|
37 } mad_decoder_t;
|
|
38
|
|
39 static int preinit(sh_audio_t *sh){
|
|
40
|
|
41 mad_decoder_t *this = (mad_decoder_t *) malloc(sizeof(mad_decoder_t));
|
|
42 memset(this,0,sizeof(mad_decoder_t));
|
|
43 sh->context = this;
|
|
44
|
|
45 mad_synth_init (&this->synth);
|
|
46 mad_stream_init (&this->stream);
|
|
47 mad_frame_init (&this->frame);
|
|
48
|
|
49 sh->audio_out_minsize=2*4608;
|
|
50 sh->audio_in_minsize=4096;
|
|
51
|
|
52 return 1;
|
|
53 }
|
|
54
|
|
55 static int read_frame(sh_audio_t *sh){
|
|
56 mad_decoder_t *this = (mad_decoder_t *) sh->context;
|
|
57 int len;
|
|
58
|
|
59 while((len=demux_read_data(sh->ds,&sh->a_in_buffer[sh->a_in_buffer_len],
|
|
60 sh->a_in_buffer_size-sh->a_in_buffer_len))>0){
|
|
61 sh->a_in_buffer_len+=len;
|
|
62 while(1){
|
|
63 int ret;
|
|
64 mad_stream_buffer (&this->stream, sh->a_in_buffer, sh->a_in_buffer_len);
|
|
65 ret=mad_frame_decode (&this->frame, &this->stream);
|
|
66 if (this->stream.next_frame) {
|
|
67 int num_bytes =
|
|
68 (char*)sh->a_in_buffer+sh->a_in_buffer_len - (char*)this->stream.next_frame;
|
|
69 memmove(sh->a_in_buffer, this->stream.next_frame, num_bytes);
|
|
70 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"libmad: %d bytes processed\n",sh->a_in_buffer_len-num_bytes);
|
|
71 sh->a_in_buffer_len = num_bytes;
|
|
72 }
|
|
73 if (ret == 0) return 1; // OK!!!
|
|
74 // error! try to resync!
|
|
75 if(this->stream.error==MAD_ERROR_BUFLEN) break;
|
|
76 }
|
|
77 }
|
|
78 mp_msg(MSGT_DECAUDIO,MSGL_INFO,"Cannot sync MAD frame\n");
|
|
79 return 0;
|
|
80 }
|
|
81
|
|
82 static int init(sh_audio_t *sh){
|
|
83 mad_decoder_t *this = (mad_decoder_t *) sh->context;
|
|
84
|
|
85 this->have_frame=read_frame(sh);
|
|
86 if(!this->have_frame) return 0; // failed to sync...
|
|
87
|
|
88 sh->channels=(this->frame.header.mode == MAD_MODE_SINGLE_CHANNEL) ? 1 : 2;
|
|
89 sh->samplerate=this->frame.header.samplerate;
|
|
90 sh->i_bps=this->frame.header.bitrate/8;
|
13430
|
91 sh->samplesize=2;
|
5479
|
92
|
|
93 return 1;
|
|
94 }
|
|
95
|
|
96 static void uninit(sh_audio_t *sh){
|
|
97 mad_decoder_t *this = (mad_decoder_t *) sh->context;
|
|
98 mad_synth_finish (&this->synth);
|
|
99 mad_frame_finish (&this->frame);
|
|
100 mad_stream_finish(&this->stream);
|
|
101 free(sh->context);
|
|
102 }
|
|
103
|
|
104 /* utility to scale and round samples to 16 bits */
|
|
105 static inline signed int scale(mad_fixed_t sample) {
|
|
106 /* round */
|
|
107 sample += (1L << (MAD_F_FRACBITS - 16));
|
|
108
|
|
109 /* clip */
|
|
110 if (sample >= MAD_F_ONE)
|
|
111 sample = MAD_F_ONE - 1;
|
|
112 else if (sample < -MAD_F_ONE)
|
|
113 sample = -MAD_F_ONE;
|
|
114
|
|
115 /* quantize */
|
|
116 return sample >> (MAD_F_FRACBITS + 1 - 16);
|
|
117 }
|
|
118
|
|
119 static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen){
|
|
120 mad_decoder_t *this = (mad_decoder_t *) sh->context;
|
|
121 int len=0;
|
|
122
|
|
123 while(len<minlen && len+4608<=maxlen){
|
|
124 if(!this->have_frame) this->have_frame=read_frame(sh);
|
|
125 if(!this->have_frame) break; // failed to sync... or EOF
|
|
126 this->have_frame=0;
|
|
127
|
|
128 mad_synth_frame (&this->synth, &this->frame);
|
|
129
|
|
130 { unsigned int nchannels, nsamples;
|
|
131 mad_fixed_t const *left_ch, *right_ch;
|
|
132 struct mad_pcm *pcm = &this->synth.pcm;
|
|
133 uint16_t *output = (uint16_t*) buf;
|
|
134
|
|
135 nchannels = pcm->channels;
|
|
136 nsamples = pcm->length;
|
|
137 left_ch = pcm->samples[0];
|
|
138 right_ch = pcm->samples[1];
|
|
139
|
|
140 len+=2*nchannels*nsamples;
|
|
141 buf+=2*nchannels*nsamples;
|
|
142
|
|
143 while (nsamples--) {
|
|
144 /* output sample(s) in 16-bit signed little-endian PCM */
|
|
145
|
|
146 *output++ = scale(*left_ch++);
|
|
147
|
|
148 if (nchannels == 2)
|
|
149 *output++ = scale(*right_ch++);
|
|
150
|
|
151 }
|
|
152 }
|
|
153 }
|
|
154
|
|
155 return len?len:-1;
|
|
156 }
|
|
157
|
|
158 static int control(sh_audio_t *sh,int cmd,void* arg, ...){
|
|
159 mad_decoder_t *this = (mad_decoder_t *) sh->context;
|
|
160 // various optional functions you MAY implement:
|
|
161 switch(cmd){
|
|
162 case ADCTRL_RESYNC_STREAM:
|
|
163 this->have_frame=0;
|
|
164 mad_synth_init (&this->synth);
|
|
165 mad_stream_init (&this->stream);
|
|
166 mad_frame_init (&this->frame);
|
|
167 return CONTROL_TRUE;
|
|
168 case ADCTRL_SKIP_FRAME:
|
|
169 this->have_frame=read_frame(sh);
|
|
170 return CONTROL_TRUE;
|
|
171 }
|
|
172 return CONTROL_UNKNOWN;
|
|
173 }
|
|
174 #endif
|