1 /*
2  *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  *
10  */
11 
12 #ifndef MODULES_VIDEO_CODING_CODECS_H264_H264_ENCODER_IMPL_H_
13 #define MODULES_VIDEO_CODING_CODECS_H264_H264_ENCODER_IMPL_H_
14 
15 #include <memory>
16 #include <vector>
17 
18 #include "common_video/h264/h264_bitstream_parser.h"
19 #include "modules/video_coding/codecs/h264/include/h264.h"
20 #include "modules/video_coding/utility/quality_scaler.h"
21 
22 #include "third_party/openh264/src/codec/api/svc/codec_app_def.h"
23 
24 class ISVCEncoder;
25 
26 namespace webrtc {
27 
28 class H264EncoderImpl : public H264Encoder {
29  public:
30   explicit H264EncoderImpl(const cricket::VideoCodec& codec);
31   ~H264EncoderImpl() override;
32 
33   // |max_payload_size| is ignored.
34   // The following members of |codec_settings| are used. The rest are ignored.
35   // - codecType (must be kVideoCodecH264)
36   // - targetBitrate
37   // - maxFramerate
38   // - width
39   // - height
40   int32_t InitEncode(const VideoCodec* codec_settings,
41                      int32_t number_of_cores,
42                      size_t max_payload_size) override;
43   int32_t Release() override;
44 
45   int32_t RegisterEncodeCompleteCallback(
46       EncodedImageCallback* callback) override;
47   int32_t SetRateAllocation(const BitrateAllocation& bitrate_allocation,
48                             uint32_t framerate) override;
49 
50   // The result of encoding - an EncodedImage and RTPFragmentationHeader - are
51   // passed to the encode complete callback.
52   int32_t Encode(const VideoFrame& frame,
53                  const CodecSpecificInfo* codec_specific_info,
54                  const std::vector<FrameType>* frame_types) override;
55 
56   const char* ImplementationName() const override;
57 
58   VideoEncoder::ScalingSettings GetScalingSettings() const override;
59 
60   // Unsupported / Do nothing.
61   int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) override;
62   int32_t SetPeriodicKeyFrames(bool enable) override;
63 
64   // Exposed for testing.
PacketizationModeForTesting()65   H264PacketizationMode PacketizationModeForTesting() const {
66     return packetization_mode_;
67   }
68 
69  private:
70   bool IsInitialized() const;
71   SEncParamExt CreateEncoderParams() const;
72 
73   webrtc::H264BitstreamParser h264_bitstream_parser_;
74   // Reports statistics with histograms.
75   void ReportInit();
76   void ReportError();
77 
78   ISVCEncoder* openh264_encoder_;
79   // Settings that are used by this encoder.
80   int width_;
81   int height_;
82   float max_frame_rate_;
83   uint32_t target_bps_;
84   uint32_t max_bps_;
85   VideoCodecMode mode_;
86   // H.264 specifc parameters
87   bool frame_dropping_on_;
88   int key_frame_interval_;
89   H264PacketizationMode packetization_mode_;
90 
91   size_t max_payload_size_;
92   int32_t number_of_cores_;
93 
94   EncodedImage encoded_image_;
95   std::unique_ptr<uint8_t[]> encoded_image_buffer_;
96   EncodedImageCallback* encoded_image_callback_;
97 
98   bool has_reported_init_;
99   bool has_reported_error_;
100 };
101 
102 }  // namespace webrtc
103 
104 #endif  // MODULES_VIDEO_CODING_CODECS_H264_H264_ENCODER_IMPL_H_
105