Fixing a leak & a segfault

This commit is contained in:
Zoe Roux
2020-10-29 22:01:53 +01:00
parent c5d0d81b06
commit 29c0bc4c7e
6 changed files with 49 additions and 26 deletions

3
.gitignore vendored
View File

@@ -2,4 +2,5 @@
cmake-build-debug/*
build/*
tests/*.o
tests/ts
tests/ts
tests/*/*

View File

@@ -6,8 +6,8 @@ API int transmux(const char *path, const char *out_path, float *playable_duratio
//API int transcode(const char *path, const char *out_path, float *playable_duration);
API stream *extract_info(const char *path, const char *out_path, unsigned *stream_count, unsigned *track_count);
API stream *extract_infos(const char *path, const char *out_path, unsigned *stream_count, unsigned *track_count);
void destroy_stream(stream *s);
API void free_streams(stream *streamsPtr, int count);
API void free_streams(stream *streamsPtr, unsigned count);

View File

@@ -7,17 +7,13 @@
void destroy_stream(stream *s)
{
if (s->title)
free(s->title);
if (s->language)
free(s->language);
if (s->codec)
free(s->codec);
if (s->path)
free(s->path);
free(s->title);
free(s->language);
free(s->codec);
free(s->path);
}
void free_streams(stream *s, int count)
void free_streams(stream *s, unsigned count)
{
for (unsigned i = 0; i < count; i++)
destroy_stream(s + i);

View File

@@ -65,15 +65,16 @@ stream *extract_infos(const char *path, const char *out_path, unsigned *stream_c
{
AVFormatContext *ctx = NULL;
AVFormatContext **output_list;
stream *streams = calloc(ctx->nb_streams, sizeof(stream));
stream *streams;
if (!streams || open_input_context(&ctx, path) != 0)
if (open_input_context(&ctx, path) != 0)
return free(streams), NULL;
*stream_count = ctx->nb_streams;
*track_count = 0;
streams = calloc(ctx->nb_streams, sizeof(stream));
output_list = calloc(ctx->nb_streams, sizeof(AVFormatContext *));
if (output_list) {
if (output_list && streams) {
for (unsigned i = 0; i < *stream_count; i++) {
AVStream *stream = ctx->streams[i];
type stream_type = type_fromffmpeg(stream->codecpar->codec_type);
@@ -90,5 +91,6 @@ stream *extract_infos(const char *path, const char *out_path, unsigned *stream_c
avformat_close_input(&ctx);
if (!output_list)
return free(streams), NULL;
free(output_list);
return streams;
}

View File

@@ -26,6 +26,7 @@ int create_out_path(stream *subtitle, const char *out_path)
free(file_name);
return -2;
}
free(subtitle->path);
asprintf(&subtitle->path, "%s/%s.%s%s%s%s", folder_path,
file_name,
subtitle->language,

View File

@@ -7,28 +7,51 @@
#include "transcoder.h"
#include "stream.h"
const char *type_tostring(type t)
{
switch (t) {
case video:
return "Video";
case audio:
return "Audio";
case subtitle:
return "Subtitle";
case font:
return "Font";
default:
return "???";
}
}
int main(int argc, char **argv)
{
unsigned stream_count = 0;
unsigned subtitle_count = 0;
unsigned track_count = 0;
float playable_duration;
stream *streams;
if (argc == 3 && !strcmp(argv[1], "info")) {
streams = get_track_info(argv[2], &stream_count, &subtitle_count);
free_streams(streams, stream_count);
}
else if (argc == 3 && !strcmp(argv[1], "subextr")) {
streams = extract_subtitles(argv[2], ".", &stream_count, &subtitle_count);
streams = extract_infos(argv[2], ".", &stream_count, &track_count);
puts("Info extracted:");
for (unsigned i = 0; i < track_count; i++) {
printf("%8s: %6s - %3s (%5s), D%d, F%d at %s\n",
type_tostring(streams[i].type),
streams[i].title,
streams[i].language != NULL ? streams[i].language : "X",
streams[i].codec,
streams[i].is_default,
streams[i].is_forced,
streams[i].path);
}
free_streams(streams, stream_count);
return 0;
}
else if (argc == 4 && !strcmp(argv[1], "transmux"))
return (transmux(argv[2], argv[3], &playable_duration));
else
printf("\nUsage:\n\n\
%s INFO video_path - Test info prober\n\
%s subextr video_path - Test subtitle extractions\n\
%s transmux video_path m3u8_output_file - Test transmuxing\n", argv[0], argv[0], argv[0]);
return (0);
printf("Usage:\n\
%s info video_path - Test info prober\n\
%s transmux video_path m3u8_output_file - Test transmuxing\n", argv[0], argv[0]);
return 0;
}