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 | |
| 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 | |
| 20 | using std::string; |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | int FrameCutter(const string& in_path, int width, int height, |
| 25 | int first_frame_to_cut, int last_frame_to_cut, |
| 26 | const string& out_path) { |
| 27 | if (last_frame_to_cut < first_frame_to_cut) { |
| 28 | fprintf(stderr, "The set of frames to cut is empty! (l < f)\n"); |
| 29 | return -10; |
| 30 | } |
| 31 | |
brykt@google.com | c7896df | 2012-11-30 12:37:14 +0000 | [diff] [blame^] | 32 | FILE* in_fid = fopen(in_path.c_str() , "rb"); |
brykt@google.com | 4de3dfe | 2012-11-27 13:44:07 +0000 | [diff] [blame] | 33 | 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. |
| 39 | int frame_length = CalcBufferSize(kI420, width, height); |
| 40 | |
| 41 | webrtc::scoped_array<uint8_t> temp_buffer(new uint8_t[frame_length]); |
| 42 | |
brykt@google.com | c7896df | 2012-11-30 12:37:14 +0000 | [diff] [blame^] | 43 | FILE* out_fid = fopen(out_path.c_str(), "wb"); |
brykt@google.com | 4de3dfe | 2012-11-27 13:44:07 +0000 | [diff] [blame] | 44 | |
| 45 | if (!out_fid) { |
| 46 | fprintf(stderr, "Could not open output file: %s.\n", out_path.c_str()); |
| 47 | return -12; |
| 48 | } |
| 49 | |
| 50 | int num_frames_read = 0; |
| 51 | int num_bytes_read; |
| 52 | |
| 53 | while ((num_bytes_read = fread(temp_buffer.get(), 1, frame_length, in_fid)) |
| 54 | == frame_length) { |
| 55 | if ((num_frames_read < first_frame_to_cut) || |
| 56 | (last_frame_to_cut < num_frames_read)) { |
| 57 | fwrite(temp_buffer.get(), 1, frame_length, out_fid); |
| 58 | num_frames_read++; |
| 59 | } else { |
| 60 | num_frames_read++; |
| 61 | } |
| 62 | } |
| 63 | if (num_bytes_read > 0 && num_bytes_read < frame_length) { |
| 64 | printf("Frame to small! Last frame truncated.\n"); |
| 65 | } |
| 66 | |
| 67 | fclose(in_fid); |
| 68 | fclose(out_fid); |
| 69 | |
| 70 | printf("Done editing!\n"); |
| 71 | return 0; |
| 72 | } |
| 73 | } |
| 74 | |