summaryrefslogtreecommitdiffstats
path: root/third_party/aom/tools/auto_refactor/av1_preprocess.py
blob: ea76912cf1789343286547e5444330e6dc764662 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Copyright (c) 2021, Alliance for Open Media. All rights reserved
#
# This source code is subject to the terms of the BSD 2 Clause License and
# the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
# was not distributed with this source code in the LICENSE file, you can
# obtain it at www.aomedia.org/license/software. 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.
#

import os
import sys


def is_code_file(filename):
  return filename.endswith(".c") or filename.endswith(".h")


def is_simd_file(filename):
  simd_keywords = [
      "avx2", "sse2", "sse3", "ssse3", "sse4", "dspr2", "neon", "msa", "simd",
      "x86"
  ]
  for keyword in simd_keywords:
    if filename.find(keyword) >= 0:
      return True
  return False


def get_code_file_list(path, exclude_file_set):
  code_file_list = []
  for cur_dir, sub_dir, file_list in os.walk(path):
    for filename in file_list:
      if is_code_file(filename) and not is_simd_file(
          filename) and filename not in exclude_file_set:
        file_path = os.path.join(cur_dir, filename)
        code_file_list.append(file_path)
  return code_file_list


def av1_exclude_file_set():
  exclude_file_set = {
      "cfl_ppc.c",
      "ppc_cpudetect.c",
  }
  return exclude_file_set


def get_av1_pp_command(fake_header_dir, code_file_list):
  pre_command = "gcc -w -nostdinc -E -I./ -I../ -I" + fake_header_dir + (" "
                                                                         "-D'ATTRIBUTE_PACKED='"
                                                                         " "
                                                                         "-D'__attribute__(x)='"
                                                                         " "
                                                                         "-D'__inline__='"
                                                                         " "
                                                                         "-D'float_t=float'"
                                                                         " "
                                                                         "-D'DECLARE_ALIGNED(n,"
                                                                         " typ,"
                                                                         " "
                                                                         "val)=typ"
                                                                         " val'"
                                                                         " "
                                                                         "-D'volatile='"
                                                                         " "
                                                                         "-D'AV1_K_MEANS_DIM=2'"
                                                                         " "
                                                                         "-D'INLINE='"
                                                                         " "
                                                                         "-D'AOM_INLINE='"
                                                                         " "
                                                                         "-D'AOM_FORCE_INLINE='"
                                                                         " "
                                                                         "-D'inline='"
                                                                         )
  return pre_command + " " + " ".join(code_file_list)


def modify_av1_rtcd(build_dir):
  av1_rtcd = os.path.join(build_dir, "config/av1_rtcd.h")
  fp = open(av1_rtcd)
  string = fp.read()
  fp.close()
  new_string = string.replace("#ifdef RTCD_C", "#if 0")
  fp = open(av1_rtcd, "w")
  fp.write(new_string)
  fp.close()


def preprocess_av1(aom_dir, build_dir, fake_header_dir):
  cur_dir = os.getcwd()
  output = os.path.join(cur_dir, "av1_pp.c")
  path_list = [
      os.path.join(aom_dir, "av1/encoder"),
      os.path.join(aom_dir, "av1/common")
  ]
  code_file_list = []
  for path in path_list:
    path = os.path.realpath(path)
    code_file_list.extend(get_code_file_list(path, av1_exclude_file_set()))
  modify_av1_rtcd(build_dir)
  cmd = get_av1_pp_command(fake_header_dir, code_file_list) + " >" + output
  os.chdir(build_dir)
  os.system(cmd)
  os.chdir(cur_dir)


if __name__ == "__main__":
  aom_dir = sys.argv[1]
  build_dir = sys.argv[2]
  fake_header_dir = sys.argv[3]
  preprocess_av1(aom_dir, build_dir, fake_header_dir)