libmmbd with kodi18

The place to discuss linux version of MakeMKV
G0llum
Posts: 5
Joined: Sat Mar 24, 2018 8:45 am

Re: libmmbd with kodi18

Post by G0llum »

I applied your changes. However, I am getting the below error after make

Code: Select all

libmmbd/src/aacs.cpp: In function 'void aacs_set_fopen(AACS*, void*, void*)':
libmmbd/src/aacs.cpp:300:60: error: 'bd_disc_structure_pointer' was not declared in this scope
    BD_DISC bd_disc_structure = *reinterpret_cast<BD_DISC*>(bd_disc_structure_pointer); // Copy the BD_DISC structure
                                                            ^
Makefile:84: recipe for target 'out/libmmbd.so.0.full' failed
make: *** [out/libmmbd.so.0.full] Error 1
I double checked all changes were in the correct places. Anything else missing?
dgktkr
Posts: 18
Joined: Thu Dec 30, 2010 7:51 pm

Re: libmmbd with kodi18

Post by dgktkr »

My bad. Sorry about that.

The first line of the definition of aacs_set_fopen() should be:

Code: Select all

AACS_PUBLIC void __cdecl aacs_set_fopen(AACS *aacs, void *bd_disc_structure_pointer, void* p)
I've edited my instructions to correct this omission.

dgktkr
G0llum
Posts: 5
Joined: Sat Mar 24, 2018 8:45 am

Re: libmmbd with kodi18

Post by G0llum »

Thanks for your quick feedback. I've adjusted the changes accordingly. With this I can now successfully build MakeMKV.

Will test how it works with kodi over the next days.

I assembled all changes into the below patch file, which you can apply with

Code: Select all

patch -p1 < patch_file
in the makemkv-oss-1.12.0 folder.
Here's the content of the patch_file:

Code: Select all

