gl_common: minor cleanup/refactor
[mplayer.git] / libmpcodecs / ad_pcm.c
blob9ff4d6a90c3f90784c8c57bc55094c991c7f5357
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>
22 #include <stdbool.h>
24 #include "talloc.h"
25 #include "config.h"
26 #include "ad_internal.h"
27 #include "libaf/af_format.h"
28 #include "libaf/reorder_ch.h"
30 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 *buffer;
40 int buffer_pos;
41 int buffer_len;
42 int buffer_size;
45 LIBAD_EXTERN(pcm)
47 static int init(sh_audio_t * sh_audio)
49 WAVEFORMATEX *h = sh_audio->wf;
50 if (!h)
51 return 0;
52 sh_audio->i_bps = h->nAvgBytesPerSec;
53 sh_audio->channels = h->nChannels;
54 sh_audio->samplerate = h->nSamplesPerSec;
55 sh_audio->samplesize = (h->wBitsPerSample + 7) / 8;
56 sh_audio->sample_format = AF_FORMAT_S16_LE; // default
57 switch (sh_audio->format) { /* hardware formats: */
58 case 0x0:
59 case 0x1: // Microsoft PCM
60 case 0xfffe: // Extended
61 switch (sh_audio->samplesize) {
62 case 1: sh_audio->sample_format = AF_FORMAT_U8; break;
63 case 2: sh_audio->sample_format = AF_FORMAT_S16_LE; break;
64 case 3: sh_audio->sample_format = AF_FORMAT_S24_LE; break;
65 case 4: sh_audio->sample_format = AF_FORMAT_S32_LE; break;
67 break;
68 case 0x3: // IEEE float
69 sh_audio->sample_format = AF_FORMAT_FLOAT_LE;
70 break;
71 case 0x6: sh_audio->sample_format = AF_FORMAT_A_LAW; break;
72 case 0x7: sh_audio->sample_format = AF_FORMAT_MU_LAW; break;
73 case 0x11: sh_audio->sample_format = AF_FORMAT_IMA_ADPCM; break;
74 case 0x50: sh_audio->sample_format = AF_FORMAT_MPEG2; break;
75 /* case 0x2000: sh_audio->sample_format=AFMT_AC3; */
76 case 0x20776172: // 'raw '
77 sh_audio->sample_format = AF_FORMAT_S16_BE;
78 if (sh_audio->samplesize == 1)
79 sh_audio->sample_format = AF_FORMAT_U8;
80 break;
81 case 0x736F7774: // 'twos'
82 sh_audio->sample_format = AF_FORMAT_S16_BE;
83 // intended fall-through
84 case 0x74776F73: // 'sowt'
85 if (sh_audio->samplesize == 1)
86 sh_audio->sample_format = AF_FORMAT_S8;
87 break;
88 case 0x32336c66: // 'fl32', bigendian float32
89 case 0x32334C46: // 'FL32', bigendian float32 in aiff
90 sh_audio->sample_format = AF_FORMAT_FLOAT_BE;
91 sh_audio->samplesize = 4;
92 break;
93 case 0x666c3332: // '23lf', little endian float32, MPlayer internal fourCC
94 case 0x6D63706C: // 'lpcm'
95 sh_audio->sample_format = AF_FORMAT_FLOAT_LE;
96 sh_audio->samplesize = 4;
97 break;
98 /* case 0x34366c66: // 'fl64', bigendian float64
99 sh_audio->sample_format=AF_FORMAT_FLOAT_BE;
100 sh_audio->samplesize=8;
101 break;
102 case 0x666c3634: // '46lf', little endian float64, MPlayer internal fourCC
103 sh_audio->sample_format=AF_FORMAT_FLOAT_LE;
104 sh_audio->samplesize=8;
105 break;*/
106 case 0x34326e69: // 'in24', bigendian int24
107 sh_audio->sample_format = AF_FORMAT_S24_BE;
108 sh_audio->samplesize = 3;
109 break;
110 case 0x696e3234: // '42ni', little endian int24, MPlayer internal fourCC
111 sh_audio->sample_format = AF_FORMAT_S24_LE;
112 sh_audio->samplesize = 3;
113 break;
114 case 0x32336e69: // 'in32', bigendian int32
115 sh_audio->sample_format = AF_FORMAT_S32_BE;
116 sh_audio->samplesize = 4;
117 break;
118 case 0x696e3332: // '23ni', little endian int32, MPlayer internal fourCC
119 sh_audio->sample_format = AF_FORMAT_S32_LE;
120 sh_audio->samplesize = 4;
121 break;
122 default:
123 if (sh_audio->samplesize != 2)
124 sh_audio->sample_format = AF_FORMAT_U8;
126 if (!sh_audio->samplesize) // this would cause MPlayer to hang later
127 sh_audio->samplesize = 2;
128 sh_audio->context = talloc_zero(NULL, struct ad_pcm_context);
129 return 1;
132 static int preinit(sh_audio_t *sh)
134 sh->audio_out_minsize = 2048;
135 return 1;
138 static void uninit(sh_audio_t *sh)
140 talloc_free(sh->context);
143 static int control(sh_audio_t *sh, int cmd, void *arg, ...)
145 struct ad_pcm_context *ctx = sh->context;
146 int skip;
147 switch (cmd) {
148 case ADCTRL_RESYNC_STREAM:
149 ctx->buffer_len = 0;
150 return true;
151 case ADCTRL_SKIP_FRAME:
152 skip = sh->i_bps / 16;
153 skip = skip & (~3);
154 demux_read_data(sh->ds, NULL, skip);
155 return CONTROL_TRUE;
157 return CONTROL_UNKNOWN;
160 static int decode_audio(sh_audio_t *sh_audio, unsigned char *buf, int minlen,
161 int maxlen)
163 int unitsize = sh_audio->channels * sh_audio->samplesize;
164 minlen = (minlen + unitsize - 1) / unitsize * unitsize;
165 if (minlen > maxlen)
166 // if someone needs hundreds of channels adjust audio_out_minsize
167 // based on channels in preinit()
168 return -1;
170 int len = 0;
171 struct ad_pcm_context *ctx = sh_audio->context;
172 while (len < minlen) {
173 if (ctx->buffer_len - ctx->buffer_pos <= 0) {
174 double pts;
175 unsigned char *ptr;
176 int plen = ds_get_packet_pts(sh_audio->ds, &ptr, &pts);
177 if (plen < 0)
178 break;
179 if (ctx->buffer_size < plen) {
180 talloc_free(ctx->buffer);
181 ctx->buffer = talloc_size(ctx, plen);
182 ctx->buffer_size = plen;
184 memcpy(ctx->buffer, ptr, plen);
185 ctx->buffer_len = plen;
186 ctx->buffer_pos = 0;
187 if (pts != MP_NOPTS_VALUE) {
188 sh_audio->pts = pts;
189 sh_audio->pts_bytes = 0;
192 int from_stored = ctx->buffer_len - ctx->buffer_pos;
193 if (from_stored > minlen - len)
194 from_stored = minlen - len;
195 memcpy(buf + len, ctx->buffer + ctx->buffer_pos, from_stored);
196 ctx->buffer_pos += from_stored;
197 sh_audio->pts_bytes += from_stored;
198 len += from_stored;
200 if (len % unitsize) {
201 mp_msg(MSGT_DECAUDIO, MSGL_WARN, "[ad_pcm] discarding partial sample "
202 "at end\n");
203 len -= len % unitsize;
205 if (len == 0)
206 len = -1; // The loop above only exits at error/EOF
207 if (len > 0 && sh_audio->channels >= 5) {
208 reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT,
209 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
210 sh_audio->channels, len / sh_audio->samplesize,
211 sh_audio->samplesize);
213 return len;