Skip to content

Commit

Permalink
Produced too big output file. Fixed. Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sapharow committed Feb 21, 2017
1 parent ef41bb2 commit 1243898
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
25 changes: 18 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MyEncodedStream : public fp::VideoStream {
sprintf(buffer, "enc_video_%04x.mp2", id);
break;
case fp::StreamType::Video_H264:
sprintf(buffer, "enc_video_%04x.mp2", id);
sprintf(buffer, "enc_video_%04x.h264", id);
break;
case fp::StreamType::Audio_11172_2:
sprintf(buffer, "enc_audio_%04x.mp1", id);
Expand Down Expand Up @@ -67,11 +67,6 @@ class MyVideoStream : public fp::VideoStream {
MyVideoStream(fp::VideoStreamMeta* meta)
: fp::VideoStream(meta)
{
static int i = 0;
if (i) {
return;
}

// Create transcoder
fp::VideoStreamMeta outputMeta;
outputMeta.id = 0;
Expand All @@ -93,7 +88,6 @@ class MyVideoStream : public fp::VideoStream {
break;
case fp::StreamType::Video_H262:
sprintf(buffer, "video_%04x.mp2", id());
i++;
break;
case fp::StreamType::Audio_11172_2:
sprintf(buffer, "audio_%04x.mp1", id());
Expand All @@ -110,9 +104,15 @@ class MyVideoStream : public fp::VideoStream {
}

m_File = fopen(buffer, "wb");

m_StartTime = std::chrono::high_resolution_clock::now();
}

~MyVideoStream() override {
auto end = std::chrono::high_resolution_clock::now();
auto msPassed = std::chrono::duration_cast<std::chrono::milliseconds>(end - m_StartTime).count();
printf("transcode speed = %.2f frames/sec\n", (float)m_NFrames * 1000.f / (float)msPassed);

if (m_File) {
fclose(m_File);
}
Expand All @@ -123,6 +123,7 @@ class MyVideoStream : public fp::VideoStream {
if (m_Transcoder) {
try {
m_Transcoder->supplyFrame(data, size, metadata);
m_NFrames++;
} catch (std::exception& e) {
printf("%s\n", e.what());
}
Expand All @@ -132,10 +133,15 @@ class MyVideoStream : public fp::VideoStream {
fwrite(data, 1, size, m_File);
}
}

uint64_t nFrames() const { return m_NFrames; }

private:
FILE* m_File = nullptr;
fp::trans::TranscoderRef m_Transcoder;
fp::VideoStreamRef m_Output;
uint64_t m_NFrames = 0;
std::chrono::high_resolution_clock::time_point m_StartTime;
};

class MySource : public fp::cap::FileSource {
Expand All @@ -149,6 +155,11 @@ class MySource : public fp::cap::FileSource {
}

fp::VideoStreamRef createVideoStream(fp::VideoStreamMeta* meta) override {
static int i = 0;
if (i) {
return nullptr;
}
i++;
return std::make_shared<MyVideoStream>(meta);
}

Expand Down
5 changes: 4 additions & 1 deletion src/transcode/softwareVideoTranscoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,12 @@ namespace fp {
}

size_t SoftwareDecoderContext::decodeFrame(const uint8_t* data, size_t size, Stream::Metadata* metadata, const DecodedFrameRef& outFrame) {
if (!outFrame) {
throw std::invalid_argument("Provided null output frame");
}
auto sFrame = dynamic_cast<SoftwareDecodedFrame*>(outFrame.get());
if (!sFrame) {
throw std::runtime_error("Provided incompatible class implementation");
throw std::invalid_argument("Provided incompatible class implementation");
}

AVPacket decodePacket;
Expand Down
4 changes: 2 additions & 2 deletions src/transcode/videoTranscoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace fp {
for (size_t i=0; i<m_DecodedFramePoolSize; i++) {
size_t nBytesEncoded = m_EncoderContext->encodeFrame(m_DecodedFramePool[i], m_EncodedDataBuffer);
if (nBytesEncoded) {
output()->supplyFrame(m_EncodedDataBuffer.data(), m_EncodedDataBuffer.size());
output()->supplyFrame(m_EncodedDataBuffer.data(), nBytesEncoded);
}
}
m_DecodedFramePoolSize = 0;
Expand All @@ -98,7 +98,7 @@ namespace fp {

void VideoTranscoder::insertFrame() {
// Assume m_DecodedFrame[1..m_DecodedFrameCount] are sorted by PTS
if (m_DecodedFramePoolSize+1 > MAX_REF_FRAMES) {
if (m_DecodedFramePoolSize+1 >= m_DecodedFramePool.size()) {
// We can't put more frames in buffer
// TODO:
printf("We can't put more frames in buffer\n");
Expand Down

0 comments on commit 1243898

Please sign in to comment.