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.

182 lines
5.6KB

  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_HEAD_SIZE 19
  32. static int opus_header(AVFormatContext *avf, int idx)
  33. {
  34. struct ogg *ogg = avf->priv_data;
  35. struct ogg_stream *os = &ogg->streams[idx];
  36. AVStream *st = avf->streams[idx];
  37. struct oggopus_private *priv = os->private;
  38. uint8_t *packet = os->buf + os->pstart;
  39. if (!priv) {
  40. priv = os->private = av_mallocz(sizeof(*priv));
  41. if (!priv)
  42. return AVERROR(ENOMEM);
  43. }
  44. if (os->flags & OGG_FLAG_BOS) {
  45. if (os->psize < OPUS_HEAD_SIZE || (AV_RL8(packet + 8) & 0xF0) != 0)
  46. return AVERROR_INVALIDDATA;
  47. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  48. st->codec->codec_id = AV_CODEC_ID_OPUS;
  49. st->codec->channels = AV_RL8 (packet + 9);
  50. priv->pre_skip = AV_RL16(packet + 10);
  51. st->codec->delay = priv->pre_skip;
  52. /*orig_sample_rate = AV_RL32(packet + 12);*/
  53. /*gain = AV_RL16(packet + 16);*/
  54. /*channel_map = AV_RL8 (packet + 18);*/
  55. if (ff_alloc_extradata(st->codec, os->psize))
  56. return AVERROR(ENOMEM);
  57. memcpy(st->codec->extradata, packet, os->psize);
  58. st->codec->sample_rate = 48000;
  59. avpriv_set_pts_info(st, 64, 1, 48000);
  60. priv->need_comments = 1;
  61. return 1;
  62. }
  63. if (priv->need_comments) {
  64. if (os->psize < 8 || memcmp(packet, "OpusTags", 8))
  65. return AVERROR_INVALIDDATA;
  66. ff_vorbis_comment(avf, &st->metadata, packet + 8, os->psize - 8);
  67. priv->need_comments--;
  68. return 1;
  69. }
  70. return 0;
  71. }
  72. static int opus_duration(uint8_t *src, int size)
  73. {
  74. unsigned nb_frames = 1;
  75. unsigned toc = src[0];
  76. unsigned toc_config = toc >> 3;
  77. unsigned toc_count = toc & 3;
  78. unsigned frame_size = toc_config < 12 ? FFMAX(480, 960 * (toc_config & 3)) :
  79. toc_config < 16 ? 480 << (toc_config & 1) :
  80. 120 << (toc_config & 3);
  81. if (toc_count == 3) {
  82. if (size<2)
  83. return AVERROR_INVALIDDATA;
  84. nb_frames = src[1] & 0x3F;
  85. } else if (toc_count) {
  86. nb_frames = 2;
  87. }
  88. return frame_size * nb_frames;
  89. }
  90. static int opus_packet(AVFormatContext *avf, int idx)
  91. {
  92. struct ogg *ogg = avf->priv_data;
  93. struct ogg_stream *os = &ogg->streams[idx];
  94. AVStream *st = avf->streams[idx];
  95. struct oggopus_private *priv = os->private;
  96. uint8_t *packet = os->buf + os->pstart;
  97. int ret;
  98. if (!os->psize)
  99. return AVERROR_INVALIDDATA;
  100. if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS)) {
  101. int seg, d;
  102. int duration;
  103. uint8_t *last_pkt = os->buf + os->pstart;
  104. uint8_t *next_pkt = last_pkt;
  105. duration = 0;
  106. seg = os->segp;
  107. d = opus_duration(last_pkt, os->psize);
  108. if (d < 0) {
  109. os->pflags |= AV_PKT_FLAG_CORRUPT;
  110. return 0;
  111. }
  112. duration += d;
  113. last_pkt = next_pkt = next_pkt + os->psize;
  114. for (; seg < os->nsegs; seg++) {
  115. if (os->segments[seg] < 255) {
  116. int d = opus_duration(last_pkt, os->segments[seg]);
  117. if (d < 0) {
  118. duration = os->granule;
  119. break;
  120. }
  121. duration += d;
  122. last_pkt = next_pkt + os->segments[seg];
  123. }
  124. next_pkt += os->segments[seg];
  125. }
  126. os->lastpts =
  127. os->lastdts = os->granule - duration;
  128. }
  129. if ((ret = opus_duration(packet, os->psize)) < 0)
  130. return ret;
  131. os->pduration = ret;
  132. if (os->lastpts != AV_NOPTS_VALUE) {
  133. if (st->start_time == AV_NOPTS_VALUE)
  134. st->start_time = os->lastpts;
  135. priv->cur_dts = os->lastdts = os->lastpts -= priv->pre_skip;
  136. }
  137. priv->cur_dts += os->pduration;
  138. if ((os->flags & OGG_FLAG_EOS)) {
  139. int64_t skip = priv->cur_dts - os->granule + priv->pre_skip;
  140. skip = FFMIN(skip, os->pduration);
  141. if (skip > 0) {
  142. os->pduration = skip < os->pduration ? os->pduration - skip : 1;
  143. os->end_trimming = skip;
  144. av_log(avf, AV_LOG_DEBUG,
  145. "Last packet was truncated to %d due to end trimming.\n",
  146. os->pduration);
  147. }
  148. }
  149. return 0;
  150. }
  151. const struct ogg_codec ff_opus_codec = {
  152. .name = "Opus",
  153. .magic = "OpusHead",
  154. .magicsize = 8,
  155. .header = opus_header,
  156. .packet = opus_packet,
  157. .nb_header = 1,
  158. };