libmmbd usage help.

Discussion of advanced MakeMKV functionality, expert mode, conversion profiles
Post Reply
Message
Author
javaman
Posts: 3
Joined: Mon Nov 11, 2013 11:27 pm

libmmbd usage help.

#1 Post by javaman » Mon Nov 11, 2013 11:44 pm

I was tinkering with the libmmbd and was able to create the context and open the disc but I can't seem to get the mmbd_decrypt_unit to work. The tsunit data comes back different, but the "decrypted" m2ts file is unplayable and still appears to be encrypted.

the bluray I was testing with was the original total recall.

Code: Select all

byte tsunit[TSUNIT_LENGTH];

const char * mmbdVersion = mmbd_get_version_string();
printf("Libmmd.dll version %s\r\n", mmbdVersion);

MMBD * mmbdContext = mmbd_create_context(NULL, OutputProc, NULL);

if(mmbdContext != NULL)
{
    const char* deviceid = "F:";
    const char* m2tsFile = "00002.m2ts";

    char path[MAX_PATH];
    sprintf(path, "%s\\BDMV\\STREAM\\%s", deviceid, m2tsFile);

    printf("Opening Drive %s\r\n", deviceid);
    int retval = mmbd_open(mmbdContext, deviceid);
    if(retval == 0)
    {
        printf("MKV: %d\r\n", mmbd_get_mkb_version(mmbdContext));
        const char * outputPath = "C:\\dev\\data\\test.m2ts";

        printf("Copying %s to %s\r\n", path, outputPath);
					
        ::DeleteFile(outputPath);

        FILE * input = fopen(path, "rb");
        FILE * output = fopen(outputPath, "wb");
        uint64_t fileOffset = 0;

        while (fread(tsunit, TSUNIT_LENGTH, 1, input) == 1)
        {
            retval = mmbd_decrypt_unit(mmbdContext, MMBD_FILE_M2TS, fileOffset, tsunit);
            if (retval != 0)
            {
                printf("Decrypt Failed.\r\n");
            }
            fileOffset += TSUNIT_LENGTH;
            fwrite(tsunit, TSUNIT_LENGTH, 1, output);
        }
        fclose(input);
        fclose(output);

        printf("Closing Drive %s\r\n", deviceid);
        mmbd_close(mmbdContext);
    }
    mmbd_destroy_context(mmbdContext);
}
The output of the test is

Code: Select all

Libmmd.dll version libmmbd v1.1.0
MakeMKV v1.8.6 win(x64-release) started
Debug logging enabled, log will be saved as C:\Users\vash\AppData\Local\Temp/MakeMKV-8708-1.tmp
Opening Drive F:
Using direct disc access mode
Operation successfully completed
MKV: 3
Copying F:\BDMV\STREAM\00002.m2ts to C:\dev\data\test.m2ts
Closing Drive F:
The Debug log file, which can't be accessed until mmbd_destroy_context is called which also deletes it, so I had to turn off my computer right before the mmbd_destroy_context call to preserve it contains the following.

Code: Select all

Debug log started at Mon Nov 11 20:22:30 2013 , written by MakeMKV v1.8.6 win(x64-release)
Using 524544KB for read cache.
001005:0000 MakeMKV v1.8.6 win(x64-release) started
001004:0000 Debug logging enabled, log will be saved as C:\Users\vash\AppData\Local\Temp/MakeMKV-10140-1.tmp
003007:0000 Using direct disc access mode
DAFD=PAAAADAAAAEABDQEEAQEBAQ==
DCE=PTd3BDhngQ7K3J26NZ8WKgi0m4p6g3IxzJTLuNgNl67Y8SCiCxCwlTBoFNAhYjMVNka3zk5D72DNbAULwzsaQDjb9MdLeky3uKAIWJ3k9da5jyV1WcjibMS4G3IGDa56IYnfpX5YgLpLWXh6ep6yRcRHe2TSoYIs2Q==
DISCID=6F4D5B3F496187C4CBE7CB6E6A0B526BF7D76FB2
005011:0000 Operation successfully completed
Any suggestions on what I may be doing wrong would be great.

