xref: /haiku/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.h (revision f2b4344867e97c3f4e742a1b4a15e6879644601a)
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 class AVCodecEncoder : public Encoder {
21 public:
22 								AVCodecEncoder(uint32 codecID,
23 									int bitRateScale);
24 
25 	virtual						~AVCodecEncoder();
26 
27 	virtual	status_t			AcceptedFormat(
28 									const media_format* proposedInputFormat,
29 									media_format* _acceptedInputFormat = NULL);
30 
31 	virtual	status_t			SetUp(const media_format* inputFormat);
32 
33 	virtual status_t			GetEncodeParameters(
34 									encode_parameters* parameters) const;
35 	virtual status_t			SetEncodeParameters(
36 									encode_parameters* parameters);
37 
38 	virtual status_t			Encode(const void* buffer, int64 frameCount,
39 									media_encode_info* info);
40 
41 	// TODO: Turns out we need Flush() after all. We buffer encoded audio
42 	// in a FIFO, since the user suggested buffer size may not fit the
43 	// codec buffer size.
44 
45 private:
46 			status_t			_Setup();
47 
48 			bool				_OpenCodecIfNeeded();
49 			void				_CloseCodecIfNeeded();
50 
51 			status_t			_EncodeAudio(const void* buffer,
52 									int64 frameCount,
53 									media_encode_info* info);
54 			status_t			_EncodeAudio(const uint8* buffer,
55 									size_t bufferSize, int64 frameCount,
56 									media_encode_info* info);
57 
58 			status_t			_EncodeVideo(const void* buffer,
59 									int64 frameCount,
60 									media_encode_info* info);
61 
62 private:
63 			media_format		fInputFormat;
64 			encode_parameters	fEncodeParameters;
65 			int					fBitRateScale;
66 
67 			// FFmpeg related members
68 			// TODO: Refactor common base class from AVCodec[De|En]Coder!
69 			CodecID				fCodecID;
70 			AVCodec*			fCodec;
71 			AVCodecContext*		fOwnContext;
72 			AVCodecContext*		fContext;
73 
74 			enum {
75 				CODEC_INIT_NEEDED = 0,
76 				CODEC_INIT_DONE,
77 				CODEC_INIT_FAILED
78 			};
79 			uint32				fCodecInitStatus;
80 
81 			// For video (color space conversion):
82 			AVPicture			fSrcFrame;
83 			AVPicture			fDstFrame;
84 			AVFrame*			fFrame;
85 			SwsContext*			fSwsContext;
86 
87 			// For encoded audio:
88 			AVFifoBuffer*		fAudioFifo;
89 
90 			int64				fFramesWritten;
91 
92 			uint8*				fChunkBuffer;
93 };
94 
95 #endif // AVCODEC_ENCODER_H
96