1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/encoder/VP8TrackEncoder.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,97 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef VP8TrackEncoder_h_ 1.10 +#define VP8TrackEncoder_h_ 1.11 + 1.12 +#include "TrackEncoder.h" 1.13 +#include "vpx/vpx_codec.h" 1.14 + 1.15 +namespace mozilla { 1.16 + 1.17 +typedef struct vpx_codec_ctx vpx_codec_ctx_t; 1.18 +typedef struct vpx_codec_enc_cfg vpx_codec_enc_cfg_t; 1.19 +typedef struct vpx_image vpx_image_t; 1.20 + 1.21 +/** 1.22 + * VP8TrackEncoder implements VideoTrackEncoder by using libvpx library. 1.23 + * We implement a realtime and fixed FPS encoder. In order to achieve that, 1.24 + * there is a pick target frame and drop frame encoding policy implemented in 1.25 + * GetEncodedTrack. 1.26 + */ 1.27 +class VP8TrackEncoder : public VideoTrackEncoder 1.28 +{ 1.29 + enum EncodeOperation { 1.30 + ENCODE_NORMAL_FRAME, // VP8 track encoder works normally. 1.31 + ENCODE_I_FRAME, // The next frame will be encoded as I-Frame. 1.32 + SKIP_FRAME, // Skip the next frame. 1.33 + }; 1.34 +public: 1.35 + VP8TrackEncoder(); 1.36 + virtual ~VP8TrackEncoder(); 1.37 + 1.38 + already_AddRefed<TrackMetadataBase> GetMetadata() MOZ_FINAL MOZ_OVERRIDE; 1.39 + 1.40 + nsresult GetEncodedTrack(EncodedFrameContainer& aData) MOZ_FINAL MOZ_OVERRIDE; 1.41 + 1.42 +protected: 1.43 + nsresult Init(int32_t aWidth, int32_t aHeight, 1.44 + int32_t aDisplayWidth, int32_t aDisplayHeight, 1.45 + TrackRate aTrackRate) MOZ_FINAL MOZ_OVERRIDE; 1.46 + 1.47 +private: 1.48 + // Calculate the target frame's encoded duration. 1.49 + TrackTicks CalculateEncodedDuration(TrackTicks aDurationCopied); 1.50 + 1.51 + // Calculate the mRemainingTicks for next target frame. 1.52 + TrackTicks CalculateRemainingTicks(TrackTicks aDurationCopied, 1.53 + TrackTicks aEncodedDuration); 1.54 + 1.55 + // Get the EncodeOperation for next target frame. 1.56 + EncodeOperation GetNextEncodeOperation(TimeDuration aTimeElapsed, 1.57 + TrackTicks aProcessedDuration); 1.58 + 1.59 + // Get the encoded data from encoder to aData. 1.60 + nsresult GetEncodedPartitions(EncodedFrameContainer& aData); 1.61 + 1.62 + // Prepare the input data to the mVPXImageWrapper for encoding. 1.63 + nsresult PrepareRawFrame(VideoChunk &aChunk); 1.64 + 1.65 + // Prepare the muted frame data to the mVPXImageWrapper for encoding. 1.66 + void PrepareMutedFrame(); 1.67 + 1.68 + // Output frame rate. 1.69 + uint32_t mEncodedFrameRate; 1.70 + // Duration for the output frame, reciprocal to mEncodedFrameRate. 1.71 + TrackTicks mEncodedFrameDuration; 1.72 + // Encoded timestamp. 1.73 + TrackTicks mEncodedTimestamp; 1.74 + // Duration to the next encode frame. 1.75 + TrackTicks mRemainingTicks; 1.76 + 1.77 + // Muted frame, we only create it once. 1.78 + nsTArray<uint8_t> mMuteFrame; 1.79 + 1.80 + // I420 frame, convert the 4:4:4, 4:2:2 to I420. 1.81 + nsTArray<uint8_t> mI420Frame; 1.82 + 1.83 + /** 1.84 + * A local segment queue which takes the raw data out from mRawSegment in the 1.85 + * call of GetEncodedTrack(). Since we implement the fixed FPS encoding 1.86 + * policy, it needs to be global in order to store the leftover segments 1.87 + * taken from mRawSegment. 1.88 + */ 1.89 + VideoSegment mSourceSegment; 1.90 + 1.91 + // VP8 relative members. 1.92 + // Codec context structure. 1.93 + nsAutoPtr<vpx_codec_ctx_t> mVPXContext; 1.94 + // Image Descriptor. 1.95 + nsAutoPtr<vpx_image_t> mVPXImageWrapper; 1.96 +}; 1.97 + 1.98 +} // namespace mozilla 1.99 + 1.100 +#endif