Split scan convert operation for a new cache. [Part 2]
[xy_vsfilter.git] / src / subtitles / RTS.h
blob1d7d375c8f01aa879e6fbcd8cbd9ff5da0b1d5ae
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)
35 class CMyFont : public CFont
37 public:
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;
45 class CPolygon;
47 struct OverlayList
49 SharedPtrOverlay overlay;
50 OverlayList* next;
52 OverlayList()
54 next = NULL;
56 ~OverlayList()
58 delete next;
62 struct CompositeDrawItem
64 SharedPtrDrawItem shadow;
65 SharedPtrDrawItem outline;
66 SharedPtrDrawItem body;
69 typedef CAtlList<CompositeDrawItem> CompositeDrawItemList;
70 typedef CAtlList<CompositeDrawItemList> CompositeDrawItemListList;
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(PathData* path_data, const CPoint& org);
83 void Transform_C(PathData* path_data, const CPoint &org );
84 void Transform_SSE2(PathData* path_data, const CPoint &org );
85 bool CreateOpaqueBox();
87 protected:
88 CStringW m_str;
90 virtual bool CreatePath(PathData* path_data) = 0;
92 bool DoPaint(const CPoint& p, const CPoint& trans_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 PaintAll(SharedPtrCWord word,
114 const CPoint& shadowPos, const CPoint& outlinePos, const CPoint& bodyPos, const CPoint& org,
115 OverlayList* shadow, OverlayList* outline, OverlayList* body);
116 static void Paint(SharedPtrCWord word, const CPoint& psub, const CPoint& trans_org, OverlayList* overlay_list);
117 static void PaintFromOverlay(const CPoint& p, const CPoint& trans_org2, OverlayKey &subpixel_variance_key, SharedPtrOverlay& overlay);
118 void PaintFromNoneBluredOverlay(SharedPtrOverlay raterize_result, const OverlayKey& overlay_key, SharedPtrOverlay* overlay);
119 bool PaintFromScanLineData(const CPoint& psub, const ScanLineData2& scan_line_data2, const OverlayKey& key, SharedPtrOverlay* overlay);
120 bool PaintFromPathData(const CPoint& psub, const CPoint& trans_org, const PathData& path_data, const OverlayKey& key, SharedPtrOverlay* overlay );
121 bool PaintFromRawData( const CPoint& psub, const CPoint& trans_org, const OverlayKey& key, SharedPtrOverlay* overlay );
123 //friend class CWordCache;
124 friend class CWordCacheKey;
125 friend class PathDataCacheKey;
126 friend std::size_t hash_value(const CWord& key);
129 class CText : public CWord
131 public:
132 struct TextInfo
134 int m_width, m_ascent, m_descent;
136 typedef ::boost::shared_ptr<TextInfo> SharedPtrTextInfo;
137 protected:
138 virtual bool CreatePath(PathData* path_data);
140 static void GetTextInfo(TextInfo *output, const FwSTSStyle& style, const CStringW& str);
141 public:
142 CText(const FwSTSStyle& style, const CStringW& str, int ktype, int kstart, int kend);
143 CText(const CText& src);
145 virtual SharedPtrCWord Copy();
146 virtual bool Append(const SharedPtrCWord& w);
149 class CPolygon : public CWord
151 bool Get6BitFixedPoint(CStringW& str, LONG& ret);
152 bool GetPOINT(CStringW& str, POINT& ret);
153 bool ParseStr();
155 protected:
156 double m_scalex, m_scaley;
157 int m_baseline;
159 CAtlArray<BYTE> m_pathTypesOrg;
160 CAtlArray<CPoint> m_pathPointsOrg;
162 virtual bool CreatePath(PathData* path_data);
164 public:
165 CPolygon(const FwSTSStyle& style, const CStringW& str, int ktype, int kstart, int kend, double scalex, double scaley, int baseline);
166 CPolygon(CPolygon&); // can't use a const reference because we need to use CAtlArray::Copy which expects a non-const reference
167 virtual ~CPolygon();
169 virtual SharedPtrCWord Copy();
170 virtual bool Append(const SharedPtrCWord& w);
173 enum eftype
175 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])}
176 EF_ORG, // {\org(x=param[0], y=param[1])}
177 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])
178 EF_BANNER, // Banner;delay=param[0][;lefttoright=param[1];fadeawaywidth=param[2]]
179 EF_SCROLL, // Scroll up/down=param[3];top=param[0];bottom=param[1];delay=param[2][;fadeawayheight=param[4]]
182 #define EF_NUMBEROFEFFECTS 5
184 class Effect
186 public:
187 enum eftype type;
188 int param[8];
189 int t[4];
192 class CClipper
194 private:
195 SharedPtrCPolygon m_polygon;
197 CSize m_size;
198 bool m_inverse;
200 Effect m_effect;
201 int m_effectType;
203 bool m_painted;
205 SharedArrayByte m_pAlphaMask;
207 void PaintBaseClipper();
208 void PaintBannerClipper();
209 void PaintScrollClipper();
211 void Paint();
212 public:
213 CClipper(CStringW str, CSize size, double scalex, double scaley, bool inverse);
214 void SetEffect(const Effect& effect, int effectType);
215 virtual ~CClipper();
217 const SharedArrayByte& GetAlphaMask();
220 class CLine: private CAtlList<SharedPtrCWord>
222 public:
223 int m_width, m_ascent, m_descent, m_borderX, m_borderY;
225 virtual ~CLine();
227 void Compact();
229 int GetWordCount();
230 void AddWord2Tail(SharedPtrCWord words);
231 bool IsEmpty();
233 CRect PaintAll(CompositeDrawItemList* output, SubPicDesc& spd, const CRect& clipRect, SharedArrayByte pAlphaMask, CPoint p, const CPoint& org, const int time, const int alpha);
236 class CSubtitle: private CAtlList<CLine*>
238 int GetFullWidth();
239 int GetFullLineWidth(POSITION pos);
240 int GetWrapWidth(POSITION pos, int maxwidth);
241 CLine* GetNextLine(POSITION& pos, int maxwidth);
243 public:
244 int m_scrAlignment;
245 int m_wrapStyle;
246 bool m_fAnimated;
247 bool m_fAnimated2; //If this Subtitle has animate effect
248 int m_relativeTo;
250 Effect* m_effects[EF_NUMBEROFEFFECTS];
252 CAtlList<SharedPtrCWord> m_words;
254 CClipper* m_pClipper;
256 CRect m_rect, m_clip;
257 int m_topborder, m_bottomborder;
258 bool m_clipInverse;
260 double m_scalex, m_scaley;
262 public:
263 CSubtitle();
264 virtual ~CSubtitle();
265 virtual void Empty();
267 void CreateClippers(CSize size);
269 void MakeLines(CSize size, CRect marginRect);
271 POSITION GetHeadLinePosition();
272 CLine* GetNextLine(POSITION& pos);
275 struct CSubtitle2
277 CSubtitle2():s(NULL){}
279 CSubtitle2(CSubtitle* s_,const CRect& clipRect_, const CPoint& org_, const CPoint& org2_, const CPoint& p_,
280 int alpha_, int time_)
281 : s(s_), clipRect(clipRect_), org(org_), org2(org2_), p(p_), alpha(alpha_), time(time_)
286 CSubtitle *s;
287 const CRect clipRect;
288 const CPoint org;
289 const CPoint org2;
290 const CPoint p;
291 int alpha;
292 int time;
295 typedef CAtlList<CSubtitle2> CSubtitle2List;
297 class CScreenLayoutAllocator
299 typedef struct
301 CRect r;
302 int segment, entry, layer;
303 } SubRect;
305 CAtlList<SubRect> m_subrects;
307 public:
308 virtual void Empty();
310 void AdvanceToSegment(int segment, const CAtlArray<int>& sa);
311 CRect AllocRect(CSubtitle* s, int segment, int entry, int layer, int collisions);
314 [uuid("537DCACA-2812-4a4f-B2C6-1A34C17ADEB0")]
315 class CRenderedTextSubtitle : public CSubPicProviderImpl, public ISubStream, public CSimpleTextSubtitle
317 public:
318 enum AssCmdType
320 CMD_1c = 0,
321 CMD_2c,
322 CMD_3c,
323 CMD_4c,
324 CMD_1a,
325 CMD_2a,
326 CMD_3a,
327 CMD_4a,
328 CMD_alpha,
329 CMD_an,
330 CMD_a,
331 CMD_blur,
332 CMD_bord,
333 CMD_be,
334 CMD_b,
335 CMD_clip,
336 CMD_iclip,
337 CMD_c,
338 CMD_fade,
339 CMD_fad,
340 CMD_fax,
341 CMD_fay,
342 CMD_fe,
343 CMD_fn,
344 CMD_frx,
345 CMD_fry,
346 CMD_frz,
347 CMD_fr,
348 CMD_fscx,
349 CMD_fscy,
350 CMD_fsc,
351 CMD_fsp,
352 CMD_fs,
353 CMD_i,
354 CMD_kt,
355 CMD_kf,
356 CMD_K,
357 CMD_ko,
358 CMD_k,
359 CMD_move,
360 CMD_org,
361 CMD_pbo,
362 CMD_pos,
363 CMD_p,
364 CMD_q,
365 CMD_r,
366 CMD_shad,
367 CMD_s,
368 CMD_t,
369 CMD_u,
370 CMD_xbord,
371 CMD_xshad,
372 CMD_ybord,
373 CMD_yshad,
374 CMD_COUNT
376 static const int MIN_CMD_LENGTH = 1;//c etc
377 static const int MAX_CMD_LENGTH = 5;//alpha, iclip, xbord, xshad, ybord, yshad
378 static CAtlMap<CStringW, AssCmdType, CStringElementTraits<CStringW>> m_cmdMap;
380 struct AssTag;
381 typedef CAtlList<AssTag> AssTagList;
382 typedef ::boost::shared_ptr<const AssTagList> SharedPtrConstAssTagList;
383 struct AssTag
385 CStringW cmd;
386 AssCmdType cmdType;
387 CAtlArray<CStringW> strParams;
388 AssTagList embeded;
390 private:
391 CAtlMap<int, CSubtitle*> m_subtitleCache;
393 CScreenLayoutAllocator m_sla;
395 CSize m_size;
396 CRect m_vidrect;
398 // temp variables, used when parsing the script
399 int m_time, m_delay;
400 int m_animStart, m_animEnd;
401 double m_animAccel;
402 int m_ktype, m_kstart, m_kend;
403 int m_nPolygon;
404 int m_polygonBaselineOffset;
405 double m_fps;
406 int m_period;//1000/m_fps
408 static void InitCmdMap();
410 void ParseEffect(CSubtitle* sub, const CString& str);
411 void ParseString(CSubtitle* sub, CStringW str, const FwSTSStyle& style);
412 void ParsePolygon(CSubtitle* sub, const CStringW& str, const FwSTSStyle& style);
413 static bool ParseSSATag(AssTagList *assTags, const CStringW& str);
414 bool ParseSSATag(CSubtitle* sub, const AssTagList& assTags, STSStyle& style, const STSStyle& org, bool fAnimate = false);
415 bool ParseSSATag(CSubtitle* sub, const CStringW& str, STSStyle& style, const STSStyle& org, bool fAnimate = false);
417 bool ParseHtmlTag(CSubtitle* sub, CStringW str, STSStyle& style, STSStyle& org);
419 double CalcAnimation(double dst, double src, bool fAnimate);
421 void Draw(SubPicDesc& spd, CompositeDrawItemListList& drawItemListList);
423 CSubtitle* GetSubtitle(int entry);
425 protected:
426 virtual void OnChanged();
428 public:
429 CRenderedTextSubtitle(CCritSec* pLock);
430 virtual ~CRenderedTextSubtitle();
432 virtual void Copy(CSimpleTextSubtitle& sts);
433 virtual void Empty();
435 public:
436 bool Init(CSize size, CRect vidrect); // will call Deinit()
437 void Deinit();
439 DECLARE_IUNKNOWN
440 STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void** ppv);
442 // ISubPicProviderEx
443 STDMETHODIMP_(POSITION) GetStartPosition(REFERENCE_TIME rt, double fps);
444 STDMETHODIMP_(POSITION) GetNext(POSITION pos);
445 STDMETHODIMP_(REFERENCE_TIME) GetStart(POSITION pos, double fps);
446 STDMETHODIMP_(REFERENCE_TIME) GetStop(POSITION pos, double fps);
447 STDMETHODIMP_(VOID) GetStartStop(POSITION pos, double fps, /*out*/REFERENCE_TIME &start, /*out*/REFERENCE_TIME &stop);
448 STDMETHODIMP_(bool) IsAnimated(POSITION pos);
449 STDMETHODIMP Render(SubPicDesc& spd, REFERENCE_TIME rt, double fps, RECT& bbox);
450 STDMETHODIMP RenderEx(SubPicDesc& spd, REFERENCE_TIME rt, double fps, CAtlList<CRect>& rectList);
451 STDMETHODIMP ParseScript(SubPicDesc& spd, REFERENCE_TIME rt, double fps, CSubtitle2List *outputSub2List );
452 static void DoRender( SubPicDesc& spd, const CSubtitle2List& sub2List,
453 CAtlList<CRect> *rectList, CompositeDrawItemListList *drawItemListList /*output*/);
454 static void RenderOneSubtitle(SubPicDesc& spd, const CSubtitle2& sub2,
455 CAtlList<CRect>* rectList, CompositeDrawItemList* drawItemList /*output*/);
456 STDMETHODIMP_(bool) IsColorTypeSupported(int type);
458 // IPersist
459 STDMETHODIMP GetClassID(CLSID* pClassID);
461 // ISubStream
462 STDMETHODIMP_(int) GetStreamCount();
463 STDMETHODIMP GetStreamInfo(int i, WCHAR** ppName, LCID* pLCID);
464 STDMETHODIMP_(int) GetStream();
465 STDMETHODIMP SetStream(int iStream);
466 STDMETHODIMP Reload();