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