FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
aadec.c
Go to the documentation of this file.
1 /*
2  * Audible AA demuxer
3  * Copyright (c) 2015 Vesselin Bontchev
4  *
5  * Header parsing is borrowed from https://github.com/jteeuwen/audible project.
6  * Copyright (c) 2001-2014, Jim Teeuwen
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "avformat.h"
27 #include "internal.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/tea.h"
31 #include "libavutil/opt.h"
32 
33 #define AA_MAGIC 1469084982 /* this identifies an audible .aa file */
34 #define MAX_CODEC_SECOND_SIZE 3982
35 #define MAX_TOC_ENTRIES 16
36 #define MAX_DICTIONARY_ENTRIES 128
37 #define TEA_BLOCK_SIZE 8
38 #define CHAPTER_HEADER_SIZE 8
39 #define TIMEPREC 1000
40 #define MP3_FRAME_SIZE 104
41 
42 typedef struct AADemuxContext {
43  AVClass *class;
49  struct AVTEA *tea_ctx;
52  int64_t content_start;
53  int64_t content_end;
56 
57 static int get_second_size(char *codec_name)
58 {
59  int result = -1;
60 
61  if (!strcmp(codec_name, "mp332")) {
62  result = 3982;
63  } else if (!strcmp(codec_name, "acelp16")) {
64  result = 2000;
65  } else if (!strcmp(codec_name, "acelp85")) {
66  result = 1045;
67  }
68 
69  return result;
70 }
71 
73 {
74  int i, j, idx, largest_idx = -1;
75  uint32_t nkey, nval, toc_size, npairs, header_seed = 0, start;
76  char key[128], val[128], codec_name[64] = {0};
77  uint8_t output[24], dst[8], src[8];
78  int64_t largest_size = -1, current_size = -1, chapter_pos;
79  struct toc_entry {
80  uint32_t offset;
81  uint32_t size;
82  } TOC[MAX_TOC_ENTRIES];
83  uint32_t header_key_part[4];
84  uint8_t header_key[16] = {0};
86  AVIOContext *pb = s->pb;
87  AVStream *st;
88 
89  /* parse .aa header */
90  avio_skip(pb, 4); // file size
91  avio_skip(pb, 4); // magic string
92  toc_size = avio_rb32(pb); // TOC size
93  avio_skip(pb, 4); // unidentified integer
94  if (toc_size > MAX_TOC_ENTRIES)
95  return AVERROR_INVALIDDATA;
96  for (i = 0; i < toc_size; i++) { // read TOC
97  avio_skip(pb, 4); // TOC entry index
98  TOC[i].offset = avio_rb32(pb); // block offset
99  TOC[i].size = avio_rb32(pb); // block size
100  }
101  avio_skip(pb, 24); // header termination block (ignored)
102  npairs = avio_rb32(pb); // read dictionary entries
103  if (npairs > MAX_DICTIONARY_ENTRIES)
104  return AVERROR_INVALIDDATA;
105  for (i = 0; i < npairs; i++) {
106  memset(val, 0, sizeof(val));
107  memset(key, 0, sizeof(key));
108  avio_skip(pb, 1); // unidentified integer
109  nkey = avio_rb32(pb); // key string length
110  nval = avio_rb32(pb); // value string length
111  avio_get_str(pb, nkey, key, sizeof(key));
112  avio_get_str(pb, nval, val, sizeof(val));
113  if (!strcmp(key, "codec")) {
114  av_log(s, AV_LOG_DEBUG, "Codec is <%s>\n", val);
115  strncpy(codec_name, val, sizeof(codec_name) - 1);
116  } else if (!strcmp(key, "HeaderSeed")) {
117  av_log(s, AV_LOG_DEBUG, "HeaderSeed is <%s>\n", val);
118  header_seed = atoi(val);
119  } else if (!strcmp(key, "HeaderKey")) { // this looks like "1234567890 1234567890 1234567890 1234567890"
120  av_log(s, AV_LOG_DEBUG, "HeaderKey is <%s>\n", val);
121  sscanf(val, "%"SCNu32"%"SCNu32"%"SCNu32"%"SCNu32,
122  &header_key_part[0], &header_key_part[1], &header_key_part[2], &header_key_part[3]);
123  for (idx = 0; idx < 4; idx++) {
124  AV_WB32(&header_key[idx * 4], header_key_part[idx]); // convert each part to BE!
125  }
126  av_log(s, AV_LOG_DEBUG, "Processed HeaderKey is ");
127  for (i = 0; i < 16; i++)
128  av_log(s, AV_LOG_DEBUG, "%02x", header_key[i]);
129  av_log(s, AV_LOG_DEBUG, "\n");
130  } else {
131  av_dict_set(&s->metadata, key, val, 0);
132  }
133  }
134 
135  /* verify fixed key */
136  if (c->aa_fixed_key_len != 16) {
137  av_log(s, AV_LOG_ERROR, "aa_fixed_key value needs to be 16 bytes!\n");
138  return AVERROR(EINVAL);
139  }
140 
141  /* verify codec */
142  if ((c->codec_second_size = get_second_size(codec_name)) == -1) {
143  av_log(s, AV_LOG_ERROR, "unknown codec <%s>!\n", codec_name);
144  return AVERROR(EINVAL);
145  }
146 
147  /* decryption key derivation */
148  c->tea_ctx = av_tea_alloc();
149  if (!c->tea_ctx)
150  return AVERROR(ENOMEM);
151  av_tea_init(c->tea_ctx, c->aa_fixed_key, 16);
152  output[0] = output[1] = 0; // purely for padding purposes
153  memcpy(output + 2, header_key, 16);
154  idx = 0;
155  for (i = 0; i < 3; i++) { // TEA CBC with weird mixed endianness
156  AV_WB32(src, header_seed);
157  AV_WB32(src + 4, header_seed + 1);
158  header_seed += 2;
159  av_tea_crypt(c->tea_ctx, dst, src, 1, NULL, 0); // TEA ECB encrypt
160  for (j = 0; j < TEA_BLOCK_SIZE && idx < 18; j+=1, idx+=1) {
161  output[idx] = output[idx] ^ dst[j];
162  }
163  }
164  memcpy(c->file_key, output + 2, 16); // skip first 2 bytes of output
165  av_log(s, AV_LOG_DEBUG, "File key is ");
166  for (i = 0; i < 16; i++)
167  av_log(s, AV_LOG_DEBUG, "%02x", c->file_key[i]);
168  av_log(s, AV_LOG_DEBUG, "\n");
169 
170  /* decoder setup */
171  st = avformat_new_stream(s, NULL);
172  if (!st) {
173  av_freep(&c->tea_ctx);
174  return AVERROR(ENOMEM);
175  }
177  if (!strcmp(codec_name, "mp332")) {
179  st->codecpar->sample_rate = 22050;
181  avpriv_set_pts_info(st, 64, 8, 32000 * TIMEPREC);
182  // encoded audio frame is MP3_FRAME_SIZE bytes (+1 with padding, unlikely)
183  } else if (!strcmp(codec_name, "acelp85")) {
185  st->codecpar->block_align = 19;
186  st->codecpar->channels = 1;
187  st->codecpar->sample_rate = 8500;
188  st->codecpar->bit_rate = 8500;
190  avpriv_set_pts_info(st, 64, 8, 8500 * TIMEPREC);
191  } else if (!strcmp(codec_name, "acelp16")) {
193  st->codecpar->block_align = 20;
194  st->codecpar->channels = 1;
195  st->codecpar->sample_rate = 16000;
196  st->codecpar->bit_rate = 16000;
198  avpriv_set_pts_info(st, 64, 8, 16000 * TIMEPREC);
199  }
200 
201  /* determine, and jump to audio start offset */
202  for (i = 1; i < toc_size; i++) { // skip the first entry!
203  current_size = TOC[i].size;
204  if (current_size > largest_size) {
205  largest_idx = i;
206  largest_size = current_size;
207  }
208  }
209  start = TOC[largest_idx].offset;
210  avio_seek(pb, start, SEEK_SET);
211 
212  // extract chapter positions. since all formats have constant bit rate, use it
213  // as time base in bytes/s, for easy stream position <-> timestamp conversion
214  st->start_time = 0;
215  c->content_start = start;
216  c->content_end = start + largest_size;
217 
218  while ((chapter_pos = avio_tell(pb)) >= 0 && chapter_pos < c->content_end) {
219  int chapter_idx = s->nb_chapters;
220  uint32_t chapter_size = avio_rb32(pb);
221  if (chapter_size == 0) break;
222  chapter_pos -= start + CHAPTER_HEADER_SIZE * chapter_idx;
223  avio_skip(pb, 4 + chapter_size);
224  if (!avpriv_new_chapter(s, chapter_idx, st->time_base,
225  chapter_pos * TIMEPREC, (chapter_pos + chapter_size) * TIMEPREC, NULL))
226  return AVERROR(ENOMEM);
227  }
228 
229  st->duration = (largest_size - CHAPTER_HEADER_SIZE * s->nb_chapters) * TIMEPREC;
230 
231  ff_update_cur_dts(s, st, 0);
232  avio_seek(pb, start, SEEK_SET);
233  c->current_chapter_size = 0;
234  c->seek_offset = 0;
235 
236  return 0;
237 }
238 
240 {
241  uint8_t dst[TEA_BLOCK_SIZE];
243  int i;
244  int trailing_bytes;
245  int blocks;
247  int written = 0;
248  int ret;
249  AADemuxContext *c = s->priv_data;
250  uint64_t pos = avio_tell(s->pb);
251 
252  // are we at the end of the audio content?
253  if (pos >= c->content_end) {
254  return AVERROR_EOF;
255  }
256 
257  // are we at the start of a chapter?
258  if (c->current_chapter_size == 0) {
260  if (c->current_chapter_size == 0) {
261  return AVERROR_EOF;
262  }
263  av_log(s, AV_LOG_DEBUG, "Chapter %d (%" PRId64 " bytes)\n", c->chapter_idx, c->current_chapter_size);
264  c->chapter_idx = c->chapter_idx + 1;
265  avio_skip(s->pb, 4); // data start offset
266  pos += 8;
268  }
269 
270  // is this the last block in this chapter?
273  }
274 
275  // decrypt c->current_codec_second_size bytes
277  for (i = 0; i < blocks; i++) {
278  ret = avio_read(s->pb, src, TEA_BLOCK_SIZE);
279  if (ret != TEA_BLOCK_SIZE)
280  return (ret < 0) ? ret : AVERROR_EOF;
281  av_tea_init(c->tea_ctx, c->file_key, 16);
282  av_tea_crypt(c->tea_ctx, dst, src, 1, NULL, 1);
283  memcpy(buf + written, dst, TEA_BLOCK_SIZE);
284  written = written + TEA_BLOCK_SIZE;
285  }
286  trailing_bytes = c->current_codec_second_size % TEA_BLOCK_SIZE;
287  if (trailing_bytes != 0) { // trailing bytes are left unencrypted!
288  ret = avio_read(s->pb, src, trailing_bytes);
289  if (ret != trailing_bytes)
290  return (ret < 0) ? ret : AVERROR_EOF;
291  memcpy(buf + written, src, trailing_bytes);
292  written = written + trailing_bytes;
293  }
294 
295  // update state
297  if (c->current_chapter_size <= 0)
298  c->current_chapter_size = 0;
299 
300  if (c->seek_offset > written)
301  c->seek_offset = 0; // ignore wrong estimate
302 
303  ret = av_new_packet(pkt, written - c->seek_offset);
304  if (ret < 0)
305  return ret;
306  memcpy(pkt->data, buf + c->seek_offset, written - c->seek_offset);
307  pkt->pos = pos;
308 
309  c->seek_offset = 0;
310  return 0;
311 }
312 
314  int stream_index, int64_t timestamp, int flags)
315 {
316  AADemuxContext *c = s->priv_data;
317  AVChapter *ch;
318  int64_t chapter_pos, chapter_start, chapter_size;
319  int chapter_idx = 0;
320 
321  // find chapter containing seek timestamp
322  if (timestamp < 0)
323  timestamp = 0;
324 
325  while (chapter_idx < s->nb_chapters && timestamp >= s->chapters[chapter_idx]->end) {
326  ++chapter_idx;
327  }
328 
329  if (chapter_idx >= s->nb_chapters) {
330  chapter_idx = s->nb_chapters - 1;
331  if (chapter_idx < 0) return -1; // there is no chapter.
332  timestamp = s->chapters[chapter_idx]->end;
333  }
334 
335  ch = s->chapters[chapter_idx];
336 
337  // sync by clamping timestamp to nearest valid block position in its chapter
338  chapter_size = ch->end / TIMEPREC - ch->start / TIMEPREC;
339  chapter_pos = av_rescale_rnd((timestamp - ch->start) / TIMEPREC,
340  1, c->codec_second_size,
342  * c->codec_second_size;
343  if (chapter_pos >= chapter_size)
344  chapter_pos = chapter_size;
345  chapter_start = c->content_start + (ch->start / TIMEPREC) + CHAPTER_HEADER_SIZE * (1 + chapter_idx);
346 
347  // reinit read state
348  avio_seek(s->pb, chapter_start + chapter_pos, SEEK_SET);
350  c->current_chapter_size = chapter_size - chapter_pos;
351  c->chapter_idx = 1 + chapter_idx;
352 
353  // for unaligned frames, estimate offset of first frame in block (assume no padding)
354  if (s->streams[0]->codecpar->codec_id == AV_CODEC_ID_MP3) {
355  c->seek_offset = (MP3_FRAME_SIZE - chapter_pos % MP3_FRAME_SIZE) % MP3_FRAME_SIZE;
356  }
357 
358  ff_update_cur_dts(s, s->streams[0], ch->start + (chapter_pos + c->seek_offset) * TIMEPREC);
359 
360  return 1;
361 }
362 
363 static int aa_probe(AVProbeData *p)
364 {
365  uint8_t *buf = p->buf;
366 
367  // first 4 bytes are file size, next 4 bytes are the magic
368  if (AV_RB32(buf+4) != AA_MAGIC)
369  return 0;
370 
371  return AVPROBE_SCORE_MAX / 2;
372 }
373 
375 {
376  AADemuxContext *c = s->priv_data;
377 
378  av_freep(&c->tea_ctx);
379 
380  return 0;
381 }
382 
383 #define OFFSET(x) offsetof(AADemuxContext, x)
384 static const AVOption aa_options[] = {
385  { "aa_fixed_key", // extracted from libAAX_SDK.so and AAXSDKWin.dll files!
386  "Fixed key used for handling Audible AA files", OFFSET(aa_fixed_key),
387  AV_OPT_TYPE_BINARY, {.str="77214d4b196a87cd520045fd2a51d673"},
388  .flags = AV_OPT_FLAG_DECODING_PARAM },
389  { NULL },
390 };
391 
392 static const AVClass aa_class = {
393  .class_name = "aa",
394  .item_name = av_default_item_name,
395  .option = aa_options,
396  .version = LIBAVUTIL_VERSION_INT,
397 };
398 
400  .name = "aa",
401  .long_name = NULL_IF_CONFIG_SMALL("Audible AA format files"),
402  .priv_class = &aa_class,
403  .priv_data_size = sizeof(AADemuxContext),
404  .extensions = "aa",
405  .read_probe = aa_probe,
411 };
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1580
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2504
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVOption.
Definition: opt.h:246
int seek_offset
Definition: aadec.c:54
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1465
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:4882
uint8_t file_key[16]
Definition: aadec.c:50
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
#define AA_MAGIC
Definition: aadec.c:33
static int aa_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: aadec.c:239
int64_t content_start
Definition: aadec.c:52
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3900
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
int current_codec_second_size
Definition: aadec.c:47
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
const char * key
static AVPacket pkt
#define src
Definition: vp8dsp.c:254
struct AVTEA * av_tea_alloc(void)
Allocate an AVTEA context To free the struct: av_free(ptr)
Definition: tea.c:35
AVChapter * avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition: utils.c:4583
#define MP3_FRAME_SIZE
Definition: aadec.c:40
Format I/O context.
Definition: avformat.h:1351
void ff_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
Update cur_dts of all streams based on the given timestamp and AVStream.
Definition: utils.c:1953
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
Public dictionary API.
uint8_t
Round toward +infinity.
Definition: mathematics.h:83
static int aa_probe(AVProbeData *p)
Definition: aadec.c:363
AVOptions.
#define MAX_CODEC_SECOND_SIZE
Definition: aadec.c:34
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:801
static const AVOption aa_options[]
Definition: aadec.c:384
enum AVStreamParseType need_parsing
Definition: avformat.h:1092
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4455
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1419
Definition: tea.c:30
uint8_t * data
Definition: avcodec.h:1445
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
AVInputFormat ff_aa_demuxer
Definition: aadec.c:399
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition: avformat.h:797
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:648
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3929
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1591
#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
int64_t content_end
Definition: aadec.c:53
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:559
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3896
static int aa_read_header(AVFormatContext *s)
Definition: aadec.c:72
uint8_t * aa_fixed_key
Definition: aadec.c:44
AVChapter ** chapters
Definition: avformat.h:1581
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
int64_t current_chapter_size
Definition: aadec.c:51
int block_align
Audio only.
Definition: avcodec.h:4017
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
Public header for libavutil TEA algorithm.
void av_tea_crypt(AVTEA *ctx, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt)
Encrypt or decrypt a buffer using a previously initialized context.
Definition: tea.c:95
struct AVTEA * tea_ctx
Definition: aadec.c:49
#define MAX_DICTIONARY_ENTRIES
Definition: aadec.c:36
#define s(width, name)
Definition: cbs_vp9.c:257
#define OFFSET(x)
Definition: aadec.c:383
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
offset must point to a pointer immediately followed by an int for the length
Definition: opt.h:229
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
static int aa_read_close(AVFormatContext *s)
Definition: aadec.c:374
Stream structure.
Definition: avformat.h:874
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:1311
#define TIMEPREC
Definition: aadec.c:39
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
#define AVFMT_NOGENSEARCH
Format does not allow to fall back on generic search.
Definition: avformat.h:476
#define MAX_TOC_ENTRIES
Definition: aadec.c:35
void * buf
Definition: avisynth_c.h:690
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
Describe the class of an AVClass context structure.
Definition: log.h:67
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
#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 get_second_size(char *codec_name)
Definition: aadec.c:57
static const AVClass aa_class
Definition: aadec.c:392
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
Round toward -infinity.
Definition: mathematics.h:82
#define flags(name, subs,...)
Definition: cbs_av1.c:596
int chapter_idx
Definition: aadec.c:48
int64_t start
Definition: avformat.h:1311
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:923
int sample_rate
Audio only.
Definition: avcodec.h:4010
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
Main libavformat public API header.
void av_tea_init(AVTEA *ctx, const uint8_t key[16], int rounds)
Initialize an AVTEA context.
Definition: tea.c:42
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:913
static double c[64]
#define TEA_BLOCK_SIZE
Definition: aadec.c:37
int codec_second_size
Definition: aadec.c:46
#define CHAPTER_HEADER_SIZE
Definition: aadec.c:38
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:477
int aa_fixed_key_len
Definition: aadec.c:45
void * priv_data
Format private data.
Definition: avformat.h:1379
static int aa_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: aadec.c:313
int channels
Audio only.
Definition: avcodec.h:4006
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
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:1021
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:880
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:903
This structure stores compressed data.
Definition: avcodec.h:1422
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56