Minor refactor.
[xy_vsfilter.git] / src / subtitles / RTS.h
blob9b5562c9e68c28283550a017a366784a984b8146
1 /*
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)
8 * any later version.
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
22 #pragma once
24 #include "STS.h"
25 #include "Rasterizer.h"
26 #include "../SubPic/SubPicProviderImpl.h"
27 #include <atlcoll.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
38 public:
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;
46 class CPolygon;
48 struct OverlayList
50 SharedPtrOverlay overlay;
51 OverlayList* next;
53 OverlayList()
55 next = NULL;
57 ~OverlayList()
59 delete next;
63 struct CompositeDrawItem
65 SharedPtrDrawItem shadow;
66 SharedPtrDrawItem outline;
67 SharedPtrDrawItem body;
70 typedef CAtlList<CompositeDrawItem> CompositeDrawItemList;
71 typedef CAtlList<CompositeDrawItemList> CompositeDrawItemListList;
73 class CWord;
74 typedef CWord* PCWord;
75 typedef ::boost::shared_ptr<CWord> SharedPtrCWord;
76 typedef ::boost::shared_ptr<CPolygon> SharedPtrCPolygon;
78 class OverlayKey;
79 class CWord
81 bool NeedTransform();
82 void Transform(SharedPtrPathData path_data, const CPoint& org);
84 void Transform_C(const SharedPtrPathData& path_data, const CPoint &org );
85 void Transform_SSE2(const SharedPtrPathData& path_data, const CPoint &org );
86 bool CreateOpaqueBox();
88 protected:
89 CStringW m_str;
91 virtual bool CreatePath(const SharedPtrPathData& path_data) = 0;
93 bool DoPaint(const CPoint& p, const CPoint& trans_org, SharedPtrOverlay* overlay, const OverlayKey& key);
94 public:
95 bool m_fWhiteSpaceChar, m_fLineBreak;
97 FwSTSStyle m_style;
99 SharedPtrCPolygon m_pOpaqueBox;
101 int m_ktype, m_kstart, m_kend;
103 int m_width, m_ascent, m_descent;
105 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)
106 CWord(const CWord&);
107 virtual ~CWord();
109 virtual SharedPtrCWord Copy() = 0;
110 virtual bool Append(const SharedPtrCWord& w);
112 //use static func instead of obj member to avoid constructing a shared_ptr from this
113 //shared_from_this may cause a exception if the obj is not owned by a shared_ptr
114 static void Paint(SharedPtrCWord word, const CPoint& psub, const CPoint& trans_org, OverlayList* overlay_list);
116 //friend class CWordCache;
117 friend class CWordCacheKey;
118 friend class PathDataCacheKey;
119 friend std::size_t hash_value(const CWord& key);
122 class CText : public CWord
124 public:
125 struct TextInfo
127 int m_width, m_ascent, m_descent;
129 typedef ::boost::shared_ptr<TextInfo> SharedPtrTextInfo;
130 protected:
131 virtual bool CreatePath(const SharedPtrPathData& path_data);
133 static void GetTextInfo(TextInfo *output, const FwSTSStyle& style, const CStringW& str);
134 public:
135 CText(const FwSTSStyle& style, const CStringW& str, int ktype, int kstart, int kend);
136 CText(const CText& src);
138 virtual SharedPtrCWord Copy();
139 virtual bool Append(const SharedPtrCWord& w);
142 class CPolygon : public CWord
144 bool GetLONG(CStringW& str, LONG& ret);
145 bool GetPOINT(CStringW& str, POINT& ret);
146 bool ParseStr();
148 protected:
149 double m_scalex, m_scaley;
150 int m_baseline;
152 CAtlArray<BYTE> m_pathTypesOrg;
153 CAtlArray<CPoint> m_pathPointsOrg;
155 virtual bool CreatePath(const SharedPtrPathData& path_data);
157 public:
158 CPolygon(const FwSTSStyle& style, const CStringW& str, int ktype, int kstart, int kend, double scalex, double scaley, int baseline);
159 CPolygon(CPolygon&); // can't use a const reference because we need to use CAtlArray::Copy which expects a non-const reference
160 virtual ~CPolygon();
162 virtual SharedPtrCWord Copy();
163 virtual bool Append(const SharedPtrCWord& w);
166 class CClipper
168 private:
169 SharedPtrCPolygon m_polygon;
170 public:
171 CClipper(CStringW str, CSize size, double scalex, double scaley, bool inverse);
172 virtual ~CClipper();
174 CSize m_size;
175 bool m_inverse;
176 SharedArrayByte m_pAlphaMask;
179 class CLine: private CAtlList<SharedPtrCWord>
181 public:
182 int m_width, m_ascent, m_descent, m_borderX, m_borderY;
184 virtual ~CLine();
186 void Compact();
188 int GetWordCount();
189 void AddWord2Tail(SharedPtrCWord words);
190 bool IsEmpty();
192 CRect PaintAll(CompositeDrawItemList* output, SubPicDesc& spd, const CRect& clipRect, SharedArrayByte pAlphaMask, CPoint p, const CPoint& org, const int time, const int alpha);
195 enum eftype
197 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])}
198 EF_ORG, // {\org(x=param[0], y=param[1])}
199 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])
200 EF_BANNER, // Banner;delay=param[0][;lefttoright=param[1];fadeawaywidth=param[2]]
201 EF_SCROLL, // Scroll up/down=param[3];top=param[0];bottom=param[1];delay=param[2][;fadeawayheight=param[4]]
204 #define EF_NUMBEROFEFFECTS 5
206 class Effect
208 public:
209 enum eftype type;
210 int param[8];
211 int t[4];
214 class CSubtitle: private CAtlList<CLine*>
216 int GetFullWidth();
217 int GetFullLineWidth(POSITION pos);
218 int GetWrapWidth(POSITION pos, int maxwidth);
219 CLine* GetNextLine(POSITION& pos, int maxwidth);
221 public:
222 int m_scrAlignment;
223 int m_wrapStyle;
224 bool m_fAnimated;
225 bool m_fAnimated2; //If this Subtitle has animate effect
226 int m_relativeTo;
228 Effect* m_effects[EF_NUMBEROFEFFECTS];
230 CAtlList<SharedPtrCWord> m_words;
232 CClipper* m_pClipper;
234 CRect m_rect, m_clip;
235 int m_topborder, m_bottomborder;
236 bool m_clipInverse;
238 double m_scalex, m_scaley;
240 public:
241 CSubtitle();
242 virtual ~CSubtitle();
243 virtual void Empty();
245 void CreateClippers(CSize size);
247 void MakeLines(CSize size, CRect marginRect);
249 POSITION GetHeadLinePosition();
250 CLine* GetNextLine(POSITION& pos);
253 class CScreenLayoutAllocator
255 typedef struct
257 CRect r;
258 int segment, entry, layer;
259 } SubRect;
261 CAtlList<SubRect> m_subrects;
263 public:
264 virtual void Empty();
266 void AdvanceToSegment(int segment, const CAtlArray<int>& sa);
267 CRect AllocRect(CSubtitle* s, int segment, int entry, int layer, int collisions);
270 [uuid("537DCACA-2812-4a4f-B2C6-1A34C17ADEB0")]
271 class CRenderedTextSubtitle : public CSubPicProviderImpl, public ISubStream, public CSimpleTextSubtitle
273 public:
274 enum AssCmdType
276 CMD_1c = 0,
277 CMD_2c,
278 CMD_3c,
279 CMD_4c,
280 CMD_1a,
281 CMD_2a,
282 CMD_3a,
283 CMD_4a,
284 CMD_alpha,
285 CMD_an,
286 CMD_a,
287 CMD_blur,
288 CMD_bord,
289 CMD_be,
290 CMD_b,
291 CMD_clip,
292 CMD_iclip,
293 CMD_c,
294 CMD_fade,
295 CMD_fad,
296 CMD_fax,
297 CMD_fay,
298 CMD_fe,
299 CMD_fn,
300 CMD_frx,
301 CMD_fry,
302 CMD_frz,
303 CMD_fr,
304 CMD_fscx,
305 CMD_fscy,
306 CMD_fsc,
307 CMD_fsp,
308 CMD_fs,
309 CMD_i,
310 CMD_kt,
311 CMD_kf,
312 CMD_K,
313 CMD_ko,
314 CMD_k,
315 CMD_move,
316 CMD_org,
317 CMD_pbo,
318 CMD_pos,
319 CMD_p,
320 CMD_q,
321 CMD_r,
322 CMD_shad,
323 CMD_s,
324 CMD_t,
325 CMD_u,
326 CMD_xbord,
327 CMD_xshad,
328 CMD_ybord,
329 CMD_yshad,
330 CMD_COUNT
332 static const int MIN_CMD_LENGTH = 1;//c etc
333 static const int MAX_CMD_LENGTH = 5;//alpha, iclip, xbord, xshad, ybord, yshad
334 static CAtlMap<CStringW, AssCmdType, CStringElementTraits<CStringW>> m_cmdMap;
336 struct AssTag;
337 typedef CAtlList<AssTag> AssTagList;
338 typedef ::boost::shared_ptr<const AssTagList> SharedPtrConstAssTagList;
339 struct AssTag
341 CStringW cmd;
342 AssCmdType cmdType;
343 CAtlArray<CStringW> strParams;
344 AssTagList embeded;
346 private:
347 CAtlMap<int, CSubtitle*> m_subtitleCache;
349 CScreenLayoutAllocator m_sla;
351 CSize m_size;
352 CRect m_vidrect;
354 // temp variables, used when parsing the script
355 int m_time, m_delay;
356 int m_animStart, m_animEnd;
357 double m_animAccel;
358 int m_ktype, m_kstart, m_kend;
359 int m_nPolygon;
360 int m_polygonBaselineOffset;
361 double m_fps;
363 static void InitCmdMap();
365 void ParseEffect(CSubtitle* sub, const CString& str);
366 void ParseString(CSubtitle* sub, CStringW str, const FwSTSStyle& style);
367 void ParsePolygon(CSubtitle* sub, const CStringW& str, const FwSTSStyle& style);
368 static bool ParseSSATag(AssTagList *assTags, const CStringW& str);
369 bool ParseSSATag(CSubtitle* sub, const AssTagList& assTags, STSStyle& style, const STSStyle& org, bool fAnimate = false);
370 bool ParseSSATag(CSubtitle* sub, const CStringW& str, STSStyle& style, const STSStyle& org, bool fAnimate = false);
372 bool ParseHtmlTag(CSubtitle* sub, CStringW str, STSStyle& style, STSStyle& org);
374 double CalcAnimation(double dst, double src, bool fAnimate);
376 void Draw(SubPicDesc& spd, CompositeDrawItemListList& drawItemListList);
378 CSubtitle* GetSubtitle(int entry);
380 protected:
381 virtual void OnChanged();
383 public:
384 CRenderedTextSubtitle(CCritSec* pLock);
385 virtual ~CRenderedTextSubtitle();
387 virtual void Copy(CRenderedTextSubtitle& rts);
388 virtual void Copy(CSimpleTextSubtitle& sts);
389 virtual void Empty();
391 public:
392 bool Init(CSize size, CRect vidrect); // will call Deinit()
393 void Deinit();
395 DECLARE_IUNKNOWN
396 STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void** ppv);
398 // ISubPicProviderEx
399 STDMETHODIMP_(POSITION) GetStartPosition(REFERENCE_TIME rt, double fps);
400 STDMETHODIMP_(POSITION) GetNext(POSITION pos);
401 STDMETHODIMP_(REFERENCE_TIME) GetStart(POSITION pos, double fps);
402 STDMETHODIMP_(REFERENCE_TIME) GetStop(POSITION pos, double fps);
403 STDMETHODIMP_(VOID) GetStartStop(POSITION pos, double fps, /*out*/REFERENCE_TIME &start, /*out*/REFERENCE_TIME &stop);
404 STDMETHODIMP_(bool) IsAnimated(POSITION pos);
405 STDMETHODIMP Render(SubPicDesc& spd, REFERENCE_TIME rt, double fps, RECT& bbox);
406 STDMETHODIMP RenderEx(SubPicDesc& spd, REFERENCE_TIME rt, double fps, CAtlList<CRect>& rectList);
408 STDMETHODIMP_(bool) IsColorTypeSupported(int type);
410 // IPersist
411 STDMETHODIMP GetClassID(CLSID* pClassID);
413 // ISubStream
414 STDMETHODIMP_(int) GetStreamCount();
415 STDMETHODIMP GetStreamInfo(int i, WCHAR** ppName, LCID* pLCID);
416 STDMETHODIMP_(int) GetStream();
417 STDMETHODIMP SetStream(int iStream);
418 STDMETHODIMP Reload();