-vash

mike admin
Posts: 4075
Joined: Wed Nov 26, 2008 2:26 am
Contact:

Re: libmmbd usage help.

#2 Post by mike admin » Tue Nov 12, 2013 4:28 am

You need to pass M2TS file ID to mmbd_decrypt_unit. Different M2TS files are encrypted by different keys. The ID is the number that is part of M2TS name. So the file 00123.M2TS will have ID 123.

Code: Select all

unsigned int m2tsId = strtoul(m2tsFile,NULL,10);
retval = mmbd_decrypt_unit(mmbdContext, MMBD_FILE_M2TS | m2tsId, fileOffset, tsunit);

javaman
Posts: 3
Joined: Mon Nov 11, 2013 11:27 pm

Re: libmmbd usage help.

#3 Post by javaman » Tue Nov 12, 2013 4:04 pm

I added the m2tsId and the file still isn't playable.

Code: Select all

byte tsunit[TSUNIT_LENGTH];

const char * mmbdVersion = mmbd_get_version_string();
printf("Libmmd.dll version %s\r\n", mmbdVersion);

MMBD * mmbdContext = mmbd_create_context(NULL, OutputProc, NULL);

if(mmbdContext != NULL)
{
    const char* deviceid = "F:";
    const char* m2tsFile = "00002";
    unsigned int m2tsId = strtoul(m2tsFile, NULL, 10);

    char path[MAX_PATH];
    sprintf(path, "%s\\BDMV\\STREAM\\%s.m2ts", deviceid, m2tsFile);

    printf("Opening Drive %s\r\n", deviceid);
    int retval = mmbd_open(mmbdContext, deviceid);
    if(retval == 0)
    {
        printf("MKV: %d\r\n", mmbd_get_mkb_version(mmbdContext));
        const char * outputPath = "C:\\dev\\data\\test.m2ts";

        printf("Copying %s to %s\r\n", path, outputPath);
					
        ::DeleteFile(outputPath);

        FILE * input = fopen(path, "rb");
        FILE * output = fopen(outputPath, "wb");
        uint64_t fileOffset = 0;

        while (fread(tsunit, TSUNIT_LENGTH, 1, input) == 1)
        {
            retval = mmbd_decrypt_unit(mmbdContext, MMBD_FILE_M2TS | m2tsId, fileOffset, tsunit);
            if (retval != 0)
            {
                printf("Decrypt Failed.\r\n");
            }
            fileOffset += TSUNIT_LENGTH;
            fwrite(tsunit, TSUNIT_LENGTH, 1, output);
        }
        fclose(input);
        fclose(output);

        printf("Closing Drive %s\r\n", deviceid);
        mmbd_close(mmbdContext);
    }
    mmbd_destroy_context(mmbdContext);
}

BlueMax
Posts: 1
Joined: Thu Nov 14, 2013 12:29 pm

Re: libmmbd usage help.

#4 Post by BlueMax » Thu Nov 14, 2013 12:33 pm

I tried this too using your sample code and the m2ts file is not playable. To me it looks like it is using the wrong key to decrypt because I see the data is different after I run it through the decryption.

mike admin
Posts: 4075
Joined: Wed Nov 26, 2008 2:26 am
Contact:

Re: libmmbd usage help.

#5 Post by mike admin » Fri Nov 15, 2013 9:51 am

TSUNIT_LENGTH is 6K? What happens if you replace mmbd_decrypt_unit with aacs_decrypt_unit (just cast MMBD to AACS) ?

javaman
Posts: 3
Joined: Mon Nov 11, 2013 11:27 pm

Re: libmmbd usage help.

#6 Post by javaman » Mon Nov 18, 2013 2:06 am

Code: Select all

#define TSUNIT_LENGTH 6144
Changed to use the aacs_decrypt function

Code: Select all

retval = aacs_decrypt_unit((AACS *)mmbdContext, tsunit);
retval is always 1 and the file still isn't playable.

Post Reply