void avfilter_unref_buffer(AVFilterBufferRef *ref)
{
    if(!(--ref->buf->refcount))
        ref->buf->free(ref->buf);
    av_free(ref->video);
    av_free(ref);
}
['static int output_packet(AVInputStream *ist, int ist_index,\n                         AVOutputStream **ost_table, int nb_ostreams,\n                         const AVPacket *pkt)\n{\n    AVFormatContext *os;\n    AVOutputStream *ost;\n    int ret, i;\n    int got_picture;\n    AVFrame picture;\n    void *buffer_to_free;\n    static unsigned int samples_size= 0;\n    AVSubtitle subtitle, *subtitle_to_free;\n#if CONFIG_AVFILTER\n    int frame_available;\n#endif\n    AVPacket avpkt;\n    int bps = av_get_bits_per_sample_format(ist->st->codec->sample_fmt)>>3;\n    if(ist->next_pts == AV_NOPTS_VALUE)\n        ist->next_pts= ist->pts;\n    if (pkt == NULL) {\n        av_init_packet(&avpkt);\n        avpkt.data = NULL;\n        avpkt.size = 0;\n        goto handle_eof;\n    } else {\n        avpkt = *pkt;\n    }\n    if(pkt->dts != AV_NOPTS_VALUE)\n        ist->next_pts = ist->pts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);\n    while (avpkt.size > 0 || (!pkt && ist->next_pts != ist->pts)) {\n        uint8_t *data_buf, *decoded_data_buf;\n        int data_size, decoded_data_size;\n    handle_eof:\n        ist->pts= ist->next_pts;\n        if(avpkt.size && avpkt.size != pkt->size &&\n           ((!ist->showed_multi_packet_warning && verbose>0) || verbose>1)){\n            fprintf(stderr, "Multiple frames in a packet from stream %d\\n", pkt->stream_index);\n            ist->showed_multi_packet_warning=1;\n        }\n        decoded_data_buf = NULL;\n        decoded_data_size= 0;\n        data_buf  = avpkt.data;\n        data_size = avpkt.size;\n        subtitle_to_free = NULL;\n        if (ist->decoding_needed) {\n            switch(ist->st->codec->codec_type) {\n            case AVMEDIA_TYPE_AUDIO:{\n                if(pkt && samples_size < FFMAX(pkt->size*sizeof(*samples), AVCODEC_MAX_AUDIO_FRAME_SIZE)) {\n                    samples_size = FFMAX(pkt->size*sizeof(*samples), AVCODEC_MAX_AUDIO_FRAME_SIZE);\n                    av_free(samples);\n                    samples= av_malloc(samples_size);\n                }\n                decoded_data_size= samples_size;\n                ret = avcodec_decode_audio3(ist->st->codec, samples, &decoded_data_size,\n                                            &avpkt);\n                if (ret < 0)\n                    goto fail_decode;\n                avpkt.data += ret;\n                avpkt.size -= ret;\n                data_size   = ret;\n                if (decoded_data_size <= 0) {\n                    continue;\n                }\n                decoded_data_buf = (uint8_t *)samples;\n                ist->next_pts += ((int64_t)AV_TIME_BASE/bps * decoded_data_size) /\n                    (ist->st->codec->sample_rate * ist->st->codec->channels);\n                break;}\n            case AVMEDIA_TYPE_VIDEO:\n                    decoded_data_size = (ist->st->codec->width * ist->st->codec->height * 3) / 2;\n                    avcodec_get_frame_defaults(&picture);\n                    ret = avcodec_decode_video2(ist->st->codec,\n                                                &picture, &got_picture, &avpkt);\n                    ist->st->quality= picture.quality;\n                    if (ret < 0)\n                        goto fail_decode;\n                    if (!got_picture) {\n                        goto discard_packet;\n                    }\n                    if (ist->st->codec->time_base.num != 0) {\n                        int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;\n                        ist->next_pts += ((int64_t)AV_TIME_BASE *\n                                          ist->st->codec->time_base.num * ticks) /\n                            ist->st->codec->time_base.den;\n                    }\n                    avpkt.size = 0;\n                    break;\n            case AVMEDIA_TYPE_SUBTITLE:\n                ret = avcodec_decode_subtitle2(ist->st->codec,\n                                               &subtitle, &got_picture, &avpkt);\n                if (ret < 0)\n                    goto fail_decode;\n                if (!got_picture) {\n                    goto discard_packet;\n                }\n                subtitle_to_free = &subtitle;\n                avpkt.size = 0;\n                break;\n            default:\n                goto fail_decode;\n            }\n        } else {\n            switch(ist->st->codec->codec_type) {\n            case AVMEDIA_TYPE_AUDIO:\n                ist->next_pts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /\n                    ist->st->codec->sample_rate;\n                break;\n            case AVMEDIA_TYPE_VIDEO:\n                if (ist->st->codec->time_base.num != 0) {\n                    int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;\n                    ist->next_pts += ((int64_t)AV_TIME_BASE *\n                                      ist->st->codec->time_base.num * ticks) /\n                        ist->st->codec->time_base.den;\n                }\n                break;\n            }\n            ret = avpkt.size;\n            avpkt.size = 0;\n        }\n        buffer_to_free = NULL;\n        if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {\n            pre_process_video_frame(ist, (AVPicture *)&picture,\n                                    &buffer_to_free);\n        }\n#if CONFIG_AVFILTER\n        if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ist->input_video_filter) {\n            av_vsrc_buffer_add_frame(ist->input_video_filter, &picture,\n                                     ist->pts,\n                                     ist->st->codec->sample_aspect_ratio);\n        }\n#endif\n        if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {\n            if (audio_volume != 256) {\n                short *volp;\n                volp = samples;\n                for(i=0;i<(decoded_data_size / sizeof(short));i++) {\n                    int v = ((*volp) * audio_volume + 128) >> 8;\n                    if (v < -32768) v = -32768;\n                    if (v >  32767) v = 32767;\n                    *volp++ = v;\n                }\n            }\n        }\n        if (rate_emu) {\n            int64_t pts = av_rescale(ist->pts, 1000000, AV_TIME_BASE);\n            int64_t now = av_gettime() - ist->start;\n            if (pts > now)\n                usleep(pts - now);\n        }\n#if CONFIG_AVFILTER\n        frame_available = ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO ||\n            !ist->out_video_filter || avfilter_poll_frame(ist->out_video_filter->inputs[0]);\n#endif\n        if (start_time == 0 || ist->pts >= start_time)\n#if CONFIG_AVFILTER\n        while (frame_available) {\n            if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ist->out_video_filter)\n                get_filtered_video_pic(ist->out_video_filter, &ist->picref, &picture, &ist->pts);\n#endif\n            for(i=0;i<nb_ostreams;i++) {\n                int frame_size;\n                ost = ost_table[i];\n                if (ost->source_index == ist_index) {\n                    os = output_files[ost->file_index];\n                    if (ost->encoding_needed) {\n                        assert(ist->decoding_needed);\n                        switch(ost->st->codec->codec_type) {\n                        case AVMEDIA_TYPE_AUDIO:\n                            do_audio_out(os, ost, ist, decoded_data_buf, decoded_data_size);\n                            break;\n                        case AVMEDIA_TYPE_VIDEO:\n#if CONFIG_AVFILTER\n                            if (ist->picref->video)\n                                ost->st->codec->sample_aspect_ratio = ist->picref->video->pixel_aspect;\n#endif\n                            do_video_out(os, ost, ist, &picture, &frame_size);\n                            if (vstats_filename && frame_size)\n                                do_video_stats(os, ost, frame_size);\n                            break;\n                        case AVMEDIA_TYPE_SUBTITLE:\n                            do_subtitle_out(os, ost, ist, &subtitle,\n                                            pkt->pts);\n                            break;\n                        default:\n                            abort();\n                        }\n                    } else {\n                        AVFrame avframe;\n                        AVPacket opkt;\n                        int64_t ost_tb_start_time= av_rescale_q(start_time, AV_TIME_BASE_Q, ost->st->time_base);\n                        av_init_packet(&opkt);\n                        if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) && !copy_initial_nonkeyframes)\n                            continue;\n                        avcodec_get_frame_defaults(&avframe);\n                        ost->st->codec->coded_frame= &avframe;\n                        avframe.key_frame = pkt->flags & AV_PKT_FLAG_KEY;\n                        if(ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)\n                            audio_size += data_size;\n                        else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {\n                            video_size += data_size;\n                            ost->sync_opts++;\n                        }\n                        opkt.stream_index= ost->index;\n                        if(pkt->pts != AV_NOPTS_VALUE)\n                            opkt.pts= av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time;\n                        else\n                            opkt.pts= AV_NOPTS_VALUE;\n                        if (pkt->dts == AV_NOPTS_VALUE)\n                            opkt.dts = av_rescale_q(ist->pts, AV_TIME_BASE_Q, ost->st->time_base);\n                        else\n                            opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base);\n                        opkt.dts -= ost_tb_start_time;\n                        opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base);\n                        opkt.flags= pkt->flags;\n                        if(   ost->st->codec->codec_id != CODEC_ID_H264\n                           && ost->st->codec->codec_id != CODEC_ID_MPEG1VIDEO\n                           && ost->st->codec->codec_id != CODEC_ID_MPEG2VIDEO\n                           ) {\n                            if(av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, data_buf, data_size, pkt->flags & AV_PKT_FLAG_KEY))\n                                opkt.destruct= av_destruct_packet;\n                        } else {\n                            opkt.data = data_buf;\n                            opkt.size = data_size;\n                        }\n                        write_frame(os, &opkt, ost->st->codec, bitstream_filters[ost->file_index][opkt.stream_index]);\n                        ost->st->codec->frame_number++;\n                        ost->frame_number++;\n                        av_free_packet(&opkt);\n                    }\n                }\n            }\n#if CONFIG_AVFILTER\n            frame_available = (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&\n                              ist->out_video_filter && avfilter_poll_frame(ist->out_video_filter->inputs[0]);\n            if(ist->picref)\n                avfilter_unref_buffer(ist->picref);\n        }\n#endif\n        av_free(buffer_to_free);\n        if (subtitle_to_free) {\n            if (subtitle_to_free->rects != NULL) {\n                for (i = 0; i < subtitle_to_free->num_rects; i++) {\n                    av_freep(&subtitle_to_free->rects[i]->pict.data[0]);\n                    av_freep(&subtitle_to_free->rects[i]->pict.data[1]);\n                    av_freep(&subtitle_to_free->rects[i]);\n                }\n                av_freep(&subtitle_to_free->rects);\n            }\n            subtitle_to_free->num_rects = 0;\n            subtitle_to_free = NULL;\n        }\n    }\n discard_packet:\n    if (pkt == NULL) {\n        for(i=0;i<nb_ostreams;i++) {\n            ost = ost_table[i];\n            if (ost->source_index == ist_index) {\n                AVCodecContext *enc= ost->st->codec;\n                os = output_files[ost->file_index];\n                if(ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <=1)\n                    continue;\n                if(ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE))\n                    continue;\n                if (ost->encoding_needed) {\n                    for(;;) {\n                        AVPacket pkt;\n                        int fifo_bytes;\n                        av_init_packet(&pkt);\n                        pkt.stream_index= ost->index;\n                        switch(ost->st->codec->codec_type) {\n                        case AVMEDIA_TYPE_AUDIO:\n                            fifo_bytes = av_fifo_size(ost->fifo);\n                            ret = 0;\n                            if (fifo_bytes > 0) {\n                                int osize = av_get_bits_per_sample_format(enc->sample_fmt) >> 3;\n                                int fs_tmp = enc->frame_size;\n                                av_fifo_generic_read(ost->fifo, audio_buf, fifo_bytes, NULL);\n                                if (enc->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {\n                                    enc->frame_size = fifo_bytes / (osize * enc->channels);\n                                } else {\n                                    int frame_bytes = enc->frame_size*osize*enc->channels;\n                                    if (allocated_audio_buf_size < frame_bytes)\n                                        ffmpeg_exit(1);\n                                    memset(audio_buf+fifo_bytes, 0, frame_bytes - fifo_bytes);\n                                }\n                                ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, (short *)audio_buf);\n                                pkt.duration = av_rescale((int64_t)enc->frame_size*ost->st->time_base.den,\n                                                          ost->st->time_base.num, enc->sample_rate);\n                                enc->frame_size = fs_tmp;\n                            }\n                            if(ret <= 0) {\n                                ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, NULL);\n                            }\n                            if (ret < 0) {\n                                fprintf(stderr, "Audio encoding failed\\n");\n                                ffmpeg_exit(1);\n                            }\n                            audio_size += ret;\n                            pkt.flags |= AV_PKT_FLAG_KEY;\n                            break;\n                        case AVMEDIA_TYPE_VIDEO:\n                            ret = avcodec_encode_video(enc, bit_buffer, bit_buffer_size, NULL);\n                            if (ret < 0) {\n                                fprintf(stderr, "Video encoding failed\\n");\n                                ffmpeg_exit(1);\n                            }\n                            video_size += ret;\n                            if(enc->coded_frame && enc->coded_frame->key_frame)\n                                pkt.flags |= AV_PKT_FLAG_KEY;\n                            if (ost->logfile && enc->stats_out) {\n                                fprintf(ost->logfile, "%s", enc->stats_out);\n                            }\n                            break;\n                        default:\n                            ret=-1;\n                        }\n                        if(ret<=0)\n                            break;\n                        pkt.data= bit_buffer;\n                        pkt.size= ret;\n                        if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)\n                            pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);\n                        write_frame(os, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);\n                    }\n                }\n            }\n        }\n    }\n    return 0;\n fail_decode:\n    return -1;\n}', 'void avfilter_unref_buffer(AVFilterBufferRef *ref)\n{\n    if(!(--ref->buf->refcount))\n        ref->buf->free(ref->buf);\n    av_free(ref->video);\n    av_free(ref);\n}']