2012-11-27 13:44:07 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2012 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 <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2016-05-01 14:53:46 -07:00
|
|
|
#include <memory>
|
2012-11-27 13:44:07 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "common_video/libyuv/include/webrtc_libyuv.h"
|
|
|
|
|
#include "rtc_tools/frame_editing/frame_editing_lib.h"
|
2012-11-27 13:44:07 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2017-02-02 08:10:00 -08:00
|
|
|
int EditFrames(const std::string& in_path,
|
|
|
|
|
int width,
|
|
|
|
|
int height,
|
2012-12-20 11:42:45 +00:00
|
|
|
int first_frame_to_process,
|
|
|
|
|
int interval,
|
2017-02-02 08:10:00 -08:00
|
|
|
int last_frame_to_process,
|
|
|
|
|
const std::string& out_path) {
|
2012-12-20 11:42:45 +00:00
|
|
|
if (last_frame_to_process < first_frame_to_process) {
|
2012-11-27 13:44:07 +00:00
|
|
|
fprintf(stderr, "The set of frames to cut is empty! (l < f)\n");
|
|
|
|
|
return -10;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-30 12:37:14 +00:00
|
|
|
FILE* in_fid = fopen(in_path.c_str(), "rb");
|
2012-11-27 13:44:07 +00:00
|
|
|
if (!in_fid) {
|
|
|
|
|
fprintf(stderr, "Could not read input file: %s.\n", in_path.c_str());
|
|
|
|
|
return -11;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Frame size of I420.
|
2017-04-28 07:18:05 -07:00
|
|
|
size_t frame_length = CalcBufferSize(VideoType::kI420, width, height);
|
2012-11-27 13:44:07 +00:00
|
|
|
|
2016-05-01 14:53:46 -07:00
|
|
|
std::unique_ptr<uint8_t[]> temp_buffer(new uint8_t[frame_length]);
|
2012-11-27 13:44:07 +00:00
|
|
|
|
2012-11-30 12:37:14 +00:00
|
|
|
FILE* out_fid = fopen(out_path.c_str(), "wb");
|
2012-11-27 13:44:07 +00:00
|
|
|
|
|
|
|
|
if (!out_fid) {
|
|
|
|
|
fprintf(stderr, "Could not open output file: %s.\n", out_path.c_str());
|
2012-12-13 14:46:40 +00:00
|
|
|
fclose(in_fid);
|
2012-11-27 13:44:07 +00:00
|
|
|
return -12;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int num_frames_read = 0;
|
2012-12-13 14:46:40 +00:00
|
|
|
int num_frames_read_between = 0;
|
Use size_t more consistently for packet/payload lengths.
See design doc at https://docs.google.com/a/chromium.org/document/d/1I6nmE9D_BmCY-IoV6MDPY2V6WYpEI-dg2apWXTfZyUI/edit?usp=sharing for more information.
This CL was reviewed and approved in pieces in the following CLs:
https://webrtc-codereview.appspot.com/24209004/
https://webrtc-codereview.appspot.com/24229004/
https://webrtc-codereview.appspot.com/24259004/
https://webrtc-codereview.appspot.com/25109004/
https://webrtc-codereview.appspot.com/26099004/
https://webrtc-codereview.appspot.com/27069004/
https://webrtc-codereview.appspot.com/27969004/
https://webrtc-codereview.appspot.com/27989004/
https://webrtc-codereview.appspot.com/29009004/
https://webrtc-codereview.appspot.com/30929004/
https://webrtc-codereview.appspot.com/30939004/
https://webrtc-codereview.appspot.com/31999004/
Committing as TBR to the original reviewers.
BUG=chromium:81439
TEST=none
TBR=pthatcher,henrik.lundin,tina.legrand,stefan,tkchin,glaznev,kjellander,perkj,mflodman,henrika,asapersson,niklas.enbom
Review URL: https://webrtc-codereview.appspot.com/23129004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7726 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-11-20 22:28:14 +00:00
|
|
|
size_t num_bytes_read;
|
2012-11-27 13:44:07 +00:00
|
|
|
|
|
|
|
|
while ((num_bytes_read = fread(temp_buffer.get(), 1, frame_length, in_fid)) ==
|
|
|
|
|
frame_length) {
|
2012-12-13 14:46:40 +00:00
|
|
|
num_frames_read++;
|
2012-12-20 11:42:45 +00:00
|
|
|
if ((num_frames_read < first_frame_to_process) ||
|
|
|
|
|
(last_frame_to_process < num_frames_read)) {
|
2012-11-27 13:44:07 +00:00
|
|
|
fwrite(temp_buffer.get(), 1, frame_length, out_fid);
|
|
|
|
|
} else {
|
2012-12-13 14:46:40 +00:00
|
|
|
num_frames_read_between++;
|
2012-12-20 11:42:45 +00:00
|
|
|
if (interval <= 0) {
|
|
|
|
|
if (interval == -1) {
|
|
|
|
|
// Remove all frames.
|
|
|
|
|
} else {
|
|
|
|
|
if (((num_frames_read_between - 1) % interval) == 0) {
|
|
|
|
|
// Keep only every |interval| frame.
|
|
|
|
|
fwrite(temp_buffer.get(), 1, frame_length, out_fid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (interval > 0) {
|
|
|
|
|
for (int i = 1; i <= interval; ++i) {
|
|
|
|
|
fwrite(temp_buffer.get(), 1, frame_length, out_fid);
|
|
|
|
|
}
|
2012-12-13 14:46:40 +00:00
|
|
|
}
|
2012-11-27 13:44:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (num_bytes_read > 0 && num_bytes_read < frame_length) {
|
|
|
|
|
printf("Frame to small! Last frame truncated.\n");
|
|
|
|
|
}
|
|
|
|
|
fclose(in_fid);
|
|
|
|
|
fclose(out_fid);
|
|
|
|
|
|
|
|
|
|
printf("Done editing!\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2012-12-20 11:42:45 +00:00
|
|
|
} // namespace webrtc
|