aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Dato <nicolas.dato@gmail.com>2025-07-05 22:38:06 -0300
committerNicolas Dato <nicolas.dato@gmail.com>2025-07-05 22:38:06 -0300
commit3f3e5015bcdad476cd930306aa11f6c14e0cedf3 (patch)
treefc088e751f8879f53e40370c23836433510305a5
parent519a4bf995d1a8eb112feaa7fbee8b39657e15da (diff)
downloadlibtuberia-3f3e5015bcdad476cd930306aa11f6c14e0cedf3.tar.gz
improving example
-rw-r--r--example/decode_resize_encode.c57
1 files changed, 40 insertions, 17 deletions
diff --git a/example/decode_resize_encode.c b/example/decode_resize_encode.c
index d436317..efdf0b5 100644
--- a/example/decode_resize_encode.c
+++ b/example/decode_resize_encode.c
@@ -1,5 +1,3 @@
-#include <bits/pthreadtypes.h>
-#include <inttypes.h>
#include <libavcodec/avcodec.h>
#include <libavfilter/avfilter.h>
#include <libavformat/avformat.h>
@@ -10,19 +8,26 @@
#include <tuberia.h>
#include <unistd.h>
-const char *optstring = "hi:o:s:T";
+#define DEFAULT_WIDTH 720
+#define DEFAULT_HEIGHT 404
+#define DEFAULT_THREADS 0
+
+const char *optstring = "hi:o:s:St:T";
static void printhelp(const char *argv0)
{
printf(
- "%s -i <input> -o <output> -s <size> [-T]\n"
+ "%s -i <input> -o <output> [-s <size>] [-t <threads>] [-S] [-T]\n"
"%s -h\n"
"\n"
" -i input the input file\n"
" -o output the output file\n"
- " -s WIDTHxHEIGHT the size to scale the video\n"
+ " -s WIDTHxHEIGHT the size to scale the video, default %dx%d\n"
+ " -S don't show libtuberia stages status\n"
+ " -t threads number of threads to decode/encode, 0 = auto, default %d\n"
" -T don't use libtuberia\n"
- " -h print this help\n", argv0, argv0);
+ " -h print this help\n",
+ argv0, argv0, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_THREADS);
}
static void close_codec(AVCodecContext **ctx)
@@ -36,7 +41,7 @@ static void close_input(AVFormatContext **ctx)
}
static int open_input(AVFormatContext **avformatin, AVCodecContext **avcodecin,
- int *video_idx, const char *file)
+ int *video_idx, const char *file, int threads)
{
AVFormatContext *ctx = NULL;
AVCodec *codec = NULL;
@@ -68,6 +73,7 @@ static int open_input(AVFormatContext **avformatin, AVCodecContext **avcodecin,
*avcodecin = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(*avcodecin, ctx->streams[i]->codecpar);
(*avcodecin)->time_base = ctx->streams[i]->time_base;
+ (*avcodecin)->thread_count = threads;
if (avcodec_open2(*avcodecin, codec, NULL) < 0) {
fprintf(stderr, "Couldn't open codec %d (%s)\n",
ctx->streams[i]->codecpar->codec_id,
@@ -102,7 +108,7 @@ static void close_output(AVFormatContext **ctx)
static int open_output(AVFormatContext **avformatout,
AVCodecContext **avcodecout, struct SwsContext **sws, const char *file,
- const AVFormatContext *input, int width, int height)
+ const AVFormatContext *input, int width, int height, int threads)
{
AVFormatContext *ctx = NULL;
int i = 0;
@@ -138,6 +144,7 @@ static int open_output(AVFormatContext **avformatout,
*avcodecout = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(*avcodecout, stream->codecpar);
(*avcodecout)->time_base = stream->time_base;
+ (*avcodecout)->thread_count = threads;
if (avcodec_open2(*avcodecout, codec, NULL) < 0) {
fprintf(stderr, "Couldn't open codec %s (%d)\n",
avcodec_get_name(codecparin->codec_id),
@@ -406,7 +413,7 @@ static tube *build_tuberia(AVCodecContext *avcodecin, AVCodecContext *avcodecout
static void do_tuberia(AVFormatContext *avformatin, AVCodecContext *avcodecin,
AVFormatContext *avformatout, struct SwsContext *sws,
- AVCodecContext *avcodecout, int video_idx)
+ AVCodecContext *avcodecout, int video_idx, int show_stats)
{
tube *ctx = NULL;
AVPacket *packet = NULL;
@@ -439,7 +446,9 @@ static void do_tuberia(AVFormatContext *avformatin, AVCodecContext *avcodecin,
fprintf(stderr, "Couldn't inject packet to tuberia after 15s\n");
break;
}
- print_tube_stats(ctx);
+ if (show_stats) {
+ print_tube_stats(ctx);
+ }
} else {
tube_inject_at(ctx, 3, 15000, packet);
}
@@ -500,8 +509,10 @@ int main(int argc, char **argv)
int c = 0;
char *input = NULL;
char *output = NULL;
- int width = 720;
- int height = 404;
+ int width = DEFAULT_WIDTH;
+ int height = DEFAULT_HEIGHT;
+ int threads = DEFAULT_THREADS;
+ int show_stats = 1;
int notuberia = 0;
AVFormatContext *avformatin = NULL;
AVFormatContext *avformatout = NULL;
@@ -529,12 +540,22 @@ int main(int argc, char **argv)
return -1;
}
break;
+ case 'S':
+ show_stats = 0;
+ break;
+ case 't':
+ if (sscanf(optarg, "%d", &threads) != 1) {
+ fprintf(stderr, "Threads parameter must be a number, instead it is %s\n",
+ optarg);
+ return -1;
+ }
+ break;
case 'T':
notuberia = 1;
break;
case '?':
default:
- fprintf(stderr, "Incorrect arguments. Run %s -h\n", argv[0]);
+ fprintf(stderr, "Incorrect arguments.\nRun %s -h\n", argv[0]);
return -1;
}
}
@@ -543,20 +564,22 @@ int main(int argc, char **argv)
return -1;
}
- if (open_input(&avformatin, &avcodecin, &video_idx, input) < 0) {
+ if (open_input(&avformatin, &avcodecin, &video_idx, input, threads) < 0) {
fprintf(stderr, "Couldn't open input %s\n", input);
return -1;
}
if (open_output(&avformatout, &avcodecout, &sws, output, avformatin, width,
- height) < 0) {
+ height, threads) < 0) {
fprintf(stderr, "Couldn't initiate output %s\n", output);
return -1;
}
if (notuberia) {
- do_no_tuberia(avformatin, avcodecin, avformatout, sws, avcodecout, video_idx);
+ do_no_tuberia(avformatin, avcodecin, avformatout, sws, avcodecout,
+ video_idx);
} else {
- do_tuberia(avformatin, avcodecin, avformatout, sws, avcodecout, video_idx);
+ do_tuberia(avformatin, avcodecin, avformatout, sws, avcodecout,
+ video_idx, show_stats);
}
close_input(&avformatin);