Merge pull request #3819 from arnova/subtitles_for_stacks
[vuplus_xbmc] / xbmc / cores / paplayer / CodecFactory.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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/>.
18  *
19  */
20
21 #include "system.h"
22 #include "CodecFactory.h"
23 #include "MP3codec.h"
24 #include "OGGcodec.h"
25 #include "FLACcodec.h"
26 #include "WAVcodec.h"
27 #include "ModplugCodec.h"
28 #include "NSFCodec.h"
29 #ifdef HAS_SPC_CODEC
30 #include "SPCCodec.h"
31 #endif
32 #include "SIDCodec.h"
33 #include "VGMCodec.h"
34 #include "YMCodec.h"
35 #include "ADPCMCodec.h"
36 #include "TimidityCodec.h"
37 #ifdef HAS_ASAP_CODEC
38 #include "ASAPCodec.h"
39 #endif
40 #include "URL.h"
41 #include "DVDPlayerCodec.h"
42 #include "PCMCodec.h"
43 #include "utils/StringUtils.h"
44
45 ICodec* CodecFactory::CreateCodec(const CStdString& strFileType)
46 {
47   if (strFileType.Equals("mp3") || strFileType.Equals("mp2"))
48     return new MP3Codec();
49   else if (strFileType.Equals("pcm") || strFileType.Equals("l16"))
50     return new PCMCodec();
51   else if (strFileType.Equals("ape") || strFileType.Equals("mac"))
52     return new DVDPlayerCodec();
53   else if (strFileType.Equals("cdda"))
54     return new DVDPlayerCodec();
55   else if (strFileType.Equals("mpc") || strFileType.Equals("mp+") || strFileType.Equals("mpp"))
56     return new DVDPlayerCodec();
57   else if (strFileType.Equals("shn"))
58     return new DVDPlayerCodec();
59   else if (strFileType.Equals("mka"))
60     return new DVDPlayerCodec();
61   else if (strFileType.Equals("flac"))
62     return new FLACCodec();
63   else if (strFileType.Equals("wav"))
64     return new DVDPlayerCodec();
65   else if (strFileType.Equals("dts") || strFileType.Equals("ac3") ||
66            strFileType.Equals("m4a") || strFileType.Equals("aac") ||
67            strFileType.Equals("pvr"))
68     return new DVDPlayerCodec();
69   else if (strFileType.Equals("wv"))
70     return new DVDPlayerCodec();
71   else if (strFileType.Equals("669")  ||  strFileType.Equals("abc") ||
72            strFileType.Equals("amf")  ||  strFileType.Equals("ams") ||
73            strFileType.Equals("dbm")  ||  strFileType.Equals("dmf") ||
74            strFileType.Equals("dsm")  ||  strFileType.Equals("far") ||
75            strFileType.Equals("it")   ||  strFileType.Equals("j2b") ||
76            strFileType.Equals("mdl")  ||  strFileType.Equals("med") ||
77            strFileType.Equals("mod")  ||  strFileType.Equals("itgz")||
78            strFileType.Equals("mt2")  ||  strFileType.Equals("mtm") ||
79            strFileType.Equals("okt")  ||  strFileType.Equals("pat") ||
80            strFileType.Equals("psm")  ||  strFileType.Equals("ptm") ||
81            strFileType.Equals("s3m")  ||  strFileType.Equals("stm") ||
82            strFileType.Equals("ult")  ||  strFileType.Equals("umx") ||
83            strFileType.Equals("xm")   || strFileType.Equals("mdgz") ||
84            strFileType.Equals("s3gz") || strFileType.Equals("xmgz"))
85     return new ModplugCodec();
86   else if (strFileType.Equals("nsf") || strFileType.Equals("nsfstream"))
87     return new NSFCodec();
88 #ifdef HAS_SPC_CODEC
89   else if (strFileType.Equals("spc"))
90     return new SPCCodec();
91 #endif
92   else if (strFileType.Equals("sid") || strFileType.Equals("sidstream"))
93     return new SIDCodec();
94   else if (VGMCodec::IsSupportedFormat(strFileType))
95     return new VGMCodec();
96   else if (strFileType.Equals("ym"))
97     return new YMCodec();
98   else if (strFileType.Equals("wma"))
99     return new DVDPlayerCodec();
100   else if (strFileType.Equals("aiff") || strFileType.Equals("aif"))
101     return new DVDPlayerCodec();
102   else if (strFileType.Equals("xwav"))
103     return new ADPCMCodec();
104   else if (TimidityCodec::IsSupportedFormat(strFileType))
105     return new TimidityCodec();
106 #ifdef HAS_ASAP_CODEC
107   else if (ASAPCodec::IsSupportedFormat(strFileType) || strFileType.Equals("asapstream"))
108     return new ASAPCodec();
109 #endif
110   else if (strFileType.Equals("tta"))
111     return new DVDPlayerCodec();
112   else if (strFileType.Equals("tak"))
113     return new DVDPlayerCodec();
114
115   return NULL;
116 }
117
118 ICodec* CodecFactory::CreateCodecDemux(const CStdString& strFile, const CStdString& strContent, unsigned int filecache)
119 {
120   CURL urlFile(strFile);
121   if( strContent.Equals("audio/mpeg")
122   ||  strContent.Equals("audio/mpeg3")
123   ||  strContent.Equals("audio/mp3") )
124     return new MP3Codec();
125   else if (StringUtils::StartsWithNoCase(strContent, "audio/l16"))
126   {
127     PCMCodec * pcm_codec = new PCMCodec();
128     pcm_codec->SetMimeParams(strContent);
129     return pcm_codec;
130   }
131   else if( strContent.Equals("audio/aac") || strContent.Equals("audio/aacp") ||
132       strContent.Equals("audio/x-ms-wma") ||
133       strContent.Equals("audio/x-ape") || strContent.Equals("audio/ape"))
134   {
135     DVDPlayerCodec *pCodec = new DVDPlayerCodec;
136     pCodec->SetContentType(strContent);
137     return pCodec;
138   }
139   else if( strContent.Equals("application/ogg") || strContent.Equals("audio/ogg"))
140     return CreateOGGCodec(strFile,filecache);
141   else if (strContent.Equals("audio/x-xbmc-pcm"))
142   {
143     // audio/x-xbmc-pcm this is the used codec for AirTunes
144     // (apples audio only streaming)
145     DVDPlayerCodec *dvdcodec = new DVDPlayerCodec();
146     dvdcodec->SetContentType(strContent);
147     return dvdcodec;
148   }
149   else if (strContent.Equals("audio/flac") || strContent.Equals("audio/x-flac") || strContent.Equals("application/x-flac"))
150     return new FLACCodec();
151
152   if (urlFile.GetProtocol() == "shout")
153   {
154     return new MP3Codec(); // if we got this far with internet radio - content-type was wrong. gamble on mp3.
155   }
156
157   if (urlFile.GetFileType().Equals("wav") || strContent.Equals("audio/wav") || strContent.Equals("audio/x-wav"))
158   {
159     ICodec* codec;
160     //lets see what it contains...
161     //this kinda sucks 'cause if it's a plain wav file the file
162     //will be opened, sniffed and closed 2 times before it is opened *again* for wav
163     //would be better if the papcodecs could work with bitstreams instead of filenames.
164     DVDPlayerCodec *dvdcodec = new DVDPlayerCodec();
165     dvdcodec->SetContentType("audio/x-spdif-compressed");
166     if (dvdcodec->Init(strFile, filecache))
167     {
168       return dvdcodec;
169     }
170     delete dvdcodec;
171     codec = new ADPCMCodec();
172     if (codec->Init(strFile, filecache))
173     {
174       return codec;
175     }
176     delete codec;
177
178     codec = new WAVCodec();
179     if (codec->Init(strFile, filecache))
180     {
181       return codec;
182     }
183     delete codec;
184   }
185   else if (urlFile.GetFileType().Equals("ogg") || urlFile.GetFileType().Equals("oggstream") || urlFile.GetFileType().Equals("oga"))
186     return CreateOGGCodec(strFile,filecache);
187
188   //default
189   return CreateCodec(urlFile.GetFileType());
190 }
191
192 ICodec* CodecFactory::CreateOGGCodec(const CStdString& strFile,
193                                      unsigned int filecache)
194 {
195   // oldnemesis: we want to use OGGCodec() for OGG music since unlike DVDCodec 
196   // it provides better timings for Karaoke. However OGGCodec() cannot handle 
197   // ogg-flac and ogg videos, that's why this block.
198   ICodec* codec = new OGGCodec();
199   try
200   {
201     if (codec->Init(strFile, filecache))
202       return codec;
203   }
204   catch( ... )
205   {
206   }
207   delete codec;
208   return new DVDPlayerCodec();
209 }
210