--- a/libmmbd/src/aacs.cpp
+++ b/libmmbd/src/aacs.cpp
@@ -23,6 +23,109 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <string>
+
+typedef struct bd_mutex_s BD_MUTEX;
+struct bd_mutex_s {
+    void *impl;
+};
+
+
+typedef void    (*fptr_void)(...);
+typedef int     (*fptr_int)(...);
+typedef int32_t (*fptr_int32)(...);
+typedef void*   (*fptr_p_void)(...);
+
+typedef struct bd_aacs BD_AACS;
+
+struct bd_aacs {
+    void           *h_libaacs;   /* library handle from dlopen */
+    void           *aacs;        /* aacs handle from aacs_open() */
+
+    const uint8_t *disc_id;
+    uint32_t       mkbv;
+
+    /* function pointers */
+    fptr_int       decrypt_unit;
+    fptr_int       decrypt_bus;
+
+    int            impl_id;
+};
+
+typedef struct bd_bdplus BD_BDPLUS;
+
+struct bd_bdplus {
+    void           *h_libbdplus; /* library handle from dlopen */
+
+    void           *bdplus;      /* bdplus handle from bdplus_open() */
+
+    /* functions */
+    fptr_int32     event;
+    fptr_p_void    m2ts;
+    fptr_int32     m2ts_close;
+    fptr_int32     seek;
+    fptr_int32     fixup;
+
+    /* old API */
+    fptr_p_void    title;
+
+    int impl_id;
+};
+
+typedef struct bd_dec BD_DEC;
+struct bd_dec {
+    int        use_menus;
+    BD_AACS   *aacs;
+    BD_BDPLUS *bdplus;
+};
+
+
+typedef struct bd_file_s BD_FILE_H;
+struct bd_file_s
+{
+    void* internal;
+    void    (*close) (BD_FILE_H *file);
+    int64_t (*seek)  (BD_FILE_H *file, int64_t offset, int32_t origin);
+    int64_t (*tell)  (BD_FILE_H *file);
+    int     (*eof)   (BD_FILE_H *file);
+    int64_t (*read)  (BD_FILE_H *file, uint8_t *buf, int64_t size);
+    int64_t (*write) (BD_FILE_H *file, const uint8_t *buf, int64_t size);
+};
+
+typedef struct
+{
+    char    d_name[256];
+} BD_DIRENT;
+
+typedef struct bd_dir_s BD_DIR_H;
+struct bd_dir_s
+{
+    void* internal;
+    void (*close)(BD_DIR_H *dir);
+    int (*read)(BD_DIR_H *dir, BD_DIRENT *entry);
+};
+
+typedef struct bd_disc BD_DISC;
+
+struct bd_disc {
+    BD_MUTEX  ovl_mutex;     /* protect access to overlay root */
+    BD_MUTEX  properties_mutex; /* protect access to properties file */
+
+    char     *disc_root;     /* disc filesystem root (if disc is mounted) */
+    char     *overlay_root;  /* overlay filesystem root (if set) */
+
+    BD_DEC   *dec;
+
+    void         *fs_handle;
+    BD_FILE_H * (*pf_file_open_bdrom)(void *, const char *);
+    BD_DIR_H *  (*pf_dir_open_bdrom)(void *, const char *);
+    void        (*pf_fs_close)(void *);
+
+    const char   *udf_volid;
+    char         *properties_file;  /* NULL if not yet used */
+
+    int8_t        avchd;  /* -1 - unknown. 0 - no. 1 - yes */
+};

 /*
     WARNING!
@@ -97,6 +200,8 @@
     return (AACS*)mmbd;
 }

+static std::string *cxx_string_pointer;  // declare a persistent file scope variable
+
 AACS_PUBLIC int __cdecl aacs_open_device(AACS *aacs, const char *path, const char *keyfile_path)
 {
     if (keyfile_path) {
@@ -110,7 +215,8 @@
         }
     }

-    if (mmbd_open((MMBD*)aacs,path)) {
+    const char * c_string_path = (*cxx_string_pointer).c_str(); // convert to the C string we're after
+    if (mmbd_open((MMBD*)aacs,c_string_path)) {  // change path to c_string_path
         return AACS_ERROR_CORRUPTED_DISC;
     }

@@ -189,8 +295,11 @@
     return (const uint8_t *)"mmbd_fake_aacs_get_bdj_root_cert_hash";
 }

-AACS_PUBLIC void __cdecl aacs_set_fopen(AACS *aacs, void *handle, void* p)
+AACS_PUBLIC void __cdecl aacs_set_fopen(AACS *aacs, void *bd_disc_structure_pointer, void* p)
 {
+   BD_DISC bd_disc_structure = *reinterpret_cast<BD_DISC*>(bd_disc_structure_pointer); // Copy the BD_DISC structure
+
+   cxx_string_pointer = reinterpret_cast<std::string*>(bd_disc_structure.fs_handle); // Copy the C++ string object pointer of interest
 }

 static void* file_open = NULL;
--- a/libmmbd/src/aacs.h
+++ b/libmmbd/src/aacs.h
@@ -197,7 +197,7 @@

 AACS_PUBLIC struct aacs_basic_cci * __cdecl aacs_get_basic_cci(AACS *, uint32_t title);

-AACS_PUBLIC void __cdecl aacs_set_fopen(AACS *aacs, void *handle, void* p);
+AACS_PUBLIC void __cdecl aacs_set_fopen(AACS *aacs, void *bd_disc_structure_pointer, void* p);
 AACS_PUBLIC void* __cdecl aacs_register_file(void* p);

 #ifdef __cplusplus
--- q/libmmbd/src/bdplus.cpp
+++ b/libmmbd/src/bdplus.cpp
@@ -44,6 +44,12 @@
     uint64_t    offset;
 } BDPLUS_CTX;

+// provide a minimal bdplus_set_fopen()
+AACS_PUBLIC BDPLUS_CTX* __cdecl bdplus_set_fopen(BDPLUS_CTX* ctx, void *bd_disc_structure_pointer, void* p)
+{
+    return ctx;
+}
+
 /*
     This function requires aacs_vid value produced by aacs_get_vid, not an actual disc VID.
 */
