Ubuntu Pastebin

Paste from neo at Sat, 2 Jul 2016 15:15:56 +0000

Download as text
  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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
diff -ru ffmpegthumbs-16.04.2/cmake/FindFFmpeg.cmake ffmpegthumbs-git/cmake/FindFFmpeg.cmake
--- ffmpegthumbs-16.04.2/cmake/FindFFmpeg.cmake	2016-05-29 21:15:06.000000000 +0100
+++ ffmpegthumbs-git/cmake/FindFFmpeg.cmake	2016-07-02 12:08:56.000000000 +0100
@@ -99,6 +99,7 @@
 
   # Check for all possible component.
   find_component(AVCODEC  libavcodec  avcodec  libavcodec/avcodec.h)
+  find_component(AVFILTER libavfilter avfilter libavfilter/avfilter.h)
   find_component(AVFORMAT libavformat avformat libavformat/avformat.h)
   find_component(AVDEVICE libavdevice avdevice libavdevice/avdevice.h)
   find_component(AVUTIL   libavutil   avutil   libavutil/avutil.h)
diff -ru ffmpegthumbs-16.04.2/CMakeLists.txt ffmpegthumbs-git/CMakeLists.txt
--- ffmpegthumbs-16.04.2/CMakeLists.txt	2016-05-29 21:15:06.000000000 +0100
+++ ffmpegthumbs-git/CMakeLists.txt	2016-07-02 12:08:56.000000000 +0100
@@ -36,7 +36,7 @@
 
 add_library(ffmpegthumbs MODULE ${ffmpegthumbs_PART_SRCS})
 
-target_link_libraries(ffmpegthumbs Qt5::Gui KF5::KIOWidgets ${AVUTIL_LIBRARIES} ${AVFORMAT_LIBRARIES} ${AVCODEC_LIBRARIES} ${SWSCALE_LIBRARIES} )
+target_link_libraries(ffmpegthumbs Qt5::Gui KF5::KIOWidgets ${AVUTIL_LIBRARIES} ${AVFILTER_LIBRARIES} ${AVFORMAT_LIBRARIES} ${AVCODEC_LIBRARIES} ${SWSCALE_LIBRARIES} )
 
 install(TARGETS ffmpegthumbs DESTINATION ${PLUGIN_INSTALL_DIR})
 
diff -ru ffmpegthumbs-16.04.2/ffmpegthumbnailer/moviedecoder.cpp ffmpegthumbs-git/ffmpegthumbnailer/moviedecoder.cpp
--- ffmpegthumbs-16.04.2/ffmpegthumbnailer/moviedecoder.cpp	2016-05-29 21:15:06.000000000 +0100
+++ ffmpegthumbs-git/ffmpegthumbnailer/moviedecoder.cpp	2016-07-02 12:08:56.000000000 +0100
@@ -40,6 +40,10 @@
         , m_FormatContextWasGiven(pavContext != NULL)
         , m_AllowSeek(true)
         , m_initialized(false)
+        , m_bufferSinkContext(nullptr)
+        , m_bufferSourceContext(nullptr)
+        , m_filterGraph(nullptr)
+        , m_filterFrame(nullptr)
 {
     initialize(filename);
 }
@@ -51,6 +55,9 @@
 
 void MovieDecoder::initialize(const QString& filename)
 {
+    m_lastWidth = -1;
+    m_lastHeight = -1;
+    m_lastPixfmt = AV_PIX_FMT_NONE;
     av_register_all();
     avcodec_register_all();
 
@@ -67,7 +74,7 @@
     }
 
     initializeVideo();
-    m_pFrame = avcodec_alloc_frame();
+    m_pFrame = av_frame_alloc();
 
     if (m_pFrame) {
         m_initialized=true;
@@ -82,6 +89,7 @@
 
 void MovieDecoder::destroy()
 {
+    deleteFilterGraph();
     if (m_pVideoCodecContext) {
         avcodec_close(m_pVideoCodecContext);
         m_pVideoCodecContext = NULL;
@@ -93,13 +101,13 @@
     }
 
     if (m_pPacket) {
-        av_free_packet(m_pPacket);
+        av_packet_unref(m_pPacket);
         delete m_pPacket;
         m_pPacket = NULL;
     }
 
     if (m_pFrame) {
-        av_free(m_pFrame);
+        av_frame_free(&m_pFrame);
         m_pFrame = NULL;
     }
 
@@ -239,7 +247,7 @@
         return false;
     }
 
-    avcodec_get_frame_defaults(m_pFrame);
+    av_frame_unref(m_pFrame);
 
     int frameFinished = 0;
 
@@ -264,7 +272,7 @@
     int attempts = 0;
 
     if (m_pPacket) {
-        av_free_packet(m_pPacket);
+        av_packet_unref(m_pPacket);
         delete m_pPacket;
     }
 
@@ -275,7 +283,7 @@
         if (framesAvailable) {
             frameDecoded = m_pPacket->stream_index == m_VideoStream;
             if (!frameDecoded) {
-                av_free_packet(m_pPacket);
+                av_packet_unref(m_pPacket);
             }
         }
     }
