FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 #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 #if CONFIG_BIT_DEMUXER
33 static int probe(AVProbeData *p)
34 {
35  int i = 0, j, valid = 0;
36 
37  while (2 * i + 3 < p->buf_size){
38  if (AV_RL16(&p->buf[2 * i++]) != SYNC_WORD)
39  return 0;
40  j = AV_RL16(&p->buf[2 * i++]);
41  if (j != 0 && j != 0x10 && j != 0x40 && j != 0x50 && j != 0x76)
42  return 0;
43  if (j)
44  valid++;
45  i += j;
46  }
47  if (valid > 10)
48  return AVPROBE_SCORE_MAX;
49  if (valid > 2)
50  return AVPROBE_SCORE_EXTENSION - 1;
51  return 0;
52 }
53 
54 static int read_header(AVFormatContext *s)
55 {
56  AVStream* st;
57 
59  if (!st)
60  return AVERROR(ENOMEM);
61 
64  st->codecpar->sample_rate=8000;
65  st->codecpar->block_align = 16;
66  st->codecpar->channels=1;
67 
68  avpriv_set_pts_info(st, 64, 1, 100);
69  return 0;
70 }
71 
72 static int read_packet(AVFormatContext *s,
73  AVPacket *pkt)
74 {
75  AVIOContext *pb = s->pb;
76  PutBitContext pbo;
77  uint16_t buf[8 * MAX_FRAME_SIZE + 2];
78  int packet_size;
79  uint16_t* src=buf;
80  int i, j, ret;
81  int64_t pos= avio_tell(pb);
82 
83  if(avio_feof(pb))
84  return AVERROR_EOF;
85 
86  avio_rl16(pb); // sync word
87  packet_size = avio_rl16(pb) / 8;
88  if(packet_size > MAX_FRAME_SIZE)
89  return AVERROR_INVALIDDATA;
90 
91  ret = avio_read(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
92  if(ret<0)
93  return ret;
94  if(ret != 8 * packet_size * sizeof(uint16_t))
95  return AVERROR(EIO);
96 
97  if (av_new_packet(pkt, packet_size) < 0)
98  return AVERROR(ENOMEM);
99 
100  init_put_bits(&pbo, pkt->data, packet_size);
101  for(j=0; j < packet_size; j++)
102  for(i=0; i<8;i++)
103  put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
104 
105  flush_put_bits(&pbo);
106 
107  pkt->duration=1;
108  pkt->pos = pos;
109  return 0;
110 }
111 
112 AVInputFormat ff_bit_demuxer = {
113  .name = "bit",
114  .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
115  .read_probe = probe,
116  .read_header = read_header,
117  .read_packet = read_packet,
118  .extensions = "bit",
119 };
120 #endif
121 
122 #if CONFIG_BIT_MUXER
123 static int write_header(AVFormatContext *s)
124 {
125  AVCodecParameters *par = s->streams[0]->codecpar;
126 
127  if ((par->codec_id != AV_CODEC_ID_G729) || par->channels != 1) {
128  av_log(s, AV_LOG_ERROR,
129  "only codec g729 with 1 channel is supported by this format\n");
130  return AVERROR(EINVAL);
131  }
132 
133  par->bits_per_coded_sample = 16;
134  par->block_align = (par->bits_per_coded_sample * par->channels) >> 3;
135 
136  return 0;
137 }
138 
139 static int write_packet(AVFormatContext *s, AVPacket *pkt)
140 {
141  AVIOContext *pb = s->pb;
142  GetBitContext gb;
143  int i;
144 
145  if (pkt->size != 10)
146  return AVERROR(EINVAL);
147 
148  avio_wl16(pb, SYNC_WORD);
149  avio_wl16(pb, 8 * pkt->size);
150 
151  init_get_bits(&gb, pkt->data, 8 * pkt->size);
152  for (i = 0; i < 8 * pkt->size; i++)
153  avio_wl16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
154 
155  return 0;
156 }
157 
158 AVOutputFormat ff_bit_muxer = {
159  .name = "bit",
160  .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
161  .mime_type = "audio/bit",
162  .extensions = "bit",
163  .audio_codec = AV_CODEC_ID_G729,
164  .video_codec = AV_CODEC_ID_NONE,
165  .write_header = write_header,
166  .write_packet = write_packet,
167 };
168 #endif
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:671
#define NULL
Definition: coverity.c:32
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:474
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:206
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1699
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4737
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4152
int size
Definition: avcodec.h:1680
static AVPacket pkt
#define src
Definition: vp8dsp.c:254
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
This struct describes the properties of an encoded stream.
Definition: avcodec.h:4144
Format I/O context.
Definition: avformat.h:1349
uint8_t
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1697
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4367
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1417
#define BIT_1
Definition: bit.c:30
uint8_t * data
Definition: avcodec.h:1679
#define AVERROR_EOF
End of file.
Definition: error.h:55
bitstream reader API header.
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:556
static int probe(AVProbeData *p)
Definition: act.c:36
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:637
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4148
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:464
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
int block_align
Audio only.
Definition: avcodec.h:4269
#define BIT_0
Definition: bit.c:29
const char * name
Definition: avformat.h:524
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:528
Stream structure.
Definition: avformat.h:889
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
void * buf
Definition: avisynth_c.h:690
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:313
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:425
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:471
This structure contains the data a format has to probe a file.
Definition: avformat.h:461
int sample_rate
Audio only.
Definition: avcodec.h:4262
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:473
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:743
Main libavformat public API header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
#define MAX_FRAME_SIZE
Definition: bit.c:26
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
#define SYNC_WORD
Definition: bit.c:28
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:4194
int channels
Audio only.
Definition: avcodec.h:4258
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
AVCodecParameters * codecpar
Definition: avformat.h:1252
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:356
This structure stores compressed data.
Definition: avcodec.h:1656
bitstream writer API