Paint Machine. [Part 5]
[xy_vsfilter.git] / src / subtitles / RTS.h
blob66f969e8ddee3a9fb695ef929b107c89c00d67cb
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 class CClipper;
48 typedef ::boost::shared_ptr<CClipper> SharedPtrCClipper;
50 struct CompositeDrawItem;
51 typedef CAtlList<CompositeDrawItem> CompositeDrawItemList;
52 typedef CAtlList<CompositeDrawItemList> CompositeDrawItemListList;
54 class CWord;
55 typedef CWord* PCWord;
56 typedef ::boost::shared_ptr<CWord> SharedPtrCWord;
57 typedef ::boost::shared_ptr<CPolygon> SharedPtrCPolygon;
59 class CClipperPaintMachine;
60 typedef ::boost::shared_ptr<CClipperPaintMachine> SharedPtrCClipperPaintMachine;
62 class OverlayKey;
63 class CWord
65 bool NeedTransform();
66 void Transform(PathData* path_data, const CPoint& org);
68 void Transform_C(PathData* path_data, const CPoint &org );
69 void Transform_SSE2(PathData* path_data, const CPoint &org );
70 bool CreateOpaqueBox();
72 protected:
73 CStringW m_str;
75 virtual bool CreatePath(PathData* path_data) = 0;
77 bool DoPaint(const CPoint& p, const CPoint& trans_org, SharedPtrOverlay* overlay, const OverlayKey& key);
78 public:
79 bool m_fWhiteSpaceChar, m_fLineBreak;
81 FwSTSStyle m_style;
83 SharedPtrCPolygon m_pOpaqueBox;
85 int m_ktype, m_kstart, m_kend;
87 int m_width, m_ascent, m_descent;
89 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)
90 CWord(const CWord&);
91 virtual ~CWord();
93 virtual SharedPtrCWord Copy() = 0;
94 virtual bool Append(const SharedPtrCWord& w);
98 static void PaintFromOverlay(const CPoint& p, const CPoint& trans_org2, OverlayKey &subpixel_variance_key, SharedPtrOverlay& overlay);
99 void PaintFromNoneBluredOverlay(SharedPtrOverlay raterize_result, const OverlayKey& overlay_key, SharedPtrOverlay* overlay);
100 bool PaintFromScanLineData2(const CPoint& psub, const ScanLineData2& scan_line_data2, const OverlayKey& key, SharedPtrOverlay* overlay);
101 bool PaintFromPathData(const CPoint& psub, const CPoint& trans_org, const PathData& path_data, const OverlayKey& key, SharedPtrOverlay* overlay );
102 bool PaintFromRawData( const CPoint& psub, const CPoint& trans_org, const OverlayKey& key, SharedPtrOverlay* overlay );
104 //friend class CWordCache;
105 friend class CWordCacheKey;
106 friend class PathDataCacheKey;
107 friend class ClipperTraits;
108 friend class ClipperAlphaMaskCacheKey;
109 friend class CWordPaintMachine;
110 friend std::size_t hash_value(const CWord& key);
113 class CText : public CWord
115 public:
116 struct TextInfo
118 int m_width, m_ascent, m_descent;
120 typedef ::boost::shared_ptr<TextInfo> SharedPtrTextInfo;
121 protected:
122 virtual bool CreatePath(PathData* path_data);
124 static void GetTextInfo(TextInfo *output, const FwSTSStyle& style, const CStringW& str);
125 public:
126 CText(const FwSTSStyle& style, const CStringW& str, int ktype, int kstart, int kend);
127 CText(const CText& src);
129 virtual SharedPtrCWord Copy();
130 virtual bool Append(const SharedPtrCWord& w);
133 class CPolygon : public CWord
135 bool Get6BitFixedPoint(CStringW& str, LONG& ret);
136 bool GetPOINT(CStringW& str, POINT& ret);
137 bool ParseStr();
139 protected:
140 double m_scalex, m_scaley;
141 int m_baseline;
143 CAtlArray<BYTE> m_pathTypesOrg;
144 CAtlArray<CPoint> m_pathPointsOrg;
146 virtual bool CreatePath(PathData* path_data);
148 public:
149 CPolygon(const FwSTSStyle& style, const CStringW& str, int ktype, int kstart, int kend, double scalex, double scaley, int baseline);
150 CPolygon(CPolygon&); // can't use a const reference because we need to use CAtlArray::Copy which expects a non-const reference
151 virtual ~CPolygon();
153 virtual SharedPtrCWord Copy();
154 virtual bool Append(const SharedPtrCWord& w);
156 friend class ClipperTraits;
157 friend class ClipperAlphaMaskCacheKey;
160 enum eftype
162 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])}
163 EF_ORG, // {\org(x=param[0], y=param[1])}
164 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])
165 EF_BANNER, // Banner;delay=param[0][;lefttoright=param[1];fadeawaywidth=param[2]]
166 EF_SCROLL, // Scroll up/down=param[3];top=param[0];bottom=param[1];delay=param[2][;fadeawayheight=param[4]]
169 #define EF_NUMBEROFEFFECTS 5
171 class Effect
173 public:
174 Effect()
176 memset(param, 0, sizeof(param));
177 memset(t, 0, sizeof(t));
179 bool operator==(const Effect& rhs)const
181 return type==rhs.type
182 && !memcmp(param, rhs.param, sizeof(param))
183 && !memcmp(t, rhs.t, sizeof(t));
185 public:
186 enum eftype type;
187 int param[8];
188 int t[4];
191 class CClipper
193 private:
194 SharedPtrCPolygon m_polygon;
196 CSize m_size;
197 bool m_inverse;
199 Effect m_effect;
200 int m_effectType;
202 bool m_painted;
204 GrayImage2* PaintSimpleClipper();
205 GrayImage2* PaintBaseClipper();
206 GrayImage2* PaintBannerClipper();
207 GrayImage2* PaintScrollClipper();
209 GrayImage2* Paint();
210 public:
211 CClipper(CStringW str, CSize size, double scalex, double scaley, bool inverse);
212 void SetEffect(const Effect& effect, int effectType);
213 virtual ~CClipper();
215 static SharedPtrGrayImage2 GetAlphaMask(const SharedPtrCClipper& clipper);
217 friend class ClipperTraits;
218 friend class ClipperAlphaMaskCacheKey;
219 friend class CClipperPaintMachine;
222 class CLine: private CAtlList<SharedPtrCWord>
224 public:
225 int m_width, m_ascent, m_descent, m_borderX, m_borderY;
227 virtual ~CLine();
229 void Compact();
231 int GetWordCount();
232 void AddWord2Tail(SharedPtrCWord words);
233 bool IsEmpty();
235 CRect PaintAll(CompositeDrawItemList* output, SubPicDesc& spd, const CRect& clipRect,
236 const SharedPtrCClipperPaintMachine &clipper,
237 CPoint p, const CPoint& org, const int time, const int alpha);
240 class CSubtitle: private CAtlList<CLine*>
242 int GetFullWidth();
243 int GetFullLineWidth(POSITION pos);
244 int GetWrapWidth(POSITION pos, int maxwidth);
245 CLine* GetNextLine(POSITION& pos, int maxwidth);
247 public:
248 int m_scrAlignment;
249 int m_wrapStyle;
250 bool m_fAnimated;
251 bool m_fAnimated2; //If this Subtitle has animate effect
252 int m_relativeTo;
254 Effect* m_effects[EF_NUMBEROFEFFECTS];
256 CAtlList<SharedPtrCWord> m_words;
258 SharedPtrCClipper m_pClipper;
260 CRect m_rect, m_clip;
261 int m_topborder, m_bottomborder;
262 bool m_clipInverse;
264 double m_scalex, m_scaley;
266 public:
267 CSubtitle();
268 virtual ~CSubtitle();
269 virtual void Empty();
271 void CreateClippers(CSize size);
273 void MakeLines(CSize size, CRect marginRect);
275 POSITION GetHeadLinePosition();
276 CLine* GetNextLine(POSITION& pos);
279 struct CSubtitle2
281 CSubtitle2():s(NULL){}
283 CSubtitle2(CSubtitle* s_,const CRect& clipRect_, const CPoint& org_, const CPoint& org2_, const CPoint& p_,
284 int alpha_, int time_)
285 : s(s_), clipRect(clipRect_), org(org_), org2(org2_), p(p_), alpha(alpha_), time(time_)
290 CSubtitle *s;
291 const CRect clipRect;
292 const CPoint org;
293 const CPoint org2;
294 const CPoint p;
295 int alpha;
296 int time;
299 typedef CAtlList<CSubtitle2> CSubtitle2List;
301 class CScreenLayoutAllocator
303 typedef struct
305 CRect r;
306 int segment, entry, layer;
307 } SubRect;
309 CAtlList<SubRect> m_subrects;
311 public:
312 virtual void Empty();
314 void AdvanceToSegment(int segment, const CAtlArray<int>& sa);
315 CRect AllocRect(CSubtitle* s, int segment, int entry, int layer, int collisions);
318 [uuid("537DCACA-2812-4a4f-B2C6-1A34C17ADEB0")]
319 class CRenderedTextSubtitle : public CSubPicProviderImpl, public ISubStream, public CSimpleTextSubtitle
321 public:
322 enum AssCmdType
324 CMD_1c = 0,
325 CMD_2c,
326 CMD_3c,
327 CMD_4c,
328 CMD_1a,
329 CMD_2a,
330 CMD_3a,
331 CMD_4a,
332 CMD_alpha,
333 CMD_an,
334 CMD_a,
335 CMD_blur,
336 CMD_bord,
337 CMD_be,
338 CMD_b,
339 CMD_clip,
340 CMD_iclip,
341 CMD_c,
342 CMD_fade,
343 CMD_fad,
344 CMD_fax,
345 CMD_fay,
346 CMD_fe,
347 CMD_fn,
348 CMD_frx,
349 CMD_fry,
350 CMD_frz,
351 CMD_fr,
352 CMD_fscx,
353 CMD_fscy,
354 CMD_fsc,
355 CMD_fsp,
356 CMD_fs,
357 CMD_i,
358 CMD_kt,
359 CMD_kf,
360 CMD_K,
361 CMD_ko,
362 CMD_k,
363 CMD_move,
364 CMD_org,
365 CMD_pbo,
366 CMD_pos,
367 CMD_p,
368 CMD_q,
369 CMD_r,
370 CMD_shad,
371 CMD_s,
372 CMD_t,
373 CMD_u,
374 CMD_xbord,
375 CMD_xshad,
376 CMD_ybord,
377 CMD_yshad,
378 CMD_COUNT
380 static const int MIN_CMD_LENGTH = 1;//c etc
381 static const int MAX_CMD_LENGTH = 5;//alpha, iclip, xbord, xshad, ybord, yshad
382 static CAtlMap<CStringW, AssCmdType, CStringElementTraits<CStringW>> m_cmdMap;
384 struct AssTag;
385 typedef CAtlList<AssTag> AssTagList;
386 typedef ::boost::shared_ptr<const AssTagList> SharedPtrConstAssTagList;
387 struct AssTag
389 AssCmdType cmdType;
390 CAtlArray<CStringW> strParams;
391 AssTagList embeded;
393 private:
394 CAtlMap<int, CSubtitle*> m_subtitleCache;
396 CScreenLayoutAllocator m_sla;
398 CSize m_size;
399 CRect m_vidrect;
401 // temp variables, used when parsing the script
402 int m_time, m_delay;
403 int m_animStart, m_animEnd;
404 double m_animAccel;
405 int m_ktype, m_kstart, m_kend;
406 int m_nPolygon;
407 int m_polygonBaselineOffset;
408 double m_fps;
409 int m_period;//1000/m_fps
411 static void InitCmdMap();
413 void ParseEffect(CSubtitle* sub, const CString& str);
414 void ParseString(CSubtitle* sub, CStringW str, const FwSTSStyle& style);
415 void ParsePolygon(CSubtitle* sub, const CStringW& str, const FwSTSStyle& style);
417 static bool ParseSSATag(AssTagList *assTags, const CStringW& str);
418 bool ParseSSATag(CSubtitle* sub, const AssTagList& assTags, STSStyle& style, const STSStyle& org, bool fAnimate = false);
419 bool ParseSSATag(CSubtitle* sub, const CStringW& str, STSStyle& style, const STSStyle& org, bool fAnimate = false);
421 bool ParseHtmlTag(CSubtitle* sub, CStringW str, STSStyle& style, STSStyle& org);
423 double CalcAnimation(double dst, double src, bool fAnimate);
425 CSubtitle* GetSubtitle(int entry);
427 protected:
428 virtual void OnChanged();
430 public:
431 CRenderedTextSubtitle(CCritSec* pLock);
432 virtual ~CRenderedTextSubtitle();
434 virtual void Copy(CSimpleTextSubtitle& sts);
435 virtual void Empty();
437 public:
438 bool Init(CSize size, CRect vidrect); // will call Deinit()
439 void Deinit();
441 DECLARE_IUNKNOWN
442 STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void** ppv);
444 // ISubPicProviderEx
445 STDMETHODIMP_(POSITION) GetStartPosition(REFERENCE_TIME rt, double fps);
446 STDMETHODIMP_(POSITION) GetNext(POSITION pos);
447 STDMETHODIMP_(REFERENCE_TIME) GetStart(POSITION pos, double fps);
448 STDMETHODIMP_(REFERENCE_TIME) GetStop(POSITION pos, double fps);
449 STDMETHODIMP_(VOID) GetStartStop(POSITION pos, double fps, /*out*/REFERENCE_TIME &start, /*out*/REFERENCE_TIME &stop);
450 STDMETHODIMP_(bool) IsAnimated(POSITION pos);
451 STDMETHODIMP Render(SubPicDesc& spd, REFERENCE_TIME rt, double fps, RECT& bbox);
452 STDMETHODIMP RenderEx(SubPicDesc& spd, REFERENCE_TIME rt, double fps, CAtlList<CRect>& rectList);
453 STDMETHODIMP ParseScript(SubPicDesc& spd, REFERENCE_TIME rt, double fps, CSubtitle2List *outputSub2List );
454 static void DoRender( SubPicDesc& spd, const CSubtitle2List& sub2List,
455 CAtlList<CRect> *rectList, CompositeDrawItemListList *compDrawItemListList /*output*/);
456 static void RenderOneSubtitle(SubPicDesc& spd, const CSubtitle2& sub2,
457 CAtlList<CRect>* rectList, CompositeDrawItemList* compDrawItemList /*output*/);
458 STDMETHODIMP_(bool) IsColorTypeSupported(int type);
460 // IPersist
461 STDMETHODIMP GetClassID(CLSID* pClassID);
463 // ISubStream
464 STDMETHODIMP_(int) GetStreamCount();
465 STDMETHODIMP GetStreamInfo(int i, WCHAR** ppName, LCID* pLCID);
466 STDMETHODIMP_(int) GetStream();
467 STDMETHODIMP SetStream(int iStream);
468 STDMETHODIMP Reload();