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>
|
|
|
|
#include <setjmp.h>
|
|
|
|
|
|
|
|
#include <libavcodec/avcodec.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>
|
2012-08-06 15:46:42 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_JPEG
|
2012-08-06 15:46:42 +00:00
|
|
|
#include <jpeglib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "osdep/io.h"
|
|
|
|
|
|
|
|
#include "image_writer.h"
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_talloc.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "video/img_format.h"
|
|
|
|
#include "video/mp_image.h"
|
|
|
|
#include "video/fmt-conversion.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,
|
2015-04-29 19:05:04 +00:00
|
|
|
.high_bit_depth = 1,
|
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,
|
2015-04-29 19:01:08 +00:00
|
|
|
.jpeg_source_chroma = 1,
|
2015-07-18 16:33:54 +00:00
|
|
|
.tag_csp = 0,
|
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},
|
|
|
|
{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[] = {
|
2017-03-18 14:03:05 +00:00
|
|
|
OPT_CHOICE_C("format", format, 0, mp_image_writer_formats),
|
2016-09-05 19:04:55 +00:00
|
|
|
OPT_INTRANGE("jpeg-quality", jpeg_quality, 0, 0, 100),
|
|
|
|
OPT_FLAG("jpeg-source-chroma", jpeg_source_chroma, 0),
|
|
|
|
OPT_INTRANGE("png-compression", png_compression, 0, 0, 9),
|
|
|
|
OPT_INTRANGE("png-filter", png_filter, 0, 0, 5),
|
|
|
|
OPT_FLAG("high-bit-depth", high_bit_depth, 0),
|
|
|
|
OPT_FLAG("tag-colorspace", tag_csp, 0),
|
|
|
|
{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;
|
|
|
|
}
|
|
|
|
|
2015-04-20 21:02:20 +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
|
|
|
{
|
2015-04-20 21:02:20 +00:00
|
|
|
bool success = 0;
|
2012-08-06 15:46:42 +00:00
|
|
|
AVFrame *pic = NULL;
|
2013-03-09 06:01:43 +00:00
|
|
|
AVPacket pkt = {0};
|
|
|
|
int got_output = 0;
|
|
|
|
|
|
|
|
av_init_packet(&pkt);
|
2012-08-06 15:46:42 +00:00
|
|
|
|
2017-03-18 14:03:05 +00:00
|
|
|
struct AVCodec *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;
|
2017-04-02 15:21:42 +00:00
|
|
|
avctx->color_range = mp_csp_levels_to_avcol_range(image->params.color.levels);
|
2012-08-06 15:49:37 +00:00
|
|
|
avctx->pix_fmt = imgfmt2pixfmt(image->imgfmt);
|
2017-04-02 15:21:42 +00:00
|
|
|
// Annoying deprecated garbage for the jpg encoder.
|
|
|
|
if (image->params.color.levels == MP_CSP_LEVELS_PC)
|
|
|
|
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;
|
|
|
|
}
|
2017-03-18 14:03:05 +00:00
|
|
|
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);
|
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) {
|
2012-08-06 15:46:42 +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;
|
|
|
|
for (int n = 0; n < 4; n++) {
|
|
|
|
pic->data[n] = image->planes[n];
|
|
|
|
pic->linesize[n] = image->stride[n];
|
|
|
|
}
|
2014-12-28 13:05:28 +00:00
|
|
|
pic->format = avctx->pix_fmt;
|
|
|
|
pic->width = avctx->width;
|
|
|
|
pic->height = avctx->height;
|
2017-04-02 15:21:42 +00:00
|
|
|
pic->color_range = avctx->color_range;
|
Revert "Revert recent vo_opengl related commits"
Omitted a simple, but devastasting check. Fixed the relevant commits
now.
This reverts commit 8d24e9d9b8ad1b5d82139980eca148dc0f4a1eab.
diff --git a/video/out/gl_video.c b/video/out/gl_video.c
index 9c8a643..f1ea03e 100644
--- a/video/out/gl_video.c
+++ b/video/out/gl_video.c
@@ -1034,9 +1034,9 @@ static void compile_shaders(struct gl_video *p)
shader_def_opt(&header_conv, "USE_CONV_GAMMA", use_conv_gamma);
shader_def_opt(&header_conv, "USE_CONST_LUMA", use_const_luma);
shader_def_opt(&header_conv, "USE_LINEAR_LIGHT_BT1886",
- gamma_fun == MP_CSP_TRC_BT_1886);
+ use_linear_light && gamma_fun == MP_CSP_TRC_BT_1886);
shader_def_opt(&header_conv, "USE_LINEAR_LIGHT_SRGB",
- gamma_fun == MP_CSP_TRC_SRGB);
+ use_linear_light && gamma_fun == MP_CSP_TRC_SRGB);
shader_def_opt(&header_conv, "USE_SIGMOID", use_sigmoid);
if (p->opts.alpha_mode > 0 && p->has_alpha && p->plane_count > 3)
shader_def(&header_conv, "USE_ALPHA_PLANE", "3");
2015-02-28 19:15:12 +00:00
|
|
|
if (ctx->opts->tag_csp) {
|
2016-06-29 07:16:13 +00:00
|
|
|
pic->color_primaries = mp_csp_prim_to_avcol_pri(image->params.color.primaries);
|
|
|
|
pic->color_trc = mp_csp_trc_to_avcol_trc(image->params.color.gamma);
|
Revert "Revert recent vo_opengl related commits"
Omitted a simple, but devastasting check. Fixed the relevant commits
now.
This reverts commit 8d24e9d9b8ad1b5d82139980eca148dc0f4a1eab.
diff --git a/video/out/gl_video.c b/video/out/gl_video.c
index 9c8a643..f1ea03e 100644
--- a/video/out/gl_video.c
+++ b/video/out/gl_video.c
@@ -1034,9 +1034,9 @@ static void compile_shaders(struct gl_video *p)
shader_def_opt(&header_conv, "USE_CONV_GAMMA", use_conv_gamma);
shader_def_opt(&header_conv, "USE_CONST_LUMA", use_const_luma);
shader_def_opt(&header_conv, "USE_LINEAR_LIGHT_BT1886",
- gamma_fun == MP_CSP_TRC_BT_1886);
+ use_linear_light && gamma_fun == MP_CSP_TRC_BT_1886);
shader_def_opt(&header_conv, "USE_LINEAR_LIGHT_SRGB",
- gamma_fun == MP_CSP_TRC_SRGB);
+ use_linear_light && gamma_fun == MP_CSP_TRC_SRGB);
shader_def_opt(&header_conv, "USE_SIGMOID", use_sigmoid);
if (p->opts.alpha_mode > 0 && p->has_alpha && p->plane_count > 3)
shader_def(&header_conv, "USE_ALPHA_PLANE", "3");
2015-02-28 19:15:12 +00:00
|
|
|
}
|
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;
|
2016-06-24 16:20:36 +00:00
|
|
|
ret = avcodec_receive_packet(avctx, &pkt);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error_exit;
|
|
|
|
got_output = 1;
|
2012-08-06 15:46:42 +00:00
|
|
|
|
2013-03-09 06:01:43 +00:00
|
|
|
fwrite(pkt.data, pkt.size, 1, fp);
|
2012-08-06 15:46:42 +00:00
|
|
|
|
2013-03-09 06:01:43 +00:00
|
|
|
success = !!got_output;
|
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);
|
2015-10-28 22:48:56 +00:00
|
|
|
av_packet_unref(&pkt);
|
2012-08-06 15:46:42 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
// NOTE: do not write error message, too much effort to connect the libjpeg
|
|
|
|
// log callbacks with mplayer's log function mp_msp()
|
|
|
|
|
|
|
|
// Return control to the setjmp point
|
|
|
|
longjmp(*(jmp_buf*)cinfo->client_data, 1);
|
|
|
|
}
|
|
|
|
|
2015-04-20 21:02:20 +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];
|
2012-08-06 15:46:42 +00:00
|
|
|
jpeg_write_scanlines(&cinfo, row_pointer,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
jpeg_finish_compress(&cinfo);
|
|
|
|
|
|
|
|
jpeg_destroy_compress(&cinfo);
|
|
|
|
|
2015-04-20 21:02:20 +00:00
|
|
|
return true;
|
2012-08-06 15:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-04-29 19:05:04 +00:00
|
|
|
static int get_encoder_format(struct AVCodec *codec, int srcfmt, bool highdepth)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
// Ignore formats larger than 8 bit per pixel.
|
|
|
|
if (!highdepth && IMGFMT_RGB_DEPTH(fmt) > 32)
|
|
|
|
continue;
|
|
|
|
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
|
|
|
{
|
2017-03-18 14:03:05 +00:00
|
|
|
struct 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);
|
|
|
|
if (!target)
|
|
|
|
target = get_encoder_format(codec, srcfmt, true);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-04-20 21:04:34 +00:00
|
|
|
struct mp_image *convert_image(struct mp_image *image, int destfmt,
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
mp_image_params_guess_csp(&p);
|
|
|
|
|
|
|
|
// If RGB, just assume everything is correct.
|
|
|
|
if (p.color.space != MP_CSP_RGB) {
|
|
|
|
// Currently, assume what FFmpeg's jpg encoder needs.
|
|
|
|
// Of course this works only for non-HDR (no HDR support in libswscale).
|
|
|
|
p.color.levels = MP_CSP_LEVELS_PC;
|
|
|
|
p.color.space = MP_CSP_BT_601;
|
|
|
|
p.chroma_location = MP_CHROMA_CENTER;
|
|
|
|
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);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
mp_image_copy_attributes(dst, image);
|
|
|
|
|
2017-04-02 15:21:42 +00:00
|
|
|
dst->params = p;
|
|
|
|
|
2015-04-20 21:04:34 +00:00
|
|
|
if (mp_image_swscale(dst, image, mp_sws_hq_flags) < 0) {
|
|
|
|
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,
|
|
|
|
const char *filename, struct mp_log *log)
|
|
|
|
{
|
|
|
|
struct image_writer_opts defs = image_writer_opts_defaults;
|
2012-08-06 15:46:42 +00:00
|
|
|
if (!opts)
|
|
|
|
opts = &defs;
|
|
|
|
|
2017-03-18 14:03:05 +00:00
|
|
|
struct image_writer_ctx ctx = { log, opts, image->fmt };
|
|
|
|
bool (*write)(struct image_writer_ctx *, mp_image_t *, FILE *) = write_lavc;
|
|
|
|
int destfmt = 0;
|
|
|
|
|
|
|
|
#if HAVE_JPEG
|
|
|
|
if (opts->format == AV_CODEC_ID_MJPEG) {
|
|
|
|
write = write_jpeg;
|
|
|
|
destfmt = IMGFMT_RGB24;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!destfmt)
|
|
|
|
destfmt = get_target_format(&ctx);
|
2012-08-06 15:46:42 +00:00
|
|
|
|
2015-04-20 21:04:34 +00:00
|
|
|
struct mp_image *dst = convert_image(image, destfmt, log);
|
|
|
|
if (!dst)
|
|
|
|
return false;
|
2012-08-06 15:46:42 +00:00
|
|
|
|
|
|
|
FILE *fp = fopen(filename, "wb");
|
2015-04-20 21:02:20 +00:00
|
|
|
bool success = false;
|
2012-08-06 15:46:42 +00:00
|
|
|
if (fp == NULL) {
|
2013-12-21 16:59:38 +00:00
|
|
|
mp_err(log, "Error opening '%s' for writing!\n", filename);
|
2012-08-06 15:46:42 +00:00
|
|
|
} else {
|
2017-03-18 14:03:05 +00:00
|
|
|
success = write(&ctx, dst, fp);
|
2012-08-06 15:46:42 +00:00
|
|
|
success = !fclose(fp) && success;
|
|
|
|
if (!success)
|
2013-12-21 16:59:38 +00:00
|
|
|
mp_err(log, "Error writing file '%s'!\n", filename);
|
2012-08-06 15:46:42 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2013-12-21 16:59:38 +00:00
|
|
|
write_image(image, &opts, filename, log);
|
2012-10-27 15:30:26 +00:00
|
|
|
}
|