aboutsummaryrefslogtreecommitdiff
path: root/Src/pcm/avi_pcm_decoder.cpp
blob: d3e4ba73a8c985fe42d72753ad987e377691545e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "avi_pcm_decoder.h"
#include "avi_alaw_decoder.h"
#include "avi_ulaw_decoder.h"

int AVIDecoder::CreateAudioDecoder(const nsavi::AVIH *avi_header, 
																	 const nsavi::STRH *stream_header, const nsavi::STRF *stream_format, const nsavi::STRD *stream_data, 
																	 unsigned int preferred_bits, unsigned int max_channels, bool floating_point, 
																	 ifc_aviaudiodecoder **decoder)
{
	nsavi::audio_format *waveformat = (nsavi::audio_format *)stream_format;

	if (waveformat->format == nsavi::audio_format_pcm		|| waveformat->format == nsavi::audio_format_extensible)
	{
		*decoder = new AVIPCMDecoder(waveformat);
		return CREATEDECODER_SUCCESS;
	}
	else if (waveformat->format == nsavi::audio_format_alaw)
	{
			*decoder = new AVIALawDecoder(waveformat);
		return CREATEDECODER_SUCCESS;
	}
		else if (waveformat->format == nsavi::audio_format_ulaw)
	{
			*decoder = new AVIULawDecoder(waveformat);
		return CREATEDECODER_SUCCESS;
	}

	return CREATEDECODER_NOT_MINE;

}

#define CBCLASS AVIDecoder
START_DISPATCH;
CB(CREATE_AUDIO_DECODER, CreateAudioDecoder)
END_DISPATCH;
#undef CBCLASS

AVIPCMDecoder::AVIPCMDecoder(const nsavi::audio_format *waveformat) : waveformat(waveformat)
{
}

int AVIPCMDecoder::OutputFrameSize(size_t *frame_size)
{
	*frame_size = waveformat->block_align; // TODO
	return AVI_SUCCESS;
}

int AVIPCMDecoder::GetOutputProperties(unsigned int *sampleRate, unsigned int *channels, unsigned int *bitsPerSample, bool *isFloat)
{
	if (waveformat)
	{
		*sampleRate = waveformat->sample_rate;
		*channels = waveformat->channels;
		*bitsPerSample = waveformat->bits_per_sample;
		*isFloat = false; // TODO
		return AVI_SUCCESS;
	}
	else
	{
		return AVI_FAILURE;
	}
}

int AVIPCMDecoder::DecodeChunk(uint16_t type, void **inputBuffer, size_t *inputBufferBytes, void *outputBuffer, size_t *outputBufferBytes)
{
	size_t copy_size = min(*inputBufferBytes, *outputBufferBytes);
	int remainder = copy_size % waveformat->block_align;
	copy_size -= remainder;
	memcpy(outputBuffer, *inputBuffer, copy_size);
	*inputBuffer = (uint8_t *)(*inputBuffer) + copy_size;
	*inputBufferBytes = *inputBufferBytes - copy_size;
	*outputBufferBytes = copy_size;
	return AVI_SUCCESS;
}

void AVIPCMDecoder::Close()
{
	delete this;
}

#define CBCLASS AVIPCMDecoder
START_DISPATCH;
CB(OUTPUT_FRAME_SIZE, OutputFrameSize)
CB(GET_OUTPUT_PROPERTIES, GetOutputProperties)
CB(DECODE_CHUNK, DecodeChunk)
VCB(CLOSE, Close)
END_DISPATCH;
#undef CBCLASS