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.

149 lines
4.2KB

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