Scale. [Part 10] (Add rounding for \clip to fixing unwanted artifacts)
[xy_vsfilter.git] / src / subtitles / RTS.h
blobd095b5d5460320098339bae094bda6f5878d70dc
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 "../SubPic/ISimpleSubPic.h"
28 #include <atlcoll.h>
29 #include <boost/flyweight/key_value.hpp>
30 #include <boost/smart_ptr.hpp>
31 #include "mru_cache.h"
33 #define RTS_POS_SEGMENT_INDEX_BITS 16
34 #define RTS_POS_SUB_INDEX_MASK ((1<<RTS_POS_SEGMENT_INDEX_BITS)-1)
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 class CClipper;
49 typedef ::boost::shared_ptr<CClipper> SharedPtrCClipper;
51 struct CompositeDrawItem;
52 typedef CAtlList<CompositeDrawItem> CompositeDrawItemList;
53 typedef CAtlList<CompositeDrawItemList> CompositeDrawItemListList;
55 class CWord;
56 typedef CWord* PCWord;
57 typedef ::boost::shared_ptr<CWord> SharedPtrCWord;
58 typedef ::boost::shared_ptr<CPolygon> SharedPtrCPolygon;
60 class CClipperPaintMachine;
61 typedef ::boost::shared_ptr<CClipperPaintMachine> SharedPtrCClipperPaintMachine;
63 class OverlayKey;
64 interface IXySubRenderFrame;
66 class CWord
68 bool NeedTransform();
69 void Transform(PathData* path_data, const CPointCoor2& org);
71 void Transform_C(PathData* path_data, const CPointCoor2 &org );
72 void Transform_SSE2(PathData* path_data, const CPointCoor2 &org );
73 bool CreateOpaqueBox();
75 protected:
76 virtual bool CreatePath(PathData* path_data) = 0;
78 bool DoPaint(const CPointCoor2& p, const CPointCoor2& trans_org, SharedPtrOverlay* overlay, const OverlayKey& key);
79 public:
80 // str[0] = 0 -> m_fLineBreak = true (in this case we only need and use the height of m_font from the whole class)
81 CWord(const FwSTSStyle& style, const CStringW& str, int ktype, int kstart, int kend
82 , double target_scale_x=1.0, double target_scale_y=1.0
83 , bool round_to_whole_pixel_after_scale_to_target = false);
84 CWord(const CWord&);
85 virtual ~CWord();
87 virtual SharedPtrCWord Copy() = 0;
88 virtual bool Append(const SharedPtrCWord& w);
90 bool operator==(const CWord& rhs)const;
92 static void PaintFromOverlay(const CPointCoor2& p, const CPointCoor2& trans_org2, OverlayKey &subpixel_variance_key, SharedPtrOverlay& overlay);
93 void PaintFromNoneBluredOverlay(SharedPtrOverlay raterize_result, const OverlayKey& overlay_key, SharedPtrOverlay* overlay);
94 bool PaintFromScanLineData2(const CPointCoor2& psub, const ScanLineData2& scan_line_data2, const OverlayKey& key, SharedPtrOverlay* overlay);
95 bool PaintFromPathData(const CPointCoor2& psub, const CPointCoor2& trans_org, const PathData& path_data, const OverlayKey& key, SharedPtrOverlay* overlay );
96 bool PaintFromRawData( const CPointCoor2& psub, const CPointCoor2& trans_org, const OverlayKey& key, SharedPtrOverlay* overlay );
98 protected:
99 XyFwStringW m_str;
100 public:
101 bool m_fWhiteSpaceChar, m_fLineBreak;
103 FwSTSStyle m_style;
105 SharedPtrCPolygon m_pOpaqueBox;
107 int m_ktype, m_kstart, m_kend;
109 int m_width, m_ascent, m_descent;
111 double m_target_scale_x, m_target_scale_y;
112 bool m_round_to_whole_pixel_after_scale_to_target;//it is necessary to avoid some artifacts
114 //friend class CWordCache;
115 friend class PathDataCacheKey;
116 friend class ClipperTraits;
117 friend class ClipperAlphaMaskCacheKey;
118 friend class CWordPaintMachine;
119 friend class CWordPaintResultKey;
120 friend class CClipper;
123 class CText : public CWord
125 public:
126 struct TextInfo
128 int m_width, m_ascent, m_descent;
130 typedef ::boost::shared_ptr<TextInfo> SharedPtrTextInfo;
131 protected:
132 virtual bool CreatePath(PathData* path_data);
134 static void GetTextInfo(TextInfo *output, const FwSTSStyle& style, const CStringW& str);
135 public:
136 CText(const FwSTSStyle& style, const CStringW& str, int ktype, int kstart, int kend
137 , double target_scale_x=1.0, double target_scale_y=1.0);
138 CText(const CText& src);
140 virtual SharedPtrCWord Copy();
141 virtual bool Append(const SharedPtrCWord& w);
144 class CPolygon : public CWord
146 bool Get6BitFixedPoint(CStringW& str, LONG& ret);
147 bool GetPOINT(CStringW& str, POINT& ret);
148 bool ParseStr();
150 protected:
151 double m_scalex, m_scaley;
152 int m_baseline;
154 CAtlArray<BYTE> m_pathTypesOrg;
155 CAtlArray<CPoint> m_pathPointsOrg;
157 virtual bool CreatePath(PathData* path_data);
159 public:
160 CPolygon(const FwSTSStyle& style, const CStringW& str, int ktype, int kstart, int kend
161 , double scalex, double scaley, int baseline
162 , double target_scale_x=1.0, double target_scale_y=1.0
163 , bool round_to_whole_pixel_after_scale_to_target = false);
164 // can't use a const reference because we need to use CAtlArray::Copy which expects a non-const reference
165 CPolygon(CPolygon&);
166 virtual ~CPolygon();
168 virtual SharedPtrCWord Copy();
169 virtual bool Append(const SharedPtrCWord& w);
171 friend class ClipperTraits;
172 friend class ClipperAlphaMaskCacheKey;
175 enum eftype
177 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])}
178 EF_ORG, // {\org(x=param[0], y=param[1])}
179 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])
180 EF_BANNER, // Banner;delay=param[0][;lefttoright=param[1];fadeawaywidth=param[2]]
181 EF_SCROLL, // Scroll up/down=param[3];top=param[0];bottom=param[1];delay=param[2][;fadeawayheight=param[4]]
184 #define EF_NUMBEROFEFFECTS 5
186 class Effect
188 public:
189 Effect()
191 memset(param, 0, sizeof(param));
192 memset(t, 0, sizeof(t));
194 bool operator==(const Effect& rhs)const
196 return type==rhs.type
197 && !memcmp(param, rhs.param, sizeof(param))
198 && !memcmp(t, rhs.t, sizeof(t));
200 public:
201 enum eftype type;
202 int param[8];
203 int t[4];
206 class CClipper
208 private:
209 SharedPtrCPolygon m_polygon;
211 CSizeCoor2 m_size;
212 bool m_inverse;
214 Effect m_effect;
215 int m_effectType;
217 bool m_painted;
219 GrayImage2* PaintSimpleClipper();
220 GrayImage2* PaintBaseClipper();
221 GrayImage2* PaintBannerClipper();
222 GrayImage2* PaintScrollClipper();
224 GrayImage2* Paint();
225 public:
226 CClipper(CStringW str, CSizeCoor2 size, double scalex, double scaley, bool inverse
227 , double target_scale_x=1.0, double target_scale_y=1.0);
228 void SetEffect(const Effect& effect, int effectType);
229 virtual ~CClipper();
231 static SharedPtrGrayImage2 GetAlphaMask(const SharedPtrCClipper& clipper);
233 friend class ClipperTraits;
234 friend class ClipperAlphaMaskCacheKey;
235 friend class CClipperPaintMachine;
238 class CLine: private CAtlList<SharedPtrCWord>
240 public:
241 int m_width, m_ascent, m_descent, m_borderX, m_borderY;
243 virtual ~CLine();
245 void Compact();
247 int GetWordCount();
248 void AddWord2Tail(SharedPtrCWord words);
249 bool IsEmpty();
251 CRectCoor2 PaintAll(CompositeDrawItemList* output, const CRectCoor2& clipRect,
252 const SharedPtrCClipperPaintMachine &clipper,
253 CPoint p, const CPoint& org, const int time, const int alpha);
256 class CSubtitle: private CAtlList<CLine*>
258 int GetFullWidth();
259 int GetFullLineWidth(POSITION pos);
260 int GetWrapWidth(POSITION pos, int maxwidth);
261 CLine* GetNextLine(POSITION& pos, int maxwidth);
263 public:
264 int m_scrAlignment;
265 int m_wrapStyle;
266 bool m_fAnimated;
267 bool m_fAnimated2; //If this Subtitle has animate effect
268 int m_relativeTo;
270 Effect* m_effects[EF_NUMBEROFEFFECTS];
272 CAtlList<SharedPtrCWord> m_words;
274 SharedPtrCClipper m_pClipper;
276 CRect m_rect, m_clip;
277 int m_topborder, m_bottomborder;
278 bool m_clipInverse;
280 double m_scalex, m_scaley;
281 double m_target_scale_x, m_target_scale_y;
282 public:
283 CSubtitle();
284 virtual ~CSubtitle();
285 virtual void Empty();
287 void CreateClippers(CSize size1, const CSizeCoor2& size_scale_to);
289 void MakeLines(CSize size, CRect marginRect);
291 POSITION GetHeadLinePosition();
292 CLine* GetNextLine(POSITION& pos);
295 struct CSubtitle2
297 CSubtitle2():s(NULL){}
299 CSubtitle2(CSubtitle* s_,const CRectCoor2& clipRect_, const CPoint& org_, const CPoint& org2_
300 , const CPoint& p_, int alpha_, int time_)
301 : s(s_), clipRect(clipRect_), org(org_), org2(org2_), p(p_), alpha(alpha_), time(time_)
306 CSubtitle *s;
307 const CRectCoor2 clipRect;
308 const CPoint org;
309 const CPoint org2;
310 const CPoint p;
311 int alpha;
312 int time;
315 typedef CAtlList<CSubtitle2> CSubtitle2List;
317 class CScreenLayoutAllocator
319 typedef struct
321 CRect r;
322 int segment, entry, layer;
323 } SubRect;
325 CAtlList<SubRect> m_subrects;
327 public:
328 virtual void Empty();
330 void AdvanceToSegment(int segment, const CAtlArray<int>& sa);
331 CRect AllocRect(CSubtitle* s, int segment, int entry, int layer, int collisions);
334 [uuid("537DCACA-2812-4a4f-B2C6-1A34C17ADEB0")]
335 class CRenderedTextSubtitle : public CSubPicProviderImpl, public ISubStream, public ISubPicProviderEx2, public CSimpleTextSubtitle
337 public:
338 enum AssCmdType
340 CMD_1c = 0,
341 CMD_2c,
342 CMD_3c,
343 CMD_4c,
344 CMD_1a,
345 CMD_2a,
346 CMD_3a,
347 CMD_4a,
348 CMD_alpha,
349 CMD_an,
350 CMD_a,
351 CMD_blur,
352 CMD_bord,
353 CMD_be,
354 CMD_b,
355 CMD_clip,
356 CMD_iclip,
357 CMD_c,
358 CMD_fade,
359 CMD_fad,
360 CMD_fax,
361 CMD_fay,
362 CMD_fe,
363 CMD_fn,
364 CMD_frx,
365 CMD_fry,
366 CMD_frz,
367 CMD_fr,
368 CMD_fscx,
369 CMD_fscy,
370 CMD_fsc,
371 CMD_fsp,
372 CMD_fs,
373 CMD_i,
374 CMD_kt,
375 CMD_kf,
376 CMD_K,
377 CMD_ko,
378 CMD_k,
379 CMD_move,
380 CMD_org,
381 CMD_pbo,
382 CMD_pos,
383 CMD_p,
384 CMD_q,
385 CMD_r,
386 CMD_shad,
387 CMD_s,
388 CMD_t,
389 CMD_u,
390 CMD_xbord,
391 CMD_xshad,
392 CMD_ybord,
393 CMD_yshad,
394 CMD_COUNT
396 static const int MIN_CMD_LENGTH = 1;//c etc
397 static const int MAX_CMD_LENGTH = 5;//alpha, iclip, xbord, xshad, ybord, yshad
398 static CAtlMap<CStringW, AssCmdType, CStringElementTraits<CStringW>> m_cmdMap;
400 struct AssTag;
401 typedef CAtlList<AssTag> AssTagList;
402 typedef ::boost::shared_ptr<const AssTagList> SharedPtrConstAssTagList;
403 struct AssTag
405 AssCmdType cmdType;
406 CAtlArray<CStringW> strParams;
407 AssTagList embeded;
409 private:
410 CAtlMap<int, CSubtitle*> m_subtitleCache;
412 CScreenLayoutAllocator m_sla;
414 CSizeCoor2 m_size_scale_to;
416 CSize m_size;
417 CRect m_vidrect;
419 // temp variables, used when parsing the script
420 int m_time, m_delay;
421 int m_animStart, m_animEnd;
422 double m_animAccel;
423 int m_ktype, m_kstart, m_kend;
424 int m_nPolygon;
425 int m_polygonBaselineOffset;
426 double m_fps;
427 int m_period;//1000/m_fps
428 double m_target_scale_x, m_target_scale_y;
430 static void InitCmdMap();
432 void ParseEffect(CSubtitle* sub, const CString& str);
433 void ParseString(CSubtitle* sub, CStringW str, const FwSTSStyle& style);
434 void ParsePolygon(CSubtitle* sub, const CStringW& str, const FwSTSStyle& style);
436 static bool ParseSSATag(AssTagList *assTags, const CStringW& str);
437 bool ParseSSATag(CSubtitle* sub, const AssTagList& assTags, STSStyle& style, const STSStyle& org, bool fAnimate = false);
438 bool ParseSSATag(CSubtitle* sub, const CStringW& str, STSStyle& style, const STSStyle& org, bool fAnimate = false);
440 bool ParseHtmlTag(CSubtitle* sub, CStringW str, STSStyle& style, STSStyle& org);
442 double CalcAnimation(double dst, double src, bool fAnimate);
444 CSubtitle* GetSubtitle(int entry);
446 protected:
447 virtual void OnChanged();
449 public:
450 CRenderedTextSubtitle(CCritSec* pLock);
451 virtual ~CRenderedTextSubtitle();
453 virtual void Copy(CSimpleTextSubtitle& sts);
454 virtual void Empty();
456 public:
457 bool Init(const SIZECoor2& size_scale_to, const SIZE& size1, const CRect& video_rect); // will call Deinit()
458 void Deinit();
460 DECLARE_IUNKNOWN
461 STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void** ppv);
463 // ISubPicProviderEx2
464 STDMETHODIMP Lock();
465 STDMETHODIMP Unlock();
466 STDMETHODIMP RenderEx(IXySubRenderFrame**subRenderFrame, int spd_type,
467 const SIZECoor2& size_scale_to,
468 const SIZE& size1, const CRect& video_rect,
469 REFERENCE_TIME rt, double fps);
471 // ISubPicProviderEx && ISubPicProviderEx2
472 STDMETHODIMP_(POSITION) GetStartPosition(REFERENCE_TIME rt, double fps);
473 STDMETHODIMP_(POSITION) GetNext(POSITION pos);
474 STDMETHODIMP_(REFERENCE_TIME) GetStart(POSITION pos, double fps);
475 STDMETHODIMP_(REFERENCE_TIME) GetStop(POSITION pos, double fps);
476 STDMETHODIMP_(VOID) GetStartStop(POSITION pos, double fps, /*out*/REFERENCE_TIME &start, /*out*/REFERENCE_TIME &stop);
477 STDMETHODIMP_(bool) IsAnimated(POSITION pos);
478 STDMETHODIMP Render(SubPicDesc& spd, REFERENCE_TIME rt, double fps, RECTCoor2& bbox);
479 STDMETHODIMP RenderEx(SubPicDesc& spd, REFERENCE_TIME rt, double fps, CAtlList<CRectCoor2>& rectList);
480 HRESULT ParseScript(REFERENCE_TIME rt, double fps, CSubtitle2List *outputSub2List );
481 static void DoRender( const SIZECoor2& output_size, const CSubtitle2List& sub2List,
482 CompositeDrawItemListList *compDrawItemListList /*output*/);
483 static void RenderOneSubtitle(const SIZECoor2& output_size, const CSubtitle2& sub2,
484 CompositeDrawItemList* compDrawItemList /*output*/);
485 STDMETHODIMP_(bool) IsColorTypeSupported(int type);
487 // IPersist
488 STDMETHODIMP GetClassID(CLSID* pClassID);
490 // ISubStream
491 STDMETHODIMP_(int) GetStreamCount();
492 STDMETHODIMP GetStreamInfo(int i, WCHAR** ppName, LCID* pLCID);
493 STDMETHODIMP_(int) GetStream();
494 STDMETHODIMP SetStream(int iStream);
495 STDMETHODIMP Reload();