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']),
     )
Exemple #2
0
    def ConfigurationFixups(self, config):
        """Ensure that gold-q and key-q are smaller than fixed-q. """
        fixed_q_value = encoder.Option('fixed-q').GetValue(config)
        if encoder.Option('gold-q').GetValue(config) > fixed_q_value:
            config = encoder.Option('gold-q').SetValue(config, fixed_q_value)
        if encoder.Option('key-q').GetValue(config) > fixed_q_value:
            config = encoder.Option('key-q').SetValue(config, fixed_q_value)

        return config
Exemple #3
0
 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, name='vp8'):
     super(Vp8Codec, self).__init__(name)
     self.extension = 'webm'
     self.options = [
         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']),
     ]
     self.start_encoder = encoder.Encoder(
         self, """ --lag-in-frames=0 \
   --kf-min-dist=3000 \
   --kf-max-dist=3000 --cpu-used=0 --static-thresh=0 \
   --token-parts=1 --drop-frame=0 --end-usage=cbr --min-q=2 --max-q=56 \
   --undershoot-pct=100 --overshoot-pct=15 --buf-sz=1000 \
   --buf-initial-sz=800 --buf-optimal-sz=1000 --max-intra-rate=1200 \
   --resize-allowed=0 --drop-frame=0 --passes=1 --good --noise-sensitivity=0 """
     )
Exemple #5
0
 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'])
Exemple #6
0
 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 ConfigurationFixups(self, config):
   """Ensure that gold-q and key-q are set from fixed-q. """
   key_q_value = encoder.Option('key-q').GetValue(config)
   fixed_q = float(key_q_value) * 2.0
   gold_q = float(fixed_q) / 1.5
   if fixed_q > 63:
     fixed_q = 63
   if gold_q > 63:
     gold_q = 63
   config = encoder.Option('gold-q').SetValue(config, str(int(gold_q)))
   config = encoder.Option('fixed-q').SetValue(config, str(int(fixed_q)))
   return config
Exemple #8
0
 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']),
     )
Exemple #9
0
 def test_Init(self):
   codec = vp8_cq.Vp8CodecCqMode()
   self.assertEqual('vp8-cq', codec.name)
   # Verifying that the default config's value for min-q is still 32.
   # This is required for later tests to work properly.
   self.assertEqual('32', encoder.Option('min-q').GetValue(
           codec.start_encoder.parameters))
Exemple #10
0
 def test_RandomlyRemoveParameterWithOnlyMandatory(self):
     config = encoder.OptionValueSet(
         encoder.OptionSet(
             encoder.Option('foo', ['foo', 'bar']).Mandatory()),
         '--foo=foo')
     newconfig = config.RandomlyRemoveParameter()
     self.assertFalse(newconfig)
Exemple #11
0
 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())
Exemple #12
0
 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')
Exemple #13
0
 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())
Exemple #14
0
    def _SuggestTweakToName(self, encoding, name):
        """Returns a parameter string based on this encoding that has the
    parameter identified by "name" changed in a way worth testing.
    If no sensible change is found, returns None."""
        parameters = encoding.encoder.parameters
        value = int(encoder.Option(name).GetValue(parameters))
        new_value = None
        if encoding.result['bitrate'] > encoding.bitrate:
            delta = 1
            new_value = 63
            candidates = range(value + 1, 64)
        else:
            delta = -1
            new_value = 0
            candidates = range(value - 1, -1, -1)
        # The range of Q values is from 0 to 63.
        if value + delta > 63:
            print name, 'maxed out at 63'
            return None  # Already maxed out
        if value + delta < 0:
            print name, 'mined out at 0'
            return None  # Already at bottom
        # If a previous result returned a score (which will be lower, since
        # the starting point is the highest score), try the middle value
        # between this and that. If none exists, go for the extreme values.
        for search_value in candidates:
            temp_params = encoder.Option(name).SetValue(
                parameters, str(search_value))
            temp_params = self.ConfigurationFixups(temp_params)
            temp_encoder = encoder.Encoder(self, temp_params)
            temp_encoding = encoder.Encoding(temp_encoder, encoding.bitrate,
                                             encoding.videofile)
            temp_encoding.Recover()
            if temp_encoding.Score():
                print name, 'found scored value', search_value
                new_value = int((value + search_value) / 2)
                if new_value in (value, search_value):
                    print name, 'already tried', value, '+1'
                    return None  # Already tried one-step-up
                break

        print name, "suggesting value", new_value
        parameters = encoder.Option(name).SetValue(parameters, str(new_value))
        parameters = self.ConfigurationFixups(parameters)
        return parameters
