FFmpegfs Fuse Multi Media Filesystem 2.19
Loading...
Searching...
No Matches
ffmpeg_audiofifo.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017-2026 by Norbert Schlia (nschlia@oblivion-software.de)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 */
9
17#ifdef __cplusplus
18extern "C" {
19#endif
20#pragma GCC diagnostic push
21#pragma GCC diagnostic ignored "-Wconversion"
22#pragma GCC diagnostic ignored "-Wsign-conversion"
23#include <libavutil/audio_fifo.h>
24#pragma GCC diagnostic pop
25#ifdef __cplusplus
26}
27#endif
28
29#include "ffmpeg_audiofifo.h"
30
32 : m_fifo(nullptr)
33{
34}
35
36FFmpeg_AudioFifo::FFmpeg_AudioFifo(AVSampleFormat sample_fmt, int channels, int nb_samples)
37 : m_fifo(nullptr)
38{
39 alloc(sample_fmt, channels, nb_samples);
40}
41
46
48 : m_fifo(fifo.m_fifo)
49{
50 fifo.m_fifo = nullptr;
51}
52
53FFmpeg_AudioFifo& FFmpeg_AudioFifo::operator=(FFmpeg_AudioFifo&& fifo) noexcept
54{
55 if (this != &fifo)
56 {
57 reset();
58 m_fifo = fifo.m_fifo;
59 fifo.m_fifo = nullptr;
60 }
61 return *this;
62}
63
64int FFmpeg_AudioFifo::alloc(AVSampleFormat sample_fmt, int channels, int nb_samples)
65{
66 reset();
67
68 m_fifo = av_audio_fifo_alloc(sample_fmt, channels, nb_samples);
69 if (m_fifo == nullptr)
70 {
71 return AVERROR(ENOMEM);
72 }
73
74 return 0;
75}
76
78{
79 if (m_fifo != nullptr)
80 {
81 av_audio_fifo_free(m_fifo);
82 m_fifo = nullptr;
83 }
84}
85
87{
88 return m_fifo == nullptr;
89}
90
92{
93 if (m_fifo == nullptr)
94 {
95 return 0;
96 }
97 return av_audio_fifo_size(m_fifo);
98}
99
100int FFmpeg_AudioFifo::realloc(int nb_samples)
101{
102 if (m_fifo == nullptr)
103 {
104 return AVERROR(EINVAL);
105 }
106 return av_audio_fifo_realloc(m_fifo, nb_samples);
107}
108
109int FFmpeg_AudioFifo::write(void **data, int nb_samples)
110{
111 if (m_fifo == nullptr)
112 {
113 return AVERROR(EINVAL);
114 }
115 return av_audio_fifo_write(m_fifo, data, nb_samples);
116}
117
118int FFmpeg_AudioFifo::read(void **data, int nb_samples)
119{
120 if (m_fifo == nullptr)
121 {
122 return AVERROR(EINVAL);
123 }
124 return av_audio_fifo_read(m_fifo, data, nb_samples);
125}
126
128{
129 return m_fifo;
130}
131
132const AVAudioFifo* FFmpeg_AudioFifo::get() const
133{
134 return m_fifo;
135}
136
138{
139 AVAudioFifo *fifo = m_fifo;
140 m_fifo = nullptr;
141 return fifo;
142}
143
144FFmpeg_AudioFifo::operator AVAudioFifo*()
145{
146 return m_fifo;
147}
148
149FFmpeg_AudioFifo::operator const AVAudioFifo*() const
150{
151 return m_fifo;
152}
RAII wrapper for AVAudioFifo.
int write(void **data, int nb_samples)
Write audio samples into the FIFO.
FFmpeg_AudioFifo()
Construct an empty FIFO wrapper.
AVAudioFifo * release()
Release ownership without freeing the FIFO.
void reset()
Free the owned FIFO and reset the wrapper to empty.
AVAudioFifo * m_fifo
Pointer to underlying AVAudioFifo.
AVAudioFifo * get()
Get the owned FFmpeg FIFO pointer.
int read(void **data, int nb_samples)
Read audio samples from the FIFO.
bool empty() const
Check whether the wrapper currently owns a FIFO.
~FFmpeg_AudioFifo()
Release the owned FIFO, if any.
int size() const
Return the number of samples currently stored in the FIFO.
int realloc(int nb_samples)
Resize the FIFO allocation.
int alloc(AVSampleFormat sample_fmt, int channels, int nb_samples=1)
Allocate a new audio FIFO, replacing any existing one.
FFmpeg AVAudioFifo RAII wrapper.