vocenc.c 3.1 KB

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