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.

156 lines
4.4KB

  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 "libavcodec/flac.h"
  22. #include "avformat.h"
  23. #include "flacenc.h"
  24. #include "metadata.h"
  25. #include "vorbiscomment.h"
  26. #include "libavcodec/bytestream.h"
  27. int ff_flac_write_header(ByteIOContext *pb, AVCodecContext *codec,
  28. int last_block)
  29. {
  30. uint8_t header[8] = {
  31. 0x66, 0x4C, 0x61, 0x43, 0x00, 0x00, 0x00, 0x22
  32. };
  33. uint8_t *streaminfo;
  34. enum FLACExtradataFormat format;
  35. header[4] = last_block ? 0x80 : 0x00;
  36. if (!ff_flac_is_extradata_valid(codec, &format, &streaminfo))
  37. return -1;
  38. /* write "fLaC" stream marker and first metadata block header if needed */
  39. if (format == FLAC_EXTRADATA_FORMAT_STREAMINFO) {
  40. put_buffer(pb, header, 8);
  41. }
  42. /* write STREAMINFO or full header */
  43. put_buffer(pb, codec->extradata, codec->extradata_size);
  44. return 0;
  45. }
  46. static int flac_write_block_padding(ByteIOContext *pb, unsigned int n_padding_bytes,
  47. int last_block)
  48. {
  49. put_byte(pb, last_block ? 0x81 : 0x01);
  50. put_be24(pb, n_padding_bytes);
  51. while (n_padding_bytes > 0) {
  52. put_byte(pb, 0);
  53. n_padding_bytes--;
  54. }
  55. return 0;
  56. }
  57. static int flac_write_block_comment(ByteIOContext *pb, AVMetadata *m,
  58. int last_block, int bitexact)
  59. {
  60. const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
  61. unsigned int len, count;
  62. uint8_t *p, *p0;
  63. len = ff_vorbiscomment_length(m, vendor, &count);
  64. p0 = av_malloc(len+4);
  65. if (!p0)
  66. return AVERROR(ENOMEM);
  67. p = p0;
  68. bytestream_put_byte(&p, last_block ? 0x84 : 0x04);
  69. bytestream_put_be24(&p, len);
  70. ff_vorbiscomment_write(&p, m, vendor, count);
  71. put_buffer(pb, p0, len+4);
  72. av_freep(&p0);
  73. p = NULL;
  74. return 0;
  75. }
  76. static int flac_write_header(struct AVFormatContext *s)
  77. {
  78. int ret;
  79. AVCodecContext *codec = s->streams[0]->codec;
  80. ret = ff_flac_write_header(s->pb, codec, 0);
  81. if (ret)
  82. return ret;
  83. ret = flac_write_block_comment(s->pb, s->metadata, 0,
  84. codec->flags & CODEC_FLAG_BITEXACT);
  85. if (ret)
  86. return ret;
  87. /* The command line flac encoder defaults to placing a seekpoint
  88. * every 10s. So one might add padding to allow that later
  89. * but there seems to be no simple way to get the duration here.
  90. * So let's try the flac default of 8192 bytes */
  91. flac_write_block_padding(s->pb, 8192, 1);
  92. return ret;
  93. }
  94. static int flac_write_trailer(struct AVFormatContext *s)
  95. {
  96. ByteIOContext *pb = s->pb;
  97. uint8_t *streaminfo;
  98. enum FLACExtradataFormat format;
  99. int64_t file_size;
  100. if (!ff_flac_is_extradata_valid(s->streams[0]->codec, &format, &streaminfo))
  101. return -1;
  102. if (!url_is_streamed(pb)) {
  103. /* rewrite the STREAMINFO header block data */
  104. file_size = url_ftell(pb);
  105. url_fseek(pb, 8, SEEK_SET);
  106. put_buffer(pb, streaminfo, FLAC_STREAMINFO_SIZE);
  107. url_fseek(pb, file_size, SEEK_SET);
  108. put_flush_packet(pb);
  109. } else {
  110. av_log(s, AV_LOG_WARNING, "unable to rewrite FLAC header.\n");
  111. }
  112. return 0;
  113. }
  114. static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  115. {
  116. put_buffer(s->pb, pkt->data, pkt->size);
  117. put_flush_packet(s->pb);
  118. return 0;
  119. }
  120. AVOutputFormat flac_muxer = {
  121. "flac",
  122. NULL_IF_CONFIG_SMALL("raw FLAC"),
  123. "audio/x-flac",
  124. "flac",
  125. 0,
  126. CODEC_ID_FLAC,
  127. CODEC_ID_NONE,
  128. flac_write_header,
  129. flac_write_packet,
  130. flac_write_trailer,
  131. .flags= AVFMT_NOTIMESTAMPS,
  132. .metadata_conv = ff_vorbiscomment_metadata_conv,
  133. };