Shaka Packager SDK
Loading...
Searching...
No Matches
video_util.cc
1// Copyright 2019 Google LLC. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file or at
5// https://developers.google.com/open-source/licenses/bsd
6
7#include <packager/media/base/video_util.h>
8
9#include <limits>
10
11namespace {
12
13uint64_t CalculateGCD(uint64_t a, uint64_t b) {
14 while (b != 0) {
15 uint64_t temp = a;
16 a = b;
17 b = temp % b;
18 }
19 return a;
20}
21
22void ReducePixelWidthHeight(uint64_t* pixel_width, uint64_t* pixel_height) {
23 if (*pixel_width == 0 || *pixel_height == 0)
24 return;
25 const uint64_t kMaxUint32 = std::numeric_limits<uint32_t>::max();
26 while (true) {
27 uint64_t gcd = CalculateGCD(*pixel_width, *pixel_height);
28 *pixel_width /= gcd;
29 *pixel_height /= gcd;
30 // Both width and height needs to be 32 bit or less.
31 if (*pixel_width <= kMaxUint32 && *pixel_height <= kMaxUint32)
32 break;
33 *pixel_width >>= 1;
34 *pixel_height >>= 1;
35 }
36}
37
38} // namespace
39
40namespace shaka {
41namespace media {
42
43void DerivePixelWidthHeight(uint32_t frame_width,
44 uint32_t frame_height,
45 uint32_t display_width,
46 uint32_t display_height,
47 uint32_t* pixel_width,
48 uint32_t* pixel_height) {
49 // DAR = PAR * FAR => PAR = DAR / FAR.
50 // Thus:
51 // pixel_width display_width frame_width
52 // ----------- = ------------- / -----------
53 // pixel_height display_height frame_height
54 // So:
55 // pixel_width display_width x frame_height
56 // ----------- = ------------------------------
57 // pixel_height display_height x frame_width
58 uint64_t pixel_width_unreduced =
59 static_cast<uint64_t>(display_width) * frame_height;
60 uint64_t pixel_height_unreduced =
61 static_cast<uint64_t>(display_height) * frame_width;
62 ReducePixelWidthHeight(&pixel_width_unreduced, &pixel_height_unreduced);
63 *pixel_width = pixel_width_unreduced;
64 *pixel_height = pixel_height_unreduced;
65}
66
67} // namespace media
68} // namespace shaka
All the methods that are virtual are virtual for mocking.