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.

215 lines
6.7KB

  1. /*
  2. * raw FLAC muxer
  3. * Copyright (c) 2006-2009 Justin Ruggles
  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/channel_layout.h"
  22. #include "libavutil/opt.h"
  23. #include "libavcodec/flac.h"
  24. #include "avformat.h"
  25. #include "avio_internal.h"
  26. #include "flacenc.h"
  27. #include "vorbiscomment.h"
  28. #include "libavcodec/bytestream.h"
  29. typedef struct FlacMuxerContext {
  30. const AVClass *class;
  31. int write_header;
  32. /* updated streaminfo sent by the encoder at the end */
  33. uint8_t *streaminfo;
  34. } FlacMuxerContext;
  35. static int flac_write_block_padding(AVIOContext *pb, unsigned int n_padding_bytes,
  36. int last_block)
  37. {
  38. avio_w8(pb, last_block ? 0x81 : 0x01);
  39. avio_wb24(pb, n_padding_bytes);
  40. ffio_fill(pb, 0, n_padding_bytes);
  41. return 0;
  42. }
  43. static int flac_write_block_comment(AVIOContext *pb, AVDictionary **m,
  44. int last_block, int bitexact)
  45. {
  46. const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
  47. int64_t len;
  48. uint8_t *p, *p0;
  49. ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
  50. len = ff_vorbiscomment_length(*m, vendor);
  51. if (len >= ((1<<24) - 4))
  52. return AVERROR(EINVAL);
  53. p0 = av_malloc(len+4);
  54. if (!p0)
  55. return AVERROR(ENOMEM);
  56. p = p0;
  57. bytestream_put_byte(&p, last_block ? 0x84 : 0x04);
  58. bytestream_put_be24(&p, len);
  59. ff_vorbiscomment_write(&p, m, vendor);
  60. avio_write(pb, p0, len+4);
  61. av_freep(&p0);
  62. p = NULL;
  63. return 0;
  64. }
  65. static int flac_write_header(struct AVFormatContext *s)
  66. {
  67. int ret;
  68. int padding = s->metadata_header_padding;
  69. AVCodecContext *codec = s->streams[0]->codec;
  70. FlacMuxerContext *c = s->priv_data;
  71. if (!c->write_header)
  72. return 0;
  73. if (s->nb_streams > 1) {
  74. av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
  75. return AVERROR(EINVAL);
  76. }
  77. if (codec->codec_id != AV_CODEC_ID_FLAC) {
  78. av_log(s, AV_LOG_ERROR, "unsupported codec\n");
  79. return AVERROR(EINVAL);
  80. }
  81. if (padding < 0)
  82. padding = 8192;
  83. /* The FLAC specification states that 24 bits are used to represent the
  84. * size of a metadata block so we must clip this value to 2^24-1. */
  85. padding = av_clip_uintp2(padding, 24);
  86. ret = ff_flac_write_header(s->pb, codec->extradata,
  87. codec->extradata_size, 0);
  88. if (ret)
  89. return ret;
  90. /* add the channel layout tag */
  91. if (codec->channel_layout &&
  92. !(codec->channel_layout & ~0x3ffffULL) &&
  93. !ff_flac_is_native_layout(codec->channel_layout)) {
  94. AVDictionaryEntry *chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK",
  95. NULL, 0);
  96. if (chmask) {
  97. av_log(s, AV_LOG_WARNING, "A WAVEFORMATEXTENSIBLE_CHANNEL_MASK is "
  98. "already present, this muxer will not overwrite it.\n");
  99. } else {
  100. uint8_t buf[32];
  101. snprintf(buf, sizeof(buf), "0x%"PRIx64, codec->channel_layout);
  102. av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
  103. }
  104. }
  105. ret = flac_write_block_comment(s->pb, &s->metadata, !padding,
  106. s->flags & AVFMT_FLAG_BITEXACT);
  107. if (ret)
  108. return ret;
  109. /* The command line flac encoder defaults to placing a seekpoint
  110. * every 10s. So one might add padding to allow that later
  111. * but there seems to be no simple way to get the duration here.
  112. * So just add the amount requested by the user. */
  113. if (padding)
  114. flac_write_block_padding(s->pb, padding, 1);
  115. return ret;
  116. }
  117. static int flac_write_trailer(struct AVFormatContext *s)
  118. {
  119. AVIOContext *pb = s->pb;
  120. int64_t file_size;
  121. FlacMuxerContext *c = s->priv_data;
  122. uint8_t *streaminfo = c->streaminfo ? c->streaminfo :
  123. s->streams[0]->codec->extradata;
  124. if (!c->write_header || !streaminfo)
  125. return 0;
  126. if (pb->seekable) {
  127. /* rewrite the STREAMINFO header block data */
  128. file_size = avio_tell(pb);
  129. avio_seek(pb, 8, SEEK_SET);
  130. avio_write(pb, streaminfo, FLAC_STREAMINFO_SIZE);
  131. avio_seek(pb, file_size, SEEK_SET);
  132. avio_flush(pb);
  133. } else {
  134. av_log(s, AV_LOG_WARNING, "unable to rewrite FLAC header.\n");
  135. }
  136. av_freep(&c->streaminfo);
  137. return 0;
  138. }
  139. static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  140. {
  141. FlacMuxerContext *c = s->priv_data;
  142. uint8_t *streaminfo;
  143. int streaminfo_size;
  144. /* check for updated streaminfo */
  145. streaminfo = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
  146. &streaminfo_size);
  147. if (streaminfo && streaminfo_size == FLAC_STREAMINFO_SIZE) {
  148. av_freep(&c->streaminfo);
  149. c->streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
  150. if (!c->streaminfo)
  151. return AVERROR(ENOMEM);
  152. memcpy(c->streaminfo, streaminfo, FLAC_STREAMINFO_SIZE);
  153. }
  154. if (pkt->size)
  155. avio_write(s->pb, pkt->data, pkt->size);
  156. return 0;
  157. }
  158. static const AVOption flacenc_options[] = {
  159. { "write_header", "Write the file header", offsetof(FlacMuxerContext, write_header), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
  160. { NULL },
  161. };
  162. static const AVClass flac_muxer_class = {
  163. .class_name = "flac muxer",
  164. .item_name = av_default_item_name,
  165. .option = flacenc_options,
  166. .version = LIBAVUTIL_VERSION_INT,
  167. };
  168. AVOutputFormat ff_flac_muxer = {
  169. .name = "flac",
  170. .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
  171. .priv_data_size = sizeof(FlacMuxerContext),
  172. .mime_type = "audio/x-flac",
  173. .extensions = "flac",
  174. .audio_codec = AV_CODEC_ID_FLAC,
  175. .video_codec = AV_CODEC_ID_NONE,
  176. .write_header = flac_write_header,
  177. .write_packet = flac_write_packet,
  178. .write_trailer = flac_write_trailer,
  179. .flags = AVFMT_NOTIMESTAMPS,
  180. .priv_class = &flac_muxer_class,
  181. };