Skip to content
Snippets Groups Projects
Commit a95d4ed1 authored by David Staessens's avatar David Staessens Committed by Chih-Yu Huang
Browse files

v4l2_codec2: Move NALParser code to separate file.

This CL moves the simple NALParser to a dedicated file. The parser will
be expanded in a subsequent CL so moving the code in a separate CL will
make it easier to distinguish newly made changes.

Bug: 155138142
Test: arc.VideoEncodeAccel.h264_192p_i420_vm
Change-Id: I9691de7e610a4c9881c29dbdc337ae9d01668145
parent b6746213
No related branches found
No related tags found
No related merge requests found
...@@ -18,6 +18,7 @@ cc_library { ...@@ -18,6 +18,7 @@ cc_library {
srcs: [ srcs: [
"EncodeHelpers.cpp", "EncodeHelpers.cpp",
"FormatConverter.cpp", "FormatConverter.cpp",
"NalParser.cpp",
"V4L2ComponentCommon.cpp", "V4L2ComponentCommon.cpp",
"VideoTypes.cpp", "VideoTypes.cpp",
], ],
......
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
#include <ui/GraphicBuffer.h> #include <ui/GraphicBuffer.h>
#include <utils/Log.h> #include <utils/Log.h>
#include <v4l2_codec2/common/NalParser.h>
namespace android { namespace android {
media::VideoCodecProfile c2ProfileToVideoCodecProfile(C2Config::profile_t profile) { media::VideoCodecProfile c2ProfileToVideoCodecProfile(C2Config::profile_t profile) {
...@@ -160,32 +162,4 @@ void extractCSDInfo(std::unique_ptr<C2StreamInitDataInfo::output>* const csd, co ...@@ -160,32 +162,4 @@ void extractCSDInfo(std::unique_ptr<C2StreamInitDataInfo::output>* const csd, co
std::memcpy((*csd)->m.value, tmpConfigData.get(), configDataLength); std::memcpy((*csd)->m.value, tmpConfigData.get(), configDataLength);
} }
NalParser::NalParser(const uint8_t* data, size_t length)
: mCurrNalDataPos(data), mDataEnd(data + length) {
mNextNalStartCodePos = findNextStartCodePos();
}
bool NalParser::locateNextNal() {
if (mNextNalStartCodePos == mDataEnd) return false;
mCurrNalDataPos = mNextNalStartCodePos + kNalStartCodeLength; // skip start code.
mNextNalStartCodePos = findNextStartCodePos();
return true;
}
const uint8_t* NalParser::data() const {
return mCurrNalDataPos;
}
size_t NalParser::length() const {
if (mNextNalStartCodePos == mDataEnd) return mDataEnd - mCurrNalDataPos;
size_t length = mNextNalStartCodePos - mCurrNalDataPos;
// The start code could be 3 or 4 bytes, i.e., 0x000001 or 0x00000001.
return *(mNextNalStartCodePos - 1) == 0x00 ? length - 1 : length;
}
const uint8_t* NalParser::findNextStartCodePos() const {
return std::search(mCurrNalDataPos, mDataEnd, kNalStartCode,
kNalStartCode + kNalStartCodeLength);
}
} // namespace android } // namespace android
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <v4l2_codec2/common/NalParser.h>
#include <algorithm>
namespace android {
NalParser::NalParser(const uint8_t* data, size_t length)
: mCurrNalDataPos(data), mDataEnd(data + length) {
mNextNalStartCodePos = findNextStartCodePos();
}
bool NalParser::locateNextNal() {
if (mNextNalStartCodePos == mDataEnd) return false;
mCurrNalDataPos = mNextNalStartCodePos + kNalStartCodeLength; // skip start code.
mNextNalStartCodePos = findNextStartCodePos();
return true;
}
const uint8_t* NalParser::data() const {
return mCurrNalDataPos;
}
size_t NalParser::length() const {
if (mNextNalStartCodePos == mDataEnd) return mDataEnd - mCurrNalDataPos;
size_t length = mNextNalStartCodePos - mCurrNalDataPos;
// The start code could be 3 or 4 bytes, i.e., 0x000001 or 0x00000001.
return *(mNextNalStartCodePos - 1) == 0x00 ? length - 1 : length;
}
const uint8_t* NalParser::findNextStartCodePos() const {
return std::search(mCurrNalDataPos, mDataEnd, kNalStartCode,
kNalStartCode + kNalStartCodeLength);
}
} // namespace android
...@@ -52,38 +52,6 @@ android_ycbcr getGraphicBlockInfo(const C2ConstGraphicBlock& block); ...@@ -52,38 +52,6 @@ android_ycbcr getGraphicBlockInfo(const C2ConstGraphicBlock& block);
void extractCSDInfo(std::unique_ptr<C2StreamInitDataInfo::output>* const csd, const uint8_t* data, void extractCSDInfo(std::unique_ptr<C2StreamInitDataInfo::output>* const csd, const uint8_t* data,
size_t length); size_t length);
// Helper class to parse H264 NAL units from data.
class NalParser {
public:
NalParser(const uint8_t* data, size_t length);
// Locates the next NAL after |mNextNalStartCodePos|. If there is one, updates |mCurrNalDataPos|
// to the first byte of the NAL data (start code is not included), and |mNextNalStartCodePos| to
// the position of the next start code, and returns true.
// If there is no more NAL, returns false.
//
// Note: This method must be called prior to data() and length().
bool locateNextNal();
// Gets current NAL data (start code is not included).
const uint8_t* data() const;
// Gets the byte length of current NAL data (start code is not included).
size_t length() const;
private:
const uint8_t* findNextStartCodePos() const;
// The byte pattern for the start of a H264 NAL unit.
const uint8_t kNalStartCode[3] = {0x00, 0x00, 0x01};
// The length in bytes of the NAL-unit start pattern.
const size_t kNalStartCodeLength = 3;
const uint8_t* mCurrNalDataPos;
const uint8_t* mDataEnd;
const uint8_t* mNextNalStartCodePos;
};
} // namespace android } // namespace android
#endif // ANDROID_V4L2_CODEC2_COMMON_HELPERS_H #endif // ANDROID_V4L2_CODEC2_COMMON_HELPERS_H
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ANDROID_V4L2_CODEC2_COMMON_NALPARSER_H
#define ANDROID_V4L2_CODEC2_COMMON_NALPARSER_H
#include <stdint.h>
namespace android {
// Helper class to parse H264 NAL units from data.
class NalParser {
public:
NalParser(const uint8_t* data, size_t length);
// Locates the next NAL after |mNextNalStartCodePos|. If there is one, updates |mCurrNalDataPos|
// to the first byte of the NAL data (start code is not included), and |mNextNalStartCodePos| to
// the position of the next start code, and returns true.
// If there is no more NAL, returns false.
//
// Note: This method must be called prior to data() and length().
bool locateNextNal();
// Gets current NAL data (start code is not included).
const uint8_t* data() const;
// Gets the byte length of current NAL data (start code is not included).
size_t length() const;
private:
const uint8_t* findNextStartCodePos() const;
// The byte pattern for the start of a H264 NAL unit.
const uint8_t kNalStartCode[3] = {0x00, 0x00, 0x01};
// The length in bytes of the NAL-unit start pattern.
const size_t kNalStartCodeLength = 3;
const uint8_t* mCurrNalDataPos;
const uint8_t* mDataEnd;
const uint8_t* mNextNalStartCodePos;
};
} // namespace android
#endif // ANDROID_V4L2_CODEC2_COMMON_NALPARSER_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment