diff options
| author | Carl Hetherington <cth@carlh.net> | 2012-10-14 13:02:57 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2012-10-14 13:02:57 +0100 |
| commit | 48c8f23660184486efbb34df9d677108b0eab204 (patch) | |
| tree | ac1a4ea6aa23ed159bbcf8d10d1000739b5ad252 /src | |
| parent | b1fc0de953b1c2fce8a31b267b63dfcaf67830c7 (diff) | |
Try moving subtitle adjustment for crop into the decoder.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/decoder.cc | 9 | ||||
| -rw-r--r-- | src/lib/subtitle.cc | 39 | ||||
| -rw-r--r-- | src/lib/subtitle.h | 16 | ||||
| -rw-r--r-- | src/wx/film_viewer.cc | 34 |
4 files changed, 43 insertions, 55 deletions
diff --git a/src/lib/decoder.cc b/src/lib/decoder.cc index e4b892ea4..c4759a872 100644 --- a/src/lib/decoder.cc +++ b/src/lib/decoder.cc @@ -48,6 +48,7 @@ extern "C" { #include "filter.h" #include "delay_line.h" #include "ffmpeg_compatibility.h" +#include "subtitle.h" using namespace std; using namespace boost; @@ -303,6 +304,14 @@ Decoder::process_video (AVFrame* frame, shared_ptr<Subtitle> sub) image->make_black (); } + if (sub && _opt->apply_crop) { + list<shared_ptr<SubtitleImage> > im = sub->images (); + for (list<shared_ptr<SubtitleImage> >::iterator i = im.begin(); i != im.end(); ++i) { + Position p = (*i)->position (); + (*i)->set_position (Position (p.x - _fs->crop.left, p.y - _fs->crop.top)); + } + } + TIMING ("Decoder emits %1", _video_frame); Video (image, _video_frame, sub); ++_video_frame; diff --git a/src/lib/subtitle.cc b/src/lib/subtitle.cc index 040a057fc..0ea5a72d5 100644 --- a/src/lib/subtitle.cc +++ b/src/lib/subtitle.cc @@ -70,46 +70,27 @@ SubtitleImage::SubtitleImage (AVSubtitleRect const * rect) } } -SubtitleTransform -subtitle_transform ( - int target_base_width, int target_base_height, +Rectangle +transformed_subtitle_area ( float target_x_scale, float target_y_scale, - Position sub_pos, int sub_width, int sub_height, + Rectangle sub_area, shared_ptr<FilmState> fs ) { - SubtitleTransform tx; + Rectangle tx; - /* The area of the original subtitle, in the coordinate space of the source video frame */ - Rectangle sub_area (sub_pos.x, sub_pos.y + fs->subtitle_offset, sub_width, sub_height); - - /* The cropped area of the source video frame, in the same coordinate space */ - Rectangle cropped_target_area ( - fs->crop.left, - fs->crop.top, - target_base_width - (fs->crop.left + fs->crop.right), - target_base_height - (fs->crop.top + fs->crop.bottom) - ); - - /* Hence the area of the cropped subtitle, in the same coordinate space */ - Rectangle cropped_sub_area = sub_area.intersection (cropped_target_area); - - /* The crop that should be applied to the subtitle, in its coordinate space */ - tx.crop.x = cropped_sub_area.x - sub_area.x; - tx.crop.y = cropped_sub_area.y - sub_area.y; - tx.crop.w = cropped_sub_area.w; - tx.crop.h = cropped_sub_area.h; + sub_area.y += fs->subtitle_offset; /* We will scale the subtitle by the same amount as the video frame, and also by the additional subtitle_scale */ - tx.transformed.w = cropped_sub_area.w * target_x_scale * fs->subtitle_scale; - tx.transformed.h = cropped_sub_area.h * target_y_scale * fs->subtitle_scale; + tx.w = sub_area.w * target_x_scale * fs->subtitle_scale; + tx.h = sub_area.h * target_y_scale * fs->subtitle_scale; /* Then we need a corrective translation, consisting of two parts: * * 1. that which is the result of the scaling of the subtitle by target_x_scale and target_y_scale; this will be - * (sub_area.x - fs->crop_left) * target_x_scale and (sub_area.y - fs->crop_top) * target_y_scale. + * sub_area.x * target_x_scale and sub_area.y * target_y_scale. * * 2. that to shift the origin of the scale by fs->subtitle_scale to the centre of the subtitle; this will be * (width_before_subtitle_scale * (1 - fs->subtitle_scale) / 2) and @@ -118,8 +99,8 @@ subtitle_transform ( * Combining these two translations gives these expressions. */ - tx.transformed.x = target_x_scale * ((sub_area.x - fs->crop.left) + (cropped_sub_area.w * (1 - fs->subtitle_scale) / 2)); - tx.transformed.y = target_y_scale * ((sub_area.y - fs->crop.top) + (cropped_sub_area.h * (1 - fs->subtitle_scale) / 2)); + tx.x = target_x_scale * (sub_area.x + (sub_area.w * (1 - fs->subtitle_scale) / 2)); + tx.y = target_y_scale * (sub_area.y + (sub_area.h * (1 - fs->subtitle_scale) / 2)); return tx; } diff --git a/src/lib/subtitle.h b/src/lib/subtitle.h index fcb6bc70c..0b82320a1 100644 --- a/src/lib/subtitle.h +++ b/src/lib/subtitle.h @@ -45,17 +45,9 @@ private: std::list<boost::shared_ptr<SubtitleImage> > _images; }; -struct SubtitleTransform -{ -public: - Rectangle crop; - Rectangle transformed; -}; - -extern SubtitleTransform subtitle_transform ( - int target_base_width, int target_base_height, +extern Rectangle transformed_subtitle_area ( float target_x_scale, float target_y_scale, - Position sub_pos, int sub_width, int sub_height, + Rectangle sub_area, boost::shared_ptr<FilmState> fs ); @@ -64,6 +56,10 @@ class SubtitleImage public: SubtitleImage (AVSubtitleRect const *); + void set_position (Position p) { + _position = p; + } + Position position () const { return _position; } diff --git a/src/wx/film_viewer.cc b/src/wx/film_viewer.cc index 725ba57de..bfeb44cb9 100644 --- a/src/wx/film_viewer.cc +++ b/src/wx/film_viewer.cc @@ -50,11 +50,11 @@ public: /** Handle a paint event */ void paint_event (wxPaintEvent& ev) { - if (!_film) { + if (!_film || _film->num_thumbs() == 0) { wxPaintDC dc (this); return; } - + if (_frame_rebuild_needed) { _image.reset (new wxImage (std_to_wx (_film->thumb_file (_index)))); @@ -82,7 +82,7 @@ public: if (_film->with_subtitles ()) { for (list<SubtitleView>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) { - dc.DrawBitmap (*i->bitmap, i->transformed_position.x, i->transformed_position.y, true); + dc.DrawBitmap (*i->bitmap, i->transformed_area.x, i->transformed_area.y, true); } } } @@ -178,16 +178,14 @@ private: for (list<SubtitleView>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) { - SubtitleTransform tx = subtitle_transform ( - _image->GetWidth(), _image->GetHeight(), - x_scale, y_scale, - i->base_position, i->base_image.GetWidth(), i->base_image.GetHeight(), - _film->state_copy() + i->transformed_area = transformed_subtitle_area ( + x_scale, y_scale, i->base_area, _film->state_copy() ); - i->transformed_image = i->base_image.GetSubImage (wxRect (tx.crop.x, tx.crop.y, tx.crop.w, tx.crop.h)); - i->transformed_image.Rescale (tx.transformed.w, tx.transformed.h, wxIMAGE_QUALITY_HIGH); - i->transformed_position = Position (tx.transformed.x, tx.transformed.y); + i->transformed_image = i->base_image; + i->transformed_image.Rescale (i->transformed_area.w, i->transformed_area.h, wxIMAGE_QUALITY_HIGH); + i->transformed_area.x -= _film->crop().left; + i->transformed_area.y -= _film->crop().top; i->bitmap.reset (new wxBitmap (i->transformed_image)); } } @@ -204,12 +202,16 @@ private: struct SubtitleView { SubtitleView (Position p, wxString const & i) - : base_position (p) - , base_image (i) - {} + : base_image (i) + { + base_area.x = p.x; + base_area.y = p.y; + base_area.w = base_image.GetWidth (); + base_area.h = base_image.GetHeight (); + } - Position base_position; - Position transformed_position; + Rectangle base_area; + Rectangle transformed_area; wxImage base_image; wxImage transformed_image; shared_ptr<wxBitmap> bitmap; |
