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 "config_components.h"
25 #include "avio_internal.h"
29 #include "libavcodec/get_bits.h"
30 #include "libavcodec/put_bits.h"
32 #define MAX_FRAME_SIZE 10
34 #define SYNC_WORD 0x6b21
38 #if CONFIG_BIT_DEMUXER
39 static int probe(const AVProbeData
*p
)
41 int i
= 0, j
, valid
= 0;
43 while (2 * i
+ 3 < p
->buf_size
){
44 if (AV_RL16(&p
->buf
[2 * i
++]) != SYNC_WORD
)
46 j
= AV_RL16(&p
->buf
[2 * i
++]);
47 if (j
!= 0 && j
!= 0x10 && j
!= 0x40 && j
!= 0x50 && j
!= 0x76)
54 return AVPROBE_SCORE_MAX
;
56 return AVPROBE_SCORE_EXTENSION
- 1;
60 static int read_header(AVFormatContext
*s
)
64 st
=avformat_new_stream(s
, NULL
);
66 return AVERROR(ENOMEM
);
68 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
69 st
->codecpar
->codec_id
=AV_CODEC_ID_G729
;
70 st
->codecpar
->sample_rate
=8000;
71 st
->codecpar
->block_align
= 16;
72 st
->codecpar
->ch_layout
.nb_channels
= 1;
74 avpriv_set_pts_info(st
, 64, 1, 100);
78 static int read_packet(AVFormatContext
*s
,
81 AVIOContext
*pb
= s
->pb
;
83 uint16_t buf
[8 * MAX_FRAME_SIZE
+ 2];
87 int64_t pos
= avio_tell(pb
);
92 avio_rl16(pb
); // sync word
93 packet_size
= avio_rl16(pb
) / 8;
94 if(packet_size
> MAX_FRAME_SIZE
)
95 return AVERROR_INVALIDDATA
;
97 ret
= ffio_read_size(pb
, (uint8_t*)buf
, (8 * packet_size
) * sizeof(uint16_t));
101 if ((ret
= av_new_packet(pkt
, packet_size
)) < 0)
104 init_put_bits(&pbo
, pkt
->data
, packet_size
);
105 for(j
=0; j
< packet_size
; j
++)
107 put_bits(&pbo
,1, AV_RL16(src
++) == BIT_1
? 1 : 0);
109 flush_put_bits(&pbo
);
116 const FFInputFormat ff_bit_demuxer
= {
118 .p
.long_name
= NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
119 .p
.extensions
= "bit",
121 .read_header
= read_header
,
122 .read_packet
= read_packet
,
127 static av_cold
int init(AVFormatContext
*s
)
129 AVCodecParameters
*par
= s
->streams
[0]->codecpar
;
131 if (par
->ch_layout
.nb_channels
!= 1) {
132 av_log(s
, AV_LOG_ERROR
,
133 "only codec g729 with 1 channel is supported by this format\n");
134 return AVERROR(EINVAL
);
137 par
->bits_per_coded_sample
= 16;
138 par
->block_align
= (par
->bits_per_coded_sample
* par
->ch_layout
.nb_channels
) >> 3;
143 static int write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
145 AVIOContext
*pb
= s
->pb
;
150 return AVERROR(EINVAL
);
152 avio_wl16(pb
, SYNC_WORD
);
153 avio_wl16(pb
, 8 * pkt
->size
);
155 init_get_bits(&gb
, pkt
->data
, 8 * pkt
->size
);
156 for (i
= 0; i
< 8 * pkt
->size
; i
++)
157 avio_wl16(pb
, get_bits1(&gb
) ? BIT_1
: BIT_0
);
162 const FFOutputFormat ff_bit_muxer
= {
164 .p
.long_name
= NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
165 .p
.mime_type
= "audio/bit",
166 .p
.extensions
= "bit",
167 .p
.audio_codec
= AV_CODEC_ID_G729
,
168 .p
.video_codec
= AV_CODEC_ID_NONE
,
169 .p
.subtitle_codec
= AV_CODEC_ID_NONE
,
170 .flags_internal
= FF_OFMT_FLAG_MAX_ONE_OF_EACH
|
171 FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
,
173 .write_packet
= write_packet
,