1 /*
2     SPDX-FileCopyrightText: 2003 Fabrice Bellard
3     SPDX-FileCopyrightText: 2020 Mladen Milinkovic <max@smoothware.net>
4 
5     SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #ifndef AUDIODECODER_H
9 #define AUDIODECODER_H
10 
11 #include "videoplayer/backend/decoder.h"
12 #include <AL/alc.h>
13 
14 
15 struct SwrContext;
16 
17 namespace SubtitleComposer {
18 class VideoState;
19 struct Frame;
20 
21 class AudioDecoder : public Decoder
22 {
23 	Q_OBJECT
24 
25 public:
26 	AudioDecoder(VideoState *state, QObject *parent = nullptr);
27 
28 	void destroy() override;
29 	void abort() override;
30 
31 	void setListenerGain(double gain);
32 	double pitch() const;
33 	void setPitch(double pitch);
34 
35 private:
36 	void run() override;
37 
38 	struct Params {
39 		int freq;
40 		int channels;
41 		uint64_t channelLayout;
42 		AVSampleFormat fmt;
43 		int frameSize;
44 		int bytesPerSec;
45 	};
46 
47 	int decodeFrame(Frame *af);
48 	int getFrame(AVFrame *frame);
49 	void queueFrame(Frame *af);
50 	void queueBuffer(uint8_t *data, int len);
51 	int syncAudio(int nbSamples);
52 
53 	bool open(int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate);
54 	void close();
55 	void flush();
56 	void play();
57 	void pause();
58 
59 	VideoState *m_vs;
60 
61 	Params m_fmtSrc;
62 	Params m_fmtTgt;
63 	SwrContext *m_swrCtx;
64 	int m_hwBufQueueSize; // in bytes
65 	uint8_t *m_audioBuf;
66 	unsigned int m_bufSize; // in bytes
67 	uint8_t *m_audioBuf1;
68 	unsigned int m_buf1Size;
69 
70 	double m_diffCum; /* used for AV difference average computation */
71 	double m_diffAvgCoef;
72 	int m_diffAvgCount;
73 
74 	ALCdevice *m_alDev;
75 	ALCcontext *m_alCtx;
76 	unsigned int m_alSrc;
77 	int m_bufCnt;
78 	int m_bufFmt;
79 
80 	friend class StreamDemuxer;
81 };
82 }
83 
84 #endif // AUDIODECODER_H
85