⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 indexdb.cpp

📁 这是DVD中伺服部分的核心代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
******************************************************************************
**                                                                          **
**  Copyright (c) 2005-2006 Videon Central, Inc.                            **
**  All rights reserved.                                                    **
**                                                                          **
**  The computer program contained herein contains proprietary information  **
**  which is the property of Videon Central, Inc.  The program may be used  **
**  and/or copied only with the written permission of Videon Central, Inc.  **
**  or in accordance with the terms and conditions stipulated in the        **
**  agreement/contract under which the programs have been supplied.         **
**                                                                          **
******************************************************************************
*****************************************************************************/
/**
 * @file indexdb.cpp
 * 
 *      API to the Index Database.
 * The Index Database handles the storage of and access to Index data from the BD-ROM Index file.
 *
 * $Id: indexdb.cpp,v 1.20 2006/11/09 18:19:52 rbehe Exp $
 */

#include <string.h>
#include "vdvd_types.h"
#include "indexdb.h"
#include "mvplaylistdb.h"
#include "loader_app.h"
#include "osapi.h"
#include "utility.h"
#include "dbgprint.h"
#ifdef DMALLOC
#include "dmalloc.h"
#endif

/* Debug macros */
#define DBG_INDEX   DBG_ERROR
#define DBG_ON(x)   (DBG_INDEX >= x)

/**
 * Local handle
 */
typedef struct tagINDEX_HANDLE
{
    LOADER_HANDLE   hLoader;
    INDEX_OBJ_REF   FirstPlayback;
    INDEX_OBJ_REF   TopMenu;
    USHORT          usNumberOfTitles;
    INDEX_OBJ_REF   Title[INDEX_MAX_TITLES];
} INDEX_HANDLE;

/**
 * Local variables
 */
static INDEX_HANDLE  *hIndex = NULL;

/**
 * Local functions
 */
static INDEX_STATUS  indexLoadIndicator(LOADER_FILE_HANDLE file);
static INDEX_STATUS  indexLoadIndexes(LOADER_FILE_HANDLE file, ULONG ulStartAddress);
static INDEX_STATUS  indexLoadIndexes_FirstPlay(LOADER_FILE_HANDLE file, INDEX_OBJ_REF *pFirstPlay);
static INDEX_STATUS  indexLoadIndexes_TopMenu(LOADER_FILE_HANDLE file, INDEX_OBJ_REF *pTopMenu);
static INDEX_STATUS  indexLoadIndexes_Title(LOADER_FILE_HANDLE file, INDEX_OBJ_REF *pTitle);

/**
 * IndexCreate -- Create the index database
 *
 * @param
 *      tLoader -- handle to the loader.
 *
 * @retval
 *      INDEX_STATUS
 */
INDEX_STATUS  IndexCreate(LOADER_HANDLE tLoader)
{
    INDEX_STATUS tStatus = INDEX_SUCCESS;
    
    /* validate parameter */
    if (tLoader == NULL)
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("IndexCreate: Null pointer!\n"));
        return (INDEX_NULL_PTR);
    }

    /* If database is already created, return a failure */
    if (hIndex != NULL)
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("IndexCreate: Database already created!\n"));
        tStatus = INDEX_FAILURE;
    }
    else
    {
        /* Allocate local handle */
        hIndex = (INDEX_HANDLE *)OS_MemAlloc(sizeof(INDEX_HANDLE) );
        if (hIndex == NULL)
        {
            DBGPRINT(DBG_ON(DBG_ERROR), ("IndexCreate: Failed to create handle!\n"));
            tStatus = INDEX_FAILURE;
        }
        else
        {
            /* Keep loader handle */
            hIndex->hLoader = tLoader;
        }
    }

    return (tStatus);
}

/**
 * IndexDelete -- Delete the index database
 *
 * @param
 *      none.
 *
 * @retval
 *      INDEX_STATUS
 */
INDEX_STATUS  IndexDelete(void)
{
    INDEX_STATUS tStatus = INDEX_SUCCESS;

    /* If database has not been created, return a failure */
    if (hIndex == NULL)
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("IndexDelete: Database not created!\n"));
        tStatus = INDEX_FAILURE;
    }
    else
    {
        /* free local handle */
        OS_MemFree(hIndex);
        hIndex = NULL;
    }

    return (tStatus);
}

