2012-08-06 15:46:42 +00:00
|
|
|
/*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2012-08-06 15:46:42 +00:00
|
|
|
*
|
2017-06-18 13:45:24 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2012-08-06 15:46:42 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2012-08-06 15:46:42 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-06-18 13:45:24 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2012-08-06 15:46:42 +00:00
|
|
|
*
|
2017-06-18 13:45:24 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2012-08-06 15:46:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-07-20 18:02:14 +00:00
|
|
|
#include <inttypes.h>
|
2012-08-06 15:46:42 +00:00
|
|
|
|
|
|
|
#include <libavcodec/avcodec.h>
|
2023-03-24 23:30:57 +00:00
|
|
|
#include <libavformat/avformat.h>
|
2012-11-09 00:06:43 +00:00
|
|
|
#include <libavutil/mem.h>
|
2016-02-09 12:24:04 +00:00
|
|
|
#include <libavutil/opt.h>
|
2023-03-24 23:30:57 +00:00
|
|
|
#include <libavutil/pixdesc.h>
|
2023-11-04 02:55:38 +00:00
|
|
|
#include <libplacebo/utils/libav.h>
|
2012-08-06 15:46:42 +00:00
|
|
|
|
2023-03-24 23:30:57 +00:00
|
|
|
#include "common/msg.h"
|
2012-08-06 15:46:42 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_JPEG
|
2019-09-14 20:06:00 +00:00
|
|
|
#include <setjmp.h>
|
2012-08-06 15:46:42 +00:00
|
|
|
#include <jpeglib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "osdep/io.h"
|
2024-03-19 04:38:58 +00:00
|
|
|
#include "misc/path_utils.h"
|
2012-08-06 15:46:42 +00:00
|
|
|
|
2023-03-24 23:30:57 +00:00
|
|
|
#include "common/av_common.h"
|
|
|
|
#include "common/msg.h"
|
2012-08-06 15:46:42 +00:00
|
|
|
#include "image_writer.h"
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_talloc.h"
|
2023-03-24 23:30:57 +00:00
|
|
|
#include "video/fmt-conversion.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "video/img_format.h"
|
|
|
|
#include "video/mp_image.h"
|
|
|
|
#include "video/sws_utils.h"
|
2012-08-06 15:46:42 +00:00
|
|
|
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/m_option.h"
|
2012-08-06 15:46:42 +00:00
|
|
|
|
|
|
|
const struct image_writer_opts image_writer_opts_defaults = {
|
2017-03-18 14:03:05 +00:00
|
|
|
.format = AV_CODEC_ID_MJPEG,
|
2023-02-20 03:32:50 +00:00
|
|
|
.high_bit_depth = true,
|
2012-08-06 15:46:42 +00:00
|
|
|
.png_compression = 7,
|
2013-06-15 13:14:06 +00:00
|
|
|
.png_filter = 5,
|
2012-08-15 20:38:43 +00:00
|
|
|
.jpeg_quality = 90,
|
2023-02-20 03:32:50 +00:00
|
|
|
.jpeg_source_chroma = true,
|
2019-09-14 20:18:02 +00:00
|
|
|
.webp_quality = 75,
|
2019-08-02 14:04:54 +00:00
|
|
|
.webp_compression = 4,
|
2022-04-23 18:24:09 +00:00
|
|
|
.jxl_distance = 1.0,
|
2023-02-14 11:39:37 +00:00
|
|
|
.jxl_effort = 4,
|
2023-03-24 23:30:57 +00:00
|
|
|
.avif_encoder = "libaom-av1",
|
|
|
|
.avif_opts = (char*[]){
|
|
|
|
"usage", "allintra",
|
2023-12-03 17:51:36 +00:00
|
|
|
"crf", "0",
|
2023-03-24 23:30:57 +00:00
|
|
|
"cpu-used", "8",
|
|
|
|
NULL
|
|
|
|
},
|
2023-02-20 03:32:50 +00:00
|
|
|
.tag_csp = true,
|
2012-08-06 15:46:42 +00:00
|
|
|
};
|
|
|
|
|
2017-03-18 14:03:05 +00:00
|
|
|
const struct m_opt_choice_alternatives mp_image_writer_formats[] = {
|
|
|
|
{"jpg", AV_CODEC_ID_MJPEG},
|
|
|
|
{"jpeg", AV_CODEC_ID_MJPEG},
|
|
|
|
{"png", AV_CODEC_ID_PNG},
|
2019-09-14 20:18:02 +00:00
|
|
|
{"webp", AV_CODEC_ID_WEBP},
|
2022-04-23 18:24:09 +00:00
|
|
|
{"jxl", AV_CODEC_ID_JPEGXL},
|
2023-03-24 23:30:57 +00:00
|
|
|
{"avif", AV_CODEC_ID_AV1},
|
2017-03-18 14:03:05 +00:00
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
2012-08-06 15:48:30 +00:00
|
|
|
#define OPT_BASE_STRUCT struct image_writer_opts
|
|
|
|
|
2016-09-05 19:04:55 +00:00
|
|
|
const struct m_option image_writer_opts[] = {
|
options: change option macros and all option declarations
Change all OPT_* macros such that they don't define the entire m_option
initializer, and instead expand only to a part of it, which sets certain
fields. This requires changing almost every option declaration, because
they all use these macros. A declaration now always starts with
{"name", ...
followed by designated initializers only (possibly wrapped in macros).
The OPT_* macros now initialize the .offset and .type fields only,
sometimes also .priv and others.
I think this change makes the option macros less tricky. The old code
had to stuff everything into macro arguments (and attempted to allow
setting arbitrary fields by letting the user pass designated
initializers in the vararg parts). Some of this was made messy due to
C99 and C11 not allowing 0-sized varargs with ',' removal. It's also
possible that this change is pointless, other than cosmetic preferences.
Not too happy about some things. For example, the OPT_CHOICE()
indentation I applied looks a bit ugly.
Much of this change was done with regex search&replace, but some places
required manual editing. In particular, code in "obscure" areas (which I
didn't include in compilation) might be broken now.
In wayland_common.c the author of some option declarations confused the
flags parameter with the default value (though the default value was
also properly set below). I fixed this with this change.
2020-03-14 20:28:01 +00:00
|
|
|
{"format", OPT_CHOICE_C(format, mp_image_writer_formats)},
|
|
|
|
{"jpeg-quality", OPT_INT(jpeg_quality), M_RANGE(0, 100)},
|
2023-02-20 03:32:50 +00:00
|
|
|
{"jpeg-source-chroma", OPT_BOOL(jpeg_source_chroma)},
|
options: change option macros and all option declarations
Change all OPT_* macros such that they don't define the entire m_option
initializer, and instead expand only to a part of it, which sets certain
fields. This requires changing almost every option declaration, because
they all use these macros. A declaration now always starts with
{"name", ...
followed by designated initializers only (possibly wrapped in macros).
The OPT_* macros now initialize the .offset and .type fields only,
sometimes also .priv and others.
I think this change makes the option macros less tricky. The old code
had to stuff everything into macro arguments (and attempted to allow
setting arbitrary fields by letting the user pass designated
initializers in the vararg parts). Some of this was made messy due to
C99 and C11 not allowing 0-sized varargs with ',' removal. It's also
possible that this change is pointless, other than cosmetic preferences.
Not too happy about some things. For example, the OPT_CHOICE()
indentation I applied looks a bit ugly.
Much of this change was done with regex search&replace, but some places
required manual editing. In particular, code in "obscure" areas (which I
didn't include in compilation) might be broken now.
In wayland_common.c the author of some option declarations confused the
flags parameter with the default value (though the default value was
also properly set below). I fixed this with this change.
2020-03-14 20:28:01 +00:00
|
|
|
{"png-compression", OPT_INT(png_compression), M_RANGE(0, 9)},
|
|
|
|
{"png-filter", OPT_INT(png_filter), M_RANGE(0, 5)},
|
2023-02-20 03:32:50 +00:00
|
|
|
{"webp-lossless", OPT_BOOL(webp_lossless)},
|
options: change option macros and all option declarations
Change all OPT_* macros such that they don't define the entire m_option
initializer, and instead expand only to a part of it, which sets certain
fields. This requires changing almost every option declaration, because
they all use these macros. A declaration now always starts with
{"name", ...
followed by designated initializers only (possibly wrapped in macros).
The OPT_* macros now initialize the .offset and .type fields only,
sometimes also .priv and others.
I think this change makes the option macros less tricky. The old code
had to stuff everything into macro arguments (and attempted to allow
setting arbitrary fields by letting the user pass designated
initializers in the vararg parts). Some of this was made messy due to
C99 and C11 not allowing 0-sized varargs with ',' removal. It's also
possible that this change is pointless, other than cosmetic preferences.
Not too happy about some things. For example, the OPT_CHOICE()
indentation I applied looks a bit ugly.
Much of this change was done with regex search&replace, but some places
required manual editing. In particular, code in "obscure" areas (which I
didn't include in compilation) might be broken now.
In wayland_common.c the author of some option declarations confused the
flags parameter with the default value (though the default value was
also properly set below). I fixed this with this change.
2020-03-14 20:28:01 +00:00
|
|
|
{"webp-quality", OPT_INT(webp_quality), M_RANGE(0, 100)},
|
|
|
|
{"webp-compression", OPT_INT(webp_compression), M_RANGE(0, 6)},
|
2022-04-23 18:24:09 +00:00
|
|
|
{"jxl-distance", OPT_DOUBLE(jxl_distance), M_RANGE(0.0, 15.0)},
|
|
|
|
{"jxl-effort", OPT_INT(jxl_effort), M_RANGE(1, 9)},
|
2023-03-24 23:30:57 +00:00
|
|
|
{"avif-encoder", OPT_STRING(avif_encoder)},
|
|
|
|
{"avif-opts", OPT_KEYVALUELIST(avif_opts)},
|
|
|
|
{"avif-pixfmt", OPT_STRING(avif_pixfmt)},
|
2023-02-20 03:32:50 +00:00
|
|
|
{"high-bit-depth", OPT_BOOL(high_bit_depth)},
|
|
|
|
{"tag-colorspace", OPT_BOOL(tag_csp)},
|
2016-09-05 19:04:55 +00:00
|
|
|
{0},
|
2012-08-06 15:48:30 +00:00
|
|
|
};
|
|
|
|
|
2012-08-06 15:46:42 +00:00
|
|
|
struct image_writer_ctx {
|
2013-12-21 16:59:38 +00:00
|
|
|
struct mp_log *log;
|
2012-08-06 15:46:42 +00:00
|
|
|
const struct image_writer_opts *opts;
|
2015-04-09 19:01:34 +00:00
|
|
|
struct mp_imgfmt_desc original_format;
|
2012-08-06 15:46:42 +00:00
|
|
|
};
|
|
|
|
|
2017-04-02 15:21:42 +00:00
|
|
|
static enum AVPixelFormat replace_j_format(enum AVPixelFormat fmt)
|
|
|
|
{
|
|
|
|
switch (fmt) {
|
|
|
|
case AV_PIX_FMT_YUV420P: return AV_PIX_FMT_YUVJ420P;
|
|
|
|
case AV_PIX_FMT_YUV422P: return AV_PIX_FMT_YUVJ422P;
|
|
|
|
case AV_PIX_FMT_YUV444P: return AV_PIX_FMT_YUVJ444P;
|
|
|
|
}
|
|
|
|
return fmt;
|
|
|
|
}
|
|
|
|
|
2023-07-17 19:23:20 +00:00
|
|
|
static void prepare_avframe(AVFrame *pic, AVCodecContext *avctx,
|
|
|
|
mp_image_t *image, bool tag_csp,
|
|
|
|
struct mp_log *log)
|
|
|
|
{
|
|
|
|
for (int n = 0; n < 4; n++) {
|
|
|
|
pic->data[n] = image->planes[n];
|
|
|
|
pic->linesize[n] = image->stride[n];
|
|
|
|
}
|
|
|
|
pic->format = avctx->pix_fmt;
|
|
|
|
pic->width = avctx->width;
|
|
|
|
pic->height = avctx->height;
|
2024-02-04 09:24:42 +00:00
|
|
|
pl_avframe_set_repr(pic, image->params.repr);
|
|
|
|
avctx->colorspace = pic->colorspace;
|
|
|
|
avctx->color_range = pic->color_range;
|
2023-07-17 19:23:20 +00:00
|
|
|
|
|
|
|
if (!tag_csp)
|
|
|
|
return;
|
2024-02-04 09:24:42 +00:00
|
|
|
pl_avframe_set_color(pic, image->params.color);
|
|
|
|
avctx->color_primaries = pic->color_primaries;
|
|
|
|
avctx->color_trc = pic->color_trc;
|
2023-07-17 19:23:20 +00:00
|
|
|
avctx->chroma_sample_location = pic->chroma_location =
|
2023-11-04 04:15:27 +00:00
|
|
|
pl_chroma_to_av(image->params.chroma_location);
|
2024-02-04 09:24:42 +00:00
|
|
|
|
2023-07-17 19:23:20 +00:00
|
|
|
mp_dbg(log, "mapped color params:\n"
|
|
|
|
" trc = %s\n"
|
|
|
|
" primaries = %s\n"
|
|
|
|
" range = %s\n"
|
|
|
|
" colorspace = %s\n"
|
|
|
|
" chroma_location = %s\n",
|
|
|
|
av_color_transfer_name(avctx->color_trc),
|
|
|
|
av_color_primaries_name(avctx->color_primaries),
|
|
|
|
av_color_range_name(avctx->color_range),
|
|
|
|
av_color_space_name(avctx->colorspace),
|
|
|
|
av_chroma_location_name(avctx->chroma_sample_location)
|
|
|
|
);
|
|
|
|
}
|
2023-03-24 23:30:57 +00:00
|
|
|
|
2024-03-19 09:06:44 +00:00
|
|
|
static bool write_lavc(struct image_writer_ctx *ctx, mp_image_t *image, FILE *fp)
|
2012-08-06 15:46:42 +00:00
|
|
|
{
|
2021-12-13 15:12:31 +00:00
|
|
|
bool success = false;
|
2012-08-06 15:46:42 +00:00
|
|
|
AVFrame *pic = NULL;
|
2021-12-13 15:12:31 +00:00
|
|
|
AVPacket *pkt = NULL;
|
2012-08-06 15:46:42 +00:00
|
|
|
|
2021-05-01 19:53:56 +00:00
|
|
|
const AVCodec *codec;
|
2019-09-14 20:18:02 +00:00
|
|
|
if (ctx->opts->format == AV_CODEC_ID_WEBP) {
|
|
|
|
codec = avcodec_find_encoder_by_name("libwebp"); // non-animated encoder
|
|
|
|
} else {
|
|
|
|
codec = avcodec_find_encoder(ctx->opts->format);
|
|
|
|
}
|
|
|
|
|
2012-08-06 15:46:42 +00:00
|
|
|
AVCodecContext *avctx = NULL;
|
2012-08-06 15:49:37 +00:00
|
|
|
if (!codec)
|
2012-08-06 15:46:42 +00:00
|
|
|
goto print_open_fail;
|
2012-08-06 15:49:37 +00:00
|
|
|
avctx = avcodec_alloc_context3(codec);
|
2012-08-06 15:46:42 +00:00
|
|
|
if (!avctx)
|
|
|
|
goto print_open_fail;
|
|
|
|
|
|
|
|
avctx->time_base = AV_TIME_BASE_Q;
|
2012-11-21 18:40:33 +00:00
|
|
|
avctx->width = image->w;
|
|
|
|
avctx->height = image->h;
|
2012-08-06 15:49:37 +00:00
|
|
|
avctx->pix_fmt = imgfmt2pixfmt(image->imgfmt);
|
2019-09-14 20:18:02 +00:00
|
|
|
if (codec->id == AV_CODEC_ID_MJPEG) {
|
|
|
|
// Annoying deprecated garbage for the jpg encoder.
|
2023-11-04 02:55:38 +00:00
|
|
|
if (image->params.repr.levels == PL_COLOR_LEVELS_FULL)
|
2019-09-14 20:18:02 +00:00
|
|
|
avctx->pix_fmt = replace_j_format(avctx->pix_fmt);
|
|
|
|
}
|
2013-12-21 17:04:16 +00:00
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
|
|
|
|
MP_ERR(ctx, "Image format %s not supported by lavc.\n",
|
|
|
|
mp_imgfmt_to_name(image->imgfmt));
|
|
|
|
goto error_exit;
|
|
|
|
}
|
2023-06-29 10:27:30 +00:00
|
|
|
|
|
|
|
if (codec->id == AV_CODEC_ID_MJPEG) {
|
|
|
|
avctx->flags |= AV_CODEC_FLAG_QSCALE;
|
|
|
|
// jpeg_quality is set below
|
|
|
|
} else if (codec->id == AV_CODEC_ID_PNG) {
|
2012-08-06 15:49:37 +00:00
|
|
|
avctx->compression_level = ctx->opts->png_compression;
|
2016-02-09 12:24:04 +00:00
|
|
|
av_opt_set_int(avctx, "pred", ctx->opts->png_filter,
|
|
|
|
AV_OPT_SEARCH_CHILDREN);
|
2019-09-14 20:18:02 +00:00
|
|
|
} else if (codec->id == AV_CODEC_ID_WEBP) {
|
2019-08-02 14:04:54 +00:00
|
|
|
avctx->compression_level = ctx->opts->webp_compression;
|
2019-09-14 20:18:02 +00:00
|
|
|
av_opt_set_int(avctx, "lossless", ctx->opts->webp_lossless,
|
|
|
|
AV_OPT_SEARCH_CHILDREN);
|
|
|
|
av_opt_set_int(avctx, "quality", ctx->opts->webp_quality,
|
|
|
|
AV_OPT_SEARCH_CHILDREN);
|
2022-04-23 18:24:09 +00:00
|
|
|
} else if (codec->id == AV_CODEC_ID_JPEGXL) {
|
|
|
|
av_opt_set_double(avctx, "distance", ctx->opts->jxl_distance,
|
|
|
|
AV_OPT_SEARCH_CHILDREN);
|
|
|
|
av_opt_set_int(avctx, "effort", ctx->opts->jxl_effort,
|
|
|
|
AV_OPT_SEARCH_CHILDREN);
|
2013-06-15 13:14:06 +00:00
|
|
|
}
|
2012-08-06 15:46:42 +00:00
|
|
|
|
2012-08-06 15:49:37 +00:00
|
|
|
if (avcodec_open2(avctx, codec, NULL) < 0) {
|
2024-03-20 02:23:23 +00:00
|
|
|
print_open_fail:
|
2013-12-21 16:59:38 +00:00
|
|
|
MP_ERR(ctx, "Could not open libavcodec encoder for saving images\n");
|
2012-08-06 15:46:42 +00:00
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
2014-03-16 11:51:58 +00:00
|
|
|
pic = av_frame_alloc();
|
2012-08-06 15:46:42 +00:00
|
|
|
if (!pic)
|
|
|
|
goto error_exit;
|
2023-07-17 19:23:20 +00:00
|
|
|
prepare_avframe(pic, avctx, image, ctx->opts->tag_csp, ctx->log);
|
2023-06-29 10:27:30 +00:00
|
|
|
if (codec->id == AV_CODEC_ID_MJPEG) {
|
|
|
|
int qscale = 1 + (100 - ctx->opts->jpeg_quality) * 30 / 100;
|
|
|
|
pic->quality = qscale * FF_QP2LAMBDA;
|
|
|
|
}
|
2016-06-24 16:20:36 +00:00
|
|
|
|
|
|
|
int ret = avcodec_send_frame(avctx, pic);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error_exit;
|
2017-02-20 13:05:14 +00:00
|
|
|
ret = avcodec_send_frame(avctx, NULL); // send EOF
|
|
|
|
if (ret < 0)
|
|
|
|
goto error_exit;
|
2021-12-13 15:12:31 +00:00
|
|
|
pkt = av_packet_alloc();
|
|
|
|
if (!pkt)
|
|
|
|
goto error_exit;
|
|
|
|
ret = avcodec_receive_packet(avctx, pkt);
|
2016-06-24 16:20:36 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto error_exit;
|
2012-08-06 15:46:42 +00:00
|
|
|
|
2023-07-10 12:03:58 +00:00
|
|
|
success = fwrite(pkt->data, pkt->size, 1, fp) == 1;
|
2012-08-06 15:46:42 +00:00
|
|
|
|
|
|
|
error_exit:
|
2017-07-16 10:51:48 +00:00
|
|
|
avcodec_free_context(&avctx);
|
2014-03-16 11:51:58 +00:00
|
|
|
av_frame_free(&pic);
|
2021-12-13 15:12:31 +00:00
|
|
|
av_packet_free(&pkt);
|
2024-02-24 18:59:22 +00:00
|
|
|
return success;
|
2012-08-06 15:46:42 +00:00
|
|
|
}
|
|
|
|
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_JPEG
|
2012-08-06 15:46:42 +00:00
|
|
|
|
|
|
|
static void write_jpeg_error_exit(j_common_ptr cinfo)
|
|
|
|
{
|
2024-03-20 02:23:23 +00:00
|
|
|
// NOTE: do not write error message, too much effort to connect the libjpeg
|
|
|
|
// log callbacks with mplayer's log function mp_msp()
|
2012-08-06 15:46:42 +00:00
|
|
|
|
2024-03-20 02:23:23 +00:00
|
|
|
// Return control to the setjmp point
|
|
|
|
longjmp(*(jmp_buf*)cinfo->client_data, 1);
|
2012-08-06 15:46:42 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 09:06:44 +00:00
|
|
|
static bool write_jpeg(struct image_writer_ctx *ctx, mp_image_t *image, FILE *fp)
|
2012-08-06 15:46:42 +00:00
|
|
|
{
|
|
|
|
struct jpeg_compress_struct cinfo;
|
|
|
|
struct jpeg_error_mgr jerr;
|
|
|
|
|
|
|
|
cinfo.err = jpeg_std_error(&jerr);
|
|
|
|
jerr.error_exit = write_jpeg_error_exit;
|
|
|
|
|
|
|
|
jmp_buf error_return_jmpbuf;
|
|
|
|
cinfo.client_data = &error_return_jmpbuf;
|
|
|
|
if (setjmp(cinfo.client_data)) {
|
|
|
|
jpeg_destroy_compress(&cinfo);
|
2015-04-20 21:02:20 +00:00
|
|
|
return false;
|
2012-08-06 15:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jpeg_create_compress(&cinfo);
|
|
|
|
jpeg_stdio_dest(&cinfo, fp);
|
|
|
|
|
2012-11-21 18:40:33 +00:00
|
|
|
cinfo.image_width = image->w;
|
|
|
|
cinfo.image_height = image->h;
|
2012-08-06 15:46:42 +00:00
|
|
|
cinfo.input_components = 3;
|
|
|
|
cinfo.in_color_space = JCS_RGB;
|
|
|
|
|
2012-08-06 15:51:04 +00:00
|
|
|
cinfo.write_JFIF_header = TRUE;
|
|
|
|
cinfo.JFIF_major_version = 1;
|
|
|
|
cinfo.JFIF_minor_version = 2;
|
|
|
|
|
2012-08-06 15:46:42 +00:00
|
|
|
jpeg_set_defaults(&cinfo);
|
2015-08-01 23:10:18 +00:00
|
|
|
jpeg_set_quality(&cinfo, ctx->opts->jpeg_quality, 0);
|
2012-08-06 15:51:04 +00:00
|
|
|
|
2015-04-29 19:01:08 +00:00
|
|
|
if (ctx->opts->jpeg_source_chroma) {
|
|
|
|
cinfo.comp_info[0].h_samp_factor = 1 << ctx->original_format.chroma_xs;
|
|
|
|
cinfo.comp_info[0].v_samp_factor = 1 << ctx->original_format.chroma_ys;
|
|
|
|
}
|
2015-04-09 19:01:34 +00:00
|
|
|
|
2012-08-06 15:46:42 +00:00
|
|
|
jpeg_start_compress(&cinfo, TRUE);
|
|
|
|
|
|
|
|
while (cinfo.next_scanline < cinfo.image_height) {
|
|
|
|
JSAMPROW row_pointer[1];
|
|
|
|
row_pointer[0] = image->planes[0] +
|
2016-01-17 18:39:55 +00:00
|
|
|
(ptrdiff_t)cinfo.next_scanline * image->stride[0];
|
2024-03-20 03:05:37 +00:00
|
|
|
jpeg_write_scanlines(&cinfo, row_pointer, 1);
|
2012-08-06 15:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jpeg_finish_compress(&cinfo);
|
|
|
|
|
|
|
|
jpeg_destroy_compress(&cinfo);
|
|
|
|
|
2024-03-19 09:06:44 +00:00
|
|
|
return true;
|
2023-03-24 23:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void log_side_data(struct image_writer_ctx *ctx, AVPacketSideData *data,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
if (!mp_msg_test(ctx->log, MSGL_DEBUG))
|
|
|
|
return;
|
|
|
|
char dbgbuff[129];
|
|
|
|
if (size)
|
|
|
|
MP_DBG(ctx, "write_avif() packet side data:\n");
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
AVPacketSideData *sd = &data[i];
|
2023-07-17 19:30:17 +00:00
|
|
|
for (int k = 0; k < MPMIN(sd->size, 64); k++)
|
|
|
|
snprintf(dbgbuff + k*2, 3, "%02x", (int)sd->data[k]);
|
2023-03-24 23:30:57 +00:00
|
|
|
MP_DBG(ctx, " [%d] = {[%s], '%s'}\n",
|
|
|
|
i, av_packet_side_data_name(sd->type), dbgbuff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 09:06:44 +00:00
|
|
|
static bool write_avif(struct image_writer_ctx *ctx, mp_image_t *image, FILE *fp)
|
2023-03-24 23:30:57 +00:00
|
|
|
{
|
|
|
|
const AVCodec *codec = NULL;
|
|
|
|
const AVOutputFormat *ofmt = NULL;
|
|
|
|
AVCodecContext *avctx = NULL;
|
|
|
|
AVIOContext *avioctx = NULL;
|
|
|
|
AVFormatContext *fmtctx = NULL;
|
|
|
|
AVStream *stream = NULL;
|
|
|
|
AVFrame *pic = NULL;
|
|
|
|
AVPacket *pkt = NULL;
|
|
|
|
int ret;
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
codec = avcodec_find_encoder_by_name(ctx->opts->avif_encoder);
|
|
|
|
if (!codec) {
|
|
|
|
MP_ERR(ctx, "Could not find encoder '%s', for saving images\n",
|
|
|
|
ctx->opts->avif_encoder);
|
|
|
|
goto free_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
ofmt = av_guess_format("avif", NULL, NULL);
|
|
|
|
if (!ofmt) {
|
|
|
|
MP_ERR(ctx, "Could not guess output format 'avif'\n");
|
|
|
|
goto free_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
avctx = avcodec_alloc_context3(codec);
|
|
|
|
if (!avctx) {
|
|
|
|
MP_ERR(ctx, "Failed to allocate AVContext.\n");
|
|
|
|
goto free_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
avctx->width = image->w;
|
|
|
|
avctx->height = image->h;
|
|
|
|
avctx->time_base = (AVRational){1, 30};
|
|
|
|
avctx->pkt_timebase = (AVRational){1, 30};
|
|
|
|
avctx->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
avctx->pix_fmt = imgfmt2pixfmt(image->imgfmt);
|
2023-07-17 18:47:34 +00:00
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
|
|
|
|
MP_ERR(ctx, "Image format %s not supported by lavc.\n",
|
|
|
|
mp_imgfmt_to_name(image->imgfmt));
|
|
|
|
goto free_data;
|
|
|
|
}
|
2023-03-24 23:30:57 +00:00
|
|
|
|
2023-07-17 18:31:38 +00:00
|
|
|
av_opt_set_int(avctx, "still-picture", 1, AV_OPT_SEARCH_CHILDREN);
|
2023-03-24 23:30:57 +00:00
|
|
|
|
|
|
|
AVDictionary *avd = NULL;
|
|
|
|
mp_set_avdict(&avd, ctx->opts->avif_opts);
|
|
|
|
av_opt_set_dict2(avctx, &avd, AV_OPT_SEARCH_CHILDREN);
|
|
|
|
av_dict_free(&avd);
|
|
|
|
|
|
|
|
pic = av_frame_alloc();
|
|
|
|
if (!pic) {
|
|
|
|
MP_ERR(ctx, "Could not allocate AVFrame\n");
|
|
|
|
goto free_data;
|
|
|
|
}
|
|
|
|
|
2023-07-17 19:23:20 +00:00
|
|
|
prepare_avframe(pic, avctx, image, ctx->opts->tag_csp, ctx->log);
|
2023-03-24 23:30:57 +00:00
|
|
|
// Not setting this flag caused ffmpeg to output avif that was not passing
|
|
|
|
// standard checks but ffmpeg would still read and not complain...
|
|
|
|
avctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
|
|
|
|
|
|
|
|
ret = avcodec_open2(avctx, codec, NULL);
|
|
|
|
if (ret < 0) {
|
|
|
|
MP_ERR(ctx, "Could not open libavcodec encoder for saving images\n");
|
|
|
|
goto free_data;
|
|
|
|
}
|
|
|
|
|
2024-03-19 09:06:44 +00:00
|
|
|
avio_open_dyn_buf(&avioctx);
|
|
|
|
MP_HANDLE_OOM(avioctx);
|
2023-03-24 23:30:57 +00:00
|
|
|
|
|
|
|
fmtctx = avformat_alloc_context();
|
|
|
|
if (!fmtctx) {
|
|
|
|
MP_ERR(ctx, "Could not allocate format context\n");
|
|
|
|
goto free_data;
|
|
|
|
}
|
|
|
|
fmtctx->pb = avioctx;
|
|
|
|
fmtctx->oformat = ofmt;
|
|
|
|
|
|
|
|
stream = avformat_new_stream(fmtctx, codec);
|
|
|
|
if (!stream) {
|
|
|
|
MP_ERR(ctx, "Could not allocate stream\n");
|
|
|
|
goto free_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = avcodec_parameters_from_context(stream->codecpar, avctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
MP_ERR(ctx, "Could not copy parameters from context\n");
|
|
|
|
goto free_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = avformat_init_output(fmtctx, NULL);
|
|
|
|
if (ret < 0) {
|
|
|
|
MP_ERR(ctx, "Could not initialize output\n");
|
|
|
|
goto free_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = avformat_write_header(fmtctx, NULL);
|
|
|
|
if (ret < 0) {
|
|
|
|
MP_ERR(ctx, "Could not write format header\n");
|
|
|
|
goto free_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
pkt = av_packet_alloc();
|
|
|
|
if (!pkt) {
|
|
|
|
MP_ERR(ctx, "Could not allocate packet\n");
|
|
|
|
goto free_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = avcodec_send_frame(avctx, pic);
|
|
|
|
if (ret < 0) {
|
|
|
|
MP_ERR(ctx, "Error sending frame\n");
|
|
|
|
goto free_data;
|
|
|
|
}
|
2023-07-17 18:16:22 +00:00
|
|
|
ret = avcodec_send_frame(avctx, NULL); // send EOF
|
|
|
|
if (ret < 0)
|
|
|
|
goto free_data;
|
2023-03-24 23:30:57 +00:00
|
|
|
|
|
|
|
int pts = 0;
|
|
|
|
log_side_data(ctx, avctx->coded_side_data, avctx->nb_coded_side_data);
|
|
|
|
while (ret >= 0) {
|
|
|
|
ret = avcodec_receive_packet(avctx, pkt);
|
|
|
|
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
|
|
|
|
break;
|
|
|
|
if (ret < 0) {
|
|
|
|
MP_ERR(ctx, "Error receiving packet\n");
|
|
|
|
goto free_data;
|
|
|
|
}
|
2023-07-17 18:16:22 +00:00
|
|
|
pkt->dts = pkt->pts = ++pts;
|
2023-03-24 23:30:57 +00:00
|
|
|
pkt->stream_index = stream->index;
|
|
|
|
log_side_data(ctx, pkt->side_data, pkt->side_data_elems);
|
|
|
|
|
|
|
|
ret = av_write_frame(fmtctx, pkt);
|
|
|
|
if (ret < 0) {
|
|
|
|
MP_ERR(ctx, "Error writing frame\n");
|
|
|
|
goto free_data;
|
|
|
|
}
|
|
|
|
av_packet_unref(pkt);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = av_write_trailer(fmtctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
MP_ERR(ctx, "Could not write trailer\n");
|
|
|
|
goto free_data;
|
|
|
|
}
|
2023-07-20 18:02:14 +00:00
|
|
|
MP_DBG(ctx, "write_avif(): avio_size() = %"PRIi64"\n", avio_size(avioctx));
|
2023-03-24 23:30:57 +00:00
|
|
|
|
2024-03-19 09:06:44 +00:00
|
|
|
uint8_t *buf = NULL;
|
|
|
|
int written_size = avio_close_dyn_buf(avioctx, &buf);
|
|
|
|
success = fwrite(buf, written_size, 1, fp) == 1;
|
|
|
|
av_freep(&buf);
|
2023-03-24 23:30:57 +00:00
|
|
|
|
|
|
|
free_data:
|
|
|
|
avformat_free_context(fmtctx);
|
|
|
|
avcodec_free_context(&avctx);
|
|
|
|
av_packet_free(&pkt);
|
|
|
|
av_frame_free(&pic);
|
|
|
|
|
|
|
|
return success;
|
2012-08-06 15:46:42 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 19:53:56 +00:00
|
|
|
static int get_encoder_format(const AVCodec *codec, int srcfmt, bool highdepth)
|
2015-04-29 19:05:04 +00:00
|
|
|
{
|
|
|
|
const enum AVPixelFormat *pix_fmts = codec->pix_fmts;
|
|
|
|
int current = 0;
|
|
|
|
for (int n = 0; pix_fmts && pix_fmts[n] != AV_PIX_FMT_NONE; n++) {
|
|
|
|
int fmt = pixfmt2imgfmt(pix_fmts[n]);
|
|
|
|
if (!fmt)
|
|
|
|
continue;
|
2020-05-17 12:57:13 +00:00
|
|
|
if (!highdepth) {
|
|
|
|
// Ignore formats larger than 8 bit per pixel. (Or which are unknown.)
|
|
|
|
struct mp_regular_imgfmt rdesc;
|
|
|
|
if (!mp_get_regular_imgfmt(&rdesc, fmt)) {
|
|
|
|
int ofmt = mp_find_other_endian(fmt);
|
|
|
|
if (!mp_get_regular_imgfmt(&rdesc, ofmt))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (rdesc.component_size > 1)
|
|
|
|
continue;
|
|
|
|
}
|
2015-04-29 19:05:04 +00:00
|
|
|
current = current ? mp_imgfmt_select_best(current, fmt, srcfmt) : fmt;
|
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2017-03-18 14:03:05 +00:00
|
|
|
static int get_target_format(struct image_writer_ctx *ctx)
|
2015-04-06 18:34:46 +00:00
|
|
|
{
|
2021-05-01 19:53:56 +00:00
|
|
|
const AVCodec *codec = avcodec_find_encoder(ctx->opts->format);
|
2015-04-06 18:34:46 +00:00
|
|
|
if (!codec)
|
|
|
|
goto unknown;
|
|
|
|
|
2017-03-18 14:03:05 +00:00
|
|
|
int srcfmt = ctx->original_format.id;
|
|
|
|
|
2015-04-29 19:05:04 +00:00
|
|
|
int target = get_encoder_format(codec, srcfmt, ctx->opts->high_bit_depth);
|
2023-11-06 12:36:31 +00:00
|
|
|
if (!target) {
|
|
|
|
mp_dbg(ctx->log, "Falling back to high-depth format.\n");
|
2015-04-29 19:05:04 +00:00
|
|
|
target = get_encoder_format(codec, srcfmt, true);
|
2023-11-06 12:36:31 +00:00
|
|
|
}
|
2015-04-06 18:34:46 +00:00
|
|
|
|
2015-04-29 19:05:04 +00:00
|
|
|
if (!target)
|
2015-04-06 18:34:46 +00:00
|
|
|
goto unknown;
|
|
|
|
|
2015-04-29 19:05:04 +00:00
|
|
|
return target;
|
2015-04-06 18:34:46 +00:00
|
|
|
|
|
|
|
unknown:
|
2017-03-18 14:03:05 +00:00
|
|
|
return IMGFMT_RGB0;
|
2012-08-06 15:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *image_writer_file_ext(const struct image_writer_opts *opts)
|
|
|
|
{
|
|
|
|
struct image_writer_opts defs = image_writer_opts_defaults;
|
|
|
|
|
|
|
|
if (!opts)
|
|
|
|
opts = &defs;
|
|
|
|
|
2017-03-18 14:03:05 +00:00
|
|
|
return m_opt_choice_str(mp_image_writer_formats, opts->format);
|
|
|
|
}
|
|
|
|
|
2018-02-07 19:18:36 +00:00
|
|
|
bool image_writer_high_depth(const struct image_writer_opts *opts)
|
|
|
|
{
|
2022-04-29 03:59:45 +00:00
|
|
|
return opts->format == AV_CODEC_ID_PNG
|
|
|
|
|| opts->format == AV_CODEC_ID_JPEGXL
|
2024-06-21 16:35:11 +00:00
|
|
|
|| opts->format == AV_CODEC_ID_AV1;
|
2018-02-07 19:18:36 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 19:59:46 +00:00
|
|
|
bool image_writer_flexible_csp(const struct image_writer_opts *opts)
|
|
|
|
{
|
2023-07-17 21:16:46 +00:00
|
|
|
if (!opts->tag_csp)
|
|
|
|
return false;
|
2024-06-21 16:35:11 +00:00
|
|
|
return opts->format == AV_CODEC_ID_JPEGXL
|
2023-03-24 23:30:57 +00:00
|
|
|
|| opts->format == AV_CODEC_ID_AV1
|
2024-06-21 16:35:11 +00:00
|
|
|
|| opts->format == AV_CODEC_ID_PNG;
|
2023-02-13 19:59:46 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 14:03:05 +00:00
|
|
|
int image_writer_format_from_ext(const char *ext)
|
|
|
|
{
|
|
|
|
for (int n = 0; mp_image_writer_formats[n].name; n++) {
|
|
|
|
if (ext && strcmp(mp_image_writer_formats[n].name, ext) == 0)
|
|
|
|
return mp_image_writer_formats[n].value;
|
|
|
|
}
|
|
|
|
return 0;
|
2012-08-06 15:46:42 +00:00
|
|
|
}
|
|
|
|
|
2019-09-14 20:06:00 +00:00
|
|
|
static struct mp_image *convert_image(struct mp_image *image, int destfmt,
|
2023-11-04 02:55:38 +00:00
|
|
|
enum pl_color_levels yuv_levels,
|
2022-12-12 21:01:57 +00:00
|
|
|
const struct image_writer_opts *opts,
|
2019-10-31 14:44:09 +00:00
|
|
|
struct mpv_global *global,
|
2019-09-14 20:06:00 +00:00
|
|
|
struct mp_log *log)
|
2012-08-06 15:46:42 +00:00
|
|
|
{
|
2015-12-19 19:04:31 +00:00
|
|
|
int d_w, d_h;
|
|
|
|
mp_image_params_get_dsize(&image->params, &d_w, &d_h);
|
2012-08-06 15:46:42 +00:00
|
|
|
|
2017-04-02 15:21:42 +00:00
|
|
|
struct mp_image_params p = {
|
|
|
|
.imgfmt = destfmt,
|
|
|
|
.w = d_w,
|
|
|
|
.h = d_h,
|
|
|
|
.p_w = 1,
|
|
|
|
.p_h = 1,
|
2023-01-25 17:13:28 +00:00
|
|
|
.color = image->params.color,
|
2023-11-04 02:55:38 +00:00
|
|
|
.repr = image->params.repr,
|
2023-12-03 17:29:35 +00:00
|
|
|
.chroma_location = image->params.chroma_location,
|
|
|
|
.crop = {0, 0, d_w, d_h},
|
2017-04-02 15:21:42 +00:00
|
|
|
};
|
|
|
|
mp_image_params_guess_csp(&p);
|
|
|
|
|
2022-12-12 21:01:57 +00:00
|
|
|
if (!image_writer_flexible_csp(opts)) {
|
2023-07-24 21:45:31 +00:00
|
|
|
// If our format can't tag csps, set something sane
|
2023-11-04 02:55:38 +00:00
|
|
|
p.color.primaries = PL_COLOR_PRIM_BT_709;
|
|
|
|
p.color.transfer = PL_COLOR_TRC_UNKNOWN;
|
|
|
|
p.light = MP_CSP_LIGHT_DISPLAY;
|
2023-11-03 23:52:14 +00:00
|
|
|
p.color.hdr = (struct pl_hdr_metadata){0};
|
2023-11-04 02:55:38 +00:00
|
|
|
if (p.repr.sys != PL_COLOR_SYSTEM_RGB) {
|
|
|
|
p.repr.levels = yuv_levels;
|
|
|
|
p.repr.sys = PL_COLOR_SYSTEM_BT_601;
|
2023-11-04 04:15:27 +00:00
|
|
|
p.chroma_location = PL_CHROMA_CENTER;
|
2022-12-12 21:01:57 +00:00
|
|
|
}
|
2017-04-02 15:21:42 +00:00
|
|
|
mp_image_params_guess_csp(&p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mp_image_params_equal(&p, &image->params))
|
2015-04-20 21:04:34 +00:00
|
|
|
return mp_image_new_ref(image);
|
|
|
|
|
2023-12-03 17:28:32 +00:00
|
|
|
mp_verbose(log, "will convert image to %s\n", mp_imgfmt_to_name(p.imgfmt));
|
2023-07-24 21:45:18 +00:00
|
|
|
|
2023-09-08 19:35:20 +00:00
|
|
|
struct mp_image *src = image;
|
|
|
|
if (mp_image_crop_valid(&src->params) &&
|
|
|
|
(mp_rect_w(src->params.crop) != src->w ||
|
|
|
|
mp_rect_h(src->params.crop) != src->h))
|
|
|
|
{
|
|
|
|
src = mp_image_new_ref(src);
|
|
|
|
if (!src) {
|
|
|
|
mp_err(log, "mp_image_new_ref failed!\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
mp_image_crop_rc(src, src->params.crop);
|
|
|
|
}
|
|
|
|
|
2017-04-02 15:21:42 +00:00
|
|
|
struct mp_image *dst = mp_image_alloc(p.imgfmt, p.w, p.h);
|
2015-04-20 21:04:34 +00:00
|
|
|
if (!dst) {
|
|
|
|
mp_err(log, "Out of memory.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-09-08 19:35:20 +00:00
|
|
|
mp_image_copy_attributes(dst, src);
|
2015-04-20 21:04:34 +00:00
|
|
|
|
2017-04-02 15:21:42 +00:00
|
|
|
dst->params = p;
|
|
|
|
|
2019-10-31 14:44:09 +00:00
|
|
|
struct mp_sws_context *sws = mp_sws_alloc(NULL);
|
|
|
|
sws->log = log;
|
|
|
|
if (global)
|
|
|
|
mp_sws_enable_cmdline_opts(sws, global);
|
2023-09-08 19:35:20 +00:00
|
|
|
bool ok = mp_sws_scale(sws, dst, src) >= 0;
|
2019-10-31 14:44:09 +00:00
|
|
|
talloc_free(sws);
|
|
|
|
|
2023-09-08 19:35:20 +00:00
|
|
|
if (src != image)
|
|
|
|
talloc_free(src);
|
|
|
|
|
2019-10-31 14:44:09 +00:00
|
|
|
if (!ok) {
|
2015-04-20 21:04:34 +00:00
|
|
|
mp_err(log, "Error when converting image.\n");
|
|
|
|
talloc_free(dst);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool write_image(struct mp_image *image, const struct image_writer_opts *opts,
|
2023-03-24 23:30:57 +00:00
|
|
|
const char *filename, struct mpv_global *global,
|
2024-03-19 04:38:58 +00:00
|
|
|
struct mp_log *log, bool overwrite)
|
2015-04-20 21:04:34 +00:00
|
|
|
{
|
|
|
|
struct image_writer_opts defs = image_writer_opts_defaults;
|
2012-08-06 15:46:42 +00:00
|
|
|
if (!opts)
|
|
|
|
opts = &defs;
|
|
|
|
|
2023-12-03 17:28:32 +00:00
|
|
|
mp_verbose(log, "input: %s\n", mp_image_params_to_str(&image->params));
|
2023-11-06 12:36:31 +00:00
|
|
|
|
2017-03-18 14:03:05 +00:00
|
|
|
struct image_writer_ctx ctx = { log, opts, image->fmt };
|
2024-03-19 09:06:44 +00:00
|
|
|
bool (*write)(struct image_writer_ctx *, mp_image_t *, FILE *) = write_lavc;
|
2017-03-18 14:03:05 +00:00
|
|
|
int destfmt = 0;
|
|
|
|
|
|
|
|
#if HAVE_JPEG
|
|
|
|
if (opts->format == AV_CODEC_ID_MJPEG) {
|
|
|
|
write = write_jpeg;
|
|
|
|
destfmt = IMGFMT_RGB24;
|
|
|
|
}
|
2023-03-24 23:30:57 +00:00
|
|
|
#endif
|
|
|
|
if (opts->format == AV_CODEC_ID_AV1) {
|
|
|
|
write = write_avif;
|
2023-12-03 17:25:09 +00:00
|
|
|
if (opts->avif_pixfmt && opts->avif_pixfmt[0])
|
|
|
|
destfmt = mp_imgfmt_from_name(bstr0(opts->avif_pixfmt));
|
2024-06-21 16:35:11 +00:00
|
|
|
} else if (opts->format == AV_CODEC_ID_WEBP && !opts->webp_lossless) {
|
2019-09-14 20:18:02 +00:00
|
|
|
// For lossy images, libwebp has its own RGB->YUV conversion.
|
|
|
|
// We don't want that, so force YUV/YUVA here.
|
|
|
|
int alpha = image->fmt.flags & MP_IMGFLAG_ALPHA;
|
|
|
|
destfmt = alpha ? pixfmt2imgfmt(AV_PIX_FMT_YUVA420P) : IMGFMT_420P;
|
|
|
|
}
|
2017-03-18 14:03:05 +00:00
|
|
|
|
|
|
|
if (!destfmt)
|
|
|
|
destfmt = get_target_format(&ctx);
|
2012-08-06 15:46:42 +00:00
|
|
|
|
2023-11-04 02:55:38 +00:00
|
|
|
enum pl_color_levels levels; // Ignored if destfmt is a RGB format
|
2019-09-14 20:18:02 +00:00
|
|
|
if (opts->format == AV_CODEC_ID_WEBP) {
|
2023-11-04 02:55:38 +00:00
|
|
|
levels = PL_COLOR_LEVELS_LIMITED;
|
2019-09-14 20:18:02 +00:00
|
|
|
} else {
|
2023-11-04 02:55:38 +00:00
|
|
|
levels = PL_COLOR_LEVELS_FULL;
|
2019-09-14 20:18:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 21:01:57 +00:00
|
|
|
struct mp_image *dst = convert_image(image, destfmt, levels, opts, global, log);
|
2015-04-20 21:04:34 +00:00
|
|
|
if (!dst)
|
|
|
|
return false;
|
2012-08-06 15:46:42 +00:00
|
|
|
|
2024-03-19 09:06:44 +00:00
|
|
|
bool success = false;
|
|
|
|
FILE *fp = fopen(filename, overwrite ? "wb" : "wbx");
|
|
|
|
if (!fp) {
|
|
|
|
mp_err(log, "Error creating '%s' for writing: %s!\n",
|
|
|
|
filename, mp_strerror(errno));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
success = write(&ctx, dst, fp);
|
|
|
|
if (fclose(fp) || !success) {
|
2023-03-24 23:30:57 +00:00
|
|
|
mp_err(log, "Error writing file '%s'!\n", filename);
|
2024-03-19 09:06:44 +00:00
|
|
|
unlink(filename);
|
|
|
|
}
|
2012-08-06 15:46:42 +00:00
|
|
|
|
2024-03-19 09:06:44 +00:00
|
|
|
done:
|
2015-04-20 21:04:34 +00:00
|
|
|
talloc_free(dst);
|
2012-08-06 15:46:42 +00:00
|
|
|
return success;
|
|
|
|
}
|
2012-10-27 15:30:26 +00:00
|
|
|
|
2013-12-21 16:59:38 +00:00
|
|
|
void dump_png(struct mp_image *image, const char *filename, struct mp_log *log)
|
2012-10-27 15:30:26 +00:00
|
|
|
{
|
|
|
|
struct image_writer_opts opts = image_writer_opts_defaults;
|
2017-03-18 14:03:05 +00:00
|
|
|
opts.format = AV_CODEC_ID_PNG;
|
2024-03-19 04:38:58 +00:00
|
|
|
write_image(image, &opts, filename, NULL, log, true);
|
2012-10-27 15:30:26 +00:00
|
|
|
}
|