aiffenc.c 4.6 KB

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