sync with en/mplayer.1 rev. 30611
[mplayer/glamo.git] / libmpcodecs / ad_pcm.c
blob6e43cec514be26dffa69b8e86ac19a850407165c
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
23 #include "config.h"
24 #include "ad_internal.h"
25 #include "libaf/af_format.h"
26 #include "libaf/reorder_ch.h"
28 static const ad_info_t info =
30 "Uncompressed PCM audio decoder",
31 "pcm",
32 "Nick Kurshev",
33 "A'rpi",
37 LIBAD_EXTERN(pcm)
39 static int init(sh_audio_t *sh_audio)
41 WAVEFORMATEX *h=sh_audio->wf;
42 if (!h)
43 return 0;
44 sh_audio->i_bps=h->nAvgBytesPerSec;
45 sh_audio->channels=h->nChannels;
46 sh_audio->samplerate=h->nSamplesPerSec;
47 sh_audio->samplesize=(h->wBitsPerSample+7)/8;
48 sh_audio->sample_format=AF_FORMAT_S16_LE; // default
49 switch(sh_audio->format){ /* hardware formats: */
50 case 0x0:
51 case 0x1: // Microsoft PCM
52 case 0xfffe: // Extended
53 switch (sh_audio->samplesize) {
54 case 1: sh_audio->sample_format=AF_FORMAT_U8; break;
55 case 2: sh_audio->sample_format=AF_FORMAT_S16_LE; break;
56 case 3: sh_audio->sample_format=AF_FORMAT_S24_LE; break;
57 case 4: sh_audio->sample_format=AF_FORMAT_S32_LE; break;
59 break;
60 case 0x3: // IEEE float
61 sh_audio->sample_format=AF_FORMAT_FLOAT_LE;
62 break;
63 case 0x6: sh_audio->sample_format=AF_FORMAT_A_LAW;break;
64 case 0x7: sh_audio->sample_format=AF_FORMAT_MU_LAW;break;
65 case 0x11: sh_audio->sample_format=AF_FORMAT_IMA_ADPCM;break;
66 case 0x50: sh_audio->sample_format=AF_FORMAT_MPEG2;break;
67 /* case 0x2000: sh_audio->sample_format=AFMT_AC3; */
68 case 0x20776172: // 'raw '
69 sh_audio->sample_format=AF_FORMAT_S16_BE;
70 if(sh_audio->samplesize==1) sh_audio->sample_format=AF_FORMAT_U8;
71 break;
72 case 0x736F7774: // 'twos'
73 sh_audio->sample_format=AF_FORMAT_S16_BE;
74 // intended fall-through
75 case 0x74776F73: // 'sowt'
76 if(sh_audio->samplesize==1) sh_audio->sample_format=AF_FORMAT_S8;
77 break;
78 case 0x32336c66: // 'fl32', bigendian float32
79 sh_audio->sample_format=AF_FORMAT_FLOAT_BE;
80 sh_audio->samplesize=4;
81 break;
82 case 0x666c3332: // '23lf', little endian float32, MPlayer internal fourCC
83 sh_audio->sample_format=AF_FORMAT_FLOAT_LE;
84 sh_audio->samplesize=4;
85 break;
86 /* case 0x34366c66: // 'fl64', bigendian float64
87 sh_audio->sample_format=AF_FORMAT_FLOAT_BE;
88 sh_audio->samplesize=8;
89 break;
90 case 0x666c3634: // '46lf', little endian float64, MPlayer internal fourCC
91 sh_audio->sample_format=AF_FORMAT_FLOAT_LE;
92 sh_audio->samplesize=8;
93 break;*/
94 case 0x34326e69: // 'in24', bigendian int24
95 sh_audio->sample_format=AF_FORMAT_S24_BE;
96 sh_audio->samplesize=3;
97 break;
98 case 0x696e3234: // '42ni', little endian int24, MPlayer internal fourCC
99 sh_audio->sample_format=AF_FORMAT_S24_LE;
100 sh_audio->samplesize=3;
101 break;
102 case 0x32336e69: // 'in32', bigendian int32
103 sh_audio->sample_format=AF_FORMAT_S32_BE;
104 sh_audio->samplesize=4;
105 break;
106 case 0x696e3332: // '23ni', little endian int32, MPlayer internal fourCC
107 sh_audio->sample_format=AF_FORMAT_S32_LE;
108 sh_audio->samplesize=4;
109 break;
110 default: if(sh_audio->samplesize!=2) sh_audio->sample_format=AF_FORMAT_U8;
112 if (!sh_audio->samplesize) // this would cause MPlayer to hang later
113 sh_audio->samplesize = 2;
114 return 1;
117 static int preinit(sh_audio_t *sh)
119 sh->audio_out_minsize=2048;
120 return 1;
123 static void uninit(sh_audio_t *sh)
127 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
129 int skip;
130 switch(cmd)
132 case ADCTRL_SKIP_FRAME:
133 skip=sh->i_bps/16;
134 skip=skip&(~3);
135 demux_read_data(sh->ds,NULL,skip);
136 return CONTROL_TRUE;
138 return CONTROL_UNKNOWN;
141 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
143 unsigned len = sh_audio->channels*sh_audio->samplesize;
144 len = (minlen + len - 1) / len * len;
145 if (len > maxlen)
146 // if someone needs hundreds of channels adjust audio_out_minsize
147 // based on channels in preinit()
148 return -1;
149 len=demux_read_data(sh_audio->ds,buf,len);
150 if (len > 0 && sh_audio->channels >= 5) {
151 reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT,
152 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
153 sh_audio->channels,
154 len / sh_audio->samplesize, sh_audio->samplesize);
156 return len;