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