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.

333 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 "libavutil/intfloat.h"
  22. #include "libavutil/opt.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #include "aiff.h"
  26. #include "avio_internal.h"
  27. #include "isom.h"
  28. #include "id3v2.h"
  29. typedef struct {
  30. const AVClass *class;
  31. int64_t form;
  32. int64_t frames;
  33. int64_t ssnd;
  34. int audio_stream_idx;
  35. AVPacketList *pict_list;
  36. int write_id3v2;
  37. int id3v2_version;
  38. } AIFFOutputContext;
  39. static int put_id3v2_tags(AVFormatContext *s, AIFFOutputContext *aiff)
  40. {
  41. int ret;
  42. uint64_t pos, end, size;
  43. ID3v2EncContext id3v2 = { 0 };
  44. AVIOContext *pb = s->pb;
  45. AVPacketList *pict_list = aiff->pict_list;
  46. if (!pb->seekable)
  47. return 0;
  48. if (!s->metadata && !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);
  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. AVCodecContext *enc;
  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->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  95. aiff->audio_stream_idx = i;
  96. } else if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO) {
  97. av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in AIFF.\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. enc = s->streams[aiff->audio_stream_idx]->codec;
  106. /* First verify if format is ok */
  107. if (!enc->codec_tag)
  108. return -1;
  109. if (enc->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 (!enc->block_align) {
  118. av_log(s, AV_LOG_ERROR, "block align not set\n");
  119. return -1;
  120. }
  121. /* Version chunk */
  122. ffio_wfourcc(pb, "FVER");
  123. avio_wb32(pb, 4);
  124. avio_wb32(pb, 0xA2805140);
  125. }
  126. if (enc->channels > 2 && enc->channel_layout) {
  127. ffio_wfourcc(pb, "CHAN");
  128. avio_wb32(pb, 12);
  129. ff_mov_write_chan(pb, enc->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, enc->channels); /* Number of channels */
  139. aiff->frames = avio_tell(pb);
  140. avio_wb32(pb, 0); /* Number of frames */
  141. if (!enc->bits_per_coded_sample)
  142. enc->bits_per_coded_sample = av_get_bits_per_sample(enc->codec_id);
  143. if (!enc->bits_per_coded_sample) {
  144. av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
  145. return -1;
  146. }
  147. if (!enc->block_align)
  148. enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
  149. avio_wb16(pb, enc->bits_per_coded_sample); /* Sample size */
  150. sample_rate = av_double2int(enc->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, enc->codec_tag);
  155. avio_wb16(pb, 0);
  156. }
  157. if (enc->codec_tag == MKTAG('Q','D','M','2') && enc->extradata_size) {
  158. ffio_wfourcc(pb, "wave");
  159. avio_wb32(pb, enc->extradata_size);
  160. avio_write(pb, enc->extradata, enc->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]->codec->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. int ret;
  182. AVPacketList *pict_list, *last;
  183. if (s->streams[pkt->stream_index]->codec->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. pict_list = av_mallocz(sizeof(AVPacketList));
  193. if (!pict_list)
  194. return AVERROR(ENOMEM);
  195. if ((ret = av_copy_packet(&pict_list->pkt, pkt)) < 0) {
  196. av_freep(&pict_list);
  197. return ret;
  198. }
  199. if (!aiff->pict_list)
  200. aiff->pict_list = pict_list;
  201. else {
  202. last = aiff->pict_list;
  203. while (last->next)
  204. last = last->next;
  205. last->next = pict_list;
  206. }
  207. }
  208. return 0;
  209. }
  210. static int aiff_write_trailer(AVFormatContext *s)
  211. {
  212. int ret;
  213. AVIOContext *pb = s->pb;
  214. AIFFOutputContext *aiff = s->priv_data;
  215. AVPacketList *pict_list = aiff->pict_list;
  216. AVCodecContext *enc = s->streams[aiff->audio_stream_idx]->codec;
  217. /* Chunks sizes must be even */
  218. int64_t file_size, end_size;
  219. end_size = file_size = avio_tell(pb);
  220. if (file_size & 1) {
  221. avio_w8(pb, 0);
  222. end_size++;
  223. }
  224. if (s->pb->seekable) {
  225. /* Number of sample frames */
  226. avio_seek(pb, aiff->frames, SEEK_SET);
  227. avio_wb32(pb, (file_size-aiff->ssnd-12)/enc->block_align);
  228. /* Sound Data chunk size */
  229. avio_seek(pb, aiff->ssnd, SEEK_SET);
  230. avio_wb32(pb, file_size - aiff->ssnd - 4);
  231. /* return to the end */
  232. avio_seek(pb, end_size, SEEK_SET);
  233. /* Write ID3 tags */
  234. if (aiff->write_id3v2)
  235. if ((ret = put_id3v2_tags(s, aiff)) < 0)
  236. return ret;
  237. /* File length */
  238. file_size = avio_tell(pb);
  239. avio_seek(pb, aiff->form, SEEK_SET);
  240. avio_wb32(pb, file_size - aiff->form - 4);
  241. avio_flush(pb);
  242. }
  243. while (pict_list) {
  244. AVPacketList *next = pict_list->next;
  245. av_free_packet(&pict_list->pkt);
  246. av_freep(&pict_list);
  247. pict_list = next;
  248. }
  249. return 0;
  250. }
  251. #define OFFSET(x) offsetof(AIFFOutputContext, x)
  252. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  253. static const AVOption options[] = {
  254. { "write_id3v2", "Enable ID3 tags writing.",
  255. OFFSET(write_id3v2), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, ENC },
  256. { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
  257. OFFSET(id3v2_version), AV_OPT_TYPE_INT, {.i64 = 4}, 3, 4, ENC },
  258. { NULL },
  259. };
  260. static const AVClass aiff_muxer_class = {
  261. .class_name = "AIFF muxer",
  262. .item_name = av_default_item_name,
  263. .option = options,
  264. .version = LIBAVUTIL_VERSION_INT,
  265. };
  266. AVOutputFormat ff_aiff_muxer = {
  267. .name = "aiff",
  268. .long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
  269. .mime_type = "audio/aiff",
  270. .extensions = "aif,aiff,afc,aifc",
  271. .priv_data_size = sizeof(AIFFOutputContext),
  272. .audio_codec = AV_CODEC_ID_PCM_S16BE,
  273. .video_codec = AV_CODEC_ID_PNG,
  274. .write_header = aiff_write_header,
  275. .write_packet = aiff_write_packet,
  276. .write_trailer = aiff_write_trailer,
  277. .codec_tag = (const AVCodecTag* const []){ ff_codec_aiff_tags, 0 },
  278. .priv_class = &aiff_muxer_class,
  279. };