<div dir="ltr"><div>I have a series of JPEG files I would like to downsample and downresize with zscale filter but It always gives me the error code in the subject of this email.</div><div><br></div><div>This is the output from ffprobe:</div><div><br></div><div>ffprobe -v info -i /home/gianluca/Pictures/DSCF7253.JPG <br>ffprobe version N-116035-g6aeb084c39 Copyright (c) 2007-2024 the FFmpeg developers<br>  built with gcc 11 (Ubuntu 11.4.0-1ubuntu1~22.04)<br>  configuration: --prefix=/usr --disable-static --enable-shared --enable-gpl --enable-version3 --disable-programs --disable-doc --disable-avdevice --disable-swresample --enable-swscale --disable-postproc --disable-network --disable-everything --enable-protocol='file, subfile, pipe' --enable-demuxer='image2, image2pipe, image_jpeg_pipe, mjpeg' --enable-muxer=mjpeg --enable-decoder='mjpeg, mjpegb' --enable-encoder=mjpeg --enable-parser=mjpeg --enable-filter='format, null, copy, buffer, buffersink, scale, libplacebo, zscale' --enable-libplacebo --enable-libzimg --enable-vulkan --enable-ffprobe --enable-ffmpeg<br>  libavutil      59. 26.100 / 59. 26.100<br>  libavcodec     61.  8.100 / 61.  8.100<br>  libavformat    61.  4.100 / 61.  4.100<br>  libavfilter    10.  2.102 / 10.  2.102<br>  libswscale      8.  2.100 /  8.  2.100<br>Input #0, image2, from '/home/gianluca/Pictures/DSCF7253.JPG':<br>  Duration: 00:00:00.04, start: 0.000000, bitrate: 1274307 kb/s<br>  Stream #0:0: Video: mjpeg (Baseline), yuvj422p(pc, bt470bg/unknown/unknown), 4896x3264, 25 fps, 25 tbr, 25 tbn</div><div><br></div><div>Then I try with this code to downspample and downresize the image:</div><div><br></div><div>#include <stdio.h><br>#include <stdlib.h><br>#include <string.h><br>#include <errno.h><br>#include <libgen.h><br><br>#include <libavcodec/avcodec.h><br>#include <libavformat/avformat.h><br>#include <libavfilter/avfilter.h><br>#include <libavfilter/buffersrc.h><br>#include <libavfilter/buffersink.h><br>#include <libavutil/log.h><br><br>int main(int argc, char **argv)<br>{<br>    if (argc != 2) {<br>        fprintf(stderr, "usage: %s <filename>\n", argv[0]);<br>        exit(EXIT_FAILURE);<br>    }<br><br>    const char *filename = argv[1];<br>    const char *filename2 = basename(filename);<br><br>    av_log_set_level(AV_LOG_TRACE);<br><br>    AVFormatContext *fmt_ctx = avformat_alloc_context();<br>    avformat_open_input(&fmt_ctx, filename, NULL, NULL);<br><br>    AVCodecContext *dec_ctx;<br>    AVCodec *dec;<br>    <br>    av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);<br>    dec_ctx = avcodec_alloc_context3(dec);<br>    avcodec_open2(dec_ctx, dec, NULL);<br><br>    AVFrame *frame = av_frame_alloc();<br>    AVPacket *packet = av_packet_alloc();<br><br>    av_read_frame(fmt_ctx, packet);<br>    avcodec_send_packet(dec_ctx, packet);<br>    avcodec_receive_frame(dec_ctx, frame);<br>    av_packet_unref(packet);<br><br>    av_log(NULL, AV_LOG_TRACE, "filename: %s, w: %d, h: %d, fmt: %d\n", filename, frame->width, frame->height, frame->format);<br>    <br>    AVFilterGraph *filter_graph = avfilter_graph_alloc();<br><br>    char buffersrc_args[512];<br>    snprintf(buffersrc_args, sizeof(buffersrc_args), "video_size=%dx%d:pix_fmt=%d:time_base=1/25", frame->width, frame->height, frame->format);<br>    AVFilterContext *buffersrc_ctx;<br>    avfilter_graph_create_filter(&buffersrc_ctx, avfilter_get_by_name("buffer"), NULL, buffersrc_args, NULL, filter_graph);<br><br>    char zscale_args[512];<br>    snprintf(zscale_args, sizeof(zscale_args), "w=%d:h=%d:f=bicubic:r=full:p=709:t=iec61966-2-1:m=470bg", (frame->width / 2), (frame->height / 2));<br>    AVFilterContext *zscale_ctx = NULL;<br>    const AVFilter *zscale_filter = avfilter_get_by_name("zscale");<br>    avfilter_graph_create_filter(&zscale_ctx, zscale_filter, NULL, zscale_args, NULL, filter_graph);<br><br>    AVFilterContext *buffersink_ctx;<br>    avfilter_graph_create_filter(&buffersink_ctx, avfilter_get_by_name("buffersink"), NULL, NULL, NULL, filter_graph);<br><br>    avfilter_link(buffersrc_ctx, 0, zscale_ctx, 0);<br>    avfilter_link(zscale_ctx, 0, buffersink_ctx, 0);<br><br>    avfilter_graph_config(filter_graph, NULL);<br><br>    AVFrame *filtered_frame = av_frame_alloc();<br>    av_buffersrc_add_frame(buffersrc_ctx, frame);<br><br>    while (1) {<br>        int ret = av_buffersink_get_frame(buffersink_ctx, filtered_frame);<br>        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)<br>            break;<br>        if (ret < 0) {<br>            fprintf(stderr, "av_buffersink_get_frame(): %s\n", av_err2str(ret));<br>            exit(EXIT_FAILURE);<br>        }<br>    }<br><br>    av_log(NULL, AV_LOG_TRACE, "filename: %s, w: %d, h: %d, fmt: %d\n", filename2, filtered_frame->width, filtered_frame->height, filtered_frame->format);<br><br>    AVCodecContext *enc_ctx;<br>    AVCodec *enc;<br>    AVPacket *enc_packet = av_packet_alloc();<br><br>    enc = avcodec_find_encoder_by_name("mjpeg");<br>    enc_ctx = avcodec_alloc_context3(enc);<br>    enc_ctx->width = filtered_frame->width;<br>    enc_ctx->height = filtered_frame->height;<br>    enc_ctx->bit_rate = dec_ctx->bit_rate * 1024;<br>    enc_ctx->time_base = (AVRational) {1, 25};<br>    enc_ctx->framerate = (AVRational) {25, 1};<br>    enc_ctx->pix_fmt = filtered_frame->format;<br>    enc_ctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;<br>    enc_ctx->compression_level = 0;<br>    enc_ctx->color_range = AVCOL_RANGE_JPEG;<br><br>    avcodec_open2(enc_ctx, enc, NULL);<br><br>    avcodec_send_frame(enc_ctx, filtered_frame);<br>    avcodec_receive_packet(enc_ctx, enc_packet);<br><br>    FILE *dst_file = fopen(filename2, "wb");<br>    fwrite(enc_packet->data, 1, enc_packet->size, dst_file);<br>    fclose(dst_file);<br><br>    av_packet_unref(enc_packet);<br>    av_frame_free(&frame);<br>    av_frame_free(&filtered_frame);<br><br>    avfilter_graph_free(&filter_graph);<br><br>    avformat_close_input(&fmt_ctx);<br>    avformat_free_context(fmt_ctx);<br><br>    exit(EXIT_SUCCESS);<br>}</div><div><br></div><div>What am I doing wrong ?</div><div><br></div><div>Sincerely</div><div><br></div><div>Gianluca<br></div></div>