2012-08-06 15:46:42 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mplayer.
|
|
|
|
*
|
|
|
|
* mplayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mplayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with mplayer. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
2012-08-06 15:46:42 +00:00
|
|
|
|
2012-11-10 15:19:45 +00:00
|
|
|
#include "compat/libav.h"
|
|
|
|
|
2012-08-06 15:46:42 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_JPEG
|
|
|
|
#include <jpeglib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "osdep/io.h"
|
|
|
|
|
|
|
|
#include "image_writer.h"
|
|
|
|
#include "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-08-06 20:41:30 +00:00
|
|
|
#include "mpvcore/m_option.h"
|
2012-08-06 15:46:42 +00:00
|
|
|
|
|
|
|
const struct image_writer_opts image_writer_opts_defaults = {
|
2012-08-15 20:38:43 +00:00
|
|
|
.format = "jpg",
|
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,
|
2012-08-06 15:51:04 +00:00
|
|
|
.jpeg_optimize = 100,
|
|
|
|
.jpeg_smooth = 0,
|
|
|
|
.jpeg_dpi = 72,
|
|
|
|
.jpeg_progressive = 0,
|
|
|
|
.jpeg_baseline = 1,
|
2012-08-06 15:46:42 +00:00
|
|
|
};
|
|
|
|
|
2012-08-06 15:48:30 +00:00
|
|
|
#define OPT_BASE_STRUCT struct image_writer_opts
|
|
|
|
|
|
|
|
const struct m_sub_options image_writer_conf = {
|
|
|
|
.opts = (m_option_t[]) {
|
|
|
|
OPT_INTRANGE("jpeg-quality", jpeg_quality, 0, 0, 100),
|
2012-08-06 15:51:04 +00:00
|
|
|
OPT_INTRANGE("jpeg-optimize", jpeg_optimize, 0, 0, 100),
|
|
|
|
OPT_INTRANGE("jpeg-smooth", jpeg_smooth, 0, 0, 100),
|
|
|
|
OPT_INTRANGE("jpeg-dpi", jpeg_dpi, M_OPT_MIN, 1, 99999),
|
2013-02-08 20:09:18 +00:00
|
|
|
OPT_FLAG("jpeg-progressive", jpeg_progressive, 0),
|
|
|
|
OPT_FLAG("jpeg-baseline", jpeg_baseline, 0),
|
2012-08-06 15:48:30 +00:00
|
|
|
OPT_INTRANGE("png-compression", png_compression, 0, 0, 9),
|
2013-06-15 13:14:06 +00:00
|
|
|
OPT_INTRANGE("png-filter", png_filter, 0, 0, 5),
|
2012-08-06 17:05:32 +00:00
|
|
|
OPT_STRING("format", format, 0),
|
2012-08-06 15:48:30 +00:00
|
|
|
{0},
|
|
|
|
},
|
|
|
|
.size = sizeof(struct image_writer_opts),
|
|
|
|
.defaults = &image_writer_opts_defaults,
|
|
|
|
};
|
|
|
|
|
2012-08-06 15:46:42 +00:00
|
|
|
struct image_writer_ctx {
|
|
|
|
const struct image_writer_opts *opts;
|
|
|
|
const struct img_writer *writer;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct img_writer {
|
|
|
|
const char *file_ext;
|
|
|
|
int (*write)(struct image_writer_ctx *ctx, mp_image_t *image, FILE *fp);
|
2012-08-06 15:47:28 +00:00
|
|
|
int *pixfmts;
|
2012-08-06 15:49:37 +00:00
|
|
|
int lavc_codec;
|
2012-08-06 15:46:42 +00:00
|
|
|
};
|
|
|
|
|
2012-08-06 15:49:37 +00:00
|
|
|
static int write_lavc(struct image_writer_ctx *ctx, mp_image_t *image, FILE *fp)
|
2012-08-06 15:46:42 +00:00
|
|
|
{
|
|
|
|
int success = 0;
|
|
|
|
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
|
|
|
|
2012-08-06 15:49:37 +00:00
|
|
|
struct AVCodec *codec = avcodec_find_encoder(ctx->writer->lavc_codec);
|
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);
|
2013-06-15 13:14:06 +00:00
|
|
|
if (ctx->writer->lavc_codec == AV_CODEC_ID_PNG) {
|
2012-08-06 15:49:37 +00:00
|
|
|
avctx->compression_level = ctx->opts->png_compression;
|
2013-06-15 13:14:06 +00:00
|
|
|
avctx->prediction_method = ctx->opts->png_filter;
|
|
|
|
}
|
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:
|
2012-08-06 15:49:37 +00:00
|
|
|
mp_msg(MSGT_CPLAYER, MSGL_INFO, "Could not open libavcodec encoder"
|
2012-08-06 15:46:42 +00:00
|
|
|
" for saving images\n");
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
pic = avcodec_alloc_frame();
|
|
|
|
if (!pic)
|
|
|
|
goto error_exit;
|
|
|
|
avcodec_get_frame_defaults(pic);
|
|
|
|
for (int n = 0; n < 4; n++) {
|
|
|
|
pic->data[n] = image->planes[n];
|
|
|
|
pic->linesize[n] = image->stride[n];
|
|
|
|
}
|
2013-03-09 06:01:43 +00:00
|
|
|
int ret = avcodec_encode_video2(avctx, &pkt, pic, &got_output);
|
|
|
|
if (ret < 0)
|
2012-08-06 15:46:42 +00:00
|
|
|
goto error_exit;
|
|
|
|
|
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:
|
|
|
|
if (avctx)
|
|
|
|
avcodec_close(avctx);
|
|
|
|
av_free(avctx);
|
2012-11-03 17:06:23 +00:00
|
|
|
avcodec_free_frame(&pic);
|
2013-03-09 06:01:43 +00:00
|
|
|
av_free_packet(&pkt);
|
2012-08-06 15:46:42 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_JPEG
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int write_jpeg(struct image_writer_ctx *ctx, mp_image_t *image, FILE *fp)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
cinfo.density_unit = 1; /* 0=unknown, 1=dpi, 2=dpcm */
|
2012-11-21 18:40:33 +00:00
|
|
|
cinfo.X_density = ctx->opts->jpeg_dpi;
|
|
|
|
cinfo.Y_density = ctx->opts->jpeg_dpi;
|
2012-08-06 15:51:04 +00:00
|
|
|
cinfo.write_Adobe_marker = TRUE;
|
|
|
|
|
2012-08-06 15:46:42 +00:00
|
|
|
jpeg_set_defaults(&cinfo);
|
2012-08-06 15:51:04 +00:00
|
|
|
jpeg_set_quality(&cinfo, ctx->opts->jpeg_quality, ctx->opts->jpeg_baseline);
|
|
|
|
cinfo.optimize_coding = ctx->opts->jpeg_optimize;
|
|
|
|
cinfo.smoothing_factor = ctx->opts->jpeg_smooth;
|
|
|
|
|
|
|
|
if (ctx->opts->jpeg_progressive)
|
|
|
|
jpeg_simple_progression(&cinfo);
|
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] +
|
|
|
|
cinfo.next_scanline * image->stride[0];
|
|
|
|
jpeg_write_scanlines(&cinfo, row_pointer,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
jpeg_finish_compress(&cinfo);
|
|
|
|
|
|
|
|
jpeg_destroy_compress(&cinfo);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const struct img_writer img_writers[] = {
|
2013-03-09 07:49:56 +00:00
|
|
|
{ "png", write_lavc, .lavc_codec = AV_CODEC_ID_PNG },
|
|
|
|
{ "ppm", write_lavc, .lavc_codec = AV_CODEC_ID_PPM },
|
2012-08-06 15:49:37 +00:00
|
|
|
{ "pgm", write_lavc,
|
2013-03-09 07:49:56 +00:00
|
|
|
.lavc_codec = AV_CODEC_ID_PGM,
|
video: decouple internal pixel formats from FourCCs
mplayer's video chain traditionally used FourCCs for pixel formats. For
example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the
string 'YV12' interpreted as unsigned int. Additionally, it used to
encode information into the numeric values of some formats. The RGB
formats had their bit depth and endian encoded into the least
significant byte. Extended planar formats (420P10 etc.) had chroma
shift, endian, and component bit depth encoded. (This has been removed
in recent commits.)
Replace the FourCC mess with a simple enum. Remove all the redundant
formats like YV12/I420/IYUV. Replace some image format names by
something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P.
Add img_fourcc.h, which contains the old IDs for code that actually uses
FourCCs. Change the way demuxers, that output raw video, identify the
video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to
request the rawvideo decoder, and sh_video->imgfmt specifies the pixel
format. Like the previous hack, this is supposed to avoid the need for
a complete codecs.cfg entry per format, or other lookup tables. (Note
that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT
raw video, but this is still considered better than adding a raw video
decoder - even if trivial, it would be full of annoying lookup tables.)
The TV code has not been tested.
Some corrective changes regarding endian and other image format flags
creep in.
2012-12-23 19:03:30 +00:00
|
|
|
.pixfmts = (int[]) { IMGFMT_Y8, 0 },
|
2012-08-06 15:49:37 +00:00
|
|
|
},
|
|
|
|
{ "pgmyuv", write_lavc,
|
2013-03-09 07:49:56 +00:00
|
|
|
.lavc_codec = AV_CODEC_ID_PGMYUV,
|
video: decouple internal pixel formats from FourCCs
mplayer's video chain traditionally used FourCCs for pixel formats. For
example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the
string 'YV12' interpreted as unsigned int. Additionally, it used to
encode information into the numeric values of some formats. The RGB
formats had their bit depth and endian encoded into the least
significant byte. Extended planar formats (420P10 etc.) had chroma
shift, endian, and component bit depth encoded. (This has been removed
in recent commits.)
Replace the FourCC mess with a simple enum. Remove all the redundant
formats like YV12/I420/IYUV. Replace some image format names by
something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P.
Add img_fourcc.h, which contains the old IDs for code that actually uses
FourCCs. Change the way demuxers, that output raw video, identify the
video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to
request the rawvideo decoder, and sh_video->imgfmt specifies the pixel
format. Like the previous hack, this is supposed to avoid the need for
a complete codecs.cfg entry per format, or other lookup tables. (Note
that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT
raw video, but this is still considered better than adding a raw video
decoder - even if trivial, it would be full of annoying lookup tables.)
The TV code has not been tested.
Some corrective changes regarding endian and other image format flags
creep in.
2012-12-23 19:03:30 +00:00
|
|
|
.pixfmts = (int[]) { IMGFMT_420P, 0 },
|
2012-08-06 15:49:37 +00:00
|
|
|
},
|
2012-08-06 15:50:31 +00:00
|
|
|
{ "tga", write_lavc,
|
2013-03-09 07:49:56 +00:00
|
|
|
.lavc_codec = AV_CODEC_ID_TARGA,
|
video: decouple internal pixel formats from FourCCs
mplayer's video chain traditionally used FourCCs for pixel formats. For
example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the
string 'YV12' interpreted as unsigned int. Additionally, it used to
encode information into the numeric values of some formats. The RGB
formats had their bit depth and endian encoded into the least
significant byte. Extended planar formats (420P10 etc.) had chroma
shift, endian, and component bit depth encoded. (This has been removed
in recent commits.)
Replace the FourCC mess with a simple enum. Remove all the redundant
formats like YV12/I420/IYUV. Replace some image format names by
something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P.
Add img_fourcc.h, which contains the old IDs for code that actually uses
FourCCs. Change the way demuxers, that output raw video, identify the
video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to
request the rawvideo decoder, and sh_video->imgfmt specifies the pixel
format. Like the previous hack, this is supposed to avoid the need for
a complete codecs.cfg entry per format, or other lookup tables. (Note
that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT
raw video, but this is still considered better than adding a raw video
decoder - even if trivial, it would be full of annoying lookup tables.)
The TV code has not been tested.
Some corrective changes regarding endian and other image format flags
creep in.
2012-12-23 19:03:30 +00:00
|
|
|
.pixfmts = (int[]) { IMGFMT_BGR24, IMGFMT_BGRA, IMGFMT_BGR15_LE,
|
|
|
|
IMGFMT_Y8, 0},
|
2012-08-06 15:50:31 +00:00
|
|
|
},
|
2012-08-06 15:46:42 +00:00
|
|
|
#ifdef CONFIG_JPEG
|
|
|
|
{ "jpg", write_jpeg },
|
|
|
|
{ "jpeg", write_jpeg },
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct img_writer *get_writer(const struct image_writer_opts *opts)
|
|
|
|
{
|
2012-08-06 17:05:32 +00:00
|
|
|
const char *type = opts->format;
|
2012-08-06 15:46:42 +00:00
|
|
|
|
|
|
|
for (size_t n = 0; n < sizeof(img_writers) / sizeof(img_writers[0]); n++) {
|
|
|
|
const struct img_writer *writer = &img_writers[n];
|
|
|
|
if (type && strcmp(type, writer->file_ext) == 0)
|
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &img_writers[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
return get_writer(opts)->file_ext;
|
|
|
|
}
|
|
|
|
|
2012-10-26 17:29:47 +00:00
|
|
|
int write_image(struct mp_image *image, const struct image_writer_opts *opts,
|
|
|
|
const char *filename)
|
2012-08-06 15:46:42 +00:00
|
|
|
{
|
|
|
|
struct mp_image *allocated_image = NULL;
|
|
|
|
struct image_writer_opts defs = image_writer_opts_defaults;
|
2012-10-26 18:19:06 +00:00
|
|
|
int d_w = image->display_w ? image->display_w : image->w;
|
|
|
|
int d_h = image->display_h ? image->display_h : image->h;
|
|
|
|
bool is_anamorphic = image->w != d_w || image->h != d_h;
|
2012-08-06 15:46:42 +00:00
|
|
|
|
|
|
|
if (!opts)
|
|
|
|
opts = &defs;
|
|
|
|
|
|
|
|
const struct img_writer *writer = get_writer(opts);
|
2012-08-06 15:47:28 +00:00
|
|
|
struct image_writer_ctx ctx = { opts, writer };
|
|
|
|
int destfmt = IMGFMT_RGB24;
|
|
|
|
|
|
|
|
if (writer->pixfmts) {
|
|
|
|
destfmt = writer->pixfmts[0]; // default to first pixel format
|
|
|
|
for (int *fmt = writer->pixfmts; *fmt; fmt++) {
|
|
|
|
if (*fmt == image->imgfmt) {
|
|
|
|
destfmt = *fmt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-06 15:46:42 +00:00
|
|
|
|
2013-03-09 06:01:43 +00:00
|
|
|
// Caveat: no colorspace/levels conversion done if pixel formats equal
|
|
|
|
// it's unclear what colorspace/levels the target wants
|
2012-08-06 15:47:10 +00:00
|
|
|
if (image->imgfmt != destfmt || is_anamorphic) {
|
2012-12-22 20:46:22 +00:00
|
|
|
struct mp_image *dst = mp_image_alloc(destfmt, d_w, d_h);
|
2012-12-19 11:04:57 +00:00
|
|
|
mp_image_copy_attributes(dst, image);
|
2012-10-27 16:06:09 +00:00
|
|
|
|
2013-07-18 11:48:57 +00:00
|
|
|
mp_image_swscale(dst, image, mp_sws_hq_flags);
|
2012-08-06 15:46:42 +00:00
|
|
|
|
|
|
|
allocated_image = dst;
|
|
|
|
image = dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *fp = fopen(filename, "wb");
|
|
|
|
int success = 0;
|
|
|
|
if (fp == NULL) {
|
|
|
|
mp_msg(MSGT_CPLAYER, MSGL_ERR,
|
|
|
|
"Error opening '%s' for writing!\n", filename);
|
|
|
|
} else {
|
|
|
|
success = writer->write(&ctx, image, fp);
|
|
|
|
success = !fclose(fp) && success;
|
|
|
|
if (!success)
|
|
|
|
mp_msg(MSGT_CPLAYER, MSGL_ERR, "Error writing file '%s'!\n",
|
|
|
|
filename);
|
|
|
|
}
|
|
|
|
|
2012-12-22 20:46:22 +00:00
|
|
|
talloc_free(allocated_image);
|
2012-08-06 15:46:42 +00:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
2012-10-27 15:30:26 +00:00
|
|
|
|
|
|
|
void dump_png(struct mp_image *image, const char *filename)
|
|
|
|
{
|
|
|
|
struct image_writer_opts opts = image_writer_opts_defaults;
|
|
|
|
opts.format = "png";
|
|
|
|
write_image(image, &opts, filename);
|
|
|
|
}
|