bit.c 3.9 KB

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