<commit_before>/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 <NAME>, Videolabs
 *
 * Authors: <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.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "VLCThumbnailer.h"

#include "Media.h"
#include "File.h"
#include "logging/Logger.h"
#include "MediaLibrary.h"
#include "utils/VLCInstance.h"
#include "utils/ModificationsNotifier.h"

#ifdef HAVE_JPEG
#include "imagecompressors/JpegCompressor.h"
#elif defined(HAVE_EVAS)
#include "imagecompressors/EvasCompressor.h"
#else
#error No image compressor available
#endif

namespace medialibrary
{

VLCThumbnailer::VLCThumbnailer()
    : m_instance( VLCInstance::get() )
    , m_thumbnailRequired( false )
    , m_width( 0 )
    , m_height( 0 )
    , m_prevSize( 0 )
{
#ifdef HAVE_JPEG
    m_compressor.reset( new JpegCompressor );
#elif defined(HAVE_EVAS)
    m_compressor.reset( new EvasCompressor );
#endif
}

bool VLCThumbnailer::initialize()
{
    return true;
}

File::ParserStep VLCThumbnailer::step() const
{
    return File::ParserStep::Thumbnailer;
}

parser::Task::Status VLCThumbnailer::run( parser::Task& task )
{
    auto media = task.media;
    auto file = task.file;

    if ( media->type() == IMedia::Type::AudioType )
    {
        // There's no point in generating a thumbnail for a non-video media.
        task.file->markStepCompleted( File::ParserStep::Thumbnailer );
        task.file->saveParserStep();
        return parser::Task::Status::Success;
    }

    LOG_INFO( "Generating ", file->mrl(), " thumbnail..." );

    if ( task.vlcMedia.isValid() == false )
    {
        auto fromType = file->mrl().find( "://" ) != std::string::npos ? VLC::Media::FromType::FromLocation :
                                                                          VLC::Media::FromType::FromPath;
        task.vlcMedia = VLC::Media( m_instance, file->mrl(), fromType );
    }

    task.vlcMedia.addOption( ":no-audio" );
    task.vlcMedia.addOption( ":no-osd" );
    task.vlcMedia.addOption( ":no-spu" );
    task.vlcMedia.addOption( ":input-fast-seek" );
    auto duration = task.vlcMedia.duration();
    if ( duration > 0 )
    {
        std::ostringstream ss;
        // Duration is in ms, start-time in seconds, and we're aiming at 1/4th of the media
        ss << ":start-time=" << duration / 4000;
        task.vlcMedia.addOption( ss.str() );
    }

    VLC::MediaPlayer mp( task.vlcMedia );

    setupVout( mp );

    auto res = startPlayback( task, mp );
    if ( res != parser::Task::Status::Success )
    {
        // If the media became an audio file, it's not an error
        if ( task.media->type() == Media::Type::AudioType )
        {
            task.file->markStepCompleted( File::ParserStep::Thumbnailer );
            task.file->saveParserStep();
            LOG_INFO( file->mrl(), " type has changed to Audio. Skipping thumbnail generation" );
            return parser::Task::Status::Success;
        }
        // Otherwise, we failed to start the playback and this is an error indeed
        LOG_WARN( "Failed to generate ", file->mrl(), " thumbnail: Can't start playback" );
        return res;
    }
<commit_msg>Thumbnailer: Disable HW decoding<commit_after>/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 <NAME>, Videolabs
 *
 * Authors: <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.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "VLCThumbnailer.h"

#include "Media.h"
#include "File.h"
#include "logging/Logger.h"
#include "MediaLibrary.h"
#include "utils/VLCInstance.h"
#include "utils/ModificationsNotifier.h"

#ifdef HAVE_JPEG
#include "imagecompressors/JpegCompressor.h"
#elif defined(HAVE_EVAS)
#include "imagecompressors/EvasCompressor.h"
#else
#error No image compressor available
#endif

namespace medialibrary
{

VLCThumbnailer::VLCThumbnailer()
    : m_instance( VLCInstance::get() )
    , m_thumbnailRequired( false )
    , m_width( 0 )
    , m_height( 0 )
    , m_prevSize( 0 )
{
#ifdef HAVE_JPEG
    m_compressor.reset( new JpegCompressor );
#elif defined(HAVE_EVAS)
    m_compressor.reset( new EvasCompressor );
#endif
}

bool VLCThumbnailer::initialize()
{
    return true;
}

File::ParserStep VLCThumbnailer::step() const
{
    return File::ParserStep::Thumbnailer;
}

parser::Task::Status VLCThumbnailer::run( parser::Task& task )
{
    auto media = task.media;
    auto file = task.file;

    if ( media->type() == IMedia::Type::AudioType )
    {
        // There's no point in generating a thumbnail for a non-video media.
        task.file->markStepCompleted( File::ParserStep::Thumbnailer );
        task.file->saveParserStep();
        return parser::Task::Status::Success;
    }

    LOG_INFO( "Generating ", file->mrl(), " thumbnail..." );

    if ( task.vlcMedia.isValid() == false )
    {
        auto fromType = file->mrl().find( "://" ) != std::string::npos ? VLC::Media::FromType::FromLocation :
                                                                          VLC::Media::FromType::FromPath;
        task.vlcMedia = VLC::Media( m_instance, file->mrl(), fromType );
    }

    task.vlcMedia.addOption( ":no-audio" );
    task.vlcMedia.addOption( ":no-osd" );
    task.vlcMedia.addOption( ":no-spu" );
    task.vlcMedia.addOption( ":input-fast-seek" );
    task.vlcMedia.addOption( ":avcodec-hw=none" );
    auto duration = task.vlcMedia.duration();
    if ( duration > 0 )
    {
        std::ostringstream ss;
        // Duration is in ms, start-time in seconds, and we're aiming at 1/4th of the media
        ss << ":start-time=" << duration / 4000;
        task.vlcMedia.addOption( ss.str() );
    }

    VLC::MediaPlayer mp( task.vlcMedia );

    setupVout( mp );

    auto res = startPlayback( task, mp );
    if ( res != parser::Task::Status::Success )
    {
        // If the media became an audio file, it's not an error
        if ( task.media->type() == Media::Type::AudioType )
        {
            task.file->markStepCompleted( File::ParserStep::Thumbnailer );
            task.file->saveParserStep();
            LOG_INFO( file->mrl(), " type has changed to Audio. Skipping thumbnail generation" );
            return parser::Task::Status::Success;
        }
        // Otherwise, we failed to start the playback and this is an error indeed
        LOG_WARN( "Failed to generate ", file->mrl(), " thumbnail: Can't start playback" );
        return res;
    }

{'fraction_non_alphanumeric': 0.1734644331148337, 'fraction_numerical': 0.0065549890750182084, 'mean_word_length': 3.2777777777777777, 'pattern_counts': {'":': 13, '<': 19, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '815307', 'n_tokens_mistral': 2491, 'n_tokens_neox': 2374, 'n_words': 691}