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/mixer/write_frame.h>
\r
30 #include <core/producer/frame/image_transform.h>
\r
31 #include <core/producer/frame/pixel_format.h>
\r
32 #include <core/producer/frame/frame_factory.h>
\r
34 #include <tbb/parallel_for.h>
\r
36 #if defined(_MSC_VER)
\r
37 #pragma warning (push)
\r
38 #pragma warning (disable : 4244)
\r
42 #define __STDC_CONSTANT_MACROS
\r
43 #define __STDC_LIMIT_MACROS
\r
44 #include <libswscale/swscale.h>
\r
45 #include <libavformat/avformat.h>
\r
46 #include <libavcodec/avcodec.h>
\r
48 #if defined(_MSC_VER)
\r
49 #pragma warning (pop)
\r
54 core::pixel_format::type get_pixel_format(PixelFormat pix_fmt)
\r
58 case PIX_FMT_GRAY8: return core::pixel_format::gray;
\r
59 case PIX_FMT_BGRA: return core::pixel_format::bgra;
\r
60 case PIX_FMT_ARGB: return core::pixel_format::argb;
\r
61 case PIX_FMT_RGBA: return core::pixel_format::rgba;
\r
62 case PIX_FMT_ABGR: return core::pixel_format::abgr;
\r
63 case PIX_FMT_YUV444P: return core::pixel_format::ycbcr;
\r
64 case PIX_FMT_YUV422P: return core::pixel_format::ycbcr;
\r
65 case PIX_FMT_YUV420P: return core::pixel_format::ycbcr;
\r
66 case PIX_FMT_YUV411P: return core::pixel_format::ycbcr;
\r
67 case PIX_FMT_YUV410P: return core::pixel_format::ycbcr;
\r
68 case PIX_FMT_YUVA420P: return core::pixel_format::ycbcra;
\r
69 default: return core::pixel_format::invalid;
\r
73 core::pixel_format_desc get_pixel_format_desc(PixelFormat pix_fmt, size_t width, size_t height)
\r
76 AVPicture dummy_pict;
\r
77 avpicture_fill(&dummy_pict, nullptr, pix_fmt, width, height);
\r
79 core::pixel_format_desc desc;
\r
80 desc.pix_fmt = get_pixel_format(pix_fmt);
\r
82 switch(desc.pix_fmt)
\r
84 case core::pixel_format::gray:
\r
86 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 1));
\r
89 case core::pixel_format::bgra:
\r
90 case core::pixel_format::argb:
\r
91 case core::pixel_format::rgba:
\r
92 case core::pixel_format::abgr:
\r
94 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 4));
\r
97 case core::pixel_format::ycbcr:
\r
98 case core::pixel_format::ycbcra:
\r
100 // Find chroma height
\r
101 size_t size2 = dummy_pict.data[2] - dummy_pict.data[1];
\r
102 size_t h2 = size2/dummy_pict.linesize[1];
\r
104 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));
\r
105 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[1], h2, 1));
\r
106 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[2], h2, 1));
\r
108 if(desc.pix_fmt == core::pixel_format::ycbcra)
\r
109 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[3], height, 1));
\r
113 desc.pix_fmt = core::pixel_format::invalid;
\r
118 struct video_decoder::implementation : boost::noncopyable
\r
120 std::shared_ptr<SwsContext> sws_context_;
\r
121 const std::shared_ptr<core::frame_factory> frame_factory_;
\r
122 AVCodecContext& codec_context_;
\r
125 const PixelFormat pix_fmt_;
\r
126 core::pixel_format_desc desc_;
\r
129 explicit implementation(AVCodecContext& codec_context, const safe_ptr<core::frame_factory>& frame_factory)
\r
130 : frame_factory_(frame_factory)
\r
131 , codec_context_(codec_context)
\r
132 , width_(codec_context_.width)
\r
133 , height_(codec_context_.height)
\r
134 , pix_fmt_(codec_context_.pix_fmt)
\r
135 , desc_(get_pixel_format_desc(pix_fmt_, width_, height_))
\r
137 if(desc_.pix_fmt == core::pixel_format::invalid)
\r
139 CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";
\r
141 desc_ = get_pixel_format_desc(PIX_FMT_BGRA, width_, height_);
\r
143 sws_context_.reset(sws_getContext(width_, height_, pix_fmt_, width_, height_, PIX_FMT_BGRA, SWS_BILINEAR, nullptr, nullptr, ¶m), sws_freeContext);
\r
145 BOOST_THROW_EXCEPTION(operation_failed() <<
\r
146 msg_info("Could not create software scaling context.") <<
\r
147 boost::errinfo_api_function("sws_getContext"));
\r
151 std::vector<safe_ptr<core::write_frame>> execute(packet&& video_packet)
\r
153 std::vector<safe_ptr<core::write_frame>> result;
\r
155 switch(video_packet.type)
\r
158 avcodec_flush_buffers(&codec_context_);
\r
161 safe_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);
\r
163 int frame_finished = 0;
\r
164 const int errn = avcodec_decode_video2(&codec_context_, decoded_frame.get(), &frame_finished, video_packet.av_packet.get());
\r
168 BOOST_THROW_EXCEPTION(
\r
169 invalid_operation() <<
\r
170 msg_info(av_error_str(errn)) <<
\r
171 boost::errinfo_api_function("avcodec_decode_video") <<
\r
172 boost::errinfo_errno(AVUNERROR(errn)));
\r
175 if(frame_finished != 0)
\r
176 result.push_back(make_write_frame(decoded_frame));
\r
182 safe_ptr<core::write_frame> make_write_frame(safe_ptr<AVFrame> decoded_frame)
\r
184 auto write = frame_factory_->create_frame(this, desc_);
\r
185 if(sws_context_ == nullptr)
\r
187 tbb::parallel_for(0, static_cast<int>(desc_.planes.size()), 1, [&](int n)
\r
189 auto plane = desc_.planes[n];
\r
190 auto result = write->image_data(n).begin();
\r
191 auto decoded = decoded_frame->data[n];
\r
192 auto decoded_linesize = decoded_frame->linesize[n];
\r
194 // Copy line by line since ffmpeg sometimes pads each line.
\r
195 tbb::parallel_for(tbb::blocked_range<size_t>(0, static_cast<int>(desc_.planes[n].height)), [&](const tbb::blocked_range<size_t>& r)
\r
197 for(size_t y = r.begin(); y != r.end(); ++y)
\r
198 memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);
\r
204 // Use sws_scale when provided colorspace has no hw-accel.
\r
205 safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
\r
206 avcodec_get_frame_defaults(av_frame.get());
\r
207 avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), write->image_data().begin(), PIX_FMT_BGRA, width_, height_);
\r
209 sws_scale(sws_context_.get(), decoded_frame->data, decoded_frame->linesize, 0, height_, av_frame->data, av_frame->linesize);
\r
212 // DVVIDEO is in lower field. Make it upper field if needed.
\r
213 if(codec_context_.codec_id == CODEC_ID_DVVIDEO && frame_factory_->get_video_format_desc().mode == core::video_mode::upper)
\r
214 write->get_image_transform().set_fill_translation(0.0f, 1.0/static_cast<double>(height_));
\r
220 video_decoder::video_decoder(AVCodecContext& codec_context, const safe_ptr<core::frame_factory>& frame_factory) : impl_(new implementation(codec_context, frame_factory)){}
\r
221 std::vector<safe_ptr<core::write_frame>> video_decoder::execute(packet&& video_packet){return impl_->execute(std::move(video_packet));}
\r