aboutsummaryrefslogtreecommitdiff
path: root/Src/h264/h264_flv_decoder.cpp
blob: f9a6f6c72dda7ced0202836b72112accef2a40c7 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "h264_flv_decoder.h"
#include "../Winamp/wa_ipc.h" // for YV12_PLANES
#include <Mferror.h>

int FLVDecoderCreator::CreateVideoDecoder(int format_type, int width, int height, ifc_flvvideodecoder **decoder)
{
	if (format_type == FLV::VIDEO_FORMAT_AVC)
	{
		MFTDecoder *ctx = new MFTDecoder();
		if (!ctx || FAILED(ctx->Open())) {
			delete ctx;
			return CREATEDECODER_FAILURE;
		}
		*decoder = new FLVH264(ctx);
		return CREATEDECODER_SUCCESS;
	}
	return CREATEDECODER_NOT_MINE;
}

int FLVDecoderCreator::HandlesVideo(int format_type)
{
	if (format_type == FLV::VIDEO_FORMAT_AVC)
	{
		return CREATEDECODER_SUCCESS;
	}
	return CREATEDECODER_NOT_MINE;
}

#define CBCLASS FLVDecoderCreator
START_DISPATCH;
CB(CREATE_VIDEO_DECODER, CreateVideoDecoder)
CB(HANDLES_VIDEO, HandlesVideo)
END_DISPATCH;
#undef CBCLASS

/* --- */
uint32_t GetNALUSize(uint64_t nalu_size_bytes, const uint8_t *h264_data, size_t data_len);
uint32_t Read24(const uint8_t *data);

FLVH264::FLVH264(MFTDecoder *decoder) : decoder(decoder)
{
	sequence_headers_parsed=0;
	nalu_size_bytes=0;
}

FLVH264::~FLVH264()
{
	for (size_t i=0;i<buffered_frames.size();i++) {
		nullsoft_h264_frame_data frame_data = buffered_frames[i];
		decoder->FreeFrame((YV12_PLANES *)frame_data.data, frame_data.decoder_data);
	}
	delete decoder;
}

int FLVH264::GetOutputFormat(int *x, int *y, int *color_format)
{
	UINT width, height;
	bool local_flip=false;
	double aspect_ratio;
	if (SUCCEEDED(decoder->GetOutputFormat(&width, &height, &local_flip, &aspect_ratio))) {
		*x = width;
		*y = height;
		*color_format = '21VY';
		return FLV_VIDEO_SUCCESS;
	}
	return FLV_VIDEO_FAILURE;	
}

