Shaka Player Embedded
frames.cc
Go to the documentation of this file.
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "shaka/media/frames.h"
16 
17 #include <glog/logging.h>
18 
19 namespace shaka {
20 namespace media {
21 
22 std::ostream& operator<<(std::ostream& os, PixelFormat format) {
23  switch (format) {
24 #define CASE(F) \
25  case PixelFormat::F: \
26  return os << #F
27  CASE(YUV420P);
28  CASE(NV12);
29  CASE(RGB24);
31 #undef CASE
32 
33  default:
34  if (format >= PixelFormat::AppFormat1)
35  return os << "AppFormat(" << static_cast<int>(format) << ")";
36  else
37  return os << "Unknown(" << static_cast<int>(format) << ")";
38  }
39 }
40 
41 std::ostream& operator<<(std::ostream& os, SampleFormat format) {
42  switch (format) {
43 #define CASE(F) \
44  case SampleFormat::F: \
45  return os << #F
46  CASE(PackedU8);
47  CASE(PackedS16);
48  CASE(PackedS32);
49  CASE(PackedS64);
52  CASE(PlanarU8);
53  CASE(PlanarS16);
54  CASE(PlanarS32);
55  CASE(PlanarS64);
58 #undef CASE
59 
60  default:
61  if (format >= SampleFormat::AppFormat1)
62  return os << "AppFormat(" << static_cast<int>(format) << ")";
63  else
64  return os << "Unknown(" << static_cast<int>(format) << ")";
65  }
66 }
67 
69  if (holds_alternative<PixelFormat>(format)) {
70  switch (get<PixelFormat>(format)) {
72  case PixelFormat::NV12:
73  return true;
74  default:
75  return false;
76  }
77  } else {
78  switch (get<SampleFormat>(format)) {
85  return true;
86  default:
87  return false;
88  }
89  }
90 }
91 
93  size_t channels) {
94  if (holds_alternative<PixelFormat>(format)) {
95  switch (get<PixelFormat>(format)) {
97  return 3;
98  case PixelFormat::NV12:
99  return 2;
100  case PixelFormat::RGB24:
102  return 1;
103 
104  default:
105  return 0;
106  }
107  } else {
108  return IsPlanarFormat(format) ? channels : 1;
109  }
110 }
111 
112 
114 
115 BaseFrame::BaseFrame(std::shared_ptr<const StreamInfo> stream_info, double pts,
116  double dts, double duration, bool is_key_frame)
117  : stream_info(stream_info),
118  pts(pts),
119  dts(dts),
120  duration(duration),
121  is_key_frame(is_key_frame) {}
123 
124 size_t BaseFrame::EstimateSize() const {
125  return sizeof(*this) + sizeof(Impl);
126 }
127 
128 
130 
132  std::shared_ptr<const StreamInfo> stream, double pts, double dts,
133  double duration, bool is_key_frame, const uint8_t* data, size_t data_size,
134  double timestamp_offset,
135  std::shared_ptr<eme::FrameEncryptionInfo> encryption_info)
136  : BaseFrame(stream, pts, dts, duration, is_key_frame),
137  data(data),
138  data_size(data_size),
139  timestamp_offset(timestamp_offset),
140  encryption_info(encryption_info) {}
142 
144  uint8_t* dest) const {
145  if (!encryption_info)
146  return MediaStatus::Success;
147  if (!implementation)
149 
150  const eme::DecryptStatus decrypt_status =
151  implementation->Decrypt(encryption_info.get(), data, data_size, dest);
152  switch (decrypt_status) {
154  return MediaStatus::Success;
157  default:
159  }
160 }
161 
163  // BaseFrame::EstimateSize includes sizeof(BaseFrame) and so does
164  // sizeof(this), so we need to remove the extra.
165  return BaseFrame::EstimateSize() + sizeof(*this) - sizeof(BaseFrame) +
166  data_size + sizeof(Impl);
167 }
168 
169 
171 
172 DecodedFrame::DecodedFrame(std::shared_ptr<const StreamInfo> stream, double pts,
173  double dts, double duration,
175  size_t sample_count,
176  const std::vector<const uint8_t*>& data,
177  const std::vector<size_t>& linesize)
178  : BaseFrame(stream, pts, dts, duration, true),
179  sample_count(sample_count),
180  data(data),
181  linesize(linesize),
182  format(format) {}
184 
186  // BaseFrame::EstimateSize includes sizeof(BaseFrame) and so does
187  // sizeof(this), so we need to remove the extra.
188  size_t ret = BaseFrame::EstimateSize() + sizeof(*this) - sizeof(BaseFrame) +
189  sizeof(Impl);
190  for (size_t line : linesize) {
191  if (holds_alternative<PixelFormat>(format))
192  ret += line * stream_info->height;
193  else
194  ret += line;
195  }
196  return ret;
197 }
198 
199 } // namespace media
200 } // namespace shaka
virtual MediaStatus Decrypt(const eme::Implementation *implementation, uint8_t *dest) const
Definition: frames.cc:143
const char * dest
Definition: media_utils.cc:31
EncodedFrame(std::shared_ptr< const StreamInfo > stream, double pts, double dts, double duration, bool is_key_frame, const uint8_t *data, size_t data_size, double timestamp_offset, std::shared_ptr< eme::FrameEncryptionInfo > encryption_info)
Definition: frames.cc:131
const bool is_key_frame
Definition: frames.h:211
const size_t data_size
Definition: frames.h:247
~DecodedFrame() override
Definition: frames.cc:183
DecodedFrame(std::shared_ptr< const StreamInfo > stream_info, double pts, double dts, double duration, variant< PixelFormat, SampleFormat > format, size_t sample_count, const std::vector< const uint8_t *> &data, const std::vector< size_t > &linesize)
Definition: frames.cc:172
const std::vector< size_t > linesize
Definition: frames.h:322
const std::shared_ptr< eme::FrameEncryptionInfo > encryption_info
Definition: frames.h:259
const std::shared_ptr< const StreamInfo > stream_info
Definition: frames.h:199
std::ostream & operator<<(std::ostream &os, PixelFormat format)
Definition: frames.cc:22
#define CASE(F)
const variant< PixelFormat, SampleFormat > format
Definition: frames.h:325
bool IsPlanarFormat(variant< PixelFormat, SampleFormat > format)
Definition: frames.cc:68
BaseFrame(std::shared_ptr< const StreamInfo > stream_info, double pts, double dts, double duration, bool is_key_frame)
Definition: frames.cc:115
size_t EstimateSize() const override
Definition: frames.cc:162
virtual size_t EstimateSize() const
Definition: frames.cc:124
const uint8_t *const data
Definition: frames.h:244
virtual DecryptStatus Decrypt(const FrameEncryptionInfo *info, const uint8_t *data, size_t data_size, uint8_t *dest) const =0
size_t EstimateSize() const override
Definition: frames.cc:185
size_t GetPlaneCount(variant< PixelFormat, SampleFormat > format, size_t channels)
Definition: frames.cc:92
~EncodedFrame() override
Definition: frames.cc:141
const double duration
Definition: frames.h:208
const double dts
Definition: frames.h:205
virtual ~BaseFrame()
Definition: frames.cc:122
const double pts
Definition: frames.h:202