summaryrefslogtreecommitdiffstats
path: root/Src/mp4v/avi_mp4v_decoder.cpp
blob: 185cb84ca7e3973d7d3c7185a30e9747c365bf9e (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
#include "avi_mp4v_decoder.h"
#include "../Winamp/wa_ipc.h"
#include "../nsavi/read.h"
#include <mmsystem.h>
#include <Amvideo.h>
#include <Dvdmedia.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 == 'DIVX' || format->compression == 'divx' // xvid  
			|| format->compression == 'xvid' || format->compression == 'XVID' // divx
			|| format->compression == 'v4pm' // mp4v
			|| format->compression == '05XD' // divx 5
			|| format->compression == nsaviFOURCC('S','E','D','G') // dunno what this is exactly
			/* || format->compression == '3VID' // divx 3, let's hope it plays */
			) {
				VIDEOINFOHEADER header = {0, };
				//header.rcSource.right = format->width;
				//header.rcSource.bottom = format->height;
				//header.rcTarget = header.rcSource;
				memcpy(&header.bmiHeader, &format->video_format_size_bytes, 40);
				
			MFTDecoder *ctx = new MFTDecoder;
			if (!ctx) {
				return CREATEDECODER_FAILURE;
			}
			if (FAILED(ctx->Open(&header))) {
				delete ctx;
				return CREATEDECODER_FAILURE;
			}
			*decoder = new AVIMP4V(ctx, stream_header, format);
			return CREATEDECODER_SUCCESS;
		}
	}

	return CREATEDECODER_NOT_MINE;
}


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

AVIMP4V::AVIMP4V(MFTDecoder *decoder, const nsavi::STRH *stream_header, const nsavi::video_format *stream_format) 
: decoder(decoder), stream_header(stream_header), stream_format(stream_format)
{
	if (stream_format->size_bytes > 40) {
		//MPEG4Video_DecodeFrame(decoder, ((const uint8_t *)stream_format) + 44, stream_format->size_bytes - stream_format->video_format_size_bytes, 0);
	}
}

AVIMP4V::~AVIMP4V()
{
	delete decoder;
}

int AVIMP4V::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 AVIMP4V::DecodeChunk(uint16_t type, const void *inputBuffer, size_t inputBufferBytes)
{
	if (decoder) {
		decoder->Feed(inputBuffer, inputBufferBytes, 0);
		return AVI_SUCCESS;
	}

	return AVI_FAILURE;
}

void AVIMP4V::Flush()
{
	if (decoder) {
		decoder->Flush();
	}
}

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

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

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

void AVIMP4V::HurryUp(int state)
{
	//if (decoder)
		//MPEG4Video_HurryUp(decoder, state);
}

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

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