|
|
@ -1,7 +1,7 @@ |
|
|
|
#include "image_io.hpp" |
|
|
|
#include "utilities.hpp" |
|
|
|
|
|
|
|
#include <av/sws.hpp> |
|
|
|
#include "av/sws.hpp" |
|
|
|
|
|
|
|
extern "C" { |
|
|
|
#include <libavutil/frame.h> |
|
|
@ -159,11 +159,29 @@ void encode_as_jpg(const av::frame& frame, const std::string& filename){ |
|
|
|
|
|
|
|
const auto opened_codec = av::codec_open(codec_ctx, codec, nullptr); |
|
|
|
|
|
|
|
const auto buffer_size = avpicture_get_size(pix_fmt, codec_ctx->width, codec_ctx->height); |
|
|
|
std::vector<uint8_t> buffer(make_u(buffer_size), 0); |
|
|
|
const auto output_size = avcodec_encode_video(codec_ctx.get(), buffer.data(), buffer_size, frame.get()); |
|
|
|
assert(output_size <= buffer_size); |
|
|
|
|
|
|
|
std::ofstream file(filename); |
|
|
|
file.write(reinterpret_cast<char*>(buffer.data()), output_size); |
|
|
|
// const auto buffer_size = avpicture_get_size(pix_fmt, codec_ctx->width, codec_ctx->height);
|
|
|
|
// std::vector<uint8_t> buffer(make_u(buffer_size), 0);
|
|
|
|
// const auto output_size = avcodec_encode_video(codec_ctx.get(), buffer.data(), buffer_size, frame.get());
|
|
|
|
// assert(output_size <= buffer_size);
|
|
|
|
if(avcodec_send_frame(codec_ctx.get(), frame.get()) < 0) { |
|
|
|
std::cerr << "Error when encoding frame" << std::endl; |
|
|
|
} |
|
|
|
|
|
|
|
AVPacket * p = av_packet_alloc(); |
|
|
|
std::ofstream file(filename); |
|
|
|
|
|
|
|
int ret = 0; |
|
|
|
while (ret >= 0) { |
|
|
|
ret = avcodec_receive_packet(codec_ctx.get(), p); |
|
|
|
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) |
|
|
|
return; |
|
|
|
else if (ret < 0) { |
|
|
|
std::cerr << "error" << std::endl; |
|
|
|
return; |
|
|
|
} |
|
|
|
std::clog << "encoded frame " << p->pts << " of size " << p->size << std::endl; |
|
|
|
|
|
|
|
file.write(reinterpret_cast<char*>(p->data), p->size); |
|
|
|
av_packet_unref(p); |
|
|
|
} |
|
|
|
} |
|
|
|