Shaka Player Embedded
shaka_utils.cc
Go to the documentation of this file.
1 // Copyright 2018 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 <glog/logging.h>
16 
17 #include <algorithm>
18 
19 #include "shaka/utils.h"
20 
21 namespace shaka {
22 
24  Rational<uint32_t> sample_aspect_ratio,
27  if (!sample_aspect_ratio)
28  sample_aspect_ratio = Rational<uint32_t>{1, 1};
29 
30  switch (mode) {
31  // Stretch video to fill screen.
33  *src = frame;
34  *dest = bounds;
35  break;
36 
37  // Crop video to maintain aspect ratio while filling the screen.
38  case VideoFillMode::Zoom: {
39  *dest = bounds;
40  const Rational<uint32_t> bounds_ratio{bounds.w, bounds.h};
41  // We either use the whole width or the whole height; the other dimension
42  // gets cropped based on the aspect ratio.
43  // Convert number of pixels to image size, then use display aspect ratio.
44  src->w = std::min(
45  frame.w, (frame.h / sample_aspect_ratio * bounds_ratio).truncate());
46  src->h = std::min(
47  frame.h, (frame.w * sample_aspect_ratio / bounds_ratio).truncate());
48  break;
49  }
50 
51  // Add black boxes around video to maintain aspect ratio.
53  *src = frame;
54  const Rational<uint32_t> aspect_ratio =
55  Rational<uint32_t>{frame.w, frame.h} * sample_aspect_ratio;
56  // Get the width based on the height and the display aspect ratio.
57  dest->w = std::min(bounds.w, (bounds.h * aspect_ratio).truncate());
58  dest->h = std::min(bounds.h, (bounds.w / aspect_ratio).truncate());
59  DCHECK(dest->w == bounds.w || dest->h == bounds.h);
60  break;
61  }
62 
63  default:
64  LOG(FATAL) << "Unknown video fill mode: " << static_cast<int>(mode);
65  }
66 
67  // Center the rect within its region.
68  src->x = frame.x + (frame.w - src->w) / 2;
69  src->y = frame.y + (frame.h - src->h) / 2;
70  dest->x = bounds.x + (bounds.w - dest->w) / 2;
71  dest->y = bounds.y + (bounds.h - dest->h) / 2;
72 
73  // Should always produce a sub-region of the input bounds.
74  DCHECK_GE(src->x, frame.x);
75  DCHECK_GE(src->y, frame.y);
76  DCHECK_LE(src->x + src->w, frame.x + frame.w);
77  DCHECK_LE(src->y + src->h, frame.y + frame.h);
78  DCHECK_GE(dest->x, bounds.x);
79  DCHECK_GE(dest->y, bounds.y);
80  DCHECK_LE(dest->x + dest->w, bounds.x + bounds.w);
81  DCHECK_LE(dest->y + dest->h, bounds.y + bounds.h);
82 }
83 
84 } // namespace shaka
const char * dest
Definition: media_utils.cc:31
VideoFillMode
Definition: utils.h:41
std::shared_ptr< shaka::media::DecodedFrame > frame
void FitVideoToRegion(ShakaRect< uint32_t > frame, ShakaRect< uint32_t > bounds, Rational< uint32_t > sample_aspect_ratio, VideoFillMode mode, ShakaRect< uint32_t > *src, ShakaRect< uint32_t > *dest)
Definition: shaka_utils.cc:23