FFmpeg: libavformat/bit.c Source File

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 #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_EXTENSION;
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=AV_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(avio_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  if (av_new_packet(pkt, packet_size) < 0)
94  return AVERROR(ENOMEM);
95 
96  init_put_bits(&pbo, pkt->data, packet_size);
97  for(j=0; j < packet_size; j++)
98  for(i=0; i<8;i++)
99  put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
100 
101  flush_put_bits(&pbo);
102 
103  pkt->duration=1;
104  pkt->pos = pos;
105  return 0;
106 }
107 
108  AVInputFormat ff_bit_demuxer = {
109  .name = "bit",
110  .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
111  .read_probe = probe,
112  .read_header = read_header,
113  .read_packet = read_packet,
114  .extensions = "bit",
115 };
116 
117 #if CONFIG_MUXERS
118 static int write_header(AVFormatContext *s)
119 {
120  AVCodecContext *enc = s->streams[0]->codec;
121 
122  if ((enc->codec_id != AV_CODEC_ID_G729) || enc->channels != 1) {
123  av_log(s, AV_LOG_ERROR,
124  "only codec g729 with 1 channel is supported by this format\n");
125  return AVERROR(EINVAL);
126  }
127 
128  enc->bits_per_coded_sample = 16;
129  enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
130 
131  return 0;
132 }
133 
134 static int write_packet(AVFormatContext *s, AVPacket *pkt)
135 {
136  AVIOContext *pb = s->pb;
137  GetBitContext gb;
138  int i;
139 
140  if (pkt->size != 10)
141  return AVERROR(EINVAL);
142 
143  avio_wl16(pb, SYNC_WORD);
144  avio_wl16(pb, 8 * 10);
145 
146  init_get_bits(&gb, pkt->data, 8*10);
147  for(i=0; i< 8 * 10; i++)
148  avio_wl16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
149 
150  return 0;
151 }
152 
153 AVOutputFormat ff_bit_muxer = {
154  .name = "bit",
155  .long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
156  .mime_type = "audio/bit",
157  .extensions = "bit",
158  .audio_codec = AV_CODEC_ID_G729,
159  .video_codec = AV_CODEC_ID_NONE,
160  .write_header = write_header,
161  .write_packet = write_packet,
162 };
163 #endif
NULL
#define NULL
Definition: coverity.c:32
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:424
s
const char * s
Definition: avisynth_c.h:631
AVIOContext
Bytestream IO Context.
Definition: avio.h:111
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:168
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1487
avpriv_set_pts_info
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:4149
AVPacket::size
int size
Definition: avcodec.h:1468
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
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:87
AVCodecContext::block_align
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2324
AVFormatContext
Format I/O context.
Definition: avformat.h:1314
uint8_t
uint8_t
Definition: audio_convert.c:194
ff_bit_demuxer
AVInputFormat ff_bit_demuxer
Definition: bit.c:108
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1485
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3805
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1382
BIT_1
#define BIT_1
Definition: bit.c:30
AVPacket::data
uint8_t * data
Definition: avcodec.h:1467
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
get_bits.h
bitstream reader API header.
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:442
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2917
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
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: avpacket.c:86
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVERROR
#define AVERROR(e)
Definition: error.h:43
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:176
probe
static int probe(AVProbeData *p)
Definition: bit.c:32
AVStream::codec
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:896
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:463
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:462
BIT_0
#define BIT_0
Definition: bit.c:29
AVOutputFormat::name
const char * name
Definition: avformat.h:523
src
#define src
Definition: vp9dsp.c:530
AVStream
Stream structure.
Definition: avformat.h:877
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:1540
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:1549
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:2287
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1356
AVCodecContext
main external API structure.
Definition: avcodec.h:1532
buf
void * buf
Definition: avisynth_c.h:553
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:312
read_header
static int read_header(AVFormatContext *s)
Definition: bit.c:50
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:418
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:470
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:460
read_packet
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: bit.c:68
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:651
avformat.h
Main libavformat public API header.
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
MAX_FRAME_SIZE
#define MAX_FRAME_SIZE
Definition: bit.c:26
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
SYNC_WORD
#define SYNC_WORD
Definition: bit.c:28
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:2288
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:497
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:661
avio_feof
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:306
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1444
write_packet
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
put_bits.h
bitstream writer API

Generated on Mon Feb 15 2016 15:20:45 for FFmpeg by   doxygen 1.8.6

AltStyle によって変換されたページ (->オリジナル) /