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/avassert.h"
22 #include "libavutil/avstring.h"
23 #include "libavutil/opt.h"
24 #include "avformat.h"
25 #include "url.h"
26 
27 typedef struct SubfileContext {
28  const AVClass *class;
30  int64_t start;
31  int64_t end;
32  int64_t pos;
34 
35 #define OFFSET(field) offsetof(SubfileContext, field)
36 #define D AV_OPT_FLAG_DECODING_PARAM
37 
38 static const AVOption subfile_options[] = {
39  { "start", "start offset", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, D },
40  { "end", "end offset", OFFSET(end), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, D },
41  { NULL }
42 };
43 
44 #undef OFFSET
45 #undef D
46 
47 static const AVClass subfile_class = {
48  .class_name = "subfile",
49  .item_name = av_default_item_name,
50  .option = subfile_options,
51  .version = LIBAVUTIL_VERSION_INT,
52 };
53 
54 static int slave_seek(URLContext *h)
55 {
56  SubfileContext *c = h->priv_data;
57  int64_t ret;
58 
59  if ((ret = ffurl_seek(c->h, c->pos, SEEK_SET)) != c->pos) {
60  if (ret >= 0)
61  ret = AVERROR_BUG;
62  av_log(h, AV_LOG_ERROR, "Impossible to seek in file: %s\n",
63  av_err2str(ret));
64  return ret;
65  }
66  return 0;
67 }
68 
69 static int subfile_open(URLContext *h, const char *filename, int flags,
71 {
72  SubfileContext *c = h->priv_data;
73  int ret;
74 
75  if (!c->end)
76  c->end = INT64_MAX;
77 
78  if (c->end <= c->start) {
79  av_log(h, AV_LOG_ERROR, "end before start\n");
80  return AVERROR(EINVAL);
81  }
82  av_strstart(filename, "subfile:", &filename);
83  ret = ffurl_open_whitelist(&c->h, filename, flags, &h->interrupt_callback,
84  options, h->protocol_whitelist, h->protocol_blacklist, h);
85  if (ret < 0)
86  return ret;
87  c->pos = c->start;
88  if ((ret = slave_seek(h)) < 0) {
89  ffurl_close(c->h);
90  return ret;
91  }
92  return 0;
93 }
94 
96 {
97  SubfileContext *c = h->priv_data;
98  return ffurl_close(c->h);
99 }
100 
101 static int subfile_read(URLContext *h, unsigned char *buf, int size)
102 {
103  SubfileContext *c = h->priv_data;
104  int64_t rest = c->end - c->pos;
105  int ret;
106 
107  if (rest <= 0)
108  return AVERROR_EOF;
109  size = FFMIN(size, rest);
110  ret = ffurl_read(c->h, buf, size);
111  if (ret >= 0)
112  c->pos += ret;
113  return ret;
114 }
115 
116 static int64_t subfile_seek(URLContext *h, int64_t pos, int whence)
117 {
118  SubfileContext *c = h->priv_data;
119  int64_t new_pos = -1, end;
120  int ret;
121 
122  if (whence == AVSEEK_SIZE || whence == SEEK_END) {
123  end = c->end;
124  if (end == INT64_MAX && (end = ffurl_seek(c->h, 0, AVSEEK_SIZE)) < 0)
125  return end;
126  }
127 
128  if (whence == AVSEEK_SIZE)
129  return end - c->start;
130  switch (whence) {
131  case SEEK_SET:
132  new_pos = c->start + pos;
133  break;
134  case SEEK_CUR:
135  new_pos += pos;
136  break;
137  case SEEK_END:
138  new_pos = end + c->pos;
139  break;
140  }
141  if (new_pos < c->start)
142  return AVERROR(EINVAL);
143  c->pos = new_pos;
144  if ((ret = slave_seek(h)) < 0)
145  return ret;
146  return c->pos - c->start;
147 }
148 
150  .name = "subfile",
151  .url_open2 = subfile_open,
152  .url_read = subfile_read,
153  .url_seek = subfile_seek,
154  .url_close = subfile_close,
155  .priv_data_size = sizeof(SubfileContext),
156  .priv_data_class = &subfile_class,
157  .default_whitelist = "file",
158 };
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
ffurl_seek
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: avio.c:437
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
end
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
SubfileContext::pos
int64_t pos
Definition: subfile.c:32
AVOption
AVOption.
Definition: opt.h:246
SubfileContext::end
int64_t end
Definition: subfile.c:31
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:531
ffurl_close
int ffurl_close(URLContext *h)
Definition: avio.c:470
AVDictionary
Definition: dict.c:30
URLProtocol
Definition: url.h:54
SubfileContext::h
URLContext * h
Definition: subfile.c:29
start
void INT64 start
Definition: avisynth_c.h:767
OFFSET
#define OFFSET(field)
Definition: subfile.c:35
subfile_open
static int subfile_open(URLContext *h, const char *filename, int flags, AVDictionary **options)
Definition: subfile.c:69
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
buf
void * buf
Definition: avisynth_c.h:766
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:307
D
#define D
Definition: subfile.c:36
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Definition: opt.h:224
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
subfile_read
static int subfile_read(URLContext *h, unsigned char *buf, int size)
Definition: subfile.c:101
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
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:30
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:119
size
int size
Definition: twinvq_data.h:11134
URLProtocol::name
const char * name
Definition: url.h:55
subfile_class
static const AVClass subfile_class
Definition: subfile.c:47
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
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:34
URLContext
Definition: url.h:38
subfile_seek
static int64_t subfile_seek(URLContext *h, int64_t pos, int whence)
Definition: subfile.c:116
url.h
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:72
avformat.h
ffurl_read
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf.
Definition: avio.c:410
subfile_options
static const AVOption subfile_options[]
Definition: subfile.c:38
SubfileContext
Definition: subfile.c:27
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
h
h
Definition: vp9dsp_template.c:2038
avstring.h
slave_seek
static int slave_seek(URLContext *h)
Definition: subfile.c:54
ff_subfile_protocol
const URLProtocol ff_subfile_protocol
Definition: subfile.c:149
subfile_close
static int subfile_close(URLContext *h)
Definition: subfile.c:95