2011-10-11 08:44:27 -05:00
|
|
|
#ifndef OPENSHOT_FRAMEMAPPER_H
|
|
|
|
|
#define OPENSHOT_FRAMEMAPPER_H
|
|
|
|
|
|
|
|
|
|
/**
|
2013-09-09 23:32:16 -05:00
|
|
|
* @file
|
|
|
|
|
* @brief Header file for the FrameMapper class
|
|
|
|
|
* @author Copyright (c) 2008-2013 OpenShot Studios, LLC
|
2011-10-11 08:44:27 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <vector>
|
2012-10-17 09:57:02 -05:00
|
|
|
#include <tr1/memory>
|
|
|
|
|
#include "../include/Cache.h"
|
2013-09-08 16:40:57 -05:00
|
|
|
#include "../include/ReaderBase.h"
|
2012-10-17 09:57:02 -05:00
|
|
|
#include "../include/Frame.h"
|
2011-10-11 08:44:27 -05:00
|
|
|
#include "../include/FrameRate.h"
|
|
|
|
|
#include "../include/Exceptions.h"
|
|
|
|
|
#include "../include/KeyFrame.h"
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
namespace openshot
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* This enumeration determines how frame rates are increased or decreased, and
|
|
|
|
|
* whether to apply pull-down techniques or not. Pull-down techniques are only
|
|
|
|
|
* needed to remove artificial fields added when converting between 24 fps (film)
|
|
|
|
|
* and television fps (29.97 fps NTSC or 25 fps PAL).
|
|
|
|
|
*/
|
2013-09-10 12:59:06 -05:00
|
|
|
enum PulldownType
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
|
|
|
|
PULLDOWN_CLASSIC, // Classic 2:3:2:3 pull-down
|
|
|
|
|
PULLDOWN_ADVANCED, // Advanced 2:3:3:2 pull-down (minimal dirty frames)
|
|
|
|
|
PULLDOWN_NONE, // Do not apply pull-down techniques, just repeat or skip entire frames
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2013-09-09 23:32:16 -05:00
|
|
|
* @brief This struct holds a single field (half a frame).
|
2011-10-11 08:44:27 -05:00
|
|
|
*
|
|
|
|
|
* A frame of video is made up of 2 fields (half a frame). This struct points to which original
|
|
|
|
|
* frame, and whether this is the ODD or EVEN lines (i.e. top or bottom).
|
|
|
|
|
*/
|
|
|
|
|
struct Field
|
|
|
|
|
{
|
|
|
|
|
int Frame;
|
|
|
|
|
bool isOdd;
|
|
|
|
|
|
|
|
|
|
Field() : Frame(0), isOdd(true) { };
|
|
|
|
|
|
|
|
|
|
Field(int frame, bool isodd)
|
|
|
|
|
{
|
|
|
|
|
Frame = frame;
|
|
|
|
|
isOdd = isodd;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2012-10-14 23:24:27 -05:00
|
|
|
/**
|
2013-09-09 23:32:16 -05:00
|
|
|
* @brief This struct holds a the range of samples needed by this frame
|
2012-10-14 23:24:27 -05:00
|
|
|
*
|
|
|
|
|
* When frame rate is changed, the audio needs to be redistributed among the remaining
|
|
|
|
|
* frames. This struct holds the range samples needed by the this frame.
|
|
|
|
|
*/
|
|
|
|
|
struct SampleRange
|
|
|
|
|
{
|
2012-10-15 17:45:20 -05:00
|
|
|
int frame_start;
|
|
|
|
|
int sample_start;
|
2012-10-14 23:24:27 -05:00
|
|
|
|
2012-10-15 17:45:20 -05:00
|
|
|
int frame_end;
|
|
|
|
|
int sample_end;
|
2012-10-17 09:57:02 -05:00
|
|
|
|
|
|
|
|
int total;
|
2012-10-14 23:24:27 -05:00
|
|
|
};
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
/**
|
2013-09-09 23:32:16 -05:00
|
|
|
* @brief This struct holds two fields which together make up a complete video frame.
|
2011-10-11 08:44:27 -05:00
|
|
|
*
|
|
|
|
|
* These fields can point at different original frame numbers, for example the odd lines from
|
|
|
|
|
* frame 3, and the even lines of frame 4, if required by a pull-down technique.
|
|
|
|
|
*/
|
|
|
|
|
struct MappedFrame
|
|
|
|
|
{
|
|
|
|
|
Field Odd;
|
|
|
|
|
Field Even;
|
2012-10-14 23:24:27 -05:00
|
|
|
SampleRange Samples;
|
2011-10-11 08:44:27 -05:00
|
|
|
};
|
|
|
|
|
|
2012-10-14 23:24:27 -05:00
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
/**
|
2013-09-09 23:32:16 -05:00
|
|
|
* @brief This class creates a mapping between 2 different frame rates, applying a specific pull-down technique.
|
2011-10-11 08:44:27 -05:00
|
|
|
*
|
|
|
|
|
* This class creates a mapping between 2 different video files, and supports many pull-down techniques,
|
|
|
|
|
* such as 2:3:2:3 or 2:3:3:2, and also supports inverse telecine. Pull-down techniques are only needed to remove
|
|
|
|
|
* artificial fields added when converting between 24 fps (film) and television fps (29.97 fps NTSC or 25 fps PAL).
|
|
|
|
|
*
|
|
|
|
|
* Please see the following <b>Example Code</b>:
|
|
|
|
|
* \code
|
|
|
|
|
* // Create a frame mapper for a clip with 100 frames, and convert the frame rate (from 24 fps to 29.97 fps)
|
|
|
|
|
* FrameMapper mapping(100, Framerate(24, 1), Framerate(30000, 1001), PULLDOWN_CLASSIC);
|
|
|
|
|
* Frame frame2 = mapping.GetFrame(2);
|
|
|
|
|
* assert(frame2.Odd.Frame == 2);
|
|
|
|
|
* \endcode
|
|
|
|
|
*/
|
2013-09-08 16:40:57 -05:00
|
|
|
class FrameMapper : public ReaderBase {
|
2011-10-11 08:44:27 -05:00
|
|
|
private:
|
|
|
|
|
vector<Field> fields; // List of all fields
|
|
|
|
|
vector<MappedFrame> frames; // List of all frames
|
|
|
|
|
bool field_toggle; // Internal odd / even toggle (used when building the mapping)
|
2012-10-15 17:45:20 -05:00
|
|
|
Framerate original; // The original frame rate
|
|
|
|
|
Framerate target; // The target frame rate
|
2013-09-10 12:59:06 -05:00
|
|
|
PulldownType pulldown; // The pull-down technique
|
2013-09-08 16:40:57 -05:00
|
|
|
ReaderBase *reader; // The source video reader
|
2012-10-17 09:57:02 -05:00
|
|
|
Cache final_cache; // Cache of actual Frame objects
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
// Internal methods used by init
|
|
|
|
|
void AddField(int frame);
|
|
|
|
|
void AddField(Field field);
|
|
|
|
|
|
|
|
|
|
// Use the original and target frame rates and a pull-down technique to create
|
|
|
|
|
// a mapping between the original fields and frames or a video to a new frame rate.
|
|
|
|
|
// This might repeat or skip fields and frames of the original video, depending on
|
|
|
|
|
// whether the frame rate is increasing or decreasing.
|
|
|
|
|
void Init();
|
|
|
|
|
|
2012-10-15 17:45:20 -05:00
|
|
|
/// Calculate the # of samples per video frame (for a specific frame number)
|
|
|
|
|
int GetSamplesPerFrame(int frame_number, Fraction rate);
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
public:
|
|
|
|
|
/// Default constructor for FrameMapper class
|
2013-09-10 12:59:06 -05:00
|
|
|
FrameMapper(ReaderBase *reader, Framerate target, PulldownType pulldown);
|
2011-10-11 08:44:27 -05:00
|
|
|
|
2012-10-17 09:57:02 -05:00
|
|
|
/// Close the internal reader
|
|
|
|
|
void Close();
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
/// Get a frame based on the target frame rate and the new frame number of a frame
|
2012-10-17 09:57:02 -05:00
|
|
|
MappedFrame GetMappedFrame(int TargetFrameNumber) throw(OutOfBoundsFrame);
|
|
|
|
|
|
2013-09-08 16:40:57 -05:00
|
|
|
/// This method is required for all derived classes of ReaderBase, and return the
|
2012-10-17 09:57:02 -05:00
|
|
|
/// openshot::Frame object, which contains the image and audio information for that
|
|
|
|
|
/// frame of video.
|
|
|
|
|
///
|
|
|
|
|
/// @returns The requested frame of video
|
|
|
|
|
/// @param[in] number The frame number that is requested.
|
|
|
|
|
tr1::shared_ptr<Frame> GetFrame(int requested_frame) throw(ReaderClosed);
|
2011-10-11 08:44:27 -05:00
|
|
|
|
2012-10-14 02:36:05 -05:00
|
|
|
/// Get the target framerate
|
2012-10-15 17:45:20 -05:00
|
|
|
Framerate TargetFPS() { return target; };
|
2012-10-14 02:36:05 -05:00
|
|
|
|
|
|
|
|
/// Get the source framerate
|
2012-10-15 17:45:20 -05:00
|
|
|
Framerate SourceFPS() { return original; };
|
2012-10-14 02:36:05 -05:00
|
|
|
|
|
|
|
|
/// Set the target framerate
|
2012-10-15 17:45:20 -05:00
|
|
|
void TargetFPS(Framerate new_fps) { target = new_fps; };
|
2012-10-14 02:36:05 -05:00
|
|
|
|
|
|
|
|
/// Set the source framerate
|
2012-10-15 17:45:20 -05:00
|
|
|
void SourceFPS(Framerate new_fps) { original = new_fps; };
|
2012-10-14 02:36:05 -05:00
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
/**
|
2013-09-09 23:32:16 -05:00
|
|
|
* @brief Re-map time to slow down, speed up, or reverse a clip based on a Keyframe.
|
2011-10-11 08:44:27 -05:00
|
|
|
*
|
|
|
|
|
* This method re-maps the time of a clip, or in other words, changes the sequence and/or
|
|
|
|
|
* duration of frames in a clip. Because this method accepts a Keyframe, the time, sequence,
|
|
|
|
|
* and direction of frames can be based on LINEAR, BEZIER, or CONSTANT values.
|
|
|
|
|
*
|
|
|
|
|
* The X axis of the Keyframe represents the time (in frames) of this clip. If you make the
|
|
|
|
|
* X axis longer than the number of frames in the clip, if will slow down your clip. If the
|
|
|
|
|
* X axis is shorter than your clip, it will speed up your clip. The Y axis determines which
|
|
|
|
|
* original frame from the media file will be played. For example, if you clip has 100 frames,
|
|
|
|
|
* and your Keyframe goes from (1,1) to (100,100), the clip will playback in the original sequence,
|
|
|
|
|
* and at the original speed. If your Keyframe goes from (1,100) to (100,1), it will playback in the
|
|
|
|
|
* reverse direction, and at normal speed. If your Keyframe goes from (1,100) to (500,1),
|
|
|
|
|
* it will play in reverse at 1/5 the original speed.
|
|
|
|
|
*
|
|
|
|
|
* Please see the following <b>Example Code</b>:
|
|
|
|
|
* \code
|
|
|
|
|
* // Create a mapping between 24 fps and 24 fps (a mapping is required for time-remapping)
|
|
|
|
|
* FrameMapper mapping(100, Framerate(24, 1), Framerate(24, 1), PULLDOWN_NONE);
|
|
|
|
|
*
|
|
|
|
|
* // Print the mapping (before it's been time-mapped)
|
|
|
|
|
* mapping.PrintMapping();
|
|
|
|
|
* cout << "-----------------------" << endl;
|
|
|
|
|
*
|
|
|
|
|
* // Create a Keyframe to re-map time (forward, reverse, and then forward again)
|
|
|
|
|
* Keyframe kf;
|
|
|
|
|
* kf.AddPoint(Point(Coordinate(1, 1), LINEAR));
|
|
|
|
|
* kf.AddPoint(Point(Coordinate(40, 40), LINEAR));
|
|
|
|
|
* kf.AddPoint(Point(Coordinate(60, 20), LINEAR)); // Reverse for 20 frames
|
|
|
|
|
* kf.AddPoint(Point(Coordinate(100, 100), LINEAR)); // Play to end (in fast forward)
|
|
|
|
|
*
|
|
|
|
|
* // Use the Keyframe to remap the time of this clip
|
|
|
|
|
* mapping.MapTime(kf);
|
|
|
|
|
*
|
|
|
|
|
* // Print the mapping again (to see the time remapping)
|
|
|
|
|
* mapping.PrintMapping();
|
|
|
|
|
* \endcode
|
|
|
|
|
*/
|
|
|
|
|
void MapTime(Keyframe new_time) throw(OutOfBoundsFrame);
|
|
|
|
|
|
2012-10-17 09:57:02 -05:00
|
|
|
/// Open the internal reader
|
|
|
|
|
void Open() throw(InvalidFile);
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
/// Print all of the original frames and which new frames they map to
|
|
|
|
|
void PrintMapping();
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|