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

Function opus_encode_frame

libavcodec/opus/enc.c:547–615  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

545}
546
547static int opus_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
548 const AVFrame *frame, int *got_packet_ptr)
549{
550 OpusEncContext *s = avctx->priv_data;
551 int ret, frame_size, discard_padding, alloc_size = 0;
552
553 if (frame) { /* Add new frame to queue */
554 if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
555 return ret;
556 ff_bufqueue_add(avctx, &s->bufqueue, av_frame_clone(frame));
557 } else {
558 ff_opus_psy_signal_eof(&s->psyctx);
559 if (!s->afq.remaining_samples || !avctx->frame_num)
560 return 0; /* We've been flushed and there's nothing left to encode */
561 }
562
563 /* Run the psychoacoustic system */
564 if (ff_opus_psy_process(&s->psyctx, &s->packet))
565 return 0;
566
567 frame_size = OPUS_BLOCK_SIZE(s->packet.framesize);
568
569 if (!frame) {
570 /* This can go negative, that's not a problem, we only pad if positive */
571 int pad_empty = s->packet.frames*(frame_size/s->avctx->frame_size) - s->bufqueue.available + 1;
572 /* Pad with empty 2.5 ms frames to whatever framesize was decided,
573 * this should only happen at the very last flush frame. The frames
574 * allocated here will be freed (because they have no other references)
575 * after they get used by celt_frame_setup_input() */
576 for (int i = 0; i < pad_empty; i++) {
577 AVFrame *empty = spawn_empty_frame(s);
578 if (!empty)
579 return AVERROR(ENOMEM);
580 ff_bufqueue_add(avctx, &s->bufqueue, empty);
581 }
582 }
583
584 for (int i = 0; i < s->packet.frames; i++) {
585 celt_encode_frame(s, &s->rc[i], &s->frame[i], i);
586 alloc_size += s->frame[i].framebits >> 3;
587 }
588
589 /* Worst case toc + the frame lengths if needed */
590 alloc_size += 2 + s->packet.frames*2;
591
592 if ((ret = ff_alloc_packet(avctx, avpkt, alloc_size)) < 0)
593 return ret;
594
595 /* Assemble packet */
596 opus_packet_assembler(s, avpkt);
597
598 /* Update the psychoacoustic system */
599 ff_opus_psy_postencode_update(&s->psyctx, s->frame);
600
601 /* Remove samples from queue and skip if needed */
602 ff_af_queue_remove(&s->afq, s->packet.frames*frame_size, &avpkt->pts, &avpkt->duration);
603
604 discard_padding = s->packet.frames*frame_size - ff_samples_from_time_base(avctx, avpkt->duration);

Callers

nothing calls this directly

Calls 13

ff_af_queue_addFunction · 0.85
ff_bufqueue_addFunction · 0.85
av_frame_cloneFunction · 0.85
ff_opus_psy_signal_eofFunction · 0.85
ff_opus_psy_processFunction · 0.85
celt_encode_frameFunction · 0.85
ff_alloc_packetFunction · 0.85
opus_packet_assemblerFunction · 0.85
ff_af_queue_removeFunction · 0.85
av_packet_new_side_dataFunction · 0.85

Tested by

no test coverage detected