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.

303 lines
9.3KB

  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 <stdint.h>
  22. #include "libavutil/intfloat.h"
  23. #include "libavutil/opt.h"
  24. #include "libavcodec/packet_internal.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "aiff.h"
  28. #include "avio_internal.h"
  29. #include "isom.h"
  30. #include "id3v2.h"
  31. typedef struct AIFFOutputContext {
  32. const AVClass *class;
  33. int64_t form;
  34. int64_t frames;
  35. int64_t ssnd;
  36. int audio_stream_idx;
  37. PacketList *pict_list, *pict_list_end;
  38. int write_id3v2;
  39. int id3v2_version;
  40. } AIFFOutputContext;
  41. static int put_id3v2_tags(AVFormatContext *s, AIFFOutputContext *aiff)
  42. {
  43. int ret;
  44. uint64_t pos, end, size;
  45. ID3v2EncContext id3v2 = { 0 };
  46. AVIOContext *pb = s->pb;
  47. PacketList *pict_list = aiff->pict_list;
  48. if (!s->metadata && !s->nb_chapters && !aiff->pict_list)
  49. return 0;
  50. avio_wl32(pb, MKTAG('I', 'D', '3', ' '));
  51. avio_wb32(pb, 0);
  52. pos = avio_tell(pb);
  53. ff_id3v2_start(&id3v2, pb, aiff->id3v2_version, ID3v2_DEFAULT_MAGIC);
  54. ff_id3v2_write_metadata(s, &id3v2);
  55. while (pict_list) {
  56. if ((ret = ff_id3v2_write_apic(s, &id3v2, &pict_list->pkt)) < 0)
  57. return ret;
  58. pict_list = pict_list->next;
  59. }
  60. ff_id3v2_finish(&id3v2, pb, s->metadata_header_padding);
  61. end = avio_tell(pb);
  62. size = end - pos;
  63. /* Update chunk size */
  64. avio_seek(pb, pos - 4, SEEK_SET);
  65. avio_wb32(pb, size);
  66. avio_seek(pb, end, SEEK_SET);
  67. if (size & 1)
  68. avio_w8(pb, 0);
  69. return 0;
  70. }
  71. static void put_meta(AVFormatContext *s, const char *key, uint32_t id)
  72. {
  73. AVDictionaryEntry *tag;
  74. AVIOContext *pb = s->pb;
  75. if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
  76. int size = strlen(tag->value);
  77. avio_wl32(pb, id);
  78. avio_wb32(pb, FFALIGN(size, 2));
  79. avio_write(pb, tag->value, size);
  80. if (size & 1)
  81. avio_w8(pb, 0);
  82. }
  83. }
  84. static int aiff_write_header(AVFormatContext *s)
  85. {
  86. AIFFOutputContext *aiff = s->priv_data;
  87. AVIOContext *pb = s->pb;
  88. AVCodecParameters *par;
  89. uint64_t sample_rate;
  90. int i, aifc = 0;
  91. aiff->audio_stream_idx = -1;
  92. for (i = 0; i < s->nb_streams; i++) {
  93. AVStream *st = s->streams[i];
  94. if (aiff->audio_stream_idx < 0 && st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  95. aiff->audio_stream_idx = i;
  96. } else if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
  97. av_log(s, AV_LOG_ERROR, "AIFF allows only one audio stream and a picture.\n");
  98. return AVERROR(EINVAL);
  99. }
  100. }
  101. if (aiff->audio_stream_idx < 0) {
  102. av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
  103. return AVERROR(EINVAL);
  104. }
  105. par = s->streams[aiff->audio_stream_idx]->codecpar;
  106. /* First verify if format is ok */
  107. if (!par->codec_tag)
  108. return AVERROR(EINVAL);
  109. if (par->codec_tag != MKTAG('N','O','N','E'))
  110. aifc = 1;
  111. /* FORM AIFF header */
  112. ffio_wfourcc(pb, "FORM");
  113. aiff->form = avio_tell(pb);
  114. avio_wb32(pb, 0); /* file length */
  115. ffio_wfourcc(pb, aifc ? "AIFC" : "AIFF");
  116. if (aifc) { // compressed audio
  117. if (!par->block_align) {
  118. av_log(s, AV_LOG_ERROR, "block align not set\n");
  119. return AVERROR(EINVAL);
  120. }
  121. /* Version chunk */
  122. ffio_wfourcc(pb, "FVER");
  123. avio_wb32(pb, 4);
  124. avio_wb32(pb, 0xA2805140);
  125. }
  126. if (par->channels > 2 && par->channel_layout) {
  127. ffio_wfourcc(pb, "CHAN");
  128. avio_wb32(pb, 12);
  129. ff_mov_write_chan(pb, par->channel_layout);
  130. }
  131. put_meta(s, "title", MKTAG('N', 'A', 'M', 'E'));
  132. put_meta(s, "author", MKTAG('A', 'U', 'T', 'H'));
  133. put_meta(s, "copyright", MKTAG('(', 'c', ')', ' '));
  134. put_meta(s, "comment", MKTAG('A', 'N', 'N', 'O'));
  135. /* Common chunk */
  136. ffio_wfourcc(pb, "COMM");
  137. avio_wb32(pb, aifc ? 24 : 18); /* size */
  138. avio_wb16(pb, par->channels); /* Number of channels */
  139. aiff->frames = avio_tell(pb);
  140. avio_wb32(pb, 0); /* Number of frames */
  141. if (!par->bits_per_coded_sample)
  142. par->bits_per_coded_sample = av_get_bits_per_sample(par->codec_id);
  143. if (!par->bits_per_coded_sample) {
  144. av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
  145. return AVERROR(EINVAL);
  146. }
  147. if (!par->block_align)
  148. par->block_align = (par->bits_per_coded_sample * par->channels) >> 3;
  149. avio_wb16(pb, par->bits_per_coded_sample); /* Sample size */
  150. sample_rate = av_double2int(par->sample_rate);
  151. avio_wb16(pb, (sample_rate >> 52) + (16383 - 1023));
  152. avio_wb64(pb, UINT64_C(1) << 63 | sample_rate << 11);
  153. if (aifc) {
  154. avio_wl32(pb, par->codec_tag);
  155. avio_wb16(pb, 0);
  156. }
  157. if ( (par->codec_tag == MKTAG('Q','D','M','2')
  158. || par->codec_tag == MKTAG('Q','c','l','p')) && par->extradata_size) {
  159. ffio_wfourcc(pb, "wave");
  160. avio_wb32(pb, par->extradata_size);
  161. avio_write(pb, par->extradata, par->extradata_size);
  162. }
  163. /* Sound data chunk */
  164. ffio_wfourcc(pb, "SSND");
  165. aiff->ssnd = avio_tell(pb); /* Sound chunk size */
  166. avio_wb32(pb, 0); /* Sound samples data size */
  167. avio_wb32(pb, 0); /* Data offset */
  168. avio_wb32(pb, 0); /* Block-size (block align) */
  169. avpriv_set_pts_info(s->streams[aiff->audio_stream_idx], 64, 1,
  170. s->streams[aiff->audio_stream_idx]->codecpar->sample_rate);
  171. return 0;
  172. }
  173. static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
  174. {
  175. AIFFOutputContext *aiff = s->priv_data;
  176. AVIOContext *pb = s->pb;
  177. if (pkt->stream_index == aiff->audio_stream_idx)
  178. avio_write(pb, pkt->data, pkt->size);
  179. else {
  180. /* warn only once for each stream */
  181. if (s->streams[pkt->stream_index]->nb_frames == 1) {
  182. av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
  183. " ignoring.\n", pkt->stream_index);
  184. }
  185. if (s->streams[pkt->stream_index]->nb_frames >= 1)
  186. return 0;
  187. return avpriv_packet_list_put(&aiff->pict_list, &aiff->pict_list_end,
  188. pkt, av_packet_ref, 0);
  189. }
  190. return 0;
  191. }
  192. static int aiff_write_trailer(AVFormatContext *s)
  193. {
  194. int ret = 0;
  195. AVIOContext *pb = s->pb;
  196. AIFFOutputContext *aiff = s->priv_data;
  197. AVCodecParameters *par = s->streams[aiff->audio_stream_idx]->codecpar;
  198. /* Chunks sizes must be even */
  199. int64_t file_size, data_size;
  200. data_size = avio_tell(pb);
  201. if (data_size & 1)
  202. avio_w8(pb, 0);
  203. if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
  204. /* Write ID3 tags */
  205. if (aiff->write_id3v2)
  206. if ((ret = put_id3v2_tags(s, aiff)) < 0)
  207. return ret;
  208. /* File length */
  209. file_size = avio_tell(pb);
  210. avio_seek(pb, aiff->form, SEEK_SET);
  211. avio_wb32(pb, file_size - aiff->form - 4);
  212. /* Number of sample frames */
  213. avio_seek(pb, aiff->frames, SEEK_SET);
  214. avio_wb32(pb, (data_size - aiff->ssnd - 12) / par->block_align);
  215. /* Sound Data chunk size */
  216. avio_seek(pb, aiff->ssnd, SEEK_SET);
  217. avio_wb32(pb, data_size - aiff->ssnd - 4);
  218. }
  219. return ret;
  220. }
  221. static void aiff_deinit(AVFormatContext *s)
  222. {
  223. AIFFOutputContext *aiff = s->priv_data;
  224. avpriv_packet_list_free(&aiff->pict_list, &aiff->pict_list_end);
  225. }
  226. #define OFFSET(x) offsetof(AIFFOutputContext, x)
  227. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  228. static const AVOption options[] = {
  229. { "write_id3v2", "Enable ID3 tags writing.",
  230. OFFSET(write_id3v2), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC },
  231. { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
  232. OFFSET(id3v2_version), AV_OPT_TYPE_INT, {.i64 = 4}, 3, 4, ENC },
  233. { NULL },
  234. };
  235. static const AVClass aiff_muxer_class = {
  236. .class_name = "AIFF muxer",
  237. .item_name = av_default_item_name,
  238. .option = options,
  239. .version = LIBAVUTIL_VERSION_INT,
  240. };
  241. AVOutputFormat ff_aiff_muxer = {
  242. .name = "aiff",
  243. .long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
  244. .mime_type = "audio/aiff",
  245. .extensions = "aif,aiff,afc,aifc",
  246. .priv_data_size = sizeof(AIFFOutputContext),
  247. .audio_codec = AV_CODEC_ID_PCM_S16BE,
  248. .video_codec = AV_CODEC_ID_PNG,
  249. .write_header = aiff_write_header,
  250. .write_packet = aiff_write_packet,
  251. .write_trailer = aiff_write_trailer,
  252. .deinit = aiff_deinit,
  253. .codec_tag = ff_aiff_codec_tags_list,
  254. .priv_class = &aiff_muxer_class,
  255. };