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.

166 lines
4.2KB

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