MCPcopy Create free account
hub / github.com/FFmpeg/FFmpeg / libopus_encode

Function libopus_encode

libavcodec/libopusenc.c:456–530  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

454}
455
456static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt,
457 const AVFrame *frame, int *got_packet_ptr)
458{
459 LibopusEncContext *opus = avctx->priv_data;
460 const int bytes_per_sample = av_get_bytes_per_sample(avctx->sample_fmt);
461 const int channels = avctx->ch_layout.nb_channels;
462 const int sample_size = channels * bytes_per_sample;
463 const uint8_t *audio;
464 int ret;
465 int discard_padding;
466
467 if (frame) {
468 ret = ff_af_queue_add(&opus->afq, frame);
469 if (ret < 0)
470 return ret;
471 if (opus->encoder_channel_map != NULL) {
472 audio = opus->samples;
473 libopus_copy_samples_with_channel_map(
474 opus->samples, frame->data[0], opus->encoder_channel_map,
475 channels, frame->nb_samples, bytes_per_sample);
476 } else if (frame->nb_samples < opus->opts.packet_size) {
477 audio = opus->samples;
478 memcpy(opus->samples, frame->data[0], frame->nb_samples * sample_size);
479 } else
480 audio = frame->data[0];
481 } else {
482 if (!opus->afq.remaining_samples || (!opus->afq.frame_alloc && !opus->afq.frame_count))
483 return 0;
484 audio = opus->samples;
485 memset(opus->samples, 0, opus->opts.packet_size * sample_size);
486 }
487
488 /* Maximum packet size taken from opusenc in opus-tools. 120ms packets
489 * consist of 6 frames in one packet. The maximum frame size is 1275
490 * bytes along with the largest possible packet header of 7 bytes. */
491 if ((ret = ff_alloc_packet(avctx, avpkt, (1275 * 6 + 7) * opus->stream_count)) < 0)
492 return ret;
493
494 if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
495 ret = opus_multistream_encode_float(opus->enc, (const float *)audio,
496 opus->opts.packet_size,
497 avpkt->data, avpkt->size);
498 else
499 ret = opus_multistream_encode(opus->enc, (const opus_int16 *)audio,
500 opus->opts.packet_size,
501 avpkt->data, avpkt->size);
502
503 if (ret < 0) {
504 av_log(avctx, AV_LOG_ERROR,
505 "Error encoding frame: %s\n", opus_strerror(ret));
506 return ff_opus_error_to_averror(ret);
507 }
508
509 av_shrink_packet(avpkt, ret);
510
511 ff_af_queue_remove(&opus->afq, opus->opts.packet_size,
512 &avpkt->pts, &avpkt->duration);
513

Callers

nothing calls this directly

Calls 10

av_get_bytes_per_sampleFunction · 0.85
ff_af_queue_addFunction · 0.85
ff_alloc_packetFunction · 0.85
av_logFunction · 0.85
ff_opus_error_to_averrorFunction · 0.85
av_shrink_packetFunction · 0.85
ff_af_queue_removeFunction · 0.85
av_packet_new_side_dataFunction · 0.85

Tested by

no test coverage detected