85 lines
4.6 KiB
C++
85 lines
4.6 KiB
C++
// Offline codec acceptance probe: links the production decoder factory and pinned archive.
|
|
#include "livekit/video_decoder_factory.h"
|
|
#include "api/environment/environment_factory.h"
|
|
#include "api/video/encoded_image.h"
|
|
#include "api/video/i420_buffer.h"
|
|
#include <array>
|
|
#include <chrono>
|
|
#include <condition_variable>
|
|
#include <cstdint>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <mutex>
|
|
#include <string>
|
|
|
|
class Output final : public webrtc::DecodedImageCallback {
|
|
public:
|
|
explicit Output(const char* path) : file(path, std::ios::binary) {}
|
|
int32_t Decoded(webrtc::VideoFrame& frame) override {
|
|
auto pixels = frame.video_frame_buffer()->ToI420();
|
|
if (!pixels) return -1;
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
width = pixels->width(); height = pixels->height();
|
|
auto write = [&](const uint8_t* data, int stride, int columns, int rows) {
|
|
for (int row = 0; row < rows; ++row) file.write(reinterpret_cast<const char*>(data + row * stride), columns);
|
|
};
|
|
write(pixels->DataY(), pixels->StrideY(), width, height);
|
|
write(pixels->DataU(), pixels->StrideU(), (width+1)/2, (height+1)/2);
|
|
write(pixels->DataV(), pixels->StrideV(), (width+1)/2, (height+1)/2);
|
|
file.flush(); ++frames; ready.notify_one(); return 0;
|
|
}
|
|
std::ofstream file;
|
|
std::mutex mutex;
|
|
std::condition_variable ready;
|
|
int width=0,height=0,frames=0;
|
|
};
|
|
static uint32_t little32(const uint8_t* p) {
|
|
return uint32_t(p[0]) | uint32_t(p[1])<<8 | uint32_t(p[2])<<16 | uint32_t(p[3])<<24;
|
|
}
|
|
int main(int argc,char** argv) {
|
|
if (argc!=4) { std::cerr<<"usage: codec_probe <AV1|H264|VP8|VP9> <single-frame fixture> <output.i420>\n"; return 2; }
|
|
const std::string codec=argv[1];
|
|
webrtc::VideoCodecType type;
|
|
if(codec=="AV1")type=webrtc::kVideoCodecAV1;
|
|
else if(codec=="H264")type=webrtc::kVideoCodecH264;
|
|
else if(codec=="VP8")type=webrtc::kVideoCodecVP8;
|
|
else if(codec=="VP9")type=webrtc::kVideoCodecVP9;
|
|
else return 2;
|
|
std::ifstream input(argv[2],std::ios::binary|std::ios::ate);
|
|
if(!input){std::cerr<<"fixture open failed\n";return 2;}
|
|
auto length=input.tellg();input.seekg(0);
|
|
if(length<=0||length>1024*1024){std::cerr<<"fixture size invalid\n";return 2;}
|
|
uint32_t bytes=static_cast<uint32_t>(length);
|
|
if(codec!="H264") {
|
|
std::array<uint8_t,44> header{};
|
|
input.read(reinterpret_cast<char*>(header.data()),header.size());
|
|
const std::string fourcc=codec=="AV1"?"AV01":codec=="VP8"?"VP80":"VP90";
|
|
if(!input||std::string(reinterpret_cast<char*>(header.data()),4)!="DKIF"||header[6]!=32||header[7]!=0||std::string(reinterpret_cast<char*>(header.data()+8),4)!=fourcc||little32(header.data()+24)!=1){std::cerr<<"expected one matching IVF frame\n";return 2;}
|
|
bytes=little32(header.data()+32);
|
|
if(bytes==0||bytes!=static_cast<uint32_t>(length)-44){std::cerr<<"IVF frame length invalid\n";return 2;}
|
|
}
|
|
auto data=webrtc::EncodedImageBuffer::Create(bytes);
|
|
input.read(reinterpret_cast<char*>(data->data()),bytes);
|
|
if(!input){std::cerr<<"fixture truncated\n";return 2;}
|
|
webrtc::SdpVideoFormat format(codec);
|
|
if(codec=="AV1")format=webrtc::SdpVideoFormat::AV1Profile0();
|
|
if(codec=="H264")format.parameters={{"profile-level-id","64001f"},{"packetization-mode","1"},{"level-asymmetry-allowed","1"}};
|
|
livekit_ffi::VideoDecoderFactory factory(false);
|
|
if(!factory.QueryCodecSupport(format,false).is_supported){std::cerr<<"production factory rejected codec\n";return 1;}
|
|
auto environment=webrtc::CreateEnvironment();
|
|
auto decoder=factory.Create(environment,format);
|
|
if(!decoder){std::cerr<<"decoder creation failed\n";return 1;}
|
|
webrtc::VideoDecoder::Settings settings;
|
|
settings.set_codec_type(type);settings.set_number_of_cores(1);settings.set_max_render_resolution({8192,8192});
|
|
if(!decoder->Configure(settings)){std::cerr<<"decoder configuration failed\n";return 1;}
|
|
Output output(argv[3]);if(!output.file)return 2;
|
|
decoder->RegisterDecodeCompleteCallback(&output);
|
|
webrtc::EncodedImage image;image.SetEncodedData(std::move(data));image.SetFrameType(webrtc::VideoFrameType::kVideoFrameKey);image.SetRtpTimestamp(90000);
|
|
const auto result=decoder->Decode(image,0);
|
|
{std::unique_lock<std::mutex> lock(output.mutex);output.ready.wait_for(lock,std::chrono::seconds(3),[&]{return output.frames!=0;});}
|
|
const auto info=decoder->GetDecoderInfo();
|
|
decoder->Release();
|
|
std::cout<<"codec="<<codec<<" decoder="<<info.implementation_name<<" hardware="<<info.is_hardware_accelerated<<" decode_result="<<result<<" frames="<<output.frames<<" width="<<output.width<<" height="<<output.height<<"\n";
|
|
return result==0&&output.frames==1&&output.width>0&&output.height>0&&!info.is_hardware_accelerated&&output.file.good()?0:1;
|
|
}
|