comparison utils.c @ 2300:9c8cfadff191 libavformat

correct initial timestamps which have AV_NOPTS_VALUE
author michael
date Sat, 04 Aug 2007 22:46:13 +0000
parents acdc747b9ff3
children 45a6610e6a4f
comparison
equal deleted inserted replaced
2299:fe59c768ecf7 2300:9c8cfadff191
587 } 587 }
588 } 588 }
589 return 0; 589 return 0;
590 } 590 }
591 591
592 static void update_initial_timestamps(AVFormatContext *s, int stream_index, int64_t dts){
593 AVStream *st= s->streams[stream_index];
594 AVPacketList *pktl= s->packet_buffer;
595
596 if(st->first_dts != AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE)
597 return;
598
599 st->first_dts= dts - st->cur_dts;
600 st->cur_dts= dts;
601
602 for(; pktl; pktl= pktl->next){
603 if(pktl->pkt.stream_index != stream_index)
604 continue;
605 //FIXME think more about this check
606 if(pktl->pkt.pts != AV_NOPTS_VALUE && pktl->pkt.pts == pktl->pkt.dts)
607 pktl->pkt.pts += st->first_dts;
608
609 if(pktl->pkt.dts != AV_NOPTS_VALUE)
610 pktl->pkt.dts += st->first_dts;
611 }
612 }
613
592 static void compute_pkt_fields(AVFormatContext *s, AVStream *st, 614 static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
593 AVCodecParserContext *pc, AVPacket *pkt) 615 AVCodecParserContext *pc, AVPacket *pkt)
594 { 616 {
595 int num, den, presentation_delayed, delay, i; 617 int num, den, presentation_delayed, delay, i;
596 int64_t offset; 618 int64_t offset;
631 /* This may be redundant, but it should not hurt. */ 653 /* This may be redundant, but it should not hurt. */
632 if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts > pkt->dts) 654 if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts > pkt->dts)
633 presentation_delayed = 1; 655 presentation_delayed = 1;
634 656
635 if(st->cur_dts == AV_NOPTS_VALUE){ 657 if(st->cur_dts == AV_NOPTS_VALUE){
636 st->cur_dts = -delay * pkt->duration; 658 st->cur_dts = 0; //FIXME maybe set it to 0 during init
637 } 659 }
638 660
639 // av_log(NULL, AV_LOG_DEBUG, "IN delayed:%d pts:%"PRId64", dts:%"PRId64" cur_dts:%"PRId64" st:%d pc:%p\n", presentation_delayed, pkt->pts, pkt->dts, st->cur_dts, pkt->stream_index, pc); 661 // av_log(NULL, AV_LOG_DEBUG, "IN delayed:%d pts:%"PRId64", dts:%"PRId64" cur_dts:%"PRId64" st:%d pc:%p\n", presentation_delayed, pkt->pts, pkt->dts, st->cur_dts, pkt->stream_index, pc);
640 /* interpolate PTS and DTS if they are not present */ 662 /* interpolate PTS and DTS if they are not present */
641 if(delay <=1){ 663 if(delay <=1){
642 if (presentation_delayed) { 664 if (presentation_delayed) {
643 /* DTS = decompression time stamp */ 665 /* DTS = decompression time stamp */
644 /* PTS = presentation time stamp */ 666 /* PTS = presentation time stamp */
645 if (pkt->dts == AV_NOPTS_VALUE) 667 if (pkt->dts == AV_NOPTS_VALUE)
646 pkt->dts = st->last_IP_pts; 668 pkt->dts = st->last_IP_pts;
669 update_initial_timestamps(s, pkt->stream_index, pkt->dts);
647 if (pkt->dts == AV_NOPTS_VALUE) 670 if (pkt->dts == AV_NOPTS_VALUE)
648 pkt->dts = st->cur_dts; 671 pkt->dts = st->cur_dts;
649 672
650 /* this is tricky: the dts must be incremented by the duration 673 /* this is tricky: the dts must be incremented by the duration
651 of the frame we are displaying, i.e. the last I or P frame */ 674 of the frame we are displaying, i.e. the last I or P frame */
667 } 690 }
668 691
669 /* presentation is not delayed : PTS and DTS are the same */ 692 /* presentation is not delayed : PTS and DTS are the same */
670 if(pkt->pts == AV_NOPTS_VALUE) 693 if(pkt->pts == AV_NOPTS_VALUE)
671 pkt->pts = pkt->dts; 694 pkt->pts = pkt->dts;
695 update_initial_timestamps(s, pkt->stream_index, pkt->pts);
672 if(pkt->pts == AV_NOPTS_VALUE) 696 if(pkt->pts == AV_NOPTS_VALUE)
673 pkt->pts = st->cur_dts; 697 pkt->pts = st->cur_dts;
674 pkt->dts = pkt->pts; 698 pkt->dts = pkt->pts;
675 st->cur_dts = pkt->pts + pkt->duration; 699 st->cur_dts = pkt->pts + pkt->duration;
676 } 700 }
682 st->pts_buffer[i]= (i-delay-1) * pkt->duration; 706 st->pts_buffer[i]= (i-delay-1) * pkt->duration;
683 for(i=0; i<delay && st->pts_buffer[i] > st->pts_buffer[i+1]; i++) 707 for(i=0; i<delay && st->pts_buffer[i] > st->pts_buffer[i+1]; i++)
684 FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i+1]); 708 FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i+1]);
685 if(pkt->dts == AV_NOPTS_VALUE) 709 if(pkt->dts == AV_NOPTS_VALUE)
686 pkt->dts= st->pts_buffer[0]; 710 pkt->dts= st->pts_buffer[0];
711 if(delay>1){
712 update_initial_timestamps(s, pkt->stream_index, pkt->dts); // this should happen on the first packet
713 }
687 if(pkt->dts > st->cur_dts) 714 if(pkt->dts > st->cur_dts)
688 st->cur_dts = pkt->dts; 715 st->cur_dts = pkt->dts;
689 } 716 }
690 717
691 // av_log(NULL, AV_LOG_ERROR, "OUTdelayed:%d/%d pts:%"PRId64", dts:%"PRId64" cur_dts:%"PRId64"\n", presentation_delayed, delay, pkt->pts, pkt->dts, st->cur_dts); 718 // av_log(NULL, AV_LOG_ERROR, "OUTdelayed:%d/%d pts:%"PRId64", dts:%"PRId64" cur_dts:%"PRId64"\n", presentation_delayed, delay, pkt->pts, pkt->dts, st->cur_dts);
1765 if(st->parser && st->parser->parser->split && !st->codec->extradata) 1792 if(st->parser && st->parser->parser->split && !st->codec->extradata)
1766 break; 1793 break;
1767 if (st->codec->codec_type == CODEC_TYPE_AUDIO && 1794 if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
1768 st->codec->codec_id == CODEC_ID_NONE) 1795 st->codec->codec_id == CODEC_ID_NONE)
1769 break; 1796 break;
1797 if(st->first_dts == AV_NOPTS_VALUE)
1798 break;
1770 } 1799 }
1771 if (i == ic->nb_streams) { 1800 if (i == ic->nb_streams) {
1772 /* NOTE: if the format has no header, then we need to read 1801 /* NOTE: if the format has no header, then we need to read
1773 some packets to get most of the streams, so we cannot 1802 some packets to get most of the streams, so we cannot
1774 stop here */ 1803 stop here */
2048 st->index = s->nb_streams; 2077 st->index = s->nb_streams;
2049 st->id = id; 2078 st->id = id;
2050 st->start_time = AV_NOPTS_VALUE; 2079 st->start_time = AV_NOPTS_VALUE;
2051 st->duration = AV_NOPTS_VALUE; 2080 st->duration = AV_NOPTS_VALUE;
2052 st->cur_dts = AV_NOPTS_VALUE; 2081 st->cur_dts = AV_NOPTS_VALUE;
2082 st->first_dts = AV_NOPTS_VALUE;
2053 2083
2054 /* default pts settings is MPEG like */ 2084 /* default pts settings is MPEG like */
2055 av_set_pts_info(st, 33, 1, 90000); 2085 av_set_pts_info(st, 33, 1, 90000);
2056 st->last_IP_pts = AV_NOPTS_VALUE; 2086 st->last_IP_pts = AV_NOPTS_VALUE;
2057 for(i=0; i<MAX_REORDER_DELAY+1; i++) 2087 for(i=0; i<MAX_REORDER_DELAY+1; i++)