blob: 5af018456782a5c1b2d0e3613f796e24b41a66d9 [file] [log] [blame]
brykt@google.com4de3dfe2012-11-27 13:44:07 +00001/*
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
kwibergbfefb032016-05-01 14:53:46 -070014#include <memory>
brykt@google.com4de3dfe2012-11-27 13:44:07 +000015#include <string>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "common_video/libyuv/include/webrtc_libyuv.h"
18#include "rtc_tools/frame_editing/frame_editing_lib.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020019#include "typedefs.h" // NOLINT(build/include)
brykt@google.com4de3dfe2012-11-27 13:44:07 +000020
brykt@google.com4de3dfe2012-11-27 13:44:07 +000021namespace webrtc {
22
ehmaldonado1dffc622017-02-02 08:10:00 -080023int EditFrames(const std::string& in_path, int width, int height,
brykt@google.comf5568902012-12-20 11:42:45 +000024 int first_frame_to_process, int interval,
ehmaldonado1dffc622017-02-02 08:10:00 -080025 int last_frame_to_process, const std::string& out_path) {
brykt@google.comf5568902012-12-20 11:42:45 +000026 if (last_frame_to_process < first_frame_to_process) {
brykt@google.com4de3dfe2012-11-27 13:44:07 +000027 fprintf(stderr, "The set of frames to cut is empty! (l < f)\n");
28 return -10;
29 }
30
brykt@google.comc7896df2012-11-30 12:37:14 +000031 FILE* in_fid = fopen(in_path.c_str() , "rb");
brykt@google.com4de3dfe2012-11-27 13:44:07 +000032 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.
nisseeb44b392017-04-28 07:18:05 -070038 size_t frame_length = CalcBufferSize(VideoType::kI420, width, height);
brykt@google.com4de3dfe2012-11-27 13:44:07 +000039
kwibergbfefb032016-05-01 14:53:46 -070040 std::unique_ptr<uint8_t[]> temp_buffer(new uint8_t[frame_length]);
brykt@google.com4de3dfe2012-11-27 13:44:07 +000041
brykt@google.comc7896df2012-11-30 12:37:14 +000042 FILE* out_fid = fopen(out_path.c_str(), "wb");
brykt@google.com4de3dfe2012-11-27 13:44:07 +000043
44 if (!out_fid) {
45 fprintf(stderr, "Could not open output file: %s.\n", out_path.c_str());
brykt@google.come19b0782012-12-13 14:46:40 +000046 fclose(in_fid);
brykt@google.com4de3dfe2012-11-27 13:44:07 +000047 return -12;
48 }
49
50 int num_frames_read = 0;
brykt@google.come19b0782012-12-13 14:46:40 +000051 int num_frames_read_between = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000052 size_t num_bytes_read;
brykt@google.com4de3dfe2012-11-27 13:44:07 +000053
54 while ((num_bytes_read = fread(temp_buffer.get(), 1, frame_length, in_fid))
55 == frame_length) {
brykt@google.come19b0782012-12-13 14:46:40 +000056 num_frames_read++;
brykt@google.comf5568902012-12-20 11:42:45 +000057 if ((num_frames_read < first_frame_to_process) ||
58 (last_frame_to_process < num_frames_read)) {
brykt@google.com4de3dfe2012-11-27 13:44:07 +000059 fwrite(temp_buffer.get(), 1, frame_length, out_fid);
brykt@google.com4de3dfe2012-11-27 13:44:07 +000060 } else {
brykt@google.come19b0782012-12-13 14:46:40 +000061 num_frames_read_between++;
brykt@google.comf5568902012-12-20 11:42:45 +000062 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.come19b0782012-12-13 14:46:40 +000075 }
brykt@google.com4de3dfe2012-11-27 13:44:07 +000076 }
77 }
78 if (num_bytes_read > 0 && num_bytes_read < frame_length) {
79 printf("Frame to small! Last frame truncated.\n");
80 }
brykt@google.com4de3dfe2012-11-27 13:44:07 +000081 fclose(in_fid);
82 fclose(out_fid);
83
84 printf("Done editing!\n");
85 return 0;
86}
brykt@google.comf5568902012-12-20 11:42:45 +000087} // namespace webrtc