Codec2.0(C2)是android系统为vendor提供的用于实现video/audio/filter模块的的HAL层接口API,vendor可用这个API实现他们自己的HAL层,Codec2.0是用于替换现有的OMX-IL。
数据流程
C2LinearBlock
创建share ptr类型block,fetchLinearBlock
对block进行初始化,block->map().get()
得到C2WriteView
对象,然后进行input数据拷贝。
std::atomic_int mLinearPoolId;
std::shared_ptr<C2Allocator> mAllocIon;
std::shared_ptr<C2BlockPool> mLinearPool;
// create allocator store
std::shared_ptr<C2AllocatorStore> store = GetCodec2PlatformAllocatorStore();
CHECK_EQ(store->fetchAllocator(C2AllocatorStore::DEFAULT_LINEAR, &mAllocIon), C2_OK);
mLinearPool = std::make_shared<C2PooledBlockPool>(mAllocIon, mLinearPoolId++);
mLinearPool.reset(new C2PooledBlockPool(mAllocIon, mLinearPoolId++));
// create block
std::shared_ptr<C2LinearBlock> block;
mLinearPool->fetchLinearBlock(
size,
{C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE},
&block);
// Write view provides read/write access for a linear memory segment
C2WriteView view = block->map().get();
if (view.error() != C2_OK) {
fprintf(stderr, "get C2WriteView failed : %d.", view.error());
break;
}
// into work-input
memcpy(view.base(), data, size);
work->input.buffers.clear();
work->input.buffers.emplace_back(new LinearBuffer(block));
work->worklets.clear();
work->worklets.emplace_back(new C2Worklet);
surface创建流程
Codec2中创建使用surface通过这几个类接口完成:Surface,SurfaceComposerClient,SurfaceControl。
frameworks/native/include/gui/Surface.h
frameworks/native/include/gui/SurfaceControl.h
frameworks/native/include/gui/IGraphicBufferProducer.h
frameworks/native/include/gui/ISurfaceComposerClient.h
sp<Surface> mSurface;
sp<SurfaceComposerClient> mComposerClient;
sp<SurfaceControl> mControl;
mComposerClient = new SurfaceComposerClient();
CHECK_EQ(mComposerClient->initCheck(), (status_t)OK);
// 调用createSurface创建Surface,保存在SurfaceControl的mNativeObject对象中
mControl = mComposerClient->createSurface(
String8("A Surface"),
1280,
800,
HAL_PIXEL_FORMAT_YV12);
CHECK(mControl != nullptr);
CHECK(mControl->isValid());
SurfaceComposerClient::Transaction{}
.setLayer(mControl, INT_MAX)
.show(mControl)
.apply();
// 调用getSurface对象,然后返回surface对象
mSurface = mControl->getSurface();
CHECK(mSurface != nullptr);
mSurface->connect(NATIVE_WINDOW_API_CPU, mProducerListener);
frameworks/native/libs/nativewindow/include/system/window.h
// parameter for NATIVE_WINDOW_[API_][DIS]CONNECT
NATIVE_WINDOW_API_EGL
NATIVE_WINDOW_API_CPU
NATIVE_WINDOW_API_MEDIA
NATIVE_WINDOW_API_CAMERA
output数据流程
处理output,先获取output的graphic block,然后创建IGraphicBufferProducer::QueueBufferInput,然后通过mComponent->queueToOutputSurface将block送给OutputSurface,这个过程没有数据拷贝,用的都是graphicbuffer的内存映射。
// getIGraphicBufferProducer可以继续用吗?
const sp<IGraphicBufferProducer> &producer = mSurface->getIGraphicBufferProducer();
uint32_t generation = (getpid() << 10);
C2BlockPool::local_id_t outputPoolId = C2BlockPool::BASIC_LINEAR;
producer->setGenerationNumber(generation);
if (mComponent->setOutputSurface(
outputPoolId,
producer,
generation) != C2_OK) {
fprintf(stderr, "setSurface: component setOutputSurface failed.\n");
return;
}
const C2ConstGraphicBlock block = output->data().graphicBlocks().front();
fprintf(stderr, "%d got block.\n", gettid());
// TODO: revisit this after C2Fence implementation.
android::IGraphicBufferProducer::QueueBufferInput qbi(
(work->worklets.front()->output.ordinal.timestamp * 1000ll).peekll(),
false, // droppable
HAL_DATASPACE_UNKNOWN,
Rect(block.width(), block.height()),
// the buffer is scaled in both dimensions to match the window size
NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
0,
Fence::NO_FENCE,
0);
// 这部分在CCodecBufferChannel中也是这么用的
qbi.setSurfaceDamage(Region::INVALID_REGION);
android::IGraphicBufferProducer::QueueBufferOutput qbo;
status_t result = mComponent->queueToOutputSurface(block, qbi, &qbo);
if (result != OK) {
fprintf(stderr, "queueBuffer failed: %d", result);
return;
}
硬解码是通过GraphicBuffer将output buffer送给component的output surface显示的:
media/codec2/hidl/1.0/utils/OutputBufferQueue.cpp
mComponent->queueToOutputSurface
会调用OutputBufferQueue::outputBuffer
:
status_t OutputBufferQueue::outputBuffer(
const C2ConstGraphicBlock& block,
const BnGraphicBufferProducer::QueueBufferInput& input,
BnGraphicBufferProducer::QueueBufferOutput* output) {
uint32_t generation;
uint64_t bqId;
int32_t bqSlot;
bool display = displayBufferQueueBlock(block);
if (!getBufferQueueAssignment(block, &generation, &bqId, &bqSlot) ||
bqId == 0) {
// Block not from bufferqueue -- it must be attached before queuing.
mMutex.lock();
sp<IGraphicBufferProducer> outputIgbp = mIgbp;
uint32_t outputGeneration = mGeneration;
mMutex.unlock();
status_t status = attachToBufferQueue(
block, outputIgbp, outputGeneration, &bqSlot);
if (status != OK) {
LOG(WARNING) << "outputBuffer -- attaching failed.";
return INVALID_OPERATION;
}
status = outputIgbp->queueBuffer(static_cast<int>(bqSlot),
input, output);
if (status != OK) {
LOG(ERROR) << "outputBuffer -- queueBuffer() failed "
"on non-bufferqueue-based block. "
"Error = " << status << ".";
return status;
}
return OK;
}
mMutex.lock();
sp<IGraphicBufferProducer> outputIgbp = mIgbp;
uint32_t outputGeneration = mGeneration;
uint64_t outputBqId = mBqId;
mMutex.unlock();
if (!outputIgbp) {
LOG(VERBOSE) << "outputBuffer -- output surface is null.";
return NO_INIT;
}
if (!display) {
LOG(WARNING) << "outputBuffer -- cannot display "
"bufferqueue-based block to the bufferqueue.";
return UNKNOWN_ERROR;
}
if (bqId != outputBqId || generation != outputGeneration) {
int32_t diff = (int32_t) outputGeneration - (int32_t) generation;
LOG(WARNING) << "outputBuffer -- buffers from old generation to "
<< outputGeneration << " , diff: " << diff
<< " , slot: " << bqSlot;
return DEAD_OBJECT;
}
status_t status = outputIgbp->queueBuffer(static_cast<int>(bqSlot),
input, output);
if (status != OK) {
LOG(ERROR) << "outputBuffer -- queueBuffer() failed "
"on bufferqueue-based block. "
"Error = " << status << ".";
return status;
}
return OK;
}
C2work queue
C2Work的 work queue管理input和output,先queue进去8个C2Work,然后读数据的时候依次从work queue
里面取得work填好数据,送给decoder,然后解码后还回来一个C2Work,从解码回来的C2Work中取得output,output送给surface之后,将该work的input.buffers,worklets清空,放进work queue里面。
// 从work qeue获取可用buffer,如果是空等100m继续从qeue中可用buffer
std::unique_ptr<C2Work> work;
while (!work) {
ULock l(mQueueLock);
if (!mWorkQueue.empty()) {
work.swap(mWorkQueue.front());
mWorkQueue.pop_front();
fprintf(stderr, "pop work from work queue.\n");
} else {
fprintf(stderr, "wait_for work queue.\n");
mQueueCondition.wait_for(l, 100ms);
}
}
// 拿到work后将数据block发给你倒buffers中,并清空worklets,放一个新的C2Work到worklets中
work->input.flags = (C2FrameData::flags_t)0;
work->input.ordinal.timestamp = timestamp;
work->input.ordinal.frameIndex = numFrames;
work->input.buffers.clear();
work->input.buffers.emplace_back(new LinearBuffer(block));
work->worklets.clear();
work->worklets.emplace_back(new C2Worklet);
// 解码后的数据通过onWorkDone回调传回来C2Work类型的item
for (auto &item : workItems) {
mProcessedWork.push_back(std::move(item));
}