tta.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/bitstream.h"
  22. #include "avformat.h"
  23. typedef struct {
  24. int totalframes, currentframe;
  25. } TTAContext;
  26. static int tta_probe(AVProbeData *p)
  27. {
  28. const uint8_t *d = p->buf;
  29. if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
  30. return 80;
  31. return 0;
  32. }
  33. static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
  34. {
  35. TTAContext *c = s->priv_data;
  36. AVStream *st;
  37. int i, channels, bps, samplerate, datalen, framelen;
  38. uint64_t framepos;
  39. if (get_le32(s->pb) != AV_RL32("TTA1"))
  40. return -1; // not tta file
  41. url_fskip(s->pb, 2); // FIXME: flags
  42. channels = get_le16(s->pb);
  43. bps = get_le16(s->pb);
  44. samplerate = get_le32(s->pb);
  45. if(samplerate <= 0 || samplerate > 1000000){
  46. av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
  47. return -1;
  48. }
  49. datalen = get_le32(s->pb);
  50. if(datalen < 0){
  51. av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
  52. return -1;
  53. }
  54. url_fskip(s->pb, 4); // header crc
  55. framelen = samplerate*256/245;
  56. c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
  57. c->currentframe = 0;
  58. if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
  59. av_log(s, AV_LOG_ERROR, "totalframes too large\n");
  60. return -1;
  61. }
  62. st = av_new_stream(s, 0);
  63. if (!st)
  64. return AVERROR(ENOMEM);
  65. av_set_pts_info(st, 64, 1, samplerate);
  66. st->start_time = 0;
  67. st->duration = datalen;
  68. framepos = url_ftell(s->pb) + 4*c->totalframes + 4;
  69. for (i = 0; i < c->totalframes; i++) {
  70. uint32_t size = get_le32(s->pb);
  71. av_add_index_entry(st, framepos, i*framelen, size, 0, AVINDEX_KEYFRAME);
  72. framepos += size;
  73. }
  74. url_fskip(s->pb, 4); // seektable crc
  75. st->codec->codec_type = CODEC_TYPE_AUDIO;
  76. st->codec->codec_id = CODEC_ID_TTA;
  77. st->codec->channels = channels;
  78. st->codec->sample_rate = samplerate;
  79. st->codec->bits_per_coded_sample = bps;
  80. st->codec->extradata_size = url_ftell(s->pb);
  81. if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
  82. //this check is redundant as get_buffer should fail
  83. av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
  84. return -1;
  85. }
  86. st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
  87. url_fseek(s->pb, 0, SEEK_SET);
  88. get_buffer(s->pb, st->codec->extradata, st->codec->extradata_size);
  89. return 0;
  90. }
  91. static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
  92. {
  93. TTAContext *c = s->priv_data;
  94. AVStream *st = s->streams[0];
  95. int size, ret;
  96. // FIXME!
  97. if (c->currentframe > c->totalframes)
  98. return -1;
  99. size = st->index_entries[c->currentframe].size;
  100. ret = av_get_packet(s->pb, pkt, size);
  101. pkt->dts = st->index_entries[c->currentframe++].timestamp;
  102. return ret;
  103. }
  104. static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  105. {
  106. TTAContext *c = s->priv_data;
  107. AVStream *st = s->streams[stream_index];
  108. int index = av_index_search_timestamp(st, timestamp, flags);
  109. if (index < 0)
  110. return -1;
  111. c->currentframe = index;
  112. url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
  113. return 0;
  114. }
  115. AVInputFormat tta_demuxer = {
  116. "tta",
  117. NULL_IF_CONFIG_SMALL("True Audio"),
  118. sizeof(TTAContext),
  119. tta_probe,
  120. tta_read_header,
  121. tta_read_packet,
  122. NULL,
  123. tta_read_seek,
  124. .extensions = "tta",
  125. };