forked from zaps166/libsimplewebm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
129 lines (116 loc) · 3.34 KB
/
example.cpp
File metadata and controls
129 lines (116 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* MIT License
*
* Copyright (c) 2016 Błażej Szczygieł
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "OpusVorbisDecoder.hpp"
#include "VPXDecoder.hpp"
#include <mkvparser/mkvparser.h>
#include <stdio.h>
class MkvReader : public mkvparser::IMkvReader
{
public:
MkvReader(const char *filePath) :
m_file(fopen(filePath, "rb"))
{}
~MkvReader()
{
if (m_file)
fclose(m_file);
}
int Read(long long pos, long len, unsigned char *buf)
{
if (!m_file)
return -1;
fseek(m_file, pos, SEEK_SET);
const size_t size = fread(buf, 1, len, m_file);
if (size < size_t(len))
return -1;
return 0;
}
int Length(long long *total, long long *available)
{
if (!m_file)
return -1;
const off_t pos = ftell(m_file);
fseek(m_file, 0, SEEK_END);
if (total)
*total = ftell(m_file);
if (available)
*available = ftell(m_file);
fseek(m_file, pos, SEEK_SET);
return 0;
}
private:
FILE *m_file;
};
int main(int argc, char *argv[])
{
if (argc != 2)
return -1;
WebMDemuxer demuxer(new MkvReader(argv[1]));
if (demuxer.isOpen())
{
VPXDecoder videoDec(demuxer, 8);
OpusVorbisDecoder audioDec(demuxer);
WebMFrame videoFrame, audioFrame;
VPXDecoder::Image image;
short *pcm = audioDec.isOpen() ? new short[audioDec.getBufferSamples() * demuxer.getChannels()] : NULL;
fprintf(stderr, "Length: %f\n", demuxer.getLength());
while (demuxer.readFrame(&videoFrame, &audioFrame))
{
if (videoDec.isOpen() && videoFrame.isValid())
{
if (!videoDec.decode(videoFrame))
{
fprintf(stderr, "Video decode error\n");
break;
}
while (videoDec.getImage(image) == VPXDecoder::NO_ERROR)
{
// for (int p = 0; p < 3; ++p)
// {
// const int w = image.getWidth(p);
// const int h = image.getHeight(p);
// int offset = 0;
// for (int y = 0; y < h; ++y)
// {
// fwrite(image.planes[p] + offset, 1, w, stdout);
// offset += image.linesize[p];
// }
// }
}
}
if (audioDec.isOpen() && audioFrame.isValid())
{
int numOutSamples;
if (!audioDec.getPCMS16(audioFrame, pcm, numOutSamples))
{
fprintf(stderr, "Audio decode error\n");
break;
}
// fwrite(pcm, 1, numOutSamples * demuxer.getChannels() * sizeof(short), stdout);
}
}
delete[] pcm;
}
return 0;
}