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.

87 lines
2.4KB

  1. /*
  2. * WavPack muxer
  3. * Copyright (c) 2012 Paul B Mahol
  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/intreadwrite.h"
  22. #include "avformat.h"
  23. #include "internal.h"
  24. #include "apetag.h"
  25. typedef struct WVMuxContext {
  26. int64_t samples;
  27. } WVMuxContext;
  28. static int write_header(AVFormatContext *s)
  29. {
  30. AVCodecContext *codec = s->streams[0]->codec;
  31. if (s->nb_streams > 1) {
  32. av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
  33. return AVERROR(EINVAL);
  34. }
  35. if (codec->codec_id != AV_CODEC_ID_WAVPACK) {
  36. av_log(s, AV_LOG_ERROR, "unsupported codec\n");
  37. return AVERROR(EINVAL);
  38. }
  39. return 0;
  40. }
  41. static int write_packet(AVFormatContext *ctx, AVPacket *pkt)
  42. {
  43. WVMuxContext *s = ctx->priv_data;
  44. if (pkt->size >= 24)
  45. s->samples += AV_RL32(pkt->data + 20);
  46. avio_write(ctx->pb, pkt->data, pkt->size);
  47. return 0;
  48. }
  49. static int write_trailer(AVFormatContext *ctx)
  50. {
  51. WVMuxContext *s = ctx->priv_data;
  52. ff_ape_write(ctx);
  53. if (ctx->pb->seekable && s->samples) {
  54. avio_seek(ctx->pb, 12, SEEK_SET);
  55. if (s->samples < 0xFFFFFFFFu)
  56. avio_wl32(ctx->pb, s->samples);
  57. else
  58. avio_wl32(ctx->pb, 0xFFFFFFFFu);
  59. avio_flush(ctx->pb);
  60. }
  61. return 0;
  62. }
  63. AVOutputFormat ff_wv_muxer = {
  64. .name = "wv",
  65. .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
  66. .priv_data_size = sizeof(WVMuxContext),
  67. .extensions = "wv",
  68. .audio_codec = AV_CODEC_ID_WAVPACK,
  69. .video_codec = AV_CODEC_ID_NONE,
  70. .write_header = write_header,
  71. .write_packet = write_packet,
  72. .write_trailer = write_trailer,
  73. .flags = AVFMT_NOTIMESTAMPS,
  74. };