int FLVH264::DecodeSample(const void *inputBuffer, size_t inputBufferBytes, int32_t timestamp)
{
	const uint8_t *h264_data = (const uint8_t *)inputBuffer;
	if (*h264_data == 0 && inputBufferBytes >= 10) // sequence headers 
	{
		h264_data++; // skip packet type 
		uint32_t timestamp_offset = Read24(h264_data); 
		h264_data+=3;
		inputBufferBytes -=4;
		h264_data+=4; // don't care about level & profile
		inputBufferBytes -=4;
		nalu_size_bytes = (*h264_data++ & 0x3)+1;
		inputBufferBytes--;
		size_t num_sps = *h264_data++ & 0x1F;
		inputBufferBytes--;
		for (size_t i=0;i!=num_sps;i++)
		{
			if (inputBufferBytes > 2)
			{
				uint16_t sps_size = (h264_data[0] << 8) | h264_data[1];
				h264_data+=2;
				inputBufferBytes-=2;
				//H264_ProcessSPS(decoder, h264_data+1, sps_size);
				if (inputBufferBytes >= sps_size)
				{
					decoder->Feed(h264_data, sps_size, timestamp+timestamp_offset);
					h264_data+=sps_size;
					inputBufferBytes-=sps_size;
				}
			}
		}
		if (inputBufferBytes)
		{
			size_t num_pps = *h264_data++;
			inputBufferBytes--;
			for (size_t i=0;i!=num_pps;i++)
			{
				if (inputBufferBytes > 2)
				{
					uint16_t sps_size = (h264_data[0] << 8) | h264_data[1];
					h264_data+=2;
					inputBufferBytes-=2;
					//H264_ProcessPPS(decoder, h264_data+1, sps_size);
					if (inputBufferBytes >= sps_size)
					{
						decoder->Feed(h264_data, sps_size, timestamp+timestamp_offset);
						h264_data+=sps_size;
						inputBufferBytes-=sps_size;
					}
				}
			}
		}
		sequence_headers_parsed=1;
	}
	else if (*h264_data == 1) // frame
	{
		h264_data++;
		inputBufferBytes--;
		if (inputBufferBytes < 3)
			return FLV_VIDEO_FAILURE;
		uint32_t timestamp_offset = Read24(h264_data);

		h264_data+=3;
		inputBufferBytes-=3;

		while (inputBufferBytes)
		{
			uint32_t this_size =GetNALUSize(nalu_size_bytes, h264_data, inputBufferBytes);
			if (this_size == 0)
				return FLV_VIDEO_FAILURE;

			inputBufferBytes-=nalu_size_bytes;
			h264_data+=nalu_size_bytes;
			if (this_size > inputBufferBytes)
				return FLV_VIDEO_FAILURE;
			for (;;) {
				HRESULT hr = decoder->Feed(h264_data, this_size, timestamp+timestamp_offset);
				if (hr == MF_E_NOTACCEPTING) {
					nullsoft_h264_frame_data frame_data;
					if (FAILED(decoder->GetFrame((YV12_PLANES **)&frame_data.data, &frame_data.decoder_data, &frame_data.local_timestamp))) {
						continue;
					}
					buffered_frames.push_back(frame_data);
				} else if (FAILED(hr)) {
					return FLV_VIDEO_FAILURE;
				} else {
					break;
				}
			}			

			inputBufferBytes-=this_size;
			h264_data+=this_size;
		}
	}

	return FLV_VIDEO_SUCCESS;
}

void FLVH264::Flush()
{
	for (size_t i=0;i<buffered_frames.size();i++) {
		nullsoft_h264_frame_data frame_data = buffered_frames[i];
		decoder->FreeFrame((YV12_PLANES *)frame_data.data, frame_data.decoder_data);
	}
	decoder->Flush();
}

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

int FLVH264::GetPicture(void **data, void **decoder_data, uint64_t *timestamp)
{
	if (!buffered_frames.empty()) {
		nullsoft_h264_frame_data frame_data = buffered_frames[0];
		buffered_frames.erase(buffered_frames.begin());
		*data = frame_data.data;
		*decoder_data = frame_data.decoder_data;
		*timestamp = frame_data.local_timestamp;
		return FLV_VIDEO_SUCCESS;
	}

	if (SUCCEEDED(decoder->GetFrame((YV12_PLANES **)data, decoder_data, timestamp))) {
		return FLV_VIDEO_SUCCESS;
	} else {
		return FLV_VIDEO_FAILURE;
	}
}

void FLVH264::FreePicture(void *data, void *decoder_data)
{
	decoder->FreeFrame((YV12_PLANES *)data, decoder_data);
}

int FLVH264::Ready()
{
	return sequence_headers_parsed;
}

#define CBCLASS FLVH264
START_DISPATCH;
CB(FLV_VIDEO_GETOUTPUTFORMAT, GetOutputFormat)
CB(FLV_VIDEO_DECODE, DecodeSample)
VCB(FLV_VIDEO_FLUSH, Flush)
VCB(FLV_VIDEO_CLOSE, Close)
CB(FLV_VIDEO_GET_PICTURE, GetPicture)
VCB(FLV_VIDEO_FREE_PICTURE, FreePicture)
CB(FLV_VIDEO_READY, Ready)
END_DISPATCH;
#undef CBCLASS