Skip to content

Commit

Permalink
Introduce possibility of CPU-specific optimization
Browse files Browse the repository at this point in the history
Added Arch structure, containing function pointers to optimizable
functions. Original C implementations can be overridden at runtime with
faster, CPU-specific implementations.
  • Loading branch information
VSZS authored and agosdahu committed Feb 6, 2025
1 parent 4826ee2 commit 53d42bc
Show file tree
Hide file tree
Showing 15 changed files with 370 additions and 2 deletions.
12 changes: 10 additions & 2 deletions code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ include_directories (include)
aux_source_directory (dep_external/src/wav DIR_DEP_EXTERNAL_WAV)
aux_source_directory (src/common DIR_IAMF_COMMON)
aux_source_directory (src/iamf_dec DIR_IAMF_DEC)
aux_source_directory (src/iamf_dec/arch DIR_IAMF_DEC_ARCH)
aux_source_directory (src/iamf_dec/arch/arm DIR_IAMF_DEC_ARCH_ARM)
aux_source_directory (src/iamf_dec/arch/x86 DIR_IAMF_DEC_ARCH_X86)
if(NOT ${find_opus} STREQUAL "find_opus-NOTFOUND")
aux_source_directory (src/iamf_dec/opus DIR_IAMF_DEC_OPUS)
endif()
Expand All @@ -92,6 +95,9 @@ include_directories(
${EXTER_INCLUDE_DIR}
${PROJECT_SOURCE_DIR}/src/common
${PROJECT_SOURCE_DIR}/src/iamf_dec
${PROJECT_SOURCE_DIR}/src/iamf_dec/arch
${PROJECT_SOURCE_DIR}/src/iamf_dec/arch/arm
${PROJECT_SOURCE_DIR}/src/iamf_dec/arch/x86
${PROJECT_SOURCE_DIR}/src/iamf_dec/opus
${PROJECT_SOURCE_DIR}/src/iamf_dec/aac
${PROJECT_SOURCE_DIR}/src/iamf_dec/flac
Expand All @@ -108,7 +114,8 @@ endif()

if(BUILD_SHARED_LIBS)
add_library(${PROJECT_NAME} SHARED ${DIR_DEP_EXTERNAL_WAV} ${DIR_IAMF_COMMON}
${DIR_IAMF_DEC_OPUS} ${DIR_IAMF_DEC_AAC} ${DIR_IAMF_DEC_FLAC} ${DIR_IAMF_DEC_PCM} ${DIR_IAMF_DEC})
${DIR_IAMF_DEC_OPUS} ${DIR_IAMF_DEC_AAC} ${DIR_IAMF_DEC_FLAC} ${DIR_IAMF_DEC_PCM} ${DIR_IAMF_DEC}
${DIR_IAMF_DEC_ARCH} ${DIR_IAMF_DEC_ARCH_ARM} ${DIR_IAMF_DEC_ARCH_X86})

if(NOT ${find_opus} STREQUAL "find_opus-NOTFOUND")
target_link_libraries (${PROJECT_NAME} opus)
Expand All @@ -132,7 +139,8 @@ if(BUILD_SHARED_LIBS)
else()
add_library(${PROJECT_NAME} STATIC ${DIR_DEP_EXTERNAL_WAV} ${DIR_IAMF_COMMON}
${DIR_IAMF_DEC_OPUS} ${DIR_IAMF_DEC_AAC} ${DIR_IAMF_DEC_PCM}
${DIR_IAMF_DEC_FLAC} ${DIR_IAMF_DEC})
${DIR_IAMF_DEC_FLAC} ${DIR_IAMF_DEC} ${DIR_IAMF_DEC_ARCH} ${DIR_IAMF_DEC_ARCH_ARM}
${DIR_IAMF_DEC_ARCH_X86})
endif()


