FFmpeg
Loading...
Searching...
No Matches
astenc.c
Go to the documentation of this file.
1/*
2 * AST muxer
3 * Copyright (c) 2012 James Almer
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 "avformat.h"
23#include "avio_internal.h"
24#include "internal.h"
25#include "ast.h"
26#include "mux.h"
28#include "libavutil/opt.h"
29
38
39#define CHECK_LOOP(type) \
40 if (ast->loop ## type > 0) { \
41 ast->loop ## type = av_rescale_rnd(ast->loop ## type, par->sample_rate, 1000, AV_ROUND_DOWN); \
42 if (ast->loop ## type < 0 || ast->loop ## type > UINT_MAX) { \
43 av_log(s, AV_LOG_ERROR, "Invalid loop" #type " value\n"); \
44 return AVERROR(EINVAL); \
45 } \
46 }
47
49{
50 ASTMuxContext *ast = s->priv_data;
51 AVIOContext *pb = s->pb;
52 AVCodecParameters *par = s->streams[0]->codecpar;
53 unsigned int codec_tag;
54
55 if (par->codec_id == AV_CODEC_ID_ADPCM_AFC) {
56 av_log(s, AV_LOG_ERROR, "muxing ADPCM AFC is not implemented\n");
58 }
59
61 if (!codec_tag) {
62 av_log(s, AV_LOG_ERROR, "unsupported codec\n");
63 return AVERROR(EINVAL);
64 }
65
66 if (ast->loopend > 0 && ast->loopstart >= ast->loopend) {
67 av_log(s, AV_LOG_ERROR, "loopend can't be less or equal to loopstart\n");
68 return AVERROR(EINVAL);
69 }
70
71 /* Convert milliseconds to samples */
72 CHECK_LOOP(start)
73 CHECK_LOOP(end)
74
75 ffio_wfourcc(pb, "STRM");
76
77 ast->size = avio_tell(pb);
78 avio_wb32(pb, 0); /* File size minus header */
79 avio_wb16(pb, codec_tag);
80 avio_wb16(pb, 16); /* Bit depth */
82 avio_wb16(pb, 0); /* Loop flag */
83 avio_wb32(pb, par->sample_rate);
84
85 ast->samples = avio_tell(pb);
86 avio_wb32(pb, 0); /* Number of samples */
87 avio_wb32(pb, 0); /* Loopstart */
88 avio_wb32(pb, 0); /* Loopend */
89 avio_wb32(pb, 0); /* Size of first block */
90
91 /* Unknown */
92 avio_wb32(pb, 0);
93 avio_wl32(pb, 0x7F);
94 avio_wb64(pb, 0);
95 avio_wb64(pb, 0);
96 avio_wb32(pb, 0);
97
98 return 0;
99}
100
102{
103 AVIOContext *pb = s->pb;
104 ASTMuxContext *ast = s->priv_data;
105 AVCodecParameters *par = s->streams[0]->codecpar;
106 int size = pkt->size / par->ch_layout.nb_channels;
107
108 if (s->streams[0]->nb_frames == 0)
109 ast->fbs = size;
110
111 ffio_wfourcc(pb, "BLCK");
112 avio_wb32(pb, size); /* Block size */
113
114 /* padding */
115 ffio_fill(pb, 0, 24);
116
117 avio_write(pb, pkt->data, pkt->size);
118
119 return 0;
120}
121
123{
124 AVIOContext *pb = s->pb;
125 ASTMuxContext *ast = s->priv_data;
126 AVCodecParameters *par = s->streams[0]->codecpar;
127 int64_t file_size = avio_tell(pb);
128 int64_t samples = (file_size - 64 - (32 * s->streams[0]->nb_frames)) / par->block_align; /* PCM_S16BE_PLANAR */
129
130 av_log(s, AV_LOG_DEBUG, "total samples: %"PRId64"\n", samples);
131
132 if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
133 /* Number of samples */
134 avio_seek(pb, ast->samples, SEEK_SET);
135 avio_wb32(pb, samples);
136
137 /* Loopstart if provided */
138 if (ast->loopstart > 0) {
139 if (ast->loopstart >= samples) {
140 av_log(s, AV_LOG_WARNING, "Loopstart value is out of range and will be ignored\n");
141 ast->loopstart = -1;
142 avio_skip(pb, 4);
143 } else {
144 avio_wb32(pb, ast->loopstart);
145 }
146 } else {
147 avio_skip(pb, 4);
148 }
149
150 /* Loopend if provided. Otherwise number of samples again */
151 if (ast->loopend && ast->loopstart >= 0) {
152 if (ast->loopend > samples) {
153 av_log(s, AV_LOG_WARNING, "Loopend value is out of range and will be ignored\n");
154 ast->loopend = samples;
155 }
156 avio_wb32(pb, ast->loopend);
157 } else {
158 avio_wb32(pb, samples);
159 }
160
161 /* Size of first block */
162 avio_wb32(pb, ast->fbs);
163
164 /* File size minus header */
165 avio_seek(pb, ast->size, SEEK_SET);
166 avio_wb32(pb, file_size - 64);
167
168 /* Loop flag */
169 if (ast->loopstart >= 0) {
170 avio_skip(pb, 6);
171 avio_wb16(pb, 0xFFFF);
172 }
173
174 avio_seek(pb, file_size, SEEK_SET);
175 }
176 return 0;
177}
178
179#define OFFSET(obj) offsetof(ASTMuxContext, obj)
180static const AVOption options[] = {
181 { "loopstart", "Loopstart position in milliseconds.", OFFSET(loopstart), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
182 { "loopend", "Loopend position in milliseconds.", OFFSET(loopend), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
183 { NULL },
184};
185
186static const AVClass ast_muxer_class = {
187 .class_name = "AST muxer",
188 .item_name = av_default_item_name,
189 .option = options,
190 .version = LIBAVUTIL_VERSION_INT,
191};
192
194 .p.name = "ast",
195 .p.long_name = NULL_IF_CONFIG_SMALL("AST (Audio Stream)"),
196 .p.extensions = "ast",
197 .priv_data_size = sizeof(ASTMuxContext),
198 .p.audio_codec = AV_CODEC_ID_PCM_S16BE_PLANAR,
199 .p.video_codec = AV_CODEC_ID_NONE,
200 .p.subtitle_codec = AV_CODEC_ID_NONE,
201 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH,
202 .write_header = ast_write_header,
203 .write_packet = ast_write_packet,
204 .write_trailer = ast_write_trailer,
205 .p.priv_class = &ast_muxer_class,
206 .p.codec_tag = ff_ast_codec_tags_list,
207};
const FFOutputFormat ff_ast_muxer
Definition astenc.c:193
const AVCodecTag ff_codec_ast_tags[]
Definition ast.c:26
const AVCodecTag *const ff_ast_codec_tags_list[]
Definition ast.c:32
static int ast_write_trailer(AVFormatContext *s)
Definition astenc.c:122
#define OFFSET(obj)
Definition astenc.c:179
static int ast_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition astenc.c:101
static const AVClass ast_muxer_class
Definition astenc.c:186
static int ast_write_header(AVFormatContext *s)
Definition astenc.c:48
#define CHECK_LOOP(type)
Definition astenc.c:39
Main libavformat public API header.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
void avio_wl32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:360
void avio_wb32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:368
void avio_wb16(AVIOContext *s, unsigned int val)
Definition aviobuf.c:446
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
void avio_wb64(AVIOContext *s, uint64_t val)
Definition aviobuf.c:434
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition aviobuf.c:192
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
Definition opt.h:351
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition opt.h:262
@ AV_CODEC_ID_PCM_S16BE_PLANAR
Definition codec_id.h:360
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_ADPCM_AFC
Definition codec_id.h:401
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition utils.c:133
#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
AVOptions.
int64_t samples
Definition astenc.c:33
int64_t size
Definition astenc.c:32
int64_t loopstart
Definition astenc.c:34
int64_t loopend
Definition astenc.c:35
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
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
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
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
#define av_log(a,...)
int size