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.

315 lines
9.6KB

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