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.

139 lines
3.8KB

  1. /*
  2. * TTA demuxer
  3. * Copyright (c) 2006 Alex Beregszaszi
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #define ALT_BITSREAM_READER_LE
  21. #include "bitstream.h"
  22. typedef struct {
  23. int totalframes, currentframe;
  24. uint32_t *seektable;
  25. } TTAContext;
  26. static int tta_probe(AVProbeData *p)
  27. {
  28. const uint8_t *d = p->buf;
  29. if (p->buf_size < 4)
  30. return 0;
  31. if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
  32. return 80;
  33. return 0;
  34. }
  35. static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
  36. {
  37. TTAContext *c = s->priv_data;
  38. AVStream *st;
  39. int i, channels, bps, samplerate, datalen, framelen, start;
  40. start = url_ftell(&s->pb);
  41. if (get_le32(&s->pb) != ff_get_fourcc("TTA1"))
  42. return -1; // not tta file
  43. url_fskip(&s->pb, 2); // FIXME: flags
  44. channels = get_le16(&s->pb);
  45. bps = get_le16(&s->pb);
  46. samplerate = get_le32(&s->pb);
  47. datalen = get_le32(&s->pb);
  48. url_fskip(&s->pb, 4); // header crc
  49. framelen = 1.04489795918367346939 * samplerate;
  50. c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
  51. c->currentframe = 0;
  52. c->seektable = av_mallocz(sizeof(uint32_t)*c->totalframes);
  53. if (!c->seektable)
  54. return AVERROR_NOMEM;
  55. for (i = 0; i < c->totalframes; i++)
  56. c->seektable[i] = get_le32(&s->pb);
  57. url_fskip(&s->pb, 4); // seektable crc
  58. st = av_new_stream(s, 0);
  59. // av_set_pts_info(st, 32, 1, 1000);
  60. if (!st)
  61. return AVERROR_NOMEM;
  62. st->codec->codec_type = CODEC_TYPE_AUDIO;
  63. st->codec->codec_id = CODEC_ID_TTA;
  64. st->codec->channels = channels;
  65. st->codec->sample_rate = samplerate;
  66. st->codec->bits_per_sample = bps;
  67. st->codec->extradata_size = url_ftell(&s->pb) - start;
  68. st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
  69. url_fseek(&s->pb, start, SEEK_SET); // or SEEK_CUR and -size ? :)
  70. get_buffer(&s->pb, st->codec->extradata, st->codec->extradata_size);
  71. return 0;
  72. }
  73. static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
  74. {
  75. TTAContext *c = s->priv_data;
  76. int ret, size;
  77. // FIXME!
  78. if (c->currentframe > c->totalframes)
  79. size = 0;
  80. else
  81. size = c->seektable[c->currentframe];
  82. c->currentframe++;
  83. if (av_new_packet(pkt, size) < 0)
  84. return AVERROR_IO;
  85. pkt->pos = url_ftell(&s->pb);
  86. pkt->stream_index = 0;
  87. ret = get_buffer(&s->pb, pkt->data, size);
  88. if (ret <= 0) {
  89. av_free_packet(pkt);
  90. return AVERROR_IO;
  91. }
  92. pkt->size = ret;
  93. // av_log(s, AV_LOG_INFO, "TTA packet #%d desired size: %d read size: %d at pos %d\n",
  94. // c->currentframe, size, ret, pkt->pos);
  95. return 0; //ret;
  96. }
  97. static int tta_read_close(AVFormatContext *s)
  98. {
  99. TTAContext *c = s->priv_data;
  100. if (c->seektable)
  101. av_free(c->seektable);
  102. return 0;
  103. }
  104. AVInputFormat tta_iformat = {
  105. "tta",
  106. "true-audio",
  107. sizeof(TTAContext),
  108. tta_probe,
  109. tta_read_header,
  110. tta_read_packet,
  111. tta_read_close,
  112. .extensions = "tta",
  113. };
  114. int tta_init(void)
  115. {
  116. av_register_input_format(&tta_iformat);
  117. return 0;
  118. }