add a return code to ddvd_run()
[libdreamdvd.git] / main.c
1 /*
2  * DreamDVD V0.9 - DVD-Player for Dreambox
3  * Copyright (C) 2007 by Seddi
4  *
5  * This DVD Player is based upon the great work from the libdvdnav project,
6  * a52dec library, ffmpeg and the knowledge from all the people who made
7  * watching DVD within linux possible.
8  *
9  * DreamDVD is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * DreamDVD is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
22  *
23  * part of libdreamdvd
24  */
25
26 #include "main.h"
27 #include "mpegaudioenc.h"
28 #include "a52dec.h"
29
30 #include <poll.h>
31
32 #define CONVERT_TO_DVB_COMPLIANT_AC3
33
34 /*
35  * local helper functions
36  */
37 static ssize_t safe_write(int fd, const void *buf, size_t count)
38 {
39         const uint8_t *ptr = buf;
40         size_t written = 0;
41         ssize_t n;
42
43         while (written < count) {
44                 n = write(fd, &ptr[written], count - written);
45                 if (n < 0) {
46                         if (errno != EINTR) {
47                                 perror("write");
48                                 return written ? written : -1;
49                         }
50                 } else {
51                         written += n;
52                 }
53         }
54
55         return written;
56 }
57
58 static void write_string(const char *filename, const char *string)
59 {
60         FILE *f;
61
62         f = fopen(filename, "w");
63         if (f == NULL) {
64                 perror(filename);
65                 return;
66         }
67
68         fputs(string, f);
69         fclose(f);
70 }
71
72 static int open_pipe(int fd[2])
73 {
74         int flags;
75
76         fd[0] = fd[1] = -1;
77
78         if (pipe(fd) < 0) {
79                 perror("pipe");
80                 goto err;
81         }
82
83         flags = fcntl(fd[0], F_GETFL);
84         if (flags < 0) {
85                 perror("F_GETFL");
86                 goto err;
87         }
88         if (fcntl(fd[0], F_SETFL, flags | O_NONBLOCK) < 0) {
89                 perror("F_SETFL");
90                 goto err;
91         }
92
93         return 0;
94
95 err:
96         if (fd[0] != -1) {
97                 close(fd[0]);
98                 fd[0] = -1;
99         }
100         if (fd[1] != -1) {
101                 close(fd[1]);
102                 fd[1] = -1;
103         }
104
105         return -1;
106 }
107
108 /*
109  * external functions
110  */
111
112 // create ddvd handle and set defaults
113 struct ddvd *ddvd_create(void)
114 {
115         struct ddvd *pconfig;
116
117         pconfig = malloc(sizeof(struct ddvd));
118         if (pconfig == NULL) {
119                 perror("malloc");
120                 return NULL;
121         }
122
123         memset(pconfig, 0, sizeof(struct ddvd));
124
125         // defaults
126         ddvd_set_ac3thru(pconfig, 0);
127         ddvd_set_language(pconfig, "en");
128         ddvd_set_dvd_path(pconfig, "/dev/cdroms/cdrom0");
129         ddvd_set_video(pconfig, DDVD_4_3_LETTERBOX, DDVD_PAL);
130         ddvd_set_lfb(pconfig, NULL, 720, 576, 1, 720);
131         pconfig->next_time_update = 0;
132         strcpy(pconfig->title_string, "");
133
134         // open pipes
135         if (open_pipe(pconfig->key_pipe) < 0) {
136                 ddvd_close(pconfig);
137                 return NULL;
138         }
139         if (open_pipe(pconfig->message_pipe) < 0) {
140                 ddvd_close(pconfig);
141                 return NULL;
142         }
143
144         return pconfig;
145 }
146
147 // destroy ddvd handle
148 void ddvd_close(struct ddvd *pconfig)
149 {
150         if (pconfig->message_pipe[0] != -1)
151                 close(pconfig->message_pipe[0]);
152         if (pconfig->message_pipe[1] != -1)
153                 close(pconfig->message_pipe[1]);
154         if (pconfig->key_pipe[0] != -1)
155                 close(pconfig->key_pipe[0]);
156         if (pconfig->key_pipe[1] != -1)
157                 close(pconfig->key_pipe[1]);
158         if (pconfig->dvd_path != NULL)
159                 free(pconfig->dvd_path);
160
161         free(pconfig);
162 }
163
164 // get message_pipe fd for polling functions in the host app
165 int ddvd_get_messagepipe_fd(struct ddvd *pconfig)
166 {
167         return pconfig->message_pipe[0];
168 }
169
170 // set framebuffer options
171 void ddvd_set_lfb(struct ddvd *pconfig, unsigned char *lfb, int xres, int yres, int bypp, int stride)
172 {
173         pconfig->lfb = lfb;
174         pconfig->xres = xres;
175         pconfig->yres = yres;
176         pconfig->stride = stride;
177         pconfig->bypp = bypp;
178 }
179
180 // set path to dvd block device or file structure/iso
181 void ddvd_set_dvd_path(struct ddvd *pconfig, const char *path)
182 {
183         if (pconfig->dvd_path != NULL)
184                 free(pconfig->dvd_path);
185
186         pconfig->dvd_path = strdup(path);
187 }
188
189 // set language
190 void ddvd_set_language(struct ddvd *pconfig, const char lang[2])
191 {
192         memcpy(pconfig->language, lang, 2);
193 }
194
195 // set internal ac3 decoding (needs liba52 which will be dynamically loaded)
196 void ddvd_set_ac3thru(struct ddvd *pconfig, int ac3thru)
197 {
198         pconfig->ac3thru = ac3thru;
199 }
200
201 // set video options
202 void ddvd_set_video(struct ddvd *pconfig, int aspect, int tv_system)
203 {
204         pconfig->aspect = aspect;
205         pconfig->tv_system = tv_system;
206 }
207
208 // send commands/keys to the main player
209 void ddvd_send_key(struct ddvd *pconfig, int key)
210 {
211         safe_write(pconfig->key_pipe[1], &key, sizeof(int));
212 }
213
214 // skip n seconds in playing n>0 forward - n<0 backward
215 void ddvd_skip_seconds(struct ddvd *pconfig, int seconds)
216 {
217         if (seconds < 0)
218                 ddvd_send_key(pconfig, DDVD_SKIP_BWD);
219         else
220                 ddvd_send_key(pconfig, DDVD_SKIP_FWD);
221
222         ddvd_send_key(pconfig, seconds);
223 }
224
225 // jump to beginning of given title
226 void ddvd_set_title(struct ddvd *pconfig, int title)
227 {
228         ddvd_send_key(pconfig, DDVD_SET_TITLE);
229         ddvd_send_key(pconfig, title);
230 }
231
232 // jump to beginning of given chapter
233 void ddvd_set_chapter(struct ddvd *pconfig, int chapter)
234 {
235         ddvd_send_key(pconfig, DDVD_SET_CHAPTER);
236         ddvd_send_key(pconfig, chapter);
237 }
238
239 // get and process the next message from the main player
240 int ddvd_get_next_message(struct ddvd *pconfig, int blocked)
241 {
242         int res;
243         int i;
244
245         if (ddvd_readpipe(pconfig->message_pipe[0], &res, sizeof(int), blocked) != sizeof(int))
246                 res = DDVD_NULL;
247
248         switch (res)            // more data to process ?
249         {
250         case DDVD_COLORTABLE_UPDATE:
251                 for (i = 0; i < 4; i++)
252                         ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_col[i], sizeof(struct ddvd_color), 1);
253                 break;
254         case DDVD_SHOWOSD_TIME:
255                 ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_time, sizeof(pconfig->last_time), 1);
256                 break;
257         case DDVD_SHOWOSD_STATE_FFWD:
258                 ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_trickspeed, sizeof(pconfig->last_trickspeed), 1);
259                 ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_time, sizeof(pconfig->last_time), 1);
260                 break;
261         case DDVD_SHOWOSD_STATE_FBWD:
262                 ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_trickspeed, sizeof(pconfig->last_trickspeed), 1);
263                 ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_time, sizeof(pconfig->last_time), 1);
264                 break;
265         case DDVD_SHOWOSD_STRING:
266                 ddvd_readpipe(pconfig->message_pipe[0], pconfig->last_string, sizeof(pconfig->last_string), 1);
267                 break;
268         case DDVD_SHOWOSD_AUDIO:
269                 ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_audio_id, sizeof(int), 1);
270                 ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_audio_lang, sizeof(uint16_t), 1);
271                 ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_audio_type, sizeof(int), 1);
272                 break;
273         case DDVD_SHOWOSD_SUBTITLE:
274                 ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_spu_id, sizeof(int), 1);
275                 ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_spu_lang, sizeof(uint16_t), 1);
276                 break;
277         default:
278                 break;
279         }
280
281         return res;
282 }
283
284 // get last colortable for 8bit mode (4 colors)
285 void ddvd_get_last_colortable(struct ddvd *pconfig, void *colortable)
286 {
287         memcpy(colortable, pconfig->last_col, sizeof(pconfig->last_col));
288 }
289
290 // get last received playing time as struct ddvd_time
291 void ddvd_get_last_time(struct ddvd *pconfig, void *timestamp)
292 {
293         memcpy(timestamp, &pconfig->last_time, sizeof(pconfig->last_time));
294 }
295
296 // get the actual trickspeed (2-64x) when in trickmode
297 void ddvd_get_last_trickspeed(struct ddvd *pconfig, void *trickspeed)
298 {
299         memcpy(trickspeed, &pconfig->last_trickspeed, sizeof(pconfig->last_trickspeed));
300 }
301
302 // get last text message from player
303 void ddvd_get_last_string(struct ddvd *pconfig, void *text)
304 {
305         memcpy(text, pconfig->last_string, sizeof(pconfig->last_string));
306 }
307
308 // get the active audio track
309 void ddvd_get_last_audio(struct ddvd *pconfig, void *id, void *lang, void *type)
310 {
311         memcpy(id, &pconfig->last_audio_id, sizeof(pconfig->last_audio_id));
312         memcpy(lang, &pconfig->last_audio_lang, sizeof(pconfig->last_audio_lang));
313         memcpy(type, &pconfig->last_audio_type, sizeof(pconfig->last_audio_type));
314 }
315
316 // get the active SPU track
317 void ddvd_get_last_spu(struct ddvd *pconfig, void *id, void *lang)
318 {
319         memcpy(id, &pconfig->last_spu_id, sizeof(pconfig->last_spu_id));
320         memcpy(lang, &pconfig->last_spu_lang, sizeof(pconfig->last_spu_lang));
321 }
322
323 void ddvd_get_title_string(struct ddvd *pconfig, char *title_string)
324 {
325         memcpy(title_string, pconfig->title_string, sizeof(pconfig->title_string));
326 }
327
328 // the main player loop
329 enum ddvd_result ddvd_run(struct ddvd *playerconfig)
330 {
331         if (playerconfig->lfb == NULL) {
332                 printf("Frame/backbuffer not given to libdreamdvd. Will not start the player !\n");
333                 return DDVD_INVAL;
334         }
335
336         ddvd_screeninfo_xres = playerconfig->xres;
337         ddvd_screeninfo_yres = playerconfig->yres;
338         ddvd_screeninfo_stride = playerconfig->stride;
339         int ddvd_screeninfo_bypp = playerconfig->bypp;
340         int message_pipe = playerconfig->message_pipe[1];
341         int key_pipe = playerconfig->key_pipe[0];
342         unsigned char *p_lfb = playerconfig->lfb;
343         enum ddvd_result res = DDVD_OK;
344         int msg;
345         // try to load liba52.so.0 for softdecoding
346         int have_liba52 = ddvd_load_liba52();
347
348         uint8_t *last_iframe = NULL;
349         uint8_t *spu_buffer = NULL;
350         uint8_t *spu_backbuffer = NULL;
351
352         // init backbuffer (SPU)
353         ddvd_lbb = malloc(720 * 576);   // the spu backbuffer is always max DVD PAL 720x576 pixel (NTSC 720x480)
354         if (ddvd_lbb == NULL) {
355                 perror("SPU-Backbuffer <mem allocation failed>");
356                 res = DDVD_NOMEM;
357                 goto err_malloc;
358         }
359
360         last_iframe = malloc(320 * 1024);
361         if (last_iframe == NULL) {
362                 perror("malloc");
363                 res = DDVD_NOMEM;
364                 goto err_malloc;
365         }
366
367         spu_buffer = malloc(2 * (128 * 1024));
368         if (spu_buffer == NULL) {
369                 perror("malloc");
370                 res = DDVD_NOMEM;
371                 goto err_malloc;
372         }
373
374         spu_backbuffer = malloc(3 * 2 * (128 * 1024));
375         if (spu_backbuffer == NULL) {
376                 perror("malloc");
377                 res = DDVD_NOMEM;
378                 goto err_malloc;
379         }
380
381         memset(ddvd_lbb, 0, ddvd_screeninfo_xres * ddvd_screeninfo_yres);
382         memset(p_lfb, 0, ddvd_screeninfo_stride * ddvd_screeninfo_yres);        //clear screen
383
384         msg = DDVD_SCREEN_UPDATE;
385         safe_write(message_pipe, &msg, sizeof(int));
386
387         printf("Opening output...\n");
388
389 #if CONFIG_API_VERSION == 1
390         ddvd_output_fd = open("/dev/video", O_WRONLY);
391         if (ddvd_output_fd == -1) {
392                 perror("/dev/video");
393                 res = DDVD_BUSY;
394                 goto err_open_output_fd;
395         }
396
397         ddvd_fdvideo = open("/dev/dvb/card0/video0", O_RDWR);
398         if (ddvd_fdvideo == -1) {
399                 perror("/dev/dvb/card0/video0");
400                 res = DDVD_BUSY;
401                 goto err_open_fdvideo;
402         }
403
404         ddvd_fdaudio = open("/dev/dvb/card0/audio0", O_RDWR);
405         if (ddvd_fdaudio == -1) {
406                 perror("/dev/dvb/card0/audio0");
407                 res = DDVD_BUSY;
408                 goto err_open_fdaudio;
409         }
410
411         ddvd_ac3_fd = open("/dev/sound/dsp1", O_RDWR);
412         if (ddvd_ac3_fd == -1) {
413                 perror("/dev/sound/dsp1");
414                 res = DDVD_BUSY;
415                 goto err_open_ac3_fd;
416         }
417
418         if (ioctl(ddvd_fdvideo, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_MEMORY) < 0)
419                 perror("VIDEO_SELECT_SOURCE");
420         if (ioctl(ddvd_fdvideo, VIDEO_CLEAR_BUFFER) < 0)
421                 perror("VIDEO_CLEAR_BUFFER");
422         if (ioctl(ddvd_fdvideo, VIDEO_PLAY) < 0)
423                 perror("VIDEO_PLAY");
424
425         if (ioctl(ddvd_fdaudio, AUDIO_SELECT_SOURCE, AUDIO_SOURCE_MEMORY) < 0)
426                 perror("AUDIO_SELECT_SOURCE");
427         if (ioctl(ddvd_fdaudio, AUDIO_CLEAR_BUFFER) < 0)
428                 perror("AUDIO_CLEAR_BUFFER");
429         if (ioctl(ddvd_fdaudio, AUDIO_PLAY) < 0)
430                 perror("AUDIO_PLAY");
431
432 #elif CONFIG_API_VERSION == 3
433         ddvd_output_fd = ddvd_fdvideo = open("/dev/dvb/adapter0/video0", O_RDWR);
434         if (ddvd_fdvideo == -1) {
435                 perror("/dev/dvb/adapter0/video0");
436                 res = DDVD_BUSY;
437                 goto err_open_fdvideo;
438         }
439
440         ddvd_ac3_fd = ddvd_fdaudio = open("/dev/dvb/adapter0/audio0", O_RDWR);
441         if (ddvd_fdaudio == -1) {
442                 perror("/dev/dvb/adapter0/audio0");
443                 res = DDVD_BUSY;
444                 goto err_open_ac3_fd;
445         }
446
447         if (ioctl(ddvd_fdvideo, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_MEMORY) < 0)
448                 perror("VIDEO_SELECT_SOURCE");
449         if (ioctl(ddvd_fdvideo, VIDEO_CLEAR_BUFFER) < 0)
450                 perror("VIDEO_CLEAR_BUFFER");
451         if (ioctl(ddvd_fdvideo, VIDEO_SET_STREAMTYPE, 0) < 0)   // set mpeg2
452                 perror("VIDEO_SET_STREAMTYPE");
453         if (ioctl(ddvd_fdvideo, VIDEO_PLAY) < 0)
454                 perror("VIDEO_PLAY");
455
456         if (ioctl(ddvd_fdaudio, AUDIO_SELECT_SOURCE, AUDIO_SOURCE_MEMORY) < 0)
457                 perror("AUDIO_SELECT_SOURCE");
458         if (ioctl(ddvd_fdaudio, AUDIO_CLEAR_BUFFER) < 0)
459                 perror("AUDIO_CLEAR_BUFFER");
460         if (ioctl(ddvd_fdaudio, AUDIO_PLAY) < 0)
461                 perror("AUDIO_PLAY");
462 #else
463 #error please define CONFIG_API_VERSION to be 1 or 3
464 #endif
465
466 // show startup screen
467 #if SHOW_START_SCREEN == 1
468 #if CONFIG_API_VERSION == 1
469         int i;
470         //that really sucks but there is no other way
471         for (i = 0; i < 10; i++)
472                 safe_write(ddvd_output_fd, ddvd_startup_logo, sizeof(ddvd_startup_logo));
473 #else
474         unsigned char pes_header[] = { 0x00, 0x00, 0x01, 0xE0, 0x00, 0x00, 0x80, 0x00, 0x00 };
475         safe_write(ddvd_output_fd, pes_header, sizeof(pes_header));
476         safe_write(ddvd_output_fd, ddvd_startup_logo, sizeof(ddvd_startup_logo));
477         safe_write(ddvd_output_fd, ddvd_startup_logo, sizeof(ddvd_startup_logo));
478 #endif
479 #endif
480
481         int audio_type = DDVD_AC3;
482
483 #if CONFIG_API_VERSION == 3
484         ddvd_set_pcr_offset();
485 #endif
486
487         uint8_t mem[DVD_VIDEO_LB_LEN];
488         uint8_t *buf = mem;
489         int result, event, len;
490
491         unsigned char lpcm_data[2048 * 6 * 6 /*4608 */ ];
492         int mpa_header_length;
493         int mpa_count, mpa_count2;
494         uint8_t mpa_data[2048 * 4];
495         int ac3_len;
496         int16_t ac3_tmp[2048 * 6 * 6];
497
498         ddvd_mpa_init(48000, 192000);   //init MPA Encoder with 48kHz and 192k Bitrate
499
500         int ac3thru = 1;
501         if (have_liba52) {
502                 state = a52_init(0);    //init AC3 Decoder 
503                 ac3thru = playerconfig->ac3thru;
504         }
505
506         char osdtext[512];
507         strcpy(osdtext, "");
508
509         int tv_aspect = playerconfig->aspect;   //0-> 4:3 lb 1-> 4:3 ps 2-> 16:9 3-> always 16:9
510         int dvd_aspect = 0;     //0-> 4:3 2-> 16:9
511         int dvd_scale_perm = 0;
512         int tv_scale = 0;       //0-> off 1-> letterbox 2-> panscan
513         int spu_active_id = -1;
514         int finished = 0;
515         int audio_id;
516
517         ddvd_trickmode = TOFF;
518         ddvd_trickspeed = 0;
519
520         int rccode;
521         int ismute = 0;
522
523         if (ac3thru) {
524                 if (ioctl(ddvd_fdaudio, AUDIO_SET_AV_SYNC, 1) < 0)
525                         perror("AUDIO_SET_AV_SYNC");
526 #ifdef CONVERT_TO_DVB_COMPLIANT_AC3
527                 if (ioctl(ddvd_fdaudio, AUDIO_SET_BYPASS_MODE, 0) < 0)  // AC3 (dvb compliant)
528 #else
529                 if (ioctl(ddvd_fdaudio, AUDIO_SET_BYPASS_MODE, 3) < 0)  // AC3 VOB
530 #endif
531                         perror("AUDIO_SET_BYPASS_MODE");
532         } else {
533                 if (ioctl(ddvd_fdaudio, AUDIO_SET_AV_SYNC, 1) < 0)
534                         perror("AUDIO_SET_AV_SYNC");
535                 if (ioctl(ddvd_fdaudio, AUDIO_SET_BYPASS_MODE, 1) < 0)
536                         perror("AUDIO_SET_BYPASS_MODE");
537         }
538
539         // set video system
540         int pal_ntsc = playerconfig->tv_system;
541         int saa;
542         if (pal_ntsc == 1)
543                 saa = SAA_NTSC;
544         else
545                 saa = SAA_PAL;
546         int saafd = open("/dev/dbox/saa0", O_RDWR);
547         if (saafd >= 0) {
548                 if (ioctl(saafd, SAAIOSENC, &saa) < 0)
549                         perror("SAAIOSENC");
550                 close(saafd);
551         }
552
553         /* open dvdnav handle */
554         printf("Opening DVD...%s\n", playerconfig->dvd_path);
555         if (dvdnav_open(&dvdnav, playerconfig->dvd_path) != DVDNAV_STATUS_OK) {
556                 printf("Error on dvdnav_open\n");
557                 sprintf(osdtext, "Error: Cant open DVD Source: %s", playerconfig->dvd_path);
558                 msg = DDVD_SHOWOSD_STRING;
559                 safe_write(message_pipe, &msg, sizeof(int));
560                 safe_write(message_pipe, &osdtext, sizeof(osdtext));
561                 res = DDVD_FAIL_OPEN;
562                 goto err_dvdnav_open;
563         }
564
565         /* set read ahead cache usage to no */
566         if (dvdnav_set_readahead_flag(dvdnav, 0) != DVDNAV_STATUS_OK) {
567                 printf("Error on dvdnav_set_readahead_flag: %s\n", dvdnav_err_to_string(dvdnav));
568                 res = DDVD_FAIL_PREFS;
569                 goto err_dvdnav;
570         }
571
572         /* set the language */
573         if (dvdnav_menu_language_select(dvdnav, playerconfig->language) != DVDNAV_STATUS_OK ||
574             dvdnav_audio_language_select(dvdnav, playerconfig->language) != DVDNAV_STATUS_OK ||
575             dvdnav_spu_language_select(dvdnav, playerconfig->language) != DVDNAV_STATUS_OK) {
576                 printf("Error on setting languages: %s\n", dvdnav_err_to_string(dvdnav));
577                 res = DDVD_FAIL_PREFS;
578                 goto err_dvdnav;
579         }
580
581         /* set the PGC positioning flag to have position information relatively to the
582          * whole feature instead of just relatively to the current chapter */
583         if (dvdnav_set_PGC_positioning_flag(dvdnav, 1) != DVDNAV_STATUS_OK) {
584                 printf("Error on dvdnav_set_PGC_positioning_flag: %s\n", dvdnav_err_to_string(dvdnav));
585                 res = DDVD_FAIL_PREFS;
586                 goto err_dvdnav;
587         }
588
589         int audio_lock = 0;
590         int spu_lock = 0;
591         int audio_format[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
592         unsigned long long vpts, apts, spts, pts;
593
594         audio_id = dvdnav_get_active_audio_stream(dvdnav);
595         ddvd_playmode = PLAY;
596
597         ddvd_lbb_changed = 0;
598
599         unsigned long long spu_backpts[3];
600
601         ddvd_play_empty(FALSE);
602         ddvd_get_time();        //set timestamp
603
604         playerconfig->in_menu = 0;
605
606         const char *dvd_titlestring;
607         if (dvdnav_get_title_string(dvdnav, &dvd_titlestring) == DVDNAV_STATUS_OK)
608                 strcpy(playerconfig->title_string, dvd_titlestring);
609
610         msg = DDVD_SHOWOSD_TITLESTRING;
611         safe_write(message_pipe, &msg, sizeof(int));
612
613         /* the read loop which regularly calls dvdnav_get_next_block
614          * and handles the returned events */
615         int reached_eof = 0;
616         int reached_sof = 0;
617         while (!finished) {
618                 pci_t *pci = 0;
619                 dsi_t *dsi = 0;
620                 int buttonN = -1;
621                 int in_menu = 0;
622
623                 /* the main reading function */
624                 if (ddvd_playmode == PLAY) {    //skip when not in play mode
625                         // trickmode
626                         if (ddvd_trickmode) {
627                                 if (ddvd_trick_timer_end <= ddvd_get_time()) {
628                                         if (ddvd_trickmode == FASTBW) { //jump back ?
629                                                 uint32_t pos, len;
630                                                 dvdnav_get_position(dvdnav, &pos, &len);
631                                                 //90000 = 1 Sek. -> 45000 = 0.5 Sek. -> Speed Faktor=2
632                                                 int64_t posneu = ((pos * ddvd_lastCellEventInfo.pgc_length) / len) - (45000 * 2 * ddvd_trickspeed);
633                                                 int64_t posneu2 = posneu <= 0 ? 0 : (posneu * len) / ddvd_lastCellEventInfo.pgc_length;
634                                                 dvdnav_sector_search(dvdnav, posneu2, SEEK_SET);
635                                                 if (posneu < 0) {       // reached begin of movie
636                                                         reached_sof = 1;
637                                                         msg = DDVD_SHOWOSD_TIME;
638                                                 } else {
639                                                         msg = DDVD_SHOWOSD_STATE_FBWD;
640                                                 }
641                                         } else if (ddvd_trickmode == FASTFW) {  //jump forward ?
642                                                 uint32_t pos, len;
643                                                 dvdnav_get_position(dvdnav, &pos, &len);
644                                                 //90000 = 1 Sek. -> 22500 = 0.25 Sek. -> Speed Faktor=2
645                                                 int64_t posneu = ((pos * ddvd_lastCellEventInfo.pgc_length) / len) + (22500 * 2 * ddvd_trickspeed);
646                                                 int64_t posneu2 = (posneu * len) / ddvd_lastCellEventInfo.pgc_length;
647                                                 if (posneu2 && len && posneu2 >= len) { // reached end of movie
648                                                         posneu2 = len - 250;
649                                                         reached_eof = 1;
650                                                         msg = DDVD_SHOWOSD_TIME;
651                                                 } else {
652                                                         msg = DDVD_SHOWOSD_STATE_FFWD;
653                                                 }
654                                                 dvdnav_sector_search(dvdnav, posneu2, SEEK_SET);
655                                         }
656                                         ddvd_trick_timer_end = ddvd_get_time() + 300;
657                                         ddvd_lpcm_count = 0;
658                                 }
659                         }
660
661                         result = dvdnav_get_next_block(dvdnav, buf, &event, &len);
662                         if (result == DVDNAV_STATUS_ERR) {
663                                 printf("Error getting next block: %s\n", dvdnav_err_to_string(dvdnav));
664                                 sprintf(osdtext, "Error: Getting next block: %s", dvdnav_err_to_string(dvdnav));
665                                 msg = DDVD_SHOWOSD_STRING;
666                                 safe_write(message_pipe, &msg, sizeof(int));
667                                 safe_write(message_pipe, &osdtext, sizeof(osdtext));
668                                 res = DDVD_FAIL_READ;
669                                 goto err_dvdnav;
670                         }
671
672 send_message:
673                         // send OSD Data
674                         if (msg > 0) {
675                                 struct ddvd_time info;
676                                 switch (msg) {
677                                 case DDVD_SHOWOSD_TIME:
678                                         info = ddvd_get_osd_time(playerconfig);
679                                         safe_write(message_pipe, &msg, sizeof(int));
680                                         safe_write(message_pipe, &info, sizeof(struct ddvd_time));
681                                         break;
682                                 case DDVD_SHOWOSD_STATE_FFWD:
683                                         info = ddvd_get_osd_time(playerconfig);
684                                         safe_write(message_pipe, &msg, sizeof(int));
685                                         safe_write(message_pipe, &ddvd_trickspeed, sizeof(int));
686                                         safe_write(message_pipe, &info, sizeof(struct ddvd_time));
687                                         break;
688                                 case DDVD_SHOWOSD_STATE_FBWD:
689                                         info = ddvd_get_osd_time(playerconfig);
690                                         safe_write(message_pipe, &msg, sizeof(int));
691                                         safe_write(message_pipe, &ddvd_trickspeed, sizeof(int));
692                                         safe_write(message_pipe, &info, sizeof(struct ddvd_time));
693                                         break;
694                                 default:
695                                         break;
696                                 }
697                                 msg = 0;
698                         }
699
700                         if (reached_eof) {
701                                 msg = DDVD_EOF_REACHED;
702                                 safe_write(message_pipe, &msg, sizeof(int));
703                                 reached_eof = 0;
704                         }
705
706                         if (reached_sof) {
707                                 msg = DDVD_SOF_REACHED;
708                                 safe_write(message_pipe, &msg, sizeof(int));
709                                 reached_sof = 0;
710                         }
711
712                         if (ddvd_get_time() > playerconfig->next_time_update) {
713                                 msg = DDVD_SHOWOSD_TIME;
714                                 goto send_message;
715                         }
716                         // send iFrame
717                         if (ddvd_iframesend < 0) {
718 #if CONFIG_API_VERSION == 1
719                                 ddvd_device_clear();
720 #endif
721                                 ddvd_iframesend = 0;
722                         }
723
724                         if (ddvd_iframesend > 0) {
725 #if CONFIG_API_VERSION == 1
726                                 ddvd_device_clear();
727 #endif
728                                 if (ddvd_still_frame && ddvd_last_iframe_len) {
729 #if 0
730                                         static int ifnum = 0;
731                                         static char ifname[255];
732                                         snprintf(ifname, 255, "/tmp/dvd.iframe.%3.3d.asm.pes", ifnum++);
733                                         FILE *f = fopen(ifname, "wb");
734                                         fwrite(last_iframe, 1, ddvd_last_iframe_len, f);
735                                         fclose(f);
736 #endif
737
738 #if CONFIG_API_VERSION == 1
739                                         //that really sucks but there is no other way
740                                         int i;
741                                         for (i = 0; i < 10; i++)
742                                                 safe_write(ddvd_output_fd, last_iframe, ddvd_last_iframe_len);
743 #else
744                                         safe_write(ddvd_output_fd, last_iframe, ddvd_last_iframe_len);
745 #endif
746                                         //printf("Show iframe with size: %d\n",ddvd_last_iframe_len);
747                                         ddvd_last_iframe_len = 0;
748                                 }
749
750                                 ddvd_iframesend = -1;
751                         }
752                         // wait timer
753                         if (ddvd_wait_timer_active) {
754                                 if (ddvd_wait_timer_end <= ddvd_get_time()) {
755                                         ddvd_wait_timer_active = 0;
756                                         dvdnav_still_skip(dvdnav);
757                                         //printf("wait timer done\n");
758                                 }
759                         }
760                         // SPU timer
761                         if (ddvd_spu_timer_active) {
762                                 if (ddvd_spu_timer_end <= ddvd_get_time()) {
763                                         ddvd_spu_timer_active = 0;
764                                         memset(ddvd_lbb, 0, ddvd_screeninfo_xres * ddvd_screeninfo_yres);       //clear SPU backbuffer
765                                         ddvd_lbb_changed = 1;
766                                         //printf("spu timer done\n");
767                                 }
768                         }
769
770                         switch (event) {
771                         case DVDNAV_BLOCK_OK:
772                                 /* We have received a regular block of the currently playing MPEG stream.
773                                  * So we do some demuxing and decoding. */
774
775                                 // select audio data
776                                 if (((buf[14 + 3]) & 0xF0) == 0xC0)
777                                         audio_format[(buf[14 + 3]) - 0xC0] = DDVD_MPEG;
778                                 if ((buf[14 + 3]) == 0xBD && ((buf[14 + buf[14 + 8] + 9]) & 0xF8) == 0x80)
779                                         audio_format[(buf[14 + buf[14 + 8] + 9]) - 0x80] = DDVD_AC3;
780                                 if ((buf[14 + 3]) == 0xBD && ((buf[14 + buf[14 + 8] + 9]) & 0xF8) == 0x88)
781                                         audio_format[(buf[14 + buf[14 + 8] + 9]) - 0x88] = DDVD_DTS;
782                                 if ((buf[14 + 3]) == 0xBD && ((buf[14 + buf[14 + 8] + 9]) & 0xF8) == 0xA0)
783                                         audio_format[(buf[14 + buf[14 + 8] + 9]) - 0xA0] = DDVD_LPCM;
784
785                                 if ((buf[14 + 3] & 0xF0) == 0xE0) {     // video
786                                         if (buf[14 + 7] & 128) {
787                                                 /* damn gcc bug */
788                                                 vpts = ((unsigned long long)(((buf[14 + 9] >> 1) & 7))) << 30;
789                                                 vpts |= buf[14 + 10] << 22;
790                                                 vpts |= (buf[14 + 11] >> 1) << 15;
791                                                 vpts |= buf[14 + 12] << 7;
792                                                 vpts |= (buf[14 + 14] >> 1);
793                                                 //printf("VPTS? %X\n",(int)vpts);
794                                         }
795 #if CONFIG_API_VERSION == 1
796                                         // Eliminate 00 00 01 B4 sequence error packet because it breaks the pallas mpeg decoder
797                                         // This is very strange because the 00 00 01 B4 is partly inside the header extension ...
798                                         if (buf[21] == 0x00 && buf[22] == 0x00 && buf[23] == 0x01 && buf[24] == 0xB4) {
799                                                 buf[21] = 0x01;
800                                         }
801                                         if (buf[22] == 0x00 && buf[23] == 0x00 && buf[24] == 0x01 && buf[25] == 0xB4) {
802                                                 buf[22] = 0x01;
803                                         }
804 #endif
805                                         // If DVD Aspect is 16:9 Zoom (3) and TV is 16:9 then patch the mpeg stream
806                                         // up to 16:9 anamorph because the decoder dont support the 16:9 zoom mode
807                                         if (dvd_aspect == 3 && tv_aspect >= 2) {
808                                                 if (buf[33] == 0 && buf[33 + 1] == 0 && buf[33 + 2] == 1 && buf[33 + 3] == 0xB3) {
809                                                         buf[33 + 7] = (buf[33 + 7] & 0xF) + 0x30;
810                                                 }
811                                                 if (buf[36] == 0 && buf[36 + 1] == 0 && buf[36 + 2] == 1 && buf[36 + 3] == 0xB3) {
812                                                         buf[36 + 7] = (buf[36 + 7] & 0xF) + 0x30;
813                                                 }
814                                         }
815
816                                         safe_write(ddvd_output_fd, buf + 14, 2048 - 14);
817
818                                         // 14+8 header_length
819                                         // 14+(header_length)+3  -> start mpeg header
820                                         // buf[14+buf[14+8]+3] start mpeg header
821
822                                         int datalen = (buf[19] + (buf[18] << 8) + 6) - buf[14 + 8];     // length mpeg packet
823                                         int data = buf[14 + buf[14 + 8] + 3];   // start mpeg packet(header)
824
825                                         int do_copy = (ddvd_iframerun == 0x01) && !(buf[data] == 0 && buf[data + 1] == 0 && buf[data + 2] == 1) ? 1 : 0;
826                                         int have_pictureheader = 0;
827                                         int haveslice = 0;
828                                         int setrun = 0;
829
830                                         while (datalen > 6) {
831                                                 if (buf[data] == 0 && buf[data + 1] == 0 && buf[data + 2] == 1) {
832                                                         if (buf[data + 3] == 0x00)      //picture
833                                                         {
834                                                                 if (!setrun) {
835                                                                         ddvd_iframerun = ((buf[data + 5] >> 3) & 0x07);
836                                                                         setrun = 1;
837                                                                 }
838
839                                                                 if (ddvd_iframerun < 0x01 || 0x03 < ddvd_iframerun) {
840                                                                         data++;
841                                                                         datalen--;
842                                                                         continue;
843                                                                 }
844                                                                 have_pictureheader = 1;
845                                                                 data += 5;
846                                                                 datalen -= 5;
847                                                                 datalen = 6;
848                                                         } else if (buf[data + 3] == 0xB3 && datalen >= 8)       //sequence header
849                                                         {
850                                                                 ddvd_last_iframe_len = 0;       // clear iframe buffer
851                                                                 data += 7;
852                                                                 datalen -= 7;
853                                                         } else if (buf[data + 3] == 0xBE)       //padding stream
854                                                         {
855                                                                 break;
856                                                         } else if (0x01 <= buf[data + 3] && buf[data + 3] <= 0xaf)      //slice ?
857                                                         {
858                                                                 if (!have_pictureheader && ddvd_last_iframe_len == 0)
859                                                                         haveslice = 1;
860                                                         }
861                                                 }
862                                                 data++;
863                                                 datalen--;
864                                         }
865                                         if ((ddvd_iframerun <= 0x01 || do_copy) && ddvd_still_frame) {
866                                                 if (haveslice)
867                                                         ddvd_iframerun = 0xFF;
868                                                 else if (ddvd_last_iframe_len < (320 * 1024) - (buf[19] + (buf[18] << 8) + 6)) {
869                                                         memcpy(last_iframe + ddvd_last_iframe_len, buf + 14, buf[19] + (buf[18] << 8) + 6);
870                                                         ddvd_last_iframe_len += buf[19] + (buf[18] << 8) + 6;
871                                                 }
872                                         }
873                                 } else if ((buf[14 + 3]) == 0xC0 + audio_id)    // mpeg audio
874                                 {
875                                         if (audio_type != DDVD_MPEG) {
876                                                 //printf("Switch to MPEG Audio\n");
877                                                 if (ioctl(ddvd_fdaudio, AUDIO_SET_AV_SYNC, 1) < 0)
878                                                         perror("AUDIO_SET_AV_SYNC");
879                                                 if (ioctl(ddvd_fdaudio, AUDIO_SET_BYPASS_MODE, 1) < 0)
880                                                         perror("AUDIO_SET_BYPASS_MODE");
881                                                 audio_type = DDVD_MPEG;
882                                         }
883
884                                         if (buf[14 + 7] & 128) {
885                                                 /* damn gcc bug */
886                                                 apts = ((unsigned long long)(((buf[14 + 9] >> 1) & 7))) << 30;
887                                                 apts |= buf[14 + 10] << 22;
888                                                 apts |= (buf[14 + 11] >> 1) << 15;
889                                                 apts |= buf[14 + 12] << 7;
890                                                 apts |= (buf[14 + 14] >> 1);
891                                                 //printf("APTS? %X\n",(int)apts);
892                                         }
893
894                                         safe_write(ddvd_ac3_fd, buf + 14, buf[19] + (buf[18] << 8) + 6);
895                                 } else if ((buf[14 + 3]) == 0xBD && (buf[14 + buf[14 + 8] + 9]) == 0xA0 + audio_id)     // lpcm audio
896                                 {
897                                         if (audio_type != DDVD_LPCM) {
898                                                 //printf("Switch to LPCM Audio\n");
899                                                 if (ioctl(ddvd_fdaudio, AUDIO_SET_AV_SYNC, 1) < 0)
900                                                         perror("AUDIO_SET_AV_SYNC");
901                                                 if (ioctl(ddvd_fdaudio, AUDIO_SET_BYPASS_MODE, 1) < 0)
902                                                         perror("AUDIO_SET_BYPASS_MODE");
903                                                 audio_type = DDVD_LPCM;
904                                                 ddvd_lpcm_count = 0;
905                                         }
906                                         if (buf[14 + 7] & 128) {
907                                                 /* damn gcc bug */
908                                                 apts = ((unsigned long long)(((buf[14 + 9] >> 1) & 7))) << 30;
909                                                 apts |= buf[14 + 10] << 22;
910                                                 apts |= (buf[14 + 11] >> 1) << 15;
911                                                 apts |= buf[14 + 12] << 7;
912                                                 apts |= (buf[14 + 14] >> 1);
913                                                 //printf("APTS? %X\n",(int)apts);
914                                         }
915                                         int i = 0;
916                                         char abuf[(((buf[18] << 8) | buf[19]) - buf[22] - 14)];
917 #if BYTE_ORDER == BIG_ENDIAN
918                                         // just copy, byte order is correct on ppc machines
919                                         memcpy(abuf, buf + 14 + buf[14 + 8] + 9 + 7, (((buf[18] << 8) | buf[19]) - buf[22] - 14));
920                                         i = (((buf[18] << 8) | buf[19]) - buf[22] - 14);
921 #else
922                                         // byte swapping .. we become the wrong byteorder on lpcm on the 7025
923                                         while (i < (((buf[18] << 8) | buf[19]) - buf[22] - 14)) {
924                                                 abuf[i + 0] = (buf[14 + buf[14 + 8] + 9 + 7 + i + 1]);
925                                                 abuf[i + 1] = (buf[14 + buf[14 + 8] + 9 + 7 + i + 0]);
926                                                 i += 2;
927                                         }
928 #endif
929                                         // we will encode the raw lpcm data to mpeg audio and send them with pts
930                                         // information to the decoder to get a sync. playing the pcm data via
931                                         // oss will break the pic/sound sync. So believe it or not, this is the 
932                                         // smartest way to get a synced lpcm track ;-)
933                                         if (ddvd_lpcm_count == 0) {     // save mpeg header with pts
934                                                 memcpy(mpa_data, buf + 14, buf[14 + 8] + 9);
935                                                 mpa_header_length = buf[14 + 8] + 9;
936                                         }
937                                         if (ddvd_lpcm_count + i >= 4608) {      //we have to send 4608 bytes to the encoder
938                                                 memcpy(lpcm_data + ddvd_lpcm_count, abuf, 4608 - ddvd_lpcm_count);
939                                                 //encode
940                                                 mpa_count = ddvd_mpa_encode_frame(mpa_data + mpa_header_length, 4608, lpcm_data);
941                                                 //patch pes__packet_length
942                                                 mpa_count = mpa_count + mpa_header_length - 6;
943                                                 mpa_data[4] = mpa_count >> 8;
944                                                 mpa_data[5] = mpa_count & 0xFF;
945                                                 //patch header type to mpeg
946                                                 mpa_data[3] = 0xC0;
947                                                 //write
948                                                 safe_write(ddvd_ac3_fd, mpa_data, mpa_count + mpa_header_length);
949                                                 memcpy(lpcm_data, abuf + (4608 - ddvd_lpcm_count), i - (4608 - ddvd_lpcm_count));
950                                                 ddvd_lpcm_count = i - (4608 - ddvd_lpcm_count);
951                                                 memcpy(mpa_data, buf + 14, buf[14 + 8] + 9);
952                                                 mpa_header_length = buf[14 + 8] + 9;
953                                         } else {
954                                                 memcpy(lpcm_data + ddvd_lpcm_count, abuf, i);
955                                                 ddvd_lpcm_count += i;
956                                         }
957
958                                         //safe_write(ddvd_ac3_fd, buf+14 , buf[19]+(buf[18]<<8)+6);
959                                 } else if ((buf[14 + 3]) == 0xBD && (buf[14 + buf[14 + 8] + 9]) == 0x88 + audio_id) {   // dts audio
960                                         if (audio_type != DDVD_DTS) {
961                                                 //printf("Switch to DTS Audio (thru)\n");
962                                                 if (ioctl(ddvd_fdaudio, AUDIO_SET_AV_SYNC, 1) < 0)
963                                                         perror("AUDIO_SET_AV_SYNC");
964                                                 if (ioctl(ddvd_fdaudio, AUDIO_SET_BYPASS_MODE, 5) < 0)
965                                                         perror("AUDIO_SET_BYPASS_MODE");
966                                                 audio_type = DDVD_DTS;
967                                         }
968
969                                         if (buf[14 + 7] & 128) {
970                                                 /* damn gcc bug */
971                                                 apts = ((unsigned long long)(((buf[14 + 9] >> 1) & 7))) << 30;
972                                                 apts |= buf[14 + 10] << 22;
973                                                 apts |= (buf[14 + 11] >> 1) << 15;
974                                                 apts |= buf[14 + 12] << 7;
975                                                 apts |= (buf[14 + 14] >> 1);
976                                                 //printf("APTS? %X\n",(int)apts);
977                                         }
978
979                                         safe_write(ddvd_ac3_fd, buf + 14, buf[19] + (buf[18] << 8) + 6);        // not working yet ....
980                                 } else if ((buf[14 + 3]) == 0xBD && (buf[14 + buf[14 + 8] + 9]) == 0x80 + audio_id) {   // ac3 audio
981                                         if (audio_type != DDVD_AC3) {
982                                                 //printf("Switch to AC3 Audio\n");
983                                                 if (ac3thru || !have_liba52) {  // !have_liba52 and !ac3thru should never happen, but who knows ;)
984                                                         if (ioctl(ddvd_fdaudio, AUDIO_SET_AV_SYNC, 1) < 0)
985                                                                 perror("AUDIO_SET_AV_SYNC");
986 #ifdef CONVERT_TO_DVB_COMPLIANT_AC3
987                                                         if (ioctl(ddvd_fdaudio, AUDIO_SET_BYPASS_MODE, 0) < 0)  // AC3 (dvb compliant)
988 #else
989                                                         if (ioctl(ddvd_fdaudio, AUDIO_SET_BYPASS_MODE, 3) < 0)  // AC3 VOB
990 #endif
991                                                                 perror("AUDIO_SET_BYPASS_MODE");
992                                                 } else {
993                                                         if (ioctl(ddvd_fdaudio, AUDIO_SET_AV_SYNC, 1) < 0)
994                                                                 perror("AUDIO_SET_AV_SYNC");
995                                                         if (ioctl(ddvd_fdaudio, AUDIO_SET_BYPASS_MODE, 1) < 0)
996                                                                 perror("AUDIO_SET_BYPASS_MODE");
997                                                 }
998                                                 audio_type = DDVD_AC3;
999                                         }
1000
1001                                         if (buf[14 + 7] & 128) {
1002                                                 /* damn gcc bug */
1003                                                 apts = ((unsigned long long)(((buf[14 + 9] >> 1) & 7))) << 30;
1004                                                 apts |= buf[14 + 10] << 22;
1005                                                 apts |= (buf[14 + 11] >> 1) << 15;
1006                                                 apts |= buf[14 + 12] << 7;
1007                                                 apts |= (buf[14 + 14] >> 1);
1008                                                 //printf("APTS? %X\n",(int)apts);
1009                                         }
1010
1011                                         if (ac3thru || !have_liba52) {  // !have_liba52 and !ac3thru should never happen, but who knows ;)
1012 #ifdef CONVERT_TO_DVB_COMPLIANT_AC3
1013                                                 unsigned short pes_len = (buf[14 + 4] << 8 | buf[14 + 5]);
1014                                                 pes_len -= 4;   // strip first 4 bytes of pes payload
1015                                                 buf[14 + 4] = pes_len >> 8;     // patch pes len
1016                                                 buf[15 + 4] = pes_len & 0xFF;
1017
1018                                                 safe_write(ddvd_ac3_fd, buf + 14, 9 + buf[14 + 8]);     // write pes_header
1019                                                 safe_write(ddvd_ac3_fd, buf + 14 + 9 + buf[14 + 8] + 4, pes_len - (3 + buf[14 + 8]));   // write pes_payload
1020 #else
1021                                                 safe_write(ddvd_ac3_fd, buf + 14, buf[19] + (buf[18] << 8) + 6);
1022 #endif
1023                                                 //fwrite(buf+buf[22]+27, 1, ((buf[18]<<8)|buf[19])-buf[22]-7, fac3); //debugwrite
1024                                         } else {
1025                                                 // a bit more funny than lpcm sound, because we do a complete recoding here
1026                                                 // we will decode the ac3 data to plain lpcm and will then encode to mpeg
1027                                                 // audio and send them with pts information to the decoder to get a sync.
1028
1029                                                 // decode and convert ac3 to raw lpcm
1030                                                 ac3_len = ddvd_ac3_decode(buf + buf[22] + 27, ((buf[18] << 8) | buf[19]) - buf[22] - 7, ac3_tmp);
1031
1032                                                 // save the pes header incl. PTS
1033                                                 memcpy(mpa_data, buf + 14, buf[14 + 8] + 9);
1034                                                 mpa_header_length = buf[14 + 8] + 9;
1035
1036                                                 //apts-=(((unsigned long long)(ddvd_lpcm_count)*90)/192);
1037
1038                                                 //mpa_data[14]=(int)((apts<<1)&0xFF);
1039                                                 //mpa_data[12]=(int)((apts>>7)&0xFF);
1040                                                 //mpa_data[11]=(int)(((apts<<1)>>15)&0xFF);
1041                                                 //mpa_data[10]=(int)((apts>>22)&0xFF);
1042
1043                                                 // copy lpcm data into buffer for encoding
1044                                                 memcpy(lpcm_data + ddvd_lpcm_count, ac3_tmp, ac3_len);
1045                                                 ddvd_lpcm_count += ac3_len;
1046
1047                                                 // encode the whole packet to mpa
1048                                                 mpa_count2 = mpa_count = 0;
1049                                                 while (ddvd_lpcm_count >= 4608) {
1050                                                         mpa_count = ddvd_mpa_encode_frame(mpa_data + mpa_header_length + mpa_count2, 4608, lpcm_data);
1051                                                         mpa_count2 += mpa_count;
1052                                                         ddvd_lpcm_count -= 4608;
1053                                                         memcpy(lpcm_data, lpcm_data + 4608, ddvd_lpcm_count);
1054                                                 }
1055
1056                                                 // patch pes__packet_length
1057                                                 mpa_count = mpa_count2 + mpa_header_length - 6;
1058                                                 mpa_data[4] = mpa_count >> 8;
1059                                                 mpa_data[5] = mpa_count & 0xFF;
1060
1061                                                 // patch header type to mpeg
1062                                                 mpa_data[3] = 0xC0;
1063
1064                                                 // write to decoder
1065                                                 safe_write(ddvd_ac3_fd, mpa_data, mpa_count2 + mpa_header_length);
1066
1067                                         }
1068                                 } else if ((buf[14 + 3]) == 0xBD && ((buf[14 + buf[14 + 8] + 9]) & 0xE0) == 0x20 && ((buf[14 + buf[14 + 8] + 9]) & 0x1F) == spu_active_id) {    // SPU packet
1069                                         memcpy(spu_buffer + ddvd_spu_ptr, buf + buf[22] + 14 + 10, 2048 - (buf[22] + 14 + 10));
1070                                         ddvd_spu_ptr += 2048 - (buf[22] + 14 + 10);
1071
1072                                         if (buf[14 + 7] & 128) {
1073                                                 /* damn gcc bug */
1074 #if CONFIG_API_VERSION == 3
1075                                                 spts = ((unsigned long long)(((buf[14 + 9] >> 1) & 7))) << 30;
1076                                                 spts |= buf[14 + 10] << 22;
1077                                                 spts |= (buf[14 + 11] >> 1) << 15;
1078                                                 spts |= buf[14 + 12] << 7;
1079                                                 spts |= (buf[14 + 14] >> 1);
1080 #else
1081                                                 spts = (buf[14 + 9] >> 1) << 29;        // need a corrected "spts" because vulcan/pallas will give us a 32bit pts instead of 33bit
1082                                                 spts |= buf[14 + 10] << 21;
1083                                                 spts |= (buf[14 + 11] >> 1) << 14;
1084                                                 spts |= buf[14 + 12] << 6;
1085                                                 spts |= buf[14 + 12] >> 2;
1086 #endif
1087                                                 //printf("SPTS? %X\n",(int)spts);
1088                                         }
1089
1090                                         if (ddvd_spu_ptr >= (spu_buffer[0] << 8 | spu_buffer[1]))       // SPU packet complete ?
1091                                         {
1092                                                 if (ddvd_spu_backnr == 3)       // backbuffer already full ?
1093                                                 {
1094                                                         int tmplen = (spu_backbuffer[0] << 8 | spu_backbuffer[1]);
1095                                                         memcpy(spu_backbuffer, spu_backbuffer + tmplen, ddvd_spu_backptr - tmplen);     // delete oldest SPU packet
1096                                                         spu_backpts[0] = spu_backpts[1];
1097                                                         spu_backpts[1] = spu_backpts[2];
1098                                                         ddvd_spu_backnr = 2;
1099                                                         ddvd_spu_backptr -= tmplen;
1100                                                 }
1101
1102                                                 memcpy(spu_backbuffer + ddvd_spu_backptr, spu_buffer, (spu_buffer[0] << 8 | spu_buffer[1]));    // copy into backbuffer
1103                                                 spu_backpts[ddvd_spu_backnr++] = spts;  // store pts
1104                                                 ddvd_spu_backptr += (spu_buffer[0] << 8 | spu_buffer[1]);       // increase ptr
1105
1106                                                 ddvd_spu_ptr = 0;
1107                                         }
1108                                 }
1109                                 break;
1110
1111                         case DVDNAV_NOP:
1112                                 /* Nothing to do here. */
1113                                 break;
1114
1115                         case DVDNAV_STILL_FRAME:
1116                                 /* We have reached a still frame. So we start a timer to wait
1117                                  * the amount of time specified by the still's length while still handling
1118                                  * user input to make menus and other interactive stills work.
1119                                  * A length of 0xff means an indefinite still which has to be skipped
1120                                  * indirectly by some user interaction. */
1121                                 {
1122                                         if (ddvd_iframesend == 0 && ddvd_last_iframe_len)
1123                                                 ddvd_iframesend = 1;
1124
1125                                         dvdnav_still_event_t *still_event = (dvdnav_still_event_t *) buf;
1126                                         if (still_event->length < 0xff) {
1127                                                 if (!ddvd_wait_timer_active) {
1128                                                         ddvd_wait_timer_active = 1;
1129                                                         ddvd_wait_timer_end = ddvd_get_time() + (still_event->length * 1000);   //ms
1130                                                 }
1131                                         } else
1132                                                 ddvd_wait_for_user = 1;
1133                                 }
1134                                 break;
1135
1136                         case DVDNAV_WAIT:
1137                                 /* We have reached a point in DVD playback, where timing is critical.
1138                                  * We dont use readahead, so we can simply skip the wait state. */
1139                                 dvdnav_wait_skip(dvdnav);
1140                                 break;
1141
1142                         case DVDNAV_SPU_CLUT_CHANGE:
1143                                 /* We received a new color lookup table so we read and store
1144                                  * it */
1145                                 {
1146                                         int i = 0, i2 = 0;
1147                                         uint8_t pal[16 * 4];
1148 #if BYTE_ORDER == BIG_ENDIAN
1149                                         memcpy(pal, buf, 16 * sizeof(uint32_t));
1150 #else
1151                                         for (; i < 16; ++i)
1152                                                 *(int *)(pal + i * 4) = htonl(*(int *)(buf + i * 4));
1153                                         i = 0;
1154 #endif
1155                                         while (i < 16 * 4) {
1156                                                 int y = buf[i + 1];
1157                                                 signed char cr = buf[i + 2];    //v
1158                                                 signed char cb = buf[i + 3];    //u
1159                                                 //printf("%d %d %d ->", y, cr, cb);
1160                                                 y = pal[i + 1];
1161                                                 cr = pal[i + 2];        //v
1162                                                 cb = pal[i + 3];        //u
1163                                                 //printf(" %d %d %d\n", y, cr, cb);
1164                                                 i += 4;
1165                                         }
1166                                         i = 0;
1167
1168                                         while (i < 16 * 4) {
1169                                                 int y = pal[i + 1];
1170                                                 signed char cr = pal[i + 2];    //v
1171                                                 signed char cb = pal[i + 3];    //u
1172
1173                                                 y = 76310 * (y - 16);   //yuv2rgb
1174                                                 cr -= 128;
1175                                                 cb -= 128;
1176                                                 int r = CLAMP((y + 104635 * cr) >> 16);
1177                                                 int g = CLAMP((y - 53294 * cr - 25690 * cb) >> 16);
1178                                                 int b = CLAMP((y + 132278 * cb) >> 16);
1179
1180                                                 ddvd_bl[i2] = b << 8;
1181                                                 ddvd_gn[i2] = g << 8;
1182                                                 ddvd_rd[i2] = r << 8;
1183                                                 i += 4;
1184                                                 i2++;
1185                                         }
1186                                         //CHANGE COLORMAP
1187                                 }
1188                                 break;
1189
1190                         case DVDNAV_SPU_STREAM_CHANGE:
1191                                 /* We received a new SPU stream ID */
1192                                 if (spu_lock)
1193                                         break;
1194
1195                                 dvdnav_spu_stream_change_event_t *ev = (dvdnav_spu_stream_change_event_t *) buf;
1196                                 switch (tv_scale) {
1197                                 case 0: //off
1198                                         spu_active_id = ev->physical_wide;
1199                                         break;
1200                                 case 1: //letterbox
1201                                         spu_active_id = ev->physical_letterbox;
1202                                         break;
1203                                 case 2: //panscan
1204                                         spu_active_id = ev->physical_pan_scan;
1205                                         break;
1206                                 default:        // should not happen
1207                                         spu_active_id = ev->physical_wide;
1208                                         break;
1209                                 }
1210                                 //printf("SPU Stream change: w %X l: %X p: %X active: %X\n",ev->physical_wide,ev->physical_letterbox,ev->physical_pan_scan,spu_active_id);
1211                                 break;
1212
1213                         case DVDNAV_AUDIO_STREAM_CHANGE:
1214                                 /* We received a new Audio stream ID  */
1215                                 if (!audio_lock)
1216                                         audio_id = dvdnav_get_active_audio_stream(dvdnav);
1217                                 break;
1218
1219                         case DVDNAV_HIGHLIGHT:
1220                                 /* Lets display some Buttons */
1221                                 if (ddvd_clear_buttons == 0) {
1222                                         dvdnav_highlight_event_t *highlight_event = (dvdnav_highlight_event_t *) buf;
1223
1224                                         pci = dvdnav_get_current_nav_pci(dvdnav);
1225                                         dsi = dvdnav_get_current_nav_dsi(dvdnav);
1226                                         dvdnav_highlight_area_t hl;
1227
1228                                         int libdvdnav_workaround = 0;
1229
1230                                         if (pci->hli.hl_gi.btngr_ns) {
1231                                                 int btns_per_group = 36 / pci->hli.hl_gi.btngr_ns;
1232                                                 btni_t *btni = NULL;
1233                                                 int modeMask = 1 << tv_scale;
1234
1235                                                 if (!btni && pci->hli.hl_gi.btngr_ns >= 1 && (pci->hli.hl_gi.btngr1_dsp_ty & modeMask)) {
1236                                                         btni = &pci->hli.btnit[0 * btns_per_group + highlight_event->buttonN - 1];
1237                                                 }
1238                                                 if (!btni && pci->hli.hl_gi.btngr_ns >= 2 && (pci->hli.hl_gi.btngr2_dsp_ty & modeMask)) {
1239                                                         btni = &pci->hli.btnit[1 * btns_per_group + highlight_event->buttonN - 1];
1240                                                 }
1241                                                 if (!btni && pci->hli.hl_gi.btngr_ns >= 3 && (pci->hli.hl_gi.btngr3_dsp_ty & modeMask)) {
1242                                                         btni = &pci->hli.btnit[2 * btns_per_group + highlight_event->buttonN - 1];
1243                                                 }
1244
1245                                                 if (btni && btni->btn_coln != 0) {
1246                                                         // get and set clut for actual button
1247                                                         unsigned char tmp, tmp2;
1248                                                         struct ddvd_color colneu;
1249                                                         int i;
1250                                                         msg = DDVD_COLORTABLE_UPDATE;
1251                                                         if (ddvd_screeninfo_bypp == 1)
1252                                                                 safe_write(message_pipe, &msg, sizeof(int));
1253                                                         for (i = 0; i < 4; i++) {
1254                                                                 tmp = ((pci->hli.btn_colit.btn_coli[btni->btn_coln - 1][0]) >> (16 + 4 * i)) & 0xf;
1255                                                                 tmp2 = ((pci->hli.btn_colit.btn_coli[btni->btn_coln - 1][0]) >> (4 * i)) & 0xf;
1256                                                                 colneu.blue = ddvd_bl[i + 252] = ddvd_bl[tmp];
1257                                                                 colneu.green = ddvd_gn[i + 252] = ddvd_gn[tmp];
1258                                                                 colneu.red = ddvd_rd[i + 252] = ddvd_rd[tmp];
1259                                                                 colneu.trans = ddvd_tr[i + 252] = (0xF - tmp2) * 0x1111;
1260                                                                 if (ddvd_screeninfo_bypp == 1)
1261                                                                         safe_write(message_pipe, &colneu, sizeof(struct ddvd_color));
1262                                                         }
1263                                                         msg = DDVD_NULL;
1264                                                         //CHANGE COLORMAP
1265
1266                                                         memset(p_lfb, 0, ddvd_screeninfo_stride * ddvd_screeninfo_yres);        //clear screen ..
1267                                                         //copy button into screen
1268                                                         for (i = btni->y_start; i < btni->y_end; i++) {
1269                                                                 if (ddvd_screeninfo_bypp == 1)
1270                                                                         memcpy(p_lfb + btni->x_start + ddvd_screeninfo_xres * (i),
1271                                                                                ddvd_lbb + btni->x_start + ddvd_screeninfo_xres * (i),
1272                                                                                btni->x_end - btni->x_start);
1273                                                                 else
1274                                                                         ddvd_blit_to_argb(p_lfb + btni->x_start * ddvd_screeninfo_bypp +
1275                                                                                           ddvd_screeninfo_stride * i,
1276                                                                                           ddvd_lbb + btni->x_start + ddvd_screeninfo_xres * (i),
1277                                                                                           btni->x_end - btni->x_start);
1278                                                         }
1279
1280                                                         libdvdnav_workaround = 1;
1281                                                 }
1282                                         }
1283                                         if (!libdvdnav_workaround && dvdnav_get_highlight_area(pci, highlight_event->buttonN, 0, &hl) == DVDNAV_STATUS_OK) {
1284                                                 // get and set clut for actual button
1285                                                 unsigned char tmp, tmp2;
1286                                                 struct ddvd_color colneu;
1287                                                 int i;
1288                                                 msg = DDVD_COLORTABLE_UPDATE;
1289                                                 if (ddvd_screeninfo_bypp == 1)
1290                                                         safe_write(message_pipe, &msg, sizeof(int));
1291                                                 for (i = 0; i < 4; i++) {
1292                                                         tmp = ((hl.palette) >> (16 + 4 * i)) & 0xf;
1293                                                         tmp2 = ((hl.palette) >> (4 * i)) & 0xf;
1294                                                         colneu.blue = ddvd_bl[i + 252] = ddvd_bl[tmp];
1295                                                         colneu.green = ddvd_gn[i + 252] = ddvd_gn[tmp];
1296                                                         colneu.red = ddvd_rd[i + 252] = ddvd_rd[tmp];
1297                                                         colneu.trans = ddvd_tr[i + 252] = (0xF - tmp2) * 0x1111;
1298                                                         if (ddvd_screeninfo_bypp == 1)
1299                                                                 safe_write(message_pipe, &colneu, sizeof(struct ddvd_color));
1300                                                 }
1301                                                 msg = DDVD_NULL;
1302                                                 //CHANGE COLORMAP
1303
1304                                                 memset(p_lfb, 0, ddvd_screeninfo_stride * ddvd_screeninfo_yres);        //clear screen ..
1305                                                 //copy button into screen
1306                                                 for (i = hl.sy; i < hl.ey; i++) {
1307                                                         if (ddvd_screeninfo_bypp == 1)
1308                                                                 memcpy(p_lfb + hl.sx + ddvd_screeninfo_xres * (i),
1309                                                                        ddvd_lbb + hl.sx + ddvd_screeninfo_xres * (i), hl.ex - hl.sx);
1310                                                         else
1311                                                                 ddvd_blit_to_argb(p_lfb + hl.sx * ddvd_screeninfo_bypp + ddvd_screeninfo_stride * i,
1312                                                                                   ddvd_lbb + hl.sx + ddvd_screeninfo_xres * (i), hl.ex - hl.sx);
1313                                                 }
1314                                                 libdvdnav_workaround = 1;
1315                                         }
1316                                         if (!libdvdnav_workaround)
1317                                                 memset(p_lfb, 0, ddvd_screeninfo_stride * ddvd_screeninfo_yres);        //clear screen .. 
1318
1319                                         msg = DDVD_SCREEN_UPDATE;
1320                                         safe_write(message_pipe, &msg, sizeof(int));
1321                                 } else {
1322                                         ddvd_clear_buttons = 0;
1323                                         //printf("clear buttons\n");
1324                                 }
1325                                 break;
1326
1327                         case DVDNAV_VTS_CHANGE:
1328                                 /* Some status information like video aspect and video scale permissions do
1329                                  * not change inside a VTS. Therefore we will set it new at this place */
1330                                 ddvd_play_empty(TRUE);
1331                                 audio_lock = 0; // reset audio & spu lock
1332                                 spu_lock = 0;
1333                                 audio_format[0] = audio_format[1] = audio_format[2] = audio_format[4] = audio_format[4] = audio_format[5] = audio_format[6] = audio_format[7] = -1;
1334
1335                                 dvd_aspect = dvdnav_get_video_aspect(dvdnav);
1336                                 dvd_scale_perm = dvdnav_get_video_scale_permission(dvdnav);
1337                                 tv_scale = ddvd_check_aspect(dvd_aspect, dvd_scale_perm, tv_aspect);
1338                                 //printf("DVD Aspect: %d TV Aspect: %d Scale: %d Allowed: %d\n",dvd_aspect,tv_aspect,tv_scale,dvd_scale_perm);
1339                                 break;
1340
1341                         case DVDNAV_CELL_CHANGE:
1342                                 /* Store new cell information */
1343                                 memcpy(&ddvd_lastCellEventInfo, buf, sizeof(dvdnav_cell_change_event_t));
1344
1345                                 if ((ddvd_still_frame & CELL_STILL) && ddvd_iframesend == 0 && ddvd_last_iframe_len)
1346                                         ddvd_iframesend = 1;
1347
1348                                 ddvd_still_frame = (dvdnav_get_next_still_flag(dvdnav) != 0) ? CELL_STILL : 0;
1349                                 break;
1350
1351                         case DVDNAV_NAV_PACKET:
1352                                 /* A NAV packet provides PTS discontinuity information, angle linking information and
1353                                  * button definitions for DVD menus. We have to handle some stilframes here */
1354                                 pci = dvdnav_get_current_nav_pci(dvdnav);
1355                                 dsi = dvdnav_get_current_nav_dsi(dvdnav);
1356
1357                                 if ((ddvd_still_frame & NAV_STILL) && ddvd_iframesend == 0 && ddvd_last_iframe_len)
1358                                         ddvd_iframesend = 1;
1359
1360                                 if (dsi->vobu_sri.next_video == 0xbfffffff)
1361                                         ddvd_still_frame |= NAV_STILL;  //|= 1;
1362                                 else
1363                                         ddvd_still_frame &= ~NAV_STILL; //&= 1;
1364                                 break;
1365
1366                         case DVDNAV_HOP_CHANNEL:
1367                                 /* This event is issued whenever a non-seamless operation has been executed.
1368                                  * So we drop our buffers */
1369                                 ddvd_play_empty(TRUE);
1370                                 break;
1371
1372                         case DVDNAV_STOP:
1373                                 /* Playback should end here. */
1374                                 printf("DVDNAV_STOP\n");
1375                                 finished = 1;
1376                                 break;
1377
1378                         default:
1379                                 printf("Unknown event (%i)\n", event);
1380                                 finished = 1;
1381                                 break;
1382                         }
1383                 }
1384                 // spu handling
1385 #if CONFIG_API_VERSION == 1
1386                 unsigned int tpts;
1387                 if (ioctl(ddvd_output_fd, VIDEO_GET_PTS, &tpts) < 0)
1388                         perror("VIDEO_GET_PTS");
1389                 pts = (unsigned long long)tpts;
1390                 signed long long diff = spts - pts;
1391                 if (ddvd_spu_backnr > 0 && diff <= 0xFF)        // we only have a 32bit pts on vulcan/pallas (instead of 33bit) so we need some tolerance on syncing SPU for menus
1392                         // so on non animated menus the buttons will be displayed to soon, but we we have to accept it
1393 #else
1394                 if (ioctl(ddvd_fdvideo, VIDEO_GET_PTS, &pts) < 0)
1395                         perror("VIDEO_GET_PTS");
1396                 if (ddvd_spu_backnr > 0 && pts >= spu_backpts[0])
1397 #endif
1398                 {
1399                         int tmplen = (spu_backbuffer[0] << 8 | spu_backbuffer[1]);
1400
1401                         ddvd_display_time = ddvd_spu_decode_data(spu_backbuffer, tmplen);       // decode
1402                         ddvd_lbb_changed = 1;
1403
1404                         struct ddvd_color colneu;
1405                         int ctmp;
1406                         msg = DDVD_COLORTABLE_UPDATE;
1407                         if (ddvd_screeninfo_bypp == 1)
1408                                 safe_write(message_pipe, &msg, sizeof(int));
1409                         for (ctmp = 0; ctmp < 4; ctmp++) {
1410                                 colneu.blue = ddvd_bl[ctmp + 252];
1411                                 colneu.green = ddvd_gn[ctmp + 252];
1412                                 colneu.red = ddvd_rd[ctmp + 252];
1413                                 colneu.trans = ddvd_tr[ctmp + 252];
1414                                 if (ddvd_screeninfo_bypp == 1)
1415                                         safe_write(message_pipe, &colneu, sizeof(struct ddvd_color));
1416                         }
1417                         msg = DDVD_NULL;
1418
1419                         memcpy(spu_backbuffer, spu_backbuffer + tmplen, ddvd_spu_backptr - tmplen);     // delete SPU packet
1420                         spu_backpts[0] = spu_backpts[1];
1421                         spu_backpts[1] = spu_backpts[2];
1422                         ddvd_spu_backnr--;
1423                         ddvd_spu_backptr -= tmplen;
1424
1425                         // set timer
1426                         if (ddvd_display_time > 0) {
1427                                 ddvd_spu_timer_active = 1;
1428                                 ddvd_spu_timer_end = ddvd_get_time() + (ddvd_display_time * 10);        //ms
1429                         } else
1430                                 ddvd_spu_timer_active = 0;
1431
1432                         pci = dvdnav_get_current_nav_pci(dvdnav);       //update highlight buttons
1433                         dsi = dvdnav_get_current_nav_dsi(dvdnav);
1434                         if (pci->hli.hl_gi.btn_ns > 0) {
1435                                 dvdnav_get_current_highlight(dvdnav, &buttonN);
1436                                 dvdnav_button_select(dvdnav, pci, buttonN);
1437                                 ddvd_lbb_changed = 0;
1438                                 in_menu = 1;
1439                         }
1440                 }
1441
1442                 if (!in_menu) {
1443                         if (!pci)
1444                                 pci = dvdnav_get_current_nav_pci(dvdnav);
1445
1446                         in_menu = pci && pci->hli.hl_gi.btn_ns > 0;
1447                 }
1448
1449                 if (in_menu && !playerconfig->in_menu) {
1450                         int bla = DDVD_MENU_OPENED;
1451                         safe_write(message_pipe, &bla, sizeof(int));
1452                         playerconfig->in_menu = 1;
1453                 } else if (!in_menu && playerconfig->in_menu) {
1454                         int bla = DDVD_MENU_CLOSED;
1455                         safe_write(message_pipe, &bla, sizeof(int));
1456                         playerconfig->in_menu = 0;
1457                 }
1458
1459                 if (ddvd_wait_for_user) {
1460                         struct pollfd pfd[1];   // make new pollfd array
1461                         pfd[0].fd = key_pipe;
1462                         pfd[0].events = POLLIN | POLLPRI | POLLERR;
1463                         poll(pfd, 1, -1);
1464                 }
1465                 //Userinput
1466                 if (ddvd_readpipe(key_pipe, &rccode, sizeof(int), 0) == sizeof(int)) {
1467                         int keydone = 1;
1468
1469                         if (!dsi)
1470                                 dsi = dvdnav_get_current_nav_dsi(dvdnav);
1471
1472                         if (buttonN == -1)
1473                                 dvdnav_get_current_highlight(dvdnav, &buttonN);
1474
1475                         switch (rccode) // actions inside and outside of menu
1476                         {
1477                         case DDVD_SET_MUTE:
1478                                 ismute = 1;
1479                                 break;
1480                         case DDVD_UNSET_MUTE:
1481                                 ismute = 0;
1482                                 break;
1483                         default:
1484                                 keydone = 0;
1485                                 break;
1486                         }
1487
1488                         if (!keydone && in_menu) {
1489                                 switch (rccode) //Actions inside a Menu
1490                                 {
1491                                 case DDVD_KEY_UP:       //Up
1492                                         dvdnav_upper_button_select(dvdnav, pci);
1493                                         break;
1494                                 case DDVD_KEY_DOWN:     //Down
1495                                         dvdnav_lower_button_select(dvdnav, pci);
1496                                         break;
1497                                 case DDVD_KEY_LEFT:     //left
1498                                         dvdnav_left_button_select(dvdnav, pci);
1499                                         break;
1500                                 case DDVD_KEY_RIGHT:    //right
1501                                         dvdnav_right_button_select(dvdnav, pci);
1502                                         break;
1503                                 case DDVD_KEY_OK:       //OK
1504                                         ddvd_wait_for_user = 0;
1505                                         memset(p_lfb, 0, ddvd_screeninfo_stride * ddvd_screeninfo_yres);        //clear screen ..
1506                                         memset(ddvd_lbb, 0, ddvd_screeninfo_xres * ddvd_screeninfo_yres);       //clear backbuffer
1507                                         msg = DDVD_SCREEN_UPDATE;
1508                                         safe_write(message_pipe, &msg, sizeof(int));
1509                                         ddvd_clear_buttons = 1;
1510                                         dvdnav_button_activate(dvdnav, pci);
1511                                         ddvd_play_empty(TRUE);
1512                                         if (ddvd_wait_timer_active)
1513                                                 ddvd_wait_timer_active = 0;
1514                                         break;
1515                                 case DDVD_KEY_EXIT:     //Exit
1516                                         printf("DDVD_KEY_EXIT (menu)\n");
1517                                         finished = 1;
1518                                         break;
1519                                 case DDVD_KEY_MENU:     //Dream
1520                                         if (dvdnav_menu_call(dvdnav, DVD_MENU_Root) == DVDNAV_STATUS_OK)
1521                                                 ddvd_play_empty(TRUE);
1522                                         break;
1523                                 case DDVD_KEY_AUDIOMENU:        //Audio
1524                                         if (dvdnav_menu_call(dvdnav, DVD_MENU_Audio) == DVDNAV_STATUS_OK)
1525                                                 ddvd_play_empty(TRUE);
1526                                         break;
1527                                 case DDVD_SKIP_FWD:
1528                                 case DDVD_SKIP_BWD:
1529                                 case DDVD_SET_TITLE:
1530                                 case DDVD_SET_CHAPTER:
1531                                         // we must empty the pipe here...
1532                                         ddvd_readpipe(key_pipe, &keydone, sizeof(int), 1);
1533                                 default:
1534                                         break;
1535                                 }
1536                         } else if (!keydone)    //Actions inside a Movie
1537                         {
1538                                 switch (rccode) //Main Actions
1539                                 {
1540                                 case DDVD_KEY_PREV_CHAPTER:     //left
1541                                         {
1542                                                 int titleNo, chapterNumber, chapterNo;
1543                                                 dvdnav_current_title_info(dvdnav, &titleNo, &chapterNo);
1544                                                 dvdnav_get_number_of_parts(dvdnav, titleNo, &chapterNumber);
1545                                                 chapterNo--;
1546                                                 if (chapterNo > chapterNumber)
1547                                                         chapterNo = 1;
1548                                                 if (chapterNo <= 0)
1549                                                         chapterNo = chapterNumber;
1550                                                 dvdnav_part_play(dvdnav, titleNo, chapterNo);
1551                                                 ddvd_play_empty(TRUE);
1552                                                 msg = DDVD_SHOWOSD_TIME;
1553                                                 break;
1554                                         }
1555                                 case DDVD_KEY_NEXT_CHAPTER:     //right
1556                                         {
1557                                                 int titleNo, chapterNumber, chapterNo;
1558                                                 dvdnav_current_title_info(dvdnav, &titleNo, &chapterNo);
1559                                                 dvdnav_get_number_of_parts(dvdnav, titleNo, &chapterNumber);
1560                                                 chapterNo++;
1561                                                 if (chapterNo > chapterNumber)
1562                                                         chapterNo = 1;
1563                                                 if (chapterNo <= 0)
1564                                                         chapterNo = chapterNumber;
1565                                                 dvdnav_part_play(dvdnav, titleNo, chapterNo);
1566                                                 ddvd_play_empty(TRUE);
1567                                                 msg = DDVD_SHOWOSD_TIME;
1568                                                 break;
1569                                         }
1570                                 case DDVD_KEY_PREV_TITLE:
1571                                         {
1572                                                 int titleNo, titleNumber, chapterNo;
1573                                                 dvdnav_current_title_info(dvdnav, &titleNo, &chapterNo);
1574                                                 dvdnav_get_number_of_titles(dvdnav, &titleNumber);
1575                                                 titleNo--;
1576                                                 if (titleNo > titleNumber)
1577                                                         titleNo = 1;
1578                                                 if (titleNo <= 0)
1579                                                         titleNo = titleNumber;
1580                                                 dvdnav_part_play(dvdnav, titleNo, 1);
1581                                                 ddvd_play_empty(TRUE);
1582                                                 msg = DDVD_SHOWOSD_TIME;
1583                                                 break;
1584                                         }
1585                                 case DDVD_KEY_NEXT_TITLE:
1586                                         {
1587                                                 int titleNo, titleNumber, chapterNo;
1588                                                 dvdnav_current_title_info(dvdnav, &titleNo, &chapterNo);
1589                                                 dvdnav_get_number_of_titles(dvdnav, &titleNumber);
1590                                                 titleNo++;
1591                                                 if (titleNo > titleNumber)
1592                                                         titleNo = 1;
1593                                                 if (titleNo <= 0)
1594                                                         titleNo = titleNumber;
1595                                                 dvdnav_part_play(dvdnav, titleNo, 1);
1596                                                 ddvd_play_empty(TRUE);
1597                                                 msg = DDVD_SHOWOSD_TIME;
1598                                                 break;
1599                                         }
1600                                 case DDVD_KEY_PAUSE:    // Pause
1601                                         {
1602                                                 if (ddvd_playmode == PLAY) {
1603                                                         ddvd_playmode = PAUSE;
1604                                                         if (ioctl(ddvd_fdaudio, AUDIO_PAUSE) < 0)
1605                                                                 perror("AUDIO_PAUSE");
1606                                                         if (ioctl(ddvd_fdvideo, VIDEO_FREEZE) < 0)
1607                                                                 perror("VIDEO_FREEZE");
1608                                                         msg = DDVD_SHOWOSD_STATE_PAUSE;
1609                                                         safe_write(message_pipe, &msg, sizeof(int));
1610                                                         break;
1611                                                 } else if (ddvd_playmode != PAUSE)
1612                                                         break;
1613                                                 // fall through to PLAY
1614                                         }
1615                                 case DDVD_KEY_PLAY:     // Play
1616                                         {
1617                                                 if (ddvd_playmode == PAUSE || ddvd_trickmode) {
1618                                                         ddvd_playmode = PLAY;
1619 key_play:
1620 #if CONFIG_API_VERSION == 1
1621                                                         ddvd_device_clear();
1622 #endif
1623                                                         if (ddvd_trickmode && !ismute)
1624                                                                 if (ioctl(ddvd_fdaudio, AUDIO_SET_MUTE, 0) < 0)
1625                                                                         perror("AUDIO_SET_MUTE");
1626                                                         ddvd_trickmode = TOFF;
1627                                                         if (ddvd_playmode == PLAY) {
1628                                                                 if (ioctl(ddvd_fdaudio, AUDIO_CONTINUE) < 0)
1629                                                                         perror("AUDIO_CONTINUE");
1630                                                                 if (ioctl(ddvd_fdvideo, VIDEO_CONTINUE) < 0)
1631                                                                         perror("VIDEO_CONTINUE");
1632                                                                 msg = DDVD_SHOWOSD_STATE_PLAY;
1633                                                                 safe_write(message_pipe, &msg, sizeof(int));
1634                                                         }
1635                                                         msg = DDVD_SHOWOSD_TIME;
1636                                                 }
1637                                                 break;
1638                                         }
1639                                 case DDVD_KEY_MENU:     //Dream
1640                                         if (dvdnav_menu_call(dvdnav, DVD_MENU_Root) == DVDNAV_STATUS_OK)
1641                                                 ddvd_play_empty(TRUE);
1642                                         break;
1643                                 case DDVD_KEY_AUDIOMENU:        //Audio
1644                                         if (dvdnav_menu_call(dvdnav, DVD_MENU_Audio) == DVDNAV_STATUS_OK)
1645                                                 ddvd_play_empty(TRUE);
1646                                         break;
1647                                 case DDVD_KEY_EXIT:     //EXIT 
1648                                         {
1649                                                 printf("DDVD_KEY_EXIT\n");
1650                                                 finished = 1;
1651                                                 break;
1652                                         }
1653                                 case DDVD_KEY_FFWD:     //FastForward
1654                                 case DDVD_KEY_FBWD:     //FastBackward
1655                                         {
1656                                                 if (ddvd_trickmode == TOFF) {
1657                                                         if (ioctl(ddvd_fdaudio, AUDIO_SET_MUTE, 1) < 0)
1658                                                                 perror("AUDIO_SET_MUTE");
1659                                                         ddvd_trickspeed = 2;
1660                                                         ddvd_trickmode = (rccode == DDVD_KEY_FBWD ? FASTBW : FASTFW);
1661                                                 } else if (ddvd_trickmode == (rccode == DDVD_KEY_FBWD ? FASTFW : FASTBW)) {
1662                                                         ddvd_trickspeed /= 2;
1663                                                         if (ddvd_trickspeed == 1) {
1664                                                                 ddvd_trickspeed = 0;
1665                                                                 goto key_play;
1666                                                         }
1667                                                 } else if (ddvd_trickspeed < 64)
1668                                                         ddvd_trickspeed *= 2;
1669                                                 break;
1670                                         }
1671                                 case DDVD_KEY_AUDIO:    //change audio track 
1672                                         {
1673                                                 uint16_t audio_lang = 0xFFFF;
1674                                                 while (audio_lang == 0xFFFF) {
1675                                                         audio_id++;
1676                                                         audio_lang = dvdnav_audio_stream_to_lang(dvdnav, audio_id);
1677                                                         if (audio_lang == 0xFFFF)
1678                                                                 audio_id = -1;
1679                                                 }
1680                                                 ddvd_play_empty(TRUE);
1681                                                 audio_lock = 1;
1682                                                 ddvd_lpcm_count = 0;
1683                                                 msg = DDVD_SHOWOSD_AUDIO;
1684                                                 safe_write(message_pipe, &msg, sizeof(int));
1685                                                 safe_write(message_pipe, &audio_id, sizeof(int));
1686                                                 safe_write(message_pipe, &audio_lang, sizeof(uint16_t));
1687                                                 safe_write(message_pipe, &audio_format[audio_id], sizeof(int));
1688                                                 break;
1689                                         }
1690                                 case DDVD_KEY_SUBTITLE: //change spu track 
1691                                         {
1692                                                 uint16_t spu_lang = 0xFFFF;
1693                                                 while (spu_lang == 0xFFFF) {
1694                                                         spu_active_id++;
1695                                                         spu_lang = dvdnav_spu_stream_to_lang(dvdnav, spu_active_id);
1696                                                         if (spu_lang == 0xFFFF) {
1697                                                                 spu_lang = 0x2D2D;      // SPU "off" 
1698                                                                 spu_active_id = -1;
1699                                                         }
1700                                                 }
1701                                                 spu_lock = 1;
1702                                                 msg = DDVD_SHOWOSD_SUBTITLE;
1703                                                 safe_write(message_pipe, &msg, sizeof(int));
1704                                                 safe_write(message_pipe, &spu_active_id, sizeof(int));
1705                                                 safe_write(message_pipe, &spu_lang, sizeof(uint16_t));
1706                                                 break;
1707                                         }
1708                                 case DDVD_GET_TIME:     // frontend wants actual time
1709                                         msg = DDVD_SHOWOSD_TIME;
1710                                         break;
1711                                 case DDVD_SKIP_FWD:
1712                                 case DDVD_SKIP_BWD:
1713                                         {
1714                                                 int skip;
1715                                                 ddvd_readpipe(key_pipe, &skip, sizeof(int), 1);
1716                                                 if (ddvd_trickmode == TOFF) {
1717                                                         uint32_t pos, len;
1718                                                         dvdnav_get_position(dvdnav, &pos, &len);
1719                                                         printf("DDVD_SKIP pos=%u len=%u \n", pos, len);
1720                                                         //90000 = 1 Sek.
1721                                                         if (!len)
1722                                                                 len = 1;
1723                                                         long long int posneu = ((pos * ddvd_lastCellEventInfo.pgc_length) / len) + (90000 * skip);
1724                                                         printf("DDVD_SKIP posneu1=%lld\n", posneu);
1725                                                         long long int posneu2 = posneu <= 0 ? 0 : (posneu * len) / ddvd_lastCellEventInfo.pgc_length;
1726                                                         printf("DDVD_SKIP posneu2=%lld\n", posneu2);
1727                                                         if (len && posneu2 && posneu2 >= len)   // reached end of movie
1728                                                         {
1729                                                                 posneu2 = len - 250;
1730                                                                 reached_eof = 1;
1731                                                         }
1732                                                         dvdnav_sector_search(dvdnav, posneu2, SEEK_SET);
1733                                                         ddvd_lpcm_count = 0;
1734                                                         msg = DDVD_SHOWOSD_TIME;
1735                                                 }
1736                                                 break;
1737                                         }
1738                                 case DDVD_SET_TITLE:
1739                                         {
1740                                                 int title, totalTitles;
1741                                                 ddvd_readpipe(key_pipe, &title, sizeof(int), 1);
1742                                                 dvdnav_get_number_of_titles(dvdnav, &totalTitles);
1743                                                 printf("DDVD_SET_TITLE %d/%d\n", title, totalTitles);
1744                                                 if (title <= totalTitles) {
1745                                                         dvdnav_part_play(dvdnav, title, 0);
1746                                                         ddvd_play_empty(TRUE);
1747                                                         msg = DDVD_SHOWOSD_TIME;
1748                                                 }
1749                                                 break;
1750                                         }
1751                                 case DDVD_SET_CHAPTER:
1752                                         {
1753                                                 int chapter, totalChapters, chapterNo, titleNo;
1754                                                 ddvd_readpipe(key_pipe, &chapter, sizeof(int), 1);
1755                                                 dvdnav_current_title_info(dvdnav, &titleNo, &chapterNo);
1756                                                 dvdnav_get_number_of_parts(dvdnav, titleNo, &totalChapters);
1757                                                 printf("DDVD_SET_CHAPTER %d/%d in title %d\n", chapter, totalChapters, titleNo);
1758                                                 if (chapter <= totalChapters) {
1759                                                         dvdnav_part_play(dvdnav, titleNo, chapter);
1760                                                         ddvd_play_empty(TRUE);
1761                                                         msg = DDVD_SHOWOSD_TIME;
1762                                                 }
1763                                                 break;
1764                                         }
1765                                 default:
1766                                         break;
1767                                 }
1768                         }
1769                 }
1770                 // spu handling
1771                 if (ddvd_lbb_changed == 1) {
1772                         if (ddvd_screeninfo_bypp == 1)
1773                                 memcpy(p_lfb, ddvd_lbb, ddvd_screeninfo_xres * ddvd_screeninfo_yres);   //copy SPU backbuffer into screen
1774                         else {
1775                                 int i = 0;
1776                                 for (; i < ddvd_screeninfo_yres; ++i)
1777                                         ddvd_blit_to_argb(p_lfb + i * ddvd_screeninfo_stride, ddvd_lbb + i * ddvd_screeninfo_xres, ddvd_screeninfo_xres);
1778                         }
1779                         int msg_old = msg;      // save and restore msg it may not bee empty
1780                         msg = DDVD_SCREEN_UPDATE;
1781                         safe_write(message_pipe, &msg, sizeof(int));
1782                         msg = msg_old;
1783                         ddvd_lbb_changed = 0;
1784                 }
1785
1786         }
1787
1788 err_dvdnav:
1789         /* destroy dvdnav handle */
1790         if (dvdnav_close(dvdnav) != DVDNAV_STATUS_OK)
1791                 printf("Error on dvdnav_close: %s\n", dvdnav_err_to_string(dvdnav));
1792
1793 err_dvdnav_open:
1794         if (ioctl(ddvd_fdvideo, VIDEO_CLEAR_BUFFER) < 0)
1795                 perror("VIDEO_CLEAR_BUFFER");
1796         if (ioctl(ddvd_fdvideo, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_DEMUX) < 0)
1797                 perror("VIDEO_SELECT_SOURCE");
1798         if (ioctl(ddvd_fdaudio, AUDIO_CLEAR_BUFFER) < 0)
1799                 perror("AUDIO_CLEAR_BUFFER");
1800         if (ioctl(ddvd_fdaudio, AUDIO_SELECT_SOURCE, AUDIO_SOURCE_DEMUX) < 0)
1801                 perror("AUDIO_SELECT_SOURCE");
1802
1803         if (ioctl(ddvd_fdaudio, AUDIO_SET_AV_SYNC, 1) < 0)      // restore AudioDecoder State
1804                 perror("AUDIO_SET_AV_SYNC");
1805         if (ioctl(ddvd_fdaudio, AUDIO_SET_BYPASS_MODE, 1) < 0)
1806                 perror("AUDIO_SET_BYPASS_MODE");
1807
1808         close(ddvd_ac3_fd);
1809 err_open_ac3_fd:
1810         close(ddvd_fdaudio);
1811 err_open_fdaudio:
1812         close(ddvd_fdvideo);
1813 err_open_fdvideo:
1814         close(ddvd_output_fd);
1815 err_open_output_fd:
1816
1817         if (have_liba52) {
1818                 a52_free(state);
1819                 ddvd_close_liba52();
1820         }
1821
1822         //Clear Screen
1823         memset(p_lfb, 0, ddvd_screeninfo_stride * ddvd_screeninfo_yres);
1824         msg = DDVD_SCREEN_UPDATE;
1825         safe_write(message_pipe, &msg, sizeof(int));
1826
1827 err_malloc:
1828         // clean up
1829         if (ddvd_lbb != NULL)
1830                 free(ddvd_lbb);
1831         if (last_iframe != NULL)
1832                 free(last_iframe);
1833         if (spu_buffer != NULL)
1834                 free(spu_buffer);
1835         if (spu_backbuffer != NULL)
1836                 free(spu_backbuffer);
1837
1838 #if CONFIG_API_VERSION == 3
1839         ddvd_unset_pcr_offset();
1840 #endif
1841         return res;
1842 }
1843
1844 /*
1845  * internal functions
1846  */
1847
1848 // reading from pipe
1849 static int ddvd_readpipe(int pipefd, void *dest, size_t bytes, int blocked_read)
1850 {
1851         size_t bytes_completed = 0;
1852
1853         while (bytes_completed < bytes) {
1854                 ssize_t rd = read(pipefd, dest + bytes_completed, bytes - bytes_completed);
1855                 if (rd < 0) {
1856                         if (errno == EAGAIN) {
1857                                 if (blocked_read || bytes_completed) {
1858                                         usleep(1);
1859                                         continue;
1860                                 }
1861                                 break;  // leave while loop
1862                         }
1863                         /* else if (errno == ????) // hier sollte evtl noch geschaut werden welcher error code kommt wenn die pipe geschlossen wurde... 
1864                            break; */
1865                         printf("unhandled read error %d(%m)\n", errno);
1866                 }
1867
1868                 bytes_completed += rd;
1869                 if (!blocked_read && !bytes_completed)
1870                         break;
1871         }
1872
1873         return bytes_completed;
1874 }
1875
1876 // get actual playing time
1877 static struct ddvd_time ddvd_get_osd_time(struct ddvd *playerconfig)
1878 {
1879         int titleNo;
1880         struct ddvd_time info;
1881         uint32_t pos, len;
1882
1883         info.pos_minutes = info.pos_hours = info.pos_seconds = info.pos_chapter = info.pos_title = 0;
1884         info.end_minutes = info.end_hours = info.end_seconds = info.end_chapter = 0;
1885
1886         dvdnav_get_number_of_titles(dvdnav, &info.end_title);
1887         dvdnav_current_title_info(dvdnav, &titleNo, &info.pos_chapter);
1888
1889         if (titleNo) {
1890                 dvdnav_get_number_of_parts(dvdnav, titleNo, &info.end_chapter);
1891                 dvdnav_get_position_in_title(dvdnav, &pos, &len);
1892
1893                 uint64_t len_s = ddvd_lastCellEventInfo.pgc_length / 90000;
1894                 uint64_t pos_s = ((ddvd_lastCellEventInfo.pgc_length / len) * pos) / 90000;
1895
1896                 info.pos_minutes = pos_s / 60;
1897                 info.pos_hours = info.pos_minutes / 60;
1898                 info.pos_minutes = info.pos_minutes - (info.pos_hours * 60);
1899                 info.pos_seconds = pos_s - ((info.pos_hours * 60) + info.pos_minutes) * 60;
1900                 info.end_minutes = len_s / 60;
1901                 info.end_hours = info.end_minutes / 60;
1902                 info.end_minutes = info.end_minutes - (info.end_hours * 60);
1903                 info.end_seconds = len_s - ((info.end_hours * 60) + info.end_minutes) * 60;
1904
1905                 info.pos_title = titleNo;
1906         }
1907
1908         playerconfig->next_time_update = ddvd_get_time() + 1000;
1909
1910         return info;
1911 }
1912
1913 // video out aspect/scale
1914 static int ddvd_check_aspect(int dvd_aspect, int dvd_scale_perm, int tv_aspect)
1915 {
1916         const char *aspect = NULL, *policy = NULL, *policy2 = NULL, *saa2 = NULL;
1917         int tv_scale = 0;
1918         int saa;
1919
1920         if (tv_aspect == 2)     // tv 16:9
1921         {
1922                 if (dvd_aspect == 0) {
1923                         aspect = "4:3";
1924                         policy = "VIDEO_PAN_SCAN";      //no scaling needed
1925                         policy2 = "panscan";
1926                         tv_scale = 0;   //off
1927                         saa = SAA_WSS_43F;
1928                         saa2 = "4:3_full_format";
1929                 } else {
1930                         aspect = "16:9";
1931                         policy = "VIDEO_CENTER_CUT_OUT";        //no scaling needed
1932                         policy2 = "bestfit";
1933                         tv_scale = 0;   //off
1934                         saa = SAA_WSS_169F;
1935                         saa2 = "16:9_full_format";
1936                 }
1937         } else if (tv_aspect == 3)      // tv always 16:9
1938         {
1939                 aspect = "16:9";
1940                 policy = "VIDEO_CENTER_CUT_OUT";        //no scaling needed
1941                 policy2 = "panscan";
1942                 tv_scale = 0;   //off
1943                 saa = SAA_WSS_169F;
1944                 saa2 = "16:9_full_format";
1945         } else                  // tv 4:3
1946         {
1947                 aspect = "4:3";
1948                 saa = SAA_WSS_43F;
1949                 saa2 = "4:3_full_format";
1950                 if (dvd_aspect == 0)    // dvd 4:3 ...
1951                 {
1952                         policy = "VIDEO_PAN_SCAN";      //no scaling needed
1953                         policy2 = "panscan";
1954                         tv_scale = 0;   //off
1955                 } else          // dvd 16:9 ... need scaling prefer letterbox (tv_aspect 0)
1956                 {
1957                         if (!(dvd_scale_perm & 1))      // letterbox allowed
1958                         {
1959                                 policy = "VIDEO_LETTER_BOX";
1960                                 policy2 = "letterbox";
1961                                 tv_scale = 1;   //letterbox
1962                         } else if (!(dvd_scale_perm & 2))       // panscan allowed
1963                         {
1964                                 policy = "VIDEO_PAN_SCAN";
1965                                 policy2 = "panscan";
1966                                 tv_scale = 2;   //panscan
1967                         } else  // should not happen
1968                         {
1969                                 policy = "VIDEO_LETTER_BOX";
1970                                 policy2 = "letterbox";
1971                                 tv_scale = 1;   //off
1972                         }
1973                 }
1974                 if (tv_aspect == 1 && tv_scale == 1 && !(dvd_scale_perm & 2))   // prefer panscan if allowed (tv_aspect 1)
1975                 {
1976                         policy = "VIDEO_PAN_SCAN";
1977                         policy2 = "panscan";
1978                         tv_scale = 2;   //panscan
1979                 }
1980         }
1981
1982         if (ioctl(ddvd_fdvideo, VIDEO_SET_DISPLAY_FORMAT, policy) < 0)
1983                 perror("VIDEO_SET_DISPLAY_FORMAT");
1984
1985         int saafd = open("/dev/dbox/saa0", O_RDWR);
1986         if (saafd >= 0) {
1987                 if (ioctl(saafd, SAAIOSWSS, &saa) < 0)
1988                         perror("SAAIOSWSS");
1989                 close(saafd);
1990         }
1991
1992         // switch video aspect
1993         write_string("/proc/stb/video/aspect", aspect);
1994         // switch video scaling
1995         write_string("/proc/stb/video/policy", policy2);
1996         // switch wss
1997         write_string("/proc/stb/denc/0/wss", saa2);
1998
1999         return tv_scale;
2000 }
2001
2002 // get timestamp
2003 static uint64_t ddvd_get_time(void)
2004 {
2005         static time_t t0 = 0;
2006         struct timeval t;
2007
2008         if (gettimeofday(&t, NULL) == 0) {
2009                 if (t0 == 0)
2010                         t0 = t.tv_sec;  // this avoids an overflow (we only work with deltas)
2011                 return (uint64_t) (t.tv_sec - t0) * 1000 + (uint64_t) (t.tv_usec / 1000);
2012         }
2013
2014         return 0;
2015 }
2016
2017 // Empty all Buffers
2018 static void ddvd_play_empty(int device_clear)
2019 {
2020         ddvd_wait_for_user = 0;
2021         ddvd_clear_buttons = 0;
2022         ddvd_lpcm_count = 0;
2023         ddvd_iframerun = 0;
2024         ddvd_still_frame = 0;
2025         ddvd_iframesend = 0;
2026         ddvd_last_iframe_len = 0;
2027         ddvd_spu_ptr = 0;
2028         ddvd_spu_backnr = 0;
2029         ddvd_spu_backptr = 0;
2030
2031         ddvd_wait_timer_active = 0;
2032         ddvd_wait_timer_end = 0;
2033
2034         ddvd_spu_timer_active = 0;
2035         ddvd_spu_timer_end = 0;
2036
2037         //memset(ddvd_lbb, 0, ddvd_screeninfo_xres*ddvd_screeninfo_yres); //clear SPU backbuffer
2038         //ddvd_lbb_changed=1;
2039
2040         if (device_clear)
2041                 ddvd_device_clear();
2042 }
2043
2044 // Empty Device Buffers
2045 static void ddvd_device_clear(void)
2046 {
2047         if (ioctl(ddvd_fdaudio, AUDIO_STOP) < 0)
2048                 perror("AUDIO_STOP");
2049         if (ioctl(ddvd_fdaudio, AUDIO_CLEAR_BUFFER) < 0)
2050                 perror("AUDIO_CLEAR_BUFFER");
2051         if (ioctl(ddvd_fdaudio, AUDIO_PLAY) < 0)
2052                 perror("AUDIO_PLAY");
2053
2054         if (ioctl(ddvd_fdvideo, VIDEO_CLEAR_BUFFER) < 0)
2055                 perror("VIDEO_CLEAR_BUFFER");
2056         if (ioctl(ddvd_fdvideo, VIDEO_PLAY) < 0)
2057                 perror("VIDEO_PLAY");
2058
2059         if (ioctl(ddvd_fdaudio, AUDIO_SET_AV_SYNC, 1) < 0)
2060                 perror("AUDIO_SET_AV_SYNC");
2061 }
2062
2063 // SPU Decoder
2064 static int ddvd_spu_decode_data(const uint8_t * buffer, int len)
2065 {
2066         int x1spu, x2spu, y1spu, y2spu, xspu, yspu;
2067         int offset[2];
2068         int size, datasize, controlsize, aligned, id;
2069         int menubutton = 0;
2070         int display_time = -1;
2071
2072         size = (buffer[0] << 8 | buffer[1]);
2073         datasize = (buffer[2] << 8 | buffer[3]);
2074         controlsize = (buffer[datasize + 2] << 8 | buffer[datasize + 3]);
2075
2076         //printf("SPU_dec: Size: %X Datasize: %X Controlsize: %X\n",size,datasize,controlsize);
2077         // parse header
2078         int i = datasize + 4;
2079
2080         while (i < size && buffer[i] != 0xFF) {
2081                 switch (buffer[i]) {
2082                 case 0x00:      /* menu button special color handling */
2083                         menubutton = 1;
2084                         memset(ddvd_lbb, 0, ddvd_screeninfo_xres * ddvd_screeninfo_yres);       //clear backbuffer
2085                         i++;
2086                         break;
2087                 case 0x01:      /* show */
2088                         i++;
2089                         break;
2090                 case 0x02:      /* hide */
2091                         i++;
2092                         break;
2093                 case 0x03:      /* palette */
2094                         {
2095                                 ddvd_spudec_clut_t *clut = (ddvd_spudec_clut_t *) (buffer + i + 1);
2096                                 //printf("update palette %d %d %d %d\n", clut->entry0, clut->entry1, clut->entry2, clut->entry3);
2097
2098                                 ddvd_bl[3 + 252] = ddvd_bl[clut->entry0];
2099                                 ddvd_gn[3 + 252] = ddvd_gn[clut->entry0];
2100                                 ddvd_rd[3 + 252] = ddvd_rd[clut->entry0];
2101
2102                                 ddvd_bl[2 + 252] = ddvd_bl[clut->entry1];
2103                                 ddvd_gn[2 + 252] = ddvd_gn[clut->entry1];
2104                                 ddvd_rd[2 + 252] = ddvd_rd[clut->entry1];
2105
2106                                 ddvd_bl[1 + 252] = ddvd_bl[clut->entry2];
2107                                 ddvd_gn[1 + 252] = ddvd_gn[clut->entry2];
2108                                 ddvd_rd[1 + 252] = ddvd_rd[clut->entry2];
2109
2110                                 ddvd_bl[0 + 252] = ddvd_bl[clut->entry3];
2111                                 ddvd_gn[0 + 252] = ddvd_gn[clut->entry3];
2112                                 ddvd_rd[0 + 252] = ddvd_rd[clut->entry3];
2113
2114                                 //CHANGE COLORMAP
2115                                 i += 3;
2116                         }
2117                         break;
2118                 case 0x04:      /* transparency palette */
2119                         {
2120                                 ddvd_spudec_clut_t *clut = (ddvd_spudec_clut_t *) (buffer + i + 1);
2121                                 //printf("update transp palette %d %d %d %d\n", clut->entry0, clut->entry1, clut->entry2, clut->entry3);
2122
2123                                 ddvd_tr[0 + 252] = (0xF - clut->entry3) * 0x1111;
2124                                 ddvd_tr[1 + 252] = (0xF - clut->entry2) * 0x1111;
2125                                 ddvd_tr[2 + 252] = (0xF - clut->entry1) * 0x1111;
2126                                 ddvd_tr[3 + 252] = (0xF - clut->entry0) * 0x1111;
2127
2128                                 //CHANGE COLORMAP
2129                                 i += 3;
2130                         }
2131                         break;
2132                 case 0x05:      /* image coordinates */
2133                         //printf("image coords\n");
2134                         xspu = x1spu = (((unsigned int)buffer[i + 1]) << 4) + (buffer[i + 2] >> 4);
2135                         yspu = y1spu = (((unsigned int)buffer[i + 4]) << 4) + (buffer[i + 5] >> 4);
2136                         x2spu = (((buffer[i + 2] & 0x0f) << 8) + buffer[i + 3]);
2137                         y2spu = (((buffer[i + 5] & 0x0f) << 8) + buffer[i + 6]);
2138                         //printf("%d %d %d %d\n", xspu, yspu, x2spu, y2spu);
2139                         i += 7;
2140                         break;
2141                 case 0x06:      /* image 1 / image 2 offsets */
2142                         //printf("image offsets\n");
2143                         offset[0] = (((unsigned int)buffer[i + 1]) << 8) + buffer[i + 2];
2144                         offset[1] = (((unsigned int)buffer[i + 3]) << 8) + buffer[i + 4];
2145                         //printf("%d %d\n", offset[0], offset[1]);
2146                         i += 5;
2147                         break;
2148                 default:
2149                         i++;
2150                         break;
2151                 }
2152         }
2153
2154         //get display time
2155         if (i + 6 <= size) {
2156                 if (buffer[i + 5] == 0x02 && buffer[i + 6] == 0xFF) {
2157                         display_time = ((buffer[i + 1] << 8) + buffer[i + 2]);
2158                         //printf("Display Time: %d\n",ddvd_display_time);
2159                 }
2160         }
2161         //printf("SPU_dec: Image coords x1: %d y1: %d x2: %d y2: %d\n",x1spu,y1spu,x2spu,y2spu);
2162         //printf("Offset[0]: %X Offset[1]: %X\n",offset[0],offset[1]);
2163
2164         // parse picture
2165
2166         aligned = 1;
2167         id = 0;
2168
2169         while (offset[1] < datasize + 2 && yspu <= 575) // there are some special cases the decoder tries to write more than 576 lines in our buffer and we dont want this ;)
2170         {
2171                 u_int len;
2172                 u_int code;
2173
2174                 code = (aligned ? (buffer[offset[id]++] >> 4) : (buffer[offset[id] - 1] & 0xF));
2175                 aligned = aligned ? 0 : 1;
2176
2177                 if (code < 0x0004) {
2178                         code = (code << 4) | (aligned ? (buffer[offset[id]++] >> 4) : (buffer[offset[id] - 1] & 0xF));
2179                         aligned = aligned ? 0 : 1;
2180                         if (code < 0x0010) {
2181                                 code = (code << 4) | (aligned ? (buffer[offset[id]++] >> 4) : (buffer[offset[id] - 1] & 0xF));
2182                                 aligned = aligned ? 0 : 1;
2183                                 if (code < 0x0040) {
2184                                         code = (code << 4) | (aligned ? (buffer[offset[id]++] >> 4) : (buffer[offset[id] - 1] & 0xF));
2185                                         aligned = aligned ? 0 : 1;
2186                                 }
2187                         }
2188                 }
2189
2190                 len = code >> 2;
2191
2192                 if (len == 0)
2193                         len = x2spu - xspu;
2194
2195                 memset(ddvd_lbb + xspu + ddvd_screeninfo_xres * (yspu), (code & 3) + 252, len); //drawpixel into backbuffer
2196                 xspu += len;
2197                 if (xspu >= x2spu) {
2198                         if (!aligned) {
2199                                 code = (aligned ? (buffer[offset[id]++] >> 4) : (buffer[offset[id] - 1] & 0xF));
2200                                 aligned = aligned ? 0 : 1;
2201                         }
2202                         xspu = x1spu;   //next line
2203                         yspu++;
2204                         id = id ? 0 : 1;
2205                 }
2206         }
2207
2208         return display_time;
2209 }
2210
2211 // blit to argb in 32bit mode
2212 static void ddvd_blit_to_argb(void *_dst, const void *_src, int pix)
2213 {
2214         unsigned long *dst = _dst;
2215         const unsigned char *src = _src;
2216         while (pix--) {
2217                 int p = (*src++);
2218                 int a, r, g, b;
2219                 if (p == 0) {
2220                         r = g = b = a = 0;      //clear screen (transparency)
2221                 } else {
2222                         a = 0xFF - (ddvd_tr[p] >> 8);
2223                         r = ddvd_rd[p] >> 8;
2224                         g = ddvd_gn[p] >> 8;
2225                         b = ddvd_bl[p] >> 8;
2226                 }
2227                 *dst++ = (a << 24) | (r << 16) | (g << 8) | (b << 0);
2228         }
2229 }
2230
2231 #if CONFIG_API_VERSION == 3
2232
2233 // set decoder buffer offsets to a minimum
2234 static void ddvd_set_pcr_offset(void)
2235 {
2236         write_string("/proc/stb/pcr/pcr_stc_offset", "200");
2237         write_string("/proc/stb/vmpeg/0/sync_offset", "200");
2238 }
2239
2240 // reset decoder buffer offsets
2241 static void ddvd_unset_pcr_offset(void)
2242 {
2243         write_string("/proc/stb/pcr/pcr_stc_offset", "2710");
2244         write_string("/proc/stb/vmpeg/0/sync_offset", "2710");
2245 }
2246
2247 #endif