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
23 #include "libavcodec/get_bits.h"
24 #include "libavcodec/put_bits.h"
26 #define MAX_FRAME_SIZE 10
28 #define SYNC_WORD 0x6b21
32 #if CONFIG_BIT_DEMUXER
33 static int probe(AVProbeData *p)
35 int i = 0, j, valid = 0;
37 while (2 * i + 3 < p->buf_size){
38 if (AV_RL16(&p->buf[2 * i++]) != SYNC_WORD)
40 j = AV_RL16(&p->buf[2 * i++]);
41 if (j != 0 && j != 0x10 && j != 0x40 && j != 0x50 && j != 0x76)
48 return AVPROBE_SCORE_MAX;
50 return AVPROBE_SCORE_EXTENSION - 1;
54 static int read_header(AVFormatContext *s)
58 st=avformat_new_stream(s, NULL);
60 return AVERROR(ENOMEM);
62 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
63 st->codecpar->codec_id=AV_CODEC_ID_G729;
64 st->codecpar->sample_rate=8000;
65 st->codecpar->block_align = 16;
66 st->codecpar->channels=1;
68 avpriv_set_pts_info(st, 64, 1, 100);
72 static int read_packet(AVFormatContext *s,
75 AVIOContext *pb = s->pb;
77 uint16_t buf[8 * MAX_FRAME_SIZE + 2];
81 int64_t pos= avio_tell(pb);
86 avio_rl16(pb); // sync word
87 packet_size = avio_rl16(pb) / 8;
88 if(packet_size > MAX_FRAME_SIZE)
89 return AVERROR_INVALIDDATA;
91 ret = avio_read(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
94 if(ret != 8 * packet_size * sizeof(uint16_t))
97 if (av_new_packet(pkt, packet_size) < 0)
98 return AVERROR(ENOMEM);
100 init_put_bits(&pbo, pkt->data, packet_size);
101 for(j=0; j < packet_size; j++)
103 put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
105 flush_put_bits(&pbo);
112 AVInputFormat ff_bit_demuxer = {
114 .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
116 .read_header = read_header,
117 .read_packet = read_packet,
123 static int write_header(AVFormatContext *s)
125 AVCodecParameters *par = s->streams[0]->codecpar;
127 if ((par->codec_id != AV_CODEC_ID_G729) || par->channels != 1) {
128 av_log(s, AV_LOG_ERROR,
129 "only codec g729 with 1 channel is supported by this format\n");
130 return AVERROR(EINVAL);
133 par->bits_per_coded_sample = 16;
134 par->block_align = (par->bits_per_coded_sample * par->channels) >> 3;
139 static int write_packet(AVFormatContext *s, AVPacket *pkt)
141 AVIOContext *pb = s->pb;
146 return AVERROR(EINVAL);
148 avio_wl16(pb, SYNC_WORD);
149 avio_wl16(pb, 8 * pkt->size);
151 init_get_bits(&gb, pkt->data, 8 * pkt->size);
152 for (i = 0; i < 8 * pkt->size; i++)
153 avio_wl16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
158 AVOutputFormat ff_bit_muxer = {
160 .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
161 .mime_type = "audio/bit",
163 .audio_codec = AV_CODEC_ID_G729,
164 .video_codec = AV_CODEC_ID_NONE,
165 .write_header = write_header,
166 .write_packet = write_packet,