--- a/libmmbd/src/bdplus.h
+++ b/libmmbd/src/bdplus.h
@@ -29,6 +29,7 @@
 extern "C" {
 #endif

+AACS_PUBLIC BDPLUS_CTX* __cdecl bdplus_set_fopen(BDPLUS_CTX* ctx, void *bd_disc_structure_pointer, void* p);
 AACS_PUBLIC BDPLUS_CTX* __cdecl bdplus_init(const char *path, const char *keyfile_path,const uint8_t* aacs_vid);
 AACS_PUBLIC int __cdecl bdplus_free(BDPLUS_CTX* ctx);

--- a/libmmbd/src/libmmbd.vers
+++ b/libmmbd/src/libmmbd.vers
@@ -33,6 +33,7 @@
   aacs_get_device_nonce;
   aacs_get_bus_encryption;
   aacs_register_file;
+  bdplus_set_fopen;
   bdplus_init;
   bdplus_free;
   bdplus_set_title;
G0llum
Posts: 5
Joined: Sat Mar 24, 2018 8:45 am

Re: libmmbd with kodi18

Post by G0llum »

Only partial success so far. With the changes applied I can play some BDs while attempting to play back another initially crashed kodi. After a few tries and playing with the settings inside kodi I can now play back all BDs from kodi - with all settings back to where they initially were, which is kind of strange.

The worst is, that after each BD playback, one or more instances of makemkvcon remain as defunct in my process list.
Since I'm monitoring my system for background processes this keeps my pc from going to sleep.

I can probably tune my monitoing to ignore defunct processes but it's still kinda ugly to have these zombie makemkvcon processes piling up.

Anyone have a cure for this?
TheUlpio
Posts: 2
Joined: Sun Jan 15, 2017 4:14 pm

Re: libmmbd with kodi18

Post by TheUlpio »

@G0llum: i've applied your patch based on the great work of @dgktkr and, after some days of intensive use, i never seen any kind of problem.

I can play a bd, stop it, change with a bd+ and play it without any issue.

After a heavy sequence of play bd, stop it, change disk, play it... I didn't noticed any defunct makemkvcon process nor kodi crashes.

I tested this on my self compiled LibreELEC cloned from git (the last today), with kodi versions 18.0-ALPHA2 Git:9af16f4 and 18.0-ALPHA2 Git:72f69fc on a x86_64 platform (athlon X2 245e), with a Nvidia 730GT using either top and ps commands.

I use my own kodi settings (VDPAU) and never need to tune them after the patch

If you have more tips on how to reproduce your problem i'll be happy to test and compare results.
steo86
Posts: 8
Joined: Mon Dec 04, 2017 11:26 am

Re: libmmbd with kodi18

Post by steo86 »

Hi have just installed kodi 18 via nightly-build ppa and compiled build makemkv from scratch
as followed:

Code: Select all

wget http://www.makemkv.com/download/makemkv-bin-1.12.0.tar.gz
wget http://www.makemkv.com/download/makemkv-oss-1.12.0.tar.gz

sudo apt-get install build-essential pkg-config libc6-dev libssl-dev libexpat1-dev libavcodec-dev libgl1-mesa-dev libqt4-dev zlib1g-dev

tar -xzf makemkv-oss-1.12.0.tar.gz
cd ./makemkv-oss-1.12.0
sudo nano ./patch_file
---
paste Content from here:
http://makemkv.com/forum2/viewtopic.php?f=3&t=16824&sid=c50aa1bc707de1a23e41303bd756c447&start=15#p61629
---
patch -p1 < patch_file
./configure
make
sudo make install

cd ..
tar -xzf makemkv-bin-1.12.0.tar.gz
cd ./makemkv-bin-1.12.0
make
sudo make install
But if i click on the index.bdmv or movieobject.bdmv in kodi18, kodi 18 still crashes (restarts).
Did i anything wrong? How can I debug this?
mosquitogang201
Posts: 5
Joined: Sat Dec 23, 2017 3:43 am

Re: libmmbd with kodi18

Post by mosquitogang201 »

I have tested the patch and can confirm that this gets blurays working in kodi 18, but I see two issues. Defunct instances of makemkvcon hang around. Also, the patch breaks bluray playback with mpv, and probably with other applications that use the older libbluray api, ie not bd_open_files(). Still though, I think this is a proof of concept that we can fix this within makemkv.

EDIT: I have attached a modified patch file. I moved the new declarations in aacs.cpp over to aacs.h. The main change, I edited aacs_open_device() so that if *path is NULL, we use the new *cxx_string_pointer; otherwise we don't touch the *path that is provided to mmbd_open. This works with Kodi and works with mpv. I still get the zombie makemkvcon, but it's only when loading bluray from kodi, using *cxx_string_pointer. Bluray with mpv does not leave a zombie process.

EDIT 2: I don't think this patch is the source of the zombie makemkvcon. I previously had hardcoded a path for mmbd_open(), and that method with kodi results in zombie processes too. Not sure how to debug, or if it can even be fixed within libmmbd.

EDIT 3: Should not have moved the static variable declaration to aacs.h... fixed.
Attachments
kodi_mmbd-v3_patch.txt
(6.14 KiB) Downloaded 2906 times
Last edited by mosquitogang201 on Sat Apr 14, 2018 9:56 pm, edited 1 time in total.
christian
Posts: 5
Joined: Wed Apr 04, 2018 10:55 am

Re: libmmbd with kodi18

Post by christian »

@TheUlpio

How do you build makemkv as an add-on to work in libreelec?

I compiled makemkv on ubuntu with the new patch in this thread. Then I copied the binary and the libmmbd in my existing add-on directory on libreelec. But its not working. I got the message playback is not possible because of AACS. What I´m doing wrong?

Edit: Its working now. I build the complete add-on with a new compiled makemkv-oss source.
It´s working with the latest Libreelec nightly.
TheUlpio
Posts: 2
Joined: Sun Jan 15, 2017 4:14 pm

Re: libmmbd with kodi18

Post by TheUlpio »

christian wrote:@TheUlpio

How do you build makemkv as an add-on to work in libreelec?

I compiled makemkv on ubuntu with the new patch in this thread. Then I copied the binary and the libmmbd in my existing add-on directory on libreelec. But its not working. I got the message playback is not possible because of AACS. What I´m doing wrong?

Edit: Its working now. I build the complete add-on with a new compiled makemkv-oss source.
It´s working with the latest Libreelec nightly.
I don't build makemkv as an addon, i build it in-tree using my own package.mk. Building the addon in an host distro involves to set-up a cross compile environment imho. Finally i'm glad to ear that your addon is working :)

