Compare commits

...

10 commits

Author SHA1 Message Date
49071365f7 Handle use of ffmpeg deprecated function 2024-06-24 13:20:21 +09:00
00703f8299 Update LICENSE year 2024-03-07 16:29:51 +09:00
f17539ac14 Minor fix: un/signed int comparison warning 2024-03-07 16:28:17 +09:00
d551c9da3b Fix use of deprecated OpenCL function
Also specify use of OpenCL 3.0 instead of depending on default.
2024-03-07 16:25:46 +09:00
1d27fd3b21 Fix usage of deprecated AVFrame::pkt_duration
Uses a macro to check FFMpeg version before setting AVFrame::duration or
AVFrame::pkt_duration.
2024-03-07 16:25:31 +09:00
2e03098540 Fix uninit. flag: videos outputted pngs sometimes 2023-05-28 21:46:57 +09:00
1c3554b3ca Update LICENSE year 2023-05-23 22:54:32 +09:00
c1c271b77e Support multiple OpenCL platforms
Previously, the code only checked the first OpenCL platform and failed
if thet platform didn't have any devices.
2023-05-23 22:51:08 +09:00
379fb4cadf Update year in LICENSE 2022-02-18 15:26:32 +09:00
d429cf44ff
Update README.md 2022-02-18 15:25:53 +09:00
7 changed files with 73 additions and 30 deletions

View file

@ -40,3 +40,6 @@ target_link_libraries(DitheringProject PUBLIC
${PNG_LIBRARIES}
${FFMPEG_LIBAVCODEC_LINK_LIBRARIES}
)
target_compile_definitions(DitheringProject PRIVATE
CL_TARGET_OPENCL_VERSION=300)

View file

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2021 Stephen Seo
Copyright (c) 2021-2024 Stephen Seo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View file

@ -6,9 +6,9 @@
of the course. Newer versions are meant to be maintenance releases; fixes that
keep the project working. As of 2022-01-18, there may be (after now) a version
`1.1.0` or `1.0.1` due to keeping up to date with the FFmpeg dependency as a new
major version of FFmpeg was released (version `5.0`).
major version of FFmpeg was released (version `5.0`).~~
To be clear, version `1.0.0` of this project is expected to work with FFmpeg
~~To be clear, version `1.0.0` of this project is expected to work with FFmpeg
version `4.4`.~~
Fixes were applied for FFmpeg version 5. A macro is used to change incompatible

View file

@ -7,6 +7,7 @@ Args::Args()
: do_dither_image_(true),
do_dither_grayscaled_(false),
do_overwrite_(false),
do_video_pngs_(false),
input_filename(),
output_filename() {}

View file

