|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef VP8TrackEncoder_h_ |
|
7 #define VP8TrackEncoder_h_ |
|
8 |
|
9 #include "TrackEncoder.h" |
|
10 #include "vpx/vpx_codec.h" |
|
11 |
|
12 namespace mozilla { |
|
13 |
|
14 typedef struct vpx_codec_ctx vpx_codec_ctx_t; |
|
15 typedef struct vpx_codec_enc_cfg vpx_codec_enc_cfg_t; |
|
16 typedef struct vpx_image vpx_image_t; |
|
17 |
|
18 /** |
|
19 * VP8TrackEncoder implements VideoTrackEncoder by using libvpx library. |
|
20 * We implement a realtime and fixed FPS encoder. In order to achieve that, |
|
21 * there is a pick target frame and drop frame encoding policy implemented in |
|
22 * GetEncodedTrack. |
|
23 */ |
|
24 class VP8TrackEncoder : public VideoTrackEncoder |
|
25 { |
|
26 enum EncodeOperation { |
|
27 ENCODE_NORMAL_FRAME, // VP8 track encoder works normally. |
|
28 ENCODE_I_FRAME, // The next frame will be encoded as I-Frame. |
|
29 SKIP_FRAME, // Skip the next frame. |
|
30 }; |
|
31 public: |
|
32 VP8TrackEncoder(); |
|
33 virtual ~VP8TrackEncoder(); |
|
34 |
|
35 already_AddRefed<TrackMetadataBase> GetMetadata() MOZ_FINAL MOZ_OVERRIDE; |
|
36 |
|
37 nsresult GetEncodedTrack(EncodedFrameContainer& aData) MOZ_FINAL MOZ_OVERRIDE; |
|
38 |
|
39 protected: |
|
40 nsresult Init(int32_t aWidth, int32_t aHeight, |
|
41 int32_t aDisplayWidth, int32_t aDisplayHeight, |
|
42 TrackRate aTrackRate) MOZ_FINAL MOZ_OVERRIDE; |
|
43 |
|
44 private: |
|
45 // Calculate the target frame's encoded duration. |
|
46 TrackTicks CalculateEncodedDuration(TrackTicks aDurationCopied); |
|
47 |
|
48 // Calculate the mRemainingTicks for next target frame. |
|
49 TrackTicks CalculateRemainingTicks(TrackTicks aDurationCopied, |
|
50 TrackTicks aEncodedDuration); |
|
51 |
|
52 // Get the EncodeOperation for next target frame. |
|
53 EncodeOperation GetNextEncodeOperation(TimeDuration aTimeElapsed, |
|
54 TrackTicks aProcessedDuration); |
|
55 |
|
56 // Get the encoded data from encoder to aData. |
|
57 nsresult GetEncodedPartitions(EncodedFrameContainer& aData); |
|
58 |
|
59 // Prepare the input data to the mVPXImageWrapper for encoding. |
|
60 nsresult PrepareRawFrame(VideoChunk &aChunk); |
|
61 |
|
62 // Prepare the muted frame data to the mVPXImageWrapper for encoding. |
|
63 void PrepareMutedFrame(); |
|
64 |
|
65 // Output frame rate. |
|
66 uint32_t mEncodedFrameRate; |
|
67 // Duration for the output frame, reciprocal to mEncodedFrameRate. |
|
68 TrackTicks mEncodedFrameDuration; |
|
69 // Encoded timestamp. |
|
70 TrackTicks mEncodedTimestamp; |
|
71 // Duration to the next encode frame. |
|
72 TrackTicks mRemainingTicks; |
|
73 |
|
74 // Muted frame, we only create it once. |
|
75 nsTArray<uint8_t> mMuteFrame; |
|
76 |
|
77 // I420 frame, convert the 4:4:4, 4:2:2 to I420. |
|
78 nsTArray<uint8_t> mI420Frame; |
|
79 |
|
80 /** |
|
81 * A local segment queue which takes the raw data out from mRawSegment in the |
|
82 * call of GetEncodedTrack(). Since we implement the fixed FPS encoding |
|
83 * policy, it needs to be global in order to store the leftover segments |
|
84 * taken from mRawSegment. |
|
85 */ |
|
86 VideoSegment mSourceSegment; |
|
87 |
|
88 // VP8 relative members. |
|
89 // Codec context structure. |
|
90 nsAutoPtr<vpx_codec_ctx_t> mVPXContext; |
|
91 // Image Descriptor. |
|
92 nsAutoPtr<vpx_image_t> mVPXImageWrapper; |
|
93 }; |
|
94 |
|
95 } // namespace mozilla |
|
96 |
|
97 #endif |