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(PathData
* path_data
, const CPoint
& org
);
83 void Transform_C(PathData
* path_data
, const CPoint
&org
);
84 void Transform_SSE2(PathData
* path_data
, const CPoint
&org
);
85 bool CreateOpaqueBox();
90 virtual bool CreatePath(PathData
* 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
);
117 static void PaintFromOverlay(const CPoint
& p
, const CPoint
& trans_org2
, OverlayKey
&subpixel_variance_key
, SharedPtrOverlay
& overlay
);
118 void PaintFromNoneBluredOverlay(SharedPtrOverlay raterize_result
, const OverlayKey
& overlay_key
, SharedPtrOverlay
* overlay
);
119 bool PaintFromScanLineData(const CPoint
& psub
, const ScanLineData2
& scan_line_data2
, const OverlayKey
& key
, SharedPtrOverlay
* overlay
);
120 bool PaintFromPathData(const CPoint
& psub
, const CPoint
& trans_org
, const PathData
& path_data
, const OverlayKey
& key
, SharedPtrOverlay
* overlay
);
121 bool PaintFromRawData( const CPoint
& psub
, const CPoint
& trans_org
, const OverlayKey
& key
, SharedPtrOverlay
* overlay
);
123 //friend class CWordCache;
124 friend class CWordCacheKey
;
125 friend class PathDataCacheKey
;
126 friend std::size_t hash_value(const CWord
& key
);
129 class CText
: public CWord
134 int m_width
, m_ascent
, m_descent
;
136 typedef ::boost::shared_ptr
<TextInfo
> SharedPtrTextInfo
;
138 virtual bool CreatePath(PathData
* path_data
);
140 static void GetTextInfo(TextInfo
*output
, const FwSTSStyle
& style
, const CStringW
& str
);
142 CText(const FwSTSStyle
& style
, const CStringW
& str
, int ktype
, int kstart
, int kend
);
143 CText(const CText
& src
);
145 virtual SharedPtrCWord
Copy();
146 virtual bool Append(const SharedPtrCWord
& w
);
149 class CPolygon
: public CWord
151 bool Get6BitFixedPoint(CStringW
& str
, LONG
& ret
);
152 bool GetPOINT(CStringW
& str
, POINT
& ret
);
156 double m_scalex
, m_scaley
;
159 CAtlArray
<BYTE
> m_pathTypesOrg
;
160 CAtlArray
<CPoint
> m_pathPointsOrg
;
162 virtual bool CreatePath(PathData
* path_data
);
165 CPolygon(const FwSTSStyle
& style
, const CStringW
& str
, int ktype
, int kstart
, int kend
, double scalex
, double scaley
, int baseline
);
166 CPolygon(CPolygon
&); // can't use a const reference because we need to use CAtlArray::Copy which expects a non-const reference
169 virtual SharedPtrCWord
Copy();
170 virtual bool Append(const SharedPtrCWord
& w
);
175 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])}
176 EF_ORG
, // {\org(x=param[0], y=param[1])}
177 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])
178 EF_BANNER
, // Banner;delay=param[0][;lefttoright=param[1];fadeawaywidth=param[2]]
179 EF_SCROLL
, // Scroll up/down=param[3];top=param[0];bottom=param[1];delay=param[2][;fadeawayheight=param[4]]
182 #define EF_NUMBEROFEFFECTS 5
195 SharedPtrCPolygon m_polygon
;
205 SharedArrayByte m_pAlphaMask
;
207 void PaintBaseClipper();
208 void PaintBannerClipper();
209 void PaintScrollClipper();
213 CClipper(CStringW str
, CSize size
, double scalex
, double scaley
, bool inverse
);
214 void SetEffect(const Effect
& effect
, int effectType
);
217 const SharedArrayByte
& GetAlphaMask();
220 class CLine
: private CAtlList
<SharedPtrCWord
>
223 int m_width
, m_ascent
, m_descent
, m_borderX
, m_borderY
;
230 void AddWord2Tail(SharedPtrCWord words
);
233 CRect
PaintAll(CompositeDrawItemList
* output
, SubPicDesc
& spd
, const CRect
& clipRect
, SharedArrayByte pAlphaMask
, CPoint p
, const CPoint
& org
, const int time
, const int alpha
);
236 class CSubtitle
: private CAtlList
<CLine
*>
239 int GetFullLineWidth(POSITION pos
);
240 int GetWrapWidth(POSITION pos
, int maxwidth
);
241 CLine
* GetNextLine(POSITION
& pos
, int maxwidth
);
247 bool m_fAnimated2
; //If this Subtitle has animate effect
250 Effect
* m_effects
[EF_NUMBEROFEFFECTS
];
252 CAtlList
<SharedPtrCWord
> m_words
;
254 CClipper
* m_pClipper
;
256 CRect m_rect
, m_clip
;
257 int m_topborder
, m_bottomborder
;
260 double m_scalex
, m_scaley
;
264 virtual ~CSubtitle();
265 virtual void Empty();
267 void CreateClippers(CSize size
);
269 void MakeLines(CSize size
, CRect marginRect
);
271 POSITION
GetHeadLinePosition();
272 CLine
* GetNextLine(POSITION
& pos
);
277 CSubtitle2():s(NULL
){}
279 CSubtitle2(CSubtitle
* s_
,const CRect
& clipRect_
, const CPoint
& org_
, const CPoint
& org2_
, const CPoint
& p_
,
280 int alpha_
, int time_
)
281 : s(s_
), clipRect(clipRect_
), org(org_
), org2(org2_
), p(p_
), alpha(alpha_
), time(time_
)
287 const CRect clipRect
;
295 typedef CAtlList
<CSubtitle2
> CSubtitle2List
;
297 class CScreenLayoutAllocator
302 int segment
, entry
, layer
;
305 CAtlList
<SubRect
> m_subrects
;
308 virtual void Empty();
310 void AdvanceToSegment(int segment
, const CAtlArray
<int>& sa
);
311 CRect
AllocRect(CSubtitle
* s
, int segment
, int entry
, int layer
, int collisions
);
314 [uuid("537DCACA-2812-4a4f-B2C6-1A34C17ADEB0")]
315 class CRenderedTextSubtitle
: public CSubPicProviderImpl
, public ISubStream
, public CSimpleTextSubtitle
376 static const int MIN_CMD_LENGTH
= 1;//c etc
377 static const int MAX_CMD_LENGTH
= 5;//alpha, iclip, xbord, xshad, ybord, yshad
378 static CAtlMap
<CStringW
, AssCmdType
, CStringElementTraits
<CStringW
>> m_cmdMap
;
381 typedef CAtlList
<AssTag
> AssTagList
;
382 typedef ::boost::shared_ptr
<const AssTagList
> SharedPtrConstAssTagList
;
387 CAtlArray
<CStringW
> strParams
;
391 CAtlMap
<int, CSubtitle
*> m_subtitleCache
;
393 CScreenLayoutAllocator m_sla
;
398 // temp variables, used when parsing the script
400 int m_animStart
, m_animEnd
;
402 int m_ktype
, m_kstart
, m_kend
;
404 int m_polygonBaselineOffset
;
406 int m_period
;//1000/m_fps
408 static void InitCmdMap();
410 void ParseEffect(CSubtitle
* sub
, const CString
& str
);
411 void ParseString(CSubtitle
* sub
, CStringW str
, const FwSTSStyle
& style
);
412 void ParsePolygon(CSubtitle
* sub
, const CStringW
& str
, const FwSTSStyle
& style
);
413 static bool ParseSSATag(AssTagList
*assTags
, const CStringW
& str
);
414 bool ParseSSATag(CSubtitle
* sub
, const AssTagList
& assTags
, STSStyle
& style
, const STSStyle
& org
, bool fAnimate
= false);
415 bool ParseSSATag(CSubtitle
* sub
, const CStringW
& str
, STSStyle
& style
, const STSStyle
& org
, bool fAnimate
= false);
417 bool ParseHtmlTag(CSubtitle
* sub
, CStringW str
, STSStyle
& style
, STSStyle
& org
);
419 double CalcAnimation(double dst
, double src
, bool fAnimate
);
421 void Draw(SubPicDesc
& spd
, CompositeDrawItemListList
& drawItemListList
);
423 CSubtitle
* GetSubtitle(int entry
);
426 virtual void OnChanged();
429 CRenderedTextSubtitle(CCritSec
* pLock
);
430 virtual ~CRenderedTextSubtitle();
432 virtual void Copy(CSimpleTextSubtitle
& sts
);
433 virtual void Empty();
436 bool Init(CSize size
, CRect vidrect
); // will call Deinit()
440 STDMETHODIMP
NonDelegatingQueryInterface(REFIID riid
, void** ppv
);
443 STDMETHODIMP_(POSITION
) GetStartPosition(REFERENCE_TIME rt
, double fps
);
444 STDMETHODIMP_(POSITION
) GetNext(POSITION pos
);
445 STDMETHODIMP_(REFERENCE_TIME
) GetStart(POSITION pos
, double fps
);
446 STDMETHODIMP_(REFERENCE_TIME
) GetStop(POSITION pos
, double fps
);
447 STDMETHODIMP_(VOID
) GetStartStop(POSITION pos
, double fps
, /*out*/REFERENCE_TIME
&start
, /*out*/REFERENCE_TIME
&stop
);
448 STDMETHODIMP_(bool) IsAnimated(POSITION pos
);
449 STDMETHODIMP
Render(SubPicDesc
& spd
, REFERENCE_TIME rt
, double fps
, RECT
& bbox
);
450 STDMETHODIMP
RenderEx(SubPicDesc
& spd
, REFERENCE_TIME rt
, double fps
, CAtlList
<CRect
>& rectList
);
451 STDMETHODIMP
ParseScript(SubPicDesc
& spd
, REFERENCE_TIME rt
, double fps
, CSubtitle2List
*outputSub2List
);
452 static void DoRender( SubPicDesc
& spd
, const CSubtitle2List
& sub2List
,
453 CAtlList
<CRect
> *rectList
, CompositeDrawItemListList
*drawItemListList
/*output*/);
454 static void RenderOneSubtitle(SubPicDesc
& spd
, const CSubtitle2
& sub2
,
455 CAtlList
<CRect
>* rectList
, CompositeDrawItemList
* drawItemList
/*output*/);
456 STDMETHODIMP_(bool) IsColorTypeSupported(int type
);
459 STDMETHODIMP
GetClassID(CLSID
* pClassID
);
462 STDMETHODIMP_(int) GetStreamCount();
463 STDMETHODIMP
GetStreamInfo(int i
, WCHAR
** ppName
, LCID
* pLCID
);
464 STDMETHODIMP_(int) GetStream();
465 STDMETHODIMP
SetStream(int iStream
);
466 STDMETHODIMP
Reload();