Remove HAVE_MATRIXVIEW references
[mplayer/kovensky.git] / libmpcodecs / ad_pcm.c
blobcc0de742d8dd96751b6f9f0f8316ee3b12cd50d6
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
5 #include "talloc.h"
6 #include "config.h"
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",
14 "pcm",
15 "Nick Kurshev",
16 "A'rpi",
20 struct ad_pcm_context {
21 unsigned char *packet_ptr;
22 int packet_len;
25 LIBAD_EXTERN(pcm)
27 static int init(sh_audio_t *sh_audio)
29 WAVEFORMATEX *h=sh_audio->wf;
30 if (!h)
31 return 0;
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: */
38 case 0x0:
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;
47 break;
48 case 0x3: // IEEE float
49 sh_audio->sample_format=AF_FORMAT_FLOAT_LE;
50 break;
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;
59 break;
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;
65 break;
66 case 0x32336c66: // 'fl32', bigendian float32
67 sh_audio->sample_format=AF_FORMAT_FLOAT_BE;
68 sh_audio->samplesize=4;
69 break;
70 case 0x666c3332: // '23lf', little endian float32, MPlayer internal fourCC
71 sh_audio->sample_format=AF_FORMAT_FLOAT_LE;
72 sh_audio->samplesize=4;
73 break;
74 /* case 0x34366c66: // 'fl64', bigendian float64
75 sh_audio->sample_format=AF_FORMAT_FLOAT_BE;
76 sh_audio->samplesize=8;
77 break;
78 case 0x666c3634: // '46lf', little endian float64, MPlayer internal fourCC
79 sh_audio->sample_format=AF_FORMAT_FLOAT_LE;
80 sh_audio->samplesize=8;
81 break;*/
82 case 0x34326e69: // 'in24', bigendian int24
83 sh_audio->sample_format=AF_FORMAT_S24_BE;
84 sh_audio->samplesize=3;
85 break;
86 case 0x696e3234: // '42ni', little endian int24, MPlayer internal fourCC
87 sh_audio->sample_format=AF_FORMAT_S24_LE;
88 sh_audio->samplesize=3;
89 break;
90 case 0x32336e69: // 'in32', bigendian int32
91 sh_audio->sample_format=AF_FORMAT_S32_BE;
92 sh_audio->samplesize=4;
93 break;
94 case 0x696e3332: // '23ni', little endian int32, MPlayer internal fourCC
95 sh_audio->sample_format=AF_FORMAT_S32_LE;
96 sh_audio->samplesize=4;
97 break;
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);
103 return 1;
106 static int preinit(sh_audio_t *sh)
108 sh->audio_out_minsize=2048;
109 return 1;
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, ...)
119 int skip;
120 switch(cmd)
122 case ADCTRL_SKIP_FRAME:
123 skip=sh->i_bps/16;
124 skip=skip&(~3);
125 demux_read_data(sh->ds,NULL,skip);
126 return CONTROL_TRUE;
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;
135 if (minlen > maxlen)
136 // if someone needs hundreds of channels adjust audio_out_minsize
137 // based on channels in preinit()
138 return -1;
140 len = 0;
141 struct ad_pcm_context *ctx = sh_audio->context;
142 while (len < minlen) {
143 if (ctx->packet_len == 0) {
144 double pts;
145 int plen = ds_get_packet_pts(sh_audio->ds, &ctx->packet_ptr, &pts);
146 if (plen < 0)
147 break;
148 ctx->packet_len = plen;
149 if (pts != MP_NOPTS_VALUE) {
150 sh_audio->pts = pts;
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;
161 len += from_stored;
163 if (len == 0)
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,
168 sh_audio->channels,
169 len / sh_audio->samplesize, sh_audio->samplesize);
171 return len;