]> git.sesse.net Git - ffmpeg/blob - libavformat/bit.c
bit: loose seekback & wrong bitrate code.
[ffmpeg] / libavformat / bit.c
1 #include "avformat.h"
2 #include "libavcodec/get_bits.h"
3 #include "libavcodec/put_bits.h"
4
5 #define MAX_FRAME_SIZE 10
6
7 #define SYNC_WORD  0x6b21
8 #define BIT_0      0x7f
9 #define BIT_1      0x81
10
11 static int probe(AVProbeData *p)
12 {
13     int i, j;
14
15     for(i=0; i+3<p->buf_size && i< 10*0x50; ){
16         if(AV_RL16(&p->buf[0]) != SYNC_WORD)
17             return 0;
18         j=AV_RL16(&p->buf[2]);
19         if(j!=0x40 && j!=0x50)
20             return 0;
21         i+=j;
22     }
23     return AVPROBE_SCORE_MAX/2;
24 }
25
26 static int read_header(AVFormatContext *s, AVFormatParameters *ap)
27 {
28     AVStream* st;
29     ByteIOContext *pb = s->pb;
30
31     st=av_new_stream(s, 0);
32     if (!st)
33         return AVERROR(ENOMEM);
34
35     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
36     st->codec->codec_id=CODEC_ID_G729;
37     st->codec->sample_rate=8000;
38     st->codec->block_align = 16;
39     st->codec->channels=1;
40
41     av_set_pts_info(st, 64, 1, 100);
42     return 0;
43 }
44
45 static int read_packet(AVFormatContext *s,
46                           AVPacket *pkt)
47 {
48     ByteIOContext *pb = s->pb;
49     PutBitContext pbo;
50     uint16_t buf[8 * MAX_FRAME_SIZE + 2];
51     int packet_size;
52     int sync;
53     uint16_t* src=buf;
54     int i, j, ret;
55
56     if(url_feof(pb))
57         return AVERROR_EOF;
58
59     sync = get_le16(pb); // sync word
60     packet_size = get_le16(pb) / 8;
61     if(packet_size > MAX_FRAME_SIZE)
62         return AVERROR(EIO);
63
64     ret = get_buffer(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
65     if(ret<0)
66         return ret;
67     if(ret != 8 * packet_size * sizeof(uint16_t))
68         return AVERROR(EIO);
69
70     av_new_packet(pkt, packet_size);
71
72     init_put_bits(&pbo, pkt->data, packet_size);
73     for(j=0; j < packet_size; j++)
74         for(i=0; i<8;i++)
75             put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
76
77     flush_put_bits(&pbo);
78
79     pkt->duration=1;
80     return 0;
81 }
82
83 AVInputFormat ff_bit_demuxer = {
84     "bit",
85     "G.729 BIT file format",
86     0,
87     probe,
88     read_header,
89     read_packet,
90     .extensions = "bit"
91 };
92
93 #ifdef CONFIG_MUXERS
94 static int write_header(AVFormatContext *s)
95 {
96     AVCodecContext *enc = s->streams[0]->codec;
97
98     enc->codec_id = CODEC_ID_G729;
99     enc->channels = 1;
100     enc->bits_per_coded_sample = 16;
101     enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
102
103     return 0;
104 }
105
106 static int write_packet(AVFormatContext *s, AVPacket *pkt)
107 {
108     ByteIOContext *pb = s->pb;
109     GetBitContext gb;
110     int i;
111     uint16_t b;
112
113     put_le16(pb, SYNC_WORD);
114     put_le16(pb, 8 * 10);
115
116     init_get_bits(&gb, pkt->data, 8*10);
117     for(i=0; i< 8 * 10; i++)
118         put_le16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
119     put_flush_packet(pb);
120
121     return 0;
122 }
123
124 AVOutputFormat ff_bit_muxer = {
125     "bit",
126     "G.729 BIT file format",
127     "audio/bit",
128     "bit",
129     0,
130     CODEC_ID_G729,
131     CODEC_ID_NONE,
132     write_header,
133     write_packet,
134     .extensions = "bit"
135 };
136 #endif