FFmpeg
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 "demux.h"
26 #include "internal.h"
27 #include "mux.h"
28 #include "libavcodec/get_bits.h"
29 #include "libavcodec/put_bits.h"
30 
31 #define MAX_FRAME_SIZE 10
32 
33 #define SYNC_WORD 0x6b21
34 #define BIT_0 0x7f
35 #define BIT_1 0x81
36 
37 #if CONFIG_BIT_DEMUXER
38 static int probe(const AVProbeData *p)
39 {
40  int i = 0, j, valid = 0;
41 
42  while (2 * i + 3 < p->buf_size){
43  if (AV_RL16(&p->buf[2 * i++]) != SYNC_WORD)
44  return 0;
45  j = AV_RL16(&p->buf[2 * i++]);
46  if (j != 0 && j != 0x10 && j != 0x40 && j != 0x50 && j != 0x76)
47  return 0;
48  if (j)
49  valid++;
50  i += j;
51  }
52  if (valid > 10)
53  return AVPROBE_SCORE_MAX;
54  if (valid > 2)
55  return AVPROBE_SCORE_EXTENSION - 1;
56  return 0;
57 }
58 
59 static int read_header(AVFormatContext *s)
60 {
61  AVStream* st;
62 
64  if (!st)
65  return AVERROR(ENOMEM);
66 
69  st->codecpar->sample_rate=8000;
70  st->codecpar->block_align = 16;
72 
73  avpriv_set_pts_info(st, 64, 1, 100);
74  return 0;
75 }
76 
77 static int read_packet(AVFormatContext *s,
78  AVPacket *pkt)
79 {
80  AVIOContext *pb = s->pb;
81  PutBitContext pbo;
82  uint16_t buf[8 * MAX_FRAME_SIZE + 2];
83  int packet_size;
84  uint16_t* src=buf;
85  int i, j, ret;
86  int64_t pos= avio_tell(pb);
87 
88  if(avio_feof(pb))
89  return AVERROR_EOF;
90 
91  avio_rl16(pb); // sync word
92  packet_size = avio_rl16(pb) / 8;
93  if(packet_size > MAX_FRAME_SIZE)
94  return AVERROR_INVALIDDATA;
95 
96  ret = avio_read(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
97  if(ret<0)
98  return ret;
99  if(ret != 8 * packet_size * sizeof(uint16_t))
100  return AVERROR(EIO);
101 
102  if ((ret = av_new_packet(pkt, packet_size)) < 0)
103  return ret;
104 
105  init_put_bits(&pbo, pkt->data, packet_size);
106  for(j=0; j < packet_size; j++)
107  for(i=0; i<8;i++)
108  put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
109 
110  flush_put_bits(&pbo);
111 
112  pkt->duration=1;
113  pkt->pos = pos;
114  return 0;
115 }
116 
118  .p.name = "bit",
119  .p.long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
120  .p.extensions = "bit",
121  .read_probe = probe,
122  .read_header = read_header,
123  .read_packet = read_packet,
124 };
125 #endif
126 
127 #if CONFIG_BIT_MUXER
128 static av_cold int init(AVFormatContext *s)
129 {
130  AVCodecParameters *par = s->streams[0]->codecpar;
131 
132  if (par->ch_layout.nb_channels != 1) {
134  "only codec g729 with 1 channel is supported by this format\n");
135  return AVERROR(EINVAL);
136  }
137 
138  par->bits_per_coded_sample = 16;
139  par->block_align = (par->bits_per_coded_sample * par->ch_layout.nb_channels) >> 3;
140 
141  return 0;
142 }
143 
145 {
146  AVIOContext *pb = s->pb;
147  GetBitContext gb;
148  int i;
149 
150  if (pkt->size != 10)
151  return AVERROR(EINVAL);
152 
153  avio_wl16(pb, SYNC_WORD);
154  avio_wl16(pb, 8 * pkt->size);
155 
156  init_get_bits(&gb, pkt->data, 8 * pkt->size);
157  for (i = 0; i < 8 * pkt->size; i++)
158  avio_wl16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
159 
160  return 0;
161 }
162 
164  .p.name = "bit",
165  .p.long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
166  .p.mime_type = "audio/bit",
167  .p.extensions = "bit",
168  .p.audio_codec = AV_CODEC_ID_G729,
169  .p.video_codec = AV_CODEC_ID_NONE,
170  .p.subtitle_codec = AV_CODEC_ID_NONE,
171  .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
173  .init = init,
174  .write_packet = write_packet,
175 };
176 #endif
AVOutputFormat::name
const char * name
Definition: avformat.h:510
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
int64_t
long long int64_t
Definition: coverity.c:34
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:62
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:223
AVPacket::data
uint8_t * data
Definition: packet.h:539
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:557
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
#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
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:327
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:514
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:437
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
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:867
BIT_1
#define BIT_1
Definition: bit.c:35
GetBitContext
Definition: get_bits.h:108
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:714
MAX_FRAME_SIZE
#define MAX_FRAME_SIZE
Definition: bit.c:31
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
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
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
get_bits.h
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
PutBitContext
Definition: put_bits.h:50
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:540
NULL
#define NULL
Definition: coverity.c:32
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
FFOutputFormat
Definition: mux.h:61
SYNC_WORD
#define SYNC_WORD
Definition: bit.c:33
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:461
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AVPacket::size
int size
Definition: packet.h:540
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
ff_bit_demuxer
const FFInputFormat ff_bit_demuxer
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:191
FF_OFMT_FLAG_MAX_ONE_OF_EACH
#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
demux.h
ff_bit_muxer
const FFOutputFormat ff_bit_muxer
write_packet
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition: ffmpeg_mux.c:209
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
probe
static int probe(const AVProbeData *p)
Definition: act.c:39
AV_CODEC_ID_G729
@ AV_CODEC_ID_G729
Definition: codec_id.h:499
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
BIT_0
#define BIT_0
Definition: bit.c:34
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:143
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:559
FFInputFormat
Definition: demux.h:42
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
put_bits.h
src
#define src
Definition: vp8dsp.c:248
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346
mux.h