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
;
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 void DoPaint(const CPoint
& p
, const CPoint
& 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 Paint(SharedPtrCWord word
, const CPoint
& psub
, const CPoint
& trans_org
, OverlayList
* overlay_list
);
115 //friend class CWordCache;
116 friend class CWordCacheKey
;
117 friend class PathDataCacheKey
;
118 friend std::size_t hash_value(const CWord
& key
);
121 class CText
: public CWord
124 virtual bool CreatePath(const SharedPtrPathData
& path_data
);
127 CText(const FwSTSStyle
& style
, const CStringW
& str
, int ktype
, int kstart
, int kend
);
128 CText(const CText
& src
);
130 virtual SharedPtrCWord
Copy();
131 virtual bool Append(const SharedPtrCWord
& w
);
134 class CPolygon
: public CWord
136 bool GetLONG(CStringW
& str
, LONG
& ret
);
137 bool GetPOINT(CStringW
& str
, POINT
& ret
);
141 double m_scalex
, m_scaley
;
144 CAtlArray
<BYTE
> m_pathTypesOrg
;
145 CAtlArray
<CPoint
> m_pathPointsOrg
;
147 virtual bool CreatePath(const SharedPtrPathData
& path_data
);
150 CPolygon(const FwSTSStyle
& style
, const CStringW
& str
, int ktype
, int kstart
, int kend
, double scalex
, double scaley
, int baseline
);
151 CPolygon(CPolygon
&); // can't use a const reference because we need to use CAtlArray::Copy which expects a non-const reference
154 virtual SharedPtrCWord
Copy();
155 virtual bool Append(const SharedPtrCWord
& w
);
161 SharedPtrCPolygon m_polygon
;
163 CClipper(CStringW str
, CSize size
, double scalex
, double scaley
, bool inverse
);
168 SharedArrayByte m_pAlphaMask
;
171 class CLine
: private CAtlList
<SharedPtrCWord
>
174 int m_width
, m_ascent
, m_descent
, m_borderX
, m_borderY
;
181 void AddWord2Tail(SharedPtrCWord words
);
184 CRect
PaintShadow(CompositeDrawItemList
* output
, SubPicDesc
& spd
, CRect
& clipRect
, SharedArrayByte pAlphaMask
, CPoint p
, CPoint org
, int time
, int alpha
);
185 CRect
PaintOutline(CompositeDrawItemList
* output
, SubPicDesc
& spd
, CRect
& clipRect
, SharedArrayByte pAlphaMask
, CPoint p
, CPoint org
, int time
, int alpha
);
186 CRect
PaintBody(CompositeDrawItemList
* output
, SubPicDesc
& spd
, CRect
& clipRect
, SharedArrayByte pAlphaMask
, CPoint p
, CPoint org
, int time
, int alpha
);
191 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])}
192 EF_ORG
, // {\org(x=param[0], y=param[1])}
193 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])
194 EF_BANNER
, // Banner;delay=param[0][;lefttoright=param[1];fadeawaywidth=param[2]]
195 EF_SCROLL
, // Scroll up/down=param[3];top=param[0];bottom=param[1];delay=param[2][;fadeawayheight=param[4]]
198 #define EF_NUMBEROFEFFECTS 5
208 class CSubtitle
: private CAtlList
<CLine
*>
211 int GetFullLineWidth(POSITION pos
);
212 int GetWrapWidth(POSITION pos
, int maxwidth
);
213 CLine
* GetNextLine(POSITION
& pos
, int maxwidth
);
219 bool m_fAnimated2
; //If this Subtitle has animate effect
222 Effect
* m_effects
[EF_NUMBEROFEFFECTS
];
224 CAtlList
<SharedPtrCWord
> m_words
;
226 CClipper
* m_pClipper
;
228 CRect m_rect
, m_clip
;
229 int m_topborder
, m_bottomborder
;
232 double m_scalex
, m_scaley
;
236 virtual ~CSubtitle();
237 virtual void Empty();
239 void CreateClippers(CSize size
);
241 void MakeLines(CSize size
, CRect marginRect
);
243 POSITION
GetHeadLinePosition();
244 CLine
* GetNextLine(POSITION
& pos
);
247 class CScreenLayoutAllocator
252 int segment
, entry
, layer
;
255 CAtlList
<SubRect
> m_subrects
;
258 virtual void Empty();
260 void AdvanceToSegment(int segment
, const CAtlArray
<int>& sa
);
261 CRect
AllocRect(CSubtitle
* s
, int segment
, int entry
, int layer
, int collisions
);
264 [uuid("537DCACA-2812-4a4f-B2C6-1A34C17ADEB0")]
265 class CRenderedTextSubtitle
: public CSubPicProviderImpl
, public ISubStream
, public CSimpleTextSubtitle
326 static const int MIN_CMD_LENGTH
= 1;//c etc
327 static const int MAX_CMD_LENGTH
= 5;//alpha, iclip, xbord, xshad, ybord, yshad
328 static CAtlMap
<CStringW
, AssCmdType
, CStringElementTraits
<CStringW
>> m_cmdMap
;
331 typedef CAtlList
<AssTag
> AssTagList
;
332 typedef ::boost::shared_ptr
<const AssTagList
> SharedPtrConstAssTagList
;
337 CAtlArray
<CStringW
> strParams
;
341 CAtlMap
<int, CSubtitle
*> m_subtitleCache
;
343 CScreenLayoutAllocator m_sla
;
348 // temp variables, used when parsing the script
350 int m_animStart
, m_animEnd
;
352 int m_ktype
, m_kstart
, m_kend
;
354 int m_polygonBaselineOffset
;
357 static void InitCmdMap();
359 void ParseEffect(CSubtitle
* sub
, const CString
& str
);
360 void ParseString(CSubtitle
* sub
, CStringW str
, const FwSTSStyle
& style
);
361 void ParsePolygon(CSubtitle
* sub
, const CStringW
& str
, const FwSTSStyle
& style
);
362 static bool ParseSSATag(AssTagList
*assTags
, const CStringW
& str
);
363 bool ParseSSATag(CSubtitle
* sub
, const AssTagList
& assTags
, STSStyle
& style
, const STSStyle
& org
, bool fAnimate
= false);
364 bool ParseSSATag(CSubtitle
* sub
, const CStringW
& str
, STSStyle
& style
, const STSStyle
& org
, bool fAnimate
= false);
366 bool ParseHtmlTag(CSubtitle
* sub
, CStringW str
, STSStyle
& style
, STSStyle
& org
);
368 double CalcAnimation(double dst
, double src
, bool fAnimate
);
370 void Draw(SubPicDesc
& spd
, CompositeDrawItemList
& drawItemList
);
372 CSubtitle
* GetSubtitle(int entry
);
375 virtual void OnChanged();
378 CRenderedTextSubtitle(CCritSec
* pLock
);
379 virtual ~CRenderedTextSubtitle();
381 virtual void Copy(CRenderedTextSubtitle
& rts
);
382 virtual void Copy(CSimpleTextSubtitle
& sts
);
383 virtual void Empty();
386 bool Init(CSize size
, CRect vidrect
); // will call Deinit()
390 STDMETHODIMP
NonDelegatingQueryInterface(REFIID riid
, void** ppv
);
393 STDMETHODIMP_(POSITION
) GetStartPosition(REFERENCE_TIME rt
, double fps
);
394 STDMETHODIMP_(POSITION
) GetNext(POSITION pos
);
395 STDMETHODIMP_(REFERENCE_TIME
) GetStart(POSITION pos
, double fps
);
396 STDMETHODIMP_(REFERENCE_TIME
) GetStop(POSITION pos
, double fps
);
397 STDMETHODIMP_(VOID
) GetStartStop(POSITION pos
, double fps
, /*out*/REFERENCE_TIME
&start
, /*out*/REFERENCE_TIME
&stop
);
398 STDMETHODIMP_(bool) IsAnimated(POSITION pos
);
399 STDMETHODIMP
Render(SubPicDesc
& spd
, REFERENCE_TIME rt
, double fps
, RECT
& bbox
);
400 STDMETHODIMP
RenderEx(SubPicDesc
& spd
, REFERENCE_TIME rt
, double fps
, CAtlList
<CRect
>& rectList
);
402 STDMETHODIMP_(bool) IsColorTypeSupported(int type
);
405 STDMETHODIMP
GetClassID(CLSID
* pClassID
);
408 STDMETHODIMP_(int) GetStreamCount();
409 STDMETHODIMP
GetStreamInfo(int i
, WCHAR
** ppName
, LCID
* pLCID
);
410 STDMETHODIMP_(int) GetStream();
411 STDMETHODIMP
SetStream(int iStream
);
412 STDMETHODIMP
Reload();