Do content digest in the examine contents job (fixes #6).
[dcpomatic.git] / src / lib / examine_content_job.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 /** @file  src/examine_content_job.cc
21  *  @brief A class to run through content at high speed to find its length.
22  */
23
24 #include <boost/filesystem.hpp>
25 #include "examine_content_job.h"
26 #include "options.h"
27 #include "decoder_factory.h"
28 #include "decoder.h"
29 #include "transcoder.h"
30 #include "log.h"
31 #include "film.h"
32 #include "video_decoder.h"
33
34 using std::string;
35 using std::vector;
36 using std::pair;
37 using boost::shared_ptr;
38
39 ExamineContentJob::ExamineContentJob (shared_ptr<Film> f, shared_ptr<Job> req)
40         : Job (f, req)
41 {
42
43 }
44
45 ExamineContentJob::~ExamineContentJob ()
46 {
47 }
48
49 string
50 ExamineContentJob::name () const
51 {
52         if (_film->name().empty ()) {
53                 return "Examine content";
54         }
55         
56         return String::compose ("Examine content of %1", _film->name());
57 }
58
59 void
60 ExamineContentJob::run ()
61 {
62         descend (0.5);
63         _film->set_content_digest (md5_digest (_film->content_path ()));
64         ascend ();
65
66         descend (0.5);
67
68         /* Set the film's length to either
69            a) a length judged by running through the content or
70            b) the length from a decoder's header.
71         */
72         if (!_film->trust_content_header()) {
73                 /* Decode the content to get an accurate length */
74                 
75                 /* We don't want to use any existing length here, as progress
76                    will be messed up.
77                 */
78                 _film->unset_length ();
79                 _film->set_crop (Crop ());
80                 
81                 shared_ptr<DecodeOptions> o (new DecodeOptions);
82                 o->decode_audio = false;
83                 
84                 Decoders decoders = decoder_factory (_film, o, this);
85                 
86                 set_progress_unknown ();
87                 while (!decoders.video->pass()) {
88                         /* keep going */
89                 }
90                 
91                 _film->set_length (decoders.video->video_frame());
92                 
93                 _film->log()->log (String::compose ("Video length examined as %1 frames", _film->length().get()));
94                 
95         } else {
96
97                 /* Get a quick decoder to get the content's length from its header */
98                 
99                 shared_ptr<DecodeOptions> o (new DecodeOptions);
100                 Decoders d = decoder_factory (_film, o, 0);
101                 _film->set_length (d.video->length());
102         
103                 _film->log()->log (String::compose ("Video length obtained from header as %1 frames", _film->length().get()));
104         }
105
106         ascend ();
107         set_progress (1);
108         set_state (FINISHED_OK);
109 }