Add some const& to uses of auto.
[dcpomatic.git] / src / lib / dcp_examiner.cc
index 536c9ec1ab81885a9c71f0ecfc332245a1845193..0f9ae9544812f414bbd7053369c999214a2de5df 100644 (file)
 
 
 #include "config.h"
+#include "constants.h"
 #include "dcp_content.h"
 #include "dcp_examiner.h"
 #include "dcpomatic_log.h"
 #include "exceptions.h"
 #include "image.h"
+#include "text_content.h"
 #include "util.h"
 #include <dcp/cpl.h>
 #include <dcp/dcp.h>
@@ -81,7 +83,7 @@ DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content, bool tolerant)
 
                for (auto cpl: cpls) {
                        int unsatisfied = 0;
-                       for (auto reel: cpl->reels()) {
+                       for (auto const& reel: cpl->reels()) {
                                if (reel->main_picture() && !reel->main_picture()->asset_ref().resolved()) {
                                        ++unsatisfied;
                                }
@@ -128,9 +130,9 @@ DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content, bool tolerant)
 
        LOG_GENERAL("Looking at %1 reels", selected_cpl->reels().size());
 
+       int reel_index = 0;
        for (auto reel: selected_cpl->reels()) {
                LOG_GENERAL("Reel %1", reel->id());
-               vector<shared_ptr<dcpomatic::Font>> reel_fonts;
 
                if (reel->main_picture()) {
                        if (!reel->main_picture()->asset_ref().resolved()) {
@@ -175,11 +177,13 @@ DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content, bool tolerant)
                        auto asset = reel->main_sound()->asset();
 
                        if (!_audio_channels) {
-                               _audio_channels = asset->channels ();
-                       } else if (_audio_channels.get() != asset->channels ()) {
+                               _audio_channels = asset->channels();
+                       } else if (_audio_channels.get() != asset->channels()) {
                                throw DCPError (_("Mismatched audio channel counts in DCP"));
                        }
 
+                       _active_audio_channels = std::max(_active_audio_channels.get_value_or(0), asset->active_channels());
+
                        if (!_audio_frame_rate) {
                                _audio_frame_rate = asset->sampling_rate ();
                        } else if (_audio_frame_rate.get() != asset->sampling_rate ()) {
@@ -203,8 +207,24 @@ DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content, bool tolerant)
                        _text_count[TextType::OPEN_SUBTITLE] = 1;
                        _open_subtitle_language = try_to_parse_language(reel->main_subtitle()->language());
 
-                       for (auto const& font: reel->main_subtitle()->asset()->font_data()) {
-                               reel_fonts.push_back(make_shared<dcpomatic::Font>(font.first, font.second));
+                       auto asset = reel->main_subtitle()->asset();
+                       for (auto const& font: asset->font_data()) {
+                               _fonts.push_back({reel_index, asset->id(), make_shared<dcpomatic::Font>(font.first, font.second)});
+                       }
+                       if (asset->font_data().empty()) {
+                               _fonts.push_back({reel_index, asset->id(), make_shared<dcpomatic::Font>("")});
+                       }
+               }
+
+               _text_count[TextType::CLOSED_CAPTION] = std::max(_text_count[TextType::CLOSED_CAPTION], static_cast<int>(reel->closed_captions().size()));
+               if (_dcp_text_tracks.size() < reel->closed_captions().size()) {
+                       /* We only want to add 1 DCPTextTrack to _dcp_text_tracks per closed caption.  I guess it's possible that different
+                        * reels have different numbers of tracks (though I don't think they should) so make sure that _dcp_text_tracks ends
+                        * up with the maximum.
+                        */
+                       _dcp_text_tracks.clear();
+                       for (auto ccap: reel->closed_captions()) {
+                               _dcp_text_tracks.push_back(DCPTextTrack(ccap->annotation_text().get_value_or(""), try_to_parse_language(ccap->language())));
                        }
                }
 
@@ -218,8 +238,13 @@ DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content, bool tolerant)
 
                        LOG_GENERAL("Closed caption %1 of reel %2 found", ccap->id(), reel->id());
 
-                       _text_count[TextType::CLOSED_CAPTION]++;
-                       _dcp_text_tracks.push_back(DCPTextTrack(ccap->annotation_text().get_value_or(""), try_to_parse_language(ccap->language())));
+                       auto asset = ccap->asset();
+                       for (auto const& font: asset->font_data()) {
+                               _fonts.push_back({reel_index, asset->id(), make_shared<dcpomatic::Font>(font.first, font.second)});
+                       }
+                       if (asset->font_data().empty()) {
+                               _fonts.push_back({reel_index, asset->id(), make_shared<dcpomatic::Font>("")});
+                       }
                }
 
                if (reel->main_markers ()) {
@@ -248,11 +273,7 @@ DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content, bool tolerant)
                        _reel_lengths.push_back(reel->atmos()->actual_duration());
                }
 
-               if (reel_fonts.empty()) {
-                       reel_fonts.push_back(make_shared<dcpomatic::Font>(""));
-               }
-
-               _fonts.push_back(reel_fonts);
+               ++reel_index;
        }
 
        _encrypted = selected_cpl->any_encrypted();
@@ -340,3 +361,21 @@ DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content, bool tolerant)
 
        _cpl = selected_cpl->id();
 }
+
+
+void
+DCPExaminer::add_fonts(shared_ptr<TextContent> content)
+{
+       for (auto const& font: _fonts) {
+               _font_id_allocator.add_font(font.reel_index, font.asset_id, font.font->id());
+       }
+
+       _font_id_allocator.allocate();
+
+       for (auto const& font: _fonts) {
+               auto font_copy = make_shared<dcpomatic::Font>(*font.font);
+               font_copy->set_id(_font_id_allocator.font_id(font.reel_index, font.asset_id, font.font->id()));
+               content->add_font(font_copy);
+       }
+}
+