FFmpeg: Clean up code somewhat

Remove deprecated functions, make use of free functions that clear the
pointers before returning, etc.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
This commit is contained in:
Christopher Snowhill 2025-02-17 18:31:39 -08:00
parent 80e909fbbe
commit b571c3f62a
2 changed files with 24 additions and 69 deletions

View file

@ -30,14 +30,16 @@
if([url fragment]) { if([url fragment]) {
// input url already has fragment defined - no need to expand further // input url already has fragment defined - no need to expand further
return [NSMutableArray arrayWithObject:url]; return @[url];
} }
NSMutableArray *tracks = [[NSMutableArray alloc] init];
id audioSourceClass = NSClassFromString(@"AudioSource"); id audioSourceClass = NSClassFromString(@"AudioSource");
id<CogSource> source = [audioSourceClass audioSourceForURL:url]; id<CogSource> source = [audioSourceClass audioSourceForURL:url];
if(![source open:url]) if(![source open:url])
return [NSArray array]; return @[];
int errcode, i; int errcode, i;
AVStream *stream; AVStream *stream;
@ -62,42 +64,32 @@
formatCtx = avformat_alloc_context(); formatCtx = avformat_alloc_context();
if(!formatCtx) { if(!formatCtx) {
ALog(@"Unable to allocate AVFormat context"); ALog(@"Unable to allocate AVFormat context");
return [NSArray array]; goto exit;
} }
NSString *urlString = [url absoluteString]; NSString *urlString = [url absoluteString];
if((errcode = avformat_open_input(&formatCtx, [urlString UTF8String], NULL, NULL)) < 0) { if((errcode = avformat_open_input(&formatCtx, [urlString UTF8String], NULL, NULL)) < 0) {
av_strerror(errcode, errDescr, 4096); av_strerror(errcode, errDescr, 4096);
ALog(@"Error opening file, errcode = %d, error = %s", errcode, errDescr); ALog(@"Error opening file, errcode = %d, error = %s", errcode, errDescr);
return [NSArray array]; goto exit;
} }
} else { } else {
buffer = av_malloc(32 * 1024); buffer = av_malloc(32 * 1024);
if(!buffer) { if(!buffer) {
ALog(@"Out of memory!"); ALog(@"Out of memory!");
[source close]; goto exit;
source = nil;
return [NSArray array];
} }
ioCtx = avio_alloc_context(buffer, 32 * 1024, 0, (__bridge void *)source, ffmpeg_read, ffmpeg_write, ffmpeg_seek); ioCtx = avio_alloc_context(buffer, 32 * 1024, 0, (__bridge void *)source, ffmpeg_read, ffmpeg_write, ffmpeg_seek);
if(!ioCtx) { if(!ioCtx) {
ALog(@"Unable to create AVIO context"); ALog(@"Unable to create AVIO context");
av_free(buffer); goto exit;
[source close];
source = nil;
return [NSArray array];
} }
formatCtx = avformat_alloc_context(); formatCtx = avformat_alloc_context();
if(!formatCtx) { if(!formatCtx) {
ALog(@"Unable to allocate AVFormat context"); ALog(@"Unable to allocate AVFormat context");
buffer = ioCtx->buffer; goto exit;
av_free(ioCtx);
av_free(buffer);
[source close];
source = nil;
return [NSArray array];
} }
formatCtx->pb = ioCtx; formatCtx->pb = ioCtx;
@ -105,32 +97,14 @@
if((errcode = avformat_open_input(&formatCtx, "", NULL, NULL)) < 0) { if((errcode = avformat_open_input(&formatCtx, "", NULL, NULL)) < 0) {
av_strerror(errcode, errDescr, 4096); av_strerror(errcode, errDescr, 4096);
ALog(@"Error opening file, errcode = %d, error = %s", errcode, errDescr); ALog(@"Error opening file, errcode = %d, error = %s", errcode, errDescr);
avformat_close_input(&(formatCtx)); goto exit;
buffer = ioCtx->buffer;
av_free(ioCtx);
av_free(buffer);
[source close];
source = nil;
return [NSArray array];
} }
} }
if((errcode = avformat_find_stream_info(formatCtx, NULL)) < 0) { if((errcode = avformat_find_stream_info(formatCtx, NULL)) < 0) {
av_strerror(errcode, errDescr, 4096); av_strerror(errcode, errDescr, 4096);
ALog(@"Can't find stream info, errcode = %d, error = %s", errcode, errDescr); ALog(@"Can't find stream info, errcode = %d, error = %s", errcode, errDescr);
avformat_close_input(&(formatCtx)); goto exit;
if(ioCtx) {
buffer = ioCtx->buffer;
av_free(ioCtx);
}
if(buffer) {
av_free(buffer);
}
if(source) {
[source close];
source = nil;
}
return [NSArray array];
} }
int streamIndex = -1; int streamIndex = -1;
@ -155,23 +129,9 @@
if(streamIndex < 0) { if(streamIndex < 0) {
ALog(@"no audio codec found"); ALog(@"no audio codec found");
avformat_close_input(&(formatCtx)); goto exit;
if(ioCtx) {
buffer = ioCtx->buffer;
av_free(ioCtx);
}
if(buffer) {
av_free(buffer);
}
if(source) {
[source close];
source = nil;
}
return [NSArray array];
} }
NSMutableArray *tracks = [NSMutableArray array];
int subsongs = formatCtx->nb_chapters; int subsongs = formatCtx->nb_chapters;
if(subsongs < 1) subsongs = 1; if(subsongs < 1) subsongs = 1;
@ -179,20 +139,23 @@
[tracks addObject:[NSURL URLWithString:[[url absoluteString] stringByAppendingFormat:@"#%i", i]]]; [tracks addObject:[NSURL URLWithString:[[url absoluteString] stringByAppendingFormat:@"#%i", i]]];
} }
avformat_close_input(&(formatCtx)); exit:
if(formatCtx) {
avformat_close_input(&(formatCtx));
}
if(ioCtx) { if(ioCtx) {
buffer = ioCtx->buffer; buffer = ioCtx->buffer;
av_free(ioCtx); avio_context_free(&ioCtx);
} }
if(buffer) { if(buffer) {
av_free(buffer); av_freep(&buffer);
} }
if(source) { if(source) {
[source close]; [source close];
source = nil; source = nil;
} }
return tracks; return [NSArray arrayWithArray:tracks];
} }
@end @end

