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"
27 #include "../SubPic/ISimpleSubPic.h"
29 #include <boost/flyweight/key_value.hpp>
30 #include <boost/smart_ptr.hpp>
31 #include "mru_cache.h"
33 #define RTS_POS_SEGMENT_INDEX_BITS 16
34 #define RTS_POS_SUB_INDEX_MASK ((1<<RTS_POS_SEGMENT_INDEX_BITS)-1)
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
;
49 typedef ::boost::shared_ptr
<CClipper
> SharedPtrCClipper
;
51 struct CompositeDrawItem
;
52 typedef CAtlList
<CompositeDrawItem
> CompositeDrawItemList
;
53 typedef CAtlList
<CompositeDrawItemList
> CompositeDrawItemListList
;
56 typedef CWord
* PCWord
;
57 typedef ::boost::shared_ptr
<CWord
> SharedPtrCWord
;
58 typedef ::boost::shared_ptr
<CPolygon
> SharedPtrCPolygon
;
60 class CClipperPaintMachine
;
61 typedef ::boost::shared_ptr
<CClipperPaintMachine
> SharedPtrCClipperPaintMachine
;
64 interface IXySubRenderFrame
;
69 void Transform(PathData
* path_data
, const CPointCoor2
& org
);
71 void Transform_C(PathData
* path_data
, const CPointCoor2
&org
);
72 void Transform_SSE2(PathData
* path_data
, const CPointCoor2
&org
);
73 bool CreateOpaqueBox();
76 virtual bool CreatePath(PathData
* path_data
) = 0;
78 bool DoPaint(const CPointCoor2
& p
, const CPointCoor2
& trans_org
, SharedPtrOverlay
* overlay
, const OverlayKey
& key
);
80 // str[0] = 0 -> m_fLineBreak = true (in this case we only need and use the height of m_font from the whole class)
81 CWord(const FwSTSStyle
& style
, const CStringW
& str
, int ktype
, int kstart
, int kend
82 , double target_scale_x
=1.0, double target_scale_y
=1.0
83 , bool round_to_whole_pixel_after_scale_to_target
= false);
87 virtual SharedPtrCWord
Copy() = 0;
88 virtual bool Append(const SharedPtrCWord
& w
);
90 bool operator==(const CWord
& rhs
)const;
92 static void PaintFromOverlay(const CPointCoor2
& p
, const CPointCoor2
& trans_org2
, OverlayKey
&subpixel_variance_key
, SharedPtrOverlay
& overlay
);
93 void PaintFromNoneBluredOverlay(SharedPtrOverlay raterize_result
, const OverlayKey
& overlay_key
, SharedPtrOverlay
* overlay
);
94 bool PaintFromScanLineData2(const CPointCoor2
& psub
, const ScanLineData2
& scan_line_data2
, const OverlayKey
& key
, SharedPtrOverlay
* overlay
);
95 bool PaintFromPathData(const CPointCoor2
& psub
, const CPointCoor2
& trans_org
, const PathData
& path_data
, const OverlayKey
& key
, SharedPtrOverlay
* overlay
);
96 bool PaintFromRawData( const CPointCoor2
& psub
, const CPointCoor2
& trans_org
, const OverlayKey
& key
, SharedPtrOverlay
* overlay
);
101 bool m_fWhiteSpaceChar
, m_fLineBreak
;
105 SharedPtrCPolygon m_pOpaqueBox
;
107 int m_ktype
, m_kstart
, m_kend
;
109 int m_width
, m_ascent
, m_descent
;
111 double m_target_scale_x
, m_target_scale_y
;
112 bool m_round_to_whole_pixel_after_scale_to_target
;//it is necessary to avoid some artifacts
114 //friend class CWordCache;
115 friend class PathDataCacheKey
;
116 friend class ClipperTraits
;
117 friend class ClipperAlphaMaskCacheKey
;
118 friend class CWordPaintMachine
;
119 friend class CWordPaintResultKey
;
120 friend class CClipper
;
123 class CText
: public CWord
128 int m_width
, m_ascent
, m_descent
;
130 typedef ::boost::shared_ptr
<TextInfo
> SharedPtrTextInfo
;
132 virtual bool CreatePath(PathData
* path_data
);
134 static void GetTextInfo(TextInfo
*output
, const FwSTSStyle
& style
, const CStringW
& str
);
136 CText(const FwSTSStyle
& style
, const CStringW
& str
, int ktype
, int kstart
, int kend
137 , double target_scale_x
=1.0, double target_scale_y
=1.0);
138 CText(const CText
& src
);
140 virtual SharedPtrCWord
Copy();
141 virtual bool Append(const SharedPtrCWord
& w
);
144 class CPolygon
: public CWord
146 bool Get6BitFixedPoint(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(PathData
* path_data
);
160 CPolygon(const FwSTSStyle
& style
, const CStringW
& str
, int ktype
, int kstart
, int kend
161 , double scalex
, double scaley
, int baseline
162 , double target_scale_x
=1.0, double target_scale_y
=1.0
163 , bool round_to_whole_pixel_after_scale_to_target
= false);
164 // can't use a const reference because we need to use CAtlArray::Copy which expects a non-const reference
168 virtual SharedPtrCWord
Copy();
169 virtual bool Append(const SharedPtrCWord
& w
);
171 friend class ClipperTraits
;
172 friend class ClipperAlphaMaskCacheKey
;
177 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])}
178 EF_ORG
, // {\org(x=param[0], y=param[1])}
179 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])
180 EF_BANNER
, // Banner;delay=param[0][;lefttoright=param[1];fadeawaywidth=param[2]]
181 EF_SCROLL
, // Scroll up/down=param[3];top=param[0];bottom=param[1];delay=param[2][;fadeawayheight=param[4]]
184 #define EF_NUMBEROFEFFECTS 5
191 memset(param
, 0, sizeof(param
));
192 memset(t
, 0, sizeof(t
));
194 bool operator==(const Effect
& rhs
)const
196 return type
==rhs
.type
197 && !memcmp(param
, rhs
.param
, sizeof(param
))
198 && !memcmp(t
, rhs
.t
, sizeof(t
));
209 SharedPtrCPolygon m_polygon
;
219 GrayImage2
* PaintSimpleClipper();
220 GrayImage2
* PaintBaseClipper();
221 GrayImage2
* PaintBannerClipper();
222 GrayImage2
* PaintScrollClipper();
226 CClipper(CStringW str
, CSizeCoor2 size
, double scalex
, double scaley
, bool inverse
227 , double target_scale_x
=1.0, double target_scale_y
=1.0);
228 void SetEffect(const Effect
& effect
, int effectType
);
231 static SharedPtrGrayImage2
GetAlphaMask(const SharedPtrCClipper
& clipper
);
233 friend class ClipperTraits
;
234 friend class ClipperAlphaMaskCacheKey
;
235 friend class CClipperPaintMachine
;
238 class CLine
: private CAtlList
<SharedPtrCWord
>
241 int m_width
, m_ascent
, m_descent
, m_borderX
, m_borderY
;
248 void AddWord2Tail(SharedPtrCWord words
);
251 CRectCoor2
PaintAll(CompositeDrawItemList
* output
, const CRectCoor2
& clipRect
,
252 const SharedPtrCClipperPaintMachine
&clipper
,
253 CPoint p
, const CPoint
& org
, const int time
, const int alpha
);
256 class CSubtitle
: private CAtlList
<CLine
*>
259 int GetFullLineWidth(POSITION pos
);
260 int GetWrapWidth(POSITION pos
, int maxwidth
);
261 CLine
* GetNextLine(POSITION
& pos
, int maxwidth
);
267 bool m_fAnimated2
; //If this Subtitle has animate effect
270 Effect
* m_effects
[EF_NUMBEROFEFFECTS
];
272 CAtlList
<SharedPtrCWord
> m_words
;
274 SharedPtrCClipper m_pClipper
;
276 CRect m_rect
, m_clip
;
277 int m_topborder
, m_bottomborder
;
280 double m_scalex
, m_scaley
;
281 double m_target_scale_x
, m_target_scale_y
;
284 virtual ~CSubtitle();
285 virtual void Empty();
287 void CreateClippers(CSize size1
, const CSizeCoor2
& size_scale_to
);
289 void MakeLines(CSize size
, CRect marginRect
);
291 POSITION
GetHeadLinePosition();
292 CLine
* GetNextLine(POSITION
& pos
);
297 CSubtitle2():s(NULL
){}
299 CSubtitle2(CSubtitle
* s_
,const CRectCoor2
& clipRect_
, const CPoint
& org_
, const CPoint
& org2_
300 , const CPoint
& p_
, int alpha_
, int time_
)
301 : s(s_
), clipRect(clipRect_
), org(org_
), org2(org2_
), p(p_
), alpha(alpha_
), time(time_
)
307 const CRectCoor2 clipRect
;
315 typedef CAtlList
<CSubtitle2
> CSubtitle2List
;
317 class CScreenLayoutAllocator
322 int segment
, entry
, layer
;
325 CAtlList
<SubRect
> m_subrects
;
328 virtual void Empty();
330 void AdvanceToSegment(int segment
, const CAtlArray
<int>& sa
);
331 CRect
AllocRect(CSubtitle
* s
, int segment
, int entry
, int layer
, int collisions
);
334 [uuid("537DCACA-2812-4a4f-B2C6-1A34C17ADEB0")]
335 class CRenderedTextSubtitle
: public CSubPicProviderImpl
, public ISubStream
, public ISubPicProviderEx2
, public CSimpleTextSubtitle
396 static const int MIN_CMD_LENGTH
= 1;//c etc
397 static const int MAX_CMD_LENGTH
= 5;//alpha, iclip, xbord, xshad, ybord, yshad
398 static CAtlMap
<CStringW
, AssCmdType
, CStringElementTraits
<CStringW
>> m_cmdMap
;
401 typedef CAtlList
<AssTag
> AssTagList
;
402 typedef ::boost::shared_ptr
<const AssTagList
> SharedPtrConstAssTagList
;
406 CAtlArray
<CStringW
> strParams
;
410 CAtlMap
<int, CSubtitle
*> m_subtitleCache
;
412 CScreenLayoutAllocator m_sla
;
414 CSizeCoor2 m_size_scale_to
;
419 // temp variables, used when parsing the script
421 int m_animStart
, m_animEnd
;
423 int m_ktype
, m_kstart
, m_kend
;
425 int m_polygonBaselineOffset
;
427 int m_period
;//1000/m_fps
428 double m_target_scale_x
, m_target_scale_y
;
430 static void InitCmdMap();
432 void ParseEffect(CSubtitle
* sub
, const CString
& str
);
433 void ParseString(CSubtitle
* sub
, CStringW str
, const FwSTSStyle
& style
);
434 void ParsePolygon(CSubtitle
* sub
, const CStringW
& str
, const FwSTSStyle
& style
);
436 static bool ParseSSATag(AssTagList
*assTags
, const CStringW
& str
);
437 bool ParseSSATag(CSubtitle
* sub
, const AssTagList
& assTags
, STSStyle
& style
, const STSStyle
& org
, bool fAnimate
= false);
438 bool ParseSSATag(CSubtitle
* sub
, const CStringW
& str
, STSStyle
& style
, const STSStyle
& org
, bool fAnimate
= false);
440 bool ParseHtmlTag(CSubtitle
* sub
, CStringW str
, STSStyle
& style
, STSStyle
& org
);
442 double CalcAnimation(double dst
, double src
, bool fAnimate
);
444 CSubtitle
* GetSubtitle(int entry
);
447 virtual void OnChanged();
450 CRenderedTextSubtitle(CCritSec
* pLock
);
451 virtual ~CRenderedTextSubtitle();
453 virtual void Copy(CSimpleTextSubtitle
& sts
);
454 virtual void Empty();
457 bool Init(const SIZECoor2
& size_scale_to
, const SIZE
& size1
, const CRect
& video_rect
); // will call Deinit()
461 STDMETHODIMP
NonDelegatingQueryInterface(REFIID riid
, void** ppv
);
463 // ISubPicProviderEx2
465 STDMETHODIMP
Unlock();
466 STDMETHODIMP
RenderEx(IXySubRenderFrame
**subRenderFrame
, int spd_type
,
467 const SIZECoor2
& size_scale_to
,
468 const SIZE
& size1
, const CRect
& video_rect
,
469 REFERENCE_TIME rt
, double fps
);
471 // ISubPicProviderEx && ISubPicProviderEx2
472 STDMETHODIMP_(POSITION
) GetStartPosition(REFERENCE_TIME rt
, double fps
);
473 STDMETHODIMP_(POSITION
) GetNext(POSITION pos
);
474 STDMETHODIMP_(REFERENCE_TIME
) GetStart(POSITION pos
, double fps
);
475 STDMETHODIMP_(REFERENCE_TIME
) GetStop(POSITION pos
, double fps
);
476 STDMETHODIMP_(VOID
) GetStartStop(POSITION pos
, double fps
, /*out*/REFERENCE_TIME
&start
, /*out*/REFERENCE_TIME
&stop
);
477 STDMETHODIMP_(bool) IsAnimated(POSITION pos
);
478 STDMETHODIMP
Render(SubPicDesc
& spd
, REFERENCE_TIME rt
, double fps
, RECTCoor2
& bbox
);
479 STDMETHODIMP
RenderEx(SubPicDesc
& spd
, REFERENCE_TIME rt
, double fps
, CAtlList
<CRectCoor2
>& rectList
);
480 HRESULT
ParseScript(REFERENCE_TIME rt
, double fps
, CSubtitle2List
*outputSub2List
);
481 static void DoRender( const SIZECoor2
& output_size
, const CSubtitle2List
& sub2List
,
482 CompositeDrawItemListList
*compDrawItemListList
/*output*/);
483 static void RenderOneSubtitle(const SIZECoor2
& output_size
, const CSubtitle2
& sub2
,
484 CompositeDrawItemList
* compDrawItemList
/*output*/);
485 STDMETHODIMP_(bool) IsColorTypeSupported(int type
);
488 STDMETHODIMP
GetClassID(CLSID
* pClassID
);
491 STDMETHODIMP_(int) GetStreamCount();
492 STDMETHODIMP
GetStreamInfo(int i
, WCHAR
** ppName
, LCID
* pLCID
);
493 STDMETHODIMP_(int) GetStream();
494 STDMETHODIMP
SetStream(int iStream
);
495 STDMETHODIMP
Reload();