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.

152 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., 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. if(samplerate <= 0 || samplerate > 1000000){
  48. av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
  49. return -1;
  50. }
  51. datalen = get_le32(&s->pb);
  52. if(datalen < 0){
  53. av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
  54. return -1;
  55. }
  56. url_fskip(&s->pb, 4); // header crc
  57. framelen = 1.04489795918367346939 * samplerate;
  58. c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
  59. c->currentframe = 0;
  60. if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
  61. av_log(s, AV_LOG_ERROR, "totalframes too large\n");
  62. return -1;
  63. }
  64. c->seektable = av_mallocz(sizeof(uint32_t)*c->totalframes);
  65. if (!c->seektable)
  66. return AVERROR_NOMEM;
  67. for (i = 0; i < c->totalframes; i++)
  68. c->seektable[i] = get_le32(&s->pb);
  69. url_fskip(&s->pb, 4); // seektable crc
  70. st = av_new_stream(s, 0);
  71. // av_set_pts_info(st, 32, 1, 1000);
  72. if (!st)
  73. return AVERROR_NOMEM;
  74. st->codec->codec_type = CODEC_TYPE_AUDIO;
  75. st->codec->codec_id = CODEC_ID_TTA;
  76. st->codec->channels = channels;
  77. st->codec->sample_rate = samplerate;
  78. st->codec->bits_per_sample = bps;
  79. st->codec->extradata_size = url_ftell(&s->pb) - start;
  80. if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
  81. //this check is redundant as get_buffer should fail
  82. av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
  83. return -1;
  84. }
  85. st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
  86. url_fseek(&s->pb, start, SEEK_SET); // or SEEK_CUR and -size ? :)
  87. get_buffer(&s->pb, st->codec->extradata, st->codec->extradata_size);
  88. return 0;
  89. }
  90. static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
  91. {
  92. TTAContext *c = s->priv_data;
  93. int ret, size;
  94. // FIXME!
  95. if (c->currentframe > c->totalframes)
  96. size = 0;
  97. else
  98. size = c->seektable[c->currentframe];
  99. c->currentframe++;
  100. if (av_new_packet(pkt, size) < 0)
  101. return AVERROR_IO;
  102. pkt->pos = url_ftell(&s->pb);
  103. pkt->stream_index = 0;
  104. ret = get_buffer(&s->pb, pkt->data, size);
  105. if (ret <= 0) {
  106. av_free_packet(pkt);
  107. return AVERROR_IO;
  108. }
  109. pkt->size = ret;
  110. // av_log(s, AV_LOG_INFO, "TTA packet #%d desired size: %d read size: %d at pos %d\n",
  111. // c->currentframe, size, ret, pkt->pos);
  112. return 0; //ret;
  113. }
  114. static int tta_read_close(AVFormatContext *s)
  115. {
  116. TTAContext *c = s->priv_data;
  117. if (c->seektable)
  118. av_free(c->seektable);
  119. return 0;
  120. }
  121. AVInputFormat tta_demuxer = {
  122. "tta",
  123. "true-audio",
  124. sizeof(TTAContext),
  125. tta_probe,
  126. tta_read_header,
  127. tta_read_packet,
  128. tta_read_close,
  129. .extensions = "tta",
  130. };