To return in topic, can you check if the disk playback on your system originates zombie makemkvcon processes?
christian
Posts: 5
Joined: Wed Apr 04, 2018 10:55 am

Re: libmmbd with kodi18

Post by christian »

I can confirm that libreelec 9 generates 2 processes of makemkvcon.bin per movie. The first process is by reading the titles of the bluray and the second by starting the main movie. When I watch a second movie it also generates two of them. So after two movies I have four processes.

But I don't know how it was on kodi 17.6. Maybe the same. Maybe someone can test it on kodi 17.6 or on linux with VLC Player by changing the vlc library with the makemkv library.

Edit:
For Bluray Menus. I can confirm that they are also working with libmmbd. I have installed java and Libbluray-j2se-1.0.1 as an add-on in libreelec 9.0. For instructions search in the libreelec forum. Its only working on generic systems.
christian
Posts: 5
Joined: Wed Apr 04, 2018 10:55 am

Re: libmmbd with kodi18

Post by christian »

After updating to Milhouse Build #0425 the makemkv does not work anymore. With version #0424 everything is fine.
Anyone with the same problem?
G0llum
Posts: 5
Joined: Sat Mar 24, 2018 8:45 am

Re: libmmbd with kodi18

Post by G0llum »

I am not using libreelec but I was able to compile latest MakeMKV on Ubuntu 16.04 on top of kodi 18 built from sources on April 29. It works with DVDs but BD playback is broken as with the previous unpatched release.

