WINDOWS ?= FALSE
GDB ?= FALSE

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

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
else
CFLAGS := $(shell pkg-config --cflags $(SHARED_LIBS)) -Wall
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
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"}')

ifeq ($(WINDOWS), FALSE)
    EXAMPLES = ni_demuxing_decoding              \
               ni_encode_video                   \
               ni_multi_thread_encode_video      \
               ni_encode_video_dynamic_resolution\
               ni_transcode_filter
    ifeq ($(FFMPEG_421_AND_ABOVE),TRUE)
    EXAMPLES += ni_hevc_tile_decoding            \
                ni_hevc_tile_encoding            \
                ni_hevc_tile_transcoding
    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_transcode_ai_test             \
                ni_multi_thread_transcoding      \
                ni_multi_encode_scale
    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

ifneq ($(FFMPEG_342_AND_ABOVE), TRUE)
	$(error ERROR libavcodec API examples not supported on FFmpeg versions below 3.4.2)
endif

	${CC} ${CFLAGS} -c -o $@ $<

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

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