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