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.

192 lines
5.9KB

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