video_encoder.h revision f31f56d8d48dbc3c981d445891e83a68dc9d8f6a
1ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org/*
2ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org *
4ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org *  Use of this source code is governed by a BSD-style license
5ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org *  that can be found in the LICENSE file in the root of the source
6ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org *  tree. An additional intellectual property rights grant can be found
7ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org *  in the file PATENTS.  All contributing project authors may
8ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org */
10ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org
11ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org#ifndef WEBRTC_VIDEO_ENCODER_H_
12ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org#define WEBRTC_VIDEO_ENCODER_H_
13ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org
14ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org#include <vector>
15ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org
166cd6ba8ae016200a7a13b43294b8faf5d1d4affdpbos@webrtc.org#include "webrtc/common_types.h"
17ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org#include "webrtc/typedefs.h"
18ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org#include "webrtc/video_frame.h"
19ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org
20ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.orgnamespace webrtc {
21ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org
22ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.orgclass RTPFragmentationHeader;
23ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org// TODO(pbos): Expose these through a public (root) header or change these APIs.
24ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.orgstruct CodecSpecificInfo;
25ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.orgstruct VideoCodec;
26ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org
27ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.orgclass EncodedImageCallback {
28ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org public:
29ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org  virtual ~EncodedImageCallback() {}
30ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org
31ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org  // Callback function which is called when an image has been encoded.
32f31f56d8d48dbc3c981d445891e83a68dc9d8f6achangbin.shao@webrtc.org  virtual int32_t Encoded(const EncodedImage& encoded_image,
33f31f56d8d48dbc3c981d445891e83a68dc9d8f6achangbin.shao@webrtc.org                          const CodecSpecificInfo* codec_specific_info,
34f31f56d8d48dbc3c981d445891e83a68dc9d8f6achangbin.shao@webrtc.org                          const RTPFragmentationHeader* fragmentation) = 0;
35ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org};
36ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org
37ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.orgclass VideoEncoder {
38ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org public:
39ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org  enum EncoderType {
40ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org    kVp8,
415b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org    kVp9,
42ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org  };
43ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org
44ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org  static VideoEncoder* Create(EncoderType codec_type);
45ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org
466cd6ba8ae016200a7a13b43294b8faf5d1d4affdpbos@webrtc.org  static VideoCodecVP8 GetDefaultVp8Settings();
475b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  static VideoCodecVP9 GetDefaultVp9Settings();
486cd6ba8ae016200a7a13b43294b8faf5d1d4affdpbos@webrtc.org  static VideoCodecH264 GetDefaultH264Settings();
496cd6ba8ae016200a7a13b43294b8faf5d1d4affdpbos@webrtc.org
50ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org  virtual ~VideoEncoder() {}
51ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org
525b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // Initialize the encoder with the information from the codecSettings
535b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //
545b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // Input:
555b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //          - codec_settings    : Codec settings
565b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //          - number_of_cores   : Number of cores available for the encoder
575b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //          - max_payload_size  : The maximum size each payload is allowed
585b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //                                to have. Usually MTU - overhead.
595b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //
605b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // Return value                  : Set bit rate if OK
615b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //                                 <0 - Errors:
625b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //                                  WEBRTC_VIDEO_CODEC_ERR_PARAMETER
635b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //                                  WEBRTC_VIDEO_CODEC_ERR_SIZE
645b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //                                  WEBRTC_VIDEO_CODEC_LEVEL_EXCEEDED
655b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //                                  WEBRTC_VIDEO_CODEC_MEMORY
665b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //                                  WEBRTC_VIDEO_CODEC_ERROR
67ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org  virtual int32_t InitEncode(const VideoCodec* codec_settings,
68ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org                             int32_t number_of_cores,
694591fbd09f9cb6e83433c49a12dd8524c2806502pkasting@chromium.org                             size_t max_payload_size) = 0;
705b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org
715b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // Register an encode complete callback object.
725b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //
735b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // Input:
745b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //          - callback         : Callback object which handles encoded images.
755b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //
765b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // Return value                : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
77ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org  virtual int32_t RegisterEncodeCompleteCallback(
78ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org      EncodedImageCallback* callback) = 0;
79ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org
805b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // Free encoder memory.
815b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // Return value                : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
825b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  virtual int32_t Release() = 0;
83b1dac33cac5a64cbec6b0fd72624fa9d3060376chenrike@webrtc.org
845b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // Encode an I420 image (as a part of a video stream). The encoded image
855b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // will be returned to the user through the encode complete callback.
865b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //
875b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // Input:
885b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //          - frame             : Image to be encoded
895b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //          - frame_types       : Frame type to be generated by the encoder.
905b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //
915b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // Return value                 : WEBRTC_VIDEO_CODEC_OK if OK
925b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //                                <0 - Errors:
935b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //                                  WEBRTC_VIDEO_CODEC_ERR_PARAMETER
945b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //                                  WEBRTC_VIDEO_CODEC_MEMORY
955b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //                                  WEBRTC_VIDEO_CODEC_ERROR
965b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //                                  WEBRTC_VIDEO_CODEC_TIMEOUT
97ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org  virtual int32_t Encode(const I420VideoFrame& frame,
98ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org                         const CodecSpecificInfo* codec_specific_info,
99ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org                         const std::vector<VideoFrameType>* frame_types) = 0;
100ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org
1015b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // Inform the encoder of the new packet loss rate and the round-trip time of
1025b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // the network.
1035b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //
1045b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // Input:
1055b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //          - packet_loss : Fraction lost
1065b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //                          (loss rate in percent = 100 * packetLoss / 255)
1075b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //          - rtt         : Round-trip time in milliseconds
1085b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // Return value           : WEBRTC_VIDEO_CODEC_OK if OK
1095b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //                          <0 - Errors: WEBRTC_VIDEO_CODEC_ERROR
11016825b1a828bb4ff40f7682040e43a239b7b8ca3pkasting@chromium.org  virtual int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) = 0;
1115b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org
1125b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // Inform the encoder about the new target bit rate.
1135b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //
1145b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // Input:
1155b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //          - bitrate         : New target bit rate
1165b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //          - framerate       : The target frame rate
1175b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  //
1185b8831782074d490969171de5f8c67251f36d9ccmarpan@webrtc.org  // Return value                : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
119ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org  virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate) = 0;
120ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org
121ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org  virtual int32_t SetPeriodicKeyFrames(bool enable) { return -1; }
122ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org  virtual int32_t CodecConfigParameters(uint8_t* /*buffer*/, int32_t /*size*/) {
123ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org    return -1;
124ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org  }
125ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org};
126ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org
127ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org}  // namespace webrtc
128ab990ae43a2b84b103cb3c50bc38502375c13e68pbos@webrtc.org#endif  // WEBRTC_VIDEO_ENCODER_H_
129