// // decode_video.swift // SwiftFFmpegExamples // // Created by sunlubo on 2019/1/9. // import SwiftFFmpeg #if canImport(Darwin) import Darwin #else import Glibc #endif private func saveToPGM( _ buf: UnsafeMutablePointer, _ wrap: Int, _ xsize: Int, _ ysize: Int, _ filename: String ) { let file = fopen(filename, "w") defer { fclose(file) } let header = String(format: "P5\n%d %d\n%d\n", xsize, ysize, 255) fwrite(header, 1, header.utf8.count, file) for i in 0...allocate( capacity: inbufSize + AVConstant.inputBufferPaddingSize) inbuf.initialize(to: 0) defer { inbuf.deallocate() } while feof(file) == 0 { // read raw data from the input file var size = fread(inbuf, 1, inbufSize, file) if size == 0 { break } // use the parser to split the data into frames var data = inbuf while size > 0 { let (buf, bufSize, used) = try parser.parse(data: data, size: size) pkt.data = buf pkt.size = bufSize data = data.advanced(by: used) size -= used if pkt.size > 0 { try decode(codecCtx: codecCtx, frame: frame, pkt: pkt, output: output) } } } // flush the decoder try decode(codecCtx: codecCtx, frame: frame, pkt: nil, output: output) }