#
# Copyright (C) 2014-2025 Amlogic, Inc. All rights reserved.
#
# All information contained herein is Amlogic confidential.
#
# This software is provided to you pursuant to Software License Agreement
# (SLA) with Amlogic Inc ("Amlogic"). This software may be used
# only in accordance with the terms of this agreement.
#
# Redistribution and use in source and binary forms, with or without
# modification is strictly prohibited without prior written permission from
# Amlogic.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#


cmake_minimum_required(VERSION 3.5)

project(subtitle)


set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++17 -pthread")

include_directories(subtitle/render/textrender)
set(textrender_SRCS
        subtitle/render/textrender/draw_context.cpp
        subtitle/render/textrender/pen.cpp
        subtitle/render/textrender/shape.cpp
        subtitle/render/textrender/surface.cpp
        subtitle/render/textrender/text.cpp
        subtitle/render/textrender/rectangle.cpp
        subtitle/render/textrender/round_rectangle.cpp
        subtitle/render/textrender/FontSelector.cpp
    )

include_directories(../external/libaribcaption/include)
include_directories(../external/libaribcaption/src)
set(aribcaption_SRCS
        ../external/libaribcaption/include/aribcaption/aligned_alloc.hpp
        ../external/libaribcaption/include/aribcaption/aribcaption.h
        ../external/libaribcaption/include/aribcaption/aribcaption.hpp
        ../external/libaribcaption/include/aribcaption/aribcc_export.h
        ../external/libaribcaption/include/aribcaption/caption.h
        ../external/libaribcaption/include/aribcaption/caption.hpp
        ../external/libaribcaption/include/aribcaption/color.h
        ../external/libaribcaption/include/aribcaption/color.hpp
        ../external/libaribcaption/include/aribcaption/context.h
        ../external/libaribcaption/include/aribcaption/context.hpp
        ../external/libaribcaption/include/aribcaption/decoder.h
        ../external/libaribcaption/include/aribcaption/decoder.hpp
        ../external/libaribcaption/src/base/aligned_alloc.cpp
        ../external/libaribcaption/src/base/always_inline.hpp
        ../external/libaribcaption/src/base/cfstr_helper.hpp
        ../external/libaribcaption/src/base/floating_helper.hpp
        ../external/libaribcaption/src/base/language_code.hpp
        ../external/libaribcaption/src/base/logger.cpp
        ../external/libaribcaption/src/base/logger.hpp
        ../external/libaribcaption/src/base/md5.c
        ../external/libaribcaption/src/base/md5.h
        ../external/libaribcaption/src/base/md5_helper.hpp
        ../external/libaribcaption/src/base/result.hpp
        ../external/libaribcaption/src/base/scoped_cfref.hpp
        ../external/libaribcaption/src/base/scoped_com_initializer.hpp
        ../external/libaribcaption/src/base/scoped_holder.hpp
        ../external/libaribcaption/src/base/unicode_helper.hpp
        ../external/libaribcaption/src/base/utf_helper.hpp
        ../external/libaribcaption/src/base/wchar_helper.hpp
        ../external/libaribcaption/src/common/caption_capi.cpp
        ../external/libaribcaption/src/common/context.cpp
        ../external/libaribcaption/src/common/context_capi.cpp
        ../external/libaribcaption/src/decoder/b24_codesets.cpp
        ../external/libaribcaption/src/decoder/b24_codesets.hpp
        ../external/libaribcaption/src/decoder/b24_colors.cpp
        ../external/libaribcaption/src/decoder/b24_colors.hpp
        ../external/libaribcaption/src/decoder/b24_controlsets.hpp
        ../external/libaribcaption/src/decoder/b24_conv_tables.hpp
        ../external/libaribcaption/src/decoder/b24_drcs_conv.cpp
        ../external/libaribcaption/src/decoder/b24_drcs_conv.hpp
        ../external/libaribcaption/src/decoder/b24_gaiji_table.hpp
        ../external/libaribcaption/src/decoder/b24_macros.hpp
        ../external/libaribcaption/src/decoder/decoder.cpp
        ../external/libaribcaption/src/decoder/decoder_capi.cpp
        ../external/libaribcaption/src/decoder/decoder_impl.cpp
        ../external/libaribcaption/src/decoder/decoder_impl.hpp
    )

