bit.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "libavcodec/get_bits.h"
  23. #include "libavcodec/put_bits.h"
  24. #define MAX_FRAME_SIZE 10
  25. #define SYNC_WORD 0x6b21
  26. #define BIT_0 0x7f
  27. #define BIT_1 0x81
  28. static int probe(AVProbeData *p)
  29. {
  30. int i, j;
  31. if(p->buf_size < 0x40)
  32. return 0;
  33. for(i=0; i+3<p->buf_size && i< 10*0x50; ){
  34. if(AV_RL16(&p->buf[0]) != SYNC_WORD)
  35. return 0;
  36. j=AV_RL16(&p->buf[2]);
  37. if(j!=0x40 && j!=0x50)
  38. return 0;
  39. i+=j;
  40. }
  41. return AVPROBE_SCORE_MAX/2;
  42. }
  43. static int read_header(AVFormatContext *s, AVFormatParameters *ap)
  44. {
  45. AVStream* st;
  46. st=avformat_new_stream(s, NULL);
  47. if (!st)
  48. return AVERROR(ENOMEM);
  49. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  50. st->codec->codec_id=CODEC_ID_G729;
  51. st->codec->sample_rate=8000;
  52. st->codec->block_align = 16;
  53. st->codec->channels=1;
  54. av_set_pts_info(st, 64, 1, 100);
  55. return 0;
  56. }
  57. static int read_packet(AVFormatContext *s,
  58. AVPacket *pkt)
  59. {
  60. AVIOContext *pb = s->pb;
  61. PutBitContext pbo;
  62. uint16_t buf[8 * MAX_FRAME_SIZE + 2];
  63. int packet_size;
  64. uint16_t* src=buf;
  65. int i, j, ret;
  66. int64_t pos= avio_tell(pb);
  67. if(url_feof(pb))
  68. return AVERROR_EOF;
  69. avio_rl16(pb); // sync word
  70. packet_size = avio_rl16(pb) / 8;
  71. if(packet_size > MAX_FRAME_SIZE)
  72. return AVERROR_INVALIDDATA;
  73. ret = avio_read(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
  74. if(ret<0)
  75. return ret;
  76. if(ret != 8 * packet_size * sizeof(uint16_t))
  77. return AVERROR(EIO);
  78. av_new_packet(pkt, packet_size);
  79. init_put_bits(&pbo, pkt->data, packet_size);
  80. for(j=0; j < packet_size; j++)
  81. for(i=0; i<8;i++)
  82. put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
  83. flush_put_bits(&pbo);
  84. pkt->duration=1;
  85. pkt->pos = pos;
  86. return 0;
  87. }
  88. AVInputFormat ff_bit_demuxer = {
  89. .name = "bit",
  90. .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
  91. .read_probe = probe,
  92. .read_header = read_header,
  93. .read_packet = read_packet,
  94. .extensions = "bit",
  95. };
  96. #ifdef CONFIG_MUXERS
  97. static int write_header(AVFormatContext *s)
  98. {
  99. AVCodecContext *enc = s->streams[0]->codec;
  100. enc->codec_id = CODEC_ID_G729;
  101. enc->channels = 1;
  102. enc->bits_per_coded_sample = 16;
  103. enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
  104. return 0;
  105. }
  106. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  107. {
  108. ByteIOContext *pb = s->pb;
  109. GetBitContext gb;
  110. int i;
  111. avio_wl16(pb, SYNC_WORD);
  112. avio_wl16(pb, 8 * 10);
  113. init_get_bits(&gb, pkt->data, 8*10);
  114. for(i=0; i< 8 * 10; i++)
  115. avio_wl16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
  116. avio_flush(pb);
  117. return 0;
  118. }
  119. AVOutputFormat ff_bit_muxer = {
  120. .name = "bit",
  121. .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
  122. .mime_type = "audio/bit",
  123. .extensions = "bit",
  124. .audio_codec = CODEC_ID_G729,
  125. .video_codec = CODEC_ID_NONE,
  126. .write_header = write_header,
  127. .write_packet = write_packet,
  128. };
  129. #endif