]> git.sesse.net Git - ffmpeg/blob - libavformat/bit.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / bit.c
1 /*
2  * G.729 bit format muxer and demuxer
3  * Copyright (c) 2007-2008 Vladimir Voroshilov
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 #include "avformat.h"
22 #include "libavcodec/get_bits.h"
23 #include "libavcodec/put_bits.h"
24
25 #define MAX_FRAME_SIZE 10
26
27 #define SYNC_WORD  0x6b21
28 #define BIT_0      0x7f
29 #define BIT_1      0x81
30
31 static int probe(AVProbeData *p)
32 {
33     int i, j;
34
35     if(p->buf_size < 0x40)
36         return 0;
37
38     for(i=0; i+3<p->buf_size && i< 10*0x50; ){
39         if(AV_RL16(&p->buf[0]) != SYNC_WORD)
40             return 0;
41         j=AV_RL16(&p->buf[2]);
42         if(j!=0x40 && j!=0x50)
43             return 0;
44         i+=j;
45     }
46     return AVPROBE_SCORE_MAX/2;
47 }
48
49 static int read_header(AVFormatContext *s, AVFormatParameters *ap)
50 {
51     AVStream* st;
52
53     st=avformat_new_stream(s, NULL);
54     if (!st)
55         return AVERROR(ENOMEM);
56
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;
62
63     av_set_pts_info(st, 64, 1, 100);
64     return 0;
65 }
66
67 static int read_packet(AVFormatContext *s,
68                           AVPacket *pkt)
69 {
70     AVIOContext *pb = s->pb;
71     PutBitContext pbo;
72     uint16_t buf[8 * MAX_FRAME_SIZE + 2];
73     int packet_size;
74     uint16_t* src=buf;
75     int i, j, ret;
76     int64_t pos= avio_tell(pb);
77
78     if(url_feof(pb))
79         return AVERROR_EOF;
80
81     avio_rl16(pb); // sync word
82     packet_size = avio_rl16(pb) / 8;
83     if(packet_size > MAX_FRAME_SIZE)
84         return AVERROR_INVALIDDATA;
85
86     ret = avio_read(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
87     if(ret<0)
88         return ret;
89     if(ret != 8 * packet_size * sizeof(uint16_t))
90         return AVERROR(EIO);
91
92     av_new_packet(pkt, packet_size);
93
94     init_put_bits(&pbo, pkt->data, packet_size);
95     for(j=0; j < packet_size; j++)
96         for(i=0; i<8;i++)
97             put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
98
99     flush_put_bits(&pbo);
100
101     pkt->duration=1;
102     pkt->pos = pos;
103     return 0;
104 }
105
106 AVInputFormat ff_bit_demuxer = {
107     .name        = "bit",
108     .long_name   = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
109     .read_probe  = probe,
110     .read_header = read_header,
111     .read_packet = read_packet,
112     .extensions  = "bit",
113 };
114
115 #ifdef CONFIG_MUXERS
116 static int write_header(AVFormatContext *s)
117 {
118     AVCodecContext *enc = s->streams[0]->codec;
119
120     enc->codec_id = CODEC_ID_G729;
121     enc->channels = 1;
122     enc->bits_per_coded_sample = 16;
123     enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
124
125     return 0;
126 }
127
128 static int write_packet(AVFormatContext *s, AVPacket *pkt)
129 {
130     ByteIOContext *pb = s->pb;
131     GetBitContext gb;
132     int i;
133
134     avio_wl16(pb, SYNC_WORD);
135     avio_wl16(pb, 8 * 10);
136
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);
140     avio_flush(pb);
141
142     return 0;
143 }
144
145 AVOutputFormat ff_bit_muxer = {
146     .name         = "bit",
147     .long_name    = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
148     .mime_type    = "audio/bit",
149     .extensions   = "bit",
150     .audio_codec  = CODEC_ID_G729,
151     .video_codec  = CODEC_ID_NONE,
152     .write_header = write_header,
153     .write_packet = write_packet,
154 };
155 #endif