tta.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. typedef struct {
  25. int totalframes, currentframe;
  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. uint64_t framepos, start_offset;
  40. if (!av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
  41. ff_id3v1_read(s);
  42. start_offset = avio_tell(s->pb);
  43. if (avio_rl32(s->pb) != AV_RL32("TTA1"))
  44. return -1; // not tta file
  45. avio_skip(s->pb, 2); // FIXME: flags
  46. channels = avio_rl16(s->pb);
  47. bps = avio_rl16(s->pb);
  48. samplerate = avio_rl32(s->pb);
  49. if(samplerate <= 0 || samplerate > 1000000){
  50. av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
  51. return -1;
  52. }
  53. datalen = avio_rl32(s->pb);
  54. if(datalen < 0){
  55. av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
  56. return -1;
  57. }
  58. avio_skip(s->pb, 4); // header crc
  59. framelen = samplerate*256/245;
  60. c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
  61. c->currentframe = 0;
  62. if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
  63. av_log(s, AV_LOG_ERROR, "totalframes too large\n");
  64. return -1;
  65. }
  66. st = av_new_stream(s, 0);
  67. if (!st)
  68. return AVERROR(ENOMEM);
  69. av_set_pts_info(st, 64, 1, samplerate);
  70. st->start_time = 0;
  71. st->duration = datalen;
  72. framepos = avio_tell(s->pb) + 4*c->totalframes + 4;
  73. for (i = 0; i < c->totalframes; i++) {
  74. uint32_t size = avio_rl32(s->pb);
  75. av_add_index_entry(st, framepos, i*framelen, size, 0, AVINDEX_KEYFRAME);
  76. framepos += size;
  77. }
  78. avio_skip(s->pb, 4); // seektable crc
  79. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  80. st->codec->codec_id = CODEC_ID_TTA;
  81. st->codec->channels = channels;
  82. st->codec->sample_rate = samplerate;
  83. st->codec->bits_per_coded_sample = bps;
  84. st->codec->extradata_size = avio_tell(s->pb) - start_offset;
  85. if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
  86. //this check is redundant as avio_read should fail
  87. av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
  88. return -1;
  89. }
  90. st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
  91. avio_seek(s->pb, start_offset, SEEK_SET);
  92. avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
  93. return 0;
  94. }
  95. static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
  96. {
  97. TTAContext *c = s->priv_data;
  98. AVStream *st = s->streams[0];
  99. int size, ret;
  100. // FIXME!
  101. if (c->currentframe > c->totalframes)
  102. return -1;
  103. size = st->index_entries[c->currentframe].size;
  104. ret = av_get_packet(s->pb, pkt, size);
  105. pkt->dts = st->index_entries[c->currentframe++].timestamp;
  106. return ret;
  107. }
  108. static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  109. {
  110. TTAContext *c = s->priv_data;
  111. AVStream *st = s->streams[stream_index];
  112. int index = av_index_search_timestamp(st, timestamp, flags);
  113. if (index < 0)
  114. return -1;
  115. c->currentframe = index;
  116. avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
  117. return 0;
  118. }
  119. AVInputFormat ff_tta_demuxer = {
  120. "tta",
  121. NULL_IF_CONFIG_SMALL("True Audio"),
  122. sizeof(TTAContext),
  123. tta_probe,
  124. tta_read_header,
  125. tta_read_packet,
  126. NULL,
  127. tta_read_seek,
  128. .extensions = "tta",
  129. };