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 <Encoder.h> 10 #include <MediaFormats.h> 11 12 using namespace BCodecKit; 13 14 extern "C" { 15 #include "avcodec.h" 16 #include "swscale.h" 17 #include "libavutil/fifo.h" 18 } 19 20 21 typedef AVCodecID CodecID; 22 23 24 class AVCodecEncoder : public BEncoder { 25 public: 26 AVCodecEncoder(uint32 codecID, 27 int bitRateScale); 28 29 virtual ~AVCodecEncoder(); 30 31 virtual status_t AcceptedFormat( 32 const media_format* proposedInputFormat, 33 media_format* _acceptedInputFormat = NULL); 34 35 virtual status_t SetUp(const media_format* inputFormat); 36 37 virtual status_t GetEncodeParameters( 38 encode_parameters* parameters) const; 39 virtual status_t SetEncodeParameters( 40 encode_parameters* parameters); 41 42 virtual status_t Encode(const void* buffer, int64 frameCount, 43 media_encode_info* info); 44 45 // TODO: Turns out we need Flush() after all. We buffer encoded audio 46 // in a FIFO, since the user suggested buffer size may not fit the 47 // codec buffer size. 48 49 private: 50 void _Init(); 51 status_t _Setup(); 52 53 bool _OpenCodecIfNeeded(); 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 status_t _EncodeVideoFrame(AVFrame* frame, 66 AVPacket* pkt, 67 media_encode_info* info); 68 69 private: 70 media_format fInputFormat; 71 encode_parameters fEncodeParameters; 72 int fBitRateScale; 73 74 // FFmpeg related members 75 // TODO: Refactor common base class from AVCodec[De|En]Coder! 76 CodecID fCodecID; 77 AVCodec* fCodec; 78 AVCodecContext* fCodecContext; 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