]> git.sesse.net Git - ffmpeg/blob - libavformat/codec2.c
avformat: Constify all muxer/demuxers
[ffmpeg] / libavformat / codec2.c
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;
43     int frames_per_packet;
44 } Codec2Context;
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 ||
56         p->buf[4] <  EXPECTED_CODEC2_MINOR_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()
65 static int codec2_mode_frame_size(AVFormatContext *s, int mode)
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
79     if (mode < 0 || mode > CODEC2_MODE_MAX) {
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
88 static int codec2_mode_block_align(AVFormatContext *s, int mode)
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
102     if (mode < 0 || mode > CODEC2_MODE_MAX) {
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).
112 static int codec2_mode_bit_rate(AVFormatContext *s, int mode)
113 {
114     int frame_size  = codec2_mode_frame_size(s, mode);
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
124 static int codec2_read_header_common(AVFormatContext *s, AVStream *st)
125 {
126     int mode = codec2_mode_from_extradata(st->codecpar->extradata);
127
128     st->codecpar->codec_type        = AVMEDIA_TYPE_AUDIO;
129     st->codecpar->codec_id          = AV_CODEC_ID_CODEC2;
130     st->codecpar->sample_rate       = 8000;
131     st->codecpar->channels          = 1;
132     st->codecpar->format            = AV_SAMPLE_FMT_S16;
133     st->codecpar->channel_layout    = AV_CH_LAYOUT_MONO;
134     st->codecpar->bit_rate          = codec2_mode_bit_rate(s, mode);
135     st->codecpar->frame_size        = codec2_mode_frame_size(s, mode);
136     st->codecpar->block_align       = codec2_mode_block_align(s, mode);
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
149 static int codec2_read_header(AVFormatContext *s)
150 {
151     AVStream *st = avformat_new_stream(s, NULL);
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
163     ret = ff_alloc_extradata(st->codecpar, CODEC2_EXTRADATA_SIZE);
164     if (ret) {
165         return ret;
166     }
167
168     ret = ffio_read_size(s->pb, st->codecpar->extradata, CODEC2_EXTRADATA_SIZE);
169     if (ret < 0) {
170         return ret;
171     }
172
173     version = AV_RB16(st->codecpar->extradata);
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
184 static int codec2_read_packet(AVFormatContext *s, AVPacket *pkt)
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;
191     frame_size  = st->codecpar->frame_size;
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     size = c2->frames_per_packet * block_align;
199     ret = av_get_packet(s->pb, pkt, size);
200     if (ret < 0) {
201         return ret;
202     }
203
204     //only set duration - compute_pkt_fields() and ff_pcm_read_seek() takes care of everything else
205     //tested by spamming the seek functionality in ffplay
206     n = ret / block_align;
207     pkt->duration = n * frame_size;
208
209     return ret;
210 }
211
212 static int codec2_write_header(AVFormatContext *s)
213 {
214     AVStream *st;
215
216     if (s->nb_streams != 1 || s->streams[0]->codecpar->codec_id != AV_CODEC_ID_CODEC2) {
217         av_log(s, AV_LOG_ERROR, ".c2 files must have exactly one codec2 stream\n");
218         return AVERROR(EINVAL);
219     }
220
221     st = s->streams[0];
222
223     if (st->codecpar->extradata_size != CODEC2_EXTRADATA_SIZE) {
224         av_log(s, AV_LOG_ERROR, ".c2 files require exactly %i bytes of extradata (got %i)\n",
225                CODEC2_EXTRADATA_SIZE, st->codecpar->extradata_size);
226         return AVERROR(EINVAL);
227     }
228
229     avio_wb24(s->pb, CODEC2_MAGIC);
230     avio_write(s->pb, st->codecpar->extradata, CODEC2_EXTRADATA_SIZE);
231
232     return 0;
233 }
234
235 static int codec2raw_read_header(AVFormatContext *s)
236 {
237     Codec2Context *c2 = s->priv_data;
238     AVStream *st;
239     int ret;
240
241     if (c2->mode < 0) {
242         //FIXME: using a default value of -1 for mandatory options is an incredibly ugly hack
243         av_log(s, AV_LOG_ERROR, "-mode must be set in order to make sense of raw codec2 files\n");
244         return AVERROR(EINVAL);
245     }
246
247     st = avformat_new_stream(s, NULL);
248     if (!st) {
249         return AVERROR(ENOMEM);
250     }
251
252     ret = ff_alloc_extradata(st->codecpar, CODEC2_EXTRADATA_SIZE);
253     if (ret) {
254         return ret;
255     }
256
257     s->internal->data_offset = 0;
258     codec2_make_extradata(st->codecpar->extradata, c2->mode);
259
260     return codec2_read_header_common(s, st);
261 }
262
263 //transcoding report2074.c2 to wav went from 7.391s to 5.322s with -frames_per_packet 1000 compared to default, same sha1sum
264 #define FRAMES_PER_PACKET \
265     { "frames_per_packet", "Number of frames to read at a time. Higher = faster decoding, lower granularity", \
266       offsetof(Codec2Context, frames_per_packet), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM}
267
268 static const AVOption codec2_options[] = {
269     FRAMES_PER_PACKET,
270     { NULL },
271 };
272
273 static const AVOption codec2raw_options[] = {
274     CODEC2_AVOPTIONS("codec2 mode [mandatory]", Codec2Context, -1, -1, AV_OPT_FLAG_DECODING_PARAM),
275     FRAMES_PER_PACKET,
276     { NULL },
277 };
278
279 static const AVClass codec2_mux_class = {
280     .class_name = "codec2 muxer",
281     .item_name  = av_default_item_name,
282     .version    = LIBAVUTIL_VERSION_INT,
283     .category   = AV_CLASS_CATEGORY_DEMUXER,
284 };
285
286 static const AVClass codec2_demux_class = {
287     .class_name = "codec2 demuxer",
288     .item_name  = av_default_item_name,
289     .option     = codec2_options,
290     .version    = LIBAVUTIL_VERSION_INT,
291     .category   = AV_CLASS_CATEGORY_DEMUXER,
292 };
293
294 static const AVClass codec2raw_demux_class = {
295     .class_name = "codec2raw demuxer",
296     .item_name  = av_default_item_name,
297     .option     = codec2raw_options,
298     .version    = LIBAVUTIL_VERSION_INT,
299     .category   = AV_CLASS_CATEGORY_DEMUXER,
300 };
301
302 #if CONFIG_CODEC2_DEMUXER
303 const AVInputFormat ff_codec2_demuxer = {
304     .name           = "codec2",
305     .long_name      = NULL_IF_CONFIG_SMALL("codec2 .c2 demuxer"),
306     .priv_data_size = sizeof(Codec2Context),
307     .extensions     = "c2",
308     .read_probe     = codec2_probe,
309     .read_header    = codec2_read_header,
310     .read_packet    = codec2_read_packet,
311     .read_seek      = ff_pcm_read_seek,
312     .flags          = AVFMT_GENERIC_INDEX,
313     .raw_codec_id   = AV_CODEC_ID_CODEC2,
314     .priv_class     = &codec2_demux_class,
315 };
316 #endif
317
318 #if CONFIG_CODEC2_MUXER
319 const AVOutputFormat ff_codec2_muxer = {
320     .name           = "codec2",
321     .long_name      = NULL_IF_CONFIG_SMALL("codec2 .c2 muxer"),
322     .priv_data_size = sizeof(Codec2Context),
323     .extensions     = "c2",
324     .audio_codec    = AV_CODEC_ID_CODEC2,
325     .video_codec    = AV_CODEC_ID_NONE,
326     .write_header   = codec2_write_header,
327     .write_packet   = ff_raw_write_packet,
328     .flags          = AVFMT_NOTIMESTAMPS,
329     .priv_class     = &codec2_mux_class,
330 };
331 #endif
332
333 #if CONFIG_CODEC2RAW_DEMUXER
334 const AVInputFormat ff_codec2raw_demuxer = {
335     .name           = "codec2raw",
336     .long_name      = NULL_IF_CONFIG_SMALL("raw codec2 demuxer"),
337     .priv_data_size = sizeof(Codec2Context),
338     .read_header    = codec2raw_read_header,
339     .read_packet    = codec2_read_packet,
340     .read_seek      = ff_pcm_read_seek,
341     .flags          = AVFMT_GENERIC_INDEX,
342     .raw_codec_id   = AV_CODEC_ID_CODEC2,
343     .priv_class     = &codec2raw_demux_class,
344 };
345 #endif