Extend -vf format to allow substituting the format for a compatible one
[mplayer/glamo.git] / libmpdemux / demux_rawaudio.c
blob949b2d5d7fafbc82d58af9cb8ab044d032abff61
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 "config.h"
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <string.h>
26 #include "m_option.h"
28 #include "stream/stream.h"
29 #include "demuxer.h"
30 #include "stheader.h"
33 static int channels = 2;
34 static int samplerate = 44100;
35 static int samplesize = 2;
36 static int bitrate = 0;
37 static int format = 0x1; // Raw PCM
39 const m_option_t demux_rawaudio_opts[] = {
40 { "channels", &channels, CONF_TYPE_INT,CONF_RANGE,1,8, NULL },
41 { "rate", &samplerate, CONF_TYPE_INT,CONF_RANGE,1000,8*48000, NULL },
42 { "samplesize", &samplesize, CONF_TYPE_INT,CONF_RANGE,1,8, NULL },
43 { "bitrate", &bitrate, CONF_TYPE_INT,CONF_MIN,0,0, NULL },
44 { "format", &format, CONF_TYPE_INT, CONF_MIN, 0 , 0, NULL },
45 {NULL, NULL, 0, 0, 0, 0, NULL}
49 static demuxer_t* demux_rawaudio_open(demuxer_t* demuxer) {
50 sh_audio_t* sh_audio;
51 WAVEFORMATEX* w;
53 sh_audio = new_sh_audio(demuxer,0);
54 sh_audio->wf = w = malloc(sizeof(WAVEFORMATEX));
55 w->wFormatTag = sh_audio->format = format;
56 w->nChannels = sh_audio->channels = channels;
57 w->nSamplesPerSec = sh_audio->samplerate = samplerate;
58 if (bitrate > 999)
59 w->nAvgBytesPerSec = bitrate/8;
60 else if (bitrate > 0)
61 w->nAvgBytesPerSec = bitrate*125;
62 else
63 w->nAvgBytesPerSec = samplerate*samplesize*channels;
64 w->nBlockAlign = channels*samplesize;
65 sh_audio->samplesize = samplesize;
66 w->wBitsPerSample = 8*samplesize;
67 w->cbSize = 0;
69 demuxer->movi_start = demuxer->stream->start_pos;
70 demuxer->movi_end = demuxer->stream->end_pos;
72 demuxer->audio->id = 0;
73 demuxer->audio->sh = sh_audio;
74 sh_audio->ds = demuxer->audio;
75 sh_audio->needs_parsing = 1;
77 return demuxer;
80 static int demux_rawaudio_fill_buffer(demuxer_t* demuxer, demux_stream_t *ds) {
81 sh_audio_t* sh_audio = demuxer->audio->sh;
82 int l = sh_audio->wf->nAvgBytesPerSec;
83 off_t spos = stream_tell(demuxer->stream);
84 demux_packet_t* dp;
86 if(demuxer->stream->eof)
87 return 0;
89 dp = new_demux_packet(l);
90 dp->pts = (spos - demuxer->movi_start) / (float)(sh_audio->wf->nAvgBytesPerSec);
91 dp->pos = (spos - demuxer->movi_start);
93 l = stream_read(demuxer->stream,dp->buffer,l);
94 resize_demux_packet(dp, l);
95 ds_add_packet(ds,dp);
97 return 1;
100 static void demux_rawaudio_seek(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){
101 stream_t* s = demuxer->stream;
102 sh_audio_t* sh_audio = demuxer->audio->sh;
103 off_t base,pos;
105 base = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : stream_tell(s);
106 if(flags & SEEK_FACTOR)
107 pos = base + ((demuxer->movi_end - demuxer->movi_start)*rel_seek_secs);
108 else
109 pos = base + (rel_seek_secs*sh_audio->i_bps);
111 pos -= (pos % (sh_audio->channels * sh_audio->samplesize) );
112 stream_seek(s,pos);
113 // printf("demux_rawaudio: streamtell=%d\n",(int)stream_tell(demuxer->stream));
116 const demuxer_desc_t demuxer_desc_rawaudio = {
117 "Raw audio demuxer",
118 "rawaudio",
119 "rawaudio",
120 "?",
122 DEMUXER_TYPE_RAWAUDIO,
123 0, // no autodetect
124 NULL,
125 demux_rawaudio_fill_buffer,
126 demux_rawaudio_open,
127 NULL,
128 demux_rawaudio_seek,