aboutsummaryrefslogtreecommitdiff
path: root/Src/h264/avi_h264_decoder.cpp
blob: 7411ead71a3d116eda7bccc47bdd41eb7fd895dc (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
#include "avi_h264_decoder.h"
#include "../Winamp/wa_ipc.h"
#include <mmsystem.h>
#include <assert.h>
#include <Mferror.h>


int AVIDecoderCreator::CreateVideoDecoder(const nsavi::AVIH *avi_header, const nsavi::STRH *stream_header, const nsavi::STRF *stream_format, const nsavi::STRD *stream_data, ifc_avivideodecoder **decoder)
{
	nsavi::video_format *format = (nsavi::video_format *)stream_format;
	if (format)
	{
		if (format->compression == '462H')
		{
			MFTDecoder *ctx = new MFTDecoder();
			if (!ctx)
				return CREATEDECODER_FAILURE;

			if (FAILED(ctx->Open())) {
				delete ctx;
				return CREATEDECODER_FAILURE;
			}
			*decoder = new AVIH264(ctx, stream_header);
			return CREATEDECODER_SUCCESS;
		}
	}
	return CREATEDECODER_NOT_MINE;
}


#define CBCLASS AVIDecoderCreator
START_DISPATCH;
CB(CREATE_VIDEO_DECODER, CreateVideoDecoder)
END_DISPATCH;
#undef CBCLASS

AVIH264::AVIH264(MFTDecoder *ctx, const nsavi::STRH *stream_header) : decoder(ctx), stream_header(stream_header)
{
}

AVIH264::~AVIH264()
{
	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 AVIH264::GetOutputProperties(int *x, int *y, int *color_format, double *aspect_ratio, int *flip)
{
	UINT width, height;
	bool local_flip=false;
	if (SUCCEEDED(decoder->GetOutputFormat(&width, &height, &local_flip, aspect_ratio))) {
		*x = width;
		*y = height;
		*color_format = '21VY';
		*flip = local_flip;
		return AVI_SUCCESS;
	}
	return AVI_FAILURE;		
}

int AVIH264::DecodeChunk(uint16_t type, const void *inputBuffer, size_t inputBufferBytes)
{
	for (;;) {
		HRESULT hr = decoder->FeedRaw(inputBuffer, inputBufferBytes, 0);
		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 AVI_FAILURE;
		} else {
			break;
		}
	}
	return AVI_SUCCESS;
}

void AVIH264::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 AVIH264::GetPicture(void **data, void **decoder_data)
{
	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;
		return AVI_SUCCESS;
	}

	if (SUCCEEDED(decoder->GetFrame((YV12_PLANES **)data, decoder_data, 0))) {
		return AVI_SUCCESS;
	} else {
		return AVI_FAILURE;
	}
}

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

void AVIH264::EndOfStream()
{
	decoder->Drain();
}

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

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

#define CBCLASS AVIH264
START_DISPATCH;
CB(GET_OUTPUT_PROPERTIES, GetOutputProperties)
CB(DECODE_CHUNK, DecodeChunk)
VCB(FLUSH, Flush)
VCB(CLOSE, Close)
CB(GET_PICTURE, GetPicture)
VCB(FREE_PICTURE, FreePicture)
VCB(END_OF_STREAM, EndOfStream)
VCB(HURRY_UP, HurryUp)
END_DISPATCH;
#undef CBCLASS