Update slider position when playing.
[dcpomatic.git] / src / wx / ffmpeg_player.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <wx/wx.h>
21 #include <wx/tglbtn.h>
22 #include <iostream>
23 #include <stdint.h>
24 extern "C" {
25 #include <libavcodec/avcodec.h> 
26 #include <libavformat/avformat.h>
27 #include <libswscale/swscale.h>
28 }
29 #include "lib/exceptions.h"
30 #include "wx/ffmpeg_player.h"
31
32 using namespace std;
33
34 FFmpegPlayer::FFmpegPlayer (wxWindow* parent)
35         : _format_context (0)
36         , _video_stream (-1)
37         , _frame (0)
38         , _video_codec_context (0)
39         , _video_codec (0)
40         , _scale_context (0)
41         , _frame_valid (false)
42         , _last_frame_in_seconds (0)
43         , _panel (new wxPanel (parent))
44         , _slider (new wxSlider (parent, wxID_ANY, 0, 0, 4096))
45         , _play_button (new wxToggleButton (parent, wxID_ANY, wxT ("Play")))
46         , _panel_width (0)
47         , _panel_height (0)
48         , _full_width (0)
49         , _full_height (0)
50         , _top_crop_in_source (0)
51         , _bottom_crop_in_source (0)
52         , _left_crop_in_source (0)
53         , _right_crop_in_source (0)
54         , _ratio (1.85)
55 {
56         _rgb[0] = 0;
57
58         avcodec_register_all ();
59         av_register_all ();
60         
61         _frame = avcodec_alloc_frame ();
62         if (!_frame) {
63                 throw DecodeError ("could not allocate frame");
64         }
65         
66         _panel->Bind (wxEVT_PAINT, &FFmpegPlayer::paint_panel, this);
67         _panel->Bind (wxEVT_SIZE, &FFmpegPlayer::panel_sized, this);
68         _slider->Bind (wxEVT_SCROLL_THUMBTRACK, &FFmpegPlayer::slider_moved, this);
69         _slider->Bind (wxEVT_SCROLL_PAGEUP, &FFmpegPlayer::slider_moved, this);
70         _slider->Bind (wxEVT_SCROLL_PAGEDOWN, &FFmpegPlayer::slider_moved, this);
71         _play_button->Bind (wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, &FFmpegPlayer::play_clicked, this);
72         _timer.Bind (wxEVT_TIMER, &FFmpegPlayer::timer, this);
73 }
74
75 FFmpegPlayer::~FFmpegPlayer ()
76 {
77         delete[] _rgb[0];
78         
79         if (_scale_context) {
80                 sws_freeContext (_scale_context);
81         }
82         
83         if (_video_codec_context) {
84                 avcodec_close (_video_codec_context);
85         }
86         
87         av_free (_frame);
88
89         if (_format_context) {
90                 avformat_close_input (&_format_context);
91         }
92 }
93
94 void
95 FFmpegPlayer::timer (wxTimerEvent& ev)
96 {
97         if (!can_display ()) {
98                 return;
99         }
100         
101         _panel->Refresh ();
102         _panel->Update ();
103         decode_frame ();
104         convert_frame ();
105
106         double const video_length_in_seconds = static_cast<double>(_format_context->duration) / AV_TIME_BASE;
107         if (_last_frame_in_seconds) {
108                 int const new_slider_position = 4096 * _last_frame_in_seconds / video_length_in_seconds;
109                 if (new_slider_position != _slider->GetValue()) {
110                         _slider->SetValue (new_slider_position);
111                 }
112         }
113 }
114
115 void
116 FFmpegPlayer::update_panel ()
117 {
118         _panel->Refresh ();
119         _panel->Update ();
120 }
121
122 void
123 FFmpegPlayer::decode_frame ()
124 {
125         assert (_format_context);
126         
127         while (1) {
128                 int r = av_read_frame (_format_context, &_packet);
129                 if (r < 0) {
130                         return;
131                 }
132                 
133                 avcodec_get_frame_defaults (_frame);
134                 if (_packet.stream_index == _video_stream) {
135                         int frame_finished;
136                         int const r = avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet);
137                         if (r >= 0 && frame_finished) {
138                                 _frame_valid = true;
139                                 _last_frame_in_seconds = av_q2d (_format_context->streams[_packet.stream_index]->time_base)
140                                         * av_frame_get_best_effort_timestamp(_frame);
141                                 av_free_packet (&_packet);
142                                 return;
143                         }
144                 }
145                 
146                 av_free_packet (&_packet);
147         }
148 }
149
150 void
151 FFmpegPlayer::convert_frame ()
152 {
153         if (!_scale_context || !_rgb[0] || !_frame_valid) {
154                 return;
155         }
156         
157         sws_scale (
158                 _scale_context,
159                 _frame->data, _frame->linesize,
160                 0, _video_codec_context->height,
161                 _rgb, _rgb_stride
162                 );
163         
164         uint8_t* in = _rgb[0];
165         uint8_t* out = _rgb[0];
166         
167         in += top_crop_in_view() * _full_width * 3;
168         for (int y = 0; y < cropped_height_in_view(); ++y) {
169                 /* in is the start of the appropriate full-width line */
170                 memmove (out, in + left_crop_in_view() * 3, cropped_width_in_view() * 3);
171                 in += _full_width * 3;
172                 out += cropped_width_in_view() * 3;
173         }
174 }
175
176 void
177 FFmpegPlayer::paint_panel (wxPaintEvent& ev)
178 {
179         wxPaintDC dc (_panel);
180         
181         wxImage i (cropped_width_in_view(), cropped_height_in_view(), _rgb[0], true);
182         wxBitmap b (i);
183         dc.DrawBitmap (b, 0, 0);
184 }
185
186 void
187 FFmpegPlayer::slider_moved (wxCommandEvent& ev)
188 {
189         if (!can_display ()) {
190                 return;
191         }
192
193         double const video_length_in_seconds = static_cast<double>(_format_context->duration) / AV_TIME_BASE;
194         double const new_position_in_seconds = video_length_in_seconds * _slider->GetValue() / 4096;
195         int64_t const t = static_cast<int64_t>(new_position_in_seconds) / av_q2d (_format_context->streams[_video_stream]->time_base);
196         av_seek_frame (_format_context, _video_stream, t, 0);
197         avcodec_flush_buffers (_video_codec_context);
198
199         decode_frame ();
200         convert_frame ();
201         update_panel ();
202 }
203
204 float
205 FFmpegPlayer::frames_per_second () const
206 {
207         assert (_format_context);
208         
209         AVStream* s = _format_context->streams[_video_stream];
210         
211         if (s->avg_frame_rate.num && s->avg_frame_rate.den) {
212                 return av_q2d (s->avg_frame_rate);
213         }
214         
215         return av_q2d (s->r_frame_rate);
216 }
217
218 void
219 FFmpegPlayer::allocate_buffer_and_scaler ()
220 {
221         if (!_format_context || !_panel_width || !_panel_height) {
222                 return;
223         }
224
225         float const panel_ratio = static_cast<float> (_panel_width) / _panel_height;
226         
227         int new_width;
228         int new_height;
229         if (panel_ratio < _ratio) {
230                 /* panel is less widescreen than the target ratio; clamp width */
231                 new_width = _panel_width;
232                 new_height = new_width / _ratio;
233         } else {
234                 /* panel is more widescreen than the target ratio; clamp height */
235                 new_height = _panel_height;
236                 new_width = new_height * _ratio;
237         }
238
239         if (new_width == 0 || new_height == 0) {
240              return;
241         }
242         
243         _full_height = new_height + ((_top_crop_in_source + _bottom_crop_in_source) * height_source_to_view_scaling());
244         _full_width = new_width + ((_left_crop_in_source + _right_crop_in_source) * width_source_to_view_scaling());
245         
246         delete[] _rgb[0];
247         
248         _rgb[0] = new uint8_t[_full_width * _full_height * 3];
249         memset (_rgb[0], 0, _full_width * _full_height * 3);
250         _rgb_stride[0] = _full_width * 3;
251         
252         if (_scale_context) {
253                 sws_freeContext (_scale_context);
254         }
255         
256         _scale_context = sws_getContext (
257                 _video_codec_context->width, _video_codec_context->height, _video_codec_context->pix_fmt,
258                 _full_width, _full_height, PIX_FMT_RGB24,
259                 SWS_BICUBIC, 0, 0, 0
260                 );
261
262         if (!_scale_context) {
263                 throw DecodeError ("could not create scaler");
264         }
265 }
266
267 float
268 FFmpegPlayer::width_source_to_view_scaling () const
269 {
270         return static_cast<float> (_full_width) / _video_codec_context->width;
271 }
272
273 float
274 FFmpegPlayer::height_source_to_view_scaling () const
275 {
276         return static_cast<float> (_full_height) / _video_codec_context->height;
277 }
278
279 int
280 FFmpegPlayer::cropped_width_in_view () const
281 {
282         return _full_width - ((_left_crop_in_source + _right_crop_in_source) * width_source_to_view_scaling());
283 }
284
285 int
286 FFmpegPlayer::cropped_height_in_view () const
287 {
288         return _full_height - ((_top_crop_in_source + _bottom_crop_in_source) * height_source_to_view_scaling());
289 }
290
291 int
292 FFmpegPlayer::left_crop_in_view () const
293 {
294         return _left_crop_in_source * width_source_to_view_scaling();
295 }
296
297 int
298 FFmpegPlayer::top_crop_in_view () const
299 {
300         return _top_crop_in_source * height_source_to_view_scaling();
301 }
302
303 void
304 FFmpegPlayer::panel_sized (wxSizeEvent& ev)
305 {
306         _panel_width = ev.GetSize().GetWidth();
307         _panel_height = ev.GetSize().GetHeight();
308         allocate_buffer_and_scaler ();
309
310         convert_frame ();
311         update_panel ();
312 }
313
314 void
315 FFmpegPlayer::set_file (string f)
316 {
317         if (_video_codec_context) {
318                 avcodec_close (_video_codec_context);
319         }
320
321         if (_format_context) {
322                 avformat_close_input (&_format_context);
323         }
324         
325         if (avformat_open_input (&_format_context, f.c_str(), 0, 0) < 0) {
326                 throw OpenFileError (f);
327         }
328         
329         if (avformat_find_stream_info (_format_context, 0) < 0) {
330                 throw DecodeError ("could not find stream information");
331         }
332         
333         for (uint32_t i = 0; i < _format_context->nb_streams; ++i) {
334                 AVStream* s = _format_context->streams[i];
335                 if (s->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
336                         _video_stream = i;
337                 } else if (s->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
338                         /* XXX */
339                 }
340         }
341         
342         if (_video_stream < 0) {
343                 throw DecodeError ("could not find video stream");
344         }
345         
346         _video_codec_context = _format_context->streams[_video_stream]->codec;
347         _video_codec = avcodec_find_decoder (_video_codec_context->codec_id);
348         
349         if (_video_codec == 0) {
350                 throw DecodeError ("could not find video stream");
351         }
352         
353         if (avcodec_open2 (_video_codec_context, _video_codec, 0) < 0) {
354                 throw DecodeError ("could not open video decoder");
355         }
356
357         allocate_buffer_and_scaler ();
358         check_play_state ();
359         update_panel ();
360 }
361
362 void
363 FFmpegPlayer::set_top_crop (int t)
364 {
365         _top_crop_in_source = t;
366
367         allocate_buffer_and_scaler ();
368         convert_frame ();
369         update_panel ();
370 }
371
372 void
373 FFmpegPlayer::set_bottom_crop (int b)
374 {
375         _bottom_crop_in_source = b;
376
377         allocate_buffer_and_scaler ();
378         convert_frame ();
379         update_panel ();
380 }
381
382 void
383 FFmpegPlayer::set_left_crop (int l)
384 {
385         _left_crop_in_source = l;
386
387         allocate_buffer_and_scaler ();
388         convert_frame ();
389         update_panel ();
390 }
391
392 void
393 FFmpegPlayer::set_right_crop (int r)
394 {
395         _right_crop_in_source = r;
396
397         allocate_buffer_and_scaler ();
398         convert_frame ();
399         update_panel ();
400 }
401
402 void
403 FFmpegPlayer::set_ratio (float r)
404 {
405         _ratio = r;
406
407         allocate_buffer_and_scaler ();
408         convert_frame ();
409         update_panel ();
410 }
411
412 void
413 FFmpegPlayer::play_clicked (wxCommandEvent &)
414 {
415         check_play_state ();
416 }
417
418 void
419 FFmpegPlayer::check_play_state ()
420 {
421         if (_play_button->GetValue()) {
422                 _timer.Start (1000 / frames_per_second());
423         } else {
424                 _timer.Stop ();
425         }
426 }
427
428 bool
429 FFmpegPlayer::can_display () const
430 {
431         return (_format_context && _scale_context);
432 }