Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / media / filters / ivf_parser.h
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef MEDIA_FILTERS_IVF_PARSER_H_
6 #define MEDIA_FILTERS_IVF_PARSER_H_
7
8 #include <stddef.h>
9 #include <stdint.h>
10
11 namespace media {
12
13 const char kIvfHeaderSignature[] = "DKIF";
14
15 #pragma pack(push, 1)
16 struct IvfFileHeader {
17   // Byte swap interger fields between native and (on disk) little endian.
18   void ByteSwap();
19
20   char signature[4];        // signature: 'DKIF'
21   uint16_t version;         // version (should be 0)
22   uint16_t header_size;     // size of header in bytes
23   uint32_t fourcc;          // codec FourCC (e.g., 'VP80')
24   uint16_t width;           // width in pixels
25   uint16_t height;          // height in pixels
26   uint32_t timebase_denum;  // timebase denumerator
27   uint32_t timebase_num;    // timebase numerator. For example, if
28                             // timebase_denum is 30 and timebase_num is 2, the
29                             // unit of IvfFrameHeader.timestamp is 2/30
30                             // seconds.
31   uint32_t num_frames;      // number of frames in file
32   uint32_t unused;          // unused
33 };
34 static_assert(
35     sizeof(IvfFileHeader) == 32,
36     "sizeof(IvfFileHeader) must be fixed since it will be used with file IO");
37
38 struct IvfFrameHeader {
39   // Byte swap interger fields between native and (on disk) little endian.
40   void ByteSwap();
41
42   uint32_t frame_size;  // Size of frame in bytes (not including the header)
43   uint64_t timestamp;   // 64-bit presentation timestamp in unit timebase,
44                         // which is defined in IvfFileHeader.
45 };
46 static_assert(
47     sizeof(IvfFrameHeader) == 12,
48     "sizeof(IvfFrameHeader) must be fixed since it will be used with file IO");
49 #pragma pack(pop)
50
51 // IVF is a simple container format for video frame. It is used by libvpx to
52 // transport VP8 and VP9 bitstream.
53 class IvfParser {
54  public:
55   IvfParser();
56
57   IvfParser(const IvfParser&) = delete;
58   IvfParser& operator=(const IvfParser&) = delete;
59
60   // Initializes the parser for IVF |stream| with size |size| and parses the
61   // file header. Returns true on success.
62   bool Initialize(const uint8_t* stream,
63                   size_t size,
64                   IvfFileHeader* file_header);
65
66   // Parses the next frame. Returns true if the next frame is parsed without
67   // error. |frame_header| will be filled with the frame header and |payload|
68   // will point to frame payload (inside the |stream| buffer given to
69   // Initialize.)
70   bool ParseNextFrame(IvfFrameHeader* frame_header, const uint8_t** payload);
71
72  private:
73   bool ParseFileHeader(IvfFileHeader* file_header);
74
75   // Current reading position of input stream.
76   const uint8_t* ptr_;
77
78   // The end position of input stream.
79   const uint8_t* end_;
80 };
81
82 }  // namespace media
83
84 #endif  // MEDIA_FILTERS_IVF_PARSER_H_