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.

157 lines
4.6KB

  1. /*
  2. * TTA demuxer
  3. * Copyright (c) 2006 Alex Beregszaszi
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavcodec/get_bits.h"
  22. #include "avformat.h"
  23. #include "id3v1.h"
  24. #include "libavutil/dict.h"
  25. typedef struct {
  26. int totalframes, currentframe;
  27. } TTAContext;
  28. static int tta_probe(AVProbeData *p)
  29. {
  30. const uint8_t *d = p->buf;
  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;
  40. uint64_t framepos, start_offset;
  41. if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
  42. ff_id3v1_read(s);
  43. start_offset = avio_tell(s->pb);
  44. if (avio_rl32(s->pb) != AV_RL32("TTA1"))
  45. return -1; // not tta file
  46. avio_skip(s->pb, 2); // FIXME: flags
  47. channels = avio_rl16(s->pb);
  48. bps = avio_rl16(s->pb);
  49. samplerate = avio_rl32(s->pb);
  50. if(samplerate <= 0 || samplerate > 1000000){
  51. av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
  52. return -1;
  53. }
  54. datalen = avio_rl32(s->pb);
  55. if(datalen < 0){
  56. av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
  57. return -1;
  58. }
  59. avio_skip(s->pb, 4); // header crc
  60. framelen = samplerate*256/245;
  61. c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
  62. c->currentframe = 0;
  63. if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
  64. av_log(s, AV_LOG_ERROR, "totalframes too large\n");
  65. return -1;
  66. }
  67. st = av_new_stream(s, 0);
  68. if (!st)
  69. return AVERROR(ENOMEM);
  70. av_set_pts_info(st, 64, 1, samplerate);
  71. st->start_time = 0;
  72. st->duration = datalen;
  73. framepos = avio_tell(s->pb) + 4*c->totalframes + 4;
  74. for (i = 0; i < c->totalframes; i++) {
  75. uint32_t size = avio_rl32(s->pb);
  76. av_add_index_entry(st, framepos, i*framelen, size, 0, AVINDEX_KEYFRAME);
  77. framepos += size;
  78. }
  79. avio_skip(s->pb, 4); // seektable crc
  80. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  81. st->codec->codec_id = CODEC_ID_TTA;
  82. st->codec->channels = channels;
  83. st->codec->sample_rate = samplerate;
  84. st->codec->bits_per_coded_sample = bps;
  85. st->codec->extradata_size = avio_tell(s->pb) - start_offset;
  86. if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
  87. //this check is redundant as avio_read should fail
  88. av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
  89. return -1;
  90. }
  91. st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
  92. avio_seek(s->pb, start_offset, SEEK_SET);
  93. avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
  94. return 0;
  95. }
  96. static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
  97. {
  98. TTAContext *c = s->priv_data;
  99. AVStream *st = s->streams[0];
  100. int size, ret;
  101. // FIXME!
  102. if (c->currentframe > c->totalframes)
  103. return -1;
  104. size = st->index_entries[c->currentframe].size;
  105. ret = av_get_packet(s->pb, pkt, size);
  106. pkt->dts = st->index_entries[c->currentframe++].timestamp;
  107. return ret;
  108. }
  109. static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  110. {
  111. TTAContext *c = s->priv_data;
  112. AVStream *st = s->streams[stream_index];
  113. int index = av_index_search_timestamp(st, timestamp, flags);
  114. if (index < 0)
  115. return -1;
  116. c->currentframe = index;
  117. avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
  118. return 0;
  119. }
  120. AVInputFormat ff_tta_demuxer = {
  121. .name = "tta",
  122. .long_name = NULL_IF_CONFIG_SMALL("True Audio"),
  123. .priv_data_size = sizeof(TTAContext),
  124. .read_probe = tta_probe,
  125. .read_header = tta_read_header,
  126. .read_packet = tta_read_packet,
  127. .read_seek = tta_read_seek,
  128. .extensions = "tta",
  129. };