Add parsed data cache.
[xy_vsfilter.git] / src / subtitles / RTS.h
blobcd8c158363d0827b9734838177bb9520cf2cbd91
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;
72 class CWord;
73 typedef CWord* PCWord;
74 typedef ::boost::shared_ptr<CWord> SharedPtrCWord;
75 typedef ::boost::shared_ptr<CPolygon> SharedPtrCPolygon;
77 class OverlayKey;
78 class CWord
80 bool NeedTransform();
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();
87 protected:
88 CStringW m_str;
90 virtual bool CreatePath(const SharedPtrPathData& path_data) = 0;
92 void DoPaint(const CPoint& p, const CPoint& org, SharedPtrOverlay* overlay, const OverlayKey& key);
93 public:
94 bool m_fWhiteSpaceChar, m_fLineBreak;
96 FwSTSStyle m_style;
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)
105 CWord(const CWord&);
106 virtual ~CWord();
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
123 protected:
124 virtual bool CreatePath(const SharedPtrPathData& path_data);
126 public:
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);
138 bool ParseStr();
140 protected:
141 double m_scalex, m_scaley;
142 int m_baseline;
144 CAtlArray<BYTE> m_pathTypesOrg;
145 CAtlArray<CPoint> m_pathPointsOrg;
147 virtual bool CreatePath(const SharedPtrPathData& path_data);
149 public:
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
152 virtual ~CPolygon();
154 virtual SharedPtrCWord Copy();
155 virtual bool Append(const SharedPtrCWord& w);
158 class CClipper
160 private:
161 SharedPtrCPolygon m_polygon;
162 public:
163 CClipper(CStringW str, CSize size, double scalex, double scaley, bool inverse);
164 virtual ~CClipper();
166 CSize m_size;
167 bool m_inverse;
168 SharedArrayByte m_pAlphaMask;
171 class CLine: private CAtlList<SharedPtrCWord>
173 public:
174 int m_width, m_ascent, m_descent, m_borderX, m_borderY;
176 virtual ~CLine();
178 void Compact();
180 int GetWordCount();
181 void AddWord2Tail(SharedPtrCWord words);
182 bool IsEmpty();
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);
189 enum eftype
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
200 class Effect
202 public:
203 enum eftype type;
204 int param[8];
205 int t[4];
208 class CSubtitle: private CAtlList<CLine*>
210 int GetFullWidth();
211 int GetFullLineWidth(POSITION pos);
212 int GetWrapWidth(POSITION pos, int maxwidth);
213 CLine* GetNextLine(POSITION& pos, int maxwidth);
215 public:
216 int m_scrAlignment;
217 int m_wrapStyle;
218 bool m_fAnimated;
219 bool m_fAnimated2; //If this Subtitle has animate effect
220 int m_relativeTo;
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;
230 bool m_clipInverse;
232 double m_scalex, m_scaley;
234 public:
235 CSubtitle();
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
249 typedef struct
251 CRect r;
252 int segment, entry, layer;
253 } SubRect;
255 CAtlList<SubRect> m_subrects;
257 public:
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
267 public:
268 enum AssCmdType
270 CMD_1c = 0,
271 CMD_2c,
272 CMD_3c,
273 CMD_4c,
274 CMD_1a,
275 CMD_2a,
276 CMD_3a,
277 CMD_4a,
278 CMD_alpha,
279 CMD_an,
280 CMD_a,
281 CMD_blur,
282 CMD_bord,
283 CMD_be,
284 CMD_b,
285 CMD_clip,
286 CMD_iclip,
287 CMD_c,
288 CMD_fade,
289 CMD_fad,
290 CMD_fax,
291 CMD_fay,
292 CMD_fe,
293 CMD_fn,
294 CMD_frx,
295 CMD_fry,
296 CMD_frz,
297 CMD_fr,
298 CMD_fscx,
299 CMD_fscy,
300 CMD_fsc,
301 CMD_fsp,
302 CMD_fs,
303 CMD_i,
304 CMD_kt,
305 CMD_kf,
306 CMD_K,
307 CMD_ko,
308 CMD_k,
309 CMD_move,
310 CMD_org,
311 CMD_pbo,
312 CMD_pos,
313 CMD_p,
314 CMD_q,
315 CMD_r,
316 CMD_shad,
317 CMD_s,
318 CMD_t,
319 CMD_u,
320 CMD_xbord,
321 CMD_xshad,
322 CMD_ybord,
323 CMD_yshad,
324 CMD_COUNT
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;
330 struct AssTag;
331 typedef CAtlList<AssTag> AssTagList;
332 typedef ::boost::shared_ptr<const AssTagList> SharedPtrConstAssTagList;
333 struct AssTag
335 CStringW cmd;
336 AssCmdType cmdType;
337 CAtlArray<CStringW> strParams;
338 AssTagList embeded;
340 private:
341 CAtlMap<int, CSubtitle*> m_subtitleCache;
343 CScreenLayoutAllocator m_sla;
345 CSize m_size;
346 CRect m_vidrect;
348 // temp variables, used when parsing the script
349 int m_time, m_delay;
350 int m_animStart, m_animEnd;
351 double m_animAccel;
352 int m_ktype, m_kstart, m_kend;
353 int m_nPolygon;
354 int m_polygonBaselineOffset;
355 double m_fps;
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);
374 protected:
375 virtual void OnChanged();
377 public:
378 CRenderedTextSubtitle(CCritSec* pLock);
379 virtual ~CRenderedTextSubtitle();
381 virtual void Copy(CRenderedTextSubtitle& rts);
382 virtual void Copy(CSimpleTextSubtitle& sts);
383 virtual void Empty();
385 public:
386 bool Init(CSize size, CRect vidrect); // will call Deinit()
387 void Deinit();
389 DECLARE_IUNKNOWN
390 STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void** ppv);
392 // ISubPicProviderEx
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);
404 // IPersist
405 STDMETHODIMP GetClassID(CLSID* pClassID);
407 // ISubStream
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();