Merge master.
[dcpomatic.git] / src / lib / subtitle.cc
1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
2
3 /*
4     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21
22 /** @file  src/subtitle.cc
23  *  @brief Representations of subtitles.
24  */
25
26 #include "subtitle.h"
27 #include "image.h"
28 #include "exceptions.h"
29
30 #include "i18n.h"
31
32 using namespace std;
33 using namespace boost;
34 using libdcp::Size;
35
36 /** Construct a TimedSubtitle.  This is a subtitle image, position,
37  *  and a range of time over which it should be shown.
38  *  @param sub AVSubtitle to read.
39  */
40 TimedSubtitle::TimedSubtitle (AVSubtitle const & sub)
41 {
42         assert (sub.num_rects > 0);
43         
44         /* Subtitle PTS in seconds (within the source, not taking into account any of the
45            source that we may have chopped off for the DCP)
46         */
47         double const packet_time = static_cast<double> (sub.pts) / AV_TIME_BASE;
48         
49         /* hence start time for this sub */
50         _from = (packet_time + (double (sub.start_display_time) / 1e3)) * TIME_HZ;
51         _to = (packet_time + (double (sub.end_display_time) / 1e3)) * TIME_HZ;
52
53         if (sub.num_rects > 1) {
54                 throw DecodeError (_("multi-part subtitles not yet supported"));
55         }
56
57         AVSubtitleRect const * rect = sub.rects[0];
58
59         if (rect->type != SUBTITLE_BITMAP) {
60                 throw DecodeError (_("non-bitmap subtitles not yet supported"));
61         }
62         
63         shared_ptr<Image> image (new SimpleImage (PIX_FMT_RGBA, libdcp::Size (rect->w, rect->h), true));
64
65         /* Start of the first line in the subtitle */
66         uint8_t* sub_p = rect->pict.data[0];
67         /* sub_p looks up into a RGB palette which is here */
68         uint32_t const * palette = (uint32_t *) rect->pict.data[1];
69         /* Start of the output data */
70         uint32_t* out_p = (uint32_t *) image->data()[0];
71         
72         for (int y = 0; y < rect->h; ++y) {
73                 uint8_t* sub_line_p = sub_p;
74                 uint32_t* out_line_p = out_p;
75                 for (int x = 0; x < rect->w; ++x) {
76                         *out_line_p++ = palette[*sub_line_p++];
77                 }
78                 sub_p += rect->pict.linesize[0];
79                 out_p += image->stride()[0] / sizeof (uint32_t);
80         }
81
82         _subtitle.reset (new Subtitle (Position (rect->x, rect->y), image));
83 }       
84
85 /** @param t Time from the start of the source */
86 bool
87 TimedSubtitle::displayed_at (Time t) const
88 {
89         return t >= _from && t <= _to;
90 }
91
92 /** Construct a subtitle, which is an image and a position.
93  *  @param p Position within the (uncropped) source frame.
94  *  @param i Image of the subtitle (should be RGBA).
95  */
96 Subtitle::Subtitle (Position p, shared_ptr<Image> i)
97         : _position (p)
98         , _image (i)
99 {
100
101 }
102
103 /** Given the area of a subtitle, work out the area it should
104  *  take up when its video frame is scaled up, and it is optionally
105  *  itself scaled and offset.
106  *  @param target_x_scale the x scaling of the video frame that the subtitle is in.
107  *  @param target_y_scale the y scaling of the video frame that the subtitle is in.
108  *  @param sub_area The area of the subtitle within the original source.
109  *  @param subtitle_offset y offset to apply to the subtitle position (+ve is down)
110  *  in the coordinate space of the source.
111  *  @param subtitle_scale scaling factor to apply to the subtitle image.
112  */
113 dvdomatic::Rect
114 subtitle_transformed_area (
115         float target_x_scale, float target_y_scale,
116         dvdomatic::Rect sub_area, int subtitle_offset, float subtitle_scale
117         )
118 {
119         dvdomatic::Rect tx;
120
121         sub_area.y += subtitle_offset;
122
123         /* We will scale the subtitle by the same amount as the video frame, and also by the additional
124            subtitle_scale
125         */
126         tx.width = sub_area.width * target_x_scale * subtitle_scale;
127         tx.height = sub_area.height * target_y_scale * subtitle_scale;
128
129         /* Then we need a corrective translation, consisting of two parts:
130          *
131          * 1.  that which is the result of the scaling of the subtitle by target_x_scale and target_y_scale; this will be
132          *     sub_area.x * target_x_scale and sub_area.y * target_y_scale.
133          *
134          * 2.  that to shift the origin of the scale by subtitle_scale to the centre of the subtitle; this will be
135          *     (width_before_subtitle_scale * (1 - subtitle_scale) / 2) and
136          *     (height_before_subtitle_scale * (1 - subtitle_scale) / 2).
137          *
138          * Combining these two translations gives these expressions.
139          */
140         
141         tx.x = rint (target_x_scale * (sub_area.x + (sub_area.width * (1 - subtitle_scale) / 2)));
142         tx.y = rint (target_y_scale * (sub_area.y + (sub_area.height * (1 - subtitle_scale) / 2)));
143
144         return tx;
145 }
146
147 /** @return area that this subtitle takes up, in the original uncropped source's coordinate space */
148 dvdomatic::Rect
149 Subtitle::area () const
150 {
151         return dvdomatic::Rect (_position.x, _position.y, _image->size().width, _image->size().height);
152 }