blob: 79c6033a3056e561053eaeb09a0310c73d449ff1 [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
14#include <string>
15
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000016#include "webrtc/base/scoped_ptr.h"
brykt@google.com4de3dfe2012-11-27 13:44:07 +000017#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
brykt@google.com4de3dfe2012-11-27 13:44:07 +000018#include "webrtc/typedefs.h"
19
20using std::string;
21
22namespace webrtc {
23
brykt@google.comf5568902012-12-20 11:42:45 +000024int EditFrames(const string& in_path, int width, int height,
25 int first_frame_to_process, int interval,
26 int last_frame_to_process, const string& out_path) {
27 if (last_frame_to_process < first_frame_to_process) {
brykt@google.com4de3dfe2012-11-27 13:44:07 +000028 fprintf(stderr, "The set of frames to cut is empty! (l < f)\n");
29 return -10;
30 }
31
brykt@google.comc7896df2012-11-30 12:37:14 +000032 FILE* in_fid = fopen(in_path.c_str() , "rb");
brykt@google.com4de3dfe2012-11-27 13:44:07 +000033 if (!in_fid) {
34 fprintf(stderr, "Could not read input file: %s.\n", in_path.c_str());
35 return -11;
36 }
37
38 // Frame size of I420.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000039 size_t frame_length = CalcBufferSize(kI420, width, height);
brykt@google.com4de3dfe2012-11-27 13:44:07 +000040
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000041 rtc::scoped_ptr<uint8_t[]> temp_buffer(new uint8_t[frame_length]);
brykt@google.com4de3dfe2012-11-27 13:44:07 +000042
brykt@google.comc7896df2012-11-30 12:37:14 +000043 FILE* out_fid = fopen(out_path.c_str(), "wb");
brykt@google.com4de3dfe2012-11-27 13:44:07 +000044
45 if (!out_fid) {
46 fprintf(stderr, "Could not open output file: %s.\n", out_path.c_str());
brykt@google.come19b0782012-12-13 14:46:40 +000047 fclose(in_fid);
brykt@google.com4de3dfe2012-11-27 13:44:07 +000048 return -12;
49 }
50
51 int num_frames_read = 0;
brykt@google.come19b0782012-12-13 14:46:40 +000052 int num_frames_read_between = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000053 size_t num_bytes_read;
brykt@google.com4de3dfe2012-11-27 13:44:07 +000054
55 while ((num_bytes_read = fread(temp_buffer.get(), 1, frame_length, in_fid))
56 == frame_length) {
brykt@google.come19b0782012-12-13 14:46:40 +000057 num_frames_read++;
brykt@google.comf5568902012-12-20 11:42:45 +000058 if ((num_frames_read < first_frame_to_process) ||
59 (last_frame_to_process < num_frames_read)) {
brykt@google.com4de3dfe2012-11-27 13:44:07 +000060 fwrite(temp_buffer.get(), 1, frame_length, out_fid);
brykt@google.com4de3dfe2012-11-27 13:44:07 +000061 } else {
brykt@google.come19b0782012-12-13 14:46:40 +000062 num_frames_read_between++;
brykt@google.comf5568902012-12-20 11:42:45 +000063 if (interval <= 0) {
64 if (interval == -1) {
65 // Remove all frames.
66 } else {
67 if (((num_frames_read_between - 1) % interval) == 0) {
68 // Keep only every |interval| frame.
69 fwrite(temp_buffer.get(), 1, frame_length, out_fid);
70 }
71 }
72 } else if (interval > 0) {
73 for (int i = 1; i <= interval; ++i) {
74 fwrite(temp_buffer.get(), 1, frame_length, out_fid);
75 }
brykt@google.come19b0782012-12-13 14:46:40 +000076 }
brykt@google.com4de3dfe2012-11-27 13:44:07 +000077 }
78 }
79 if (num_bytes_read > 0 && num_bytes_read < frame_length) {
80 printf("Frame to small! Last frame truncated.\n");
81 }
brykt@google.com4de3dfe2012-11-27 13:44:07 +000082 fclose(in_fid);
83 fclose(out_fid);
84
85 printf("Done editing!\n");
86 return 0;
87}
brykt@google.comf5568902012-12-20 11:42:45 +000088} // namespace webrtc