int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush){
    AVPacketList *pktl, **next_point, *this_pktl;
    int stream_count=0;
    int streams[MAX_STREAMS];
    if(pkt){
        AVStream *st= s->streams[ pkt->stream_index];
        this_pktl = av_mallocz(sizeof(AVPacketList));
        this_pktl->pkt= *pkt;
        if(pkt->destruct == av_destruct_packet)
            pkt->destruct= NULL;
        else
            av_dup_packet(&this_pktl->pkt);
        next_point = &s->packet_buffer;
        while(*next_point){
            AVStream *st2= s->streams[ (*next_point)->pkt.stream_index];
            int64_t left=  st2->time_base.num * (int64_t)st ->time_base.den;
            int64_t right= st ->time_base.num * (int64_t)st2->time_base.den;
            if((*next_point)->pkt.dts * left > pkt->dts * right)
                break;
            next_point= &(*next_point)->next;
        }
        this_pktl->next= *next_point;
        *next_point= this_pktl;
    }
    memset(streams, 0, sizeof(streams));
    pktl= s->packet_buffer;
    while(pktl){
        if(streams[ pktl->pkt.stream_index ] == 0)
            stream_count++;
        streams[ pktl->pkt.stream_index ]++;
        pktl= pktl->next;
    }
    if(stream_count && (s->nb_streams == stream_count || flush)){
        pktl= s->packet_buffer;
        *out= pktl->pkt;
        s->packet_buffer= pktl->next;
        av_freep(&pktl);
        return 1;
    }else{
        av_init_packet(out);
        return 0;
    }
}
['int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush){\n    AVPacketList *pktl, **next_point, *this_pktl;\n    int stream_count=0;\n    int streams[MAX_STREAMS];\n    if(pkt){\n        AVStream *st= s->streams[ pkt->stream_index];\n        this_pktl = av_mallocz(sizeof(AVPacketList));\n        this_pktl->pkt= *pkt;\n        if(pkt->destruct == av_destruct_packet)\n            pkt->destruct= NULL;\n        else\n            av_dup_packet(&this_pktl->pkt);\n        next_point = &s->packet_buffer;\n        while(*next_point){\n            AVStream *st2= s->streams[ (*next_point)->pkt.stream_index];\n            int64_t left=  st2->time_base.num * (int64_t)st ->time_base.den;\n            int64_t right= st ->time_base.num * (int64_t)st2->time_base.den;\n            if((*next_point)->pkt.dts * left > pkt->dts * right)\n                break;\n            next_point= &(*next_point)->next;\n        }\n        this_pktl->next= *next_point;\n        *next_point= this_pktl;\n    }\n    memset(streams, 0, sizeof(streams));\n    pktl= s->packet_buffer;\n    while(pktl){\n        if(streams[ pktl->pkt.stream_index ] == 0)\n            stream_count++;\n        streams[ pktl->pkt.stream_index ]++;\n        pktl= pktl->next;\n    }\n    if(stream_count && (s->nb_streams == stream_count || flush)){\n        pktl= s->packet_buffer;\n        *out= pktl->pkt;\n        s->packet_buffer= pktl->next;\n        av_freep(&pktl);\n        return 1;\n    }else{\n        av_init_packet(out);\n        return 0;\n    }\n}', 'void *av_mallocz(unsigned int size)\n{\n    void *ptr = av_malloc(size);\n    if (ptr)\n        memset(ptr, 0, size);\n    return ptr;\n}', 'void *av_malloc(unsigned int size)\n{\n    void *ptr;\n#ifdef CONFIG_MEMALIGN_HACK\n    long diff;\n#endif\n    if(size > (INT_MAX-16) )\n        return NULL;\n#ifdef CONFIG_MEMALIGN_HACK\n    ptr = malloc(size+16);\n    if(!ptr)\n        return ptr;\n    diff= ((-(long)ptr - 1)&15) + 1;\n    ptr = (char*)ptr + diff;\n    ((char*)ptr)[-1]= diff;\n#elif defined (HAVE_MEMALIGN)\n    ptr = memalign(16,size);\n#else\n    ptr = malloc(size);\n#endif\n    return ptr;\n}']