Exemple #15
0
 def test_SuggestTweakDecreasesCq(self):
   codec = vp8_cq.Vp8CodecCqMode()
   videofile = encoder.Videofile('foofile_640_480_30.yuv')
   my_encoder = codec.start_encoder
   encoding = encoder.Encoding(my_encoder, 500, videofile)
   encoding.result = { 'bitrate' : 200 }
   # Since the bitrate is too high, the suggstion should be to increase it.
   new_encoding = codec.SuggestTweak(encoding)
   self.assertEqual('31', encoder.Option('min-q').GetValue(
           new_encoding.encoder.parameters))
Exemple #16
0
 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'),
     )
Exemple #17
0
 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']), )
Exemple #18
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']))
Exemple #19
0
 def SuggestTweak(self, encoding):
     """Suggest a tweak based on an encoding result.
 For fixed QP, suggest increasing min-q when bitrate is too high, otherwise
 suggest decreasing it."""
     if not encoding.result:
         return None
     if encoding.result['bitrate'] > encoding.bitrate:
         delta = 1
     else:
         delta = -1
     parameters = encoding.encoder.parameters
     value = int(encoder.Option('min-q').GetValue(parameters))
     # The range of min-q is from 0 to 63.
     if value + delta > 63:
         return None  # Already maxed out
     if value + delta < 0:
         return None  # Already at bottom
     new_value = value + delta
     parameters = encoder.Option('min-q').SetValue(parameters,
                                                   str(new_value))
     parameters = self.ConfigurationFixups(parameters)
     return encoder.Encoding(encoder.Encoder(self, parameters),
                             encoding.bitrate, encoding.videofile)
Exemple #20
0
 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']),
         encoder.DummyOption('vbv-maxrate'),
         encoder.DummyOption('vbv-bufsize'),
     )
Exemple #21
0
 def test_InitSingle(self):
     opts = encoder.OptionSet(encoder.Option('foo', ['foo', 'bar']))
     self.assertTrue(opts.Option('foo'))
     self.assertFalse(opts.HasOption('bar'))
Exemple #22
0
 def test_UnlistedValueFailsToParse(self):
     options = encoder.OptionSet(
         encoder.Option('foo', ['foo', 'bar']).Mandatory())
     with self.assertRaises(encoder.ParseError):
         encoder.OptionValueSet(options, '--foo=nonsense')
Exemple #23
0
 def test_MissingMandatoryFailsToParse(self):
     options = encoder.OptionSet(
         encoder.Option('foo', ['foo', 'bar']).Mandatory())
     with self.assertRaises(encoder.ParseError):
         encoder.OptionValueSet(options, '')
Exemple #24
0
 def ConfigurationFixups(self, config):
     """Ensure fixed CQ by setting min-q and max-q to the same value."""
     return encoder.Option('max-q').SetValue(
         config,
         encoder.Option('min-q').GetValue(config))
Exemple #25
0
 def test_StartParams(self):
   codec = vp8_mpeg_1d.Vp8CodecMpeg1dMode()
   params = codec.start_encoder.parameters
   self.assertEqual(int(encoder.Option('key-q').GetValue(params))*2,
                    int(encoder.Option('fixed-q').GetValue(params)))
Exemple #26
0
 def test_InitMany(self):
     opts = encoder.OptionSet(encoder.Option('foo', ['foo', 'bar']),
                              encoder.Option('bar', ['bar', 'baz']))
     self.assertTrue(opts.Option('foo'))
     self.assertTrue(opts.Option('bar'))
     self.assertFalse(opts.HasOption('baz'))
Exemple #27
0
 def test_RandomlyRemoveParameterSuccessfully(self):
     config = encoder.OptionValueSet(
         encoder.OptionSet(encoder.Option('foo', ['foo', 'bar'])),
         '--foo=foo')
     newconfig = config.RandomlyRemoveParameter()
     self.assertEqual('', newconfig.ToString())
Exemple #28
0
 def test_RegisterOption(self):
     opts = encoder.OptionSet()
     self.assertFalse(opts.HasOption('foo'))
     opts.RegisterOption(encoder.Option('foo', ['foo', 'bar']))
     self.assertTrue(opts.HasOption('foo'))
     self.assertTrue(opts.Option('foo'))
Exemple #29
0
 def test_GetValue(self):
     valueset = encoder.OptionValueSet(
         encoder.OptionSet(encoder.Option('foo', ['baz', 'bar'])),
         '--foo=bar')
     self.assertEqual('bar', valueset.GetValue('foo'))
Exemple #30
0
 def test_GetValueNotPresent(self):
     option = encoder.Option('foo', ['foo', 'bar'])
     config = encoder.OptionValueSet(encoder.OptionSet(option),
                                     '--notfoo=foo')
     with self.assertRaises(encoder.Error):
         config.GetValue('foo')