LCOV - code coverage report
Current view: top level - libavformat - subfile.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 59 0.0 %
Date: 2017-12-10 21:22:29 Functions: 0 5 0.0 %

          Line data    Source code
       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;
      29             :     URLContext *h;
      30             :     int64_t start;
      31             :     int64_t end;
      32             :     int64_t pos;
      33             : } SubfileContext;
      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           0 : static int slave_seek(URLContext *h)
      55             : {
      56           0 :     SubfileContext *c = h->priv_data;
      57             :     int64_t ret;
      58             : 
      59           0 :     if ((ret = ffurl_seek(c->h, c->pos, SEEK_SET)) != c->pos) {
      60           0 :         if (ret >= 0)
      61           0 :             ret = AVERROR_BUG;
      62           0 :         av_log(h, AV_LOG_ERROR, "Impossible to seek in file: %s\n",
      63           0 :                av_err2str(ret));
      64           0 :         return ret;
      65             :     }
      66           0 :     return 0;
      67             : }
      68             : 
      69           0 : static int subfile_open(URLContext *h, const char *filename, int flags,
      70             :                         AVDictionary **options)
      71             : {
      72           0 :     SubfileContext *c = h->priv_data;
      73             :     int ret;
      74             : 
      75           0 :     if (!c->end)
      76           0 :         c->end = INT64_MAX;
      77             : 
      78           0 :     if (c->end <= c->start) {
      79           0 :         av_log(h, AV_LOG_ERROR, "end before start\n");
      80           0 :         return AVERROR(EINVAL);
      81             :     }
      82           0 :     av_strstart(filename, "subfile:", &filename);
      83           0 :     ret = ffurl_open_whitelist(&c->h, filename, flags, &h->interrupt_callback,
      84             :                                options, h->protocol_whitelist, h->protocol_blacklist, h);
      85           0 :     if (ret < 0)
      86           0 :         return ret;
      87           0 :     c->pos = c->start;
      88           0 :     if ((ret = slave_seek(h)) < 0) {
      89           0 :         ffurl_close(c->h);
      90           0 :         return ret;
      91             :     }
      92           0 :     return 0;
      93             : }
      94             : 
      95           0 : static int subfile_close(URLContext *h)
      96             : {
      97           0 :     SubfileContext *c = h->priv_data;
      98           0 :     return ffurl_close(c->h);
      99             : }
     100             : 
     101           0 : static int subfile_read(URLContext *h, unsigned char *buf, int size)
     102             : {
     103           0 :     SubfileContext *c = h->priv_data;
     104           0 :     int64_t rest = c->end - c->pos;
     105             :     int ret;
     106             : 
     107           0 :     if (rest <= 0)
     108           0 :         return AVERROR_EOF;
     109           0 :     size = FFMIN(size, rest);
     110           0 :     ret = ffurl_read(c->h, buf, size);
     111           0 :     if (ret >= 0)
     112           0 :         c->pos += ret;
     113           0 :     return ret;
     114             : }
     115             : 
     116           0 : static int64_t subfile_seek(URLContext *h, int64_t pos, int whence)
     117             : {
     118           0 :     SubfileContext *c = h->priv_data;
     119           0 :     int64_t new_pos = -1;
     120             :     int ret;
     121             : 
     122           0 :     if (whence == AVSEEK_SIZE)
     123           0 :         return c->end - c->start;
     124           0 :     switch (whence) {
     125           0 :     case SEEK_SET:
     126           0 :         new_pos = c->start + pos;
     127           0 :         break;
     128           0 :     case SEEK_CUR:
     129           0 :         new_pos += pos;
     130           0 :         break;
     131           0 :     case SEEK_END:
     132           0 :         new_pos = c->end + c->pos;
     133           0 :         break;
     134             :     }
     135           0 :     if (new_pos < c->start)
     136           0 :         return AVERROR(EINVAL);
     137           0 :     c->pos = new_pos;
     138           0 :     if ((ret = slave_seek(h)) < 0)
     139           0 :         return ret;
     140           0 :     return c->pos - c->start;
     141             : }
     142             : 
     143             : const URLProtocol ff_subfile_protocol = {
     144             :     .name                = "subfile",
     145             :     .url_open2           = subfile_open,
     146             :     .url_read            = subfile_read,
     147             :     .url_seek            = subfile_seek,
     148             :     .url_close           = subfile_close,
     149             :     .priv_data_size      = sizeof(SubfileContext),
     150             :     .priv_data_class     = &subfile_class,
     151             :     .default_whitelist   = "file",
     152             : };

Generated by: LCOV version 1.13