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"
48 #include "DllAvCodec.h"
49 #include "DllSwScale.h"
50 #include "filesystem/File.h"
51 #include "TextureCache.h"
54 bool CDVDFileInfo::GetFileDuration(const CStdString &path, int& duration)
56 std::auto_ptr<CDVDInputStream> input;
57 std::auto_ptr<CDVDDemux> demux;
59 input.reset(CDVDFactoryInputStream::CreateInputStream(NULL, path, ""));
63 if (!input->Open(path, ""))
66 demux.reset(CDVDFactoryDemuxer::CreateDemuxer(input.get()));
70 duration = demux->GetStreamLength();
77 int DegreeToOrientation(int degrees)
92 bool CDVDFileInfo::ExtractThumb(const CStdString &strPath, CTextureDetails &details, CStreamDetails *pStreamDetails)
94 unsigned int nTime = XbmcThreads::SystemClockMillis();
95 CDVDInputStream *pInputStream = CDVDFactoryInputStream::CreateInputStream(NULL, strPath, "");
98 CLog::Log(LOGERROR, "InputStream: Error creating stream for %s", strPath.c_str());
102 if (pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD))
104 CLog::Log(LOGERROR, "InputStream: dvd streams not supported for thumb extraction, file: %s", strPath.c_str());
109 if (!pInputStream->Open(strPath.c_str(), ""))
111 CLog::Log(LOGERROR, "InputStream: Error opening, %s", strPath.c_str());
117 CDVDDemux *pDemuxer = NULL;
121 pDemuxer = CDVDFactoryDemuxer::CreateDemuxer(pInputStream);
125 CLog::Log(LOGERROR, "%s - Error creating demuxer", __FUNCTION__);
131 CLog::Log(LOGERROR, "%s - Exception thrown when opening demuxer", __FUNCTION__);
139 DemuxerToStreamDetails(pInputStream, pDemuxer, *pStreamDetails, strPath);
141 int nVideoStream = -1;
142 for (int i = 0; i < pDemuxer->GetNrOfStreams(); i++)
144 CDemuxStream* pStream = pDemuxer->GetStream(i);
147 if(pStream->type == STREAM_VIDEO)
150 pStream->SetDiscard(AVDISCARD_ALL);
155 int packetsTried = 0;
157 if (nVideoStream != -1)
159 CDVDVideoCodec *pVideoCodec;
161 CDVDStreamInfo hint(*pDemuxer->GetStream(nVideoStream), true);
162 hint.software = true;
164 if (hint.codec == AV_CODEC_ID_MPEG2VIDEO || hint.codec == AV_CODEC_ID_MPEG1VIDEO)
166 // libmpeg2 is not thread safe so use ffmepg for mpeg2/mpeg1 thumb extraction
167 CDVDCodecOptions dvdOptions;
168 pVideoCodec = CDVDFactoryCodec::OpenCodec(new CDVDVideoCodecFFmpeg(), hint, dvdOptions);
172 pVideoCodec = CDVDFactoryCodec::CreateVideoCodec( hint );
177 int nTotalLen = pDemuxer->GetStreamLength();
178 int nSeekTo = nTotalLen / 3;
180 CLog::Log(LOGDEBUG,"%s - seeking to pos %dms (total: %dms) in %s", __FUNCTION__, nSeekTo, nTotalLen, strPath.c_str());
181 if (pDemuxer->SeekTime(nSeekTo, true))
183 int iDecoderState = VC_ERROR;
184 DVDVideoPicture picture;
186 memset(&picture, 0, sizeof(picture));
188 // num streams * 80 frames, should get a valid frame, if not abort.
189 int abort_index = pDemuxer->GetNrOfStreams() * 80;
192 DemuxPacket* pPacket = pDemuxer->Read();
198 if (pPacket->iStreamId != nVideoStream)
200 CDVDDemuxUtils::FreeDemuxPacket(pPacket);
204 iDecoderState = pVideoCodec->Decode(pPacket->pData, pPacket->iSize, pPacket->dts, pPacket->pts);
205 CDVDDemuxUtils::FreeDemuxPacket(pPacket);
207 if (iDecoderState & VC_ERROR)
210 if (iDecoderState & VC_PICTURE)
212 memset(&picture, 0, sizeof(DVDVideoPicture));
213 if (pVideoCodec->GetPicture(&picture))
215 if(!(picture.iFlags & DVP_FLAG_DROPPED))
220 } while (abort_index--);
222 if (iDecoderState & VC_PICTURE && !(picture.iFlags & DVP_FLAG_DROPPED))
225 unsigned int nWidth = g_advancedSettings.GetThumbSize();
226 double aspect = (double)picture.iDisplayWidth / (double)picture.iDisplayHeight;
227 if(hint.forced_aspect && hint.aspect != 0)
228 aspect = hint.aspect;
229 unsigned int nHeight = (unsigned int)((double)g_advancedSettings.GetThumbSize() / aspect);
231 DllSwScale dllSwScale;
234 uint8_t *pOutBuf = new uint8_t[nWidth * nHeight * 4];
235 struct SwsContext *context = dllSwScale.sws_getContext(picture.iWidth, picture.iHeight,
236 PIX_FMT_YUV420P, nWidth, nHeight, PIX_FMT_BGRA, SWS_FAST_BILINEAR | SwScaleCPUFlags(), NULL, NULL, NULL);
240 uint8_t *src[] = { picture.data[0], picture.data[1], picture.data[2], 0 };
241 int srcStride[] = { picture.iLineSize[0], picture.iLineSize[1], picture.iLineSize[2], 0 };
242 uint8_t *dst[] = { pOutBuf, 0, 0, 0 };
243 int dstStride[] = { (int)nWidth*4, 0, 0, 0 };
244 int orientation = DegreeToOrientation(hint.orientation);
245 dllSwScale.sws_scale(context, src, srcStride, 0, picture.iHeight, dst, dstStride);
246 dllSwScale.sws_freeContext(context);
248 details.width = nWidth;
249 details.height = nHeight;
250 CPicture::CacheTexture(pOutBuf, nWidth, nHeight, nWidth * 4, orientation, nWidth, nHeight, CTextureCache::GetCachedPath(details.file));
260 CLog::Log(LOGDEBUG,"%s - decode failed in %s after %d packets.", __FUNCTION__, strPath.c_str(), packetsTried);
275 if(file.OpenForWrite(CTextureCache::GetCachedPath(details.file)))
279 unsigned int nTotalTime = XbmcThreads::SystemClockMillis() - nTime;
280 CLog::Log(LOGDEBUG,"%s - measured %u ms to extract thumb from file <%s> in %d packets. ", __FUNCTION__, nTotalTime, strPath.c_str(), packetsTried);
285 * \brief Open the item pointed to by pItem and extact streamdetails
286 * \return true if the stream details have changed
288 bool CDVDFileInfo::GetFileStreamDetails(CFileItem *pItem)
293 CStdString strFileNameAndPath;
294 if (pItem->HasVideoInfoTag())
295 strFileNameAndPath = pItem->GetVideoInfoTag()->m_strFileNameAndPath;
297 if (strFileNameAndPath.empty())
298 strFileNameAndPath = pItem->GetPath();
300 CStdString playablePath = strFileNameAndPath;
301 if (URIUtils::IsStack(playablePath))
302 playablePath = XFILE::CStackDirectory::GetFirstStackedFile(playablePath);
304 CDVDInputStream *pInputStream = CDVDFactoryInputStream::CreateInputStream(NULL, playablePath, "");
308 if (pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD) || !pInputStream->Open(playablePath.c_str(), ""))
314 CDVDDemux *pDemuxer = CDVDFactoryDemuxer::CreateDemuxer(pInputStream);
317 bool retVal = DemuxerToStreamDetails(pInputStream, pDemuxer, pItem->GetVideoInfoTag()->m_streamDetails, strFileNameAndPath);
329 /* returns true if details have been added */
330 bool CDVDFileInfo::DemuxerToStreamDetails(CDVDInputStream *pInputStream, CDVDDemux *pDemux, CStreamDetails &details, const CStdString &path)
335 for (int iStream=0; iStream<pDemux->GetNrOfStreams(); iStream++)
337 CDemuxStream *stream = pDemux->GetStream(iStream);
338 if (stream->type == STREAM_VIDEO)
340 CStreamDetailVideo *p = new CStreamDetailVideo();
341 p->m_iWidth = ((CDemuxStreamVideo *)stream)->iWidth;
342 p->m_iHeight = ((CDemuxStreamVideo *)stream)->iHeight;
343 p->m_fAspect = ((CDemuxStreamVideo *)stream)->fAspect;
344 if (p->m_fAspect == 0.0f)
345 p->m_fAspect = (float)p->m_iWidth / p->m_iHeight;
346 pDemux->GetStreamCodecName(iStream, p->m_strCodec);
347 p->m_iDuration = pDemux->GetStreamLength();
350 if (URIUtils::IsStack(path))
353 XFILE::CStackDirectory stack;
354 stack.GetDirectory(path, files);
356 // skip first path as we already know the duration
357 for (int i = 1; i < files.Size(); i++)
360 if (CDVDFileInfo::GetFileDuration(files[i]->GetPath(), duration))
361 p->m_iDuration = p->m_iDuration + duration;
365 // finally, calculate seconds
366 if (p->m_iDuration > 0)
367 p->m_iDuration = p->m_iDuration / 1000;
369 details.AddStream(p);
373 else if (stream->type == STREAM_AUDIO)
375 CStreamDetailAudio *p = new CStreamDetailAudio();
376 p->m_iChannels = ((CDemuxStreamAudio *)stream)->iChannels;
377 p->m_strLanguage = stream->language;
378 pDemux->GetStreamCodecName(iStream, p->m_strCodec);
379 details.AddStream(p);
383 else if (stream->type == STREAM_SUBTITLE)
385 CStreamDetailSubtitle *p = new CStreamDetailSubtitle();
386 p->m_strLanguage = stream->language;
387 details.AddStream(p);
392 details.DetermineBestStreams();
393 #ifdef HAVE_LIBBLURAY
394 // correct bluray runtime. we need the duration from the input stream, not the demuxer.
395 if (pInputStream->IsStreamType(DVDSTREAM_TYPE_BLURAY))
397 if(((CDVDInputStreamBluray*)pInputStream)->GetTotalTime() > 0)
399 ((CStreamDetailVideo*)details.GetNthStream(CStreamDetail::VIDEO,0))->m_iDuration = ((CDVDInputStreamBluray*)pInputStream)->GetTotalTime() / 1000;