FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/img2enc.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 54 137 39.4%
Branches: 23 96 24.0%

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