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