FFmpeg
Loading...
Searching...
No Matches
bit.c
Go to the documentation of this file.
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
22#include "config_components.h"
23
24#include "avformat.h"
25#include "avio_internal.h"
26#include "demux.h"
27#include "internal.h"
28#include "mux.h"
29#include "libavcodec/get_bits.h"
30#include "libavcodec/put_bits.h"
31
32#define MAX_FRAME_SIZE 10
33
34#define SYNC_WORD 0x6b21
35#define BIT_0 0x7f
36#define BIT_1 0x81
37
38#if CONFIG_BIT_DEMUXER
39static int probe(const AVProbeData *p)
40{
41 int i = 0, j, valid = 0;
42
43 while (2 * i + 3 < p->buf_size){
44 if (AV_RL16(&p->buf[2 * i++]) != SYNC_WORD)
45 return 0;
46 j = AV_RL16(&p->buf[2 * i++]);
47 if (j != 0 && j != 0x10 && j != 0x40 && j != 0x50 && j != 0x76)
48 return 0;
49 if (j)
50 valid++;
51 i += j;
52 }
53 if (valid > 10)
54 return AVPROBE_SCORE_MAX;
55 if (valid > 2)
56 return AVPROBE_SCORE_EXTENSION - 1;
57 return 0;
58}
59
60static int read_header(AVFormatContext *s)
61{
62 AVStream* st;
63
65 if (!st)
66 return AVERROR(ENOMEM);
67
70 st->codecpar->sample_rate=8000;
71 st->codecpar->block_align = 16;
73
74 avpriv_set_pts_info(st, 64, 1, 100);
75 return 0;
76}
77
78static int read_packet(AVFormatContext *s,
80{
81 AVIOContext *pb = s->pb;
82 PutBitContext pbo;
83 uint16_t buf[8 * MAX_FRAME_SIZE + 2];
84 int packet_size;
85 uint16_t* src=buf;
86 int i, j, ret;
88
89 if(avio_feof(pb))
90 return AVERROR_EOF;
91
92 avio_rl16(pb); // sync word
93 packet_size = avio_rl16(pb) / 8;
94 if(packet_size > MAX_FRAME_SIZE)
96
97 ret = ffio_read_size(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
98 if(ret<0)
99 return ret;
100
101 if ((ret = av_new_packet(pkt, packet_size)) < 0)
102 return ret;
103
104 init_put_bits(&pbo, pkt->data, packet_size);
105 for(j=0; j < packet_size; j++)
106 for(i=0; i<8;i++)
107 put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
108
109 flush_put_bits(&pbo);
110
111 pkt->duration=1;
112 pkt->pos = pos;
113 return 0;
114}
115
117 .p.name = "bit",
118 .p.long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
119 .p.extensions = "bit",
120 .read_probe = probe,
121 .read_header = read_header,
122 .read_packet = read_packet,
123};
124#endif
125
126#if CONFIG_BIT_MUXER
127static av_cold int init(AVFormatContext *s)
128{
129 AVCodecParameters *par = s->streams[0]->codecpar;
130
131 if (par->ch_layout.nb_channels != 1) {
133 "only codec g729 with 1 channel is supported by this format\n");
134 return AVERROR(EINVAL);
135 }
136
137 par->bits_per_coded_sample = 16;
138 par->block_align = (par->bits_per_coded_sample * par->ch_layout.nb_channels) >> 3;
139
140 return 0;
141}
142
144{
145 AVIOContext *pb = s->pb;
146 GetBitContext gb;
147 int i;
148
149 if (pkt->size != 10)
150 return AVERROR(EINVAL);
151
152 avio_wl16(pb, SYNC_WORD);
153 avio_wl16(pb, 8 * pkt->size);
154
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);
158
159 return 0;
160}
161
163 .p.name = "bit",
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 |
172 .init = init,
173 .write_packet = write_packet,
174};
175#endif
#define MAX_FRAME_SIZE
Definition 8svx.c:64
static int probe(const AVProbeData *p)
Definition act.c:39
const FFInputFormat ff_bit_demuxer
const FFOutputFormat ff_bit_muxer
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
void avio_wl16(AVIOContext *s, unsigned int val)
Definition aviobuf.c:440
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define SYNC_WORD
Definition bit.c:34
#define BIT_0
Definition bit.c:35
#define BIT_1
Definition bit.c:36
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition ffmpeg_mux.c:204
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
bitstream reader API header.
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
@ AV_CODEC_ID_G729
Definition codec_id.h:506
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define AV_RL16(p)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition mux.h:50
#define FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
If this flag is set, then the only permitted audio/video/subtitle codec ids are AVOutputFormat....
Definition mux.h:59
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
unsigned int pos
Definition spdifenc.c:431
int nb_channels
Number of channels in this layout.
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
#define av_log(a,...)
#define src
Definition vp8dsp.c:248