int lame_encode_finish(lame_global_flags *gfp,char *mp3buffer, int mp3buffer_size) { int imp3,mp3count,mp3buffer_size_remaining; short int buffer[2][1152]; memset((char *)buffer,0,sizeof(buffer)); mp3count = 0; while (mf_samples_to_encode > 0) { mp3buffer_size_remaining = mp3buffer_size - mp3count; /* if user specifed buffer size = 0, dont check size */ if (mp3buffer_size == 0) mp3buffer_size_remaining=0; imp3=lame_encode(gfp,buffer,mp3buffer,mp3buffer_size_remaining); if (imp3 == -1) { /* fatel error: mp3buffer too small */ desalloc_buffer(&bs); /* Deallocate all buffers */ return -1; } mp3buffer += imp3; mp3count += imp3; mf_samples_to_encode -= gfp->framesize; } gfp->frameNum--; if (!gfp->gtkflag && !gfp->silent) { timestatus(gfp->out_samplerate,gfp->frameNum,gfp->totalframes,gfp->framesize); #ifdef BRHIST if (disp_brhist) { brhist_add_count(); brhist_disp(); brhist_disp_total(gfp); } #endif fprintf(stderr,"\n"); fflush(stderr); } III_FlushBitstream(); mp3buffer_size_remaining = mp3buffer_size - mp3count; /* if user specifed buffer size = 0, dont check size */ if (mp3buffer_size == 0) mp3buffer_size_remaining=0; imp3= copy_buffer(mp3buffer,mp3buffer_size_remaining,&bs); if (imp3 == -1) { /* fatel error: mp3buffer too small */ desalloc_buffer(&bs); /* Deallocate all buffers */ return -1; } mp3count += imp3; desalloc_buffer(&bs); /* Deallocate all buffers */ return mp3count; }
int lame_encoder(lame_global_flags * gf, FILE * outf, int nogap, char *inPath, char *outPath) { unsigned char mp3buffer[LAME_MAXMP3BUFFER]; int Buffer[2][1152]; int iread, imp3; static const char *mode_names[2][4] = { {"stereo", "j-stereo", "dual-ch", "single-ch"}, {"stereo", "force-ms", "dual-ch", "single-ch"} }; int frames; if (silent < 10) { lame_print_config(gf); /* print useful information about options being used */ fprintf(stderr, "Encoding %s%s to %s\n", strcmp(inPath, "-") ? inPath : "<stdin>", strlen(inPath) + strlen(outPath) < 66 ? "" : "\n ", strcmp(outPath, "-") ? outPath : "<stdout>"); fprintf(stderr, "Encoding as %g kHz ", 1.e-3 * lame_get_out_samplerate(gf)); { const char *appendix = ""; switch (lame_get_VBR(gf)) { case vbr_mt: case vbr_rh: case vbr_mtrh: appendix = "ca. "; fprintf(stderr, "VBR(q=%i)", lame_get_VBR_q(gf)); break; case vbr_abr: fprintf(stderr, "average %d kbps", lame_get_VBR_mean_bitrate_kbps(gf)); break; default: fprintf(stderr, "%3d kbps", lame_get_brate(gf)); break; } fprintf(stderr, " %s MPEG-%u%s Layer III (%s%gx) qval=%i\n", mode_names[lame_get_force_ms(gf)][lame_get_mode(gf)], 2 - lame_get_version(gf), lame_get_out_samplerate(gf) < 16000 ? ".5" : "", appendix, 0.1 * (int) (10. * lame_get_compression_ratio(gf) + 0.5), lame_get_quality(gf)); } if (silent <= -10) lame_print_internals(gf); fflush(stderr); } /* encode until we hit eof */ do { /* read in 'iread' samples */ iread = get_audio(gf, Buffer); frames = lame_get_frameNum(gf); /********************** status display *****************************/ if (silent <= 0) { if (update_interval > 0) { timestatus_klemm(gf); } else { if (0 == frames % 50) { #ifdef BRHIST brhist_jump_back(); #endif timestatus(lame_get_out_samplerate(gf), frames, lame_get_totalframes(gf), lame_get_framesize(gf)); #ifdef BRHIST if (brhist) brhist_disp(gf); #endif } } } /* encode */ imp3 = lame_encode_buffer_int(gf, Buffer[0], Buffer[1], iread, mp3buffer, sizeof(mp3buffer)); /* was our output buffer big enough? */ if (imp3 < 0) { if (imp3 == -1) fprintf(stderr, "mp3 buffer is not big enough... \n"); else fprintf(stderr, "mp3 internal error: error code=%i\n", imp3); return 1; } if (fwrite(mp3buffer, 1, imp3, outf) != imp3) { fprintf(stderr, "Error writing mp3 output \n"); return 1; } } while (iread); if (nogap) imp3 = lame_encode_flush_nogap(gf, mp3buffer, sizeof(mp3buffer)); /* may return one more mp3 frame */ else imp3 = lame_encode_flush(gf, mp3buffer, sizeof(mp3buffer)); /* may return one more mp3 frame */ if (imp3 < 0) { if (imp3 == -1) fprintf(stderr, "mp3 buffer is not big enough... \n"); else fprintf(stderr, "mp3 internal error: error code=%i\n", imp3); return 1; } if (silent <= 0) { #ifdef BRHIST brhist_jump_back(); #endif frames = lame_get_frameNum(gf); timestatus(lame_get_out_samplerate(gf), frames, lame_get_totalframes(gf), lame_get_framesize(gf)); #ifdef BRHIST if (brhist) { brhist_disp(gf); } brhist_disp_total(gf); #endif timestatus_finish(); } fwrite(mp3buffer, 1, imp3, outf); return 0; }