FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/subfile.c
Date: 2026-08-28 22:32:55
Exec Total Coverage
Lines: 0 63 0.0%
Functions: 0 5 0.0%
Branches: 0 29 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2014 Nicolas George
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/avassert.h"
22 #include "libavutil/avstring.h"
23 #include "libavutil/common.h"
24 #include "libavutil/error.h"
25 #include "libavutil/opt.h"
26 #include "url.h"
27
28 typedef struct SubfileContext {
29 const AVClass *class;
30 URLContext *h;
31 int64_t start;
32 int64_t end;
33 int64_t pos;
34 } SubfileContext;
35
36 #define OFFSET(field) offsetof(SubfileContext, field)
37 #define D AV_OPT_FLAG_DECODING_PARAM
38
39 static const AVOption subfile_options[] = {
40 { "start", "start offset", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, D },
41 { "end", "end offset", OFFSET(end), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, D },
42 { NULL }
43 };
44
45 #undef OFFSET
46 #undef D
47
48 static const AVClass subfile_class = {
49 .class_name = "subfile",
50 .item_name = av_default_item_name,
51 .option = subfile_options,
52 .version = LIBAVUTIL_VERSION_INT,
53 };
54
55 static int slave_seek(URLContext *h)
56 {
57 SubfileContext *c = h->priv_data;
58 int64_t ret;
59
60 if ((ret = ffurl_seek(c->h, c->pos, SEEK_SET)) != c->pos) {
61 if (ret >= 0)
62 ret = AVERROR_BUG;
63 av_log(h, AV_LOG_ERROR, "Impossible to seek in file: %s\n",
64 av_err2str(ret));
65 return ret;
66 }
67 return 0;
68 }
69
70 static int subfile_open(URLContext *h, const char *filename, int flags,
71 AVDictionary **options)
72 {
73 SubfileContext *c = h->priv_data;
74 int ret;
75
76 if (!c->end)
77 c->end = INT64_MAX;
78
79 if (c->end <= c->start) {
80 av_log(h, AV_LOG_ERROR, "end before start\n");
81 return AVERROR(EINVAL);
82 }
83 av_strstart(filename, "subfile:", &filename);
84 ret = ffurl_open_whitelist(&c->h, filename, flags, &h->interrupt_callback,
85 options, h->protocol_whitelist, h->protocol_blacklist, h);
86 if (ret < 0)
87 return ret;
88 c->pos = c->start;
89 if ((ret = slave_seek(h)) < 0) {
90 ffurl_closep(&c->h);
91 return ret;
92 }
93 return 0;
94 }
95
96 static int subfile_close(URLContext *h)
97 {
98 SubfileContext *c = h->priv_data;
99 return ffurl_closep(&c->h);
100 }
101
102 static int subfile_read(URLContext *h, unsigned char *buf, int size)
103 {
104 SubfileContext *c = h->priv_data;
105 int64_t rest = c->end - c->pos;
106 int ret;
107
108 if (rest <= 0)
109 return AVERROR_EOF;
110 size = FFMIN(size, rest);
111 ret = ffurl_read(c->h, buf, size);
112 if (ret >= 0)
113 c->pos += ret;
114 return ret;
115 }
116
117 static int64_t subfile_seek(URLContext *h, int64_t pos, int whence)
118 {
119 SubfileContext *c = h->priv_data;
120 int64_t new_pos, end;
121 int ret;
122
123 end = c->end;
124 if (end == INT64_MAX && (end = ffurl_seek(c->h, 0, AVSEEK_SIZE)) < 0)
125 return end;
126
127 switch (whence) {
128 case AVSEEK_SIZE:
129 return end - c->start;
130 case SEEK_SET:
131 new_pos = c->start + av_clip(pos, 0, end - c->start);
132 break;
133 case SEEK_CUR:
134 new_pos = c->pos + av_clip(pos, -(c->pos - c->start), end - c->pos);
135 break;
136 case SEEK_END:
137 new_pos = end + av_clip(pos, -(end - c->start), 0);
138 break;
139 default:
140 av_assert0(0);
141 }
142 if (new_pos < c->start)
143 return AVERROR(EINVAL);
144 c->pos = new_pos;
145 if ((ret = slave_seek(h)) < 0)
146 return ret;
147 return c->pos - c->start;
148 }
149
150 const URLProtocol ff_subfile_protocol = {
151 .name = "subfile",
152 .url_open2 = subfile_open,
153 .url_read = subfile_read,
154 .url_seek = subfile_seek,
155 .url_close = subfile_close,
156 .priv_data_size = sizeof(SubfileContext),
157 .priv_data_class = &subfile_class,
158 .default_whitelist = "file",
159 };
160