Debian Codegraph @

lib/aviplay/aviplayprop.cpp

raw
1 #include "avm_args.h"
2 #include "avm_except.h"
3 #include "avm_creators.h" // codec sorting
4 #include "aviplay_impl.h"
5 #include "configfile.h"
6 #include "avm_output.h"
7
8 #include <string.h>
9 #include <stdarg.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12
13
14 AVM_BEGIN_NAMESPACE;
15
16 static const char regName[] = "aviplay";
17
18 // first in the list is default
19 static const char subtitleRendererTxt[] =
20 #ifdef HAVE_LIBXFT
21 "XFT_DRAW,"
22 #endif
23 #ifndef X_DISPLAY_MISSING
24 "X11_DRAW,"
25 #endif
26 "nosubtitles";
27
28 static const char audioRendererTxt[] =
29 #ifdef HAVE_OSS
30 "OSS,"
31 #endif
32 #ifdef HAVE_LIBSDL
33 "SDL,"
34 #endif
35 #ifdef HAVE_SUNAUDIO
36 "SUN,"
37 #endif
38 "noaudio";
39
40 static const char videoRendererTxt[] =
41 #ifdef HAVE_LIBSDL
42 "SDL_XV,SDL_X11,"
43 #endif
44 "novideo";
45
46 const Args::Option* IAviPlayer::getOptions()
47 {
48 static const bool DEFAULT_TRUE = true;
49 static const int DEFAULT_0 = 0;
50 static const int DEFAULT_RATE = 44100;
51 static const int DEFAULT_VOLUME = 1000;
52 static const int DEFAULT_BALANCE = 500;
53 static const Args::Option options[] =
54 {
55 // FIXME convert rest of flags later
56 // type, short, long, help, defvalue, minval, maxval
57 ARGSOPTION("i", "", "", ""), // empty field to skip PROPERTY_END
58
59 ARGSOPTION("S", "codecs-audio", "", "order of audio codecs"),
60 ARGSOPTION("S", "codecs-video", "", "order of video codecs"),
61 ARGSOPTION("S", "audio-renderer", "","audio renderer", audioRendererTxt),
62 ARGSOPTION("S", "video-renderer", "", "video renderer", videoRendererTxt),
63 ARGSOPTION("S", "subtitle-renderer", "", "subtitle renderer", subtitleRendererTxt),
64 ARGSOPTION("B", "use-yuv", "", "use YUV hw acceleration", &DEFAULT_TRUE),
65 ARGSOPTION("B", "autorepeat", "", "repeat movie after end of the movie", &DEFAULT_TRUE),
66 ARGSOPTION("I", "audio-stream", "", "select audio channel", &DEFAULT_0, -1, 255),
67 ARGSOPTION("I", "video-stream", "", "select video channel", &DEFAULT_0, -1, 255),
68
69 ARGSOPTION("S", "subtitle-font", "subfont", "select font for subtitles", "-adobe-helvetica-medium-r-normal--*-160-100-100-p-*"),
70 ARGSOPTION("I", "subtitle-async-ms", "", "async time for subtitle (ms)"),
71 ARGSOPTION("I", "subtitle-extend-ms", "", "extended time for subtitle (ms)"),
72 ARGSOPTION("S", "subtitle-codepage", "subcp", "encoding code page of subtitles (iconv --list)", "default"),
73 ARGSOPTION("B", "subtitle-enabled", "", "enable subtitles", &DEFAULT_TRUE),
74 ARGSOPTION("B", "subtitle-wrap", "", "subtitles wrap to next line"),
75 ARGSOPTION("I", "subtitle-bgcolor", "", "subtitles background color"),
76 ARGSOPTION("I", "subtitle-fgcolor", "", "subtitles foreground color"),
77 ARGSOPTION("I", "subtitle-hposition", "", "subtitles horizontal position"),
78
79 ARGSOPTION("B", "use-http-proxy", "", "use http proxy for connection"),
80 ARGSOPTION("S", "http-proxy", "", "address of http proxy"),
81 ARGSOPTION("B", "audio-resampling", "", "resample audio"),
82 ARGSOPTION("I", "audio-resampling-rate", "", "target resampling rate", &DEFAULT_RATE, 1, 100000),
83 ARGSOPTION("I", "audio-playing-rate", "", "audio playing rate", &DEFAULT_RATE, 1, 100000),
84 ARGSOPTION("B", "audio-master-timer", "", ""),
85 ARGSOPTION("I", "audio-volume", "", "audio volume <%d, %d>", &DEFAULT_VOLUME, 0, 1000), // 1000 is 1.0
86 ARGSOPTION("I", "audio-balance", "", "audio balance <%d, %d>", &DEFAULT_BALANCE, 0, 1000), // 500 is middle
87 ARGSOPTION("S", "url-audio", "", "url for audio stream"),
88
89 ARGSOPTION("B", "preserve-aspect", "", "preserve aspect during resize", &DEFAULT_TRUE),
90 ARGSOPTION("B", "buffered", "", "buffer video ahead", &DEFAULT_TRUE),
91 ARGSOPTION("B", "direct", "", "decompress directly to image memory", &DEFAULT_TRUE),
92 ARGSOPTION("B", "dropping", "", "drop frame when a/v desync", &DEFAULT_TRUE),
93 ARGSOPTION("B", "quality_auto", "", "change postprocessing automaticaly", &DEFAULT_TRUE),
94
95 ARGSOPTION("B", "display-frame-pos", "", "show more informative about position"),
96 ARGSOPTION("I", "av-async-ms", "", "audio/video async time (ms)"),
97 ARGSOPTION("#", "", "", "")
98 };
99
100 return options;
101 }
102
103 #define LOCK_THREADS() \
104 if (!locked)\
105 {\
106 lockThreads("SetProperties");\
107 locked = true;\
108 }
109
110 int AviPlayer::Set(...)
111 {
112 va_list args;
113 Property prop;
114 avm::string strtmp;
115 bool setvidbuf = false;
116 bool locked = false;
117 bool renderer = false;
118 bool useyuv;
119 va_start(args, this);
120 while ((prop = (Property) va_arg(args, int)) != 0)
121 {
122 avm::vector<const CodecInfo*> vci;
123 int int_param = 0;
124 const char* string_param = 0;
125
126 if (prop < LAST_PROPERTY)
127 {
128 switch (getOptions()[prop].getType())
129 {
130 case Args::Option::REGINT:
131 case Args::Option::REGBOOL:
132 int_param = va_arg(args, int);
133 break;
134 case Args::Option::REGSTRING:
135 string_param = va_arg(args, char*);
136 break;
137 default:
138 ;
139 }
140
141 //printf("PropertySet %d %s\n", prop, options[prop].olong);
142
143 switch (prop)
144 {
145 case AUDIO_RENDERER:
146 break;
147 case VIDEO_RENDERER:
148 break;
149 case VIDEO_CODECS:
150 LOCK_THREADS();
151 SortVideoCodecs(string_param);
152
153 strtmp.erase();
154 CodecInfo::Get(vci);
155 for (size_t i = 0; i < vci.size(); ++i)
156 {
157 strtmp += vci[i]->GetPrivateName();
158 if ((i + 1) < vci.size())
159 strtmp += ',';
160 }
161 string_param = strtmp.c_str();
162
163 if (0)
164 {
165 char* origval = 0;
166 Get(ASYNC_TIME_MS, &origval, NULL);
167 printf("ORIG %s\n NEW %s\n R: %d\n", string_param, origval, strcmp(string_param, origval));
168 }
169 if (m_pVideostream && m_pVideostream->IsStreaming())
170 {
171
172 //printf("POS1 %f\n", m_pVideostream->GetTime());
173 restartVideoStreaming();
174 if (m_pAudioRenderer)
175 {
176 m_pVideostream->SeekTime(m_pAudioRenderer->GetTime());
177 framepos_t p = m_pVideostream->GetPos();
178 if ((m_pVideostream->GetNextKeyFrame() - p) < 40)
179 m_pVideostream->SeekToNextKeyFrame();
180 else
181 m_pVideostream->SeekToPrevKeyFrame();
182 }
183 //printf("POS5 %f\n", m_pVideostream->GetTime());
184 }
185 break;
186 case AUDIO_CODECS:
187 LOCK_THREADS();
188 SortAudioCodecs(string_param);
189 strtmp.erase();
190 CodecInfo::Get(vci, CodecInfo::Audio);
191 for (size_t i = 0; i < vci.size(); i++)
192 {
193 strtmp += vci[i]->GetPrivateName();
194 if (i + 1 < vci.size())
195 strtmp += ',';
196 }
197 string_param = strtmp.c_str();
198 createAudioRenderer();
199 break;
200 case USE_YUV:
201 LOCK_THREADS();
202 Get(USE_YUV, &useyuv, 0);
203 if (int_param != useyuv)
204 renderer = true;
205 break;
206 case VIDEO_QUALITY_AUTO:
207 LOCK_THREADS();
208 if (m_bQualityAuto != int_param)
209 {
210 setvidbuf = true;
211 m_bQualityAuto = int_param;
212 }
213 break;
214 case VIDEO_BUFFERED:
215 LOCK_THREADS();
216 if (m_bVideoBuffered != int_param)
217 {
218 setvidbuf = true;
219 m_bVideoBuffered = int_param;
220 }
221 break;
222 case VIDEO_DIRECT:
223 LOCK_THREADS();
224 if (m_bVideoDirect != int_param)
225 {
226 setvidbuf = true;
227 m_bVideoDirect = int_param;
228 }
229 break;
230 case VIDEO_DROPPING:
231 m_bVideoDropping = int_param;
232 break;
233 case ASYNC_TIME_MS:
234 m_fAsync = (float) int_param / 1000.0f;
235 if (m_pAudioRenderer)
236 m_pAudioRenderer->SetAsync(m_fAsync);
237 break;
238 case SUBTITLE_FONT:
239 LOCK_THREADS();
240 setFont(string_param);
241 break;
242 case SUBTITLE_ASYNC_TIME_MS:
243 m_fSubAsync = (float) int_param / 1000.0f;
244 break;
245 case AUDIO_VOLUME:
246 if (m_pAudioRenderer)
247 m_pAudioRenderer->SetVolume(int_param);
248 break;
249 case AUDIO_BALANCE:
250 if (m_pAudioRenderer)
251 m_pAudioRenderer->SetBalance(int_param);
252 break;
253 case AUDIO_URL:
254 LOCK_THREADS();
255 setAudioUrl(string_param);
256 break;
257 case VIDEO_STREAM:
258 //delete m_pVideostream;
259 //m_pVideostream = m_pClip->GetStream(0, IAviReadStream::Video);
260 break;
261 case AUDIO_STREAM:
262 LOCK_THREADS();
263 setAudioStream(int_param);
264 break;
265 default:
266 //AVM_WRITE("Property", "Unknown (?unsupported?) propety %d\n", prop);
267 break;
268 }
269
270 switch (getOptions()[prop].getType())
271 {
272 case Args::Option::REGINT:
273 case Args::Option::REGBOOL:
274 RegWriteInt(regName, getOptions()[prop].getLongOption(), int_param);
275 break;
276 case Args::Option::REGSTRING:
277 RegWriteString(regName, getOptions()[prop].getLongOption(), string_param);
278 break;
279 default:
280 ;
281 }
282 }
283 }
284 va_end(args);
285
286 if (locked)
287 {
288 if (setvidbuf)
289 setVideoBuffering();
290 if (renderer && m_pVideostream)
291 {
292 double pos = GetTime();
293 m_pVideostream->StopStreaming();
294 m_pVideostream->StartStreaming();
295 createVideoRenderer();
296
297 double posn = m_pVideostream->SeekTimeToKeyFrame(pos);
298 if (m_pAudiostream && ((posn + 5) < pos))
299 Reseek(posn); // complet reseek
300 }
301 unlockThreads();
302 }
303
304 return 0;
305 }
306 #undef LOCK_THREADS
307
308 int AviPlayer::Get(...) const
309 {
310 va_list args;
311 Property prop;
312
313 //printf("Sizeof %d\n", sizeof(Property));
314 va_start(args, this);
315 while ((prop = (Property) va_arg(args, int)) != 0)
316 {
317 bool ok = true;
318 bool isBool = false;
319 avm::string string_param = "_empty_";
320 int int_param = 0;
321
322 if (prop < LAST_PROPERTY)
323 {
324 //printf("PropertyGet %d %s (readcnt: %d)\n", prop, options[prop].olong, propertyRead[prop]);
325 Property force = prop;
326 isBool = (getOptions()[prop].getType() == Args::Option::REGBOOL);
327
328 if (!propertyRead[prop])
329 force = LAST_PROPERTY; // force reading from registry
330
331 switch (force)
332 {
333 case VIDEO_BUFFERED:
334 int_param = m_bVideoBuffered;
335 break;
336 case VIDEO_DROPPING:
337 int_param = m_bVideoDropping;
338 break;
339 case VIDEO_DIRECT:
340 int_param = m_bVideoDirect;
341 break;
342 case VIDEO_QUALITY_AUTO:
343 int_param = m_bQualityAuto;
344 break;
345 case ASYNC_TIME_MS:
346 int_param = int(m_fAsync * 1000.0);
347 break;
348 case SUBTITLE_ASYNC_TIME_MS:
349 int_param = int(m_fSubAsync * 1000.0);
350 break;
351 case AUDIO_VOLUME:
352 int_param = (m_pAudioRenderer) ? m_pAudioRenderer->GetVolume() : 0;
353 break;
354 case AUDIO_BALANCE:
355 int_param = (m_pAudioRenderer) ? m_pAudioRenderer->GetBalance() : 500;
356 break;
357 default:
358 //printf("Default propety: %s (idx = %d)\n", propertyList[prop].text, prop);
359 switch (getOptions()[prop].getType())
360 {
361 case Args::Option::REGBOOL:
362 case Args::Option::REGINT:
363 int_param = RegReadInt(regName, getOptions()[prop].getLongOption(),
364 getOptions()[prop].getInt());
365 break;
366 case Args::Option::REGSTRING:
367 string_param = RegReadString(regName, getOptions()[prop].getLongOption(),
368 getOptions()[prop].getString());
369 break;
370 default:
371 ;
372 }
373
374 propertyRead[prop]++;
375 break;
376 }
377 }
378 else
379 {
380 switch (prop)
381 {
382 case QUERY_AVG_QUALITY:
383 int_param = int(m_Quality.average());
384 break;
385 case QUERY_AVG_DROP:
386 int_param = int(m_Drop.average());
387 break;
388 case QUERY_AUDIO_RENDERERS:
389 string_param = audioRendererTxt;
390 break;
391 case QUERY_VIDEO_RENDERERS:
392 string_param = videoRendererTxt;
393 break;
394 case QUERY_SUBTITLE_RENDERERS:
395 string_param = subtitleRendererTxt;
396 break;
397 case QUERY_EOF:
398 isBool = true;
399 int_param = (m_pVideostream) ? m_pVideostream->Eof() : true;
400 //printf("VSTREAM %d\n", int_param);
401 // audiostream doesn't detect eof - have to use Length FIXME
402 if (int_param && m_pAudioRenderer)
403 int_param = m_pAudioRenderer->Eof();
404 //printf("???EOF %d %d %f %f\n", int_param, m_pAudiostream->Eof(),
405 // m_pAudiostream->GetTime(), m_pAudiostream->GetLengthTime());
406 break;
407 case QUERY_VIDEO_WIDTH:
408 {
409 int w = m_iWidth, h;
410 if (m_VideoRenderers.size())
411 m_VideoRenderers[0]->GetSize(w, h);
412 int_param = w;
413 }
414 break;
415 case QUERY_VIDEO_HEIGHT:
416 {
417 int w, h = m_iHeight;
418 if (m_VideoRenderers.size())
419 m_VideoRenderers[0]->GetSize(w, h);
420 int_param = h;
421 }
422 break;
423 case QUERY_VIDEO_STREAMS:
424 int_param = (m_pClip) ? (int)m_pClip->VideoStreamCount() : 0;
425 break;
426 case QUERY_AUDIO_STREAMS:
427 if (m_pClipAudio)
428 int_param = (int)m_pClipAudio->AudioStreamCount();
429 else
430 int_param = (m_pClip) ? (int)m_pClip->AudioStreamCount() : 0;
431 break;
432 default:
433 AVM_WRITE("aviplay", "Unexpected property value: %d\n", prop);
434 break;
435 }
436 }
437
438 int* ptr = va_arg(args, int*);
439 //printf("Ptr: %p\n", ptr);
440 if (ok && ptr)
441 {
442 if (isBool)
443 *(bool*)ptr = (bool) int_param;
444 else
445 {
446 if (string_param == "_empty_")
447 *ptr = int_param;
448 else
449 *(char**)ptr = (char*) strdup(string_param.c_str());
450 }
451 }
452 }
453 va_end(args);
454
455 return 0;
456 }
457
458 void AviPlayer::setVideoBuffering()
459 {
460 lockThreads("SetVideoBuffered");
461
462 if (m_pVideostream)
463 {
464 IImageAllocator* ia = (m_bVideoDirect && m_VideoRenderers.size()) ? m_VideoRenderers[0] : 0;
465 uint_t bufs = (m_bVideoBuffered) ? 6 : 1;
466
467 //printf("Buffering: %d Allocator: %p\n", bufs, ia);
468 m_pVideostream->SetBuffering(bufs, ia);
469 }
470 unlockThreads();
471 }
472
473 int AviPlayer::setAudioStream(int channel)
474 {
475 IReadStream* astream = 0;
476 if (channel > 127)
477 channel = 127;
478 else if (channel < 0)
479 channel = 0;
480
481 IReadFile* qf = m_pClipAudio;
482 if (!qf)
483 qf = m_pClip;
484
485 while (qf && !astream && channel >= 0)
486 astream = qf->GetStream(channel--, IReadStream::Audio);
487
488 if (astream != m_pAudiostream)
489 {
490 double tpos = 0.0;
491 if (m_pAudioRenderer)
492 {
493 tpos = m_pAudioRenderer->GetTime();
494 delete m_pAudioRenderer;
495 m_pAudioRenderer = 0;
496 }
497 else if (m_pVideostream)
498 tpos = m_pVideostream->GetTime();
499
500 if (m_pAudiostream)
501 m_pAudiostream->StopStreaming();
502 // NOTE: never call delete on m_pAudiostream !
503 m_pAudiostream = astream;
504 createAudioRenderer();
505 if (m_pAudioRenderer)
506 {
507 m_pAudioRenderer->SeekTime(tpos);
508 m_pAudioRenderer->Start();
509 }
510 }
511 return 0;
512 }
513
514 int AviPlayer::setAudioUrl(const char* url)
515 {
516 lockThreads("SetAudioUrl");
517
518 // compare Url's from the end (as filename could be either
519 // fullpath or just filename
520 size_t u = strlen(url);
521 size_t f = m_Filename.size();
522
523 while (u > 0 && f > 0)
524 if (url[--u] != m_Filename[--f])
525 break;
526 IReadFile* newClipAudio =
527 (u == 0 || f == 0) ? m_pClip : CreateReadFile(url);
528
529 if (newClipAudio)
530 {
531 IReadFile* tmp = m_pClipAudio;
532 m_pClipAudio = (newClipAudio != m_pClip) ? newClipAudio : 0;
533 int def_audio;
534 Get(AUDIO_STREAM, &def_audio, 0);
535
536 setAudioStream(def_audio);
537 delete tmp;
538 }
539
540 unlockThreads();
541 return 0;
542 }
543
544 AVM_END_NAMESPACE;