michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef VP8TrackEncoder_h_ michael@0: #define VP8TrackEncoder_h_ michael@0: michael@0: #include "TrackEncoder.h" michael@0: #include "vpx/vpx_codec.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: typedef struct vpx_codec_ctx vpx_codec_ctx_t; michael@0: typedef struct vpx_codec_enc_cfg vpx_codec_enc_cfg_t; michael@0: typedef struct vpx_image vpx_image_t; michael@0: michael@0: /** michael@0: * VP8TrackEncoder implements VideoTrackEncoder by using libvpx library. michael@0: * We implement a realtime and fixed FPS encoder. In order to achieve that, michael@0: * there is a pick target frame and drop frame encoding policy implemented in michael@0: * GetEncodedTrack. michael@0: */ michael@0: class VP8TrackEncoder : public VideoTrackEncoder michael@0: { michael@0: enum EncodeOperation { michael@0: ENCODE_NORMAL_FRAME, // VP8 track encoder works normally. michael@0: ENCODE_I_FRAME, // The next frame will be encoded as I-Frame. michael@0: SKIP_FRAME, // Skip the next frame. michael@0: }; michael@0: public: michael@0: VP8TrackEncoder(); michael@0: virtual ~VP8TrackEncoder(); michael@0: michael@0: already_AddRefed GetMetadata() MOZ_FINAL MOZ_OVERRIDE; michael@0: michael@0: nsresult GetEncodedTrack(EncodedFrameContainer& aData) MOZ_FINAL MOZ_OVERRIDE; michael@0: michael@0: protected: michael@0: nsresult Init(int32_t aWidth, int32_t aHeight, michael@0: int32_t aDisplayWidth, int32_t aDisplayHeight, michael@0: TrackRate aTrackRate) MOZ_FINAL MOZ_OVERRIDE; michael@0: michael@0: private: michael@0: // Calculate the target frame's encoded duration. michael@0: TrackTicks CalculateEncodedDuration(TrackTicks aDurationCopied); michael@0: michael@0: // Calculate the mRemainingTicks for next target frame. michael@0: TrackTicks CalculateRemainingTicks(TrackTicks aDurationCopied, michael@0: TrackTicks aEncodedDuration); michael@0: michael@0: // Get the EncodeOperation for next target frame. michael@0: EncodeOperation GetNextEncodeOperation(TimeDuration aTimeElapsed, michael@0: TrackTicks aProcessedDuration); michael@0: michael@0: // Get the encoded data from encoder to aData. michael@0: nsresult GetEncodedPartitions(EncodedFrameContainer& aData); michael@0: michael@0: // Prepare the input data to the mVPXImageWrapper for encoding. michael@0: nsresult PrepareRawFrame(VideoChunk &aChunk); michael@0: michael@0: // Prepare the muted frame data to the mVPXImageWrapper for encoding. michael@0: void PrepareMutedFrame(); michael@0: michael@0: // Output frame rate. michael@0: uint32_t mEncodedFrameRate; michael@0: // Duration for the output frame, reciprocal to mEncodedFrameRate. michael@0: TrackTicks mEncodedFrameDuration; michael@0: // Encoded timestamp. michael@0: TrackTicks mEncodedTimestamp; michael@0: // Duration to the next encode frame. michael@0: TrackTicks mRemainingTicks; michael@0: michael@0: // Muted frame, we only create it once. michael@0: nsTArray mMuteFrame; michael@0: michael@0: // I420 frame, convert the 4:4:4, 4:2:2 to I420. michael@0: nsTArray mI420Frame; michael@0: michael@0: /** michael@0: * A local segment queue which takes the raw data out from mRawSegment in the michael@0: * call of GetEncodedTrack(). Since we implement the fixed FPS encoding michael@0: * policy, it needs to be global in order to store the leftover segments michael@0: * taken from mRawSegment. michael@0: */ michael@0: VideoSegment mSourceSegment; michael@0: michael@0: // VP8 relative members. michael@0: // Codec context structure. michael@0: nsAutoPtr mVPXContext; michael@0: // Image Descriptor. michael@0: nsAutoPtr mVPXImageWrapper; michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif