sunlubo / SwiftFFmpeg

A Swift wrapper for the FFmpeg API
Apache License 2.0
511 stars 83 forks source link

how to handle swsContext.scale #66

Closed wibed closed 1 year ago

wibed commented 1 year ago

i dont know how to construct the expected type.

the function on SwsContext.scale expects for dst a array of pointers

- dst: the array containing the pointers to the planes of the destination image

yet i am having a hard time constructing an array of pointers as a pointer for dst i resorted to use the source value as a basis to allocate the pointer for the destination value following the description of the function

public func scale(
    src: UnsafePointer<UnsafePointer<UInt8>?>,
    srcStride: UnsafePointer<Int32>,
    srcSliceY: Int,
    srcSliceHeight: Int,
    dst: UnsafePointer<UnsafeMutablePointer<UInt8>?>, // WHAT I TRY TO CONSTRUCT
    dstStride: UnsafePointer<Int32>
  )

first i tried, but got a bad dst image pointers error i think i declared unmutable pointers as mutable, which resulted in the error.

let dstSlice = frame.data.map({ UnsafeMutablePointer($0)} )

i tried using, but got an Cannot convert value of type 'UnsafePointer<UnsafeMutablePointer<UInt8>>' to expected argument type >'UnsafePointer<UnsafeMutablePointer<UInt8>?>' error

let dstData = frame.data.map({ _ in UnsafeMutablePointer<UInt8>.allocate(capacity: 1) })

i dont know how to allocate an optional pointer to pass it on for SwsContext. it may be necessary to mirror c's way of handling pointers with a struct as seen in BufferData.

if you could give me a hint on how to handle this, would be greatly appreciated.

sunlubo commented 1 year ago

Refer scaling_video.

    let data = UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>.allocate(capacity: 4)
    data.initialize(to: nil)

    let linesizes = UnsafeMutablePointer<Int32>.allocate(capacity: 4)
    linesizes.initialize(to: 0)

    av_image_alloc(
      data, linesizes, Int32(width), Int32(height), pixelFormat, Int32(align)
    )