FFmpeg
subfile.c
Go to the documentation of this file.
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;
28  int64_t start;
29  int64_t end;
30  int64_t pos;
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,
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 
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 
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 };
ffurl_seek
static int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
Change the position that will be used by the next read/write operation on the resource accessed by h.
Definition: url.h:232
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
SubfileContext::pos
int64_t pos
Definition: subfile.c:30
AVOption
AVOption.
Definition: opt.h:251
SubfileContext::end
int64_t end
Definition: subfile.c:29
AVSEEK_SIZE
#define AVSEEK_SIZE
ORing this as the "whence" parameter to a seek function causes it to return the filesize without seek...
Definition: avio.h:487
AVDictionary
Definition: dict.c:34
URLProtocol
Definition: url.h:53
SubfileContext::h
URLContext * h
Definition: subfile.c:27
OFFSET
#define OFFSET(field)
Definition: subfile.c:33
subfile_open
static int subfile_open(URLContext *h, const char *filename, int flags, AVDictionary **options)
Definition: subfile.c:67
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
ffurl_open_whitelist
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it.
Definition: avio.c:300
D
#define D
Definition: subfile.c:34
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Definition: opt.h:226
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
subfile_read
static int subfile_read(URLContext *h, unsigned char *buf, int size)
Definition: subfile.c:99
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
SubfileContext::start
int64_t start
Definition: subfile.c:28
options
const OptionDef options[]
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
size
int size
Definition: twinvq_data.h:10344
URLProtocol::name
const char * name
Definition: url.h:54
subfile_class
static const AVClass subfile_class
Definition: subfile.c:45
av_strstart
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:36
URLContext
Definition: url.h:37
subfile_seek
static int64_t subfile_seek(URLContext *h, int64_t pos, int whence)
Definition: subfile.c:114
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
url.h
ffurl_closep
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:439
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
pos
unsigned int pos
Definition: spdifenc.c:413
subfile_options
static const AVOption subfile_options[]
Definition: subfile.c:36
SubfileContext
Definition: subfile.c:25
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
h
h
Definition: vp9dsp_template.c:2038
avstring.h
slave_seek
static int slave_seek(URLContext *h)
Definition: subfile.c:52
ff_subfile_protocol
const URLProtocol ff_subfile_protocol
Definition: subfile.c:147
subfile_close
static int subfile_close(URLContext *h)
Definition: subfile.c:93
ffurl_read
static int ffurl_read(URLContext *h, uint8_t *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf.
Definition: url.h:183