/**
 * IndexLoad -- Load the Index data from BDROM disc into the database.
 *
 * @param
 *      none
 *
 * @retval
 *      INDEX_STATUS
 */
INDEX_STATUS  IndexLoad(void)
{
    LOADER_FILE_HANDLE  IndexFile       = 0;
    LOADER_FILE_HANDLE  IndexFileBackup = 0;
    INDEX_STATUS        tStatus;
    UBYTE               ubData[16];
    ULONG               ulIndexStartAddr;

    /* Clear db in case previsouly loaded disc still not cleared */
    hIndex->FirstPlayback.tObjType = INDEX_OBJ_HDMV;
    hIndex->FirstPlayback.tObjectId.ulHdmvMobjID = 0xffff;
    hIndex->TopMenu.tObjType = INDEX_OBJ_HDMV;
    hIndex->TopMenu.tObjectId.ulHdmvMobjID = 0xffff;
    hIndex->usNumberOfTitles = 0;

    /* Open the Index file */
    if (LoaderFileOpen(hIndex->hLoader, "/mnt/cdrom/BDMV/index.bdmv", &IndexFile) != LOADER_SUCCESS)
    {
        IndexFile = 0;
    }

    /* Open the Index backup file */
    if (LoaderFileOpen(hIndex->hLoader, "/mnt/cdrom/BDMV/BACKUP/index.bdmv", &IndexFileBackup) != LOADER_SUCCESS)
    {
        IndexFileBackup = 0;
    }

    /* Make sure at least one file opened successfully. */
    if (IndexFile == 0)
    {
        if (IndexFileBackup == 0)
        {
            DBGPRINT(DBG_ON(DBG_ERROR), ("IndexLoad: Could not open index file!\n"));
            tStatus = INDEX_FILE_ERROR;
            goto errout;
        }
        else
        {
            /* use backup file */
            IndexFile = IndexFileBackup;
            IndexFileBackup = 0;
        }
    }
 
    /* Load the Type indicator and version number from the file */
    tStatus = indexLoadIndicator(IndexFile);
    if (tStatus != INDEX_SUCCESS)
    {
        /* Main file failed so try the backup file */
        if (IndexFileBackup == 0)
        {
            DBGPRINT(DBG_ON(DBG_ERROR), ("IndexLoad: Failed to load indicator!\n"));
            goto errout;
        }
        else
        {
            /* Load the Type indicator and version number from the backup file */
            tStatus = indexLoadIndicator(IndexFileBackup);
            if (tStatus != INDEX_SUCCESS)
            {
                DBGPRINT(DBG_ON(DBG_ERROR), ("IndexLoad: Failed to load indicator!\n"));
                goto errout;
            }
        }
    }

    /* Read the indexes start address from the file */
    if (LoaderFileRead(hIndex->hLoader, IndexFile, (PVOID)ubData, 4, NULL) != LOADER_SUCCESS)
    {
        /* Main file failed so try the backup file */
        if (IndexFileBackup == 0)
        {
            DBGPRINT(DBG_ON(DBG_ERROR), ("IndexLoad: Failed to read indexes start address!\n"));
            tStatus = INDEX_FILE_ERROR;
            goto errout;
        }
        else
        {
            /* Read the type indicator, version number, and indexes start address from the backup file */
            if (LoaderFileRead(hIndex->hLoader, IndexFileBackup, (PVOID)ubData, 4, NULL) != LOADER_SUCCESS)
            {
                DBGPRINT(DBG_ON(DBG_ERROR), ("IndexLoad: Failed to read indexes start address!\n"));
                tStatus = INDEX_FILE_ERROR;
                goto errout;
            }
        }
    }

    /* Set indexes start address */
    ulIndexStartAddr = MAKE_DWORD(&ubData[0]);

    /* Load the Indexes from the file */
    tStatus = indexLoadIndexes(IndexFile, ulIndexStartAddr);
    if (tStatus != INDEX_SUCCESS)
    {
        /* Main file failed so try the backup file */
        if (IndexFileBackup == 0)
        {
            DBGPRINT(DBG_ON(DBG_ERROR), ("IndexLoad: Failed to load indexes!\n"));
            goto errout;
        }
        else
        {
            /* Load the Indexes from the backup file */
            tStatus = indexLoadIndexes(IndexFileBackup, ulIndexStartAddr);
            if (tStatus != INDEX_SUCCESS)
            {
                DBGPRINT(DBG_ON(DBG_ERROR), ("IndexLoad: Failed to load indexes!\n"));
                goto errout;
            }
        }
    }

    /* Close the index file */
    LoaderFileClose(hIndex->hLoader, IndexFile);

    /* Close the backup file */
    if (IndexFileBackup != 0)
    {
        LoaderFileClose(hIndex->hLoader, IndexFileBackup);
    }

    return (INDEX_SUCCESS);

errout:

    if (IndexFile != 0)
    {
        LoaderFileClose(hIndex->hLoader, IndexFile);
    }

    if (IndexFileBackup != 0)
    {
        LoaderFileClose(hIndex->hLoader, IndexFileBackup);
    }

    return (tStatus);
}

