+int FileAC3::write_packet()
+{
+ AVCodecContext *&avctx = codec_context;
+ int ret = avcodec_receive_packet(avctx, &avpkt);
+ if( ret >= 0 ) {
+ ret = 0;
+ if( avpkt.data && avpkt.size > 0 ) {
+ int sz = fwrite(avpkt.data, 1, avpkt.size, fd);
+ if( sz == avpkt.size ) ret = 1;
+ }
+ if( !ret )
+ eprintf(_("Error while writing samples. \n%m\n"));
+ av_packet_unref(&avpkt);
+ }
+ else if( ret == AVERROR_EOF )
+ ret = 0;
+ return ret;
+}
+
+int FileAC3::encode_frame(AVFrame *frame)
+{
+ AVCodecContext *&avctx = codec_context;
+ int ret = 0, pkts = 0;
+ for( int retry=100; --retry>=0; ) {
+ ret = avcodec_send_frame(avctx, frame);
+ if( ret >= 0 ) return pkts;
+ if( ret != AVERROR(EAGAIN) ) break;
+ if( (ret=write_packet()) < 0 ) break;
+ if( !ret ) return pkts;
+ ++pkts;
+ }
+ if( ret < 0 ) {
+ char errmsg[BCTEXTLEN];
+ av_strerror(ret, errmsg, sizeof(errmsg));
+ fprintf(stderr, "FileAC3::encode_frame: encode failed: %s\n", errmsg);
+ }
+ return ret;
+}
+
+int FileAC3::encode_flush()
+{
+ AVCodecContext *&avctx = codec_context;
+ int ret = avcodec_send_frame(avctx, 0);
+ while( (ret=write_packet()) > 0 );
+ if( ret < 0 ) {
+ char errmsg[BCTEXTLEN];
+ av_strerror(ret, errmsg, sizeof(errmsg));
+ fprintf(stderr, "FileAC3::encode_flush: encode failed: %s\n", errmsg);
+ }
+ return ret;
+}