/src/ffmpeg/libavformat/avformat.c
Line | Count | Source |
1 | | /* |
2 | | * Various functions used by both muxers and demuxers |
3 | | * Copyright (c) 2000, 2001, 2002 Fabrice Bellard |
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 <math.h> |
23 | | #include "libavutil/avassert.h" |
24 | | #include "libavutil/avstring.h" |
25 | | #include "libavutil/channel_layout.h" |
26 | | #include "libavutil/frame.h" |
27 | | #include "libavutil/iamf.h" |
28 | | #include "libavutil/intreadwrite.h" |
29 | | #include "libavutil/mem.h" |
30 | | #include "libavutil/opt.h" |
31 | | #include "libavutil/pixfmt.h" |
32 | | #include "libavutil/samplefmt.h" |
33 | | #include "libavcodec/avcodec.h" |
34 | | #include "libavcodec/codec.h" |
35 | | #include "libavcodec/bsf.h" |
36 | | #include "libavcodec/codec_desc.h" |
37 | | #include "libavcodec/packet_internal.h" |
38 | | #include "avformat.h" |
39 | | #include "avformat_internal.h" |
40 | | #include "avio.h" |
41 | | #include "demux.h" |
42 | | #include "mux.h" |
43 | | #include "internal.h" |
44 | | |
45 | | void ff_free_stream(AVStream **pst) |
46 | 0 | { |
47 | 0 | AVStream *st = *pst; |
48 | 0 | FFStream *const sti = ffstream(st); |
49 | |
|
50 | 0 | if (!st) |
51 | 0 | return; |
52 | | |
53 | 0 | if (st->attached_pic.data) |
54 | 0 | av_packet_unref(&st->attached_pic); |
55 | |
|
56 | 0 | av_parser_close(sti->parser); |
57 | 0 | avcodec_free_context(&sti->avctx); |
58 | 0 | av_bsf_free(&sti->bsfc); |
59 | 0 | av_freep(&sti->index_entries); |
60 | 0 | av_freep(&sti->probe_data.buf); |
61 | |
|
62 | 0 | av_packet_free(&sti->parse_pkt); |
63 | |
|
64 | 0 | av_bsf_free(&sti->extract_extradata.bsf); |
65 | |
|
66 | 0 | if (sti->info) { |
67 | 0 | av_freep(&sti->info->duration_error); |
68 | 0 | av_freep(&sti->info); |
69 | 0 | } |
70 | |
|
71 | 0 | av_dict_free(&st->metadata); |
72 | 0 | avcodec_parameters_free(&st->codecpar); |
73 | 0 | av_freep(&st->priv_data); |
74 | |
|
75 | 0 | av_freep(pst); |
76 | 0 | } |
77 | | |
78 | | void ff_free_stream_group(AVStreamGroup **pstg) |
79 | 0 | { |
80 | 0 | AVStreamGroup *stg = *pstg; |
81 | |
|
82 | 0 | if (!stg) |
83 | 0 | return; |
84 | | |
85 | 0 | av_freep(&stg->streams); |
86 | 0 | av_dict_free(&stg->metadata); |
87 | 0 | av_freep(&stg->priv_data); |
88 | 0 | switch (stg->type) { |
89 | 0 | case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT: { |
90 | 0 | av_iamf_audio_element_free(&stg->params.iamf_audio_element); |
91 | 0 | break; |
92 | 0 | } |
93 | 0 | case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION: { |
94 | 0 | av_iamf_mix_presentation_free(&stg->params.iamf_mix_presentation); |
95 | 0 | break; |
96 | 0 | } |
97 | 0 | case AV_STREAM_GROUP_PARAMS_TILE_GRID: |
98 | 0 | av_opt_free(stg->params.tile_grid); |
99 | 0 | av_freep(&stg->params.tile_grid->offsets); |
100 | 0 | av_packet_side_data_free(&stg->params.tile_grid->coded_side_data, |
101 | 0 | &stg->params.tile_grid->nb_coded_side_data); |
102 | 0 | av_freep(&stg->params.tile_grid); |
103 | 0 | break; |
104 | 0 | case AV_STREAM_GROUP_PARAMS_LCEVC: |
105 | 0 | av_opt_free(stg->params.lcevc); |
106 | 0 | av_freep(&stg->params.lcevc); |
107 | 0 | break; |
108 | 0 | default: |
109 | 0 | break; |
110 | 0 | } |
111 | | |
112 | 0 | av_freep(pstg); |
113 | 0 | } |
114 | | |
115 | | void ff_remove_stream(AVFormatContext *s, AVStream *st) |
116 | 0 | { |
117 | 0 | av_assert0(s->nb_streams>0); |
118 | 0 | av_assert0(s->streams[ s->nb_streams - 1 ] == st); |
119 | | |
120 | 0 | ff_free_stream(&s->streams[ --s->nb_streams ]); |
121 | 0 | } |
122 | | |
123 | | void ff_remove_stream_group(AVFormatContext *s, AVStreamGroup *stg) |
124 | 0 | { |
125 | 0 | av_assert0(s->nb_stream_groups > 0); |
126 | 0 | av_assert0(s->stream_groups[ s->nb_stream_groups - 1 ] == stg); |
127 | | |
128 | 0 | ff_free_stream_group(&s->stream_groups[ --s->nb_stream_groups ]); |
129 | 0 | } |
130 | | |
131 | | /* XXX: suppress the packet queue */ |
132 | | void ff_flush_packet_queue(AVFormatContext *s) |
133 | 0 | { |
134 | 0 | FormatContextInternal *const fci = ff_fc_internal(s); |
135 | 0 | FFFormatContext *const si = &fci->fc; |
136 | 0 | avpriv_packet_list_free(&fci->parse_queue); |
137 | 0 | avpriv_packet_list_free(&si->packet_buffer); |
138 | 0 | avpriv_packet_list_free(&fci->raw_packet_buffer); |
139 | |
|
140 | 0 | fci->raw_packet_buffer_size = 0; |
141 | 0 | } |
142 | | |
143 | | void avformat_free_context(AVFormatContext *s) |
144 | 1.36k | { |
145 | 1.36k | FormatContextInternal *fci; |
146 | 1.36k | FFFormatContext *si; |
147 | | |
148 | 1.36k | if (!s) |
149 | 0 | return; |
150 | 1.36k | fci = ff_fc_internal(s); |
151 | 1.36k | si = &fci->fc; |
152 | | |
153 | 1.36k | if (s->oformat && ffofmt(s->oformat)->deinit && fci->initialized) |
154 | 0 | ffofmt(s->oformat)->deinit(s); |
155 | | |
156 | 1.36k | av_opt_free(s); |
157 | 1.36k | if (s->iformat && s->iformat->priv_class && s->priv_data) |
158 | 0 | av_opt_free(s->priv_data); |
159 | 1.36k | if (s->oformat && s->oformat->priv_class && s->priv_data) |
160 | 0 | av_opt_free(s->priv_data); |
161 | | |
162 | 1.36k | for (unsigned i = 0; i < s->nb_streams; i++) |
163 | 0 | ff_free_stream(&s->streams[i]); |
164 | 1.36k | for (unsigned i = 0; i < s->nb_stream_groups; i++) |
165 | 0 | ff_free_stream_group(&s->stream_groups[i]); |
166 | 1.36k | s->nb_stream_groups = 0; |
167 | 1.36k | s->nb_streams = 0; |
168 | | |
169 | 1.36k | for (unsigned i = 0; i < s->nb_programs; i++) { |
170 | 0 | av_dict_free(&s->programs[i]->metadata); |
171 | 0 | av_freep(&s->programs[i]->stream_index); |
172 | 0 | av_freep(&s->programs[i]); |
173 | 0 | } |
174 | 1.36k | s->nb_programs = 0; |
175 | | |
176 | 1.36k | av_freep(&s->programs); |
177 | 1.36k | av_freep(&s->priv_data); |
178 | 1.36k | while (s->nb_chapters--) { |
179 | 0 | av_dict_free(&s->chapters[s->nb_chapters]->metadata); |
180 | 0 | av_freep(&s->chapters[s->nb_chapters]); |
181 | 0 | } |
182 | 1.36k | av_freep(&s->chapters); |
183 | 1.36k | av_dict_free(&s->metadata); |
184 | 1.36k | av_dict_free(&si->id3v2_meta); |
185 | 1.36k | av_packet_free(&si->pkt); |
186 | 1.36k | av_packet_free(&si->parse_pkt); |
187 | 1.36k | avpriv_packet_list_free(&si->packet_buffer); |
188 | 1.36k | av_freep(&s->streams); |
189 | 1.36k | av_freep(&s->stream_groups); |
190 | 1.36k | if (s->iformat) |
191 | 0 | ff_flush_packet_queue(s); |
192 | 1.36k | av_freep(&s->url); |
193 | 1.36k | av_free(s); |
194 | 1.36k | } |
195 | | |
196 | | /** |
197 | | * Copy all stream parameters from source to destination stream, with the |
198 | | * exception of the index field, which is usually set by avformat_new_stream(). |
199 | | * |
200 | | * @param dst pointer to destination AVStream |
201 | | * @param src pointer to source AVStream |
202 | | * @return >=0 on success, AVERROR code on error |
203 | | */ |
204 | | static int stream_params_copy(AVStream *dst, const AVStream *src) |
205 | 0 | { |
206 | 0 | int ret; |
207 | |
|
208 | 0 | dst->id = src->id; |
209 | 0 | dst->time_base = src->time_base; |
210 | 0 | dst->start_time = src->start_time; |
211 | 0 | dst->duration = src->duration; |
212 | 0 | dst->nb_frames = src->nb_frames; |
213 | 0 | dst->disposition = src->disposition; |
214 | 0 | dst->discard = src->discard; |
215 | 0 | dst->sample_aspect_ratio = src->sample_aspect_ratio; |
216 | 0 | dst->avg_frame_rate = src->avg_frame_rate; |
217 | 0 | dst->event_flags = src->event_flags; |
218 | 0 | dst->r_frame_rate = src->r_frame_rate; |
219 | 0 | dst->pts_wrap_bits = src->pts_wrap_bits; |
220 | |
|
221 | 0 | av_dict_free(&dst->metadata); |
222 | 0 | ret = av_dict_copy(&dst->metadata, src->metadata, 0); |
223 | 0 | if (ret < 0) |
224 | 0 | return ret; |
225 | | |
226 | 0 | ret = avcodec_parameters_copy(dst->codecpar, src->codecpar); |
227 | 0 | if (ret < 0) |
228 | 0 | return ret; |
229 | | |
230 | 0 | av_packet_unref(&dst->attached_pic); |
231 | 0 | if (src->attached_pic.data) { |
232 | 0 | ret = av_packet_ref(&dst->attached_pic, &src->attached_pic); |
233 | 0 | if (ret < 0) |
234 | 0 | return ret; |
235 | 0 | } |
236 | | |
237 | 0 | return 0; |
238 | 0 | } |
239 | | |
240 | | AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src) |
241 | 0 | { |
242 | 0 | AVStream *st; |
243 | 0 | int ret; |
244 | |
|
245 | 0 | st = avformat_new_stream(dst_ctx, NULL); |
246 | 0 | if (!st) |
247 | 0 | return NULL; |
248 | | |
249 | 0 | ret = stream_params_copy(st, src); |
250 | 0 | if (ret < 0) { |
251 | 0 | ff_remove_stream(dst_ctx, st); |
252 | 0 | return NULL; |
253 | 0 | } |
254 | | |
255 | 0 | return st; |
256 | 0 | } |
257 | | |
258 | | const char *avformat_stream_group_name(enum AVStreamGroupParamsType type) |
259 | 0 | { |
260 | 0 | switch(type) { |
261 | 0 | case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT: return "IAMF Audio Element"; |
262 | 0 | case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION: return "IAMF Mix Presentation"; |
263 | 0 | case AV_STREAM_GROUP_PARAMS_TILE_GRID: return "Tile Grid"; |
264 | 0 | case AV_STREAM_GROUP_PARAMS_LCEVC: return "LCEVC (Split video and enhancement)"; |
265 | 0 | } |
266 | 0 | return NULL; |
267 | 0 | } |
268 | | |
269 | | AVProgram *av_new_program(AVFormatContext *ac, int id) |
270 | 0 | { |
271 | 0 | AVProgram *program = NULL; |
272 | 0 | int ret; |
273 | |
|
274 | 0 | av_log(ac, AV_LOG_TRACE, "new_program: id=0x%04x\n", id); |
275 | |
|
276 | 0 | for (unsigned i = 0; i < ac->nb_programs; i++) |
277 | 0 | if (ac->programs[i]->id == id) |
278 | 0 | program = ac->programs[i]; |
279 | |
|
280 | 0 | if (!program) { |
281 | 0 | program = av_mallocz(sizeof(*program)); |
282 | 0 | if (!program) |
283 | 0 | return NULL; |
284 | 0 | ret = av_dynarray_add_nofree(&ac->programs, &ac->nb_programs, program); |
285 | 0 | if (ret < 0) { |
286 | 0 | av_free(program); |
287 | 0 | return NULL; |
288 | 0 | } |
289 | 0 | program->discard = AVDISCARD_NONE; |
290 | 0 | program->pmt_version = -1; |
291 | 0 | program->id = id; |
292 | 0 | program->pts_wrap_reference = AV_NOPTS_VALUE; |
293 | 0 | program->pts_wrap_behavior = AV_PTS_WRAP_IGNORE; |
294 | 0 | program->start_time = |
295 | 0 | program->end_time = AV_NOPTS_VALUE; |
296 | 0 | } |
297 | 0 | return program; |
298 | 0 | } |
299 | | |
300 | | void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx) |
301 | 0 | { |
302 | 0 | AVProgram *program = NULL; |
303 | 0 | void *tmp; |
304 | |
|
305 | 0 | if (idx >= ac->nb_streams) { |
306 | 0 | av_log(ac, AV_LOG_ERROR, "stream index %d is not valid\n", idx); |
307 | 0 | return; |
308 | 0 | } |
309 | | |
310 | 0 | for (unsigned i = 0; i < ac->nb_programs; i++) { |
311 | 0 | if (ac->programs[i]->id != progid) |
312 | 0 | continue; |
313 | 0 | program = ac->programs[i]; |
314 | 0 | for (unsigned j = 0; j < program->nb_stream_indexes; j++) |
315 | 0 | if (program->stream_index[j] == idx) |
316 | 0 | return; |
317 | | |
318 | 0 | tmp = av_realloc_array(program->stream_index, program->nb_stream_indexes+1, sizeof(unsigned int)); |
319 | 0 | if (!tmp) |
320 | 0 | return; |
321 | 0 | program->stream_index = tmp; |
322 | 0 | program->stream_index[program->nb_stream_indexes++] = idx; |
323 | 0 | return; |
324 | 0 | } |
325 | 0 | } |
326 | | |
327 | | AVProgram *av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s) |
328 | 0 | { |
329 | 0 | for (unsigned i = 0; i < ic->nb_programs; i++) { |
330 | 0 | if (ic->programs[i] == last) { |
331 | 0 | last = NULL; |
332 | 0 | } else { |
333 | 0 | if (!last) |
334 | 0 | for (unsigned j = 0; j < ic->programs[i]->nb_stream_indexes; j++) |
335 | 0 | if (ic->programs[i]->stream_index[j] == s) |
336 | 0 | return ic->programs[i]; |
337 | 0 | } |
338 | 0 | } |
339 | 0 | return NULL; |
340 | 0 | } |
341 | | |
342 | | int av_find_default_stream_index(AVFormatContext *s) |
343 | 0 | { |
344 | 0 | int best_stream = 0; |
345 | 0 | int best_score = INT_MIN; |
346 | |
|
347 | 0 | if (s->nb_streams <= 0) |
348 | 0 | return -1; |
349 | 0 | for (unsigned i = 0; i < s->nb_streams; i++) { |
350 | 0 | const AVStream *const st = s->streams[i]; |
351 | 0 | const FFStream *const sti = cffstream(st); |
352 | 0 | int score = 0; |
353 | 0 | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { |
354 | 0 | if (st->disposition & AV_DISPOSITION_ATTACHED_PIC) |
355 | 0 | score -= 400; |
356 | 0 | if (st->codecpar->width && st->codecpar->height) |
357 | 0 | score += 50; |
358 | 0 | score+= 25; |
359 | 0 | } |
360 | 0 | if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { |
361 | 0 | if (st->codecpar->sample_rate) |
362 | 0 | score += 50; |
363 | 0 | } |
364 | 0 | if (sti->codec_info_nb_frames) |
365 | 0 | score += 12; |
366 | |
|
367 | 0 | if (st->discard != AVDISCARD_ALL) |
368 | 0 | score += 200; |
369 | |
|
370 | 0 | if (score > best_score) { |
371 | 0 | best_score = score; |
372 | 0 | best_stream = i; |
373 | 0 | } |
374 | 0 | } |
375 | 0 | return best_stream; |
376 | 0 | } |
377 | | |
378 | | int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type, |
379 | | int wanted_stream_nb, int related_stream, |
380 | | const AVCodec **decoder_ret, int flags) |
381 | 0 | { |
382 | 0 | int nb_streams = ic->nb_streams; |
383 | 0 | int ret = AVERROR_STREAM_NOT_FOUND; |
384 | 0 | int best_count = -1, best_multiframe = -1, best_disposition = -1; |
385 | 0 | int count, multiframe, disposition; |
386 | 0 | int64_t best_bitrate = -1; |
387 | 0 | int64_t bitrate; |
388 | 0 | unsigned *program = NULL; |
389 | 0 | const AVCodec *decoder = NULL, *best_decoder = NULL; |
390 | |
|
391 | 0 | if (related_stream >= 0 && wanted_stream_nb < 0) { |
392 | 0 | AVProgram *p = av_find_program_from_stream(ic, NULL, related_stream); |
393 | 0 | if (p) { |
394 | 0 | program = p->stream_index; |
395 | 0 | nb_streams = p->nb_stream_indexes; |
396 | 0 | } |
397 | 0 | } |
398 | 0 | for (unsigned i = 0; i < nb_streams; i++) { |
399 | 0 | int real_stream_index = program ? program[i] : i; |
400 | 0 | AVStream *st = ic->streams[real_stream_index]; |
401 | 0 | AVCodecParameters *par = st->codecpar; |
402 | 0 | if (par->codec_type != type) |
403 | 0 | continue; |
404 | 0 | if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb) |
405 | 0 | continue; |
406 | 0 | if (type == AVMEDIA_TYPE_AUDIO && !(par->ch_layout.nb_channels && par->sample_rate)) |
407 | 0 | continue; |
408 | 0 | if (decoder_ret) { |
409 | 0 | decoder = ff_find_decoder(ic, st, par->codec_id); |
410 | 0 | if (!decoder) { |
411 | 0 | if (ret < 0) |
412 | 0 | ret = AVERROR_DECODER_NOT_FOUND; |
413 | 0 | continue; |
414 | 0 | } |
415 | 0 | } |
416 | 0 | disposition = !(st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED | AV_DISPOSITION_VISUAL_IMPAIRED)) |
417 | 0 | + !! (st->disposition & AV_DISPOSITION_DEFAULT); |
418 | 0 | count = ffstream(st)->codec_info_nb_frames; |
419 | 0 | bitrate = par->bit_rate; |
420 | 0 | multiframe = FFMIN(5, count); |
421 | 0 | if ((best_disposition > disposition) || |
422 | 0 | (best_disposition == disposition && best_multiframe > multiframe) || |
423 | 0 | (best_disposition == disposition && best_multiframe == multiframe && best_bitrate > bitrate) || |
424 | 0 | (best_disposition == disposition && best_multiframe == multiframe && best_bitrate == bitrate && best_count >= count)) |
425 | 0 | continue; |
426 | 0 | best_disposition = disposition; |
427 | 0 | best_count = count; |
428 | 0 | best_bitrate = bitrate; |
429 | 0 | best_multiframe = multiframe; |
430 | 0 | ret = real_stream_index; |
431 | 0 | best_decoder = decoder; |
432 | 0 | if (program && i == nb_streams - 1 && ret < 0) { |
433 | 0 | program = NULL; |
434 | 0 | nb_streams = ic->nb_streams; |
435 | | /* no related stream found, try again with everything */ |
436 | 0 | i = 0; |
437 | 0 | } |
438 | 0 | } |
439 | 0 | if (decoder_ret) |
440 | 0 | *decoder_ret = best_decoder; |
441 | 0 | return ret; |
442 | 0 | } |
443 | | |
444 | | /** |
445 | | * Matches a stream specifier (but ignores requested index). |
446 | | * |
447 | | * @param indexptr set to point to the requested stream index if there is one |
448 | | * |
449 | | * @return <0 on error |
450 | | * 0 if st is NOT a matching stream |
451 | | * >0 if st is a matching stream |
452 | | */ |
453 | | static int match_stream_specifier(const AVFormatContext *s, const AVStream *st, |
454 | | const char *spec, const char **indexptr, |
455 | | const AVStreamGroup **g, const AVProgram **p) |
456 | 0 | { |
457 | 0 | int match = 1; /* Stores if the specifier matches so far. */ |
458 | 0 | while (*spec) { |
459 | 0 | if (*spec <= '9' && *spec >= '0') { /* opt:index */ |
460 | 0 | if (indexptr) |
461 | 0 | *indexptr = spec; |
462 | 0 | return match; |
463 | 0 | } else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' || |
464 | 0 | *spec == 't' || *spec == 'V') { /* opt:[vasdtV] */ |
465 | 0 | enum AVMediaType type; |
466 | 0 | int nopic = 0; |
467 | |
|
468 | 0 | switch (*spec++) { |
469 | 0 | case 'v': type = AVMEDIA_TYPE_VIDEO; break; |
470 | 0 | case 'a': type = AVMEDIA_TYPE_AUDIO; break; |
471 | 0 | case 's': type = AVMEDIA_TYPE_SUBTITLE; break; |
472 | 0 | case 'd': type = AVMEDIA_TYPE_DATA; break; |
473 | 0 | case 't': type = AVMEDIA_TYPE_ATTACHMENT; break; |
474 | 0 | case 'V': type = AVMEDIA_TYPE_VIDEO; nopic = 1; break; |
475 | 0 | default: av_assert0(0); |
476 | 0 | } |
477 | 0 | if (*spec && *spec++ != ':') /* If we are not at the end, then another specifier must follow. */ |
478 | 0 | return AVERROR(EINVAL); |
479 | | |
480 | 0 | if (type != st->codecpar->codec_type) |
481 | 0 | match = 0; |
482 | 0 | if (nopic && (st->disposition & AV_DISPOSITION_ATTACHED_PIC)) |
483 | 0 | match = 0; |
484 | 0 | } else if (*spec == 'g' && *(spec + 1) == ':') { |
485 | 0 | int64_t group_idx = -1, group_id = -1; |
486 | 0 | int found = 0; |
487 | 0 | char *endptr; |
488 | 0 | spec += 2; |
489 | 0 | if (*spec == '#' || (*spec == 'i' && *(spec + 1) == ':')) { |
490 | 0 | spec += 1 + (*spec == 'i'); |
491 | 0 | group_id = strtol(spec, &endptr, 0); |
492 | 0 | if (spec == endptr || (*endptr && *endptr++ != ':')) |
493 | 0 | return AVERROR(EINVAL); |
494 | 0 | spec = endptr; |
495 | 0 | } else { |
496 | 0 | group_idx = strtol(spec, &endptr, 0); |
497 | | /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */ |
498 | 0 | if (spec == endptr || (*endptr && *endptr++ != ':')) |
499 | 0 | return AVERROR(EINVAL); |
500 | 0 | spec = endptr; |
501 | 0 | } |
502 | 0 | if (match) { |
503 | 0 | if (group_id > 0) { |
504 | 0 | for (unsigned i = 0; i < s->nb_stream_groups; i++) { |
505 | 0 | if (group_id == s->stream_groups[i]->id) { |
506 | 0 | group_idx = i; |
507 | 0 | break; |
508 | 0 | } |
509 | 0 | } |
510 | 0 | } |
511 | 0 | if (group_idx < 0 || group_idx >= s->nb_stream_groups) |
512 | 0 | return AVERROR(EINVAL); |
513 | 0 | for (unsigned j = 0; j < s->stream_groups[group_idx]->nb_streams; j++) { |
514 | 0 | if (st->index == s->stream_groups[group_idx]->streams[j]->index) { |
515 | 0 | found = 1; |
516 | 0 | if (g) |
517 | 0 | *g = s->stream_groups[group_idx]; |
518 | 0 | break; |
519 | 0 | } |
520 | 0 | } |
521 | 0 | } |
522 | 0 | if (!found) |
523 | 0 | match = 0; |
524 | 0 | } else if (*spec == 'p' && *(spec + 1) == ':') { |
525 | 0 | int prog_id; |
526 | 0 | int found = 0; |
527 | 0 | char *endptr; |
528 | 0 | spec += 2; |
529 | 0 | prog_id = strtol(spec, &endptr, 0); |
530 | | /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */ |
531 | 0 | if (spec == endptr || (*endptr && *endptr++ != ':')) |
532 | 0 | return AVERROR(EINVAL); |
533 | 0 | spec = endptr; |
534 | 0 | if (match) { |
535 | 0 | for (unsigned i = 0; i < s->nb_programs; i++) { |
536 | 0 | if (s->programs[i]->id != prog_id) |
537 | 0 | continue; |
538 | | |
539 | 0 | for (unsigned j = 0; j < s->programs[i]->nb_stream_indexes; j++) { |
540 | 0 | if (st->index == s->programs[i]->stream_index[j]) { |
541 | 0 | found = 1; |
542 | 0 | if (p) |
543 | 0 | *p = s->programs[i]; |
544 | 0 | i = s->nb_programs; |
545 | 0 | break; |
546 | 0 | } |
547 | 0 | } |
548 | 0 | } |
549 | 0 | } |
550 | 0 | if (!found) |
551 | 0 | match = 0; |
552 | 0 | } else if (*spec == '#' || |
553 | 0 | (*spec == 'i' && *(spec + 1) == ':')) { |
554 | 0 | int stream_id; |
555 | 0 | char *endptr; |
556 | 0 | spec += 1 + (*spec == 'i'); |
557 | 0 | stream_id = strtol(spec, &endptr, 0); |
558 | 0 | if (spec == endptr || *endptr) /* Disallow empty id and make sure we are at the end. */ |
559 | 0 | return AVERROR(EINVAL); |
560 | 0 | return match && (stream_id == st->id); |
561 | 0 | } else if (*spec == 'm' && *(spec + 1) == ':') { |
562 | 0 | const AVDictionaryEntry *tag; |
563 | 0 | char *key, *val; |
564 | 0 | int ret; |
565 | |
|
566 | 0 | if (match) { |
567 | 0 | spec += 2; |
568 | 0 | val = strchr(spec, ':'); |
569 | |
|
570 | 0 | key = val ? av_strndup(spec, val - spec) : av_strdup(spec); |
571 | 0 | if (!key) |
572 | 0 | return AVERROR(ENOMEM); |
573 | | |
574 | 0 | tag = av_dict_get(st->metadata, key, NULL, 0); |
575 | 0 | if (tag) { |
576 | 0 | if (!val || !strcmp(tag->value, val + 1)) |
577 | 0 | ret = 1; |
578 | 0 | else |
579 | 0 | ret = 0; |
580 | 0 | } else |
581 | 0 | ret = 0; |
582 | |
|
583 | 0 | av_freep(&key); |
584 | 0 | } |
585 | 0 | return match && ret; |
586 | 0 | } else if (*spec == 'u' && *(spec + 1) == '\0') { |
587 | 0 | const AVCodecParameters *par = st->codecpar; |
588 | 0 | int val; |
589 | 0 | switch (par->codec_type) { |
590 | 0 | case AVMEDIA_TYPE_AUDIO: |
591 | 0 | val = par->sample_rate && par->ch_layout.nb_channels; |
592 | 0 | if (par->format == AV_SAMPLE_FMT_NONE) |
593 | 0 | return 0; |
594 | 0 | break; |
595 | 0 | case AVMEDIA_TYPE_VIDEO: |
596 | 0 | val = par->width && par->height; |
597 | 0 | if (par->format == AV_PIX_FMT_NONE) |
598 | 0 | return 0; |
599 | 0 | break; |
600 | 0 | case AVMEDIA_TYPE_UNKNOWN: |
601 | 0 | val = 0; |
602 | 0 | break; |
603 | 0 | default: |
604 | 0 | val = 1; |
605 | 0 | break; |
606 | 0 | } |
607 | 0 | return match && (par->codec_id != AV_CODEC_ID_NONE && val != 0); |
608 | 0 | } else { |
609 | 0 | return AVERROR(EINVAL); |
610 | 0 | } |
611 | 0 | } |
612 | | |
613 | 0 | return match; |
614 | 0 | } |
615 | | |
616 | | int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st, |
617 | | const char *spec) |
618 | 0 | { |
619 | 0 | int ret, index; |
620 | 0 | char *endptr; |
621 | 0 | const char *indexptr = NULL; |
622 | 0 | const AVStreamGroup *g = NULL; |
623 | 0 | const AVProgram *p = NULL; |
624 | 0 | int nb_streams; |
625 | |
|
626 | 0 | ret = match_stream_specifier(s, st, spec, &indexptr, &g, &p); |
627 | 0 | if (ret < 0) |
628 | 0 | goto error; |
629 | | |
630 | 0 | if (!indexptr) |
631 | 0 | return ret; |
632 | | |
633 | 0 | index = strtol(indexptr, &endptr, 0); |
634 | 0 | if (*endptr) { /* We can't have anything after the requested index. */ |
635 | 0 | ret = AVERROR(EINVAL); |
636 | 0 | goto error; |
637 | 0 | } |
638 | | |
639 | | /* This is not really needed but saves us a loop for simple stream index specifiers. */ |
640 | 0 | if (spec == indexptr) |
641 | 0 | return (index == st->index); |
642 | | |
643 | | /* If we requested a matching stream index, we have to ensure st is that. */ |
644 | 0 | nb_streams = g ? g->nb_streams : (p ? p->nb_stream_indexes : s->nb_streams); |
645 | 0 | for (int i = 0; i < nb_streams && index >= 0; i++) { |
646 | 0 | unsigned idx = g ? g->streams[i]->index : (p ? p->stream_index[i] : i); |
647 | 0 | const AVStream *candidate = s->streams[idx]; |
648 | 0 | ret = match_stream_specifier(s, candidate, spec, NULL, NULL, NULL); |
649 | 0 | if (ret < 0) |
650 | 0 | goto error; |
651 | 0 | if (ret > 0 && index-- == 0 && st == candidate) |
652 | 0 | return 1; |
653 | 0 | } |
654 | 0 | return 0; |
655 | | |
656 | 0 | error: |
657 | 0 | if (ret == AVERROR(EINVAL)) |
658 | 0 | av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec); |
659 | 0 | return ret; |
660 | 0 | } |
661 | | |
662 | | AVRational av_guess_sample_aspect_ratio(AVFormatContext *format, AVStream *stream, AVFrame *frame) |
663 | 0 | { |
664 | 0 | AVRational undef = {0, 1}; |
665 | 0 | AVRational stream_sample_aspect_ratio = stream ? stream->sample_aspect_ratio : undef; |
666 | 0 | AVRational codec_sample_aspect_ratio = stream && stream->codecpar ? stream->codecpar->sample_aspect_ratio : undef; |
667 | 0 | AVRational frame_sample_aspect_ratio = frame ? frame->sample_aspect_ratio : codec_sample_aspect_ratio; |
668 | |
|
669 | 0 | av_reduce(&stream_sample_aspect_ratio.num, &stream_sample_aspect_ratio.den, |
670 | 0 | stream_sample_aspect_ratio.num, stream_sample_aspect_ratio.den, INT_MAX); |
671 | 0 | if (stream_sample_aspect_ratio.num <= 0 || stream_sample_aspect_ratio.den <= 0) |
672 | 0 | stream_sample_aspect_ratio = undef; |
673 | |
|
674 | 0 | av_reduce(&frame_sample_aspect_ratio.num, &frame_sample_aspect_ratio.den, |
675 | 0 | frame_sample_aspect_ratio.num, frame_sample_aspect_ratio.den, INT_MAX); |
676 | 0 | if (frame_sample_aspect_ratio.num <= 0 || frame_sample_aspect_ratio.den <= 0) |
677 | 0 | frame_sample_aspect_ratio = undef; |
678 | |
|
679 | 0 | if (stream_sample_aspect_ratio.num) |
680 | 0 | return stream_sample_aspect_ratio; |
681 | 0 | else |
682 | 0 | return frame_sample_aspect_ratio; |
683 | 0 | } |
684 | | |
685 | | AVRational av_guess_frame_rate(AVFormatContext *format, AVStream *st, AVFrame *frame) |
686 | 0 | { |
687 | 0 | AVRational fr = st->r_frame_rate; |
688 | 0 | const AVCodecDescriptor *desc = cffstream(st)->codec_desc; |
689 | 0 | AVRational avg_fr = st->avg_frame_rate; |
690 | |
|
691 | 0 | if (avg_fr.num > 0 && avg_fr.den > 0 && fr.num > 0 && fr.den > 0 && |
692 | 0 | av_q2d(avg_fr) < 70 && av_q2d(fr) > 210) { |
693 | 0 | fr = avg_fr; |
694 | 0 | } |
695 | |
|
696 | 0 | if (desc && (desc->props & AV_CODEC_PROP_FIELDS)) { |
697 | 0 | const AVCodecContext *const avctx = ffstream(st)->avctx; |
698 | 0 | AVRational codec_fr = avctx->framerate; |
699 | |
|
700 | 0 | if ( codec_fr.num > 0 && codec_fr.den > 0 && |
701 | 0 | (fr.num == 0 || av_q2d(codec_fr) < av_q2d(fr)*0.7 && fabs(1.0 - av_q2d(av_div_q(avg_fr, fr))) > 0.1)) |
702 | 0 | fr = codec_fr; |
703 | 0 | } |
704 | |
|
705 | 0 | return fr; |
706 | 0 | } |
707 | | |
708 | | #if FF_API_INTERNAL_TIMING |
709 | | int avformat_transfer_internal_stream_timing_info(const AVOutputFormat *ofmt, |
710 | | AVStream *ost, const AVStream *ist, |
711 | | enum AVTimebaseSource copy_tb) |
712 | 0 | { |
713 | 0 | const AVCodecDescriptor *desc = cffstream(ist)->codec_desc; |
714 | 0 | const AVCodecContext *const dec_ctx = cffstream(ist)->avctx; |
715 | |
|
716 | 0 | AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 }; |
717 | 0 | AVRational dec_ctx_framerate = dec_ctx ? dec_ctx->framerate : (AVRational){ 0, 0 }; |
718 | 0 | AVRational dec_ctx_tb = dec_ctx_framerate.num ? av_inv_q(av_mul_q(dec_ctx_framerate, mul)) |
719 | 0 | : (ist->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ? (AVRational){0, 1} |
720 | 0 | : ist->time_base); |
721 | 0 | AVRational enc_tb = ist->time_base; |
722 | | |
723 | | /* |
724 | | * Avi is a special case here because it supports variable fps but |
725 | | * having the fps and timebase differe significantly adds quite some |
726 | | * overhead |
727 | | */ |
728 | 0 | if (!strcmp(ofmt->name, "avi")) { |
729 | 0 | #if FF_API_R_FRAME_RATE |
730 | 0 | if (copy_tb == AVFMT_TBCF_AUTO && ist->r_frame_rate.num |
731 | 0 | && av_q2d(ist->r_frame_rate) >= av_q2d(ist->avg_frame_rate) |
732 | 0 | && 0.5/av_q2d(ist->r_frame_rate) > av_q2d(ist->time_base) |
733 | 0 | && 0.5/av_q2d(ist->r_frame_rate) > av_q2d(dec_ctx_tb) |
734 | 0 | && av_q2d(ist->time_base) < 1.0/500 && av_q2d(dec_ctx_tb) < 1.0/500 |
735 | 0 | || copy_tb == AVFMT_TBCF_R_FRAMERATE) { |
736 | 0 | enc_tb.num = ist->r_frame_rate.den; |
737 | 0 | enc_tb.den = 2*ist->r_frame_rate.num; |
738 | 0 | } else |
739 | 0 | #endif |
740 | 0 | if (copy_tb == AVFMT_TBCF_AUTO && dec_ctx_framerate.num && |
741 | 0 | av_q2d(av_inv_q(dec_ctx_framerate)) > 2*av_q2d(ist->time_base) |
742 | 0 | && av_q2d(ist->time_base) < 1.0/500 |
743 | 0 | || (copy_tb == AVFMT_TBCF_DECODER && |
744 | 0 | (dec_ctx_framerate.num || ist->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))) { |
745 | 0 | enc_tb = dec_ctx_tb; |
746 | 0 | enc_tb.den *= 2; |
747 | 0 | } |
748 | 0 | } else if (!(ofmt->flags & AVFMT_VARIABLE_FPS) |
749 | 0 | && !av_match_name(ofmt->name, "mov,mp4,3gp,3g2,psp,ipod,ismv,f4v")) { |
750 | 0 | if (copy_tb == AVFMT_TBCF_AUTO && dec_ctx_framerate.num |
751 | 0 | && av_q2d(av_inv_q(dec_ctx_framerate)) > av_q2d(ist->time_base) |
752 | 0 | && av_q2d(ist->time_base) < 1.0/500 |
753 | 0 | || (copy_tb == AVFMT_TBCF_DECODER && |
754 | 0 | (dec_ctx_framerate.num || ist->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))) { |
755 | 0 | enc_tb = dec_ctx_tb; |
756 | 0 | } |
757 | 0 | } |
758 | |
|
759 | 0 | if (ost->codecpar->codec_tag == AV_RL32("tmcd") |
760 | 0 | && dec_ctx_tb.num < dec_ctx_tb.den |
761 | 0 | && dec_ctx_tb.num > 0 |
762 | 0 | && 121LL*dec_ctx_tb.num > dec_ctx_tb.den) { |
763 | 0 | enc_tb = dec_ctx_tb; |
764 | 0 | } |
765 | |
|
766 | 0 | av_reduce(&ffstream(ost)->transferred_mux_tb.num, |
767 | 0 | &ffstream(ost)->transferred_mux_tb.den, |
768 | 0 | enc_tb.num, enc_tb.den, INT_MAX); |
769 | |
|
770 | 0 | return 0; |
771 | 0 | } |
772 | | |
773 | | AVRational av_stream_get_codec_timebase(const AVStream *st) |
774 | 0 | { |
775 | 0 | return cffstream(st)->avctx ? cffstream(st)->avctx->time_base : cffstream(st)->transferred_mux_tb; |
776 | 0 | } |
777 | | #endif |
778 | | |
779 | | void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, |
780 | | unsigned int pts_num, unsigned int pts_den) |
781 | 0 | { |
782 | 0 | FFStream *const sti = ffstream(st); |
783 | 0 | AVRational new_tb; |
784 | 0 | if (av_reduce(&new_tb.num, &new_tb.den, pts_num, pts_den, INT_MAX)) { |
785 | 0 | if (new_tb.num != pts_num) |
786 | 0 | av_log(NULL, AV_LOG_DEBUG, |
787 | 0 | "st:%d removing common factor %d from timebase\n", |
788 | 0 | st->index, pts_num / new_tb.num); |
789 | 0 | } else |
790 | 0 | av_log(NULL, AV_LOG_WARNING, |
791 | 0 | "st:%d has too large timebase, reducing\n", st->index); |
792 | |
|
793 | 0 | if (new_tb.num <= 0 || new_tb.den <= 0) { |
794 | 0 | av_log(NULL, AV_LOG_ERROR, |
795 | 0 | "Ignoring attempt to set invalid timebase %d/%d for st:%d\n", |
796 | 0 | new_tb.num, new_tb.den, |
797 | 0 | st->index); |
798 | 0 | return; |
799 | 0 | } |
800 | 0 | st->time_base = new_tb; |
801 | 0 | if (sti->avctx) |
802 | 0 | sti->avctx->pkt_timebase = new_tb; |
803 | 0 | st->pts_wrap_bits = pts_wrap_bits; |
804 | 0 | } |
805 | | |
806 | | const AVCodec *ff_find_decoder(AVFormatContext *s, const AVStream *st, |
807 | | enum AVCodecID codec_id) |
808 | 0 | { |
809 | 0 | switch (st->codecpar->codec_type) { |
810 | 0 | case AVMEDIA_TYPE_VIDEO: |
811 | 0 | if (s->video_codec) return s->video_codec; |
812 | 0 | break; |
813 | 0 | case AVMEDIA_TYPE_AUDIO: |
814 | 0 | if (s->audio_codec) return s->audio_codec; |
815 | 0 | break; |
816 | 0 | case AVMEDIA_TYPE_SUBTITLE: |
817 | 0 | if (s->subtitle_codec) return s->subtitle_codec; |
818 | 0 | break; |
819 | 0 | } |
820 | | |
821 | 0 | return avcodec_find_decoder(codec_id); |
822 | 0 | } |
823 | | |
824 | | int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src) |
825 | 0 | { |
826 | 0 | #define OFF(field) offsetof(AVFormatContext, field) |
827 | 0 | static const unsigned offsets[] = { |
828 | 0 | OFF(codec_whitelist), OFF(format_whitelist), |
829 | 0 | OFF(protocol_whitelist), OFF(protocol_blacklist), |
830 | 0 | }; |
831 | 0 | #undef OFF |
832 | 0 | av_assert0(!dst->codec_whitelist && |
833 | 0 | !dst->format_whitelist && |
834 | 0 | !dst->protocol_whitelist && |
835 | 0 | !dst->protocol_blacklist); |
836 | 0 | for (unsigned i = 0; i < FF_ARRAY_ELEMS(offsets); i++) { |
837 | 0 | const char *src_str = *(char *const*)((const char*)src + offsets[i]); |
838 | |
|
839 | 0 | if (src_str) { |
840 | 0 | char *dst_str = av_strdup(src_str); |
841 | 0 | if (!dst_str) { |
842 | 0 | av_log(dst, AV_LOG_ERROR, "Failed to duplicate black/whitelist\n"); |
843 | 0 | return AVERROR(ENOMEM); |
844 | 0 | } |
845 | | |
846 | 0 | *(char **)((char*)dst + offsets[i]) = dst_str; |
847 | 0 | } |
848 | 0 | } |
849 | 0 | return 0; |
850 | 0 | } |
851 | | |
852 | | int ff_is_intra_only(enum AVCodecID id) |
853 | 0 | { |
854 | 0 | const AVCodecDescriptor *d = avcodec_descriptor_get(id); |
855 | 0 | if (!d) |
856 | 0 | return 0; |
857 | 0 | if ((d->type == AVMEDIA_TYPE_VIDEO || d->type == AVMEDIA_TYPE_AUDIO) && |
858 | 0 | !(d->props & AV_CODEC_PROP_INTRA_ONLY)) |
859 | 0 | return 0; |
860 | 0 | return 1; |
861 | 0 | } |
862 | | |
863 | | void ff_format_set_url(AVFormatContext *s, char *url) |
864 | 0 | { |
865 | 0 | av_assert0(url); |
866 | 0 | av_freep(&s->url); |
867 | 0 | s->url = url; |
868 | 0 | } |
869 | | |
870 | | int ff_format_io_close(AVFormatContext *s, AVIOContext **pb) |
871 | 0 | { |
872 | 0 | int ret = 0; |
873 | 0 | if (*pb) |
874 | 0 | ret = s->io_close2(s, *pb); |
875 | | *pb = NULL; |
876 | 0 | return ret; |
877 | 0 | } |