brykt@google.com | 4de3dfe | 2012-11-27 13:44:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 14 | #include <memory> |
brykt@google.com | 4de3dfe | 2012-11-27 13:44:07 +0000 | [diff] [blame] | 15 | #include <string> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "common_video/libyuv/include/webrtc_libyuv.h" |
| 18 | #include "rtc_tools/frame_editing/frame_editing_lib.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame^] | 19 | #include "typedefs.h" // NOLINT(build/include) |
brykt@google.com | 4de3dfe | 2012-11-27 13:44:07 +0000 | [diff] [blame] | 20 | |
brykt@google.com | 4de3dfe | 2012-11-27 13:44:07 +0000 | [diff] [blame] | 21 | namespace webrtc { |
| 22 | |
ehmaldonado | 1dffc62 | 2017-02-02 08:10:00 -0800 | [diff] [blame] | 23 | int EditFrames(const std::string& in_path, int width, int height, |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 24 | int first_frame_to_process, int interval, |
ehmaldonado | 1dffc62 | 2017-02-02 08:10:00 -0800 | [diff] [blame] | 25 | int last_frame_to_process, const std::string& out_path) { |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 26 | if (last_frame_to_process < first_frame_to_process) { |
brykt@google.com | 4de3dfe | 2012-11-27 13:44:07 +0000 | [diff] [blame] | 27 | fprintf(stderr, "The set of frames to cut is empty! (l < f)\n"); |
| 28 | return -10; |
| 29 | } |
| 30 | |
brykt@google.com | c7896df | 2012-11-30 12:37:14 +0000 | [diff] [blame] | 31 | FILE* in_fid = fopen(in_path.c_str() , "rb"); |
brykt@google.com | 4de3dfe | 2012-11-27 13:44:07 +0000 | [diff] [blame] | 32 | if (!in_fid) { |
| 33 | fprintf(stderr, "Could not read input file: %s.\n", in_path.c_str()); |
| 34 | return -11; |
| 35 | } |
| 36 | |
| 37 | // Frame size of I420. |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 38 | size_t frame_length = CalcBufferSize(VideoType::kI420, width, height); |
brykt@google.com | 4de3dfe | 2012-11-27 13:44:07 +0000 | [diff] [blame] | 39 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 40 | std::unique_ptr<uint8_t[]> temp_buffer(new uint8_t[frame_length]); |
brykt@google.com | 4de3dfe | 2012-11-27 13:44:07 +0000 | [diff] [blame] | 41 | |
brykt@google.com | c7896df | 2012-11-30 12:37:14 +0000 | [diff] [blame] | 42 | FILE* out_fid = fopen(out_path.c_str(), "wb"); |
brykt@google.com | 4de3dfe | 2012-11-27 13:44:07 +0000 | [diff] [blame] | 43 | |
| 44 | if (!out_fid) { |
| 45 | fprintf(stderr, "Could not open output file: %s.\n", out_path.c_str()); |
brykt@google.com | e19b078 | 2012-12-13 14:46:40 +0000 | [diff] [blame] | 46 | fclose(in_fid); |
brykt@google.com | 4de3dfe | 2012-11-27 13:44:07 +0000 | [diff] [blame] | 47 | return -12; |
| 48 | } |
| 49 | |
| 50 | int num_frames_read = 0; |
brykt@google.com | e19b078 | 2012-12-13 14:46:40 +0000 | [diff] [blame] | 51 | int num_frames_read_between = 0; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 52 | size_t num_bytes_read; |
brykt@google.com | 4de3dfe | 2012-11-27 13:44:07 +0000 | [diff] [blame] | 53 | |
| 54 | while ((num_bytes_read = fread(temp_buffer.get(), 1, frame_length, in_fid)) |
| 55 | == frame_length) { |
brykt@google.com | e19b078 | 2012-12-13 14:46:40 +0000 | [diff] [blame] | 56 | num_frames_read++; |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 57 | if ((num_frames_read < first_frame_to_process) || |
| 58 | (last_frame_to_process < num_frames_read)) { |
brykt@google.com | 4de3dfe | 2012-11-27 13:44:07 +0000 | [diff] [blame] | 59 | fwrite(temp_buffer.get(), 1, frame_length, out_fid); |
brykt@google.com | 4de3dfe | 2012-11-27 13:44:07 +0000 | [diff] [blame] | 60 | } else { |
brykt@google.com | e19b078 | 2012-12-13 14:46:40 +0000 | [diff] [blame] | 61 | num_frames_read_between++; |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 62 | if (interval <= 0) { |
| 63 | if (interval == -1) { |
| 64 | // Remove all frames. |
| 65 | } else { |
| 66 | if (((num_frames_read_between - 1) % interval) == 0) { |
| 67 | // Keep only every |interval| frame. |
| 68 | fwrite(temp_buffer.get(), 1, frame_length, out_fid); |
| 69 | } |
| 70 | } |
| 71 | } else if (interval > 0) { |
| 72 | for (int i = 1; i <= interval; ++i) { |
| 73 | fwrite(temp_buffer.get(), 1, frame_length, out_fid); |
| 74 | } |
brykt@google.com | e19b078 | 2012-12-13 14:46:40 +0000 | [diff] [blame] | 75 | } |
brykt@google.com | 4de3dfe | 2012-11-27 13:44:07 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | if (num_bytes_read > 0 && num_bytes_read < frame_length) { |
| 79 | printf("Frame to small! Last frame truncated.\n"); |
| 80 | } |
brykt@google.com | 4de3dfe | 2012-11-27 13:44:07 +0000 | [diff] [blame] | 81 | fclose(in_fid); |
| 82 | fclose(out_fid); |
| 83 | |
| 84 | printf("Done editing!\n"); |
| 85 | return 0; |
| 86 | } |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 87 | } // namespace webrtc |