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.1KB

  1. /*
  2. * True Audio (TTA) muxer
  3. * Copyright (c) 2016 James Almer
  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 "libavutil/crc.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "apetag.h"
  24. #include "avformat.h"
  25. #include "avio_internal.h"
  26. #include "internal.h"
  27. typedef struct TTAMuxContext {
  28. AVIOContext *seek_table;
  29. AVPacketList *queue, *queue_end;
  30. uint32_t nb_samples;
  31. int frame_size;
  32. int last_frame;
  33. } TTAMuxContext;
  34. static int tta_write_header(AVFormatContext *s)
  35. {
  36. TTAMuxContext *tta = s->priv_data;
  37. AVCodecParameters *par = s->streams[0]->codecpar;
  38. int ret;
  39. if (s->nb_streams != 1) {
  40. av_log(s, AV_LOG_ERROR, "Only one stream is supported\n");
  41. return AVERROR(EINVAL);
  42. }
  43. if (par->codec_id != AV_CODEC_ID_TTA) {
  44. av_log(s, AV_LOG_ERROR, "Unsupported codec\n");
  45. return AVERROR(EINVAL);
  46. }
  47. if (par->extradata && par->extradata_size < 22) {
  48. av_log(s, AV_LOG_ERROR, "Invalid TTA extradata\n");
  49. return AVERROR_INVALIDDATA;
  50. }
  51. if ((ret = avio_open_dyn_buf(&tta->seek_table)) < 0)
  52. return ret;
  53. /* Ignore most extradata information if present. It can be innacurate
  54. if for example remuxing from Matroska */
  55. ffio_init_checksum(s->pb, ff_crcEDB88320_update, UINT32_MAX);
  56. ffio_init_checksum(tta->seek_table, ff_crcEDB88320_update, UINT32_MAX);
  57. avio_write(s->pb, "TTA1", 4);
  58. avio_wl16(s->pb, par->extradata ? AV_RL16(par->extradata + 4) : 1);
  59. avio_wl16(s->pb, par->channels);
  60. avio_wl16(s->pb, par->bits_per_raw_sample);
  61. avio_wl32(s->pb, par->sample_rate);
  62. /* Prevent overflow */
  63. if (par->sample_rate > 0x7FFFFFu) {
  64. av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
  65. return AVERROR(EINVAL);
  66. }
  67. tta->frame_size = par->sample_rate * 256 / 245;
  68. avpriv_set_pts_info(s->streams[0], 64, 1, par->sample_rate);
  69. return 0;
  70. }
  71. static int tta_write_packet(AVFormatContext *s, AVPacket *pkt)
  72. {
  73. TTAMuxContext *tta = s->priv_data;
  74. AVPacketList *pktl = av_mallocz(sizeof(*pktl));
  75. int ret;
  76. if (!pktl)
  77. return AVERROR(ENOMEM);
  78. ret = av_packet_ref(&pktl->pkt, pkt);
  79. if (ret < 0) {
  80. av_free(pktl);
  81. return ret;
  82. }
  83. if (tta->queue_end)
  84. tta->queue_end->next = pktl;
  85. else
  86. tta->queue = pktl;
  87. tta->queue_end = pktl;
  88. avio_wl32(tta->seek_table, pkt->size);
  89. tta->nb_samples += pkt->duration;
  90. if (tta->frame_size != pkt->duration) {
  91. if (tta->last_frame) {
  92. /* Two frames with a different duration than the default frame
  93. size means the TTA stream comes from a faulty container, and
  94. there's no way the last frame duration will be correct. */
  95. av_log(s, AV_LOG_ERROR, "Invalid frame durations\n");
  96. return AVERROR_INVALIDDATA;
  97. }
  98. /* First frame with a different duration than the default frame size.
  99. Assume it's the last frame in the stream and continue. */
  100. tta->last_frame++;
  101. }
  102. return 0;
  103. }
  104. static void tta_queue_flush(AVFormatContext *s)
  105. {
  106. TTAMuxContext *tta = s->priv_data;
  107. AVPacketList *pktl;
  108. while (pktl = tta->queue) {
  109. AVPacket *pkt = &pktl->pkt;
  110. avio_write(s->pb, pkt->data, pkt->size);
  111. av_packet_unref(pkt);
  112. tta->queue = pktl->next;
  113. av_free(pktl);
  114. }
  115. tta->queue_end = NULL;
  116. }
  117. static int tta_write_trailer(AVFormatContext *s)
  118. {
  119. TTAMuxContext *tta = s->priv_data;
  120. uint8_t *ptr;
  121. unsigned int crc;
  122. int size;
  123. avio_wl32(s->pb, tta->nb_samples);
  124. crc = ffio_get_checksum(s->pb) ^ UINT32_MAX;
  125. avio_wl32(s->pb, crc);
  126. /* Write Seek table */
  127. crc = ffio_get_checksum(tta->seek_table) ^ UINT32_MAX;
  128. avio_wl32(tta->seek_table, crc);
  129. size = avio_close_dyn_buf(tta->seek_table, &ptr);
  130. avio_write(s->pb, ptr, size);
  131. av_free(ptr);
  132. /* Write audio data */
  133. tta_queue_flush(s);
  134. ff_ape_write_tag(s);
  135. avio_flush(s->pb);
  136. return 0;
  137. }
  138. AVOutputFormat ff_tta_muxer = {
  139. .name = "tta",
  140. .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
  141. .mime_type = "audio/x-tta",
  142. .extensions = "tta",
  143. .priv_data_size = sizeof(TTAMuxContext),
  144. .audio_codec = AV_CODEC_ID_TTA,
  145. .video_codec = AV_CODEC_ID_NONE,
  146. .write_header = tta_write_header,
  147. .write_packet = tta_write_packet,
  148. .write_trailer = tta_write_trailer,
  149. };