Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define INBUF_SIZE 4096
- #define AUDIO_INBUF_SIZE 1152
- #define AUDIO_REFILL_THRESH 4096
- OSStatus compressWithFF(NSString* source, NSString* dest, long bitRate, int channels);
- OSStatus compressWithFF(NSString* source, NSString* dest, long bitRate, int channels){
- OSStatus error = noErr;
- AVCodec *codec;
- AVCodecContext *c= NULL;
- int frame_size, outbuf_size, ret;
- FILE *outfile, *f;
- uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
- short *samples;
- uint8_t *outbuf;
- AVPacket avpkt;
- AVFrame *encoded_frame = NULL;
- int got_packet_ptr = 0;
- long progresspoint;
- ExtAudioFileRef sourceFile = 0;
- CFURLRef sourceURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (__bridge CFStringRef)source, kCFURLPOSIXPathStyle, false);
- XThrowIfError(ExtAudioFileOpenURL(sourceURL, &sourceFile), "ExtAudioFileOpenURL failed");
- UInt64 fileLength;
- UInt32 flsize = sizeof(fileLength);
- //get da size
- ExtAudioFileGetProperty(sourceFile, kExtAudioFileProperty_FileLengthFrames, &flsize, &fileLength);
- NSLog(@"--> file length in frames is %llu", fileLength);
- av_init_packet(&avpkt);
- printf("Audio encoding\n");
- avcodec_register_all();
- codec = avcodec_find_encoder(CODEC_ID_MP3);
- if (!codec) {
- fprintf(stderr, "codec not found\n");
- exit(1);
- }
- c = avcodec_alloc_context3(codec);
- /* put sample parameters */
- c->bit_rate = bitRate;
- c->sample_rate = 44100;
- c->channels = channels;
- c->sample_fmt = AV_SAMPLE_FMT_S16;
- c->request_channels = 1;
- c->channel_layout = AV_CH_LAYOUT_MONO;
- /* open it */
- if (avcodec_open2(c, codec, 0) < 0) {
- fprintf(stderr, "could not open codec\n");
- exit(1);
- }
- f = fopen([source UTF8String], "rb");
- if (!f) {
- fprintf(stderr, "could not open %s\n", "mf");
- exit(1);
- }
- fseek(f, 10, SEEK_SET);
- outfile = fopen([dest UTF8String], "wb");
- if (!outfile) {
- //fprintf(stderr, "could not open %s\n", outfilename);
- }
- /* the codec gives us the frame size, in samples this is always 1152 methinks... */
- frame_size = c->frame_size;
- //were going to need to set aside 1100 sampels times 2 bytes (16 bit) STEREO
- samples = (short*)malloc(frame_size * 2 * c->channels);
- outbuf_size = 3000;
- outbuf = (uint8_t*)malloc(outbuf_size);
- /* encode until eof */
- avpkt.data = outbuf;
- // read into inbuff objects of size 1 byte , audio inbuf size of them, from f
- avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
- NSLog(@"just red a thousand bytes into inbuff from the original file file");
- avpkt.size /= 2;
- while (avpkt.size > 0) {
- int got_frame = 0;
- if (!encoded_frame) {
- if (!(encoded_frame = avcodec_alloc_frame())) {
- fprintf(stderr, "out of memory\n");
- exit(1);
- }
- } else {
- avcodec_get_frame_defaults(encoded_frame);
- encoded_frame->nb_samples = AUDIO_INBUF_SIZE / (c->channels * av_get_bytes_per_sample(c->sample_fmt));
- //encoded_frame->nb_samples = frame_size;
- if ((ret = avcodec_fill_audio_frame(encoded_frame, c->channels, c->sample_fmt,
- inbuf, AUDIO_INBUF_SIZE, 1)) < 0) {
- av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
- exit(1);
- }
- if (avcodec_encode_audio2(c, &avpkt, encoded_frame, &got_packet_ptr) < 0) {
- av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
- exit(1);
- }
- ret = avpkt.size;
- if (got_packet_ptr) {
- /* if a frame has been encoded, output it */
- int data_size = av_samples_get_buffer_size(NULL, c->channels,
- encoded_frame->nb_samples,
- c->sample_fmt, 1);
- //fwrite(encoded_frame->data[0], 1, data_size, outfile);
- fwrite(avpkt.data, 1, avpkt.size, outfile);
- progresspoint += avpkt.size;
- //NSLog(@"LOL------> %lu of %llu", progresspoint, fileLength);
- dispatch_async(dispatch_get_main_queue(), ^{
- float progress = (float)progresspoint / (float)fileLength;
- NSNumber *prog = [NSNumber numberWithFloat:progress];
- NSString *title = [NSString stringWithString:@"converting"];
- NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
- [userInfo setObject:prog forKey:@"progress"];
- [userInfo setObject:title forKey:@"title"];
- [[NSNotificationCenter defaultCenter] postNotificationName:@"progress" object:title userInfo:userInfo];
- });
- av_free_packet(&avpkt);
- }
- avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
- avpkt.size = avpkt.size / 2;
- avpkt.dts =
- avpkt.pts = AV_NOPTS_VALUE;
- #if 0
- if (avpkt.size < AUDIO_REFILL_THRESH) {
- /* Refill the input buffer, to avoid trying to decode
- * incomplete frames. Instead of this, one could also use
- * a parser, or use a proper container format through
- * libavformat. */
- memmove(inbuf, avpkt.data, avpkt.size);
- printf("memmove done\n");
- avpkt.data = inbuf;
- len = fread(avpkt.data + avpkt.size, 1,
- AUDIO_INBUF_SIZE - avpkt.size, f);
- printf("fread done\n");
- if (len > 0)
- avpkt.size += len;
- printf("refill done\n");
- }
- #endif
- }
- }
- fclose(outfile);
- fclose(f);
- avcodec_close(c);
- av_free(c);
- av_free(encoded_frame);
- free(outbuf);
- free(samples);
- return error;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement