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.

192 lines
5.7KB

  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. #include "rawenc.h"
  28. typedef struct {
  29. int64_t form;
  30. int64_t frames;
  31. int64_t ssnd;
  32. } AIFFOutputContext;
  33. static void put_meta(AVFormatContext *s, const char *key, uint32_t id)
  34. {
  35. AVDictionaryEntry *tag;
  36. AVIOContext *pb = s->pb;
  37. if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
  38. int size = strlen(tag->value);
  39. avio_wl32(pb, id);
  40. avio_wb32(pb, FFALIGN(size, 2));
  41. avio_write(pb, tag->value, size);
  42. if (size & 1)
  43. avio_w8(pb, 0);
  44. }
  45. }
  46. static int aiff_write_header(AVFormatContext *s)
  47. {
  48. AIFFOutputContext *aiff = s->priv_data;
  49. AVIOContext *pb = s->pb;
  50. AVCodecContext *enc = s->streams[0]->codec;
  51. uint64_t sample_rate;
  52. int aifc = 0;
  53. /* First verify if format is ok */
  54. if (!enc->codec_tag)
  55. return -1;
  56. if (enc->codec_tag != MKTAG('N','O','N','E'))
  57. aifc = 1;
  58. /* FORM AIFF header */
  59. ffio_wfourcc(pb, "FORM");
  60. aiff->form = avio_tell(pb);
  61. avio_wb32(pb, 0); /* file length */
  62. ffio_wfourcc(pb, aifc ? "AIFC" : "AIFF");
  63. if (aifc) { // compressed audio
  64. if (!enc->block_align) {
  65. av_log(s, AV_LOG_ERROR, "block align not set\n");
  66. return -1;
  67. }
  68. /* Version chunk */
  69. ffio_wfourcc(pb, "FVER");
  70. avio_wb32(pb, 4);
  71. avio_wb32(pb, 0xA2805140);
  72. }
  73. if (enc->channels > 2 && enc->channel_layout) {
  74. ffio_wfourcc(pb, "CHAN");
  75. avio_wb32(pb, 12);
  76. ff_mov_write_chan(pb, enc->channel_layout);
  77. }
  78. put_meta(s, "title", MKTAG('N', 'A', 'M', 'E'));
  79. put_meta(s, "author", MKTAG('A', 'U', 'T', 'H'));
  80. put_meta(s, "copyright", MKTAG('(', 'c', ')', ' '));
  81. put_meta(s, "comment", MKTAG('A', 'N', 'N', 'O'));
  82. /* Common chunk */
  83. ffio_wfourcc(pb, "COMM");
  84. avio_wb32(pb, aifc ? 24 : 18); /* size */
  85. avio_wb16(pb, enc->channels); /* Number of channels */
  86. aiff->frames = avio_tell(pb);
  87. avio_wb32(pb, 0); /* Number of frames */
  88. if (!enc->bits_per_coded_sample)
  89. enc->bits_per_coded_sample = av_get_bits_per_sample(enc->codec_id);
  90. if (!enc->bits_per_coded_sample) {
  91. av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
  92. return -1;
  93. }
  94. if (!enc->block_align)
  95. enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
  96. avio_wb16(pb, enc->bits_per_coded_sample); /* Sample size */
  97. sample_rate = av_double2int(enc->sample_rate);
  98. avio_wb16(pb, (sample_rate >> 52) + (16383 - 1023));
  99. avio_wb64(pb, UINT64_C(1) << 63 | sample_rate << 11);
  100. if (aifc) {
  101. avio_wl32(pb, enc->codec_tag);
  102. avio_wb16(pb, 0);
  103. }
  104. if (enc->codec_tag == MKTAG('Q','D','M','2') && enc->extradata_size) {
  105. ffio_wfourcc(pb, "wave");
  106. avio_wb32(pb, enc->extradata_size);
  107. avio_write(pb, enc->extradata, enc->extradata_size);
  108. }
  109. /* Sound data chunk */
  110. ffio_wfourcc(pb, "SSND");
  111. aiff->ssnd = avio_tell(pb); /* Sound chunk size */
  112. avio_wb32(pb, 0); /* Sound samples data size */
  113. avio_wb32(pb, 0); /* Data offset */
  114. avio_wb32(pb, 0); /* Block-size (block align) */
  115. avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  116. /* Data is starting here */
  117. avio_flush(pb);
  118. return 0;
  119. }
  120. static int aiff_write_trailer(AVFormatContext *s)
  121. {
  122. AVIOContext *pb = s->pb;
  123. AIFFOutputContext *aiff = s->priv_data;
  124. AVCodecContext *enc = s->streams[0]->codec;
  125. /* Chunks sizes must be even */
  126. int64_t file_size, end_size;
  127. end_size = file_size = avio_tell(pb);
  128. if (file_size & 1) {
  129. avio_w8(pb, 0);
  130. end_size++;
  131. }
  132. if (s->pb->seekable) {
  133. /* File length */
  134. avio_seek(pb, aiff->form, SEEK_SET);
  135. avio_wb32(pb, file_size - aiff->form - 4);
  136. /* Number of sample frames */
  137. avio_seek(pb, aiff->frames, SEEK_SET);
  138. avio_wb32(pb, (file_size-aiff->ssnd-12)/enc->block_align);
  139. /* Sound Data chunk size */
  140. avio_seek(pb, aiff->ssnd, SEEK_SET);
  141. avio_wb32(pb, file_size - aiff->ssnd - 4);
  142. /* return to the end */
  143. avio_seek(pb, end_size, SEEK_SET);
  144. avio_flush(pb);
  145. }
  146. return 0;
  147. }
  148. AVOutputFormat ff_aiff_muxer = {
  149. .name = "aiff",
  150. .long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
  151. .mime_type = "audio/aiff",
  152. .extensions = "aif,aiff,afc,aifc",
  153. .priv_data_size = sizeof(AIFFOutputContext),
  154. .audio_codec = AV_CODEC_ID_PCM_S16BE,
  155. .video_codec = AV_CODEC_ID_NONE,
  156. .write_header = aiff_write_header,
  157. .write_packet = ff_raw_write_packet,
  158. .write_trailer = aiff_write_trailer,
  159. .codec_tag = (const AVCodecTag* const []){ ff_codec_aiff_tags, 0 },
  160. };