view menus.c @ 8:12c17225098e

add angle info and start parameter
author paulo
date Tue, 17 Dec 2013 00:49:36 -0800
parents aa7d0e4f300e
children
line source
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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 *
20 * $Id: menus.c 1135 2008-09-06 21:55:51Z rathann $
21 *
22 */
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <inttypes.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include "dvd_types.h"
31 #include <dvdread/dvd_reader.h>
32 #include <dvdread/nav_types.h>
33 #include <dvdread/ifo_types.h> /* For vm_cmd_t */
34 #include "dvdnav.h"
35 #include "dvdnav_events.h"
37 /* shall we use libdvdnav's read ahead cache? */
38 #define DVD_READ_CACHE 1
40 /* which is the default language for menus/audio/subpictures? */
41 #define DVD_LANGUAGE "en"
43 #ifdef WIN32
44 #define S_IRWXG 0
45 #endif
47 int main(int argc, char **argv) {
48 dvdnav_t *dvdnav;
49 uint8_t mem[DVD_VIDEO_LB_LEN];
50 int finished = 0;
51 int output_fd = 0;
52 int dump = 0, tt_dump = 0, tt_skip = 0;
54 /* open dvdnav handle */
55 printf("Opening DVD...\n");
56 if (dvdnav_open(&dvdnav, "/dev/dvd") != DVDNAV_STATUS_OK) {
57 printf("Error on dvdnav_open\n");
58 return 1;
59 }
61 /* set read ahead cache usage */
62 if (dvdnav_set_readahead_flag(dvdnav, DVD_READ_CACHE) != DVDNAV_STATUS_OK) {
63 printf("Error on dvdnav_set_readahead_flag: %s\n", dvdnav_err_to_string(dvdnav));
64 return 2;
65 }
67 /* set the language */
68 if (dvdnav_menu_language_select(dvdnav, DVD_LANGUAGE) != DVDNAV_STATUS_OK ||
69 dvdnav_audio_language_select(dvdnav, DVD_LANGUAGE) != DVDNAV_STATUS_OK ||
70 dvdnav_spu_language_select(dvdnav, DVD_LANGUAGE) != DVDNAV_STATUS_OK) {
71 printf("Error on setting languages: %s\n", dvdnav_err_to_string(dvdnav));
72 return 2;
73 }
75 /* set the PGC positioning flag to have position information relatively to the
76 * whole feature instead of just relatively to the current chapter */
77 if (dvdnav_set_PGC_positioning_flag(dvdnav, 1) != DVDNAV_STATUS_OK) {
78 printf("Error on dvdnav_set_PGC_positioning_flag: %s\n", dvdnav_err_to_string(dvdnav));
79 return 2;
80 }
82 /* start at title and part, if specified */
83 if (argc > 1) {
84 int start_title = atoi(argv[1]);
85 int start_part = 1;
86 if (argc > 2)
87 start_part = atoi(argv[2]);
88 dvdnav_part_play(dvdnav, start_title, start_part);
89 if (argc > 3)
90 dvdnav_angle_change(dvdnav, atoi(argv[3]));
91 }
94 /* the read loop which regularly calls dvdnav_get_next_block
95 * and handles the returned events */
96 printf("Reading...\n");
97 while (!finished) {
98 int result, event, len;
99 uint8_t *buf = mem;
101 /* the main reading function */
102 #if DVD_READ_CACHE
103 result = dvdnav_get_next_cache_block(dvdnav, &buf, &event, &len);
104 #else
105 result = dvdnav_get_next_block(dvdnav, buf, &event, &len);
106 #endif
108 if (result == DVDNAV_STATUS_ERR) {
109 printf("Error getting next block: %s\n", dvdnav_err_to_string(dvdnav));
110 return 3;
111 }
113 switch (event) {
114 case DVDNAV_BLOCK_OK:
115 /* We have received a regular block of the currently playing MPEG stream.
116 * A real player application would now pass this block through demuxing
117 * and decoding. We simply write it to disc here. */
119 if (!output_fd && (dump || tt_dump)) {
120 printf("Opening output...\n");
121 output_fd = open("libdvdnav.mpg", O_CREAT | O_WRONLY | O_APPEND, S_IRWXU | S_IRWXG);
122 if (output_fd == -1) {
123 printf("Error opening output\n");
124 return 4;
125 }
126 }
128 if (dump || tt_dump)
129 write(output_fd, buf, len);
131 break;
132 case DVDNAV_NOP:
133 /* Nothing to do here. */
134 break;
135 case DVDNAV_STILL_FRAME:
136 /* We have reached a still frame. A real player application would wait
137 * the amount of time specified by the still's length while still handling
138 * user input to make menus and other interactive stills work.
139 * A length of 0xff means an indefinite still which has to be skipped
140 * indirectly by some user interaction. */
141 {
142 dvdnav_still_event_t *still_event = (dvdnav_still_event_t *)buf;
143 if (still_event->length < 0xff)
144 printf("Skipping %d seconds of still frame\n", still_event->length);
145 else
146 printf("Skipping indefinite length still frame\n");
147 dvdnav_still_skip(dvdnav);
148 }
149 break;
150 case DVDNAV_WAIT:
151 /* We have reached a point in DVD playback, where timing is critical.
152 * Player application with internal fifos can introduce state
153 * inconsistencies, because libdvdnav is always the fifo's length
154 * ahead in the stream compared to what the application sees.
155 * Such applications should wait until their fifos are empty
156 * when they receive this type of event. */
157 printf("Skipping wait condition\n");
158 dvdnav_wait_skip(dvdnav);
159 break;
160 case DVDNAV_SPU_CLUT_CHANGE:
161 /* Player applications should pass the new colour lookup table to their
162 * SPU decoder */
163 break;
164 case DVDNAV_SPU_STREAM_CHANGE:
165 /* Player applications should inform their SPU decoder to switch channels */
166 break;
167 case DVDNAV_AUDIO_STREAM_CHANGE:
168 /* Player applications should inform their audio decoder to switch channels */
169 break;
170 case DVDNAV_HIGHLIGHT:
171 /* Player applications should inform their overlay engine to highlight the
172 * given button */
173 {
174 dvdnav_highlight_event_t *highlight_event = (dvdnav_highlight_event_t *)buf;
175 printf("Selected button %d\n", highlight_event->buttonN);
176 }
177 break;
178 case DVDNAV_VTS_CHANGE:
179 /* Some status information like video aspect and video scale permissions do
180 * not change inside a VTS. Therefore this event can be used to query such
181 * information only when necessary and update the decoding/displaying
182 * accordingly. */
183 break;
184 case DVDNAV_CELL_CHANGE:
185 /* Some status information like the current Title and Part numbers do not
186 * change inside a cell. Therefore this event can be used to query such
187 * information only when necessary and update the decoding/displaying
188 * accordingly. */
189 {
190 int32_t tt = 0, ptt = 0;
191 int32_t ac = 0, an = 0;
192 uint32_t tpos, tlen;
193 char input = '\0';
195 dvdnav_current_title_info(dvdnav, &tt, &ptt);
196 dvdnav_get_position(dvdnav, &tpos, &tlen);
197 dvdnav_get_angle_info(dvdnav, &ac, &an);
198 printf("Cell change: Title %d, Chapter %d\n", tt, ptt);
199 printf("Angle: %d (Total: %d) \n", ac, an);
200 printf("At position %.0f%% inside the feature (%d / %d) \n", 100 * (double)tpos / (double)tlen, tpos, tlen);
202 dump = 0;
203 if (tt_dump && tt != tt_dump)
204 tt_dump = 0;
206 if (tt_skip && tt != tt_skip)
207 tt_skip = 0;
209 if (output_fd && !tt_dump) {
210 printf("Closing output...\n");
211 output_fd = close(output_fd);
212 }
214 if (!dump && !tt_dump && !tt_skip) {
215 fflush(stdin);
216 while ((input != 'a') && (input != 's') && (input != 'q') && (input != 't') && (input != 'l')) {
217 printf("(a)ppend cell to output\n(s)kip cell\nappend until end of (t)itle\nskip tit(l)e\n(q)uit\n");
218 scanf("%c", &input);
219 }
221 switch (input) {
222 case 'a':
223 dump = 1;
224 break;
225 case 't':
226 tt_dump = tt;
227 break;
228 case 'l':
229 tt_skip = tt;
230 break;
231 case 'q':
232 finished = 1;
233 }
234 }
235 }
236 break;
237 case DVDNAV_NAV_PACKET:
238 /* A NAV packet provides PTS discontinuity information, angle linking information and
239 * button definitions for DVD menus. Angles are handled completely inside libdvdnav.
240 * For the menus to work, the NAV packet information has to be passed to the overlay
241 * engine of the player so that it knows the dimensions of the button areas. */
242 {
243 pci_t *pci;
245 /* Applications with fifos should not use these functions to retrieve NAV packets,
246 * they should implement their own NAV handling, because the packet you get from these
247 * functions will already be ahead in the stream which can cause state inconsistencies.
248 * Applications with fifos should therefore pass the NAV packet through the fifo
249 * and decoding pipeline just like any other data. */
250 pci = dvdnav_get_current_nav_pci(dvdnav);
251 dvdnav_get_current_nav_dsi(dvdnav);
253 if(pci->hli.hl_gi.btn_ns > 0) {
254 int button;
256 printf("Found %i DVD menu buttons...\n", pci->hli.hl_gi.btn_ns);
258 for (button = 0; button < pci->hli.hl_gi.btn_ns; button++) {
259 btni_t *btni = &(pci->hli.btnit[button]);
260 printf("Button %i top-left @ (%i,%i), bottom-right @ (%i,%i)\n",
261 button + 1, btni->x_start, btni->y_start,
262 btni->x_end, btni->y_end);
263 }
265 button = 0;
266 while ((button <= 0) || (button > pci->hli.hl_gi.btn_ns)) {
267 printf("Which button (1 to %i): ", pci->hli.hl_gi.btn_ns);
268 scanf("%i", &button);
269 }
271 printf("Selecting button %i...\n", button);
272 /* This is the point where applications with fifos have to hand in a NAV packet
273 * which has traveled through the fifos. See the notes above. */
274 dvdnav_button_select_and_activate(dvdnav, pci, button);
275 }
276 }
277 break;
278 case DVDNAV_HOP_CHANNEL:
279 /* This event is issued whenever a non-seamless operation has been executed.
280 * Applications with fifos should drop the fifos content to speed up responsiveness. */
281 break;
282 case DVDNAV_STOP:
283 /* Playback should end here. */
284 finished = 1;
285 break;
286 default:
287 printf("Unknown event (%i)\n", event);
288 finished = 1;
289 break;
290 }
291 #if DVD_READ_CACHE
292 dvdnav_free_cache_block(dvdnav, buf);
293 #endif
294 }
296 /* destroy dvdnav handle */
297 if (dvdnav_close(dvdnav) != DVDNAV_STATUS_OK) {
298 printf("Error on dvdnav_close: %s\n", dvdnav_err_to_string(dvdnav));
299 return 5;
300 }
301 close(output_fd);
303 return 0;
304 }