Debian Codegraph @

xbmc/video/tags/VideoTagLoaderFFmpeg.cpp

raw
1 /*
2 * Copyright (C) 2017-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "VideoTagLoaderFFmpeg.h"
10
11 #include "FileItem.h"
12 #include "NfoFile.h"
13 #include "addons/Scraper.h"
14 #include "cores/FFmpeg.h"
15 #include "filesystem/File.h"
16 #include "filesystem/StackDirectory.h"
17 #include "utils/StringUtils.h"
18 #include "video/VideoInfoTag.h"
19
20 using namespace XFILE;
21
22 static int vfs_file_read(void *h, uint8_t* buf, int size)
23 {
24 CFile* pFile = static_cast<CFile*>(h);
25 return pFile->Read(buf, size);
26 }
27
28 static int64_t vfs_file_seek(void *h, int64_t pos, int whence)
29 {
30 CFile* pFile = static_cast<CFile*>(h);
31 if (whence == AVSEEK_SIZE)
32 return pFile->GetLength();
33 else
34 return pFile->Seek(pos, whence & ~AVSEEK_FORCE);
35 }
36
37 CVideoTagLoaderFFmpeg::CVideoTagLoaderFFmpeg(const CFileItem& item,
38 const ADDON::ScraperPtr& info,
39 bool lookInFolder)
40 : IVideoInfoTagLoader(item, info, lookInFolder)
41 , m_info(info)
42 {
43 std::string filename =
44 item.IsStack() ? CStackDirectory::GetFirstStackedFile(item.GetPath()) : item.GetPath();
45
46 m_file = new CFile;
47
48 if (!m_file->Open(filename))
49 {
50 delete m_file;
51 m_file = nullptr;
52 return;
53 }
54
55 int blockSize = m_file->GetChunkSize();
56 int bufferSize = blockSize > 1 ? blockSize : 4096;
57 uint8_t* buffer = (uint8_t*)av_malloc(bufferSize);
58 m_ioctx = avio_alloc_context(buffer, bufferSize, 0,
59 m_file, vfs_file_read, nullptr,
60 vfs_file_seek);
61
62 m_fctx = avformat_alloc_context();
63 m_fctx->pb = m_ioctx;
64
65 if (m_file->IoControl(IOCTRL_SEEK_POSSIBLE, nullptr) != 1)
66 m_ioctx->seekable = 0;
67
68 const AVInputFormat* iformat = nullptr;
69 av_probe_input_buffer(m_ioctx, &iformat, m_item.GetPath().c_str(), nullptr, 0, 0);
70 if (avformat_open_input(&m_fctx, m_item.GetPath().c_str(), iformat, nullptr) < 0)
71 {
72 delete m_file;
73 m_file = nullptr;
74 }
75 }
76
77 CVideoTagLoaderFFmpeg::~CVideoTagLoaderFFmpeg()
78 {
79 if (m_fctx)
80 avformat_close_input(&m_fctx);
81 if (m_ioctx)
82 {
83 av_free(m_ioctx->buffer);
84 av_free(m_ioctx);
85 }
86 delete m_file;
87 }
88
89 bool CVideoTagLoaderFFmpeg::HasInfo() const
90 {
91 if (!m_file)
92 return false;
93
94 for (size_t i = 0; i < m_fctx->nb_streams; ++i)
95 {
96 AVDictionaryEntry* avtag;
97 avtag = av_dict_get(m_fctx->streams[i]->metadata, "filename", nullptr, AV_DICT_IGNORE_SUFFIX);
98 if (avtag && strcmp(avtag->value,"kodi-metadata") == 0)
99 {
100 m_metadata_stream = i;
101 return true;
102 }
103 else if (avtag && strcmp(avtag->value,"kodi-override-metadata") == 0)
104 {
105 m_metadata_stream = i;
106 m_override_data = true;
107 return true;
108 }
109 }
110
111 AVDictionaryEntry* avtag = nullptr;
112 if (m_item.IsType(".mkv"))
113 {
114 avtag = av_dict_get(m_fctx->metadata, "IMDBURL", nullptr, AV_DICT_IGNORE_SUFFIX);
115 if (!avtag)
116 avtag = av_dict_get(m_fctx->metadata, "TMDBURL", nullptr, AV_DICT_IGNORE_SUFFIX);
117 if (!avtag)
118 avtag = av_dict_get(m_fctx->metadata, "TITLE", nullptr, AV_DICT_IGNORE_SUFFIX);
119 } else if (m_item.IsType(".mp4") || m_item.IsType(".avi"))
120 avtag = av_dict_get(m_fctx->metadata, "title", nullptr, AV_DICT_IGNORE_SUFFIX);
121
122 return avtag != nullptr;
123 }
124
125 CInfoScanner::INFO_TYPE CVideoTagLoaderFFmpeg::Load(CVideoInfoTag& tag,
126 bool, std::vector<EmbeddedArt>* art)
127 {
128 if (m_item.IsType(".mkv"))
129 return LoadMKV(tag, art);
130 else if (m_item.IsType(".mp4"))
131 return LoadMP4(tag, art);
132 else if (m_item.IsType(".avi"))
133 return LoadAVI(tag, art);
134 else
135 return CInfoScanner::NO_NFO;
136
137 }
138
139 CInfoScanner::INFO_TYPE CVideoTagLoaderFFmpeg::LoadMKV(CVideoInfoTag& tag,
140 std::vector<EmbeddedArt>* art)
141 {
142 // embedded art
143 for (size_t i = 0; i < m_fctx->nb_streams; ++i)
144 {
145 if ((m_fctx->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC) == 0)
146 continue;
147 AVDictionaryEntry* avtag;
148 avtag = av_dict_get(m_fctx->streams[i]->metadata, "filename", nullptr, AV_DICT_IGNORE_SUFFIX);
149 std::string value;
150 if (avtag)
151 value = avtag->value;
152 avtag = av_dict_get(m_fctx->streams[i]->metadata, "mimetype", nullptr, AV_DICT_IGNORE_SUFFIX);
153 if (!value.empty() && avtag)
154 {
155 std::string type;
156 if (value == "fanart.png" || value == "fanart.jpg")
157 type = "fanart";
158 else if (value == "cover.png" || value == "cover.jpg")
159 type = "poster";
160 else if (value == "small_cover.png" || value == "small_cover.jpg")
161 type = "thumb";
162 if (type.empty())
163 continue;
164 size_t size = m_fctx->streams[i]->attached_pic.size;
165 if (art)
166 art->emplace_back(m_fctx->streams[i]->attached_pic.data, size, avtag->value, type);
167 else
168 tag.m_coverArt.emplace_back(size, avtag->value, type);
169 }
170 }
171
172 if (m_metadata_stream != -1)
173 {
174 CNfoFile nfo;
175 auto* data = m_fctx->streams[m_metadata_stream]->codecpar->extradata;
176 const char* content = reinterpret_cast<const char*>(data);
177 if (!m_override_data)
178 {
179 nfo.GetDetails(tag, content);
180 return CInfoScanner::FULL_NFO;
181 }
182 else
183 {
184 nfo.Create(content, m_info);
185 m_url = nfo.ScraperUrl();
186 return CInfoScanner::URL_NFO;
187 }
188 }
189
190 AVDictionaryEntry* avtag = nullptr;
191 bool hastag = false;
192 while ((avtag = av_dict_get(m_fctx->metadata, "", avtag, AV_DICT_IGNORE_SUFFIX)))
193 {
194 if (StringUtils::CompareNoCase(avtag->key, "imdburl") == 0 ||
195 StringUtils::CompareNoCase(avtag->key, "tmdburl") == 0)
196 {
197 CNfoFile nfo;
198 nfo.Create(avtag->value, m_info);
199 m_url = nfo.ScraperUrl();
200 return CInfoScanner::URL_NFO;
201 }
202 else if (StringUtils::CompareNoCase(avtag->key, "title") == 0)
203 tag.SetTitle(avtag->value);
204 else if (StringUtils::CompareNoCase(avtag->key, "director") == 0)
205 {
206 std::vector<std::string> dirs = StringUtils::Split(avtag->value, " / ");
207 tag.SetDirector(dirs);
208 }
209 else if (StringUtils::CompareNoCase(avtag->key, "date_released") == 0)
210 tag.SetYear(atoi(avtag->value));
211 hastag = true;
212 }
213
214 return hastag ? CInfoScanner::TITLE_NFO : CInfoScanner::NO_NFO;
215 }
216
217 // https://wiki.multimedia.cx/index.php/FFmpeg_Metadata
218 CInfoScanner::INFO_TYPE CVideoTagLoaderFFmpeg::LoadMP4(CVideoInfoTag& tag,
219 std::vector<EmbeddedArt>* art)
220 {
221 bool hasfull = false;
222 AVDictionaryEntry* avtag = nullptr;
223 // If either description or synopsis is found, assume user wants to use the tag info only
224 while ((avtag = av_dict_get(m_fctx->metadata, "", avtag, AV_DICT_IGNORE_SUFFIX)))
225 {
226 if (strcmp(avtag->key, "title") == 0)
227 tag.SetTitle(avtag->value);
228 else if (strcmp(avtag->key, "composer") == 0)
229 tag.SetWritingCredits(StringUtils::Split(avtag->value, " / "));
230 else if (strcmp(avtag->key, "genre") == 0)
231 tag.SetGenre(StringUtils::Split(avtag->value, " / "));
232 else if (strcmp(avtag->key,"date") == 0)
233 tag.SetYear(atoi(avtag->value));
234 else if (strcmp(avtag->key, "description") == 0)
235 {
236 tag.SetPlotOutline(avtag->value);
237 hasfull = true;
238 }
239 else if (strcmp(avtag->key, "synopsis") == 0)
240 {
241 tag.SetPlot(avtag->value);
242 hasfull = true;
243 }
244 else if (strcmp(avtag->key, "track") == 0)
245 tag.m_iTrack = std::stoi(avtag->value);
246 else if (strcmp(avtag->key, "album") == 0)
247 tag.SetAlbum(avtag->value);
248 else if (strcmp(avtag->key, "artist") == 0)
249 tag.SetArtist(StringUtils::Split(avtag->value, " / "));
250 }
251
252 for (size_t i = 0; i < m_fctx->nb_streams; ++i)
253 {
254 if ((m_fctx->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC) == 0)
255 continue;
256
257 size_t size = m_fctx->streams[i]->attached_pic.size;
258 const std::string type = "poster";
259 if (art)
260 art->emplace_back(m_fctx->streams[i]->attached_pic.data, size, "image/png", type);
261 else
262 tag.m_coverArt.emplace_back(size, "image/png", type);
263 }
264
265 return hasfull ? CInfoScanner::FULL_NFO : CInfoScanner::TITLE_NFO;
266 }
267
268 // https://wiki.multimedia.cx/index.php/FFmpeg_Metadata#AVI
269 CInfoScanner::INFO_TYPE CVideoTagLoaderFFmpeg::LoadAVI(CVideoInfoTag& tag,
270 std::vector<EmbeddedArt>* art)
271 {
272 AVDictionaryEntry* avtag = nullptr;
273 while ((avtag = av_dict_get(m_fctx->metadata, "", avtag, AV_DICT_IGNORE_SUFFIX)))
274 {
275 if (strcmp(avtag->key, "title") == 0)
276 tag.SetTitle(avtag->value);
277 else if (strcmp(avtag->key,"date") == 0)
278 tag.SetYear(atoi(avtag->value));
279 }
280
281 return CInfoScanner::TITLE_NFO;
282 }