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 void _Init(); 52 status_t _Setup(); 53 54 bool _OpenCodecIfNeeded(); 55 void _CloseCodecIfNeeded(); 56 57 status_t _EncodeAudio(const void* buffer, 58 int64 frameCount, 59 media_encode_info* info); 60 status_t _EncodeAudio(const uint8* buffer, 61 size_t bufferSize, int64 frameCount, 62 media_encode_info* info); 63 64 status_t _EncodeVideo(const void* buffer, 65 int64 frameCount, 66 media_encode_info* info); 67 68 private: 69 media_format fInputFormat; 70 encode_parameters fEncodeParameters; 71 int fBitRateScale; 72 73 // FFmpeg related members 74 // TODO: Refactor common base class from AVCodec[De|En]Coder! 75 CodecID fCodecID; 76 AVCodec* fCodec; 77 AVCodecContext* fOwnContext; 78 AVCodecContext* fContext; 79 80 enum { 81 CODEC_INIT_NEEDED = 0, 82 CODEC_INIT_DONE, 83 CODEC_INIT_FAILED 84 }; 85 uint32 fCodecInitStatus; 86 87 // For video (color space conversion): 88 AVPicture fSrcFrame; 89 AVPicture fDstFrame; 90 AVFrame* fFrame; 91 SwsContext* fSwsContext; 92 93 // For encoded audio: 94 AVFifoBuffer* fAudioFifo; 95 96 int64 fFramesWritten; 97 98 uint8* fChunkBuffer; 99 }; 100 101 #endif // AVCODEC_ENCODER_H 102