/* * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ #include "rtc_tools/video_file_reader.h" #include #include #include #include #include #include "absl/types/optional.h" #include "api/video/i420_buffer.h" #include "rtc_base/logging.h" #include "rtc_base/refcountedobject.h" #include "rtc_base/string_to_number.h" #include "rtc_base/stringencode.h" #include "rtc_base/stringutils.h" namespace webrtc { namespace test { namespace { // Common base class for .yuv and .y4m files. class VideoFile : public Video { public: VideoFile(int width, int height, const std::vector& frame_positions, FILE* file) : width_(width), height_(height), frame_positions_(frame_positions), file_(file) {} ~VideoFile() override { fclose(file_); } size_t number_of_frames() const override { return frame_positions_.size(); } int width() const override { return width_; } int height() const override { return height_; } rtc::scoped_refptr GetFrame( size_t frame_index) const override { RTC_CHECK_LT(frame_index, frame_positions_.size()); fsetpos(file_, &frame_positions_[frame_index]); rtc::scoped_refptr buffer = I420Buffer::Create(width_, height_); fread(reinterpret_cast(buffer->MutableDataY()), /* size= */ 1, width_ * height_, file_); fread(reinterpret_cast(buffer->MutableDataU()), /* size= */ 1, buffer->ChromaWidth() * buffer->ChromaHeight(), file_); fread(reinterpret_cast(buffer->MutableDataV()), /* size= */ 1, buffer->ChromaWidth() * buffer->ChromaHeight(), file_); if (ferror(file_) != 0) { RTC_LOG(LS_ERROR) << "Could not read YUV data for frame " << frame_index; return nullptr; } return buffer; } private: const int width_; const int height_; const std::vector frame_positions_; FILE* const file_; }; } // namespace Video::Iterator::Iterator(const rtc::scoped_refptr& video, size_t index) : video_(video), index_(index) {} Video::Iterator::Iterator(const Video::Iterator& other) = default; Video::Iterator::Iterator(Video::Iterator&& other) = default; Video::Iterator& Video::Iterator::operator=(Video::Iterator&&) = default; Video::Iterator& Video::Iterator::operator=(const Video::Iterator&) = default; Video::Iterator::~Iterator() = default; rtc::scoped_refptr Video::Iterator::operator*() const { return video_->GetFrame(index_); } bool Video::Iterator::operator==(const Video::Iterator& other) const { return index_ == other.index_; } bool Video::Iterator::operator!=(const Video::Iterator& other) const { return !(*this == other); } Video::Iterator Video::Iterator::operator++(int) { const Iterator copy = *this; ++*this; return copy; } Video::Iterator& Video::Iterator::operator++() { ++index_; return *this; } Video::Iterator Video::begin() const { return Iterator(this, 0); } Video::Iterator Video::end() const { return Iterator(this, number_of_frames()); } rtc::scoped_refptr