ivf_parser.h 2.8 KB

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