diff --git a/src/opentimelineio/bundle.cpp b/src/opentimelineio/bundle.cpp index 3fee7640f..e4a2b8b68 100644 --- a/src/opentimelineio/bundle.cpp +++ b/src/opentimelineio/bundle.cpp @@ -206,14 +206,26 @@ namespace bundle { } else if (auto seq = dynamic_retainer_cast(ref.second)) { - // Handle image sequence references + // Handle image sequence references. A frame step + // that is not positive has no valid image count. + if (seq->frame_step() <= 0) + { + if (error_status) + *error_status = ErrorStatus( + ErrorStatus::FILE_WRITE_FAILED, + "media reference '" + seq->name() + + "' has a frame step that is not positive"); + return false; + } if (policy != MediaReferencePolicy::all_missing) { - for (int frame = 0; - frame < seq->number_of_images_in_sequence(); - frame += seq->frame_step()) + // The image numbers are indices into the + // sequence, the frame step is applied by + // target_url_for_image_number() + int const images = seq->number_of_images_in_sequence(); + for (int image = 0; image < images; ++image) { - file = file_from_url(seq->target_url_for_image_number(frame)); + file = file_from_url(seq->target_url_for_image_number(image)); if (file.has_value()) { const auto path = from_utf8(*file); diff --git a/tests/test_bundle.cpp b/tests/test_bundle.cpp index 20a5d2609..532e7f450 100644 --- a/tests/test_bundle.cpp +++ b/tests/test_bundle.cpp @@ -107,12 +107,11 @@ void create_refs(Timeline* timeline, std::filesystem::path const& path) } else if (auto seq = dynamic_cast(ref.second)) { - auto const base = file_from_url(seq->target_url_base()); - for (int frame = seq->start_frame(); - frame <= seq->end_frame(); - frame += seq->frame_step()) + for (int image = 0; + image < seq->number_of_images_in_sequence(); + ++image) { - if (auto const file = file_from_url(seq->target_url_for_image_number(frame))) + if (auto const file = file_from_url(seq->target_url_for_image_number(image))) { auto const file_path = from_utf8(*file); create_file(file_path.is_relative() ? @@ -438,6 +437,47 @@ main(int argc, char** argv) assertTrue(std::filesystem::exists(from_utf8(*file))); }); + tests.add_test("test_image_sequence_frame_step", [] { + TempDir temp; + + // A sequence of every other frame, 1 through 23 + auto tl = create_simple_timeline(); + auto seq = new ImageSequenceReference( + "", + "render.", + ".exr", + 1, 2, 24, 0, + ImageSequenceReference::MissingFramePolicy::error, + TimeRange(0, 24, 24)); + find_clip_by_name(tl, "video clip 1")->set_media_reference(seq); + create_refs(tl, temp.path()); + + // All of the images are in the bundle, and only those + WriteOptions write_options; + write_options.relative_media_base_dir = to_utf8(temp.path()); + auto const otiod_path = temp.path() / "frame_step.otiod"; + OTIO_NS::ErrorStatus error; + assertTrue(write_otiod(tl, to_utf8(otiod_path), write_options, &error)); + for (int frame = 1; frame <= 24; ++frame) + { + std::stringstream ss; + ss << "render." << frame << ".exr"; + assertEqual( + std::filesystem::exists(otiod_path / media_dir / ss.str()), + frame % 2 == 1); + } + + // A frame step that is not positive is an error + for (int step : { 0, -1 }) + { + seq->set_frame_step(step); + assertFalse(dry_run(tl, write_options).has_value()); + write_options.policy = MediaReferencePolicy::all_missing; + assertFalse(dry_run(tl, write_options).has_value()); + write_options.policy = MediaReferencePolicy::error_if_not_file; + } + }); + tests.add_test("test_otioz_media_policy", [] { // Create a timeline with file and non-file references