FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libquvi.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Clément Bœsch
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
8  * License 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <quvi/quvi.h>
22 
23 #include "libavformat/avformat.h"
24 #include "libavformat/internal.h"
25 #include "libavutil/avassert.h"
26 #include "libavutil/opt.h"
27 
28 typedef struct {
29  const AVClass *class;
30  char *format;
33 
34 #define OFFSET(x) offsetof(LibQuviContext, x)
35 #define FLAGS AV_OPT_FLAG_DECODING_PARAM
36 static const AVOption libquvi_options[] = {
37  { "format", "request specific format", OFFSET(format), AV_OPT_TYPE_STRING, {.str="best"}, .flags = FLAGS },
38  { NULL }
39 };
40 
42  .class_name = "libquvi",
43  .item_name = av_default_item_name,
44  .option = libquvi_options,
45  .version = LIBAVUTIL_VERSION_INT,
46 };
47 
49 {
50  LibQuviContext *qc = s->priv_data;
51  if (qc->fmtctx)
53  return 0;
54 }
55 
57 {
58  int i, ret;
59  quvi_t q;
60  quvi_media_t m;
61  QUVIcode rc;
62  LibQuviContext *qc = s->priv_data;
63  char *media_url, *pagetitle;
64 
65  rc = quvi_init(&q);
66  if (rc != QUVI_OK) {
67  av_log(s, AV_LOG_ERROR, "%s\n", quvi_strerror(q, rc));
68  return AVERROR_EXTERNAL;
69  }
70 
71  quvi_setopt(q, QUVIOPT_FORMAT, qc->format);
72 
73  rc = quvi_parse(q, s->filename, &m);
74  if (rc != QUVI_OK) {
75  av_log(s, AV_LOG_ERROR, "%s\n", quvi_strerror(q, rc));
76  ret = AVERROR_EXTERNAL;
77  goto err_quvi_close;
78  }
79 
80  rc = quvi_getprop(m, QUVIPROP_MEDIAURL, &media_url);
81  if (rc != QUVI_OK) {
82  av_log(s, AV_LOG_ERROR, "%s\n", quvi_strerror(q, rc));
83  ret = AVERROR_EXTERNAL;
84  goto err_quvi_cleanup;
85  }
86 
87  if (!(qc->fmtctx = avformat_alloc_context())) {
88  ret = AVERROR(ENOMEM);
89  goto err_quvi_cleanup;
90  }
91 
92  if ((ret = ff_copy_whitelists(qc->fmtctx, s)) < 0) {
94  qc->fmtctx = NULL;
95  goto err_quvi_cleanup;
96  }
97 
98  if (!qc->fmtctx->format_whitelist) {
99  qc->fmtctx->format_whitelist = av_strdup("avi,asf,flv,mov,mpeg,mpegts,aac,h264,hevc,mp3,ogg,matroska,mxf,mp2");
100  if (!qc->fmtctx->format_whitelist) {
102  qc->fmtctx = NULL;
103  goto err_quvi_cleanup;
104  }
105  }
106  if (strncmp(media_url, "http:", 5) && strncmp(media_url, "https:", 6)) {
108  qc->fmtctx = NULL;
109  goto err_quvi_cleanup;
110  }
111 
112  ret = avformat_open_input(&qc->fmtctx, media_url, NULL, NULL);
113  if (ret < 0)
114  goto err_quvi_cleanup;
115 
116  rc = quvi_getprop(m, QUVIPROP_PAGETITLE, &pagetitle);
117  if (rc == QUVI_OK)
118  av_dict_set(&s->metadata, "title", pagetitle, 0);
119 
120  for (i = 0; i < qc->fmtctx->nb_streams; i++) {
122  AVStream *ist = qc->fmtctx->streams[i];
123  if (!st) {
124  ret = AVERROR(ENOMEM);
125  goto err_close_input;
126  }
129  }
130 
131  return 0;
132 
133  err_close_input:
135  err_quvi_cleanup:
136  quvi_parse_close(&m);
137  err_quvi_close:
138  quvi_close(&q);
139  return ret;
140 }
141 
143 {
144  LibQuviContext *qc = s->priv_data;
145  return av_read_frame(qc->fmtctx, pkt);
146 }
147 
148 static int libquvi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
149 {
150  LibQuviContext *qc = s->priv_data;
151  return av_seek_frame(qc->fmtctx, stream_index, timestamp, flags);
152 }
153 
155 {
156  int score;
157  quvi_t q;
158  QUVIcode rc;
159 
160  rc = quvi_init(&q);
161  if (rc != QUVI_OK)
162  return AVERROR(ENOMEM);
163  score = quvi_supported(q, (char *)p->filename) == QUVI_OK ? AVPROBE_SCORE_EXTENSION : 0;
164  quvi_close(&q);
165  return score;
166 }
167 
169  .name = "libquvi",
170  .long_name = NULL_IF_CONFIG_SMALL("libquvi demuxer"),
171  .priv_data_size = sizeof(LibQuviContext),
177  .priv_class = &libquvi_context_class,
178  .flags = AVFMT_NOFILE,
179 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
AVFormatContext * fmtctx
Definition: libquvi.c:31
AVOption.
Definition: opt.h:245
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
const char * filename
Definition: avformat.h:449
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4133
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
int num
numerator
Definition: rational.h:44
char * format
Definition: libquvi.c:30
static AVPacket pkt
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
Definition: options.c:182
Format I/O context.
Definition: avformat.h:1302
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
AVOptions.
static int libquvi_read_header(AVFormatContext *s)
Definition: libquvi.c:56
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3789
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1370
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:111
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
#define av_log(a,...)
static int libquvi_probe(AVProbeData *p)
Definition: libquvi.c:154
unsigned m
Definition: audioconvert.c:187
static const AVOption libquvi_options[]
Definition: libquvi.c:36
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1516
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
simple assert() macros that are a bit more flexible than ISO C assert().
char * format_whitelist
',' separated list of allowed demuxers.
Definition: avformat.h:1723
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:884
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1358
static const AVClass libquvi_context_class
Definition: libquvi.c:41
char filename[1024]
input or output filename
Definition: avformat.h:1378
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
#define OFFSET(x)
Definition: libquvi.c:34
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:638
Stream structure.
Definition: avformat.h:865
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
static int libquvi_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: libquvi.c:142
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
static const char * format
Definition: movenc-test.c:47
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:69
Describe the class of an AVClass context structure.
Definition: log.h:67
static int libquvi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: libquvi.c:148
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:458
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:3725
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1493
int ff_copy_whitelists(AVFormatContext *dst, AVFormatContext *src)
Copies the whilelists from one context to the other.
Definition: utils.c:140
static int flags
Definition: cpu.c:47
int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Seek to the keyframe at timestamp.
Definition: utils.c:2219
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:465
AVInputFormat ff_libquvi_demuxer
Definition: libquvi.c:168
int pts_wrap_bits
number of bits in pts (used for wrapping control)
Definition: avformat.h:1031
int den
denominator
Definition: rational.h:45
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:3761
void * priv_data
Format private data.
Definition: avformat.h:1330
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:414
static int libquvi_close(AVFormatContext *s)
Definition: libquvi.c:48
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:649
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:907
#define FLAGS
Definition: libquvi.c:35
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
This structure stores compressed data.
Definition: avcodec.h:1443