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
;
48 typedef ::boost::shared_ptr
<CClipper
> SharedPtrCClipper
;
50 struct CompositeDrawItem
;
51 typedef CAtlList
<CompositeDrawItem
> CompositeDrawItemList
;
52 typedef CAtlList
<CompositeDrawItemList
> CompositeDrawItemListList
;
55 typedef CWord
* PCWord
;
56 typedef ::boost::shared_ptr
<CWord
> SharedPtrCWord
;
57 typedef ::boost::shared_ptr
<CPolygon
> SharedPtrCPolygon
;
59 class CClipperPaintMachine
;
60 typedef ::boost::shared_ptr
<CClipperPaintMachine
> SharedPtrCClipperPaintMachine
;
66 void Transform(PathData
* path_data
, const CPoint
& org
);
68 void Transform_C(PathData
* path_data
, const CPoint
&org
);
69 void Transform_SSE2(PathData
* path_data
, const CPoint
&org
);
70 bool CreateOpaqueBox();
75 virtual bool CreatePath(PathData
* path_data
) = 0;
77 bool DoPaint(const CPoint
& p
, const CPoint
& trans_org
, SharedPtrOverlay
* overlay
, const OverlayKey
& key
);
79 bool m_fWhiteSpaceChar
, m_fLineBreak
;
83 SharedPtrCPolygon m_pOpaqueBox
;
85 int m_ktype
, m_kstart
, m_kend
;
87 int m_width
, m_ascent
, m_descent
;
89 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)
93 virtual SharedPtrCWord
Copy() = 0;
94 virtual bool Append(const SharedPtrCWord
& w
);
98 static void PaintFromOverlay(const CPoint
& p
, const CPoint
& trans_org2
, OverlayKey
&subpixel_variance_key
, SharedPtrOverlay
& overlay
);
99 void PaintFromNoneBluredOverlay(SharedPtrOverlay raterize_result
, const OverlayKey
& overlay_key
, SharedPtrOverlay
* overlay
);
100 bool PaintFromScanLineData2(const CPoint
& psub
, const ScanLineData2
& scan_line_data2
, const OverlayKey
& key
, SharedPtrOverlay
* overlay
);
101 bool PaintFromPathData(const CPoint
& psub
, const CPoint
& trans_org
, const PathData
& path_data
, const OverlayKey
& key
, SharedPtrOverlay
* overlay
);
102 bool PaintFromRawData( const CPoint
& psub
, const CPoint
& trans_org
, const OverlayKey
& key
, SharedPtrOverlay
* overlay
);
104 //friend class CWordCache;
105 friend class CWordCacheKey
;
106 friend class PathDataCacheKey
;
107 friend class ClipperTraits
;
108 friend class ClipperAlphaMaskCacheKey
;
109 friend class CWordPaintMachine
;
110 friend std::size_t hash_value(const CWord
& key
);
113 class CText
: public CWord
118 int m_width
, m_ascent
, m_descent
;
120 typedef ::boost::shared_ptr
<TextInfo
> SharedPtrTextInfo
;
122 virtual bool CreatePath(PathData
* path_data
);
124 static void GetTextInfo(TextInfo
*output
, const FwSTSStyle
& style
, const CStringW
& str
);
126 CText(const FwSTSStyle
& style
, const CStringW
& str
, int ktype
, int kstart
, int kend
);
127 CText(const CText
& src
);
129 virtual SharedPtrCWord
Copy();
130 virtual bool Append(const SharedPtrCWord
& w
);
133 class CPolygon
: public CWord
135 bool Get6BitFixedPoint(CStringW
& str
, LONG
& ret
);
136 bool GetPOINT(CStringW
& str
, POINT
& ret
);
140 double m_scalex
, m_scaley
;
143 CAtlArray
<BYTE
> m_pathTypesOrg
;
144 CAtlArray
<CPoint
> m_pathPointsOrg
;
146 virtual bool CreatePath(PathData
* path_data
);
149 CPolygon(const FwSTSStyle
& style
, const CStringW
& str
, int ktype
, int kstart
, int kend
, double scalex
, double scaley
, int baseline
);
150 CPolygon(CPolygon
&); // can't use a const reference because we need to use CAtlArray::Copy which expects a non-const reference
153 virtual SharedPtrCWord
Copy();
154 virtual bool Append(const SharedPtrCWord
& w
);
156 friend class ClipperTraits
;
157 friend class ClipperAlphaMaskCacheKey
;
162 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])}
163 EF_ORG
, // {\org(x=param[0], y=param[1])}
164 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])
165 EF_BANNER
, // Banner;delay=param[0][;lefttoright=param[1];fadeawaywidth=param[2]]
166 EF_SCROLL
, // Scroll up/down=param[3];top=param[0];bottom=param[1];delay=param[2][;fadeawayheight=param[4]]
169 #define EF_NUMBEROFEFFECTS 5
176 memset(param
, 0, sizeof(param
));
177 memset(t
, 0, sizeof(t
));
179 bool operator==(const Effect
& rhs
)const
181 return type
==rhs
.type
182 && !memcmp(param
, rhs
.param
, sizeof(param
))
183 && !memcmp(t
, rhs
.t
, sizeof(t
));
194 SharedPtrCPolygon m_polygon
;
204 GrayImage2
* PaintSimpleClipper();
205 GrayImage2
* PaintBaseClipper();
206 GrayImage2
* PaintBannerClipper();
207 GrayImage2
* PaintScrollClipper();
211 CClipper(CStringW str
, CSize size
, double scalex
, double scaley
, bool inverse
);
212 void SetEffect(const Effect
& effect
, int effectType
);
215 static SharedPtrGrayImage2
GetAlphaMask(const SharedPtrCClipper
& clipper
);
217 friend class ClipperTraits
;
218 friend class ClipperAlphaMaskCacheKey
;
219 friend class CClipperPaintMachine
;
222 class CLine
: private CAtlList
<SharedPtrCWord
>
225 int m_width
, m_ascent
, m_descent
, m_borderX
, m_borderY
;
232 void AddWord2Tail(SharedPtrCWord words
);
235 CRect
PaintAll(CompositeDrawItemList
* output
, SubPicDesc
& spd
, const CRect
& clipRect
,
236 const SharedPtrCClipperPaintMachine
&clipper
,
237 CPoint p
, const CPoint
& org
, const int time
, const int alpha
);
240 class CSubtitle
: private CAtlList
<CLine
*>
243 int GetFullLineWidth(POSITION pos
);
244 int GetWrapWidth(POSITION pos
, int maxwidth
);
245 CLine
* GetNextLine(POSITION
& pos
, int maxwidth
);
251 bool m_fAnimated2
; //If this Subtitle has animate effect
254 Effect
* m_effects
[EF_NUMBEROFEFFECTS
];
256 CAtlList
<SharedPtrCWord
> m_words
;
258 SharedPtrCClipper m_pClipper
;
260 CRect m_rect
, m_clip
;
261 int m_topborder
, m_bottomborder
;
264 double m_scalex
, m_scaley
;
268 virtual ~CSubtitle();
269 virtual void Empty();
271 void CreateClippers(CSize size
);
273 void MakeLines(CSize size
, CRect marginRect
);
275 POSITION
GetHeadLinePosition();
276 CLine
* GetNextLine(POSITION
& pos
);
281 CSubtitle2():s(NULL
){}
283 CSubtitle2(CSubtitle
* s_
,const CRect
& clipRect_
, const CPoint
& org_
, const CPoint
& org2_
, const CPoint
& p_
,
284 int alpha_
, int time_
)
285 : s(s_
), clipRect(clipRect_
), org(org_
), org2(org2_
), p(p_
), alpha(alpha_
), time(time_
)
291 const CRect clipRect
;
299 typedef CAtlList
<CSubtitle2
> CSubtitle2List
;
301 class CScreenLayoutAllocator
306 int segment
, entry
, layer
;
309 CAtlList
<SubRect
> m_subrects
;
312 virtual void Empty();
314 void AdvanceToSegment(int segment
, const CAtlArray
<int>& sa
);
315 CRect
AllocRect(CSubtitle
* s
, int segment
, int entry
, int layer
, int collisions
);
318 [uuid("537DCACA-2812-4a4f-B2C6-1A34C17ADEB0")]
319 class CRenderedTextSubtitle
: public CSubPicProviderImpl
, public ISubStream
, public CSimpleTextSubtitle
380 static const int MIN_CMD_LENGTH
= 1;//c etc
381 static const int MAX_CMD_LENGTH
= 5;//alpha, iclip, xbord, xshad, ybord, yshad
382 static CAtlMap
<CStringW
, AssCmdType
, CStringElementTraits
<CStringW
>> m_cmdMap
;
385 typedef CAtlList
<AssTag
> AssTagList
;
386 typedef ::boost::shared_ptr
<const AssTagList
> SharedPtrConstAssTagList
;
390 CAtlArray
<CStringW
> strParams
;
394 CAtlMap
<int, CSubtitle
*> m_subtitleCache
;
396 CScreenLayoutAllocator m_sla
;
401 // temp variables, used when parsing the script
403 int m_animStart
, m_animEnd
;
405 int m_ktype
, m_kstart
, m_kend
;
407 int m_polygonBaselineOffset
;
409 int m_period
;//1000/m_fps
411 static void InitCmdMap();
413 void ParseEffect(CSubtitle
* sub
, const CString
& str
);
414 void ParseString(CSubtitle
* sub
, CStringW str
, const FwSTSStyle
& style
);
415 void ParsePolygon(CSubtitle
* sub
, const CStringW
& str
, const FwSTSStyle
& style
);
417 static bool ParseSSATag(AssTagList
*assTags
, const CStringW
& str
);
418 bool ParseSSATag(CSubtitle
* sub
, const AssTagList
& assTags
, STSStyle
& style
, const STSStyle
& org
, bool fAnimate
= false);
419 bool ParseSSATag(CSubtitle
* sub
, const CStringW
& str
, STSStyle
& style
, const STSStyle
& org
, bool fAnimate
= false);
421 bool ParseHtmlTag(CSubtitle
* sub
, CStringW str
, STSStyle
& style
, STSStyle
& org
);
423 double CalcAnimation(double dst
, double src
, bool fAnimate
);
425 CSubtitle
* GetSubtitle(int entry
);
428 virtual void OnChanged();
431 CRenderedTextSubtitle(CCritSec
* pLock
);
432 virtual ~CRenderedTextSubtitle();
434 virtual void Copy(CSimpleTextSubtitle
& sts
);
435 virtual void Empty();
438 bool Init(CSize size
, CRect vidrect
); // will call Deinit()
442 STDMETHODIMP
NonDelegatingQueryInterface(REFIID riid
, void** ppv
);
445 STDMETHODIMP_(POSITION
) GetStartPosition(REFERENCE_TIME rt
, double fps
);
446 STDMETHODIMP_(POSITION
) GetNext(POSITION pos
);
447 STDMETHODIMP_(REFERENCE_TIME
) GetStart(POSITION pos
, double fps
);
448 STDMETHODIMP_(REFERENCE_TIME
) GetStop(POSITION pos
, double fps
);
449 STDMETHODIMP_(VOID
) GetStartStop(POSITION pos
, double fps
, /*out*/REFERENCE_TIME
&start
, /*out*/REFERENCE_TIME
&stop
);
450 STDMETHODIMP_(bool) IsAnimated(POSITION pos
);
451 STDMETHODIMP
Render(SubPicDesc
& spd
, REFERENCE_TIME rt
, double fps
, RECT
& bbox
);
452 STDMETHODIMP
RenderEx(SubPicDesc
& spd
, REFERENCE_TIME rt
, double fps
, CAtlList
<CRect
>& rectList
);
453 STDMETHODIMP
ParseScript(SubPicDesc
& spd
, REFERENCE_TIME rt
, double fps
, CSubtitle2List
*outputSub2List
);
454 static void DoRender( SubPicDesc
& spd
, const CSubtitle2List
& sub2List
,
455 CAtlList
<CRect
> *rectList
, CompositeDrawItemListList
*compDrawItemListList
/*output*/);
456 static void RenderOneSubtitle(SubPicDesc
& spd
, const CSubtitle2
& sub2
,
457 CAtlList
<CRect
>* rectList
, CompositeDrawItemList
* compDrawItemList
/*output*/);
458 STDMETHODIMP_(bool) IsColorTypeSupported(int type
);
461 STDMETHODIMP
GetClassID(CLSID
* pClassID
);
464 STDMETHODIMP_(int) GetStreamCount();
465 STDMETHODIMP
GetStreamInfo(int i
, WCHAR
** ppName
, LCID
* pLCID
);
466 STDMETHODIMP_(int) GetStream();
467 STDMETHODIMP
SetStream(int iStream
);
468 STDMETHODIMP
Reload();