Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[cad131]: examples / menus.c Maximize Restore History

Download this file

menus.c    279 lines (249 with data), 9.5 kB

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