include_directories(../external/libzvbisubtitle/src)
set(zvbi_SRCS
        ../external/libzvbisubtitle/src/bit_slicer.c
        ../external/libzvbisubtitle/src/cache.c
        ../external/libzvbisubtitle/src/caption.c
        ../external/libzvbisubtitle/src/conv.c
        ../external/libzvbisubtitle/src/dvb_mux.c
        ../external/libzvbisubtitle/src/dvb_demux.c
        ../external/libzvbisubtitle/src/exp-html.c
        ../external/libzvbisubtitle/src/exp-templ.c
        ../external/libzvbisubtitle/src/exp-txt.c
        ../external/libzvbisubtitle/src/exp-vtx.c
        ../external/libzvbisubtitle/src/exp-gfx.c
        ../external/libzvbisubtitle/src/export.c
        ../external/libzvbisubtitle/src/hamm.c
        ../external/libzvbisubtitle/src/idl_demux.c
        ../external/libzvbisubtitle/src/io.c
        ../external/libzvbisubtitle/src/io-bktr.c
        ../external/libzvbisubtitle/src/io-dvb.c
        ../external/libzvbisubtitle/src/io-v4l.c
        ../external/libzvbisubtitle/src/io-v4l2.c
        ../external/libzvbisubtitle/src/io-v4l2k.c
        ../external/libzvbisubtitle/src/lang.c
        ../external/libzvbisubtitle/src/misc.c
        ../external/libzvbisubtitle/src/packet.c
        ../external/libzvbisubtitle/src/teletext.c
        ../external/libzvbisubtitle/src/page_table.c
        ../external/libzvbisubtitle/src/pfc_demux.c
        ../external/libzvbisubtitle/src/proxy-client.c
        ../external/libzvbisubtitle/src/raw_decoder.c
        ../external/libzvbisubtitle/src/sampling_par.c
        ../external/libzvbisubtitle/src/search.c
        ../external/libzvbisubtitle/src/ure.c
        ../external/libzvbisubtitle/src/sliced_filter.c
        ../external/libzvbisubtitle/src/tables.c
        ../external/libzvbisubtitle/src/trigger.c
        ../external/libzvbisubtitle/src/vbi.c
        ../external/libzvbisubtitle/src/vps.c
        ../external/libzvbisubtitle/src/wss.c
        ../external/libzvbisubtitle/src/xds_demux.c
        ../external/libzvbisubtitle/src/decoder.c
        ../external/libzvbisubtitle/src/dtvcc.c
    )


include_directories(../external/libfreetype2subtitle/include)
include_directories(../external/libfreetype2subtitle/src)
set(freetype2subtitle_SRCS
        ../external/libfreetype2subtitle/src/autofit/autofit.c
        ../external/libfreetype2subtitle/src/base/ftbase.c
        ../external/libfreetype2subtitle/src/base/ftbbox.c
        ../external/libfreetype2subtitle/src/base/ftbitmap.c
        ../external/libfreetype2subtitle/src/base/ftdebug.c
        ../external/libfreetype2subtitle/src/base/ftfstype.c
        ../external/libfreetype2subtitle/src/base/ftgasp.c
        ../external/libfreetype2subtitle/src/base/ftglyph.c
        ../external/libfreetype2subtitle/src/base/ftinit.c
        ../external/libfreetype2subtitle/src/base/ftmm.c
        ../external/libfreetype2subtitle/src/base/ftstroke.c
        ../external/libfreetype2subtitle/src/base/fttype1.c
        ../external/libfreetype2subtitle/src/base/ftsystem.c
        ../external/libfreetype2subtitle/src/cid/type1cid.c
        ../external/libfreetype2subtitle/src/cff/cff.c
        ../external/libfreetype2subtitle/src/gzip/ftgzip.c
        ../external/libfreetype2subtitle/src/psaux/psaux.c
        ../external/libfreetype2subtitle/src/pshinter/pshinter.c
        ../external/libfreetype2subtitle/src/psnames/psnames.c
        ../external/libfreetype2subtitle/src/raster/raster.c
        ../external/libfreetype2subtitle/src/sdf/sdf.c
        ../external/libfreetype2subtitle/src/sfnt/sfnt.c
        ../external/libfreetype2subtitle/src/smooth/smooth.c
        ../external/libfreetype2subtitle/src/svg/svg.c
        ../external/libfreetype2subtitle/src/truetype/truetype.c
        ../external/libfreetype2subtitle/src/type1/type1.c
    )

