From 1e0129246dfeb48bb90c49870ae4d7ee0b23b3cc Mon Sep 17 00:00:00 2001 From: "espendiller@gmx.de" Date: Tue, 28 Dec 2010 19:49:05 +0000 Subject: [PATCH] pkoshevoy [at] sorensonmedia.com: Reordering packets is dangerous, so instead implemented stream re-lacing (does not change order of packets in individual streams) git-svn-id: https://httpsegmenter.googlecode.com/svn/trunk@10 50c576d4-0ac1-1411-950c-f994e1bbdc23 --- segmenter.c | 53 +++++++++++++++++++++++------------------------------ 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/segmenter.c b/segmenter.c index 1c7b766..044aba8 100644 --- a/segmenter.c +++ b/segmenter.c @@ -199,9 +199,6 @@ typedef struct SMPacketLink /* the packet */ AVPacket packet; - /* a boolean flag indicating whether this is an audio packet */ - int isAudio; - /* a link to the next packet */ struct SMPacketLink * next; @@ -215,10 +212,9 @@ typedef struct SMPacketList } TSMPacketList; static TSMPacketLink * -createLink(const AVPacket * packet, double timeStamp, int isAudio) +createLink(const AVPacket * packet, double timeStamp) { TSMPacketLink * link = (TSMPacketLink *) malloc(sizeof(TSMPacketLink)); - link->isAudio = isAudio; link->timeStamp = timeStamp; link->next = NULL; memcpy(&link->packet, packet, sizeof(AVPacket)); @@ -226,14 +222,19 @@ createLink(const AVPacket * packet, double timeStamp, int isAudio) } static int -firstPrecedesSecond(const TSMPacketLink * first, +packetsCorrectlySorted(const TSMPacketLink * first, const TSMPacketLink * second) { - if (first->timeStamp < second->timeStamp || - first->timeStamp == second->timeStamp && first->isAudio) + if (first->packet.stream_index == second->packet.stream_index) { - /* audio packet with the same timestamp as a video packet - should precede the video packet */ + /* assume that the packets in each stream are already correctly sorted */ + return 1; + } + + if (first->timeStamp < second->timeStamp) + { + /* improve lacing so that that audio/video packets that should be + together do not get stuck into separate segments. */ return 1; } @@ -241,9 +242,9 @@ firstPrecedesSecond(const TSMPacketLink * first, } static void -insertPacket(TSMPacketList * packets, const AVPacket * packet, double timeStamp, int isAudio) +insertPacket(TSMPacketList * packets, const AVPacket * packet, double timeStamp) { - TSMPacketLink * link = createLink(packet, timeStamp, isAudio); + TSMPacketLink * link = createLink(packet, timeStamp); if (!packets->head) { assert(!packets->tail); @@ -252,15 +253,6 @@ insertPacket(TSMPacketList * packets, const AVPacket * packet, double timeStamp, packets->tail = link; packets->size = 1; } - else if (firstPrecedesSecond(packets->tail, link)) - { - /* attach at the tail */ - assert(packets->size > 0); - - packets->tail->next = link; - packets->tail = link; - packets->size++; - } else { /* insert link into the list sorted in ascending time stamp order */ @@ -269,7 +261,7 @@ insertPacket(TSMPacketList * packets, const AVPacket * packet, double timeStamp, assert(packets->size > 0); for (TSMPacketLink * i = packets->head; i != NULL; i = i->next) { - if (firstPrecedesSecond(i, link)) + if (packetsCorrectlySorted(i, link)) { prev = i; continue; @@ -289,8 +281,12 @@ insertPacket(TSMPacketList * packets, const AVPacket * packet, double timeStamp, return; } - /* sanity check, this should never happen */ - assert(0); + /* attach at the tail */ + assert(packets->size > 0); + + packets->tail->next = link; + packets->tail = link; + packets->size++; } } @@ -343,8 +339,8 @@ int main(int argc, char **argv) AVOutputFormat *ofmt; AVFormatContext *ic = NULL; AVFormatContext *oc; - AVStream *video_st; - AVStream *audio_st; + AVStream *video_st = NULL; + AVStream *audio_st = NULL; AVCodec *codec; char *output_filename; char *remove_filename; @@ -537,10 +533,7 @@ int main(int argc, char **argv) break; } - insertPacket(packetQueue, - &packet, - timeStamp, - packet.stream_index == audio_index); + insertPacket(packetQueue, &packet, timeStamp); } }