1 /*
2 * Core demuxing component
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 <stdint.h>
23
24 #include "config_components.h"
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixfmt.h"
34 #include "libavutil/time.h"
35 #include "libavutil/timestamp.h"
36
37 #include "libavcodec/bsf.h"
38 #include "libavcodec/internal.h"
39 #include "libavcodec/packet_internal.h"
40 #include "libavcodec/raw.h"
41
42 #include "avformat.h"
43 #include "avio_internal.h"
44 #include "demux.h"
45 #include "id3v2.h"
46 #include "internal.h"
47 #include "url.h"
48
49 static int need_parse_video_info(AVStream *st);
50 static int need_parse_audio_info(AVStream *st);
51
wrap_timestamp(const AVStream * st,int64_t timestamp)52 static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
53 {
54 const FFStream *const sti = cffstream(st);
55 if (sti->pts_wrap_behavior != AV_PTS_WRAP_IGNORE && st->pts_wrap_bits < 64 &&
56 sti->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) {
57 if (sti->pts_wrap_behavior == AV_PTS_WRAP_ADD_OFFSET &&
58 timestamp < sti->pts_wrap_reference)
59 return timestamp + (1ULL << st->pts_wrap_bits);
60 else if (sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET &&
61 timestamp >= sti->pts_wrap_reference)
62 return timestamp - (1ULL << st->pts_wrap_bits);
63 }
64 return timestamp;
65 }
66
ff_wrap_timestamp(const AVStream * st,int64_t timestamp)67 int64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp)
68 {
69 return wrap_timestamp(st, timestamp);
70 }
71
find_probe_decoder(AVFormatContext * s,const AVStream * st,enum AVCodecID codec_id)72 static const AVCodec *find_probe_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
73 {
74 const AVCodec *codec;
75
76 #if CONFIG_H264_DECODER
77 /* Other parts of the code assume this decoder to be used for h264,
78 * so force it if possible. */
79 if (codec_id == AV_CODEC_ID_H264)
80 return avcodec_find_decoder_by_name("h264");
81 #endif
82
83 codec = ff_find_decoder(s, st, codec_id);
84 if (!codec)
85 return NULL;
86
87 if (codec->capabilities & AV_CODEC_CAP_AVOID_PROBING) {
88 const AVCodec *probe_codec = NULL;
89 void *iter = NULL;
90 while ((probe_codec = av_codec_iterate(&iter))) {
91 if (probe_codec->id == codec->id &&
92 av_codec_is_decoder(probe_codec) &&
93 !(probe_codec->capabilities & (AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_EXPERIMENTAL))) {
94 return probe_codec;
95 }
96 }
97 }
98
99 return codec;
100 }
101
102 #ifdef OHOS_AUXILIARY_TRACK
need_parse_video_info(AVStream * st)103 static int need_parse_video_info(AVStream *st)
104 {
105 if (st == NULL || st->codecpar == NULL) {
106 return 0;
107 }
108 int is_video_codec = 0;
109 switch (st->codecpar->codec_id) {
110 case AV_CODEC_ID_MPEG4:
111 case AV_CODEC_ID_H264:
112 case AV_CODEC_ID_HEVC:
113 case AV_CODEC_ID_VVC:
114 is_video_codec = 1;
115 break;
116 default:
117 break;
118 }
119 return (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
120 || (st->codecpar->codec_type == AVMEDIA_TYPE_AUXILIARY && is_video_codec == 1);
121 }
122
need_parse_audio_info(AVStream * st)123 static int need_parse_audio_info(AVStream *st)
124 {
125 if (st == NULL || st->codecpar == NULL) {
126 return 0;
127 }
128 int is_audio_codec = 0;
129 switch (st->codecpar->codec_id) {
130 case AV_CODEC_ID_AAC:
131 case AV_CODEC_ID_AAC_LATM:
132 case AV_CODEC_ID_MP1:
133 case AV_CODEC_ID_MP2:
134 case AV_CODEC_ID_MP3:
135 case AV_CODEC_ID_AVS3DA:
136 case AV_CODEC_ID_AC3:
137 is_audio_codec = 1;
138 break;
139 default:
140 break;
141 }
142 return (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
143 || (st->codecpar->codec_type == AVMEDIA_TYPE_AUXILIARY && is_audio_codec == 1);
144 }
145 #endif
146
set_codec_from_probe_data(AVFormatContext * s,AVStream * st,AVProbeData * pd)147 static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st,
148 AVProbeData *pd)
149 {
150 static const struct {
151 const char *name;
152 enum AVCodecID id;
153 enum AVMediaType type;
154 } fmt_id_type[] = {
155 { "aac", AV_CODEC_ID_AAC, AVMEDIA_TYPE_AUDIO },
156 { "ac3", AV_CODEC_ID_AC3, AVMEDIA_TYPE_AUDIO },
157 { "aptx", AV_CODEC_ID_APTX, AVMEDIA_TYPE_AUDIO },
158 { "dts", AV_CODEC_ID_DTS, AVMEDIA_TYPE_AUDIO },
159 { "dvbsub", AV_CODEC_ID_DVB_SUBTITLE, AVMEDIA_TYPE_SUBTITLE },
160 { "dvbtxt", AV_CODEC_ID_DVB_TELETEXT, AVMEDIA_TYPE_SUBTITLE },
161 { "eac3", AV_CODEC_ID_EAC3, AVMEDIA_TYPE_AUDIO },
162 { "h264", AV_CODEC_ID_H264, AVMEDIA_TYPE_VIDEO },
163 { "hevc", AV_CODEC_ID_HEVC, AVMEDIA_TYPE_VIDEO },
164 { "loas", AV_CODEC_ID_AAC_LATM, AVMEDIA_TYPE_AUDIO },
165 { "m4v", AV_CODEC_ID_MPEG4, AVMEDIA_TYPE_VIDEO },
166 { "mjpeg_2000", AV_CODEC_ID_JPEG2000, AVMEDIA_TYPE_VIDEO },
167 { "mp3", AV_CODEC_ID_MP3, AVMEDIA_TYPE_AUDIO },
168 { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
169 { "truehd", AV_CODEC_ID_TRUEHD, AVMEDIA_TYPE_AUDIO },
170 #ifdef OHOS_OPT_COMPAT
171 { "vvc", AV_CODEC_ID_VVC, AVMEDIA_TYPE_VIDEO },
172 #endif
173 { 0 }
174 };
175 int score;
176 const AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score);
177 FFStream *const sti = ffstream(st);
178
179 if (fmt) {
180 av_log(s, AV_LOG_DEBUG,
181 "Probe with size=%d, packets=%d detected %s with score=%d\n",
182 pd->buf_size, s->max_probe_packets - sti->probe_packets,
183 fmt->name, score);
184 for (int i = 0; fmt_id_type[i].name; i++) {
185 if (!strcmp(fmt->name, fmt_id_type[i].name)) {
186 if (fmt_id_type[i].type != AVMEDIA_TYPE_AUDIO &&
187 st->codecpar->sample_rate)
188 continue;
189 if (sti->request_probe > score &&
190 st->codecpar->codec_id != fmt_id_type[i].id)
191 continue;
192 st->codecpar->codec_id = fmt_id_type[i].id;
193 st->codecpar->codec_type = fmt_id_type[i].type;
194 sti->need_context_update = 1;
195 return score;
196 }
197 }
198 }
199 return 0;
200 }
201
init_input(AVFormatContext * s,const char * filename,AVDictionary ** options)202 static int init_input(AVFormatContext *s, const char *filename,
203 AVDictionary **options)
204 {
205 int ret;
206 AVProbeData pd = { filename, NULL, 0 };
207 int score = AVPROBE_SCORE_RETRY;
208
209 if (s->pb) {
210 s->flags |= AVFMT_FLAG_CUSTOM_IO;
211 if (!s->iformat)
212 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
213 s, 0, s->format_probesize);
214 else if (s->iformat->flags & AVFMT_NOFILE)
215 av_log(s, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
216 "will be ignored with AVFMT_NOFILE format.\n");
217 return 0;
218 }
219
220 if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
221 (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score))))
222 return score;
223
224 if ((ret = s->io_open(s, &s->pb, filename, AVIO_FLAG_READ | s->avio_flags, options)) < 0)
225 return ret;
226
227 if (s->iformat)
228 return 0;
229 return av_probe_input_buffer2(s->pb, &s->iformat, filename,
230 s, 0, s->format_probesize);
231 }
232
update_stream_avctx(AVFormatContext * s)233 static int update_stream_avctx(AVFormatContext *s)
234 {
235 int ret;
236 for (unsigned i = 0; i < s->nb_streams; i++) {
237 AVStream *const st = s->streams[i];
238 FFStream *const sti = ffstream(st);
239
240 if (!sti->need_context_update)
241 continue;
242
243 /* close parser, because it depends on the codec */
244 if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) {
245 av_parser_close(sti->parser);
246 sti->parser = NULL;
247 }
248
249 #if FF_API_OLD_CHANNEL_LAYOUT
250 FF_DISABLE_DEPRECATION_WARNINGS
251 if (st->codecpar->ch_layout.nb_channels &&
252 !st->codecpar->channels) {
253 st->codecpar->channels = st->codecpar->ch_layout.nb_channels;
254 st->codecpar->channel_layout = st->codecpar->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
255 st->codecpar->ch_layout.u.mask : 0;
256
257 }
258 FF_ENABLE_DEPRECATION_WARNINGS
259 #endif
260
261 /* update internal codec context, for the parser */
262 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
263 if (ret < 0)
264 return ret;
265
266 sti->need_context_update = 0;
267 }
268 return 0;
269 }
270
avformat_open_input(AVFormatContext ** ps,const char * filename,const AVInputFormat * fmt,AVDictionary ** options)271 int avformat_open_input(AVFormatContext **ps, const char *filename,
272 const AVInputFormat *fmt, AVDictionary **options)
273 {
274 AVFormatContext *s = *ps;
275 FFFormatContext *si;
276 AVDictionary *tmp = NULL;
277 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
278 int ret = 0;
279
280 if (!s && !(s = avformat_alloc_context()))
281 return AVERROR(ENOMEM);
282 si = ffformatcontext(s);
283 if (!s->av_class) {
284 av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
285 return AVERROR(EINVAL);
286 }
287 if (fmt)
288 s->iformat = fmt;
289
290 if (options)
291 av_dict_copy(&tmp, *options, 0);
292
293 if (s->pb) // must be before any goto fail
294 s->flags |= AVFMT_FLAG_CUSTOM_IO;
295
296 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
297 goto fail;
298
299 if (!(s->url = av_strdup(filename ? filename : ""))) {
300 ret = AVERROR(ENOMEM);
301 goto fail;
302 }
303
304 if ((ret = init_input(s, filename, &tmp)) < 0)
305 goto fail;
306 s->probe_score = ret;
307
308 if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) {
309 s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist);
310 if (!s->protocol_whitelist) {
311 ret = AVERROR(ENOMEM);
312 goto fail;
313 }
314 }
315
316 if (!s->protocol_blacklist && s->pb && s->pb->protocol_blacklist) {
317 s->protocol_blacklist = av_strdup(s->pb->protocol_blacklist);
318 if (!s->protocol_blacklist) {
319 ret = AVERROR(ENOMEM);
320 goto fail;
321 }
322 }
323
324 if (s->format_whitelist && av_match_list(s->iformat->name, s->format_whitelist, ',') <= 0) {
325 av_log(s, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", s->format_whitelist);
326 ret = AVERROR(EINVAL);
327 goto fail;
328 }
329
330 avio_skip(s->pb, s->skip_initial_bytes);
331
332 /* Check filename in case an image number is expected. */
333 if (s->iformat->flags & AVFMT_NEEDNUMBER) {
334 if (!av_filename_number_test(filename)) {
335 ret = AVERROR(EINVAL);
336 goto fail;
337 }
338 }
339
340 s->duration = s->start_time = AV_NOPTS_VALUE;
341
342 /* Allocate private data. */
343 if (s->iformat->priv_data_size > 0) {
344 if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) {
345 ret = AVERROR(ENOMEM);
346 goto fail;
347 }
348 if (s->iformat->priv_class) {
349 *(const AVClass **) s->priv_data = s->iformat->priv_class;
350 av_opt_set_defaults(s->priv_data);
351 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
352 goto fail;
353 }
354 }
355
356 /* e.g. AVFMT_NOFILE formats will not have an AVIOContext */
357 if (s->pb)
358 ff_id3v2_read_dict(s->pb, &si->id3v2_meta, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
359
360 if (s->iformat->read_header)
361 if ((ret = s->iformat->read_header(s)) < 0) {
362 if (s->iformat->flags_internal & FF_FMT_INIT_CLEANUP)
363 goto close;
364 goto fail;
365 }
366
367 if (!s->metadata) {
368 s->metadata = si->id3v2_meta;
369 si->id3v2_meta = NULL;
370 } else if (si->id3v2_meta) {
371 av_log(s, AV_LOG_WARNING, "Discarding ID3 tags because more suitable tags were found.\n");
372 av_dict_free(&si->id3v2_meta);
373 }
374
375 if (id3v2_extra_meta) {
376 if (!strcmp(s->iformat->name, "mp3") || !strcmp(s->iformat->name, "aac") ||
377 !strcmp(s->iformat->name, "tta") || !strcmp(s->iformat->name, "wav")) {
378 if ((ret = ff_id3v2_parse_apic(s, id3v2_extra_meta)) < 0)
379 goto close;
380 if ((ret = ff_id3v2_parse_chapters(s, id3v2_extra_meta)) < 0)
381 goto close;
382 if ((ret = ff_id3v2_parse_priv(s, id3v2_extra_meta)) < 0)
383 goto close;
384 } else
385 av_log(s, AV_LOG_DEBUG, "demuxer does not support additional id3 data, skipping\n");
386 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
387 }
388
389 if ((ret = avformat_queue_attached_pictures(s)) < 0)
390 goto close;
391
392 if (s->pb && !si->data_offset)
393 si->data_offset = avio_tell(s->pb);
394
395 si->raw_packet_buffer_size = 0;
396
397 update_stream_avctx(s);
398
399 if (options) {
400 av_dict_free(options);
401 *options = tmp;
402 }
403 *ps = s;
404 return 0;
405
406 close:
407 if (s->iformat->read_close)
408 s->iformat->read_close(s);
409 fail:
410 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
411 av_dict_free(&tmp);
412 if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
413 avio_closep(&s->pb);
414 avformat_free_context(s);
415 *ps = NULL;
416 return ret;
417 }
418
avformat_close_input(AVFormatContext ** ps)419 void avformat_close_input(AVFormatContext **ps)
420 {
421 AVFormatContext *s;
422 AVIOContext *pb;
423
424 if (!ps || !*ps)
425 return;
426
427 s = *ps;
428 pb = s->pb;
429
430 if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) ||
431 (s->flags & AVFMT_FLAG_CUSTOM_IO))
432 pb = NULL;
433
434 if (s->iformat)
435 if (s->iformat->read_close)
436 s->iformat->read_close(s);
437
438 avformat_free_context(s);
439
440 *ps = NULL;
441
442 avio_close(pb);
443 }
444
force_codec_ids(AVFormatContext * s,AVStream * st)445 static void force_codec_ids(AVFormatContext *s, AVStream *st)
446 {
447 switch (st->codecpar->codec_type) {
448 case AVMEDIA_TYPE_VIDEO:
449 if (s->video_codec_id)
450 st->codecpar->codec_id = s->video_codec_id;
451 break;
452 case AVMEDIA_TYPE_AUDIO:
453 if (s->audio_codec_id)
454 st->codecpar->codec_id = s->audio_codec_id;
455 break;
456 case AVMEDIA_TYPE_SUBTITLE:
457 if (s->subtitle_codec_id)
458 st->codecpar->codec_id = s->subtitle_codec_id;
459 break;
460 case AVMEDIA_TYPE_DATA:
461 if (s->data_codec_id)
462 st->codecpar->codec_id = s->data_codec_id;
463 break;
464 }
465 }
466
probe_codec(AVFormatContext * s,AVStream * st,const AVPacket * pkt)467 static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
468 {
469 FFFormatContext *const si = ffformatcontext(s);
470 FFStream *const sti = ffstream(st);
471
472 if (sti->request_probe > 0) {
473 AVProbeData *const pd = &sti->probe_data;
474 int end;
475 av_log(s, AV_LOG_DEBUG, "probing stream %d pp:%d\n", st->index, sti->probe_packets);
476 --sti->probe_packets;
477
478 if (pkt) {
479 uint8_t *new_buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
480 if (!new_buf) {
481 av_log(s, AV_LOG_WARNING,
482 "Failed to reallocate probe buffer for stream %d\n",
483 st->index);
484 goto no_packet;
485 }
486 pd->buf = new_buf;
487 memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size);
488 pd->buf_size += pkt->size;
489 memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE);
490 } else {
491 no_packet:
492 sti->probe_packets = 0;
493 if (!pd->buf_size) {
494 av_log(s, AV_LOG_WARNING,
495 "nothing to probe for stream %d\n", st->index);
496 }
497 }
498
499 end = si->raw_packet_buffer_size >= s->probesize
500 || sti->probe_packets <= 0;
501
502 if (end || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
503 int score = set_codec_from_probe_data(s, st, pd);
504 if ( (st->codecpar->codec_id != AV_CODEC_ID_NONE && score > AVPROBE_SCORE_STREAM_RETRY)
505 || end) {
506 pd->buf_size = 0;
507 av_freep(&pd->buf);
508 sti->request_probe = -1;
509 if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
510 av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
511 } else
512 av_log(s, AV_LOG_WARNING, "probed stream %d failed\n", st->index);
513 }
514 force_codec_ids(s, st);
515 }
516 }
517 return 0;
518 }
519
update_wrap_reference(AVFormatContext * s,AVStream * st,int stream_index,AVPacket * pkt)520 static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
521 {
522 FFStream *const sti = ffstream(st);
523 int64_t ref = pkt->dts;
524 int pts_wrap_behavior;
525 int64_t pts_wrap_reference;
526 AVProgram *first_program;
527
528 if (ref == AV_NOPTS_VALUE)
529 ref = pkt->pts;
530 if (sti->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow)
531 return 0;
532 ref &= (1LL << st->pts_wrap_bits)-1;
533
534 // reference time stamp should be 60 s before first time stamp
535 pts_wrap_reference = ref - av_rescale(60, st->time_base.den, st->time_base.num);
536 // if first time stamp is not more than 1/8 and 60s before the wrap point, subtract rather than add wrap offset
537 pts_wrap_behavior = (ref < (1LL << st->pts_wrap_bits) - (1LL << st->pts_wrap_bits-3)) ||
538 (ref < (1LL << st->pts_wrap_bits) - av_rescale(60, st->time_base.den, st->time_base.num)) ?
539 AV_PTS_WRAP_ADD_OFFSET : AV_PTS_WRAP_SUB_OFFSET;
540
541 first_program = av_find_program_from_stream(s, NULL, stream_index);
542
543 if (!first_program) {
544 int default_stream_index = av_find_default_stream_index(s);
545 FFStream *const default_sti = ffstream(s->streams[default_stream_index]);
546 if (default_sti->pts_wrap_reference == AV_NOPTS_VALUE) {
547 for (unsigned i = 0; i < s->nb_streams; i++) {
548 FFStream *const sti = ffstream(s->streams[i]);
549 if (av_find_program_from_stream(s, NULL, i))
550 continue;
551 sti->pts_wrap_reference = pts_wrap_reference;
552 sti->pts_wrap_behavior = pts_wrap_behavior;
553 }
554 } else {
555 sti->pts_wrap_reference = default_sti->pts_wrap_reference;
556 sti->pts_wrap_behavior = default_sti->pts_wrap_behavior;
557 }
558 } else {
559 AVProgram *program = first_program;
560 while (program) {
561 if (program->pts_wrap_reference != AV_NOPTS_VALUE) {
562 pts_wrap_reference = program->pts_wrap_reference;
563 pts_wrap_behavior = program->pts_wrap_behavior;
564 break;
565 }
566 program = av_find_program_from_stream(s, program, stream_index);
567 }
568
569 // update every program with differing pts_wrap_reference
570 program = first_program;
571 while (program) {
572 if (program->pts_wrap_reference != pts_wrap_reference) {
573 for (unsigned i = 0; i < program->nb_stream_indexes; i++) {
574 FFStream *const sti = ffstream(s->streams[program->stream_index[i]]);
575 sti->pts_wrap_reference = pts_wrap_reference;
576 sti->pts_wrap_behavior = pts_wrap_behavior;
577 }
578
579 program->pts_wrap_reference = pts_wrap_reference;
580 program->pts_wrap_behavior = pts_wrap_behavior;
581 }
582 program = av_find_program_from_stream(s, program, stream_index);
583 }
584 }
585 return 1;
586 }
587
ff_read_packet(AVFormatContext * s,AVPacket * pkt)588 int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
589 {
590 FFFormatContext *const si = ffformatcontext(s);
591 int err;
592
593 #if FF_API_INIT_PACKET
594 FF_DISABLE_DEPRECATION_WARNINGS
595 pkt->data = NULL;
596 pkt->size = 0;
597 av_init_packet(pkt);
598 FF_ENABLE_DEPRECATION_WARNINGS
599 #else
600 av_packet_unref(pkt);
601 #endif
602
603 for (;;) {
604 PacketListEntry *pktl = si->raw_packet_buffer.head;
605 AVStream *st;
606 FFStream *sti;
607 const AVPacket *pkt1;
608
609 if (pktl) {
610 AVStream *const st = s->streams[pktl->pkt.stream_index];
611 if (si->raw_packet_buffer_size >= s->probesize)
612 if ((err = probe_codec(s, st, NULL)) < 0)
613 return err;
614 if (ffstream(st)->request_probe <= 0) {
615 avpriv_packet_list_get(&si->raw_packet_buffer, pkt);
616 si->raw_packet_buffer_size -= pkt->size;
617 return 0;
618 }
619 }
620
621 err = s->iformat->read_packet(s, pkt);
622 if (err < 0) {
623 av_packet_unref(pkt);
624
625 /* Some demuxers return FFERROR_REDO when they consume
626 data and discard it (ignored streams, junk, extradata).
627 We must re-call the demuxer to get the real packet. */
628 if (err == FFERROR_REDO)
629 continue;
630 if (!pktl || err == AVERROR(EAGAIN))
631 return err;
632 for (unsigned i = 0; i < s->nb_streams; i++) {
633 AVStream *const st = s->streams[i];
634 FFStream *const sti = ffstream(st);
635 if (sti->probe_packets || sti->request_probe > 0)
636 if ((err = probe_codec(s, st, NULL)) < 0)
637 return err;
638 av_assert0(sti->request_probe <= 0);
639 }
640 continue;
641 }
642
643 err = av_packet_make_refcounted(pkt);
644 if (err < 0) {
645 av_packet_unref(pkt);
646 return err;
647 }
648
649 if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
650 av_log(s, AV_LOG_WARNING,
651 "Packet corrupt (stream = %d, dts = %s)",
652 pkt->stream_index, av_ts2str(pkt->dts));
653 if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) {
654 av_log(s, AV_LOG_WARNING, ", dropping it.\n");
655 av_packet_unref(pkt);
656 continue;
657 }
658 av_log(s, AV_LOG_WARNING, ".\n");
659 }
660
661 av_assert0(pkt->stream_index < (unsigned)s->nb_streams &&
662 "Invalid stream index.\n");
663
664 st = s->streams[pkt->stream_index];
665 sti = ffstream(st);
666
667 if (update_wrap_reference(s, st, pkt->stream_index, pkt) && sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET) {
668 // correct first time stamps to negative values
669 if (!is_relative(sti->first_dts))
670 sti->first_dts = wrap_timestamp(st, sti->first_dts);
671 if (!is_relative(st->start_time))
672 st->start_time = wrap_timestamp(st, st->start_time);
673 if (!is_relative(sti->cur_dts))
674 sti->cur_dts = wrap_timestamp(st, sti->cur_dts);
675 }
676
677 pkt->dts = wrap_timestamp(st, pkt->dts);
678 pkt->pts = wrap_timestamp(st, pkt->pts);
679
680 force_codec_ids(s, st);
681
682 /* TODO: audio: time filter; video: frame reordering (pts != dts) */
683 if (s->use_wallclock_as_timestamps)
684 pkt->dts = pkt->pts = av_rescale_q(av_gettime(), AV_TIME_BASE_Q, st->time_base);
685
686 if (!pktl && sti->request_probe <= 0)
687 return 0;
688
689 err = avpriv_packet_list_put(&si->raw_packet_buffer,
690 pkt, NULL, 0);
691 if (err < 0) {
692 av_packet_unref(pkt);
693 return err;
694 }
695 pkt1 = &si->raw_packet_buffer.tail->pkt;
696 si->raw_packet_buffer_size += pkt1->size;
697
698 if ((err = probe_codec(s, st, pkt1)) < 0)
699 return err;
700 }
701 }
702
703 /**
704 * Return the frame duration in seconds. Return 0 if not available.
705 */
compute_frame_duration(AVFormatContext * s,int * pnum,int * pden,AVStream * st,AVCodecParserContext * pc,AVPacket * pkt)706 static void compute_frame_duration(AVFormatContext *s, int *pnum, int *pden,
707 AVStream *st, AVCodecParserContext *pc,
708 AVPacket *pkt)
709 {
710 FFStream *const sti = ffstream(st);
711 AVRational codec_framerate = sti->avctx->framerate;
712 int frame_size, sample_rate;
713
714 *pnum = 0;
715 *pden = 0;
716 #ifdef OHOS_AUXILIARY_TRACK
717 if (need_parse_video_info(st)) {
718 #else
719 switch (st->codecpar->codec_type) {
720 case AVMEDIA_TYPE_VIDEO:
721 #endif
722 if (st->r_frame_rate.num && (!pc || !codec_framerate.num)) {
723 *pnum = st->r_frame_rate.den;
724 *pden = st->r_frame_rate.num;
725 } else if (st->time_base.num * 1000LL > st->time_base.den) {
726 *pnum = st->time_base.num;
727 *pden = st->time_base.den;
728 } else if (codec_framerate.den * 1000LL > codec_framerate.num) {
729 av_assert0(sti->avctx->ticks_per_frame);
730 av_reduce(pnum, pden,
731 codec_framerate.den,
732 codec_framerate.num * (int64_t)sti->avctx->ticks_per_frame,
733 INT_MAX);
734
735 if (pc && pc->repeat_pict) {
736 av_reduce(pnum, pden,
737 (*pnum) * (1LL + pc->repeat_pict),
738 (*pden),
739 INT_MAX);
740 }
741 /* If this codec can be interlaced or progressive then we need
742 * a parser to compute duration of a packet. Thus if we have
743 * no parser in such case leave duration undefined. */
744 if (sti->avctx->ticks_per_frame > 1 && !pc)
745 *pnum = *pden = 0;
746 }
747 #ifdef OHOS_AUXILIARY_TRACK
748 } else if (need_parse_audio_info(st)) {
749 #else
750 break;
751 case AVMEDIA_TYPE_AUDIO:
752 #endif
753 if (sti->avctx_inited) {
754 frame_size = av_get_audio_frame_duration(sti->avctx, pkt->size);
755 sample_rate = sti->avctx->sample_rate;
756 } else {
757 frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size);
758 sample_rate = st->codecpar->sample_rate;
759 }
760 if (frame_size <= 0 || sample_rate <= 0)
761 #ifdef OHOS_AUXILIARY_TRACK
762 return;
763 #else
764 break;
765 #endif
766 *pnum = frame_size;
767 *pden = sample_rate;
768 #ifndef OHOS_AUXILIARY_TRACK
769 break;
770 default:
771 break;
772 #endif
773 }
774 }
775
776 static int has_decode_delay_been_guessed(AVStream *st)
777 {
778 FFStream *const sti = ffstream(st);
779 if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1;
780 if (!sti->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
781 return 1;
782 #if CONFIG_H264_DECODER
783 if (sti->avctx->has_b_frames &&
784 avpriv_h264_has_num_reorder_frames(sti->avctx) == sti->avctx->has_b_frames)
785 return 1;
786 #endif
787 if (sti->avctx->has_b_frames < 3)
788 return sti->nb_decoded_frames >= 7;
789 else if (sti->avctx->has_b_frames < 4)
790 return sti->nb_decoded_frames >= 18;
791 else
792 return sti->nb_decoded_frames >= 20;
793 }
794
795 static PacketListEntry *get_next_pkt(AVFormatContext *s, AVStream *st,
796 PacketListEntry *pktl)
797 {
798 FFFormatContext *const si = ffformatcontext(s);
799 if (pktl->next)
800 return pktl->next;
801 if (pktl == si->packet_buffer.tail)
802 return si->parse_queue.head;
803 return NULL;
804 }
805
806 static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts)
807 {
808 FFStream *const sti = ffstream(st);
809 #ifdef OHOS_OPT_COMPAT
810 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
811 st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
812 st->codecpar->codec_id != AV_CODEC_ID_VVC;
813 #else
814 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
815 st->codecpar->codec_id != AV_CODEC_ID_HEVC;
816 #endif
817 if (!onein_oneout) {
818 int delay = sti->avctx->has_b_frames;
819
820 if (dts == AV_NOPTS_VALUE) {
821 int64_t best_score = INT64_MAX;
822 for (int i = 0; i < delay; i++) {
823 if (sti->pts_reorder_error_count[i]) {
824 int64_t score = sti->pts_reorder_error[i] / sti->pts_reorder_error_count[i];
825 if (score < best_score) {
826 best_score = score;
827 dts = pts_buffer[i];
828 }
829 }
830 }
831 } else {
832 for (int i = 0; i < delay; i++) {
833 if (pts_buffer[i] != AV_NOPTS_VALUE) {
834 int64_t diff = FFABS(pts_buffer[i] - dts)
835 + (uint64_t)sti->pts_reorder_error[i];
836 diff = FFMAX(diff, sti->pts_reorder_error[i]);
837 sti->pts_reorder_error[i] = diff;
838 sti->pts_reorder_error_count[i]++;
839 if (sti->pts_reorder_error_count[i] > 250) {
840 sti->pts_reorder_error[i] >>= 1;
841 sti->pts_reorder_error_count[i] >>= 1;
842 }
843 }
844 }
845 }
846 }
847
848 if (dts == AV_NOPTS_VALUE)
849 dts = pts_buffer[0];
850
851 return dts;
852 }
853
854 /**
855 * Updates the dts of packets of a stream in pkt_buffer, by re-ordering the pts
856 * of the packets in a window.
857 */
858 static void update_dts_from_pts(AVFormatContext *s, int stream_index,
859 PacketListEntry *pkt_buffer)
860 {
861 AVStream *const st = s->streams[stream_index];
862 int delay = ffstream(st)->avctx->has_b_frames;
863
864 int64_t pts_buffer[MAX_REORDER_DELAY+1];
865
866 for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
867 pts_buffer[i] = AV_NOPTS_VALUE;
868
869 for (; pkt_buffer; pkt_buffer = get_next_pkt(s, st, pkt_buffer)) {
870 if (pkt_buffer->pkt.stream_index != stream_index)
871 continue;
872
873 if (pkt_buffer->pkt.pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
874 pts_buffer[0] = pkt_buffer->pkt.pts;
875 for (int i = 0; i < delay && pts_buffer[i] > pts_buffer[i + 1]; i++)
876 FFSWAP(int64_t, pts_buffer[i], pts_buffer[i + 1]);
877
878 pkt_buffer->pkt.dts = select_from_pts_buffer(st, pts_buffer, pkt_buffer->pkt.dts);
879 }
880 }
881 }
882
883 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
884 int64_t dts, int64_t pts, AVPacket *pkt)
885 {
886 FFFormatContext *const si = ffformatcontext(s);
887 AVStream *const st = s->streams[stream_index];
888 FFStream *const sti = ffstream(st);
889 PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : si->parse_queue.head;
890
891 uint64_t shift;
892
893 if (sti->first_dts != AV_NOPTS_VALUE ||
894 dts == AV_NOPTS_VALUE ||
895 sti->cur_dts == AV_NOPTS_VALUE ||
896 sti->cur_dts < INT_MIN + RELATIVE_TS_BASE ||
897 dts < INT_MIN + (sti->cur_dts - RELATIVE_TS_BASE) ||
898 is_relative(dts))
899 return;
900
901 sti->first_dts = dts - (sti->cur_dts - RELATIVE_TS_BASE);
902 sti->cur_dts = dts;
903 shift = (uint64_t)sti->first_dts - RELATIVE_TS_BASE;
904
905 if (is_relative(pts))
906 pts += shift;
907
908 for (PacketListEntry *pktl_it = pktl; pktl_it; pktl_it = get_next_pkt(s, st, pktl_it)) {
909 if (pktl_it->pkt.stream_index != stream_index)
910 continue;
911 if (is_relative(pktl_it->pkt.pts))
912 pktl_it->pkt.pts += shift;
913
914 if (is_relative(pktl_it->pkt.dts))
915 pktl_it->pkt.dts += shift;
916
917 if (st->start_time == AV_NOPTS_VALUE && pktl_it->pkt.pts != AV_NOPTS_VALUE) {
918 st->start_time = pktl_it->pkt.pts;
919 #ifdef OHOS_AUXILIARY_TRACK
920 if (need_parse_audio_info(st) && st->codecpar->sample_rate)
921 #else
922 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
923 #endif
924 st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
925 }
926 }
927
928 if (has_decode_delay_been_guessed(st))
929 update_dts_from_pts(s, stream_index, pktl);
930
931 if (st->start_time == AV_NOPTS_VALUE) {
932 #ifdef OHOS_AUXILIARY_TRACK
933 if (need_parse_audio_info(st) || !(pkt->flags & AV_PKT_FLAG_DISCARD)) {
934 #else
935 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || !(pkt->flags & AV_PKT_FLAG_DISCARD)) {
936 #endif
937 st->start_time = pts;
938 }
939 #ifdef OHOS_AUXILIARY_TRACK
940 if (need_parse_audio_info(st) && st->codecpar->sample_rate)
941 #else
942 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate)
943 #endif
944 st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
945 }
946 }
947
948 static void update_initial_durations(AVFormatContext *s, AVStream *st,
949 int stream_index, int64_t duration)
950 {
951 FFFormatContext *const si = ffformatcontext(s);
952 FFStream *const sti = ffstream(st);
953 PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : si->parse_queue.head;
954 int64_t cur_dts = RELATIVE_TS_BASE;
955
956 if (sti->first_dts != AV_NOPTS_VALUE) {
957 if (sti->update_initial_durations_done)
958 return;
959 sti->update_initial_durations_done = 1;
960 cur_dts = sti->first_dts;
961 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
962 if (pktl->pkt.stream_index == stream_index) {
963 if (pktl->pkt.pts != pktl->pkt.dts ||
964 pktl->pkt.dts != AV_NOPTS_VALUE ||
965 pktl->pkt.duration)
966 break;
967 cur_dts -= duration;
968 }
969 }
970 if (pktl && pktl->pkt.dts != sti->first_dts) {
971 av_log(s, AV_LOG_DEBUG, "first_dts %s not matching first dts %s (pts %s, duration %"PRId64") in the queue\n",
972 av_ts2str(sti->first_dts), av_ts2str(pktl->pkt.dts), av_ts2str(pktl->pkt.pts), pktl->pkt.duration);
973 return;
974 }
975 if (!pktl) {
976 av_log(s, AV_LOG_DEBUG, "first_dts %s but no packet with dts in the queue\n", av_ts2str(sti->first_dts));
977 return;
978 }
979 pktl = si->packet_buffer.head ? si->packet_buffer.head : si->parse_queue.head;
980 sti->first_dts = cur_dts;
981 } else if (sti->cur_dts != RELATIVE_TS_BASE)
982 return;
983
984 for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
985 if (pktl->pkt.stream_index != stream_index)
986 continue;
987 if ((pktl->pkt.pts == pktl->pkt.dts ||
988 pktl->pkt.pts == AV_NOPTS_VALUE) &&
989 (pktl->pkt.dts == AV_NOPTS_VALUE ||
990 pktl->pkt.dts == sti->first_dts ||
991 pktl->pkt.dts == RELATIVE_TS_BASE) &&
992 !pktl->pkt.duration &&
993 av_sat_add64(cur_dts, duration) == cur_dts + (uint64_t)duration
994 ) {
995 pktl->pkt.dts = cur_dts;
996 if (!sti->avctx->has_b_frames)
997 pktl->pkt.pts = cur_dts;
998 pktl->pkt.duration = duration;
999 } else
1000 break;
1001 cur_dts = pktl->pkt.dts + pktl->pkt.duration;
1002 }
1003 if (!pktl)
1004 sti->cur_dts = cur_dts;
1005 }
1006
1007 static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
1008 AVCodecParserContext *pc, AVPacket *pkt,
1009 int64_t next_dts, int64_t next_pts)
1010 {
1011 FFFormatContext *const si = ffformatcontext(s);
1012 FFStream *const sti = ffstream(st);
1013 int num, den, presentation_delayed, delay;
1014 int64_t offset;
1015 AVRational duration;
1016 #ifdef OHOS_OPT_COMPAT
1017 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
1018 st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
1019 st->codecpar->codec_id != AV_CODEC_ID_VVC;
1020 #else
1021 int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
1022 st->codecpar->codec_id != AV_CODEC_ID_HEVC;
1023 #endif
1024 if (s->flags & AVFMT_FLAG_NOFILLIN)
1025 return;
1026 #ifdef OHOS_AUXILIARY_TRACK
1027 if (need_parse_video_info(st) && pkt->dts != AV_NOPTS_VALUE) {
1028 #else
1029 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && pkt->dts != AV_NOPTS_VALUE) {
1030 #endif
1031 if (pkt->dts == pkt->pts && sti->last_dts_for_order_check != AV_NOPTS_VALUE) {
1032 if (sti->last_dts_for_order_check <= pkt->dts) {
1033 sti->dts_ordered++;
1034 } else {
1035 av_log(s, sti->dts_misordered ? AV_LOG_DEBUG : AV_LOG_WARNING,
1036 "DTS %"PRIi64" < %"PRIi64" out of order\n",
1037 pkt->dts,
1038 sti->last_dts_for_order_check);
1039 sti->dts_misordered++;
1040 }
1041 if (sti->dts_ordered + sti->dts_misordered > 250) {
1042 sti->dts_ordered >>= 1;
1043 sti->dts_misordered >>= 1;
1044 }
1045 }
1046
1047 sti->last_dts_for_order_check = pkt->dts;
1048 if (sti->dts_ordered < 8 * sti->dts_misordered && pkt->dts == pkt->pts)
1049 pkt->dts = AV_NOPTS_VALUE;
1050 }
1051
1052 if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
1053 pkt->dts = AV_NOPTS_VALUE;
1054
1055 if (pc && pc->pict_type == AV_PICTURE_TYPE_B
1056 && !sti->avctx->has_b_frames)
1057 //FIXME Set low_delay = 0 when has_b_frames = 1
1058 sti->avctx->has_b_frames = 1;
1059
1060 /* do we have a video B-frame ? */
1061 delay = sti->avctx->has_b_frames;
1062 presentation_delayed = 0;
1063
1064 /* XXX: need has_b_frame, but cannot get it if the codec is
1065 * not initialized */
1066 if (delay &&
1067 pc && pc->pict_type != AV_PICTURE_TYPE_B)
1068 presentation_delayed = 1;
1069
1070 if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
1071 st->pts_wrap_bits < 63 && pkt->dts > INT64_MIN + (1LL << st->pts_wrap_bits) &&
1072 pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) {
1073 if (is_relative(sti->cur_dts) || pkt->dts - (1LL<<(st->pts_wrap_bits - 1)) > sti->cur_dts) {
1074 pkt->dts -= 1LL << st->pts_wrap_bits;
1075 } else
1076 pkt->pts += 1LL << st->pts_wrap_bits;
1077 }
1078
1079 /* Some MPEG-2 in MPEG-PS lack dts (issue #171 / input_file.mpg).
1080 * We take the conservative approach and discard both.
1081 * Note: If this is misbehaving for an H.264 file, then possibly
1082 * presentation_delayed is not set correctly. */
1083 if (delay == 1 && pkt->dts == pkt->pts &&
1084 pkt->dts != AV_NOPTS_VALUE && presentation_delayed) {
1085 av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination %"PRIi64"\n", pkt->dts);
1086 if ( strcmp(s->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2")
1087 && strcmp(s->iformat->name, "flv")) // otherwise we discard correct timestamps for vc1-wmapro.ism
1088 pkt->dts = AV_NOPTS_VALUE;
1089 }
1090
1091 duration = av_mul_q((AVRational) {pkt->duration, 1}, st->time_base);
1092 if (pkt->duration <= 0) {
1093 compute_frame_duration(s, &num, &den, st, pc, pkt);
1094 if (den && num) {
1095 duration = (AVRational) {num, den};
1096 pkt->duration = av_rescale_rnd(1,
1097 num * (int64_t) st->time_base.den,
1098 den * (int64_t) st->time_base.num,
1099 AV_ROUND_DOWN);
1100 }
1101 }
1102
1103 if (pkt->duration > 0 && (si->packet_buffer.head || si->parse_queue.head))
1104 update_initial_durations(s, st, pkt->stream_index, pkt->duration);
1105
1106 /* Correct timestamps with byte offset if demuxers only have timestamps
1107 * on packet boundaries */
1108 if (pc && sti->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) {
1109 /* this will estimate bitrate based on this frame's duration and size */
1110 offset = av_rescale(pc->offset, pkt->duration, pkt->size);
1111 if (pkt->pts != AV_NOPTS_VALUE)
1112 pkt->pts += offset;
1113 if (pkt->dts != AV_NOPTS_VALUE)
1114 pkt->dts += offset;
1115 }
1116
1117 /* This may be redundant, but it should not hurt. */
1118 if (pkt->dts != AV_NOPTS_VALUE &&
1119 pkt->pts != AV_NOPTS_VALUE &&
1120 pkt->pts > pkt->dts)
1121 presentation_delayed = 1;
1122
1123 if (s->debug & FF_FDEBUG_TS)
1124 av_log(s, AV_LOG_DEBUG,
1125 "IN delayed:%d pts:%s, dts:%s cur_dts:%s st:%d pc:%p duration:%"PRId64" delay:%d onein_oneout:%d\n",
1126 presentation_delayed, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts),
1127 pkt->stream_index, pc, pkt->duration, delay, onein_oneout);
1128
1129 /* Interpolate PTS and DTS if they are not present. We skip H264
1130 * currently because delay and has_b_frames are not reliably set. */
1131 if ((delay == 0 || (delay == 1 && pc)) &&
1132 onein_oneout) {
1133 if (presentation_delayed) {
1134 /* DTS = decompression timestamp */
1135 /* PTS = presentation timestamp */
1136 if (pkt->dts == AV_NOPTS_VALUE)
1137 pkt->dts = sti->last_IP_pts;
1138 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1139 if (pkt->dts == AV_NOPTS_VALUE)
1140 pkt->dts = sti->cur_dts;
1141
1142 /* This is tricky: the dts must be incremented by the duration
1143 * of the frame we are displaying, i.e. the last I- or P-frame. */
1144 if (sti->last_IP_duration == 0 && (uint64_t)pkt->duration <= INT32_MAX)
1145 sti->last_IP_duration = pkt->duration;
1146 if (pkt->dts != AV_NOPTS_VALUE)
1147 sti->cur_dts = av_sat_add64(pkt->dts, sti->last_IP_duration);
1148 if (pkt->dts != AV_NOPTS_VALUE &&
1149 pkt->pts == AV_NOPTS_VALUE &&
1150 sti->last_IP_duration > 0 &&
1151 ((uint64_t)sti->cur_dts - (uint64_t)next_dts + 1) <= 2 &&
1152 next_dts != next_pts &&
1153 next_pts != AV_NOPTS_VALUE)
1154 pkt->pts = next_dts;
1155
1156 if ((uint64_t)pkt->duration <= INT32_MAX)
1157 sti->last_IP_duration = pkt->duration;
1158 sti->last_IP_pts = pkt->pts;
1159 /* Cannot compute PTS if not present (we can compute it only
1160 * by knowing the future. */
1161 } else if (pkt->pts != AV_NOPTS_VALUE ||
1162 pkt->dts != AV_NOPTS_VALUE ||
1163 pkt->duration > 0 ) {
1164
1165 /* presentation is not delayed : PTS and DTS are the same */
1166 if (pkt->pts == AV_NOPTS_VALUE)
1167 pkt->pts = pkt->dts;
1168 update_initial_timestamps(s, pkt->stream_index, pkt->pts,
1169 pkt->pts, pkt);
1170 if (pkt->pts == AV_NOPTS_VALUE)
1171 pkt->pts = sti->cur_dts;
1172 pkt->dts = pkt->pts;
1173 if (pkt->pts != AV_NOPTS_VALUE && duration.num >= 0)
1174 sti->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1);
1175 }
1176 }
1177
1178 if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
1179 sti->pts_buffer[0] = pkt->pts;
1180 for (int i = 0; i < delay && sti->pts_buffer[i] > sti->pts_buffer[i + 1]; i++)
1181 FFSWAP(int64_t, sti->pts_buffer[i], sti->pts_buffer[i + 1]);
1182
1183 if (has_decode_delay_been_guessed(st))
1184 pkt->dts = select_from_pts_buffer(st, sti->pts_buffer, pkt->dts);
1185 }
1186 // We skipped it above so we try here.
1187 if (!onein_oneout)
1188 // This should happen on the first packet
1189 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts, pkt);
1190 if (pkt->dts > sti->cur_dts)
1191 sti->cur_dts = pkt->dts;
1192
1193 if (s->debug & FF_FDEBUG_TS)
1194 av_log(s, AV_LOG_DEBUG, "OUTdelayed:%d/%d pts:%s, dts:%s cur_dts:%s st:%d (%d)\n",
1195 presentation_delayed, delay, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts), st->index, st->id);
1196
1197 /* update flags */
1198 if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA || ff_is_intra_only(st->codecpar->codec_id))
1199 pkt->flags |= AV_PKT_FLAG_KEY;
1200 }
1201
1202 /**
1203 * Parse a packet, add all split parts to parse_queue.
1204 *
1205 * @param pkt Packet to parse; must not be NULL.
1206 * @param flush Indicates whether to flush. If set, pkt must be blank.
1207 */
1208 static int parse_packet(AVFormatContext *s, AVPacket *pkt,
1209 int stream_index, int flush)
1210 {
1211 FFFormatContext *const si = ffformatcontext(s);
1212 AVPacket *out_pkt = si->parse_pkt;
1213 AVStream *st = s->streams[stream_index];
1214 FFStream *const sti = ffstream(st);
1215 const uint8_t *data = pkt->data;
1216 int size = pkt->size;
1217 int ret = 0, got_output = flush;
1218
1219 if (!size && !flush && sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) {
1220 // preserve 0-size sync packets
1221 compute_pkt_fields(s, st, sti->parser, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
1222 }
1223
1224 while (size > 0 || (flush && got_output)) {
1225 int64_t next_pts = pkt->pts;
1226 int64_t next_dts = pkt->dts;
1227 int len;
1228
1229 len = av_parser_parse2(sti->parser, sti->avctx,
1230 &out_pkt->data, &out_pkt->size, data, size,
1231 pkt->pts, pkt->dts, pkt->pos);
1232 #ifdef OHOS_ABORT_FIX
1233 if (len == -0x20000000) {
1234 s->pb->error = AVERROR_INVALIDDATA;
1235 av_log(s, AV_LOG_ERROR, "Parser returned an error: %d\n", len);
1236 ret = AVERROR(EINVAL);
1237 goto fail;
1238 }
1239 #endif
1240
1241 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
1242 pkt->pos = -1;
1243 /* increment read pointer */
1244 av_assert1(data || !len);
1245 data = len ? data + len : data;
1246 size -= len;
1247
1248 got_output = !!out_pkt->size;
1249
1250 if (!out_pkt->size)
1251 continue;
1252
1253 if (pkt->buf && out_pkt->data == pkt->data) {
1254 /* reference pkt->buf only when out_pkt->data is guaranteed to point
1255 * to data in it and not in the parser's internal buffer. */
1256 /* XXX: Ensure this is the case with all parsers when sti->parser->flags
1257 * is PARSER_FLAG_COMPLETE_FRAMES and check for that instead? */
1258 out_pkt->buf = av_buffer_ref(pkt->buf);
1259 if (!out_pkt->buf) {
1260 ret = AVERROR(ENOMEM);
1261 goto fail;
1262 }
1263 } else {
1264 ret = av_packet_make_refcounted(out_pkt);
1265 if (ret < 0)
1266 goto fail;
1267 }
1268
1269 if (pkt->side_data) {
1270 out_pkt->side_data = pkt->side_data;
1271 out_pkt->side_data_elems = pkt->side_data_elems;
1272 pkt->side_data = NULL;
1273 pkt->side_data_elems = 0;
1274 }
1275
1276 /* set the duration */
1277 #ifdef OHOS_CHECK_NULL_PTR
1278 if (!sti || !sti->parser) {
1279 av_log(s, AV_LOG_ERROR, "Null pointer detected in parse_packet\n");
1280 ret = AVERROR(EINVAL);
1281 goto fail;
1282 }
1283 #endif
1284 out_pkt->duration = (sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0;
1285 #ifdef OHOS_AUXILIARY_TRACK
1286 if (need_parse_audio_info(st)) {
1287 #else
1288 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1289 #endif
1290 if (sti->avctx->sample_rate > 0) {
1291 out_pkt->duration =
1292 av_rescale_q_rnd(sti->parser->duration,
1293 (AVRational) { 1, sti->avctx->sample_rate },
1294 st->time_base,
1295 AV_ROUND_DOWN);
1296 }
1297 }
1298
1299 out_pkt->stream_index = st->index;
1300 out_pkt->pts = sti->parser->pts;
1301 out_pkt->dts = sti->parser->dts;
1302 out_pkt->pos = sti->parser->pos;
1303 out_pkt->flags |= pkt->flags & (AV_PKT_FLAG_DISCARD | AV_PKT_FLAG_CORRUPT);
1304
1305 if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1306 out_pkt->pos = sti->parser->frame_offset;
1307
1308 if (sti->parser->key_frame == 1 ||
1309 (sti->parser->key_frame == -1 &&
1310 sti->parser->pict_type == AV_PICTURE_TYPE_I))
1311 out_pkt->flags |= AV_PKT_FLAG_KEY;
1312
1313 if (sti->parser->key_frame == -1 && sti->parser->pict_type ==AV_PICTURE_TYPE_NONE && (pkt->flags&AV_PKT_FLAG_KEY))
1314 out_pkt->flags |= AV_PKT_FLAG_KEY;
1315
1316 compute_pkt_fields(s, st, sti->parser, out_pkt, next_dts, next_pts);
1317
1318 ret = avpriv_packet_list_put(&si->parse_queue,
1319 out_pkt, NULL, 0);
1320 if (ret < 0)
1321 goto fail;
1322 }
1323
1324 /* end of the stream => close and free the parser */
1325 if (flush) {
1326 av_parser_close(sti->parser);
1327 sti->parser = NULL;
1328 }
1329
1330 fail:
1331 if (ret < 0)
1332 av_packet_unref(out_pkt);
1333 av_packet_unref(pkt);
1334 return ret;
1335 }
1336
1337 static int64_t ts_to_samples(AVStream *st, int64_t ts)
1338 {
1339 return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den);
1340 }
1341
1342 static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
1343 {
1344 FFFormatContext *const si = ffformatcontext(s);
1345 int ret, got_packet = 0;
1346 #ifdef OHOS_ABORT_FIX
1347 int index = 0;
1348 #endif
1349 AVDictionary *metadata = NULL;
1350
1351 while (!got_packet && !si->parse_queue.head) {
1352 AVStream *st;
1353 FFStream *sti;
1354
1355 /* read next packet */
1356 ret = ff_read_packet(s, pkt);
1357 if (ret < 0) {
1358 if (ret == AVERROR(EAGAIN))
1359 return ret;
1360 /* flush the parsers */
1361 for (unsigned i = 0; i < s->nb_streams; i++) {
1362 AVStream *const st = s->streams[i];
1363 FFStream *const sti = ffstream(st);
1364 #ifdef OHOS_ABORT_FIX
1365 if (sti->parser && sti->need_parsing) {
1366 index = parse_packet(s, pkt, st->index, 1);
1367 if (index < 0)
1368 return index;
1369 }
1370 #else
1371 if (sti->parser && sti->need_parsing)
1372 parse_packet(s, pkt, st->index, 1);
1373 #endif
1374 }
1375 /* all remaining packets are now in parse_queue =>
1376 * really terminate parsing */
1377 break;
1378 }
1379 ret = 0;
1380 st = s->streams[pkt->stream_index];
1381 sti = ffstream(st);
1382
1383 st->event_flags |= AVSTREAM_EVENT_FLAG_NEW_PACKETS;
1384
1385 /* update context if required */
1386 if (sti->need_context_update) {
1387 if (avcodec_is_open(sti->avctx)) {
1388 av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
1389 avcodec_close(sti->avctx);
1390 sti->info->found_decoder = 0;
1391 }
1392
1393 /* close parser, because it depends on the codec */
1394 if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) {
1395 av_parser_close(sti->parser);
1396 sti->parser = NULL;
1397 }
1398
1399 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
1400 if (ret < 0) {
1401 av_packet_unref(pkt);
1402 return ret;
1403 }
1404
1405 sti->need_context_update = 0;
1406 }
1407
1408 if (pkt->pts != AV_NOPTS_VALUE &&
1409 pkt->dts != AV_NOPTS_VALUE &&
1410 pkt->pts < pkt->dts) {
1411 av_log(s, AV_LOG_WARNING,
1412 "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n",
1413 pkt->stream_index,
1414 av_ts2str(pkt->pts),
1415 av_ts2str(pkt->dts),
1416 pkt->size);
1417 }
1418 if (s->debug & FF_FDEBUG_TS)
1419 av_log(s, AV_LOG_DEBUG,
1420 "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n",
1421 pkt->stream_index,
1422 av_ts2str(pkt->pts),
1423 av_ts2str(pkt->dts),
1424 pkt->size, pkt->duration, pkt->flags);
1425
1426 if (sti->need_parsing && !sti->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
1427 sti->parser = av_parser_init(st->codecpar->codec_id);
1428 if (!sti->parser) {
1429 av_log(s, AV_LOG_VERBOSE, "parser not found for codec "
1430 "%s, packets or times may be invalid.\n",
1431 avcodec_get_name(st->codecpar->codec_id));
1432 /* no parser available: just output the raw packets */
1433 sti->need_parsing = AVSTREAM_PARSE_NONE;
1434 } else if (sti->need_parsing == AVSTREAM_PARSE_HEADERS)
1435 sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
1436 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
1437 sti->parser->flags |= PARSER_FLAG_ONCE;
1438 else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1439 sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
1440 }
1441
1442 if (!sti->need_parsing || !sti->parser) {
1443 /* no parsing needed: we just output the packet as is */
1444 compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
1445 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
1446 (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
1447 ff_reduce_index(s, st->index);
1448 av_add_index_entry(st, pkt->pos, pkt->dts,
1449 0, 0, AVINDEX_KEYFRAME);
1450 }
1451 got_packet = 1;
1452 } else if (st->discard < AVDISCARD_ALL) {
1453 if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0)
1454 return ret;
1455 st->codecpar->sample_rate = sti->avctx->sample_rate;
1456 st->codecpar->bit_rate = sti->avctx->bit_rate;
1457 #if FF_API_OLD_CHANNEL_LAYOUT
1458 FF_DISABLE_DEPRECATION_WARNINGS
1459 st->codecpar->channels = sti->avctx->ch_layout.nb_channels;
1460 st->codecpar->channel_layout = sti->avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
1461 sti->avctx->ch_layout.u.mask : 0;
1462 FF_ENABLE_DEPRECATION_WARNINGS
1463 #endif
1464 ret = av_channel_layout_copy(&st->codecpar->ch_layout, &sti->avctx->ch_layout);
1465 if (ret < 0)
1466 return ret;
1467 st->codecpar->codec_id = sti->avctx->codec_id;
1468 } else {
1469 /* free packet */
1470 av_packet_unref(pkt);
1471 }
1472 if (pkt->flags & AV_PKT_FLAG_KEY)
1473 sti->skip_to_keyframe = 0;
1474 if (sti->skip_to_keyframe) {
1475 av_packet_unref(pkt);
1476 got_packet = 0;
1477 }
1478 }
1479
1480 if (!got_packet && si->parse_queue.head)
1481 ret = avpriv_packet_list_get(&si->parse_queue, pkt);
1482
1483 if (ret >= 0) {
1484 AVStream *const st = s->streams[pkt->stream_index];
1485 FFStream *const sti = ffstream(st);
1486 int discard_padding = 0;
1487 if (sti->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
1488 int64_t pts = pkt->pts - (is_relative(pkt->pts) ? RELATIVE_TS_BASE : 0);
1489 int64_t sample = ts_to_samples(st, pts);
1490 int64_t duration = ts_to_samples(st, pkt->duration);
1491 int64_t end_sample = sample + duration;
1492 if (duration > 0 && end_sample >= sti->first_discard_sample &&
1493 sample < sti->last_discard_sample)
1494 discard_padding = FFMIN(end_sample - sti->first_discard_sample, duration);
1495 }
1496 if (sti->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
1497 sti->skip_samples = sti->start_skip_samples;
1498 sti->skip_samples = FFMAX(0, sti->skip_samples);
1499 if (sti->skip_samples || discard_padding) {
1500 uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
1501 if (p) {
1502 AV_WL32(p, sti->skip_samples);
1503 AV_WL32(p + 4, discard_padding);
1504 av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %u / discard %u\n",
1505 (unsigned)sti->skip_samples, (unsigned)discard_padding);
1506 }
1507 sti->skip_samples = 0;
1508 }
1509
1510 if (sti->inject_global_side_data) {
1511 for (int i = 0; i < st->nb_side_data; i++) {
1512 const AVPacketSideData *const src_sd = &st->side_data[i];
1513 uint8_t *dst_data;
1514
1515 if (av_packet_get_side_data(pkt, src_sd->type, NULL))
1516 continue;
1517
1518 dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
1519 if (!dst_data) {
1520 av_log(s, AV_LOG_WARNING, "Could not inject global side data\n");
1521 continue;
1522 }
1523
1524 memcpy(dst_data, src_sd->data, src_sd->size);
1525 }
1526 sti->inject_global_side_data = 0;
1527 }
1528 }
1529
1530 if (!si->metafree) {
1531 int metaret = av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
1532 if (metadata) {
1533 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
1534 av_dict_copy(&s->metadata, metadata, 0);
1535 av_dict_free(&metadata);
1536 av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN);
1537 }
1538 si->metafree = metaret == AVERROR_OPTION_NOT_FOUND;
1539 }
1540
1541 if (s->debug & FF_FDEBUG_TS)
1542 av_log(s, AV_LOG_DEBUG,
1543 "read_frame_internal stream=%d, pts=%s, dts=%s, "
1544 "size=%d, duration=%"PRId64", flags=%d\n",
1545 pkt->stream_index,
1546 av_ts2str(pkt->pts),
1547 av_ts2str(pkt->dts),
1548 pkt->size, pkt->duration, pkt->flags);
1549
1550 /* A demuxer might have returned EOF because of an IO error, let's
1551 * propagate this back to the user. */
1552 if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN))
1553 ret = s->pb->error;
1554
1555 return ret;
1556 }
1557
1558 int av_read_frame(AVFormatContext *s, AVPacket *pkt)
1559 {
1560 FFFormatContext *const si = ffformatcontext(s);
1561 const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1562 int eof = 0;
1563 int ret;
1564 AVStream *st;
1565
1566 if (!genpts) {
1567 ret = si->packet_buffer.head
1568 ? avpriv_packet_list_get(&si->packet_buffer, pkt)
1569 : read_frame_internal(s, pkt);
1570 if (ret < 0)
1571 return ret;
1572 goto return_packet;
1573 }
1574
1575 for (;;) {
1576 PacketListEntry *pktl = si->packet_buffer.head;
1577
1578 if (pktl) {
1579 AVPacket *next_pkt = &pktl->pkt;
1580
1581 if (next_pkt->dts != AV_NOPTS_VALUE) {
1582 int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1583 // last dts seen for this stream. if any of packets following
1584 // current one had no dts, we will set this to AV_NOPTS_VALUE.
1585 int64_t last_dts = next_pkt->dts;
1586 av_assert2(wrap_bits <= 64);
1587 while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
1588 if (pktl->pkt.stream_index == next_pkt->stream_index &&
1589 av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2ULL << (wrap_bits - 1)) < 0) {
1590 if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2ULL << (wrap_bits - 1))) {
1591 // not B-frame
1592 next_pkt->pts = pktl->pkt.dts;
1593 }
1594 if (last_dts != AV_NOPTS_VALUE) {
1595 // Once last dts was set to AV_NOPTS_VALUE, we don't change it.
1596 last_dts = pktl->pkt.dts;
1597 }
1598 }
1599 pktl = pktl->next;
1600 }
1601 if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) {
1602 // Fixing the last reference frame had none pts issue (For MXF etc).
1603 // We only do this when
1604 // 1. eof.
1605 // 2. we are not able to resolve a pts value for current packet.
1606 // 3. the packets for this stream at the end of the files had valid dts.
1607 next_pkt->pts = last_dts + next_pkt->duration;
1608 }
1609 pktl = si->packet_buffer.head;
1610 }
1611
1612 /* read packet from packet buffer, if there is data */
1613 st = s->streams[next_pkt->stream_index];
1614 if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
1615 next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
1616 ret = avpriv_packet_list_get(&si->packet_buffer, pkt);
1617 goto return_packet;
1618 }
1619 }
1620
1621 ret = read_frame_internal(s, pkt);
1622 if (ret < 0) {
1623 if (pktl && ret != AVERROR(EAGAIN)) {
1624 eof = 1;
1625 continue;
1626 } else
1627 return ret;
1628 }
1629
1630 ret = avpriv_packet_list_put(&si->packet_buffer,
1631 pkt, NULL, 0);
1632 if (ret < 0) {
1633 av_packet_unref(pkt);
1634 return ret;
1635 }
1636 }
1637
1638 return_packet:
1639 st = s->streams[pkt->stream_index];
1640 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
1641 ff_reduce_index(s, st->index);
1642 av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
1643 }
1644
1645 if (is_relative(pkt->dts))
1646 pkt->dts -= RELATIVE_TS_BASE;
1647 if (is_relative(pkt->pts))
1648 pkt->pts -= RELATIVE_TS_BASE;
1649
1650 return ret;
1651 }
1652
1653 /**
1654 * Return TRUE if the stream has accurate duration in any stream.
1655 *
1656 * @return TRUE if the stream has accurate duration for at least one component.
1657 */
1658 static int has_duration(AVFormatContext *ic)
1659 {
1660 for (unsigned i = 0; i < ic->nb_streams; i++) {
1661 const AVStream *const st = ic->streams[i];
1662 if (st->duration != AV_NOPTS_VALUE)
1663 return 1;
1664 }
1665 if (ic->duration != AV_NOPTS_VALUE)
1666 return 1;
1667 return 0;
1668 }
1669
1670 /**
1671 * Estimate the stream timings from the one of each components.
1672 *
1673 * Also computes the global bitrate if possible.
1674 */
1675 static void update_stream_timings(AVFormatContext *ic)
1676 {
1677 int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text;
1678 int64_t duration, duration1, duration_text, filesize;
1679
1680 start_time = INT64_MAX;
1681 start_time_text = INT64_MAX;
1682 end_time = INT64_MIN;
1683 end_time_text = INT64_MIN;
1684 duration = INT64_MIN;
1685 duration_text = INT64_MIN;
1686
1687 for (unsigned i = 0; i < ic->nb_streams; i++) {
1688 AVStream *const st = ic->streams[i];
1689 int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ||
1690 st->codecpar->codec_type == AVMEDIA_TYPE_DATA;
1691
1692 if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
1693 start_time1 = av_rescale_q(st->start_time, st->time_base,
1694 AV_TIME_BASE_Q);
1695 if (is_text)
1696 start_time_text = FFMIN(start_time_text, start_time1);
1697 else
1698 start_time = FFMIN(start_time, start_time1);
1699 end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
1700 AV_TIME_BASE_Q,
1701 AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
1702 if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) {
1703 end_time1 += start_time1;
1704 if (is_text)
1705 end_time_text = FFMAX(end_time_text, end_time1);
1706 else
1707 end_time = FFMAX(end_time, end_time1);
1708 }
1709 for (AVProgram *p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) {
1710 if (p->start_time == AV_NOPTS_VALUE || p->start_time > start_time1)
1711 p->start_time = start_time1;
1712 if (p->end_time < end_time1)
1713 p->end_time = end_time1;
1714 }
1715 }
1716 if (st->duration != AV_NOPTS_VALUE) {
1717 duration1 = av_rescale_q(st->duration, st->time_base,
1718 AV_TIME_BASE_Q);
1719 if (is_text)
1720 duration_text = FFMAX(duration_text, duration1);
1721 else
1722 duration = FFMAX(duration, duration1);
1723 }
1724 }
1725 if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE))
1726 start_time = start_time_text;
1727 else if (start_time > start_time_text)
1728 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE);
1729
1730 if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE))
1731 end_time = end_time_text;
1732 else if (end_time < end_time_text)
1733 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream endtime %f\n", end_time_text / (float)AV_TIME_BASE);
1734
1735 if (duration == INT64_MIN || (duration < duration_text && (uint64_t)duration_text - duration < AV_TIME_BASE))
1736 duration = duration_text;
1737 else if (duration < duration_text)
1738 av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream duration %f\n", duration_text / (float)AV_TIME_BASE);
1739
1740 if (start_time != INT64_MAX) {
1741 ic->start_time = start_time;
1742 if (end_time != INT64_MIN) {
1743 if (ic->nb_programs > 1) {
1744 for (unsigned i = 0; i < ic->nb_programs; i++) {
1745 AVProgram *const p = ic->programs[i];
1746
1747 if (p->start_time != AV_NOPTS_VALUE &&
1748 p->end_time > p->start_time &&
1749 p->end_time - (uint64_t)p->start_time <= INT64_MAX)
1750 duration = FFMAX(duration, p->end_time - p->start_time);
1751 }
1752 } else if (end_time >= start_time && end_time - (uint64_t)start_time <= INT64_MAX) {
1753 duration = FFMAX(duration, end_time - start_time);
1754 }
1755 }
1756 }
1757 if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) {
1758 ic->duration = duration;
1759 }
1760 if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) {
1761 /* compute the bitrate */
1762 double bitrate = (double) filesize * 8.0 * AV_TIME_BASE /
1763 (double) ic->duration;
1764 if (bitrate >= 0 && bitrate <= INT64_MAX)
1765 ic->bit_rate = bitrate;
1766 }
1767 }
1768
1769 static void fill_all_stream_timings(AVFormatContext *ic)
1770 {
1771 update_stream_timings(ic);
1772 for (unsigned i = 0; i < ic->nb_streams; i++) {
1773 AVStream *const st = ic->streams[i];
1774
1775 if (st->start_time == AV_NOPTS_VALUE) {
1776 if (ic->start_time != AV_NOPTS_VALUE)
1777 st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q,
1778 st->time_base);
1779 if (ic->duration != AV_NOPTS_VALUE)
1780 st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q,
1781 st->time_base);
1782 }
1783 }
1784 }
1785
1786 static void estimate_timings_from_bit_rate(AVFormatContext *ic)
1787 {
1788 FFFormatContext *const si = ffformatcontext(ic);
1789 int show_warning = 0;
1790
1791 /* if bit_rate is already set, we believe it */
1792 if (ic->bit_rate <= 0) {
1793 int64_t bit_rate = 0;
1794 for (unsigned i = 0; i < ic->nb_streams; i++) {
1795 const AVStream *const st = ic->streams[i];
1796 const FFStream *const sti = cffstream(st);
1797 if (st->codecpar->bit_rate <= 0 && sti->avctx->bit_rate > 0)
1798 st->codecpar->bit_rate = sti->avctx->bit_rate;
1799 if (st->codecpar->bit_rate > 0) {
1800 if (INT64_MAX - st->codecpar->bit_rate < bit_rate) {
1801 bit_rate = 0;
1802 break;
1803 }
1804 bit_rate += st->codecpar->bit_rate;
1805 #ifdef OHOS_AUXILIARY_TRACK
1806 } else if (need_parse_video_info(st) && sti->codec_info_nb_frames > 1) {
1807 #else
1808 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->codec_info_nb_frames > 1) {
1809 #endif
1810 // If we have a videostream with packets but without a bitrate
1811 // then consider the sum not known
1812 bit_rate = 0;
1813 break;
1814 }
1815 }
1816 ic->bit_rate = bit_rate;
1817 }
1818
1819 /* if duration is already set, we believe it */
1820 if (ic->duration == AV_NOPTS_VALUE &&
1821 ic->bit_rate != 0) {
1822 int64_t filesize = ic->pb ? avio_size(ic->pb) : 0;
1823 if (filesize > si->data_offset) {
1824 filesize -= si->data_offset;
1825 for (unsigned i = 0; i < ic->nb_streams; i++) {
1826 AVStream *const st = ic->streams[i];
1827
1828 if ( st->time_base.num <= INT64_MAX / ic->bit_rate
1829 && st->duration == AV_NOPTS_VALUE) {
1830 st->duration = av_rescale(filesize, 8LL * st->time_base.den,
1831 ic->bit_rate *
1832 (int64_t) st->time_base.num);
1833 show_warning = 1;
1834 }
1835 }
1836 }
1837 }
1838 if (show_warning)
1839 av_log(ic, AV_LOG_WARNING,
1840 "Estimating duration from bitrate, this may be inaccurate\n");
1841 }
1842
1843 #define DURATION_MAX_READ_SIZE 250000LL
1844 #define DURATION_MAX_RETRY 6
1845
1846 /* only usable for MPEG-PS streams */
1847 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
1848 {
1849 FFFormatContext *const si = ffformatcontext(ic);
1850 AVPacket *const pkt = si->pkt;
1851 int num, den, read_size, ret;
1852 int found_duration = 0;
1853 int is_end;
1854 int64_t filesize, offset, duration;
1855 int retry = 0;
1856
1857 /* flush packet queue */
1858 ff_flush_packet_queue(ic);
1859
1860 for (unsigned i = 0; i < ic->nb_streams; i++) {
1861 AVStream *const st = ic->streams[i];
1862 FFStream *const sti = ffstream(st);
1863
1864 if (st->start_time == AV_NOPTS_VALUE &&
1865 sti->first_dts == AV_NOPTS_VALUE &&
1866 st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN)
1867 av_log(ic, AV_LOG_WARNING,
1868 "start time for stream %d is not set in estimate_timings_from_pts\n", i);
1869
1870 if (sti->parser) {
1871 av_parser_close(sti->parser);
1872 sti->parser = NULL;
1873 }
1874 }
1875
1876 if (ic->skip_estimate_duration_from_pts) {
1877 av_log(ic, AV_LOG_INFO, "Skipping duration calculation in estimate_timings_from_pts\n");
1878 goto skip_duration_calc;
1879 }
1880
1881 av_opt_set_int(ic, "skip_changes", 1, AV_OPT_SEARCH_CHILDREN);
1882 /* estimate the end time (duration) */
1883 /* XXX: may need to support wrapping */
1884 filesize = ic->pb ? avio_size(ic->pb) : 0;
1885 do {
1886 is_end = found_duration;
1887 offset = filesize - (DURATION_MAX_READ_SIZE << retry);
1888 if (offset < 0)
1889 offset = 0;
1890
1891 avio_seek(ic->pb, offset, SEEK_SET);
1892 read_size = 0;
1893 for (;;) {
1894 AVStream *st;
1895 FFStream *sti;
1896 if (read_size >= DURATION_MAX_READ_SIZE << (FFMAX(retry - 1, 0)))
1897 break;
1898
1899 do {
1900 ret = ff_read_packet(ic, pkt);
1901 } while (ret == AVERROR(EAGAIN));
1902 if (ret != 0)
1903 break;
1904 read_size += pkt->size;
1905 st = ic->streams[pkt->stream_index];
1906 sti = ffstream(st);
1907 if (pkt->pts != AV_NOPTS_VALUE &&
1908 (st->start_time != AV_NOPTS_VALUE ||
1909 sti->first_dts != AV_NOPTS_VALUE)) {
1910 if (pkt->duration == 0) {
1911 compute_frame_duration(ic, &num, &den, st, sti->parser, pkt);
1912 if (den && num) {
1913 pkt->duration = av_rescale_rnd(1,
1914 num * (int64_t) st->time_base.den,
1915 den * (int64_t) st->time_base.num,
1916 AV_ROUND_DOWN);
1917 }
1918 }
1919 duration = pkt->pts + pkt->duration;
1920 found_duration = 1;
1921 if (st->start_time != AV_NOPTS_VALUE)
1922 duration -= st->start_time;
1923 else
1924 duration -= sti->first_dts;
1925 if (duration > 0) {
1926 if (st->duration == AV_NOPTS_VALUE || sti->info->last_duration<= 0 ||
1927 (st->duration < duration && FFABS(duration - sti->info->last_duration) < 60LL*st->time_base.den / st->time_base.num))
1928 st->duration = duration;
1929 sti->info->last_duration = duration;
1930 }
1931 }
1932 av_packet_unref(pkt);
1933 }
1934
1935 /* check if all audio/video streams have valid duration */
1936 if (!is_end) {
1937 is_end = 1;
1938 for (unsigned i = 0; i < ic->nb_streams; i++) {
1939 const AVStream *const st = ic->streams[i];
1940 switch (st->codecpar->codec_type) {
1941 case AVMEDIA_TYPE_VIDEO:
1942 case AVMEDIA_TYPE_AUDIO:
1943 #ifdef OHOS_AUXILIARY_TRACK
1944 case AVMEDIA_TYPE_AUXILIARY:
1945 #endif
1946 if (st->duration == AV_NOPTS_VALUE)
1947 is_end = 0;
1948 }
1949 }
1950 }
1951 } while (!is_end &&
1952 offset &&
1953 ++retry <= DURATION_MAX_RETRY);
1954
1955 av_opt_set_int(ic, "skip_changes", 0, AV_OPT_SEARCH_CHILDREN);
1956
1957 /* warn about audio/video streams which duration could not be estimated */
1958 for (unsigned i = 0; i < ic->nb_streams; i++) {
1959 const AVStream *const st = ic->streams[i];
1960 const FFStream *const sti = cffstream(st);
1961
1962 if (st->duration == AV_NOPTS_VALUE) {
1963 switch (st->codecpar->codec_type) {
1964 case AVMEDIA_TYPE_VIDEO:
1965 case AVMEDIA_TYPE_AUDIO:
1966 #ifdef OHOS_AUXILIARY_TRACK
1967 case AVMEDIA_TYPE_AUXILIARY:
1968 #endif
1969 if (st->start_time != AV_NOPTS_VALUE || sti->first_dts != AV_NOPTS_VALUE) {
1970 av_log(ic, AV_LOG_WARNING, "stream %d : no PTS found at end of file, duration not set\n", i);
1971 } else
1972 av_log(ic, AV_LOG_WARNING, "stream %d : no TS found at start of file, duration not set\n", i);
1973 }
1974 }
1975 }
1976 skip_duration_calc:
1977 fill_all_stream_timings(ic);
1978
1979 avio_seek(ic->pb, old_offset, SEEK_SET);
1980 for (unsigned i = 0; i < ic->nb_streams; i++) {
1981 AVStream *const st = ic->streams[i];
1982 FFStream *const sti = ffstream(st);
1983
1984 sti->cur_dts = sti->first_dts;
1985 sti->last_IP_pts = AV_NOPTS_VALUE;
1986 sti->last_dts_for_order_check = AV_NOPTS_VALUE;
1987 for (int j = 0; j < MAX_REORDER_DELAY + 1; j++)
1988 sti->pts_buffer[j] = AV_NOPTS_VALUE;
1989 }
1990 }
1991
1992 /* 1:1 map to AVDurationEstimationMethod */
1993 static const char *const duration_name[] = {
1994 [AVFMT_DURATION_FROM_PTS] = "pts",
1995 [AVFMT_DURATION_FROM_STREAM] = "stream",
1996 [AVFMT_DURATION_FROM_BITRATE] = "bit rate",
1997 };
1998
1999 static const char *duration_estimate_name(enum AVDurationEstimationMethod method)
2000 {
2001 return duration_name[method];
2002 }
2003
2004 static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
2005 {
2006 int64_t file_size;
2007
2008 /* get the file size, if possible */
2009 if (ic->iformat->flags & AVFMT_NOFILE) {
2010 file_size = 0;
2011 } else {
2012 file_size = avio_size(ic->pb);
2013 file_size = FFMAX(0, file_size);
2014 }
2015
2016 if ((!strcmp(ic->iformat->name, "mpeg") ||
2017 !strcmp(ic->iformat->name, "mpegts")) &&
2018 file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
2019 /* get accurate estimate from the PTSes */
2020 estimate_timings_from_pts(ic, old_offset);
2021 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
2022 } else if (has_duration(ic)) {
2023 /* at least one component has timings - we use them for all
2024 * the components */
2025 fill_all_stream_timings(ic);
2026 /* nut demuxer estimate the duration from PTS */
2027 if (!strcmp(ic->iformat->name, "nut"))
2028 ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
2029 else
2030 ic->duration_estimation_method = AVFMT_DURATION_FROM_STREAM;
2031 } else {
2032 /* less precise: use bitrate info */
2033 estimate_timings_from_bit_rate(ic);
2034 ic->duration_estimation_method = AVFMT_DURATION_FROM_BITRATE;
2035 }
2036 update_stream_timings(ic);
2037
2038 for (unsigned i = 0; i < ic->nb_streams; i++) {
2039 AVStream *const st = ic->streams[i];
2040 if (st->time_base.den)
2041 av_log(ic, AV_LOG_TRACE, "stream %u: start_time: %s duration: %s\n", i,
2042 av_ts2timestr(st->start_time, &st->time_base),
2043 av_ts2timestr(st->duration, &st->time_base));
2044 }
2045 av_log(ic, AV_LOG_TRACE,
2046 "format: start_time: %s duration: %s (estimate from %s) bitrate=%"PRId64" kb/s\n",
2047 av_ts2timestr(ic->start_time, &AV_TIME_BASE_Q),
2048 av_ts2timestr(ic->duration, &AV_TIME_BASE_Q),
2049 duration_estimate_name(ic->duration_estimation_method),
2050 (int64_t)ic->bit_rate / 1000);
2051 }
2052
2053 static int determinable_frame_size(const AVCodecContext *avctx)
2054 {
2055 switch(avctx->codec_id) {
2056 case AV_CODEC_ID_MP1:
2057 case AV_CODEC_ID_MP2:
2058 case AV_CODEC_ID_MP3:
2059 case AV_CODEC_ID_CODEC2:
2060 return 1;
2061 }
2062
2063 return 0;
2064 }
2065
2066 static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr)
2067 {
2068 const FFStream *const sti = cffstream(st);
2069 const AVCodecContext *const avctx = sti->avctx;
2070
2071 #define FAIL(errmsg) do { \
2072 if (errmsg_ptr) \
2073 *errmsg_ptr = errmsg; \
2074 return 0; \
2075 } while (0)
2076
2077 if ( avctx->codec_id == AV_CODEC_ID_NONE
2078 && avctx->codec_type != AVMEDIA_TYPE_DATA)
2079 FAIL("unknown codec");
2080 #ifdef OHOS_AUXILIARY_TRACK
2081 if (need_parse_audio_info(st)) {
2082 #else
2083 switch (avctx->codec_type) {
2084 case AVMEDIA_TYPE_AUDIO:
2085 #endif
2086 if (!avctx->frame_size && determinable_frame_size(avctx))
2087 FAIL("unspecified frame size");
2088 if (sti->info->found_decoder >= 0 &&
2089 avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
2090 FAIL("unspecified sample format");
2091 if (!avctx->sample_rate)
2092 FAIL("unspecified sample rate");
2093 if (!avctx->ch_layout.nb_channels)
2094 FAIL("unspecified number of channels");
2095 if (sti->info->found_decoder >= 0 && !sti->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
2096 FAIL("no decodable DTS frames");
2097 #ifdef OHOS_AUXILIARY_TRACK
2098 } else if (need_parse_video_info(st)) {
2099 #else
2100 break;
2101 case AVMEDIA_TYPE_VIDEO:
2102 #endif
2103 if (!avctx->width)
2104 #ifdef OHOS_OPTIMIZE_DELAY
2105 if (!(st->codecpar->codec_id == AV_CODEC_ID_HEVC) &&
2106 !((unsigned int)st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2107 #endif
2108 FAIL("unspecified size");
2109 if (sti->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
2110 FAIL("unspecified pixel format");
2111 if (st->codecpar->codec_id == AV_CODEC_ID_RV30 || st->codecpar->codec_id == AV_CODEC_ID_RV40)
2112 if (!st->sample_aspect_ratio.num && !st->codecpar->sample_aspect_ratio.num && !sti->codec_info_nb_frames)
2113 FAIL("no frame in rv30/40 and no sar");
2114 #ifdef OHOS_AUXILIARY_TRACK
2115 } else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2116 #else
2117 break;
2118 case AVMEDIA_TYPE_SUBTITLE:
2119 #endif
2120 if (avctx->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !avctx->width)
2121 FAIL("unspecified size");
2122 #ifdef OHOS_AUXILIARY_TRACK
2123 } else if (avctx->codec_type == AVMEDIA_TYPE_DATA) {
2124 #else
2125 break;
2126 case AVMEDIA_TYPE_DATA:
2127 #endif
2128 if (avctx->codec_id == AV_CODEC_ID_NONE) return 1;
2129 }
2130
2131 return 1;
2132 }
2133
2134 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
2135 static int try_decode_frame(AVFormatContext *s, AVStream *st,
2136 const AVPacket *avpkt, AVDictionary **options)
2137 {
2138 FFStream *const sti = ffstream(st);
2139 AVCodecContext *const avctx = sti->avctx;
2140 const AVCodec *codec;
2141 int got_picture = 1, ret = 0;
2142 AVFrame *frame = av_frame_alloc();
2143 AVSubtitle subtitle;
2144 AVPacket pkt = *avpkt;
2145 int do_skip_frame = 0;
2146 enum AVDiscard skip_frame;
2147
2148 if (!frame)
2149 return AVERROR(ENOMEM);
2150
2151 if (!avcodec_is_open(avctx) &&
2152 sti->info->found_decoder <= 0 &&
2153 (st->codecpar->codec_id != -sti->info->found_decoder || !st->codecpar->codec_id)) {
2154 AVDictionary *thread_opt = NULL;
2155
2156 codec = find_probe_decoder(s, st, st->codecpar->codec_id);
2157
2158 if (!codec) {
2159 sti->info->found_decoder = -st->codecpar->codec_id;
2160 ret = -1;
2161 goto fail;
2162 }
2163
2164 /* Force thread count to 1 since the H.264 decoder will not extract
2165 * SPS and PPS to extradata during multi-threaded decoding. */
2166 av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
2167 /* Force lowres to 0. The decoder might reduce the video size by the
2168 * lowres factor, and we don't want that propagated to the stream's
2169 * codecpar */
2170 av_dict_set(options ? options : &thread_opt, "lowres", "0", 0);
2171 if (s->codec_whitelist)
2172 av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0);
2173 ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);
2174 if (!options)
2175 av_dict_free(&thread_opt);
2176 if (ret < 0) {
2177 sti->info->found_decoder = -avctx->codec_id;
2178 goto fail;
2179 }
2180 sti->info->found_decoder = 1;
2181 } else if (!sti->info->found_decoder)
2182 sti->info->found_decoder = 1;
2183
2184 if (sti->info->found_decoder < 0) {
2185 ret = -1;
2186 goto fail;
2187 }
2188
2189 if (avpriv_codec_get_cap_skip_frame_fill_param(avctx->codec)) {
2190 do_skip_frame = 1;
2191 skip_frame = avctx->skip_frame;
2192 avctx->skip_frame = AVDISCARD_ALL;
2193 }
2194
2195 while ((pkt.size > 0 || (!pkt.data && got_picture)) &&
2196 ret >= 0 &&
2197 (!has_codec_parameters(st, NULL) || !has_decode_delay_been_guessed(st) ||
2198 (!sti->codec_info_nb_frames &&
2199 (avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) {
2200 got_picture = 0;
2201 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
2202 #ifdef OHOS_AUXILIARY_TRACK
2203 avctx->codec_type == AVMEDIA_TYPE_AUDIO || avctx->codec_type == AVMEDIA_TYPE_AUXILIARY) {
2204 #else
2205 avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2206 #endif
2207 ret = avcodec_send_packet(avctx, &pkt);
2208 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2209 break;
2210 if (ret >= 0)
2211 pkt.size = 0;
2212 ret = avcodec_receive_frame(avctx, frame);
2213 if (ret >= 0)
2214 got_picture = 1;
2215 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
2216 ret = 0;
2217 } else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2218 ret = avcodec_decode_subtitle2(avctx, &subtitle,
2219 &got_picture, &pkt);
2220 if (got_picture)
2221 avsubtitle_free(&subtitle);
2222 if (ret >= 0)
2223 pkt.size = 0;
2224 }
2225 if (ret >= 0) {
2226 if (got_picture)
2227 sti->nb_decoded_frames++;
2228 ret = got_picture;
2229 }
2230 }
2231
2232 fail:
2233 if (do_skip_frame) {
2234 avctx->skip_frame = skip_frame;
2235 }
2236
2237 av_frame_free(&frame);
2238 return ret;
2239 }
2240
2241 static int chapter_start_cmp(const void *p1, const void *p2)
2242 {
2243 const AVChapter *const ch1 = *(AVChapter**)p1;
2244 const AVChapter *const ch2 = *(AVChapter**)p2;
2245 int delta = av_compare_ts(ch1->start, ch1->time_base, ch2->start, ch2->time_base);
2246 if (delta)
2247 return delta;
2248 return FFDIFFSIGN(ch1->id, ch2->id);
2249 }
2250
2251 static int compute_chapters_end(AVFormatContext *s)
2252 {
2253 int64_t max_time = 0;
2254 AVChapter **timetable;
2255
2256 if (!s->nb_chapters)
2257 return 0;
2258
2259 if (s->duration > 0 && s->start_time < INT64_MAX - s->duration)
2260 max_time = s->duration +
2261 ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
2262
2263 timetable = av_memdup(s->chapters, s->nb_chapters * sizeof(*timetable));
2264 if (!timetable)
2265 return AVERROR(ENOMEM);
2266 qsort(timetable, s->nb_chapters, sizeof(*timetable), chapter_start_cmp);
2267
2268 for (unsigned i = 0; i < s->nb_chapters; i++)
2269 if (timetable[i]->end == AV_NOPTS_VALUE) {
2270 AVChapter *const ch = timetable[i];
2271 int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q,
2272 ch->time_base)
2273 : INT64_MAX;
2274
2275 if (i + 1 < s->nb_chapters) {
2276 const AVChapter *const ch1 = timetable[i + 1];
2277 int64_t next_start = av_rescale_q(ch1->start, ch1->time_base,
2278 ch->time_base);
2279 if (next_start > ch->start && next_start < end)
2280 end = next_start;
2281 }
2282 ch->end = (end == INT64_MAX || end < ch->start) ? ch->start : end;
2283 }
2284 av_free(timetable);
2285 return 0;
2286 }
2287
2288 static int get_std_framerate(int i)
2289 {
2290 if (i < 30*12)
2291 return (i + 1) * 1001;
2292 i -= 30*12;
2293
2294 if (i < 30)
2295 return (i + 31) * 1001 * 12;
2296 i -= 30;
2297
2298 if (i < 3)
2299 return ((const int[]) { 80, 120, 240})[i] * 1001 * 12;
2300
2301 i -= 3;
2302
2303 return ((const int[]) { 24, 30, 60, 12, 15, 48 })[i] * 1000 * 12;
2304 }
2305
2306 /* Is the time base unreliable?
2307 * This is a heuristic to balance between quick acceptance of the values in
2308 * the headers vs. some extra checks.
2309 * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
2310 * MPEG-2 commonly misuses field repeat flags to store different framerates.
2311 * And there are "variable" fps files this needs to detect as well. */
2312 static int tb_unreliable(AVCodecContext *c)
2313 {
2314 if (c->time_base.den >= 101LL * c->time_base.num ||
2315 c->time_base.den < 5LL * c->time_base.num ||
2316 // c->codec_tag == AV_RL32("DIVX") ||
2317 // c->codec_tag == AV_RL32("XVID") ||
2318 c->codec_tag == AV_RL32("mp4v") ||
2319 c->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
2320 c->codec_id == AV_CODEC_ID_GIF ||
2321 c->codec_id == AV_CODEC_ID_HEVC ||
2322 c->codec_id == AV_CODEC_ID_H264)
2323 return 1;
2324 return 0;
2325 }
2326
2327 int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
2328 {
2329 FFStream *const sti = ffstream(st);
2330 FFStreamInfo *info = sti->info;
2331 int64_t last = info->last_dts;
2332
2333 if ( ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last
2334 && ts - (uint64_t)last < INT64_MAX) {
2335 double dts = (is_relative(ts) ? ts - RELATIVE_TS_BASE : ts) * av_q2d(st->time_base);
2336 int64_t duration = ts - last;
2337
2338 if (!info->duration_error)
2339 info->duration_error = av_mallocz(sizeof(info->duration_error[0])*2);
2340 if (!info->duration_error)
2341 return AVERROR(ENOMEM);
2342
2343 // if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2344 // av_log(NULL, AV_LOG_ERROR, "%f\n", dts);
2345 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2346 if (info->duration_error[0][1][i] < 1e10) {
2347 int framerate = get_std_framerate(i);
2348 double sdts = dts*framerate/(1001*12);
2349 for (int j = 0; j < 2; j++) {
2350 int64_t ticks = llrint(sdts+j*0.5);
2351 double error = sdts - ticks + j*0.5;
2352 info->duration_error[j][0][i] += error;
2353 info->duration_error[j][1][i] += error*error;
2354 }
2355 }
2356 }
2357 if (info->rfps_duration_sum <= INT64_MAX - duration) {
2358 info->duration_count++;
2359 info->rfps_duration_sum += duration;
2360 }
2361
2362 if (info->duration_count % 10 == 0) {
2363 int n = info->duration_count;
2364 for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2365 if (info->duration_error[0][1][i] < 1e10) {
2366 double a0 = info->duration_error[0][0][i] / n;
2367 double error0 = info->duration_error[0][1][i] / n - a0*a0;
2368 double a1 = info->duration_error[1][0][i] / n;
2369 double error1 = info->duration_error[1][1][i] / n - a1*a1;
2370 if (error0 > 0.04 && error1 > 0.04) {
2371 info->duration_error[0][1][i] = 2e10;
2372 info->duration_error[1][1][i] = 2e10;
2373 }
2374 }
2375 }
2376 }
2377
2378 // ignore the first 4 values, they might have some random jitter
2379 if (info->duration_count > 3 && is_relative(ts) == is_relative(last))
2380 info->duration_gcd = av_gcd(info->duration_gcd, duration);
2381 }
2382 if (ts != AV_NOPTS_VALUE)
2383 info->last_dts = ts;
2384
2385 return 0;
2386 }
2387
2388 void ff_rfps_calculate(AVFormatContext *ic)
2389 {
2390 for (unsigned i = 0; i < ic->nb_streams; i++) {
2391 AVStream *const st = ic->streams[i];
2392 FFStream *const sti = ffstream(st);
2393 #ifdef OHOS_AUXILIARY_TRACK
2394 if (!(need_parse_video_info(st) == 1))
2395 #else
2396 if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
2397 #endif
2398 continue;
2399 // the check for tb_unreliable() is not completely correct, since this is not about handling
2400 // an unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
2401 // ipmovie.c produces.
2402 if (tb_unreliable(sti->avctx) && sti->info->duration_count > 15 && sti->info->duration_gcd > FFMAX(1, st->time_base.den/(500LL*st->time_base.num)) && !st->r_frame_rate.num &&
2403 sti->info->duration_gcd < INT64_MAX / st->time_base.num)
2404 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, st->time_base.den, st->time_base.num * sti->info->duration_gcd, INT_MAX);
2405 if (sti->info->duration_count > 1 && !st->r_frame_rate.num
2406 && tb_unreliable(sti->avctx)) {
2407 int num = 0;
2408 double best_error = 0.01;
2409 AVRational ref_rate = st->r_frame_rate.num ? st->r_frame_rate : av_inv_q(st->time_base);
2410
2411 for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
2412 if (sti->info->codec_info_duration &&
2413 sti->info->codec_info_duration*av_q2d(st->time_base) < (1001*11.5)/get_std_framerate(j))
2414 continue;
2415 if (!sti->info->codec_info_duration && get_std_framerate(j) < 1001*12)
2416 continue;
2417
2418 if (av_q2d(st->time_base) * sti->info->rfps_duration_sum / sti->info->duration_count < (1001*12.0 * 0.8)/get_std_framerate(j))
2419 continue;
2420
2421 for (int k = 0; k < 2; k++) {
2422 int n = sti->info->duration_count;
2423 double a = sti->info->duration_error[k][0][j] / n;
2424 double error = sti->info->duration_error[k][1][j]/n - a*a;
2425
2426 if (error < best_error && best_error> 0.000000001) {
2427 best_error= error;
2428 num = get_std_framerate(j);
2429 }
2430 if (error < 0.02)
2431 av_log(ic, AV_LOG_DEBUG, "rfps: %f %f\n", get_std_framerate(j) / 12.0/1001, error);
2432 }
2433 }
2434 // do not increase frame rate by more than 1 % in order to match a standard rate.
2435 if (num && (!ref_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(ref_rate)))
2436 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
2437 }
2438 if ( !st->avg_frame_rate.num
2439 && st->r_frame_rate.num && sti->info->rfps_duration_sum
2440 && sti->info->codec_info_duration <= 0
2441 && sti->info->duration_count > 2
2442 && fabs(1.0 / (av_q2d(st->r_frame_rate) * av_q2d(st->time_base)) - sti->info->rfps_duration_sum / (double)sti->info->duration_count) <= 1.0
2443 ) {
2444 av_log(ic, AV_LOG_DEBUG, "Setting avg frame rate based on r frame rate\n");
2445 st->avg_frame_rate = st->r_frame_rate;
2446 }
2447
2448 av_freep(&sti->info->duration_error);
2449 sti->info->last_dts = AV_NOPTS_VALUE;
2450 sti->info->duration_count = 0;
2451 sti->info->rfps_duration_sum = 0;
2452 }
2453 }
2454
2455 static int extract_extradata_check(AVStream *st)
2456 {
2457 const AVBitStreamFilter *const f = av_bsf_get_by_name("extract_extradata");
2458 if (!f)
2459 return 0;
2460
2461 if (f->codec_ids) {
2462 const enum AVCodecID *ids;
2463 for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++)
2464 if (*ids == st->codecpar->codec_id)
2465 return 1;
2466 }
2467
2468 return 0;
2469 }
2470
2471 static int extract_extradata_init(AVStream *st)
2472 {
2473 FFStream *const sti = ffstream(st);
2474 const AVBitStreamFilter *f;
2475 int ret;
2476
2477 f = av_bsf_get_by_name("extract_extradata");
2478 if (!f)
2479 goto finish;
2480
2481 /* check that the codec id is supported */
2482 ret = extract_extradata_check(st);
2483 if (!ret)
2484 goto finish;
2485
2486 ret = av_bsf_alloc(f, &sti->extract_extradata.bsf);
2487 if (ret < 0)
2488 return ret;
2489
2490 ret = avcodec_parameters_copy(sti->extract_extradata.bsf->par_in,
2491 st->codecpar);
2492 if (ret < 0)
2493 goto fail;
2494
2495 sti->extract_extradata.bsf->time_base_in = st->time_base;
2496
2497 ret = av_bsf_init(sti->extract_extradata.bsf);
2498 if (ret < 0)
2499 goto fail;
2500
2501 finish:
2502 sti->extract_extradata.inited = 1;
2503
2504 return 0;
2505 fail:
2506 av_bsf_free(&sti->extract_extradata.bsf);
2507 return ret;
2508 }
2509
2510 static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt)
2511 {
2512 FFStream *const sti = ffstream(st);
2513 AVPacket *const pkt_ref = si->parse_pkt;
2514 int ret;
2515
2516 if (!sti->extract_extradata.inited) {
2517 ret = extract_extradata_init(st);
2518 if (ret < 0)
2519 return ret;
2520 }
2521
2522 if (sti->extract_extradata.inited && !sti->extract_extradata.bsf)
2523 return 0;
2524
2525 ret = av_packet_ref(pkt_ref, pkt);
2526 if (ret < 0)
2527 return ret;
2528
2529 ret = av_bsf_send_packet(sti->extract_extradata.bsf, pkt_ref);
2530 if (ret < 0) {
2531 av_packet_unref(pkt_ref);
2532 return ret;
2533 }
2534
2535 while (ret >= 0 && !sti->avctx->extradata) {
2536 ret = av_bsf_receive_packet(sti->extract_extradata.bsf, pkt_ref);
2537 if (ret < 0) {
2538 if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2539 return ret;
2540 continue;
2541 }
2542
2543 for (int i = 0; i < pkt_ref->side_data_elems; i++) {
2544 AVPacketSideData *const side_data = &pkt_ref->side_data[i];
2545 if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) {
2546 sti->avctx->extradata = side_data->data;
2547 sti->avctx->extradata_size = side_data->size;
2548 side_data->data = NULL;
2549 side_data->size = 0;
2550 break;
2551 }
2552 }
2553 av_packet_unref(pkt_ref);
2554 }
2555
2556 return 0;
2557 }
2558
2559 static int add_coded_side_data(AVStream *st, AVCodecContext *avctx)
2560 {
2561 for (int i = 0; i < avctx->nb_coded_side_data; i++) {
2562 const AVPacketSideData *const sd_src = &avctx->coded_side_data[i];
2563 uint8_t *dst_data;
2564 dst_data = av_stream_new_side_data(st, sd_src->type, sd_src->size);
2565 if (!dst_data)
2566 return AVERROR(ENOMEM);
2567 memcpy(dst_data, sd_src->data, sd_src->size);
2568 }
2569 return 0;
2570 }
2571
2572 int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2573 {
2574 FFFormatContext *const si = ffformatcontext(ic);
2575 int count = 0, ret = 0;
2576 int64_t read_size;
2577 AVPacket *pkt1 = si->pkt;
2578 int64_t old_offset = avio_tell(ic->pb);
2579 // new streams might appear, no options for those
2580 int orig_nb_streams = ic->nb_streams;
2581 int flush_codecs;
2582 int64_t max_analyze_duration = ic->max_analyze_duration;
2583 int64_t max_stream_analyze_duration;
2584 int64_t max_subtitle_analyze_duration;
2585 int64_t probesize = ic->probesize;
2586 int eof_reached = 0;
2587 int *missing_streams = av_opt_ptr(ic->iformat->priv_class, ic->priv_data, "missing_streams");
2588
2589 flush_codecs = probesize > 0;
2590
2591 av_opt_set_int(ic, "skip_clear", 1, AV_OPT_SEARCH_CHILDREN);
2592
2593 max_stream_analyze_duration = max_analyze_duration;
2594 max_subtitle_analyze_duration = max_analyze_duration;
2595 if (!max_analyze_duration) {
2596 max_stream_analyze_duration =
2597 max_analyze_duration = 5*AV_TIME_BASE;
2598 max_subtitle_analyze_duration = 30*AV_TIME_BASE;
2599 if (!strcmp(ic->iformat->name, "flv"))
2600 max_stream_analyze_duration = 90*AV_TIME_BASE;
2601 if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts"))
2602 max_stream_analyze_duration = 7*AV_TIME_BASE;
2603 }
2604
2605 if (ic->pb) {
2606 FFIOContext *const ctx = ffiocontext(ic->pb);
2607 av_log(ic, AV_LOG_DEBUG, "Before avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d nb_streams:%d\n",
2608 avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, ic->nb_streams);
2609 }
2610
2611 for (unsigned i = 0; i < ic->nb_streams; i++) {
2612 const AVCodec *codec;
2613 AVDictionary *thread_opt = NULL;
2614 AVStream *const st = ic->streams[i];
2615 FFStream *const sti = ffstream(st);
2616 AVCodecContext *const avctx = sti->avctx;
2617 #ifdef OHOS_AUXILIARY_TRACK
2618 if (need_parse_video_info(st) ||
2619 #else
2620 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
2621 #endif
2622 st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2623 /* if (!st->time_base.num)
2624 st->time_base = */
2625 if (!avctx->time_base.num)
2626 avctx->time_base = st->time_base;
2627 }
2628
2629 /* check if the caller has overridden the codec id */
2630 // only for the split stuff
2631 if (!sti->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && sti->request_probe <= 0) {
2632 sti->parser = av_parser_init(st->codecpar->codec_id);
2633 if (sti->parser) {
2634 if (sti->need_parsing == AVSTREAM_PARSE_HEADERS) {
2635 sti->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
2636 } else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) {
2637 sti->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
2638 }
2639 } else if (sti->need_parsing) {
2640 av_log(ic, AV_LOG_VERBOSE, "parser not found for codec "
2641 "%s, packets or times may be invalid.\n",
2642 avcodec_get_name(st->codecpar->codec_id));
2643 }
2644 }
2645
2646 ret = avcodec_parameters_to_context(avctx, st->codecpar);
2647 if (ret < 0)
2648 goto find_stream_info_err;
2649 if (sti->request_probe <= 0)
2650 sti->avctx_inited = 1;
2651
2652 codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
2653
2654 /* Force thread count to 1 since the H.264 decoder will not extract
2655 * SPS and PPS to extradata during multi-threaded decoding. */
2656 av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
2657 /* Force lowres to 0. The decoder might reduce the video size by the
2658 * lowres factor, and we don't want that propagated to the stream's
2659 * codecpar */
2660 av_dict_set(options ? &options[i] : &thread_opt, "lowres", "0", 0);
2661
2662 if (ic->codec_whitelist)
2663 av_dict_set(options ? &options[i] : &thread_opt, "codec_whitelist", ic->codec_whitelist, 0);
2664
2665 // Try to just open decoders, in case this is enough to get parameters.
2666 // Also ensure that subtitle_header is properly set.
2667 if (!has_codec_parameters(st, NULL) && sti->request_probe <= 0 ||
2668 st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2669 if (codec && !avctx->codec)
2670 if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)
2671 av_log(ic, AV_LOG_WARNING,
2672 "Failed to open codec in %s\n",__FUNCTION__);
2673 }
2674 if (!options)
2675 av_dict_free(&thread_opt);
2676 }
2677
2678 read_size = 0;
2679 for (;;) {
2680 const AVPacket *pkt;
2681 AVStream *st;
2682 FFStream *sti;
2683 AVCodecContext *avctx;
2684 int analyzed_all_streams;
2685 unsigned i;
2686 if (ff_check_interrupt(&ic->interrupt_callback)) {
2687 ret = AVERROR_EXIT;
2688 av_log(ic, AV_LOG_DEBUG, "interrupted\n");
2689 break;
2690 }
2691
2692 /* check if one codec still needs to be handled */
2693 for (i = 0; i < ic->nb_streams; i++) {
2694 AVStream *const st = ic->streams[i];
2695 FFStream *const sti = ffstream(st);
2696 int fps_analyze_framecount = 20;
2697 int count;
2698
2699 if (!has_codec_parameters(st, NULL))
2700 break;
2701 /* If the timebase is coarse (like the usual millisecond precision
2702 * of mkv), we need to analyze more frames to reliably arrive at
2703 * the correct fps. */
2704 if (av_q2d(st->time_base) > 0.0005)
2705 fps_analyze_framecount *= 2;
2706 if (!tb_unreliable(sti->avctx))
2707 fps_analyze_framecount = 0;
2708 if (ic->fps_probe_size >= 0)
2709 fps_analyze_framecount = ic->fps_probe_size;
2710 if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)
2711 fps_analyze_framecount = 0;
2712 /* variable fps and no guess at the real fps */
2713 count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?
2714 sti->info->codec_info_duration_fields/2 :
2715 sti->info->duration_count;
2716 if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&
2717 #ifdef OHOS_AUXILIARY_TRACK
2718 need_parse_video_info(st)) {
2719 #else
2720 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2721 #endif
2722 if (count < fps_analyze_framecount)
2723 break;
2724 }
2725 // Look at the first 3 frames if there is evidence of frame delay
2726 // but the decoder delay is not set.
2727 if (sti->info->frame_delay_evidence && count < 2 && sti->avctx->has_b_frames == 0)
2728 break;
2729 if (!sti->avctx->extradata &&
2730 (!sti->extract_extradata.inited || sti->extract_extradata.bsf) &&
2731 extract_extradata_check(st))
2732 break;
2733 if (sti->first_dts == AV_NOPTS_VALUE &&
2734 !(ic->iformat->flags & AVFMT_NOTIMESTAMPS) &&
2735 sti->codec_info_nb_frames < ((st->disposition & AV_DISPOSITION_ATTACHED_PIC) ? 1 : ic->max_ts_probe) &&
2736 (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
2737 #ifdef OHOS_AUXILIARY_TRACK
2738 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || st->codecpar->codec_type == AVMEDIA_TYPE_AUXILIARY))
2739 #else
2740 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))
2741 #endif
2742 break;
2743 }
2744 analyzed_all_streams = 0;
2745 if (!missing_streams || !*missing_streams)
2746 if (i == ic->nb_streams) {
2747 analyzed_all_streams = 1;
2748 /* NOTE: If the format has no header, then we need to read some
2749 * packets to get most of the streams, so we cannot stop here. */
2750 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
2751 /* If we found the info for all the codecs, we can stop. */
2752 ret = count;
2753 av_log(ic, AV_LOG_DEBUG, "All info found\n");
2754 flush_codecs = 0;
2755 break;
2756 }
2757 }
2758 /* We did not get all the codec info, but we read too much data. */
2759 if (read_size >= probesize) {
2760 ret = count;
2761 av_log(ic, AV_LOG_DEBUG,
2762 "Probe buffer size limit of %"PRId64" bytes reached\n", probesize);
2763 for (unsigned i = 0; i < ic->nb_streams; i++) {
2764 AVStream *const st = ic->streams[i];
2765 FFStream *const sti = ffstream(st);
2766 if (!st->r_frame_rate.num &&
2767 sti->info->duration_count <= 1 &&
2768 st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
2769 strcmp(ic->iformat->name, "image2"))
2770 av_log(ic, AV_LOG_WARNING,
2771 "Stream #%d: not enough frames to estimate rate; "
2772 "consider increasing probesize\n", i);
2773 }
2774 break;
2775 }
2776
2777 /* NOTE: A new stream can be added there if no header in file
2778 * (AVFMTCTX_NOHEADER). */
2779 ret = read_frame_internal(ic, pkt1);
2780 if (ret == AVERROR(EAGAIN))
2781 continue;
2782
2783 if (ret < 0) {
2784 /* EOF or error*/
2785 eof_reached = 1;
2786 break;
2787 }
2788
2789 if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
2790 ret = avpriv_packet_list_put(&si->packet_buffer,
2791 pkt1, NULL, 0);
2792 if (ret < 0)
2793 goto unref_then_goto_end;
2794
2795 pkt = &si->packet_buffer.tail->pkt;
2796 } else {
2797 pkt = pkt1;
2798 }
2799
2800 st = ic->streams[pkt->stream_index];
2801 sti = ffstream(st);
2802 if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2803 read_size += pkt->size;
2804
2805 avctx = sti->avctx;
2806 if (!sti->avctx_inited) {
2807 ret = avcodec_parameters_to_context(avctx, st->codecpar);
2808 if (ret < 0)
2809 goto unref_then_goto_end;
2810 sti->avctx_inited = 1;
2811 }
2812
2813 if (pkt->dts != AV_NOPTS_VALUE && sti->codec_info_nb_frames > 1) {
2814 /* check for non-increasing dts */
2815 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2816 sti->info->fps_last_dts >= pkt->dts) {
2817 av_log(ic, AV_LOG_DEBUG,
2818 "Non-increasing DTS in stream %d: packet %d with DTS "
2819 "%"PRId64", packet %d with DTS %"PRId64"\n",
2820 st->index, sti->info->fps_last_dts_idx,
2821 sti->info->fps_last_dts, sti->codec_info_nb_frames,
2822 pkt->dts);
2823 sti->info->fps_first_dts =
2824 sti->info->fps_last_dts = AV_NOPTS_VALUE;
2825 }
2826 /* Check for a discontinuity in dts. If the difference in dts
2827 * is more than 1000 times the average packet duration in the
2828 * sequence, we treat it as a discontinuity. */
2829 if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2830 sti->info->fps_last_dts_idx > sti->info->fps_first_dts_idx &&
2831 (pkt->dts - (uint64_t)sti->info->fps_last_dts) / 1000 >
2832 (sti->info->fps_last_dts - (uint64_t)sti->info->fps_first_dts) /
2833 (sti->info->fps_last_dts_idx - sti->info->fps_first_dts_idx)) {
2834 av_log(ic, AV_LOG_WARNING,
2835 "DTS discontinuity in stream %d: packet %d with DTS "
2836 "%"PRId64", packet %d with DTS %"PRId64"\n",
2837 st->index, sti->info->fps_last_dts_idx,
2838 sti->info->fps_last_dts, sti->codec_info_nb_frames,
2839 pkt->dts);
2840 sti->info->fps_first_dts =
2841 sti->info->fps_last_dts = AV_NOPTS_VALUE;
2842 }
2843
2844 /* update stored dts values */
2845 if (sti->info->fps_first_dts == AV_NOPTS_VALUE) {
2846 sti->info->fps_first_dts = pkt->dts;
2847 sti->info->fps_first_dts_idx = sti->codec_info_nb_frames;
2848 }
2849 sti->info->fps_last_dts = pkt->dts;
2850 sti->info->fps_last_dts_idx = sti->codec_info_nb_frames;
2851 }
2852 if (sti->codec_info_nb_frames > 1) {
2853 int64_t t = 0;
2854 int64_t limit;
2855
2856 if (st->time_base.den > 0)
2857 t = av_rescale_q(sti->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q);
2858 if (st->avg_frame_rate.num > 0)
2859 t = FFMAX(t, av_rescale_q(sti->codec_info_nb_frames, av_inv_q(st->avg_frame_rate), AV_TIME_BASE_Q));
2860
2861 if ( t == 0
2862 && sti->codec_info_nb_frames > 30
2863 && sti->info->fps_first_dts != AV_NOPTS_VALUE
2864 && sti->info->fps_last_dts != AV_NOPTS_VALUE) {
2865 int64_t dur = av_sat_sub64(sti->info->fps_last_dts, sti->info->fps_first_dts);
2866 t = FFMAX(t, av_rescale_q(dur, st->time_base, AV_TIME_BASE_Q));
2867 }
2868
2869 if (analyzed_all_streams) limit = max_analyze_duration;
2870 else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration;
2871 else limit = max_stream_analyze_duration;
2872
2873 if (t >= limit) {
2874 av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n",
2875 limit,
2876 t, pkt->stream_index);
2877 if (ic->flags & AVFMT_FLAG_NOBUFFER)
2878 av_packet_unref(pkt1);
2879 break;
2880 }
2881 if (pkt->duration > 0) {
2882 if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE && pkt->pts != AV_NOPTS_VALUE && st->start_time != AV_NOPTS_VALUE && pkt->pts >= st->start_time
2883 && (uint64_t)pkt->pts - st->start_time < INT64_MAX
2884 ) {
2885 sti->info->codec_info_duration = FFMIN(pkt->pts - st->start_time, sti->info->codec_info_duration + pkt->duration);
2886 } else
2887 sti->info->codec_info_duration += pkt->duration;
2888 sti->info->codec_info_duration_fields += sti->parser && sti->need_parsing && avctx->ticks_per_frame == 2
2889 ? sti->parser->repeat_pict + 1 : 2;
2890 }
2891 }
2892 #ifdef OHOS_AUXILIARY_TRACK
2893 if (need_parse_video_info(st)) {
2894 #else
2895 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2896 #endif
2897 #if FF_API_R_FRAME_RATE
2898 ff_rfps_add_frame(ic, st, pkt->dts);
2899 #endif
2900 if (pkt->dts != pkt->pts && pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
2901 sti->info->frame_delay_evidence = 1;
2902 }
2903 if (!sti->avctx->extradata) {
2904 ret = extract_extradata(si, st, pkt);
2905 if (ret < 0)
2906 goto unref_then_goto_end;
2907 }
2908
2909 /* If still no information, we try to open the codec and to
2910 * decompress the frame. We try to avoid that in most cases as
2911 * it takes longer and uses more memory. For MPEG-4, we need to
2912 * decompress for QuickTime.
2913 *
2914 * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
2915 * least one frame of codec data, this makes sure the codec initializes
2916 * the channel configuration and does not only trust the values from
2917 * the container. */
2918 try_decode_frame(ic, st, pkt,
2919 (options && i < orig_nb_streams) ? &options[i] : NULL);
2920
2921 if (ic->flags & AVFMT_FLAG_NOBUFFER)
2922 av_packet_unref(pkt1);
2923
2924 sti->codec_info_nb_frames++;
2925 count++;
2926 }
2927
2928 if (eof_reached) {
2929 for (unsigned stream_index = 0; stream_index < ic->nb_streams; stream_index++) {
2930 AVStream *const st = ic->streams[stream_index];
2931 AVCodecContext *const avctx = ffstream(st)->avctx;
2932 if (!has_codec_parameters(st, NULL)) {
2933 const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
2934 if (codec && !avctx->codec) {
2935 AVDictionary *opts = NULL;
2936 if (ic->codec_whitelist)
2937 av_dict_set(&opts, "codec_whitelist", ic->codec_whitelist, 0);
2938 if (avcodec_open2(avctx, codec, (options && stream_index < orig_nb_streams) ? &options[stream_index] : &opts) < 0)
2939 av_log(ic, AV_LOG_WARNING,
2940 "Failed to open codec in %s\n",__FUNCTION__);
2941 av_dict_free(&opts);
2942 }
2943 }
2944
2945 // EOF already reached while reading the stream above.
2946 // So continue with reoordering DTS with whatever delay we have.
2947 if (si->packet_buffer.head && !has_decode_delay_been_guessed(st)) {
2948 update_dts_from_pts(ic, stream_index, si->packet_buffer.head);
2949 }
2950 }
2951 }
2952
2953 if (flush_codecs) {
2954 AVPacket *empty_pkt = si->pkt;
2955 int err = 0;
2956 av_packet_unref(empty_pkt);
2957
2958 for (unsigned i = 0; i < ic->nb_streams; i++) {
2959 AVStream *const st = ic->streams[i];
2960 FFStream *const sti = ffstream(st);
2961
2962 /* flush the decoders */
2963 if (sti->info->found_decoder == 1) {
2964 err = try_decode_frame(ic, st, empty_pkt,
2965 (options && i < orig_nb_streams)
2966 ? &options[i] : NULL);
2967
2968 if (err < 0) {
2969 av_log(ic, AV_LOG_INFO,
2970 "decoding for stream %d failed\n", st->index);
2971 }
2972 }
2973 }
2974 }
2975
2976 ff_rfps_calculate(ic);
2977
2978 for (unsigned i = 0; i < ic->nb_streams; i++) {
2979 AVStream *const st = ic->streams[i];
2980 FFStream *const sti = ffstream(st);
2981 AVCodecContext *const avctx = sti->avctx;
2982 #ifdef OHOS_AUXILIARY_TRACK
2983 if (need_parse_video_info(st)) {
2984 #else
2985 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
2986 #endif
2987 if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) {
2988 uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
2989 if (avpriv_pix_fmt_find(PIX_FMT_LIST_RAW, tag) == avctx->pix_fmt)
2990 avctx->codec_tag= tag;
2991 }
2992
2993 /* estimate average framerate if not set by demuxer */
2994 if (sti->info->codec_info_duration_fields &&
2995 !st->avg_frame_rate.num &&
2996 sti->info->codec_info_duration) {
2997 int best_fps = 0;
2998 double best_error = 0.01;
2999 AVRational codec_frame_rate = avctx->framerate;
3000
3001 if (sti->info->codec_info_duration >= INT64_MAX / st->time_base.num / 2||
3002 sti->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||
3003 sti->info->codec_info_duration < 0)
3004 continue;
3005 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
3006 sti->info->codec_info_duration_fields * (int64_t) st->time_base.den,
3007 sti->info->codec_info_duration * 2 * (int64_t) st->time_base.num, 60000);
3008
3009 /* Round guessed framerate to a "standard" framerate if it's
3010 * within 1% of the original estimate. */
3011 for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
3012 AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
3013 double error = fabs(av_q2d(st->avg_frame_rate) /
3014 av_q2d(std_fps) - 1);
3015
3016 if (error < best_error) {
3017 best_error = error;
3018 best_fps = std_fps.num;
3019 }
3020
3021 if (si->prefer_codec_framerate && codec_frame_rate.num > 0 && codec_frame_rate.den > 0) {
3022 error = fabs(av_q2d(codec_frame_rate) /
3023 av_q2d(std_fps) - 1);
3024 if (error < best_error) {
3025 best_error = error;
3026 best_fps = std_fps.num;
3027 }
3028 }
3029 }
3030 if (best_fps)
3031 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
3032 best_fps, 12 * 1001, INT_MAX);
3033 }
3034
3035 if (!st->r_frame_rate.num) {
3036 if ( avctx->time_base.den * (int64_t) st->time_base.num
3037 <= avctx->time_base.num * (uint64_t)avctx->ticks_per_frame * st->time_base.den) {
3038 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
3039 avctx->time_base.den, (int64_t)avctx->time_base.num * avctx->ticks_per_frame, INT_MAX);
3040 } else {
3041 st->r_frame_rate.num = st->time_base.den;
3042 st->r_frame_rate.den = st->time_base.num;
3043 }
3044 }
3045 if (sti->display_aspect_ratio.num && sti->display_aspect_ratio.den) {
3046 AVRational hw_ratio = { avctx->height, avctx->width };
3047 st->sample_aspect_ratio = av_mul_q(sti->display_aspect_ratio,
3048 hw_ratio);
3049 }
3050 #ifdef OHOS_AUXILIARY_TRACK
3051 } else if (need_parse_audio_info(st)) {
3052 #else
3053 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
3054 #endif
3055 if (!avctx->bits_per_coded_sample)
3056 avctx->bits_per_coded_sample =
3057 av_get_bits_per_sample(avctx->codec_id);
3058 // set stream disposition based on audio service type
3059 switch (avctx->audio_service_type) {
3060 case AV_AUDIO_SERVICE_TYPE_EFFECTS:
3061 st->disposition = AV_DISPOSITION_CLEAN_EFFECTS;
3062 break;
3063 case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
3064 st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED;
3065 break;
3066 case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
3067 st->disposition = AV_DISPOSITION_HEARING_IMPAIRED;
3068 break;
3069 case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
3070 st->disposition = AV_DISPOSITION_COMMENT;
3071 break;
3072 case AV_AUDIO_SERVICE_TYPE_KARAOKE:
3073 st->disposition = AV_DISPOSITION_KARAOKE;
3074 break;
3075 }
3076 }
3077 }
3078
3079 if (probesize)
3080 estimate_timings(ic, old_offset);
3081
3082 av_opt_set_int(ic, "skip_clear", 0, AV_OPT_SEARCH_CHILDREN);
3083
3084 if (ret >= 0 && ic->nb_streams)
3085 /* We could not have all the codec parameters before EOF. */
3086 ret = -1;
3087 for (unsigned i = 0; i < ic->nb_streams; i++) {
3088 AVStream *const st = ic->streams[i];
3089 FFStream *const sti = ffstream(st);
3090 const char *errmsg;
3091
3092 /* if no packet was ever seen, update context now for has_codec_parameters */
3093 if (!sti->avctx_inited) {
3094 #ifdef OHOS_AUXILIARY_TRACK
3095 if (need_parse_audio_info(st) &&
3096 #else
3097 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
3098 #endif
3099 st->codecpar->format == AV_SAMPLE_FMT_NONE)
3100 st->codecpar->format = sti->avctx->sample_fmt;
3101 ret = avcodec_parameters_to_context(sti->avctx, st->codecpar);
3102 if (ret < 0)
3103 goto find_stream_info_err;
3104 }
3105 if (!has_codec_parameters(st, &errmsg)) {
3106 char buf[256];
3107 avcodec_string(buf, sizeof(buf), sti->avctx, 0);
3108 av_log(ic, AV_LOG_WARNING,
3109 "Could not find codec parameters for stream %d (%s): %s\n"
3110 "Consider increasing the value for the 'analyzeduration' (%"PRId64") and 'probesize' (%"PRId64") options\n",
3111 i, buf, errmsg, ic->max_analyze_duration, ic->probesize);
3112 } else {
3113 ret = 0;
3114 }
3115 }
3116
3117 ret = compute_chapters_end(ic);
3118 if (ret < 0)
3119 goto find_stream_info_err;
3120
3121 /* update the stream parameters from the internal codec contexts */
3122 for (unsigned i = 0; i < ic->nb_streams; i++) {
3123 AVStream *const st = ic->streams[i];
3124 FFStream *const sti = ffstream(st);
3125
3126 if (sti->avctx_inited) {
3127 ret = avcodec_parameters_from_context(st->codecpar, sti->avctx);
3128 #ifdef OHOS_AUXILIARY_TRACK
3129 AVDictionaryEntry *valPtr = av_dict_get(st->metadata, "handler_type", valPtr, AV_DICT_IGNORE_SUFFIX);
3130 if (valPtr != NULL && strcmp(valPtr->value, "auxv") == 0) {
3131 st->codecpar->codec_type = AVMEDIA_TYPE_AUXILIARY;
3132 }
3133 #endif
3134 if (ret < 0)
3135 goto find_stream_info_err;
3136 ret = add_coded_side_data(st, sti->avctx);
3137 if (ret < 0)
3138 goto find_stream_info_err;
3139 }
3140
3141 sti->avctx_inited = 0;
3142 }
3143
3144 find_stream_info_err:
3145 for (unsigned i = 0; i < ic->nb_streams; i++) {
3146 AVStream *const st = ic->streams[i];
3147 FFStream *const sti = ffstream(st);
3148 if (sti->info) {
3149 av_freep(&sti->info->duration_error);
3150 av_freep(&sti->info);
3151 }
3152 avcodec_close(sti->avctx);
3153 // FIXME: avcodec_close() frees AVOption settable fields which includes ch_layout,
3154 // so we need to restore it.
3155 av_channel_layout_copy(&sti->avctx->ch_layout, &st->codecpar->ch_layout);
3156 av_bsf_free(&sti->extract_extradata.bsf);
3157 }
3158 if (ic->pb) {
3159 FFIOContext *const ctx = ffiocontext(ic->pb);
3160 av_log(ic, AV_LOG_DEBUG, "After avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d frames:%d\n",
3161 avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, count);
3162 }
3163 return ret;
3164
3165 unref_then_goto_end:
3166 av_packet_unref(pkt1);
3167 goto find_stream_info_err;
3168 }
3169