FFmpeg  4.3
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 "libavutil/mathematics.h"
27 #include "libavutil/opt.h"
28 
29 typedef struct ASTMuxContext {
30  AVClass *class;
31  int64_t size;
32  int64_t samples;
33  int64_t loopstart;
34  int64_t loopend;
35  int fbs;
37 
38 #define CHECK_LOOP(type) \
39  if (ast->loop ## type > 0) { \
40  ast->loop ## type = av_rescale_rnd(ast->loop ## type, par->sample_rate, 1000, AV_ROUND_DOWN); \
41  if (ast->loop ## type < 0 || ast->loop ## type > UINT_MAX) { \
42  av_log(s, AV_LOG_ERROR, "Invalid loop" #type " value\n"); \
43  return AVERROR(EINVAL); \
44  } \
45  }
46 
48 {
49  ASTMuxContext *ast = s->priv_data;
50  AVIOContext *pb = s->pb;
51  AVCodecParameters *par;
52  unsigned int codec_tag;
53 
54  if (s->nb_streams == 1) {
55  par = s->streams[0]->codecpar;
56  } else {
57  av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
58  return AVERROR(EINVAL);
59  }
60 
61  if (par->codec_id == AV_CODEC_ID_ADPCM_AFC) {
62  av_log(s, AV_LOG_ERROR, "muxing ADPCM AFC is not implemented\n");
63  return AVERROR_PATCHWELCOME;
64  }
65 
66  codec_tag = ff_codec_get_tag(ff_codec_ast_tags, par->codec_id);
67  if (!codec_tag) {
68  av_log(s, AV_LOG_ERROR, "unsupported codec\n");
69  return AVERROR(EINVAL);
70  }
71 
72  if (ast->loopend > 0 && ast->loopstart >= ast->loopend) {
73  av_log(s, AV_LOG_ERROR, "loopend can't be less or equal to loopstart\n");
74  return AVERROR(EINVAL);
75  }
76 
77  /* Convert milliseconds to samples */
78  CHECK_LOOP(start)
80 
81  ffio_wfourcc(pb, "STRM");
82 
83  ast->size = avio_tell(pb);
84  avio_wb32(pb, 0); /* File size minus header */
85  avio_wb16(pb, codec_tag);
86  avio_wb16(pb, 16); /* Bit depth */
87  avio_wb16(pb, par->channels);
88  avio_wb16(pb, 0); /* Loop flag */
89  avio_wb32(pb, par->sample_rate);
90 
91  ast->samples = avio_tell(pb);
92  avio_wb32(pb, 0); /* Number of samples */
93  avio_wb32(pb, 0); /* Loopstart */
94  avio_wb32(pb, 0); /* Loopend */
95  avio_wb32(pb, 0); /* Size of first block */
96 
97  /* Unknown */
98  avio_wb32(pb, 0);
99  avio_wl32(pb, 0x7F);
100  avio_wb64(pb, 0);
101  avio_wb64(pb, 0);
102  avio_wb32(pb, 0);
103 
104  return 0;
105 }
106 
108 {
109  AVIOContext *pb = s->pb;
110  ASTMuxContext *ast = s->priv_data;
111  AVCodecParameters *par = s->streams[0]->codecpar;
112  int size = pkt->size / par->channels;
113 
114  if (s->streams[0]->nb_frames == 0)
115  ast->fbs = size;
116 
117  ffio_wfourcc(pb, "BLCK");
118  avio_wb32(pb, size); /* Block size */
119 
120  /* padding */
121  avio_wb64(pb, 0);
122  avio_wb64(pb, 0);
123  avio_wb64(pb, 0);
124 
125  avio_write(pb, pkt->data, pkt->size);
126 
127  return 0;
128 }
129 
131 {
132  AVIOContext *pb = s->pb;
133  ASTMuxContext *ast = s->priv_data;
134  AVCodecParameters *par = s->streams[0]->codecpar;
135  int64_t file_size = avio_tell(pb);
136  int64_t samples = (file_size - 64 - (32 * s->streams[0]->nb_frames)) / par->block_align; /* PCM_S16BE_PLANAR */
137 
138  av_log(s, AV_LOG_DEBUG, "total samples: %"PRId64"\n", samples);
139 
140  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
141  /* Number of samples */
142  avio_seek(pb, ast->samples, SEEK_SET);
143  avio_wb32(pb, samples);
144 
145  /* Loopstart if provided */
146  if (ast->loopstart > 0) {
147  if (ast->loopstart >= samples) {
148  av_log(s, AV_LOG_WARNING, "Loopstart value is out of range and will be ignored\n");
149  ast->loopstart = -1;
150  avio_skip(pb, 4);
151  } else
152  avio_wb32(pb, ast->loopstart);
153  } else
154  avio_skip(pb, 4);
155 
156  /* Loopend if provided. Otherwise number of samples again */
157  if (ast->loopend && ast->loopstart >= 0) {
158  if (ast->loopend > samples) {
159  av_log(s, AV_LOG_WARNING, "Loopend value is out of range and will be ignored\n");
160  ast->loopend = samples;
161  }
162  avio_wb32(pb, ast->loopend);
163  } else {
164  avio_wb32(pb, samples);
165  }
166 
167  /* Size of first block */
168  avio_wb32(pb, ast->fbs);
169 
170  /* File size minus header */
171  avio_seek(pb, ast->size, SEEK_SET);
172  avio_wb32(pb, file_size - 64);
173 
174  /* Loop flag */
175  if (ast->loopstart >= 0) {
176  avio_skip(pb, 6);
177  avio_wb16(pb, 0xFFFF);
178  }
179 
180  avio_seek(pb, file_size, SEEK_SET);
181  }
182  return 0;
183 }
184 
185 #define OFFSET(obj) offsetof(ASTMuxContext, obj)
186 static const AVOption options[] = {
187  { "loopstart", "Loopstart position in milliseconds.", OFFSET(loopstart), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
188  { "loopend", "Loopend position in milliseconds.", OFFSET(loopend), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
189  { NULL },
190 };
191 
192 static const AVClass ast_muxer_class = {
193  .class_name = "AST muxer",
194  .item_name = av_default_item_name,
195  .option = options,
196  .version = LIBAVUTIL_VERSION_INT,
197 };
198 
200  .name = "ast",
201  .long_name = NULL_IF_CONFIG_SMALL("AST (Audio Stream)"),
202  .extensions = "ast",
203  .priv_data_size = sizeof(ASTMuxContext),
204  .audio_codec = AV_CODEC_ID_PCM_S16BE_PLANAR,
205  .video_codec = AV_CODEC_ID_NONE,
209  .priv_class = &ast_muxer_class,
210  .codec_tag = (const AVCodecTag* const []){ff_codec_ast_tags, 0},
211 };
OFFSET
#define OFFSET(obj)
Definition: astenc.c:185
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
AVOutputFormat::name
const char * name
Definition: avformat.h:491
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
ffio_wfourcc
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:58
ASTMuxContext::fbs
int fbs
Definition: astenc.c:35
ast_write_trailer
static int ast_write_trailer(AVFormatContext *s)
Definition: astenc.c:130
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
ASTMuxContext::size
int64_t size
Definition: astenc.c:31
end
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVPacket::data
uint8_t * data
Definition: packet.h:355
AV_CODEC_ID_PCM_S16BE_PLANAR
@ AV_CODEC_ID_PCM_S16BE_PLANAR
Definition: codec_id.h:331
AVOption
AVOption.
Definition: opt.h:246
samples
FFmpeg Automated Testing Environment ************************************Introduction Using FATE from your FFmpeg source directory Submitting the results to the FFmpeg result aggregation server Uploading new samples to the fate suite FATE makefile targets and variables Makefile targets Makefile variables Examples Introduction **************FATE is an extended regression suite on the client side and a means for results aggregation and presentation on the server side The first part of this document explains how you can use FATE from your FFmpeg source directory to test your ffmpeg binary The second part describes how you can run FATE to submit the results to FFmpeg’s FATE server In any way you can have a look at the publicly viewable FATE results by visiting this as it can be seen if some test on some platform broke with their recent contribution This usually happens on the platforms the developers could not test on The second part of this document describes how you can run FATE to submit your results to FFmpeg’s FATE server If you want to submit your results be sure to check that your combination of OS and compiler is not already listed on the above mentioned website In the third part you can find a comprehensive listing of FATE makefile targets and variables Using FATE from your FFmpeg source directory **********************************************If you want to run FATE on your machine you need to have the samples in place You can get the samples via the build target fate rsync Use this command from the top level source this will cause FATE to fail NOTE To use a custom wrapper to run the pass ‘ target exec’ to ‘configure’ or set the TARGET_EXEC Make variable Submitting the results to the FFmpeg result aggregation server ****************************************************************To submit your results to the server you should run fate through the shell script ‘tests fate sh’ from the FFmpeg sources This script needs to be invoked with a configuration file as its first argument tests fate sh path to fate_config A configuration file template with comments describing the individual configuration variables can be found at ‘doc fate_config sh template’ Create a configuration that suits your based on the configuration template The ‘slot’ configuration variable can be any string that is not yet but it is suggested that you name it adhering to the following pattern ‘ARCH OS COMPILER COMPILER VERSION’ The configuration file itself will be sourced in a shell therefore all shell features may be used This enables you to setup the environment as you need it for your build For your first test runs the ‘fate_recv’ variable should be empty or commented out This will run everything as normal except that it will omit the submission of the results to the server The following files should be present in $workdir as specified in the configuration it may help to try out the ‘ssh’ command with one or more ‘ v’ options You should get detailed output concerning your SSH configuration and the authentication process The only thing left is to automate the execution of the fate sh script and the synchronisation of the samples directory Uploading new samples to the fate suite *****************************************If you need a sample uploaded send a mail to samples request This is for developers who have an account on the fate suite server If you upload new samples
Definition: fate.txt:139
mathematics.h
ast.h
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
ff_ast_muxer
AVOutputFormat ff_ast_muxer
Definition: astenc.c:199
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVCodecTag
Definition: internal.h:42
ast_muxer_class
static const AVClass ast_muxer_class
Definition: astenc.c:192
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_OPT_FLAG_ENCODING_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:276
AV_CODEC_ID_ADPCM_AFC
@ AV_CODEC_ID_ADPCM_AFC
Definition: codec_id.h:372
ff_codec_ast_tags
const AVCodecTag ff_codec_ast_tags[]
Definition: ast.c:25
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Definition: opt.h:224
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
ASTMuxContext::loopstart
int64_t loopstart
Definition: astenc.c:33
options
static const AVOption options[]
Definition: astenc.c:186
AVFormatContext
Format I/O context.
Definition: avformat.h:1335
internal.h
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
write_trailer
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:98
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
ASTMuxContext::samples
int64_t samples
Definition: astenc.c:32
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
ASTMuxContext::loopend
int64_t loopend
Definition: astenc.c:34
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
AVPacket::size
int size
Definition: packet.h:356
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:186
size
int size
Definition: twinvq_data.h:11134
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:213
avio_wb32
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:375
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:367
write_packet
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:701
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
AVOutputFormat
Definition: avformat.h:490
avio_internal.h
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:177
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:241
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:72
avformat.h
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
ff_codec_get_tag
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:3148
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:329
avio_wb64
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:441
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:332
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:453
ast_write_header
static int ast_write_header(AVFormatContext *s)
Definition: astenc.c:47
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:346
ast_write_packet
static int ast_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: astenc.c:107
CHECK_LOOP
#define CHECK_LOOP(type)
Definition: astenc.c:38
ASTMuxContext
Definition: astenc.c:29