FFmpeg  4.0
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 AVPRIV_CODEC2_HEADER_SIZE 7
33 #define AVPRIV_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(AVProbeData *p)
47 {
48  //must start wih C0 DE C2
49  if (AV_RB24(p->buf) != AVPRIV_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 
65 {
67 
70  st->codecpar->sample_rate = 8000;
71  st->codecpar->channels = 1;
77 
78  if (st->codecpar->bit_rate <= 0 ||
79  st->codecpar->frame_size <= 0 ||
80  st->codecpar->block_align <= 0) {
81  return AVERROR_INVALIDDATA;
82  }
83 
84  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
85 
86  return 0;
87 }
88 
90 {
92  int ret, version;
93 
94  if (!st) {
95  return AVERROR(ENOMEM);
96  }
97 
98  if (avio_rb24(s->pb) != AVPRIV_CODEC2_MAGIC) {
99  av_log(s, AV_LOG_ERROR, "not a .c2 file\n");
100  return AVERROR_INVALIDDATA;
101  }
102 
104  if (ret) {
105  return ret;
106  }
107 
109  if (ret < 0) {
110  return ret;
111  }
112 
114  if ((version >> 8) != EXPECTED_CODEC2_MAJOR_VERSION) {
115  avpriv_report_missing_feature(s, "Major version %i", version >> 8);
116  return AVERROR_PATCHWELCOME;
117  }
118 
120 
121  return codec2_read_header_common(s, st);
122 }
123 
125 {
126  Codec2Context *c2 = s->priv_data;
127  AVStream *st = s->streams[0];
128  int ret, size, n, block_align, frame_size;
129 
130  block_align = st->codecpar->block_align;
131  frame_size = st->codecpar->frame_size;
132 
133  if (block_align <= 0 || frame_size <= 0 || c2->frames_per_packet <= 0) {
134  return AVERROR(EINVAL);
135  }
136 
137  //try to read desired number of frames, compute n from to actual number of bytes read
138  size = c2->frames_per_packet * block_align;
139  ret = av_get_packet(s->pb, pkt, size);
140  if (ret < 0) {
141  return ret;
142  }
143 
144  //only set duration - compute_pkt_fields() and ff_pcm_read_seek() takes care of everything else
145  //tested by spamming the seek functionality in ffplay
146  n = ret / block_align;
147  pkt->duration = n * frame_size;
148 
149  return ret;
150 }
151 
153 {
154  AVStream *st;
155 
156  if (s->nb_streams != 1 || s->streams[0]->codecpar->codec_id != AV_CODEC_ID_CODEC2) {
157  av_log(s, AV_LOG_ERROR, ".c2 files must have exactly one codec2 stream\n");
158  return AVERROR(EINVAL);
159  }
160 
161  st = s->streams[0];
162 
164  av_log(s, AV_LOG_ERROR, ".c2 files require exactly %i bytes of extradata (got %i)\n",
166  return AVERROR(EINVAL);
167  }
168 
171 
172  return 0;
173 }
174 
176 {
177  Codec2Context *c2 = s->priv_data;
178  AVStream *st;
179  int ret;
180 
181  if (c2->mode < 0) {
182  //FIXME: using a default value of -1 for mandatory options is an incredibly ugly hack
183  av_log(s, AV_LOG_ERROR, "-mode must be set in order to make sense of raw codec2 files\n");
184  return AVERROR(EINVAL);
185  }
186 
187  st = avformat_new_stream(s, NULL);
188  if (!st) {
189  return AVERROR(ENOMEM);
190  }
191 
193  if (ret) {
194  return ret;
195  }
196 
197  s->internal->data_offset = 0;
199 
200  return codec2_read_header_common(s, st);
201 }
202 
203 //transcoding report2074.c2 to wav went from 7.391s to 5.322s with -frames_per_packet 1000 compared to default, same sha1sum
204 #define FRAMES_PER_PACKET \
205  { "frames_per_packet", "Number of frames to read at a time. Higher = faster decoding, lower granularity", \
206  offsetof(Codec2Context, frames_per_packet), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM}
207 
208 static const AVOption codec2_options[] = {
210  { NULL },
211 };
212 
213 static const AVOption codec2raw_options[] = {
214  AVPRIV_CODEC2_AVOPTIONS("codec2 mode [mandatory]", Codec2Context, -1, -1, AV_OPT_FLAG_DECODING_PARAM),
216  { NULL },
217 };
218 
219 static const AVClass codec2_mux_class = {
220  .class_name = "codec2 muxer",
221  .item_name = av_default_item_name,
222  .version = LIBAVUTIL_VERSION_INT,
223  .category = AV_CLASS_CATEGORY_DEMUXER,
224 };
225 
226 static const AVClass codec2_demux_class = {
227  .class_name = "codec2 demuxer",
228  .item_name = av_default_item_name,
229  .option = codec2_options,
230  .version = LIBAVUTIL_VERSION_INT,
231  .category = AV_CLASS_CATEGORY_DEMUXER,
232 };
233 
235  .class_name = "codec2raw demuxer",
236  .item_name = av_default_item_name,
237  .option = codec2raw_options,
238  .version = LIBAVUTIL_VERSION_INT,
239  .category = AV_CLASS_CATEGORY_DEMUXER,
240 };
241 
242 #if CONFIG_CODEC2_DEMUXER
244  .name = "codec2",
245  .long_name = NULL_IF_CONFIG_SMALL("codec2 .c2 demuxer"),
246  .priv_data_size = sizeof(Codec2Context),
247  .extensions = "c2",
253  .raw_codec_id = AV_CODEC_ID_CODEC2,
254  .priv_class = &codec2_demux_class,
255 };
256 #endif
257 
258 #if CONFIG_CODEC2_MUXER
260  .name = "codec2",
261  .long_name = NULL_IF_CONFIG_SMALL("codec2 .c2 muxer"),
262  .priv_data_size = sizeof(Codec2Context),
263  .extensions = "c2",
264  .audio_codec = AV_CODEC_ID_CODEC2,
265  .video_codec = AV_CODEC_ID_NONE,
269  .priv_class = &codec2_mux_class,
270 };
271 #endif
272 
273 #if CONFIG_CODEC2RAW_DEMUXER
275  .name = "codec2raw",
276  .long_name = NULL_IF_CONFIG_SMALL("raw codec2 demuxer"),
277  .priv_data_size = sizeof(Codec2Context),
282  .raw_codec_id = AV_CODEC_ID_CODEC2,
283  .priv_class = &codec2raw_demux_class,
284 };
285 #endif
static const AVOption codec2raw_options[]
Definition: codec2.c:213
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:679
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int size
AVOption.
Definition: opt.h:246
static const AVClass codec2_demux_class
Definition: codec2.c:226
#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:4811
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:697
#define FRAMES_PER_PACKET
Definition: codec2.c:204
int64_t data_offset
offset of the first packet
Definition: internal.h:82
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3884
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1793
static uint8_t avpriv_codec2_mode_from_extradata(uint8_t *ptr)
Definition: codec2utils.h:78
#define AV_RB24
Definition: intreadwrite.h:64
int version
Definition: avisynth_c.h:766
static AVPacket pkt
#define AVPRIV_CODEC2_HEADER_SIZE
Definition: codec2.c:32
int frame_size
Audio only.
Definition: avcodec.h:4005
Format I/O context.
Definition: avformat.h:1342
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
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1448
AVInputFormat ff_codec2raw_demuxer
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4441
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1410
int avpriv_codec2_mode_bit_rate(void *logctx, int mode)
Definition: codec2utils.c:26
static int flags
Definition: log.c:55
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
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:218
uint64_t channel_layout
Audio only.
Definition: avcodec.h:3986
#define av_log(a,...)
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3913
static int codec2_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: codec2.c:124
static int codec2_probe(AVProbeData *p)
Definition: codec2.c:46
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVOutputFormat ff_codec2_muxer
#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:186
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3880
#define EXPECTED_CODEC2_MINOR_VERSION
Definition: codec2.c:38
#define AVPRIV_CODEC2_AVOPTIONS(desc, classname, min_val, default_val, option_flags)
Definition: codec2utils.h:36
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3902
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1398
int block_align
Audio only.
Definition: avcodec.h:4001
#define AVPRIV_CODEC2_EXTRADATA_SIZE
Definition: codec2utils.h:62
static uint16_t avpriv_codec2_version_from_extradata(uint8_t *ptr)
Definition: codec2utils.h:74
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:3271
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:794
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int frames_per_packet
Definition: codec2.c:43
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:487
const char * name
Definition: avformat.h:507
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:29
int avpriv_codec2_mode_frame_size(void *logctx, int mode)
Definition: codec2utils.c:38
int n
Definition: avisynth_c.h:684
int ff_pcm_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: pcm.c:52
static int codec2_read_header_common(AVFormatContext *s, AVStream *st)
Definition: codec2.c:64
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
Stream structure.
Definition: avformat.h:873
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:469
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define EXPECTED_CODEC2_MAJOR_VERSION
Definition: codec2.c:37
int frame_size
Definition: mxfenc.c:1947
int avpriv_codec2_mode_block_align(void *logctx, int mode)
Definition: codec2utils.c:60
AVIOContext * pb
I/O context.
Definition: avformat.h:1384
#define AVPRIV_CODEC2_MAGIC
Definition: codec2.c:33
Describe the class of an AVClass context structure.
Definition: log.h:67
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:470
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:277
static int codec2raw_read_header(AVFormatContext *s)
Definition: codec2.c:175
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:458
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
static const AVClass codec2_mux_class
Definition: codec2.c:219
static const AVOption codec2_options[]
Definition: codec2.c:208
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
int sample_rate
Audio only.
Definition: avcodec.h:3994
static int codec2_write_header(AVFormatContext *s)
Definition: codec2.c:152
Main libavformat public API header.
int mode
Definition: codec2.c:42
AVInputFormat ff_codec2_demuxer
signed 16 bits
Definition: samplefmt.h:61
static const uint64_t c2
Definition: murmur3.c:50
static void avpriv_codec2_make_extradata(uint8_t *ptr, int mode)
Definition: codec2utils.h:65
void * priv_data
Format private data.
Definition: avformat.h:1370
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3898
int channels
Audio only.
Definition: avcodec.h:3990
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1020
static const AVClass codec2raw_demux_class
Definition: codec2.c:234
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:1407
mode
Use these values in ebur128_init (or&#39;ed).
Definition: ebur128.h:83
static int codec2_read_header(AVFormatContext *s)
Definition: codec2.c:89