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

View file

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