Merge svn changes up to r30463
[mplayer/kovensky.git] / libmpcodecs / ad_pcm.c
bloba5a17e1a49ce1e68acca932d5a26fb6e3fa9d6f4
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 "talloc.h"
24 #include "config.h"
25 #include "ad_internal.h"
26 #include "libaf/af_format.h"
27 #include "libaf/reorder_ch.h"
29 static const ad_info_t info =
31 "Uncompressed PCM audio decoder",
32 "pcm",
33 "Nick Kurshev",
34 "A'rpi",
38 struct ad_pcm_context {
39 unsigned char *packet_ptr;
40 int packet_len;
43 LIBAD_EXTERN(pcm)
45 static int init(sh_audio_t *sh_audio)
47 WAVEFORMATEX *h=sh_audio->wf;
48 if (!h)
49 return 0;
50 sh_audio->i_bps=h->nAvgBytesPerSec;
51 sh_audio->channels=h->nChannels;
52 sh_audio->samplerate=h->nSamplesPerSec;
53 sh_audio->samplesize=(h->wBitsPerSample+7)/8;
54 sh_audio->sample_format=AF_FORMAT_S16_LE; // default
55 switch(sh_audio->format){ /* hardware formats: */
56 case 0x0:
57 case 0x1: // Microsoft PCM
58 case 0xfffe: // Extended
59 switch (sh_audio->samplesize) {
60 case 1: sh_audio->sample_format=AF_FORMAT_U8; break;
61 case 2: sh_audio->sample_format=AF_FORMAT_S16_LE; break;
62 case 3: sh_audio->sample_format=AF_FORMAT_S24_LE; break;
63 case 4: sh_audio->sample_format=AF_FORMAT_S32_LE; break;
65 break;
66 case 0x3: // IEEE float
67 sh_audio->sample_format=AF_FORMAT_FLOAT_LE;
68 break;
69 case 0x6: sh_audio->sample_format=AF_FORMAT_A_LAW;break;
70 case 0x7: sh_audio->sample_format=AF_FORMAT_MU_LAW;break;
71 case 0x11: sh_audio->sample_format=AF_FORMAT_IMA_ADPCM;break;
72 case 0x50: sh_audio->sample_format=AF_FORMAT_MPEG2;break;
73 /* case 0x2000: sh_audio->sample_format=AFMT_AC3; */
74 case 0x20776172: // 'raw '
75 sh_audio->sample_format=AF_FORMAT_S16_BE;
76 if(sh_audio->samplesize==1) sh_audio->sample_format=AF_FORMAT_U8;
77 break;
78 case 0x736F7774: // 'twos'
79 sh_audio->sample_format=AF_FORMAT_S16_BE;
80 // intended fall-through
81 case 0x74776F73: // 'sowt'
82 if(sh_audio->samplesize==1) sh_audio->sample_format=AF_FORMAT_S8;
83 break;
84 case 0x32336c66: // 'fl32', bigendian float32
85 sh_audio->sample_format=AF_FORMAT_FLOAT_BE;
86 sh_audio->samplesize=4;
87 break;
88 case 0x666c3332: // '23lf', little endian float32, MPlayer internal fourCC
89 sh_audio->sample_format=AF_FORMAT_FLOAT_LE;
90 sh_audio->samplesize=4;
91 break;
92 /* case 0x34366c66: // 'fl64', bigendian float64
93 sh_audio->sample_format=AF_FORMAT_FLOAT_BE;
94 sh_audio->samplesize=8;
95 break;
96 case 0x666c3634: // '46lf', little endian float64, MPlayer internal fourCC
97 sh_audio->sample_format=AF_FORMAT_FLOAT_LE;
98 sh_audio->samplesize=8;
99 break;*/
100 case 0x34326e69: // 'in24', bigendian int24
101 sh_audio->sample_format=AF_FORMAT_S24_BE;
102 sh_audio->samplesize=3;
103 break;
104 case 0x696e3234: // '42ni', little endian int24, MPlayer internal fourCC
105 sh_audio->sample_format=AF_FORMAT_S24_LE;
106 sh_audio->samplesize=3;
107 break;
108 case 0x32336e69: // 'in32', bigendian int32
109 sh_audio->sample_format=AF_FORMAT_S32_BE;
110 sh_audio->samplesize=4;
111 break;
112 case 0x696e3332: // '23ni', little endian int32, MPlayer internal fourCC
113 sh_audio->sample_format=AF_FORMAT_S32_LE;
114 sh_audio->samplesize=4;
115 break;
116 default: if(sh_audio->samplesize!=2) sh_audio->sample_format=AF_FORMAT_U8;
118 if (!sh_audio->samplesize) // this would cause MPlayer to hang later
119 sh_audio->samplesize = 2;
120 sh_audio->context = talloc_zero(NULL, struct ad_pcm_context);
121 return 1;
124 static int preinit(sh_audio_t *sh)
126 sh->audio_out_minsize=2048;
127 return 1;
130 static void uninit(sh_audio_t *sh)
132 talloc_free(sh->context);
135 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
137 int skip;
138 switch(cmd)
140 case ADCTRL_SKIP_FRAME:
141 skip=sh->i_bps/16;
142 skip=skip&(~3);
143 demux_read_data(sh->ds,NULL,skip);
144 return CONTROL_TRUE;
146 return CONTROL_UNKNOWN;
149 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
151 unsigned len = sh_audio->channels*sh_audio->samplesize;
152 minlen = (minlen + len - 1) / len * len;
153 if (minlen > maxlen)
154 // if someone needs hundreds of channels adjust audio_out_minsize
155 // based on channels in preinit()
156 return -1;
158 len = 0;
159 struct ad_pcm_context *ctx = sh_audio->context;
160 while (len < minlen) {
161 if (ctx->packet_len == 0) {
162 double pts;
163 int plen = ds_get_packet_pts(sh_audio->ds, &ctx->packet_ptr, &pts);
164 if (plen < 0)
165 break;
166 ctx->packet_len = plen;
167 if (pts != MP_NOPTS_VALUE) {
168 sh_audio->pts = pts;
169 sh_audio->pts_bytes = 0;
172 int from_stored = ctx->packet_len;
173 if (from_stored > minlen - len)
174 from_stored = minlen - len;
175 memcpy(buf + len, ctx->packet_ptr, from_stored);
176 ctx->packet_len -= from_stored;
177 ctx->packet_ptr += from_stored;
178 sh_audio->pts_bytes += from_stored;
179 len += from_stored;
181 if (len == 0)
182 len = -1; // The loop above only exits at error/EOF
183 if (len > 0 && sh_audio->channels >= 5) {
184 reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT,
185 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
186 sh_audio->channels,
187 len / sh_audio->samplesize, sh_audio->samplesize);
189 return len;