| 1 | /* |
| 2 | This file is part of the CVD Library. |
| 3 | |
| 4 | Copyright (C) 2022 The Authors |
| 5 | |
| 6 | This library is free software, see LICENSE file for details |
| 7 | */ |
| 8 | #include <cvd/videoreader.h> |
| 9 | |
| 10 | #include "test_utility.h" |
| 11 | |
| 12 | #include <cvd/rgba.h> |
| 13 | |
| 14 | #include <algorithm> |
| 15 | #include <cstdlib> |
| 16 | #include <string> |
| 17 | |
| 18 | namespace |
| 19 | { |
| 20 | using CVD::Rgba; |
| 21 | using CVD::VideoReader; |
| 22 | using CVD::Testing::assert_equal; |
| 23 | using CVD::Testing::assert_near; |
| 24 | } |
| 25 | |
| 26 | int main(int argc, char* argv[]) |
| 27 | { |
| 28 | if(argc < 2) |
| 29 | { |
| 30 | std::cerr << "usage: " << argv[0] << " videoreader.mp4\n"; |
| 31 | exit(EXIT_FAILURE); |
| 32 | } |
| 33 | VideoReader reader(argv[1], 4); |
| 34 | std::cout << "timebase: " << reader.timebase().num << "/" << reader.timebase().den << "\n"; |
| 35 | for(int i = 0; i < 20; ++i) |
| 36 | { |
| 37 | auto [frame, timestamp] = reader.get_frame(); |
| 38 | |
| 39 | int64_t expected_timestamp_ms = 100 * i; |
| 40 | int64_t expected_timestamp = (expected_timestamp_ms * reader.timebase().den) / (1000 * reader.timebase().num); |
| 41 | assert_equal(expected_timestamp, timestamp, "Incorrect timestamp for frame " + std::to_string(i)); |
| 42 | |
| 43 | if(i < 10) |
| 44 | { |
| 45 | assert_near(Rgba<uint8_t>(0, 0, 0, 255), frame[32][32], "Incorrect top left square on frame " + std::to_string(i)); |
| 46 | assert_near(Rgba<uint8_t>(255, 0, 0, 255), frame[32][96], "Incorrect top right square on frame " + std::to_string(i)); |
| 47 | assert_near(Rgba<uint8_t>(0, 255, 0, 255), frame[96][32], "Incorrect bottom left square on frame " + std::to_string(i)); |
| 48 | assert_near(Rgba<uint8_t>(0, 0, 255, 255), frame[96][96], "Incorrect bottom right square on frame " + std::to_string(i)); |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | assert_near(Rgba<uint8_t>(0, 0, 0, 255), frame[32][32], "Incorrect top left square on frame " + std::to_string(i)); |
| 53 | assert_near(Rgba<uint8_t>(0, 255, 255, 255), frame[32][96], "Incorrect top right square on frame " + std::to_string(i)); |
| 54 | assert_near(Rgba<uint8_t>(255, 0, 255, 255), frame[96][32], "Incorrect bottom left square on frame " + std::to_string(i)); |
| 55 | assert_near(Rgba<uint8_t>(255, 255, 0, 255), frame[96][96], "Incorrect bottom right square on frame " + std::to_string(i)); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | auto [frame, timestamp] = reader.get_frame(); |
| 60 | if(frame.size().x != 0) |
| 61 | { |
| 62 | std::cerr << "Expected end of stream, received frame\n"; |
| 63 | exit(EXIT_FAILURE); |
| 64 | } |
| 65 | } |