2 #include "libavcodec/get_bits.h"
3 #include "libavcodec/put_bits.h"
5 #define MAX_FRAME_SIZE 10
7 #define SYNC_WORD 0x6b21
11 static int probe(AVProbeData *p)
15 for(i=0; i+3<p->buf_size && i< 10*0x50; ){
16 if(AV_RL16(&p->buf[0]) != SYNC_WORD)
18 j=AV_RL16(&p->buf[2]);
19 if(j!=0x40 && j!=0x50)
23 return AVPROBE_SCORE_MAX/2;
26 static int read_header(AVFormatContext *s, AVFormatParameters *ap)
30 st=av_new_stream(s, 0);
32 return AVERROR(ENOMEM);
34 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
35 st->codec->codec_id=CODEC_ID_G729;
36 st->codec->sample_rate=8000;
37 st->codec->block_align = 16;
38 st->codec->channels=1;
40 av_set_pts_info(st, 64, 1, 100);
44 static int read_packet(AVFormatContext *s,
47 ByteIOContext *pb = s->pb;
49 uint16_t buf[8 * MAX_FRAME_SIZE + 2];
53 int64_t pos= avio_tell(pb);
58 get_le16(pb); // sync word
59 packet_size = get_le16(pb) / 8;
60 if(packet_size > MAX_FRAME_SIZE)
61 return AVERROR_INVALIDDATA;
63 ret = get_buffer(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
66 if(ret != 8 * packet_size * sizeof(uint16_t))
69 av_new_packet(pkt, packet_size);
71 init_put_bits(&pbo, pkt->data, packet_size);
72 for(j=0; j < packet_size; j++)
74 put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
83 AVInputFormat ff_bit_demuxer = {
85 .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
87 .read_header = read_header,
88 .read_packet = read_packet,
93 static int write_header(AVFormatContext *s)
95 AVCodecContext *enc = s->streams[0]->codec;
97 enc->codec_id = CODEC_ID_G729;
99 enc->bits_per_coded_sample = 16;
100 enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
105 static int write_packet(AVFormatContext *s, AVPacket *pkt)
107 ByteIOContext *pb = s->pb;
111 put_le16(pb, SYNC_WORD);
112 put_le16(pb, 8 * 10);
114 init_get_bits(&gb, pkt->data, 8*10);
115 for(i=0; i< 8 * 10; i++)
116 put_le16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
117 put_flush_packet(pb);
122 AVOutputFormat ff_bit_muxer = {
124 .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
125 .mime_type = "audio/bit",
127 .audio_codec = CODEC_ID_G729,
128 .video_codec = CODEC_ID_NONE,
129 .write_header = write_header,
130 .write_packet = write_packet,