2 Copyright (C) 2019 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic 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.
11 DCP-o-matic 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.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "examine_ffmpeg_subtitles_job.h"
22 #include "ffmpeg_content.h"
23 #include "ffmpeg_subtitle_stream.h"
24 #include "text_content.h"
27 #include <libavcodec/avcodec.h>
28 #include <libavformat/avformat.h>
36 using boost::shared_ptr;
38 ExamineFFmpegSubtitlesJob::ExamineFFmpegSubtitlesJob (shared_ptr<const Film> film, shared_ptr<FFmpegContent> c)
46 ExamineFFmpegSubtitlesJob::~ExamineFFmpegSubtitlesJob ()
52 ExamineFFmpegSubtitlesJob::name () const
54 return _("Examining subtitles");
58 ExamineFFmpegSubtitlesJob::json_name () const
60 return N_("examine_subtitles");
64 ExamineFFmpegSubtitlesJob::run ()
66 int64_t const len = _file_group.length ();
68 int r = av_read_frame (_format_context, &_packet);
74 set_progress (float(_format_context->pb->pos) / len);
76 set_progress_unknown ();
79 if (_content->subtitle_stream() && _content->subtitle_stream()->uses_index(_format_context, _packet.stream_index) && _content->only_text()->use()) {
82 if (avcodec_decode_subtitle2(subtitle_codec_context(), &sub, &got_subtitle, &_packet) >= 0 && got_subtitle) {
83 for (unsigned int i = 0; i < sub.num_rects; ++i) {
84 AVSubtitleRect const * rect = sub.rects[i];
85 if (rect->type == SUBTITLE_BITMAP) {
86 #ifdef DCPOMATIC_HAVE_AVSUBTITLERECT_PICT
87 /* sub_p looks up into a BGRA palette which is here
88 (i.e. first byte B, second G, third R, fourth A)
90 uint8_t const * palette = rect->pict.data[1];
92 /* sub_p looks up into a BGRA palette which is here
93 (i.e. first byte B, second G, third R, fourth A)
95 uint8_t const * palette = rect->data[1];
97 for (int j = 0; j < rect->nb_colors; ++j) {
98 RGBA c (palette[2], palette[1], palette[0], palette[3]);
99 _content->subtitle_stream()->set_colour (c, c);
107 av_packet_unref (&_packet);
111 set_state (FINISHED_OK);