Debian Codegraph @

examples/menus.c

raw
1 /*
2 * Copyright (C) 2003 by the libdvdnav project
3 *
4 * This file is part of libdvdnav, a DVD navigation library.
5 *
6 * libdvdnav is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * libdvdnav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with libdvdnav; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <inttypes.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <dvdnav/dvdnav.h>
28
29 /* shall we use libdvdnav's read ahead cache? */
30 #define DVD_READ_CACHE 1
31
32 /* which is the default language for menus/audio/subpictures? */
33 #define DVD_LANGUAGE "en"
34
35 #ifdef _WIN32
36 #define S_IRWXG 0
37 #endif
38
39 int main(int argc, char **argv) {
40 dvdnav_t *dvdnav;
41 uint8_t mem[DVD_VIDEO_LB_LEN];
42 int finished = 0;
43 int output_fd = 0;
44 int dump = 0, tt_dump = 0;
45
46 if (argc < 2) {
47 printf("Specify DVD device.\n");
48 return 1;
49 }
50
51 /* open dvdnav handle */
52 printf("Opening DVD...\n");
53 if (dvdnav_open(&dvdnav, argv[1]) != DVDNAV_STATUS_OK) {
54 printf("Error on dvdnav_open\n");
55 return 1;
56 }
57
58 /* set read ahead cache usage */
59 if (dvdnav_set_readahead_flag(dvdnav, DVD_READ_CACHE) != DVDNAV_STATUS_OK) {
60 printf("Error on dvdnav_set_readahead_flag: %s\n", dvdnav_err_to_string(dvdnav));
61 return 2;
62 }
63
64 /* set the language */
65 if (dvdnav_menu_language_select(dvdnav, DVD_LANGUAGE) != DVDNAV_STATUS_OK ||
66 dvdnav_audio_language_select(dvdnav, DVD_LANGUAGE) != DVDNAV_STATUS_OK ||
67 dvdnav_spu_language_select(dvdnav, DVD_LANGUAGE) != DVDNAV_STATUS_OK) {
68 printf("Error on setting languages: %s\n", dvdnav_err_to_string(dvdnav));
69 return 2;
70 }
71
72 /* set the PGC positioning flag to have position information relatively to the
73 * whole feature instead of just relatively to the current chapter */
74 if (dvdnav_set_PGC_positioning_flag(dvdnav, 1) != DVDNAV_STATUS_OK) {
75 printf("Error on dvdnav_set_PGC_positioning_flag: %s\n", dvdnav_err_to_string(dvdnav));
76 return 2;
77 }
78
79
80 /* the read loop which regularly calls dvdnav_get_next_block
81 * and handles the returned events */
82 printf("Reading...\n");
83 while (!finished) {
84 int result, event, len;
85 uint8_t *buf = mem;
86
87 /* the main reading function */
88 #if DVD_READ_CACHE
89 result = dvdnav_get_next_cache_block(dvdnav, &buf, &event, &len);
90 #else
91 result = dvdnav_get_next_block(dvdnav, buf, &event, &len);
92 #endif
93
94 if (result == DVDNAV_STATUS_ERR) {
95 printf("Error getting next block: %s\n", dvdnav_err_to_string(dvdnav));
96 return 3;
97 }
98
99 switch (event) {
100 case DVDNAV_BLOCK_OK:
101 /* We have received a regular block of the currently playing MPEG stream.
102 * A real player application would now pass this block through demuxing
103 * and decoding. We simply write it to disc here. */
104
105 if (!output_fd) {
106 printf("Opening output...\n");
107 output_fd = open("libdvdnav.mpg", O_CREAT | O_WRONLY, S_IRWXU | S_IRWXG);
108 if (output_fd == -1) {
109 printf("Error opening output\n");
110 return 4;
111 }
112 }
113
114 if (dump || tt_dump)
115 write(output_fd, buf, len);
116
117 break;
118 case DVDNAV_NOP:
119 /* Nothing to do here. */
120 break;
121 case DVDNAV_STILL_FRAME:
122 /* We have reached a still frame. A real player application would wait
123 * the amount of time specified by the still's length while still handling
124 * user input to make menus and other interactive stills work.
125 * A length of 0xff means an indefinite still which has to be skipped
126 * indirectly by some user interaction. */
127 {
128 dvdnav_still_event_t *still_event = (dvdnav_still_event_t *)buf;
129 if (still_event->length < 0xff)
130 printf("Skipping %d seconds of still frame\n", still_event->length);
131 else
132 printf("Skipping indefinite length still frame\n");
133 dvdnav_still_skip(dvdnav);
134 }
135 break;
136 case DVDNAV_WAIT:
137 /* We have reached a point in DVD playback, where timing is critical.
138 * Player application with internal fifos can introduce state
139 * inconsistencies, because libdvdnav is always the fifo's length
140 * ahead in the stream compared to what the application sees.
141 * Such applications should wait until their fifos are empty
142 * when they receive this type of event. */
143 printf("Skipping wait condition\n");
144 dvdnav_wait_skip(dvdnav);
145 break;
146 case DVDNAV_SPU_CLUT_CHANGE:
147 /* Player applications should pass the new colour lookup table to their
148 * SPU decoder */
149 break;
150 case DVDNAV_SPU_STREAM_CHANGE:
151 /* Player applications should inform their SPU decoder to switch channels */
152 break;
153 case DVDNAV_AUDIO_STREAM_CHANGE:
154 /* Player applications should inform their audio decoder to switch channels */
155 break;
156 case DVDNAV_HIGHLIGHT:
157 /* Player applications should inform their overlay engine to highlight the
158 * given button */
159 {
160 dvdnav_highlight_event_t *highlight_event = (dvdnav_highlight_event_t *)buf;
161 printf("Selected button %d\n", highlight_event->buttonN);
162 }
163 break;
164 case DVDNAV_VTS_CHANGE:
165 /* Some status information like video aspect and video scale permissions do
166 * not change inside a VTS. Therefore this event can be used to query such
167 * information only when necessary and update the decoding/displaying
168 * accordingly. */
169 break;
170 case DVDNAV_CELL_CHANGE:
171 /* Some status information like the current Title and Part numbers do not
172 * change inside a cell. Therefore this event can be used to query such
173 * information only when necessary and update the decoding/displaying
174 * accordingly. */
175 {
176 int32_t tt = 0, ptt = 0;
177 uint32_t pos, len;
178 char input = '\0';
179
180 dvdnav_current_title_info(dvdnav, &tt, &ptt);
181 dvdnav_get_position(dvdnav, &pos, &len);
182 printf("Cell change: Title %d, Chapter %d\n", tt, ptt);
183 printf("At position %.0f%% inside the feature\n", 100 * (double)pos / (double)len);
184
185 dump = 0;
186 if (tt_dump && tt != tt_dump)
187 tt_dump = 0;
188
189 if (!dump && !tt_dump) {
190 fflush(stdin);
191 while ((input != 'a') && (input != 's') && (input != 'q') && (input != 't')) {
192 printf("(a)ppend cell to output\n(s)kip cell\nappend until end of (t)itle\n(q)uit\n");
193 scanf("%c", &input);
194 }
195
196 switch (input) {
197 case 'a':
198 dump = 1;
199 break;
200 case 't':
201 tt_dump = tt;
202 break;
203 case 'q':
204 finished = 1;
205 }
206 }
207 }
208 break;
209 case DVDNAV_NAV_PACKET:
210 /* A NAV packet provides PTS discontinuity information, angle linking information and
211 * button definitions for DVD menus. Angles are handled completely inside libdvdnav.
212 * For the menus to work, the NAV packet information has to be passed to the overlay
213 * engine of the player so that it knows the dimensions of the button areas. */
214 {
215 pci_t *pci;
216
217 /* Applications with fifos should not use these functions to retrieve NAV packets,
218 * they should implement their own NAV handling, because the packet you get from these
219 * functions will already be ahead in the stream which can cause state inconsistencies.
220 * Applications with fifos should therefore pass the NAV packet through the fifo
221 * and decoding pipeline just like any other data. */
222 pci = dvdnav_get_current_nav_pci(dvdnav);
223 dvdnav_get_current_nav_dsi(dvdnav);
224
225 if(pci->hli.hl_gi.btn_ns > 0) {
226 int button;
227
228 printf("Found %i DVD menu buttons...\n", pci->hli.hl_gi.btn_ns);
229
230 for (button = 0; button < pci->hli.hl_gi.btn_ns; button++) {
231 const btni_t *btni = &(pci->hli.btnit[button]);
232 printf("Button %i top-left @ (%i,%i), bottom-right @ (%i,%i)\n",
233 button + 1, btni->x_start, btni->y_start,
234 btni->x_end, btni->y_end);
235 }
236
237 button = 0;
238 while ((button <= 0) || (button > pci->hli.hl_gi.btn_ns)) {
239 printf("Which button (1 to %i): ", pci->hli.hl_gi.btn_ns);
240 scanf("%i", &button);
241 }
242
243 printf("Selecting button %i...\n", button);
244 /* This is the point where applications with fifos have to hand in a NAV packet
245 * which has traveled through the fifos. See the notes above. */
246 dvdnav_button_select_and_activate(dvdnav, pci, button);
247 }
248 }
249 break;
250 case DVDNAV_HOP_CHANNEL:
251 /* This event is issued whenever a non-seamless operation has been executed.
252 * Applications with fifos should drop the fifos content to speed up responsiveness. */
253 break;
254 case DVDNAV_STOP:
255 /* Playback should end here. */
256 {
257 finished = 1;
258 }
259 break;
260 default:
261 printf("Unknown event (%i)\n", event);
262 finished = 1;
263 break;
264 }
265 #if DVD_READ_CACHE
266 dvdnav_free_cache_block(dvdnav, buf);
267 #endif
268 }
269
270 /* destroy dvdnav handle */
271 if (dvdnav_close(dvdnav) != DVDNAV_STATUS_OK) {
272 printf("Error on dvdnav_close: %s\n", dvdnav_err_to_string(dvdnav));
273 return 5;
274 }
275 close(output_fd);
276
277 return 0;
278 }