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.

149 lines
3.1KB

  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. url_fskip(pb, 2);
  29. switch(get_le16(pb))
  30. {
  31. case 0x40:
  32. st->codec->bit_rate = 6400;
  33. break;
  34. case 0x50:
  35. st->codec->bit_rate = 8000;
  36. break;
  37. }
  38. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  39. st->codec->codec_id=CODEC_ID_G729;
  40. st->codec->sample_rate=8000;
  41. st->codec->block_align = 16;
  42. st->codec->channels=1;
  43. av_set_pts_info(st, 64, 1, 100);
  44. url_fseek(pb, 0, SEEK_SET);
  45. return 0;
  46. }
  47. static int read_packet(AVFormatContext *s,
  48. AVPacket *pkt)
  49. {
  50. ByteIOContext *pb = s->pb;
  51. PutBitContext pbo;
  52. uint16_t buf[8 * MAX_FRAME_SIZE + 2];
  53. int packet_size;
  54. int sync;
  55. uint16_t* src=buf;
  56. int i, j, ret;
  57. if(url_feof(pb))
  58. return AVERROR_EOF;
  59. sync = get_le16(pb); // sync word
  60. packet_size = get_le16(pb) / 8;
  61. if(packet_size > MAX_FRAME_SIZE)
  62. return AVERROR(EIO);
  63. ret = get_buffer(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
  64. if(ret<0)
  65. return ret;
  66. if(ret != 8 * packet_size * sizeof(uint16_t))
  67. return AVERROR(EIO);
  68. av_new_packet(pkt, packet_size);
  69. init_put_bits(&pbo, pkt->data, packet_size);
  70. for(j=0; j < packet_size; j++)
  71. for(i=0; i<8;i++)
  72. put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
  73. flush_put_bits(&pbo);
  74. pkt->duration=1;
  75. return 0;
  76. }
  77. AVInputFormat ff_bit_demuxer = {
  78. "bit",
  79. "G.729 BIT file format",
  80. 0,
  81. probe,
  82. read_header,
  83. read_packet,
  84. .extensions = "bit"
  85. };
  86. #ifdef CONFIG_MUXERS
  87. static int write_header(AVFormatContext *s)
  88. {
  89. AVCodecContext *enc = s->streams[0]->codec;
  90. enc->codec_id = CODEC_ID_G729;
  91. enc->channels = 1;
  92. enc->bits_per_coded_sample = 16;
  93. enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
  94. return 0;
  95. }
  96. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  97. {
  98. ByteIOContext *pb = s->pb;
  99. GetBitContext gb;
  100. int i;
  101. uint16_t b;
  102. put_le16(pb, SYNC_WORD);
  103. put_le16(pb, 8 * 10);
  104. init_get_bits(&gb, pkt->data, 8*10);
  105. for(i=0; i< 8 * 10; i++)
  106. put_le16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
  107. put_flush_packet(pb);
  108. return 0;
  109. }
  110. AVOutputFormat ff_bit_muxer = {
  111. "bit",
  112. "G.729 BIT file format",
  113. "audio/bit",
  114. "bit",
  115. 0,
  116. CODEC_ID_G729,
  117. CODEC_ID_NONE,
  118. write_header,
  119. write_packet,
  120. .extensions = "bit"
  121. };
  122. #endif