FFmpeg  4.4.9
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 <memory.h>
23 #include "libavcodec/codec2utils.h"
24 #include "libavutil/intreadwrite.h"
25 #include "avio_internal.h"
26 #include "avformat.h"
27 #include "internal.h"
28 #include "rawdec.h"
29 #include "rawenc.h"
30 #include "pcm.h"
31 
32 #define CODEC2_HEADER_SIZE 7
33 #define CODEC2_MAGIC 0xC0DEC2
34 
35 //the lowest version we should ever run across is 0.8
36 //we may run across later versions as the format evolves
37 #define EXPECTED_CODEC2_MAJOR_VERSION 0
38 #define EXPECTED_CODEC2_MINOR_VERSION 8
39 
40 typedef struct {
41  const AVClass *class;
42  int mode;
45 
46 static int codec2_probe(const AVProbeData *p)
47 {
48  //must start wih C0 DE C2
49  if (AV_RB24(p->buf) != CODEC2_MAGIC) {
50  return 0;
51  }
52 
53  //no .c2 files prior to 0.8
54  //be strict about major version while we're at it
55  if (p->buf[3] != EXPECTED_CODEC2_MAJOR_VERSION ||
57  return 0;
58  }
59 
60  //32 bits of identification -> low score
61  return AVPROBE_SCORE_EXTENSION + 1;
62 }
63 
64 //Mimics codec2_samples_per_frame()
66 {
67  int frame_size_table[CODEC2_MODE_MAX+1] = {
68  160, // 3200
69  160, // 2400
70  320, // 1600
71  320, // 1400
72  320, // 1300
73  320, // 1200
74  320, // 700
75  320, // 700B
76  320, // 700C
77  };
78 
80  av_log(s, AV_LOG_ERROR, "unknown codec2 mode %i, can't find frame_size\n", mode);
81  return 0;
82  } else {
83  return frame_size_table[mode];
84  }
85 }
86 
87 //Mimics (codec2_bits_per_frame()+7)/8
89 {
90  int block_align_table[CODEC2_MODE_MAX+1] = {
91  8, // 3200
92  6, // 2400
93  8, // 1600
94  7, // 1400
95  7, // 1300
96  6, // 1200
97  4, // 700
98  4, // 700B
99  4, // 700C
100  };
101 
103  av_log(s, AV_LOG_ERROR, "unknown codec2 mode %i, can't find block_align\n", mode);
104  return 0;
105  } else {
106  return block_align_table[mode];
107  }
108 }
109 
110 //Computes bitrate from mode, with frames rounded up to the nearest octet.
111 //So 700 bit/s (28 bits/frame) becomes 800 bits/s (32 bits/frame).
113 {
115  int block_align = codec2_mode_block_align(s, mode);
116 
117  if (frame_size <= 0 || block_align <= 0) {
118  return 0;
119  }
120 
121  return 8 * 8000 * block_align / frame_size;
122 }
123 
125 {
127 
130  st->codecpar->sample_rate = 8000;
131  st->codecpar->channels = 1;
137 
138  if (st->codecpar->bit_rate <= 0 ||
139  st->codecpar->frame_size <= 0 ||
140  st->codecpar->block_align <= 0) {
141  return AVERROR_INVALIDDATA;
142  }
143 
144  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
145 
146  return 0;
147 }
148 
150 {
152  int ret, version;
153 
154  if (!st) {
155  return AVERROR(ENOMEM);
156  }
157 
158  if (avio_rb24(s->pb) != CODEC2_MAGIC) {
159  av_log(s, AV_LOG_ERROR, "not a .c2 file\n");
160  return AVERROR_INVALIDDATA;
161  }
162 
164  if (ret) {
165  return ret;
166  }
167 
169  if (ret < 0) {
170  return ret;
171  }
172 
174  if ((version >> 8) != EXPECTED_CODEC2_MAJOR_VERSION) {
175  avpriv_report_missing_feature(s, "Major version %i", version >> 8);
176  return AVERROR_PATCHWELCOME;
177  }
178 
179  s->internal->data_offset = CODEC2_HEADER_SIZE;
180 
181  return codec2_read_header_common(s, st);
182 }
183 
185 {
186  Codec2Context *c2 = s->priv_data;
187  AVStream *st = s->streams[0];
188  int ret, size, n, block_align, frame_size;
189 
190  block_align = st->codecpar->block_align;
192 
193  if (block_align <= 0 || frame_size <= 0 || c2->frames_per_packet <= 0) {
194  return AVERROR(EINVAL);
195  }
196 
197  //try to read desired number of frames, compute n from to actual number of bytes read
198  if (c2->frames_per_packet > INT_MAX / block_align)
199  return AVERROR(EINVAL);
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 = (int64_t)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 
259  s->internal->data_offset = 0;
261 
262  return codec2_read_header_common(s, st);
263 }
264 
265 //transcoding report2074.c2 to wav went from 7.391s to 5.322s with -frames_per_packet 1000 compared to default, same sha1sum
266 #define FRAMES_PER_PACKET \
267  { "frames_per_packet", "Number of frames to read at a time. Higher = faster decoding, lower granularity", \
268  offsetof(Codec2Context, frames_per_packet), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM}
269 
270 static const AVOption codec2_options[] = {
272  { NULL },
273 };
274 
275 static const AVOption codec2raw_options[] = {
276  CODEC2_AVOPTIONS("codec2 mode [mandatory]", Codec2Context, -1, -1, AV_OPT_FLAG_DECODING_PARAM),
278  { NULL },
279 };
280 
281 static const AVClass codec2_mux_class = {
282  .class_name = "codec2 muxer",
283  .item_name = av_default_item_name,
284  .version = LIBAVUTIL_VERSION_INT,
285  .category = AV_CLASS_CATEGORY_DEMUXER,
286 };
287 
288 static const AVClass codec2_demux_class = {
289  .class_name = "codec2 demuxer",
290  .item_name = av_default_item_name,
291  .option = codec2_options,
292  .version = LIBAVUTIL_VERSION_INT,
293  .category = AV_CLASS_CATEGORY_DEMUXER,
294 };
295 
297  .class_name = "codec2raw demuxer",
298  .item_name = av_default_item_name,
299  .option = codec2raw_options,
300  .version = LIBAVUTIL_VERSION_INT,
301  .category = AV_CLASS_CATEGORY_DEMUXER,
302 };
303 
304 #if CONFIG_CODEC2_DEMUXER
306  .name = "codec2",
307  .long_name = NULL_IF_CONFIG_SMALL("codec2 .c2 demuxer"),
308  .priv_data_size = sizeof(Codec2Context),
309  .extensions = "c2",
315  .raw_codec_id = AV_CODEC_ID_CODEC2,
316  .priv_class = &codec2_demux_class,
317 };
318 #endif
319 
320 #if CONFIG_CODEC2_MUXER
322  .name = "codec2",
323  .long_name = NULL_IF_CONFIG_SMALL("codec2 .c2 muxer"),
324  .priv_data_size = sizeof(Codec2Context),
325  .extensions = "c2",
326  .audio_codec = AV_CODEC_ID_CODEC2,
327  .video_codec = AV_CODEC_ID_NONE,
331  .priv_class = &codec2_mux_class,
332 };
333 #endif
334 
335 #if CONFIG_CODEC2RAW_DEMUXER
337  .name = "codec2raw",
338  .long_name = NULL_IF_CONFIG_SMALL("raw codec2 demuxer"),
339  .priv_data_size = sizeof(Codec2Context),
344  .raw_codec_id = AV_CODEC_ID_CODEC2,
345  .priv_class = &codec2raw_demux_class,
346 };
347 #endif
AVInputFormat ff_codec2raw_demuxer
AVInputFormat ff_codec2_demuxer
AVOutputFormat ff_codec2_muxer
Main libavformat public API header.
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:451
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:310
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:463
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:462
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:480
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:230
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:790
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:698
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define AV_RB24
Definition: intreadwrite.h:64
#define AV_RB16
Definition: intreadwrite.h:53
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
static int codec2_mode_bit_rate(AVFormatContext *s, int mode)
Definition: codec2.c:112
static const AVClass codec2_mux_class
Definition: codec2.c:281
static int codec2raw_read_header(AVFormatContext *s)
Definition: codec2.c:237
static int codec2_probe(const AVProbeData *p)
Definition: codec2.c:46
static const AVOption codec2raw_options[]
Definition: codec2.c:275
static int codec2_mode_block_align(AVFormatContext *s, int mode)
Definition: codec2.c:88
static int codec2_read_header(AVFormatContext *s)
Definition: codec2.c:149
#define FRAMES_PER_PACKET
Definition: codec2.c:266
static int codec2_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: codec2.c:184
static int codec2_read_header_common(AVFormatContext *s, AVStream *st)
Definition: codec2.c:124
static const AVClass codec2_demux_class
Definition: codec2.c:288
static int codec2_write_header(AVFormatContext *s)
Definition: codec2.c:214
#define EXPECTED_CODEC2_MAJOR_VERSION
Definition: codec2.c:37
#define EXPECTED_CODEC2_MINOR_VERSION
Definition: codec2.c:38
#define CODEC2_MAGIC
Definition: codec2.c:33
static const AVClass codec2raw_demux_class
Definition: codec2.c:296
#define CODEC2_HEADER_SIZE
Definition: codec2.c:32
static int codec2_mode_frame_size(AVFormatContext *s, int mode)
Definition: codec2.c:65
static const AVOption codec2_options[]
Definition: codec2.c:270
#define CODEC2_AVOPTIONS(desc, classname, min_val, default_val, option_flags)
Definition: codec2utils.h:38
static uint8_t codec2_mode_from_extradata(uint8_t *ptr)
Definition: codec2utils.h:77
#define CODEC2_EXTRADATA_SIZE
Definition: codec2utils.h:66
#define CODEC2_MODE_MAX
Definition: codec2utils.h:32
static void codec2_make_extradata(uint8_t *ptr, int mode)
Definition: codec2utils.h:69
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:740
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:555
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:348
#define AV_CH_LAYOUT_MONO
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
@ AV_CODEC_ID_CODEC2
Definition: codec_id.h:491
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4508
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
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:4944
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:3314
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
int ff_pcm_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: pcm.c:56
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:29
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
version
Definition: libkvazaar.c:326
@ AV_CLASS_CATEGORY_DEMUXER
Definition: log.h:34
static const uint64_t c2
Definition: murmur3.c:52
int frame_size
Definition: mxfenc.c:2206
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:279
Describe the class of an AVClass context structure.
Definition: log.h:67
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:72
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
int frame_size
Audio only.
Definition: codec_par.h:181
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
int channels
Audio only.
Definition: codec_par.h:166
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
int block_align
Audio only.
Definition: codec_par.h:177
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int sample_rate
Audio only.
Definition: codec_par.h:170
Format I/O context.
Definition: avformat.h:1232
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
AVOption.
Definition: opt.h:248
const char * name
Definition: avformat.h:491
This structure stores compressed data.
Definition: packet.h:346
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
int frames_per_packet
Definition: codec2.c:43
int mode
Definition: codec2.c:42
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59
int size