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.

177 lines
5.3KB

  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 "libavutil/dict.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #include "id3v1.h"
  26. typedef struct TTAContext {
  27. int totalframes, currentframe;
  28. int frame_size;
  29. int last_frame_size;
  30. } TTAContext;
  31. static int tta_probe(AVProbeData *p)
  32. {
  33. const uint8_t *d = p->buf;
  34. if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
  35. return AVPROBE_SCORE_EXTENSION + 30;
  36. return 0;
  37. }
  38. static int tta_read_header(AVFormatContext *s)
  39. {
  40. TTAContext *c = s->priv_data;
  41. AVStream *st;
  42. int i, channels, bps, samplerate, datalen;
  43. int64_t framepos, start_offset;
  44. if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
  45. ff_id3v1_read(s);
  46. start_offset = avio_tell(s->pb);
  47. if (start_offset < 0)
  48. return start_offset;
  49. if (avio_rl32(s->pb) != AV_RL32("TTA1"))
  50. return -1; // not tta file
  51. avio_skip(s->pb, 2); // FIXME: flags
  52. channels = avio_rl16(s->pb);
  53. bps = avio_rl16(s->pb);
  54. samplerate = avio_rl32(s->pb);
  55. if(samplerate <= 0 || samplerate > 1000000){
  56. av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
  57. return -1;
  58. }
  59. datalen = avio_rl32(s->pb);
  60. if(datalen < 0){
  61. av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
  62. return -1;
  63. }
  64. avio_skip(s->pb, 4); // header crc
  65. c->frame_size = samplerate * 256 / 245;
  66. c->last_frame_size = datalen % c->frame_size;
  67. if (!c->last_frame_size)
  68. c->last_frame_size = c->frame_size;
  69. c->totalframes = datalen / c->frame_size + (c->last_frame_size < c->frame_size);
  70. c->currentframe = 0;
  71. if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
  72. av_log(s, AV_LOG_ERROR, "totalframes too large\n");
  73. return -1;
  74. }
  75. st = avformat_new_stream(s, NULL);
  76. if (!st)
  77. return AVERROR(ENOMEM);
  78. avpriv_set_pts_info(st, 64, 1, samplerate);
  79. st->start_time = 0;
  80. st->duration = datalen;
  81. framepos = avio_tell(s->pb);
  82. if (framepos < 0)
  83. return framepos;
  84. framepos += 4 * c->totalframes + 4;
  85. for (i = 0; i < c->totalframes; i++) {
  86. uint32_t size = avio_rl32(s->pb);
  87. av_add_index_entry(st, framepos, i * c->frame_size, size, 0,
  88. AVINDEX_KEYFRAME);
  89. framepos += size;
  90. }
  91. avio_skip(s->pb, 4); // seektable crc
  92. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  93. st->codecpar->codec_id = AV_CODEC_ID_TTA;
  94. st->codecpar->channels = channels;
  95. st->codecpar->sample_rate = samplerate;
  96. st->codecpar->bits_per_coded_sample = bps;
  97. st->codecpar->extradata_size = avio_tell(s->pb) - start_offset;
  98. if (st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codecpar->extradata_size) {
  99. //this check is redundant as avio_read should fail
  100. av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
  101. return -1;
  102. }
  103. st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  104. if (!st->codecpar->extradata) {
  105. st->codecpar->extradata_size = 0;
  106. return AVERROR(ENOMEM);
  107. }
  108. avio_seek(s->pb, start_offset, SEEK_SET);
  109. avio_read(s->pb, st->codecpar->extradata, st->codecpar->extradata_size);
  110. return 0;
  111. }
  112. static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
  113. {
  114. TTAContext *c = s->priv_data;
  115. AVStream *st = s->streams[0];
  116. int size, ret;
  117. // FIXME!
  118. if (c->currentframe >= c->totalframes)
  119. return AVERROR_EOF;
  120. size = st->index_entries[c->currentframe].size;
  121. ret = av_get_packet(s->pb, pkt, size);
  122. pkt->dts = st->index_entries[c->currentframe++].timestamp;
  123. pkt->duration = c->currentframe == c->totalframes ? c->last_frame_size :
  124. c->frame_size;
  125. return ret;
  126. }
  127. static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  128. {
  129. TTAContext *c = s->priv_data;
  130. AVStream *st = s->streams[stream_index];
  131. int index = av_index_search_timestamp(st, timestamp, flags);
  132. if (index < 0)
  133. return -1;
  134. if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
  135. return -1;
  136. c->currentframe = index;
  137. return 0;
  138. }
  139. AVInputFormat ff_tta_demuxer = {
  140. .name = "tta",
  141. .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
  142. .priv_data_size = sizeof(TTAContext),
  143. .read_probe = tta_probe,
  144. .read_header = tta_read_header,
  145. .read_packet = tta_read_packet,
  146. .read_seek = tta_read_seek,
  147. .extensions = "tta",
  148. };