File: videothumbnailerc.cpp

package info (click to toggle)
ffmpegthumbnailer 2.2.2%2Bgit20220218%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 864 kB
  • sloc: cpp: 11,403; ansic: 76; sh: 37; makefile: 13
file content (195 lines) | stat: -rw-r--r-- 6,394 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//    Copyright (C) 2010 Dirk Vanden Boer <dirk.vdb@gmail.com>
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 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 General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#include "videothumbnailerc.h"
#include "videothumbnailer.h"
#include "filmstripfilter.h"

#include <vector>
#include <stdexcept>

using namespace ffmpegthumbnailer;

struct thumbnailer_data
{
    VideoThumbnailer thumbnailer;
    FilmStripFilter filter;
    thumbnailer_log_callback log_cb = nullptr;
};

static void trace_message(video_thumbnailer* thumbnailer, ThumbnailerLogLevel lvl, const char* message)
{
    if (thumbnailer->tdata->log_cb)
    {
        thumbnailer->tdata->log_cb(lvl, message);
    }
}

extern "C" video_thumbnailer* video_thumbnailer_create(void)
{
    auto thumbnailer = new video_thumbnailer_struct();

    thumbnailer->thumbnail_size             = 128;
    thumbnailer->seek_percentage            = 10;
    thumbnailer->seek_time                  = nullptr;
    thumbnailer->overlay_film_strip         = 0;
    thumbnailer->workaround_bugs            = 0;
    thumbnailer->thumbnail_image_quality    = 8;
    thumbnailer->thumbnail_image_type       = Png;
    thumbnailer->maintain_aspect_ratio      = 1;
    thumbnailer->prefer_embedded_metadata   = 0;
    thumbnailer->av_format_context          = nullptr;
    thumbnailer->tdata                      = new thumbnailer_data();

    return thumbnailer;
}

extern "C" void video_thumbnailer_destroy(video_thumbnailer* thumbnailer)
{
    delete thumbnailer->tdata;
    delete thumbnailer;
}

extern "C" image_data* video_thumbnailer_create_image_data(void)
{
    auto data               = new image_data();

    data->image_data_ptr    = nullptr;
    data->image_data_size   = 0;
    data->image_data_width  = 0;
    data->image_data_height = 0;
    data->internal_data     = new std::vector<uint8_t>();

    return data;
}

extern "C" void video_thumbnailer_destroy_image_data(image_data* data)
{
    data->image_data_ptr    = nullptr;
    data->image_data_size   = 0;
    data->image_data_width  = 0;
    data->image_data_height = 0;

    auto dataVector = reinterpret_cast<std::vector<uint8_t>*>(data->internal_data);
    delete dataVector;
    data->internal_data     = nullptr;

    delete data;
}

static void setProperties(video_thumbnailer* thumbnailer)
{
    auto& videoThumbnailer = thumbnailer->tdata->thumbnailer;

    if (thumbnailer->thumbnail_size >= 0)
    {
        videoThumbnailer.setThumbnailSize(thumbnailer->thumbnail_size);
    }

    videoThumbnailer.setWorkAroundIssues(thumbnailer->workaround_bugs != 0);
    videoThumbnailer.setImageQuality(thumbnailer->thumbnail_image_quality);
    videoThumbnailer.setMaintainAspectRatio(thumbnailer->maintain_aspect_ratio != 0);
    videoThumbnailer.setPreferEmbeddedMetadata(thumbnailer->prefer_embedded_metadata != 0);

    if (thumbnailer->overlay_film_strip)
    {
        videoThumbnailer.removeFilter(&thumbnailer->tdata->filter);
        videoThumbnailer.addFilter(&thumbnailer->tdata->filter);
    }

    if (thumbnailer->seek_time != nullptr)
    {
        videoThumbnailer.setSeekTime(thumbnailer->seek_time);
    }
    else
    {
        videoThumbnailer.setSeekPercentage(thumbnailer->seek_percentage);
    }
}

extern "C" int video_thumbnailer_generate_thumbnail_to_buffer(video_thumbnailer* thumbnailer, const char* movie_filename, image_data* generated_image_data)
{
    try
    {
        auto dataVector         = reinterpret_cast<std::vector<uint8_t>*>(generated_image_data->internal_data);
        auto& videoThumbnailer  = thumbnailer->tdata->thumbnailer;
        setProperties(thumbnailer);
        auto info = videoThumbnailer.generateThumbnail(movie_filename, thumbnailer->thumbnail_image_type, *dataVector, thumbnailer->av_format_context);
        generated_image_data->image_data_ptr = &dataVector->front();
        generated_image_data->image_data_size = static_cast<int>(dataVector->size());
        generated_image_data->image_data_width = info.width;
        generated_image_data->image_data_height = info.height;
        generated_image_data->image_data_source = info.source;
    }
    catch (const std::exception& e)
    {
        trace_message(thumbnailer, ThumbnailerLogLevelError, e.what());
        return -1;
    }

    return 0;
}

extern "C" int video_thumbnailer_generate_thumbnail_to_file(video_thumbnailer* thumbnailer, const char* movie_filename, const char* output_fileName)
{
    try
    {
        auto& videoThumbnailer = thumbnailer->tdata->thumbnailer;
        setProperties(thumbnailer);
        videoThumbnailer.generateThumbnail(movie_filename, thumbnailer->thumbnail_image_type, output_fileName, thumbnailer->av_format_context);
    }
    catch (const std::exception& e)
    {
        trace_message(thumbnailer, ThumbnailerLogLevelError, e.what());
        return -1;
    }

    return 0;
}

extern "C" void video_thumbnailer_set_log_callback(video_thumbnailer* thumbnailer, thumbnailer_log_callback cb)
{
    thumbnailer->tdata->log_cb = cb;
    auto& videoThumbnailer = thumbnailer->tdata->thumbnailer;

    if (!cb)
    {
        videoThumbnailer.setLogCallback(nullptr);
    }
    else
    {
        videoThumbnailer.setLogCallback([=] (ThumbnailerLogLevel lvl, const std::string& msg) {
            cb(lvl, msg.c_str());
        });
    }
}

extern "C" int video_thumbnailer_set_size(video_thumbnailer* thumbnailer, int width, int height)
{
    auto& videoThumbnailer = thumbnailer->tdata->thumbnailer;

    try
    {
        thumbnailer->thumbnail_size = -1;
        videoThumbnailer.setThumbnailSize(width, height);
        return 0;
    }
    catch (const std::exception& e)
    {
        trace_message(thumbnailer, ThumbnailerLogLevelError, e.what());
        return -1;
    }
}