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();
34
35 th_ycbcr_buffer buffer;
36 size_t no;
37 int length;
38};
39
41{
42public:
43 OggReader(const OggReader&) = delete;
44 OggReader& operator=(const OggReader&) = delete;
45
46 OggReader(const Filename& filename, CliComm& cli);
47 ~OggReader();
48
49 bool seek(size_t frame, size_t sample);
50 [[nodiscard]] unsigned getSampleRate() const { return narrow<unsigned>(vi.rate); }
51 void getFrameNo(RawFrame& frame, size_t frameno);
52 [[nodiscard]] const AudioFragment* getAudio(size_t sample);
53 [[nodiscard]] size_t getFrames() const { return totalFrames; }
54 [[nodiscard]] int getFrameRate() const { return frameRate; }
55
56 // metadata
57 [[nodiscard]] bool stopFrame(size_t frame) const;
58 [[nodiscard]] size_t getChapter(int chapterNo) const;
59
60private:
61 void cleanup();
62 void readTheora(ogg_packet* packet);
63 void theoraHeaderPage(ogg_page* page, th_info& ti, th_comment& tc,
64 th_setup_info*& tsi);
65 void readMetadata(th_comment& tc);
66 void readVorbis(ogg_packet* packet);
67 void vorbisHeaderPage(ogg_page* page);
68 bool nextPage(ogg_page* page);
69 bool nextPacket();
70 void recycleAudio(std::unique_ptr<AudioFragment> audio);
71 void vorbisFoundPosition();
72 size_t frameNo(ogg_packet* packet) const;
73
74 size_t findOffset(size_t frame, size_t sample);
75 size_t bisection(size_t frame, size_t sample,
76 size_t maxOffset, size_t maxSamples, size_t maxFrames);
77
78private:
79 CliComm& cli;
80 File file;
81
82 enum State {
83 PLAYING,
84 FIND_LAST,
85 FIND_FIRST,
86 FIND_KEYFRAME
87 } state{PLAYING};
88
89 // ogg state
90 ogg_sync_state sync;
91 ogg_stream_state vorbisStream, theoraStream;
92 int audioSerial{-1};
93 int videoSerial{-1};
94 int skeletonSerial{-1};
95 size_t fileOffset{0};
96 size_t fileSize;
97
98 // video
99 th_dec_ctx* theora{nullptr};
100 int frameRate;
101 size_t keyFrame{size_t(-1)};
102 size_t currentFrame{1};
103 int granuleShift;
104 size_t totalFrames;
105
107 std::vector<std::unique_ptr<Frame>> recycleFrameList;
108
109 // audio
110 int audioHeaders{3};
111 vorbis_info vi;
112 vorbis_comment vc;
113 vorbis_dsp_state vd;
114 vorbis_block vb;
115 size_t currentSample{0};
116 size_t vorbisPos{0};
117
118 std::list<std::unique_ptr<AudioFragment>> audioList;
120
121 // Metadata
122 std::vector<size_t> stopFrames;
123 struct ChapterFrame {
124 int chapter;
125 size_t frame;
126 };
127 std::vector<ChapterFrame> chapters; // sorted on chapter
128};
129
130} // namespace openmsx
131
132#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:50
size_t getChapter(int chapterNo) const
Definition OggReader.cc:954
size_t getFrames() const
Definition OggReader.hh:53
int getFrameRate() const
Definition OggReader.hh:54
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:17
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
th_ycbcr_buffer buffer
Definition OggReader.hh:35