2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "threads/SystemClock.h"
22 #include "DVDFileInfo.h"
24 #include "settings/AdvancedSettings.h"
25 #include "pictures/Picture.h"
26 #include "video/VideoInfoTag.h"
27 #include "filesystem/StackDirectory.h"
28 #include "utils/log.h"
29 #include "utils/TimeUtils.h"
30 #include "utils/URIUtils.h"
33 #include "DVDStreamInfo.h"
34 #include "DVDInputStreams/DVDInputStream.h"
36 #include "DVDInputStreams/DVDInputStreamBluray.h"
38 #include "DVDInputStreams/DVDFactoryInputStream.h"
39 #include "DVDDemuxers/DVDDemux.h"
40 #include "DVDDemuxers/DVDDemuxUtils.h"
41 #include "DVDDemuxers/DVDFactoryDemuxer.h"
42 #include "DVDDemuxers/DVDDemuxFFmpeg.h"
43 #include "DVDCodecs/DVDCodecs.h"
44 #include "DVDCodecs/DVDFactoryCodec.h"
45 #include "DVDCodecs/Video/DVDVideoCodec.h"
46 #include "DVDCodecs/Video/DVDVideoCodecFFmpeg.h"
47 #include "DVDDemuxers/DVDDemuxVobsub.h"
49 #include "DllAvCodec.h"
50 #include "DllSwScale.h"
51 #include "filesystem/File.h"
52 #include "TextureCache.h"
54 #include "utils/LangCodeExpander.h"
57 bool CDVDFileInfo::GetFileDuration(const CStdString &path, int& duration)
59 std::auto_ptr<CDVDInputStream> input;
60 std::auto_ptr<CDVDDemux> demux;
62 input.reset(CDVDFactoryInputStream::CreateInputStream(NULL, path, ""));
66 if (!input->Open(path, ""))
69 demux.reset(CDVDFactoryDemuxer::CreateDemuxer(input.get()));
73 duration = demux->GetStreamLength();
80 int DegreeToOrientation(int degrees)
95 bool CDVDFileInfo::ExtractThumb(const CStdString &strPath, CTextureDetails &details, CStreamDetails *pStreamDetails)
97 unsigned int nTime = XbmcThreads::SystemClockMillis();
98 CDVDInputStream *pInputStream = CDVDFactoryInputStream::CreateInputStream(NULL, strPath, "");
101 CLog::Log(LOGERROR, "InputStream: Error creating stream for %s", strPath.c_str());
105 if (pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD))
107 CLog::Log(LOGERROR, "InputStream: dvd streams not supported for thumb extraction, file: %s", strPath.c_str());
112 if (!pInputStream->Open(strPath.c_str(), ""))
114 CLog::Log(LOGERROR, "InputStream: Error opening, %s", strPath.c_str());
120 CDVDDemux *pDemuxer = NULL;
124 pDemuxer = CDVDFactoryDemuxer::CreateDemuxer(pInputStream);
128 CLog::Log(LOGERROR, "%s - Error creating demuxer", __FUNCTION__);
134 CLog::Log(LOGERROR, "%s - Exception thrown when opening demuxer", __FUNCTION__);
143 DemuxerToStreamDetails(pInputStream, pDemuxer, *pStreamDetails, strPath);
146 std::vector<CStdString> filenames;
147 CStdString video_path;
149 video_path = pInputStream->GetFileName();
151 video_path = strPath;
153 CUtil::ScanForExternalSubtitles(video_path, filenames);
155 for(unsigned int i=0;i<filenames.size();i++)
157 // if vobsub subtitle:
158 if (URIUtils::GetExtension(filenames[i]) == ".idx")
160 CStdString strSubFile;
161 if ( CUtil::FindVobSubPair(filenames, filenames[i], strSubFile) )
162 AddExternalSubtitleToDetails(video_path, *pStreamDetails, filenames[i], strSubFile);
166 if ( !CUtil::IsVobSub(filenames, filenames[i]) )
168 AddExternalSubtitleToDetails(video_path, *pStreamDetails, filenames[i]);
174 int nVideoStream = -1;
175 for (int i = 0; i < pDemuxer->GetNrOfStreams(); i++)
177 CDemuxStream* pStream = pDemuxer->GetStream(i);
180 if(pStream->type == STREAM_VIDEO)
183 pStream->SetDiscard(AVDISCARD_ALL);
188 int packetsTried = 0;
190 if (nVideoStream != -1)
192 CDVDVideoCodec *pVideoCodec;
194 CDVDStreamInfo hint(*pDemuxer->GetStream(nVideoStream), true);
195 hint.software = true;
197 if (hint.codec == AV_CODEC_ID_MPEG2VIDEO || hint.codec == AV_CODEC_ID_MPEG1VIDEO)
199 // libmpeg2 is not thread safe so use ffmepg for mpeg2/mpeg1 thumb extraction
200 CDVDCodecOptions dvdOptions;
201 pVideoCodec = CDVDFactoryCodec::OpenCodec(new CDVDVideoCodecFFmpeg(), hint, dvdOptions);
205 pVideoCodec = CDVDFactoryCodec::CreateVideoCodec( hint );
210 int nTotalLen = pDemuxer->GetStreamLength();
211 int nSeekTo = nTotalLen / 3;
213 CLog::Log(LOGDEBUG,"%s - seeking to pos %dms (total: %dms) in %s", __FUNCTION__, nSeekTo, nTotalLen, strPath.c_str());
214 if (pDemuxer->SeekTime(nSeekTo, true))
216 int iDecoderState = VC_ERROR;
217 DVDVideoPicture picture;
219 memset(&picture, 0, sizeof(picture));
221 // num streams * 80 frames, should get a valid frame, if not abort.
222 int abort_index = pDemuxer->GetNrOfStreams() * 80;
225 DemuxPacket* pPacket = pDemuxer->Read();
231 if (pPacket->iStreamId != nVideoStream)
233 CDVDDemuxUtils::FreeDemuxPacket(pPacket);
237 iDecoderState = pVideoCodec->Decode(pPacket->pData, pPacket->iSize, pPacket->dts, pPacket->pts);
238 CDVDDemuxUtils::FreeDemuxPacket(pPacket);
240 if (iDecoderState & VC_ERROR)
243 if (iDecoderState & VC_PICTURE)
245 memset(&picture, 0, sizeof(DVDVideoPicture));
246 if (pVideoCodec->GetPicture(&picture))
248 if(!(picture.iFlags & DVP_FLAG_DROPPED))
253 } while (abort_index--);
255 if (iDecoderState & VC_PICTURE && !(picture.iFlags & DVP_FLAG_DROPPED))
258 unsigned int nWidth = g_advancedSettings.GetThumbSize();
259 double aspect = (double)picture.iDisplayWidth / (double)picture.iDisplayHeight;
260 if(hint.forced_aspect && hint.aspect != 0)
261 aspect = hint.aspect;
262 unsigned int nHeight = (unsigned int)((double)g_advancedSettings.GetThumbSize() / aspect);
264 DllSwScale dllSwScale;
267 uint8_t *pOutBuf = new uint8_t[nWidth * nHeight * 4];
268 struct SwsContext *context = dllSwScale.sws_getContext(picture.iWidth, picture.iHeight,
269 PIX_FMT_YUV420P, nWidth, nHeight, PIX_FMT_BGRA, SWS_FAST_BILINEAR | SwScaleCPUFlags(), NULL, NULL, NULL);
273 uint8_t *src[] = { picture.data[0], picture.data[1], picture.data[2], 0 };
274 int srcStride[] = { picture.iLineSize[0], picture.iLineSize[1], picture.iLineSize[2], 0 };
275 uint8_t *dst[] = { pOutBuf, 0, 0, 0 };
276 int dstStride[] = { (int)nWidth*4, 0, 0, 0 };
277 int orientation = DegreeToOrientation(hint.orientation);
278 dllSwScale.sws_scale(context, src, srcStride, 0, picture.iHeight, dst, dstStride);
279 dllSwScale.sws_freeContext(context);
281 details.width = nWidth;
282 details.height = nHeight;
283 CPicture::CacheTexture(pOutBuf, nWidth, nHeight, nWidth * 4, orientation, nWidth, nHeight, CTextureCache::GetCachedPath(details.file));
293 CLog::Log(LOGDEBUG,"%s - decode failed in %s after %d packets.", __FUNCTION__, strPath.c_str(), packetsTried);
308 if(file.OpenForWrite(CTextureCache::GetCachedPath(details.file)))
312 unsigned int nTotalTime = XbmcThreads::SystemClockMillis() - nTime;
313 CLog::Log(LOGDEBUG,"%s - measured %u ms to extract thumb from file <%s> in %d packets. ", __FUNCTION__, nTotalTime, strPath.c_str(), packetsTried);
318 * \brief Open the item pointed to by pItem and extact streamdetails
319 * \return true if the stream details have changed
321 bool CDVDFileInfo::GetFileStreamDetails(CFileItem *pItem)
326 CStdString strFileNameAndPath;
327 if (pItem->HasVideoInfoTag())
328 strFileNameAndPath = pItem->GetVideoInfoTag()->m_strFileNameAndPath;
330 if (strFileNameAndPath.empty())
331 strFileNameAndPath = pItem->GetPath();
333 CStdString playablePath = strFileNameAndPath;
334 if (URIUtils::IsStack(playablePath))
335 playablePath = XFILE::CStackDirectory::GetFirstStackedFile(playablePath);
337 CDVDInputStream *pInputStream = CDVDFactoryInputStream::CreateInputStream(NULL, playablePath, "");
341 if (pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD) || !pInputStream->Open(playablePath.c_str(), ""))
347 CDVDDemux *pDemuxer = CDVDFactoryDemuxer::CreateDemuxer(pInputStream);
350 bool retVal = DemuxerToStreamDetails(pInputStream, pDemuxer, pItem->GetVideoInfoTag()->m_streamDetails, strFileNameAndPath);
362 bool CDVDFileInfo::DemuxerToStreamDetails(CDVDInputStream *pInputStream, CDVDDemux *pDemuxer, const std::vector<CStreamDetailSubtitle> subs, CStreamDetails &details)
364 bool result = DemuxerToStreamDetails(pInputStream, pDemuxer, details);
365 for (unsigned int i = 0; i < subs.size(); i++)
367 CStreamDetailSubtitle* sub = new CStreamDetailSubtitle();
368 sub->m_strLanguage = subs[i].m_strLanguage;
369 details.AddStream(sub);
375 /* returns true if details have been added */
376 bool CDVDFileInfo::DemuxerToStreamDetails(CDVDInputStream *pInputStream, CDVDDemux *pDemux, CStreamDetails &details, const CStdString &path)
381 for (int iStream=0; iStream<pDemux->GetNrOfStreams(); iStream++)
383 CDemuxStream *stream = pDemux->GetStream(iStream);
384 if (stream->type == STREAM_VIDEO)
386 CStreamDetailVideo *p = new CStreamDetailVideo();
387 p->m_iWidth = ((CDemuxStreamVideo *)stream)->iWidth;
388 p->m_iHeight = ((CDemuxStreamVideo *)stream)->iHeight;
389 p->m_fAspect = ((CDemuxStreamVideo *)stream)->fAspect;
390 if (p->m_fAspect == 0.0f)
391 p->m_fAspect = (float)p->m_iWidth / p->m_iHeight;
392 pDemux->GetStreamCodecName(iStream, p->m_strCodec);
393 p->m_iDuration = pDemux->GetStreamLength();
396 if (URIUtils::IsStack(path))
399 XFILE::CStackDirectory stack;
400 stack.GetDirectory(path, files);
402 // skip first path as we already know the duration
403 for (int i = 1; i < files.Size(); i++)
406 if (CDVDFileInfo::GetFileDuration(files[i]->GetPath(), duration))
407 p->m_iDuration = p->m_iDuration + duration;
411 // finally, calculate seconds
412 if (p->m_iDuration > 0)
413 p->m_iDuration = p->m_iDuration / 1000;
415 details.AddStream(p);
419 else if (stream->type == STREAM_AUDIO)
421 CStreamDetailAudio *p = new CStreamDetailAudio();
422 p->m_iChannels = ((CDemuxStreamAudio *)stream)->iChannels;
423 p->m_strLanguage = stream->language;
424 pDemux->GetStreamCodecName(iStream, p->m_strCodec);
425 details.AddStream(p);
429 else if (stream->type == STREAM_SUBTITLE)
431 CStreamDetailSubtitle *p = new CStreamDetailSubtitle();
432 p->m_strLanguage = stream->language;
433 details.AddStream(p);
438 details.DetermineBestStreams();
439 #ifdef HAVE_LIBBLURAY
440 // correct bluray runtime. we need the duration from the input stream, not the demuxer.
441 if (pInputStream->IsStreamType(DVDSTREAM_TYPE_BLURAY))
443 if(((CDVDInputStreamBluray*)pInputStream)->GetTotalTime() > 0)
445 ((CStreamDetailVideo*)details.GetNthStream(CStreamDetail::VIDEO,0))->m_iDuration = ((CDVDInputStreamBluray*)pInputStream)->GetTotalTime() / 1000;
452 bool CDVDFileInfo::AddExternalSubtitleToDetails(const CStdString &path, CStreamDetails &details, const std::string& filename, const std::string& subfilename)
454 std::string ext = URIUtils::GetExtension(filename);
455 std::string vobsubfile = subfilename;
458 if (vobsubfile.empty())
459 vobsubfile = URIUtils::ReplaceExtension(filename, ".sub");
462 if(!v.Open(filename, vobsubfile))
465 int count = v.GetNrOfStreams();
467 for(int i = 0; i < count; i++)
469 CStreamDetailSubtitle *dsub = new CStreamDetailSubtitle();
470 CDemuxStream* stream = v.GetStream(i);
471 std::string lang = stream->language;
472 if (lang.length() == 2)
475 g_LangCodeExpander.ConvertToThreeCharCode(lang3, lang);
476 dsub->m_strLanguage = lang3;
479 dsub->m_strLanguage = lang;
486 CStdString strReplace(URIUtils::ReplaceExtension(filename,".idx"));
487 if (XFILE::CFile::Exists(strReplace))
491 CStreamDetailSubtitle *dsub = new CStreamDetailSubtitle();
492 ExternalStreamInfo info;
493 CUtil::GetExternalStreamDetailsFromFilename(path, filename, info);
494 dsub->m_strLanguage = info.language;
495 details.AddStream(dsub);