FFmpeg
Loading...
Searching...
No Matches
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/common.h"
24#include "libavutil/error.h"
25#include "libavutil/opt.h"
26#include "url.h"
27
35
36#define OFFSET(field) offsetof(SubfileContext, field)
37#define D AV_OPT_FLAG_DECODING_PARAM
38
39static 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
48static 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
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
70static int subfile_open(URLContext *h, const char *filename, int flags,
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
97{
98 SubfileContext *c = h->priv_data;
99 return ffurl_closep(&c->h);
100}
101
102static 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
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
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};
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define D
Definition avdct.c:35
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:461
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition avio.c:656
#define AVSEEK_SIZE
Passing this as the "whence" parameter to a seek function causes it to return the filesize without se...
Definition avio.h:468
#define flags(name, subs,...)
Definition cbs_h264.c:74
common internal and external API header
#define av_clip
Definition common.h:100
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
error code definitions
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition opt.h:262
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_EOF
End of file.
Definition error.h:57
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
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
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define FFMIN(a, b)
Definition macros.h:49
AVOptions.
const URLProtocol ff_subfile_protocol
Definition subfile.c:150
unsigned int pos
Definition spdifenc.c:431
Describe the class of an AVClass context structure.
Definition log.h:76
AVOption.
Definition opt.h:428
URLContext * h
Definition subfile.c:30
int64_t pos
Definition subfile.c:33
int64_t start
Definition subfile.c:31
int64_t end
Definition subfile.c:32
static int slave_seek(URLContext *h)
Definition subfile.c:55
static const AVClass subfile_class
Definition subfile.c:48
static int subfile_read(URLContext *h, unsigned char *buf, int size)
Definition subfile.c:102
static int64_t subfile_seek(URLContext *h, int64_t pos, int whence)
Definition subfile.c:117
static int subfile_close(URLContext *h)
Definition subfile.c:96
static const AVOption subfile_options[]
Definition subfile.c:39
static int subfile_open(URLContext *h, const char *filename, int flags, AVDictionary **options)
Definition subfile.c:70
#define OFFSET(field)
Definition subfile.c:36
#define av_log(a,...)
int size
unbuffered private I/O API
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:224
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
static double c[64]