<commit_before>
/*
 *  SubtitleStream.cpp
 *  sfeMovie project
 *
 *  Copyright (C) 2010-2014 <NAME>
 *  <EMAIL>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

extern "C"
{
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
}

#include <sfeMovie/Movie.hpp>
#include "SubtitleStream.hpp"
#include "Log.hpp"

#include <iostream>
#include <cassert>
#include <stdint.h>

namespace sfe
{
    const int RGBASize = 4;
    
    SubtitleStream::SubtitleStream(AVFormatContext*& formatCtx, AVStream*& stream, DataSource& dataSource, std::shared_ptr<Timer> timer, Delegate& delegate) :
        Stream(formatCtx, stream, dataSource, timer), m_delegate(delegate), m_library(nullptr), m_renderer(nullptr), m_track(nullptr)
    {
        const AVCodecDescriptor* desc = av_codec_get_codec_descriptor(m_stream->codec);
        CHECK(desc != NULL, "Could not get the codec descriptor!");
        
        if((desc->props & AV_CODEC_PROP_BITMAP_SUB)==0)
        {
			m_library  = ass_library_init();
            ass_set_message_cb(m_library, ass_log,nullptr);

			m_renderer = ass_renderer_init(m_library);
            m_track    = ass_new_track(m_library);

            ass_set_frame_size(m_renderer, m_stream->codec->width, m_stream->codec->height);
            ass_set_fonts(m_renderer, NULL, NULL, 1, NULL , 1);
            ass_set_font_scale(m_renderer, 5.0f);

            ass_process_codec_private(m_track, reinterpret_cast<char*>(m_stream->codec->subtitle_header), m_stream->codec->subtitle_header_size);
		}
    }
    
    /** Default destructor
     */
    SubtitleStream::~SubtitleStream()
    {
        if (m_track)
        {
            ass_free_track(m_track);
            m_track = nullptr;
        }

		if(m_renderer)
		{
			ass_renderer_done(m_renderer);
			m_renderer = nullptr;
		}
		
		if(m_library)
		{
			ass_library_done(m_library);
			m_library = nullptr;
		}
			
    }
    
    MediaType SubtitleStream::getStreamKind() const
    {
        return Subtitle;
    }
    
    void SubtitleStream::update()
    {
        //only get new subtitles if we are running low
        if (m_status == Playing && hasPackets())
        {
            if (!onGetData())
                setStatus(Stopped);
        }
        
        if (m_pendingSubtitles.size() > 0)
        {
            //activate subtitle
            if (m_pendingSubtitles.front()->start < m_timer->getOffset())
            {
                std::shared_ptr<SubtitleData> iter = m_pendingSubtitles.front();

                auto time = m_timer->getOffset().asMilliseconds();
                //this is the case for ass subtitles
                if (iter->sprites.size() < 1)
                {
                    int change = 0;
                    ASS_Image* img = ass_render_frame(m_renderer, m_track, m_timer->getOffset().asMilliseconds(), &change);
                    if (change)
                    {
                        while (img)
                        {
                            iter->sprites.push_back(sf::Sprite());
                            iter->textures.push_back(sf::Texture());
                            iter->positions.push_back(sf::Vector2i(img->dst_x, img->dst_y));

                            sf::Sprite& sprite = iter->sprites.back();
                            sf::Texture& texture = iter->textures.back();

                            texture.create(img->w, img->h);
                            texture.update(img->bitmap);
                            texture.setSmooth(true);

                            sprite.setTexture(texture);

                            img = img->next;
                        }
                    }
                }

                m_delegate.didUpdateSubtitle(*this, iter->sprites, iter->positions);
                m_visibleSubtitles.push_back(iter);
                m_pendingSubtitles.pop_front();
            }
        }
        
        
        if (m_visibleSubtitles.size()>0)
        {
            //remove subtitle
            if (m_visibleSubtitles.front()->end < m_timer->getOffset())
            {
                std::shared_ptr<SubtitleData> subtitle = m_visibleSubtitles.front();
                m_visibleSubtitles.pop_front();
                
                if (m_visibleSubtitles.size() == 0)
                {
                    m_delegate.didWipeOutSubtitles(*this);
                }
            }
        }
    }<commit_msg>Set default font and changed rendering code<commit_after>
