FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ffmetadec.c
Go to the documentation of this file.
1 /*
2  * Metadata demuxer
3  * Copyright (c) 2010 Anton Khirnov
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 "libavutil/mathematics.h"
23 #include "avformat.h"
24 #include "ffmeta.h"
25 #include "internal.h"
26 #include "libavutil/dict.h"
27 
28 static int probe(AVProbeData *p)
29 {
30  if(!memcmp(p->buf, ID_STRING, strlen(ID_STRING)))
31  return AVPROBE_SCORE_MAX;
32  return 0;
33 }
34 
35 static void get_line(AVIOContext *s, uint8_t *buf, int size)
36 {
37  do {
38  uint8_t c;
39  int i = 0;
40 
41  while ((c = avio_r8(s))) {
42  if (c == '\\') {
43  if (i < size - 1)
44  buf[i++] = c;
45  c = avio_r8(s);
46  } else if (c == '\n')
47  break;
48 
49  if (i < size - 1)
50  buf[i++] = c;
51  }
52  buf[i] = 0;
53  } while (!avio_feof(s) && (buf[0] == ';' || buf[0] == '#' || buf[0] == 0));
54 }
55 
57 {
58  uint8_t line[256];
59  int64_t start, end;
60  AVRational tb = {1, 1e9};
61 
62  get_line(s->pb, line, sizeof(line));
63 
64  if (sscanf(line, "TIMEBASE=%d/%d", &tb.num, &tb.den))
65  get_line(s->pb, line, sizeof(line));
66  if (!sscanf(line, "START=%"SCNd64, &start)) {
67  av_log(s, AV_LOG_ERROR, "Expected chapter start timestamp, found %s.\n", line);
68  start = (s->nb_chapters && s->chapters[s->nb_chapters - 1]->end != AV_NOPTS_VALUE) ?
69  s->chapters[s->nb_chapters - 1]->end : 0;
70  } else
71  get_line(s->pb, line, sizeof(line));
72 
73  if (!sscanf(line, "END=%"SCNd64, &end)) {
74  av_log(s, AV_LOG_ERROR, "Expected chapter end timestamp, found %s.\n", line);
75  end = AV_NOPTS_VALUE;
76  }
77 
78  return avpriv_new_chapter(s, s->nb_chapters, tb, start, end, NULL);
79 }
80 
81 static uint8_t *unescape(const uint8_t *buf, int size)
82 {
83  uint8_t *ret = av_malloc(size + 1);
84  uint8_t *p1 = ret;
85  const uint8_t *p2 = buf;
86 
87  if (!ret)
88  return NULL;
89 
90  while (p2 < buf + size) {
91  if (*p2 == '\\')
92  p2++;
93  *p1++ = *p2++;
94  }
95  *p1 = 0;
96  return ret;
97 }
98 
99 static int read_tag(const uint8_t *line, AVDictionary **m)
100 {
101  uint8_t *key, *value;
102  const uint8_t *p = line;
103 
104  /* find first not escaped '=' */
105  while (1) {
106  if (*p == '=')
107  break;
108  else if (*p == '\\')
109  p++;
110 
111  if (*p++)
112  continue;
113 
114  return 0;
115  }
116 
117  if (!(key = unescape(line, p - line)))
118  return AVERROR(ENOMEM);
119  if (!(value = unescape(p + 1, strlen(p + 1)))) {
120  av_free(key);
121  return AVERROR(ENOMEM);
122  }
123 
125  return 0;
126 }
127 
129 {
130  AVDictionary **m = &s->metadata;
131  uint8_t line[1024];
132 
133  while(!avio_feof(s->pb)) {
134  get_line(s->pb, line, sizeof(line));
135 
136  if (!memcmp(line, ID_STREAM, strlen(ID_STREAM))) {
138 
139  if (!st)
140  return AVERROR(ENOMEM);
141 
144 
145  m = &st->metadata;
146  } else if (!memcmp(line, ID_CHAPTER, strlen(ID_CHAPTER))) {
147  AVChapter *ch = read_chapter(s);
148 
149  if (!ch)
150  return AVERROR(ENOMEM);
151 
152  m = &ch->metadata;
153  } else
154  read_tag(line, m);
155  }
156 
157  s->start_time = 0;
158  if (s->nb_chapters)
159  s->duration = av_rescale_q(s->chapters[s->nb_chapters - 1]->end,
160  s->chapters[s->nb_chapters - 1]->time_base,
162 
163  return 0;
164 }
165 
167 {
168  return AVERROR_EOF;
169 }
170 
172  .name = "ffmetadata",
173  .long_name = NULL_IF_CONFIG_SMALL("FFmpeg metadata in text"),
174  .read_probe = probe,
175  .read_header = read_header,
176  .read_packet = read_packet,
177 };
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1543
static void get_line(AVIOContext *s, uint8_t *buf, int size)
Definition: ffmetadec.c:35
#define ID_CHAPTER
Definition: ffmeta.h:26
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:147
static int read_header(AVFormatContext *s)
Definition: ffmetadec.c:128
Dummy codec for streams containing only metadata information.
Definition: avcodec.h:651
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ffmetadec.c:166
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3981
int num
Numerator.
Definition: rational.h:59
static AVPacket pkt
AVDictionary * metadata
Definition: avformat.h:1299
static int probe(AVProbeData *p)
Definition: ffmetadec.c:28
static int read_tag(const uint8_t *line, AVDictionary **m)
Definition: ffmetadec.c:99
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that's been allocated with av_malloc() or another memory allocation function...
Definition: dict.h:73
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:4335
Format I/O context.
Definition: avformat.h:1338
Public dictionary API.
static uint8_t * unescape(const uint8_t *buf, int size)
Definition: ffmetadec.c:81
uint8_t
#define av_malloc(s)
Opaque data information usually continuous.
Definition: avutil.h:197
#define ID_STRING
Definition: ffmeta.h:25
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4211
#define AVERROR_EOF
End of file.
Definition: error.h:55
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
#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:1554
#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:176
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3977
AVChapter ** chapters
Definition: avformat.h:1544
Definition: graph2dot.c:48
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:595
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:76
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
#define ID_STREAM
Definition: ffmeta.h:27
AVDictionary * metadata
Definition: avformat.h:958
Stream structure.
Definition: avformat.h:889
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:1298
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:254
AVIOContext * pb
I/O context.
Definition: avformat.h:1380
void * buf
Definition: avisynth_c.h:690
AVInputFormat ff_ffmetadata_demuxer
Definition: ffmetadec.c:171
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
Rational number (pair of numerator and denominator).
Definition: rational.h:58
This structure contains the data a format has to probe a file.
Definition: avformat.h:461
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds. ...
Definition: avformat.h:1423
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:473
Main libavformat public API header.
static AVChapter * read_chapter(AVFormatContext *s)
Definition: ffmetadec.c:56
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:1297
int den
Denominator.
Definition: rational.h:60
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_FM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LT), 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_YASM &&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
#define av_free(p)
static AVCodec * c
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1433
void INT64 start
Definition: avisynth_c.h:690
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
AVCodecParameters * codecpar
Definition: avformat.h:1241
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:328
This structure stores compressed data.
Definition: avcodec.h:1578
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:242
#define tb
Definition: regdef.h:68