| 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 |
|
| 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 |
mi.channels = audio_dec_ctx->channels; |
| 189 |
mi.sampling = audio_dec_ctx->sample_rate; |
| 190 |
} |
| 191 |
|
| 192 |
auto duration = av_ctx->duration == AV_NOPTS_VALUE ? 0 : av_ctx->duration; |
| 193 |
duration = duration + (duration <= INT64_MAX - 5000 ? 5000 : 0); |
| 194 |
mi.duration = duration / AV_TIME_BASE; |
| 195 |
mi.resolution = QString("%1x%2").arg(mi.width).arg(mi.height); |
| 196 |
mi.title = fi.fileName(); //FIXME this |
| 197 |
mi.filePath = fi.canonicalFilePath(); |
| 198 |
mi.creation = fi.created().toString(); |
| 199 |
mi.fileSize = fi.size(); |
| 200 |
mi.fileType = fi.suffix(); |
| 201 |
|
| 202 |
AVDictionaryEntry *tag = nullptr; |
| 203 |
while ((tag = g_mvideo_av_dict_get(av_ctx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)) != nullptr) { |
| 204 |
if (tag->key && strcmp(tag->key, "creation_time") == 0) { |
| 205 |
auto dt = QDateTime::fromString(tag->value, Qt::ISODate); |
| 206 |
mi.creation = dt.toString(); |
| 207 |
qInfo() << __func__ << dt.toString(); |
| 208 |
break; |
| 209 |
} |
| 210 |
qInfo() << "tag:" << tag->key << tag->value; |
| 211 |
} |
| 212 |
|
| 213 |
g_mvideo_avformat_close_input(&av_ctx); |
| 214 |
mi.valid = true; |
| 215 |
|
| 216 |
// if (ok) *ok = true; |
| 217 |
return mi; |
| 218 |
} |
| 219 |
|
| 220 |
MovieService::MovieService(QObject *parent) |
| 221 |
{ |
| 222 |
Q_UNUSED(parent); |
| 223 |
} |
| 224 |
|
| 225 |
void MovieService::initThumb() |
| 226 |
{ |
| 227 |
QLibrary library(libPath("libffmpegthumbnailer.so")); |
| 228 |
m_creat_video_thumbnailer = (mvideo_thumbnailer_create) library.resolve("video_thumbnailer_create"); |
| 229 |
m_mvideo_thumbnailer_destroy = (mvideo_thumbnailer_destroy) library.resolve("video_thumbnailer_destroy"); |
| 230 |
m_mvideo_thumbnailer_create_image_data = (mvideo_thumbnailer_create_image_data) library.resolve("video_thumbnailer_create_image_data"); |
| 231 |
m_mvideo_thumbnailer_destroy_image_data = (mvideo_thumbnailer_destroy_image_data) library.resolve("video_thumbnailer_destroy_image_data"); |
| 232 |
m_mvideo_thumbnailer_generate_thumbnail_to_buffer = (mvideo_thumbnailer_generate_thumbnail_to_buffer) library.resolve("video_thumbnailer_generate_thumbnail_to_buffer"); |
| 233 |
m_video_thumbnailer = m_creat_video_thumbnailer(); |
| 234 |
|
| 235 |
if (m_creat_video_thumbnailer == nullptr |
| 236 |
|| m_mvideo_thumbnailer_destroy == nullptr |
| 237 |
|| m_mvideo_thumbnailer_create_image_data == nullptr |
| 238 |
|| m_mvideo_thumbnailer_destroy_image_data == nullptr |
| 239 |
|| m_mvideo_thumbnailer_generate_thumbnail_to_buffer == nullptr |
| 240 |
|| m_video_thumbnailer == nullptr) { |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
m_image_data = m_mvideo_thumbnailer_create_image_data(); |
| 245 |
m_video_thumbnailer->thumbnail_size = 400 * qApp->devicePixelRatio(); |
| 246 |
m_bInitThumb = true; |
| 247 |
} |
| 248 |
|
| 249 |
void MovieService::initFFmpeg() |
| 250 |
{ |
| 251 |
QLibrary avcodecLibrary(libPath("libavcodec.so")); |
| 252 |
QLibrary avformatLibrary(libPath("libavformat.so")); |
| 253 |
QLibrary avutilLibrary(libPath("libavutil.so")); |
| 254 |
|
| 255 |
g_mvideo_avformat_open_input = (mvideo_avformat_open_input) avformatLibrary.resolve("avformat_open_input"); |
| 256 |
g_mvideo_avformat_find_stream_info = (mvideo_avformat_find_stream_info) avformatLibrary.resolve("avformat_find_stream_info"); |
| 257 |
g_mvideo_av_find_best_stream = (mvideo_av_find_best_stream) avformatLibrary.resolve("av_find_best_stream"); |
| 258 |
g_mvideo_avformat_close_input = (mvideo_avformat_close_input) avformatLibrary.resolve("avformat_close_input"); |
| 259 |
g_mvideo_av_dict_get = (mvideo_av_dict_get) avutilLibrary.resolve("av_dict_get"); |
| 260 |
|
| 261 |
m_initFFmpeg = true; |
| 262 |
} |
| 263 |
|
| 264 |
QString MovieService::libPath(const QString &strlib) |
| 265 |
{ |
| 266 |
QDir dir; |
| 267 |
QString path = QLibraryInfo::location(QLibraryInfo::LibrariesPath); |
| 268 |
dir.setPath(path); |
| 269 |
QStringList list = dir.entryList(QStringList() << (strlib + "*"), QDir::NoDotAndDotDot | QDir::Files); //filter name with strlib |
| 270 |
if (list.contains(strlib)) { |
| 271 |
return strlib; |
| 272 |
} else { |
| 273 |
list.sort(); |
| 274 |
} |
| 275 |
|
| 276 |
Q_ASSERT(list.size() > 0); |
| 277 |
return list.last(); |
| 278 |
} |