MCPcopy Create free account
hub / github.com/audacity/audacity / EncodeAudio

Method EncodeAudio

modules/import-export/mod-ffmpeg/ExportFFmpeg.cpp:1331–1464  ·  view source on GitHub ↗

Returns 0 if no more output, 1 if more output, negative if error

Source from the content-addressed store, hash-verified

1329
1330// Returns 0 if no more output, 1 if more output, negative if error
1331int FFmpegExporter::EncodeAudio(AVPacketWrapper& pkt, int16_t* audio_samples, int nb_samples)
1332{
1333 // Assume *pkt is already initialized.
1334
1335 int i, ch, buffer_size, ret, got_output = 0;
1336 AVDataBuffer<uint8_t> samples;
1337
1338 std::unique_ptr<AVFrameWrapper> frame;
1339
1340 if (audio_samples) {
1341 frame = mFFmpeg->CreateAVFrameWrapper();
1342
1343 if (!frame)
1344 return AUDACITY_AVERROR(ENOMEM);
1345
1346 frame->SetSamplesCount(nb_samples);
1347 frame->SetFormat(mEncAudioCodecCtx->GetSampleFmt());
1348 frame->SetChannelLayout(mEncAudioCodecCtx->GetChannelLayout());
1349
1350 buffer_size = mFFmpeg->av_samples_get_buffer_size(
1351 NULL, mEncAudioCodecCtx->GetChannels(), nb_samples,
1352 mEncAudioCodecCtx->GetSampleFmt(), 0);
1353
1354 if (buffer_size < 0) {
1355 throw ExportException(_("FFmpeg : ERROR - Could not get sample buffer size"));
1356 }
1357
1358 samples = mFFmpeg->CreateMemoryBuffer<uint8_t>(buffer_size);
1359
1360 if (samples.empty()) {
1361 throw ExportException(_("FFmpeg : ERROR - Could not allocate bytes for samples buffer"));
1362 }
1363 /* setup the data pointers in the AVFrame */
1364 ret = mFFmpeg->avcodec_fill_audio_frame(
1365 frame->GetWrappedValue(), mEncAudioCodecCtx->GetChannels(),
1366 mEncAudioCodecCtx->GetSampleFmt(), samples.data(), buffer_size, 0);
1367
1368 if (ret < 0) {
1369 throw ExportException(_("FFmpeg : ERROR - Could not setup audio frame"));
1370 }
1371
1372 const int channelsCount = mEncAudioCodecCtx->GetChannels();
1373
1374 for (ch = 0; ch < mEncAudioCodecCtx->GetChannels(); ch++)
1375 {
1376 for (i = 0; i < nb_samples; i++) {
1377 switch (static_cast<AudacityAVSampleFormat>(
1378 mEncAudioCodecCtx->GetSampleFmt()))
1379 {
1380 case AUDACITY_AV_SAMPLE_FMT_U8:
1381 ((uint8_t*)(frame->GetData(0)))[ch + i*channelsCount] = audio_samples[ch + i*channelsCount]/258 + 128;
1382 break;
1383 case AUDACITY_AV_SAMPLE_FMT_U8P:
1384 ((uint8_t*)(frame->GetData(ch)))[i] = audio_samples[ch + i*channelsCount]/258 + 128;
1385 break;
1386 case AUDACITY_AV_SAMPLE_FMT_S16:
1387 ((int16_t*)(frame->GetData(0)))[ch + i*channelsCount] = audio_samples[ch + i*channelsCount];
1388 break;

Callers

nothing calls this directly

Calls 8

CreateAVFrameWrapperMethod · 0.80
GetChannelsMethod · 0.80
GetIndexMethod · 0.80
SetFormatMethod · 0.45
emptyMethod · 0.45
GetWrappedValueMethod · 0.45
dataMethod · 0.45
GetDataMethod · 0.45

Tested by

no test coverage detected