Skip to content

Commit

Permalink
FFmpeg: Clean up code somewhat
Browse files Browse the repository at this point in the history
Remove deprecated functions, make use of free functions that clear the
pointers before returning, etc.

Signed-off-by: Christopher Snowhill <[email protected]>
  • Loading branch information
kode54 committed Feb 18, 2025
1 parent 1c92c56 commit 212ef0f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 69 deletions.
75 changes: 19 additions & 56 deletions Plugins/FFMPEG/FFMPEGContainer.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ + (NSArray *)urlsForContainerURL:(NSURL *)url {

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;
Expand All @@ -62,75 +64,47 @@ + (NSArray *)urlsForContainerURL:(NSURL *)url {
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;

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;
Expand All @@ -155,44 +129,33 @@ + (NSArray *)urlsForContainerURL:(NSURL *)url {

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;

for(i = 0; i < subsongs; ++i) {
[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
18 changes: 5 additions & 13 deletions Plugins/FFMPEG/FFMPEGDecoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// Copyright 2008 __MyCompanyName__. All rights reserved.
//

// test
#import "FFMPEGDecoder.h"

#import "NSDictionary+Merge.h"
Expand Down Expand Up @@ -496,35 +495,28 @@ - (void)close {
}

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);
}
}

Expand Down

0 comments on commit 212ef0f

Please sign in to comment.