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.

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