Merge with MPC-HC 6d1472b2f18266d92e5bc068667de348c0cd3b3b.
[xy_vsfilter.git] / src / subtitles / RTS.h
blobc638d8dd8e70a2e49d632b65359df6601941802a
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;
173 friend class PathDataCacheKey;
176 enum eftype
178 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])}
179 EF_ORG, // {\org(x=param[0], y=param[1])}
180 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])
181 EF_BANNER, // Banner;delay=param[0][;lefttoright=param[1];fadeawaywidth=param[2]]
182 EF_SCROLL, // Scroll up/down=param[3];top=param[0];bottom=param[1];delay=param[2][;fadeawayheight=param[4]]
185 #define EF_NUMBEROFEFFECTS 5
187 class Effect
189 public:
190 Effect()
192 memset(param, 0, sizeof(param));
193 memset(t, 0, sizeof(t));
195 bool operator==(const Effect& rhs)const
197 return type==rhs.type
198 && !memcmp(param, rhs.param, sizeof(param))
199 && !memcmp(t, rhs.t, sizeof(t));
201 public:
202 enum eftype type;
203 int param[8];
204 int t[4];
207 class CClipper
209 private:
210 SharedPtrCPolygon m_polygon;
212 CSizeCoor2 m_size;
213 bool m_inverse;
215 Effect m_effect;
216 int m_effectType;
218 bool m_painted;
220 GrayImage2* PaintSimpleClipper();
221 GrayImage2* PaintBaseClipper();
222 GrayImage2* PaintBannerClipper();
223 GrayImage2* PaintScrollClipper();
225 GrayImage2* Paint();
226 public:
227 CClipper(CStringW str, CSizeCoor2 size, double scalex, double scaley, bool inverse
228 , double target_scale_x=1.0, double target_scale_y=1.0);
229 void SetEffect(const Effect& effect, int effectType);
230 virtual ~CClipper();
232 static SharedPtrGrayImage2 GetAlphaMask(const SharedPtrCClipper& clipper);
234 friend class ClipperTraits;
235 friend class ClipperAlphaMaskCacheKey;
236 friend class CClipperPaintMachine;
239 class CLine: private CAtlList<SharedPtrCWord>
241 public:
242 int m_width, m_ascent, m_descent, m_borderX, m_borderY;
244 virtual ~CLine();
246 void Compact();
248 int GetWordCount();
249 void AddWord2Tail(SharedPtrCWord words);
250 bool IsEmpty();
252 CRectCoor2 PaintAll(CompositeDrawItemList* output, const CRectCoor2& clipRect,
253 const SharedPtrCClipperPaintMachine &clipper,
254 CPoint p, const CPoint& org, const int time, const int alpha);
257 class CSubtitle: private CAtlList<CLine*>
259 int GetFullWidth();
260 int GetFullLineWidth(POSITION pos);
261 int GetWrapWidth(POSITION pos, int maxwidth);
262 CLine* GetNextLine(POSITION& pos, int maxwidth);
264 public:
265 int m_scrAlignment;
266 int m_wrapStyle;
267 bool m_fAnimated;
268 bool m_fAnimated2; //If this Subtitle has animate effect
269 int m_relativeTo;
271 Effect* m_effects[EF_NUMBEROFEFFECTS];
273 CAtlList<SharedPtrCWord> m_words;
275 SharedPtrCClipper m_pClipper;
277 CRect m_rect, m_clip;
278 int m_topborder, m_bottomborder;
279 bool m_clipInverse;
281 double m_scalex, m_scaley;
282 double m_target_scale_x, m_target_scale_y;
283 public:
284 CSubtitle();
285 virtual ~CSubtitle();
286 virtual void Empty();
288 void CreateClippers(CSize size1, const CSizeCoor2& size_scale_to);
290 void MakeLines(CSize size, CRect marginRect);
292 POSITION GetHeadLinePosition();
293 CLine* GetNextLine(POSITION& pos);
296 struct CSubtitle2
298 CSubtitle2():s(NULL){}
300 CSubtitle2(CSubtitle* s_,const CRectCoor2& clipRect_, const CPoint& org_, const CPoint& org2_
301 , const CPoint& p_, int alpha_, int time_)
302 : s(s_), clipRect(clipRect_), org(org_), org2(org2_), p(p_), alpha(alpha_), time(time_)
307 CSubtitle *s;
308 const CRectCoor2 clipRect;
309 const CPoint org;
310 const CPoint org2;
311 const CPoint p;
312 int alpha;
313 int time;
316 typedef CAtlList<CSubtitle2> CSubtitle2List;
318 class CScreenLayoutAllocator
320 typedef struct
322 CRect r;
323 int segment, entry, layer;
324 } SubRect;
326 CAtlList<SubRect> m_subrects;
328 public:
329 virtual void Empty();
331 void AdvanceToSegment(int segment, const CAtlArray<int>& sa);
332 CRect AllocRect(CSubtitle* s, int segment, int entry, int layer, int collisions);
335 [uuid("537DCACA-2812-4a4f-B2C6-1A34C17ADEB0")]
336 class CRenderedTextSubtitle : public CSubPicProviderImpl, public ISubStream, public ISubPicProviderEx2, public CSimpleTextSubtitle
338 public:
339 enum AssCmdType
341 CMD_1c = 0,
342 CMD_2c,
343 CMD_3c,
344 CMD_4c,
345 CMD_1a,
346 CMD_2a,
347 CMD_3a,
348 CMD_4a,
349 CMD_alpha,
350 CMD_an,
351 CMD_a,
352 CMD_blur,
353 CMD_bord,
354 CMD_be,
355 CMD_b,
356 CMD_clip,
357 CMD_iclip,
358 CMD_c,
359 CMD_fade,
360 CMD_fad,
361 CMD_fax,
362 CMD_fay,
363 CMD_fe,
364 CMD_fn,
365 CMD_frx,
366 CMD_fry,
367 CMD_frz,
368 CMD_fr,
369 CMD_fscx,
370 CMD_fscy,
371 CMD_fsc,
372 CMD_fsp,
373 CMD_fs,
374 CMD_i,
375 CMD_kt,
376 CMD_kf,
377 CMD_K,
378 CMD_ko,
379 CMD_k,
380 CMD_move,
381 CMD_org,
382 CMD_pbo,
383 CMD_pos,
384 CMD_p,
385 CMD_q,
386 CMD_r,
387 CMD_shad,
388 CMD_s,
389 CMD_t,
390 CMD_u,
391 CMD_xbord,
392 CMD_xshad,
393 CMD_ybord,
394 CMD_yshad,
395 CMD_COUNT
397 static const int MIN_CMD_LENGTH = 1;//c etc
398 static const int MAX_CMD_LENGTH = 5;//alpha, iclip, xbord, xshad, ybord, yshad
399 static CAtlMap<CStringW, AssCmdType, CStringElementTraits<CStringW>> m_cmdMap;
401 struct AssTag;
402 typedef CAtlList<AssTag> AssTagList;
403 typedef ::boost::shared_ptr<const AssTagList> SharedPtrConstAssTagList;
404 struct AssTag
406 AssCmdType cmdType;
407 CAtlArray<CStringW> strParams;
408 AssTagList embeded;
410 private:
411 CAtlMap<int, CSubtitle*> m_subtitleCache;
413 CScreenLayoutAllocator m_sla;
415 CSizeCoor2 m_size_scale_to;
417 CSize m_size;
418 CRect m_vidrect;
420 // temp variables, used when parsing the script
421 int m_time, m_delay;
422 int m_animStart, m_animEnd;
423 double m_animAccel;
424 int m_ktype, m_kstart, m_kend;
425 int m_nPolygon;
426 int m_polygonBaselineOffset;
427 double m_fps;
428 int m_period;//1000/m_fps
429 double m_target_scale_x, m_target_scale_y;
431 static void InitCmdMap();
433 void ParseEffect(CSubtitle* sub, const CString& str);
434 void ParseString(CSubtitle* sub, CStringW str, const FwSTSStyle& style);
435 void ParsePolygon(CSubtitle* sub, const CStringW& str, const FwSTSStyle& style);
437 static bool ParseSSATag(AssTagList *assTags, const CStringW& str);
438 bool ParseSSATag(CSubtitle* sub, const AssTagList& assTags, STSStyle& style, const STSStyle& org, bool fAnimate = false);
439 bool ParseSSATag(CSubtitle* sub, const CStringW& str, STSStyle& style, const STSStyle& org, bool fAnimate = false);
441 bool ParseHtmlTag(CSubtitle* sub, CStringW str, STSStyle& style, STSStyle& org);
443 double CalcAnimation(double dst, double src, bool fAnimate);
445 CSubtitle* GetSubtitle(int entry);
447 protected:
448 virtual void OnChanged();
450 public:
451 CRenderedTextSubtitle(CCritSec* pLock);
452 virtual ~CRenderedTextSubtitle();
454 virtual void Copy(CSimpleTextSubtitle& sts);
455 virtual void Empty();
457 public:
458 bool Init(const SIZECoor2& size_scale_to, const SIZE& size1, const CRect& video_rect); // will call Deinit()
459 void Deinit();
461 DECLARE_IUNKNOWN
462 STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void** ppv);
464 // ISubPicProviderEx2
465 STDMETHODIMP Lock();
466 STDMETHODIMP Unlock();
467 STDMETHODIMP RenderEx(IXySubRenderFrame**subRenderFrame, int spd_type,
468 const SIZECoor2& size_scale_to,
469 const SIZE& size1, const CRect& video_rect,
470 REFERENCE_TIME rt, double fps);
472 // ISubPicProviderEx && ISubPicProviderEx2
473 STDMETHODIMP_(POSITION) GetStartPosition(REFERENCE_TIME rt, double fps);
474 STDMETHODIMP_(POSITION) GetNext(POSITION pos);
475 STDMETHODIMP_(REFERENCE_TIME) GetStart(POSITION pos, double fps);
476 STDMETHODIMP_(REFERENCE_TIME) GetStop(POSITION pos, double fps);
477 STDMETHODIMP_(VOID) GetStartStop(POSITION pos, double fps, /*out*/REFERENCE_TIME &start, /*out*/REFERENCE_TIME &stop);
478 STDMETHODIMP_(bool) IsAnimated(POSITION pos);
479 STDMETHODIMP Render(SubPicDesc& spd, REFERENCE_TIME rt, double fps, RECTCoor2& bbox);
480 STDMETHODIMP RenderEx(SubPicDesc& spd, REFERENCE_TIME rt, double fps, CAtlList<CRectCoor2>& rectList);
481 HRESULT ParseScript(REFERENCE_TIME rt, double fps, CSubtitle2List *outputSub2List );
482 static void DoRender( const SIZECoor2& output_size, const CSubtitle2List& sub2List,
483 CompositeDrawItemListList *compDrawItemListList /*output*/);
484 static void RenderOneSubtitle(const SIZECoor2& output_size, const CSubtitle2& sub2,
485 CompositeDrawItemList* compDrawItemList /*output*/);
486 STDMETHODIMP_(bool) IsColorTypeSupported(int type);
488 // IPersist
489 STDMETHODIMP GetClassID(CLSID* pClassID);
491 // ISubStream
492 STDMETHODIMP_(int) GetStreamCount();
493 STDMETHODIMP GetStreamInfo(int i, WCHAR** ppName, LCID* pLCID);
494 STDMETHODIMP_(int) GetStream();
495 STDMETHODIMP SetStream(int iStream);
496 STDMETHODIMP Reload();