]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/util/util.cpp
2.1.0: -frame_transform: Split frame_transform into image and audio-transforms.
[casparcg] / modules / ffmpeg / producer / util / util.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../../stdafx.h"\r
23 \r
24 #include "util.h"\r
25 \r
26 #include "flv.h"\r
27 \r
28 #include "../tbb_avcodec.h"\r
29 #include "../../ffmpeg_error.h"\r
30 \r
31 #include <tbb/concurrent_unordered_map.h>\r
32 #include <tbb/concurrent_queue.h>\r
33 \r
34 #include <core/frame/frame_transform.h>\r
35 #include <core/frame/frame_factory.h>\r
36 #include <core/producer/frame_producer.h>\r
37 #include <core/frame/write_frame.h>\r
38 \r
39 #include <common/except.h>\r
40 \r
41 #include <tbb/parallel_for.h>\r
42 \r
43 #include <common/assert.h>\r
44 #include <boost/filesystem.hpp>\r
45 #include <boost/lexical_cast.hpp>\r
46 \r
47 #include <asmlib.h>\r
48 \r
49 #if defined(_MSC_VER)\r
50 #pragma warning (push)\r
51 #pragma warning (disable : 4244)\r
52 #endif\r
53 extern "C" \r
54 {\r
55         #include <libswscale/swscale.h>\r
56         #include <libavcodec/avcodec.h>\r
57         #include <libavformat/avformat.h>\r
58 }\r
59 #if defined(_MSC_VER)\r
60 #pragma warning (pop)\r
61 #endif\r
62 \r
63 namespace caspar { namespace ffmpeg {\r
64                 \r
65 std::shared_ptr<core::audio_buffer> flush_audio()\r
66 {\r
67         static std::shared_ptr<core::audio_buffer> audio(new core::audio_buffer());\r
68         return audio;\r
69 }\r
70 \r
71 std::shared_ptr<core::audio_buffer> empty_audio()\r
72 {\r
73         static std::shared_ptr<core::audio_buffer> audio(new core::audio_buffer());\r
74         return audio;\r
75 }\r
76 \r
77 std::shared_ptr<AVFrame>                        flush_video()\r
78 {\r
79         static std::shared_ptr<AVFrame> video(avcodec_alloc_frame(), av_free);\r
80         return video;\r
81 }\r
82 \r
83 std::shared_ptr<AVFrame>                        empty_video()\r
84 {\r
85         static std::shared_ptr<AVFrame> video(avcodec_alloc_frame(), av_free);\r
86         return video;\r
87 }\r
88 \r
89 core::field_mode get_mode(const AVFrame& frame)\r
90 {\r
91         if(!frame.interlaced_frame)\r
92                 return core::field_mode::progressive;\r
93 \r
94         return frame.top_field_first ? core::field_mode::upper : core::field_mode::lower;\r
95 }\r
96 \r
97 core::pixel_format get_pixel_format(PixelFormat pix_fmt)\r
98 {\r
99         switch(pix_fmt)\r
100         {\r
101         case CASPAR_PIX_FMT_LUMA:       return core::pixel_format::luma;\r
102         case PIX_FMT_GRAY8:                     return core::pixel_format::gray;\r
103         case PIX_FMT_BGRA:                      return core::pixel_format::bgra;\r
104         case PIX_FMT_ARGB:                      return core::pixel_format::argb;\r
105         case PIX_FMT_RGBA:                      return core::pixel_format::rgba;\r
106         case PIX_FMT_ABGR:                      return core::pixel_format::abgr;\r
107         case PIX_FMT_YUV444P:           return core::pixel_format::ycbcr;\r
108         case PIX_FMT_YUV422P:           return core::pixel_format::ycbcr;\r
109         case PIX_FMT_YUV420P:           return core::pixel_format::ycbcr;\r
110         case PIX_FMT_YUV411P:           return core::pixel_format::ycbcr;\r
111         case PIX_FMT_YUV410P:           return core::pixel_format::ycbcr;\r
112         case PIX_FMT_YUVA420P:          return core::pixel_format::ycbcra;\r
113         default:                                        return core::pixel_format::invalid;\r
114         }\r
115 }\r
116 \r
117 core::pixel_format_desc get_pixel_format_desc(PixelFormat pix_fmt, int width, int height)\r
118 {\r
119         // Get linesizes\r
120         AVPicture dummy_pict;   \r
121         avpicture_fill(&dummy_pict, nullptr, pix_fmt == CASPAR_PIX_FMT_LUMA ? PIX_FMT_GRAY8 : pix_fmt, width, height);\r
122 \r
123         core::pixel_format_desc desc = get_pixel_format(pix_fmt);\r
124                 \r
125         switch(desc.format.value())\r
126         {\r
127         case core::pixel_format::gray:\r
128         case core::pixel_format::luma:\r
129                 {\r
130                         desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));                                               \r
131                         return desc;\r
132                 }\r
133         case core::pixel_format::bgra:\r
134         case core::pixel_format::argb:\r
135         case core::pixel_format::rgba:\r
136         case core::pixel_format::abgr:\r
137                 {\r
138                         desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 4));                                             \r
139                         return desc;\r
140                 }\r
141         case core::pixel_format::ycbcr:\r
142         case core::pixel_format::ycbcra:\r
143                 {               \r
144                         // Find chroma height\r
145                         int size2 = static_cast<int>(dummy_pict.data[2] - dummy_pict.data[1]);\r
146                         int h2 = size2/dummy_pict.linesize[1];                  \r
147 \r
148                         desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));\r
149                         desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[1], h2, 1));\r
150                         desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[2], h2, 1));\r
151 \r
152                         if(desc.format == core::pixel_format::ycbcra)                                           \r
153                                 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[3], height, 1));       \r
154                         return desc;\r
155                 }               \r
156         default:                \r
157                 desc.format = core::pixel_format::invalid;\r
158                 return desc;\r
159         }\r
160 }\r
161 \r
162 int make_alpha_format(int format)\r
163 {\r
164         switch(get_pixel_format(static_cast<PixelFormat>(format)).value())\r
165         {\r
166         case core::pixel_format::ycbcr:\r
167         case core::pixel_format::ycbcra:\r
168                 return CASPAR_PIX_FMT_LUMA;\r
169         default:\r
170                 return format;\r
171         }\r
172 }\r
173 \r
174 spl::shared_ptr<core::write_frame> make_write_frame(const void* tag, const spl::shared_ptr<AVFrame>& decoded_frame, const spl::shared_ptr<core::frame_factory>& frame_factory, int flags)\r
175 {                       \r
176         static tbb::concurrent_unordered_map<int, tbb::concurrent_queue<std::shared_ptr<SwsContext>>> sws_contexts_;\r
177         \r
178         if(decoded_frame->width < 1 || decoded_frame->height < 1)\r
179                 return frame_factory->create_frame(tag, core::pixel_format_desc(core::pixel_format::invalid));\r
180 \r
181         const auto width  = decoded_frame->width;\r
182         const auto height = decoded_frame->height;\r
183         auto desc                 = get_pixel_format_desc(static_cast<PixelFormat>(decoded_frame->format), width, height);\r
184         \r
185         if(flags & core::frame_producer::flags::alpha_only)\r
186                 desc = get_pixel_format_desc(static_cast<PixelFormat>(make_alpha_format(decoded_frame->format)), width, height);\r
187 \r
188         std::shared_ptr<core::write_frame> write;\r
189 \r
190         if(desc.format == core::pixel_format::invalid)\r
191         {\r
192                 auto pix_fmt = static_cast<PixelFormat>(decoded_frame->format);\r
193                 auto target_pix_fmt = PIX_FMT_BGRA;\r
194 \r
195                 if(pix_fmt == PIX_FMT_UYVY422)\r
196                         target_pix_fmt = PIX_FMT_YUV422P;\r
197                 else if(pix_fmt == PIX_FMT_YUYV422)\r
198                         target_pix_fmt = PIX_FMT_YUV422P;\r
199                 else if(pix_fmt == PIX_FMT_UYYVYY411)\r
200                         target_pix_fmt = PIX_FMT_YUV411P;\r
201                 else if(pix_fmt == PIX_FMT_YUV420P10)\r
202                         target_pix_fmt = PIX_FMT_YUV420P;\r
203                 else if(pix_fmt == PIX_FMT_YUV422P10)\r
204                         target_pix_fmt = PIX_FMT_YUV422P;\r
205                 else if(pix_fmt == PIX_FMT_YUV444P10)\r
206                         target_pix_fmt = PIX_FMT_YUV444P;\r
207                 \r
208                 auto target_desc = get_pixel_format_desc(target_pix_fmt, width, height);\r
209 \r
210                 write = frame_factory->create_frame(tag, target_desc);\r
211 \r
212                 std::shared_ptr<SwsContext> sws_context;\r
213 \r
214                 //CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";\r
215 \r
216                 int key = ((width << 22) & 0xFFC00000) | ((height << 6) & 0x003FC000) | ((pix_fmt << 7) & 0x00007F00) | ((target_pix_fmt << 0) & 0x0000007F);\r
217                         \r
218                 auto& pool = sws_contexts_[key];\r
219                                                 \r
220                 if(!pool.try_pop(sws_context))\r
221                 {\r
222                         double param;\r
223                         sws_context.reset(sws_getContext(width, height, pix_fmt, width, height, target_pix_fmt, SWS_BILINEAR, nullptr, nullptr, &param), sws_freeContext);\r
224                 }\r
225                         \r
226                 if(!sws_context)\r
227                 {\r
228                         BOOST_THROW_EXCEPTION(operation_failed() << msg_info("Could not create software scaling context.") << \r
229                                                                         boost::errinfo_api_function("sws_getContext"));\r
230                 }       \r
231                 \r
232                 spl::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);      \r
233                 avcodec_get_frame_defaults(av_frame.get());                     \r
234                 if(target_pix_fmt == PIX_FMT_BGRA)\r
235                 {\r
236                         auto size = avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), write->image_data(0).begin(), PIX_FMT_BGRA, width, height);\r
237                         CASPAR_VERIFY(size == write->image_data(0).size()); \r
238                 }\r
239                 else\r
240                 {\r
241                         av_frame->width  = width;\r
242                         av_frame->height = height;\r
243                         for(int n = 0; n < target_desc.planes.size(); ++n)\r
244                         {\r
245                                 av_frame->data[n]               = write->image_data(n).begin();\r
246                                 av_frame->linesize[n]   = target_desc.planes[n].linesize;\r
247                         }\r
248                 }\r
249 \r
250                 sws_scale(sws_context.get(), decoded_frame->data, decoded_frame->linesize, 0, height, av_frame->data, av_frame->linesize);      \r
251                 pool.push(sws_context); \r
252         }\r
253         else\r
254         {\r
255                 write = frame_factory->create_frame(tag, desc);\r
256 \r
257                 for(int n = 0; n < static_cast<int>(desc.planes.size()); ++n)\r
258                 {\r
259                         auto plane            = desc.planes[n];\r
260                         auto result           = write->image_data(n).begin();\r
261                         auto decoded          = decoded_frame->data[n];\r
262                         auto decoded_linesize = decoded_frame->linesize[n];\r
263                         \r
264                         CASPAR_ASSERT(decoded);\r
265                         CASPAR_ASSERT(write->image_data(n).begin());\r
266 \r
267                         // Copy line by line since ffmpeg sometimes pads each line.\r
268                         tbb::affinity_partitioner ap;\r
269                         tbb::parallel_for(tbb::blocked_range<int>(0, desc.planes[n].height), [&](const tbb::blocked_range<int>& r)\r
270                         {\r
271                                 for(int y = r.begin(); y != r.end(); ++y)\r
272                                         A_memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);\r
273                         }, ap);\r
274                 }\r
275         }\r
276 \r
277         if(decoded_frame->height == 480) // NTSC DV\r
278         {\r
279                 write->frame_transform().image_transform.fill_translation[1] += 2.0/static_cast<double>(frame_factory->video_format_desc().height);\r
280                 write->frame_transform().image_transform.fill_scale[1] = 1.0 - 6.0*1.0/static_cast<double>(frame_factory->video_format_desc().height);\r
281         }\r
282         \r
283         // Fix field-order if needed\r
284         if(get_mode(*decoded_frame) == core::field_mode::lower && frame_factory->video_format_desc().field_mode == core::field_mode::upper)\r
285                 write->frame_transform().image_transform.fill_translation[1] += 1.0/static_cast<double>(frame_factory->video_format_desc().height);\r
286         else if(get_mode(*decoded_frame) == core::field_mode::upper && frame_factory->video_format_desc().field_mode == core::field_mode::lower)\r
287                 write->frame_transform().image_transform.fill_translation[1] -= 1.0/static_cast<double>(frame_factory->video_format_desc().height);\r
288 \r
289         return spl::make_shared_ptr(write);\r
290 }\r
291 \r
292 spl::shared_ptr<AVFrame> make_av_frame(caspar::core::data_frame& frame)\r
293 {\r
294         std::array<void*, 4> data = {};\r
295         for(int n = 0; n < frame.get_pixel_format_desc().planes.size(); ++n)\r
296                 data[n] = frame.image_data(n).begin();\r
297 \r
298         return make_av_frame(data, frame.get_pixel_format_desc());\r
299 }\r
300 \r
301 spl::shared_ptr<AVFrame> make_av_frame(std::array<void*, 4> data, const core::pixel_format_desc& pix_desc)\r
302 {\r
303         spl::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);      \r
304         avcodec_get_frame_defaults(av_frame.get());\r
305         \r
306         auto planes              = pix_desc.planes;\r
307         auto format              = pix_desc.format.value();\r
308 \r
309         av_frame->width  = planes[0].width;\r
310         av_frame->height = planes[0].height;\r
311         for(int n = 0; n < planes.size(); ++n)  \r
312         {\r
313                 av_frame->data[n]         = reinterpret_cast<uint8_t*>(data[n]);\r
314                 av_frame->linesize[n] = planes[n].linesize;     \r
315         }\r
316         switch(format)\r
317         {\r
318         case core::pixel_format::rgba:\r
319                 av_frame->format = PIX_FMT_RGBA; \r
320                 break;\r
321         case core::pixel_format::argb:\r
322                 av_frame->format = PIX_FMT_ARGB; \r
323                 break;\r
324         case core::pixel_format::bgra:\r
325                 av_frame->format = PIX_FMT_BGRA; \r
326                 break;\r
327         case core::pixel_format::abgr:\r
328                 av_frame->format = PIX_FMT_ABGR; \r
329                 break;\r
330         case core::pixel_format::gray:\r
331                 av_frame->format = PIX_FMT_GRAY8; \r
332                 break;\r
333         case core::pixel_format::ycbcr:\r
334         {\r
335                 int y_w = planes[0].width;\r
336                 int y_h = planes[0].height;\r
337                 int c_w = planes[1].width;\r
338                 int c_h = planes[1].height;\r
339 \r
340                 if(c_h == y_h && c_w == y_w)\r
341                         av_frame->format = PIX_FMT_YUV444P;\r
342                 else if(c_h == y_h && c_w*2 == y_w)\r
343                         av_frame->format = PIX_FMT_YUV422P;\r
344                 else if(c_h == y_h && c_w*4 == y_w)\r
345                         av_frame->format = PIX_FMT_YUV411P;\r
346                 else if(c_h*2 == y_h && c_w*2 == y_w)\r
347                         av_frame->format = PIX_FMT_YUV420P;\r
348                 else if(c_h*2 == y_h && c_w*4 == y_w)\r
349                         av_frame->format = PIX_FMT_YUV410P;\r
350 \r
351                 break;\r
352         }\r
353         case core::pixel_format::ycbcra:\r
354                 av_frame->format = PIX_FMT_YUVA420P;\r
355                 break;\r
356         }\r
357         return av_frame;\r
358 }\r
359 \r
360 bool is_sane_fps(AVRational time_base)\r
361 {\r
362         double fps = static_cast<double>(time_base.den) / static_cast<double>(time_base.num);\r
363         return fps > 20.0 && fps < 65.0;\r
364 }\r
365 \r
366 AVRational fix_time_base(AVRational time_base)\r
367 {\r
368         if(time_base.num == 1)\r
369                 time_base.num = static_cast<int>(std::pow(10.0, static_cast<int>(std::log10(static_cast<float>(time_base.den)))-1));    \r
370                         \r
371         if(!is_sane_fps(time_base))\r
372         {\r
373                 auto tmp = time_base;\r
374                 tmp.den /= 2;\r
375                 if(is_sane_fps(tmp))\r
376                         time_base = tmp;\r
377         }\r
378 \r
379         return time_base;\r
380 }\r
381 \r
382 double read_fps(AVFormatContext& context, double fail_value)\r
383 {                                               \r
384         auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);\r
385         auto audio_index = av_find_best_stream(&context, AVMEDIA_TYPE_AUDIO, -1, -1, 0, 0);\r
386         \r
387         if(video_index > -1)\r
388         {\r
389                 const auto video_context = context.streams[video_index]->codec;\r
390                 const auto video_stream  = context.streams[video_index];\r
391                                                 \r
392                 AVRational time_base = video_context->time_base;\r
393 \r
394                 if(boost::filesystem::path(context.filename).extension().string() == ".flv")\r
395                 {\r
396                         try\r
397                         {\r
398                                 auto meta = read_flv_meta_info(context.filename);\r
399                                 return boost::lexical_cast<double>(meta["framerate"]);\r
400                         }\r
401                         catch(...)\r
402                         {\r
403                                 return 0.0;\r
404                         }\r
405                 }\r
406                 else\r
407                 {\r
408                         time_base.num *= video_context->ticks_per_frame;\r
409 \r
410                         if(!is_sane_fps(time_base))\r
411                         {                       \r
412                                 time_base = fix_time_base(time_base);\r
413 \r
414                                 if(!is_sane_fps(time_base) && audio_index > -1)\r
415                                 {\r
416                                         auto& audio_context = *context.streams[audio_index]->codec;\r
417                                         auto& audio_stream  = *context.streams[audio_index];\r
418 \r
419                                         double duration_sec = audio_stream.duration / static_cast<double>(audio_context.sample_rate);\r
420                                                                 \r
421                                         time_base.num = static_cast<int>(duration_sec*100000.0);\r
422                                         time_base.den = static_cast<int>(video_stream->nb_frames*100000);\r
423                                 }\r
424                         }\r
425                 }\r
426                 \r
427                 double fps = static_cast<double>(time_base.den) / static_cast<double>(time_base.num);\r
428 \r
429                 double closest_fps = 0.0;\r
430                 for(int n = 0; n < core::video_format::count; ++n)\r
431                 {\r
432                         auto format = core::video_format_desc(core::video_format(n));\r
433 \r
434                         double diff1 = std::abs(format.fps - fps);\r
435                         double diff2 = std::abs(closest_fps - fps);\r
436 \r
437                         if(diff1 < diff2)\r
438                                 closest_fps = format.fps;\r
439                 }\r
440         \r
441                 return closest_fps;\r
442         }\r
443 \r
444         return fail_value;      \r
445 }\r
446 \r
447 void fix_meta_data(AVFormatContext& context)\r
448 {\r
449         auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);\r
450 \r
451         if(video_index > -1)\r
452         {\r
453                 auto video_stream   = context.streams[video_index];\r
454                 auto video_context  = context.streams[video_index]->codec;\r
455                                                 \r
456                 if(boost::filesystem::path(context.filename).extension().string() == ".flv")\r
457                 {\r
458                         try\r
459                         {\r
460                                 auto meta = read_flv_meta_info(context.filename);\r
461                                 double fps = boost::lexical_cast<double>(meta["framerate"]);\r
462                                 video_stream->nb_frames = static_cast<int64_t>(boost::lexical_cast<double>(meta["duration"])*fps);\r
463                         }\r
464                         catch(...){}\r
465                 }\r
466                 else\r
467                 {\r
468                         auto stream_time = video_stream->time_base;\r
469                         auto duration    = video_stream->duration;\r
470                         auto codec_time  = video_context->time_base;\r
471                         auto ticks               = video_context->ticks_per_frame;\r
472 \r
473                         if(video_stream->nb_frames == 0)\r
474                                 video_stream->nb_frames = (duration*stream_time.num*codec_time.den)/(stream_time.den*codec_time.num*ticks);     \r
475                 }\r
476         }\r
477 }\r
478 \r
479 spl::shared_ptr<AVPacket> create_packet()\r
480 {\r
481         spl::shared_ptr<AVPacket> packet(new AVPacket, [](AVPacket* p)\r
482         {\r
483                 av_free_packet(p);\r
484                 delete p;\r
485         });\r
486         \r
487         av_init_packet(packet.get());\r
488         return packet;\r
489 }\r
490 \r
491 spl::shared_ptr<AVCodecContext> open_codec(AVFormatContext& context, enum AVMediaType type, int& index)\r
492 {       \r
493         AVCodec* decoder;\r
494         index = THROW_ON_ERROR2(av_find_best_stream(&context, type, -1, -1, &decoder, 0), "");\r
495         //if(strcmp(decoder->name, "prores") == 0 && decoder->next && strcmp(decoder->next->name, "prores_lgpl") == 0)\r
496         //      decoder = decoder->next;\r
497 \r
498         THROW_ON_ERROR2(tbb_avcodec_open(context.streams[index]->codec, decoder), "");\r
499         return spl::shared_ptr<AVCodecContext>(context.streams[index]->codec, tbb_avcodec_close);\r
500 }\r
501 \r
502 spl::shared_ptr<AVFormatContext> open_input(const std::wstring& filename)\r
503 {\r
504         AVFormatContext* weak_context = nullptr;\r
505         THROW_ON_ERROR2(avformat_open_input(&weak_context, u8(filename).c_str(), nullptr, nullptr), filename);\r
506         spl::shared_ptr<AVFormatContext> context(weak_context, av_close_input_file);                    \r
507         THROW_ON_ERROR2(avformat_find_stream_info(weak_context, nullptr), filename);\r
508         fix_meta_data(*context);\r
509         return context;\r
510 }\r
511 \r
512 std::wstring print_mode(int width, int height, double fps, bool interlaced)\r
513 {\r
514         std::wostringstream fps_ss;\r
515         fps_ss << std::fixed << std::setprecision(2) << (!interlaced ? fps : 2.0 * fps);\r
516 \r
517         return boost::lexical_cast<std::wstring>(width) + L"x" + boost::lexical_cast<std::wstring>(height) + (!interlaced ? L"p" : L"i") + fps_ss.str();\r
518 }\r
519 \r
520 bool is_valid_file(const std::wstring filename)\r
521 {                       \r
522         if(boost::filesystem::path(filename).extension() == ".m2t")\r
523                 return true;\r
524 \r
525         std::ifstream file(filename);\r
526 \r
527         std::vector<unsigned char> buf;\r
528         for(auto file_it = std::istreambuf_iterator<char>(file); file_it != std::istreambuf_iterator<char>() && buf.size() < 2048; ++file_it)\r
529                 buf.push_back(*file_it);\r
530 \r
531         if(buf.empty())\r
532                 return nullptr;\r
533 \r
534         AVProbeData pb;\r
535         pb.filename = u8(filename).c_str();\r
536         pb.buf          = buf.data();\r
537         pb.buf_size = static_cast<int>(buf.size());\r
538 \r
539         int score = 0;\r
540         return av_probe_input_format2(&pb, true, &score) != nullptr;\r
541 }\r
542 \r
543 std::wstring probe_stem(const std::wstring stem)\r
544 {\r
545         auto stem2 = boost::filesystem::path(stem);\r
546         auto dir = stem2.parent_path();\r
547         for(auto it = boost::filesystem::directory_iterator(dir); it != boost::filesystem::directory_iterator(); ++it)\r
548         {\r
549                 if(boost::iequals(it->path().stem().wstring(), stem2.filename().wstring()) && is_valid_file(it->path().wstring()))\r
550                         return it->path().wstring();\r
551         }\r
552         return L"";\r
553 }\r
554 //\r
555 //void av_dup_frame(AVFrame* frame)\r
556 //{\r
557 //      AVFrame* new_frame = avcodec_alloc_frame();\r
558 //\r
559 //\r
560 //      const uint8_t *src_data[4] = {0};\r
561 //      memcpy(const_cast<uint8_t**>(&src_data[0]), frame->data, 4);\r
562 //      const int src_linesizes[4] = {0};\r
563 //      memcpy(const_cast<int*>(&src_linesizes[0]), frame->linesize, 4);\r
564 //\r
565 //      av_image_alloc(new_frame->data, new_frame->linesize, new_frame->width, new_frame->height, frame->format, 16);\r
566 //\r
567 //      av_image_copy(new_frame->data, new_frame->linesize, src_data, src_linesizes, frame->format, new_frame->width, new_frame->height);\r
568 //\r
569 //      frame =\r
570 //}\r
571 \r
572 }}