Use MSGT_DECVIDEO in a video decoder.
[mplayer/glamo.git] / stream / cache2.c
blobe534f381c76d15b13ac25a48f94fc0ea0952724f
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 // Initial draft of my new cache system...
22 // Note it runs in 2 processes (using fork()), but doesn't require locking!!
23 // TODO: seeking, data consistency checking
25 #define READ_USLEEP_TIME 10000
26 // These defines are used to reduce the cost of many successive
27 // seeks (e.g. when a file has no index) by spinning quickly at first.
28 #define INITIAL_FILL_USLEEP_TIME 1000
29 #define INITIAL_FILL_USLEEP_COUNT 10
30 #define FILL_USLEEP_TIME 50000
31 #define PREFILL_SLEEP_TIME 200
32 #define CONTROL_SLEEP_TIME 0
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <signal.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <errno.h>
42 #include "osdep/shmem.h"
43 #include "osdep/timer.h"
44 #if defined(__MINGW32__)
45 #include <windows.h>
46 static void ThreadProc( void *s );
47 #elif defined(__OS2__)
48 #define INCL_DOS
49 #include <os2.h>
50 static void ThreadProc( void *s );
51 #elif defined(PTHREAD_CACHE)
52 #include <pthread.h>
53 static void *ThreadProc(void *s);
54 #else
55 #include <sys/wait.h>
56 #define FORKED_CACHE 1
57 #endif
58 #ifndef FORKED_CACHE
59 #define FORKED_CACHE 0
60 #endif
62 #include "mp_msg.h"
63 #include "help_mp.h"
65 #include "stream.h"
66 #include "cache2.h"
67 extern int use_gui;
69 typedef struct {
70 // constats:
71 unsigned char *buffer; // base pointer of the allocated buffer memory
72 int buffer_size; // size of the allocated buffer memory
73 int sector_size; // size of a single sector (2048/2324)
74 int back_size; // we should keep back_size amount of old bytes for backward seek
75 int fill_limit; // we should fill buffer only if space>=fill_limit
76 int seek_limit; // keep filling cache if distance is less that seek limit
77 // filler's pointers:
78 int eof;
79 off_t min_filepos; // buffer contain only a part of the file, from min-max pos
80 off_t max_filepos;
81 off_t offset; // filepos <-> bufferpos offset value (filepos of the buffer's first byte)
82 // reader's pointers:
83 off_t read_filepos;
84 // commands/locking:
85 // int seek_lock; // 1 if we will seek/reset buffer, 2 if we are ready for cmd
86 // int fifo_flag; // 1 if we should use FIFO to notice cache about buffer reads.
87 // callback
88 stream_t* stream;
89 volatile int control;
90 volatile unsigned control_uint_arg;
91 volatile double control_double_arg;
92 volatile int control_res;
93 volatile off_t control_new_pos;
94 volatile double stream_time_length;
95 } cache_vars_t;
97 static int min_fill=0;
99 int cache_fill_status=0;
101 static void cache_wakeup(stream_t *s)
103 #if FORKED_CACHE
104 // signal process to wake up immediately
105 kill(s->cache_pid, SIGUSR1);
106 #endif
109 static void cache_stats(cache_vars_t *s)
111 int newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
112 mp_msg(MSGT_CACHE,MSGL_INFO,"0x%06X [0x%06X] 0x%06X ",(int)s->min_filepos,(int)s->read_filepos,(int)s->max_filepos);
113 mp_msg(MSGT_CACHE,MSGL_INFO,"%3d %% (%3d%%)\n",100*newb/s->buffer_size,100*min_fill/s->buffer_size);
116 static int cache_read(cache_vars_t *s, unsigned char *buf, int size)
118 int total=0;
119 while(size>0){
120 int pos,newb,len;
122 //printf("CACHE2_READ: 0x%X <= 0x%X <= 0x%X \n",s->min_filepos,s->read_filepos,s->max_filepos);
124 if(s->read_filepos>=s->max_filepos || s->read_filepos<s->min_filepos){
125 // eof?
126 if(s->eof) break;
127 // waiting for buffer fill...
128 usec_sleep(READ_USLEEP_TIME); // 10ms
129 continue; // try again...
132 newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
133 if(newb<min_fill) min_fill=newb; // statistics...
135 // printf("*** newb: %d bytes ***\n",newb);
137 pos=s->read_filepos - s->offset;
138 if(pos<0) pos+=s->buffer_size; else
139 if(pos>=s->buffer_size) pos-=s->buffer_size;
141 if(newb>s->buffer_size-pos) newb=s->buffer_size-pos; // handle wrap...
142 if(newb>size) newb=size;
144 // check:
145 if(s->read_filepos<s->min_filepos) mp_msg(MSGT_CACHE,MSGL_ERR,"Ehh. s->read_filepos<s->min_filepos !!! Report bug...\n");
147 // len=write(mem,newb)
148 //printf("Buffer read: %d bytes\n",newb);
149 memcpy(buf,&s->buffer[pos],newb);
150 buf+=newb;
151 len=newb;
152 // ...
154 s->read_filepos+=len;
155 size-=len;
156 total+=len;
159 cache_fill_status=(s->max_filepos-s->read_filepos)/(s->buffer_size / 100);
160 return total;
163 static int cache_fill(cache_vars_t *s)
165 int back,back2,newb,space,len,pos;
166 off_t read=s->read_filepos;
168 if(read<s->min_filepos || read>s->max_filepos){
169 // seek...
170 mp_msg(MSGT_CACHE,MSGL_DBG2,"Out of boundaries... seeking to 0x%"PRIX64" \n",(int64_t)read);
171 // streaming: drop cache contents only if seeking backward or too much fwd:
172 if(s->stream->type!=STREAMTYPE_STREAM ||
173 read<s->min_filepos || read>=s->max_filepos+s->seek_limit)
175 s->offset= // FIXME!?
176 s->min_filepos=s->max_filepos=read; // drop cache content :(
177 if(s->stream->eof) stream_reset(s->stream);
178 stream_seek(s->stream,read);
179 mp_msg(MSGT_CACHE,MSGL_DBG2,"Seek done. new pos: 0x%"PRIX64" \n",(int64_t)stream_tell(s->stream));
183 // calc number of back-bytes:
184 back=read - s->min_filepos;
185 if(back<0) back=0; // strange...
186 if(back>s->back_size) back=s->back_size;
188 // calc number of new bytes:
189 newb=s->max_filepos - read;
190 if(newb<0) newb=0; // strange...
192 // calc free buffer space:
193 space=s->buffer_size - (newb+back);
195 // calc bufferpos:
196 pos=s->max_filepos - s->offset;
197 if(pos>=s->buffer_size) pos-=s->buffer_size; // wrap-around
199 if(space<s->fill_limit){
200 // printf("Buffer is full (%d bytes free, limit: %d)\n",space,s->fill_limit);
201 return 0; // no fill...
204 // printf("### read=0x%X back=%d newb=%d space=%d pos=%d\n",read,back,newb,space,pos);
206 // reduce space if needed:
207 if(space>s->buffer_size-pos) space=s->buffer_size-pos;
209 // if(space>32768) space=32768; // limit one-time block size
210 if(space>4*s->sector_size) space=4*s->sector_size;
212 // if(s->seek_lock) return 0; // FIXME
214 #if 1
215 // back+newb+space <= buffer_size
216 back2=s->buffer_size-(space+newb); // max back size
217 if(s->min_filepos<(read-back2)) s->min_filepos=read-back2;
218 #else
219 s->min_filepos=read-back; // avoid seeking-back to temp area...
220 #endif
222 // ....
223 //printf("Buffer fill: %d bytes of %d\n",space,s->buffer_size);
224 //len=stream_fill_buffer(s->stream);
225 //memcpy(&s->buffer[pos],s->stream->buffer,len); // avoid this extra copy!
226 // ....
227 len=stream_read(s->stream,&s->buffer[pos],space);
228 s->eof= !len;
230 s->max_filepos+=len;
231 if(pos+len>=s->buffer_size){
232 // wrap...
233 s->offset+=s->buffer_size;
236 return len;
240 static int cache_execute_control(cache_vars_t *s) {
241 static unsigned last;
242 int quit = s->control == -2;
243 if (quit || !s->stream->control) {
244 s->stream_time_length = 0;
245 s->control_new_pos = 0;
246 s->control_res = STREAM_UNSUPPORTED;
247 s->control = -1;
248 return !quit;
250 if (GetTimerMS() - last > 99) {
251 double len;
252 if (s->stream->control(s->stream, STREAM_CTRL_GET_TIME_LENGTH, &len) == STREAM_OK)
253 s->stream_time_length = len;
254 else
255 s->stream_time_length = 0;
256 last = GetTimerMS();
258 if (s->control == -1) return 1;
259 switch (s->control) {
260 case STREAM_CTRL_GET_CURRENT_TIME:
261 case STREAM_CTRL_SEEK_TO_TIME:
262 case STREAM_CTRL_GET_ASPECT_RATIO:
263 s->control_res = s->stream->control(s->stream, s->control, &s->control_double_arg);
264 break;
265 case STREAM_CTRL_SEEK_TO_CHAPTER:
266 case STREAM_CTRL_GET_NUM_CHAPTERS:
267 case STREAM_CTRL_GET_CURRENT_CHAPTER:
268 case STREAM_CTRL_GET_NUM_ANGLES:
269 case STREAM_CTRL_GET_ANGLE:
270 case STREAM_CTRL_SET_ANGLE:
271 s->control_res = s->stream->control(s->stream, s->control, &s->control_uint_arg);
272 break;
273 default:
274 s->control_res = STREAM_UNSUPPORTED;
275 break;
277 s->control_new_pos = s->stream->pos;
278 s->control = -1;
279 return 1;
282 static void *shared_alloc(int size) {
283 #if FORKED_CACHE
284 return shmem_alloc(size);
285 #else
286 return malloc(size);
287 #endif
290 static void shared_free(void *ptr, int size) {
291 #if FORKED_CACHE
292 shmem_free(ptr, size);
293 #else
294 free(ptr);
295 #endif
298 static cache_vars_t* cache_init(int size,int sector){
299 int num;
300 cache_vars_t* s=shared_alloc(sizeof(cache_vars_t));
301 if(s==NULL) return NULL;
303 memset(s,0,sizeof(cache_vars_t));
304 num=size/sector;
305 if(num < 16){
306 num = 16;
307 }//32kb min_size
308 s->buffer_size=num*sector;
309 s->sector_size=sector;
310 s->buffer=shared_alloc(s->buffer_size);
312 if(s->buffer == NULL){
313 shared_free(s, sizeof(cache_vars_t));
314 return NULL;
317 s->fill_limit=8*sector;
318 s->back_size=s->buffer_size/2;
319 return s;
322 void cache_uninit(stream_t *s) {
323 cache_vars_t* c = s->cache_data;
324 if(s->cache_pid) {
325 #if !FORKED_CACHE
326 cache_do_control(s, -2, NULL);
327 #else
328 kill(s->cache_pid,SIGKILL);
329 waitpid(s->cache_pid,NULL,0);
330 #endif
331 s->cache_pid = 0;
333 if(!c) return;
334 shared_free(c->buffer, c->buffer_size);
335 c->buffer = NULL;
336 c->stream = NULL;
337 shared_free(s->cache_data, sizeof(cache_vars_t));
338 s->cache_data = NULL;
341 static void exit_sighandler(int x){
342 // close stream
343 exit(0);
346 static void dummy_sighandler(int x) {
350 * Main loop of the cache process or thread.
352 static void cache_mainloop(cache_vars_t *s) {
353 int sleep_count = 0;
354 do {
355 if (!cache_fill(s)) {
356 #if FORKED_CACHE
357 // Let signal wake us up, we cannot leave this
358 // enabled since we do not handle EINTR in most places.
359 // This might need extra code to work on BSD.
360 signal(SIGUSR1, dummy_sighandler);
361 #endif
362 if (sleep_count < INITIAL_FILL_USLEEP_COUNT) {
363 sleep_count++;
364 usec_sleep(INITIAL_FILL_USLEEP_TIME);
365 } else
366 usec_sleep(FILL_USLEEP_TIME); // idle
367 #if FORKED_CACHE
368 signal(SIGUSR1, SIG_IGN);
369 #endif
370 } else
371 sleep_count = 0;
372 // cache_stats(s->cache_data);
373 } while (cache_execute_control(s));
377 * \return 1 on success, 0 if the function was interrupted and -1 on error
379 int stream_enable_cache(stream_t *stream,int size,int min,int seek_limit){
380 int ss = stream->sector_size ? stream->sector_size : STREAM_BUFFER_SIZE;
381 int res = -1;
382 cache_vars_t* s;
384 if (stream->flags & STREAM_NON_CACHEABLE) {
385 mp_msg(MSGT_CACHE,MSGL_STATUS,"\rThis stream is non-cacheable\n");
386 return 1;
389 s=cache_init(size,ss);
390 if(s == NULL) return -1;
391 stream->cache_data=s;
392 s->stream=stream; // callback
393 s->seek_limit=seek_limit;
396 //make sure that we won't wait from cache_fill
397 //more data than it is allowed to fill
398 if (s->seek_limit > s->buffer_size - s->fill_limit ){
399 s->seek_limit = s->buffer_size - s->fill_limit;
401 if (min > s->buffer_size - s->fill_limit) {
402 min = s->buffer_size - s->fill_limit;
405 #if FORKED_CACHE
406 if((stream->cache_pid=fork())){
407 if ((pid_t)stream->cache_pid == -1)
408 stream->cache_pid = 0;
409 #else
411 stream_t* stream2=malloc(sizeof(stream_t));
412 memcpy(stream2,s->stream,sizeof(stream_t));
413 s->stream=stream2;
414 #if defined(__MINGW32__)
415 stream->cache_pid = _beginthread( ThreadProc, 0, s );
416 #elif defined(__OS2__)
417 stream->cache_pid = _beginthread( ThreadProc, NULL, 256 * 1024, s );
418 #else
420 pthread_t tid;
421 pthread_create(&tid, NULL, ThreadProc, s);
422 stream->cache_pid = 1;
424 #endif
425 #endif
426 if (!stream->cache_pid) {
427 mp_msg(MSGT_CACHE, MSGL_ERR,
428 "Starting cache process/thread failed: %s.\n", strerror(errno));
429 goto err_out;
431 // wait until cache is filled at least prefill_init %
432 mp_msg(MSGT_CACHE,MSGL_V,"CACHE_PRE_INIT: %"PRId64" [%"PRId64"] %"PRId64" pre:%d eof:%d \n",
433 (int64_t)s->min_filepos,(int64_t)s->read_filepos,(int64_t)s->max_filepos,min,s->eof);
434 while(s->read_filepos<s->min_filepos || s->max_filepos-s->read_filepos<min){
435 mp_msg(MSGT_CACHE,MSGL_STATUS,MSGTR_CacheFill,
436 100.0*(float)(s->max_filepos-s->read_filepos)/(float)(s->buffer_size),
437 (int64_t)s->max_filepos-s->read_filepos
439 if(s->eof) break; // file is smaller than prefill size
440 if(stream_check_interrupt(PREFILL_SLEEP_TIME)) {
441 res = 0;
442 goto err_out;
445 mp_msg(MSGT_CACHE,MSGL_STATUS,"\n");
446 return 1; // parent exits
448 err_out:
449 cache_uninit(stream);
450 return res;
453 #if FORKED_CACHE
454 #ifdef CONFIG_GUI
455 use_gui = 0; // mp_msg may not use gui stuff in forked code
456 #endif
457 signal(SIGTERM,exit_sighandler); // kill
458 cache_mainloop(s);
459 // make sure forked code never leaves this function
460 exit(0);
461 #endif
464 #if !FORKED_CACHE
465 #if defined(__MINGW32__) || defined(__OS2__)
466 static void ThreadProc( void *s ){
467 cache_mainloop(s);
468 _endthread();
470 #else
471 static void *ThreadProc( void *s ){
472 cache_mainloop(s);
473 return NULL;
475 #endif
476 #endif
478 int cache_stream_fill_buffer(stream_t *s){
479 int len;
480 if(!s->cache_pid) return stream_fill_buffer(s);
482 // cache_stats(s->cache_data);
484 if(s->pos!=((cache_vars_t*)s->cache_data)->read_filepos) mp_msg(MSGT_CACHE,MSGL_ERR,"!!! read_filepos differs!!! report this bug...\n");
486 len=cache_read(s->cache_data,s->buffer, ((cache_vars_t*)s->cache_data)->sector_size);
487 //printf("cache_stream_fill_buffer->read -> %d\n",len);
489 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; }
490 s->eof=0;
491 s->buf_pos=0;
492 s->buf_len=len;
493 s->pos+=len;
494 // printf("[%d]",len);fflush(stdout);
495 return len;
499 int cache_stream_seek_long(stream_t *stream,off_t pos){
500 cache_vars_t* s;
501 off_t newpos;
502 if(!stream->cache_pid) return stream_seek_long(stream,pos);
504 s=stream->cache_data;
505 // s->seek_lock=1;
507 mp_msg(MSGT_CACHE,MSGL_DBG2,"CACHE2_SEEK: 0x%"PRIX64" <= 0x%"PRIX64" (0x%"PRIX64") <= 0x%"PRIX64" \n",s->min_filepos,pos,s->read_filepos,s->max_filepos);
509 newpos=pos/s->sector_size; newpos*=s->sector_size; // align
510 stream->pos=s->read_filepos=newpos;
511 s->eof=0; // !!!!!!!
512 cache_wakeup(stream);
514 cache_stream_fill_buffer(stream);
516 pos-=newpos;
517 if(pos>=0 && pos<=stream->buf_len){
518 stream->buf_pos=pos; // byte position in sector
519 return 1;
522 // stream->buf_pos=stream->buf_len=0;
523 // return 1;
525 mp_msg(MSGT_CACHE,MSGL_V,"cache_stream_seek: WARNING! Can't seek to 0x%"PRIX64" !\n",(int64_t)(pos+newpos));
526 return 0;
529 int cache_do_control(stream_t *stream, int cmd, void *arg) {
530 cache_vars_t* s = stream->cache_data;
531 switch (cmd) {
532 case STREAM_CTRL_SEEK_TO_TIME:
533 s->control_double_arg = *(double *)arg;
534 s->control = cmd;
535 break;
536 case STREAM_CTRL_SEEK_TO_CHAPTER:
537 case STREAM_CTRL_SET_ANGLE:
538 s->control_uint_arg = *(unsigned *)arg;
539 s->control = cmd;
540 break;
541 // the core might call these every frame, they are too slow for this...
542 case STREAM_CTRL_GET_TIME_LENGTH:
543 // case STREAM_CTRL_GET_CURRENT_TIME:
544 *(double *)arg = s->stream_time_length;
545 return s->stream_time_length ? STREAM_OK : STREAM_UNSUPPORTED;
546 case STREAM_CTRL_GET_NUM_CHAPTERS:
547 case STREAM_CTRL_GET_CURRENT_CHAPTER:
548 case STREAM_CTRL_GET_ASPECT_RATIO:
549 case STREAM_CTRL_GET_NUM_ANGLES:
550 case STREAM_CTRL_GET_ANGLE:
551 case -2:
552 s->control = cmd;
553 break;
554 default:
555 return STREAM_UNSUPPORTED;
557 cache_wakeup(stream);
558 while (s->control != -1)
559 usec_sleep(CONTROL_SLEEP_TIME);
560 switch (cmd) {
561 case STREAM_CTRL_GET_TIME_LENGTH:
562 case STREAM_CTRL_GET_CURRENT_TIME:
563 case STREAM_CTRL_GET_ASPECT_RATIO:
564 *(double *)arg = s->control_double_arg;
565 break;
566 case STREAM_CTRL_GET_NUM_CHAPTERS:
567 case STREAM_CTRL_GET_CURRENT_CHAPTER:
568 case STREAM_CTRL_GET_NUM_ANGLES:
569 case STREAM_CTRL_GET_ANGLE:
570 *(unsigned *)arg = s->control_uint_arg;
571 break;
572 case STREAM_CTRL_SEEK_TO_CHAPTER:
573 case STREAM_CTRL_SEEK_TO_TIME:
574 case STREAM_CTRL_SET_ANGLE:
575 stream->pos = s->read_filepos = s->control_new_pos;
576 break;
578 return s->control_res;