xref: /haiku/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.h (revision cf02b29e4e0dd6d61c4bb25fcc8620e99d4908bf)
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 			AVCodec*			fCodec;
70 			AVCodecContext*		fContext;
71 
72 			enum {
73 				CODEC_INIT_NEEDED = 0,
74 				CODEC_INIT_DONE,
75 				CODEC_INIT_FAILED
76 			};
77 			uint32				fCodecInitStatus;
78 
79 			// For video (color space conversion):
80 			AVPicture			fSrcFrame;
81 			AVPicture			fDstFrame;
82 			AVFrame*			fFrame;
83 			SwsContext*			fSwsContext;
84 
85 			// For encoded audio:
86 			AVFifoBuffer*		fAudioFifo;
87 
88 			int64				fFramesWritten;
89 
90 			uint8*				fChunkBuffer;
91 };
92 
93 #endif // AVCODEC_ENCODER_H
94