1 // Copyright (c) 2016 The WebM project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS.  All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 #ifndef LIBWEBM_COMMON_VIDEO_FRAME_H_
9 #define LIBWEBM_COMMON_VIDEO_FRAME_H_
10 
11 #include <cstdint>
12 #include <memory>
13 
14 namespace libwebm {
15 
16 // VideoFrame is a storage class for compressed video frames.
17 class VideoFrame {
18  public:
19   enum Codec { kVP8, kVP9 };
20   struct Buffer {
21     Buffer() = default;
22     ~Buffer() = default;
23 
24     // Resets |data| to be of size |new_length| bytes, sets |capacity| to
25     // |new_length|, sets |length| to 0 (aka empty). Returns true for success.
26     bool Init(std::size_t new_length);
27 
28     std::unique_ptr<std::uint8_t[]> data;
29     std::size_t length = 0;
30     std::size_t capacity = 0;
31   };
32 
33   VideoFrame() = default;
34   ~VideoFrame() = default;
VideoFrame(std::int64_t pts_in_nanoseconds,Codec vpx_codec)35   VideoFrame(std::int64_t pts_in_nanoseconds, Codec vpx_codec)
36       : nanosecond_pts_(pts_in_nanoseconds), codec_(vpx_codec) {}
VideoFrame(bool keyframe,std::int64_t pts_in_nanoseconds,Codec vpx_codec)37   VideoFrame(bool keyframe, std::int64_t pts_in_nanoseconds, Codec vpx_codec)
38       : keyframe_(keyframe),
39         nanosecond_pts_(pts_in_nanoseconds),
40         codec_(vpx_codec) {}
41   bool Init(std::size_t length);
42   bool Init(std::size_t length, std::int64_t nano_pts, Codec codec);
43 
44   // Updates actual length of data stored in |buffer_.data| when it's been
45   // written via the raw pointer returned from buffer_.data.get().
46   // Returns false when buffer_.data.get() return nullptr and/or when
47   // |length| > |buffer_.length|. Returns true otherwise.
48   bool SetBufferLength(std::size_t length);
49 
50   // Accessors.
buffer()51   const Buffer& buffer() const { return buffer_; }
keyframe()52   bool keyframe() const { return keyframe_; }
nanosecond_pts()53   std::int64_t nanosecond_pts() const { return nanosecond_pts_; }
codec()54   Codec codec() const { return codec_; }
55 
56   // Mutators.
set_nanosecond_pts(std::int64_t nano_pts)57   void set_nanosecond_pts(std::int64_t nano_pts) { nanosecond_pts_ = nano_pts; }
58 
59  private:
60   Buffer buffer_;
61   bool keyframe_ = false;
62   std::int64_t nanosecond_pts_ = 0;
63   Codec codec_ = kVP9;
64 };
65 
66 }  // namespace libwebm
67 
68 #endif  // LIBWEBM_COMMON_VIDEO_FRAME_H_