1*09537850SAkhilesh Sanikop // Copyright 2019 The libgav1 Authors
2*09537850SAkhilesh Sanikop //
3*09537850SAkhilesh Sanikop // Licensed under the Apache License, Version 2.0 (the "License");
4*09537850SAkhilesh Sanikop // you may not use this file except in compliance with the License.
5*09537850SAkhilesh Sanikop // You may obtain a copy of the License at
6*09537850SAkhilesh Sanikop //
7*09537850SAkhilesh Sanikop // http://www.apache.org/licenses/LICENSE-2.0
8*09537850SAkhilesh Sanikop //
9*09537850SAkhilesh Sanikop // Unless required by applicable law or agreed to in writing, software
10*09537850SAkhilesh Sanikop // distributed under the License is distributed on an "AS IS" BASIS,
11*09537850SAkhilesh Sanikop // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*09537850SAkhilesh Sanikop // See the License for the specific language governing permissions and
13*09537850SAkhilesh Sanikop // limitations under the License.
14*09537850SAkhilesh Sanikop
15*09537850SAkhilesh Sanikop #include "examples/ivf_parser.h"
16*09537850SAkhilesh Sanikop
17*09537850SAkhilesh Sanikop #include <cstdio>
18*09537850SAkhilesh Sanikop #include <cstring>
19*09537850SAkhilesh Sanikop
20*09537850SAkhilesh Sanikop #include "examples/file_reader_constants.h"
21*09537850SAkhilesh Sanikop #include "examples/logging.h"
22*09537850SAkhilesh Sanikop
23*09537850SAkhilesh Sanikop namespace libgav1 {
24*09537850SAkhilesh Sanikop namespace {
25*09537850SAkhilesh Sanikop
ReadLittleEndian16(const uint8_t * const buffer)26*09537850SAkhilesh Sanikop size_t ReadLittleEndian16(const uint8_t* const buffer) {
27*09537850SAkhilesh Sanikop size_t value = buffer[1] << 8;
28*09537850SAkhilesh Sanikop value |= buffer[0];
29*09537850SAkhilesh Sanikop return value;
30*09537850SAkhilesh Sanikop }
31*09537850SAkhilesh Sanikop
ReadLittleEndian32(const uint8_t * const buffer)32*09537850SAkhilesh Sanikop size_t ReadLittleEndian32(const uint8_t* const buffer) {
33*09537850SAkhilesh Sanikop size_t value = buffer[3] << 24;
34*09537850SAkhilesh Sanikop value |= buffer[2] << 16;
35*09537850SAkhilesh Sanikop value |= buffer[1] << 8;
36*09537850SAkhilesh Sanikop value |= buffer[0];
37*09537850SAkhilesh Sanikop return value;
38*09537850SAkhilesh Sanikop }
39*09537850SAkhilesh Sanikop
40*09537850SAkhilesh Sanikop } // namespace
41*09537850SAkhilesh Sanikop
ParseIvfFileHeader(const uint8_t * const header_buffer,IvfFileHeader * const ivf_file_header)42*09537850SAkhilesh Sanikop bool ParseIvfFileHeader(const uint8_t* const header_buffer,
43*09537850SAkhilesh Sanikop IvfFileHeader* const ivf_file_header) {
44*09537850SAkhilesh Sanikop if (header_buffer == nullptr || ivf_file_header == nullptr) return false;
45*09537850SAkhilesh Sanikop
46*09537850SAkhilesh Sanikop if (memcmp(kIvfSignature, header_buffer, 4) != 0) {
47*09537850SAkhilesh Sanikop return false;
48*09537850SAkhilesh Sanikop }
49*09537850SAkhilesh Sanikop
50*09537850SAkhilesh Sanikop // Verify header version and length.
51*09537850SAkhilesh Sanikop const size_t ivf_header_version = ReadLittleEndian16(&header_buffer[4]);
52*09537850SAkhilesh Sanikop if (ivf_header_version != kIvfHeaderVersion) {
53*09537850SAkhilesh Sanikop LIBGAV1_EXAMPLES_LOG_ERROR("Unexpected IVF version");
54*09537850SAkhilesh Sanikop }
55*09537850SAkhilesh Sanikop
56*09537850SAkhilesh Sanikop const size_t ivf_header_size = ReadLittleEndian16(&header_buffer[6]);
57*09537850SAkhilesh Sanikop if (ivf_header_size != kIvfFileHeaderSize) {
58*09537850SAkhilesh Sanikop LIBGAV1_EXAMPLES_LOG_ERROR("Invalid IVF file header size");
59*09537850SAkhilesh Sanikop return false;
60*09537850SAkhilesh Sanikop }
61*09537850SAkhilesh Sanikop
62*09537850SAkhilesh Sanikop if (memcmp(kAv1FourCcLower, &header_buffer[8], 4) != 0 &&
63*09537850SAkhilesh Sanikop memcmp(kAv1FourCcUpper, &header_buffer[8], 4) != 0) {
64*09537850SAkhilesh Sanikop LIBGAV1_EXAMPLES_LOG_ERROR("Unsupported codec 4CC");
65*09537850SAkhilesh Sanikop return false;
66*09537850SAkhilesh Sanikop }
67*09537850SAkhilesh Sanikop
68*09537850SAkhilesh Sanikop ivf_file_header->width = ReadLittleEndian16(&header_buffer[12]);
69*09537850SAkhilesh Sanikop ivf_file_header->height = ReadLittleEndian16(&header_buffer[14]);
70*09537850SAkhilesh Sanikop ivf_file_header->frame_rate_numerator =
71*09537850SAkhilesh Sanikop ReadLittleEndian32(&header_buffer[16]);
72*09537850SAkhilesh Sanikop ivf_file_header->frame_rate_denominator =
73*09537850SAkhilesh Sanikop ReadLittleEndian32(&header_buffer[20]);
74*09537850SAkhilesh Sanikop
75*09537850SAkhilesh Sanikop return true;
76*09537850SAkhilesh Sanikop }
77*09537850SAkhilesh Sanikop
ParseIvfFrameHeader(const uint8_t * const header_buffer,IvfFrameHeader * const ivf_frame_header)78*09537850SAkhilesh Sanikop bool ParseIvfFrameHeader(const uint8_t* const header_buffer,
79*09537850SAkhilesh Sanikop IvfFrameHeader* const ivf_frame_header) {
80*09537850SAkhilesh Sanikop if (header_buffer == nullptr || ivf_frame_header == nullptr) return false;
81*09537850SAkhilesh Sanikop
82*09537850SAkhilesh Sanikop ivf_frame_header->frame_size = ReadLittleEndian32(header_buffer);
83*09537850SAkhilesh Sanikop if (ivf_frame_header->frame_size > kMaxTemporalUnitSize) {
84*09537850SAkhilesh Sanikop LIBGAV1_EXAMPLES_LOG_ERROR("Temporal Unit size exceeds maximum");
85*09537850SAkhilesh Sanikop return false;
86*09537850SAkhilesh Sanikop }
87*09537850SAkhilesh Sanikop
88*09537850SAkhilesh Sanikop ivf_frame_header->timestamp = ReadLittleEndian32(&header_buffer[4]);
89*09537850SAkhilesh Sanikop const uint64_t timestamp_hi =
90*09537850SAkhilesh Sanikop static_cast<uint64_t>(ReadLittleEndian32(&header_buffer[8])) << 32;
91*09537850SAkhilesh Sanikop ivf_frame_header->timestamp |= timestamp_hi;
92*09537850SAkhilesh Sanikop
93*09537850SAkhilesh Sanikop return true;
94*09537850SAkhilesh Sanikop }
95*09537850SAkhilesh Sanikop
96*09537850SAkhilesh Sanikop } // namespace libgav1
97