Jianhui Dai | a2e945e | 2023-06-19 01:36:42 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | #include "rtc_tools/video_encoder/encoded_image_file_writer.h" |
| 11 | |
| 12 | #include "modules/video_coding/svc/scalability_mode_util.h" |
| 13 | #include "rtc_base/logging.h" |
| 14 | |
| 15 | namespace webrtc { |
| 16 | namespace test { |
| 17 | |
| 18 | EncodedImageFileWriter::EncodedImageFileWriter( |
| 19 | const VideoCodec& video_codec_setting) |
| 20 | : video_codec_setting_(video_codec_setting) { |
| 21 | const char* codec_string = |
| 22 | CodecTypeToPayloadString(video_codec_setting.codecType); |
| 23 | |
| 24 | // Retrieve scalability mode information. |
| 25 | absl::optional<ScalabilityMode> scalability_mode = |
| 26 | video_codec_setting.GetScalabilityMode(); |
| 27 | RTC_CHECK(scalability_mode); |
| 28 | spatial_layers_ = ScalabilityModeToNumSpatialLayers(*scalability_mode); |
| 29 | temporal_layers_ = ScalabilityModeToNumTemporalLayers(*scalability_mode); |
| 30 | inter_layer_pred_mode_ = |
| 31 | ScalabilityModeToInterLayerPredMode(*scalability_mode); |
| 32 | |
| 33 | RTC_CHECK_GT(spatial_layers_, 0); |
| 34 | RTC_CHECK_GT(temporal_layers_, 0); |
| 35 | // Create writer for every decode target. |
| 36 | for (int i = 0; i < spatial_layers_; ++i) { |
| 37 | for (int j = 0; j < temporal_layers_; ++j) { |
| 38 | char buffer[256]; |
| 39 | rtc::SimpleStringBuilder name(buffer); |
| 40 | name << "output-" << codec_string << "-" |
| 41 | << ScalabilityModeToString(*scalability_mode) << "-L" << i << "T" |
| 42 | << j << ".ivf"; |
| 43 | |
| 44 | decode_target_writers_.emplace_back(std::make_pair( |
| 45 | IvfFileWriter::Wrap(FileWrapper::OpenWriteOnly(name.str()), 0), |
| 46 | name.str())); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | EncodedImageFileWriter::~EncodedImageFileWriter() { |
| 52 | for (size_t i = 0; i < decode_target_writers_.size(); ++i) { |
| 53 | decode_target_writers_[i].first->Close(); |
| 54 | RTC_LOG(LS_INFO) << "Written: " << decode_target_writers_[i].second; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | int EncodedImageFileWriter::Write(const EncodedImage& encoded_image) { |
| 59 | // L1T1 does not set `SpatialIndex` and `TemporalIndex` in `EncodedImage`. |
| 60 | const int spatial_index = encoded_image.SpatialIndex().value_or(0); |
| 61 | const int temporal_index = encoded_image.TemporalIndex().value_or(0); |
| 62 | RTC_CHECK_LT(spatial_index, spatial_layers_); |
| 63 | RTC_CHECK_LT(temporal_index, temporal_layers_); |
| 64 | |
| 65 | if (spatial_index == 0) { |
| 66 | is_base_layer_key_frame = |
| 67 | (encoded_image._frameType == VideoFrameType::kVideoFrameKey); |
| 68 | } |
| 69 | |
| 70 | switch (inter_layer_pred_mode_) { |
| 71 | case InterLayerPredMode::kOff: { |
| 72 | // Write to this spatial layer. |
| 73 | for (int j = temporal_index; j < temporal_layers_; ++j) { |
| 74 | const int index = spatial_index * temporal_layers_ + j; |
| 75 | RTC_CHECK_LT(index, decode_target_writers_.size()); |
| 76 | |
| 77 | decode_target_writers_[index].first->WriteFrame( |
| 78 | encoded_image, video_codec_setting_.codecType); |
| 79 | } |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | case InterLayerPredMode::kOn: { |
| 84 | // Write to this and higher spatial layers. |
| 85 | for (int i = spatial_index; i < spatial_layers_; ++i) { |
| 86 | for (int j = temporal_index; j < temporal_layers_; ++j) { |
| 87 | const int index = i * temporal_layers_ + j; |
| 88 | RTC_CHECK_LT(index, decode_target_writers_.size()); |
| 89 | |
| 90 | decode_target_writers_[index].first->WriteFrame( |
| 91 | encoded_image, video_codec_setting_.codecType); |
| 92 | } |
| 93 | } |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | case InterLayerPredMode::kOnKeyPic: { |
| 98 | for (int i = spatial_index; i < spatial_layers_; ++i) { |
| 99 | for (int j = temporal_index; j < temporal_layers_; ++j) { |
| 100 | const int index = i * temporal_layers_ + j; |
| 101 | RTC_CHECK_LT(index, decode_target_writers_.size()); |
| 102 | |
| 103 | decode_target_writers_[index].first->WriteFrame( |
| 104 | encoded_image, video_codec_setting_.codecType); |
| 105 | } |
| 106 | |
| 107 | // Write to higher spatial layers only if key frame. |
| 108 | if (!is_base_layer_key_frame) { |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | } // namespace test |
| 120 | } // namespace webrtc |