2 * Copyright (C) 2003-2006 Gabest
3 * http://www.gabest.org
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 GNU Make; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 #include "Rasterizer.h"
26 #include "../SubPic/SubPicProviderImpl.h"
28 #include <boost/flyweight/key_value.hpp>
29 #include <boost/smart_ptr.hpp>
30 #include "mru_cache.h"
32 #define RTS_POS_SEGMENT_INDEX_BITS 16
33 #define RTS_POS_SUB_INDEX_MASK ((1<<RTS_POS_SEGMENT_INDEX_BITS)-1)
34 #define RTS_ANIMATE_SUBPIC_DUR ((int)(1000/23.976))
36 class CMyFont
: public CFont
39 int m_ascent
, m_descent
;
41 CMyFont(const STSStyleBase
& style
);
44 typedef ::boost::flyweights::flyweight
<::boost::flyweights::key_value
<STSStyleBase
, CMyFont
>, ::boost::flyweights::no_locking
> FwCMyFont
;
50 SharedPtrOverlay overlay
;
63 struct CompositeDrawItem
65 SharedPtrDrawItem shadow
;
66 SharedPtrDrawItem outline
;
67 SharedPtrDrawItem body
;
70 typedef CAtlList
<CompositeDrawItem
> CompositeDrawItemList
;
71 typedef CAtlList
<CompositeDrawItemList
> CompositeDrawItemListList
;
74 typedef CWord
* PCWord
;
75 typedef ::boost::shared_ptr
<CWord
> SharedPtrCWord
;
76 typedef ::boost::shared_ptr
<CPolygon
> SharedPtrCPolygon
;
82 void Transform(SharedPtrPathData path_data
, const CPoint
& org
);
84 void Transform_C(const SharedPtrPathData
& path_data
, const CPoint
&org
);
85 void Transform_SSE2(const SharedPtrPathData
& path_data
, const CPoint
&org
);
86 bool CreateOpaqueBox();
91 virtual bool CreatePath(const SharedPtrPathData
& path_data
) = 0;
93 bool DoPaint(const CPoint
& p
, const CPoint
& trans_org
, SharedPtrOverlay
* overlay
, const OverlayKey
& key
);
95 bool m_fWhiteSpaceChar
, m_fLineBreak
;
99 SharedPtrCPolygon m_pOpaqueBox
;
101 int m_ktype
, m_kstart
, m_kend
;
103 int m_width
, m_ascent
, m_descent
;
105 CWord(const FwSTSStyle
& style
, const CStringW
& str
, int ktype
, int kstart
, int kend
); // str[0] = 0 -> m_fLineBreak = true (in this case we only need and use the height of m_font from the whole class)
109 virtual SharedPtrCWord
Copy() = 0;
110 virtual bool Append(const SharedPtrCWord
& w
);
112 //use static func instead of obj member to avoid constructing a shared_ptr from this
113 //shared_from_this may cause a exception if the obj is not owned by a shared_ptr
114 static void Paint(SharedPtrCWord word
, const CPoint
& psub
, const CPoint
& trans_org
, OverlayList
* overlay_list
);
116 //friend class CWordCache;
117 friend class CWordCacheKey
;
118 friend class PathDataCacheKey
;
119 friend std::size_t hash_value(const CWord
& key
);
122 class CText
: public CWord
127 int m_width
, m_ascent
, m_descent
;
129 typedef ::boost::shared_ptr
<TextInfo
> SharedPtrTextInfo
;
131 virtual bool CreatePath(const SharedPtrPathData
& path_data
);
133 static void GetTextInfo(TextInfo
*output
, const FwSTSStyle
& style
, const CStringW
& str
);
135 CText(const FwSTSStyle
& style
, const CStringW
& str
, int ktype
, int kstart
, int kend
);
136 CText(const CText
& src
);
138 virtual SharedPtrCWord
Copy();
139 virtual bool Append(const SharedPtrCWord
& w
);
142 class CPolygon
: public CWord
144 bool GetLONG(CStringW
& str
, LONG
& ret
);
145 bool GetPOINT(CStringW
& str
, POINT
& ret
);
149 double m_scalex
, m_scaley
;
152 CAtlArray
<BYTE
> m_pathTypesOrg
;
153 CAtlArray
<CPoint
> m_pathPointsOrg
;
155 virtual bool CreatePath(const SharedPtrPathData
& path_data
);
158 CPolygon(const FwSTSStyle
& style
, const CStringW
& str
, int ktype
, int kstart
, int kend
, double scalex
, double scaley
, int baseline
);
159 CPolygon(CPolygon
&); // can't use a const reference because we need to use CAtlArray::Copy which expects a non-const reference
162 virtual SharedPtrCWord
Copy();
163 virtual bool Append(const SharedPtrCWord
& w
);
169 SharedPtrCPolygon m_polygon
;
171 CClipper(CStringW str
, CSize size
, double scalex
, double scaley
, bool inverse
);
176 SharedArrayByte m_pAlphaMask
;
179 class CLine
: private CAtlList
<SharedPtrCWord
>
182 int m_width
, m_ascent
, m_descent
, m_borderX
, m_borderY
;
189 void AddWord2Tail(SharedPtrCWord words
);
192 CRect
PaintAll(CompositeDrawItemList
* output
, SubPicDesc
& spd
, const CRect
& clipRect
, SharedArrayByte pAlphaMask
, CPoint p
, const CPoint
& org
, const int time
, const int alpha
);
197 EF_MOVE
= 0, // {\move(x1=param[0], y1=param[1], x2=param[2], y2=param[3], t1=t[0], t2=t[1])} or {\pos(x=param[0], y=param[1])}
198 EF_ORG
, // {\org(x=param[0], y=param[1])}
199 EF_FADE
, // {\fade(a1=param[0], a2=param[1], a3=param[2], t1=t[0], t2=t[1], t3=t[2], t4=t[3])} or {\fad(t1=t[1], t2=t[2])
200 EF_BANNER
, // Banner;delay=param[0][;lefttoright=param[1];fadeawaywidth=param[2]]
201 EF_SCROLL
, // Scroll up/down=param[3];top=param[0];bottom=param[1];delay=param[2][;fadeawayheight=param[4]]
204 #define EF_NUMBEROFEFFECTS 5
214 class CSubtitle
: private CAtlList
<CLine
*>
217 int GetFullLineWidth(POSITION pos
);
218 int GetWrapWidth(POSITION pos
, int maxwidth
);
219 CLine
* GetNextLine(POSITION
& pos
, int maxwidth
);
225 bool m_fAnimated2
; //If this Subtitle has animate effect
228 Effect
* m_effects
[EF_NUMBEROFEFFECTS
];
230 CAtlList
<SharedPtrCWord
> m_words
;
232 CClipper
* m_pClipper
;
234 CRect m_rect
, m_clip
;
235 int m_topborder
, m_bottomborder
;
238 double m_scalex
, m_scaley
;
242 virtual ~CSubtitle();
243 virtual void Empty();
245 void CreateClippers(CSize size
);
247 void MakeLines(CSize size
, CRect marginRect
);
249 POSITION
GetHeadLinePosition();
250 CLine
* GetNextLine(POSITION
& pos
);
253 class CScreenLayoutAllocator
258 int segment
, entry
, layer
;
261 CAtlList
<SubRect
> m_subrects
;
264 virtual void Empty();
266 void AdvanceToSegment(int segment
, const CAtlArray
<int>& sa
);
267 CRect
AllocRect(CSubtitle
* s
, int segment
, int entry
, int layer
, int collisions
);
270 [uuid("537DCACA-2812-4a4f-B2C6-1A34C17ADEB0")]
271 class CRenderedTextSubtitle
: public CSubPicProviderImpl
, public ISubStream
, public CSimpleTextSubtitle
332 static const int MIN_CMD_LENGTH
= 1;//c etc
333 static const int MAX_CMD_LENGTH
= 5;//alpha, iclip, xbord, xshad, ybord, yshad
334 static CAtlMap
<CStringW
, AssCmdType
, CStringElementTraits
<CStringW
>> m_cmdMap
;
337 typedef CAtlList
<AssTag
> AssTagList
;
338 typedef ::boost::shared_ptr
<const AssTagList
> SharedPtrConstAssTagList
;
343 CAtlArray
<CStringW
> strParams
;
347 CAtlMap
<int, CSubtitle
*> m_subtitleCache
;
349 CScreenLayoutAllocator m_sla
;
354 // temp variables, used when parsing the script
356 int m_animStart
, m_animEnd
;
358 int m_ktype
, m_kstart
, m_kend
;
360 int m_polygonBaselineOffset
;
363 static void InitCmdMap();
365 void ParseEffect(CSubtitle
* sub
, const CString
& str
);
366 void ParseString(CSubtitle
* sub
, CStringW str
, const FwSTSStyle
& style
);
367 void ParsePolygon(CSubtitle
* sub
, const CStringW
& str
, const FwSTSStyle
& style
);
368 static bool ParseSSATag(AssTagList
*assTags
, const CStringW
& str
);
369 bool ParseSSATag(CSubtitle
* sub
, const AssTagList
& assTags
, STSStyle
& style
, const STSStyle
& org
, bool fAnimate
= false);
370 bool ParseSSATag(CSubtitle
* sub
, const CStringW
& str
, STSStyle
& style
, const STSStyle
& org
, bool fAnimate
= false);
372 bool ParseHtmlTag(CSubtitle
* sub
, CStringW str
, STSStyle
& style
, STSStyle
& org
);
374 double CalcAnimation(double dst
, double src
, bool fAnimate
);
376 void Draw(SubPicDesc
& spd
, CompositeDrawItemListList
& drawItemListList
);
378 CSubtitle
* GetSubtitle(int entry
);
381 virtual void OnChanged();
384 CRenderedTextSubtitle(CCritSec
* pLock
);
385 virtual ~CRenderedTextSubtitle();
387 virtual void Copy(CRenderedTextSubtitle
& rts
);
388 virtual void Copy(CSimpleTextSubtitle
& sts
);
389 virtual void Empty();
392 bool Init(CSize size
, CRect vidrect
); // will call Deinit()
396 STDMETHODIMP
NonDelegatingQueryInterface(REFIID riid
, void** ppv
);
399 STDMETHODIMP_(POSITION
) GetStartPosition(REFERENCE_TIME rt
, double fps
);
400 STDMETHODIMP_(POSITION
) GetNext(POSITION pos
);
401 STDMETHODIMP_(REFERENCE_TIME
) GetStart(POSITION pos
, double fps
);
402 STDMETHODIMP_(REFERENCE_TIME
) GetStop(POSITION pos
, double fps
);
403 STDMETHODIMP_(VOID
) GetStartStop(POSITION pos
, double fps
, /*out*/REFERENCE_TIME
&start
, /*out*/REFERENCE_TIME
&stop
);
404 STDMETHODIMP_(bool) IsAnimated(POSITION pos
);
405 STDMETHODIMP
Render(SubPicDesc
& spd
, REFERENCE_TIME rt
, double fps
, RECT
& bbox
);
406 STDMETHODIMP
RenderEx(SubPicDesc
& spd
, REFERENCE_TIME rt
, double fps
, CAtlList
<CRect
>& rectList
);
408 STDMETHODIMP_(bool) IsColorTypeSupported(int type
);
411 STDMETHODIMP
GetClassID(CLSID
* pClassID
);
414 STDMETHODIMP_(int) GetStreamCount();
415 STDMETHODIMP
GetStreamInfo(int i
, WCHAR
** ppName
, LCID
* pLCID
);
416 STDMETHODIMP_(int) GetStream();
417 STDMETHODIMP
SetStream(int iStream
);
418 STDMETHODIMP
Reload();