include_directories(
        subtitle
        subtitle/parser
        subtitle/render
        ipc
        io
        utils
        include
        display
        subtitle/parser/ExtParser
        port
        subtitle
        subtitle/render/waylandrender
        subtitle/render/directfbrender
    )

set(subtitle_common_srcs
        SubtitleService.cpp
        SubtitleServer.cpp
        ipc/SocketServer.cpp
        ipc/IpcBuffer.cpp
        ipc/IpcSocket.cpp
        ipc/FmqReceiver.cpp

        utils/AmlogicEvent.c
        utils/AmlogicTime.c
        utils/AmlogicUtil.c
        utils/AndroidCallbackMessageQueue.cpp
        utils/bprint.c
        utils/cJSON.c
        utils/CCJsonParser.cpp
        utils/ClosedCaptionJson.c
        utils/MemoryLeakTrackUtil.cpp
        utils/ringbuffer.c
        utils/Segment.cpp
        utils/SubtitleSignalHandler.cpp
        utils/VideoInfo.cpp
        utils/Watchdog.cpp

        io/CcSourceManager.c
        io/DataSourceFactory.cpp
        io/Demux.c
        io/DemuxDriver.c
        io/DemuxSource.cpp
        io/DeviceSource.cpp
        io/ExternalDataSource.cpp
        io/FileSource.cpp
        io/SocketSource.cpp
        io/Userdata.c
        io/UserdataDriver.c
        io/VbiSource.cpp

        subtitle/Subtitle.cpp
        subtitle/Presentation.cpp
        subtitle/parser/ParserFactory.cpp
        subtitle/parser/AribB24Parser.cpp
        subtitle/parser/AssParser.cpp
        subtitle/parser/ClosedCaption.c
        subtitle/parser/ClosedCaptionParser.cpp
        subtitle/parser/DvbParser.cpp
        subtitle/parser/DvdParser.cpp
	subtitle/parser/Parser.cpp
        subtitle/parser/PgsParser.cpp
        subtitle/parser/TeletextParser.cpp
        subtitle/parser/TtmlParser.cpp
        subtitle/parser/SmpteTtmlParser.cpp
        subtitle/parser/Scte27Parser.cpp
        subtitle/parser/UserDataAfd.cpp

        subtitle/parser/ExtParser/Aqtitle.cpp
        subtitle/parser/ExtParser/ExtParser.cpp
        subtitle/parser/ExtParser/ExtSubFactory.cpp
        subtitle/parser/ExtParser/ExtSubStreamReader.cpp
        subtitle/parser/ExtParser/Jacosub.cpp
        subtitle/parser/ExtParser/Lyrics.cpp
        subtitle/parser/ExtParser/Mircodvd.cpp
        subtitle/parser/ExtParser/Mplayer1.cpp
        subtitle/parser/ExtParser/Mplayer2.cpp
        subtitle/parser/ExtParser/Mpsub.cpp
        subtitle/parser/ExtParser/Pjs.cpp
        subtitle/parser/ExtParser/RealText.cpp
        subtitle/parser/ExtParser/Sami.cpp
        subtitle/parser/ExtParser/Subrip09.cpp
        subtitle/parser/ExtParser/Subrip.cpp
        subtitle/parser/ExtParser/SubStationAlpha.cpp
        subtitle/parser/ExtParser/SubViewer2.cpp
        subtitle/parser/ExtParser/SubViewer3.cpp
        subtitle/parser/ExtParser/SubViewer.cpp
        subtitle/parser/ExtParser/TextSubtitle.cpp
        subtitle/parser/ExtParser/Ttml.cpp
        subtitle/parser/ExtParser/VobSubIndex.cpp
        subtitle/parser/ExtParser/Vplayer.cpp
        subtitle/parser/ExtParser/WebVtt.cpp
        subtitle/parser/ExtParser/XmlSubtitle.cpp
        subtitle/parser/ExtParser/Qttext.cpp

        subtitle/render/AndroidHidlRemoteRender.cpp
        subtitle/render/RenderFactory.cpp

        subtitle/ai_translation/LooperMessageProcess.cpp
        subtitle/ai_translation/SubtitleAiTranslation.cpp
        subtitle/ai_translation/SubtitleWorkerThread.cpp
    )

