| 5 |
| #include <libswscale/swscale.h> |
| 6 | #include <libavutil/pixfmt.h> |
| 7 | } |
| 8 |
| 9 | struct SwsContextHolder { |
| 10 |
| 11 | SwsContext* ctx = nullptr; |
| 12 | int src_width = 0; |
| 13 | int src_height = 0; |
| 14 | AVPixelFormat src_format = AV_PIX_FMT_NONE; |
| 15 | int dst_width = 0; |
| 16 | int dst_height = 0; |
| 17 | AVPixelFormat dst_format = AV_PIX_FMT_NONE; |
| 18 |
| 19 | // Default constructor — starts with no context |
| 20 | SwsContextHolder() = default; |
| 21 |
| 22 | // Destructor — always free the context when the object is destroyed |
| 23 | ~SwsContextHolder() { |
| 24 | reset(); |
| 25 | } |
| 26 |
| 27 | // Copy constructor — disabled, context ownership must be unique |
| 28 | SwsContextHolder(const SwsContextHolder&) = delete; |
| 29 |
| 30 | // Copy assignment — disabled for the same reason |
| 31 | SwsContextHolder& operator=(const SwsContextHolder&) = delete; |
| 32 |
| 33 | // Move constructor — transfer ownership from another holder without freeing |
| 34 | SwsContextHolder(SwsContextHolder&& other) noexcept |
| 35 | : ctx(other.ctx), |
| 36 | src_width(other.src_width), src_height(other.src_height), src_format(other.src_format), |
| 37 | dst_width(other.dst_width), dst_height(other.dst_height), dst_format(other.dst_format) { |
| 38 | other.ctx = nullptr; // prevent the other holder from freeing the context |
| 39 | } |
| 40 |
| 41 | // Move assignment — free current context, then take ownership from other |
| 42 | SwsContextHolder& operator=(SwsContextHolder&& other) noexcept { |
| 43 | if (this != &other) { |
| 44 | reset(); // free whatever we currently hold |
| 45 | ctx = other.ctx; |
| 46 | src_width = other.src_width; |
| 47 | src_height = other.src_height; |
| 48 | src_format = other.src_format; |
| 49 | dst_width = other.dst_width; |
| 50 | dst_height = other.dst_height; |
| 51 | dst_format = other.dst_format; |
| 52 | other.ctx = nullptr; // prevent the other holder from freeing the context |
| 53 | } |
| 54 | return *this; |
| 55 | } |
| 56 |
| 57 | // Returns a valid context for the given parameters. |
| 58 | // Recreates the context if any parameter has changed since the last call. |
| 59 | SwsContext* get( |
| 60 | int new_src_width, int new_src_height, AVPixelFormat new_src_format, |
| 61 | int new_dst_width, int new_dst_height, AVPixelFormat new_dst_format |
| 62 | ) { |
| 63 | // Reuse existing context if all parameters match |
| 64 | if (ctx != nullptr && |
| 65 | src_width == new_src_width && src_height == new_src_height && src_format == new_src_format && |
| 66 | dst_width == new_dst_width && dst_height == new_dst_height && dst_format == new_dst_format) { |
| 67 | return ctx; |
| 68 | } |
| 69 |
| 70 | // Parameters changed — free the old context before creating a new one |
| 71 | reset(); |
| 72 |
| 73 | ctx = sws_getContext( |
| 74 | new_src_width, new_src_height, new_src_format, |
| 75 | new_dst_width, new_dst_height, new_dst_format, |
| 76 | SWS_BILINEAR, nullptr, nullptr, nullptr |
| 77 | ); |
| 78 |
| 79 | // Only store parameters if context was created successfully |
| 80 | if (ctx != nullptr) { |
| 81 | src_width = new_src_width; |
| 82 | src_height = new_src_height; |
| 83 | src_format = new_src_format; |
| 84 | dst_width = new_dst_width; |
| 85 | dst_height = new_dst_height; |
| 86 | dst_format = new_dst_format; |
| 87 | } |
| 88 |
| 89 | return ctx; |
| 90 | } |
| 91 |
| 92 | // Free the context and reset all parameters |
| 93 | void reset() { |
| 94 | if (ctx != nullptr) { |
| 95 | sws_freeContext(ctx); |
| 96 | ctx = nullptr; |
| 97 | } |
| 98 | src_width = src_height = dst_width = dst_height = 0; |
| 99 | src_format = dst_format = AV_PIX_FMT_NONE; |
| 100 | } |
| 101 | }; |
| 102 |
| 103 | #endif // SWS_CONTEXT_HOLDER_H |