bluray.c 3.55 KB
Newer Older
Accident's avatar
 
Accident committed
1 2 3 4 5 6 7 8 9 10 11
#if HAVE_CONFIG_H
#include "config.h"
#endif

#if HAVE_MALLOC_H
#include <malloc.h>
#endif

#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
cRTrn13's avatar
cRTrn13 committed
12

cRTrn13's avatar
cRTrn13 committed
13
#include <dlfcn.h>
cRTrn13's avatar
cRTrn13 committed
14
#include <stdio.h>
cRTrn13's avatar
cRTrn13 committed
15

cRTrn13's avatar
cRTrn13 committed
16
#include "bluray.h"
cRTrn13's avatar
cRTrn13 committed
17
#include "util/macro.h"
cRTrn13's avatar
cRTrn13 committed
18
#include "util/logging.h"
cRTrn13's avatar
cRTrn13 committed
19 20 21

BLURAY *bd_open(const char* device_path, const char* keyfile_path)
{
22
    BLURAY *bd = calloc(1, sizeof(BLURAY));
cRTrn13's avatar
cRTrn13 committed
23

cRTrn13's avatar
cRTrn13 committed
24 25 26
    if (device_path) {
        strncpy(bd->device_path, device_path, 100);

cRTrn13's avatar
cRTrn13 committed
27 28 29 30
        bd->aacs = NULL;
        bd->h_libaacs = NULL;
        bd->fp = NULL;

cRTrn13's avatar
cRTrn13 committed
31 32 33
        if (keyfile_path) {
            if ((bd->h_libaacs = dlopen("libaacs.so", RTLD_LAZY))) {
                DEBUG(DBG_BLURAY, "Downloaded libaacs (0x%08x)\n", bd->h_libaacs);
cRTrn13's avatar
cRTrn13 committed
34

cRTrn13's avatar
Ditto  
cRTrn13 committed
35 36
                fptr_p_void fptr = dlsym(bd->h_libaacs, "aacs_open");
                bd->aacs = fptr(device_path, keyfile_path);
cRTrn13's avatar
cRTrn13 committed
37 38 39 40 41 42 43 44
            } else {
                DEBUG(DBG_BLURAY, "libaacs not present\n");
            }
        } else {
            DEBUG(DBG_BLURAY, "No keyfile provided. You will not be able to make use of crypto functionality (0x%08x)\n", bd);
        }

        DEBUG(DBG_BLURAY, "BLURAY initialized! (0x%08x)\n", bd);
cRTrn13's avatar
cRTrn13 committed
45
    } else {
cRTrn13's avatar
cRTrn13 committed
46 47
        X_FREE(bd);

cRTrn13's avatar
cRTrn13 committed
48
        DEBUG(DBG_BLURAY, "No device path provided!\n");
cRTrn13's avatar
cRTrn13 committed
49 50 51 52 53 54 55
    }

    return bd;
}

void bd_close(BLURAY *bd)
{
cRTrn13's avatar
cRTrn13 committed
56
    if (bd->h_libaacs && bd->aacs) {
cRTrn13's avatar
Ditto  
cRTrn13 committed
57 58
        fptr_p_void fptr = dlsym(bd->h_libaacs, "aacs_close");
        fptr(bd->aacs);
cRTrn13's avatar
cRTrn13 committed
59

60 61
        dlclose(bd->h_libaacs);
    }
cRTrn13's avatar
cRTrn13 committed
62

cRTrn13's avatar
cRTrn13 committed
63 64 65
    if (bd->fp) {
        file_close(bd->fp);
    }
cRTrn13's avatar
cRTrn13 committed
66 67

    DEBUG(DBG_BLURAY, "BLURAY destroyed! (0x%08x)\n", bd);
cRTrn13's avatar
cRTrn13 committed
68 69

    X_FREE(bd);
cRTrn13's avatar
cRTrn13 committed
70
}
cRTrn13's avatar
cRTrn13 committed
71

cRTrn13's avatar
cRTrn13 committed
72
off_t bd_seek(BLURAY *bd, uint64_t pos)
cRTrn13's avatar
cRTrn13 committed
73 74 75 76
{
    if (pos < bd->s_size) {
        bd->s_pos = pos - (pos % 6144);

cRTrn13's avatar
cRTrn13 committed
77
        file_seek(bd->fp, bd->s_pos, SEEK_SET);
78 79

        DEBUG(DBG_BLURAY, "Seek to %ld (0x%08x)\n", bd->s_pos, bd);
cRTrn13's avatar
cRTrn13 committed
80 81 82 83 84 85 86
    }

    return bd->s_pos;
}

int bd_read(BLURAY *bd, unsigned char *buf, int len)
{
87 88 89 90 91 92 93
    if (bd->fp) {
        DEBUG(DBG_BLURAY, "Reading unit [%d bytes] at %ld... (0x%08x)\n", len, bd->s_pos, bd);

        if (len + bd->s_pos < bd->s_size) {
            int read_len;

            if ((read_len = file_read(bd->fp, buf, len))) {
cRTrn13's avatar
cRTrn13 committed
94
                if (bd->h_libaacs && bd->aacs) {
95 96 97 98 99 100 101
                    if ((bd->libaacs_decrypt_unit = dlsym(bd->h_libaacs, "aacs_decrypt_unit"))) {
                        if (!bd->libaacs_decrypt_unit(bd->aacs, buf, len, bd->s_pos)) {
                            DEBUG(DBG_BLURAY, "Unable decrypt unit! (0x%08x)\n", bd);

                            return 0;
                        }
                    }
cRTrn13's avatar
cRTrn13 committed
102 103
                }

104
                bd->s_pos += len;
cRTrn13's avatar
cRTrn13 committed
105

106 107 108 109
                DEBUG(DBG_BLURAY, "%d bytes read OK! (0x%08x)\n", read_len, bd);

                return read_len;
            }
cRTrn13's avatar
cRTrn13 committed
110 111 112
        }
    }

113 114
    DEBUG(DBG_BLURAY, "No valid title selected! (0x%08x)\n", bd->s_pos);

cRTrn13's avatar
cRTrn13 committed
115 116
    return 0;
}
cRTrn13's avatar
cRTrn13 committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130

int bd_select_title(BLURAY *bd, uint64_t title)
{
    char f_name[100];

    memset(f_name, 0, sizeof(f_name));
    snprintf(f_name, 100, "%s/BDMV/STREAM/%05ld.m2ts", bd->device_path, title);

    bd->s_size = 0;
    bd->s_pos = 0;

    if ((bd->fp = file_open(f_name, "rb"))) {
        file_seek(bd->fp, 0, SEEK_END);
        if ((bd->s_size = file_tell(bd->fp))) {
131 132 133
            bd_seek(bd, 0);

            DEBUG(DBG_BLURAY, "Title %s selected! (0x%08x)\n", f_name, bd);
cRTrn13's avatar
cRTrn13 committed
134 135 136

            return 1;
        }
137 138

        DEBUG(DBG_BLURAY, "Title %s empty! (0x%08x)\n", f_name, bd);
cRTrn13's avatar
cRTrn13 committed
139 140
    }

141 142
    DEBUG(DBG_BLURAY, "Unable to select title %s! (0x%08x)\n", f_name, bd);

cRTrn13's avatar
cRTrn13 committed
143 144
    return 0;
}
145 146 147 148 149

uint64_t bd_get_title_size(BLURAY *bd)
{
    return bd->s_size;
}
cRTrn13's avatar
cRTrn13 committed
150 151 152 153 154

uint64_t bd_tell(BLURAY *bd)
{
    return bd->s_pos;
}