yading@11
|
1 /*
|
yading@11
|
2 * Copyright (c) 2013 Clément Bœsch
|
yading@11
|
3 *
|
yading@11
|
4 * This file is part of FFmpeg.
|
yading@11
|
5 *
|
yading@11
|
6 * FFmpeg is free software; you can redistribute it and/or
|
yading@11
|
7 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
8 * License as published by the Free Software Foundation; either
|
yading@11
|
9 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
10 *
|
yading@11
|
11 * FFmpeg is distributed in the hope that it will be useful,
|
yading@11
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
14 * Lesser General Public License for more details.
|
yading@11
|
15 *
|
yading@11
|
16 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
17 * License along with FFmpeg; if not, write to the Free Software
|
yading@11
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
19 */
|
yading@11
|
20
|
yading@11
|
21 #include <quvi/quvi.h>
|
yading@11
|
22
|
yading@11
|
23 #include "libavformat/avformat.h"
|
yading@11
|
24 #include "libavformat/internal.h"
|
yading@11
|
25 #include "libavutil/opt.h"
|
yading@11
|
26
|
yading@11
|
27 typedef struct {
|
yading@11
|
28 const AVClass *class;
|
yading@11
|
29 char *format;
|
yading@11
|
30 AVFormatContext *fmtctx;
|
yading@11
|
31 } LibQuviContext;
|
yading@11
|
32
|
yading@11
|
33 #define OFFSET(x) offsetof(LibQuviContext, x)
|
yading@11
|
34 #define FLAGS AV_OPT_FLAG_DECODING_PARAM
|
yading@11
|
35 static const AVOption libquvi_options[] = {
|
yading@11
|
36 { "format", "request specific format", OFFSET(format), AV_OPT_TYPE_STRING, {.str="best"}, .flags = FLAGS },
|
yading@11
|
37 { NULL }
|
yading@11
|
38 };
|
yading@11
|
39
|
yading@11
|
40 static const AVClass libquvi_context_class = {
|
yading@11
|
41 .class_name = "libquvi",
|
yading@11
|
42 .item_name = av_default_item_name,
|
yading@11
|
43 .option = libquvi_options,
|
yading@11
|
44 .version = LIBAVUTIL_VERSION_INT,
|
yading@11
|
45 };
|
yading@11
|
46
|
yading@11
|
47 static int libquvi_close(AVFormatContext *s)
|
yading@11
|
48 {
|
yading@11
|
49 LibQuviContext *qc = s->priv_data;
|
yading@11
|
50 if (qc->fmtctx)
|
yading@11
|
51 avformat_close_input(&qc->fmtctx);
|
yading@11
|
52 return 0;
|
yading@11
|
53 }
|
yading@11
|
54
|
yading@11
|
55 static int libquvi_read_header(AVFormatContext *s)
|
yading@11
|
56 {
|
yading@11
|
57 int i, ret;
|
yading@11
|
58 quvi_t q;
|
yading@11
|
59 quvi_media_t m;
|
yading@11
|
60 QUVIcode rc;
|
yading@11
|
61 LibQuviContext *qc = s->priv_data;
|
yading@11
|
62 char *media_url, *pagetitle;
|
yading@11
|
63
|
yading@11
|
64 rc = quvi_init(&q);
|
yading@11
|
65 if (rc != QUVI_OK)
|
yading@11
|
66 goto quvi_fail;
|
yading@11
|
67
|
yading@11
|
68 quvi_setopt(q, QUVIOPT_FORMAT, qc->format);
|
yading@11
|
69
|
yading@11
|
70 rc = quvi_parse(q, s->filename, &m);
|
yading@11
|
71 if (rc != QUVI_OK)
|
yading@11
|
72 goto quvi_fail;
|
yading@11
|
73
|
yading@11
|
74 rc = quvi_getprop(m, QUVIPROP_MEDIAURL, &media_url);
|
yading@11
|
75 if (rc != QUVI_OK)
|
yading@11
|
76 goto quvi_fail;
|
yading@11
|
77
|
yading@11
|
78 ret = avformat_open_input(&qc->fmtctx, media_url, NULL, NULL);
|
yading@11
|
79 if (ret < 0)
|
yading@11
|
80 goto end;
|
yading@11
|
81
|
yading@11
|
82 rc = quvi_getprop(m, QUVIPROP_PAGETITLE, &pagetitle);
|
yading@11
|
83 if (rc == QUVI_OK)
|
yading@11
|
84 av_dict_set(&s->metadata, "title", pagetitle, 0);
|
yading@11
|
85
|
yading@11
|
86 for (i = 0; i < qc->fmtctx->nb_streams; i++) {
|
yading@11
|
87 AVStream *st = avformat_new_stream(s, NULL);
|
yading@11
|
88 AVStream *ist = qc->fmtctx->streams[i];
|
yading@11
|
89 if (!st) {
|
yading@11
|
90 ret = AVERROR(ENOMEM);
|
yading@11
|
91 goto end;
|
yading@11
|
92 }
|
yading@11
|
93 avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den);
|
yading@11
|
94 avcodec_copy_context(st->codec, qc->fmtctx->streams[i]->codec);
|
yading@11
|
95 }
|
yading@11
|
96
|
yading@11
|
97 return 0;
|
yading@11
|
98
|
yading@11
|
99 quvi_fail:
|
yading@11
|
100 av_log(s, AV_LOG_ERROR, "%s\n", quvi_strerror(q, rc));
|
yading@11
|
101 ret = AVERROR_EXTERNAL;
|
yading@11
|
102
|
yading@11
|
103 end:
|
yading@11
|
104 quvi_parse_close(&m);
|
yading@11
|
105 quvi_close(&q);
|
yading@11
|
106 return ret;
|
yading@11
|
107 }
|
yading@11
|
108
|
yading@11
|
109 static int libquvi_read_packet(AVFormatContext *s, AVPacket *pkt)
|
yading@11
|
110 {
|
yading@11
|
111 LibQuviContext *qc = s->priv_data;
|
yading@11
|
112 return av_read_frame(qc->fmtctx, pkt);
|
yading@11
|
113 }
|
yading@11
|
114
|
yading@11
|
115 static int libquvi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
|
yading@11
|
116 {
|
yading@11
|
117 LibQuviContext *qc = s->priv_data;
|
yading@11
|
118 return av_seek_frame(qc->fmtctx, stream_index, timestamp, flags);
|
yading@11
|
119 }
|
yading@11
|
120
|
yading@11
|
121 static int libquvi_probe(AVProbeData *p)
|
yading@11
|
122 {
|
yading@11
|
123 int score;
|
yading@11
|
124 quvi_t q;
|
yading@11
|
125 QUVIcode rc;
|
yading@11
|
126
|
yading@11
|
127 rc = quvi_init(&q);
|
yading@11
|
128 if (rc != QUVI_OK)
|
yading@11
|
129 return AVERROR(ENOMEM);
|
yading@11
|
130 score = quvi_supported(q, (char *)p->filename) == QUVI_OK ? AVPROBE_SCORE_MAX/2 : 0;
|
yading@11
|
131 quvi_close(&q);
|
yading@11
|
132 return score;
|
yading@11
|
133 }
|
yading@11
|
134
|
yading@11
|
135 AVInputFormat ff_libquvi_demuxer = {
|
yading@11
|
136 .name = "libquvi",
|
yading@11
|
137 .long_name = NULL_IF_CONFIG_SMALL("libquvi demuxer"),
|
yading@11
|
138 .priv_data_size = sizeof(LibQuviContext),
|
yading@11
|
139 .read_probe = libquvi_probe,
|
yading@11
|
140 .read_header = libquvi_read_header,
|
yading@11
|
141 .read_packet = libquvi_read_packet,
|
yading@11
|
142 .read_close = libquvi_close,
|
yading@11
|
143 .read_seek = libquvi_read_seek,
|
yading@11
|
144 .priv_class = &libquvi_context_class,
|
yading@11
|
145 .flags = AVFMT_NOFILE,
|
yading@11
|
146 };
|