wvenc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 "avformat.h"
  22. #include "internal.h"
  23. #include "avio_internal.h"
  24. #include "apetag.h"
  25. typedef struct{
  26. uint32_t duration;
  27. int off;
  28. } WVMuxContext;
  29. static int write_header(AVFormatContext *s)
  30. {
  31. WVMuxContext *wc = s->priv_data;
  32. AVCodecContext *codec = s->streams[0]->codec;
  33. if (s->nb_streams > 1) {
  34. av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
  35. return AVERROR(EINVAL);
  36. }
  37. if (codec->codec_id != AV_CODEC_ID_WAVPACK) {
  38. av_log(s, AV_LOG_ERROR, "unsupported codec\n");
  39. return AVERROR(EINVAL);
  40. }
  41. if (codec->extradata_size > 0) {
  42. av_log_missing_feature(s, "remuxing from matroska container", 0);
  43. return AVERROR_PATCHWELCOME;
  44. }
  45. wc->off = codec->channels > 2 ? 4 : 0;
  46. avpriv_set_pts_info(s->streams[0], 64, 1, codec->sample_rate);
  47. return 0;
  48. }
  49. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  50. {
  51. WVMuxContext *wc = s->priv_data;
  52. AVIOContext *pb = s->pb;
  53. wc->duration += pkt->duration;
  54. ffio_wfourcc(pb, "wvpk");
  55. avio_wl32(pb, pkt->size + 12 + wc->off);
  56. avio_wl16(pb, 0x410);
  57. avio_w8(pb, 0);
  58. avio_w8(pb, 0);
  59. avio_wl32(pb, -1);
  60. avio_wl32(pb, pkt->pts);
  61. avio_write(s->pb, pkt->data, pkt->size);
  62. avio_flush(s->pb);
  63. return 0;
  64. }
  65. static int write_trailer(AVFormatContext *s)
  66. {
  67. WVMuxContext *wc = s->priv_data;
  68. AVIOContext *pb = s->pb;
  69. ff_ape_write(s);
  70. if (pb->seekable) {
  71. avio_seek(pb, 12, SEEK_SET);
  72. avio_wl32(pb, wc->duration);
  73. avio_flush(pb);
  74. }
  75. return 0;
  76. }
  77. AVOutputFormat ff_wv_muxer = {
  78. .name = "wv",
  79. .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
  80. .priv_data_size = sizeof(WVMuxContext),
  81. .extensions = "wv",
  82. .audio_codec = AV_CODEC_ID_WAVPACK,
  83. .video_codec = AV_CODEC_ID_NONE,
  84. .write_header = write_header,
  85. .write_packet = write_packet,
  86. .write_trailer = write_trailer,
  87. };