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.

169 lines
4.4KB

  1. /*
  2. * G.729 bit format muxer and demuxer
  3. * Copyright (c) 2007-2008 Vladimir Voroshilov
  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 "libavcodec/get_bits.h"
  24. #include "libavcodec/put_bits.h"
  25. #define MAX_FRAME_SIZE 10
  26. #define SYNC_WORD 0x6b21
  27. #define BIT_0 0x7f
  28. #define BIT_1 0x81
  29. #if CONFIG_BIT_DEMUXER
  30. static int probe(const AVProbeData *p)
  31. {
  32. int i = 0, j, valid = 0;
  33. while (2 * i + 3 < p->buf_size){
  34. if (AV_RL16(&p->buf[2 * i++]) != SYNC_WORD)
  35. return 0;
  36. j = AV_RL16(&p->buf[2 * i++]);
  37. if (j != 0 && j != 0x10 && j != 0x40 && j != 0x50 && j != 0x76)
  38. return 0;
  39. if (j)
  40. valid++;
  41. i += j;
  42. }
  43. if (valid > 10)
  44. return AVPROBE_SCORE_MAX;
  45. if (valid > 2)
  46. return AVPROBE_SCORE_EXTENSION - 1;
  47. return 0;
  48. }
  49. static int read_header(AVFormatContext *s)
  50. {
  51. AVStream* st;
  52. st=avformat_new_stream(s, NULL);
  53. if (!st)
  54. return AVERROR(ENOMEM);
  55. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  56. st->codecpar->codec_id=AV_CODEC_ID_G729;
  57. st->codecpar->sample_rate=8000;
  58. st->codecpar->block_align = 16;
  59. st->codecpar->channels=1;
  60. avpriv_set_pts_info(st, 64, 1, 100);
  61. return 0;
  62. }
  63. static int read_packet(AVFormatContext *s,
  64. AVPacket *pkt)
  65. {
  66. AVIOContext *pb = s->pb;
  67. PutBitContext pbo;
  68. uint16_t buf[8 * MAX_FRAME_SIZE + 2];
  69. int packet_size;
  70. uint16_t* src=buf;
  71. int i, j, ret;
  72. int64_t pos= avio_tell(pb);
  73. if(avio_feof(pb))
  74. return AVERROR_EOF;
  75. avio_rl16(pb); // sync word
  76. packet_size = avio_rl16(pb) / 8;
  77. if(packet_size > MAX_FRAME_SIZE)
  78. return AVERROR_INVALIDDATA;
  79. ret = avio_read(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
  80. if(ret<0)
  81. return ret;
  82. if(ret != 8 * packet_size * sizeof(uint16_t))
  83. return AVERROR(EIO);
  84. if (av_new_packet(pkt, packet_size) < 0)
  85. return AVERROR(ENOMEM);
  86. init_put_bits(&pbo, pkt->data, packet_size);
  87. for(j=0; j < packet_size; j++)
  88. for(i=0; i<8;i++)
  89. put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
  90. flush_put_bits(&pbo);
  91. pkt->duration=1;
  92. pkt->pos = pos;
  93. return 0;
  94. }
  95. AVInputFormat ff_bit_demuxer = {
  96. .name = "bit",
  97. .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
  98. .read_probe = probe,
  99. .read_header = read_header,
  100. .read_packet = read_packet,
  101. .extensions = "bit",
  102. };
  103. #endif
  104. #if CONFIG_BIT_MUXER
  105. static int write_header(AVFormatContext *s)
  106. {
  107. AVCodecParameters *par = s->streams[0]->codecpar;
  108. if ((par->codec_id != AV_CODEC_ID_G729) || par->channels != 1) {
  109. av_log(s, AV_LOG_ERROR,
  110. "only codec g729 with 1 channel is supported by this format\n");
  111. return AVERROR(EINVAL);
  112. }
  113. par->bits_per_coded_sample = 16;
  114. par->block_align = (par->bits_per_coded_sample * par->channels) >> 3;
  115. return 0;
  116. }
  117. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  118. {
  119. AVIOContext *pb = s->pb;
  120. GetBitContext gb;
  121. int i;
  122. if (pkt->size != 10)
  123. return AVERROR(EINVAL);
  124. avio_wl16(pb, SYNC_WORD);
  125. avio_wl16(pb, 8 * pkt->size);
  126. init_get_bits(&gb, pkt->data, 8 * pkt->size);
  127. for (i = 0; i < 8 * pkt->size; i++)
  128. avio_wl16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
  129. return 0;
  130. }
  131. AVOutputFormat ff_bit_muxer = {
  132. .name = "bit",
  133. .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
  134. .mime_type = "audio/bit",
  135. .extensions = "bit",
  136. .audio_codec = AV_CODEC_ID_G729,
  137. .video_codec = AV_CODEC_ID_NONE,
  138. .write_header = write_header,
  139. .write_packet = write_packet,
  140. };
  141. #endif