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.

177 lines
5.2KB

  1. /*
  2. * AIFF/AIFF-C muxer
  3. * Copyright (c) 2006 Patrick Guimond
  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/intfloat.h"
  22. #include "avformat.h"
  23. #include "internal.h"
  24. #include "aiff.h"
  25. #include "avio_internal.h"
  26. #include "isom.h"
  27. typedef struct {
  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. AVCodecContext *enc = s->streams[0]->codec;
  37. uint64_t sample_rate;
  38. int aifc = 0;
  39. /* First verify if format is ok */
  40. if (!enc->codec_tag)
  41. return -1;
  42. if (enc->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. 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. if (enc->channels > 2 && enc->channel_layout) {
  60. ffio_wfourcc(pb, "CHAN");
  61. avio_wb32(pb, 12);
  62. ff_mov_write_chan(pb, enc->channel_layout);
  63. }
  64. /* Common chunk */
  65. ffio_wfourcc(pb, "COMM");
  66. avio_wb32(pb, aifc ? 24 : 18); /* size */
  67. avio_wb16(pb, enc->channels); /* Number of channels */
  68. aiff->frames = avio_tell(pb);
  69. avio_wb32(pb, 0); /* Number of frames */
  70. if (!enc->bits_per_coded_sample)
  71. enc->bits_per_coded_sample = av_get_bits_per_sample(enc->codec_id);
  72. if (!enc->bits_per_coded_sample) {
  73. av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
  74. return -1;
  75. }
  76. if (!enc->block_align)
  77. enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
  78. avio_wb16(pb, enc->bits_per_coded_sample); /* Sample size */
  79. sample_rate = av_double2int(enc->sample_rate);
  80. avio_wb16(pb, (sample_rate >> 52) + (16383 - 1023));
  81. avio_wb64(pb, UINT64_C(1) << 63 | sample_rate << 11);
  82. if (aifc) {
  83. avio_wl32(pb, enc->codec_tag);
  84. avio_wb16(pb, 0);
  85. }
  86. if (enc->codec_tag == MKTAG('Q','D','M','2') && enc->extradata_size) {
  87. ffio_wfourcc(pb, "wave");
  88. avio_wb32(pb, enc->extradata_size);
  89. avio_write(pb, enc->extradata, enc->extradata_size);
  90. }
  91. /* Sound data chunk */
  92. ffio_wfourcc(pb, "SSND");
  93. aiff->ssnd = avio_tell(pb); /* Sound chunk size */
  94. avio_wb32(pb, 0); /* Sound samples data size */
  95. avio_wb32(pb, 0); /* Data offset */
  96. avio_wb32(pb, 0); /* Block-size (block align) */
  97. avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  98. /* Data is starting here */
  99. avio_flush(pb);
  100. return 0;
  101. }
  102. static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
  103. {
  104. AVIOContext *pb = s->pb;
  105. avio_write(pb, pkt->data, pkt->size);
  106. return 0;
  107. }
  108. static int aiff_write_trailer(AVFormatContext *s)
  109. {
  110. AVIOContext *pb = s->pb;
  111. AIFFOutputContext *aiff = s->priv_data;
  112. AVCodecContext *enc = s->streams[0]->codec;
  113. /* Chunks sizes must be even */
  114. int64_t file_size, end_size;
  115. end_size = file_size = avio_tell(pb);
  116. if (file_size & 1) {
  117. avio_w8(pb, 0);
  118. end_size++;
  119. }
  120. if (s->pb->seekable) {
  121. /* File length */
  122. avio_seek(pb, aiff->form, SEEK_SET);
  123. avio_wb32(pb, file_size - aiff->form - 4);
  124. /* Number of sample frames */
  125. avio_seek(pb, aiff->frames, SEEK_SET);
  126. avio_wb32(pb, (file_size-aiff->ssnd-12)/enc->block_align);
  127. /* Sound Data chunk size */
  128. avio_seek(pb, aiff->ssnd, SEEK_SET);
  129. avio_wb32(pb, file_size - aiff->ssnd - 4);
  130. /* return to the end */
  131. avio_seek(pb, end_size, SEEK_SET);
  132. avio_flush(pb);
  133. }
  134. return 0;
  135. }
  136. AVOutputFormat ff_aiff_muxer = {
  137. .name = "aiff",
  138. .long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
  139. .mime_type = "audio/aiff",
  140. .extensions = "aif,aiff,afc,aifc",
  141. .priv_data_size = sizeof(AIFFOutputContext),
  142. .audio_codec = AV_CODEC_ID_PCM_S16BE,
  143. .video_codec = AV_CODEC_ID_NONE,
  144. .write_header = aiff_write_header,
  145. .write_packet = aiff_write_packet,
  146. .write_trailer = aiff_write_trailer,
  147. .codec_tag = (const AVCodecTag* const []){ ff_codec_aiff_tags, 0 },
  148. };