2 * Copyright (c) 2013 Clément Bœsch
4 * This file is part of FFmpeg.
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.
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.
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
21 #include <quvi/quvi.h>
23 #include "libavformat/avformat.h"
24 #include "libavformat/internal.h"
25 #include "libavutil/avassert.h"
26 #include "libavutil/opt.h"
31 AVFormatContext *fmtctx;
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 },
41 static const AVClass libquvi_context_class = {
42 .class_name = "libquvi",
43 .item_name = av_default_item_name,
44 .option = libquvi_options,
45 .version = LIBAVUTIL_VERSION_INT,
48 static int libquvi_close(AVFormatContext *s)
50 LibQuviContext *qc = s->priv_data;
52 avformat_close_input(&qc->fmtctx);
56 static int libquvi_read_header(AVFormatContext *s)
62 LibQuviContext *qc = s->priv_data;
63 char *media_url, *pagetitle;
67 av_log(s, AV_LOG_ERROR, "%s\n", quvi_strerror(q, rc));
68 return AVERROR_EXTERNAL;
71 quvi_setopt(q, QUVIOPT_FORMAT, qc->format);
73 rc = quvi_parse(q, s->filename, &m);
75 av_log(s, AV_LOG_ERROR, "%s\n", quvi_strerror(q, rc));
76 ret = AVERROR_EXTERNAL;
80 rc = quvi_getprop(m, QUVIPROP_MEDIAURL, &media_url);
82 av_log(s, AV_LOG_ERROR, "%s\n", quvi_strerror(q, rc));
83 ret = AVERROR_EXTERNAL;
84 goto err_quvi_cleanup;
87 if (!(qc->fmtctx = avformat_alloc_context())) {
88 ret = AVERROR(ENOMEM);
89 goto err_quvi_cleanup;
92 if ((ret = ff_copy_whitelists(qc->fmtctx, s)) < 0) {
93 avformat_free_context(qc->fmtctx);
95 goto err_quvi_cleanup;
98 ret = avformat_open_input(&qc->fmtctx, media_url, NULL, NULL);
100 goto err_quvi_cleanup;
102 rc = quvi_getprop(m, QUVIPROP_PAGETITLE, &pagetitle);
104 av_dict_set(&s->metadata, "title", pagetitle, 0);
106 for (i = 0; i < qc->fmtctx->nb_streams; i++) {
107 AVStream *st = avformat_new_stream(s, NULL);
108 AVStream *ist = qc->fmtctx->streams[i];
110 ret = AVERROR(ENOMEM);
111 goto err_close_input;
113 avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den);
114 avcodec_copy_context(st->codec, qc->fmtctx->streams[i]->codec);
120 avformat_close_input(&qc->fmtctx);
122 quvi_parse_close(&m);
128 static int libquvi_read_packet(AVFormatContext *s, AVPacket *pkt)
130 LibQuviContext *qc = s->priv_data;
131 return av_read_frame(qc->fmtctx, pkt);
134 static int libquvi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
136 LibQuviContext *qc = s->priv_data;
137 return av_seek_frame(qc->fmtctx, stream_index, timestamp, flags);
140 static int libquvi_probe(AVProbeData *p)
148 return AVERROR(ENOMEM);
149 score = quvi_supported(q, (char *)p->filename) == QUVI_OK ? AVPROBE_SCORE_EXTENSION : 0;
154 AVInputFormat ff_libquvi_demuxer = {
156 .long_name = NULL_IF_CONFIG_SMALL("libquvi demuxer"),
157 .priv_data_size = sizeof(LibQuviContext),
158 .read_probe = libquvi_probe,
159 .read_header = libquvi_read_header,
160 .read_packet = libquvi_read_packet,
161 .read_close = libquvi_close,
162 .read_seek = libquvi_read_seek,
163 .priv_class = &libquvi_context_class,
164 .flags = AVFMT_NOFILE,