tta.c 4.8 KB

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