set(subtitle_srcs ${subtitle_common_srcs})

if (USE_WAYLAND)
    list(APPEND subtitle_srcs
            subtitle/render/waylandrender/WLRender.cpp
            subtitle/render/waylandrender/WLGLDevice.cpp
        )
endif()

if (USE_FB)
    list(APPEND subtitle_srcs
            subtitle/render/framebufferrender/FBDevice.cpp
            subtitle/render/framebufferrender/FBRender.cpp
        )
endif()


set(TARGET subtitle)
add_library(${TARGET} SHARED ${subtitle_srcs} ${textrender_SRCS} ${zvbi_SRCS} ${aribcaption_SRCS} ${freetype2subtitle_SRCS})

if (USE_WAYLAND)
  target_include_directories(${TARGET} PUBLIC ${CMAKE_BUILD_DIR})
  target_link_libraries(${TARGET} PRIVATE wayland_client_protocol)
  add_dependencies(${TARGET} generate_wayland_protocols)
  set(WAYLAND_SCANNER_EXECUTABLE wayland-scanner)
  set(WAYLAND_PROTOCOLS_DIR ${STAGING_DIR}/usr/share/wayland-protocols)

  set(WAYLAND_PROTOCOL_OUTPUT_DIR ${CMAKE_BUILD_DIR})

  function(generate_wayland_protocol NAME CATEGORY)
    set(PROTOCOL_FILE "${WAYLAND_PROTOCOLS_DIR}/${CATEGORY}/${NAME}/${NAME}.xml")
    set(HEADER_FILE "${WAYLAND_PROTOCOL_OUTPUT_DIR}/${NAME}-client-protocol.h")
    set(CODE_FILE   "${WAYLAND_PROTOCOL_OUTPUT_DIR}/${NAME}-client-protocol.c")

    add_custom_command(
      OUTPUT ${HEADER_FILE}
      COMMAND ${WAYLAND_SCANNER_EXECUTABLE} client-header ${PROTOCOL_FILE} ${HEADER_FILE}
      DEPENDS ${PROTOCOL_FILE}
      COMMENT "Generating Wayland client header: ${NAME}"
      VERBATIM
      )

    add_custom_command(
      OUTPUT ${CODE_FILE}
      COMMAND ${WAYLAND_SCANNER_EXECUTABLE} code ${PROTOCOL_FILE} ${CODE_FILE}
      DEPENDS ${PROTOCOL_FILE}
      COMMENT "Generating Wayland client code: ${NAME}"
      VERBATIM
      )

    set(${NAME}_CLIENT_HEADER ${HEADER_FILE} PARENT_SCOPE)
    set(${NAME}_CLIENT_CODE ${CODE_FILE} PARENT_SCOPE)
  endfunction()

  if (AML_SUBTITLE_SIMPLE_SHELL)
    generate_wayland_protocol(simpleshell meson)
    add_definitions(-DAML_SUBTITLE_SIMPLE_SHELL)
  endif()
  generate_wayland_protocol(xdg-shell stable)

  if (AML_SUBTITLE_SIMPLE_SHELL)
    set(SIMPLESHELL_PROTOCOL_DEPS
      ${simpleshell_CLIENT_HEADER}
      ${simpleshell_CLIENT_CODE}
    )
  endif()
  add_custom_target(generate_wayland_protocols ALL
    DEPENDS
    ${SIMPLESHELL_PROTOCOL_DEPS}
    ${xdg-shell_CLIENT_HEADER}
    ${xdg-shell_CLIENT_CODE}
    )

  if (AML_SUBTITLE_SIMPLE_SHELL)
    set(SIMPLESHELL_PROTOCOL_SRCS ${simpleshell_CLIENT_CODE})
  endif()
  add_library(wayland_client_protocol STATIC
    ${SIMPLESHELL_PROTOCOL_SRCS}
    ${xdg-shell_CLIENT_CODE}
    )

  add_definitions(-DUSE_WAYLAND)