View file

@ -6,7 +6,6 @@
// Copyright 2008 __MyCompanyName__. All rights reserved. // Copyright 2008 __MyCompanyName__. All rights reserved.
// //
// test
#import "FFMPEGDecoder.h" #import "FFMPEGDecoder.h"
#import "NSDictionary+Merge.h" #import "NSDictionary+Merge.h"
@ -496,35 +495,28 @@ static uint8_t reverse_bits[0x100];
} }
if(lastDecodedFrame) { if(lastDecodedFrame) {
av_free(lastDecodedFrame); av_freep(&lastDecodedFrame);
lastDecodedFrame = NULL;
} }
if(codecCtx) { if(codecCtx) {
avcodec_close(codecCtx);
avcodec_free_context(&codecCtx); avcodec_free_context(&codecCtx);
codecCtx = NULL;
} }
if(formatCtx) { if(formatCtx) {
avformat_close_input(&(formatCtx)); avformat_close_input(&formatCtx);
formatCtx = NULL;
} }
if(ioCtx) { if(ioCtx) {
buffer = ioCtx->buffer; buffer = ioCtx->buffer;
av_free(ioCtx); avio_context_free(&ioCtx);
ioCtx = NULL;
} }
if(sampleBuffer) { if(sampleBuffer) {
av_free(sampleBuffer); av_freep(&sampleBuffer);
sampleBuffer = NULL;
} }
if(buffer) { if(buffer) {
av_free(buffer); av_freep(&buffer);
buffer = NULL;
} }
} }