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.

112 lines
3.6KB

  1. /*
  2. * Creative Voice File muxer.
  3. * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
  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 "voc.h"
  22. #include "internal.h"
  23. typedef struct voc_enc_context {
  24. int param_written;
  25. } VocEncContext;
  26. static int voc_write_header(AVFormatContext *s)
  27. {
  28. AVIOContext *pb = s->pb;
  29. AVCodecContext *enc = s->streams[0]->codec;
  30. const int header_size = 26;
  31. const int version = 0x0114;
  32. if (s->nb_streams != 1
  33. || s->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO)
  34. return AVERROR_PATCHWELCOME;
  35. if (!enc->codec_tag && enc->codec_id != AV_CODEC_ID_PCM_U8) {
  36. av_log(s, AV_LOG_ERROR, "unsupported codec\n");
  37. return AVERROR(EINVAL);
  38. }
  39. avio_write(pb, ff_voc_magic, sizeof(ff_voc_magic) - 1);
  40. avio_wl16(pb, header_size);
  41. avio_wl16(pb, version);
  42. avio_wl16(pb, ~version + 0x1234);
  43. return 0;
  44. }
  45. static int voc_write_packet(AVFormatContext *s, AVPacket *pkt)
  46. {
  47. VocEncContext *voc = s->priv_data;
  48. AVCodecContext *enc = s->streams[0]->codec;
  49. AVIOContext *pb = s->pb;
  50. if (!voc->param_written) {
  51. if (enc->codec_tag > 3) {
  52. avio_w8(pb, VOC_TYPE_NEW_VOICE_DATA);
  53. avio_wl24(pb, pkt->size + 12);
  54. avio_wl32(pb, enc->sample_rate);
  55. avio_w8(pb, enc->bits_per_coded_sample);
  56. avio_w8(pb, enc->channels);
  57. avio_wl16(pb, enc->codec_tag);
  58. avio_wl32(pb, 0);
  59. } else {
  60. if (s->streams[0]->codec->channels > 1) {
  61. avio_w8(pb, VOC_TYPE_EXTENDED);
  62. avio_wl24(pb, 4);
  63. avio_wl16(pb, 65536-(256000000 + enc->sample_rate*enc->channels/2)/(enc->sample_rate*enc->channels));
  64. avio_w8(pb, enc->codec_tag);
  65. avio_w8(pb, enc->channels - 1);
  66. }
  67. avio_w8(pb, VOC_TYPE_VOICE_DATA);
  68. avio_wl24(pb, pkt->size + 2);
  69. avio_w8(pb, 256 - (1000000 + enc->sample_rate/2) / enc->sample_rate);
  70. avio_w8(pb, enc->codec_tag);
  71. }
  72. voc->param_written = 1;
  73. } else {
  74. avio_w8(pb, VOC_TYPE_VOICE_DATA_CONT);
  75. avio_wl24(pb, pkt->size);
  76. }
  77. avio_write(pb, pkt->data, pkt->size);
  78. return 0;
  79. }
  80. static int voc_write_trailer(AVFormatContext *s)
  81. {
  82. avio_w8(s->pb, 0);
  83. return 0;
  84. }
  85. AVOutputFormat ff_voc_muxer = {
  86. .name = "voc",
  87. .long_name = NULL_IF_CONFIG_SMALL("Creative Voice"),
  88. .mime_type = "audio/x-voc",
  89. .extensions = "voc",
  90. .priv_data_size = sizeof(VocEncContext),
  91. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  92. .video_codec = AV_CODEC_ID_NONE,
  93. .write_header = voc_write_header,
  94. .write_packet = voc_write_packet,
  95. .write_trailer = voc_write_trailer,
  96. .codec_tag = (const AVCodecTag* const []){ ff_voc_codec_tags, 0 },
  97. .flags = AVFMT_NOTIMESTAMPS,
  98. };