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.

174 lines
5.3KB

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