Skip to content

Commit

Permalink
api2-scale-video: use new decode/encode API
Browse files Browse the repository at this point in the history
  • Loading branch information
h4tr3d committed Mar 11, 2024
1 parent 35f0e22 commit 2592eb6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 54 deletions.
119 changes: 65 additions & 54 deletions example/api2-samples/api2-scale-video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ int main(int argc, char **argv)
VideoDecoderContext vdec;
Stream vst;

int count = 0;

ictx.openInput(uri, ec);
if (ec) {
cerr << "Can't open input\n";
Expand Down Expand Up @@ -141,87 +139,100 @@ int main(int argc, char **argv)
//
// PROCESS
//
while (true) {
bool eof = false;
while (!eof) {

// READING
Packet pkt = ictx.readPacket(ec);
if (ec)
{
if (ec) {
clog << "Packet reading error: " << ec << ", " << ec.message() << endl;
break;
}

// EOF
if (!pkt)
{
break;
}

if (pkt.streamIndex() != videoStream) {
if (!pkt) {
eof = true;
} else if (pkt.streamIndex() != videoStream) {
continue;
}

clog << "Read packet: pts=" << pkt.pts() << ", dts=" << pkt.dts() << " / " << pkt.pts().seconds() << " / " << pkt.timeBase() << " / st: " << pkt.streamIndex() << endl;

// DECODING
auto inpFrame = vdec.decode(pkt, ec);

count++;
if (count > 200)
break;
// --htrd: start

if (ec) {
cerr << "Decoding error: " << ec << endl;
return 1;
} else if (!inpFrame) {
cerr << "Empty frame\n";
continue;
}
// share between encoding and flushing
auto encode_proc = [&](auto opkt) {
assert(opkt);

clog << "inpFrame: pts=" << inpFrame.pts() << " / " << inpFrame.pts().seconds() << " / " << inpFrame.timeBase() << ", " << inpFrame.width() << "x" << inpFrame.height() << ", size=" << inpFrame.size() << ", ref=" << inpFrame.isReferenced() << ":" << inpFrame.refCount() << " / type: " << inpFrame.pictureType() << endl;
// Only one output stream
opkt.setStreamIndex(0);

// Change timebase
inpFrame.setTimeBase(encoder.timeBase());
inpFrame.setStreamIndex(0);
inpFrame.setPictureType();
clog << "Write packet: pts=" << opkt.pts() << ", dts=" << opkt.dts() << " / " << opkt.pts().seconds() << " / " << opkt.timeBase() << " / st: " << opkt.streamIndex() << endl;

clog << "inpFrame: pts=" << inpFrame.pts() << " / " << inpFrame.pts().seconds() << " / " << inpFrame.timeBase() << ", " << inpFrame.width() << "x" << inpFrame.height() << ", size=" << inpFrame.size() << ", ref=" << inpFrame.isReferenced() << ":" << inpFrame.refCount() << " / type: " << inpFrame.pictureType() << endl;
octx.writePacket(opkt, ec);
if (ec) {
cerr << "Error write packet: " << ec << ", " << ec.message() << endl;
return -1; // TODO: use std::unexpected
}

// SCALE
auto outFrame = rescaler.rescale(inpFrame, ec);
if (ec) {
cerr << "Can't rescale frame: " << ec << ", " << ec.message() << endl;
return 1;
}
};

clog << "outFrame: pts=" << outFrame.pts()
<< " / " << outFrame.pts().seconds()
<< " / " << outFrame.timeBase()
<< ", " << outFrame.width() << "x" << outFrame.height()
<< ", size=" << outFrame.size()
<< ", ref=" << outFrame.isReferenced() << ":" << outFrame.refCount()
<< " / type: " << outFrame.pictureType() << endl;
vdec.decode(pkt, [&](auto inpFrame) {
assert(inpFrame);

// ENCODE
Packet opkt = encoder.encode(outFrame, ec);
if (ec) {
cerr << "Encoding error: " << ec << endl;
return 1;
} else if (!opkt) {
cerr << "Empty packet\n";
continue;
}
clog << "inpFrame: pts=" << inpFrame.pts() << " / " << inpFrame.pts().seconds() << " / " << inpFrame.timeBase() << ", " << inpFrame.width() << "x" << inpFrame.height() << ", size=" << inpFrame.size() << ", ref=" << inpFrame.isReferenced() << ":" << inpFrame.refCount() << " / type: " << inpFrame.pictureType() << endl;

// Change timebase
inpFrame.setTimeBase(encoder.timeBase());
inpFrame.setStreamIndex(0);
inpFrame.setPictureType();

clog << "inpFrame: pts=" << inpFrame.pts() << " / " << inpFrame.pts().seconds() << " / " << inpFrame.timeBase() << ", " << inpFrame.width() << "x" << inpFrame.height() << ", size=" << inpFrame.size() << ", ref=" << inpFrame.isReferenced() << ":" << inpFrame.refCount() << " / type: " << inpFrame.pictureType() << endl;

// SCALE
auto outFrame = rescaler.rescale(inpFrame, ec);
if (ec) {
cerr << "Can't rescale frame: " << ec << ", " << ec.message() << endl;
return -1; // TODO: use std::unexpected()
}

// Only one output stream
opkt.setStreamIndex(0);
clog << "outFrame: pts=" << outFrame.pts()
<< " / " << outFrame.pts().seconds()
<< " / " << outFrame.timeBase()
<< ", " << outFrame.width() << "x" << outFrame.height()
<< ", size=" << outFrame.size()
<< ", ref=" << outFrame.isReferenced() << ":" << outFrame.refCount()
<< " / type: " << outFrame.pictureType() << endl;

clog << "Write packet: pts=" << opkt.pts() << ", dts=" << opkt.dts() << " / " << opkt.pts().seconds() << " / " << opkt.timeBase() << " / st: " << opkt.streamIndex() << endl;
// ENCODE
encoder.encode(outFrame, encode_proc, ec);

if (ec) {
cerr << "Encoding error: " << ec << endl;
return -1; // TODO: use std::unexpected
}

return 1;
}, ec);

octx.writePacket(opkt, ec);
if (ec) {
cerr << "Error write packet: " << ec << ", " << ec.message() << endl;
cerr << "Decoding error: " << ec << endl;
return 1;
}

if (eof) {
clog << "Flush encoder:\n";
encoder.encodeFlush(encode_proc, ec);
if (ec) {
cerr << "Encoding error: " << ec << endl;
return 1;
}
}

// --htrd: end
}

octx.writeTrailer();
Expand Down
2 changes: 2 additions & 0 deletions src/codeccontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ class CodecContext2 : public FFWrapperPtr<AVCodecContext>, public noncopyable
if (sts <= 0)
break;
}

return {};
}

private:
Expand Down

0 comments on commit 2592eb6

Please sign in to comment.