project('bpg', 'c',
  version: '0.9.8',
  meson_version: '>=0.49.2',
  default_options: [
	'buildtype=release',
	'b_ndebug=if-release',
  ]
)

png_dep = dependency('libpng', required: true)
jpeg_dep = dependency('libjpeg', required: true)
sdl_dep = dependency('sdl', required: get_option('BPG_USE_BPGVIEW'), disabler: true)
sdl_image_dep = dependency('SDL_image', required: get_option('BPG_USE_BPGVIEW'), disabler: true)

public_incl = include_directories('.')

add_project_arguments(
  '-D_FILE_OFFSET_BITS=64',
  '-D_LARGEFILE_SOURCE',
  '-D_REENTRANT',
  '-DCONFIG_BPG_VERSION="' + meson.project_version() + '"',

  '-Wall',
  '-MMD',
  '-fno-asynchronous-unwind-tables',
  '-fdata-sections',
  '-ffunction-sections',
  '-fno-math-errno',
  '-fno-signed-zeros',
  '-fno-tree-vectorize',
  '-fomit-frame-pointer',
  language: 'c',
)

if get_option('BPG_USE_JCTVC_HIGH_BIT_DEPTH')
	add_project_arguments('-DRExt__HIGH_BIT_DEPTH_SUPPORT', language: 'c')
endif

bpg_lib = library(meson.project_name(),
  'libavcodec/hevc_cabac.c',
  'libavcodec/hevc_filter.c',
  'libavcodec/hevc.c',
  'libavcodec/hevcpred.c',
  'libavcodec/hevc_refs.c',
  'libavcodec/hevcdsp.c',
  'libavcodec/hevc_mvs.c',
  'libavcodec/hevc_ps.c',
  'libavcodec/hevc_sei.c',
  'libavcodec/utils.c',
  'libavcodec/cabac.c',
  'libavcodec/golomb.c',
  'libavcodec/videodsp.c',

  'libavutil/mem.c',
  'libavutil/buffer.c',
  'libavutil/log2_tab.c',
  'libavutil/frame.c',
  'libavutil/pixdesc.c',
  'libavutil/md5.c',

  'libbpg.c',

  install: true,
  c_args: [
	'-D_ISOC99_SOURCE',
	'-D_POSIX_C_SOURCE=200112',
	'-D_XOPEN_SOURCE=600',
	'-DHAVE_AV_CONFIG_H',
	'-D_GNU_SOURCE=1',
	'-DUSE_VAR_BIT_DEPTH',
	'-DUSE_PRED',
  ],
)

bpg_dep = declare_dependency(
  link_with: bpg_lib,
  include_directories: public_incl,
)

if not get_option('BPG_USE_BPGVIEW').disabled()
  executable('bpgview',
	'bpgview.c',
	install: true,
	dependencies: [
	  bpg_dep,
	  sdl_dep,
	  sdl_image_dep
	]
  )
endif

bpgenc_sources = ['bpgenc.c']
bpgenc_deps = [bpg_dep, png_dep, jpeg_dep]
bpgenc_args = []
if get_option('BPG_USE_X265')
  bpgenc_sources += ['x265_glue.c']
  subdir('x265/source')
  bpgenc_deps += [x265_static_dep]
  bpgenc_args += ['-DUSE_X265']
endif

executable('bpgdec',
  'bpgdec.c',
  install: true,
  dependencies: [bpg_dep, png_dep],
)

executable('bpgenc',
  bpgenc_sources,
  install: true,
  dependencies: bpgenc_deps,
  c_args: bpgenc_args,
)
