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.

136 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. if(p->buf_size < 0x40)
  12. return 0;
  13. for(i=0; i+3<p->buf_size && i< 10*0x50; ){
  14. if(AV_RL16(&p->buf[0]) != SYNC_WORD)
  15. return 0;
  16. j=AV_RL16(&p->buf[2]);
  17. if(j!=0x40 && j!=0x50)
  18. return 0;
  19. i+=j;
  20. }
  21. return AVPROBE_SCORE_MAX/2;
  22. }
  23. static int read_header(AVFormatContext *s, AVFormatParameters *ap)
  24. {
  25. AVStream* st;
  26. st=av_new_stream(s, 0);
  27. if (!st)
  28. return AVERROR(ENOMEM);
  29. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  30. st->codec->codec_id=CODEC_ID_G729;
  31. st->codec->sample_rate=8000;
  32. st->codec->block_align = 16;
  33. st->codec->channels=1;
  34. av_set_pts_info(st, 64, 1, 100);
  35. return 0;
  36. }
  37. static int read_packet(AVFormatContext *s,
  38. AVPacket *pkt)
  39. {
  40. ByteIOContext *pb = s->pb;
  41. PutBitContext pbo;
  42. uint16_t buf[8 * MAX_FRAME_SIZE + 2];
  43. int packet_size;
  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. get_le16(pb); // sync word
  50. packet_size = get_le16(pb) / 8;
  51. if(packet_size > MAX_FRAME_SIZE)
  52. return AVERROR_INVALIDDATA;
  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. .name = "bit",
  70. .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
  71. .read_probe = probe,
  72. .read_header = read_header,
  73. .read_packet = read_packet,
  74. .extensions = "bit",
  75. };
  76. #ifdef CONFIG_MUXERS
  77. static int write_header(AVFormatContext *s)
  78. {
  79. AVCodecContext *enc = s->streams[0]->codec;
  80. enc->codec_id = CODEC_ID_G729;
  81. enc->channels = 1;
  82. enc->bits_per_coded_sample = 16;
  83. enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
  84. return 0;
  85. }
  86. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  87. {
  88. ByteIOContext *pb = s->pb;
  89. GetBitContext gb;
  90. int i;
  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