summaryrefslogtreecommitdiffstats
path: root/Src/h264/h264_mp4_decoder.cpp
blob: afb48bb3c586aaa36083024d4e108b20ea5c3d7c (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
#include "h264_mp4_decoder.h"
#include "../winamp/wa_ipc.h"
#include <Mferror.h>
uint32_t Read24(const uint8_t *data)
{
	// ugh, 24bit size
	uint32_t this_size=0;
	uint8_t *this_size_p = (uint8_t *)&this_size;
	this_size_p[0] = data[2];
	this_size_p[1] = data[1];
	this_size_p[2] = data[0];
	return this_size;
}

uint32_t GetNALUSize(uint64_t nalu_size_bytes, const uint8_t *h264_data, size_t data_len)
{
	if ((data_len) < (nalu_size_bytes))
		return 0;

	switch(nalu_size_bytes)
	{
	case 1:
		return *h264_data;
	case 2:
		{
			return (h264_data[0] << 8) | h264_data[1];

		}
	case 3:
		{
			return Read24(h264_data);
		}
	case 4:
		{
			uint32_t this_size = *(uint32_t *)h264_data;
			this_size = htonl(this_size);
			return this_size;
		}
	}
	return 0;
}

H264MP4Decoder::H264MP4Decoder()
{
	nalu_size_bytes=0;
	width=0;
	height=0;
}

H264MP4Decoder::~H264MP4Decoder()
{
	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);
	}
}

int H264MP4Decoder::Open(MP4FileHandle mp4_file, MP4TrackId mp4_track)
{
	this->mp4_file=mp4_file;
	this->mp4_track=mp4_track;
	decoder.Open();
	// TODO error checking
	uint8_t **seqHeaders = 0, **pictHeaders = 0;
	uint32_t *seqHeadersSize = 0, *pictHeadersSize = 0;

	__try
	{
		MP4GetTrackH264SeqPictHeaders(mp4_file, mp4_track,
			&seqHeaders, &seqHeadersSize,
			&pictHeaders, &pictHeadersSize);

		if (seqHeadersSize)
		{
			for (uint32_t i = 0; seqHeadersSize[i] != 0; i++) 
			{
				decoder.Feed(seqHeaders[i], seqHeadersSize[i], 0);
				MP4Free(seqHeaders[i]);
			}
		}
		MP4Free(seqHeadersSize);

		if (pictHeadersSize)
		{
			for (uint32_t i = 0; pictHeadersSize[i] != 0; i++) 
			{
				decoder.Feed(pictHeaders[i], pictHeadersSize[i], 0);
				MP4Free(pictHeaders[i]);
			}
		}
		MP4Free(pictHeadersSize);
		
		MP4GetTrackH264LengthSize(mp4_file, mp4_track, &nalu_size_bytes);
	}
	__except(EXCEPTION_EXECUTE_HANDLER)
	{
		return MP4_VIDEO_FAILURE;
	}

	return MP4_VIDEO_SUCCESS;
}

int H264MP4Decoder::GetOutputFormat(int *x, int *y, int *color_format, double *aspect_ratio)
{
	bool flip;

	if (SUCCEEDED(decoder.GetOutputFormat(&width, &height, &flip, aspect_ratio))) {
		*x = width;
		*y = height;
		*color_format = htonl('YV12');
		return MP4_VIDEO_SUCCESS;
	}
	return MP4_VIDEO_FAILURE;		
}


int H264MP4Decoder::DecodeSample(const void *inputBuffer, size_t inputBufferBytes, MP4Timestamp timestamp)
{
	const uint8_t *h264_data = (const uint8_t *)inputBuffer;
	while (inputBufferBytes)
	{
		uint32_t this_size =GetNALUSize(nalu_size_bytes, h264_data, inputBufferBytes);
		if (this_size == 0)
			return MP4_VIDEO_FAILURE;

		inputBufferBytes-=nalu_size_bytes;
		h264_data+=nalu_size_bytes;
		if (this_size > inputBufferBytes)
			return MP4_VIDEO_FAILURE;

		for (;;) {
			uint64_t hundrednanos = MP4ConvertFromTrackTimestamp(mp4_file, mp4_track, timestamp, MP4_NANOSECONDS_TIME_SCALE/100ULL);
			HRESULT hr = decoder.Feed(h264_data, this_size, hundrednanos);
			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 MP4_VIDEO_FAILURE;
			} else {
				break;
			}
		}

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

int H264MP4Decoder::CanHandleCodec(const char *codecName)
{
	return !strcmp(codecName, "avc1");
}

void H264MP4Decoder::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();
}

int H264MP4Decoder::GetPicture(void **data, void **decoder_data, MP4Timestamp *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 = MP4ConvertToTrackTimestamp(mp4_file, mp4_track, frame_data.local_timestamp, MP4_NANOSECONDS_TIME_SCALE/100ULL);
		return MP4_VIDEO_SUCCESS;
	} 

	uint64_t local_timestamp;
	if (SUCCEEDED(decoder.GetFrame((YV12_PLANES **)data, decoder_data, &local_timestamp))) {
		*timestamp = MP4ConvertToTrackTimestamp(mp4_file, mp4_track, local_timestamp, MP4_NANOSECONDS_TIME_SCALE/100ULL);
		return MP4_VIDEO_SUCCESS;
	} else {
		return MP4_VIDEO_FAILURE;
	}
}

void H264MP4Decoder::FreePicture(void *data, void *decoder_data)
{
	decoder.FreeFrame((YV12_PLANES *)data, decoder_data);
}

void H264MP4Decoder::HurryUp(int state)
{
	// TODO if (decoder)
	//H264_HurryUp(decoder, state);
}



#define CBCLASS H264MP4Decoder
START_DISPATCH;
CB(MPEG4_VIDEO_OPEN, Open)
CB(MPEG4_VIDEO_GETOUTPUTFORMAT, GetOutputFormat)
CB(MPEG4_VIDEO_DECODE, DecodeSample)
CB(MPEG4_VIDEO_HANDLES_CODEC, CanHandleCodec)
VCB(MPEG4_VIDEO_FLUSH, Flush)
CB(MPEG4_VIDEO_GET_PICTURE, GetPicture)
VCB(MPEG4_VIDEO_FREE_PICTURE, FreePicture)
VCB(MPEG4_VIDEO_HURRY_UP, HurryUp)
END_DISPATCH;
#undef CBCLASS