1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/logging.h"
6 #include "base/sys_byteorder.h"
7 #include "media/filters/ivf_parser.h"
11 void IvfFileHeader::ByteSwap() {
12 version
= base::ByteSwapToLE16(version
);
13 header_size
= base::ByteSwapToLE16(header_size
);
14 fourcc
= base::ByteSwapToLE32(fourcc
);
15 width
= base::ByteSwapToLE16(width
);
16 height
= base::ByteSwapToLE16(height
);
17 timebase_denum
= base::ByteSwapToLE32(timebase_denum
);
18 timebase_num
= base::ByteSwapToLE32(timebase_num
);
19 num_frames
= base::ByteSwapToLE32(num_frames
);
20 unused
= base::ByteSwapToLE32(unused
);
23 void IvfFrameHeader::ByteSwap() {
24 frame_size
= base::ByteSwapToLE32(frame_size
);
25 timestamp
= base::ByteSwapToLE64(timestamp
);
28 IvfParser::IvfParser() : ptr_(nullptr), end_(nullptr) {}
30 bool IvfParser::Initialize(const uint8_t* stream
,
32 IvfFileHeader
* file_header
) {
38 if (size
< sizeof(IvfFileHeader
)) {
39 DLOG(ERROR
) << "EOF before file header";
43 memcpy(file_header
, ptr_
, sizeof(IvfFileHeader
));
44 file_header
->ByteSwap();
46 if (memcmp(file_header
->signature
, kIvfHeaderSignature
,
47 sizeof(file_header
->signature
)) != 0) {
48 DLOG(ERROR
) << "IVF signature mismatch";
51 DLOG_IF(WARNING
, file_header
->version
!= 0)
52 << "IVF version unknown: " << file_header
->version
53 << ", the parser may not be able to parse correctly";
54 if (file_header
->header_size
!= sizeof(IvfFileHeader
)) {
55 DLOG(ERROR
) << "IVF file header size mismatch";
59 ptr_
+= sizeof(IvfFileHeader
);
64 bool IvfParser::ParseNextFrame(IvfFrameHeader
* frame_header
,
65 const uint8_t** payload
) {
69 if (end_
< ptr_
+ sizeof(IvfFrameHeader
)) {
70 DLOG_IF(ERROR
, ptr_
!= end_
) << "Incomplete frame header";
74 memcpy(frame_header
, ptr_
, sizeof(IvfFrameHeader
));
75 frame_header
->ByteSwap();
76 ptr_
+= sizeof(IvfFrameHeader
);
78 if (end_
< ptr_
+ frame_header
->frame_size
) {
79 DLOG(ERROR
) << "Not enough frame data";
84 ptr_
+= frame_header
->frame_size
;