~ubuntu-branches/ubuntu/lucid/pitivi/lucid

« back to all changes in this revision

Viewing changes to pitivi/factories/operation.py

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2009-05-27 14:22:49 UTC
  • mfrom: (1.2.1 upstream) (3.1.13 experimental)
  • Revision ID: james.westby@ubuntu.com-20090527142249-tj0qnkc37320ylml
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# PiTiVi , Non-linear video editor
 
3
#
 
4
#       operation.py
 
5
#
 
6
# Copyright (c) 2009, Edward Hervey <bilboed@bilboed.com>
 
7
#
 
8
# This program is free software; you can redistribute it and/or
 
9
# modify it under the terms of the GNU Lesser General Public
 
10
# License as published by the Free Software Foundation; either
 
11
# version 2.1 of the License, or (at your option) any later version.
 
12
#
 
13
# This program is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
# Lesser General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU Lesser General Public
 
19
# License along with this program; if not, write to the
 
20
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
21
# Boston, MA 02111-1307, USA.
 
22
 
 
23
import gst
 
24
from pitivi.factories.base import OperationFactory
 
25
from pitivi.elements.smartscale import SmartVideoScale
 
26
from pitivi.stream import AudioStream, VideoStream
 
27
 
 
28
# FIXME: define a proper hierarchy
 
29
class OperationFactoryError(Exception):
 
30
    pass
 
31
 
 
32
class ModifierFactoryError(OperationFactoryError):
 
33
    pass
 
34
 
 
35
class TransformFactory(OperationFactory):
 
36
    """
 
37
    Factories that take exactly one input stream and output exactly one output
 
38
    stream.
 
39
    """
 
40
 
 
41
    def addTransformStreams(self, input_stream, output_stream):
 
42
        self.addInputStream(input_stream)
 
43
        self.addOutputStream(output_stream)
 
44
 
 
45
    def addInputStream(self, stream):
 
46
        if len(self.input_streams) > 1:
 
47
            raise OperationFactoryError("Can't handle more than one stream")
 
48
        return OperationFactory.addInputStream(self, stream)
 
49
 
 
50
    def addOutputStream(self, stream):
 
51
        if len(self.output_streams) > 1:
 
52
            raise OperationFactoryError("Can't handle more than one stream")
 
53
        return OperationFactory.addOutputStream(self, stream)
 
54
 
 
55
    def _requestNewInputStream(self, *args):
 
56
        raise OperationFactoryError("TransformFactory doesn't allow request pads")
 
57
 
 
58
class StreamModifierFactory(TransformFactory):
 
59
    """
 
60
    Factories that modify the nature/type of a stream.
 
61
    """
 
62
    pass
 
63
 
 
64
class AudioModifierFactory(StreamModifierFactory):
 
65
 
 
66
    def _makeBin(self, *args):
 
67
        b = gst.Bin()
 
68
        idt = gst.element_factory_make("identity", "single-segment")
 
69
        idt.props.single_segment = True
 
70
        aconv = gst.element_factory_make("audioconvert", "aconv")
 
71
        ares = gst.element_factory_make("audioresample", "ares")
 
72
        arate = gst.element_factory_make("audiorate", "arate")
 
73
        b.add(idt, aconv, ares, arate)
 
74
        gst.element_link_many(idt, aconv, ares, arate)
 
75
 
 
76
        gsink = gst.GhostPad("sink", idt.get_pad("sink"))
 
77
        gsink.set_active(True)
 
78
        b.add_pad(gsink)
 
79
 
 
80
        # if we have an output stream specified, we add a capsfilter
 
81
        if len(self.output_streams):
 
82
            cfilter = gst.element_factory_make("capsfilter")
 
83
            cfilter.props.caps = self.output_streams[0].caps
 
84
            b.add(cfilter)
 
85
            arate.link(cfilter)
 
86
 
 
87
            gsrc = gst.GhostPad("src", cfilter.get_pad("src"))
 
88
        else:
 
89
            gsrc = gst.GhostPad("src", arate.get_pad("src"))
 
90
 
 
91
        gsrc.set_active(True)
 
92
        b.add_pad(gsrc)
 
93
        return b
 
94
 
 
95
class VideoModifierFactory(StreamModifierFactory):
 
96
 
 
97
    def _makeBin(self, *args):
 
98
        b = gst.Bin()
 
99
        idt = gst.element_factory_make("identity", "single-segment")
 
100
        idt.props.single_segment = True
 
101
        csp = gst.element_factory_make("ffmpegcolorspace", "csp")
 
102
        vrate = gst.element_factory_make("videorate", "vrate")
 
103
 
 
104
        b.add(idt, csp, vrate)
 
105
        gst.element_link_many(idt, csp, vrate)
 
106
 
 
107
        gsink = gst.GhostPad("sink", idt.get_pad("sink"))
 
108
        gsink.set_active(True)
 
109
        b.add_pad(gsink)
 
110
 
 
111
        # if we have an output stream specified, we add a capsfilter
 
112
        self.debug("output_streams:%d", len(self.output_streams))
 
113
        if len(self.output_streams) and self.output_streams[0].caps.is_fixed():
 
114
            vscale = SmartVideoScale()
 
115
            vscale.set_caps(self.output_streams[0].caps)
 
116
            b.add(vscale)
 
117
            vrate.link(vscale)
 
118
 
 
119
            idt = gst.element_factory_make("capsfilter")
 
120
            idt.props.caps = self.output_streams[0].caps
 
121
            b.add(idt)
 
122
            vscale.link(idt)
 
123
 
 
124
            gsrc = gst.GhostPad("src", idt.get_pad("src"))
 
125
        else:
 
126
            gsrc = gst.GhostPad("src", vrate.get_pad("src"))
 
127
 
 
128
        gsrc.set_active(True)
 
129
        b.add_pad(gsrc)
 
130
        return b
 
131
 
 
132
def get_modifier_for_stream(input_stream=None, output_stream=None):
 
133
    """
 
134
    Returns a L{StreamModifierFactory} for the given streams.
 
135
 
 
136
    @raises ModifierFactoryError: If no modifier factory is available
 
137
    for the given streams.
 
138
    """
 
139
    if input_stream == None and output_stream == None:
 
140
        raise ModifierFactoryError("No streams provided")
 
141
    if (isinstance(input_stream, AudioStream) or input_stream == None) and \
 
142
           (isinstance(output_stream, AudioStream) or output_stream == None):
 
143
        res = AudioModifierFactory()
 
144
    elif (isinstance(input_stream, VideoStream) or input_stream == None) and \
 
145
             (isinstance(output_stream, VideoStream) or output_stream == None):
 
146
        res = VideoModifierFactory()
 
147
    else:
 
148
        raise ModifierFactoryError("No modifier for given stream type")
 
149
    if input_stream:
 
150
        res.addInputStream(input_stream)
 
151
    if output_stream:
 
152
        res.addOutputStream(output_stream)
 
153
    return res