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