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.

198 lines
6.1KB

  1. /*
  2. * raw FLAC muxer
  3. * Copyright (c) 2006-2009 Justin Ruggles
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "flacenc.h"
  26. #include "vorbiscomment.h"
  27. #include "libavcodec/bytestream.h"
  28. typedef struct FlacMuxerContext {
  29. const AVClass *class;
  30. int write_header;
  31. /* updated streaminfo sent by the encoder at the end */
  32. uint8_t *streaminfo;
  33. } FlacMuxerContext;
  34. static int flac_write_block_padding(AVIOContext *pb, unsigned int n_padding_bytes,
  35. int last_block)
  36. {
  37. avio_w8(pb, last_block ? 0x81 : 0x01);
  38. avio_wb24(pb, n_padding_bytes);
  39. while (n_padding_bytes > 0) {
  40. avio_w8(pb, 0);
  41. n_padding_bytes--;
  42. }
  43. return 0;
  44. }
  45. static int flac_write_block_comment(AVIOContext *pb, AVDictionary **m,
  46. int last_block, int bitexact)
  47. {
  48. const char *vendor = bitexact ? "Libav" : LIBAVFORMAT_IDENT;
  49. unsigned int len;
  50. uint8_t *p, *p0;
  51. ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
  52. len = ff_vorbiscomment_length(*m, vendor);
  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. AVCodecParameters *par = s->streams[0]->codecpar;
  69. FlacMuxerContext *c = s->priv_data;
  70. if (!c->write_header)
  71. return 0;
  72. ret = ff_flac_write_header(s->pb, par->extradata,
  73. par->extradata_size, 0);
  74. if (ret)
  75. return ret;
  76. /* add the channel layout tag */
  77. if (par->channel_layout &&
  78. !(par->channel_layout & ~0x3ffffULL) &&
  79. !ff_flac_is_native_layout(par->channel_layout)) {
  80. AVDictionaryEntry *chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK",
  81. NULL, 0);
  82. if (chmask) {
  83. av_log(s, AV_LOG_WARNING, "A WAVEFORMATEXTENSIBLE_CHANNEL_MASK is "
  84. "already present, this muxer will not overwrite it.\n");
  85. } else {
  86. uint8_t buf[32];
  87. snprintf(buf, sizeof(buf), "0x%"PRIx64, par->channel_layout);
  88. av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
  89. }
  90. }
  91. ret = flac_write_block_comment(s->pb, &s->metadata, 0,
  92. s->flags & AVFMT_FLAG_BITEXACT);
  93. if (ret)
  94. return ret;
  95. /* The command line flac encoder defaults to placing a seekpoint
  96. * every 10s. So one might add padding to allow that later
  97. * but there seems to be no simple way to get the duration here.
  98. * So let's try the flac default of 8192 bytes */
  99. flac_write_block_padding(s->pb, 8192, 1);
  100. return ret;
  101. }
  102. static int flac_write_trailer(struct AVFormatContext *s)
  103. {
  104. AVIOContext *pb = s->pb;
  105. int64_t file_size;
  106. FlacMuxerContext *c = s->priv_data;
  107. uint8_t *streaminfo = c->streaminfo ? c->streaminfo :
  108. s->streams[0]->codecpar->extradata;
  109. if (!c->write_header || !streaminfo)
  110. return 0;
  111. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  112. /* rewrite the STREAMINFO header block data */
  113. file_size = avio_tell(pb);
  114. avio_seek(pb, 8, SEEK_SET);
  115. avio_write(pb, streaminfo, FLAC_STREAMINFO_SIZE);
  116. avio_seek(pb, file_size, SEEK_SET);
  117. avio_flush(pb);
  118. } else {
  119. av_log(s, AV_LOG_WARNING, "unable to rewrite FLAC header.\n");
  120. }
  121. av_freep(&c->streaminfo);
  122. return 0;
  123. }
  124. static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  125. {
  126. FlacMuxerContext *c = s->priv_data;
  127. uint8_t *streaminfo;
  128. int streaminfo_size;
  129. /* check for updated streaminfo */
  130. streaminfo = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
  131. &streaminfo_size);
  132. if (streaminfo && streaminfo_size == FLAC_STREAMINFO_SIZE) {
  133. av_freep(&c->streaminfo);
  134. c->streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
  135. if (!c->streaminfo)
  136. return AVERROR(ENOMEM);
  137. memcpy(c->streaminfo, streaminfo, FLAC_STREAMINFO_SIZE);
  138. }
  139. if (pkt->size)
  140. avio_write(s->pb, pkt->data, pkt->size);
  141. return 0;
  142. }
  143. static const AVOption flacenc_options[] = {
  144. { "write_header", "Write the file header", offsetof(FlacMuxerContext, write_header), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
  145. { NULL },
  146. };
  147. static const AVClass flac_muxer_class = {
  148. .class_name = "flac muxer",
  149. .item_name = av_default_item_name,
  150. .option = flacenc_options,
  151. .version = LIBAVUTIL_VERSION_INT,
  152. };
  153. AVOutputFormat ff_flac_muxer = {
  154. .name = "flac",
  155. .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
  156. .priv_data_size = sizeof(FlacMuxerContext),
  157. .mime_type = "audio/x-flac",
  158. .extensions = "flac",
  159. .audio_codec = AV_CODEC_ID_FLAC,
  160. .video_codec = AV_CODEC_ID_NONE,
  161. .write_header = flac_write_header,
  162. .write_packet = flac_write_packet,
  163. .write_trailer = flac_write_trailer,
  164. .flags = AVFMT_NOTIMESTAMPS,
  165. .priv_class = &flac_muxer_class,
  166. };