endif()

if (USE_FB)
    add_definitions(-DUSE_FB)
    add_definitions(-DSUBTITLE_ZAPPER_4K)
endif()

if (NOT NDEBUG)
    add_definitions(-DNEED_DUMP_LINUX)
    target_link_libraries(${TARGET} PUBLIC log)
endif()

#aribcaption zvbi all start
add_definitions(-D_REENTRANT)
add_definitions(-D_GNU_SOURCE)
add_definitions(-DENABLE_DVB=1)
add_definitions(-DENABLE_V4L=1)
add_definitions(-DENABLE_V4L2=1)
add_definitions(-DHAVE_ICONV=1)
#aribcaption zvbi all end

#aribcaption start
add_definitions(-DARIBCC_USE_FONTCONFIG)
add_definitions(-DARIBCC_USE_FREETYPE)
add_definitions(-DARIBCC_IS_ANDROID)
add_definitions(-DARIBCC_CUSTOM_SPECIAL_DATA_CONVERSION)
#aribcaption end

#ATSC start
#add_definitions(-DATSC_CC_USE_DATA_CALLBACK)
#add_definitions(-DDVB_TTML_USE_DATA_CALLBACK)
#ATSC end

#disable local cc
#add_definitions(-DDISABLE_LOCAL_CC)

