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