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.

335 lines
9.8KB

  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 {
  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;
  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)
  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. AVCodecContext *enc;
  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->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  96. aiff->audio_stream_idx = i;
  97. } else if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO) {
  98. av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in AIFF.\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. enc = s->streams[aiff->audio_stream_idx]->codec;
  107. /* First verify if format is ok */
  108. if (!enc->codec_tag)
  109. return -1;
  110. if (enc->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 (!enc->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 (enc->channels > 2 && enc->channel_layout) {
  128. ffio_wfourcc(pb, "CHAN");
  129. avio_wb32(pb, 12);
  130. ff_mov_write_chan(pb, enc->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, enc->channels); /* Number of channels */
  140. aiff->frames = avio_tell(pb);
  141. avio_wb32(pb, 0); /* Number of frames */
  142. if (!enc->bits_per_coded_sample)
  143. enc->bits_per_coded_sample = av_get_bits_per_sample(enc->codec_id);
  144. if (!enc->bits_per_coded_sample) {
  145. av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
  146. return -1;
  147. }
  148. if (!enc->block_align)
  149. enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
  150. avio_wb16(pb, enc->bits_per_coded_sample); /* Sample size */
  151. sample_rate = av_double2int(enc->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, enc->codec_tag);
  156. avio_wb16(pb, 0);
  157. }
  158. if (enc->codec_tag == MKTAG('Q','D','M','2') && enc->extradata_size) {
  159. ffio_wfourcc(pb, "wave");
  160. avio_wb32(pb, enc->extradata_size);
  161. avio_write(pb, enc->extradata, enc->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]->codec->sample_rate);
  171. /* Data is starting here */
  172. avio_flush(pb);
  173. return 0;
  174. }
  175. static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
  176. {
  177. AIFFOutputContext *aiff = s->priv_data;
  178. AVIOContext *pb = s->pb;
  179. if (pkt->stream_index == aiff->audio_stream_idx)
  180. avio_write(pb, pkt->data, pkt->size);
  181. else {
  182. int ret;
  183. AVPacketList *pict_list, *last;
  184. if (s->streams[pkt->stream_index]->codec->codec_type != AVMEDIA_TYPE_VIDEO)
  185. return 0;
  186. /* warn only once for each stream */
  187. if (s->streams[pkt->stream_index]->nb_frames == 1) {
  188. av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
  189. " ignoring.\n", pkt->stream_index);
  190. }
  191. if (s->streams[pkt->stream_index]->nb_frames >= 1)
  192. return 0;
  193. pict_list = av_mallocz(sizeof(AVPacketList));
  194. if (!pict_list)
  195. return AVERROR(ENOMEM);
  196. if ((ret = av_copy_packet(&pict_list->pkt, pkt)) < 0) {
  197. av_freep(&pict_list);
  198. return ret;
  199. }
  200. if (!aiff->pict_list)
  201. aiff->pict_list = pict_list;
  202. else {
  203. last = aiff->pict_list;
  204. while (last->next)
  205. last = last->next;
  206. last->next = pict_list;
  207. }
  208. }
  209. return 0;
  210. }
  211. static int aiff_write_trailer(AVFormatContext *s)
  212. {
  213. int ret;
  214. AVIOContext *pb = s->pb;
  215. AIFFOutputContext *aiff = s->priv_data;
  216. AVPacketList *pict_list = aiff->pict_list;
  217. AVCodecContext *enc = s->streams[aiff->audio_stream_idx]->codec;
  218. /* Chunks sizes must be even */
  219. int64_t file_size, end_size;
  220. end_size = file_size = avio_tell(pb);
  221. if (file_size & 1) {
  222. avio_w8(pb, 0);
  223. end_size++;
  224. }
  225. if (s->pb->seekable) {
  226. /* Number of sample frames */
  227. avio_seek(pb, aiff->frames, SEEK_SET);
  228. avio_wb32(pb, (file_size-aiff->ssnd-12)/enc->block_align);
  229. /* Sound Data chunk size */
  230. avio_seek(pb, aiff->ssnd, SEEK_SET);
  231. avio_wb32(pb, file_size - aiff->ssnd - 4);
  232. /* return to the end */
  233. avio_seek(pb, end_size, SEEK_SET);
  234. /* Write ID3 tags */
  235. if (aiff->write_id3v2)
  236. if ((ret = put_id3v2_tags(s, aiff)) < 0)
  237. return ret;
  238. /* File length */
  239. file_size = avio_tell(pb);
  240. avio_seek(pb, aiff->form, SEEK_SET);
  241. avio_wb32(pb, file_size - aiff->form - 4);
  242. avio_flush(pb);
  243. }
  244. while (pict_list) {
  245. AVPacketList *next = pict_list->next;
  246. av_free_packet(&pict_list->pkt);
  247. av_freep(&pict_list);
  248. pict_list = next;
  249. }
  250. return 0;
  251. }
  252. #define OFFSET(x) offsetof(AIFFOutputContext, x)
  253. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  254. static const AVOption options[] = {
  255. { "write_id3v2", "Enable ID3 tags writing.",
  256. OFFSET(write_id3v2), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, ENC },
  257. { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
  258. OFFSET(id3v2_version), AV_OPT_TYPE_INT, {.i64 = 4}, 3, 4, ENC },
  259. { NULL },
  260. };
  261. static const AVClass aiff_muxer_class = {
  262. .class_name = "AIFF muxer",
  263. .item_name = av_default_item_name,
  264. .option = options,
  265. .version = LIBAVUTIL_VERSION_INT,
  266. };
  267. AVOutputFormat ff_aiff_muxer = {
  268. .name = "aiff",
  269. .long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
  270. .mime_type = "audio/aiff",
  271. .extensions = "aif,aiff,afc,aifc",
  272. .priv_data_size = sizeof(AIFFOutputContext),
  273. .audio_codec = AV_CODEC_ID_PCM_S16BE,
  274. .video_codec = AV_CODEC_ID_PNG,
  275. .write_header = aiff_write_header,
  276. .write_packet = aiff_write_packet,
  277. .write_trailer = aiff_write_trailer,
  278. .codec_tag = (const AVCodecTag* const []){ ff_codec_aiff_tags, 0 },
  279. .priv_class = &aiff_muxer_class,
  280. };