• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 3-Clause Clear License
5  * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
6  * License was not distributed with this source code in the LICENSE file, you
7  * can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
8  * Alliance for Open Media Patent License 1.0 was not distributed with this
9  * source code in the PATENTS file, you can obtain it at
10  * www.aomedia.org/license/patent.
11  */
12 #include "iamf/common/utils/sample_processing_utils.h"
13 
14 #include <cstddef>
15 #include <cstdint>
16 
17 #include "absl/log/check.h"
18 #include "absl/log/log.h"
19 #include "absl/status/status.h"
20 #include "absl/strings/str_cat.h"
21 
22 namespace iamf_tools {
23 
WritePcmSample(uint32_t sample,uint8_t sample_size,bool big_endian,uint8_t * const buffer,size_t & write_position)24 absl::Status WritePcmSample(uint32_t sample, uint8_t sample_size,
25                             bool big_endian, uint8_t* const buffer,
26                             size_t& write_position) {
27   // Validate assumptions of the logic in the `for` loop below.
28   if (sample_size % 8 != 0 || sample_size > 32) [[unlikely]] {
29     return absl::InvalidArgumentError(
30         absl::StrCat("Invalid sample size: ", sample_size));
31   }
32 
33   for (int shift = 32 - sample_size; shift < 32; shift += 8) {
34     uint8_t byte = 0;
35     if (big_endian) [[unlikely]] {
36       byte = (sample >> ((32 - sample_size) + (32 - (shift + 8)))) & 0xff;
37     } else {
38       byte = (sample >> shift) & 0xff;
39     }
40     buffer[write_position++] = byte;
41   }
42 
43   return absl::OkStatus();
44 }
45 
46 }  // namespace iamf_tools
47