From b571c3f62a9bb0bba7b36e98f5f6c7371c42e79d Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Mon, 17 Feb 2025 18:31:39 -0800 Subject: [PATCH] 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 --- Plugins/FFMPEG/FFMPEGContainer.m | 75 ++++++++------------------------ Plugins/FFMPEG/FFMPEGDecoder.m | 18 +++----- 2 files changed, 24 insertions(+), 69 deletions(-) diff --git a/Plugins/FFMPEG/FFMPEGContainer.m b/Plugins/FFMPEG/FFMPEGContainer.m index d1a70084c..002dd7b47 100644 --- a/Plugins/FFMPEG/FFMPEGContainer.m +++ b/Plugins/FFMPEG/FFMPEGContainer.m @@ -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 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 diff --git a/Plugins/FFMPEG/FFMPEGDecoder.m b/Plugins/FFMPEG/FFMPEGDecoder.m index 457283d7e..8e604a997 100644 --- a/Plugins/FFMPEG/FFMPEGDecoder.m +++ b/Plugins/FFMPEG/FFMPEGDecoder.m @@ -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); } }