vocenc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. typedef struct voc_enc_context {
  23. int param_written;
  24. } VocEncContext;
  25. static int voc_write_header(AVFormatContext *s)
  26. {
  27. ByteIOContext *pb = s->pb;
  28. const int header_size = 26;
  29. const int version = 0x0114;
  30. if (s->nb_streams != 1
  31. || s->streams[0]->codec->codec_type != CODEC_TYPE_AUDIO)
  32. return AVERROR_PATCHWELCOME;
  33. put_buffer(pb, ff_voc_magic, sizeof(ff_voc_magic) - 1);
  34. put_le16(pb, header_size);
  35. put_le16(pb, version);
  36. put_le16(pb, ~version + 0x1234);
  37. return 0;
  38. }
  39. static int voc_write_packet(AVFormatContext *s, AVPacket *pkt)
  40. {
  41. VocEncContext *voc = s->priv_data;
  42. AVCodecContext *enc = s->streams[0]->codec;
  43. ByteIOContext *pb = s->pb;
  44. if (!voc->param_written) {
  45. if (enc->codec_tag > 0xFF) {
  46. put_byte(pb, VOC_TYPE_NEW_VOICE_DATA);
  47. put_le24(pb, pkt->size + 12);
  48. put_le32(pb, enc->sample_rate);
  49. put_byte(pb, enc->bits_per_coded_sample);
  50. put_byte(pb, enc->channels);
  51. put_le16(pb, enc->codec_tag);
  52. put_le32(pb, 0);
  53. } else {
  54. if (s->streams[0]->codec->channels > 1) {
  55. put_byte(pb, VOC_TYPE_EXTENDED);
  56. put_le24(pb, 4);
  57. put_le16(pb, 65536-256000000/(enc->sample_rate*enc->channels));
  58. put_byte(pb, enc->codec_tag);
  59. put_byte(pb, enc->channels - 1);
  60. }
  61. put_byte(pb, VOC_TYPE_VOICE_DATA);
  62. put_le24(pb, pkt->size + 2);
  63. put_byte(pb, 256 - 1000000 / enc->sample_rate);
  64. put_byte(pb, enc->codec_tag);
  65. }
  66. voc->param_written = 1;
  67. } else {
  68. put_byte(pb, VOC_TYPE_VOICE_DATA_CONT);
  69. put_le24(pb, pkt->size);
  70. }
  71. put_buffer(pb, pkt->data, pkt->size);
  72. return 0;
  73. }
  74. static int voc_write_trailer(AVFormatContext *s)
  75. {
  76. put_byte(s->pb, 0);
  77. return 0;
  78. }
  79. AVOutputFormat voc_muxer = {
  80. "voc",
  81. NULL_IF_CONFIG_SMALL("Creative Voice file format"),
  82. "audio/x-voc",
  83. "voc",
  84. sizeof(VocEncContext),
  85. CODEC_ID_PCM_U8,
  86. CODEC_ID_NONE,
  87. voc_write_header,
  88. voc_write_packet,
  89. voc_write_trailer,
  90. .codec_tag=(const AVCodecTag* const []){ff_voc_codec_tags, 0},
  91. };