Home | History | Annotate | Download | only in webrtc
      1 /*
      2  *  Copyright (c) 2014 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 #ifndef WEBRTC_VIDEO_FRAME_H_
     12 #define WEBRTC_VIDEO_FRAME_H_
     13 
     14 #include <assert.h>
     15 
     16 #include "webrtc/common_video/plane.h"
     17 // TODO(pbos): Remove scoped_refptr include (and AddRef/Release if they're not
     18 // used).
     19 #include "webrtc/system_wrappers/interface/scoped_refptr.h"
     20 #include "webrtc/typedefs.h"
     21 
     22 namespace webrtc {
     23 
     24 enum PlaneType {
     25   kYPlane = 0,
     26   kUPlane = 1,
     27   kVPlane = 2,
     28   kNumOfPlanes = 3
     29 };
     30 
     31 class I420VideoFrame {
     32  public:
     33   I420VideoFrame();
     34   virtual ~I420VideoFrame();
     35   // Infrastructure for refCount implementation.
     36   // Implements dummy functions for reference counting so that non reference
     37   // counted instantiation can be done. These functions should not be called
     38   // when creating the frame with new I420VideoFrame().
     39   // Note: do not pass a I420VideoFrame created with new I420VideoFrame() or
     40   // equivalent to a scoped_refptr or memory leak will occur.
     41   virtual int32_t AddRef() {
     42     assert(false);
     43     return -1;
     44   }
     45   virtual int32_t Release() {
     46     assert(false);
     47     return -1;
     48   }
     49 
     50   // CreateEmptyFrame: Sets frame dimensions and allocates buffers based
     51   // on set dimensions - height and plane stride.
     52   // If required size is bigger than the allocated one, new buffers of adequate
     53   // size will be allocated.
     54   // Return value: 0 on success, -1 on error.
     55   virtual int CreateEmptyFrame(int width,
     56                                int height,
     57                                int stride_y,
     58                                int stride_u,
     59                                int stride_v);
     60 
     61   // CreateFrame: Sets the frame's members and buffers. If required size is
     62   // bigger than allocated one, new buffers of adequate size will be allocated.
     63   // Return value: 0 on success, -1 on error.
     64   virtual int CreateFrame(int size_y,
     65                           const uint8_t* buffer_y,
     66                           int size_u,
     67                           const uint8_t* buffer_u,
     68                           int size_v,
     69                           const uint8_t* buffer_v,
     70                           int width,
     71                           int height,
     72                           int stride_y,
     73                           int stride_u,
     74                           int stride_v);
     75 
     76   // Copy frame: If required size is bigger than allocated one, new buffers of
     77   // adequate size will be allocated.
     78   // Return value: 0 on success, -1 on error.
     79   virtual int CopyFrame(const I420VideoFrame& videoFrame);
     80 
     81   // Make a copy of |this|. The caller owns the returned frame.
     82   // Return value: a new frame on success, NULL on error.
     83   virtual I420VideoFrame* CloneFrame() const;
     84 
     85   // Swap Frame.
     86   virtual void SwapFrame(I420VideoFrame* videoFrame);
     87 
     88   // Get pointer to buffer per plane.
     89   virtual uint8_t* buffer(PlaneType type);
     90   // Overloading with const.
     91   virtual const uint8_t* buffer(PlaneType type) const;
     92 
     93   // Get allocated size per plane.
     94   virtual int allocated_size(PlaneType type) const;
     95 
     96   // Get allocated stride per plane.
     97   virtual int stride(PlaneType type) const;
     98 
     99   // Set frame width.
    100   virtual int set_width(int width);
    101 
    102   // Set frame height.
    103   virtual int set_height(int height);
    104 
    105   // Get frame width.
    106   virtual int width() const { return width_; }
    107 
    108   // Get frame height.
    109   virtual int height() const { return height_; }
    110 
    111   // Set frame timestamp (90kHz).
    112   virtual void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; }
    113 
    114   // Get frame timestamp (90kHz).
    115   virtual uint32_t timestamp() const { return timestamp_; }
    116 
    117   // Set capture ntp time in miliseconds.
    118   virtual void set_ntp_time_ms(int64_t ntp_time_ms) {
    119     ntp_time_ms_ = ntp_time_ms;
    120   }
    121 
    122   // Get capture ntp time in miliseconds.
    123   virtual int64_t ntp_time_ms() const { return ntp_time_ms_; }
    124 
    125   // Set render time in miliseconds.
    126   virtual void set_render_time_ms(int64_t render_time_ms) {
    127     render_time_ms_ = render_time_ms;
    128   }
    129 
    130   // Get render time in miliseconds.
    131   virtual int64_t render_time_ms() const { return render_time_ms_; }
    132 
    133   // Return true if underlying plane buffers are of zero size, false if not.
    134   virtual bool IsZeroSize() const;
    135 
    136   // Reset underlying plane buffers sizes to 0. This function doesn't
    137   // clear memory.
    138   virtual void ResetSize();
    139 
    140   // Return the handle of the underlying video frame. This is used when the
    141   // frame is backed by a texture. The object should be destroyed when it is no
    142   // longer in use, so the underlying resource can be freed.
    143   virtual void* native_handle() const;
    144 
    145  protected:
    146   // Verifies legality of parameters.
    147   // Return value: 0 on success, -1 on error.
    148   virtual int CheckDimensions(int width,
    149                               int height,
    150                               int stride_y,
    151                               int stride_u,
    152                               int stride_v);
    153 
    154  private:
    155   // Get the pointer to a specific plane.
    156   const Plane* GetPlane(PlaneType type) const;
    157   // Overloading with non-const.
    158   Plane* GetPlane(PlaneType type);
    159 
    160   Plane y_plane_;
    161   Plane u_plane_;
    162   Plane v_plane_;
    163   int width_;
    164   int height_;
    165   uint32_t timestamp_;
    166   int64_t ntp_time_ms_;
    167   int64_t render_time_ms_;
    168 };
    169 
    170 enum VideoFrameType {
    171   kKeyFrame = 0,
    172   kDeltaFrame = 1,
    173   kGoldenFrame = 2,
    174   kAltRefFrame = 3,
    175   kSkipFrame = 4
    176 };
    177 
    178 // TODO(pbos): Rename EncodedFrame and reformat this class' members.
    179 class EncodedImage {
    180  public:
    181   EncodedImage()
    182       : _encodedWidth(0),
    183         _encodedHeight(0),
    184         _timeStamp(0),
    185         capture_time_ms_(0),
    186         _frameType(kDeltaFrame),
    187         _buffer(NULL),
    188         _length(0),
    189         _size(0),
    190         _completeFrame(false) {}
    191 
    192   EncodedImage(uint8_t* buffer, uint32_t length, uint32_t size)
    193       : _encodedWidth(0),
    194         _encodedHeight(0),
    195         _timeStamp(0),
    196         ntp_time_ms_(0),
    197         capture_time_ms_(0),
    198         _frameType(kDeltaFrame),
    199         _buffer(buffer),
    200         _length(length),
    201         _size(size),
    202         _completeFrame(false) {}
    203 
    204   uint32_t _encodedWidth;
    205   uint32_t _encodedHeight;
    206   uint32_t _timeStamp;
    207   // NTP time of the capture time in local timebase in milliseconds.
    208   int64_t ntp_time_ms_;
    209   int64_t capture_time_ms_;
    210   VideoFrameType _frameType;
    211   uint8_t* _buffer;
    212   uint32_t _length;
    213   uint32_t _size;
    214   bool _completeFrame;
    215 };
    216 
    217 }  // namespace webrtc
    218 #endif  // WEBRTC_VIDEO_FRAME_H_
    219 
    220