/**
 * IndexGetFirstPlayback -- Get the first playback object reference.
 *
 * @param
 *      none.
 *
 * @retval
 *      Pointer to the database field containing the first playback
 *      object reference.
 */
INDEX_OBJ_REF*  IndexGetFirstPlayback(void)
{
    if (hIndex == NULL)
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("IndexGetFirstPlayback: Database not created!\n"));
        return (NULL);
    }

    return (&hIndex->FirstPlayback);
}

/**
 * IndexGetTopMenu -- Get the top menu object reference.
 *
 * @param
 *      none.
 *
 * @retval
 *      Pointer to the database field containing the top menu
 *      object reference.
 */
INDEX_OBJ_REF*  IndexGetTopMenu(void)
{
    if (hIndex == NULL)
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("IndexGetTopMenu: Database not created!\n"));
        return (NULL);
    }

    return (&hIndex->TopMenu);
}

/**
 * IndexGetNumberOfTitles -- Get the number of titles
 *
 * @param
 *      none.
 *
 * @retval
 *      Pointer to the database field containing the number of titles.
 */
USHORT*  IndexGetNumberOfTitles(void)
{
    if (hIndex == NULL)
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("IndexGetNumberOfTitles: Database not created!\n"));
        return (NULL);
    }

    return (&hIndex->usNumberOfTitles);
}

/**
 * IndexGetNumberOfTitles -- Get the number of titles
 *
 * @param
 *      none.
 *
 * @retval
 *      Pointer to the database field containing the number of titles.
 */
INDEX_OBJ_REF*  IndexGetTitle(USHORT usTitleNumber)
{
    if (hIndex == NULL)
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("IndexGetTitle: Database not created!\n"));
        return (NULL);
    }

    /* Check for valid title number */
    if (usTitleNumber > hIndex->usNumberOfTitles)
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("IndexGetTitle: Invalid title number!\n"));
        return (NULL);
    }

    return (&hIndex->Title[usTitleNumber - 1]);
}

/**
 * indexLoadIndicator -- Load Type indicator and version number from index file
 *
 * @param
 *      file -- file handle (could be main or backup file)
 *
 * @retval
 *      INDEX_STATUS
 */
static INDEX_STATUS  indexLoadIndicator(LOADER_FILE_HANDLE file)
{
    INDEX_STATUS    tStatus;
    UBYTE           ubData[8];

    /* Read the type indicator and version number from the file */
    if (LoaderFileRead(hIndex->hLoader, file, (PVOID)ubData, 8, NULL) != LOADER_SUCCESS)
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("indexLoadIndicator: Failed to read type indicator and version!\n"));
        tStatus = INDEX_FILE_ERROR;
        goto errout;
    }

    /* Check that type indicator is valid */
    if ( (ubData[0] != 'I') || (ubData[1] != 'N') || (ubData[2] != 'D') || (ubData[3] != 'X') )
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("indexLoadIndicator: Invalid type indicator!\n"));
        tStatus = INDEX_INVALID_FORMAT;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -