def test_Mandatory(self): opts = encoder.OptionSet(encoder.ChoiceOption(['foo', 'bar'])) self.assertFalse(opts.AllMandatoryOptions()) opts = encoder.OptionSet( encoder.ChoiceOption(['foo', 'bar']).Mandatory()) self.assertEquals( set([opt.name for opt in opts.AllMandatoryOptions()]), set(['foo/bar']))
def test_RandomlyPatchConfig(self): config = encoder.OptionValueSet( encoder.OptionSet(encoder.Option('foo', ['foo', 'bar'])), '--foo=foo') newconfig = config.RandomlyPatchConfig() # There is only one possible change. It should be chosen. self.assertEqual(newconfig.ToString(), '--foo=bar') # Test case where original set did not have value. config = encoder.OptionValueSet( encoder.OptionSet(encoder.Option('foo', ['foo', 'bar'])), '') newconfig = config.RandomlyPatchConfig() self.assertIn(newconfig.ToString(), ['--foo=foo', '--foo=bar'])
def test_OtherFormatter(self): valueset = encoder.OptionValueSet( encoder.OptionSet(encoder.Option('foo', ['foo', 'bar'])), '-foo foo', formatter=encoder.OptionFormatter(prefix='-', infix=' ')) self.assertEqual('-foo foo', valueset.ToString()) valueset = encoder.OptionValueSet( encoder.OptionSet(encoder.Option('foo', ['foo', 'bar']), encoder.Option('xyz', ['abc', 'def'])), '-foo foo -xyz abc', formatter=encoder.OptionFormatter(prefix='-', infix=' ')) self.assertEqual('-foo foo -xyz abc', valueset.ToString())
def test_ChangeValue(self): opts = encoder.OptionSet(encoder.ChoiceOption(['foo', 'bar'])) valueset = encoder.OptionValueSet(opts, '--foo') newset = valueset.ChangeValue('foo/bar', 'bar') self.assertEqual('--bar', newset.ToString()) # Check that old set is not modified. self.assertEqual('--foo', valueset.ToString())
def testEncodersInMultipleRepos(self): test_tools.EmptyWorkDirectory() context = StorageOnlyContext() cache = encoder.EncodingDiskCache(context) context.cache = cache my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) other_dir = os.path.join(encoder_configuration.conf.sysdir(), 'multirepo_test') os.mkdir(other_dir) other_cache = encoder.EncodingDiskCache(context, scoredir='multirepo_test') encoder_configuration.conf.override_scorepath_for_test([other_dir]) other_cache.StoreEncoder(my_encoder) encoders = cache.AllEncoderFilenames(only_workdir=True) self.assertEquals(0, len(encoders)) encoders = cache.AllEncoderFilenames(only_workdir=False) self.assertEquals(1, len(encoders)) fetched_encoder = encoder.Encoder(context, filename=encoders[0]) self.assertEquals(my_encoder.parameters.ToString(), fetched_encoder.parameters.ToString()) my_encoding = encoder.Encoding( my_encoder, 123, encoder.Videofile('x/foo_640_480_20.yuv')) testresult = {'foo': 'bar'} my_encoding.result = testresult other_cache.StoreEncoding(my_encoding) # With a specified directory, we should find it in only one place. self.assertTrue(other_cache.ReadEncodingResult(my_encoding)) # Without a specified directory, we should find it on the searchpath. self.assertTrue(cache.ReadEncodingResult(my_encoding)) # Without a searchpath, we shouldn't find it in the default cache. encoder_configuration.conf.override_scorepath_for_test([]) self.assertFalse(cache.ReadEncodingResult(my_encoding))
def __init__(self, name='vp8'): super(Vp8Codec, self).__init__(name) self.extension = 'webm' self.option_set = encoder.OptionSet( encoder.Option('overshoot-pct', ['0', '15', '30', '45']), encoder.Option('undershoot-pct', ['0', '25', '50', '75', '100']), # CQ mode is not considered for end-usage at the moment. encoder.Option('end-usage', ['cbr', 'vbr']), # End-usage cq doesn't really make sense unless we also set q to something # between min and max. This is being checked. # encoder.Option('end-usage', ['cbr', 'vbr', 'cq']), encoder.Option('end-usage', ['cbr', 'vbr']), encoder.Option('min-q', ['0', '2', '4', '8', '16', '24']), encoder.Option('max-q', ['32', '56', '63']), encoder.Option( 'buf-sz', ['200', '500', '1000', '2000', '4000', '8000', '16000']), encoder.Option( 'buf-initial-sz', ['200', '400', '800', '1000', '2000', '4000', '8000', '16000' ]), encoder.Option('max-intra-rate', ['100', '200', '400', '600', '800', '1200']), encoder.ChoiceOption(['good', 'best', 'rt']), encoder.IntegerOption('cpu-used', -16, 16), )
def __init__(self): super(DummyCodec, self).__init__('dummy') self.extension = 'fake' self.option_set = encoder.OptionSet( encoder.Option('score', ['0', '5', '10']), encoder.Option('another_parameter', ['yes']), )
def testStoreMultipleEncodings(self): context = StorageOnlyContext() cache = encoder.EncodingMemoryCache(context) # This particular test needs the context to know about the cache. context.cache = cache my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) videofile = encoder.Videofile('x/foo_640_480_20.yuv') my_encoding = encoder.Encoding(my_encoder, 123, videofile) testresult = {'foo': 'bar'} my_encoding.result = testresult cache.StoreEncoding(my_encoding) my_encoding = encoder.Encoding(my_encoder, 246, videofile) my_encoding.result = testresult cache.StoreEncoding(my_encoding) result = cache.AllScoredRates(my_encoder, videofile) self.assertEquals(2, len(result)) result = cache.AllScoredEncodings(123, videofile) self.assertEquals(1, len(result)) # Verify that it's working correctly with a new videofile object. videofile2 = encoder.Videofile(videofile.filename) result = cache.AllScoredEncodings(123, videofile2) my_encoding = encoder.Encoding(my_encoder, 123, videofile2) self.assertTrue(cache.ReadEncodingResult(my_encoding)) # Verify that it's working correctly with an encoder created via hashname. encoder2 = encoder.Encoder(context, filename=my_encoder.Hashname()) encoding2 = encoder2.Encoding(123, videofile2) self.assertTrue(cache.ReadEncodingResult(encoding2))
def testReadResultEncodedInAst(self): # We've changed the storage format for results from AST to JSON. # This test verifies that AST formatted results are still readable. context = StorageOnlyContext() cache = encoder.EncodingDiskCache(context) my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) my_encoding = encoder.Encoding( my_encoder, 123, encoder.Videofile('x/foo_640_480_20.yuv')) testresult = {'foo': 'bar'} my_encoding.result = testresult # The following code is a copy of cache.StoreEncoding, with the # encoding step changed. dirname = '%s/%s/%s' % (cache.workdir, my_encoding.encoder.Hashname(), cache.context.codec.SpeedGroup( my_encoding.bitrate)) videoname = my_encoding.videofile.basename with open('%s/%s.result' % (dirname, videoname), 'w') as resultfile: resultfile.write(str(my_encoding.result)) my_encoding.result = None result = cache.ReadEncodingResult(my_encoding) self.assertEquals(result, testresult)
def testBrokenStoredEncoding(self): context = StorageOnlyContext() other_dir = os.path.join(encoder_configuration.conf.sysdir(), 'broken_files') os.mkdir(other_dir) cache = encoder.EncodingDiskCache(context, scoredir='broken_files') # This particular test needs the context to know about the cache. context.cache = cache my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) # Cache should start off empty. self.assertFalse(cache.AllScoredEncodingsForEncoder(my_encoder)) videofile = encoder.Videofile('x/foo_640_480_20.yuv') my_encoding = encoder.Encoding(my_encoder, 123, videofile) testresult = {'foo': 'bar'} my_encoding.result = testresult cache.StoreEncoding(my_encoding) # TODO(hta): Expose the filename generation as a function for testing. with open( os.path.join( cache.workdir, my_encoding.encoder.Hashname(), cache.context.codec.SpeedGroup(my_encoding.bitrate), '%s.result' % my_encoding.videofile.basename), 'w') as scorefile: scorefile.write('stuff that is not valid json') result = cache.AllScoredEncodingsForEncoder(my_encoder) self.assertFalse(result) self.assertEquals(1, len(cache.bad_encodings))
def test_RandomlyRemoveParameterWithOnlyMandatory(self): config = encoder.OptionValueSet( encoder.OptionSet( encoder.Option('foo', ['foo', 'bar']).Mandatory()), '--foo=foo') newconfig = config.RandomlyRemoveParameter() self.assertFalse(newconfig)
def __init__(self, name='x264', formatter=None): super(X264Codec, self).__init__( name, formatter=(formatter or encoder.OptionFormatter(prefix='--', infix=' '))) self.extension = 'mkv' self.option_set = encoder.OptionSet( encoder.Option('preset', [ 'ultrafast', 'superfast', 'veryfast', 'faster', 'fast', 'medium', 'slow', 'slower', 'veryslow', 'placebo' ]), encoder.Option('rc-lookahead', ['0', '30', '60']), encoder.Option('vbv-init', ['0.5', '0.8', '0.9']), encoder.Option('ref', ['1', '2', '3', '16']), encoder.ChoiceOption(['use-vbv-maxrate']), encoder.Option('profile', ['baseline', 'main', 'high']), encoder.Option('tune', ['psnr', 'ssim']), # Experimentation on a 6-core, 12-thread system shows some gains on # large videos for thread values up to the thread count, and up to the # core count on smaller videos. # There is some damage to PSNR with more threads. encoder.IntegerOption('threads', 1, 6).Mandatory(), encoder.DummyOption('vbv-maxrate'), encoder.DummyOption('vbv-bufsize'), )
def test_Changevalue(self): config = encoder.OptionValueSet( encoder.OptionSet(encoder.Option('foo', ['foo', 'bar'])), '--foo=foo') context = encoder.Context(DummyCodec()) my_encoder = encoder.Encoder(context, config) next_encoder = my_encoder.ChangeValue('foo', 'bar') self.assertEquals(next_encoder.parameters, '--foo=bar')
def test_LockOption(self): opts = encoder.OptionSet(encoder.Option('foo', ['value1', 'value2'])) self.assertEqual(2, len(opts.Option('foo').values)) self.assertTrue(opts.Option('foo').CanChange()) opts.LockOption('foo', 'value1') self.assertTrue(opts.Option('foo').mandatory) self.assertEqual(1, len(opts.Option('foo').values)) self.assertFalse(opts.Option('foo').CanChange())
def __init__(self, name='x265'): # The x265 encoder uses default parameter delimiters, unlike # the ffmpeg family; we inherit the decoding process. super(X265Codec, self).__init__(name, formatter=encoder.OptionFormatter()) self.extension = 'hevc' self.codecname = 'hevc' self.option_set = encoder.OptionSet()
def __init__(self, name='vp9'): super(Vp9Codec, self).__init__(name) self.extension = 'webm' self.option_set = encoder.OptionSet( encoder.IntegerOption('cpu-used', 0, 16), # The "best" option gives encodes that are too slow to be useful. encoder.ChoiceOption(['good', 'rt']), )
def __init__(self, name='vp8-mp1'): super(Vp8CodecMpeg1dMode, self).__init__(name) # Set the parts that are different from the VP8 MPEG codec. self.option_set = encoder.OptionSet( encoder.IntegerOption('key-q', 0, 63).Mandatory(), encoder.DummyOption('fixed-q'), encoder.DummyOption('gold-q'), )
def __init__(self, name='mjpeg'): super(MotionJpegCodec, self).__init__(name) self.codecname = 'mjpeg' self.extension = 'mjpeg' self.option_set = encoder.OptionSet( encoder.IntegerOption('qmin', 1, 69), encoder.IntegerOption('qmax', 2, 1024), )
def testInitFromFile(self): context = encoder.Context(DummyCodec()) my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) my_encoder.Store() new_encoder = encoder.Encoder(context, filename=my_encoder.Hashname()) self.assertEquals(new_encoder.parameters, my_encoder.parameters)
def test_ParametersCanChangeMayReturnTrue(self): context = encoder.Context(DummyCodec()) my_encoder = encoder.Encoder( context, encoder.OptionValueSet( encoder.OptionSet(encoder.Option('key', ['value1', 'value2'])), '--parameters')) self.assertTrue(my_encoder.ParametersCanChange())
def testStoreFetchEncoder(self): context = StorageOnlyContext() cache = encoder.EncodingDiskCache(context) my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) new_encoder_data = cache.ReadEncoderParameters(my_encoder.Hashname()) self.assertEquals(new_encoder_data, my_encoder.parameters)
def test_ParametersCanBeStoredAndRetrieved(self): context = encoder.Context(DummyCodec()) my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) my_encoder.Store() filename = my_encoder.Hashname() next_encoder = encoder.Encoder(context, filename=filename) self.assertEqual(my_encoder.parameters, next_encoder.parameters)
def test_OptionValues(self): codec = DummyCodec() my_encoder = encoder.Encoder( encoder.Context(codec), encoder.OptionValueSet( encoder.OptionSet(encoder.IntegerOption('score', 0, 100)), '--score=77')) self.assertEqual(repr(my_encoder.OptionValues()), "{'score': '77'}") self.assertEqual(my_encoder.DisplayValues(), '77')
def testAllEncoderFilenames(self): context = StorageOnlyContext() cache = encoder.EncodingDiskCache(context) files = cache.AllEncoderFilenames() self.assertEquals(0, len(files)) my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) files = cache.AllEncoderFilenames() self.assertEquals(1, len(files)) self.assertEquals(my_encoder.Hashname(), files[0])
def test_AutoGenerateClipTime(self): my_optimizer = optimizer.Optimizer(self.codec, self.file_set, cache_class=self.cache_class, score_function=ReturnsClipTime) my_encoder = encoder.Encoder(my_optimizer.context, encoder.OptionValueSet(encoder.OptionSet(), '')) # Must use a tricked-out videofile to avoid disk access. videofile = DummyVideofile('test_640x480_20.yuv', clip_time=1) my_encoding = encoder.Encoding(my_encoder, 123, videofile) my_encoding.result = {'psnr':42, 'bitrate':123} # If cliptime is not present, this will raise an exception. my_optimizer.Score(my_encoding)
def testStoreFetchEncoder(self): context = StorageOnlyContext() cache = encoder.EncodingDiskCache(context) my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) new_encoder_data = cache.ReadEncoderParameters( os.path.join(cache.WorkDir(), my_encoder.Hashname())) self.assertEquals(new_encoder_data, my_encoder.parameters) # Using only the hashname should work too. new_encoder_data = cache.ReadEncoderParameters(my_encoder.Hashname()) self.assertEquals(new_encoder_data, my_encoder.parameters)
def __init__(self, name='vp8-mpeg'): super(Vp8CodecMpegMode, self).__init__(name) # Set the parts that are different from the VP8 codec. self.option_set = encoder.OptionSet( encoder.IntegerOption('fixed-q', 0, 63).Mandatory(), encoder.IntegerOption('gold-q', 0, 63).Mandatory(), encoder.IntegerOption('key-q', 0, 63).Mandatory(), encoder.ChoiceOption(['good', 'best', 'rt']), ) # The start encoder is exactly the same as for VP8, # except that fixed-q, gold-q, alt-q and key-q parameters are set. # TODO(hta): Remove the options that have no effect in this mode. self.start_encoder_parameters = """ --lag-in-frames=0 \
def __init__(self, name='openh264', formatter=None): self.name = name self.codecname = 'openh264' super(OpenH264Codec, self).__init__(name, formatter=formatter or encoder.OptionFormatter(prefix='-', infix=' ')) self.extension = '264' self.option_set = encoder.OptionSet( # Rate control. -1 = off, 0 = quality, 1 = bitrate, 2 = buffer based # Default is set in config file by RCMode parameter to 0. # Only 0 and 1 really make sense when rate control is used to select # the bitrate target. encoder.Option('rc', ['0', '1']))
def __init__(self, name='libavc', formatter=None): self.name = name self.codecname = 'libavc' super(LibavcCodec, self).__init__( name, formatter=(formatter or encoder.OptionFormatter(prefix='--', infix=' '))) self.extension = 'avi' self.option_set = encoder.OptionSet( # Rate control. 0 = constant QP, 1 = storage, 2 = CBR high delay, # 3 = CBR low delay # 2 and 3 seems to drop frames - sometimes, but not always. # 3 is able to run out of memory. encoder.Option('rc', ['0', '1']), )
def testStoreFetchEncoding(self): context = StorageOnlyContext() cache = encoder.EncodingDiskCache(context) my_encoder = encoder.Encoder( context, encoder.OptionValueSet(encoder.OptionSet(), '--parameters')) cache.StoreEncoder(my_encoder) my_encoding = encoder.Encoding( my_encoder, 123, encoder.Videofile('x/foo_640_480_20.yuv')) testresult = {'foo': 'bar'} my_encoding.result = testresult cache.StoreEncoding(my_encoding) my_encoding.result = None result = cache.ReadEncodingResult(my_encoding) self.assertEquals(result, testresult)