Class: Libav::Frame::Video

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
FFI::Libav
Defined in:
lib/libav/frame.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p = {}) ⇒ Video

Initialize a new frame, and optionally allocate memory for the frame data.

If only a :stream is provided, the remaining attributes will be copied from that stream.

Options

  • :stream - Libav::Stream this frame belongs to

  • :width - width of the frame

  • :height - height of the frame

  • :pixel_format - format of the frame

  • :alloc - Allocate space for the frame data [default: true]



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/libav/frame.rb', line 25

def initialize(p={})
  # Create our frame and alloc space for the frame data
  @av_frame = AVFrame.new

  @stream = p[:stream]
  @av_frame[:width] = p[:width] || @stream && @stream.width
  @av_frame[:height] = p[:height] || @stream && @stream.height
  @av_frame[:format] = p[:pixel_format] || @stream && @stream.pixel_format

  # Allocate the frame's data unless the caller doesn't want us to.
  unless p[:alloc] == false
    av_picture = AVPicture.new @av_frame.pointer
    avpicture_alloc(av_picture, @av_frame[:format], @av_frame[:width],
                    @av_frame[:height])
    ObjectSpace.define_finalizer(self, cleanup_proc(av_picture))
  end
end

Instance Attribute Details

#av_frameObject (readonly)

Returns the value of attribute av_frame.



9
10
11
# File 'lib/libav/frame.rb', line 9

def av_frame
  @av_frame
end

#numberObject

Returns the value of attribute number.



10
11
12
# File 'lib/libav/frame.rb', line 10

def number
  @number
end

#posObject

Returns the value of attribute pos.



10
11
12
# File 'lib/libav/frame.rb', line 10

def pos
  @pos
end

#streamObject (readonly)

Returns the value of attribute stream.



9
10
11
# File 'lib/libav/frame.rb', line 9

def stream
  @stream
end

Instance Method Details

#key_frame?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/libav/frame.rb', line 54

def key_frame?
  @av_frame[:key_frame] != 0
end

#pixel_formatObject



58
59
60
# File 'lib/libav/frame.rb', line 58

def pixel_format
  @av_frame[:format]
end

#releaseObject

Release frame back to buffered stream for re-use



113
114
115
# File 'lib/libav/frame.rb', line 113

def release
  stream.release_frame(self)
end

#scale(p = {}) ⇒ Object

Scale the frame

If any of the :width, :height, or :pixel_format options are not supplied, they will be copied from the :output_frame, if provided, otherwise they will be copied from this frame.

If no :scale_ctx is provided, one will be allocated and freed within this method.

If no :output_frame is provided, this method will allocate a new one.

Options

  • :width - width of the scaled frame

  • :height - height of the scaled frame

  • :pixel_format - pixel format of the scaled frame

  • :scale_ctx - optional software scaling context

+ :output_frame - optional output frame

Raises:

  • (NoMemoryError)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/libav/frame.rb', line 85

def scale(p={})
  out = p[:output_frame] ||
    self.class.new(:width => p[:width] || width,
                   :height => p[:height] || height,
                   :pixel_format => p[:pixel_format] || pixel_format,
                   :stream => stream)
  ctx = p[:scale_ctx] ||
    sws_getCachedContext(nil, width, height, pixel_format,
                         out.width, out.height, out.pixel_format,
                         SWS_BICUBIC, nil, nil, nil)
  raise NoMemoryError, "sws_getCachedContext() failed" if ctx.nil?

  # Scale the image
  rc = sws_scale(ctx, data, linesize, 0, height, out.data, out.linesize)

  # Free the scale context if one wasn't provided by the caller
  sws_freeContext(ctx) unless p[:scale_ctx]

  # Let's copy a handful of attributes to the scaled frame
  %w{ pts number pos key_frame }.each do |field|
    out.send("#{field}=", send(field))
  end

  # Return our scaled frame
  out
end

#timestampObject

Get the presentation timestamp for this frame in fractional seconds



63
64
65
# File 'lib/libav/frame.rb', line 63

def timestamp
  pts * @stream[:time_base].to_f
end