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.

165 lines
4.9KB

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