Expand Down
2 changes: 2 additions & 0 deletions code/src/iamf_dec/IAMF_decoder.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4157,6 +4157,7 @@ IAMF_DecoderHandle IAMF_decoder_open(void) {
handle->ctx.status = IAMF_DECODER_STATUS_INIT;
handle->ctx.mix_presentation_id = INVALID_ID;
handle->limiter = audio_effect_peak_limiter_create();
handle->arch = arch_create();
if (!handle->limiter || iamf_database_init(db) != IAMF_OK) {
IAMF_decoder_close(handle);
handle = 0;
Expand All @@ -4169,6 +4170,7 @@ int IAMF_decoder_close(IAMF_DecoderHandle handle) {
if (handle) {
iamf_decoder_internal_reset(handle);
if (handle->limiter) audio_effect_peak_limiter_destroy(handle->limiter);
if (handle->arch) arch_destroy(handle->arch);
free(handle);
}
#if SR
Expand Down
2 changes: 2 additions & 0 deletions code/src/iamf_dec/IAMF_decoder_private.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "IAMF_defines.h"
#include "IAMF_types.h"
#include "ae_rdr.h"
#include "arch.h"
#include "audio_effect_peak_limiter.h"
#include "demixer.h"
#include "downmix_renderer.h"
Expand Down Expand Up @@ -341,6 +342,7 @@ typedef struct IAMF_DecoderContext {
struct IAMF_Decoder {
IAMF_DecoderContext ctx;
AudioEffectPeakLimiter *limiter;
Arch *arch;
};

#endif /* IAMF_DECODER_PRIVATE_H */
36 changes: 36 additions & 0 deletions code/src/iamf_dec/arch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 3-Clause Clear License
* and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
* License was not distributed with this source code in the LICENSE file, you
* can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
* Alliance for Open Media Patent License 1.0 was not distributed with this
* source code in the PATENTS file, you can obtain it at
* www.aomedia.org/license/patent.
*/

/**
* @file arch.c
* @brief Collection of CPU-specific functions.
* @version 0.1
* @date Created 10/24/2024
**/

#include "arch.h"

#include <assert.h>

#include "IAMF_utils.h"
#include "arch/arch_init.h"

Arch* arch_create() {
Arch* arch = IAMF_MALLOCZ(Arch, 1);
memset(arch, 0x0, sizeof(Arch));

arch_init(arch);

return arch;
}

void arch_destroy(Arch* arch) { IAMF_FREE(arch); }
30 changes: 30 additions & 0 deletions code/src/iamf_dec/arch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2024, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 3-Clause Clear License
* and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
* License was not distributed with this source code in the LICENSE file, you
* can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
* Alliance for Open Media Patent License 1.0 was not distributed with this
* source code in the PATENTS file, you can obtain it at
* www.aomedia.org/license/patent.
*/

/**
* @file arch.h
* @brief Collection of CPU-specific functions.
* @version 0.1
* @date Created 10/24/2024
**/

#ifndef ARCH_H_
#define ARCH_H_

typedef struct ArchCallbacks {
// Functions with possible architecture-specific optimizations
} Arch;

Arch *arch_create();
void arch_destroy(Arch *arch);

#endif /* ARCH_H_ */
22 changes: 22 additions & 0 deletions code/src/iamf_dec/arch/arch_common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2024, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 3-Clause Clear License
* and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
* License was not distributed with this source code in the LICENSE file, you
* can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
* Alliance for Open Media Patent License 1.0 was not distributed with this
* source code in the PATENTS file, you can obtain it at
* www.aomedia.org/license/patent.
*/

/**
* @file arch_common.c
* @brief C implementation for CPU-specific functions.
* @version 0.1
* @date Created 10/24/2024
**/

#include "arch_common.h"

// TODO: Remove this line when first function added!
25 changes: 25 additions & 0 deletions code/src/iamf_dec/arch/arch_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2024, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 3-Clause Clear License
* and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
* License was not distributed with this source code in the LICENSE file, you
* can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
* Alliance for Open Media Patent License 1.0 was not distributed with this
* source code in the PATENTS file, you can obtain it at
* www.aomedia.org/license/patent.
*/

/**
* @file arch_common.h
* @brief C implementation for CPU-specific functions.
* @version 0.1
* @date Created 10/24/2024
**/

#ifndef ARCH_COMMON_H_
#define ARCH_COMMON_H_

// TODO: Remove this line when first function added!

#endif /* ARCH_COMMON_H_ */
57 changes: 57 additions & 0 deletions code/src/iamf_dec/arch/arch_init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2024, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 3-Clause Clear License
* and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
* License was not distributed with this source code in the LICENSE file, you
* can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
* Alliance for Open Media Patent License 1.0 was not distributed with this
* source code in the PATENTS file, you can obtain it at
* www.aomedia.org/license/patent.
*/

/**
* @file arch_init.c
* @brief Init CPU-specific function callbacks.
* @version 0.1
* @date Created 10/24/2024
**/

#include "arch_init.h"

#include <string.h>

#include "arch.h"
#include "arch_common.h"

// To add new architecture 'ABC', follow the following pattern:
// * add arch/ABC subfolder, with detect_ABC.h
// * inside detect_ABC.h, if ABC target detected, define IAMF_ARCH_DETECTED_ABC,
// and implement arch_override(...) function
// * all files under arch/ABC must include detect_ABC.h, and be surrounded with
// #if defined(IAMF_ARCH_DETECTED_ABC)
#include "arm/detect_arm.h"
#include "x86/detect_x86.h"
#if defined(IAMF_ARCH_DETECTED_ARM) || defined(IAMF_ARCH_DETECTED_X86)
#define HAS_ARCH_OVERRIDE (1)
#endif

#if defined(HAS_ARCH_OVERRIDE)
// Implemented in each architecture-specific subfolder,
// behind IAMF_ARCH_DETECTED_... ifdef
void arch_override(Arch* arch);
#endif

void arch_init(Arch* arch) {
memset(arch, 0x0, sizeof(Arch));

// Fill with reference implementations

// arch->myfn = &myfn_c; // myfn_c is in arch_common.h/.c
// TODO: Remove these lines when first function added!

#if defined(HAS_ARCH_OVERRIDE)
// Override with platform-specific functions, if available
arch_override(arch);
#endif
}
27 changes: 27 additions & 0 deletions code/src/iamf_dec/arch/arch_init.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2024, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 3-Clause Clear License
* and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
* License was not distributed with this source code in the LICENSE file, you
* can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
* Alliance for Open Media Patent License 1.0 was not distributed with this
* source code in the PATENTS file, you can obtain it at
* www.aomedia.org/license/patent.
*/

/**
* @file arch_init.h
* @brief Init CPU-specific function callbacks.
* @version 0.1
* @date Created 10/24/2024
**/

#ifndef ARCH_INIT_H_
#define ARCH_INIT_H_

typedef struct ArchCallbacks Arch;

void arch_init(Arch* arch);

#endif /* ARCH_INIT_H_ */
27 changes: 27 additions & 0 deletions code/src/iamf_dec/arch/arm/detect_arm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2024, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 3-Clause Clear License
* and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
* License was not distributed with this source code in the LICENSE file, you
* can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
* Alliance for Open Media Patent License 1.0 was not distributed with this
* source code in the PATENTS file, you can obtain it at
* www.aomedia.org/license/patent.
*/

/**
* @file detect_arm.h
* @brief Detect Arm architecture during compilation.
* @version 0.1
* @date Created 10/24/2024
**/

#ifndef DETECT_ARM_H_
#define DETECT_ARM_H_

#if defined(__ARM_NEON)
#define IAMF_ARCH_DETECTED_ARM (1)
#endif

#endif /* DETECT_ARM_H_ */
35 changes: 35 additions & 0 deletions code/src/iamf_dec/arch/arm/override_arm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2024, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 3-Clause Clear License
* and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
* License was not distributed with this source code in the LICENSE file, you
* can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
* Alliance for Open Media Patent License 1.0 was not distributed with this
* source code in the PATENTS file, you can obtain it at
* www.aomedia.org/license/patent.
*/

/**
* @file override_arm.c
* @brief Override with Arm function implementations.
* @version 0.1
* @date Created 10/24/2024
**/

#include "detect_arm.h"

#if defined(IAMF_ARCH_DETECTED_ARM)

#include <arm_neon.h>

#include "../../arch.h"

void arch_override(Arch *arch) {
// Override functions with Arm implementations here

// arch->myfn = &myfn_neon;
// TODO: Remove this line when first function added!
}

#endif
27 changes: 27 additions & 0 deletions code/src/iamf_dec/arch/x86/detect_x86.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2024, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 3-Clause Clear License
* and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
* License was not distributed with this source code in the LICENSE file, you
* can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
* Alliance for Open Media Patent License 1.0 was not distributed with this
* source code in the PATENTS file, you can obtain it at
* www.aomedia.org/license/patent.
*/

/**
* @file detect_x86.h
* @brief Detect x86 architecture during compilation.
* @version 0.1
* @date Created 10/24/2024
**/

#ifndef DETECT_X86_H_
#define DETECT_X86_H_

#if defined(__x86_64__) || defined(__i386__)
#define IAMF_ARCH_DETECTED_X86 (1)
#endif

#endif /* DETECT_X86_H_ */
30 changes: 30 additions & 0 deletions code/src/iamf_dec/arch/x86/override_x86.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2024, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 3-Clause Clear License
* and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
* License was not distributed with this source code in the LICENSE file, you
* can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
* Alliance for Open Media Patent License 1.0 was not distributed with this
* source code in the PATENTS file, you can obtain it at
* www.aomedia.org/license/patent.
*/

/**
* @file override_x86.c
* @brief Override with x86 function implementations.
* @version 0.1
* @date Created 10/24/2024
**/

#include "detect_x86.h"

#if defined(IAMF_ARCH_DETECTED_X86)

#include "../../arch.h"

void arch_override(Arch* arch) {
// Override functions with x86 implementations here
}

#endif /* IAMF_ARCH_DETECTED_X86 */
Loading

0 comments on commit 53d42bc

Please sign in to comment.