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 <cstdint>
10#include <limits>
11
12namespace {
13
14uint64_t CalculateGCD(uint64_t a, uint64_t b) {
15 while (b != 0) {
16 uint64_t temp = a;
17 a = b;
18 b = temp % b;
19 }
20 return a;
21}
22
23void ReducePixelWidthHeight(uint64_t* pixel_width, uint64_t* pixel_height) {
24 if (*pixel_width == 0 || *pixel_height == 0)
25 return;
26 const uint64_t kMaxUint32 = std::numeric_limits<uint32_t>::max();
27 while (true) {
28 uint64_t gcd = CalculateGCD(*pixel_width, *pixel_height);
29 *pixel_width /= gcd;
30 *pixel_height /= gcd;
31 // Both width and height needs to be 32 bit or less.
32 if (*pixel_width <= kMaxUint32 && *pixel_height <= kMaxUint32)
33 break;
34 *pixel_width >>= 1;
35 *pixel_height >>= 1;
36 }
37}
38
39} // namespace
40
41namespace shaka {
42namespace media {
43
44void DerivePixelWidthHeight(uint32_t frame_width,
45 uint32_t frame_height,
46 uint32_t display_width,
47 uint32_t display_height,
48 uint32_t* pixel_width,
49 uint32_t* pixel_height) {
50 // DAR = PAR * FAR => PAR = DAR / FAR.
51 // Thus:
52 // pixel_width display_width frame_width
53 // ----------- = ------------- / -----------
54 // pixel_height display_height frame_height
55 // So:
56 // pixel_width display_width x frame_height
57 // ----------- = ------------------------------
58 // pixel_height display_height x frame_width
59 uint64_t pixel_width_unreduced =
60 static_cast<uint64_t>(display_width) * frame_height;
61 uint64_t pixel_height_unreduced =
62 static_cast<uint64_t>(display_height) * frame_width;
63 ReducePixelWidthHeight(&pixel_width_unreduced, &pixel_height_unreduced);
64 *pixel_width = pixel_width_unreduced;
65 *pixel_height = pixel_height_unreduced;
66}
67
68} // namespace media
69} // namespace shaka
All the methods that are virtual are virtual for mocking.