Debian Codegraph @

lib/aviplay/aviplay2.cpp

raw
1
2 #include "aviplay_impl.h"
3 #include "playerwidget.h"
4 #include "avm_fourcc.h"
5 #include "avm_cpuinfo.h"
6 #include "avm_except.h"
7 #include "configfile.h"
8 #include "infotypes.h"
9
10 #include <stdio.h>
11 #include <stdlib.h> // free()
12
13 AVM_BEGIN_NAMESPACE;
14
15 class AviPlayer2: public AviPlayer, public IPlayerWidget
16 {
17 IPlayerWidget* m_pPW;
18 void* m_pDpy;
19 int m_iResizeCount;
20 int zx, zy, zw, zh;
21 public:
22 AviPlayer2(IPlayerWidget* pw, void* _dpy,
23 const char* filename, const char* subname,
24 int flags,
25 const char* vcodec, const char* acodec) :
26 AviPlayer(filename, GetPhysicalDepth(_dpy), subname, flags,
27 vcodec, acodec), m_pPW(pw ? pw : this), m_pDpy(_dpy),
28 m_iResizeCount(1), zx(0), zy(0), zw(0), zh(0)
29 {
30 m_pKillhandler = killHandler;
31 m_pKillhandlerArg = (void*)this;
32 updateResize();
33 }
34 virtual ~AviPlayer2() { m_pKillhandler = 0; }
35 virtual int Refresh()
36 {
37 for (size_t i = 0; i < m_VideoRenderers.size(); ++i)
38 m_VideoRenderers[i]->Refresh();
39 return 0;
40 }
41 // should be redesigned - only first one will get resize event for now
42 virtual int Resize(int& new_w, int& new_h)
43 {
44 if (m_VideoRenderers.size() > 0)
45 {
46 bool pres;
47 Get(VIDEO_PRESERVE_ASPECT, &pres, 0);
48 if (pres)
49 {
50 StreamInfo* si = m_pVideostream->GetStreamInfo();
51 float a = si->GetAspectRatio();
52 delete si;
53 if (a == 0)
54 a = (float)GetWidth() / (float)GetHeight();
55 new_w = (int)((float)new_h * a + 0.5f);
56 if (zw)
57 new_w = new_h * zw / zh;
58 }
59 return m_VideoRenderers.front()->Resize(new_w, new_h);
60 }
61 return 0;
62 }
63 virtual int Zoom(int x, int y, int w, int h)
64 {
65 zx = zy = zw = zh = 0;
66 for (size_t i = 0; i < m_VideoRenderers.size(); ++i)
67 if (m_VideoRenderers[i]->Zoom(x, y, w, h) == 0)
68 {
69 zx = x; zy = y; zw = w; zh = h;
70 }
71 return 0;
72 }
73 virtual int ToggleFullscreen(bool maximize=false)
74 {
75 for (size_t i = 0; i < m_VideoRenderers.size(); ++i)
76 if (m_VideoRenderers[i]->ToggleFullscreen(maximize) != 0)
77 return -1;
78 return 0;
79 }
80
81 // FIXME: will be removed - and replaced with better code later
82 // this is very impractical
83 // we need to support many other callbacks
84 // users might want to use any device they have connected
85 // to their linux to control player
86 virtual void PW_stop_func() { Stop(); } //'x'
87 virtual void PW_middle_button() //middle-click
88 {
89 m_iResizeCount++;
90 updateResize();
91 }
92 virtual void PW_pause_func() { Pause(true); }//'c'
93 virtual void PW_play_func() //'v'
94 {
95 if (!IsPlaying())
96 Start();
97 else
98 Pause(false);
99 }
100 virtual void PW_fullscreen() { ToggleFullscreen(); } //alt+enter, esc
101 virtual void PW_resize(int w, int h) { Resize(w, h); } // resize event
102 virtual void PW_refresh() { Refresh(); }
103 virtual void PW_maximize_func() { ToggleFullscreen(true); }
104
105 void updateResize()
106 {
107 int w = GetWidth(), h = GetHeight();
108 switch (m_iResizeCount % 3)
109 {
110 case 0:
111 w /= 2;
112 h /= 2;
113 break;
114 case 1:
115 break;
116 case 2:
117 w *= 2;
118 h *= 2;
119 break;
120 }
121 Resize(w, h);
122 }
123
124 protected:
125 virtual void createVideoRenderer()
126 {
127 int pos = -1, wx, wy, ww, wh;
128 while (m_VideoRenderers.size() > 0)
129 {
130 if (pos < 0)
131 pos = m_VideoRenderers.back()->GetPosition(wx, wy);
132 if (pos == 0)
133 pos = m_VideoRenderers.back()->GetSize(ww, wh);
134 delete m_VideoRenderers.back();
135 m_VideoRenderers.pop_back();
136 }
137
138 //printf("WX %d WY %d WW %d WH %d\n", wx, wy, ww, wh);
139 #ifndef X_DISPLAY_MISSING
140 if (m_pDpy && GetWidth() > 0 && GetHeight() > 0)
141 {
142 IVideoRenderer* renderer = 0;
143 bool sub = HasSubtitles();
144
145 bool useyuv;
146 Get(USE_YUV, &useyuv, 0);
147
148 if (useyuv)
149 {
150 // checking for YUY2 first
151 const fourcc_t tryFcc[] = {
152 //fccYUY2, // looks like YV12 is not supported for Color changes
153 fccYUY2,
154 fccYV12, // fastest
155 fccUYVY,
156 fccI420,
157 // forced
158 //fccYUY2,
159 fccYV12,
160 //fccUYVY,
161 0
162 };
163 int i = 0;
164
165 while (!renderer && tryFcc[i])
166 {
167 // we always allow to use last entry when HW acceleration
168 // is requested - it is giving hw scalable picture
169 m_CSP = tryFcc[i];
170 if (!tryFcc[i + 1] || SetColorSpace(m_CSP, true) == 0)
171 {
172 renderer = CreateYUVRenderer(m_pPW, m_pDpy,
173 GetWidth(), GetHeight(),
174 m_CSP, sub);
175 SetColorSpace(m_CSP, false);
176 }
177 i++;
178 }
179 }
180
181 if (!renderer)
182 {
183 // BGR renderer
184 renderer = CreateFullscreenRenderer(m_pPW, m_pDpy, GetWidth(), GetHeight(), sub);
185 m_CSP = m_iDepth;
186 m_pVideostream->GetVideoDecoder()->SetDestFmt(m_iDepth);
187 }
188
189 if (renderer)
190 {
191 StreamInfo* si = m_pVideostream->GetStreamInfo();
192 float a = si->GetAspectRatio();
193 delete si;
194 if (a > 1.)
195 {
196 int w = (int)((float)GetHeight() * a + 0.5f);
197 int h = GetHeight();
198 renderer->Resize(w, h);
199 }
200 if (pos == 0)
201 {
202 renderer->Resize(ww, wh);
203 renderer->SetPosition(wx, wy);
204 }
205 m_VideoRenderers.push_back(renderer);
206 char* fs;
207 Get(SUBTITLE_FONT, &fs, 0);
208 renderer->SetFont(sub ? fs : 0);
209 if (fs) free(fs);
210 }
211 setVideoBuffering();
212 }
213 #endif
214 }
215 virtual int setFont(const char* fn)
216 {
217 if (m_pVideostream)
218 {
219 lockThreads("setFont");
220
221 for (size_t i = 0; i < m_VideoRenderers.size(); ++i)
222 m_VideoRenderers[i]->SetFont(fn);
223
224 // direct renderer for X11 needs to reinitilize surface!
225 if (m_pVideostream->GetVideoDecoder())
226 m_pVideostream->GetVideoDecoder()->Restart();
227
228 unlockThreads();
229 }
230 return 0;
231 }
232 static void drawFunc(const CImage*, void* arg=0) {}
233 static void killHandler(int, void* arg=0) {}
234 };
235
236
237 IAviPlayer2* CreateAviPlayer2(IPlayerWidget* parent, void* dpy,
238 const char* filename, const char* subname,
239 int flags,
240 const char* vcodec, const char* acodec)
241 {
242 return new AviPlayer2(parent, dpy, filename, subname,
243 flags, vcodec, acodec);
244 }
245
246 AVM_END_NAMESPACE;