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)
35 class CMyFont
: public CFont
38 int m_ascent
, m_descent
;
40 CMyFont(const STSStyleBase
& style
);
43 typedef ::boost::flyweights::flyweight
<::boost::flyweights::key_value
<STSStyleBase
, CMyFont
>, ::boost::flyweights::no_locking
> FwCMyFont
;
49 SharedPtrOverlay overlay
;
62 struct CompositeDrawItem
64 SharedPtrDrawItem shadow
;
65 SharedPtrDrawItem outline
;
66 SharedPtrDrawItem body
;
69 typedef CAtlList
<CompositeDrawItem
> CompositeDrawItemList
;
70 typedef CAtlList
<CompositeDrawItemList
> CompositeDrawItemListList
;
73 typedef CWord
* PCWord
;
74 typedef ::boost::shared_ptr
<CWord
> SharedPtrCWord
;
75 typedef ::boost::shared_ptr
<CPolygon
> SharedPtrCPolygon
;
81 void Transform(SharedPtrPathData path_data
, const CPoint
& org
);
83 void Transform_C(const SharedPtrPathData
& path_data
, const CPoint
&org
);
84 void Transform_SSE2(const SharedPtrPathData
& path_data
, const CPoint
&org
);
85 bool CreateOpaqueBox();
90 virtual bool CreatePath(const SharedPtrPathData
& path_data
) = 0;
92 bool DoPaint(const CPoint
& p
, const CPoint
& trans_org
, SharedPtrOverlay
* overlay
, const OverlayKey
& key
);
94 bool m_fWhiteSpaceChar
, m_fLineBreak
;
98 SharedPtrCPolygon m_pOpaqueBox
;
100 int m_ktype
, m_kstart
, m_kend
;
102 int m_width
, m_ascent
, m_descent
;
104 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)
108 virtual SharedPtrCWord
Copy() = 0;
109 virtual bool Append(const SharedPtrCWord
& w
);
111 //use static func instead of obj member to avoid constructing a shared_ptr from this
112 //shared_from_this may cause a exception if the obj is not owned by a shared_ptr
113 static void PaintAll(SharedPtrCWord word
,
114 const CPoint
& shadowPos
, const CPoint
& outlinePos
, const CPoint
& bodyPos
, const CPoint
& org
,
115 OverlayList
* shadow
, OverlayList
* outline
, OverlayList
* body
);
116 static void Paint(SharedPtrCWord word
, const CPoint
& psub
, const CPoint
& trans_org
, OverlayList
* overlay_list
);
118 //friend class CWordCache;
119 friend class CWordCacheKey
;
120 friend class PathDataCacheKey
;
121 friend std::size_t hash_value(const CWord
& key
);
124 class CText
: public CWord
129 int m_width
, m_ascent
, m_descent
;
131 typedef ::boost::shared_ptr
<TextInfo
> SharedPtrTextInfo
;
133 virtual bool CreatePath(const SharedPtrPathData
& path_data
);
135 static void GetTextInfo(TextInfo
*output
, const FwSTSStyle
& style
, const CStringW
& str
);
137 CText(const FwSTSStyle
& style
, const CStringW
& str
, int ktype
, int kstart
, int kend
);
138 CText(const CText
& src
);
140 virtual SharedPtrCWord
Copy();
141 virtual bool Append(const SharedPtrCWord
& w
);
144 class CPolygon
: public CWord
146 bool GetLONG(CStringW
& str
, LONG
& ret
);
147 bool GetPOINT(CStringW
& str
, POINT
& ret
);
151 double m_scalex
, m_scaley
;
154 CAtlArray
<BYTE
> m_pathTypesOrg
;
155 CAtlArray
<CPoint
> m_pathPointsOrg
;
157 virtual bool CreatePath(const SharedPtrPathData
& path_data
);
160 CPolygon(const FwSTSStyle
& style
, const CStringW
& str
, int ktype
, int kstart
, int kend
, double scalex
, double scaley
, int baseline
);
161 CPolygon(CPolygon
&); // can't use a const reference because we need to use CAtlArray::Copy which expects a non-const reference
164 virtual SharedPtrCWord
Copy();
165 virtual bool Append(const SharedPtrCWord
& w
);
170 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])}
171 EF_ORG
, // {\org(x=param[0], y=param[1])}
172 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])
173 EF_BANNER
, // Banner;delay=param[0][;lefttoright=param[1];fadeawaywidth=param[2]]
174 EF_SCROLL
, // Scroll up/down=param[3];top=param[0];bottom=param[1];delay=param[2][;fadeawayheight=param[4]]
177 #define EF_NUMBEROFEFFECTS 5
190 SharedPtrCPolygon m_polygon
;
200 SharedArrayByte m_pAlphaMask
;
202 void PaintBaseClipper();
203 void PaintBannerClipper();
204 void PaintScrollClipper();
208 CClipper(CStringW str
, CSize size
, double scalex
, double scaley
, bool inverse
);
209 void SetEffect(const Effect
& effect
, int effectType
);
212 const SharedArrayByte
& GetAlphaMask();
215 class CLine
: private CAtlList
<SharedPtrCWord
>
218 int m_width
, m_ascent
, m_descent
, m_borderX
, m_borderY
;
225 void AddWord2Tail(SharedPtrCWord words
);
228 CRect
PaintAll(CompositeDrawItemList
* output
, SubPicDesc
& spd
, const CRect
& clipRect
, SharedArrayByte pAlphaMask
, CPoint p
, const CPoint
& org
, const int time
, const int alpha
);
231 class CSubtitle
: private CAtlList
<CLine
*>
234 int GetFullLineWidth(POSITION pos
);
235 int GetWrapWidth(POSITION pos
, int maxwidth
);
236 CLine
* GetNextLine(POSITION
& pos
, int maxwidth
);
242 bool m_fAnimated2
; //If this Subtitle has animate effect
245 Effect
* m_effects
[EF_NUMBEROFEFFECTS
];
247 CAtlList
<SharedPtrCWord
> m_words
;
249 CClipper
* m_pClipper
;
251 CRect m_rect
, m_clip
;
252 int m_topborder
, m_bottomborder
;
255 double m_scalex
, m_scaley
;
259 virtual ~CSubtitle();
260 virtual void Empty();
262 void CreateClippers(CSize size
);
264 void MakeLines(CSize size
, CRect marginRect
);
266 POSITION
GetHeadLinePosition();
267 CLine
* GetNextLine(POSITION
& pos
);
272 CSubtitle2():s(NULL
){}
274 CSubtitle2(CSubtitle
* s_
,const CRect
& clipRect_
, const CPoint
& org_
, const CPoint
& org2_
, const CPoint
& p_
,
275 int alpha_
, int time_
)
276 : s(s_
), clipRect(clipRect_
), org(org_
), org2(org2_
), p(p_
), alpha(alpha_
), time(time_
)
282 const CRect clipRect
;
290 typedef CAtlList
<CSubtitle2
> CSubtitle2List
;
292 class CScreenLayoutAllocator
297 int segment
, entry
, layer
;
300 CAtlList
<SubRect
> m_subrects
;
303 virtual void Empty();
305 void AdvanceToSegment(int segment
, const CAtlArray
<int>& sa
);
306 CRect
AllocRect(CSubtitle
* s
, int segment
, int entry
, int layer
, int collisions
);
309 [uuid("537DCACA-2812-4a4f-B2C6-1A34C17ADEB0")]
310 class CRenderedTextSubtitle
: public CSubPicProviderImpl
, public ISubStream
, public CSimpleTextSubtitle
371 static const int MIN_CMD_LENGTH
= 1;//c etc
372 static const int MAX_CMD_LENGTH
= 5;//alpha, iclip, xbord, xshad, ybord, yshad
373 static CAtlMap
<CStringW
, AssCmdType
, CStringElementTraits
<CStringW
>> m_cmdMap
;
376 typedef CAtlList
<AssTag
> AssTagList
;
377 typedef ::boost::shared_ptr
<const AssTagList
> SharedPtrConstAssTagList
;
382 CAtlArray
<CStringW
> strParams
;
386 CAtlMap
<int, CSubtitle
*> m_subtitleCache
;
388 CScreenLayoutAllocator m_sla
;
393 // temp variables, used when parsing the script
395 int m_animStart
, m_animEnd
;
397 int m_ktype
, m_kstart
, m_kend
;
399 int m_polygonBaselineOffset
;
401 int m_period
;//1000/m_fps
403 static void InitCmdMap();
405 void ParseEffect(CSubtitle
* sub
, const CString
& str
);
406 void ParseString(CSubtitle
* sub
, CStringW str
, const FwSTSStyle
& style
);
407 void ParsePolygon(CSubtitle
* sub
, const CStringW
& str
, const FwSTSStyle
& style
);
408 static bool ParseSSATag(AssTagList
*assTags
, const CStringW
& str
);
409 bool ParseSSATag(CSubtitle
* sub
, const AssTagList
& assTags
, STSStyle
& style
, const STSStyle
& org
, bool fAnimate
= false);
410 bool ParseSSATag(CSubtitle
* sub
, const CStringW
& str
, STSStyle
& style
, const STSStyle
& org
, bool fAnimate
= false);
412 bool ParseHtmlTag(CSubtitle
* sub
, CStringW str
, STSStyle
& style
, STSStyle
& org
);
414 double CalcAnimation(double dst
, double src
, bool fAnimate
);
416 void Draw(SubPicDesc
& spd
, CompositeDrawItemListList
& drawItemListList
);
418 CSubtitle
* GetSubtitle(int entry
);
421 virtual void OnChanged();
424 CRenderedTextSubtitle(CCritSec
* pLock
);
425 virtual ~CRenderedTextSubtitle();
427 virtual void Copy(CRenderedTextSubtitle
& rts
);
428 virtual void Copy(CSimpleTextSubtitle
& sts
);
429 virtual void Empty();
432 bool Init(CSize size
, CRect vidrect
); // will call Deinit()
436 STDMETHODIMP
NonDelegatingQueryInterface(REFIID riid
, void** ppv
);
439 STDMETHODIMP_(POSITION
) GetStartPosition(REFERENCE_TIME rt
, double fps
);
440 STDMETHODIMP_(POSITION
) GetNext(POSITION pos
);
441 STDMETHODIMP_(REFERENCE_TIME
) GetStart(POSITION pos
, double fps
);
442 STDMETHODIMP_(REFERENCE_TIME
) GetStop(POSITION pos
, double fps
);
443 STDMETHODIMP_(VOID
) GetStartStop(POSITION pos
, double fps
, /*out*/REFERENCE_TIME
&start
, /*out*/REFERENCE_TIME
&stop
);
444 STDMETHODIMP_(bool) IsAnimated(POSITION pos
);
445 STDMETHODIMP
Render(SubPicDesc
& spd
, REFERENCE_TIME rt
, double fps
, RECT
& bbox
);
446 STDMETHODIMP
RenderEx(SubPicDesc
& spd
, REFERENCE_TIME rt
, double fps
, CAtlList
<CRect
>& rectList
);
447 STDMETHODIMP
ParseScript(SubPicDesc
& spd
, REFERENCE_TIME rt
, double fps
, CSubtitle2List
*outputSub2List
);
448 static void DoRender( SubPicDesc
& spd
, const CSubtitle2List
& sub2List
,
449 CAtlList
<CRect
> *rectList
, CompositeDrawItemListList
*drawItemListList
/*output*/);
450 static void RenderOneSubtitle(SubPicDesc
& spd
, const CSubtitle2
& sub2
,
451 CAtlList
<CRect
>* rectList
, CompositeDrawItemList
* drawItemList
/*output*/);
452 STDMETHODIMP_(bool) IsColorTypeSupported(int type
);
455 STDMETHODIMP
GetClassID(CLSID
* pClassID
);
458 STDMETHODIMP_(int) GetStreamCount();
459 STDMETHODIMP
GetStreamInfo(int i
, WCHAR
** ppName
, LCID
* pLCID
);
460 STDMETHODIMP_(int) GetStream();
461 STDMETHODIMP
SetStream(int iStream
);
462 STDMETHODIMP
Reload();