Debian Codegraph @

src/album/imageengine/movieservice.cpp

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd.
3 *
4 * Author: ZhangYong <[email protected]>
5 *
6 * Maintainer: ZhangYong <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include "movieservice.h"
22 #include <QMetaType>
23 #include <QDirIterator>
24 #include <QStandardPaths>
25 #include <QLibrary>
26 #include <QLibraryInfo>
27
28 #include "controller/signalmanager.h"
29 #include "application.h"
30
31 extern "C" {
32 #include <libavformat/avformat.h>
33 #include <libavutil/dict.h>
34 #include <libavutil/avutil.h>
35 }
36
37 #define THUMBNAIL_SIZE 500
38 #define SEEK_TIME "00:00:01"
•39 #define HAVE_CH_LAYOUT (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
40 MovieService *MovieService::m_movieService = nullptr;
41
42 typedef int (*mvideo_avformat_open_input)(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options);
43 typedef int (*mvideo_avformat_find_stream_info)(AVFormatContext *ic, AVDictionary **options);
44 typedef int (*mvideo_av_find_best_stream)(AVFormatContext *ic, enum AVMediaType type, int wanted_stream_nb, int related_stream, AVCodec **decoder_ret, int flags);
45 typedef AVDictionaryEntry *(*mvideo_av_dict_get)(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags);
46 typedef void (*mvideo_avformat_close_input)(AVFormatContext **s);
47
48 mvideo_avformat_open_input g_mvideo_avformat_open_input = nullptr;
49 mvideo_avformat_find_stream_info g_mvideo_avformat_find_stream_info = nullptr;
50 mvideo_av_find_best_stream g_mvideo_av_find_best_stream = nullptr;
51 mvideo_av_dict_get g_mvideo_av_dict_get = nullptr;
52 mvideo_avformat_close_input g_mvideo_avformat_close_input = nullptr;
53
54 MovieService *MovieService::instance(QObject *parent)
55 {
56 Q_UNUSED(parent);
57 if (!m_movieService) {
58 m_movieService = new MovieService();
59 }
60 return m_movieService;
61 }
62
63 MovieService::~MovieService()
64 {
65 }
66
67 MovieInfo MovieService::getMovieInfo(const QUrl &url)
68 {
69 if (!m_initFFmpeg) {
70 initFFmpeg();
71 }
72
73 if (url.isLocalFile()) {
74 QFileInfo fi(url.toLocalFile());
75 if (fi.exists()) {
76 return parseFromFile(fi);
77 } else {
78 return MovieInfo();
79 }
80 } else {
81 return MovieInfo();
82 }
83 }
84
85 QImage MovieService::getMovieCover(const QUrl &url)
86 {
87 QMutexLocker locker(&m_queuqMutex);
88 if (!m_bInitThumb) {
89 initThumb();
90 m_mvideo_thumbnailer_destroy_image_data(m_image_data);
91 m_image_data = nullptr;
92 }
93
94 if (m_creat_video_thumbnailer == nullptr
95 || m_mvideo_thumbnailer_destroy == nullptr
96 || m_mvideo_thumbnailer_create_image_data == nullptr
97 || m_mvideo_thumbnailer_destroy_image_data == nullptr
98 || m_mvideo_thumbnailer_generate_thumbnail_to_buffer == nullptr
99 || m_video_thumbnailer == nullptr) {
100 return QImage();
101 }
102
103 m_video_thumbnailer->thumbnail_size = static_cast<int>(THUMBNAIL_SIZE);
104 //不取第一帧,与文管影院保持一致
105 // m_video_thumbnailer->seek_time = const_cast<char *>(SEEK_TIME);
106 m_image_data = m_mvideo_thumbnailer_create_image_data();
107 QString file = QFileInfo(url.toLocalFile()).absoluteFilePath();
108 m_mvideo_thumbnailer_generate_thumbnail_to_buffer(m_video_thumbnailer, file.toUtf8().data(), m_image_data);
109 QImage img = QImage::fromData(m_image_data->image_data_ptr, static_cast<int>(m_image_data->image_data_size), "png");
110 m_mvideo_thumbnailer_destroy_image_data(m_image_data);
111 m_image_data = nullptr;
112 return img;
113 }
114
115 MovieInfo MovieService::parseFromFile(const QFileInfo &fi)
116 {
117 struct MovieInfo mi;
118 mi.valid = false;
119 AVFormatContext *av_ctx = nullptr;
120 AVCodecParameters *video_dec_ctx = nullptr;
121 AVCodecParameters *audio_dec_ctx = nullptr;
122
123 if (!fi.exists()) {
124 // if (ok) *ok = false;
125 return mi;
126 }
127
128 auto ret = g_mvideo_avformat_open_input(&av_ctx, fi.filePath().toUtf8().constData(), nullptr, nullptr);
129 if (ret < 0) {
130 qWarning() << "avformat: could not open input";
131 // if (ok) *ok = false;
132 return mi;
133 }
134
135 if (g_mvideo_avformat_find_stream_info(av_ctx, nullptr) < 0) {
136 qWarning() << "av_find_stream_info failed";
137 // if (ok) *ok = false;
138 return mi;
139 }
140
141 if (av_ctx->nb_streams == 0) {
142 // if (ok) *ok = false;
143 return mi;
144 }
145
146 int videoRet = -1;
147 int audioRet = -1;
148 AVStream *videoStream = nullptr;
149 AVStream *audioStream = nullptr;
150 videoRet = g_mvideo_av_find_best_stream(av_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
151 audioRet = g_mvideo_av_find_best_stream(av_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
152 if (videoRet < 0 && audioRet < 0) {
153 // if (ok) *ok = false;
154 return mi;
155 }
156
157 if (videoRet >= 0) {
158 int video_stream_index = -1;
159 video_stream_index = videoRet;
160 videoStream = av_ctx->streams[video_stream_index];
161 video_dec_ctx = videoStream->codecpar;
162
163 mi.width = video_dec_ctx->width;
164 mi.height = video_dec_ctx->height;
165 mi.vCodecID = video_dec_ctx->codec_id;
166 mi.vCodeRate = video_dec_ctx->bit_rate;
167
168 if (videoStream->r_frame_rate.den != 0) {
169 mi.fps = videoStream->r_frame_rate.num / videoStream->r_frame_rate.den;
170 } else {
171 mi.fps = 0;
172 }
173 if (mi.height != 0) {
174 mi.proportion = static_cast<float>(mi.width) / static_cast<float>(mi.height);
175 } else {
176 mi.proportion = 0;
177 }
178 }
179 if (audioRet >= 0) {
180 int audio_stream_index = -1;
181 audio_stream_index = audioRet;
182 audioStream = av_ctx->streams[audio_stream_index];
183 audio_dec_ctx = audioStream->codecpar;
184
185 mi.aCodeID = audio_dec_ctx->codec_id;
186 mi.aCodeRate = audio_dec_ctx->bit_rate;
187 mi.aDigit = audio_dec_ctx->format;
•188 #if HAVE_CH_LAYOUT
189 mi.channels = audio_dec_ctx->ch_layout.nb_channels;
190 #else
191 mi.channels = audio_dec_ctx->channels;
192 #endif
193 mi.sampling = audio_dec_ctx->sample_rate;
194 }
195
196 auto duration = av_ctx->duration == AV_NOPTS_VALUE ? 0 : av_ctx->duration;
197 duration = duration + (duration <= INT64_MAX - 5000 ? 5000 : 0);
198 mi.duration = duration / AV_TIME_BASE;
199 mi.resolution = QString("%1x%2").arg(mi.width).arg(mi.height);
200 mi.title = fi.fileName(); //FIXME this
201 mi.filePath = fi.canonicalFilePath();
202 mi.creation = fi.created().toString();
203 mi.fileSize = fi.size();
204 mi.fileType = fi.suffix();
205
206 AVDictionaryEntry *tag = nullptr;
207 while ((tag = g_mvideo_av_dict_get(av_ctx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)) != nullptr) {
208 if (tag->key && strcmp(tag->key, "creation_time") == 0) {
209 auto dt = QDateTime::fromString(tag->value, Qt::ISODate);
210 mi.creation = dt.toString();
211 qInfo() << __func__ << dt.toString();
212 break;
213 }
214 qInfo() << "tag:" << tag->key << tag->value;
215 }
216
217 g_mvideo_avformat_close_input(&av_ctx);
218 mi.valid = true;
219
220 // if (ok) *ok = true;
221 return mi;
222 }
223
224 MovieService::MovieService(QObject *parent)
225 {
226 Q_UNUSED(parent);
227 }
228
229 void MovieService::initThumb()
230 {
231 QLibrary library(libPath("libffmpegthumbnailer.so"));
232 m_creat_video_thumbnailer = (mvideo_thumbnailer_create) library.resolve("video_thumbnailer_create");
233 m_mvideo_thumbnailer_destroy = (mvideo_thumbnailer_destroy) library.resolve("video_thumbnailer_destroy");
234 m_mvideo_thumbnailer_create_image_data = (mvideo_thumbnailer_create_image_data) library.resolve("video_thumbnailer_create_image_data");
235 m_mvideo_thumbnailer_destroy_image_data = (mvideo_thumbnailer_destroy_image_data) library.resolve("video_thumbnailer_destroy_image_data");
236 m_mvideo_thumbnailer_generate_thumbnail_to_buffer = (mvideo_thumbnailer_generate_thumbnail_to_buffer) library.resolve("video_thumbnailer_generate_thumbnail_to_buffer");
237 m_video_thumbnailer = m_creat_video_thumbnailer();
238
239 if (m_creat_video_thumbnailer == nullptr
240 || m_mvideo_thumbnailer_destroy == nullptr
241 || m_mvideo_thumbnailer_create_image_data == nullptr
242 || m_mvideo_thumbnailer_destroy_image_data == nullptr
243 || m_mvideo_thumbnailer_generate_thumbnail_to_buffer == nullptr
244 || m_video_thumbnailer == nullptr) {
245 return;
246 }
247
248 m_image_data = m_mvideo_thumbnailer_create_image_data();
249 m_video_thumbnailer->thumbnail_size = 400 * qApp->devicePixelRatio();
250 m_bInitThumb = true;
251 }
252
253 void MovieService::initFFmpeg()
254 {
255 QLibrary avcodecLibrary(libPath("libavcodec.so"));
256 QLibrary avformatLibrary(libPath("libavformat.so"));
257 QLibrary avutilLibrary(libPath("libavutil.so"));
258
259 g_mvideo_avformat_open_input = (mvideo_avformat_open_input) avformatLibrary.resolve("avformat_open_input");
260 g_mvideo_avformat_find_stream_info = (mvideo_avformat_find_stream_info) avformatLibrary.resolve("avformat_find_stream_info");
261 g_mvideo_av_find_best_stream = (mvideo_av_find_best_stream) avformatLibrary.resolve("av_find_best_stream");
262 g_mvideo_avformat_close_input = (mvideo_avformat_close_input) avformatLibrary.resolve("avformat_close_input");
263 g_mvideo_av_dict_get = (mvideo_av_dict_get) avutilLibrary.resolve("av_dict_get");
264
265 m_initFFmpeg = true;
266 }
267
268 QString MovieService::libPath(const QString &strlib)
269 {
270 QDir dir;
271 QString path = QLibraryInfo::location(QLibraryInfo::LibrariesPath);
272 dir.setPath(path);
273 QStringList list = dir.entryList(QStringList() << (strlib + "*"), QDir::NoDotAndDotDot | QDir::Files); //filter name with strlib
274 if (list.contains(strlib)) {
275 return strlib;
276 } else {
277 list.sort();
278 }
279
280 Q_ASSERT(list.size() > 0);
281 return list.last();
282 }