oggparsecelt.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Xiph CELT / Opus 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 "avformat.h"
  23. #include "oggdec.h"
  24. #include "libavutil/intreadwrite.h"
  25. struct oggcelt_private {
  26. int extra_headers_left;
  27. };
  28. static int celt_header(AVFormatContext *s, int idx)
  29. {
  30. struct ogg *ogg = s->priv_data;
  31. struct ogg_stream *os = ogg->streams + idx;
  32. AVStream *st = s->streams[idx];
  33. struct oggcelt_private *priv = os->private;
  34. uint8_t *p = os->buf + os->pstart;
  35. if (os->psize == 60 &&
  36. !memcmp(p, ff_celt_codec.magic, ff_celt_codec.magicsize)) {
  37. /* Main header */
  38. uint32_t version, header_size av_unused, sample_rate, nb_channels, frame_size;
  39. uint32_t overlap, bytes_per_packet av_unused, extra_headers;
  40. uint8_t *extradata;
  41. extradata = av_malloc(2 * sizeof(uint32_t) +
  42. FF_INPUT_BUFFER_PADDING_SIZE);
  43. priv = av_malloc(sizeof(struct oggcelt_private));
  44. if (!extradata || !priv) {
  45. av_free(extradata);
  46. av_free(priv);
  47. return AVERROR(ENOMEM);
  48. }
  49. version = AV_RL32(p + 28);
  50. header_size = AV_RL32(p + 32); /* unused */
  51. sample_rate = AV_RL32(p + 36);
  52. nb_channels = AV_RL32(p + 40);
  53. frame_size = AV_RL32(p + 44);
  54. overlap = AV_RL32(p + 48);
  55. bytes_per_packet = AV_RL32(p + 52); /* unused */
  56. extra_headers = AV_RL32(p + 56);
  57. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  58. st->codec->codec_id = CODEC_ID_CELT;
  59. st->codec->sample_rate = sample_rate;
  60. st->codec->channels = nb_channels;
  61. st->codec->frame_size = frame_size;
  62. st->codec->sample_fmt = AV_SAMPLE_FMT_S16;
  63. av_set_pts_info(st, 64, 1, sample_rate);
  64. priv->extra_headers_left = 1 + extra_headers;
  65. av_free(os->private);
  66. os->private = priv;
  67. AV_WL32(extradata + 0, overlap);
  68. AV_WL32(extradata + 4, version);
  69. av_free(st->codec->extradata);
  70. st->codec->extradata = extradata;
  71. st->codec->extradata_size = 2 * sizeof(uint32_t);
  72. return 1;
  73. } else if(priv && priv->extra_headers_left) {
  74. /* Extra headers (vorbiscomment) */
  75. ff_vorbis_comment(s, &st->metadata, p, os->psize);
  76. priv->extra_headers_left--;
  77. return 1;
  78. } else {
  79. return 0;
  80. }
  81. }
  82. const struct ogg_codec ff_celt_codec = {
  83. .magic = "CELT ",
  84. .magicsize = 8,
  85. .header = celt_header,
  86. };