/*
 *  SubtitleStream.cpp
 *  sfeMovie project
 *
 *  Copyright (C) 2010-2014 <NAME>
 *  <EMAIL>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

extern "C"
{
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
}

#include <sfeMovie/Movie.hpp>
#include "SubtitleStream.hpp"
#include "Log.hpp"

#include <iostream>
#include <cassert>
#include <stdint.h>


#define _r(c) ((c) >> 24)
#define _g(c) (((c) >> 16) & 0xFF)
#define _b(c) (((c) >> 8) & 0xFF)
#define _a(c) (255 - ((c) & 0xFF))

namespace sfe
{
    const int RGBASize = 4;
    
    SubtitleStream::SubtitleStream(AVFormatContext*& formatCtx, AVStream*& stream, DataSource& dataSource, std::shared_ptr<Timer> timer, Delegate& delegate) :
        Stream(formatCtx, stream, dataSource, timer), m_delegate(delegate), m_library(nullptr), m_renderer(nullptr), m_track(nullptr)
    {
        const AVCodecDescriptor* desc = av_codec_get_codec_descriptor(m_stream->codec);
        CHECK(desc != NULL, "Could not get the codec descriptor!");
        
        if((desc->props & AV_CODEC_PROP_BITMAP_SUB)==0)
        {
			m_library  = ass_library_init();
            ass_set_message_cb(m_library, ass_log,nullptr);

			m_renderer = ass_renderer_init(m_library);
            m_track    = ass_new_track(m_library);

            ass_set_frame_size(m_renderer, m_stream->codec->width, m_stream->codec->height);
            ass_set_fonts(m_renderer, NULL, "Sans", 1, NULL, 1);
            ass_set_font_scale(m_renderer, 10.0f);

            ass_process_codec_private(m_track, reinterpret_cast<char*>(m_stream->codec->subtitle_header), m_stream->codec->subtitle_header_size);
		}
    }
    
    /** Default destructor
     */
    SubtitleStream::~SubtitleStream()
    {
        if (m_track)
        {
            ass_free_track(m_track);
            m_track = nullptr;
        }

		if(m_renderer)
		{
			ass_renderer_done(m_renderer);
			m_renderer = nullptr;
		}
		
		if(m_library)
		{
			ass_library_done(m_library);
			m_library = nullptr;
		}
			
    }
    
    MediaType SubtitleStream::getStreamKind() const
    {
        return Subtitle;
    }
    
    void SubtitleStream::update()
    {
        //only get new subtitles if we are running low
        if (m_status == Playing && hasPackets())
        {
            if (!onGetData())
                setStatus(Stopped);
        }
        
        if (m_pendingSubtitles.size() > 0)
        {
            //activate subtitle
            if (m_pendingSubtitles.front()->start < m_timer->getOffset())
            {
                std::shared_ptr<SubtitleData> iter = m_pendingSubtitles.front();

                auto time = m_timer->getOffset().asMilliseconds();
                //this is the case for ass subtitles
                if (iter->sprites.size() < 1)
                {
                    int change = 0;
                    ASS_Image* img = ass_render_frame(m_renderer, m_track, m_timer->getOffset().asMilliseconds(), &change);
                   
                    if (change)
                    {                     

                        while (img)
                        {
                            if (!img->w || !img->h) continue;

                            uint8_t     r = img->color >> 24,
                                        g = img->color >> 16 & 255,
                                        b = img->color >> 8 & 255,
                                        a = 255 - (img->color & 255);

                            sf::Image buffer;
                            buffer.create(img->w, img->h);

                            iter->positions.push_back(sf::Vector2i(img->dst_x, img->dst_y));

                            for (int y = 0; y < img->h; ++y)
                            {
                                const unsigned char *map = img->bitmap + y * img->stride;

                                for (int x = 0; x < img->w; ++x)
                                {
                                    uint8_t alpha = (unsigned)a * *map++;
                                    buffer.setPixel(x, y, sf::Color(r,g,b,alpha));
                                }
                            }

                            img = img->next;

                            iter->sprites.push_back(sf::Sprite());
                            iter->textures.push_back(sf::Texture());


                            sf::Sprite& sprite = iter->sprites.back();
                            sf::Texture& texture = iter->textures.back();

                            texture.create(buffer.getSize().x, buffer.getSize().y);
                            texture.update(buffer);
                            texture.setSmooth(true);

                            sprite.setTexture(texture);
                        }                       
                    }
                }

                m_delegate.didUpdateSubtitle(*this, iter->sprites, iter->positions);
                m_visibleSubtitles.push_back(iter);
                m_pendingSubtitles.pop_front();
            }
        }
        
        
        if (m_visibleSubtitles.size()>0)
        {
            //remove subtitle
            if (m_visibleSubtitles.front()->end < m_timer->getOffset())
            {
                std::shared_ptr<SubtitleData> subtitle = m_visibleSubtitles.front();
                m_visibleSubtitles.pop_front();
                
                if (m_visibleSubtitles.size() == 0)
                {
                    m_delegate.didWipeOutSubtitles(*this);
                }
            }
        }
    }
{'fraction_non_alphanumeric': 0.10570175438596491, 'fraction_numerical': 0.008684210526315789, 'mean_word_length': 1.6600559962669155, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 112, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3270323', 'n_tokens_mistral': 3413, 'n_tokens_neox': 3219, 'n_words': 709}