blob: cd9fd9fc0aa7e5a034662b1250b3fc7b6e1f9129 [file] [log] [blame]
Arun Johnsonfb946102023-12-27 01:10:34 +00001/*
2 * Copyright 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "Codec2-MultiAccessUnitHelper"
19#include <android-base/logging.h>
20
21#include <com_android_media_codec_flags.h>
22
23#include <codec2/common/MultiAccessUnitHelper.h>
24#include <android-base/properties.h>
25
26#include <C2BufferPriv.h>
27#include <C2Debug.h>
28#include <C2PlatformSupport.h>
29
30namespace android {
31
32static C2R MultiAccessUnitParamsSetter(
33 bool mayBlock, C2InterfaceHelper::C2P<C2LargeFrame::output> &me) {
34 (void)mayBlock;
35 C2R res = C2R::Ok();
36 if (!me.F(me.v.maxSize).supportsAtAll(me.v.maxSize)) {
37 res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.maxSize)));
38 } else if (!me.F(me.v.thresholdSize).supportsAtAll(me.v.thresholdSize)) {
39 res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.thresholdSize)));
40 } else if (me.v.maxSize < me.v.thresholdSize) {
41 me.set().maxSize = me.v.thresholdSize;
42 } else if (me.v.thresholdSize == 0 && me.v.maxSize > 0) {
43 me.set().thresholdSize = me.v.maxSize;
44 }
45 std::vector<std::unique_ptr<C2SettingResult>> failures;
46 res.retrieveFailures(&failures);
47 if (!failures.empty()) {
48 me.set().maxSize = 0;
49 me.set().thresholdSize = 0;
50 }
51 return res;
52}
53
54MultiAccessUnitInterface::MultiAccessUnitInterface(
55 const std::shared_ptr<C2ComponentInterface>& interface,
56 std::shared_ptr<C2ReflectorHelper> helper)
57 : C2InterfaceHelper(helper), mC2ComponentIntf(interface) {
58 setDerivedInstance(this);
59 addParameter(
60 DefineParam(mLargeFrameParams, C2_PARAMKEY_OUTPUT_LARGE_FRAME)
61 .withDefault(new C2LargeFrame::output(0u, 0, 0))
62 .withFields({
63 C2F(mLargeFrameParams, maxSize).inRange(
64 0, c2_min(UINT_MAX, 10 * 512000 * 8 * 2u)),
65 C2F(mLargeFrameParams, thresholdSize).inRange(
66 0, c2_min(UINT_MAX, 10 * 512000 * 8 * 2u))
67 })
68 .withSetter(MultiAccessUnitParamsSetter)
69 .build());
70 std::vector<std::shared_ptr<C2ParamDescriptor>> supportedParams;
71 querySupportedParams(&supportedParams);
72 // Adding to set to do intf seperation in query/config
73 for (std::shared_ptr<C2ParamDescriptor> &desc : supportedParams) {
74 mSupportedParamIndexSet.insert(desc->index());
75 }
76
77 if (mC2ComponentIntf) {
78 c2_status_t err = mC2ComponentIntf->query_vb({&mKind}, {}, C2_MAY_BLOCK, nullptr);
79 }
80}
81
82bool MultiAccessUnitInterface::isParamSupported(C2Param::Index index) {
83 return (mSupportedParamIndexSet.count(index) != 0);
84}
85
86C2LargeFrame::output MultiAccessUnitInterface::getLargeFrameParam() const {
87 return *mLargeFrameParams;
88}
89
90C2Component::kind_t MultiAccessUnitInterface::kind() const {
91 return (C2Component::kind_t)(mKind.value);
92}
93
94void MultiAccessUnitInterface::getDecoderSampleRateAndChannelCount(
95 uint32_t &sampleRate_, uint32_t &channelCount_) const {
96 if (mC2ComponentIntf) {
97 C2StreamSampleRateInfo::output sampleRate;
98 C2StreamChannelCountInfo::output channelCount;
99 c2_status_t res = mC2ComponentIntf->query_vb(
100 {&sampleRate, &channelCount}, {}, C2_MAY_BLOCK, nullptr);
101 if (res == C2_OK) {
102 sampleRate_ = sampleRate.value;
103 channelCount_ = channelCount.value;
104 }
105 }
106}
107
108//C2MultiAccessUnitBuffer
109class C2MultiAccessUnitBuffer : public C2Buffer {
110 public:
111 explicit C2MultiAccessUnitBuffer(
112 const std::vector<C2ConstLinearBlock> &blocks):
113 C2Buffer(blocks) {
114 }
115};
116
117//MultiAccessUnitHelper
118MultiAccessUnitHelper::MultiAccessUnitHelper(
119 const std::shared_ptr<MultiAccessUnitInterface>& intf):
120 mInit(false),
121 mInterface(intf) {
122 std::shared_ptr<C2AllocatorStore> store = GetCodec2PlatformAllocatorStore();
123 if(store->fetchAllocator(C2AllocatorStore::DEFAULT_LINEAR, &mLinearAllocator) == C2_OK) {
124 mLinearPool = std::make_shared<C2PooledBlockPool>(mLinearAllocator, ++mBlockPoolId);
125 mInit = true;
126 }
127}
128
129MultiAccessUnitHelper::~MultiAccessUnitHelper() {
130 std::unique_lock<std::mutex> l(mLock);
131 mFrameHolder.clear();
132}
133
134bool MultiAccessUnitHelper::isEnabledOnPlatform() {
135 bool result = com::android::media::codec::flags::provider_->large_audio_frame();
136 if (!result) {
Arun Johnsonf5be2952024-02-05 21:05:53 +0000137 return false;
Arun Johnsonfb946102023-12-27 01:10:34 +0000138 }
139 //TODO: remove this before launch
140 result = ::android::base::GetBoolProperty("debug.media.c2.large.audio.frame", true);
141 LOG(DEBUG) << "MultiAccessUnitHelper " << (result ? "enabled" : "disabled");
142 return result;
143}
144
145std::shared_ptr<MultiAccessUnitInterface> MultiAccessUnitHelper::getInterface() {
146 return mInterface;
147}
148
149bool MultiAccessUnitHelper::getStatus() {
150 return mInit;
151}
152
153void MultiAccessUnitHelper::reset() {
154 std::lock_guard<std::mutex> l(mLock);
155 mFrameHolder.clear();
156}
157
158c2_status_t MultiAccessUnitHelper::error(
159 std::list<std::unique_ptr<C2Work>> * const worklist) {
160 if (worklist == nullptr) {
161 LOG(ERROR) << "Provided null worklist for error()";
162 return C2_OK;
163 }
164 std::unique_lock<std::mutex> l(mLock);
165 for (auto frame = mFrameHolder.begin(); frame != mFrameHolder.end(); frame++) {
166 if (frame->mLargeWork) {
167 finalizeWork(*frame, 0, true);
168 worklist->push_back(std::move(frame->mLargeWork));
169 frame->reset();
170 }
171 }
172 mFrameHolder.clear();
173 return C2_OK;
174}
175
176c2_status_t MultiAccessUnitHelper::flush(
177 std::list<std::unique_ptr<C2Work>>* const c2flushedWorks) {
178 c2_status_t c2res = C2_OK;
179 std::lock_guard<std::mutex> l(mLock);
180 for (std::unique_ptr<C2Work>& w : *c2flushedWorks) {
181 bool foundFlushedFrame = false;
182 std::list<MultiAccessUnitInfo>::iterator frame =
183 mFrameHolder.begin();
184 while (frame != mFrameHolder.end() && !foundFlushedFrame) {
185 auto it = frame->mComponentFrameIds.find(
186 w->input.ordinal.frameIndex.peekull());
187 if (it != frame->mComponentFrameIds.end()) {
188 LOG(DEBUG) << "Multi access-unit flush"
189 << w->input.ordinal.frameIndex.peekull()
190 << " with " << frame->inOrdinal.frameIndex.peekull();
191 w->input.ordinal.frameIndex = frame->inOrdinal.frameIndex;
192 bool removeEntry = w->worklets.empty()
193 || !w->worklets.front()
194 || (w->worklets.front()->output.flags
195 & C2FrameData::FLAG_INCOMPLETE) == 0;
196 if (removeEntry) {
197 frame->mComponentFrameIds.erase(it);
198 }
199 foundFlushedFrame = true;
200 }
201 if (frame->mComponentFrameIds.empty()) {
202 frame = mFrameHolder.erase(frame);
203 } else {
204 ++frame;
205 }
206 }
207 }
208 return c2res;
209}
210
211c2_status_t MultiAccessUnitHelper::scatter(
212 std::list<std::unique_ptr<C2Work>> &largeWork,
213 std::list<std::list<std::unique_ptr<C2Work>>>* const processedWork) {
214 LOG(DEBUG) << "Multiple access-unit: scatter";
215 if (processedWork == nullptr) {
216 LOG(ERROR) << "MultiAccessUnitHelper provided with no work list";
217 return C2_CORRUPTED;
218 }
219 for (std::unique_ptr<C2Work>& w : largeWork) {
220 std::list<std::unique_ptr<C2Work>> sliceWork;
221 C2WorkOrdinalStruct inputOrdinal = w->input.ordinal;
222 // To hold correspondence and processing bits b/w input and output
223 MultiAccessUnitInfo frameInfo(inputOrdinal);
224 std::set<uint64_t>& frameSet = frameInfo.mComponentFrameIds;
225 uint64_t newFrameIdx = mFrameIndex++;
226 // TODO: Do not split buffers if component inherantly supports MultipleFrames.
227 // if thats case, only replace frameindex.
228 auto cloneInputWork = [&newFrameIdx](std::unique_ptr<C2Work>& inWork, uint32_t flags) {
229 std::unique_ptr<C2Work> newWork(new C2Work);
230 newWork->input.flags = (C2FrameData::flags_t)flags;
231 newWork->input.ordinal = inWork->input.ordinal;
232 newWork->input.ordinal.frameIndex = newFrameIdx;
233 if (!inWork->input.configUpdate.empty()) {
234 for (std::unique_ptr<C2Param>& param : inWork->input.configUpdate) {
235 newWork->input.configUpdate.push_back(
236 std::move(C2Param::Copy(*(param.get()))));
237 }
238 }
239 newWork->input.infoBuffers = (inWork->input.infoBuffers);
240 if (!inWork->worklets.empty() && inWork->worklets.front() != nullptr) {
241 newWork->worklets.emplace_back(new C2Worklet);
242 newWork->worklets.front()->component = inWork->worklets.front()->component;
243 std::vector<std::unique_ptr<C2Tuning>> tunings;
244 for (std::unique_ptr<C2Tuning>& tuning : inWork->worklets.front()->tunings) {
245 tunings.push_back(std::move(
246 std::unique_ptr<C2Tuning>(
247 static_cast<C2Tuning*>(
248 C2Param::Copy(*(tuning.get())).release()))));
249 }
250 newWork->worklets.front()->tunings = std::move(tunings);
251 }
252 return newWork;
253 };
254 if (w->input.buffers.empty()
255 || (w->input.buffers.front() == nullptr)
256 || (!w->input.buffers.front()->hasInfo(
257 C2AccessUnitInfos::input::PARAM_TYPE))) {
258 LOG(DEBUG) << "Empty or MultiAU info buffer scatter frames with frameIndex "
259 << inputOrdinal.frameIndex.peekull()
260 << ") -> newFrameIndex " << newFrameIdx
261 <<" : input ts " << inputOrdinal.timestamp.peekull();
262 sliceWork.push_back(std::move(cloneInputWork(w, w->input.flags)));
263 if (!w->input.buffers.empty() && w->input.buffers.front() != nullptr) {
264 sliceWork.back()->input.buffers = std::move(w->input.buffers);
265 }
266 frameSet.insert(newFrameIdx);
267 processedWork->push_back(std::move(sliceWork));
268 } else {
269 const std::vector<std::shared_ptr<C2Buffer>>& inBuffers = w->input.buffers;
270 if (inBuffers.front()->data().linearBlocks().size() == 0) {
271 LOG(ERROR) << "ERROR: Work has Large frame info but has no linear blocks.";
272 return C2_CORRUPTED;
273 }
274 const std::vector<C2ConstLinearBlock>& multiAU =
275 inBuffers.front()->data().linearBlocks();
276 std::shared_ptr<const C2AccessUnitInfos::input> auInfo =
277 std::static_pointer_cast<const C2AccessUnitInfos::input>(
278 w->input.buffers.front()->getInfo(C2AccessUnitInfos::input::PARAM_TYPE));
279 uint32_t offset = 0; uint32_t multiAUSize = multiAU.front().size();
Arun Johnson2bd4df72024-01-29 20:07:27 +0000280 bool sendEos = false;
Arun Johnsonfb946102023-12-27 01:10:34 +0000281 for (int idx = 0; idx < auInfo->flexCount(); ++idx) {
282 std::vector<C2ConstLinearBlock> au;
283 const C2AccessUnitInfosStruct &info = auInfo->m.values[idx];
Arun Johnson2bd4df72024-01-29 20:07:27 +0000284 sendEos |= (info.flags & C2FrameData::FLAG_END_OF_STREAM);
Arun Johnsonfb946102023-12-27 01:10:34 +0000285 std::unique_ptr<C2Work> newWork = cloneInputWork(w, info.flags);
286 frameSet.insert(newFrameIdx);
287 newFrameIdx = mFrameIndex++;
288 newWork->input.ordinal.timestamp = info.timestamp;
289 au.push_back(multiAU.front().subBlock(offset, info.size));
290 if ((offset + info.size) > multiAUSize) {
291 LOG(ERROR) << "ERROR: access-unit offset > buffer size"
292 << " current offset " << (offset + info.size)
293 << " buffer size " << multiAUSize;
294 return C2_CORRUPTED;
295 }
296 newWork->input.buffers.push_back(
297 std::shared_ptr<C2Buffer>(new C2MultiAccessUnitBuffer(au)));
298 LOG(DEBUG) << "Frame scatter queuing frames WITH info in ordinal "
299 << inputOrdinal.frameIndex.peekull()
300 << " total offset " << offset << " info.size " << info.size
301 << " : TS " << newWork->input.ordinal.timestamp.peekull();
302 // add to worklist
303 sliceWork.push_back(std::move(newWork));
304 processedWork->push_back(std::move(sliceWork));
305 offset += info.size;
306 }
Arun Johnson2bd4df72024-01-29 20:07:27 +0000307 if (!sendEos && (w->input.flags & C2FrameData::FLAG_END_OF_STREAM)) {
308 if (!processedWork->empty()) {
309 std::list<std::unique_ptr<C2Work>> &sliceWork = processedWork->back();
310 if (!sliceWork.empty()) {
311 std::unique_ptr<C2Work> &work = sliceWork.back();
312 if (work) {
313 work->input.flags = C2FrameData::FLAG_END_OF_STREAM;
314 }
315 }
316 }
317 }
Arun Johnsonfb946102023-12-27 01:10:34 +0000318 }
319 if (!processedWork->empty()) {
320 {
321 C2LargeFrame::output multiAccessParams = mInterface->getLargeFrameParam();
322 if (mInterface->kind() == C2Component::KIND_DECODER) {
323 uint32_t sampleRate = 0;
324 uint32_t channelCount = 0;
325 uint32_t frameSize = 0;
326 mInterface->getDecoderSampleRateAndChannelCount(
327 sampleRate, channelCount);
328 if (sampleRate > 0 && channelCount > 0) {
329 frameSize = channelCount * 2;
330 multiAccessParams.maxSize =
331 (multiAccessParams.maxSize / frameSize) * frameSize;
332 multiAccessParams.thresholdSize =
333 (multiAccessParams.thresholdSize / frameSize) * frameSize;
334 }
335 }
336 frameInfo.mLargeFrameTuning = multiAccessParams;
337 std::lock_guard<std::mutex> l(mLock);
338 mFrameHolder.push_back(std::move(frameInfo));
339 }
340 }
341 }
342 return C2_OK;
343}
344
345c2_status_t MultiAccessUnitHelper::gather(
346 std::list<std::unique_ptr<C2Work>> &c2workItems,
347 std::list<std::unique_ptr<C2Work>>* const processedWork) {
348 LOG(DEBUG) << "Multi access-unit gather process";
349 if (processedWork == nullptr) {
350 LOG(ERROR) << "Nothing provided for processed work";
351 return C2_CORRUPTED;
352 }
353 auto addOutWork = [&processedWork](std::unique_ptr<C2Work>& work) {
354 processedWork->push_back(std::move(work));
355 };
356 {
357 std::lock_guard<std::mutex> l(mLock);
358 for (auto& work : c2workItems) {
359 LOG(DEBUG) << "FrameHolder Size: " << mFrameHolder.size();
360 uint64_t thisFrameIndex = work->input.ordinal.frameIndex.peekull();
361 bool removeEntry = work->worklets.empty()
362 || !work->worklets.front()
363 || (work->worklets.front()->output.flags
364 & C2FrameData::FLAG_INCOMPLETE) == 0;
365 bool foundFrame = false;
366 std::list<MultiAccessUnitInfo>::iterator frame =
367 mFrameHolder.begin();
368 while (!foundFrame && frame != mFrameHolder.end()) {
369 auto it = frame->mComponentFrameIds.find(thisFrameIndex);
370 if (it != frame->mComponentFrameIds.end()) {
371 foundFrame = true;
372 LOG(DEBUG) << "onWorkDone (frameIndex " << thisFrameIndex
373 << " worklstsSze " << work->worklets.size()
374 << ") -> frameIndex " << frame->inOrdinal.frameIndex.peekull();
375 if (work->result != C2_OK
376 || work->worklets.empty()
377 || !work->worklets.front()
378 || (frame->mLargeFrameTuning.thresholdSize == 0
379 || frame->mLargeFrameTuning.maxSize == 0)) {
380 if (removeEntry) {
381 frame->mComponentFrameIds.erase(it);
382 removeEntry = false;
383 }
384 if (frame->mLargeWork) {
385 finalizeWork(*frame);
386 addOutWork(frame->mLargeWork);
387 frame->reset();
388 }
389 c2_status_t workResult = work->result;
390 frame->mLargeWork = std::move(work);
391 frame->mLargeWork->input.ordinal.frameIndex =
392 frame->inOrdinal.frameIndex;
393 finalizeWork(*frame);
394 addOutWork(frame->mLargeWork);
395 frame->reset();
396 if (workResult != C2_OK) {
397 frame->mAccessUnitInfos.clear();
398 }
399 } else if (C2_OK != processWorklets(*frame, work, addOutWork)) {
400 LOG(DEBUG) << "Error while processing work";
401 }
402 if (removeEntry) {
403 LOG(DEBUG) << "Removing entry: " << thisFrameIndex
404 << " -> " << frame->inOrdinal.frameIndex.peekull();
405 frame->mComponentFrameIds.erase(it);
406 }
407 // This is to take care of the last bytes and to decide to send with
408 // FLAG_INCOMPLETE or not.
409 if ((frame->mWview
410 && (frame->mWview->offset() > frame->mLargeFrameTuning.thresholdSize))
411 || frame->mComponentFrameIds.empty()) {
412 if (frame->mLargeWork) {
413 finalizeWork(*frame);
414 addOutWork(frame->mLargeWork);
415 frame->reset();
416 }
417 }
418 if (frame->mComponentFrameIds.empty()) {
419 LOG(DEBUG) << "This frame is finished ID " << thisFrameIndex;
420 frame = mFrameHolder.erase(frame);
421 continue;
422 }
423 } else {
424 LOG(DEBUG) << "Received an out-of-order output " << thisFrameIndex
425 << " expected: " <<mFrameHolder.front().inOrdinal.frameIndex.peekull();
426 }
427 frame++;
428 }
429 if (!foundFrame) {
430 LOG(ERROR) <<" Error: Frame Holder reports no frame " << thisFrameIndex;
431 }
432 }
433 }
434 return C2_OK;
435}
436
437c2_status_t MultiAccessUnitHelper::createLinearBlock(MultiAccessUnitInfo &frame) {
438 if (!mInit) {
439 LOG(ERROR) << "Large buffer allocator failed";
440 return C2_NO_MEMORY;
441 }
442 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
443 uint32_t maxOutSize = frame.mLargeFrameTuning.maxSize;
444 c2_status_t err = mLinearPool->fetchLinearBlock(maxOutSize, usage, &frame.mBlock);
445 LOG(DEBUG) << "Allocated block with offset : " << frame.mBlock->offset()
446 << " size " << frame.mBlock->size() << " Capacity " << frame.mBlock->capacity();
447 if (err != C2_OK) {
448 LOG(ERROR) << "Error allocating Multi access-unit Buffer";
449 return err;
450 }
451 frame.mWview = std::make_shared<C2WriteView>(frame.mBlock->map().get());
452 LOG(DEBUG) << "Allocated buffer : requested size : " <<
453 frame.mLargeFrameTuning.maxSize
454 << " alloc size " << frame.mWview->size();
455 return C2_OK;
456}
457
458/*
459 * For every work from the component, we try to do aggregation of work here.
460*/
461c2_status_t MultiAccessUnitHelper::processWorklets(MultiAccessUnitInfo &frame,
462 std::unique_ptr<C2Work>& work,
463 const std::function <void(std::unique_ptr<C2Work>&)>& addWork) {
464 // This will allocate work, worklet, c2Block
465 auto allocateWork = [&](MultiAccessUnitInfo &frame,
466 bool allocateWorket = false,
467 bool allocateBuffer = false) {
468 c2_status_t ret = C2_OK;
469 if (frame.mLargeWork == nullptr) {
470 frame.mLargeWork.reset(new C2Work);
471 frame.mLargeWork->input.ordinal = frame.inOrdinal;
472 frame.mLargeWork->input.ordinal.frameIndex = frame.inOrdinal.frameIndex;
473 }
474 if (allocateWorket) {
475 if (frame.mLargeWork->worklets.size() == 0) {
476 frame.mLargeWork->worklets.emplace_back(new C2Worklet);
477 }
478 }
479 if (allocateBuffer) {
480 if (frame.mWview == nullptr) {
481 ret = createLinearBlock(frame);
482 }
483 }
484 return ret;
485 };
486 // we will only have one worklet.
487 bool foundEndOfStream = false;
488 for (auto worklet = work->worklets.begin();
489 worklet != work->worklets.end() && (*worklet) != nullptr; ++worklet) {
490 uint32_t flagsForNoCopy = C2FrameData::FLAG_DROP_FRAME
491 | C2FrameData::FLAG_DISCARD_FRAME
492 | C2FrameData::FLAG_CORRUPT;
493 if ((*worklet)->output.flags & flagsForNoCopy) {
494 if (frame.mLargeWork) {
495 finalizeWork(frame);
496 addWork(frame.mLargeWork);
497 frame.reset();
498 }
499 frame.mLargeWork = std::move(work);
500 frame.mLargeWork->input.ordinal.frameIndex = frame.inOrdinal.frameIndex;
501 finalizeWork(frame);
502 addWork(frame.mLargeWork);
503 frame.reset();
504 return C2_OK;
505 }
506 c2_status_t c2ret = allocateWork(frame, true);
507 if (c2ret != C2_OK) {
508 return c2ret;
509 }
510 C2FrameData& outputFramedata = frame.mLargeWork->worklets.front()->output;
511 if (!(*worklet)->output.configUpdate.empty()) {
512 for (auto& configUpdate : (*worklet)->output.configUpdate) {
513 outputFramedata.configUpdate.push_back(std::move(configUpdate));
514 }
515 (*worklet)->output.configUpdate.clear();
516 }
517 outputFramedata.infoBuffers.insert(outputFramedata.infoBuffers.begin(),
518 (*worklet)->output.infoBuffers.begin(),
519 (*worklet)->output.infoBuffers.end());
520 int64_t sampleTimeUs = 0;
521 uint32_t frameSize = 0;
522 uint32_t sampleRate = 0;
523 uint32_t channelCount = 0;
524 mInterface->getDecoderSampleRateAndChannelCount(sampleRate, channelCount);
525 if (sampleRate > 0 && channelCount > 0) {
526 sampleTimeUs = (1000000u) / (sampleRate * channelCount * 2);
527 frameSize = channelCount * 2;
528 }
529 LOG(DEBUG) << "maxOutSize " << frame.mLargeFrameTuning.maxSize
530 << " threshold " << frame.mLargeFrameTuning.thresholdSize;
531 if ((*worklet)->output.buffers.size() > 0) {
532 allocateWork(frame, true, true);
533 }
534 LOG(DEBUG) << "This worklet has " << (*worklet)->output.buffers.size() << " buffers"
535 << " ts: " << (*worklet)->output.ordinal.timestamp.peekull();
536 int64_t workletTimestamp = (*worklet)->output.ordinal.timestamp.peekull();
537 int64_t timestamp = workletTimestamp;
538 uint32_t flagsForCopy = ((*worklet)->output.flags) & C2FrameData::FLAG_CODEC_CONFIG;
539 for (int bufIdx = 0; bufIdx < (*worklet)->output.buffers.size(); ++bufIdx) {
540 std::shared_ptr<C2Buffer>& buffer = (*worklet)->output.buffers[bufIdx];
541 if (!buffer || buffer->data().linearBlocks().empty()) {
542 continue;
543 }
544 const std::vector<C2ConstLinearBlock>& blocks = buffer->data().linearBlocks();
545 if (blocks.size() > 0) {
546 uint32_t inputOffset = 0;
547 uint32_t inputSize = blocks.front().size();
548 frame.mInfos.insert(frame.mInfos.end(),
549 buffer->info().begin(), buffer->info().end());
550 if (frameSize != 0 && (mInterface->kind() == C2Component::KIND_DECODER)) {
551 // For decoders we only split multiples of 16bChannelCount*2
552 inputSize -= (inputSize % frameSize);
553 }
554 while (inputOffset < inputSize) {
555 if (frame.mWview->offset() >= frame.mLargeFrameTuning.thresholdSize) {
556 frame.mLargeWork->result = C2_OK;
557 finalizeWork(frame, flagsForCopy);
558 addWork(frame.mLargeWork);
559 frame.reset();
560 allocateWork(frame, true, true);
561 }
562 if (mInterface->kind() == C2Component::KIND_ENCODER) {
563 if (inputSize > frame.mLargeFrameTuning.maxSize) {
564 LOG(ERROR) << "Enc: Output buffer too small for AU, configured with "
565 << frame.mLargeFrameTuning.maxSize
566 << " block size: " << blocks.front().size()
567 << "alloc size " << frame.mWview->size();
568 if (frame.mLargeWork
569 && frame.mWview && frame.mWview->offset() > 0) {
570 finalizeWork(frame, flagsForCopy);
571 addWork(frame.mLargeWork);
572 frame.reset();
573 allocateWork(frame, true, false);
574 }
575 frame.mLargeWork->result = C2_NO_MEMORY;
576 finalizeWork(frame, 0, true);
577 addWork(frame.mLargeWork);
578 frame.reset();
579 return C2_NO_MEMORY;
580 } else if (inputSize > frame.mWview->size()) {
581 LOG(DEBUG) << "Enc: Large frame hitting bufer limit, current size "
582 << frame.mWview->offset();
583 if (frame.mLargeWork
584 && frame.mWview && frame.mWview->offset() > 0) {
585 finalizeWork(frame, flagsForCopy);
586 addWork(frame.mLargeWork);
587 frame.reset();
588 allocateWork(frame, true, true);
589 }
590 }
591 }
592 C2ReadView rView = blocks.front().map().get();
593 if (rView.error()) {
594 LOG(ERROR) << "Buffer read view error";
595 frame.mLargeWork->result = rView.error();
596 frame.mLargeWork->worklets.clear();
597 finalizeWork(frame, 0, true);
598 addWork(frame.mLargeWork);
599 frame.reset();
600 return C2_NO_MEMORY;
601 }
602 uint32_t toCopy = 0;
603 if (mInterface->kind() == C2Component::KIND_ENCODER) {
604 toCopy = inputSize;
605 } else {
606 toCopy = c2_min(frame.mWview->size(), (inputSize - inputOffset));
607 timestamp = workletTimestamp + inputOffset * sampleTimeUs;
608 LOG(DEBUG) << "ts " << timestamp
609 << " copiedOutput " << inputOffset
610 << " sampleTimeUs " << sampleTimeUs;
611 }
612 LOG(DEBUG) << " Copy size " << toCopy
613 << " ts " << timestamp;
614 memcpy(frame.mWview->data(), rView.data() + inputOffset, toCopy);
615 frame.mWview->setOffset(frame.mWview->offset() + toCopy);
616 inputOffset += toCopy;
617 mergeAccessUnitInfo(frame, flagsForCopy, toCopy, timestamp);
618 }
619 } else {
620 frame.mLargeWork->worklets.front()->output.buffers.push_back(std::move(buffer));
621 LOG(DEBUG) << "Copying worklets without linear buffer";
622 }
623 }
624 uint32_t flagsForCsdOrEnd = (*worklet)->output.flags
625 & (C2FrameData::FLAG_END_OF_STREAM | C2FrameData::FLAG_CODEC_CONFIG);
626 if (flagsForCsdOrEnd) {
627 LOG(DEBUG) << "Output worklet has CSD/EOS data";
628 frame.mLargeWork->result = C2_OK;
629 // we can assign timestamp as this will be evaluated in finalizeWork
630 frame.mLargeWork->worklets.front()->output.ordinal.timestamp = timestamp;
631 finalizeWork(frame, flagsForCsdOrEnd, true);
632 addWork(frame.mLargeWork);
633 frame.reset();
634 }
635 }
636 return C2_OK;
637}
638
639c2_status_t MultiAccessUnitHelper::finalizeWork(
640 MultiAccessUnitInfo& frame, uint32_t inFlags, bool forceComplete) {
641 if (frame.mLargeWork == nullptr) {
642 return C2_OK;
643 }
644 //prepare input ordinal
645 frame.mLargeWork->input.ordinal = frame.inOrdinal;
646 // remove this
647 int64_t timeStampUs = frame.inOrdinal.timestamp.peekull();
648 if (!frame.mAccessUnitInfos.empty()) {
649 timeStampUs = frame.mAccessUnitInfos.front().timestamp;
650 } else if (!frame.mLargeWork->worklets.empty()) {
651 std::unique_ptr<C2Worklet> &worklet = frame.mLargeWork->worklets.front();
652 if (worklet) {
653 timeStampUs = worklet->output.ordinal.timestamp.peekull();
654 }
655 }
656 LOG(DEBUG) << "Finalizing work with input Idx "
657 << frame.mLargeWork->input.ordinal.frameIndex.peekull()
658 << " timestamp " << timeStampUs;
659 uint32_t finalFlags = 0;
660 if ((!forceComplete)
661 && (frame.mLargeWork->result == C2_OK)
662 && (!frame.mComponentFrameIds.empty())) {
663 finalFlags |= C2FrameData::FLAG_INCOMPLETE;
664 }
665 if (frame.mLargeWork->result == C2_OK) {
666 finalFlags |= inFlags;
667 }
668 // update worklet if present
669 if (!frame.mLargeWork->worklets.empty() &&
670 frame.mLargeWork->worklets.front() != nullptr) {
671 frame.mLargeWork->workletsProcessed = 1;
672 C2FrameData& outFrameData = frame.mLargeWork->worklets.front()->output;
673 outFrameData.ordinal.frameIndex = frame.inOrdinal.frameIndex.peekull();
674 outFrameData.ordinal.timestamp = timeStampUs;
675 finalFlags |= frame.mLargeWork->worklets.front()->output.flags;
676 outFrameData.flags = (C2FrameData::flags_t)finalFlags;
677 // update buffers
678 if (frame.mBlock && (frame.mWview->offset() > 0)) {
679 size_t size = frame.mWview->offset();
680 LOG(DEBUG) << "Finalize : Block: Large frame size set as " << size
681 << " timestamp as " << timeStampUs
682 << "frameIndex " << outFrameData.ordinal.frameIndex.peekull();
683 frame.mWview->setOffset(0);
684 std::shared_ptr<C2Buffer> c2Buffer = C2Buffer::CreateLinearBuffer(
685 frame.mBlock->share(0, size, ::C2Fence()));
686 if (frame.mAccessUnitInfos.size() > 0) {
687 if (finalFlags & C2FrameData::FLAG_END_OF_STREAM) {
688 frame.mAccessUnitInfos.back().flags |=
689 C2FrameData::FLAG_END_OF_STREAM;
690 }
691 std::shared_ptr<C2AccessUnitInfos::output> largeFrame =
692 C2AccessUnitInfos::output::AllocShared(
693 frame.mAccessUnitInfos.size(), 0u, frame.mAccessUnitInfos);
694 frame.mInfos.push_back(largeFrame);
695 frame.mAccessUnitInfos.clear();
696 }
697 for (auto &info : frame.mInfos) {
698 c2Buffer->setInfo(std::const_pointer_cast<C2Info>(info));
699 }
700 frame.mLargeWork->worklets.front()->output.buffers.push_back(std::move(c2Buffer));
701 frame.mInfos.clear();
702 frame.mBlock.reset();
703 frame.mWview.reset();
704 }
705 }
706 LOG(DEBUG) << "Multi access-unitflag setting as " << finalFlags;
707 return C2_OK;
708}
709
710void MultiAccessUnitHelper::mergeAccessUnitInfo(
711 MultiAccessUnitInfo &frame,
712 uint32_t flags_,
713 uint32_t size,
714 int64_t timestamp) {
715 // Remove flags that are not part of Access unit info
716 uint32_t flags = flags_ & ~(C2FrameData::FLAG_INCOMPLETE
717 | C2FrameData::FLAG_DISCARD_FRAME
718 | C2FrameData::FLAG_CORRUPT
719 | C2FrameData::FLAG_CORRECTED);
720 if (frame.mAccessUnitInfos.empty()) {
721 frame.mAccessUnitInfos.emplace_back(flags, size, timestamp);
722 return;
723 }
724 if ((mInterface->kind() == C2Component::KIND_DECODER) &&
725 (frame.mAccessUnitInfos.back().flags == flags)) {
726 // merge access units here
727 C2AccessUnitInfosStruct &s = frame.mAccessUnitInfos.back();
728 s.size += size; // don't have to update timestamp
729 } else {
730 frame.mAccessUnitInfos.emplace_back(flags, size, timestamp);
731 }
732}
733
734void MultiAccessUnitHelper::MultiAccessUnitInfo::reset() {
735 mBlock.reset();
736 mWview.reset();
737 mInfos.clear();
738 mAccessUnitInfos.clear();
739 mLargeWork.reset();
740}
741
742} // namespace android