Remove the internal GUI
[mplayer/glamo.git] / libmpcodecs / ad_pcm.c
blob058f6c25cbd91ba795d2698c794bec901f7ec0a8
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 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: */
36 case 0x0:
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;
45 break;
46 case 0x3: // IEEE float
47 sh_audio->sample_format=AF_FORMAT_FLOAT_LE;
48 break;
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;
57 break;
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;
63 break;
64 case 0x32336c66: // 'fl32', bigendian float32
65 sh_audio->sample_format=AF_FORMAT_FLOAT_BE;
66 sh_audio->samplesize=4;
67 break;
68 case 0x666c3332: // '23lf', little endian float32, MPlayer internal fourCC
69 sh_audio->sample_format=AF_FORMAT_FLOAT_LE;
70 sh_audio->samplesize=4;
71 break;
72 /* case 0x34366c66: // 'fl64', bigendian float64
73 sh_audio->sample_format=AF_FORMAT_FLOAT_BE;
74 sh_audio->samplesize=8;
75 break;
76 case 0x666c3634: // '46lf', little endian float64, MPlayer internal fourCC
77 sh_audio->sample_format=AF_FORMAT_FLOAT_LE;
78 sh_audio->samplesize=8;
79 break;*/
80 case 0x34326e69: // 'in24', bigendian int24
81 sh_audio->sample_format=AF_FORMAT_S24_BE;
82 sh_audio->samplesize=3;
83 break;
84 case 0x696e3234: // '42ni', little endian int24, MPlayer internal fourCC
85 sh_audio->sample_format=AF_FORMAT_S24_LE;
86 sh_audio->samplesize=3;
87 break;
88 case 0x32336e69: // 'in32', bigendian int32
89 sh_audio->sample_format=AF_FORMAT_S32_BE;
90 sh_audio->samplesize=4;
91 break;
92 case 0x696e3332: // '23ni', little endian int32, MPlayer internal fourCC
93 sh_audio->sample_format=AF_FORMAT_S32_LE;
94 sh_audio->samplesize=4;
95 break;
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);
101 return 1;
104 static int preinit(sh_audio_t *sh)
106 sh->audio_out_minsize=2048;
107 return 1;
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, ...)
117 int skip;
118 switch(cmd)
120 case ADCTRL_SKIP_FRAME:
121 skip=sh->i_bps/16;
122 skip=skip&(~3);
123 demux_read_data(sh->ds,NULL,skip);
124 return CONTROL_TRUE;
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;
133 if (minlen > maxlen)
134 // if someone needs hundreds of channels adjust audio_out_minsize
135 // based on channels in preinit()
136 return -1;
138 len = 0;
139 struct ad_pcm_context *ctx = sh_audio->context;
140 while (len < minlen) {
141 if (ctx->packet_len == 0) {
142 double pts;
143 int plen = ds_get_packet_pts(sh_audio->ds, &ctx->packet_ptr, &pts);
144 if (plen < 0)
145 break;
146 ctx->packet_len = plen;
147 if (pts != MP_NOPTS_VALUE) {
148 sh_audio->pts = pts;
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;
159 len += from_stored;
161 if (len == 0)
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,
166 sh_audio->channels,
167 len / sh_audio->samplesize, sh_audio->samplesize);
169 return len;