I haven't looked yet at the MakeMKV sources, but I was hoping it would contain some fix to address the broken BD playback with kodi. Not sure if the patch can be applied unchanged also to the latest code.
christian
Posts: 5
Joined: Wed Apr 04, 2018 10:55 am

Re: libmmbd with kodi18

Post by christian »

The patch is also working with version 1.12.2
xoocoon
Posts: 10
Joined: Sun Mar 14, 2021 6:02 pm

libmmbd with kodi18.7 on Raspberry Pi4

Post by xoocoon »

Finally, I got this working on a Raspberry Pi 4B, i.e. I can play Blurays with Kodi + makemkv, flawlessly so far. 3D Blurays (h.264/MVC) also work. And I have no makemkvcon processes hanging so far. I hope that it stays that way and that I did not miss anything in my excitement.

My procedure:
1. Installed libfdk (needed for building ffmpeg). As it does not seem to be available in the curated Raspbian repository, I downloaded and installed the pre-built .deb packages for armhf from http://ftp.de.debian.org/debian/pool/no ... f/fdk-aac/

2. Downloaded the source of the latest ffmpeg release and build it:

Code: Select all

./configure --prefix=/tmp/ffmpeg --enable-static --disable-shared --enable-pic --enable-libfdk-aac
make install # takes more than an hour
3. Installed the prerequisites for building makemkv-oss:

Code: Select all

sudo apt update && sudo apt-get install build-essential pkg-config libc6-dev libssl-dev libexpat1-dev libavcodec-dev libgl1-mesa-dev qtbase5-dev zlib1g-dev
4. Downloaded the makemkv-oss source, unpacked it and patched it with the patch file from this forum.

Code: Select all

wget https://www.makemkv.com/download/makemkv-oss-1.16.1.tar.gz
tar -xvf makemkv-oss-1.16.1
cd makemkv-oss-1.16.1
wget https://forum.makemkv.com/forum/download/file.php?id=1356 -O kodi_mmbd-v3_patch.txt
patch -p1 < kodi_mmbd-v3_patch.txt
5. Then I had to patch the patch as there were duplicate methods bdplus_set_fopen in the files bdplus.cpp and bdplus.h. To resolve this, I commented out the old version of the method and left only the new one with the *bd_disc_structure_pointer in the signature.

6. Then I could build makemkv-oss:

Code: Select all

PKG_CONFIG_PATH=/tmp/ffmpeg/lib/pkgconfig ./configure
make --always-make
sudo make install
7. Downloading, building and installing makemkv-bin was the straight-forward:

Code: Select all

get https://www.makemkv.com/download/makemkv-bin-1.16.1.tar.gz
tar -xvf makemkv-bin-1.16.1.tar.gz
cd makemkv-bin-1.16.1
make
sudo make install
8. Then I had to link the libmmbd libray built with makemkv to the libaacs and libbdplus libraries:

Code: Select all

cd /usr/lib
sudo unlink libaacs.so.0
sudo unlink libbdplus.so.0
sudo ln -s /usr/lib/libmmbd.so.0 ./libaacs.so.0
sudo ln -s /usr/lib/libmmbd.so.0 ./libbdplus.so.0
9. At this point, I was not really able to play Blurays in Kodi. However, it was better than in my previous attempts when Kodi crashed and restarted. Now Kodi started reading the Bluray at least and presented a menu for Bluray playback options. But when choosing "Play main movie" nothing happened. Next, I enabled Java menus:

Code: Select all

