| 1 | #include "cvd/videoscaler.h" |
| 2 | |
| 3 | #include "slice.h" |
| 4 | |
| 5 | extern "C" |
| 6 | { |
| 7 | #include <libavformat/avformat.h> |
| 8 | #include <libavutil/pixdesc.h> |
| 9 | #include <libswscale/swscale.h> |
| 10 | } |
| 11 | |
| 12 | #include <algorithm> |
| 13 | #include <array> |
| 14 | #include <stdexcept> |
| 15 | |
| 16 | namespace CVD |
| 17 | { |
| 18 | namespace internal |
| 19 | { |
| 20 | namespace |
| 21 | { |
| 22 | template <typename T> |
| 23 | std::array<T*, 4> AdjustDataPointers(const AVPixFmtDescriptor& descriptor, T* const* data, const int* linesize, int y) |
| 24 | { |
| 25 | std::array<T*, 4> adjusted_data; |
| 26 | adjusted_data[0] = data[0] + linesize[0] * y; |
| 27 | adjusted_data[1] = data[1] + (linesize[1] >> descriptor.log2_chroma_h) * y; |
| 28 | adjusted_data[2] = data[2] + (linesize[2] >> descriptor.log2_chroma_h) * y; |
| 29 | adjusted_data[3] = data[3] + linesize[3] * y; |
| 30 | return adjusted_data; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | Scaler::Scaler(int width, int height, int source_format, int target_format, int colorspace, int threads) |
| 35 | : m_source_format(source_format) |
| 36 | , m_target_format(target_format) |
| 37 | { |
| 38 | for(int j = 0; j < threads; ++j) |
| 39 | { |
| 40 | m_contexts.emplace_back(sws_getContext(width, |
| 41 | height, |
| 42 | static_cast<AVPixelFormat>(source_format), |
| 43 | width, |
| 44 | height, |
| 45 | static_cast<AVPixelFormat>(target_format), |
| 46 | SWS_POINT, |
| 47 | nullptr, |
| 48 | nullptr, |
| 49 | nullptr)); |
| 50 | if(!m_contexts.back()) |
| 51 | { |
| 52 | throw std::runtime_error("Failed to create scaler context"); |
| 53 | } |
| 54 | if(colorspace != -1) |
| 55 | { |
| 56 | SetColorSpace(m_contexts.back().get(), colorspace); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void Scaler::scale(const std::uint8_t* const* input_data, |
| 62 | const int* input_linesize, |
| 63 | const int y, |
| 64 | const int height, |
| 65 | std::uint8_t* const* output_data, |
| 66 | const int* output_linesize) |
| 67 | { |
| 68 | const AVPixFmtDescriptor* source_descriptor = av_pix_fmt_desc_get(static_cast<AVPixelFormat>(m_source_format)); |
| 69 | const AVPixFmtDescriptor* target_descriptor = av_pix_fmt_desc_get(static_cast<AVPixelFormat>(m_target_format)); |
| 70 | |
| 71 | int slice_height = std::max(1, static_cast<int>((height + m_contexts.size() - 1) / m_contexts.size())); |
| 72 | while(slice_height & ((1 << std::max(source_descriptor->log2_chroma_h, target_descriptor->log2_chroma_h)) - 1)) |
| 73 | { |
| 74 | ++slice_height; |
| 75 | } |
| 76 | |
| 77 | Slice(height, slice_height, [&](int i, int j, int height) { |
| 78 | auto inputDataJ = AdjustDataPointers(*source_descriptor, input_data, input_linesize, y + j); |
| 79 | auto outputDataJ = AdjustDataPointers(*target_descriptor, output_data, output_linesize, y + j); |
| 80 | sws_scale(m_contexts[i].get(), inputDataJ.data(), input_linesize, 0, height, outputDataJ.data(), output_linesize); |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | } |