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.

147 lines
4.1KB

  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/opt.h"
  24. typedef struct {
  25. const AVClass *class;
  26. char *format;
  27. AVFormatContext *fmtctx;
  28. } LibQuviContext;
  29. #define OFFSET(x) offsetof(LibQuviContext, x)
  30. #define FLAGS AV_OPT_FLAG_DECODING_PARAM
  31. static const AVOption libquvi_options[] = {
  32. { "format", "request specific format", OFFSET(format), AV_OPT_TYPE_STRING, {.str="best"}, .flags = FLAGS },
  33. { NULL }
  34. };
  35. static const AVClass libquvi_context_class = {
  36. .class_name = "libquvi",
  37. .item_name = av_default_item_name,
  38. .option = libquvi_options,
  39. .version = LIBAVUTIL_VERSION_INT,
  40. };
  41. static int libquvi_close(AVFormatContext *s)
  42. {
  43. LibQuviContext *qc = s->priv_data;
  44. if (qc->fmtctx)
  45. avformat_close_input(&qc->fmtctx);
  46. return 0;
  47. }
  48. static int libquvi_read_header(AVFormatContext *s)
  49. {
  50. int i, ret;
  51. quvi_t q;
  52. quvi_media_t m;
  53. QUVIcode rc;
  54. LibQuviContext *qc = s->priv_data;
  55. char *media_url, *pagetitle;
  56. rc = quvi_init(&q);
  57. if (rc != QUVI_OK)
  58. goto quvi_fail;
  59. quvi_setopt(q, QUVIOPT_FORMAT, qc->format);
  60. rc = quvi_parse(q, s->filename, &m);
  61. if (rc != QUVI_OK)
  62. goto quvi_fail;
  63. rc = quvi_getprop(m, QUVIPROP_MEDIAURL, &media_url);
  64. if (rc != QUVI_OK)
  65. goto quvi_fail;
  66. ret = avformat_open_input(&qc->fmtctx, media_url, NULL, NULL);
  67. if (ret < 0)
  68. goto end;
  69. rc = quvi_getprop(m, QUVIPROP_PAGETITLE, &pagetitle);
  70. if (rc == QUVI_OK)
  71. av_dict_set(&s->metadata, "title", pagetitle, 0);
  72. for (i = 0; i < qc->fmtctx->nb_streams; i++) {
  73. AVStream *st = avformat_new_stream(s, NULL);
  74. AVStream *ist = qc->fmtctx->streams[i];
  75. if (!st) {
  76. ret = AVERROR(ENOMEM);
  77. goto end;
  78. }
  79. avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den);
  80. avcodec_copy_context(st->codec, qc->fmtctx->streams[i]->codec);
  81. }
  82. return 0;
  83. quvi_fail:
  84. av_log(s, AV_LOG_ERROR, "%s\n", quvi_strerror(q, rc));
  85. ret = AVERROR_EXTERNAL;
  86. end:
  87. quvi_parse_close(&m);
  88. quvi_close(&q);
  89. return ret;
  90. }
  91. static int libquvi_read_packet(AVFormatContext *s, AVPacket *pkt)
  92. {
  93. LibQuviContext *qc = s->priv_data;
  94. return av_read_frame(qc->fmtctx, pkt);
  95. }
  96. static int libquvi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  97. {
  98. LibQuviContext *qc = s->priv_data;
  99. return av_seek_frame(qc->fmtctx, stream_index, timestamp, flags);
  100. }
  101. static int libquvi_probe(AVProbeData *p)
  102. {
  103. int score;
  104. quvi_t q;
  105. QUVIcode rc;
  106. rc = quvi_init(&q);
  107. if (rc != QUVI_OK)
  108. return AVERROR(ENOMEM);
  109. score = quvi_supported(q, (char *)p->filename) == QUVI_OK ? AVPROBE_SCORE_EXTENSION : 0;
  110. quvi_close(&q);
  111. return score;
  112. }
  113. AVInputFormat ff_libquvi_demuxer = {
  114. .name = "libquvi",
  115. .long_name = NULL_IF_CONFIG_SMALL("libquvi demuxer"),
  116. .priv_data_size = sizeof(LibQuviContext),
  117. .read_probe = libquvi_probe,
  118. .read_header = libquvi_read_header,
  119. .read_packet = libquvi_read_packet,
  120. .read_close = libquvi_close,
  121. .read_seek = libquvi_read_seek,
  122. .priv_class = &libquvi_context_class,
  123. .flags = AVFMT_NOFILE,
  124. };