Repository navigation
Expand file tree
/
Copy pathenc_avc_pull.cpp
More file actions
165 lines (127 loc) · 4.29 KB
/
Copy pathenc_avc_pull.cpp
File metadata and controls
165 lines (127 loc) · 4.29 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright (c) Primo Software. All Rights Reserved.
*
* Use of this source code is governed by a MIT License
* that can be found in the LICENSE file in the root of the source
* tree.
*/
#include <unistd.h>
#include <iostream>
#include <string>
#include <fstream>
#include <filesystem>
#include <primo/platform/reference++.h>
#include <primo/platform/error_facility.h>
#include <primo/platform/ustring.h>
#include <primo/avblocks/avb.h>
#include "options.h"
#include "util.h"
using namespace std;
using namespace primo::avblocks;
using namespace primo::codecs;
namespace fs = std::filesystem;
namespace av = primo::avblocks;
namespace pc = primo::codecs;
void printStatus(const primo::error::ErrorInfo* e)
{
if (primo::error::ErrorFacility::Success == e->facility())
{
cout << "Success";
}
else
{
if (e->message())
{
cout << primo::ustring(e->message()) << " ";
}
cout << "(facility:" << e->facility() << " error:" << e->code() << ")" << endl;
}
cout << endl;
}
primo::ref<MediaSocket> createInputSocket(Options& opt)
{
auto socket = primo::make_ref(Library::createMediaSocket());
socket->setStreamType(StreamType::UncompressedVideo);
socket->setFile(primo::ustring(opt.yuv_file));
auto pin = primo::make_ref(Library::createMediaPin());
socket->pins()->add(pin.get());
auto vsi = primo::make_ref(Library::createVideoStreamInfo());
pin->setStreamInfo(vsi.get());
vsi->setStreamType(StreamType::UncompressedVideo);
vsi->setFrameWidth(opt.frame_size.width_);
vsi->setFrameHeight(opt.frame_size.height_);
vsi->setColorFormat(opt.yuv_color.Id);
vsi->setFrameRate(opt.fps);
vsi->setScanType(ScanType::Progressive);
return socket;
}
primo::ref<MediaSocket> createOutputSocket(Options& opt)
{
auto socket = primo::make_ref(Library::createMediaSocket());
socket->setStreamType(StreamType::H264);
socket->setStreamSubType(StreamSubType::AVC_Annex_B);
auto pin = primo::make_ref(Library::createMediaPin());
socket->pins()->add(pin.get());
auto vsi = primo::make_ref(Library::createVideoStreamInfo());
pin->setStreamInfo(vsi.get());
vsi->setStreamType(StreamType::H264);
vsi->setStreamSubType(StreamSubType::AVC_Annex_B);
return socket;
}
bool encode(Options& opt)
{
// transcoder will fail if output exists (by design)
deleteFile(primo::ustring(opt.h264_file));
ofstream outfile(opt.h264_file, ios_base::binary);
if (!outfile.is_open())
{
cout << "Could not open file " << opt.h264_file << endl;
return false;
}
// create input socket
auto inSocket = createInputSocket(opt);
// create output socket
auto outSocket = createOutputSocket(opt);
// create transcoder
auto transcoder = primo::make_ref(Library::createTranscoder());
transcoder->setAllowDemoMode(true);
transcoder->inputs()->add(inSocket.get());
transcoder->outputs()->add(outSocket.get());
if (!transcoder->open())
{
printError("Transcoder::open", transcoder->error());
return false;
}
// encode by pulling encoded samples
int32_t outputIndex = 0;
auto sample = primo::make_ref(Library::createMediaSample());
while (transcoder->pull(outputIndex, sample.get()))
{
outfile.write((const char *)sample->buffer()->data(), sample->buffer()->dataSize());
}
const primo::error::ErrorInfo *error = transcoder->error();
printError("Transcoder::pull", error);
bool success = false;
if ((error->facility() == primo::error::ErrorFacility::Codec) &&
(error->code() == primo::codecs::CodecError::EOS))
{
// ok
success = true;
}
transcoder->close();
return success;
}
int main(int argc, char* argv[])
{
Options opt;
switch(prepareOptions(opt, argc, argv))
{
case Command: return 0;
case Error: return 1;
case Parsed: break;
}
Library::initialize();
bool encodeResult = encode(opt);
Library::shutdown();
return encodeResult ? 0 : 1;
}