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
;
30 sh_audio
->i_bps
=h
->nAvgBytesPerSec
;
31 sh_audio
->channels
=h
->nChannels
;
32 sh_audio
->samplerate
=h
->nSamplesPerSec
;
33 sh_audio
->samplesize
=(h
->wBitsPerSample
+7)/8;
34 sh_audio
->sample_format
=AF_FORMAT_S16_LE
; // default
35 switch(sh_audio
->format
){ /* hardware formats: */
37 case 0x1: // Microsoft PCM
38 case 0xfffe: // Extended
39 switch (sh_audio
->samplesize
) {
40 case 1: sh_audio
->sample_format
=AF_FORMAT_U8
; break;
41 case 2: sh_audio
->sample_format
=AF_FORMAT_S16_LE
; break;
42 case 3: sh_audio
->sample_format
=AF_FORMAT_S24_LE
; break;
43 case 4: sh_audio
->sample_format
=AF_FORMAT_S32_LE
; break;
46 case 0x3: // IEEE float
47 sh_audio
->sample_format
=AF_FORMAT_FLOAT_LE
;
49 case 0x6: sh_audio
->sample_format
=AF_FORMAT_A_LAW
;break;
50 case 0x7: sh_audio
->sample_format
=AF_FORMAT_MU_LAW
;break;
51 case 0x11: sh_audio
->sample_format
=AF_FORMAT_IMA_ADPCM
;break;
52 case 0x50: sh_audio
->sample_format
=AF_FORMAT_MPEG2
;break;
53 /* case 0x2000: sh_audio->sample_format=AFMT_AC3; */
54 case 0x20776172: // 'raw '
55 sh_audio
->sample_format
=AF_FORMAT_S16_BE
;
56 if(sh_audio
->samplesize
==1) sh_audio
->sample_format
=AF_FORMAT_U8
;
58 case 0x736F7774: // 'twos'
59 sh_audio
->sample_format
=AF_FORMAT_S16_BE
;
60 // intended fall-through
61 case 0x74776F73: // 'sowt'
62 if(sh_audio
->samplesize
==1) sh_audio
->sample_format
=AF_FORMAT_S8
;
64 case 0x32336c66: // 'fl32', bigendian float32
65 sh_audio
->sample_format
=AF_FORMAT_FLOAT_BE
;
66 sh_audio
->samplesize
=4;
68 case 0x666c3332: // '23lf', little endian float32, MPlayer internal fourCC
69 sh_audio
->sample_format
=AF_FORMAT_FLOAT_LE
;
70 sh_audio
->samplesize
=4;
72 /* case 0x34366c66: // 'fl64', bigendian float64
73 sh_audio->sample_format=AF_FORMAT_FLOAT_BE;
74 sh_audio->samplesize=8;
76 case 0x666c3634: // '46lf', little endian float64, MPlayer internal fourCC
77 sh_audio->sample_format=AF_FORMAT_FLOAT_LE;
78 sh_audio->samplesize=8;
80 case 0x34326e69: // 'in24', bigendian int24
81 sh_audio
->sample_format
=AF_FORMAT_S24_BE
;
82 sh_audio
->samplesize
=3;
84 case 0x696e3234: // '42ni', little endian int24, MPlayer internal fourCC
85 sh_audio
->sample_format
=AF_FORMAT_S24_LE
;
86 sh_audio
->samplesize
=3;
88 case 0x32336e69: // 'in32', bigendian int32
89 sh_audio
->sample_format
=AF_FORMAT_S32_BE
;
90 sh_audio
->samplesize
=4;
92 case 0x696e3332: // '23ni', little endian int32, MPlayer internal fourCC
93 sh_audio
->sample_format
=AF_FORMAT_S32_LE
;
94 sh_audio
->samplesize
=4;
96 default: if(sh_audio
->samplesize
!=2) sh_audio
->sample_format
=AF_FORMAT_U8
;
98 if (!sh_audio
->samplesize
) // this would cause MPlayer to hang later
99 sh_audio
->samplesize
= 2;
100 sh_audio
->context
= talloc_zero(NULL
, struct ad_pcm_context
);
104 static int preinit(sh_audio_t
*sh
)
106 sh
->audio_out_minsize
=2048;
110 static void uninit(sh_audio_t
*sh
)
112 talloc_free(sh
->context
);
115 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...)
120 case ADCTRL_SKIP_FRAME
:
123 demux_read_data(sh
->ds
,NULL
,skip
);
126 return CONTROL_UNKNOWN
;
129 static int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
131 unsigned len
= sh_audio
->channels
*sh_audio
->samplesize
;
132 minlen
= (minlen
+ len
- 1) / len
* len
;
134 // if someone needs hundreds of channels adjust audio_out_minsize
135 // based on channels in preinit()
139 struct ad_pcm_context
*ctx
= sh_audio
->context
;
140 while (len
< minlen
) {
141 if (ctx
->packet_len
== 0) {
143 int plen
= ds_get_packet_pts(sh_audio
->ds
, &ctx
->packet_ptr
, &pts
);
146 ctx
->packet_len
= plen
;
147 if (pts
!= MP_NOPTS_VALUE
) {
149 sh_audio
->pts_bytes
= 0;
152 int from_stored
= ctx
->packet_len
;
153 if (from_stored
> minlen
- len
)
154 from_stored
= minlen
- len
;
155 memcpy(buf
+ len
, ctx
->packet_ptr
, from_stored
);
156 ctx
->packet_len
-= from_stored
;
157 ctx
->packet_ptr
+= from_stored
;
158 sh_audio
->pts_bytes
+= from_stored
;
162 len
= -1; // The loop above only exits at error/EOF
163 if (len
> 0 && sh_audio
->channels
>= 5) {
164 reorder_channel_nch(buf
, AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT
,
165 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
167 len
/ sh_audio
->samplesize
, sh_audio
->samplesize
);