Libav
nut.c
Go to the documentation of this file.
1 /*
2  * nut
3  * Copyright (c) 2004-2007 Michael Niedermayer
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "libavutil/tree.h"
24 #include "nut.h"
25 #include "riff.h"
26 #include "internal.h"
27 
29  { AV_CODEC_ID_TEXT, MKTAG('U', 'T', 'F', '8') },
30  { AV_CODEC_ID_SSA, MKTAG('S', 'S', 'A', 0) },
31  { AV_CODEC_ID_DVD_SUBTITLE, MKTAG('D', 'V', 'D', 'S') },
32  { AV_CODEC_ID_DVB_SUBTITLE, MKTAG('D', 'V', 'B', 'S') },
33  { AV_CODEC_ID_NONE, 0 }
34 };
35 
37  { AV_CODEC_ID_TEXT, MKTAG('U', 'T', 'F', '8') },
38  { AV_CODEC_ID_NONE, 0 }
39 };
40 
42  { AV_CODEC_ID_VP9, MKTAG('V', 'P', '9', '0') },
43  { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', 15) },
44  { AV_CODEC_ID_RAWVIDEO, MKTAG('B', 'G', 'R', 15) },
45  { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', 16) },
46  { AV_CODEC_ID_RAWVIDEO, MKTAG('B', 'G', 'R', 16) },
47  { AV_CODEC_ID_RAWVIDEO, MKTAG(15, 'B', 'G', 'R') },
48  { AV_CODEC_ID_RAWVIDEO, MKTAG(15, 'R', 'G', 'B') },
49  { AV_CODEC_ID_RAWVIDEO, MKTAG(16, 'B', 'G', 'R') },
50  { AV_CODEC_ID_RAWVIDEO, MKTAG(16, 'R', 'G', 'B') },
51  { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', 12) },
52  { AV_CODEC_ID_RAWVIDEO, MKTAG('B', 'G', 'R', 12) },
53  { AV_CODEC_ID_RAWVIDEO, MKTAG(12, 'B', 'G', 'R') },
54  { AV_CODEC_ID_RAWVIDEO, MKTAG(12, 'R', 'G', 'B') },
55  { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', 'A') },
56  { AV_CODEC_ID_RAWVIDEO, MKTAG('B', 'G', 'R', 'A') },
57  { AV_CODEC_ID_RAWVIDEO, MKTAG('A', 'B', 'G', 'R') },
58  { AV_CODEC_ID_RAWVIDEO, MKTAG('A', 'R', 'G', 'B') },
59  { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', 24) },
60  { AV_CODEC_ID_RAWVIDEO, MKTAG('B', 'G', 'R', 24) },
61  { AV_CODEC_ID_RAWVIDEO, MKTAG('4', '1', '1', 'P') },
62  { AV_CODEC_ID_RAWVIDEO, MKTAG('4', '2', '2', 'P') },
63  { AV_CODEC_ID_RAWVIDEO, MKTAG('4', '2', '2', 'P') },
64  { AV_CODEC_ID_RAWVIDEO, MKTAG('4', '4', '0', 'P') },
65  { AV_CODEC_ID_RAWVIDEO, MKTAG('4', '4', '0', 'P') },
66  { AV_CODEC_ID_RAWVIDEO, MKTAG('4', '4', '4', 'P') },
67  { AV_CODEC_ID_RAWVIDEO, MKTAG('4', '4', '4', 'P') },
68  { AV_CODEC_ID_RAWVIDEO, MKTAG('B', '1', 'W', '0') },
69  { AV_CODEC_ID_RAWVIDEO, MKTAG('B', '0', 'W', '1') },
70  { AV_CODEC_ID_RAWVIDEO, MKTAG('B', 'G', 'R', 8) },
71  { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', 8) },
72  { AV_CODEC_ID_RAWVIDEO, MKTAG('B', 'G', 'R', 4) },
73  { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', 4) },
74  { AV_CODEC_ID_RAWVIDEO, MKTAG('B', '4', 'B', 'Y') },
75  { AV_CODEC_ID_RAWVIDEO, MKTAG('R', '4', 'B', 'Y') },
76  { AV_CODEC_ID_RAWVIDEO, MKTAG('B', 'G', 'R', 48) },
77  { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', 48) },
78  { AV_CODEC_ID_RAWVIDEO, MKTAG(48, 'B', 'G', 'R') },
79  { AV_CODEC_ID_RAWVIDEO, MKTAG(48, 'R', 'G', 'B') },
80  { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'B', 'A', 64 ) },
81  { AV_CODEC_ID_RAWVIDEO, MKTAG('B', 'R', 'A', 64 ) },
82  { AV_CODEC_ID_RAWVIDEO, MKTAG(64 , 'R', 'B', 'A') },
83  { AV_CODEC_ID_RAWVIDEO, MKTAG(64 , 'B', 'R', 'A') },
84  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '3', 11, 10) },
85  { AV_CODEC_ID_RAWVIDEO, MKTAG(10, 11, '3', 'Y') },
86  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '3', 10, 10) },
87  { AV_CODEC_ID_RAWVIDEO, MKTAG(10, 10, '3', 'Y') },
88  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '3', 0, 10) },
89  { AV_CODEC_ID_RAWVIDEO, MKTAG(10, 0, '3', 'Y') },
90  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '1', 0, 16) },
91  { AV_CODEC_ID_RAWVIDEO, MKTAG(16, 0, '1', 'Y') },
92  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '3', 11, 16) },
93  { AV_CODEC_ID_RAWVIDEO, MKTAG(16, 11, '3', 'Y') },
94  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '3', 10, 16) },
95  { AV_CODEC_ID_RAWVIDEO, MKTAG(16, 10, '3', 'Y') },
96  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '3', 0, 16) },
97  { AV_CODEC_ID_RAWVIDEO, MKTAG(16, 0, '3', 'Y') },
98  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', 11, 8) },
99  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '2', 0, 8) },
100 
101  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '1', 0, 9) },
102  { AV_CODEC_ID_RAWVIDEO, MKTAG(9, 0, '1', 'Y') },
103  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', 11, 9) },
104  { AV_CODEC_ID_RAWVIDEO, MKTAG(9, 11, '4', 'Y') },
105  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', 10, 9) },
106  { AV_CODEC_ID_RAWVIDEO, MKTAG(9, 10, '4', 'Y') },
107  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', 0, 9) },
108  { AV_CODEC_ID_RAWVIDEO, MKTAG(9, 0, '4', 'Y') },
109 
110  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '1', 0, 10) },
111  { AV_CODEC_ID_RAWVIDEO, MKTAG(10, 0, '1', 'Y') },
112  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', 11, 10) },
113  { AV_CODEC_ID_RAWVIDEO, MKTAG(10, 11, '4', 'Y') },
114  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', 10, 10) },
115  { AV_CODEC_ID_RAWVIDEO, MKTAG(10, 10, '4', 'Y') },
116  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', 0, 10) },
117  { AV_CODEC_ID_RAWVIDEO, MKTAG(10, 0, '4', 'Y') },
118 
119  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '1', 0, 16) },
120  { AV_CODEC_ID_RAWVIDEO, MKTAG(16, 0, '1', 'Y') },
121  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', 11, 16) },
122  { AV_CODEC_ID_RAWVIDEO, MKTAG(16, 11, '4', 'Y') },
123  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', 10, 16) },
124  { AV_CODEC_ID_RAWVIDEO, MKTAG(16, 10, '4', 'Y') },
125  { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', 0, 16) },
126  { AV_CODEC_ID_RAWVIDEO, MKTAG(16, 0, '4', 'Y') },
127 
128  { AV_CODEC_ID_NONE, 0 }
129 };
130 
132  { AV_CODEC_ID_PCM_ALAW, MKTAG('A', 'L', 'A', 'W') },
133  { AV_CODEC_ID_PCM_MULAW, MKTAG('U', 'L', 'A', 'W') },
134  { AV_CODEC_ID_PCM_F32BE, MKTAG(32, 'D', 'F', 'P') },
135  { AV_CODEC_ID_PCM_F32LE, MKTAG('P', 'F', 'D', 32) },
136  { AV_CODEC_ID_PCM_F64BE, MKTAG(64, 'D', 'F', 'P') },
137  { AV_CODEC_ID_PCM_F64LE, MKTAG('P', 'F', 'D', 64) },
138  { AV_CODEC_ID_PCM_S16BE, MKTAG(16, 'D', 'S', 'P') },
139  { AV_CODEC_ID_PCM_S16LE, MKTAG('P', 'S', 'D', 16) },
140  { AV_CODEC_ID_PCM_S24BE, MKTAG(24, 'D', 'S', 'P') },
141  { AV_CODEC_ID_PCM_S24LE, MKTAG('P', 'S', 'D', 24) },
142  { AV_CODEC_ID_PCM_S32BE, MKTAG(32, 'D', 'S', 'P') },
143  { AV_CODEC_ID_PCM_S32LE, MKTAG('P', 'S', 'D', 32) },
144  { AV_CODEC_ID_PCM_S8, MKTAG('P', 'S', 'D', 8) },
145  { AV_CODEC_ID_PCM_U16BE, MKTAG(16, 'D', 'U', 'P') },
146  { AV_CODEC_ID_PCM_U16LE, MKTAG('P', 'U', 'D', 16) },
147  { AV_CODEC_ID_PCM_U24BE, MKTAG(24, 'D', 'U', 'P') },
148  { AV_CODEC_ID_PCM_U24LE, MKTAG('P', 'U', 'D', 24) },
149  { AV_CODEC_ID_PCM_U32BE, MKTAG(32, 'D', 'U', 'P') },
150  { AV_CODEC_ID_PCM_U32LE, MKTAG('P', 'U', 'D', 32) },
151  { AV_CODEC_ID_PCM_U8, MKTAG('P', 'U', 'D', 8) },
152  { AV_CODEC_ID_PCM_S16BE_PLANAR, MKTAG(16 , 'P', 'S', 'P') },
153  { AV_CODEC_ID_PCM_S16LE_PLANAR, MKTAG('P', 'S', 'P', 16) },
154  { AV_CODEC_ID_PCM_S24LE_PLANAR, MKTAG('P', 'S', 'P', 24) },
155  { AV_CODEC_ID_PCM_S32LE_PLANAR, MKTAG('P', 'S', 'P', 32) },
156  { AV_CODEC_ID_MP3, MKTAG('M', 'P', '3', ' ') },
157  { AV_CODEC_ID_NONE, 0 }
158 };
159 
160 const AVCodecTag * const ff_nut_codec_tags[] = {
163 };
164 
165 void ff_nut_reset_ts(NUTContext *nut, AVRational time_base, int64_t val)
166 {
167  int i;
168  for (i = 0; i < nut->avf->nb_streams; i++)
169  nut->stream[i].last_pts =
170  av_rescale_rnd(val,
171  time_base.num * (int64_t)nut->stream[i].time_base->den,
172  time_base.den * (int64_t)nut->stream[i].time_base->num,
173  AV_ROUND_DOWN);
174 }
175 
176 int64_t ff_lsb2full(StreamContext *stream, int64_t lsb)
177 {
178  int64_t mask = (1 << stream->msb_pts_shift) - 1;
179  int64_t delta = stream->last_pts - mask / 2;
180  return ((lsb - delta) & mask) + delta;
181 }
182 
184 {
185  return ((a->pos - b->pos) >> 32) - ((b->pos - a->pos) >> 32);
186 }
187 
189 {
190  return ((a->ts - b->ts) >> 32) - ((b->ts - a->ts) >> 32);
191 }
192 
193 int ff_nut_add_sp(NUTContext *nut, int64_t pos, int64_t back_ptr, int64_t ts)
194 {
195  Syncpoint *sp = av_mallocz(sizeof(Syncpoint));
196  struct AVTreeNode *node = av_tree_node_alloc();
197 
198  if (!sp || !node) {
199  av_freep(&sp);
200  av_freep(&node);
201  return AVERROR(ENOMEM);
202  }
203 
204  sp->pos = pos;
205  sp->back_ptr = back_ptr;
206  sp->ts = ts;
207  av_tree_insert(&nut->syncpoints, sp, (void *) ff_nut_sp_pos_cmp, &node);
208  if (node) {
209  av_free(sp);
210  av_free(node);
211  }
212 
213  return 0;
214 }
215 
216 static int enu_free(void *opaque, void *elem)
217 {
218  av_free(elem);
219  return 0;
220 }
221 
223 {
226 }
227 
229  { "default", AV_DISPOSITION_DEFAULT },
230  { "dub", AV_DISPOSITION_DUB },
231  { "original", AV_DISPOSITION_ORIGINAL },
232  { "comment", AV_DISPOSITION_COMMENT },
233  { "lyrics", AV_DISPOSITION_LYRICS },
234  { "karaoke", AV_DISPOSITION_KARAOKE },
235  { "", 0 }
236 };
237 
239  { "Author", "artist" },
240  { "X-CreationTime", "date" },
241  { "CreationTime", "date" },
242  { "SourceFilename", "filename" },
243  { "X-Language", "language" },
244  { "X-Disposition", "disposition" },
245  { "X-Replaces", "replaces" },
246  { "X-Depends", "depends" },
247  { "X-Uses", "uses" },
248  { "X-UsesFont", "usesfont" },
249  { 0 },
250 };
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:40
int num
numerator
Definition: rational.h:44
Definition: nut.h:57
#define AV_DISPOSITION_DUB
Definition: avformat.h:673
int64_t ts
Definition: nut.h:61
Definition: nut.h:89
#define AV_DISPOSITION_KARAOKE
Definition: avformat.h:677
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
struct AVTreeNode * av_tree_node_alloc(void)
Allocate an AVTreeNode.
Definition: tree.c:31
float delta
A tree container.
#define b
Definition: input.c:52
const AVMetadataConv ff_nut_metadata_conv[]
Definition: nut.c:238
const AVCodecTag ff_nut_data_tags[]
Definition: nut.c:36
int ff_nut_sp_pos_cmp(const Syncpoint *a, const Syncpoint *b)
Definition: nut.c:183
void av_tree_destroy(AVTreeNode *t)
Definition: tree.c:143
AVFormatContext * avf
Definition: nut.h:91
int64_t last_pts
Definition: nut.h:77
void * av_tree_insert(AVTreeNode **tp, void *key, int(*cmp)(void *key, const void *b), AVTreeNode **next)
Insert or remove an element.
Definition: tree.c:56
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
void ff_nut_free_sp(NUTContext *nut)
Definition: nut.c:222
static const uint16_t mask[17]
Definition: lzw.c:38
#define AVERROR(e)
Definition: error.h:43
uint64_t pos
Definition: nut.h:58
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:479
const AVCodecTag ff_nut_audio_tags[]
Definition: nut.c:131
AVRational * time_base
Definition: nut.h:79
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:374
int ff_nut_sp_pts_cmp(const Syncpoint *a, const Syncpoint *b)
Definition: nut.c:188
#define AV_DISPOSITION_LYRICS
Definition: avformat.h:676
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:996
void ff_nut_reset_ts(NUTContext *nut, AVRational time_base, int64_t val)
Definition: nut.c:165
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:29
int64_t ff_lsb2full(StreamContext *stream, int64_t lsb)
Definition: nut.c:176
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
struct AVTreeNode * syncpoints
Definition: nut.h:105
int msb_pts_shift
Definition: nut.h:80
NULL
Definition: eval.c:55
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:672
const AVCodecTag ff_nut_subtitle_tags[]
Definition: nut.c:28
static int enu_free(void *opaque, void *elem)
Definition: nut.c:216
rational number numerator/denominator
Definition: rational.h:43
StreamContext * stream
Definition: nut.h:98
Round toward -infinity.
Definition: mathematics.h:52
const Dispositions ff_nut_dispositions[]
Definition: nut.c:228
raw UTF-8 text
Definition: avcodec.h:553
const AVCodecTag ff_nut_video_tags[]
Definition: nut.c:41
int ff_nut_add_sp(NUTContext *nut, int64_t pos, int64_t back_ptr, int64_t ts)
Definition: nut.c:193
int den
denominator
Definition: rational.h:45
#define AV_DISPOSITION_COMMENT
Definition: avformat.h:675
uint64_t back_ptr
Definition: nut.h:59
#define MKTAG(a, b, c, d)
Definition: common.h:256
void av_tree_enumerate(AVTreeNode *t, void *opaque, int(*cmp)(void *opaque, void *elem), int(*enu)(void *opaque, void *elem))
Apply enu(opaque, &elem) to all the elements in the tree in a given range.
Definition: tree.c:152
const AVCodecTag *const ff_nut_codec_tags[]
Definition: nut.c:160
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
#define AV_DISPOSITION_ORIGINAL
Definition: avformat.h:674
void * elem
Definition: tree.c:27