xref: /haiku/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.h (revision 894526b51a3d931c423878fc0eb8da610fa1fb2a)
1 /*
2  * Copyright 2009, Stephan Aßmus <superstippi@gmx.de>.
3  * All rights reserved. Distributed under the terms of the MIT license.
4  */
5 #ifndef AVCODEC_ENCODER_H
6 #define AVCODEC_ENCODER_H
7 
8 
9 #include <MediaFormats.h>
10 
11 extern "C" {
12 	#include "avcodec.h"
13 	#include "swscale.h"
14 	#include "libavutil/fifo.h"
15 }
16 
17 #include "EncoderPlugin.h"
18 
19 
20 #if LIBAVCODEC_VERSION_INT > ((54 << 16) | (50 << 8))
21 typedef AVCodecID CodecID;
22 #endif
23 
24 
25 class AVCodecEncoder : public Encoder {
26 public:
27 								AVCodecEncoder(uint32 codecID,
28 									int bitRateScale);
29 
30 	virtual						~AVCodecEncoder();
31 
32 	virtual	status_t			AcceptedFormat(
33 									const media_format* proposedInputFormat,
34 									media_format* _acceptedInputFormat = NULL);
35 
36 	virtual	status_t			SetUp(const media_format* inputFormat);
37 
38 	virtual status_t			GetEncodeParameters(
39 									encode_parameters* parameters) const;
40 	virtual status_t			SetEncodeParameters(
41 									encode_parameters* parameters);
42 
43 	virtual status_t			Encode(const void* buffer, int64 frameCount,
44 									media_encode_info* info);
45 
46 	// TODO: Turns out we need Flush() after all. We buffer encoded audio
47 	// in a FIFO, since the user suggested buffer size may not fit the
48 	// codec buffer size.
49 
50 private:
51 			status_t			_Setup();
52 
53 			bool				_OpenCodecIfNeeded();
54 			void				_CloseCodecIfNeeded();
55 
56 			status_t			_EncodeAudio(const void* buffer,
57 									int64 frameCount,
58 									media_encode_info* info);
59 			status_t			_EncodeAudio(const uint8* buffer,
60 									size_t bufferSize, int64 frameCount,
61 									media_encode_info* info);
62 
63 			status_t			_EncodeVideo(const void* buffer,
64 									int64 frameCount,
65 									media_encode_info* info);
66 
67 private:
68 			media_format		fInputFormat;
69 			encode_parameters	fEncodeParameters;
70 			int					fBitRateScale;
71 
72 			// FFmpeg related members
73 			// TODO: Refactor common base class from AVCodec[De|En]Coder!
74 			CodecID				fCodecID;
75 			AVCodec*			fCodec;
76 			AVCodecContext*		fOwnContext;
77 			AVCodecContext*		fContext;
78 
79 			enum {
80 				CODEC_INIT_NEEDED = 0,
81 				CODEC_INIT_DONE,
82 				CODEC_INIT_FAILED
83 			};
84 			uint32				fCodecInitStatus;
85 
86 			// For video (color space conversion):
87 			AVPicture			fSrcFrame;
88 			AVPicture			fDstFrame;
89 			AVFrame*			fFrame;
90 			SwsContext*			fSwsContext;
91 
92 			// For encoded audio:
93 			AVFifoBuffer*		fAudioFifo;
94 
95 			int64				fFramesWritten;
96 
97 			uint8*				fChunkBuffer;
98 };
99 
100 #endif // AVCODEC_ENCODER_H
101