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.

151 lines
4.3KB

  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avformat.h"
  20. #include "bitstream.h"
  21. typedef struct {
  22. int totalframes, currentframe;
  23. uint32_t *seektable;
  24. } TTAContext;
  25. static int tta_probe(AVProbeData *p)
  26. {
  27. const uint8_t *d = p->buf;
  28. if (p->buf_size < 4)
  29. return 0;
  30. if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
  31. return 80;
  32. return 0;
  33. }
  34. static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
  35. {
  36. TTAContext *c = s->priv_data;
  37. AVStream *st;
  38. int i, channels, bps, samplerate, datalen, framelen, start;
  39. start = url_ftell(&s->pb);
  40. if (get_le32(&s->pb) != ff_get_fourcc("TTA1"))
  41. return -1; // not tta file
  42. url_fskip(&s->pb, 2); // FIXME: flags
  43. channels = get_le16(&s->pb);
  44. bps = get_le16(&s->pb);
  45. samplerate = get_le32(&s->pb);
  46. if(samplerate <= 0 || samplerate > 1000000){
  47. av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
  48. return -1;
  49. }
  50. datalen = get_le32(&s->pb);
  51. if(datalen < 0){
  52. av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
  53. return -1;
  54. }
  55. url_fskip(&s->pb, 4); // header crc
  56. framelen = 1.04489795918367346939 * samplerate;
  57. c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
  58. c->currentframe = 0;
  59. if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
  60. av_log(s, AV_LOG_ERROR, "totalframes too large\n");
  61. return -1;
  62. }
  63. c->seektable = av_mallocz(sizeof(uint32_t)*c->totalframes);
  64. if (!c->seektable)
  65. return AVERROR_NOMEM;
  66. for (i = 0; i < c->totalframes; i++)
  67. c->seektable[i] = get_le32(&s->pb);
  68. url_fskip(&s->pb, 4); // seektable crc
  69. st = av_new_stream(s, 0);
  70. // av_set_pts_info(st, 32, 1, 1000);
  71. if (!st)
  72. return AVERROR_NOMEM;
  73. st->codec->codec_type = CODEC_TYPE_AUDIO;
  74. st->codec->codec_id = CODEC_ID_TTA;
  75. st->codec->channels = channels;
  76. st->codec->sample_rate = samplerate;
  77. st->codec->bits_per_sample = bps;
  78. st->codec->extradata_size = url_ftell(&s->pb) - start;
  79. if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
  80. //this check is redundant as get_buffer should fail
  81. av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
  82. return -1;
  83. }
  84. st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
  85. url_fseek(&s->pb, start, SEEK_SET); // or SEEK_CUR and -size ? :)
  86. get_buffer(&s->pb, st->codec->extradata, st->codec->extradata_size);
  87. return 0;
  88. }
  89. static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
  90. {
  91. TTAContext *c = s->priv_data;
  92. int ret, size;
  93. // FIXME!
  94. if (c->currentframe > c->totalframes)
  95. size = 0;
  96. else
  97. size = c->seektable[c->currentframe];
  98. c->currentframe++;
  99. if (av_new_packet(pkt, size) < 0)
  100. return AVERROR_IO;
  101. pkt->pos = url_ftell(&s->pb);
  102. pkt->stream_index = 0;
  103. ret = get_buffer(&s->pb, pkt->data, size);
  104. if (ret <= 0) {
  105. av_free_packet(pkt);
  106. return AVERROR_IO;
  107. }
  108. pkt->size = ret;
  109. // av_log(s, AV_LOG_INFO, "TTA packet #%d desired size: %d read size: %d at pos %d\n",
  110. // c->currentframe, size, ret, pkt->pos);
  111. return 0; //ret;
  112. }
  113. static int tta_read_close(AVFormatContext *s)
  114. {
  115. TTAContext *c = s->priv_data;
  116. if (c->seektable)
  117. av_free(c->seektable);
  118. return 0;
  119. }
  120. AVInputFormat tta_demuxer = {
  121. "tta",
  122. "true-audio",
  123. sizeof(TTAContext),
  124. tta_probe,
  125. tta_read_header,
  126. tta_read_packet,
  127. tta_read_close,
  128. .extensions = "tta",
  129. };