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.

167 lines
5.0KB

  1. /*
  2. * AIFF/AIFF-C muxer
  3. * Copyright (c) 2006 Patrick Guimond
  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 <stdint.h>
  22. #include "libavutil/intfloat.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #include "aiff.h"
  26. #include "avio_internal.h"
  27. typedef struct AIFFOutputContext {
  28. int64_t form;
  29. int64_t frames;
  30. int64_t ssnd;
  31. } AIFFOutputContext;
  32. static int aiff_write_header(AVFormatContext *s)
  33. {
  34. AIFFOutputContext *aiff = s->priv_data;
  35. AVIOContext *pb = s->pb;
  36. AVCodecParameters *par = s->streams[0]->codecpar;
  37. uint64_t sample_rate;
  38. int aifc = 0;
  39. /* First verify if format is ok */
  40. if (!par->codec_tag)
  41. return -1;
  42. if (par->codec_tag != MKTAG('N','O','N','E'))
  43. aifc = 1;
  44. /* FORM AIFF header */
  45. ffio_wfourcc(pb, "FORM");
  46. aiff->form = avio_tell(pb);
  47. avio_wb32(pb, 0); /* file length */
  48. ffio_wfourcc(pb, aifc ? "AIFC" : "AIFF");
  49. if (aifc) { // compressed audio
  50. par->bits_per_coded_sample = 16;
  51. if (!par->block_align) {
  52. av_log(s, AV_LOG_ERROR, "block align not set\n");
  53. return -1;
  54. }
  55. /* Version chunk */
  56. ffio_wfourcc(pb, "FVER");
  57. avio_wb32(pb, 4);
  58. avio_wb32(pb, 0xA2805140);
  59. }
  60. /* Common chunk */
  61. ffio_wfourcc(pb, "COMM");
  62. avio_wb32(pb, aifc ? 24 : 18); /* size */
  63. avio_wb16(pb, par->channels); /* Number of channels */
  64. aiff->frames = avio_tell(pb);
  65. avio_wb32(pb, 0); /* Number of frames */
  66. if (!par->bits_per_coded_sample)
  67. par->bits_per_coded_sample = av_get_bits_per_sample(par->codec_id);
  68. if (!par->bits_per_coded_sample) {
  69. av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
  70. return -1;
  71. }
  72. if (!par->block_align)
  73. par->block_align = (par->bits_per_coded_sample * par->channels) >> 3;
  74. avio_wb16(pb, par->bits_per_coded_sample); /* Sample size */
  75. sample_rate = av_double2int(par->sample_rate);
  76. avio_wb16(pb, (sample_rate >> 52) + (16383 - 1023));
  77. avio_wb64(pb, UINT64_C(1) << 63 | sample_rate << 11);
  78. if (aifc) {
  79. avio_wl32(pb, par->codec_tag);
  80. avio_wb16(pb, 0);
  81. }
  82. /* Sound data chunk */
  83. ffio_wfourcc(pb, "SSND");
  84. aiff->ssnd = avio_tell(pb); /* Sound chunk size */
  85. avio_wb32(pb, 0); /* Sound samples data size */
  86. avio_wb32(pb, 0); /* Data offset */
  87. avio_wb32(pb, 0); /* Block-size (block align) */
  88. avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codecpar->sample_rate);
  89. /* Data is starting here */
  90. avio_flush(pb);
  91. return 0;
  92. }
  93. static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
  94. {
  95. AVIOContext *pb = s->pb;
  96. avio_write(pb, pkt->data, pkt->size);
  97. return 0;
  98. }
  99. static int aiff_write_trailer(AVFormatContext *s)
  100. {
  101. AVIOContext *pb = s->pb;
  102. AIFFOutputContext *aiff = s->priv_data;
  103. AVCodecParameters *par = s->streams[0]->codecpar;
  104. /* Chunks sizes must be even */
  105. int64_t file_size, end_size;
  106. end_size = file_size = avio_tell(pb);
  107. if (file_size & 1) {
  108. avio_w8(pb, 0);
  109. end_size++;
  110. }
  111. if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
  112. /* File length */
  113. avio_seek(pb, aiff->form, SEEK_SET);
  114. avio_wb32(pb, file_size - aiff->form - 4);
  115. /* Number of sample frames */
  116. avio_seek(pb, aiff->frames, SEEK_SET);
  117. avio_wb32(pb, (file_size - aiff->ssnd - 12) / par->block_align);
  118. /* Sound Data chunk size */
  119. avio_seek(pb, aiff->ssnd, SEEK_SET);
  120. avio_wb32(pb, file_size - aiff->ssnd - 4);
  121. /* return to the end */
  122. avio_seek(pb, end_size, SEEK_SET);
  123. avio_flush(pb);
  124. }
  125. return 0;
  126. }
  127. AVOutputFormat ff_aiff_muxer = {
  128. .name = "aiff",
  129. .long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
  130. .mime_type = "audio/aiff",
  131. .extensions = "aif,aiff,afc,aifc",
  132. .priv_data_size = sizeof(AIFFOutputContext),
  133. .audio_codec = AV_CODEC_ID_PCM_S16BE,
  134. .video_codec = AV_CODEC_ID_NONE,
  135. .write_header = aiff_write_header,
  136. .write_packet = aiff_write_packet,
  137. .write_trailer = aiff_write_trailer,
  138. .codec_tag = (const AVCodecTag* const []){ ff_codec_aiff_tags, 0 },
  139. };