7 #include "ad_internal.h"
8 #include "libaf/af_format.h"
9 #include "libaf/reorder_ch.h"
11 static const ad_info_t info
=
13 "Uncompressed PCM audio decoder",
20 struct ad_pcm_context
{
21 unsigned char *packet_ptr
;
27 static int init(sh_audio_t
*sh_audio
)
29 WAVEFORMATEX
*h
=sh_audio
->wf
;
32 sh_audio
->i_bps
=h
->nAvgBytesPerSec
;
33 sh_audio
->channels
=h
->nChannels
;
34 sh_audio
->samplerate
=h
->nSamplesPerSec
;
35 sh_audio
->samplesize
=(h
->wBitsPerSample
+7)/8;
36 sh_audio
->sample_format
=AF_FORMAT_S16_LE
; // default
37 switch(sh_audio
->format
){ /* hardware formats: */
39 case 0x1: // Microsoft PCM
40 case 0xfffe: // Extended
41 switch (sh_audio
->samplesize
) {
42 case 1: sh_audio
->sample_format
=AF_FORMAT_U8
; break;
43 case 2: sh_audio
->sample_format
=AF_FORMAT_S16_LE
; break;
44 case 3: sh_audio
->sample_format
=AF_FORMAT_S24_LE
; break;
45 case 4: sh_audio
->sample_format
=AF_FORMAT_S32_LE
; break;
48 case 0x3: // IEEE float
49 sh_audio
->sample_format
=AF_FORMAT_FLOAT_LE
;
51 case 0x6: sh_audio
->sample_format
=AF_FORMAT_A_LAW
;break;
52 case 0x7: sh_audio
->sample_format
=AF_FORMAT_MU_LAW
;break;
53 case 0x11: sh_audio
->sample_format
=AF_FORMAT_IMA_ADPCM
;break;
54 case 0x50: sh_audio
->sample_format
=AF_FORMAT_MPEG2
;break;
55 /* case 0x2000: sh_audio->sample_format=AFMT_AC3; */
56 case 0x20776172: // 'raw '
57 sh_audio
->sample_format
=AF_FORMAT_S16_BE
;
58 if(sh_audio
->samplesize
==1) sh_audio
->sample_format
=AF_FORMAT_U8
;
60 case 0x736F7774: // 'twos'
61 sh_audio
->sample_format
=AF_FORMAT_S16_BE
;
62 // intended fall-through
63 case 0x74776F73: // 'sowt'
64 if(sh_audio
->samplesize
==1) sh_audio
->sample_format
=AF_FORMAT_S8
;
66 case 0x32336c66: // 'fl32', bigendian float32
67 sh_audio
->sample_format
=AF_FORMAT_FLOAT_BE
;
68 sh_audio
->samplesize
=4;
70 case 0x666c3332: // '23lf', little endian float32, MPlayer internal fourCC
71 sh_audio
->sample_format
=AF_FORMAT_FLOAT_LE
;
72 sh_audio
->samplesize
=4;
74 /* case 0x34366c66: // 'fl64', bigendian float64
75 sh_audio->sample_format=AF_FORMAT_FLOAT_BE;
76 sh_audio->samplesize=8;
78 case 0x666c3634: // '46lf', little endian float64, MPlayer internal fourCC
79 sh_audio->sample_format=AF_FORMAT_FLOAT_LE;
80 sh_audio->samplesize=8;
82 case 0x34326e69: // 'in24', bigendian int24
83 sh_audio
->sample_format
=AF_FORMAT_S24_BE
;
84 sh_audio
->samplesize
=3;
86 case 0x696e3234: // '42ni', little endian int24, MPlayer internal fourCC
87 sh_audio
->sample_format
=AF_FORMAT_S24_LE
;
88 sh_audio
->samplesize
=3;
90 case 0x32336e69: // 'in32', bigendian int32
91 sh_audio
->sample_format
=AF_FORMAT_S32_BE
;
92 sh_audio
->samplesize
=4;
94 case 0x696e3332: // '23ni', little endian int32, MPlayer internal fourCC
95 sh_audio
->sample_format
=AF_FORMAT_S32_LE
;
96 sh_audio
->samplesize
=4;
98 default: if(sh_audio
->samplesize
!=2) sh_audio
->sample_format
=AF_FORMAT_U8
;
100 if (!sh_audio
->samplesize
) // this would cause MPlayer to hang later
101 sh_audio
->samplesize
= 2;
102 sh_audio
->context
= talloc_zero(NULL
, struct ad_pcm_context
);
106 static int preinit(sh_audio_t
*sh
)
108 sh
->audio_out_minsize
=2048;
112 static void uninit(sh_audio_t
*sh
)
114 talloc_free(sh
->context
);
117 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...)
122 case ADCTRL_SKIP_FRAME
:
125 demux_read_data(sh
->ds
,NULL
,skip
);
128 return CONTROL_UNKNOWN
;
131 static int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
133 unsigned len
= sh_audio
->channels
*sh_audio
->samplesize
;
134 minlen
= (minlen
+ len
- 1) / len
* len
;
136 // if someone needs hundreds of channels adjust audio_out_minsize
137 // based on channels in preinit()
141 struct ad_pcm_context
*ctx
= sh_audio
->context
;
142 while (len
< minlen
) {
143 if (ctx
->packet_len
== 0) {
145 int plen
= ds_get_packet_pts(sh_audio
->ds
, &ctx
->packet_ptr
, &pts
);
148 ctx
->packet_len
= plen
;
149 if (pts
!= MP_NOPTS_VALUE
) {
151 sh_audio
->pts_bytes
= 0;
154 int from_stored
= ctx
->packet_len
;
155 if (from_stored
> minlen
- len
)
156 from_stored
= minlen
- len
;
157 memcpy(buf
+ len
, ctx
->packet_ptr
, from_stored
);
158 ctx
->packet_len
-= from_stored
;
159 ctx
->packet_ptr
+= from_stored
;
160 sh_audio
->pts_bytes
+= from_stored
;
164 len
= -1; // The loop above only exits at error/EOF
165 if (len
> 0 && sh_audio
->channels
>= 5) {
166 reorder_channel_nch(buf
, AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT
,
167 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
169 len
/ sh_audio
->samplesize
, sh_audio
->samplesize
);