aiffenc.c 4.5 KB

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