WINDOWS ?= FALSE
GDB ?= FALSE

# use pkg-config for getting CFLAGS and LDLIBS
SHARED_LIBS=libavdevice                          \
            libavformat                          \
            libavfilter                          \
            libavcodec                           \
            libswresample                        \
            libswscale                           \
            libavutil                            \
            xcoder

ifeq ($(GDB), TRUE)
	OPTFLAG = -g
endif

ifeq ($(WINDOWS), FALSE)
CFLAGS := $(shell pkg-config --cflags $(SHARED_LIBS)) -Wall ${OPTFLAG}
LDLIBS := $(shell pkg-config --libs $(SHARED_LIBS)) -lpthread -lm
else
CFLAGS := $(shell pkg-config --cflags $(SHARED_LIBS)) -Wall ${OPTFLAG}
LDLIBS := $(shell pkg-config --libs $(SHARED_LIBS)) -lpthread -lws2_32
endif

#Check ffmpeg version. Versions prior to 4.2.1 don't support ROI and some other features
#Get libavcodec version and extract major, minor and micro
LIBAVCODEC_VERSION := $(shell pkg-config --modversion libavcodec)
LIBAVCODEC_MAJOR := $(shell echo "$(LIBAVCODEC_VERSION)" | awk -F. '{print $$1}')
LIBAVCODEC_MINOR := $(shell echo "$(LIBAVCODEC_VERSION)" | awk -F. '{print $$2}')
LIBAVCODEC_MICRO := $(shell echo "$(LIBAVCODEC_VERSION)" | awk -F. '{print $$3}')
#Check if libavcodec version is 58.54.100 or above
FFMPEG_421_AND_ABOVE = $(shell echo "$(LIBAVCODEC_MAJOR) $(LIBAVCODEC_MINOR) $(LIBAVCODEC_MICRO)" | awk '{if ($$1 > 58 || ($$1 == 58 && $$2 > 54) || ($$1 == 58 && $$2 == 54 && $$3 >= 100)) print "TRUE"; else print "FALSE"}')
#if FFMPEG_421_AND_ABOVE is TRUE, then define ROI_SUPPORTED macro
ifeq ($(FFMPEG_421_AND_ABOVE),TRUE)
CFLAGS += -DROI_SUPPORTED
endif
#xstack is not supported on versions prior to 4.1.3
FFMPEG_413_AND_ABOVE = $(shell echo "$(LIBAVCODEC_MAJOR) $(LIBAVCODEC_MINOR) $(LIBAVCODEC_MICRO)" | awk '{if ($$1 > 58 || ($$1 == 58 && $$2 > 35) || ($$1 == 58 && $$2 == 35 && $$3 >= 100)) print "TRUE"; else print "FALSE"}')
FFMPEG_342_AND_ABOVE = $(shell echo "$(LIBAVCODEC_MAJOR) $(LIBAVCODEC_MINOR) $(LIBAVCODEC_MICRO)" | awk '{if ($$1 > 57 || ($$1 == 57 && $$2 > 107) || ($$1 == 57 && $$2 == 107 && $$3 >= 100)) print "TRUE"; else print "FALSE"}')
#Netint supports AV1 from 3.4.2 onwards
ifeq ($(FFMPEG_342_AND_ABOVE),TRUE)
CFLAGS += -DAV1_SUPPORTED
endif

ifeq ($(WINDOWS), FALSE)
EXAMPLES=ni_demuxing_decoding                   \
         ni_encode_video                        \
         ni_multi_thread_yuv420to444            \
         ni_multi_thread_yuv444to420            \
         ni_multi_thread_encode_video
    ifeq ($(FFMPEG_421_AND_ABOVE),TRUE)
    EXAMPLES += ni_tile_encoding                     \
                ni_hevc_tile_decoding                \
                ni_hevc_tile_transcoding             \
                ni_encode_ai_test                    \
                ni_transcode_ai_test                 \
                ni_2D_engine_p2p_test                \
                ni_av1_tile_decoding                 \
                ni_av1_tile_transcoding              \
                ni_multiview_tile_encoding
    endif
    ifeq ($(FFMPEG_413_AND_ABOVE),TRUE) 
    EXAMPLES += ni_xstack                            \
                ni_live_xstack
    endif
    #These compile on 3.4.2 and above
    ifeq ($(FFMPEG_342_AND_ABOVE),TRUE)
    EXAMPLES += ni_multi_thread_lowdelay_encode   \
         ni_2D_engine_application                 \
         ni_multi_thread_transcoding              \
         ni_multi_encode_scale                    \
         ni_transcode_filter                      \
         ni_ppu_split
    endif
else
EXAMPLES=ni_demuxing_decoding                 \
          ni_encode_video
endif

OBJS=$(addsuffix .o,$(EXAMPLES))

.phony: all clean

all: $(EXAMPLES)

$(EXAMPLES): $(OBJS)
	$(CC) $(addsuffix .o,$@) $(CFLAGS) $(LDLIBS) -o $@

%.o : %.c
	${CC} ${CFLAGS} -c -o $@ $<

clean:
	$(RM) $(EXAMPLES) $(OBJS)

build_info:
	@echo "libavcodec version: $(LIBAVCODEC_VERSION)"
	@echo "Supports AV1: $(FFMPEG_342_AND_ABOVE)"
	@echo "Supports ROI: $(FFMPEG_421_AND_ABOVE)"
	@echo "Supports xstack: $(FFMPEG_413_AND_ABOVE)"
	@echo "Supported examples: $(EXAMPLES)"
