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.

183 lines
5.8KB

  1. /*
  2. * Opus parser for Ogg
  3. * Copyright (c) 2012 Nicolas George
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <string.h>
  22. #include "libavutil/intreadwrite.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #include "oggdec.h"
  26. struct oggopus_private {
  27. int need_comments;
  28. unsigned pre_skip;
  29. int64_t cur_dts;
  30. };
  31. #define OPUS_SEEK_PREROLL_MS 80
  32. #define OPUS_HEAD_SIZE 19
  33. static int opus_header(AVFormatContext *avf, int idx)
  34. {
  35. struct ogg *ogg = avf->priv_data;
  36. struct ogg_stream *os = &ogg->streams[idx];
  37. AVStream *st = avf->streams[idx];
  38. struct oggopus_private *priv = os->private;
  39. uint8_t *packet = os->buf + os->pstart;
  40. if (!priv) {
  41. priv = os->private = av_mallocz(sizeof(*priv));
  42. if (!priv)
  43. return AVERROR(ENOMEM);
  44. }
  45. if (os->flags & OGG_FLAG_BOS) {
  46. if (os->psize < OPUS_HEAD_SIZE || (AV_RL8(packet + 8) & 0xF0) != 0)
  47. return AVERROR_INVALIDDATA;
  48. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  49. st->codec->codec_id = AV_CODEC_ID_OPUS;
  50. st->codec->channels = AV_RL8 (packet + 9);
  51. priv->pre_skip = AV_RL16(packet + 10);
  52. st->codec->delay = priv->pre_skip;
  53. /*orig_sample_rate = AV_RL32(packet + 12);*/
  54. /*gain = AV_RL16(packet + 16);*/
  55. /*channel_map = AV_RL8 (packet + 18);*/
  56. if (ff_alloc_extradata(st->codec, os->psize))
  57. return AVERROR(ENOMEM);
  58. memcpy(st->codec->extradata, packet, os->psize);
  59. st->codec->sample_rate = 48000;
  60. av_codec_set_seek_preroll(st->codec,
  61. av_rescale(OPUS_SEEK_PREROLL_MS,
  62. st->codec->sample_rate, 1000));
  63. avpriv_set_pts_info(st, 64, 1, 48000);
  64. priv->need_comments = 1;
  65. return 1;
  66. }
  67. if (priv->need_comments) {
  68. if (os->psize < 8 || memcmp(packet, "OpusTags", 8))
  69. return AVERROR_INVALIDDATA;
  70. ff_vorbis_comment(avf, &st->metadata, packet + 8, os->psize - 8, 1);
  71. priv->need_comments--;
  72. return 1;
  73. }
  74. return 0;
  75. }
  76. static int opus_duration(uint8_t *src, int size)
  77. {
  78. unsigned nb_frames = 1;
  79. unsigned toc = src[0];
  80. unsigned toc_config = toc >> 3;
  81. unsigned toc_count = toc & 3;
  82. unsigned frame_size = toc_config < 12 ? FFMAX(480, 960 * (toc_config & 3)) :
  83. toc_config < 16 ? 480 << (toc_config & 1) :
  84. 120 << (toc_config & 3);
  85. if (toc_count == 3) {
  86. if (size<2)
  87. return AVERROR_INVALIDDATA;
  88. nb_frames = src[1] & 0x3F;
  89. } else if (toc_count) {
  90. nb_frames = 2;
  91. }
  92. return frame_size * nb_frames;
  93. }
  94. static int opus_packet(AVFormatContext *avf, int idx)
  95. {
  96. struct ogg *ogg = avf->priv_data;
  97. struct ogg_stream *os = &ogg->streams[idx];
  98. AVStream *st = avf->streams[idx];
  99. struct oggopus_private *priv = os->private;
  100. uint8_t *packet = os->buf + os->pstart;
  101. int ret;
  102. if (!os->psize)
  103. return AVERROR_INVALIDDATA;
  104. if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS)) {
  105. int seg, d;
  106. int duration;
  107. uint8_t *last_pkt = os->buf + os->pstart;
  108. uint8_t *next_pkt = last_pkt;
  109. duration = 0;
  110. seg = os->segp;
  111. d = opus_duration(last_pkt, os->psize);
  112. if (d < 0) {
  113. os->pflags |= AV_PKT_FLAG_CORRUPT;
  114. return 0;
  115. }
  116. duration += d;
  117. last_pkt = next_pkt = next_pkt + os->psize;
  118. for (; seg < os->nsegs; seg++) {
  119. next_pkt += os->segments[seg];
  120. if (os->segments[seg] < 255 && next_pkt != last_pkt) {
  121. int d = opus_duration(last_pkt, next_pkt - last_pkt);
  122. if (d > 0)
  123. duration += d;
  124. last_pkt = next_pkt;
  125. }
  126. }
  127. os->lastpts =
  128. os->lastdts = os->granule - duration;
  129. }
  130. if ((ret = opus_duration(packet, os->psize)) < 0)
  131. return ret;
  132. os->pduration = ret;
  133. if (os->lastpts != AV_NOPTS_VALUE) {
  134. if (st->start_time == AV_NOPTS_VALUE)
  135. st->start_time = os->lastpts;
  136. priv->cur_dts = os->lastdts = os->lastpts -= priv->pre_skip;
  137. }
  138. priv->cur_dts += os->pduration;
  139. if ((os->flags & OGG_FLAG_EOS)) {
  140. int64_t skip = priv->cur_dts - os->granule + priv->pre_skip;
  141. skip = FFMIN(skip, os->pduration);
  142. if (skip > 0) {
  143. os->pduration = skip < os->pduration ? os->pduration - skip : 1;
  144. os->end_trimming = skip;
  145. av_log(avf, AV_LOG_DEBUG,
  146. "Last packet was truncated to %d due to end trimming.\n",
  147. os->pduration);
  148. }
  149. }
  150. return 0;
  151. }
  152. const struct ogg_codec ff_opus_codec = {
  153. .name = "Opus",
  154. .magic = "OpusHead",
  155. .magicsize = 8,
  156. .header = opus_header,
  157. .packet = opus_packet,
  158. .nb_header = 1,
  159. };