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.

135 lines
3.0KB

  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. uint16_t* src=buf;
  44. int i, j, ret;
  45. int64_t pos= avio_tell(pb);
  46. if(url_feof(pb))
  47. return AVERROR_EOF;
  48. get_le16(pb); // sync word
  49. packet_size = get_le16(pb) / 8;
  50. if(packet_size > MAX_FRAME_SIZE)
  51. return AVERROR(EIO);
  52. ret = get_buffer(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
  53. if(ret<0)
  54. return ret;
  55. if(ret != 8 * packet_size * sizeof(uint16_t))
  56. return AVERROR(EIO);
  57. av_new_packet(pkt, packet_size);
  58. init_put_bits(&pbo, pkt->data, packet_size);
  59. for(j=0; j < packet_size; j++)
  60. for(i=0; i<8;i++)
  61. put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
  62. flush_put_bits(&pbo);
  63. pkt->duration=1;
  64. pkt->pos = pos;
  65. return 0;
  66. }
  67. AVInputFormat ff_bit_demuxer = {
  68. .name = "bit",
  69. .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
  70. .read_probe = probe,
  71. .read_header = read_header,
  72. .read_packet = read_packet,
  73. .extensions = "bit",
  74. };
  75. #ifdef CONFIG_MUXERS
  76. static int write_header(AVFormatContext *s)
  77. {
  78. AVCodecContext *enc = s->streams[0]->codec;
  79. enc->codec_id = CODEC_ID_G729;
  80. enc->channels = 1;
  81. enc->bits_per_coded_sample = 16;
  82. enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
  83. return 0;
  84. }
  85. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  86. {
  87. ByteIOContext *pb = s->pb;
  88. GetBitContext gb;
  89. int i;
  90. uint16_t b;
  91. put_le16(pb, SYNC_WORD);
  92. put_le16(pb, 8 * 10);
  93. init_get_bits(&gb, pkt->data, 8*10);
  94. for(i=0; i< 8 * 10; i++)
  95. put_le16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
  96. put_flush_packet(pb);
  97. return 0;
  98. }
  99. AVOutputFormat ff_bit_muxer = {
  100. .name = "bit",
  101. .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
  102. .mime_type = "audio/bit",
  103. .extensions = "bit",
  104. .audio_codec = CODEC_ID_G729,
  105. .video_codec = CODEC_ID_NONE,
  106. .write_header = write_header,
  107. .write_packet = write_packet,
  108. };
  109. #endif