-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAudioWorker.h
152 lines (123 loc) · 4.89 KB
/
AudioWorker.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
File: AudioWorker.h
Created on: 18/4/2015
Author: Felix de las Pozas Alvarez
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef AUDIO_WORKER_H_
#define AUDIO_WORKER_H_
// Project
#include "Worker.h"
// Qt
#include <QMutex>
// libav
extern "C"
{
#include <libavformat/avformat.h>
}
namespace TagParser
{
class Tag;
}
/** \class AudioWorker
* \brief Implements a Worker class to use on audio files that are not MP3s.
*
*/
class AudioWorker
: public Worker
{
Q_OBJECT
public:
/** \brief AudioWorker class constructor.
* \param[in] source_info QFileInfo struct of the source file.
* \param[in] configuration configuration struct reference.
*
*/
explicit AudioWorker(const QFileInfo &source_info, const Utils::TranscoderConfiguration &configuration);
/** \brief AudioWorker class virtual destructor.
*
*/
virtual ~AudioWorker()
{}
protected:
virtual void run_implementation() override;
/** \brief Builds the file name based on the metadata.
* \param[in] tags TagParser::Tag metadata pointer.
*
*/
QString parse_metadata(const TagParser::Tag *tags);
/** \brief Initializes libav library structures and data to decode the source file to
* pcm data.
*
*/
bool init_libav();
/** \brief Frees the structures allocated in the init stages of libav library.
*
*/
void deinit_libav();
/** \brief Extracts the cover picture and dumps it to the disk.
*
*/
bool extract_cover_picture() const;
/** \brief Helper method to get a user-friendly description of a libav error code.
*
*/
QString av_error_string(const int error_number) const;
/** \brief Custom I/O read for libav, using a QFile.
* \param[in] opaque pointer to the reader.
* \param[in] buffer buffer to fill
* \param[in] buffer_size buffer size.
*
*/
static int custom_IO_read(void *opaque, unsigned char *buffer, int buffer_size);
/** \brief Custom I/O seek for libav, using a QFile.
* \param[in] opaque pointer to the reader.
* \param[in] offset seek value.
* \param[in] whence seek direction.
*
*/
static long long int custom_IO_seek(void *opaque, long long int offset, int whence);
AVFormatContext *m_libav_context; /** file context. */
AVPacket *m_packet; /** libav packet (encodec data). */
int m_cover_stream_id; /** id of the cover stream on the file, or -1 if not found or already extracted. */
QString m_cover_extension; /** extension of the cover picture in the file (if any, if not it's emtpy). */
static const int s_io_buffer_size = 16384+AV_INPUT_BUFFER_PADDING_SIZE;
static QMutex s_mutex; /** mutex to init libav and write cover picture. */
private:
/** \brief Initializes additional libav structures to decode the video stream
* containing the cover picture of the source file.
*/
void init_libav_cover_extraction();
/** \brief Helper method to send the buffers to encode. Returns the value of the lame library buffer
* encoding method called.
*
*/
bool encode_buffers(unsigned int buffer_start, unsigned int buffer_length);
/** \brief Decodes the source file and encodes the resulting pcm data with the mp3
* codec into the destination files.
*
*/
void transcode();
/** \brief Process an unique audio packet and encodes it to mp3 taking into account the duration
* of the tracks. Opens and closes destinations if the packet crosses it's boundaries.
*
*/
bool process_audio_packet();
QFile m_input_file; /** input audio file. */
virtual Destinations compute_destinations() override final;
static constexpr double CD_FRAMES_PER_SECOND = 75.0; /** frames per second in a CD. */
AVCodec *m_audio_decoder; /** libav audio decoder. */
AVCodecContext *m_audio_decoder_context; /** libav audio decoder context. */
AVFrame *m_frame; /** libav frame (decoded data). */
int m_audio_stream_id; /** id of the audio stream in the fie. */
};
#endif // AUDIO_WORKER_H_