5342
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
|
3 #include <unistd.h>
|
|
4
|
|
5 #include "config.h"
|
|
6 #include "mp_msg.h"
|
|
7 #include "help_mp.h"
|
|
8
|
|
9 extern int verbose; // defined in mplayer.c
|
|
10
|
|
11 #include "stream.h"
|
|
12 #include "demuxer.h"
|
|
13
|
|
14 #include "codec-cfg.h"
|
|
15 #include "stheader.h"
|
|
16
|
|
17 #include "dec_audio.h"
|
|
18 #include "ad.h"
|
|
19 #include "../libao2/afmt.h"
|
|
20
|
|
21 #ifdef USE_FAKE_MONO
|
|
22 int fakemono=0;
|
|
23 #endif
|
|
24 /* used for ac3surround decoder - set using -channels option */
|
|
25 int audio_output_channels = 2;
|
|
26
|
|
27 static ad_functions_t* mpadec;
|
|
28
|
|
29 int init_audio(sh_audio_t *sh_audio)
|
|
30 {
|
|
31 unsigned i;
|
|
32 for (i=0; mpcodecs_ad_drivers[i] != NULL; i++)
|
|
33 if(mpcodecs_ad_drivers[i]->info->id==sh_audio->codec->driver){
|
|
34 mpadec=mpcodecs_ad_drivers[i]; break;
|
|
35 }
|
|
36 if(!mpadec){
|
|
37 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"Requested audio codec family [%s] (vfm=%d) not available (enable it at compile time!)\n",
|
|
38 sh_audio->codec->name, sh_audio->codec->driver);
|
|
39 return 0; // no such driver
|
|
40 }
|
|
41
|
5933
|
42 mp_msg(MSGT_DECAUDIO,MSGL_INFO,"Selecting Audio Decoder: [%s] %s\n",mpadec->info->short_name,mpadec->info->name);
|
5458
|
43
|
|
44 // reset in/out buffer size/pointer:
|
|
45 sh_audio->a_buffer_size=0;
|
|
46 sh_audio->a_buffer=NULL;
|
|
47 sh_audio->a_in_buffer_size=0;
|
|
48 sh_audio->a_in_buffer=NULL;
|
|
49
|
|
50 // Set up some common usefull defaults. ad->preinit() can override these:
|
5342
|
51
|
|
52 sh_audio->samplesize=2;
|
|
53 #ifdef WORDS_BIGENDIAN
|
|
54 sh_audio->sample_format=AFMT_S16_BE;
|
|
55 #else
|
|
56 sh_audio->sample_format=AFMT_S16_LE;
|
|
57 #endif
|
|
58 sh_audio->samplerate=0;
|
5458
|
59 sh_audio->i_bps=0; // input rate (bytes/sec)
|
|
60 sh_audio->o_bps=0; // output rate (bytes/sec)
|
5342
|
61
|
|
62 sh_audio->audio_out_minsize=8192;/* default size, maybe not enough for Win32/ACM*/
|
5458
|
63 sh_audio->audio_in_minsize=0;
|
5342
|
64
|
|
65 if(!mpadec->preinit(sh_audio))
|
|
66 {
|
5933
|
67 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"ADecoder preinit failed :(\n");
|
5342
|
68 return 0;
|
|
69 }
|
|
70
|
5458
|
71 /* allocate audio in buffer: */
|
|
72 if(sh_audio->audio_in_minsize>0){
|
|
73 sh_audio->a_in_buffer_size=sh_audio->audio_in_minsize;
|
|
74 mp_msg(MSGT_DECAUDIO,MSGL_V,"dec_audio: Allocating %d bytes for input buffer\n",
|
|
75 sh_audio->a_in_buffer_size);
|
|
76 sh_audio->a_in_buffer=malloc(sh_audio->a_in_buffer_size);
|
|
77 memset(sh_audio->a_in_buffer,0,sh_audio->a_in_buffer_size);
|
|
78 sh_audio->a_in_buffer_len=0;
|
|
79 }
|
|
80
|
5342
|
81 /* allocate audio out buffer: */
|
|
82 sh_audio->a_buffer_size=sh_audio->audio_out_minsize+MAX_OUTBURST; /* worst case calc.*/
|
|
83
|
|
84 mp_msg(MSGT_DECAUDIO,MSGL_V,"dec_audio: Allocating %d + %d = %d bytes for output buffer\n",
|
|
85 sh_audio->audio_out_minsize,MAX_OUTBURST,sh_audio->a_buffer_size);
|
|
86
|
|
87 sh_audio->a_buffer=malloc(sh_audio->a_buffer_size);
|
|
88 if(!sh_audio->a_buffer){
|
|
89 mp_msg(MSGT_DECAUDIO,MSGL_ERR,MSGTR_CantAllocAudioBuf);
|
|
90 return 0;
|
|
91 }
|
|
92 memset(sh_audio->a_buffer,0,sh_audio->a_buffer_size);
|
|
93 sh_audio->a_buffer_len=0;
|
|
94
|
|
95 if(!mpadec->init(sh_audio)){
|
5458
|
96 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"ADecoder init failed :(\n");
|
|
97 uninit_audio(sh_audio); // free buffers
|
5342
|
98 return 0;
|
|
99 }
|
5458
|
100
|
5342
|
101 sh_audio->inited=1;
|
|
102
|
|
103 if(!sh_audio->channels || !sh_audio->samplerate){
|
|
104 mp_msg(MSGT_DECAUDIO,MSGL_WARN,MSGTR_UnknownAudio);
|
5458
|
105 uninit_audio(sh_audio); // free buffers
|
5342
|
106 return 0;
|
|
107 }
|
|
108
|
|
109 if(!sh_audio->o_bps)
|
|
110 sh_audio->o_bps=sh_audio->channels*sh_audio->samplerate*sh_audio->samplesize;
|
5458
|
111
|
|
112 return 1;
|
5342
|
113 }
|
|
114
|
|
115 void uninit_audio(sh_audio_t *sh_audio)
|
|
116 {
|
5458
|
117 if(sh_audio->a_buffer) free(sh_audio->a_buffer);
|
|
118 sh_audio->a_buffer=NULL;
|
|
119 if(sh_audio->a_in_buffer) free(sh_audio->a_in_buffer);
|
|
120 sh_audio->a_in_buffer=NULL;
|
5342
|
121 if(!sh_audio->inited) return;
|
|
122 mp_msg(MSGT_DECAUDIO,MSGL_V,"uninit audio: %d \n",sh_audio->codec->driver);
|
|
123 mpadec->uninit(sh_audio);
|
|
124 sh_audio->inited=0;
|
|
125 }
|
|
126
|
|
127 int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
|
|
128 {
|
|
129 if(sh_audio->inited)
|
|
130 return mpadec->decode_audio(sh_audio,buf,minlen,maxlen);
|
|
131 else
|
|
132 return -1;
|
|
133 }
|
|
134
|
|
135 void resync_audio_stream(sh_audio_t *sh_audio)
|
|
136 {
|
|
137 if(!sh_audio->inited) return;
|
|
138 if(mpadec->control(sh_audio,ADCTRL_RESYNC_STREAM,NULL)==CONTROL_TRUE) return;
|
|
139 // default resync code:
|
|
140 sh_audio->a_in_buffer_len=0; // clear audio input buffer
|
|
141 }
|
|
142
|
|
143 void skip_audio_frame(sh_audio_t *sh_audio)
|
|
144 {
|
|
145 if(!sh_audio->inited) return;
|
|
146 if(mpadec->control(sh_audio,ADCTRL_SKIP_FRAME,NULL)==CONTROL_TRUE) return;
|
|
147 // default skip code:
|
|
148 ds_fill_buffer(sh_audio->ds); // skip block
|
|
149 }
|