You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.2KB

  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. #include <quvi/quvi.h>
  21. #include "libavformat/avformat.h"
  22. #include "libavformat/internal.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/opt.h"
  25. typedef struct {
  26. const AVClass *class;
  27. char *format;
  28. AVFormatContext *fmtctx;
  29. } LibQuviContext;
  30. #define OFFSET(x) offsetof(LibQuviContext, x)
  31. #define FLAGS AV_OPT_FLAG_DECODING_PARAM
  32. static const AVOption libquvi_options[] = {
  33. { "format", "request specific format", OFFSET(format), AV_OPT_TYPE_STRING, {.str="best"}, .flags = FLAGS },
  34. { NULL }
  35. };
  36. static const AVClass libquvi_context_class = {
  37. .class_name = "libquvi",
  38. .item_name = av_default_item_name,
  39. .option = libquvi_options,
  40. .version = LIBAVUTIL_VERSION_INT,
  41. };
  42. static int libquvi_close(AVFormatContext *s)
  43. {
  44. LibQuviContext *qc = s->priv_data;
  45. if (qc->fmtctx)
  46. avformat_close_input(&qc->fmtctx);
  47. return 0;
  48. }
  49. static int libquvi_read_header(AVFormatContext *s)
  50. {
  51. int i, ret;
  52. quvi_t q;
  53. quvi_media_t m;
  54. QUVIcode rc;
  55. LibQuviContext *qc = s->priv_data;
  56. char *media_url, *pagetitle;
  57. rc = quvi_init(&q);
  58. if (rc != QUVI_OK) {
  59. av_log(s, AV_LOG_ERROR, "%s\n", quvi_strerror(q, rc));
  60. return AVERROR_EXTERNAL;
  61. }
  62. quvi_setopt(q, QUVIOPT_FORMAT, qc->format);
  63. rc = quvi_parse(q, s->filename, &m);
  64. if (rc != QUVI_OK) {
  65. av_log(s, AV_LOG_ERROR, "%s\n", quvi_strerror(q, rc));
  66. ret = AVERROR_EXTERNAL;
  67. goto err_quvi_close;
  68. }
  69. rc = quvi_getprop(m, QUVIPROP_MEDIAURL, &media_url);
  70. if (rc != QUVI_OK) {
  71. av_log(s, AV_LOG_ERROR, "%s\n", quvi_strerror(q, rc));
  72. ret = AVERROR_EXTERNAL;
  73. goto err_quvi_cleanup;
  74. }
  75. if (!(qc->fmtctx = avformat_alloc_context())) {
  76. ret = AVERROR(ENOMEM);
  77. goto err_quvi_cleanup;
  78. }
  79. if ((ret = ff_copy_whitelists(qc->fmtctx, s)) < 0) {
  80. avformat_free_context(qc->fmtctx);
  81. qc->fmtctx = NULL;
  82. goto err_quvi_cleanup;
  83. }
  84. if (!qc->fmtctx->format_whitelist) {
  85. qc->fmtctx->format_whitelist = av_strdup("avi,asf,flv,mov,mpeg,mpegts,aac,h264,hevc,mp3,ogg,matroska,mxf,mp2");
  86. if (!qc->fmtctx->format_whitelist) {
  87. avformat_free_context(qc->fmtctx);
  88. qc->fmtctx = NULL;
  89. goto err_quvi_cleanup;
  90. }
  91. }
  92. if (strncmp(media_url, "http:", 5) && strncmp(media_url, "https:", 6)) {
  93. avformat_free_context(qc->fmtctx);
  94. qc->fmtctx = NULL;
  95. goto err_quvi_cleanup;
  96. }
  97. ret = avformat_open_input(&qc->fmtctx, media_url, NULL, NULL);
  98. if (ret < 0)
  99. goto err_quvi_cleanup;
  100. rc = quvi_getprop(m, QUVIPROP_PAGETITLE, &pagetitle);
  101. if (rc == QUVI_OK)
  102. av_dict_set(&s->metadata, "title", pagetitle, 0);
  103. for (i = 0; i < qc->fmtctx->nb_streams; i++) {
  104. AVStream *st = avformat_new_stream(s, NULL);
  105. AVStream *ist = qc->fmtctx->streams[i];
  106. if (!st) {
  107. ret = AVERROR(ENOMEM);
  108. goto err_close_input;
  109. }
  110. avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den);
  111. avcodec_copy_context(st->codec, qc->fmtctx->streams[i]->codec);
  112. }
  113. return 0;
  114. err_close_input:
  115. avformat_close_input(&qc->fmtctx);
  116. err_quvi_cleanup:
  117. quvi_parse_close(&m);
  118. err_quvi_close:
  119. quvi_close(&q);
  120. return ret;
  121. }
  122. static int libquvi_read_packet(AVFormatContext *s, AVPacket *pkt)
  123. {
  124. LibQuviContext *qc = s->priv_data;
  125. return av_read_frame(qc->fmtctx, pkt);
  126. }
  127. static int libquvi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  128. {
  129. LibQuviContext *qc = s->priv_data;
  130. return av_seek_frame(qc->fmtctx, stream_index, timestamp, flags);
  131. }
  132. static int libquvi_probe(AVProbeData *p)
  133. {
  134. int score;
  135. quvi_t q;
  136. QUVIcode rc;
  137. rc = quvi_init(&q);
  138. if (rc != QUVI_OK)
  139. return AVERROR(ENOMEM);
  140. score = quvi_supported(q, (char *)p->filename) == QUVI_OK ? AVPROBE_SCORE_EXTENSION : 0;
  141. quvi_close(&q);
  142. return score;
  143. }
  144. AVInputFormat ff_libquvi_demuxer = {
  145. .name = "libquvi",
  146. .long_name = NULL_IF_CONFIG_SMALL("libquvi demuxer"),
  147. .priv_data_size = sizeof(LibQuviContext),
  148. .read_probe = libquvi_probe,
  149. .read_header = libquvi_read_header,
  150. .read_packet = libquvi_read_packet,
  151. .read_close = libquvi_close,
  152. .read_seek = libquvi_read_seek,
  153. .priv_class = &libquvi_context_class,
  154. .flags = AVFMT_NOFILE,
  155. };