# A WORK IN PROGRESS
# Issues:

# The audio and video codecs for these test videos
CODEC=-vcodec mpeg4 -acodec aac -strict experimental

# There isn't much video codes to choose.

# Let's consider codecs registered in .mp4: h264, h263, mpeg4, mpeg2.
# ffmpeg doesn't encode the first, and android doesn't play the last.
# h263 does not use square pixels, thus introduce a potential disruption
# to mobile phone playback tests.
# Leaving mpeg4 the only choice there.

# The format of the output file
EXT=3gp
# If video codec is mpeg, both .3gp and .mp4 can contain it.

SOURCE=trailer_1080p.ogg
SOURCE_URI=http://download.blender.org/peach/trailer/$(SOURCE)

VBR=-qscale 4
CBR=-b 512k



FRAMEHEIGHTS=120.$(EXT);240.$(EXT);280.$(EXT);300.$(EXT);600.$(EXT);768.$(EXT)

# There are many resolutions worth testing. We only do 4:3 now.
# 1:1 or near		
RESOLUTIONS = {160x160} {240x240} {160x144}
# 5:4 or like		
RESOLUTIONS += {220x176} {208x176}
# 4:3			
RESOLUTIONS += {160x120} {320x240} {640x480} {400x300} {800x600} {1024x768} 
# 3:2
RESOLUTIONS += {160x240} {480x320} {960x640}
# 5:3 			
RESOLUTIONS += {800x480} {400x240}
# 16:9 			
RESOLUTIONS += {1024x600}
# We don't include narrow aspect like 640x320 640x240 

RESOLUTION_FILES  = $(subst },.$(EXT),$(RESOLUTIONS))
RESOLUTION_PATHS  = $(subst {,cases/VBR/15fps/,$(RESOLUTION_FILES))
RESOLUTION_PATHS += $(subst {,cases/CBR/15fps/,$(RESOLUTION_FILES))
RESOLUTION_PATHS += $(subst {,cases/VBR/25fps/,$(RESOLUTION_FILES))
RESOLUTION_PATHS += $(subst {,cases/CBR/25fps/,$(RESOLUTION_FILES))
$(RESOLUTION_PATHS): CROP = `echo $@ | sed 's|.*/\([0-9]*\)x\([0-9]*\).*|\1:\2|'`
$(RESOLUTION_PATHS): Y = `echo $@ | grep -o 'x[0-9]*'`
$(RESOLUTION_PATHS): X = `echo $@ | sed 's|.*x\([0-9]*\).*|\1 0.5625 / p|' | dc`
$(RESOLUTION_PATHS): B = `echo $@ | sed 's|.*/\([0-9]*\)x\([0-9]*\).*|\1 \2 * 200 / p|' | dc `

help:
$(SOURCE):
	wget $(SOURCE_URI)

cases cases/VBR cases/CBR:
	mkdir -p $@

cases/VBR.$(EXT): cases                                             $(SOURCE)
	ffmpeg -i $(SOURCE) -s 426x240 -vf crop=320:240 -r 15 $(VBR) $(CODEC) $@
cases/CBR.$(EXT): cases                                             $(SOURCE)
	ffmpeg -i $(SOURCE) -s 426x240 -vf crop=320:240 -r 15 $(CBR) $(CODEC) $@

cases/VBR/15fps.$(EXT): cases/VBR.$(EXT) cases/VBR
	ln -s ../VBR.$(EXT) $@
cases/CBR/15fps.$(EXT): cases/CBR.$(EXT) cases/CBR
	ln -s ../CBR.$(EXT) $@

cases/VBR/25fps.$(EXT): cases/VBR                                   $(SOURCE)
	ffmpeg -i $(SOURCE) -s 426x240 -vf crop=320:240 -r 25 $(VBR) $(CODEC) $@
cases/CBR/25fps.$(EXT): cases/CBR                                   $(SOURCE)
	ffmpeg -i $(SOURCE) -s 426x240 -vf crop=320:240 -r 25 $(CBR) $(CODEC) $@

$(subst {,cases/VBR/15fps/,$(RESOLUTION_FILES)): $(SOURCE)
	mkdir -p cases/VBR/15fps
	ffmpeg -i $(SOURCE) -s $(X)$(Y) -vf crop=$(CROP) -qscale 4 -r 15 $(CODEC) $@
$(subst {,cases/CBR/15fps/,$(RESOLUTION_FILES)): $(SOURCE)
	mkdir -p cases/CBR/15fps
	ffmpeg -i $(SOURCE) -s $(X)$(Y) -vf crop=$(CROP) -b $(B)k  -r 15 $(CODEC) $@
$(subst {,cases/VBR/25fps/,$(RESOLUTION_FILES)): $(SOURCE)
	mkdir -p cases/VBR/25fps
	ffmpeg -i $(SOURCE) -s $(X)$(Y) -vf crop=$(CROP) -qscale 4 -r 25 $(CODEC) $@
$(subst {,cases/CBR/25fps/,$(RESOLUTION_FILES)): $(SOURCE)
	mkdir -p cases/CBR/25fps
	ffmpeg -i $(SOURCE) -s $(X)$(Y) -vf crop=$(CROP) -b $(B)k  -r 25 $(CODEC) $@

test_suit: $(RESOLUTION_PATHS) \
	cases/VBR/15fps.$(EXT) cases/CBR/15fps.$(EXT) \
	cases/VBR/25fps.$(EXT) cases/CBR/25fps.$(EXT) \
	cases/VBR.$(EXT) cases/CBR.$(EXT) 
