blob: b91e57c963da8d85046ddbc3641fa0eded17bf51 [file] [log] [blame]
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +00001/*
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef TEST_TESTSUPPORT_FRAME_WRITER_H_
12#define TEST_TESTSUPPORT_FRAME_WRITER_H_
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000013
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000014#include <stdio.h>
15
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000016#include <string>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/video/video_frame.h"
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000019
20namespace webrtc {
21namespace test {
22
23// Handles writing of video files.
24class FrameWriter {
25 public:
26 virtual ~FrameWriter() {}
27
28 // Initializes the file handler, i.e. opens the input and output files etc.
29 // This must be called before reading or writing frames has started.
30 // Returns false if an error has occurred, in addition to printing to stderr.
31 virtual bool Init() = 0;
32
33 // Writes a frame of the configured frame length to the output file.
34 // Returns true if the write was successful, false otherwise.
pbos@webrtc.orga5f17872013-04-09 11:10:21 +000035 virtual bool WriteFrame(uint8_t* frame_buffer) = 0;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000036
37 // Closes the output file if open. Essentially makes this class impossible
38 // to use anymore. Will also be invoked by the destructor.
39 virtual void Close() = 0;
40
41 // Frame length in bytes of a single frame image.
kjellander@webrtc.orgfa53d872013-02-04 10:07:17 +000042 virtual size_t FrameLength() = 0;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000043};
44
brandtrb78bc752017-02-22 01:26:59 -080045// Writes raw I420 frames in sequence.
46class YuvFrameWriterImpl : public FrameWriter {
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000047 public:
48 // Creates a file handler. The input file is assumed to exist and be readable
49 // and the output file must be writable.
50 // Parameters:
51 // output_filename The file to write. Will be overwritten if already
52 // existing.
brandtrb78bc752017-02-22 01:26:59 -080053 // width, height Size of each frame to read.
54 YuvFrameWriterImpl(std::string output_filename, int width, int height);
55 ~YuvFrameWriterImpl() override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000056 bool Init() override;
57 bool WriteFrame(uint8_t* frame_buffer) override;
58 void Close() override;
59 size_t FrameLength() override;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000060
brandtrb78bc752017-02-22 01:26:59 -080061 protected:
62 const std::string output_filename_;
brandtr2a8135a2017-02-21 05:24:03 -080063 size_t frame_length_in_bytes_;
brandtrb78bc752017-02-22 01:26:59 -080064 const int width_;
65 const int height_;
brandtr2a8135a2017-02-21 05:24:03 -080066 FILE* output_file_;
brandtr872104a2017-02-21 03:59:15 -080067};
68
brandtrb78bc752017-02-22 01:26:59 -080069// Writes raw I420 frames in sequence, but with Y4M file and frame headers for
70// more convenient playback in external media players.
71class Y4mFrameWriterImpl : public YuvFrameWriterImpl {
72 public:
73 Y4mFrameWriterImpl(std::string output_filename,
74 int width,
75 int height,
76 int frame_rate);
77 ~Y4mFrameWriterImpl() override;
78 bool Init() override;
79 bool WriteFrame(uint8_t* frame_buffer) override;
80
81 private:
82 const int frame_rate_;
83};
84
ilnikd986d762017-08-28 06:08:33 -070085// LibJpeg is not available on iOS. This class will do nothing on iOS.
ilnikee42d192017-08-22 07:16:20 -070086class JpegFrameWriter {
87 public:
Yves Gerey665174f2018-06-19 15:03:05 +020088 JpegFrameWriter(const std::string& output_filename);
ilnikd986d762017-08-28 06:08:33 -070089 // Quality can be from 0 (worst) to 100 (best). Best quality is still lossy.
90 // WriteFrame can be called only once. Subsequent calls will fail.
ilnikee42d192017-08-22 07:16:20 -070091 bool WriteFrame(const VideoFrame& input_frame, int quality);
92
ilnikd986d762017-08-28 06:08:33 -070093#if !defined(WEBRTC_IOS)
ilnikee42d192017-08-22 07:16:20 -070094 private:
95 bool frame_written_;
96 const std::string output_filename_;
97 FILE* output_file_;
ilnikee42d192017-08-22 07:16:20 -070098#endif
ilnikd986d762017-08-28 06:08:33 -070099};
ilnikee42d192017-08-22 07:16:20 -0700100
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000101} // namespace test
102} // namespace webrtc
103
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200104#endif // TEST_TESTSUPPORT_FRAME_WRITER_H_