File: SphericalMetadata.cpp

package info (click to toggle)
libopenshot 0.5.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,228 kB
  • sloc: cpp: 32,692; python: 92; sh: 77; makefile: 21; ruby: 5
file content (170 lines) | stat: -rw-r--r-- 5,651 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
 * @file
 * @brief Unit tests for FFmpegWriter spherical metadata
 * @author Jonathan Thomas <jonathan@openshot.org>
 *
 * @ref License
 */

// Copyright (c) 2008-2023 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "openshot_catch.h"

#include <fstream>
#include <iostream>
#include <memory>
#include <string>

#include "FFmpegReader.h"
#include "FFmpegWriter.h"
#include "Fraction.h"
#include "Frame.h"

using namespace openshot;

TEST_CASE( "SphericalMetadata_Test", "[libopenshot][ffmpegwriter]" )
{
    // Create a reader to grab some frames
    FFmpegReader r(TEST_MEDIA_PATH "sintel_trailer-720p.mp4");
    r.Open();

    // Create a spherical metadata test video
    std::string test_file = "spherical_test.mp4";

    // Create a writer
    FFmpegWriter w(test_file);
    
    // Set options - Using MP4 with H.264 for best compatibility with spherical metadata
    w.SetVideoOptions(true, "libx264", r.info.fps, r.info.width, r.info.height, 
                      r.info.pixel_ratio, false, false, 3000000);
    w.SetAudioOptions(true, "aac", r.info.sample_rate, r.info.channels, 
                      r.info.channel_layout, 128000);

    w.PrepareStreams();

    // Add spherical metadata BEFORE opening the writer
    float test_yaw = 30.0f;
    w.AddSphericalMetadata("equirectangular", test_yaw, 0.0f, 0.0f);

    // Open writer
    w.Open();

    // Write a few frames
    for (int frame = 1; frame <= 30; frame++) {
        // Get the frame
        std::shared_ptr<Frame> f = r.GetFrame(frame);
        
        // Write the frame
        w.WriteFrame(f);
    }
    
    // Close the writer & reader
    w.Close();
    r.Close();

    // Reopen the file with FFmpegReader to verify metadata was added
    FFmpegReader test_reader(test_file);
    test_reader.Open();
    
    // Display format information for debugging
    INFO("Container format: " << test_reader.info.vcodec);
    INFO("Duration: " << test_reader.info.duration);
    INFO("Width x Height: " << test_reader.info.width << "x" << test_reader.info.height);
    
    // Check metadata map contents for debugging
    INFO("Metadata entries in reader:");
    for (const auto& entry : test_reader.info.metadata) {
        INFO("  " << entry.first << " = " << entry.second);
    }
    
    // Verify presence of spherical metadata and orientation keys
    CHECK(test_reader.info.metadata.count("spherical") > 0);
    CHECK(test_reader.info.metadata["spherical"] == "1");
    CHECK(test_reader.info.metadata.count("spherical_projection") > 0);
    CHECK(test_reader.info.metadata.count("spherical_yaw")   > 0);
    CHECK(test_reader.info.metadata.count("spherical_pitch") > 0);
    CHECK(test_reader.info.metadata.count("spherical_roll")  > 0);

    // Spot-check yaw value
    float yaw_found = std::stof(test_reader.info.metadata["spherical_yaw"]);
    CHECK(yaw_found == Approx(test_yaw).margin(0.5f));

    // Clean up
    test_reader.Close();
    std::remove(test_file.c_str());
}

TEST_CASE( "SphericalMetadata_FullOrientation", "[libopenshot][ffmpegwriter]" )
{
    // Create a reader to grab some frames
    FFmpegReader r(TEST_MEDIA_PATH "sintel_trailer-720p.mp4");
    r.Open();

    // Create a spherical metadata test video
    std::string test_file = "spherical_orientation_test.mp4";

    // Create a writer
    FFmpegWriter w(test_file);
    
    // Set options - Using MP4 with H.264 for best compatibility with spherical metadata
    w.SetVideoOptions(true, "libx264", r.info.fps, r.info.width, r.info.height, 
                      r.info.pixel_ratio, false, false, 3000000);
    w.SetAudioOptions(true, "aac", r.info.sample_rate, r.info.channels, 
                      r.info.channel_layout, 128000);

    w.PrepareStreams();

    // Add spherical metadata BEFORE opening the writer
    float test_yaw = 45.0f;
    float test_pitch = 30.0f;
    float test_roll = 15.0f;
    w.AddSphericalMetadata("equirectangular", test_yaw, test_pitch, test_roll);

    // Open writer
    w.Open();

    // Write a few frames
    for (int frame = 1; frame <= 30; frame++) {
        // Get the frame
        std::shared_ptr<Frame> f = r.GetFrame(frame);
        
        // Write the frame
        w.WriteFrame(f);
    }
    
    // Close the writer & reader
    w.Close();
    r.Close();

    // Reopen the file with FFmpegReader to verify metadata was added
    FFmpegReader test_reader(test_file);
    test_reader.Open();
    
    // Check metadata map contents for debugging
    INFO("Metadata entries in reader:");
    for (const auto& entry : test_reader.info.metadata) {
        INFO("  " << entry.first << " = " << entry.second);
    }

    // Verify presence of spherical metadata and orientation keys
    CHECK(test_reader.info.metadata.count("spherical") > 0);
    CHECK(test_reader.info.metadata["spherical"] == "1");
    CHECK(test_reader.info.metadata.count("spherical_projection") > 0);
    CHECK(test_reader.info.metadata.count("spherical_yaw")   > 0);
    CHECK(test_reader.info.metadata.count("spherical_pitch") > 0);
    CHECK(test_reader.info.metadata.count("spherical_roll")  > 0);

    // Validate each orientation value
    float yaw_found   = std::stof(test_reader.info.metadata["spherical_yaw"]);
    float pitch_found = std::stof(test_reader.info.metadata["spherical_pitch"]);
    float roll_found  = std::stof(test_reader.info.metadata["spherical_roll"]);
    CHECK(yaw_found   == Approx(test_yaw).margin(0.5f));
    CHECK(pitch_found == Approx(test_pitch).margin(0.5f));
    CHECK(roll_found  == Approx(test_roll).margin(0.5f));

    // Clean up
    test_reader.Close();
    std::remove(test_file.c_str());
}