xref: /haiku/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.h (revision 4bd0c1066b227cec4b79883bdef697c7a27f2e90)
1 /*
2  * Copyright (C) 2001 Carlos Hasan.
3  * Copyright (C) 2001 François Revol.
4  * Copyright (C) 2001 Axel Dörfler.
5  * Copyright (C) 2004 Marcus Overhagen.
6  * Copyright (C) 2009 Stephan Aßmus <superstippi@gmx.de>.
7  * Copyright (C) 2015 Adrien Destugues <pulkomandy@pulkomandy.tk>.
8  *
9  * All rights reserved. Distributed under the terms of the MIT License.
10  */
11 #ifndef AVCODEC_DECODER_H
12 #define AVCODEC_DECODER_H
13 
14 //! libavcodec based decoder for Haiku
15 
16 
17 #include <MediaFormats.h>
18 
19 
20 extern "C" {
21 	#include "avcodec.h"
22 #if LIBAVCODEC_VERSION_INT >= ((57 << 16) | (0 << 8))
23 	#include "avfilter.h"
24 	#include "buffersink.h"
25 	#include "buffersrc.h"
26 #endif
27 	#include "swresample.h"
28 	#include "swscale.h"
29 }
30 
31 
32 #include "DecoderPlugin.h"
33 #include "ReaderPlugin.h"
34 
35 #include "CodecTable.h"
36 #include "gfx_util.h"
37 
38 
39 #ifdef __x86_64
40 #define USE_SWS_FOR_COLOR_SPACE_CONVERSION 1
41 #else
42 #define USE_SWS_FOR_COLOR_SPACE_CONVERSION 0
43 // NOTE: David's color space conversion is much faster than the FFmpeg
44 // version. Perhaps the SWS code can be used for unsupported conversions?
45 // Otherwise the alternative code could simply be removed from this file.
46 #endif
47 
48 
49 class AVCodecDecoder : public Decoder {
50 public:
51 						AVCodecDecoder();
52 
53 	virtual				~AVCodecDecoder();
54 
55 	virtual	void		GetCodecInfo(media_codec_info* mci);
56 
57 	virtual	status_t	Setup(media_format* ioEncodedFormat,
58 							const void* infoBuffer, size_t infoSize);
59 
60 	virtual	status_t	NegotiateOutputFormat(media_format* inOutFormat);
61 
62 	virtual	status_t	Decode(void* outBuffer, int64* outFrameCount,
63 							media_header* mediaHeader,
64 							media_decode_info* info);
65 
66 	virtual	status_t	SeekedTo(int64 trame, bigtime_t time);
67 
68 
69 private:
70 			void		_ResetTempPacket();
71 
72 			status_t	_NegotiateAudioOutputFormat(media_format* inOutFormat);
73 
74 			status_t	_NegotiateVideoOutputFormat(media_format* inOutFormat);
75 
76 			status_t	_DecodeAudio(void* outBuffer, int64* outFrameCount,
77 							media_header* mediaHeader,
78 							media_decode_info* info);
79 
80 			status_t	_DecodeVideo(void* outBuffer, int64* outFrameCount,
81 							media_header* mediaHeader,
82 							media_decode_info* info);
83 
84 			status_t	_DecodeNextAudioFrame();
85 			void		_ApplyEssentialAudioContainerPropertiesToContext();
86 			status_t	_ResetRawDecodedAudio();
87 			void		_CheckAndFixConditionsThatHintAtBrokenAudioCodeBelow();
88 			void		_MoveAudioFramesToRawDecodedAudioAndUpdateStartTimes();
89 			status_t	_DecodeNextAudioFrameChunk();
90 			status_t	_DecodeSomeAudioFramesIntoEmptyDecodedDataBuffer();
91 			void		_UpdateMediaHeaderForAudioFrame();
92 
93 			status_t	_DecodeNextVideoFrame();
94 			void		_ApplyEssentialVideoContainerPropertiesToContext();
95 			status_t	_LoadNextChunkIfNeededAndAssignStartTime();
96 			status_t	_CopyChunkToChunkBufferAndAddPadding(const void* chunk,
97 							size_t chunkSize);
98 			status_t	_HandleNewVideoFrameAndUpdateSystemState();
99 			status_t	_FlushOneVideoFrameFromDecoderBuffer();
100 			void		_UpdateMediaHeaderForVideoFrame();
101 			status_t	_DeinterlaceAndColorConvertVideoFrame();
102 
103 #if LIBAVCODEC_VERSION_INT >= ((57 << 16) | (0 << 8))
104 			// video deinterlace filter graph
105 			status_t	_InitFilterGraph(enum AVPixelFormat pixfmt,
106 							int32 width, int32 height);
107 			status_t	_ProcessFilterGraph(AVPicture *dst,
108 							const AVPicture *src, enum AVPixelFormat pixfmt,
109 							int32 width, int32 height);
110 #endif
111 
112 			media_header		fHeader;
113 									// Contains the properties of the current
114 									// decoded audio / video frame
115 
116 			media_format		fInputFormat;
117 
118 			int64				fFrame;
119 			bool				fIsAudio;
120 
121 			// FFmpeg related members
122 			AVCodec*			fCodec;
123 			AVCodecContext*		fContext;
124 			SwrContext*			fResampleContext;
125 			uint8_t*			fDecodedData;
126 			size_t				fDecodedDataSizeInBytes;
127 			AVFrame*			fPostProcessedDecodedPicture;
128 			AVFrame*			fRawDecodedPicture;
129 			AVFrame*			fRawDecodedAudio;
130 
131 			bool 				fCodecInitDone;
132 
133 			gfx_convert_func	fFormatConversionFunc;
134 #if USE_SWS_FOR_COLOR_SPACE_CONVERSION
135 			SwsContext*			fSwsContext;
136 #endif
137 
138 			char*				fExtraData;
139 			int					fExtraDataSize;
140 			int					fBlockAlign;
141 
142 			color_space			fOutputColorSpace;
143 			int32				fOutputFrameCount;
144 			float				fOutputFrameRate;
145 			int					fOutputFrameSize;
146 									// sample size * channel count
147 			int					fInputFrameSize;
148 									// sample size * channel count
149 									// or just sample size for planar formats
150 
151 			uint8_t*			fChunkBuffer;
152 			size_t				fChunkBufferSize;
153 			bool				fAudioDecodeError;
154 
155 			AVFrame*			fDecodedDataBuffer;
156 			int32				fDecodedDataBufferOffset;
157 			int32				fDecodedDataBufferSize;
158 
159 			AVPacket			fTempPacket;
160 
161 #if LIBAVCODEC_VERSION_INT >= ((57 << 16) | (0 << 8))
162 			// video deinterlace feature
163 			AVFilterContext*	fBufferSinkContext;
164 			AVFilterContext*	fBufferSourceContext;
165 			AVFilterGraph*		fFilterGraph;
166 			AVFrame*			fFilterFrame;
167 			int32				fLastWidth;
168 			int32				fLastHeight;
169 			enum AVPixelFormat	fLastPixfmt;
170 #endif
171 };
172 
173 #endif // AVCODEC_DECODER_H
174