openMSX
OggReader.hh
Go to the documentation of this file.
1 #ifndef OGGREADER_HH
2 #define OGGREADER_HH
3 
4 #include "File.hh"
5 #include "circular_buffer.hh"
6 #include <ogg/ogg.h>
7 #include <vorbis/codec.h>
8 #include <theora/theoradec.h>
9 #include <memory>
10 #include <list>
11 #include <vector>
12 
13 namespace openmsx {
14 
15 class CliComm;
16 class RawFrame;
17 class Filename;
18 
20 {
21  static constexpr size_t UNKNOWN_POS = size_t(-1);
22  static constexpr unsigned MAX_SAMPLES = 2048;
23  size_t position;
24  unsigned length;
25  float pcm[2][MAX_SAMPLES];
26 };
27 
28 struct Frame
29 {
30  explicit Frame(const th_ycbcr_buffer& yuv);
31  ~Frame();
32 
33  th_ycbcr_buffer buffer;
34  size_t no;
35  int length;
36 };
37 
38 class OggReader
39 {
40 public:
41  OggReader(const OggReader&) = delete;
42  OggReader& operator=(const OggReader&) = delete;
43 
44  OggReader(const Filename& filename, CliComm& cli);
45  ~OggReader();
46 
47  bool seek(size_t frame, size_t sample);
48  [[nodiscard]] unsigned getSampleRate() const { return vi.rate; }
49  void getFrameNo(RawFrame& frame, size_t frameno);
50  [[nodiscard]] const AudioFragment* getAudio(size_t sample);
51  [[nodiscard]] size_t getFrames() const { return totalFrames; }
52  [[nodiscard]] int getFrameRate() const { return frameRate; }
53 
54  // metadata
55  [[nodiscard]] bool stopFrame(size_t frame) const;
56  [[nodiscard]] size_t getChapter(int chapterNo) const;
57 
58 private:
59  void cleanup();
60  void readTheora(ogg_packet* packet);
61  void theoraHeaderPage(ogg_page* page, th_info& ti, th_comment& tc,
62  th_setup_info*& tsi);
63  void readMetadata(th_comment& tc);
64  void readVorbis(ogg_packet* packet);
65  void vorbisHeaderPage(ogg_page* page);
66  bool nextPage(ogg_page* page);
67  bool nextPacket();
68  void recycleAudio(std::unique_ptr<AudioFragment> audio);
69  void vorbisFoundPosition();
70  size_t frameNo(ogg_packet* packet) const;
71 
72  size_t findOffset(size_t frame, size_t sample);
73  size_t bisection(size_t frame, size_t sample,
74  size_t maxOffset, size_t maxSamples, size_t maxFrames);
75 
76 private:
77  CliComm& cli;
78  File file;
79 
80  enum State {
81  PLAYING,
82  FIND_LAST,
83  FIND_FIRST,
84  FIND_KEYFRAME
85  } state;
86 
87  // ogg state
88  ogg_sync_state sync;
89  ogg_stream_state vorbisStream, theoraStream;
90  int audioSerial;
91  int videoSerial;
92  int skeletonSerial;
93  size_t fileOffset;
94  size_t fileSize;
95 
96  // video
97  th_dec_ctx* theora;
98  int frameRate;
99  size_t keyFrame;
100  size_t currentFrame;
101  int granuleShift;
102  size_t totalFrames;
103 
105  std::vector<std::unique_ptr<Frame>> recycleFrameList;
106 
107  // audio
108  int audioHeaders;
109  vorbis_info vi;
110  vorbis_comment vc;
111  vorbis_dsp_state vd;
112  vorbis_block vb;
113  size_t currentSample;
114  size_t vorbisPos;
115 
116  std::list<std::unique_ptr<AudioFragment>> audioList;
117  cb_queue<std::unique_ptr<AudioFragment>> recycleAudioList;
118 
119  // Metadata
120  std::vector<size_t> stopFrames;
121  struct ChapterFrame {
122  int chapter;
123  size_t frame;
124  };
125  std::vector<ChapterFrame> chapters; // sorted on chapter
126 };
127 
128 } // namespace openmsx
129 
130 #endif
This implements a queue on top of circular_buffer (not part of boost).
This class represents a filename.
Definition: Filename.hh:18
bool stopFrame(size_t frame) const
Definition: OggReader.cc:966
void getFrameNo(RawFrame &frame, size_t frameno)
Definition: OggReader.cc:613
const AudioFragment * getAudio(size_t sample)
Definition: OggReader.cc:680
OggReader(const OggReader &)=delete
unsigned getSampleRate() const
Definition: OggReader.hh:48
size_t getChapter(int chapterNo) const
Definition: OggReader.cc:971
size_t getFrames() const
Definition: OggReader.hh:51
OggReader & operator=(const OggReader &)=delete
int getFrameRate() const
Definition: OggReader.hh:52
bool seek(size_t frame, size_t sample)
Definition: OggReader.cc:935
A video frame as output by the VDP scanline conversion unit, before any postprocessing filters are ap...
Definition: RawFrame.hh:15
This file implemented 3 utility functions:
Definition: Autofire.cc:9
constexpr const char *const filename
static constexpr size_t UNKNOWN_POS
Definition: OggReader.hh:21
float pcm[2][MAX_SAMPLES]
Definition: OggReader.hh:25
static constexpr unsigned MAX_SAMPLES
Definition: OggReader.hh:22
Frame(const th_ycbcr_buffer &yuv)
Definition: OggReader.cc:22
th_ycbcr_buffer buffer
Definition: OggReader.hh:33