@@ -283,15 +291,100 @@
     return frameDecoded;
 }
 
+void MovieDecoder::deleteFilterGraph()
+{
+    if (m_filterGraph) {
+        av_frame_free(&m_filterFrame);
+        avfilter_graph_free(&m_filterGraph);
+        m_filterGraph = nullptr;
+    }
+}
+
+bool MovieDecoder::initFilterGraph(enum AVPixelFormat pixfmt, int width, int height)
+{
+    AVFilterInOut *inputs = nullptr, *outputs = nullptr;
+
+    deleteFilterGraph();
+    m_filterGraph = avfilter_graph_alloc();
+
+    QByteArray arguments("buffer=");
+    arguments += "video_size=" + QByteArray::number(width) + "x" + QByteArray::number(height) + ":";
+    arguments += "pix_fmt=" + QByteArray::number(pixfmt) + ":";
+    arguments += "time_base=1/1:pixel_aspect=0/1[in];";
+    arguments += "[in]yadif[out];";
+    arguments += "[out]buffersink";
+
+    int ret = avfilter_graph_parse2(m_filterGraph, arguments.constData(), &inputs, &outputs);
+    if (ret < 0) {
+        qWarning() << "Unable to parse filter graph";
+        return false;
+    }
+
+    if(inputs || outputs)
+        return -1;
+
+    ret = avfilter_graph_config(m_filterGraph, nullptr);
+    if (ret < 0) {
+        qWarning() << "Unable to validate filter graph";
+        return false;
+    }
+
+    m_bufferSourceContext = avfilter_graph_get_filter(m_filterGraph, "Parsed_buffer_0");
+    m_bufferSinkContext = avfilter_graph_get_filter(m_filterGraph, "Parsed_buffersink_2");
+    if (!m_bufferSourceContext || !m_bufferSinkContext) {
+        qWarning() << "Unable to get source or sink";
+        return false;
+    }
+    m_filterFrame = av_frame_alloc();
+    m_lastWidth = width;
+    m_lastHeight = height;
+    m_lastPixfmt = pixfmt;
+
+    return true;
+}
+
+bool MovieDecoder::processFilterGraph(AVPicture *dst, const AVPicture *src,
+                                enum AVPixelFormat pixfmt, int width, int height)
+{
+    if (!m_filterGraph || width != m_lastWidth ||
+        height != m_lastHeight || pixfmt != m_lastPixfmt) {
+
+        if (!initFilterGraph(pixfmt, width, height)) {
+            return false;
+        }
+    }
+
+    memcpy(m_filterFrame->data, src->data, sizeof(src->data));
+    memcpy(m_filterFrame->linesize, src->linesize, sizeof(src->linesize));
+    m_filterFrame->width = width;
+    m_filterFrame->height = height;
+    m_filterFrame->format = pixfmt;
+
+    int ret = av_buffersrc_add_frame(m_bufferSourceContext, m_filterFrame);
+    if (ret < 0) {
+        return false;
+    }
+
+    ret = av_buffersink_get_frame(m_bufferSinkContext, m_filterFrame);
+    if (ret < 0) {
+        return false;
+    }
+
+    av_picture_copy(dst, (const AVPicture *) m_filterFrame, pixfmt, width, height);
+    av_frame_unref(m_filterFrame);
+
+    return true;
+}
+
 void MovieDecoder::getScaledVideoFrame(int scaledSize, bool maintainAspectRatio, VideoFrame& videoFrame)
 {
     if (m_pFrame->interlaced_frame) {
-        avpicture_deinterlace((AVPicture*) m_pFrame, (AVPicture*) m_pFrame, m_pVideoCodecContext->pix_fmt,
+        processFilterGraph((AVPicture*) m_pFrame, (AVPicture*) m_pFrame, m_pVideoCodecContext->pix_fmt,
                               m_pVideoCodecContext->width, m_pVideoCodecContext->height);
     }
 
     int scaledWidth, scaledHeight;
-    convertAndScaleFrame(PIX_FMT_RGB24, scaledSize, maintainAspectRatio, scaledWidth, scaledHeight);
+    convertAndScaleFrame(AV_PIX_FMT_RGB24, scaledSize, maintainAspectRatio, scaledWidth, scaledHeight);
 
     videoFrame.width = scaledWidth;
     videoFrame.height = scaledHeight;
@@ -302,7 +395,7 @@
     memcpy((&(videoFrame.frameData.front())), m_pFrame->data[0], videoFrame.lineSize * videoFrame.height);
 }
 
-void MovieDecoder::convertAndScaleFrame(PixelFormat format, int scaledSize, bool maintainAspectRatio, int& scaledWidth, int& scaledHeight)
+void MovieDecoder::convertAndScaleFrame(AVPixelFormat format, int scaledSize, bool maintainAspectRatio, int& scaledWidth, int& scaledHeight)
 {
     calculateDimensions(scaledSize, maintainAspectRatio, scaledWidth, scaledHeight);
     SwsContext* scaleContext = sws_getContext(m_pVideoCodecContext->width, m_pVideoCodecContext->height,
@@ -323,7 +416,7 @@
               convertedFrame->data, convertedFrame->linesize);
     sws_freeContext(scaleContext);
 
-    av_free(m_pFrame);
+    av_frame_free(&m_pFrame);
     av_free(m_pFrameBuffer);
 
     m_pFrame        = convertedFrame;
@@ -355,9 +448,9 @@
     }
 }
 
