clean up the VideoDemuxer class to make it follow the Google c++ style (#7)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
4ea67e279d
Коммит
b05e69b953
@@ -57,7 +57,7 @@ if(HIP_FOUND AND Libva_FOUND)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++17")
|
||||
target_link_libraries(${PROJECT_NAME} Libva::va Libva::va_drm hip::device)
|
||||
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR})
|
||||
install(FILES api/rocdecode.h utils/videoDemuxer.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})
|
||||
install(FILES api/rocdecode.h utils/video_demuxer.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})
|
||||
|
||||
else()
|
||||
if (NOT HIP_FOUND)
|
||||
|
||||
@@ -28,7 +28,7 @@ THE SOFTWARE.
|
||||
#include <sys/stat.h>
|
||||
#include <libgen.h>
|
||||
#include <filesystem>
|
||||
#include "videoDemuxer.hpp"
|
||||
#include "video_demuxer.hpp"
|
||||
#include "rocdecode.h"
|
||||
|
||||
void ShowHelpAndExit(const char *option = NULL) {
|
||||
@@ -103,7 +103,7 @@ int main(int argc, char **argv) {
|
||||
|
||||
do {
|
||||
auto startTime = std::chrono::high_resolution_clock::now();
|
||||
demuxer.demux(&pVideo, &nVideoBytes, &pts);
|
||||
demuxer.Demux(&pVideo, &nVideoBytes, &pts);
|
||||
//nFrameReturned = viddec.decode(pVideo, nVideoBytes, pts);
|
||||
auto endTime = std::chrono::high_resolution_clock::now();
|
||||
auto timePerFrame = std::chrono::duration<double, std::milli>(endTime - startTime).count();
|
||||
|
||||
@@ -39,31 +39,31 @@ class VideoDemuxer {
|
||||
class StreamProvider {
|
||||
public:
|
||||
virtual ~StreamProvider() {}
|
||||
virtual int getData(uint8_t *pBuf, int nBuf) = 0;
|
||||
virtual int GetData(uint8_t *buf, int buf_size) = 0;
|
||||
};
|
||||
VideoDemuxer(const char *inputFilePath) : VideoDemuxer(createFmtContextUtil(inputFilePath)) {}
|
||||
VideoDemuxer(StreamProvider *pStreamProvider) : VideoDemuxer(createFmtContextUtil(pStreamProvider)) {av_io_ctx_ = av_fmt_input_ctx_->pb;}
|
||||
VideoDemuxer(const char *input_file_path) : VideoDemuxer(CreateFmtContextUtil(input_file_path)) {}
|
||||
VideoDemuxer(StreamProvider *stream_provider) : VideoDemuxer(CreateFmtContextUtil(stream_provider)) {av_io_ctx_ = av_fmt_input_ctx_->pb;}
|
||||
~VideoDemuxer();
|
||||
bool demux(uint8_t **ppVideo, int *pnVideoBytes, int64_t *pts = nullptr);
|
||||
bool Demux(uint8_t **video, int *video_size, int64_t *pts = nullptr);
|
||||
private:
|
||||
VideoDemuxer(AVFormatContext *av_fmt_input_ctx_);
|
||||
AVFormatContext *createFmtContextUtil(StreamProvider *pStreamProvider);
|
||||
AVFormatContext *createFmtContextUtil(const char *inputFilePath);
|
||||
static int readPacket(void *pData, uint8_t *pBuf, int nBuf);
|
||||
VideoDemuxer(AVFormatContext *av_fmt_input_ctx);
|
||||
AVFormatContext *CreateFmtContextUtil(StreamProvider *stream_provider);
|
||||
AVFormatContext *CreateFmtContextUtil(const char *input_file_path);
|
||||
static int ReadPacket(void *data, uint8_t *buf, int buf_size);
|
||||
AVFormatContext *av_fmt_input_ctx_ = nullptr;
|
||||
AVIOContext *av_io_ctx_ = nullptr;
|
||||
AVPacket* packet_ = nullptr;
|
||||
AVPacket* packetFiltered_ = nullptr;
|
||||
AVPacket* packet_filtered_ = nullptr;
|
||||
AVBSFContext *av_bsf_ctx_ = nullptr;
|
||||
AVCodecID av_videoCodecID_;
|
||||
uint8_t *pDataWithHeader = nullptr;
|
||||
AVCodecID av_video_codec_id_;
|
||||
uint8_t *data_with_header_ = nullptr;
|
||||
int av_stream_ = 0;
|
||||
bool isH264_ = false;
|
||||
bool isHEVC_ = false;
|
||||
bool isMPEG4_ = false;
|
||||
int64_t defaultTimeScale = 1000;
|
||||
double timeBase = 0.0;
|
||||
unsigned int frameCnt = 0;
|
||||
bool is_h264_ = false;
|
||||
bool is_hevc_ = false;
|
||||
bool is_mpeg4_ = false;
|
||||
int64_t default_time_scale_ = 1000;
|
||||
double time_base_ = 0.0;
|
||||
unsigned int frame_count_ = 0;
|
||||
};
|
||||
|
||||
VideoDemuxer::~VideoDemuxer() {
|
||||
@@ -73,8 +73,8 @@ VideoDemuxer::~VideoDemuxer() {
|
||||
if (packet_) {
|
||||
av_packet_free(&packet_);
|
||||
}
|
||||
if (packetFiltered_) {
|
||||
av_packet_free(&packetFiltered_);
|
||||
if (packet_filtered_) {
|
||||
av_packet_free(&packet_filtered_);
|
||||
}
|
||||
if (av_bsf_ctx_) {
|
||||
av_bsf_free(&av_bsf_ctx_);
|
||||
@@ -84,16 +84,16 @@ VideoDemuxer::~VideoDemuxer() {
|
||||
av_freep(&av_io_ctx_->buffer);
|
||||
av_freep(&av_io_ctx_);
|
||||
}
|
||||
if (pDataWithHeader) {
|
||||
av_free(pDataWithHeader);
|
||||
if (data_with_header_) {
|
||||
av_free(data_with_header_);
|
||||
}
|
||||
}
|
||||
|
||||
bool VideoDemuxer::demux(uint8_t **ppVideo, int *pnVideoBytes, int64_t *pts) {
|
||||
bool VideoDemuxer::Demux(uint8_t **video, int *video_size, int64_t *pts) {
|
||||
if (!av_fmt_input_ctx_) {
|
||||
return false;
|
||||
}
|
||||
*pnVideoBytes = 0;
|
||||
*video_size = 0;
|
||||
if (packet_->data) {
|
||||
av_packet_unref(packet_);
|
||||
}
|
||||
@@ -104,55 +104,55 @@ bool VideoDemuxer::demux(uint8_t **ppVideo, int *pnVideoBytes, int64_t *pts) {
|
||||
if (ret < 0) {
|
||||
return false;
|
||||
}
|
||||
if (isH264_ || isHEVC_) {
|
||||
if (packetFiltered_->data) {
|
||||
av_packet_unref(packetFiltered_);
|
||||
if (is_h264_ || is_hevc_) {
|
||||
if (packet_filtered_->data) {
|
||||
av_packet_unref(packet_filtered_);
|
||||
}
|
||||
if (av_bsf_send_packet(av_bsf_ctx_, packet_) != 0) {
|
||||
std::cerr << "ERROR: av_bsf_send_packet failed!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
if (av_bsf_receive_packet(av_bsf_ctx_, packetFiltered_) != 0) {
|
||||
if (av_bsf_receive_packet(av_bsf_ctx_, packet_filtered_) != 0) {
|
||||
std::cerr << "ERROR: av_bsf_receive_packet failed!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
*ppVideo = packetFiltered_->data;
|
||||
*pnVideoBytes = packetFiltered_->size;
|
||||
*video = packet_filtered_->data;
|
||||
*video_size = packet_filtered_->size;
|
||||
if (pts)
|
||||
*pts = (int64_t) (packetFiltered_->pts * defaultTimeScale * timeBase);
|
||||
*pts = (int64_t) (packet_filtered_->pts * default_time_scale_ * time_base_);
|
||||
} else {
|
||||
if (isMPEG4_ && (frameCnt == 0)) {
|
||||
int extDataSize = av_fmt_input_ctx_->streams[av_stream_]->codecpar->extradata_size;
|
||||
if (extDataSize > 0) {
|
||||
pDataWithHeader = (uint8_t *)av_malloc(extDataSize + packet_->size - 3 * sizeof(uint8_t));
|
||||
if (!pDataWithHeader) {
|
||||
if (is_mpeg4_ && (frame_count_ == 0)) {
|
||||
int ext_data_size = av_fmt_input_ctx_->streams[av_stream_]->codecpar->extradata_size;
|
||||
if (ext_data_size > 0) {
|
||||
data_with_header_ = (uint8_t *)av_malloc(ext_data_size + packet_->size - 3 * sizeof(uint8_t));
|
||||
if (!data_with_header_) {
|
||||
std::cerr << "ERROR: av_malloc failed!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
memcpy(pDataWithHeader, av_fmt_input_ctx_->streams[av_stream_]->codecpar->extradata, extDataSize);
|
||||
memcpy(pDataWithHeader + extDataSize, packet_->data + 3, packet_->size - 3 * sizeof(uint8_t));
|
||||
*ppVideo = pDataWithHeader;
|
||||
*pnVideoBytes = extDataSize + packet_->size - 3 * sizeof(uint8_t);
|
||||
memcpy(data_with_header_, av_fmt_input_ctx_->streams[av_stream_]->codecpar->extradata, ext_data_size);
|
||||
memcpy(data_with_header_ + ext_data_size, packet_->data + 3, packet_->size - 3 * sizeof(uint8_t));
|
||||
*video = data_with_header_;
|
||||
*video_size = ext_data_size + packet_->size - 3 * sizeof(uint8_t);
|
||||
}
|
||||
} else {
|
||||
*ppVideo = packet_->data;
|
||||
*pnVideoBytes = packet_->size;
|
||||
*video = packet_->data;
|
||||
*video_size = packet_->size;
|
||||
}
|
||||
if (pts)
|
||||
*pts = (int64_t)(packet_->pts * defaultTimeScale * timeBase);
|
||||
*pts = (int64_t)(packet_->pts * default_time_scale_ * time_base_);
|
||||
}
|
||||
frameCnt++;
|
||||
frame_count_++;
|
||||
return true;
|
||||
}
|
||||
|
||||
VideoDemuxer::VideoDemuxer(AVFormatContext *av_fmt_input_ctx_) : av_fmt_input_ctx_(av_fmt_input_ctx_) {
|
||||
VideoDemuxer::VideoDemuxer(AVFormatContext *av_fmt_input_ctx) : av_fmt_input_ctx_(av_fmt_input_ctx) {
|
||||
if (!av_fmt_input_ctx_) {
|
||||
std::cerr << "ERROR: av_fmt_input_ctx_ is not vaild!" << std::endl;
|
||||
return;
|
||||
}
|
||||
packet_ = av_packet_alloc();
|
||||
packetFiltered_ = av_packet_alloc();
|
||||
if (!packet_ || !packetFiltered_) {
|
||||
packet_filtered_ = av_packet_alloc();
|
||||
if (!packet_ || !packet_filtered_) {
|
||||
std::cerr << "ERROR: av_packet_alloc failed!" << std::endl;
|
||||
return;
|
||||
}
|
||||
@@ -164,29 +164,29 @@ VideoDemuxer::VideoDemuxer(AVFormatContext *av_fmt_input_ctx_) : av_fmt_input_ct
|
||||
if (av_stream_ < 0) {
|
||||
std::cerr << "ERROR: av_find_best_stream failed!" << std::endl;
|
||||
av_packet_free(&packet_);
|
||||
av_packet_free(&packetFiltered_);
|
||||
av_packet_free(&packet_filtered_);
|
||||
return;
|
||||
}
|
||||
av_videoCodecID_ = av_fmt_input_ctx_->streams[av_stream_]->codecpar->codec_id;
|
||||
AVRational rTimeBase = av_fmt_input_ctx_->streams[av_stream_]->time_base;
|
||||
timeBase = av_q2d(rTimeBase);
|
||||
av_video_codec_id_ = av_fmt_input_ctx_->streams[av_stream_]->codecpar->codec_id;
|
||||
AVRational time_base = av_fmt_input_ctx_->streams[av_stream_]->time_base;
|
||||
time_base_ = av_q2d(time_base);
|
||||
|
||||
isH264_ = av_videoCodecID_ == AV_CODEC_ID_H264 && (!strcmp(av_fmt_input_ctx_->iformat->long_name, "QuickTime / MOV")
|
||||
is_h264_ = av_video_codec_id_ == AV_CODEC_ID_H264 && (!strcmp(av_fmt_input_ctx_->iformat->long_name, "QuickTime / MOV")
|
||||
|| !strcmp(av_fmt_input_ctx_->iformat->long_name, "FLV (Flash Video)")
|
||||
|| !strcmp(av_fmt_input_ctx_->iformat->long_name, "Matroska / WebM"));
|
||||
isHEVC_ = av_videoCodecID_ == AV_CODEC_ID_HEVC && (!strcmp(av_fmt_input_ctx_->iformat->long_name, "QuickTime / MOV")
|
||||
is_hevc_ = av_video_codec_id_ == AV_CODEC_ID_HEVC && (!strcmp(av_fmt_input_ctx_->iformat->long_name, "QuickTime / MOV")
|
||||
|| !strcmp(av_fmt_input_ctx_->iformat->long_name, "FLV (Flash Video)")
|
||||
|| !strcmp(av_fmt_input_ctx_->iformat->long_name, "Matroska / WebM"));
|
||||
isMPEG4_ = av_videoCodecID_ == AV_CODEC_ID_MPEG4 && (!strcmp(av_fmt_input_ctx_->iformat->long_name, "QuickTime / MOV")
|
||||
is_mpeg4_ = av_video_codec_id_ == AV_CODEC_ID_MPEG4 && (!strcmp(av_fmt_input_ctx_->iformat->long_name, "QuickTime / MOV")
|
||||
|| !strcmp(av_fmt_input_ctx_->iformat->long_name, "FLV (Flash Video)")
|
||||
|| !strcmp(av_fmt_input_ctx_->iformat->long_name, "Matroska / WebM"));
|
||||
|
||||
if (isH264_) {
|
||||
if (is_h264_) {
|
||||
const AVBitStreamFilter *bsf = av_bsf_get_by_name("h264_mp4toannexb");
|
||||
if (!bsf) {
|
||||
std::cerr << "ERROR: av_bsf_get_by_name() failed" << std::endl;
|
||||
av_packet_free(&packet_);
|
||||
av_packet_free(&packetFiltered_);
|
||||
av_packet_free(&packet_filtered_);
|
||||
return;
|
||||
}
|
||||
if (av_bsf_alloc(bsf, &av_bsf_ctx_) != 0) {
|
||||
@@ -199,12 +199,12 @@ VideoDemuxer::VideoDemuxer(AVFormatContext *av_fmt_input_ctx_) : av_fmt_input_ct
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (isHEVC_) {
|
||||
if (is_hevc_) {
|
||||
const AVBitStreamFilter *bsf = av_bsf_get_by_name("hevc_mp4toannexb");
|
||||
if (!bsf) {
|
||||
std::cerr << "ERROR: av_bsf_get_by_name() failed" << std::endl;
|
||||
av_packet_free(&packet_);
|
||||
av_packet_free(&packetFiltered_);
|
||||
av_packet_free(&packet_filtered_);
|
||||
return;
|
||||
}
|
||||
if (av_bsf_alloc(bsf, &av_bsf_ctx_) != 0 ) {
|
||||
@@ -219,7 +219,7 @@ VideoDemuxer::VideoDemuxer(AVFormatContext *av_fmt_input_ctx_) : av_fmt_input_ct
|
||||
}
|
||||
}
|
||||
|
||||
AVFormatContext *VideoDemuxer::createFmtContextUtil(StreamProvider *pStreamProvider) {
|
||||
AVFormatContext *VideoDemuxer::CreateFmtContextUtil(StreamProvider *stream_provider) {
|
||||
AVFormatContext *ctx = nullptr;
|
||||
if (!(ctx = avformat_alloc_context())) {
|
||||
std::cerr << "ERROR: avformat_alloc_context failed" << std::endl;
|
||||
@@ -233,7 +233,7 @@ AVFormatContext *VideoDemuxer::createFmtContextUtil(StreamProvider *pStreamProvi
|
||||
return nullptr;
|
||||
}
|
||||
av_io_ctx_ = avio_alloc_context(avioc_buffer, avioc_buffer_size,
|
||||
0, pStreamProvider, &readPacket, nullptr, nullptr);
|
||||
0, stream_provider, &ReadPacket, nullptr, nullptr);
|
||||
if (!av_io_ctx_) {
|
||||
std::cerr << "ERROR: avio_alloc_context failed!" << std::endl;
|
||||
return nullptr;
|
||||
@@ -247,17 +247,17 @@ AVFormatContext *VideoDemuxer::createFmtContextUtil(StreamProvider *pStreamProvi
|
||||
return ctx;
|
||||
}
|
||||
|
||||
AVFormatContext *VideoDemuxer::createFmtContextUtil(const char *inputFilePath) {
|
||||
AVFormatContext *VideoDemuxer::CreateFmtContextUtil(const char *input_file_path) {
|
||||
avformat_network_init();
|
||||
av_log_set_level(AV_LOG_QUIET);
|
||||
AVFormatContext *ctx = nullptr;
|
||||
if (avformat_open_input(&ctx, inputFilePath, nullptr, nullptr) != 0 ) {
|
||||
if (avformat_open_input(&ctx, input_file_path, nullptr, nullptr) != 0 ) {
|
||||
std::cerr << "ERROR: avformat_open_input failed!" << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
int VideoDemuxer::readPacket(void *pData, uint8_t *pBuf, int nBuf) {
|
||||
return ((StreamProvider *)pData)->getData(pBuf, nBuf);
|
||||
int VideoDemuxer::ReadPacket(void *data, uint8_t *buf, int buf_size) {
|
||||
return ((StreamProvider *)data)->GetData(buf, buf_size);
|
||||
}
|
||||
Ссылка в новой задаче
Block a user