Debian Codegraph @

src/examples/bdplus_test.c

raw
1 /*
2 * This file is part of libbdplus
3 * Copyright (C) 2008-2010 Accident
4 * Copyright (C) 2013 VideoLAN
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
19 */
20
21 #if HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "libbdplus/bdplus.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #ifdef HAVE_LIBAACS
33 #include <libaacs/aacs.h>
34 #endif
35
36 static uint8_t _hex_byte(char c)
37 {
38 if (c >= '0' && c <= '9') {
39 return c - '0';
40 }
41 else if (c >= 'a' && c <= 'f') {
42 return c - 'a' + 10;
43 }
44 else if (c >= 'A' && c <= 'F') {
45 return c - 'A' + 10;
46 }
47 else {
48 fprintf(stderr, "invalid char in hex string: 0x%02X (%c)\n", c, c);
49 exit(1);
50 }
51
52 return 0;
53 }
54
55 static void _libaacs_get_vid(uint8_t *vid, uint8_t *mk, const char *root)
56 {
57 #ifndef HAVE_LIBAACS
58 fprintf(stderr, "libaacs support not enabled. Please provide VID in command line.\n");
59 exit(1);
60 #else
61 AACS *aacs = aacs_open(root, NULL);
62 unsigned ii;
63
64 if (!aacs) {
65 fprintf(stderr, "aacs_open() failed. Please provide VID in command line.\n");
66 exit(1);
67 }
68
69 const uint8_t *aacs_vid = aacs_get_vid(aacs);
70 if (!aacs_vid) {
71 fprintf(stderr, "aacs_get_vid() failed. Please provide VID in command line.\n");
72 aacs_close(aacs);
73 exit(1);
74 }
75 memcpy(vid, aacs_vid, 16);
76
77 const uint8_t *aacs_mk = aacs_get_mk(aacs);
78 if (!aacs_mk) {
79 fprintf(stderr, "aacs_get_mk() failed.\n");
80 aacs_close(aacs);
81 exit(1);
82 }
83 memcpy(mk, aacs_mk, 16);
84
85 aacs_close(aacs);
86
87 printf("got vid from libaacs: ");
88 for (ii = 0; ii < 16; ii++) {
89 printf("%02X", vid[ii]);
90 }
91 printf("\n");
92 #endif
93 }
94
95 int main(int argc, char **argv)
96 {
97 uint8_t vid[16], mk[16] = {0};
98 unsigned ii;
99
100 if (argc < 2) {
101 #ifndef HAVE_LIBAACS
102 fprintf(stderr, "%s /path/tobluray <VID>\r\n", argv[0]);
103 #else
104 fprintf(stderr, "%s /path/tobluray [VID]\r\n", argv[0]);
105 #endif
106 fprintf(stderr, "Where we expect to find /path/tobluray/BDSVM/\r\n");
107 exit(1);
108 }
109
110 if (argc < 3) {
111 _libaacs_get_vid(vid, mk, argv[1]);
112 } else {
113 if (strlen(argv[2]) != 32) {
114 fprintf(stderr, "invalid VID (should be 32 chars)\n");
115 exit(1);
116 }
117 for (ii = 0; ii < 16; ii++) {
118 vid[ii] = (_hex_byte(argv[2][2*ii]) << 4) | _hex_byte(argv[2][2*ii + 1]);
119 }
120 }
121
122 printf("Opening bdplus ...\n");
123
124 bdplus_t *bd = bdplus_init(argv[1], NULL, vid);
125
126 if (!bd) {
127 fprintf(stderr, "bdplus_init() failed\n");
128 exit(1);
129 }
130
131 int dd = bdplus_get_code_date(bd);
132 printf("BD+ content code generation %d (released %d-%02d-%02d)\n\n",
133 bdplus_get_code_gen(bd), dd >> 16, (dd >> 8) & 0xff, dd & 0xff);
134
135 bdplus_set_mk(bd, mk);
136 if (bdplus_start(bd) < 0) {
137 fprintf(stderr, "bdplus_start() failed\n");
138 }
139
140 /* Try to get conversion table without actually playing the disc. */
141 bdplus_event(bd, BDPLUS_RUN_CONVTAB, 32, 0);
142
143 for (ii = 0; ii < 32; ii++) {
144 bdplus_event(bd, BDPLUS_EVENT_TITLE, ii, 0);
145 }
146
147 for (ii = 0; ii < 32; ii++) {
148 bdplus_st_t *st = bdplus_m2ts(bd, ii);
149 if (st) {
150 printf("BD+ active for %05d.m2ts\n", ii);
151 bdplus_m2ts_close(st);
152 }
153 }
154
155 printf("Cleaning up...\n");
156
157 bdplus_free(bd);
158
159 exit(0);
160 }
161