class Libav::Frame::Video

Attributes

av_frame[R]
number[RW]
pos[RW]
stream[R]

Public Class Methods

new(p={}) click to toggle source

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]

# 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

Public Instance Methods

key_frame?() click to toggle source
# File lib/libav/frame.rb, line 54
def key_frame?
  @av_frame[:key_frame] != 0
end
pixel_format() click to toggle source
# File lib/libav/frame.rb, line 58
def pixel_format
  @av_frame[:format]
end
release() click to toggle source

Release frame back to buffered stream for re-use

# File lib/libav/frame.rb, line 113
def release
  stream.release_frame(self)
end
scale(p={}) click to toggle source

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

# 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
timestamp() click to toggle source

Get the presentation timestamp for this frame in fractional seconds

# File lib/libav/frame.rb, line 63
def timestamp
  pts * @stream[:time_base].to_f
end

Private Instance Methods

cleanup_proc(picture) click to toggle source

Returns Proc responsible for cleaning up the picture memory when it gets garbage collected.

# File lib/libav/frame.rb, line 121
def cleanup_proc(picture)
  proc { avpicture_free(picture) }
end