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