<commit_before># -*- Mode: Python; test-case-name: flumotion.test.test_enum -*-
# vi:si:et:sw=4:sts=4:ts=4
#
# Flumotion - a streaming media server
# Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com).
# All rights reserved.

# This file may be distributed and/or modified under the terms of
# the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This file is distributed without any warranty; without even the implied
# warranty of merchantability or fitness for a particular purpose.
# See "LICENSE.GPL" in the source distribution for more information.

# Licensees having purchased or holding a valid Flumotion Advanced
# Streaming Server license may use this file in accordance with the
# Flumotion Advanced Streaming Server Commercial License Agreement.
# See "LICENSE.Flumotion" in the source distribution for more information.

# Headers in this file shall remain intact.

import common

from twisted.trial import unittest

from flumotion.transcoder.enums import VideoScaleMethodEnum
from flumotion.component.transcoder import videosize

class TestVideoSize(unittest.TestCase):
    
    def _format(self, w, h, par):
        if w:
            sw = "%3d" % w
        else:
            sw = "???"
        if h:
            sh = "%3d" % h
        else:
            sh = "???"    
        if par:            
            spar = "%d/%d" % (par[0], par[1])
        else:
            spar = "?/?"
    
        result = "%sx%s par=%s" % (sw, sh, spar)
        if  w != None and h != None and par != None:
            dar = videosize.getVideoDAR(w, h, par)
            result += " dar=%.4f ~ %03d/%03d" % (float(dar[0]) / float(dar[1]),
                                                 dar[0], dar[1])
        else:
            result += "                     "
        return result
    
    def _expect(self, iw, ih, ipar, ow, oh, opar, mw, mh, pref, ew, eh, epar):
        rw, rh, rpar = videosize.getVideoSize(iw, ih, ipar, 
                                              ow, oh, opar, 
                                              mw, mh, pref)
        #print "IN: %s - PREF: %s - OUT: %s" % (self._format(iw, ih, ipar),
        #                                       self._format(ow, oh, opar),
        #                                       self._format(rw, rh, rpar))
        idar = (float(iw) * ipar[0]) / (float(ih) * ipar[1])
        rdar = (float(rw) * rpar[0]) / (float(rh) * rpar[1])
        self.assertTrue(abs(idar - rdar) < 0.03, "Display Aspect Ratio Changed")
        self.assertEquals((rw, rh, rpar), (ew, eh, epar))
    
    def testMaxSizeWithPreferredSize(self):
        self._expect(320, 240, (1, 1), 
                     200, None, (1, 1), 
                     300, 200, None,
                     200, 150, (1, 1))
        self._expect(320, 240, (1, 1), 
                     200, None, (1, 1), 
                     150, 100, None,
                     133, 100, (1, 1))
        self._expect(320, 240, (1, 1), 
                     200, None, (1, 1), 
                     100, 50, None,
                     67, 50, (1, 1))

        self._expect(320, 240, (1, 1), 
                     None, 180, (1, 1), 
                     300, 200, None,
                     240, 180, (1, 1))
        self._expect(320, 240, (1, 1), 
                     None, 180, (1, 1), 
                     150, 100, None,
                     133, 100, (1, 1))
        self._expect(320, 240, (1, 1), 
                     None, 180, (1, 1), 
                     100, 50, None,
                     67, 50, (1, 1))

        self._expect(320, 240, (1, 1), 
                     180, 180, (1, 1), 
                     300, 200, None,
                     180, 135, (1, 1))
        self._expect(320, 240, (1, 1), 
                     180, 180, (1, 1), 
                     150, 100, None,
                     133, 100, (1, 1))
        self._expect(320, 240, (1, 1), 
                     180, 180, (1, 1), 
                     100, 50, None,
                     67, 50, (1, 1))
        
    
    def testMaxSizeWithoutPreferredSize(self):
        self._expect(320, 240, (1, 1), 
                     None, None, (1, 1), 
                     200, 300, VideoScaleMethodEnum.upscale,
                     200, 150, (1, 1))
        self._expect(320, 240, (1, 1), 
                     None, None, (1, 2), 
                     200, 300, VideoScaleMethodEnum.upscale,
                     200, 75, (1, 2))
        self._expect(320, 240, (1, 1), 
                     None, None, (1, 3), 
                     200, 300, VideoScaleMethodEnum.upscale,
                     200, 50, (1, 3))
        self._expect(320, 240, (1, 1), 
                     None, None, (2, 1), 
                     200, 300, VideoScaleMethodEnum.upscale,
                     200, 300, (2, 1))
        self._expect(320, 240, (1, 1), 
                     None, None, (3, 1), 
                     200, 300, VideoScaleMethodEnum.upscale,
                     133, 300, (3, 1))
        
        self._expect(320, 240, (1, 1), 
                     None, None, (1, 1), 
                     400, 500, VideoScaleMethodEnum.upscale,
                     320, 240, (1, 1))
        self._expect(320, 240, (1, 1), 
                     None, None, (1, 2), 
                     400, 500, VideoScaleMethodEnum.upscale,
                     400, 150, (1, 2))
        self._expect(320, 240, (1, 1), 
                     None, None, (1, 3), 
                     400, 500, VideoScaleMethodEnum.upscale,
                     400, 100, (1, 3))
        self._expect(320, 240, (1, 1), 
                     None, None, (2, 1), 
                     400, 500, VideoScaleMethodEnum.upscale,
                     320, 480, (2, 1))
        self._expect(320, 240, (1, 1), 
                     None, None, (3, 1), 
                     400, 500, VideoScaleMethodEnum.upscale,
                     222, 500, (3, 1))
        
    
    def testPreferredSize(self):
        print
        self._expect(320, 240, (1, 1), 
                     200, 200, (1, 1), 
                     None, None, None,
                     200, 150, (1, 1))
        self._expect(240, 320, (1, 1), 
                     200, 200, (1, 1), 
                     None, None, None,
                     150, 200, (1, 1))

        self._expect(320, 240, (3, 4), 
                     200, 300, (1, 2), <commit_msg>Removed a print...<commit_after># -*- Mode: Python; test-case-name: flumotion.test.test_enum -*-
# vi:si:et:sw=4:sts=4:ts=4
#
# Flumotion - a streaming media server
# Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com).
# All rights reserved.

# This file may be distributed and/or modified under the terms of
# the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This file is distributed without any warranty; without even the implied
# warranty of merchantability or fitness for a particular purpose.
# See "LICENSE.GPL" in the source distribution for more information.

# Licensees having purchased or holding a valid Flumotion Advanced
# Streaming Server license may use this file in accordance with the
# Flumotion Advanced Streaming Server Commercial License Agreement.
# See "LICENSE.Flumotion" in the source distribution for more information.

# Headers in this file shall remain intact.

import common

from twisted.trial import unittest

from flumotion.transcoder.enums import VideoScaleMethodEnum
from flumotion.component.transcoder import videosize

class TestVideoSize(unittest.TestCase):
    
    def _format(self, w, h, par):
        if w:
            sw = "%3d" % w
        else:
            sw = "???"
        if h:
            sh = "%3d" % h
        else:
            sh = "???"    
        if par:            
            spar = "%d/%d" % (par[0], par[1])
        else:
            spar = "?/?"
    
        result = "%sx%s par=%s" % (sw, sh, spar)
        if  w != None and h != None and par != None:
            dar = videosize.getVideoDAR(w, h, par)
            result += " dar=%.4f ~ %03d/%03d" % (float(dar[0]) / float(dar[1]),
                                                 dar[0], dar[1])
        else:
            result += "                     "
        return result
    
    def _expect(self, iw, ih, ipar, ow, oh, opar, mw, mh, pref, ew, eh, epar):
        rw, rh, rpar = videosize.getVideoSize(iw, ih, ipar, 
                                              ow, oh, opar, 
                                              mw, mh, pref)
        #print "IN: %s - PREF: %s - OUT: %s" % (self._format(iw, ih, ipar),
        #                                       self._format(ow, oh, opar),
        #                                       self._format(rw, rh, rpar))
        idar = (float(iw) * ipar[0]) / (float(ih) * ipar[1])
        rdar = (float(rw) * rpar[0]) / (float(rh) * rpar[1])
        self.assertTrue(abs(idar - rdar) < 0.03, "Display Aspect Ratio Changed")
        self.assertEquals((rw, rh, rpar), (ew, eh, epar))
    
    def testMaxSizeWithPreferredSize(self):
        self._expect(320, 240, (1, 1), 
                     200, None, (1, 1), 
                     300, 200, None,
                     200, 150, (1, 1))
        self._expect(320, 240, (1, 1), 
                     200, None, (1, 1), 
                     150, 100, None,
                     133, 100, (1, 1))
        self._expect(320, 240, (1, 1), 
                     200, None, (1, 1), 
                     100, 50, None,
                     67, 50, (1, 1))

        self._expect(320, 240, (1, 1), 
                     None, 180, (1, 1), 
                     300, 200, None,
                     240, 180, (1, 1))
        self._expect(320, 240, (1, 1), 
                     None, 180, (1, 1), 
                     150, 100, None,
                     133, 100, (1, 1))
        self._expect(320, 240, (1, 1), 
                     None, 180, (1, 1), 
                     100, 50, None,
                     67, 50, (1, 1))

        self._expect(320, 240, (1, 1), 
                     180, 180, (1, 1), 
                     300, 200, None,
                     180, 135, (1, 1))
        self._expect(320, 240, (1, 1), 
                     180, 180, (1, 1), 
                     150, 100, None,
                     133, 100, (1, 1))
        self._expect(320, 240, (1, 1), 
                     180, 180, (1, 1), 
                     100, 50, None,
                     67, 50, (1, 1))
        
    
    def testMaxSizeWithoutPreferredSize(self):
        self._expect(320, 240, (1, 1), 
                     None, None, (1, 1), 
                     200, 300, VideoScaleMethodEnum.upscale,
                     200, 150, (1, 1))
        self._expect(320, 240, (1, 1), 
                     None, None, (1, 2), 
                     200, 300, VideoScaleMethodEnum.upscale,
                     200, 75, (1, 2))
        self._expect(320, 240, (1, 1), 
                     None, None, (1, 3), 
                     200, 300, VideoScaleMethodEnum.upscale,
                     200, 50, (1, 3))
        self._expect(320, 240, (1, 1), 
                     None, None, (2, 1), 
                     200, 300, VideoScaleMethodEnum.upscale,
                     200, 300, (2, 1))
        self._expect(320, 240, (1, 1), 
                     None, None, (3, 1), 
                     200, 300, VideoScaleMethodEnum.upscale,
                     133, 300, (3, 1))
        
        self._expect(320, 240, (1, 1), 
                     None, None, (1, 1), 
                     400, 500, VideoScaleMethodEnum.upscale,
                     320, 240, (1, 1))
        self._expect(320, 240, (1, 1), 
                     None, None, (1, 2), 
                     400, 500, VideoScaleMethodEnum.upscale,
                     400, 150, (1, 2))
        self._expect(320, 240, (1, 1), 
                     None, None, (1, 3), 
                     400, 500, VideoScaleMethodEnum.upscale,
                     400, 100, (1, 3))
        self._expect(320, 240, (1, 1), 
                     None, None, (2, 1), 
                     400, 500, VideoScaleMethodEnum.upscale,
                     320, 480, (2, 1))
        self._expect(320, 240, (1, 1), 
                     None, None, (3, 1), 
                     400, 500, VideoScaleMethodEnum.upscale,
                     222, 500, (3, 1))
        
    
    def testPreferredSize(self):
        self._expect(320, 240, (1, 1), 
                     200, 200, (1, 1), 
                     None, None, None,
                     200, 150, (1, 1))
        self._expect(240, 320, (1, 1), 
                     200, 200, (1, 1), 
                     None, None, None,
                     150, 200, (1, 1))

        self._expect(320, 240, (3, 4), 
                     200, 300, (1, 2), 
{'fraction_non_alphanumeric': 0.1292517006802721, 'fraction_numerical': 0.09148486980999296, 'mean_word_length': 1.212419996540391, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4916454', 'n_tokens_mistral': 4819, 'n_tokens_neox': 3722, 'n_words': 1171}