blob: 748970f0ec4d52343da202b44f7b582e721becd8 [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
16#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
17#include "webrtc/system_wrappers/interface/scoped_ptr.h"
18#include "webrtc/typedefs.h"
19
20using std::string;
21
22namespace webrtc {
23
brykt@google.come19b0782012-12-13 14:46:40 +000024int CutFrames(const string& in_path, int width, int height,
25 int first_frame_to_cut, int interval, int last_frame_to_cut,
26 const string& out_path) {
27
brykt@google.com4de3dfe2012-11-27 13:44:07 +000028 if (last_frame_to_cut < first_frame_to_cut) {
29 fprintf(stderr, "The set of frames to cut is empty! (l < f)\n");
30 return -10;
31 }
32
brykt@google.comc7896df2012-11-30 12:37:14 +000033 FILE* in_fid = fopen(in_path.c_str() , "rb");
brykt@google.com4de3dfe2012-11-27 13:44:07 +000034 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.
40 int frame_length = CalcBufferSize(kI420, width, height);
41
42 webrtc::scoped_array<uint8_t> temp_buffer(new uint8_t[frame_length]);
43
brykt@google.comc7896df2012-11-30 12:37:14 +000044 FILE* out_fid = fopen(out_path.c_str(), "wb");
brykt@google.com4de3dfe2012-11-27 13:44:07 +000045
46 if (!out_fid) {
47 fprintf(stderr, "Could not open output file: %s.\n", out_path.c_str());
brykt@google.come19b0782012-12-13 14:46:40 +000048 fclose(in_fid);
brykt@google.com4de3dfe2012-11-27 13:44:07 +000049 return -12;
50 }
51
52 int num_frames_read = 0;
brykt@google.come19b0782012-12-13 14:46:40 +000053 int num_frames_read_between = 0;
brykt@google.com4de3dfe2012-11-27 13:44:07 +000054 int num_bytes_read;
55
56 while ((num_bytes_read = fread(temp_buffer.get(), 1, frame_length, in_fid))
57 == frame_length) {
brykt@google.come19b0782012-12-13 14:46:40 +000058 num_frames_read++;
brykt@google.com4de3dfe2012-11-27 13:44:07 +000059 if ((num_frames_read < first_frame_to_cut) ||
60 (last_frame_to_cut < num_frames_read)) {
61 fwrite(temp_buffer.get(), 1, frame_length, out_fid);
brykt@google.com4de3dfe2012-11-27 13:44:07 +000062 } else {
brykt@google.come19b0782012-12-13 14:46:40 +000063 num_frames_read_between++;
64 if (num_frames_read_between % interval != 0) {
65 fwrite(temp_buffer.get(), 1, frame_length, out_fid);
66 }
brykt@google.com4de3dfe2012-11-27 13:44:07 +000067 }
68 }
69 if (num_bytes_read > 0 && num_bytes_read < frame_length) {
70 printf("Frame to small! Last frame truncated.\n");
71 }
72
73 fclose(in_fid);
74 fclose(out_fid);
75
76 printf("Done editing!\n");
77 return 0;
78}
79}
80