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.

313 lines
9.5KB

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