@ -694,34 +694,60 @@ OpenCLContext::OpenCLContext() : context_(nullptr), queue_(nullptr) {
//////////////////// set up cl_context
cl_int err_num;
cl_uint num_platforms;
cl_platform_id first_platform_id;
std::vector<cl_platform_id> platform_ids;
err_num = clGetPlatformIDs(1, &first_platform_id, &num_platforms);
if (err_num != CL_SUCCESS || num_platforms == 0) {
err_num = clGetPlatformIDs(0, nullptr, &num_platforms);
if (err_num != CL_SUCCESS) {
std::cout << "ERROR: OpenCLContext: Failed to query OpenCL platforms"
<< std::endl;
return;
} else if (num_platforms == 0) {
std::cout << "ERROR: OpenCLContext: Failed to find any OpenCL platforms"
<< std::endl;
return;
}
cl_context_properties context_properties[] = {
CL_CONTEXT_PLATFORM,
reinterpret_cast<cl_context_properties>(first_platform_id), 0};
context_ = clCreateContextFromType(context_properties, CL_DEVICE_TYPE_GPU,
nullptr, nullptr, &err_num);
platform_ids.resize(num_platforms);
err_num = clGetPlatformIDs(num_platforms, platform_ids.data(), nullptr);
if (err_num != CL_SUCCESS) {
std::cout << "ERROR: OpenCLContext: Failed to create GPU context, "
<< "trying CPU..." << std::endl;
context_ = clCreateContextFromType(context_properties, CL_DEVICE_TYPE_CPU,
std::cout << "ERROR: OpenCLContext: Failed to get OpenCL platform id(s)"
<< std::endl;
return;
}
bool success = false;
for (cl_uint i = 0; i < num_platforms; ++i) {
cl_context_properties context_properties[] = {
CL_CONTEXT_PLATFORM,
reinterpret_cast<cl_context_properties>(platform_ids[i]), 0};
context_ = clCreateContextFromType(context_properties, CL_DEVICE_TYPE_GPU,
nullptr, nullptr, &err_num);
if (err_num != CL_SUCCESS) {
std::cout << "ERROR: OpenCLContext: Failed to create CPU context"
<< std::endl;
context_ = nullptr;
return;
std::cout << "ERROR: OpenCLContext: Failed to create GPU context, "
<< "trying CPU..." << std::endl;
context_ = clCreateContextFromType(context_properties, CL_DEVICE_TYPE_CPU,
nullptr, nullptr, &err_num);
if (err_num != CL_SUCCESS) {
std::cout << "ERROR: OpenCLContext: Failed to create CPU context"
<< std::endl;
context_ = nullptr;
continue;
} else {
success = true;
break;
}
} else {
success = true;
break;
}
}
if (!success) {
std::cout << "ERROR: OpenCLContext: Failed to find a valid OpenCL context!"
<< std::endl;
}
//////////////////// end set up cl context
//////////////////// set up command queue
@ -754,7 +780,8 @@ OpenCLContext::OpenCLContext() : context_(nullptr), queue_(nullptr) {
}
// uses first available device
queue_ = clCreateCommandQueue(context_, devices.at(0), 0, &err_num);
queue_ = clCreateCommandQueueWithProperties(context_, devices.at(0), nullptr,
&err_num);
if (err_num != CL_SUCCESS) {
std::cout << "ERROR: OpenCLContext: Failed to create command queue"
<< std::endl;

View file

@ -265,7 +265,7 @@ bool Video::DitherVideo(const std::string &output_filename, Image *blue_noise,
return_value = avcodec_open2(enc_codec_context, enc_codec, nullptr);
if (return_value != 0) {
std::cout << "ERROR: Failed to init enc_codec_context" << std::endl;
avcodec_close(enc_codec_context);
IGPUP_DITHERING_avcodec_close_ctx(&enc_codec_context);
avformat_free_context(avf_enc_context);
av_frame_free(&frame);
av_packet_free(&pkt);
@ -279,7 +279,7 @@ bool Video::DitherVideo(const std::string &output_filename, Image *blue_noise,
if (return_value < 0) {
std::cout << "ERROR: Failed to set encoding codec parameters in stream"
<< std::endl;
avcodec_close(enc_codec_context);
IGPUP_DITHERING_avcodec_close_ctx(&enc_codec_context);
avformat_free_context(avf_enc_context);
av_frame_free(&frame);
av_packet_free(&pkt);
@ -298,7 +298,7 @@ bool Video::DitherVideo(const std::string &output_filename, Image *blue_noise,
if (return_value < 0) {
std::cout << "ERROR: Failed to open file \"" << output_filename
<< "\" for writing" << std::endl;
avcodec_close(enc_codec_context);
IGPUP_DITHERING_avcodec_close_ctx(&enc_codec_context);
avformat_free_context(avf_enc_context);
av_frame_free(&frame);
av_packet_free(&pkt);
@ -313,7 +313,7 @@ bool Video::DitherVideo(const std::string &output_filename, Image *blue_noise,
if (return_value < 0) {
std::cout << "ERROR: Failed to write header in output video file"
<< std::endl;
avcodec_close(enc_codec_context);
IGPUP_DITHERING_avcodec_close_ctx(&enc_codec_context);
avformat_free_context(avf_enc_context);
av_frame_free(&frame);
av_packet_free(&pkt);
@ -332,7 +332,7 @@ bool Video::DitherVideo(const std::string &output_filename, Image *blue_noise,
HandleDecodingPacket(codec_ctx, pkt, frame, blue_noise, grayscale,
color_changed, output_as_pngs);
if (!std::get<0>(ret_tuple)) {
avcodec_close(enc_codec_context);
IGPUP_DITHERING_avcodec_close_ctx(&enc_codec_context);
avformat_free_context(avf_enc_context);
av_frame_free(&frame);
av_packet_unref(pkt);
@ -345,7 +345,7 @@ bool Video::DitherVideo(const std::string &output_filename, Image *blue_noise,
if (!HandleEncodingFrame(avf_enc_context, enc_codec_context,
yuv_frame, enc_stream)) {
av_frame_free(&yuv_frame);
avcodec_close(enc_codec_context);
IGPUP_DITHERING_avcodec_close_ctx(&enc_codec_context);
avformat_free_context(avf_enc_context);
av_frame_free(&frame);
av_packet_unref(pkt);
@ -366,7 +366,7 @@ bool Video::DitherVideo(const std::string &output_filename, Image *blue_noise,
HandleDecodingPacket(codec_ctx, nullptr, frame, blue_noise, grayscale,
color_changed, output_as_pngs);
if (!std::get<0>(ret_tuple)) {
avcodec_close(enc_codec_context);
IGPUP_DITHERING_avcodec_close_ctx(&enc_codec_context);
avformat_free_context(avf_enc_context);
av_frame_free(&frame);
av_packet_free(&pkt);
@ -378,7 +378,7 @@ bool Video::DitherVideo(const std::string &output_filename, Image *blue_noise,
if (!HandleEncodingFrame(avf_enc_context, enc_codec_context, yuv_frame,
enc_stream)) {
av_frame_free(&yuv_frame);
avcodec_close(enc_codec_context);
IGPUP_DITHERING_avcodec_close_ctx(&enc_codec_context);
avformat_free_context(avf_enc_context);
av_frame_free(&frame);
av_packet_free(&pkt);
@ -394,7 +394,7 @@ bool Video::DitherVideo(const std::string &output_filename, Image *blue_noise,
// flush encoder
if (!HandleEncodingFrame(avf_enc_context, enc_codec_context, nullptr,
enc_stream)) {
avcodec_close(enc_codec_context);
IGPUP_DITHERING_avcodec_close_ctx(&enc_codec_context);
avformat_free_context(avf_enc_context);
av_frame_free(&frame);
av_packet_free(&pkt);
@ -409,7 +409,7 @@ bool Video::DitherVideo(const std::string &output_filename, Image *blue_noise,
// cleanup
if (enc_codec_context) {
avcodec_close(enc_codec_context);
IGPUP_DITHERING_avcodec_close_ctx(&enc_codec_context);
}
if (!output_as_pngs && !(avf_enc_context->oformat->flags & AVFMT_NOFILE)) {
avio_closep(&avf_enc_context->pb);
@ -614,7 +614,11 @@ std::tuple<bool, std::vector<AVFrame *>> Video::HandleDecodingPacket(
// cleanup
av_frame_free(&temp_frame);
yuv_frame->pts = frame_count_ - 1;
#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(58, 2, 100)
yuv_frame->pkt_duration = 1;
#else
yuv_frame->duration = 1;
#endif
return_frames.push_back(yuv_frame);
} // else (!output_as_pngs)
} // while (return_value >= 0)

View file

@ -11,6 +11,14 @@ extern "C" {
#include "image.h"
inline void IGPUP_DITHERING_avcodec_close_ctx(AVCodecContext **avctx) {
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 3, 100)
avcodec_free_context(avctx);
#else
avcodec_close(*avctx);
#endif
}
constexpr unsigned int kReadBufSize = 4096;
constexpr unsigned int kReadBufPaddingSize = AV_INPUT_BUFFER_PADDING_SIZE;
constexpr unsigned int kReadBufSizeWithPadding =