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