sudo apt update && sudo apt install default-jdk
# retrieve installation path
JAVA_BIN_ABSPATH=$( sudo update-alternatives --config java | grep -oP '/usr/lib\S+' )
sudo cp /etc/profile /etc/profile.orig
sudo cp /etc/environment /etc/environment.orig
cat << EOF | sudo tee -a /etc/environment
JAVA_HOME=${JAVA_BIN_ABSPATH%%/bin/java}
EOF
sudo sed -i 's/PATH="\([^"]\+\)"/PATH="\1:\$JAVA_HOME\/bin"/g' /etc/profile
sudo apt-get install libbluray-bdj libbluray-bin
10. Last but not least I created a ~/.MakeMKV/settings.conf file for the user running Kodi. It contains some basic settings and the license key, e.g.

Code: Select all

app_DefaultSelectionString = "-sel:all,+sel:(favlang|nolang),+sel:mvcvideo,=100:all,-10:favlang"
app_DestinationDir = "/media/Videos"
app_DestinationType = "3"
app_ExpertMode = "1"
app_Java = ""
app_Key = "9834nfdkj4023fe~fk3rjkfwe"
app_PreferredLanguage = "ger"
app_Proxy = ""
app_ccextractor = ""
sdf_Stop = ""
speed_HL-DT-ST_BD-RE_BU50N = ""
11. After restarting the system and Kodi I was able to play Bluerays including the disc menu in Kodi. Please note that the Bluray drive must be mounted beforehand, which is different from playing DVDs which do not need to be mounted beforehand.

If anything breaks in the next couple of weeks, I will report it here. But for now it works. Enjoy!
gebix
Posts: 1
Joined: Fri Mar 26, 2021 11:24 pm

Re: libmmbd with kodi18

Post by gebix »

Hi,

just tried to apply the patch from above and run into some problems. I'm under the actual version 1.16.3. Would be great if somebody could post an updated patch.

patching file libmmbd/src/aacs.cpp
Hunk #2 succeeded at 142 with fuzz 1 (offset 44 lines).
Hunk #3 FAILED at 113.
Hunk #4 FAILED at 192.
2 out of 4 hunks FAILED -- saving rejects to file libmmbd/src/aacs.cpp.rej
patching file libmmbd/src/aacs.h
Hunk #1 FAILED at 197.
1 out of 1 hunk FAILED -- saving rejects to file libmmbd/src/aacs.h.rej
patching file libmmbd/src/bdplus.cpp
patching file libmmbd/src/bdplus.h
patching file libmmbd/src/libmmbd.vers

and MAKE gives me:

In file included from libmmbd/src/bdplus.cpp:23:
libmmbd/src/bdplus.h:40:25: error: conflicting declaration of C function ‘int bdplus_set_fopen(BDPLUS_CTX*, void*, void*)’
40 | AACS_PUBLIC int __cdecl bdplus_set_fopen(BDPLUS_CTX* ctx,void *handle,void* p);
| ^~~~~~~~~~~~~~~~
libmmbd/src/bdplus.h:32:33: note: previous declaration ‘BDPLUS_CTX* bdplus_set_fopen(BDPLUS_CTX*, void*, void*)’
32 | AACS_PUBLIC BDPLUS_CTX* __cdecl bdplus_set_fopen(BDPLUS_CTX* ctx, void *bd_disc_structure_pointer, void* p);
| ^~~~~~~~~~~~~~~~
libmmbd/src/bdplus.cpp:48:33: error: ambiguating new declaration of ‘BDPLUS_CTX* bdplus_set_fopen(BDPLUS_CTX*, void*, void*)’
48 | AACS_PUBLIC BDPLUS_CTX* __cdecl bdplus_set_fopen(BDPLUS_CTX* ctx, void *bd_disc_structure_pointer, void* p)
| ^~~~~~~~~~~~~~~~
In file included from libmmbd/src/bdplus.cpp:23:
libmmbd/src/bdplus.h:40:25: note: old declaration ‘int bdplus_set_fopen(BDPLUS_CTX*, void*, void*)’
40 | AACS_PUBLIC int __cdecl bdplus_set_fopen(BDPLUS_CTX* ctx,void *handle,void* p);
| ^~~~~~~~~~~~~~~~
make: *** [Makefile:98: out/libmmbd.so.0.full] Error 1
Post Reply