FFmpeg
codec2.c
Go to the documentation of this file.
1 /*
2  * codec2 muxer and demuxers
3  * Copyright (c) 2017 Tomas Härdin
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 "libavcodec/codec2utils.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/opt.h"
28 #include "avio_internal.h"
29 #include "avformat.h"
30 #include "internal.h"
31 #include "mux.h"
32 #include "rawenc.h"
33 #include "pcm.h"
34 
35 #define CODEC2_HEADER_SIZE 7
36 #define CODEC2_MAGIC 0xC0DEC2
37 
38 //the lowest version we should ever run across is 0.8
39 //we may run across later versions as the format evolves
40 #define EXPECTED_CODEC2_MAJOR_VERSION 0
41 #define EXPECTED_CODEC2_MINOR_VERSION 8
42 
43 typedef struct {
44  const AVClass *class;
45  int mode;
48 
49 static int codec2_probe(const AVProbeData *p)
50 {
51  //must start wih C0 DE C2
52  if (AV_RB24(p->buf) != CODEC2_MAGIC) {
53  return 0;
54  }
55 
56  //no .c2 files prior to 0.8
57  //be strict about major version while we're at it
58  if (p->buf[3] != EXPECTED_CODEC2_MAJOR_VERSION ||
60  return 0;
61  }
62 
63  //32 bits of identification -> low score
64  return AVPROBE_SCORE_EXTENSION + 1;
65 }
66 
67 //Mimics codec2_samples_per_frame()
69 {
70  int frame_size_table[CODEC2_MODE_MAX+1] = {
71  160, // 3200
72  160, // 2400
73  320, // 1600
74  320, // 1400
75  320, // 1300
76  320, // 1200
77  320, // 700
78  320, // 700B
79  320, // 700C
80  };
81 
83  av_log(s, AV_LOG_ERROR, "unknown codec2 mode %i, can't find frame_size\n", mode);
84  return 0;
85  } else {
86  return frame_size_table[mode];
87  }
88 }
89 
90 //Mimics (codec2_bits_per_frame()+7)/8
92 {
93  int block_align_table[CODEC2_MODE_MAX+1] = {
94  8, // 3200
95  6, // 2400
96  8, // 1600
97  7, // 1400
98  7, // 1300
99  6, // 1200
100  4, // 700
101  4, // 700B
102  4, // 700C
103  };
104 
106  av_log(s, AV_LOG_ERROR, "unknown codec2 mode %i, can't find block_align\n", mode);
107  return 0;
108  } else {
109  return block_align_table[mode];
110  }
111 }
112 
113 //Computes bitrate from mode, with frames rounded up to the nearest octet.
114 //So 700 bit/s (28 bits/frame) becomes 800 bits/s (32 bits/frame).
116 {
118  int block_align = codec2_mode_block_align(s, mode);
119 
120  if (frame_size <= 0 || block_align <= 0) {
121  return 0;
122  }
123 
124  return 8 * 8000 * block_align / frame_size;
125 }
126 
128 {
130 
133  st->codecpar->sample_rate = 8000;
139 
140  if (st->codecpar->bit_rate <= 0 ||
141  st->codecpar->frame_size <= 0 ||
142  st->codecpar->block_align <= 0) {
143  return AVERROR_INVALIDDATA;
144  }
145 
146  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
147 
148  return 0;
149 }
150 
152 {
154  int ret, version;
155 
156  if (!st) {
157  return AVERROR(ENOMEM);
158  }
159 
160  if (avio_rb24(s->pb) != CODEC2_MAGIC) {
161  av_log(s, AV_LOG_ERROR, "not a .c2 file\n");
162  return AVERROR_INVALIDDATA;
163  }
164 
166  if (ret) {
167  return ret;
168  }
169 
171  if (ret < 0) {
172  return ret;
173  }
174 
176  if ((version >> 8) != EXPECTED_CODEC2_MAJOR_VERSION) {
177  avpriv_report_missing_feature(s, "Major version %i", version >> 8);
178  return AVERROR_PATCHWELCOME;
179  }
180 
182 
183  return codec2_read_header_common(s, st);
184 }
185 
187 {
188  Codec2Context *c2 = s->priv_data;
189  AVStream *st = s->streams[0];
190  int ret, size, n, block_align, frame_size;
191 
192  block_align = st->codecpar->block_align;
194 
195  if (block_align <= 0 || frame_size <= 0 || c2->frames_per_packet <= 0) {
196  return AVERROR(EINVAL);
197  }
198 
199  //try to read desired number of frames, compute n from to actual number of bytes read
200  size = c2->frames_per_packet * block_align;
201  ret = av_get_packet(s->pb, pkt, size);
202  if (ret < 0) {
203  return ret;
204  }
205 
206  //only set duration - compute_pkt_fields() and ff_pcm_read_seek() takes care of everything else
207  //tested by spamming the seek functionality in ffplay
208  n = ret / block_align;
209  pkt->duration = n * frame_size;
210 
211  return ret;
212 }
213 
215 {
216  AVStream *st;
217 
218  if (s->nb_streams != 1 || s->streams[0]->codecpar->codec_id != AV_CODEC_ID_CODEC2) {
219  av_log(s, AV_LOG_ERROR, ".c2 files must have exactly one codec2 stream\n");
220  return AVERROR(EINVAL);
221  }
222 
223  st = s->streams[0];
224 
226  av_log(s, AV_LOG_ERROR, ".c2 files require exactly %i bytes of extradata (got %i)\n",
228  return AVERROR(EINVAL);
229  }
230 
231  avio_wb24(s->pb, CODEC2_MAGIC);
233 
234  return 0;
235 }
236 
238 {
239  Codec2Context *c2 = s->priv_data;
240  AVStream *st;
241  int ret;
242 
243  if (c2->mode < 0) {
244  //FIXME: using a default value of -1 for mandatory options is an incredibly ugly hack
245  av_log(s, AV_LOG_ERROR, "-mode must be set in order to make sense of raw codec2 files\n");
246  return AVERROR(EINVAL);
247  }
248 
249  st = avformat_new_stream(s, NULL);
250  if (!st) {
251  return AVERROR(ENOMEM);
252  }
253 
255  if (ret) {
256  return ret;
257  }
258 
260 
261  return codec2_read_header_common(s, st);
262 }
263 
264 //transcoding report2074.c2 to wav went from 7.391s to 5.322s with -frames_per_packet 1000 compared to default, same sha1sum
265 #define FRAMES_PER_PACKET \
266  { "frames_per_packet", "Number of frames to read at a time. Higher = faster decoding, lower granularity", \
267  offsetof(Codec2Context, frames_per_packet), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM}
268 
269 static const AVOption codec2_options[] = {
271  { NULL },
272 };
273 
274 static const AVOption codec2raw_options[] = {
275  CODEC2_AVOPTIONS("codec2 mode [mandatory]", Codec2Context, -1, -1, AV_OPT_FLAG_DECODING_PARAM),
277  { NULL },
278 };
279 
280 static const AVClass codec2_demux_class = {
281  .class_name = "codec2 demuxer",
282  .item_name = av_default_item_name,
283  .option = codec2_options,
284  .version = LIBAVUTIL_VERSION_INT,
285  .category = AV_CLASS_CATEGORY_DEMUXER,
286 };
287 
289  .class_name = "codec2raw demuxer",
290  .item_name = av_default_item_name,
291  .option = codec2raw_options,
292  .version = LIBAVUTIL_VERSION_INT,
293  .category = AV_CLASS_CATEGORY_DEMUXER,
294 };
295 
296 #if CONFIG_CODEC2_DEMUXER
298  .name = "codec2",
299  .long_name = NULL_IF_CONFIG_SMALL("codec2 .c2 demuxer"),
300  .priv_data_size = sizeof(Codec2Context),
301  .extensions = "c2",
307  .raw_codec_id = AV_CODEC_ID_CODEC2,
308  .priv_class = &codec2_demux_class,
309 };
310 #endif
311 
312 #if CONFIG_CODEC2_MUXER
314  .p.name = "codec2",
315  .p.long_name = NULL_IF_CONFIG_SMALL("codec2 .c2 muxer"),
316  .p.extensions = "c2",
317  .p.audio_codec = AV_CODEC_ID_CODEC2,
318  .p.video_codec = AV_CODEC_ID_NONE,
319  .p.flags = AVFMT_NOTIMESTAMPS,
320  .priv_data_size = sizeof(Codec2Context),
323 };
324 #endif
325 
326 #if CONFIG_CODEC2RAW_DEMUXER
328  .name = "codec2raw",
329  .long_name = NULL_IF_CONFIG_SMALL("raw codec2 demuxer"),
330  .priv_data_size = sizeof(Codec2Context),
335  .raw_codec_id = AV_CODEC_ID_CODEC2,
336  .priv_class = &codec2raw_demux_class,
337 };
338 #endif
CODEC2_EXTRADATA_SIZE
#define CODEC2_EXTRADATA_SIZE
Definition: codec2utils.h:48
codec2_options
static const AVOption codec2_options[]
Definition: codec2.c:269
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:76
AVOutputFormat::name
const char * name
Definition: avformat.h:508
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
opt.h
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: options.c:243
pcm.h
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:58
ff_codec2raw_demuxer
const AVInputFormat ff_codec2raw_demuxer
ffformatcontext
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition: internal.h:191
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:479
EXPECTED_CODEC2_MINOR_VERSION
#define EXPECTED_CODEC2_MINOR_VERSION
Definition: codec2.c:41
codec2_read_packet
static int codec2_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: codec2.c:186
AVOption
AVOption.
Definition: opt.h:251
codec2_write_header
static int codec2_write_header(AVFormatContext *s)
Definition: codec2.c:214
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:34
codec2_read_header_common
static int codec2_read_header_common(AVFormatContext *s, AVStream *st)
Definition: codec2.c:127
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:771
codec2_mode_bit_rate
static int codec2_mode_bit_rate(AVFormatContext *s, int mode)
Definition: codec2.c:115
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:151
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVInputFormat
Definition: avformat.h:546
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
AVCodecParameters::frame_size
int frame_size
Audio only.
Definition: codec_par.h:189
codec2_make_extradata
static void codec2_make_extradata(uint8_t *ptr, int mode)
Definition: codec2utils.h:51
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
EXPECTED_CODEC2_MAJOR_VERSION
#define EXPECTED_CODEC2_MAJOR_VERSION
Definition: codec2.c:40
CODEC2_MODE_MAX
#define CODEC2_MODE_MAX
Definition: codec2utils.h:30
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:551
codec2_read_header
static int codec2_read_header(AVFormatContext *s)
Definition: codec2.c:151
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
frame_size
int frame_size
Definition: mxfenc.c:2205
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
codec2utils.h
FFFormatContext::data_offset
int64_t data_offset
offset of the first packet
Definition: internal.h:108
ff_raw_write_packet
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:31
AV_CODEC_ID_CODEC2
@ AV_CODEC_ID_CODEC2
Definition: codec_id.h:505
AV_CLASS_CATEGORY_DEMUXER
@ AV_CLASS_CATEGORY_DEMUXER
Definition: log.h:33
AVFormatContext
Format I/O context.
Definition: avformat.h:1104
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:861
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:545
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
codec2raw_read_header
static int codec2raw_read_header(AVFormatContext *s)
Definition: codec2.c:237
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
FFOutputFormat
Definition: mux.h:30
Codec2Context::frames_per_packet
int frames_per_packet
Definition: codec2.c:46
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:213
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:178
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:80
avio_rb24
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:775
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:115
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:301
size
int size
Definition: twinvq_data.h:10344
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:222
codec2_mode_from_extradata
static uint8_t codec2_mode_from_extradata(uint8_t *ptr)
Definition: codec2utils.h:59
version
version
Definition: libkvazaar.c:313
rawenc.h
codec2raw_demux_class
static const AVClass codec2raw_demux_class
Definition: codec2.c:288
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
avio_internal.h
Codec2Context::mode
int mode
Definition: codec2.c:45
CODEC2_AVOPTIONS
#define CODEC2_AVOPTIONS(desc, classname, min_val, default_val, option_flags)
Definition: codec2utils.h:36
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:185
codec2_mode_block_align
static int codec2_mode_block_align(AVFormatContext *s, int mode)
Definition: codec2.c:91
AV_OPT_FLAG_DECODING_PARAM
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:282
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
FRAMES_PER_PACKET
#define FRAMES_PER_PACKET
Definition: codec2.c:265
codec2raw_options
static const AVOption codec2raw_options[]
Definition: codec2.c:274
write_packet
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition: ffmpeg_mux.c:61
av_get_packet
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:102
codec2_mode_frame_size
static int codec2_mode_frame_size(AVFormatContext *s, int mode)
Definition: codec2.c:68
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:838
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
ff_pcm_read_seek
int ff_pcm_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: pcm.c:56
avformat.h
ff_codec2_demuxer
const AVInputFormat ff_codec2_demuxer
codec2_demux_class
static const AVClass codec2_demux_class
Definition: codec2.c:280
c2
static const uint64_t c2
Definition: murmur3.c:52
channel_layout.h
mode
mode
Definition: ebur128.h:83
Codec2Context
Definition: codec2.c:43
CODEC2_MAGIC
#define CODEC2_MAGIC
Definition: codec2.c:36
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:29
AVCodecParameters::format
int format
Definition: codec_par.h:86
avio_wb24
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:476
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:368
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:62
codec2_probe
static int codec2_probe(const AVProbeData *p)
Definition: codec2.c:49
AVPacket
This structure stores compressed data.
Definition: packet.h:351
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:91
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
ffio_read_size
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:683
CODEC2_HEADER_SIZE
#define CODEC2_HEADER_SIZE
Definition: codec2.c:35
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:346
AV_RB24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:97
ff_codec2_muxer
const FFOutputFormat ff_codec2_muxer
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98
ff_alloc_extradata
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition: utils.c:238
mux.h