struct mp_image *convert_image(struct mp_image *image, int destfmt, struct mp_log *log) { int d_w, d_h; mp_image_params_get_dsize(&image->params, &d_w, &d_h); bool is_anamorphic = image->w != d_w || image->h != d_h; // Caveat: no colorspace/levels conversion done if pixel formats equal // it's unclear what colorspace/levels the target wants if (image->imgfmt == destfmt && !is_anamorphic) return mp_image_new_ref(image); struct mp_image *dst = mp_image_alloc(destfmt, d_w, d_h); if (!dst) { mp_err(log, "Out of memory.\n"); return NULL; } mp_image_copy_attributes(dst, image); 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; }
int write_image(struct mp_image *image, const struct image_writer_opts *opts, const char *filename) { struct mp_image *allocated_image = NULL; struct image_writer_opts defs = image_writer_opts_defaults; 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; if (!opts) opts = &defs; const struct img_writer *writer = get_writer(opts); 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; } } } // Caveat: - no colorspace/levels conversion done if pixel formats equal // - RGB->YUV assumes BT.601 // - color levels broken in various ways thanks to libswscale if (image->imgfmt != destfmt || is_anamorphic) { struct mp_image *dst = alloc_mpi(d_w, d_h, destfmt); vf_clone_mpi_attributes(dst, image); int flags = SWS_LANCZOS | SWS_FULL_CHR_H_INT | SWS_FULL_CHR_H_INP | SWS_ACCURATE_RND | SWS_BITEXACT; mp_image_swscale(dst, image, flags); 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); } free_mp_image(allocated_image); return success; }
int write_image(struct mp_image *image, const struct image_writer_opts *opts, const char *filename) { struct mp_image *allocated_image = NULL; struct image_writer_opts defs = image_writer_opts_defaults; 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; if (!opts) opts = &defs; const struct img_writer *writer = get_writer(opts); 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; } } } // Caveat: no colorspace/levels conversion done if pixel formats equal // it's unclear what colorspace/levels the target wants if (image->imgfmt != destfmt || is_anamorphic) { struct mp_image *dst = mp_image_alloc(destfmt, d_w, d_h); mp_image_copy_attributes(dst, image); mp_image_swscale(dst, image, mp_sws_hq_flags); 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); } talloc_free(allocated_image); return success; }
struct mp_image *convert_image(struct mp_image *image, int destfmt, struct mp_log *log) { int d_w, d_h; mp_image_params_get_dsize(&image->params, &d_w, &d_h); 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)) return mp_image_new_ref(image); struct mp_image *dst = mp_image_alloc(p.imgfmt, p.w, p.h); if (!dst) { mp_err(log, "Out of memory.\n"); return NULL; } mp_image_copy_attributes(dst, image); dst->params = p; 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; if (!opts) opts = &defs; 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); struct mp_image *dst = convert_image(image, destfmt, log); if (!dst) return false; FILE *fp = fopen(filename, "wb"); bool success = false; if (fp == NULL) { mp_err(log, "Error opening '%s' for writing!\n", filename); } else { success = write(&ctx, dst, fp); success = !fclose(fp) && success; if (!success) mp_err(log, "Error writing file '%s'!\n", filename); } talloc_free(dst); return success; } void dump_png(struct mp_image *image, const char *filename, struct mp_log *log) { struct image_writer_opts opts = image_writer_opts_defaults; opts.format = AV_CODEC_ID_PNG; write_image(image, &opts, filename, log); }