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.

139 lines
2.9KB

  1. #include "avformat.h"
  2. #include "libavcodec/get_bits.h"
  3. #include "libavcodec/put_bits.h"
  4. #define MAX_FRAME_SIZE 10
  5. #define SYNC_WORD 0x6b21
  6. #define BIT_0 0x7f
  7. #define BIT_1 0x81
  8. static int probe(AVProbeData *p)
  9. {
  10. int i, j;
  11. for(i=0; i+3<p->buf_size && i< 10*0x50; ){
  12. if(AV_RL16(&p->buf[0]) != SYNC_WORD)
  13. return 0;
  14. j=AV_RL16(&p->buf[2]);
  15. if(j!=0x40 && j!=0x50)
  16. return 0;
  17. i+=j;
  18. }
  19. return AVPROBE_SCORE_MAX/2;
  20. }
  21. static int read_header(AVFormatContext *s, AVFormatParameters *ap)
  22. {
  23. AVStream* st;
  24. ByteIOContext *pb = s->pb;
  25. st=av_new_stream(s, 0);
  26. if (!st)
  27. return AVERROR(ENOMEM);
  28. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  29. st->codec->codec_id=CODEC_ID_G729;
  30. st->codec->sample_rate=8000;
  31. st->codec->block_align = 16;
  32. st->codec->channels=1;
  33. av_set_pts_info(st, 64, 1, 100);
  34. return 0;
  35. }
  36. static int read_packet(AVFormatContext *s,
  37. AVPacket *pkt)
  38. {
  39. ByteIOContext *pb = s->pb;
  40. PutBitContext pbo;
  41. uint16_t buf[8 * MAX_FRAME_SIZE + 2];
  42. int packet_size;
  43. int sync;
  44. uint16_t* src=buf;
  45. int i, j, ret;
  46. int64_t pos= avio_tell(pb);
  47. if(url_feof(pb))
  48. return AVERROR_EOF;
  49. sync = get_le16(pb); // sync word
  50. packet_size = get_le16(pb) / 8;
  51. if(packet_size > MAX_FRAME_SIZE)
  52. return AVERROR(EIO);
  53. ret = get_buffer(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
  54. if(ret<0)
  55. return ret;
  56. if(ret != 8 * packet_size * sizeof(uint16_t))
  57. return AVERROR(EIO);
  58. av_new_packet(pkt, packet_size);
  59. init_put_bits(&pbo, pkt->data, packet_size);
  60. for(j=0; j < packet_size; j++)
  61. for(i=0; i<8;i++)
  62. put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
  63. flush_put_bits(&pbo);
  64. pkt->duration=1;
  65. pkt->pos = pos;
  66. return 0;
  67. }
  68. AVInputFormat ff_bit_demuxer = {
  69. "bit",
  70. "G.729 BIT file format",
  71. 0,
  72. probe,
  73. read_header,
  74. read_packet,
  75. .extensions = "bit"
  76. };
  77. #ifdef CONFIG_MUXERS
  78. static int write_header(AVFormatContext *s)
  79. {
  80. AVCodecContext *enc = s->streams[0]->codec;
  81. enc->codec_id = CODEC_ID_G729;
  82. enc->channels = 1;
  83. enc->bits_per_coded_sample = 16;
  84. enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
  85. return 0;
  86. }
  87. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  88. {
  89. ByteIOContext *pb = s->pb;
  90. GetBitContext gb;
  91. int i;
  92. uint16_t b;
  93. put_le16(pb, SYNC_WORD);
  94. put_le16(pb, 8 * 10);
  95. init_get_bits(&gb, pkt->data, 8*10);
  96. for(i=0; i< 8 * 10; i++)
  97. put_le16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
  98. put_flush_packet(pb);
  99. return 0;
  100. }
  101. AVOutputFormat ff_bit_muxer = {
  102. "bit",
  103. "G.729 BIT file format",
  104. "audio/bit",
  105. "bit",
  106. 0,
  107. CODEC_ID_G729,
  108. CODEC_ID_NONE,
  109. write_header,
  110. write_packet,
  111. .extensions = "bit"
  112. };
  113. #endif