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
24 #include <common/memory/memcpy.h>
\r
26 #include <core/video_format.h>
\r
27 #include <core/producer/frame/basic_frame.h>
\r
28 #include <core/producer/frame/write_frame.h>
\r
29 #include <core/producer/frame/image_transform.h>
\r
31 #include <tbb/parallel_for.h>
\r
33 #if defined(_MSC_VER)
\r
34 #pragma warning (push)
\r
35 #pragma warning (disable : 4244)
\r
39 #define __STDC_CONSTANT_MACROS
\r
40 #define __STDC_LIMIT_MACROS
\r
41 #include <libswscale/swscale.h>
\r
42 #include <libavformat/avformat.h>
\r
44 #if defined(_MSC_VER)
\r
45 #pragma warning (pop)
\r
50 core::pixel_format::type get_pixel_format(PixelFormat pix_fmt)
\r
54 case PIX_FMT_GRAY8: return core::pixel_format::gray;
\r
55 case PIX_FMT_BGRA: return core::pixel_format::bgra;
\r
56 case PIX_FMT_ARGB: return core::pixel_format::argb;
\r
57 case PIX_FMT_RGBA: return core::pixel_format::rgba;
\r
58 case PIX_FMT_ABGR: return core::pixel_format::abgr;
\r
59 case PIX_FMT_YUV444P: return core::pixel_format::ycbcr;
\r
60 case PIX_FMT_YUV422P: return core::pixel_format::ycbcr;
\r
61 case PIX_FMT_YUV420P: return core::pixel_format::ycbcr;
\r
62 case PIX_FMT_YUV411P: return core::pixel_format::ycbcr;
\r
63 case PIX_FMT_YUV410P: return core::pixel_format::ycbcr;
\r
64 case PIX_FMT_YUVA420P: return core::pixel_format::ycbcra;
\r
65 default: return core::pixel_format::invalid;
\r
69 core::pixel_format_desc get_pixel_format_desc(PixelFormat pix_fmt, size_t width, size_t height)
\r
72 AVPicture dummy_pict;
\r
73 avpicture_fill(&dummy_pict, nullptr, pix_fmt, width, height);
\r
75 core::pixel_format_desc desc;
\r
76 desc.pix_fmt = get_pixel_format(pix_fmt);
\r
78 switch(desc.pix_fmt)
\r
80 case core::pixel_format::gray:
\r
82 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 1));
\r
85 case core::pixel_format::bgra:
\r
86 case core::pixel_format::argb:
\r
87 case core::pixel_format::rgba:
\r
88 case core::pixel_format::abgr:
\r
90 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 4));
\r
93 case core::pixel_format::ycbcr:
\r
94 case core::pixel_format::ycbcra:
\r
96 // Find chroma height
\r
97 size_t size2 = dummy_pict.data[2] - dummy_pict.data[1];
\r
98 size_t h2 = size2/dummy_pict.linesize[1];
\r
100 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));
\r
101 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[1], h2, 1));
\r
102 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[2], h2, 1));
\r
104 if(desc.pix_fmt == core::pixel_format::ycbcra)
\r
105 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[3], height, 1));
\r
109 desc.pix_fmt = core::pixel_format::invalid;
\r
114 struct video_decoder::implementation : boost::noncopyable
\r
116 std::shared_ptr<core::frame_factory> frame_factory_;
\r
117 std::shared_ptr<SwsContext> sws_context_;
\r
119 AVCodecContext* codec_context_;
\r
123 const PixelFormat pix_fmt_;
\r
124 core::pixel_format_desc desc_;
\r
127 explicit implementation(AVCodecContext* codec_context, const safe_ptr<core::frame_factory>& frame_factory)
\r
128 : frame_factory_(frame_factory)
\r
129 , codec_context_(codec_context)
\r
130 , width_(codec_context_->width)
\r
131 , height_(codec_context_->height)
\r
132 , pix_fmt_(codec_context_->pix_fmt)
\r
133 , desc_(get_pixel_format_desc(pix_fmt_, width_, height_))
\r
135 if(desc_.pix_fmt == core::pixel_format::invalid)
\r
137 CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";
\r
139 desc_ = get_pixel_format_desc(PIX_FMT_BGRA, width_, height_);
\r
141 sws_context_.reset(sws_getContext(width_, height_, pix_fmt_, width_, height_, PIX_FMT_BGRA, SWS_BILINEAR, nullptr, nullptr, ¶m), sws_freeContext);
\r
143 BOOST_THROW_EXCEPTION(operation_failed() <<
\r
144 msg_info("Could not create software scaling context.") <<
\r
145 boost::errinfo_api_function("sws_getContext"));
\r
149 safe_ptr<core::write_frame> execute(void* tag, const aligned_buffer& video_packet)
\r
151 safe_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);
\r
153 int frame_finished = 0;
\r
154 const int result = avcodec_decode_video(codec_context_, decoded_frame.get(), &frame_finished, video_packet.data(), video_packet.size());
\r
157 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("avcodec_decode_video failed"));
\r
159 auto write = frame_factory_->create_frame(tag, desc_);
\r
160 if(sws_context_ == nullptr)
\r
162 tbb::parallel_for(0, static_cast<int>(desc_.planes.size()), 1, [&](int n)
\r
164 auto plane = desc_.planes[n];
\r
165 auto result = write->image_data(n).begin();
\r
166 auto decoded = decoded_frame->data[n];
\r
167 auto decoded_linesize = decoded_frame->linesize[n];
\r
169 // Copy line by line since ffmpeg sometimes pads each line.
\r
170 tbb::parallel_for(0, static_cast<int>(desc_.planes[n].height), 1, [&](int y)
\r
172 fast_memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);
\r
178 // Uses sws_scale when we don't support the provided color-space.
\r
179 safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
\r
180 avcodec_get_frame_defaults(av_frame.get());
\r
181 avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), write->image_data().begin(), PIX_FMT_BGRA, width_, height_);
\r
183 sws_scale(sws_context_.get(), decoded_frame->data, decoded_frame->linesize, 0, height_, av_frame->data, av_frame->linesize);
\r
186 // DVVIDEO is in lower field. Make it upper field if needed.
\r
187 if(codec_context_->codec_id == CODEC_ID_DVVIDEO && frame_factory_->get_video_format_desc().mode == core::video_mode::upper)
\r
188 write->get_image_transform().set_fill_translation(0.0f, 1.0/static_cast<double>(height_));
\r
194 video_decoder::video_decoder(AVCodecContext* codec_context, const safe_ptr<core::frame_factory>& frame_factory) : impl_(new implementation(codec_context, frame_factory)){}
\r
195 safe_ptr<core::write_frame> video_decoder::execute(void* tag, const aligned_buffer& video_packet){return impl_->execute(tag, video_packet);}
\r