FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 {
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 {
73  int ret;
74 
75  if (c->end <= c->start) {
76  av_log(h, AV_LOG_ERROR, "end before start\n");
77  return AVERROR(EINVAL);
78  }
79  av_strstart(filename, "subfile:", &filename);
80  ret = ffurl_open_whitelist(&c->h, filename, flags, &h->interrupt_callback,
81  options, h->protocol_whitelist, h->protocol_blacklist, h);
82  if (ret < 0)
83  return ret;
84  c->pos = c->start;
85  if ((ret = slave_seek(h)) < 0) {
86  ffurl_close(c->h);
87  return ret;
88  }
89  return 0;
90 }
91 
93 {
95  return ffurl_close(c->h);
96 }
97 
98 static int subfile_read(URLContext *h, unsigned char *buf, int size)
99 {
100  SubfileContext *c = h->priv_data;
101  int64_t rest = c->end - c->pos;
102  int ret;
103 
104  if (rest <= 0)
105  return AVERROR_EOF;
106  size = FFMIN(size, rest);
107  ret = ffurl_read(c->h, buf, size);
108  if (ret >= 0)
109  c->pos += ret;
110  return ret;
111 }
112 
113 static int64_t subfile_seek(URLContext *h, int64_t pos, int whence)
114 {
115  SubfileContext *c = h->priv_data;
116  int64_t new_pos = -1;
117  int ret;
118 
119  if (whence == AVSEEK_SIZE)
120  return c->end - c->start;
121  switch (whence) {
122  case SEEK_SET:
123  new_pos = c->start + pos;
124  break;
125  case SEEK_CUR:
126  new_pos += pos;
127  break;
128  case SEEK_END:
129  new_pos = c->end + c->pos;
130  break;
131  }
132  if (new_pos < c->start)
133  return AVERROR(EINVAL);
134  c->pos = new_pos;
135  if ((ret = slave_seek(h)) < 0)
136  return ret;
137  return c->pos - c->start;
138 }
139 
141  .name = "subfile",
142  .url_open2 = subfile_open,
143  .url_read = subfile_read,
144  .url_seek = subfile_seek,
145  .url_close = subfile_close,
146  .priv_data_size = sizeof(SubfileContext),
147  .priv_data_class = &subfile_class,
148  .default_whitelist = "file",
149 };
static int slave_seek(URLContext *h)
Definition: subfile.c:54
#define NULL
Definition: coverity.c:32
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
AVOption.
Definition: opt.h:246
#define LIBAVUTIL_VERSION_INT
Definition: version.h:86
AVIOInterruptCB interrupt_callback
Definition: url.h:47
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
const URLProtocol ff_subfile_protocol
Definition: subfile.c:140
AVOptions.
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
static int flags
Definition: log.c:57
#define AVERROR_EOF
End of file.
Definition: error.h:55
ptrdiff_t size
Definition: opengl_enc.c:101
const OptionDef options[]
Definition: ffserver.c:3948
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static const AVOption subfile_options[]
Definition: subfile.c:38
static int subfile_read(URLContext *h, unsigned char *buf, int size)
Definition: subfile.c:98
const char * protocol_whitelist
Definition: url.h:49
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
simple assert() macros that are a bit more flexible than ISO C assert().
int64_t start
Definition: subfile.c:30
#define FFMIN(a, b)
Definition: common.h:96
int64_t pos
Definition: subfile.c:32
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
static int subfile_open(URLContext *h, const char *filename, int flags, AVDictionary **options)
Definition: subfile.c:69
URLContext * h
Definition: subfile.c:29
const char * protocol_blacklist
Definition: url.h:50
void * buf
Definition: avisynth_c.h:690
Definition: url.h:38
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
Describe the class of an AVClass context structure.
Definition: log.h:67
void * priv_data
Definition: url.h:41
#define OFFSET(field)
Definition: subfile.c:35
#define D
Definition: subfile.c:36
int64_t end
Definition: subfile.c:31
const char * name
Definition: url.h:55
int ffurl_close(URLContext *h)
Definition: avio.c:465
static int64_t subfile_seek(URLContext *h, int64_t pos, int whence)
Definition: subfile.c:113
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
Main libavformat public API header.
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:432
static int subfile_close(URLContext *h)
Definition: subfile.c:92
static double c[64]
#define AVSEEK_SIZE
ORing this as the "whence" parameter to a seek function causes it to return the filesize without seek...
Definition: avio.h:530
void INT64 start
Definition: avisynth_c.h:690
unbuffered private I/O API
static const AVClass subfile_class
Definition: subfile.c:47
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:405