00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avformat.h"
00022 #include "internal.h"
00023 #include "libavcodec/get_bits.h"
00024 #include "libavcodec/put_bits.h"
00025
00026 #define MAX_FRAME_SIZE 10
00027
00028 #define SYNC_WORD 0x6b21
00029 #define BIT_0 0x7f
00030 #define BIT_1 0x81
00031
00032 static int probe(AVProbeData *p)
00033 {
00034 int i, j;
00035
00036 if(p->buf_size < 0x40)
00037 return 0;
00038
00039 for(i=0; i+3<p->buf_size && i< 10*0x50; ){
00040 if(AV_RL16(&p->buf[0]) != SYNC_WORD)
00041 return 0;
00042 j=AV_RL16(&p->buf[2]);
00043 if(j!=0x40 && j!=0x50)
00044 return 0;
00045 i+=j;
00046 }
00047 return AVPROBE_SCORE_MAX/2;
00048 }
00049
00050 static int read_header(AVFormatContext *s)
00051 {
00052 AVStream* st;
00053
00054 st=avformat_new_stream(s, NULL);
00055 if (!st)
00056 return AVERROR(ENOMEM);
00057
00058 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00059 st->codec->codec_id=AV_CODEC_ID_G729;
00060 st->codec->sample_rate=8000;
00061 st->codec->block_align = 16;
00062 st->codec->channels=1;
00063
00064 avpriv_set_pts_info(st, 64, 1, 100);
00065 return 0;
00066 }
00067
00068 static int read_packet(AVFormatContext *s,
00069 AVPacket *pkt)
00070 {
00071 AVIOContext *pb = s->pb;
00072 PutBitContext pbo;
00073 uint16_t buf[8 * MAX_FRAME_SIZE + 2];
00074 int packet_size;
00075 uint16_t* src=buf;
00076 int i, j, ret;
00077 int64_t pos= avio_tell(pb);
00078
00079 if(url_feof(pb))
00080 return AVERROR_EOF;
00081
00082 avio_rl16(pb);
00083 packet_size = avio_rl16(pb) / 8;
00084 if(packet_size > MAX_FRAME_SIZE)
00085 return AVERROR_INVALIDDATA;
00086
00087 ret = avio_read(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
00088 if(ret<0)
00089 return ret;
00090 if(ret != 8 * packet_size * sizeof(uint16_t))
00091 return AVERROR(EIO);
00092
00093 av_new_packet(pkt, packet_size);
00094
00095 init_put_bits(&pbo, pkt->data, packet_size);
00096 for(j=0; j < packet_size; j++)
00097 for(i=0; i<8;i++)
00098 put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
00099
00100 flush_put_bits(&pbo);
00101
00102 pkt->duration=1;
00103 pkt->pos = pos;
00104 return 0;
00105 }
00106
00107 AVInputFormat ff_bit_demuxer = {
00108 .name = "bit",
00109 .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
00110 .read_probe = probe,
00111 .read_header = read_header,
00112 .read_packet = read_packet,
00113 .extensions = "bit",
00114 };
00115
00116 #if CONFIG_MUXERS
00117 static int write_header(AVFormatContext *s)
00118 {
00119 AVCodecContext *enc = s->streams[0]->codec;
00120
00121 enc->codec_id = AV_CODEC_ID_G729;
00122 enc->channels = 1;
00123 enc->bits_per_coded_sample = 16;
00124 enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
00125
00126 return 0;
00127 }
00128
00129 static int write_packet(AVFormatContext *s, AVPacket *pkt)
00130 {
00131 AVIOContext *pb = s->pb;
00132 GetBitContext gb;
00133 int i;
00134
00135 avio_wl16(pb, SYNC_WORD);
00136 avio_wl16(pb, 8 * 10);
00137
00138 init_get_bits(&gb, pkt->data, 8*10);
00139 for(i=0; i< 8 * 10; i++)
00140 avio_wl16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
00141 avio_flush(pb);
00142
00143 return 0;
00144 }
00145
00146 AVOutputFormat ff_bit_muxer = {
00147 .name = "bit",
00148 .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
00149 .mime_type = "audio/bit",
00150 .extensions = "bit",
00151 .audio_codec = AV_CODEC_ID_G729,
00152 .video_codec = AV_CODEC_ID_NONE,
00153 .write_header = write_header,
00154 .write_packet = write_packet,
00155 };
00156 #endif