#zvbi start
add_definitions(-DPACKAGE=\"zvbi\")
add_definitions(-DVERSION=\"0.2.33\")
add_definitions(-DNEED_TELETEXT_FIX_NULL_FLOF_LINKS)
add_definitions(-DNEED_TELETEXT_FIX_WRONG_CODE_STREAM_FOUR_COLOR_KEY)
add_definitions(-DNEED_TELETEXT_BLOCK_TOP_NAVIGATION_BAR)
add_definitions(-DNEED_TELETEXT_EXTENDED_LOCAL_ENHANCEMENT_DATA_CHROMA_KEY_LABEL_BAR)
#add_definitions(-DNEED_TELETEXT_USES_VECTOR_FONTS)
add_definitions(-DNEED_TELETEXT_REMOVE_FOUR_COLOR_KEY_BACKGROUND)
#add_definitions(-DNEED_TELETEXT_REMOVE_SUBTITLE_LEFT_RIGHT_BACKGROUND)
#add_definitions(-DNEED_TELETEXT_REMOVE_SUBPAGE_DISPLAY)
add_definitions(-DNEED_CC_SARNOFF_FOOTBALL_708_DEFAULT_HEADER)
add_definitions(-DNEED_CC_CALC_708_HEADER)
#add_definitions(-DNEED_KOREAN_CC_DETECT_Q_TONE_DATA)
add_definitions(-DNEED_KOREAN_CC_CALC_708_HEADER)
add_definitions(-DNEED_TELETEXT_GRAPHICS_SUBTITLE_BLACKGROUND)
#zvbi end

#freetype2 start
add_definitions(-DDARWIN_NO_CARBON)
add_definitions(-DFT2_BUILD_LIBRARY)
add_definitions(-DFT_CONFIG_OPTION_USE_PNG)
add_definitions(-DFT_CONFIG_OPTION_USE_ZLIB)
add_definitions(-DFT_CONFIG_OPTION_SYSTEM_ZLIB)

#freetype2 end

add_definitions(-DNEED_MIN_DVB)
add_definitions(-DNEED_ARIBCC_STYLE)
add_definitions(-DNEED_ARIB24_LIBARIBCAPTION)
add_definitions(-DNEED_TELETEXT_GRAPHICS_SUBTITLE_PAGENUMBER_BLACKGROUND)
#add_definitions(-DNEED_TELETEXT_CACHE_ZVBI_STATUS)
add_definitions(-DNEED_TELETEXT_SYNCHRONIZE_USING_TIMESTAMPS)
#add_definitions(-DREMOVE_TELETEXT_SEARCH_FOOTER)    #for zapper 4k
#add_definitions(-DNEED_TELETEXT_SUPPORT_LOAD_ANIMATION)
#add_definitions(-DNEED_TELETEXT_UNREMOVED_MIX_VIDEO_MODE_FOUR_COLOR_KEY_BACKGROUND)
#add_definitions(-DNEED_TELETEXT_SUPPORT_GRAPHICS_MODE_SUBTITLE_PAGE_FULL_SCREEN)
#add_definitions(-DNEED_TELETEXT_NO_DATA_BLACK_BACKGROUND_WITH_PAGE_NUMBER)
#add_definitions(-DNEED_WATCHDOG) #Linux platform currently does not support watchdog function
#add_definitions(-DNEED_TELETEXT_USES_VECTOR_FONTS)
#add_definitions(-DNEED_TELETEXT_USES_VECTOR_FONTS_2)
#add_definitions(-DNEED_TELETEXT_USES_VECTOR_FONTS_3)
#add_definitions(-DNEED_TELETEXT_USES_VECTOR_FONTS_4)
#add_definitions(-DNEED_TELETEXT_USES_VECTOR_FONTS_5)
#add_definitions(-DNEED_TELETEXT_BELOW_LEVEL_1P5_UNDECODE_X26)
#add_definitions(-DNEED_TELETEXT_USES_VECTOR_FONTS_PLUS)
#add_definitions(-DNEED_TELETEXT_FOUR_THREE_PILLARBOX_DISPLAY)
target_link_libraries(${TARGET} PUBLIC binder)
target_link_libraries(${TARGET} PUBLIC pthread)
target_link_libraries(${TARGET} PUBLIC cairo)
target_link_libraries(${TARGET} PUBLIC fontconfig)
target_link_libraries(${TARGET} PUBLIC tinyxml2)
target_link_libraries(${TARGET} PUBLIC png)
target_link_libraries(${TARGET} PUBLIC mediahal_mediasync dl)

target_link_libraries(${TARGET} PUBLIC z)

if (USE_WAYLAND)
    target_link_libraries(${TARGET} PUBLIC wayland-client)
    target_link_libraries(${TARGET} PUBLIC wayland-egl)
    target_link_libraries(${TARGET} PUBLIC EGL)
    target_link_libraries(${TARGET} PUBLIC GLESv2)
endif()

if (HAS_CCDATABASE)
    add_definitions(-DNEED_CCDATA_FROM_CCDATABASE)
    target_link_libraries(${TARGET} PUBLIC ccdataserver_client)
endif()

#set(FONT_FILES
#    ../fonts/bold_italic_teletext_amlogic.ttf
#    ../fonts/bold_teletext_amlogic.ttf
#    ../fonts/italic_teletext_amlogic.ttf
#    ../fonts/mosaic_teletext_amlogic.ttf
#    ../fonts/normal_teletext_amlogic.ttf
#)

#install(FILES ${FONT_FILES}
#        DESTINATION /usr/share/fonts/
#        PERMISSIONS OWNER_READ GROUP_READ WORLD_READ)

install(
    TARGETS ${TARGET}
)
