FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/img2enc.c
Date: 2025-08-19 23:55:23
Exec Total Coverage
Lines: 58 152 38.2%
Functions: 5 6 83.3%
Branches: 25 102 24.5%

Line Branch Exec Source
1 /*
2 * Image format
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * Copyright (c) 2004 Michael Niedermayer
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <time.h>
24
25 #include "config_components.h"
26
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/bprint.h"
30 #include "libavutil/dict.h"
31 #include "libavutil/log.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/time_internal.h"
36 #include "avformat.h"
37 #include "avio_internal.h"
38 #include "internal.h"
39 #include "img2.h"
40 #include "mux.h"
41
42 typedef struct VideoMuxData {
43 const AVClass *class; /**< Class for private options. */
44 int start_img_number;
45 int img_number;
46 int split_planes; /**< use independent file for each Y, U, V plane */
47 int update;
48 int use_strftime;
49 int frame_pts;
50 const char *muxer;
51 int use_rename;
52 AVDictionary *protocol_opts;
53 } VideoMuxData;
54
55 65 static int write_header(AVFormatContext *s)
56 {
57 65 VideoMuxData *img = s->priv_data;
58 65 AVStream *st = s->streams[0];
59 65 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(st->codecpar->format);
60
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (st->codecpar->codec_id == AV_CODEC_ID_GIF) {
62 img->muxer = "gif";
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 } else if (st->codecpar->codec_id == AV_CODEC_ID_FITS) {
64 img->muxer = "fits";
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 } else if (st->codecpar->codec_id == AV_CODEC_ID_AV1) {
66 img->muxer = "avif";
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 } else if (st->codecpar->codec_id == AV_CODEC_ID_RAWVIDEO) {
68 const char *str = strrchr(s->url, '.');
69 img->split_planes = str
70 && !av_strcasecmp(str + 1, "y")
71 && s->nb_streams == 1
72 && desc
73 &&(desc->flags & AV_PIX_FMT_FLAG_PLANAR)
74 && desc->nb_components >= 3;
75 }
76 65 img->img_number = img->start_img_number;
77
78 65 return 0;
79 }
80
81 static int write_muxed_file(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
82 {
83 VideoMuxData *img = s->priv_data;
84 AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
85 AVStream *st;
86 AVPacket *const pkt2 = ffformatcontext(s)->pkt;
87 AVFormatContext *fmt = NULL;
88 int ret;
89
90 /* URL is not used directly as we are overriding the IO context later. */
91 ret = avformat_alloc_output_context2(&fmt, NULL, img->muxer, s->url);
92 if (ret < 0)
93 return ret;
94 st = avformat_new_stream(fmt, NULL);
95 if (!st) {
96 ret = AVERROR(ENOMEM);
97 goto out;
98 }
99 st->id = pkt->stream_index;
100
101 fmt->pb = pb;
102
103 ret = av_packet_ref(pkt2, pkt);
104 if (ret < 0)
105 goto out;
106 pkt2->stream_index = 0;
107
108 if ((ret = avcodec_parameters_copy(st->codecpar, par)) < 0 ||
109 (ret = avformat_write_header(fmt, NULL)) < 0 ||
110 (ret = av_interleaved_write_frame(fmt, pkt2)) < 0 ||
111 (ret = av_write_trailer(fmt))) {}
112
113 av_packet_unref(pkt2);
114 out:
115 avformat_free_context(fmt);
116 return ret;
117 }
118
119 151 static int write_packet_pipe(AVFormatContext *s, AVPacket *pkt)
120 {
121 151 VideoMuxData *img = s->priv_data;
122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 151 times.
151 if (img->muxer) {
123 int ret = write_muxed_file(s, s->pb, pkt);
124 if (ret < 0)
125 return ret;
126 } else {
127 151 avio_write(s->pb, pkt->data, pkt->size);
128 }
129 151 img->img_number++;
130 151 return 0;
131 }
132
133 670 static int write_and_close(AVFormatContext *s, AVIOContext **pb, const unsigned char *buf, int size)
134 {
135 670 avio_write(*pb, buf, size);
136 670 avio_flush(*pb);
137 670 return ff_format_io_close(s, pb);
138 }
139
140 670 static int write_packet(AVFormatContext *s, AVPacket *pkt)
141 {
142 670 VideoMuxData *img = s->priv_data;
143 670 AVIOContext *pb[4] = {0};
144 670 char* target[4] = {0};
145 670 char* tmp[4] = {0};
146 670 AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
147 670 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(par->format);
148 int ret, i;
149 670 AVDictionary *options = NULL;
150 AVBPrint filename;
151 670 av_bprint_init(&filename, 0, AV_BPRINT_SIZE_UNLIMITED);
152
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 670 times.
670 if (img->update) {
154 av_bprintf(&filename, "%s", s->url);
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 670 times.
670 } else if (img->use_strftime) {
156 time_t now0;
157 struct tm *tm, tmpbuf;
158 time(&now0);
159 tm = localtime_r(&now0, &tmpbuf);
160 av_bprint_strftime(&filename, s->url, tm);
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 670 times.
670 } else if (img->frame_pts) {
162 if (ff_bprint_get_frame_filename(&filename, s->url, pkt->pts, AV_FRAME_FILENAME_FLAGS_MULTIPLE) < 0) {
163 av_log(s, AV_LOG_ERROR, "Cannot write filename by pts of the frames.");
164 ret = AVERROR(EINVAL);
165 goto fail;
166 }
167
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 663 times.
670 } else if (ff_bprint_get_frame_filename(&filename, s->url,
168 670 img->img_number,
169 AV_FRAME_FILENAME_FLAGS_MULTIPLE) < 0) {
170
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (img->img_number == img->start_img_number) {
171 7 av_log(s, AV_LOG_WARNING, "The specified filename '%s' does not contain an image sequence pattern or a pattern is invalid.\n", s->url);
172 7 av_log(s, AV_LOG_WARNING,
173 "Use a pattern such as %%03d for an image sequence or "
174 "use the -update option (with -frames:v 1 if needed) to write a single image.\n");
175 7 av_bprint_clear(&filename);
176 7 av_bprintf(&filename, "%s", s->url);
177 } else {
178 av_log(s, AV_LOG_ERROR, "Cannot write more than one file with the same name. Are you missing the -update option or a sequence pattern?\n");
179 ret = AVERROR(EINVAL);
180 goto fail;
181 }
182 }
183
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 670 times.
670 if (!av_bprint_is_complete(&filename)) {
184 ret = AVERROR(ENOMEM);
185 goto fail;
186 }
187
1/2
✓ Branch 0 taken 670 times.
✗ Branch 1 not taken.
670 for (i = 0; i < 4; i++) {
188 670 av_dict_copy(&options, img->protocol_opts, 0);
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 670 times.
670 if (img->use_rename) {
190 tmp[i] = av_asprintf("%s.tmp", filename.str);
191 target[i] = av_strdup(filename.str);
192 if (!tmp[i] || !target[i]) {
193 ret = AVERROR(ENOMEM);
194 goto fail;
195 }
196 }
197
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 670 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 670 times.
670 if (s->io_open(s, &pb[i], tmp[i] ? tmp[i] : filename.str, AVIO_FLAG_WRITE, &options) < 0) {
198 av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", tmp[i] ? tmp[i] : filename.str);
199 ret = AVERROR(EIO);
200 goto fail;
201 }
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 670 times.
670 if (options) {
203 av_log(s, AV_LOG_ERROR, "Could not recognize some protocol options\n");
204 ret = AVERROR(EINVAL);
205 goto fail;
206 }
207
208
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 670 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
670 if (!img->split_planes || i+1 >= desc->nb_components)
209 break;
210 filename.str[filename.len - 1] = "UVAx"[i];
211 }
212 670 av_bprint_finalize(&filename, NULL);
213
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 670 times.
670 if (img->split_planes) {
215 int ysize = par->width * par->height;
216 int usize = AV_CEIL_RSHIFT(par->width, desc->log2_chroma_w) * AV_CEIL_RSHIFT(par->height, desc->log2_chroma_h);
217 if (desc->comp[0].depth >= 9) {
218 ysize *= 2;
219 usize *= 2;
220 }
221 if ((ret = write_and_close(s, &pb[0], pkt->data , ysize)) < 0 ||
222 (ret = write_and_close(s, &pb[1], pkt->data + ysize , usize)) < 0 ||
223 (ret = write_and_close(s, &pb[2], pkt->data + ysize + usize, usize)) < 0)
224 goto fail;
225 if (desc->nb_components > 3)
226 ret = write_and_close(s, &pb[3], pkt->data + ysize + 2*usize, ysize);
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 670 times.
670 } else if (img->muxer) {
228 if ((ret = write_muxed_file(s, pb[0], pkt)) < 0)
229 goto fail;
230 ret = ff_format_io_close(s, &pb[0]);
231 } else {
232 670 ret = write_and_close(s, &pb[0], pkt->data, pkt->size);
233 }
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 670 times.
670 if (ret < 0)
235 goto fail;
236
237
2/4
✓ Branch 0 taken 670 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 670 times.
670 for (i = 0; i < 4 && tmp[i]; i++) {
238 int ret = ff_rename(tmp[i], target[i], s);
239 if (ret < 0)
240 goto fail;
241 av_freep(&tmp[i]);
242 av_freep(&target[i]);
243 }
244
245 670 img->img_number++;
246 670 return 0;
247
248 fail:
249 av_bprint_finalize(&filename, NULL);
250 av_dict_free(&options);
251 for (i = 0; i < FF_ARRAY_ELEMS(pb); i++) {
252 av_freep(&tmp[i]);
253 av_freep(&target[i]);
254 if (pb[i])
255 ff_format_io_close(s, &pb[i]);
256 }
257 return ret;
258 }
259
260 65 static int query_codec(enum AVCodecID id, int std_compliance)
261 {
262 int i;
263
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 for (i = 0; ff_img_tags[i].id != AV_CODEC_ID_NONE; i++)
264
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 if (ff_img_tags[i].id == id)
265 65 return 1;
266
267 // Anything really can be stored in img2
268 return std_compliance < FF_COMPLIANCE_NORMAL;
269 }
270
271 #define OFFSET(x) offsetof(VideoMuxData, x)
272 #define ENC AV_OPT_FLAG_ENCODING_PARAM
273 static const AVOption muxoptions[] = {
274 { "update", "continuously overwrite one file", OFFSET(update), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
275 { "start_number", "set first number in the sequence", OFFSET(start_img_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, ENC },
276 { "strftime", "use strftime for filename", OFFSET(use_strftime), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
277 { "frame_pts", "use current frame pts for filename", OFFSET(frame_pts), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
278 { "atomic_writing", "write files atomically (using temporary files and renames)", OFFSET(use_rename), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
279 { "protocol_opts", "specify protocol options for the opened files", OFFSET(protocol_opts), AV_OPT_TYPE_DICT, {0}, 0, 0, ENC },
280 { NULL },
281 };
282
283 #if CONFIG_IMAGE2_MUXER
284 static const AVClass img2mux_class = {
285 .class_name = "image2 muxer",
286 .item_name = av_default_item_name,
287 .option = muxoptions,
288 .version = LIBAVUTIL_VERSION_INT,
289 };
290
291 const FFOutputFormat ff_image2_muxer = {
292 .p.name = "image2",
293 .p.long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
294 .p.extensions = "bmp,dpx,exr,jls,jpeg,jpg,jxl,ljpg,pam,pbm,pcx,pfm,pgm,pgmyuv,phm,"
295 "png,ppm,sgi,tga,tif,tiff,jp2,j2c,j2k,xwd,sun,ras,rs,im1,im8,"
296 "im24,sunras,vbn,xbm,xface,pix,y,avif,qoi,hdr,wbmp",
297 .priv_data_size = sizeof(VideoMuxData),
298 .p.video_codec = AV_CODEC_ID_MJPEG,
299 .write_header = write_header,
300 .write_packet = write_packet,
301 .query_codec = query_codec,
302 .p.flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE,
303 .p.priv_class = &img2mux_class,
304 };
305 #endif
306 #if CONFIG_IMAGE2PIPE_MUXER
307 const FFOutputFormat ff_image2pipe_muxer = {
308 .p.name = "image2pipe",
309 .p.long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
310 .priv_data_size = sizeof(VideoMuxData),
311 .p.video_codec = AV_CODEC_ID_MJPEG,
312 .write_header = write_header,
313 .write_packet = write_packet_pipe,
314 .query_codec = query_codec,
315 .p.flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
316 };
317 #endif
318