-
Notifications
You must be signed in to change notification settings - Fork 8
/
C2FFMPEGVideoDecodeComponent.cpp
704 lines (606 loc) · 23.5 KB
/
C2FFMPEGVideoDecodeComponent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/*
* Copyright 2022 Michael Goffioul <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "C2FFMPEGVideoDecodeComponent"
#include <android-base/properties.h>
#include <log/log.h>
#include <algorithm>
#include <SimpleC2Interface.h>
#include "C2FFMPEGVideoDecodeComponent.h"
#include "ffmpeg_hwaccel.h"
#define DEBUG_FRAMES 0
#define DEBUG_WORKQUEUE 0
#define DEBUG_EXTRADATA 0
namespace android {
C2FFMPEGVideoDecodeComponent::C2FFMPEGVideoDecodeComponent(
const C2FFMPEGComponentInfo* componentInfo,
const std::shared_ptr<C2FFMPEGVideoDecodeInterface>& intf)
: SimpleC2Component(std::make_shared<SimpleInterface<C2FFMPEGVideoDecodeInterface>>(componentInfo->name, 0, intf)),
mInfo(componentInfo),
mIntf(intf),
mCodecID(componentInfo->codecID),
mCtx(NULL),
mImgConvertCtx(NULL),
mFrame(NULL),
mPacket(NULL),
mFFMPEGInitialized(false),
mCodecAlreadyOpened(false),
mExtradataReady(false),
mEOSSignalled(false) {
ALOGD("C2FFMPEGVideoDecodeComponent: mediaType = %s", componentInfo->mediaType);
}
C2FFMPEGVideoDecodeComponent::~C2FFMPEGVideoDecodeComponent() {
ALOGD("~C2FFMPEGVideoDecodeComponent: mCtx = %p", mCtx);
onRelease();
}
c2_status_t C2FFMPEGVideoDecodeComponent::initDecoder() {
if (! mFFMPEGInitialized) {
if (initFFmpeg() != C2_OK) {
ALOGE("initDecoder: FFMPEG initialization failed.");
return C2_NO_INIT;
}
mFFMPEGInitialized = true;
}
mCtx = avcodec_alloc_context3(NULL);
if (! mCtx) {
ALOGE("initDecoder: avcodec_alloc_context failed.");
return C2_NO_MEMORY;
}
C2StreamPictureSizeInfo::output size(0u, 320, 240);
c2_status_t err = mIntf->query({ &size }, {}, C2_DONT_BLOCK, nullptr);
if (err != C2_OK) {
ALOGE("initDecoder: cannot query picture size, err = %d", err);
}
mCtx->codec_type = AVMEDIA_TYPE_VIDEO;
mCtx->codec_id = mCodecID;
mCtx->extradata_size = 0;
mCtx->extradata = NULL;
mCtx->width = size.width;
mCtx->height = size.height;
ALOGD("initDecoder: %p [%s], %d x %d, %s",
mCtx, avcodec_get_name(mCtx->codec_id), size.width, size.height, mInfo->mediaType);
return C2_OK;
}
c2_status_t C2FFMPEGVideoDecodeComponent::openDecoder() {
if (mCodecAlreadyOpened) {
return C2_OK;
}
// Can't change extradata after opening the decoder.
#if DEBUG_EXTRADATA
ALOGD("openDecoder: extradata_size = %d", mCtx->extradata_size);
#endif
mExtradataReady = true;
// Find decoder again as codec_id may have changed.
if (mCtx->codec_id == AV_CODEC_ID_H264 &&
base::GetBoolProperty("persist.ffmpeg_codec2.v4l2.h264", false)) {
mCtx->codec = avcodec_find_decoder_by_name("h264_v4l2m2m");
} else {
mCtx->codec = avcodec_find_decoder(mCtx->codec_id);
}
if (! mCtx->codec) {
ALOGE("openDecoder: ffmpeg video decoder failed to find codec %d", mCtx->codec_id);
return C2_NOT_FOUND;
}
// Configure decoder.
mCtx->workaround_bugs = 1;
mCtx->idct_algo = 0;
mCtx->skip_frame = AVDISCARD_DEFAULT;
mCtx->skip_idct = AVDISCARD_DEFAULT;
mCtx->skip_loop_filter = AVDISCARD_DEFAULT;
mCtx->error_concealment = 3;
mCtx->thread_count = base::GetIntProperty("debug.ffmpeg_codec2.threads", 0);
if (base::GetBoolProperty("debug.ffmpeg_codec2.fast", false)) {
mCtx->flags2 |= AV_CODEC_FLAG2_FAST;
}
ffmpeg_hwaccel_init(mCtx);
ALOGD("openDecoder: opening ffmpeg decoder(%s): threads = %d, hw = %s",
avcodec_get_name(mCtx->codec_id), mCtx->thread_count, mCtx->hw_device_ctx ? "yes" : "no");
int err = avcodec_open2(mCtx, mCtx->codec, NULL);
if (err < 0) {
ALOGE("openDecoder: ffmpeg video decoder failed to initialize. (%s)", av_err2str(err));
return C2_NO_INIT;
}
mCodecAlreadyOpened = true;
ALOGD("openDecoder: open ffmpeg video decoder(%s) success, caps = %08x",
avcodec_get_name(mCtx->codec_id), mCtx->codec->capabilities);
mFrame = av_frame_alloc();
if (! mFrame) {
ALOGE("openDecoder: oom for video frame");
return C2_NO_MEMORY;
}
return C2_OK;
}
void C2FFMPEGVideoDecodeComponent::deInitDecoder() {
ALOGD("%p deInitDecoder: %p", this, mCtx);
if (mCtx) {
if (avcodec_is_open(mCtx)) {
avcodec_flush_buffers(mCtx);
}
if (mCtx->extradata) {
av_free(mCtx->extradata);
mCtx->extradata = NULL;
mCtx->extradata_size = 0;
}
if (mCodecAlreadyOpened) {
avcodec_close(mCtx);
mCodecAlreadyOpened = false;
}
ffmpeg_hwaccel_deinit(mCtx);
av_freep(&mCtx);
}
if (mFrame) {
av_frame_free(&mFrame);
mFrame = NULL;
}
if (mPacket) {
av_packet_free(&mPacket);
mPacket = NULL;
}
if (mImgConvertCtx) {
sws_freeContext(mImgConvertCtx);
mImgConvertCtx = NULL;
}
mEOSSignalled = false;
mExtradataReady = false;
mPendingWorkQueue.clear();
}
c2_status_t C2FFMPEGVideoDecodeComponent::processCodecConfig(C2ReadView* inBuffer) {
int orig_extradata_size = mCtx->extradata_size;
int add_extradata_size = inBuffer->capacity();
#if DEBUG_EXTRADATA
ALOGD("processCodecConfig: add = %u, current = %d", add_extradata_size, orig_extradata_size);
#endif
if (! mExtradataReady) {
mCtx->extradata_size += add_extradata_size;
mCtx->extradata = (uint8_t *) realloc(mCtx->extradata, mCtx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
if (! mCtx->extradata) {
ALOGE("processCodecConfig: ffmpeg video decoder failed to alloc extradata memory.");
return C2_NO_MEMORY;
}
memcpy(mCtx->extradata + orig_extradata_size, inBuffer->data(), add_extradata_size);
memset(mCtx->extradata + mCtx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
}
else {
ALOGW("processCodecConfig: decoder is already opened, ignoring...");
}
return C2_OK;
}
c2_status_t C2FFMPEGVideoDecodeComponent::sendInputBuffer(
C2ReadView *inBuffer, int64_t timestamp) {
if (!mPacket) {
mPacket = av_packet_alloc();
if (!mPacket) {
ALOGE("sendInputBuffer: oom for video packet");
return C2_NO_MEMORY;
}
}
mPacket->data = inBuffer ? const_cast<uint8_t *>(inBuffer->data()) : NULL;
mPacket->size = inBuffer ? inBuffer->capacity() : 0;
mPacket->pts = timestamp;
mPacket->dts = AV_NOPTS_VALUE;
int err = avcodec_send_packet(mCtx, mPacket);
av_packet_unref(mPacket);
if (err < 0) {
ALOGE("sendInputBuffer: failed to send data (%d) to decoder: %s (%08x)",
inBuffer->capacity(), av_err2str(err), err);
if (err == AVERROR(EAGAIN)) {
// Frames must be read first, notify main decoding loop.
ALOGD("sendInputBuffer: returning C2_BAD_STATE");
return C2_BAD_STATE;
}
// Otherwise don't send error to client.
}
return C2_OK;
}
c2_status_t C2FFMPEGVideoDecodeComponent::receiveFrame(bool* hasPicture) {
int err = avcodec_receive_frame(mCtx, mFrame);
*hasPicture = false;
if (err == 0) {
err = ffmpeg_hwaccel_get_frame(mCtx, mFrame);
if (err == 0) {
*hasPicture = true;
} else {
ALOGE("receiveFrame: failed to receive frame from HW decoder err = %d", err);
// Don't send error to client, skip frame!
}
} else if (err != AVERROR(EAGAIN) && err != AVERROR_EOF) {
ALOGE("receiveFrame: failed to receive frame from decoder err = %d", err);
// Don't report error to client.
}
return C2_OK;
}
c2_status_t C2FFMPEGVideoDecodeComponent::getOutputBuffer(C2GraphicView* outBuffer) {
uint8_t* data[4];
int linesize[4];
C2PlanarLayout layout = outBuffer->layout();
struct SwsContext* currentImgConvertCtx = mImgConvertCtx;
data[0] = outBuffer->data()[C2PlanarLayout::PLANE_Y];
data[1] = outBuffer->data()[C2PlanarLayout::PLANE_U];
data[2] = outBuffer->data()[C2PlanarLayout::PLANE_V];
linesize[0] = layout.planes[C2PlanarLayout::PLANE_Y].rowInc;
linesize[1] = layout.planes[C2PlanarLayout::PLANE_U].rowInc;
linesize[2] = layout.planes[C2PlanarLayout::PLANE_V].rowInc;
mImgConvertCtx = sws_getCachedContext(currentImgConvertCtx,
mFrame->width, mFrame->height, (AVPixelFormat)mFrame->format,
mFrame->width, mFrame->height, AV_PIX_FMT_YUV420P,
SWS_BICUBIC, NULL, NULL, NULL);
if (mImgConvertCtx && mImgConvertCtx != currentImgConvertCtx) {
ALOGD("getOutputBuffer: created video converter - %s => %s",
av_get_pix_fmt_name((AVPixelFormat)mFrame->format), av_get_pix_fmt_name(AV_PIX_FMT_YUV420P));
} else if (! mImgConvertCtx) {
ALOGE("getOutputBuffer: cannot initialize the conversion context");
return C2_NO_MEMORY;
}
sws_scale(mImgConvertCtx, mFrame->data, mFrame->linesize,
0, mFrame->height, data, linesize);
return C2_OK;
}
static void fillEmptyWork(const std::unique_ptr<C2Work>& work) {
work->worklets.front()->output.flags =
(C2FrameData::flags_t)(work->input.flags & C2FrameData::FLAG_END_OF_STREAM);
work->worklets.front()->output.buffers.clear();
work->worklets.front()->output.ordinal = work->input.ordinal;
work->workletsProcessed = 1u;
work->result = C2_OK;
#if DEBUG_WORKQUEUE
ALOGD("WorkQueue: drop idx=%" PRIu64 ", ts=%" PRIu64,
work->input.ordinal.frameIndex.peeku(), work->input.ordinal.timestamp.peeku());
#endif
}
static bool comparePendingWork(const PendingWork& w1, const PendingWork& w2) {
return w1.second < w2.second;
}
void C2FFMPEGVideoDecodeComponent::pushPendingWork(const std::unique_ptr<C2Work>& work) {
uint32_t outputDelay = mIntf->getOutputDelay();
if (mPendingWorkQueue.size() >= outputDelay) {
uint32_t newOutputDelay = outputDelay;
std::vector<std::unique_ptr<C2Param>> configUpdate;
switch (mCtx->codec_id) {
case AV_CODEC_ID_HEVC:
case AV_CODEC_ID_H264:
// Increase output delay step-wise.
if (outputDelay >= 18u) {
newOutputDelay = 34u;
} else if (outputDelay >= 8u) {
newOutputDelay = 18u;
} else {
newOutputDelay = 8u;
}
break;
default:
// Other codecs use constant output delay.
break;
}
if (newOutputDelay != outputDelay) {
C2PortActualDelayTuning::output delay(newOutputDelay);
std::vector<std::unique_ptr<C2SettingResult>> failures;
int err;
err = mIntf->config({ &delay }, C2_MAY_BLOCK, &failures);
if (err == C2_OK) {
ALOGD("WorkQueue: queue full, output delay set to %u", newOutputDelay);
configUpdate.push_back(C2Param::Copy(delay));
} else {
ALOGE("WorkQueue: output delay update to %u failed err = %d",
newOutputDelay, err);
}
}
auto fillEmptyWorkWithConfigUpdate = [&configUpdate](const std::unique_ptr<C2Work>& work) {
fillEmptyWork(work);
work->worklets.front()->output.configUpdate = std::move(configUpdate);
};
finish(mPendingWorkQueue.front().first, fillEmptyWorkWithConfigUpdate);
mPendingWorkQueue.pop_front();
}
#if DEBUG_WORKQUEUE
ALOGD("WorkQueue: push idx=%" PRIu64 ", ts=%" PRIu64,
work->input.ordinal.frameIndex.peeku(), work->input.ordinal.timestamp.peeku());
#endif
mPendingWorkQueue.push_back(PendingWork(work->input.ordinal.frameIndex.peeku(),
work->input.ordinal.timestamp.peeku()));
std::sort(mPendingWorkQueue.begin(), mPendingWorkQueue.end(), comparePendingWork);
}
void C2FFMPEGVideoDecodeComponent::popPendingWork(const std::unique_ptr<C2Work>& work) {
uint64_t index = work->input.ordinal.frameIndex.peeku();
auto it = std::find_if(mPendingWorkQueue.begin(), mPendingWorkQueue.end(),
[index](const PendingWork& pWork) { return index == pWork.first; });
#if DEBUG_WORKQUEUE
ALOGD("WorkQueue: pop idx=%" PRIu64 ", ts=%" PRIu64,
work->input.ordinal.frameIndex.peeku(), work->input.ordinal.timestamp.peeku());
#endif
if (it != mPendingWorkQueue.end()) {
mPendingWorkQueue.erase(it);
}
#if DEBUG_WORKQUEUE
else {
ALOGD("WorkQueue: pop work not found idx=%" PRIu64 ", ts=%" PRIu64,
work->input.ordinal.frameIndex.peeku(), work->input.ordinal.timestamp.peeku());
}
#endif
prunePendingWorksUntil(work);
}
void C2FFMPEGVideoDecodeComponent::prunePendingWorksUntil(const std::unique_ptr<C2Work>& work) {
#if DEBUG_WORKQUEUE
ALOGD("WorkQueue: prune until idx=%" PRIu64 ", ts=%" PRIu64,
work->input.ordinal.frameIndex.peeku(), work->input.ordinal.timestamp.peeku());
#endif
// Drop all works with a PTS earlier than provided argument.
while (mPendingWorkQueue.size() > 0 &&
mPendingWorkQueue.front().second < work->input.ordinal.timestamp.peeku()) {
finish(mPendingWorkQueue.front().first, fillEmptyWork);
mPendingWorkQueue.pop_front();
}
}
c2_status_t C2FFMPEGVideoDecodeComponent::onInit() {
ALOGD("onInit");
return initDecoder();
}
c2_status_t C2FFMPEGVideoDecodeComponent::onStop() {
ALOGD("onStop");
return C2_OK;
}
void C2FFMPEGVideoDecodeComponent::onReset() {
ALOGD("onReset");
deInitDecoder();
initDecoder();
}
void C2FFMPEGVideoDecodeComponent::onRelease() {
ALOGD("onRelease");
deInitDecoder();
if (mFFMPEGInitialized) {
deInitFFmpeg();
mFFMPEGInitialized = false;
}
}
c2_status_t C2FFMPEGVideoDecodeComponent::onFlush_sm() {
ALOGD("onFlush_sm");
if (mCtx && avcodec_is_open(mCtx)) {
// Make sure that the next buffer output does not still
// depend on fragments from the last one decoded.
avcodec_flush_buffers(mCtx);
mEOSSignalled = false;
}
return C2_OK;
}
c2_status_t C2FFMPEGVideoDecodeComponent::outputFrame(
const std::unique_ptr<C2Work>& work,
const std::shared_ptr<C2BlockPool> &pool
) {
c2_status_t err;
std::vector<std::unique_ptr<C2Param>> configUpdate;
#if DEBUG_FRAMES
ALOGD("outputFrame: pts=%" PRId64 " dts=%" PRId64 " ts=%" PRId64 " - %d x %d (%x)",
mFrame->pts, mFrame->pkt_dts, mFrame->best_effort_timestamp, mFrame->width, mFrame->height, mFrame->format);
#endif
if (mFrame->width != mIntf->getWidth() || mFrame->height != mIntf->getHeight()) {
ALOGD("outputFrame: video params changed - %d x %d (%x)", mFrame->width, mFrame->height, mFrame->format);
C2StreamPictureSizeInfo::output size(0u, mFrame->width, mFrame->height);
std::vector<std::unique_ptr<C2SettingResult>> failures;
err = mIntf->config({ &size }, C2_MAY_BLOCK, &failures);
if (err == OK) {
configUpdate.push_back(C2Param::Copy(size));
mCtx->width = mFrame->width;
mCtx->height = mFrame->height;
} else {
ALOGE("outputFrame: config update failed err = %d", err);
return C2_CORRUPTED;
}
}
std::shared_ptr<C2GraphicBlock> block;
err = pool->fetchGraphicBlock(mFrame->width, mFrame->height, HAL_PIXEL_FORMAT_YV12,
{ C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE }, &block);
if (err != C2_OK) {
ALOGE("outputFrame: failed to fetch graphic block %d x %d (%x) err = %d",
mFrame->width, mFrame->height, HAL_PIXEL_FORMAT_YV12, err);
return C2_CORRUPTED;
}
C2GraphicView wView = block->map().get();
err = wView.error();
if (err != C2_OK) {
ALOGE("outputFrame: graphic view map failed err = %d", err);
return C2_CORRUPTED;
}
err = getOutputBuffer(&wView);
if (err == C2_OK) {
std::shared_ptr<C2Buffer> buffer = createGraphicBuffer(std::move(block), C2Rect(mFrame->width, mFrame->height));
buffer->setInfo(mIntf->getPixelFormatInfo());
if (work && c2_cntr64_t(mFrame->best_effort_timestamp) == work->input.ordinal.frameIndex) {
prunePendingWorksUntil(work);
work->worklets.front()->output.configUpdate = std::move(configUpdate);
work->worklets.front()->output.buffers.clear();
work->worklets.front()->output.buffers.push_back(buffer);
work->worklets.front()->output.ordinal = work->input.ordinal;
work->workletsProcessed = 1u;
work->result = C2_OK;
} else {
auto fillWork = [buffer, &configUpdate, this](const std::unique_ptr<C2Work>& work) {
popPendingWork(work);
work->worklets.front()->output.configUpdate = std::move(configUpdate);
work->worklets.front()->output.flags = (C2FrameData::flags_t)0;
work->worklets.front()->output.buffers.clear();
work->worklets.front()->output.buffers.push_back(buffer);
work->worklets.front()->output.ordinal = work->input.ordinal;
work->workletsProcessed = 1u;
work->result = C2_OK;
#if DEBUG_FRAMES
ALOGD("outputFrame: work(finish) idx=%" PRIu64 ", processed=%u, result=%d",
work->input.ordinal.frameIndex.peeku(), work->workletsProcessed, work->result);
#endif
};
finish(mFrame->best_effort_timestamp, fillWork);
}
} else {
return err;
}
return C2_OK;
}
void C2FFMPEGVideoDecodeComponent::process(
const std::unique_ptr<C2Work> &work,
const std::shared_ptr<C2BlockPool> &pool
) {
size_t inSize = 0u;
bool eos = (work->input.flags & C2FrameData::FLAG_END_OF_STREAM);
C2ReadView rView = mDummyReadView;
bool hasInputBuffer = false;
if (! work->input.buffers.empty()) {
rView = work->input.buffers[0]->data().linearBlocks().front().map().get();
inSize = rView.capacity();
hasInputBuffer = true;
}
#if DEBUG_FRAMES
ALOGD("process: input flags=%08x ts=%lu idx=%lu #buf=%lu[%lu] #conf=%lu #info=%lu",
work->input.flags, work->input.ordinal.timestamp.peeku(), work->input.ordinal.frameIndex.peeku(),
work->input.buffers.size(), inSize, work->input.configUpdate.size(), work->input.infoBuffers.size());
#endif
if (mEOSSignalled) {
ALOGE("process: ignoring work while EOS reached");
work->workletsProcessed = 0u;
work->result = C2_BAD_VALUE;
return;
}
if (hasInputBuffer && rView.error()) {
ALOGE("process: read view map failed err = %d", rView.error());
work->workletsProcessed = 0u;
work->result = rView.error();
return;
}
// In all cases the work is marked as completed.
//
// There is not always a 1:1 mapping between input and output frames, in particular for
// interlaced content. Keeping the corresponding worklets in the queue quickly fills it
// in and stalls the decoder. But there's no obvious mechanism to determine, from
// FFMPEG API, whether a given packet will produce an output frame and the worklet should
// be kept around so it can be completed when the frame is produced.
//
// NOTE: This has an impact on the drain operation.
work->result = C2_OK;
work->worklets.front()->output.flags = (C2FrameData::flags_t)0;
work->workletsProcessed = 0u;
if (inSize || (eos && mCodecAlreadyOpened)) {
c2_status_t err = C2_OK;
if (work->input.flags & C2FrameData::FLAG_CODEC_CONFIG) {
work->workletsProcessed = 1u;
work->result = processCodecConfig(&rView);
return;
}
if (! mCodecAlreadyOpened) {
err = openDecoder();
if (err != C2_OK) {
work->workletsProcessed = 1u;
work->result = err;
return;
}
}
bool inputConsumed = false;
bool outputAvailable = true;
bool hasPicture = false;
#if DEBUG_FRAMES
int outputFrameCount = 0;
#endif
while (!inputConsumed || outputAvailable) {
if (!inputConsumed) {
err = sendInputBuffer(&rView, work->input.ordinal.frameIndex.peekll());
if (err == C2_OK) {
inputConsumed = true;
outputAvailable = true;
work->input.buffers.clear();
} else if (err != C2_BAD_STATE) {
work->workletsProcessed = 1u;
work->result = err;
return;
}
}
if (outputAvailable) {
hasPicture = false;
err = receiveFrame(&hasPicture);
if (err != C2_OK) {
work->workletsProcessed = 1u;
work->result = err;
return;
}
if (hasPicture) {
err = outputFrame(work, pool);
if (err != C2_OK) {
work->workletsProcessed = 1u;
work->result = err;
return;
}
#if DEBUG_FRAMES
else {
outputFrameCount++;
}
#endif
}
else {
#if DEBUG_FRAMES
if (!outputFrameCount) {
ALOGD("process: no frame");
}
#endif
outputAvailable = false;
}
}
}
}
#if DEBUG_FRAMES
else {
ALOGD("process: empty work");
}
#endif
if (eos) {
mEOSSignalled = true;
work->worklets.front()->output.flags = C2FrameData::FLAG_END_OF_STREAM;
work->workletsProcessed = 1u;
}
if (work->workletsProcessed == 0u) {
pushPendingWork(work);
}
#if DEBUG_FRAMES
ALOGD("process: work(end) idx=%" PRIu64 ", processed=%u, result=%d",
work->input.ordinal.frameIndex.peeku(), work->workletsProcessed, work->result);
#endif
}
c2_status_t C2FFMPEGVideoDecodeComponent::drain(
uint32_t drainMode,
const std::shared_ptr<C2BlockPool>& pool
) {
ALOGD("drain: mode = %u", drainMode);
if (drainMode == NO_DRAIN) {
ALOGW("drain: NO_DRAIN is no-op");
return C2_OK;
}
if (drainMode == DRAIN_CHAIN) {
ALOGW("drain: DRAIN_CHAIN not supported");
return C2_OMITTED;
}
if (! mCodecAlreadyOpened) {
ALOGW("drain: codec not opened yet");
return C2_OK;
}
bool hasPicture = false;
c2_status_t err = C2_OK;
err = sendInputBuffer(NULL, 0);
while (err == C2_OK) {
hasPicture = false;
err = receiveFrame(&hasPicture);
if (hasPicture) {
// Ignore errors at this point, just drain the decoder.
outputFrame(nullptr, pool);
} else {
err = C2_NOT_FOUND;
}
}
return C2_OK;
}
} // namespace android