| 210 | } |
| 211 | |
| 212 | bool WanDiffusion::saveVideoFrames(VARP sample, const std::string& outputDir, int requestedFrames) { |
| 213 | if (sample.get() == nullptr) { |
| 214 | MNN_ERROR("Wan VAE output is null\n"); |
| 215 | return false; |
| 216 | } |
| 217 | if (!MNNCreateDir(outputDir.c_str())) { |
| 218 | MNN_ERROR("Failed to create output dir %s\n", outputDir.c_str()); |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | auto info = sample->getInfo(); |
| 223 | if (info == nullptr) { |
| 224 | MNN_ERROR("Wan VAE output has no shape info\n"); |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | int saved = 0; |
| 229 | if (info->dim.size() == 4) { |
| 230 | MNN_PRINT("Warning: Wan VAE returned 4D/single-frame output; saving only frame_0000.png\n"); |
| 231 | std::string fileName = MNNFilePathConcat(outputDir, "frame_0000.png"); |
| 232 | return saveFrame(sample, fileName); |
| 233 | } |
| 234 | |
| 235 | if (info->dim.size() != 5) { |
| 236 | MNN_ERROR("Wan VAE output rank %d is not supported for frame saving\n", (int)info->dim.size()); |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | if (info->dim[1] != 3) { |
| 241 | MNN_PRINT( |
| 242 | "Warning: Wan VAE 5D output shape is not NCTHW with C=3; frame saving is not implemented for " |
| 243 | "this layout\n"); |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | int decodedFrames = info->dim[2]; |
| 248 | int saveFrames = std::min(decodedFrames, requestedFrames); |
| 249 | if (decodedFrames < requestedFrames) { |
| 250 | MNN_PRINT("Warning: Wan VAE returned %d frames, requested %d; saving decoded frames only\n", decodedFrames, |
| 251 | requestedFrames); |
| 252 | } |
| 253 | for (int i = 0; i < saveFrames; ++i) { |
| 254 | int startsData[5] = {0, 0, i, 0, 0}; |
| 255 | int sizesData[5] = {1, 3, 1, info->dim[3], info->dim[4]}; |
| 256 | auto frame = _Slice(sample, _Const(startsData, {5}, NCHW, halide_type_of<int>()), |
| 257 | _Const(sizesData, {5}, NCHW, halide_type_of<int>())); |
| 258 | frame = _Reshape(frame, {1, 3, info->dim[3], info->dim[4]}, NCHW); |
| 259 | |
| 260 | std::ostringstream name; |
| 261 | name << "frame_" << std::setfill('0') << std::setw(4) << i << ".png"; |
| 262 | std::string fileName = MNNFilePathConcat(outputDir, name.str()); |
| 263 | if (saveFrame(frame, fileName)) { |
| 264 | ++saved; |
| 265 | } else { |
| 266 | MNN_PRINT("Warning: failed to save %s\n", fileName.c_str()); |
| 267 | } |
| 268 | } |
| 269 |
nothing calls this directly
no test coverage detected