-void MovieDecoder::createAVFrame(AVFrame** avFrame, quint8** frameBuffer, int width, int height, PixelFormat format)
+void MovieDecoder::createAVFrame(AVFrame** avFrame, quint8** frameBuffer, int width, int height, AVPixelFormat format)
 {
-    *avFrame = avcodec_alloc_frame();
+    *avFrame = av_frame_alloc();
 
     int numBytes = avpicture_get_size(format, width, height);
     *frameBuffer = reinterpret_cast<quint8*>(av_malloc(numBytes));
diff -ru ffmpegthumbs-16.04.2/ffmpegthumbnailer/moviedecoder.h ffmpegthumbs-git/ffmpegthumbnailer/moviedecoder.h
--- ffmpegthumbs-16.04.2/ffmpegthumbnailer/moviedecoder.h	2016-05-29 21:15:06.000000000 +0100
+++ ffmpegthumbs-git/ffmpegthumbnailer/moviedecoder.h	2016-07-02 12:08:56.000000000 +0100
@@ -23,6 +23,9 @@
 extern "C" {
 #include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
+#include <libavfilter/avfilter.h>
+#include <libavfilter/buffersrc.h>
+#include <libavfilter/buffersink.h>
 }
 
 namespace ffmpegthumbnailer
@@ -52,10 +55,14 @@
 
     bool decodeVideoPacket();
     bool getVideoPacket();
-    void convertAndScaleFrame(PixelFormat format, int scaledSize, bool maintainAspectRatio, int& scaledWidth, int& scaledHeight);
-    void createAVFrame(AVFrame** avFrame, quint8** frameBuffer, int width, int height, PixelFormat format);
+    void convertAndScaleFrame(AVPixelFormat format, int scaledSize, bool maintainAspectRatio, int& scaledWidth, int& scaledHeight);
+    void createAVFrame(AVFrame** avFrame, quint8** frameBuffer, int width, int height, AVPixelFormat format);
     void calculateDimensions(int squareSize, bool maintainAspectRatio, int& destWidth, int& destHeight);
 
+    void deleteFilterGraph();
+    bool initFilterGraph(enum AVPixelFormat pixfmt, int width, int height);
+    bool processFilterGraph(AVPicture *dst, const AVPicture *src, enum AVPixelFormat pixfmt, int width, int height);
+
 private:
     int                     m_VideoStream;
     AVFormatContext*        m_pFormatContext;
@@ -68,6 +75,13 @@
     bool                    m_FormatContextWasGiven;
     bool                    m_AllowSeek;
     bool                    m_initialized;
+    AVFilterContext*        m_bufferSinkContext;
+    AVFilterContext*        m_bufferSourceContext;
+    AVFilterGraph*          m_filterGraph;
+    AVFrame*                m_filterFrame;
+    int                     m_lastWidth;
+    int                     m_lastHeight;
+    enum AVPixelFormat      m_lastPixfmt;
 };
 
 }
diff -ru ffmpegthumbs-16.04.2/tests/CMakeLists.txt ffmpegthumbs-git/tests/CMakeLists.txt
--- ffmpegthumbs-16.04.2/tests/CMakeLists.txt	2016-05-29 21:15:06.000000000 +0100
+++ ffmpegthumbs-git/tests/CMakeLists.txt	2016-07-02 12:08:56.000000000 +0100
@@ -19,7 +19,7 @@
 
 add_executable(ffmpegthumbtest ${ffmpegthumbtest_SRCS} )
 
-target_link_libraries(ffmpegthumbtest Qt5::Gui KF5::KIOWidgets ${AVUTIL_LIBRARIES} ${AVFORMAT_LIBRARIES} ${AVCODEC_LIBRARIES} ${SWSCALE_LIBRARIES})
+target_link_libraries(ffmpegthumbtest Qt5::Gui KF5::KIOWidgets ${AVUTIL_LIBRARIES} ${AVFILTER_LIBRARIES} ${AVFORMAT_LIBRARIES} ${AVCODEC_LIBRARIES} ${SWSCALE_LIBRARIES})
 
 
 
Download as text