aiffenc.c 4.8 KB

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