- bs_filter(pkt);
- av_packet_rescale_ts(pkt, st->codec->time_base, st->time_base);
- pkt->stream_index = st->index;
- return av_interleaved_write_frame(ffmpeg->fmt_ctx, pkt);
+ int ret = 0;
+ if( !bsfc ) {
+ av_packet_rescale_ts(pkt, avctx->time_base, st->time_base);
+ pkt->stream_index = st->index;
+ ret = av_interleaved_write_frame(ffmpeg->fmt_ctx, pkt);
+ }
+ else {
+ ret = av_bsf_send_packet(bsfc, pkt);
+ while( ret >= 0 ) {
+ FFPacket bs;
+ if( (ret=av_bsf_receive_packet(bsfc, bs)) < 0 ) {
+ if( ret == AVERROR(EAGAIN) ) return 0;
+ if( ret == AVERROR_EOF ) return -1;
+ break;
+ }
+ av_packet_rescale_ts(bs, avctx->time_base, st->time_base);
+ bs->stream_index = st->index;
+ ret = av_interleaved_write_frame(ffmpeg->fmt_ctx, bs);
+ }
+ }
+ if( ret < 0 )
+ ff_err(ret, "FFStream::write_packet: write packet failed\n");
+ return ret;
+}
+
+int FFStream::encode_frame(AVFrame *frame)
+{
+ int pkts = 0, ret = 0;
+ for( int retry=100; --retry>=0; ) {
+ if( frame || !pkts )
+ ret = avcodec_send_frame(avctx, frame);
+ if( !ret && frame ) return pkts;
+ if( ret < 0 && ret != AVERROR(EAGAIN) ) break;
+ FFPacket opkt;
+ ret = avcodec_receive_packet(avctx, opkt);
+ if( !frame && ret == AVERROR_EOF ) return pkts;
+ if( ret < 0 ) break;
+ ret = write_packet(opkt);
+ if( ret < 0 ) break;
+ ++pkts;
+ }
+ ff_err(ret, "FFStream::encode_frame: encode failed\n");
+ return -1;