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