oggparseopus.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. uint8_t *extradata;
  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. /*orig_sample_rate = AV_RL32(packet + 12);*/
  53. /*gain = AV_RL16(packet + 16);*/
  54. /*channel_map = AV_RL8 (packet + 18);*/
  55. extradata = av_malloc(os->psize + FF_INPUT_BUFFER_PADDING_SIZE);
  56. if (!extradata)
  57. return AVERROR(ENOMEM);
  58. memcpy(extradata, packet, os->psize);
  59. st->codec->extradata = extradata;
  60. st->codec->extradata_size = os->psize;
  61. st->codec->sample_rate = 48000;
  62. avpriv_set_pts_info(st, 64, 1, 48000);
  63. priv->need_comments = 1;
  64. return 1;
  65. }
  66. if (priv->need_comments) {
  67. if (os->psize < 8 || memcmp(packet, "OpusTags", 8))
  68. return AVERROR_INVALIDDATA;
  69. ff_vorbis_comment(avf, &st->metadata, packet + 8, os->psize - 8);
  70. priv->need_comments--;
  71. return 1;
  72. }
  73. return 0;
  74. }
  75. static int opus_packet(AVFormatContext *avf, int idx)
  76. {
  77. struct ogg *ogg = avf->priv_data;
  78. struct ogg_stream *os = &ogg->streams[idx];
  79. AVStream *st = avf->streams[idx];
  80. struct oggopus_private *priv = os->private;
  81. uint8_t *packet = os->buf + os->pstart;
  82. unsigned toc, toc_config, toc_count, frame_size, nb_frames = 1;
  83. if (!os->psize)
  84. return AVERROR_INVALIDDATA;
  85. toc = *packet;
  86. toc_config = toc >> 3;
  87. toc_count = toc & 3;
  88. frame_size = toc_config < 12 ? FFMAX(480, 960 * (toc_config & 3)) :
  89. toc_config < 16 ? 480 << (toc_config & 1) :
  90. 120 << (toc_config & 3);
  91. if (toc_count == 3) {
  92. if (os->psize < 2)
  93. return AVERROR_INVALIDDATA;
  94. nb_frames = packet[1] & 0x3F;
  95. } else if (toc_count) {
  96. nb_frames = 2;
  97. }
  98. os->pduration = frame_size * nb_frames;
  99. if (os->lastpts != AV_NOPTS_VALUE) {
  100. if (st->start_time == AV_NOPTS_VALUE)
  101. st->start_time = os->lastpts;
  102. priv->cur_dts = os->lastdts = os->lastpts -= priv->pre_skip;
  103. }
  104. priv->cur_dts += os->pduration;
  105. if ((os->flags & OGG_FLAG_EOS)) {
  106. int64_t skip = priv->cur_dts - os->granule + priv->pre_skip;
  107. skip = FFMIN(skip, os->pduration);
  108. if (skip > 0) {
  109. os->pduration = skip < os->pduration ? os->pduration - skip : 1;
  110. av_log(avf, AV_LOG_WARNING,
  111. "Last packet must be truncated to %d (unimplemented).\n",
  112. os->pduration);
  113. }
  114. }
  115. return 0;
  116. }
  117. const struct ogg_codec ff_opus_codec = {
  118. .name = "Opus",
  119. .magic = "OpusHead",
  120. .magicsize = 8,
  121. .header = opus_header,
  122. .packet = opus_packet,
  123. };