FFmpeg
Loading...
Searching...
No Matches
ilbc.c
Go to the documentation of this file.
1/*
2 * iLBC storage file format
3 * Copyright (c) 2012 Martin Storsjo
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 "rawenc.h"
30
31static const char mode20_header[] = "#!iLBC20\n";
32static const char mode30_header[] = "#!iLBC30\n";
33
35{
36 AVIOContext *pb = s->pb;
37 AVCodecParameters *par = s->streams[0]->codecpar;
38
39 if (par->block_align == 50) {
40 avio_write(pb, mode30_header, sizeof(mode30_header) - 1);
41 } else if (par->block_align == 38) {
42 avio_write(pb, mode20_header, sizeof(mode20_header) - 1);
43 } else {
44 av_log(s, AV_LOG_ERROR, "Unsupported mode\n");
45 return AVERROR(EINVAL);
46 }
47 return 0;
48}
49
50static int ilbc_probe(const AVProbeData *p)
51{
52 // Only check for "#!iLBC" which matches both formats
53 if (!memcmp(p->buf, mode20_header, 6))
54 return AVPROBE_SCORE_MAX;
55 else
56 return 0;
57}
58
60{
61 AVIOContext *pb = s->pb;
62 AVStream *st;
63 uint8_t header[9];
64 int ret;
65
66 ret = ffio_read_size(pb, header, 9);
67 if (ret < 0)
68 return ret;
69
71 if (!st)
72 return AVERROR(ENOMEM);
74 st->codecpar->sample_rate = 8000;
77 st->start_time = 0;
79 if (!memcmp(header, mode20_header, sizeof(mode20_header) - 1)) {
80 st->codecpar->block_align = 38;
81 st->codecpar->bit_rate = 15200;
82 } else if (!memcmp(header, mode30_header, sizeof(mode30_header) - 1)) {
83 st->codecpar->block_align = 50;
84 st->codecpar->bit_rate = 13333;
85 } else {
86 av_log(s, AV_LOG_ERROR, "Unrecognized iLBC file header\n");
88 }
89
90 return 0;
91}
92
95{
96 AVCodecParameters *par = s->streams[0]->codecpar;
97 int ret;
98
99 if ((ret = av_get_packet(s->pb, pkt, par->block_align)) != par->block_align)
100 return ret < 0 ? ret : AVERROR_INVALIDDATA;
101
102 pkt->stream_index = 0;
103 pkt->duration = par->block_align == 38 ? 160 : 240;
104
105 return 0;
106}
107
109 .p.name = "ilbc",
110 .p.long_name = NULL_IF_CONFIG_SMALL("iLBC storage"),
111 .p.flags = AVFMT_GENERIC_INDEX,
112 .read_probe = ilbc_probe,
113 .read_header = ilbc_read_header,
114 .read_packet = ilbc_read_packet,
115};
116
117#if CONFIG_ILBC_MUXER
119 .p.name = "ilbc",
120 .p.long_name = NULL_IF_CONFIG_SMALL("iLBC storage"),
121 .p.mime_type = "audio/iLBC",
122 .p.extensions = "lbc",
123 .p.video_codec = AV_CODEC_ID_NONE,
124 .p.audio_codec = AV_CODEC_ID_ILBC,
125 .p.subtitle_codec = AV_CODEC_ID_NONE,
126 .p.flags = AVFMT_NOTIMESTAMPS,
127 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
129 .write_header = ilbc_write_header,
130 .write_packet = ff_raw_write_packet,
131};
132#endif
const FFOutputFormat ff_ilbc_muxer
const FFInputFormat ff_ilbc_demuxer
Definition ilbc.c:108
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
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition avformat.h:498
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
static AVPacket * pkt
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_ILBC
Definition codec_id.h:512
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#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
static int ilbc_write_header(AVFormatContext *s)
Definition ilbc.c:34
static const char mode20_header[]
Definition ilbc.c:31
static int ilbc_read_header(AVFormatContext *s)
Definition ilbc.c:59
static int ilbc_probe(const AVProbeData *p)
Definition ilbc.c:50
static const char mode30_header[]
Definition ilbc.c:32
static int ilbc_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition ilbc.c:93
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition rawenc.c:31
#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
static const uint8_t header[24]
Definition sdr2.c:68
An AVChannelLayout holds information about the channel layout of audio data.
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
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
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
#define av_log(a,...)