FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/subfile.c
Date: 2024-04-26 14:42:52
Exec Total Coverage
Lines: 0 62 0.0%
Functions: 0 5 0.0%
Branches: 0 34 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/avstring.h"
22 #include "libavutil/opt.h"
23 #include "url.h"
24
25 typedef struct SubfileContext {
26 const AVClass *class;
27 URLContext *h;
28 int64_t start;
29 int64_t end;
30 int64_t pos;
31 } SubfileContext;
32
33 #define OFFSET(field) offsetof(SubfileContext, field)
34 #define D AV_OPT_FLAG_DECODING_PARAM
35
36 static const AVOption subfile_options[] = {
37 { "start", "start offset", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, D },
38 { "end", "end offset", OFFSET(end), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, D },
39 { NULL }
40 };
41
42 #undef OFFSET
43 #undef D
44
45 static const AVClass subfile_class = {
46 .class_name = "subfile",
47 .item_name = av_default_item_name,
48 .option = subfile_options,
49 .version = LIBAVUTIL_VERSION_INT,
50 };
51
52 static int slave_seek(URLContext *h)
53 {
54 SubfileContext *c = h->priv_data;
55 int64_t ret;
56
57 if ((ret = ffurl_seek(c->h, c->pos, SEEK_SET)) != c->pos) {
58 if (ret >= 0)
59 ret = AVERROR_BUG;
60 av_log(h, AV_LOG_ERROR, "Impossible to seek in file: %s\n",
61 av_err2str(ret));
62 return ret;
63 }
64 return 0;
65 }
66
67 static int subfile_open(URLContext *h, const char *filename, int flags,
68 AVDictionary **options)
69 {
70 SubfileContext *c = h->priv_data;
71 int ret;
72
73 if (!c->end)
74 c->end = INT64_MAX;
75
76 if (c->end <= c->start) {
77 av_log(h, AV_LOG_ERROR, "end before start\n");
78 return AVERROR(EINVAL);
79 }
80 av_strstart(filename, "subfile:", &filename);
81 ret = ffurl_open_whitelist(&c->h, filename, flags, &h->interrupt_callback,
82 options, h->protocol_whitelist, h->protocol_blacklist, h);
83 if (ret < 0)
84 return ret;
85 c->pos = c->start;
86 if ((ret = slave_seek(h)) < 0) {
87 ffurl_closep(&c->h);
88 return ret;
89 }
90 return 0;
91 }
92
93 static int subfile_close(URLContext *h)
94 {
95 SubfileContext *c = h->priv_data;
96 return ffurl_closep(&c->h);
97 }
98
99 static int subfile_read(URLContext *h, unsigned char *buf, int size)
100 {
101 SubfileContext *c = h->priv_data;
102 int64_t rest = c->end - c->pos;
103 int ret;
104
105 if (rest <= 0)
106 return AVERROR_EOF;
107 size = FFMIN(size, rest);
108 ret = ffurl_read(c->h, buf, size);
109 if (ret >= 0)
110 c->pos += ret;
111 return ret;
112 }
113
114 static int64_t subfile_seek(URLContext *h, int64_t pos, int whence)
115 {
116 SubfileContext *c = h->priv_data;
117 int64_t new_pos, end;
118 int ret;
119
120 if (whence == AVSEEK_SIZE || whence == SEEK_END) {
121 end = c->end;
122 if (end == INT64_MAX && (end = ffurl_seek(c->h, 0, AVSEEK_SIZE)) < 0)
123 return end;
124 }
125
126 if (whence == AVSEEK_SIZE)
127 return end - c->start;
128 switch (whence) {
129 case SEEK_SET:
130 new_pos = c->start + pos;
131 break;
132 case SEEK_CUR:
133 new_pos = c->pos + pos;
134 break;
135 case SEEK_END:
136 new_pos = end + pos;
137 break;
138 }
139 if (new_pos < c->start)
140 return AVERROR(EINVAL);
141 c->pos = new_pos;
142 if ((ret = slave_seek(h)) < 0)
143 return ret;
144 return c->pos - c->start;
145 }
146
147 const URLProtocol ff_subfile_protocol = {
148 .name = "subfile",
149 .url_open2 = subfile_open,
150 .url_read = subfile_read,
151 .url_seek = subfile_seek,
152 .url_close = subfile_close,
153 .priv_data_size = sizeof(SubfileContext),
154 .priv_data_class = &subfile_class,
155 .default_whitelist = "file",
156 };
157