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 <array>
10#include <list>
11#include <memory>
12#include <vector>
13
14namespace openmsx {
15
16class CliComm;
17class RawFrame;
18class 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 std::array<std::array<float, MAX_SAMPLES>, 2> pcm;
27};
28
29struct 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
40{
41public:
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
59private:
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
77private:
78 CliComm& cli;
79 File file;
80
81 enum State {
82 PLAYING,
83 FIND_LAST,
84 FIND_FIRST,
85 FIND_KEYFRAME
86 } state{PLAYING};
87
88 // ogg state
89 ogg_sync_state sync;
90 ogg_stream_state vorbisStream, theoraStream;
91 int audioSerial{-1};
92 int videoSerial{-1};
93 int skeletonSerial{-1};
94 size_t fileOffset{0};
95 size_t fileSize;
96
97 // video
98 th_dec_ctx* theora{nullptr};
99 int frameRate;
100 size_t keyFrame{size_t(-1)};
101 size_t currentFrame{1};
102 int granuleShift;
103 size_t totalFrames;
104
106 std::vector<std::unique_ptr<Frame>> recycleFrameList;
107
108 // audio
109 int audioHeaders{3};
110 vorbis_info vi;
111 vorbis_comment vc;
112 vorbis_dsp_state vd;
113 vorbis_block vb;
114 size_t currentSample{0};
115 size_t vorbisPos{0};
116
117 std::list<std::unique_ptr<AudioFragment>> audioList;
119
120 // Metadata
121 std::vector<size_t> stopFrames;
122 struct ChapterFrame {
123 int chapter;
124 size_t frame;
125 };
126 std::vector<ChapterFrame> chapters; // sorted on chapter
127};
128
129} // namespace openmsx
130
131#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:949
void getFrameNo(RawFrame &frame, size_t frameno)
Definition: OggReader.cc:596
OggReader & operator=(const OggReader &)=delete
const AudioFragment * getAudio(size_t sample)
Definition: OggReader.cc:663
OggReader(const OggReader &)=delete
unsigned getSampleRate() const
Definition: OggReader.hh:49
size_t getChapter(int chapterNo) const
Definition: OggReader.cc:954
size_t getFrames() const
Definition: OggReader.hh:52
int getFrameRate() const
Definition: OggReader.hh:53
bool seek(size_t frame, size_t sample)
Definition: OggReader.cc:918
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
static constexpr size_t UNKNOWN_POS
Definition: OggReader.hh:22
static constexpr unsigned MAX_SAMPLES
Definition: OggReader.hh:23
std::array< std::array< float, MAX_SAMPLES >, 2 > pcm
Definition: OggReader.hh:26
Frame(const th_ycbcr_buffer &yuv)
Definition: OggReader.cc:22
th_ycbcr_buffer buffer
Definition: OggReader.hh:34