summaryrefslogtreecommitdiffstats
path: root/Src/aacdec-mft/FLVAACDecoder.cpp
blob: 37cdb591d1107f3a5f563da8a907cbfb562d73f9 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "FLVAACDecoder.h"
#include <math.h>
#include "../nsutil/pcm.h"

int FLVDecoder::CreateAudioDecoder(int stereo, int bits, int sample_rate, int format_type, ifc_flvaudiodecoder **decoder)
{
	if (format_type == FLV::AUDIO_FORMAT_AAC)
	{
		FLVAAC *aac = new FLVAAC();
		if (!aac)
		{
			return CREATEDECODER_FAILURE;
		}
		*decoder = aac;
		return CREATEDECODER_SUCCESS;
	}
	return CREATEDECODER_NOT_MINE;
}

int FLVDecoder::HandlesAudio(int format_type)
{
	if (format_type == FLV::AUDIO_FORMAT_AAC)
	{
		return CREATEDECODER_SUCCESS;
	}
	return CREATEDECODER_NOT_MINE;
}

#define CBCLASS FLVDecoder
START_DISPATCH;
CB(CREATE_AUDIO_DECODER, CreateAudioDecoder)
CB(HANDLES_AUDIO, HandlesAudio)
END_DISPATCH;
#undef CBCLASS

/* --- */
FLVAAC::FLVAAC()
{
	bps = 16;
	preDelay=0;
	got_decoder_config = false;
}

int FLVAAC::GetOutputFormat(unsigned int *sample_rate, unsigned int *channels, unsigned int *_bits)
{
	uint32_t local_sample_rate, local_channels;
	HRESULT hr = decoder.GetOutputProperties(&local_sample_rate, &local_channels);
	if (FAILED(hr)) {
		return FLV_AUDIO_FAILURE;
	}

	*sample_rate = local_sample_rate;
	*channels = local_channels;

	*_bits = bps;
	return FLV_AUDIO_SUCCESS;
}

int FLVAAC::DecodeSample(const void *input_buffer, size_t input_buffer_bytes, void *samples, size_t *samples_size_bytes, double *bitrate)
{
	const uint8_t *type = (const uint8_t *)input_buffer;
	if (type[0] == 0)
	{
		decoder.Open(type+1, input_buffer_bytes-1);

		got_decoder_config=true;
		*samples_size_bytes=0;
		return FLV_AUDIO_SUCCESS;
		return FLV_AUDIO_FAILURE;
	}
	else if (type[0] == 1)
	{
		decoder.Feed(input_buffer, input_buffer_bytes);
		decoder.Decode(samples, samples_size_bytes, bps, false, 1.0);
		*bitrate = 0;
		return FLV_AUDIO_SUCCESS;
		
	}
	else
		return FLV_AUDIO_FAILURE;
}

void FLVAAC::Flush()
{
	decoder.Flush();
}

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

int FLVAAC::Ready()
{
	return !!got_decoder_config;
}

void FLVAAC::SetPreferences(unsigned int _max_channels, unsigned int preferred_bits)
{
	if (preferred_bits)
		bps = preferred_bits;

	// TODO: max channels
}

#define CBCLASS FLVAAC
START_DISPATCH;
CB(FLV_AUDIO_GETOUTPUTFORMAT, GetOutputFormat)
CB(FLV_AUDIO_DECODE, DecodeSample)
VCB(FLV_AUDIO_FLUSH, Flush)
VCB(FLV_AUDIO_CLOSE, Close)
CB(FLV_AUDIO_READY, Ready)
VCB(FLV_AUDIO_SETPREFERENCES, SetPreferences)
END_DISPATCH;
#undef CBCLASS