oggparsecelt.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Xiph CELT parser for Ogg
  3. * Copyright (c) 2011 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 oggcelt_private {
  27. int extra_headers_left;
  28. };
  29. static int celt_header(AVFormatContext *s, int idx)
  30. {
  31. struct ogg *ogg = s->priv_data;
  32. struct ogg_stream *os = ogg->streams + idx;
  33. AVStream *st = s->streams[idx];
  34. struct oggcelt_private *priv = os->private;
  35. uint8_t *p = os->buf + os->pstart;
  36. if (os->psize == 60 &&
  37. !memcmp(p, ff_celt_codec.magic, ff_celt_codec.magicsize)) {
  38. /* Main header */
  39. uint32_t version, sample_rate, nb_channels, frame_size;
  40. uint32_t overlap, extra_headers;
  41. uint8_t *extradata;
  42. extradata = av_malloc(2 * sizeof(uint32_t) +
  43. FF_INPUT_BUFFER_PADDING_SIZE);
  44. priv = av_malloc(sizeof(struct oggcelt_private));
  45. if (!extradata || !priv) {
  46. av_free(extradata);
  47. av_free(priv);
  48. return AVERROR(ENOMEM);
  49. }
  50. version = AV_RL32(p + 28);
  51. /* unused header size field skipped */
  52. sample_rate = AV_RL32(p + 36);
  53. nb_channels = AV_RL32(p + 40);
  54. frame_size = AV_RL32(p + 44);
  55. overlap = AV_RL32(p + 48);
  56. /* unused bytes per packet field skipped */
  57. extra_headers = AV_RL32(p + 56);
  58. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  59. st->codec->codec_id = AV_CODEC_ID_CELT;
  60. st->codec->sample_rate = sample_rate;
  61. st->codec->channels = nb_channels;
  62. st->codec->frame_size = frame_size;
  63. av_free(st->codec->extradata);
  64. st->codec->extradata = extradata;
  65. st->codec->extradata_size = 2 * sizeof(uint32_t);
  66. if (sample_rate)
  67. avpriv_set_pts_info(st, 64, 1, sample_rate);
  68. priv->extra_headers_left = 1 + extra_headers;
  69. av_free(os->private);
  70. os->private = priv;
  71. AV_WL32(extradata + 0, overlap);
  72. AV_WL32(extradata + 4, version);
  73. return 1;
  74. } else if (priv && priv->extra_headers_left) {
  75. /* Extra headers (vorbiscomment) */
  76. ff_vorbis_comment(s, &st->metadata, p, os->psize);
  77. priv->extra_headers_left--;
  78. return 1;
  79. } else {
  80. return 0;
  81. }
  82. }
  83. const struct ogg_codec ff_celt_codec = {
  84. .magic = "CELT ",
  85. .magicsize = 8,
  86. .header = celt_header,
  87. .nb_header = 2,
  88. };