Skip to content

Commit

Permalink
Check the mp4 at the beginning and end
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianFeldmann committed Mar 27, 2024
1 parent 849027e commit 9c08a16
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions YUViewLib/src/filesource/FileSourceFFmpegFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,27 +237,44 @@ ByteVector FileSourceFFmpegFile::getLhvCData()

// This is a bit of a hack. The problem is that FFmpeg can currently not extract this for us.
// Maybe this will be added in the future. So the only option we have here is to manually extract
// the lhvC data from the mp4 file.
std::ifstream input(this->fileName.toStdString(), std::ios::binary);
const auto rawFileData = functions::readData(input, 1024);
if (rawFileData.empty())
return {};
// the lhvC data from the mp4 file. In mp4, the boxes can be at the beginning or at the end of the
// file.
enum class SearchPosition
{
Beginning,
End
};

const std::string searchString = "lhvC";
const auto lhvcPos =
std::search(rawFileData.begin(), rawFileData.end(), searchString.begin(), searchString.end());
if (lhvcPos == rawFileData.end())
return {};
std::ifstream inputFile(this->fileName.toStdString(), std::ios::binary);
for (const auto searchPosition : {SearchPosition::Beginning, SearchPosition::End})
{
constexpr auto NR_SEARCH_BYTES = 5120;

if (std::distance(rawFileData.begin(), lhvcPos) < 4)
return {};
if (searchPosition == SearchPosition::End)
inputFile.seekg(-NR_SEARCH_BYTES, std::ios_base::end);

const auto boxSize = getBoxSize(lhvcPos - 4);
if (boxSize == 0 || boxSize > std::distance(lhvcPos, rawFileData.end()))
return {};
const auto rawFileData = functions::readData(inputFile, NR_SEARCH_BYTES);
if (rawFileData.empty())
continue;

const std::string searchString = "lhvC";
auto lhvcPos = std::search(
rawFileData.begin(), rawFileData.end(), searchString.begin(), searchString.end());
if (lhvcPos == rawFileData.end())
continue;

if (std::distance(rawFileData.begin(), lhvcPos) < 4)
continue;

const auto boxSize = getBoxSize(lhvcPos - 4);
if (boxSize == 0 || boxSize > std::distance(lhvcPos, rawFileData.end()))
continue;

// We just return the payload without the box size or the "lhvC" tag
return ByteVector(lhvcPos + 4, lhvcPos + boxSize - 4);
}

// We just return the payload without the box size or the "lhvC" tag
return ByteVector(lhvcPos + 4, lhvcPos + boxSize - 4);
return {};
}

QList<QByteArray> FileSourceFFmpegFile::getParameterSets()
Expand Down

0 comments on commit 9c08a16

Please sign in to comment.