-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathC2FFMPEGAudioDecodeComponent.cpp
More file actions
708 lines (602 loc) · 23.8 KB
/
C2FFMPEGAudioDecodeComponent.cpp
File metadata and controls
708 lines (602 loc) · 23.8 KB
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
705
706
707
708
/*
* Copyright 2022 Michael Goffioul <michael.goffioul@gmail.com>
*
* 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 "C2FFMPEGAudioDecodeComponent"
#include <android-base/stringprintf.h>
#include <log/log.h>
#include <SimpleC2Interface.h>
#include "C2FFMPEGAudioDecodeComponent.h"
#include <libswresample/swresample_internal.h>
#define DEBUG_FRAMES 0
#define DEBUG_EXTRADATA 0
namespace android {
static enum AVSampleFormat convertFormatToFFMPEG(C2Config::pcm_encoding_t encoding) {
switch (encoding) {
case C2Config::PCM_8: return AV_SAMPLE_FMT_U8;
case C2Config::PCM_16: return AV_SAMPLE_FMT_S16;
case C2Config::PCM_32: return AV_SAMPLE_FMT_S32;
case C2Config::PCM_FLOAT: return AV_SAMPLE_FMT_FLT;
default: return AV_SAMPLE_FMT_NONE;
}
}
__unused
static C2Config::pcm_encoding_t convertFormatToC2(enum AVSampleFormat format) {
switch (format) {
case AV_SAMPLE_FMT_U8: return C2Config::PCM_8;
case AV_SAMPLE_FMT_S16: return C2Config::PCM_16;
case AV_SAMPLE_FMT_S32: return C2Config::PCM_32;
case AV_SAMPLE_FMT_FLT: return C2Config::PCM_FLOAT;
default: return C2Config::PCM_16;
}
}
// Helper structures to encapsulate the specific codec behaviors.
// Currently only used to process extradata.
struct CodecHelper {
virtual ~CodecHelper() {}
virtual c2_status_t onCodecConfig(AVCodecContext* mCtx, C2ReadView* inBuffer);
virtual c2_status_t onOpen(AVCodecContext* mCtx);
virtual c2_status_t onOpened(AVCodecContext* mCtx);
};
c2_status_t CodecHelper::onCodecConfig(AVCodecContext* mCtx, C2ReadView* inBuffer) {
int orig_extradata_size = mCtx->extradata_size;
int add_extradata_size = inBuffer->capacity();
#if DEBUG_EXTRADATA
ALOGD("CodecHelper::onCodecConfig: add = %u, current = %d", add_extradata_size, orig_extradata_size);
#endif
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("CodecHelper::onCodecConfig: ffmpeg audio 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);
return C2_OK;
}
c2_status_t CodecHelper::onOpen(AVCodecContext* mCtx) {
#if DEBUG_EXTRADATA
ALOGD("CodecHelper::onOpen: extradata = %d", mCtx->extradata_size);
#else
// Silence compilation warning.
(void)mCtx;
#endif
return C2_OK;
}
c2_status_t CodecHelper::onOpened(AVCodecContext* mCtx) {
(void)mCtx;
return C2_OK;
}
struct VorbisCodecHelper : public CodecHelper {
VorbisCodecHelper();
~VorbisCodecHelper();
c2_status_t onCodecConfig(AVCodecContext* mCtx, C2ReadView* rView);
c2_status_t onOpen(AVCodecContext* mCtx);
uint8_t* mHeader[3];
int mHeaderLen[3];
};
VorbisCodecHelper::VorbisCodecHelper()
: CodecHelper(),
mHeader{ NULL, NULL, NULL },
mHeaderLen{ 0, 0, 0 } {
}
VorbisCodecHelper::~VorbisCodecHelper() {
for (int i = 0; i < 3; i++) {
if (mHeader[i]) {
av_free(mHeader[i]);
mHeader[i] = NULL;
}
mHeaderLen[i] = 0;
}
}
c2_status_t VorbisCodecHelper::onCodecConfig(AVCodecContext* mCtx __unused, C2ReadView* inBuffer) {
const uint8_t* data = inBuffer->data();
int len = inBuffer->capacity();
int index = 0;
switch (data[0]) {
case 1: index = 0; break;
case 3: index = 1; break;
case 5: index = 2; break;
default:
ALOGE("VorbisCodecHelper::onCodecConfig: invalid vorbis codec config (%02x)", data[0]);
return C2_BAD_VALUE;
}
if (mHeader[index]) {
ALOGW("VorbisCodecHelper::onCodecConfig: overwriting header[%d]", index);
av_free(mHeader[index]);
}
mHeader[index] = (uint8_t*)av_mallocz(len);
if (! mHeader[index]) {
ALOGE("VorbisCodecHelper::onCodecConfig: oom for vorbis extradata");
return C2_NO_MEMORY;
}
memcpy(mHeader[index], data, len);
mHeaderLen[index] = len;
#if DEBUG_EXTRADATA
ALOGD("VorbisCodecHelper::onCodecConfig: found header[%d] = %d", index, len);
#endif
return C2_OK;
}
c2_status_t VorbisCodecHelper::onOpen(AVCodecContext* mCtx) {
// Don't generate extradata twice
if (! mCtx->extradata) {
if (! setup_vorbis_extradata(&mCtx->extradata,
&mCtx->extradata_size,
(const uint8_t**)mHeader,
mHeaderLen)) {
return C2_NO_MEMORY;
}
#if DEBUG_EXTRADATA
ALOGD("VorbisCodecHelper::onOpen: extradata = %d", mCtx->extradata_size);
#endif
}
return C2_OK;
}
struct Ac3CodecHelper : public CodecHelper {
c2_status_t onOpened(AVCodecContext* mCtx);
};
c2_status_t Ac3CodecHelper::onOpened(AVCodecContext* mCtx) {
int err = av_opt_set_chlayout(mCtx->priv_data, "downmix", &mCtx->ch_layout, 0);
if (err < 0) {
ALOGE("Ac3CodecHelper::onOpened: failed to set downmix = %d: %s (%08x)",
mCtx->ch_layout.nb_channels, av_err2str(err), err);
} else {
ALOGD("Ac3CodecHelper::onOpened: set downmix = %d", mCtx->ch_layout.nb_channels);
}
return C2_OK;
}
CodecHelper* createCodecHelper(enum AVCodecID codec_id) {
switch (codec_id) {
case AV_CODEC_ID_AC3:
case AV_CODEC_ID_EAC3:
return new Ac3CodecHelper();
case AV_CODEC_ID_VORBIS:
return new VorbisCodecHelper();
default:
return new CodecHelper();
}
}
C2FFMPEGAudioDecodeComponent::C2FFMPEGAudioDecodeComponent(
const C2FFMPEGComponentInfo* componentInfo,
const std::shared_ptr<C2FFMPEGAudioDecodeInterface>& intf)
: SimpleC2Component(std::make_shared<SimpleInterface<C2FFMPEGAudioDecodeInterface>>(componentInfo->name, 0, intf)),
mInfo(componentInfo),
mIntf(intf),
mCodecID(componentInfo->codecID),
mCtx(NULL),
mFrame(NULL),
mPacket(NULL),
mFFMPEGInitialized(false),
mCodecAlreadyOpened(false),
mEOSSignalled(false),
mSwrCtx(NULL),
mTargetSampleFormat(AV_SAMPLE_FMT_NONE),
mTargetSampleRate(44100),
mTargetChannels(1) {
ALOGD("C2FFMPEGAudioDecodeComponent: mediaType = %s", componentInfo->mediaType);
}
C2FFMPEGAudioDecodeComponent::~C2FFMPEGAudioDecodeComponent() {
ALOGD("~C2FFMPEGAudioDecodeComponent: mCtx = %p", mCtx);
onRelease();
}
c2_status_t C2FFMPEGAudioDecodeComponent::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;
}
mCtx->codec_type = AVMEDIA_TYPE_AUDIO;
mCtx->codec_id = mCodecID;
updateAudioParameters();
av_channel_layout_default(&mCtx->ch_layout, mTargetChannels);
mCtx->sample_rate = mTargetSampleRate;
mCtx->bit_rate = 0;
mCtx->sample_fmt = mTargetSampleFormat;
// Avoid resampling if possible, ask the codec for the target format.
mCtx->request_sample_fmt = mCtx->sample_fmt;
mCodecHelper = createCodecHelper(mCtx->codec_id);
ALOGD("initDecoder: %p [%s], %s - sr=%d, ch=%d, fmt=%s",
mCtx, avcodec_get_name(mCtx->codec_id), mInfo->mediaType,
mCtx->sample_rate, mCtx->ch_layout.nb_channels, av_get_sample_fmt_name(mCtx->sample_fmt));
return C2_OK;
}
c2_status_t C2FFMPEGAudioDecodeComponent::openDecoder() {
if (mCodecAlreadyOpened) {
return C2_OK;
}
mCodecHelper->onOpen(mCtx);
// Find decoder
mCtx->codec = avcodec_find_decoder(mCtx->codec_id);
if (! mCtx->codec) {
ALOGE("openDecoder: ffmpeg audio 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->flags |= AV_CODEC_FLAG_BITEXACT;
ALOGD("openDecoder: begin to open ffmpeg audio decoder(%s), mCtx sample_rate: %d, channels: %d",
avcodec_get_name(mCtx->codec_id), mCtx->sample_rate, mCtx->ch_layout.nb_channels);
int err = avcodec_open2(mCtx, mCtx->codec, NULL);
if (err < 0) {
ALOGE("openDecoder: ffmpeg audio decoder failed to initialize.(%s)", av_err2str(err));
return C2_NO_INIT;
}
mCodecAlreadyOpened = true;
mCodecHelper->onOpened(mCtx);
ALOGD("openDecoder: open ffmpeg audio decoder(%s) success, mCtx sample_rate: %d, "
"channels: %d, sample_fmt: %s, bits_per_coded_sample: %d, bits_per_raw_sample: %d",
avcodec_get_name(mCtx->codec_id),
mCtx->sample_rate, mCtx->ch_layout.nb_channels,
av_get_sample_fmt_name(mCtx->sample_fmt),
mCtx->bits_per_coded_sample, mCtx->bits_per_raw_sample);
mFrame = av_frame_alloc();
if (! mFrame) {
ALOGE("openDecoder: oom for audio frame");
return C2_NO_MEMORY;
}
return C2_OK;
}
void C2FFMPEGAudioDecodeComponent::deInitDecoder() {
ALOGD("deInitDecoder: %p", mCtx);
if (mCtx) {
if (avcodec_is_open(mCtx)) {
avcodec_flush_buffers(mCtx);
}
avcodec_free_context(&mCtx);
mCodecAlreadyOpened = false;
}
if (mFrame) {
av_frame_free(&mFrame);
mFrame = NULL;
}
if (mPacket) {
av_packet_free(&mPacket);
mPacket = NULL;
}
if (mSwrCtx) {
swr_free(&mSwrCtx);
}
if (mCodecHelper) {
delete mCodecHelper;
mCodecHelper = NULL;
}
mEOSSignalled = false;
}
c2_status_t C2FFMPEGAudioDecodeComponent::processCodecConfig(C2ReadView* inBuffer) {
#if DEBUG_EXTRADATA
ALOGD("processCodecConfig: inBuffer = %d", inBuffer->capacity());
#endif
if (! mCodecAlreadyOpened) {
return mCodecHelper->onCodecConfig(mCtx, inBuffer);
} else {
ALOGW("processCodecConfig: decoder is already opened, ignoring %d bytes", inBuffer->capacity());
}
return C2_OK;
}
c2_status_t C2FFMPEGAudioDecodeComponent::sendInputBuffer(
C2ReadView *inBuffer, int64_t timestamp) {
if (!mPacket) {
mPacket = av_packet_alloc();
if (!mPacket) {
ALOGE("sendInputBuffer: oom for audio 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 = timestamp;
int err = avcodec_send_packet(mCtx, mPacket);
av_packet_unref(mPacket);
if (err < 0) {
ALOGE("sendInputBuffer: failed to send data to decoder err = %d", err);
// Don't report error to client.
}
return C2_OK;
}
c2_status_t C2FFMPEGAudioDecodeComponent::receiveFrame(bool* hasFrame) {
int err = avcodec_receive_frame(mCtx, mFrame);
if (err == 0) {
*hasFrame = true;
} else if (err == AVERROR(EAGAIN) || err == AVERROR_EOF) {
*hasFrame = false;
} else {
ALOGE("receiveFrame: failed to receive frame from decoder err = %d", err);
// Don't report error to client.
}
return C2_OK;
}
c2_status_t C2FFMPEGAudioDecodeComponent::getOutputBuffer(C2WriteView* outBuffer) {
if (! mSwrCtx ||
mSwrCtx->in_sample_fmt != mFrame->format ||
mSwrCtx->in_sample_rate != mFrame->sample_rate ||
av_channel_layout_compare(&mSwrCtx->in_ch_layout, &mFrame->ch_layout) != 0 ||
mSwrCtx->out_sample_fmt != mTargetSampleFormat ||
mSwrCtx->out_sample_rate != mTargetSampleRate ||
mSwrCtx->out_ch_layout.nb_channels != mTargetChannels) {
if (mSwrCtx) {
swr_free(&mSwrCtx);
}
AVChannelLayout newLayout;
av_channel_layout_default(&newLayout, mTargetChannels);
swr_alloc_set_opts2(&mSwrCtx,
&newLayout, mTargetSampleFormat, mTargetSampleRate,
&mFrame->ch_layout, (enum AVSampleFormat)mFrame->format, mFrame->sample_rate,
0, NULL);
av_channel_layout_uninit(&newLayout);
if (! mSwrCtx || swr_init(mSwrCtx) < 0) {
ALOGE("getOutputBuffer: cannot create audio converter - sr=%d, ch=%d, fmt=%s => sr=%d, ch=%d, fmt=%s",
mFrame->sample_rate, mFrame->ch_layout.nb_channels, av_get_sample_fmt_name((enum AVSampleFormat)mFrame->format),
mTargetSampleRate, mTargetChannels, av_get_sample_fmt_name(mTargetSampleFormat));
if (mSwrCtx) {
swr_free(&mSwrCtx);
}
return C2_NO_MEMORY;
}
ALOGD("getOutputBuffer: created audio converter - sr=%d, ch=%d, fmt=%s => sr=%d, ch=%d, fmt=%s",
mFrame->sample_rate, mFrame->ch_layout.nb_channels, av_get_sample_fmt_name((enum AVSampleFormat)mFrame->format),
mTargetSampleRate, mTargetChannels, av_get_sample_fmt_name(mTargetSampleFormat));
}
uint8_t* out[1] = { outBuffer->data() };
int ret = swr_convert(mSwrCtx, out, mFrame->nb_samples, (const uint8_t**)mFrame->extended_data, mFrame->nb_samples);
if (ret < 0) {
ALOGE("getOutputBuffer: audio conversion failed");
return C2_CORRUPTED;
} else if (ret != mFrame->nb_samples) {
ALOGW("getOutputBuffer: audio conversion truncated!");
}
#if DEBUG_FRAMES
ALOGD("getOutputBuffer: audio converted - sr=%d, ch=%d, fmt=%s, #=%d => sr=%d, ch=%d, fmt=%s, #=%d(%d)",
mFrame->sample_rate, mFrame->ch_layout.nb_channels, av_get_sample_fmt_name((enum AVSampleFormat)mFrame->format), mFrame->nb_samples,
mTargetSampleRate, mTargetChannels, av_get_sample_fmt_name(mTargetSampleFormat), mFrame->nb_samples, outBuffer->capacity());
#endif
return C2_OK;
}
void C2FFMPEGAudioDecodeComponent::updateAudioParameters() {
mTargetSampleFormat = convertFormatToFFMPEG(mIntf->getPcmEncodingInfo());
mTargetSampleRate = mIntf->getSampleRate();
mTargetChannels = mIntf->getChannelCount();
}
c2_status_t C2FFMPEGAudioDecodeComponent::onInit() {
ALOGD("onInit");
return initDecoder();
}
c2_status_t C2FFMPEGAudioDecodeComponent::onStop() {
ALOGD("onStop");
return C2_OK;
}
void C2FFMPEGAudioDecodeComponent::onReset() {
ALOGD("onReset");
deInitDecoder();
initDecoder();
}
void C2FFMPEGAudioDecodeComponent::onRelease() {
ALOGD("onRelease");
deInitDecoder();
if (mFFMPEGInitialized) {
deInitFFmpeg();
mFFMPEGInitialized = false;
}
}
c2_status_t C2FFMPEGAudioDecodeComponent::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;
}
void C2FFMPEGAudioDecodeComponent::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;
bool hasFrame = 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.
// NOTE: This has an impact on the drain operation.
work->result = C2_OK;
work->worklets.front()->output.flags = (C2FrameData::flags_t)0;
work->worklets.front()->output.buffers.clear();
work->worklets.front()->output.ordinal = work->input.ordinal;
work->workletsProcessed = 1u;
if (inSize || (eos && mCodecAlreadyOpened)) {
c2_status_t err = C2_OK;
if (work->input.flags & C2FrameData::FLAG_CODEC_CONFIG) {
work->result = processCodecConfig(&rView);
return;
}
if (! mCodecAlreadyOpened) {
err = openDecoder();
if (err != C2_OK) {
work->result = err;
return;
}
}
err = sendInputBuffer(&rView, work->input.ordinal.timestamp.peekll());
if (err != C2_OK) {
work->result = err;
return;
}
while (true) {
hasFrame = false;
err = receiveFrame(&hasFrame);
if (err != C2_OK) {
work->result = err;
return;
}
if (! hasFrame) {
break;
}
#if DEBUG_FRAMES
ALOGD("process: got frame pts=%" PRId64 " dts=%" PRId64 " ts=%" PRId64 " - sr=%d, ch=%d, fmt=%s, #=%d",
mFrame->pts, mFrame->pkt_dts, mFrame->best_effort_timestamp,
mFrame->sample_rate, mFrame->ch_layout.nb_channels, av_get_sample_fmt_name((enum AVSampleFormat)mFrame->format),
mFrame->nb_samples);
#endif
// Always target the sample format on output port. Even if we can trigger a config update
// for the sample format, Android does not support planar formats, so if the codec uses
// such format (e.g. AC3), conversion is needed. Technically we can limit the conversion to
// planer->packed, but that means Android would also do its own conversion to the wanted
// format on output port. To avoid double conversion, target directly the wanted format.
bool needConfigUpdate = (mFrame->sample_rate != mTargetSampleRate ||
mFrame->ch_layout.nb_channels != mTargetChannels);
bool needResampling = (needConfigUpdate ||
mFrame->format != mTargetSampleFormat ||
// We only support sending audio data to Android in native order.
mFrame->ch_layout.order != AV_CHANNEL_ORDER_NATIVE);
if (needConfigUpdate) {
ALOGD("process: audio params changed - sr=%d, ch=%d, fmt=%s => sr=%d, ch=%d, fmt=%s",
mTargetSampleRate, mTargetChannels, av_get_sample_fmt_name(mTargetSampleFormat),
mFrame->sample_rate, mFrame->ch_layout.nb_channels, av_get_sample_fmt_name(mTargetSampleFormat));
if (work->worklets.front()->output.buffers.size() > 0) {
// Not sure if this would ever happen, nor how to handle it...
ALOGW("process: audio params changed with non empty output buffers pending");
}
C2StreamSampleRateInfo::output sampleRate(0u, mFrame->sample_rate);
C2StreamChannelCountInfo::output channelCount(0u, mFrame->ch_layout.nb_channels);
std::vector<std::unique_ptr<C2SettingResult>> failures;
err = mIntf->config({ &sampleRate, &channelCount }, C2_MAY_BLOCK, &failures);
if (err == C2_OK) {
work->worklets.front()->output.configUpdate.push_back(C2Param::Copy(sampleRate));
work->worklets.front()->output.configUpdate.push_back(C2Param::Copy(channelCount));
updateAudioParameters();
} else {
ALOGE("process: config update failed err = %d", err);
work->result = C2_CORRUPTED;
return;
}
}
std::shared_ptr<C2LinearBlock> block;
int len = av_samples_get_buffer_size(NULL, mTargetChannels, mFrame->nb_samples, mTargetSampleFormat, 0);
err = pool->fetchLinearBlock(len, { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE }, &block);
if (err != C2_OK) {
ALOGE("process: failed to fetch linear block for #=%d err = %d",
mFrame->nb_samples, err);
work->result = C2_CORRUPTED;
return;
}
C2WriteView wView = block->map().get();
err = wView.error();
if (err != C2_OK) {
ALOGE("process: write view map failed err = %d", err);
work->result = C2_CORRUPTED;
return;
}
if (needResampling) {
err = getOutputBuffer(&wView);
if (err != C2_OK) {
work->result = err;
return;
}
}
else {
#if DEBUG_FRAMES
ALOGD("process: no audio conversion needed");
#endif
memcpy(wView.data(), mFrame->data[0], mFrame->linesize[0]);
}
std::shared_ptr<C2Buffer> buffer = createLinearBuffer(std::move(block), 0, len);
work->worklets.front()->output.buffers.push_back(buffer);
if (mFrame->best_effort_timestamp != AV_NOPTS_VALUE) {
work->worklets.front()->output.ordinal.timestamp = mFrame->best_effort_timestamp;
}
break;
}
}
#if DEBUG_FRAMES
else {
ALOGW("process: ignoring empty work");
}
#endif
if (eos) {
mEOSSignalled = true;
work->worklets.front()->output.flags = C2FrameData::FLAG_END_OF_STREAM;
}
}
c2_status_t C2FFMPEGAudioDecodeComponent::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 hasFrame = false;
c2_status_t err = C2_OK;
while (err == C2_OK) {
hasFrame = false;
err = sendInputBuffer(NULL, 0);
if (err == C2_OK) {
err = receiveFrame(&hasFrame);
if (hasFrame) {
ALOGW("drain: skip frame pts=%" PRId64 " dts=%" PRId64 " ts=%" PRId64 " - sr=%d, ch=%d, fmt=%s, #=%d",
mFrame->pts, mFrame->pkt_dts, mFrame->best_effort_timestamp,
mFrame->sample_rate, mFrame->ch_layout.nb_channels, av_get_sample_fmt_name((enum AVSampleFormat)mFrame->format),
mFrame->nb_samples);
} else {
err = C2_NOT_FOUND;
}
}
}
return C2_OK;
}
} // namespace android