2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
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
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
20 #include "../../stdafx.h"
\r
22 #include "video_decoder.h"
\r
23 #include "../../ffmpeg_error.h"
\r
25 #include <common/memory/memcpy.h>
\r
27 #include <core/video_format.h>
\r
28 #include <core/producer/frame/basic_frame.h>
\r
29 #include <core/producer/frame/write_frame.h>
\r
30 #include <core/producer/frame/image_transform.h>
\r
32 #include <tbb/parallel_for.h>
\r
34 #if defined(_MSC_VER)
\r
35 #pragma warning (push)
\r
36 #pragma warning (disable : 4244)
\r
40 #define __STDC_CONSTANT_MACROS
\r
41 #define __STDC_LIMIT_MACROS
\r
42 #include <libswscale/swscale.h>
\r
43 #include <libavformat/avformat.h>
\r
45 #if defined(_MSC_VER)
\r
46 #pragma warning (pop)
\r
51 core::pixel_format::type get_pixel_format(PixelFormat pix_fmt)
\r
55 case PIX_FMT_GRAY8: return core::pixel_format::gray;
\r
56 case PIX_FMT_BGRA: return core::pixel_format::bgra;
\r
57 case PIX_FMT_ARGB: return core::pixel_format::argb;
\r
58 case PIX_FMT_RGBA: return core::pixel_format::rgba;
\r
59 case PIX_FMT_ABGR: return core::pixel_format::abgr;
\r
60 case PIX_FMT_YUV444P: return core::pixel_format::ycbcr;
\r
61 case PIX_FMT_YUV422P: return core::pixel_format::ycbcr;
\r
62 case PIX_FMT_YUV420P: return core::pixel_format::ycbcr;
\r
63 case PIX_FMT_YUV411P: return core::pixel_format::ycbcr;
\r
64 case PIX_FMT_YUV410P: return core::pixel_format::ycbcr;
\r
65 case PIX_FMT_YUVA420P: return core::pixel_format::ycbcra;
\r
66 default: return core::pixel_format::invalid;
\r
70 core::pixel_format_desc get_pixel_format_desc(PixelFormat pix_fmt, size_t width, size_t height)
\r
73 AVPicture dummy_pict;
\r
74 avpicture_fill(&dummy_pict, nullptr, pix_fmt, width, height);
\r
76 core::pixel_format_desc desc;
\r
77 desc.pix_fmt = get_pixel_format(pix_fmt);
\r
79 switch(desc.pix_fmt)
\r
81 case core::pixel_format::gray:
\r
83 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 1));
\r
86 case core::pixel_format::bgra:
\r
87 case core::pixel_format::argb:
\r
88 case core::pixel_format::rgba:
\r
89 case core::pixel_format::abgr:
\r
91 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 4));
\r
94 case core::pixel_format::ycbcr:
\r
95 case core::pixel_format::ycbcra:
\r
97 // Find chroma height
\r
98 size_t size2 = dummy_pict.data[2] - dummy_pict.data[1];
\r
99 size_t h2 = size2/dummy_pict.linesize[1];
\r
101 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));
\r
102 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[1], h2, 1));
\r
103 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[2], h2, 1));
\r
105 if(desc.pix_fmt == core::pixel_format::ycbcra)
\r
106 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[3], height, 1));
\r
110 desc.pix_fmt = core::pixel_format::invalid;
\r
115 struct video_decoder::implementation : boost::noncopyable
\r
117 std::shared_ptr<SwsContext> sws_context_;
\r
118 const std::shared_ptr<core::frame_factory> frame_factory_;
\r
119 AVCodecContext* codec_context_;
\r
122 const PixelFormat pix_fmt_;
\r
123 core::pixel_format_desc desc_;
\r
126 explicit implementation(AVCodecContext* codec_context, const safe_ptr<core::frame_factory>& frame_factory)
\r
127 : frame_factory_(frame_factory)
\r
128 , codec_context_(codec_context)
\r
129 , width_(codec_context_->width)
\r
130 , height_(codec_context_->height)
\r
131 , pix_fmt_(codec_context_->pix_fmt)
\r
132 , desc_(get_pixel_format_desc(pix_fmt_, width_, height_))
\r
134 if(desc_.pix_fmt == core::pixel_format::invalid)
\r
136 CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";
\r
138 desc_ = get_pixel_format_desc(PIX_FMT_BGRA, width_, height_);
\r
140 sws_context_.reset(sws_getContext(width_, height_, pix_fmt_, width_, height_, PIX_FMT_BGRA, SWS_BILINEAR, nullptr, nullptr, ¶m), sws_freeContext);
\r
142 BOOST_THROW_EXCEPTION(operation_failed() <<
\r
143 msg_info("Could not create software scaling context.") <<
\r
144 boost::errinfo_api_function("sws_getContext"));
\r
148 std::shared_ptr<core::write_frame> execute(void* tag, const std::shared_ptr<aligned_buffer>& video_packet)
\r
153 if(video_packet->empty()) // Need to flush
\r
155 avcodec_flush_buffers(codec_context_);
\r
159 safe_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);
\r
161 int frame_finished = 0;
\r
162 const int errn = avcodec_decode_video(codec_context_, decoded_frame.get(), &frame_finished, video_packet->data(), video_packet->size());
\r
166 BOOST_THROW_EXCEPTION(
\r
167 invalid_operation() <<
\r
168 msg_info(av_error_str(errn)) <<
\r
169 boost::errinfo_api_function("avcodec_decode_video") <<
\r
170 boost::errinfo_errno(AVUNERROR(errn)));
\r
173 if(frame_finished == 0)
\r
176 auto write = frame_factory_->create_frame(tag, desc_);
\r
177 if(sws_context_ == nullptr)
\r
179 tbb::parallel_for(0, static_cast<int>(desc_.planes.size()), 1, [&](int n)
\r
181 auto plane = desc_.planes[n];
\r
182 auto result = write->image_data(n).begin();
\r
183 auto decoded = decoded_frame->data[n];
\r
184 auto decoded_linesize = decoded_frame->linesize[n];
\r
186 // Copy line by line since ffmpeg sometimes pads each line.
\r
187 tbb::parallel_for(0, static_cast<int>(desc_.planes[n].height), 1, [&](int y)
\r
189 fast_memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);
\r
195 // Uses sws_scale when we don't support the provided color-space.
\r
196 safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
\r
197 avcodec_get_frame_defaults(av_frame.get());
\r
198 avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), write->image_data().begin(), PIX_FMT_BGRA, width_, height_);
\r
200 sws_scale(sws_context_.get(), decoded_frame->data, decoded_frame->linesize, 0, height_, av_frame->data, av_frame->linesize);
\r
203 // DVVIDEO is in lower field. Make it upper field if needed.
\r
204 if(codec_context_->codec_id == CODEC_ID_DVVIDEO && frame_factory_->get_video_format_desc().mode == core::video_mode::upper)
\r
205 write->get_image_transform().set_fill_translation(0.0f, 1.0/static_cast<double>(height_));
\r
211 video_decoder::video_decoder(AVCodecContext* codec_context, const safe_ptr<core::frame_factory>& frame_factory) : impl_(new implementation(codec_context, frame_factory)){}
\r
212 std::shared_ptr<core::write_frame> video_decoder::execute(void* tag, const std::shared_ptr<aligned_buffer>& video_packet){return impl_->execute(tag, video_packet);}
\r