2 * G.729 bit format muxer and demuxer
3 * Copyright (c) 2007-2008 Vladimir Voroshilov
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavcodec/get_bits.h"
23 #include "libavcodec/put_bits.h"
25 #define MAX_FRAME_SIZE 10
27 #define SYNC_WORD 0x6b21
31 static int probe(AVProbeData *p)
35 if(p->buf_size < 0x40)
38 for(i=0; i+3<p->buf_size && i< 10*0x50; ){
39 if(AV_RL16(&p->buf[0]) != SYNC_WORD)
41 j=AV_RL16(&p->buf[2]);
42 if(j!=0x40 && j!=0x50)
46 return AVPROBE_SCORE_MAX/2;
49 static int read_header(AVFormatContext *s, AVFormatParameters *ap)
53 st=avformat_new_stream(s, NULL);
55 return AVERROR(ENOMEM);
57 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
58 st->codec->codec_id=CODEC_ID_G729;
59 st->codec->sample_rate=8000;
60 st->codec->block_align = 16;
61 st->codec->channels=1;
63 av_set_pts_info(st, 64, 1, 100);
67 static int read_packet(AVFormatContext *s,
70 AVIOContext *pb = s->pb;
72 uint16_t buf[8 * MAX_FRAME_SIZE + 2];
76 int64_t pos= avio_tell(pb);
81 avio_rl16(pb); // sync word
82 packet_size = avio_rl16(pb) / 8;
83 if(packet_size > MAX_FRAME_SIZE)
84 return AVERROR_INVALIDDATA;
86 ret = avio_read(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
89 if(ret != 8 * packet_size * sizeof(uint16_t))
92 av_new_packet(pkt, packet_size);
94 init_put_bits(&pbo, pkt->data, packet_size);
95 for(j=0; j < packet_size; j++)
97 put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
106 AVInputFormat ff_bit_demuxer = {
108 .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
110 .read_header = read_header,
111 .read_packet = read_packet,
116 static int write_header(AVFormatContext *s)
118 AVCodecContext *enc = s->streams[0]->codec;
120 enc->codec_id = CODEC_ID_G729;
122 enc->bits_per_coded_sample = 16;
123 enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
128 static int write_packet(AVFormatContext *s, AVPacket *pkt)
130 AVIOContext *pb = s->pb;
134 avio_wl16(pb, SYNC_WORD);
135 avio_wl16(pb, 8 * 10);
137 init_get_bits(&gb, pkt->data, 8*10);
138 for(i=0; i< 8 * 10; i++)
139 avio_wl16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
145 AVOutputFormat ff_bit_muxer = {
147 .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
148 .mime_type = "audio/bit",
150 .audio_codec = CODEC_ID_G729,
151 .video_codec = CODEC_ID_NONE,
152 .write_header = write_header,
153 .write_packet = write_packet,