From 519a4bf995d1a8eb112feaa7fbee8b39657e15da Mon Sep 17 00:00:00 2001 From: Nicolas Dato Date: Sat, 5 Jul 2025 20:15:31 -0300 Subject: renaming example --- .gitignore | 2 +- example/Makefile.am | 10 +- example/Makefile.in | 60 ++--- example/decode_filter_encode.c | 570 ----------------------------------------- example/decode_resize_encode.c | 570 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 606 insertions(+), 606 deletions(-) delete mode 100644 example/decode_filter_encode.c create mode 100644 example/decode_resize_encode.c diff --git a/.gitignore b/.gitignore index 4f04421..b10a3cb 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,4 @@ test/*.log test/*.trs test/test_itc test/test_tuberia -decode_filter_encode +decode_resize_encode diff --git a/example/Makefile.am b/example/Makefile.am index f7bee12..5043e21 100644 --- a/example/Makefile.am +++ b/example/Makefile.am @@ -1,7 +1,7 @@ -noinst_PROGRAMS = decode_filter_encode +noinst_PROGRAMS = decode_resize_encode -decode_filter_encode_CFLAGS = $(AM_CFLAGS) -I../src -decode_filter_encode_LDADD = ../src/libtuberia.a -decode_filter_encode_LDFLAGS = -lpthread -lavcodec -lavformat -lavutil -lswscale -decode_filter_encode_SOURCES = decode_filter_encode.c +decode_resize_encode_CFLAGS = $(AM_CFLAGS) -I../src +decode_resize_encode_LDADD = ../src/libtuberia.a +decode_resize_encode_LDFLAGS = -lpthread -lavcodec -lavformat -lavutil -lswscale +decode_resize_encode_SOURCES = decode_resize_encode.c diff --git a/example/Makefile.in b/example/Makefile.in index 6cdb8cf..3386dc7 100644 --- a/example/Makefile.in +++ b/example/Makefile.in @@ -86,7 +86,7 @@ POST_INSTALL = : NORMAL_UNINSTALL = : PRE_UNINSTALL = : POST_UNINSTALL = : -noinst_PROGRAMS = decode_filter_encode$(EXEEXT) +noinst_PROGRAMS = decode_resize_encode$(EXEEXT) subdir = example ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = $(top_srcdir)/configure.ac @@ -98,12 +98,12 @@ CONFIG_HEADER = $(top_builddir)/config.h CONFIG_CLEAN_FILES = CONFIG_CLEAN_VPATH_FILES = PROGRAMS = $(noinst_PROGRAMS) -am_decode_filter_encode_OBJECTS = \ - decode_filter_encode-decode_filter_encode.$(OBJEXT) -decode_filter_encode_OBJECTS = $(am_decode_filter_encode_OBJECTS) -decode_filter_encode_DEPENDENCIES = ../src/libtuberia.a -decode_filter_encode_LINK = $(CCLD) $(decode_filter_encode_CFLAGS) \ - $(CFLAGS) $(decode_filter_encode_LDFLAGS) $(LDFLAGS) -o $@ +am_decode_resize_encode_OBJECTS = \ + decode_resize_encode-decode_resize_encode.$(OBJEXT) +decode_resize_encode_OBJECTS = $(am_decode_resize_encode_OBJECTS) +decode_resize_encode_DEPENDENCIES = ../src/libtuberia.a +decode_resize_encode_LINK = $(CCLD) $(decode_resize_encode_CFLAGS) \ + $(CFLAGS) $(decode_resize_encode_LDFLAGS) $(LDFLAGS) -o $@ AM_V_P = $(am__v_P_@AM_V@) am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) am__v_P_0 = false @@ -120,7 +120,7 @@ DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) depcomp = $(SHELL) $(top_srcdir)/depcomp am__maybe_remake_depfiles = depfiles am__depfiles_remade = \ - ./$(DEPDIR)/decode_filter_encode-decode_filter_encode.Po + ./$(DEPDIR)/decode_resize_encode-decode_resize_encode.Po am__mv = mv -f AM_V_lt = $(am__v_lt_@AM_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) @@ -138,8 +138,8 @@ AM_V_CCLD = $(am__v_CCLD_@AM_V@) am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) am__v_CCLD_0 = @echo " CCLD " $@; am__v_CCLD_1 = -SOURCES = $(decode_filter_encode_SOURCES) -DIST_SOURCES = $(decode_filter_encode_SOURCES) +SOURCES = $(decode_resize_encode_SOURCES) +DIST_SOURCES = $(decode_resize_encode_SOURCES) am__can_run_installinfo = \ case $$AM_UPDATE_INFO_DIR in \ n|no|NO) false;; \ @@ -258,10 +258,10 @@ target_alias = @target_alias@ top_build_prefix = @top_build_prefix@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ -decode_filter_encode_CFLAGS = $(AM_CFLAGS) -I../src -decode_filter_encode_LDADD = ../src/libtuberia.a -decode_filter_encode_LDFLAGS = -lpthread -lavcodec -lavformat -lavutil -lswscale -decode_filter_encode_SOURCES = decode_filter_encode.c +decode_resize_encode_CFLAGS = $(AM_CFLAGS) -I../src +decode_resize_encode_LDADD = ../src/libtuberia.a +decode_resize_encode_LDFLAGS = -lpthread -lavcodec -lavformat -lavutil -lswscale +decode_resize_encode_SOURCES = decode_resize_encode.c all: all-am .SUFFIXES: @@ -299,9 +299,9 @@ $(am__aclocal_m4_deps): clean-noinstPROGRAMS: -test -z "$(noinst_PROGRAMS)" || rm -f $(noinst_PROGRAMS) -decode_filter_encode$(EXEEXT): $(decode_filter_encode_OBJECTS) $(decode_filter_encode_DEPENDENCIES) $(EXTRA_decode_filter_encode_DEPENDENCIES) - @rm -f decode_filter_encode$(EXEEXT) - $(AM_V_CCLD)$(decode_filter_encode_LINK) $(decode_filter_encode_OBJECTS) $(decode_filter_encode_LDADD) $(LIBS) +decode_resize_encode$(EXEEXT): $(decode_resize_encode_OBJECTS) $(decode_resize_encode_DEPENDENCIES) $(EXTRA_decode_resize_encode_DEPENDENCIES) + @rm -f decode_resize_encode$(EXEEXT) + $(AM_V_CCLD)$(decode_resize_encode_LINK) $(decode_resize_encode_OBJECTS) $(decode_resize_encode_LDADD) $(LIBS) mostlyclean-compile: -rm -f *.$(OBJEXT) @@ -309,7 +309,7 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/decode_filter_encode-decode_filter_encode.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/decode_resize_encode-decode_resize_encode.Po@am__quote@ # am--include-marker $(am__depfiles_remade): @$(MKDIR_P) $(@D) @@ -331,19 +331,19 @@ am--depfiles: $(am__depfiles_remade) @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ `$(CYGPATH_W) '$<'` -decode_filter_encode-decode_filter_encode.o: decode_filter_encode.c -@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(decode_filter_encode_CFLAGS) $(CFLAGS) -MT decode_filter_encode-decode_filter_encode.o -MD -MP -MF $(DEPDIR)/decode_filter_encode-decode_filter_encode.Tpo -c -o decode_filter_encode-decode_filter_encode.o `test -f 'decode_filter_encode.c' || echo '$(srcdir)/'`decode_filter_encode.c -@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/decode_filter_encode-decode_filter_encode.Tpo $(DEPDIR)/decode_filter_encode-decode_filter_encode.Po -@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='decode_filter_encode.c' object='decode_filter_encode-decode_filter_encode.o' libtool=no @AMDEPBACKSLASH@ +decode_resize_encode-decode_resize_encode.o: decode_resize_encode.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(decode_resize_encode_CFLAGS) $(CFLAGS) -MT decode_resize_encode-decode_resize_encode.o -MD -MP -MF $(DEPDIR)/decode_resize_encode-decode_resize_encode.Tpo -c -o decode_resize_encode-decode_resize_encode.o `test -f 'decode_resize_encode.c' || echo '$(srcdir)/'`decode_resize_encode.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/decode_resize_encode-decode_resize_encode.Tpo $(DEPDIR)/decode_resize_encode-decode_resize_encode.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='decode_resize_encode.c' object='decode_resize_encode-decode_resize_encode.o' libtool=no @AMDEPBACKSLASH@ @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ -@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(decode_filter_encode_CFLAGS) $(CFLAGS) -c -o decode_filter_encode-decode_filter_encode.o `test -f 'decode_filter_encode.c' || echo '$(srcdir)/'`decode_filter_encode.c +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(decode_resize_encode_CFLAGS) $(CFLAGS) -c -o decode_resize_encode-decode_resize_encode.o `test -f 'decode_resize_encode.c' || echo '$(srcdir)/'`decode_resize_encode.c -decode_filter_encode-decode_filter_encode.obj: decode_filter_encode.c -@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(decode_filter_encode_CFLAGS) $(CFLAGS) -MT decode_filter_encode-decode_filter_encode.obj -MD -MP -MF $(DEPDIR)/decode_filter_encode-decode_filter_encode.Tpo -c -o decode_filter_encode-decode_filter_encode.obj `if test -f 'decode_filter_encode.c'; then $(CYGPATH_W) 'decode_filter_encode.c'; else $(CYGPATH_W) '$(srcdir)/decode_filter_encode.c'; fi` -@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/decode_filter_encode-decode_filter_encode.Tpo $(DEPDIR)/decode_filter_encode-decode_filter_encode.Po -@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='decode_filter_encode.c' object='decode_filter_encode-decode_filter_encode.obj' libtool=no @AMDEPBACKSLASH@ +decode_resize_encode-decode_resize_encode.obj: decode_resize_encode.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(decode_resize_encode_CFLAGS) $(CFLAGS) -MT decode_resize_encode-decode_resize_encode.obj -MD -MP -MF $(DEPDIR)/decode_resize_encode-decode_resize_encode.Tpo -c -o decode_resize_encode-decode_resize_encode.obj `if test -f 'decode_resize_encode.c'; then $(CYGPATH_W) 'decode_resize_encode.c'; else $(CYGPATH_W) '$(srcdir)/decode_resize_encode.c'; fi` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/decode_resize_encode-decode_resize_encode.Tpo $(DEPDIR)/decode_resize_encode-decode_resize_encode.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='decode_resize_encode.c' object='decode_resize_encode-decode_resize_encode.obj' libtool=no @AMDEPBACKSLASH@ @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ -@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(decode_filter_encode_CFLAGS) $(CFLAGS) -c -o decode_filter_encode-decode_filter_encode.obj `if test -f 'decode_filter_encode.c'; then $(CYGPATH_W) 'decode_filter_encode.c'; else $(CYGPATH_W) '$(srcdir)/decode_filter_encode.c'; fi` +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(decode_resize_encode_CFLAGS) $(CFLAGS) -c -o decode_resize_encode-decode_resize_encode.obj `if test -f 'decode_resize_encode.c'; then $(CYGPATH_W) 'decode_resize_encode.c'; else $(CYGPATH_W) '$(srcdir)/decode_resize_encode.c'; fi` ID: $(am__tagged_files) $(am__define_uniq_tagged_files); mkid -fID $$unique @@ -469,7 +469,7 @@ clean: clean-am clean-am: clean-generic clean-noinstPROGRAMS mostlyclean-am distclean: distclean-am - -rm -f ./$(DEPDIR)/decode_filter_encode-decode_filter_encode.Po + -rm -f ./$(DEPDIR)/decode_resize_encode-decode_resize_encode.Po -rm -f Makefile distclean-am: clean-am distclean-compile distclean-generic \ distclean-tags @@ -515,7 +515,7 @@ install-ps-am: installcheck-am: maintainer-clean: maintainer-clean-am - -rm -f ./$(DEPDIR)/decode_filter_encode-decode_filter_encode.Po + -rm -f ./$(DEPDIR)/decode_resize_encode-decode_resize_encode.Po -rm -f Makefile maintainer-clean-am: distclean-am maintainer-clean-generic diff --git a/example/decode_filter_encode.c b/example/decode_filter_encode.c deleted file mode 100644 index d436317..0000000 --- a/example/decode_filter_encode.c +++ /dev/null @@ -1,570 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -const char *optstring = "hi:o:s:T"; - -static void printhelp(const char *argv0) -{ - printf( - "%s -i -o -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" - " -T don't use libtuberia\n" - " -h print this help\n", argv0, argv0); -} - -static void close_codec(AVCodecContext **ctx) -{ - avcodec_free_context(ctx); -} - -static void close_input(AVFormatContext **ctx) -{ - avformat_close_input(ctx); -} - -static int open_input(AVFormatContext **avformatin, AVCodecContext **avcodecin, - int *video_idx, const char *file) -{ - AVFormatContext *ctx = NULL; - AVCodec *codec = NULL; - int i = 0; - - printf("Opening input file %s\n", file); - if (avformat_open_input(&ctx, file, NULL, NULL)) { - return -1; - } - printf("Parsing input file %s\n", file); - if (avformat_find_stream_info(ctx, NULL) < 0) { - close_input(&ctx); - return -1; - } - - for (i = 0; i < (int)ctx->nb_streams; i++) { - if (ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { - *video_idx = i; - codec = avcodec_find_decoder(ctx->streams[i]->codecpar->codec_id); - if (codec == NULL) { - fprintf(stderr, "Couldn't find decoder for codec id %d (%s)\n", - ctx->streams[i]->codecpar->codec_id, - avcodec_get_name(ctx->streams[i]->codecpar->codec_id)); - close_input(&ctx); - return -1; - } - printf("Opening input codec %s (%d)\n", avcodec_get_name(codec->id), - codec->id); - *avcodecin = avcodec_alloc_context3(codec); - avcodec_parameters_to_context(*avcodecin, ctx->streams[i]->codecpar); - (*avcodecin)->time_base = ctx->streams[i]->time_base; - if (avcodec_open2(*avcodecin, codec, NULL) < 0) { - fprintf(stderr, "Couldn't open codec %d (%s)\n", - ctx->streams[i]->codecpar->codec_id, - avcodec_get_name(ctx->streams[i]->codecpar->codec_id)); - close_input(&ctx); - close_codec(avcodecin); - return -1; - } - break; - } - } - - *avformatin = ctx; - - return 0; -} - -static void close_sws(struct SwsContext **ctx) -{ - sws_freeContext(*ctx); - *ctx = NULL; -} - -static void close_output(AVFormatContext **ctx) -{ - av_write_trailer(*ctx); - avformat_flush(*ctx); - avio_closep(&(*ctx)->pb); - avformat_free_context(*ctx); - *ctx = NULL; -} - -static int open_output(AVFormatContext **avformatout, - AVCodecContext **avcodecout, struct SwsContext **sws, const char *file, - const AVFormatContext *input, int width, int height) -{ - AVFormatContext *ctx = NULL; - int i = 0; - const AVCodec *codec = NULL; - const AVCodecParameters *codecparin = NULL; - AVStream *stream = NULL; - - printf("Opening output file %s\n", file); - if (avformat_alloc_output_context2(&ctx, NULL, NULL, file) < 0 - || ctx == NULL) { - return -1; - } - - for (i = 0; i < (int)input->nb_streams; i++) { - codecparin = input->streams[i]->codecpar; - codec = avcodec_find_encoder(codecparin->codec_id); - if (codec == NULL) { - fprintf(stderr, "Couldn't find encoder for codec id %d (%s)\n", - codecparin->codec_id, avcodec_get_name(codecparin->codec_id)); - close_output(&ctx); - return -1; - } - printf("Opening output codec %s (%d)\n", avcodec_get_name(codec->id), - codec->id); - stream = avformat_new_stream(ctx, codec); - avcodec_parameters_copy(stream->codecpar, codecparin); - stream->time_base = input->streams[i]->time_base; - if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { - stream->codecpar->width = width; - stream->codecpar->height = height; - } - if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { - *avcodecout = avcodec_alloc_context3(codec); - avcodec_parameters_to_context(*avcodecout, stream->codecpar); - (*avcodecout)->time_base = stream->time_base; - if (avcodec_open2(*avcodecout, codec, NULL) < 0) { - fprintf(stderr, "Couldn't open codec %s (%d)\n", - avcodec_get_name(codecparin->codec_id), - codecparin->codec_id); - close_output(&ctx); - close_codec(avcodecout); - return -1; - } - printf("Opening resize context %dx%d %s -> %dx%d %s\n", - codecparin->width, codecparin->height, - av_get_pix_fmt_name((enum AVPixelFormat)codecparin->format), - stream->codecpar->width, stream->codecpar->height, - av_get_pix_fmt_name((enum AVPixelFormat)stream->codecpar->format)); - *sws = sws_getContext(codecparin->width, codecparin->height, - (enum AVPixelFormat)codecparin->format, - stream->codecpar->width, stream->codecpar->height, - (enum AVPixelFormat)stream->codecpar->format, - SWS_GAUSS, NULL, NULL, NULL); - if (*sws == NULL) { - fprintf(stderr, "Couldn't open resize context %dx%d %s -> %dx%d %s\n", - codecparin->width, codecparin->height, - av_get_pix_fmt_name((enum AVPixelFormat)codecparin->format), - stream->codecpar->width, stream->codecpar->height, - av_get_pix_fmt_name((enum AVPixelFormat)stream->codecpar->format)); - close_output(&ctx); - close_codec(avcodecout); - close_sws(sws); - } - } - } - - if (!(ctx->flags & AVFMT_NOFILE)) { - int ret = avio_open(&ctx->pb, file, AVIO_FLAG_WRITE); - if (ret < 0) { - fprintf(stderr, "Couldn't create output file %s %s (%d)\n", file, - av_err2str(ret), ret); - close_output(&ctx); - close_codec(avcodecout); - close_sws(sws); - return -1; - } - } - if (avformat_write_header(ctx, NULL) < 0) { - fprintf(stderr, "Couldn't write header to output file %s\n", file); - } - *avformatout = ctx; - return 0; -} - -static int decode_packet(AVCodecContext *ctx, AVPacket *packet, AVFrame *frame) -{ - int ret = 0; - - ret = avcodec_send_packet(ctx, packet); - if (!ret) { - ret = avcodec_receive_frame(ctx, frame); - if (!ret) { - frame->opaque = av_malloc(sizeof(int)); - *(int *)frame->opaque = packet->stream_index; - } - } else { - fprintf(stderr, "Couldn't decode video packet %s (%d)\n", - av_err2str(ret), ret); - } - av_packet_unref(packet); - - return ret; -} - -static int scale_frame(struct SwsContext *sws, AVFrame *frame, AVFrame *scaled, - int width, int height, enum AVPixelFormat pix_fmt) -{ - av_frame_copy_props(scaled, frame); - scaled->width = width; - scaled->height = height; - scaled->format = pix_fmt; - av_frame_get_buffer(scaled, 0); - sws_scale(sws, (const uint8_t * const *)frame->data, frame->linesize, - 0, frame->height, scaled->data, scaled->linesize); - scaled->opaque = frame->opaque; - frame->opaque = NULL; - av_frame_unref(frame); - - return 0; -} - -static int encode_frame(AVCodecContext *ctx, AVFrame *frame, AVPacket *packet) -{ - int ret = 0; - - ret = avcodec_send_frame(ctx, frame); - if (!ret) { - ret = avcodec_receive_packet(ctx, packet); - if (!ret && frame->opaque != NULL) { - packet->stream_index = *(int *)frame->opaque; - } - } else { - fprintf(stderr, "Couldn't encode video packet %s (%d)\n", - av_err2str(ret), ret); - } - av_freep(&frame->opaque); - av_frame_unref(frame); - - return ret; -} - -static void flush_video(AVFormatContext *avformatin, AVCodecContext *avcodecin, - struct SwsContext *sws, AVCodecContext *avcodecout, - AVFormatContext *avformatout, int idx) -{ - AVFrame *frame = NULL; - AVPacket *packet = NULL; - AVFrame *scaled = NULL; - - frame = av_frame_alloc(); - packet = av_packet_alloc(); - scaled = av_frame_alloc(); - avcodec_send_packet(avcodecin, NULL); - while (!avcodec_receive_frame(avcodecin, frame)) { - scale_frame(sws, frame, scaled, avcodecout->width, - avcodecout->height, avcodecout->pix_fmt); - avcodec_send_frame(avcodecout, scaled); - if (!encode_frame(avcodecout, scaled, packet)) { - av_packet_rescale_ts(packet, avformatin->streams[idx]->time_base, - avformatout->streams[idx]->time_base); - packet->stream_index = idx; - av_interleaved_write_frame(avformatout, packet); - } - } - avcodec_send_frame(avcodecout, NULL); - while (!avcodec_receive_packet(avcodecout, packet)) { - av_packet_rescale_ts(packet, avformatin->streams[idx]->time_base, - avformatout->streams[idx]->time_base); - packet->stream_index = idx; - av_interleaved_write_frame(avformatout, packet); - } - av_frame_free(&frame); - av_packet_free(&packet); - av_frame_free(&scaled); -} - -void tube_packet_free(void *element) -{ - av_packet_free((AVPacket **)&element); -} - -void tube_frame_free(void *element) -{ - av_frame_free((AVFrame **)&element); -} - -static void *tube_decode(void *element, void *opaque) -{ - AVPacket *packet = element; - AVCodecContext *avcodecin = opaque; - AVFrame *frame = av_frame_alloc(); - - if (decode_packet(avcodecin, packet, frame)) { - av_frame_free(&frame); - } - av_packet_free(&packet); - - return frame; -} - -struct tube_resize_opaque { - struct SwsContext *sws; - int width; - int height; - enum AVPixelFormat pix_fmt; -}; - -static void *tube_resize(void *element, void *opaque) -{ - AVFrame *frame = element; - struct tube_resize_opaque *o = opaque; - AVFrame *scaled = av_frame_alloc(); - - scale_frame(o->sws, frame, scaled, o->width, o->height, o->pix_fmt); - av_frame_free(&frame); - - return scaled; -} - -static void *tube_encode(void *element, void *opaque) -{ - AVFrame *frame = element; - AVCodecContext *avcodecout = opaque; - AVPacket *packet = av_packet_alloc(); - - if (encode_frame(avcodecout, frame, packet)) { - av_packet_free(&packet); - } - av_frame_free(&frame); - - return packet; -} - -struct tube_mux_opaque { - AVFormatContext *avformatin; - AVFormatContext *avformatout; -}; - -void tube_mux(void *element, void *opaque) -{ - AVPacket *packet = element; - struct tube_mux_opaque *o = opaque; - AVPacket packet2mux; - int idx = packet->stream_index; - - av_packet_rescale_ts(packet, o->avformatin->streams[idx]->time_base, - o->avformatout->streams[idx]->time_base); - av_packet_ref(&packet2mux, packet); - av_interleaved_write_frame(o->avformatout, &packet2mux); - av_packet_free(&packet); -} - -static void print_tube_stats(tube *ctx) -{ - int queued_read = tube_get_queued(ctx, 0); - int slots_read = tube_get_slots(ctx, 0); - int queued_decode = tube_get_queued(ctx, 1); - int slots_decode = tube_get_slots(ctx, 1); - int queued_resize = tube_get_queued(ctx, 2); - int slots_resize = tube_get_slots(ctx, 2); - int queued_encode = tube_get_queued(ctx, 3); - int slots_encode = tube_get_slots(ctx, 3); - - printf("read -> [%d/%d] -> deco -> [%d/%d] -> resize -> [%d/%d] -> encode -> [%d/%d] -> write\n", - queued_read, slots_read, queued_decode, slots_decode, - queued_resize, slots_resize, queued_encode, slots_encode); -} - -static tube *build_tuberia(AVCodecContext *avcodecin, AVCodecContext *avcodecout, - struct tube_resize_opaque *tube_resize_opaque, - struct tube_mux_opaque *tube_mux_opaque) -{ - tube *ctx = NULL; - tube_source *source = NULL; - tube_stage *decode = NULL; - tube_stage *resize = NULL; - tube_stage *encode = NULL; - - source = tube_source_alloc(10, NULL, NULL, tube_packet_free); - decode = tube_stage_alloc(10, tube_decode, avcodecin, tube_frame_free); - resize = tube_stage_alloc(10, tube_resize, tube_resize_opaque, tube_frame_free); - encode = tube_stage_alloc(10, tube_encode, avcodecout, tube_packet_free); - - if (tube_stage_append(source, decode)) { - fprintf(stderr, "Couldn't append decode stage to tuberia\n"); - return NULL; - } - if (tube_stage_append(source, resize)) { - fprintf(stderr, "Couldn't append resize stage to tuberia\n"); - return NULL; - } - if (tube_stage_append(source, encode)) { - fprintf(stderr, "Couldn't append encode stage to tuberia\n"); - return NULL; - } - ctx = tube_alloc(source, tube_mux, tube_mux_opaque); - tube_source_and_stages_free(&source); - - return ctx; -} - -static void do_tuberia(AVFormatContext *avformatin, AVCodecContext *avcodecin, - AVFormatContext *avformatout, struct SwsContext *sws, - AVCodecContext *avcodecout, int video_idx) -{ - tube *ctx = NULL; - AVPacket *packet = NULL; - int idx = 0; - struct tube_resize_opaque tube_resize_opaque = { - .sws = sws, - .width = avcodecout->width, - .height = avcodecout->height, - .pix_fmt = avcodecout->pix_fmt - }; - struct tube_mux_opaque tube_mux_opaque = { - .avformatin = avformatin, - .avformatout = avformatout, - }; - - ctx = build_tuberia(avcodecin, avcodecout, &tube_resize_opaque, - &tube_mux_opaque); - if (ctx == NULL) { - fprintf(stderr, "Couldn't create tuberia\n"); - return; - } - - printf("Starting to transcode with libtuberia\n"); - tube_start(ctx); - packet = av_packet_alloc(); - while (!av_read_frame(avformatin, packet)) { - idx = packet->stream_index; - if (idx == video_idx) { - if (tube_inject(ctx, 15000, packet)) { - fprintf(stderr, "Couldn't inject packet to tuberia after 15s\n"); - break; - } - print_tube_stats(ctx); - } else { - tube_inject_at(ctx, 3, 15000, packet); - } - packet = av_packet_alloc(); - } - tube_stop_and_wait_empty(ctx); - if (video_idx >= 0) { - flush_video(avformatin, avcodecin, sws, avcodecout, avformatout, video_idx); - } - printf("Transcode with libtuberia finished\n"); - - av_packet_free(&packet); - tube_free(&ctx); -} - -static void do_no_tuberia(AVFormatContext *avformatin, AVCodecContext *avcodecin, - AVFormatContext *avformatout, struct SwsContext *sws, - AVCodecContext *avcodecout, int video_idx) -{ - AVPacket *packet = NULL; - AVFrame *frame = NULL; - AVFrame *scaled = NULL; - int idx = 0; - - packet = av_packet_alloc(); - frame = av_frame_alloc(); - scaled = av_frame_alloc(); - - printf("Starting to transcode without libtuberia\n"); - while (!av_read_frame(avformatin, packet)) { - idx = packet->stream_index; - if (idx == video_idx) { - if (decode_packet(avcodecin, packet, frame)) { - continue; - } - scale_frame(sws, frame, scaled, avcodecout->width, - avcodecout->height, avcodecout->pix_fmt); - if (encode_frame(avcodecout, scaled, packet)) { - continue; - } - } - av_packet_rescale_ts(packet, avformatin->streams[idx]->time_base, - avformatout->streams[idx]->time_base); - av_interleaved_write_frame(avformatout, packet); - } - if (video_idx >= 0) { - flush_video(avformatin, avcodecin, sws, avcodecout, avformatout, video_idx); - } - printf("Transcode without libtuberia finished\n"); - - av_packet_free(&packet); - av_frame_free(&frame); - av_frame_free(&scaled); -} - -int main(int argc, char **argv) -{ - int c = 0; - char *input = NULL; - char *output = NULL; - int width = 720; - int height = 404; - int notuberia = 0; - AVFormatContext *avformatin = NULL; - AVFormatContext *avformatout = NULL; - AVCodecContext *avcodecin = NULL; - AVCodecContext *avcodecout = NULL; - struct SwsContext *sws = NULL; - int video_idx = -1; - - while ((c = getopt(argc, argv, optstring)) >= 0) { - switch (c) { - case 'h': - printhelp(argv[0]); - return 0; - break; - case 'i': - input = optarg; - break; - case 'o': - output = optarg; - break; - case 's': - if (sscanf(optarg, "%dx%d", &width, &height) != 2) { - fprintf(stderr, "Size parameter must be WxH, 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]); - return -1; - } - } - if (input == NULL || !input[0]) { - printhelp(argv[0]); - return -1; - } - - if (open_input(&avformatin, &avcodecin, &video_idx, input) < 0) { - fprintf(stderr, "Couldn't open input %s\n", input); - return -1; - } - if (open_output(&avformatout, &avcodecout, &sws, output, avformatin, width, - height) < 0) { - fprintf(stderr, "Couldn't initiate output %s\n", output); - return -1; - } - - if (notuberia) { - do_no_tuberia(avformatin, avcodecin, avformatout, sws, avcodecout, video_idx); - } else { - do_tuberia(avformatin, avcodecin, avformatout, sws, avcodecout, video_idx); - } - - close_input(&avformatin); - close_output(&avformatout); - close_codec(&avcodecin); - close_codec(&avcodecout); - close_sws(&sws); - - return 0; -} - diff --git a/example/decode_resize_encode.c b/example/decode_resize_encode.c new file mode 100644 index 0000000..d436317 --- /dev/null +++ b/example/decode_resize_encode.c @@ -0,0 +1,570 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +const char *optstring = "hi:o:s:T"; + +static void printhelp(const char *argv0) +{ + printf( + "%s -i -o -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" + " -T don't use libtuberia\n" + " -h print this help\n", argv0, argv0); +} + +static void close_codec(AVCodecContext **ctx) +{ + avcodec_free_context(ctx); +} + +static void close_input(AVFormatContext **ctx) +{ + avformat_close_input(ctx); +} + +static int open_input(AVFormatContext **avformatin, AVCodecContext **avcodecin, + int *video_idx, const char *file) +{ + AVFormatContext *ctx = NULL; + AVCodec *codec = NULL; + int i = 0; + + printf("Opening input file %s\n", file); + if (avformat_open_input(&ctx, file, NULL, NULL)) { + return -1; + } + printf("Parsing input file %s\n", file); + if (avformat_find_stream_info(ctx, NULL) < 0) { + close_input(&ctx); + return -1; + } + + for (i = 0; i < (int)ctx->nb_streams; i++) { + if (ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { + *video_idx = i; + codec = avcodec_find_decoder(ctx->streams[i]->codecpar->codec_id); + if (codec == NULL) { + fprintf(stderr, "Couldn't find decoder for codec id %d (%s)\n", + ctx->streams[i]->codecpar->codec_id, + avcodec_get_name(ctx->streams[i]->codecpar->codec_id)); + close_input(&ctx); + return -1; + } + printf("Opening input codec %s (%d)\n", avcodec_get_name(codec->id), + codec->id); + *avcodecin = avcodec_alloc_context3(codec); + avcodec_parameters_to_context(*avcodecin, ctx->streams[i]->codecpar); + (*avcodecin)->time_base = ctx->streams[i]->time_base; + if (avcodec_open2(*avcodecin, codec, NULL) < 0) { + fprintf(stderr, "Couldn't open codec %d (%s)\n", + ctx->streams[i]->codecpar->codec_id, + avcodec_get_name(ctx->streams[i]->codecpar->codec_id)); + close_input(&ctx); + close_codec(avcodecin); + return -1; + } + break; + } + } + + *avformatin = ctx; + + return 0; +} + +static void close_sws(struct SwsContext **ctx) +{ + sws_freeContext(*ctx); + *ctx = NULL; +} + +static void close_output(AVFormatContext **ctx) +{ + av_write_trailer(*ctx); + avformat_flush(*ctx); + avio_closep(&(*ctx)->pb); + avformat_free_context(*ctx); + *ctx = NULL; +} + +static int open_output(AVFormatContext **avformatout, + AVCodecContext **avcodecout, struct SwsContext **sws, const char *file, + const AVFormatContext *input, int width, int height) +{ + AVFormatContext *ctx = NULL; + int i = 0; + const AVCodec *codec = NULL; + const AVCodecParameters *codecparin = NULL; + AVStream *stream = NULL; + + printf("Opening output file %s\n", file); + if (avformat_alloc_output_context2(&ctx, NULL, NULL, file) < 0 + || ctx == NULL) { + return -1; + } + + for (i = 0; i < (int)input->nb_streams; i++) { + codecparin = input->streams[i]->codecpar; + codec = avcodec_find_encoder(codecparin->codec_id); + if (codec == NULL) { + fprintf(stderr, "Couldn't find encoder for codec id %d (%s)\n", + codecparin->codec_id, avcodec_get_name(codecparin->codec_id)); + close_output(&ctx); + return -1; + } + printf("Opening output codec %s (%d)\n", avcodec_get_name(codec->id), + codec->id); + stream = avformat_new_stream(ctx, codec); + avcodec_parameters_copy(stream->codecpar, codecparin); + stream->time_base = input->streams[i]->time_base; + if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { + stream->codecpar->width = width; + stream->codecpar->height = height; + } + if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { + *avcodecout = avcodec_alloc_context3(codec); + avcodec_parameters_to_context(*avcodecout, stream->codecpar); + (*avcodecout)->time_base = stream->time_base; + if (avcodec_open2(*avcodecout, codec, NULL) < 0) { + fprintf(stderr, "Couldn't open codec %s (%d)\n", + avcodec_get_name(codecparin->codec_id), + codecparin->codec_id); + close_output(&ctx); + close_codec(avcodecout); + return -1; + } + printf("Opening resize context %dx%d %s -> %dx%d %s\n", + codecparin->width, codecparin->height, + av_get_pix_fmt_name((enum AVPixelFormat)codecparin->format), + stream->codecpar->width, stream->codecpar->height, + av_get_pix_fmt_name((enum AVPixelFormat)stream->codecpar->format)); + *sws = sws_getContext(codecparin->width, codecparin->height, + (enum AVPixelFormat)codecparin->format, + stream->codecpar->width, stream->codecpar->height, + (enum AVPixelFormat)stream->codecpar->format, + SWS_GAUSS, NULL, NULL, NULL); + if (*sws == NULL) { + fprintf(stderr, "Couldn't open resize context %dx%d %s -> %dx%d %s\n", + codecparin->width, codecparin->height, + av_get_pix_fmt_name((enum AVPixelFormat)codecparin->format), + stream->codecpar->width, stream->codecpar->height, + av_get_pix_fmt_name((enum AVPixelFormat)stream->codecpar->format)); + close_output(&ctx); + close_codec(avcodecout); + close_sws(sws); + } + } + } + + if (!(ctx->flags & AVFMT_NOFILE)) { + int ret = avio_open(&ctx->pb, file, AVIO_FLAG_WRITE); + if (ret < 0) { + fprintf(stderr, "Couldn't create output file %s %s (%d)\n", file, + av_err2str(ret), ret); + close_output(&ctx); + close_codec(avcodecout); + close_sws(sws); + return -1; + } + } + if (avformat_write_header(ctx, NULL) < 0) { + fprintf(stderr, "Couldn't write header to output file %s\n", file); + } + *avformatout = ctx; + return 0; +} + +static int decode_packet(AVCodecContext *ctx, AVPacket *packet, AVFrame *frame) +{ + int ret = 0; + + ret = avcodec_send_packet(ctx, packet); + if (!ret) { + ret = avcodec_receive_frame(ctx, frame); + if (!ret) { + frame->opaque = av_malloc(sizeof(int)); + *(int *)frame->opaque = packet->stream_index; + } + } else { + fprintf(stderr, "Couldn't decode video packet %s (%d)\n", + av_err2str(ret), ret); + } + av_packet_unref(packet); + + return ret; +} + +static int scale_frame(struct SwsContext *sws, AVFrame *frame, AVFrame *scaled, + int width, int height, enum AVPixelFormat pix_fmt) +{ + av_frame_copy_props(scaled, frame); + scaled->width = width; + scaled->height = height; + scaled->format = pix_fmt; + av_frame_get_buffer(scaled, 0); + sws_scale(sws, (const uint8_t * const *)frame->data, frame->linesize, + 0, frame->height, scaled->data, scaled->linesize); + scaled->opaque = frame->opaque; + frame->opaque = NULL; + av_frame_unref(frame); + + return 0; +} + +static int encode_frame(AVCodecContext *ctx, AVFrame *frame, AVPacket *packet) +{ + int ret = 0; + + ret = avcodec_send_frame(ctx, frame); + if (!ret) { + ret = avcodec_receive_packet(ctx, packet); + if (!ret && frame->opaque != NULL) { + packet->stream_index = *(int *)frame->opaque; + } + } else { + fprintf(stderr, "Couldn't encode video packet %s (%d)\n", + av_err2str(ret), ret); + } + av_freep(&frame->opaque); + av_frame_unref(frame); + + return ret; +} + +static void flush_video(AVFormatContext *avformatin, AVCodecContext *avcodecin, + struct SwsContext *sws, AVCodecContext *avcodecout, + AVFormatContext *avformatout, int idx) +{ + AVFrame *frame = NULL; + AVPacket *packet = NULL; + AVFrame *scaled = NULL; + + frame = av_frame_alloc(); + packet = av_packet_alloc(); + scaled = av_frame_alloc(); + avcodec_send_packet(avcodecin, NULL); + while (!avcodec_receive_frame(avcodecin, frame)) { + scale_frame(sws, frame, scaled, avcodecout->width, + avcodecout->height, avcodecout->pix_fmt); + avcodec_send_frame(avcodecout, scaled); + if (!encode_frame(avcodecout, scaled, packet)) { + av_packet_rescale_ts(packet, avformatin->streams[idx]->time_base, + avformatout->streams[idx]->time_base); + packet->stream_index = idx; + av_interleaved_write_frame(avformatout, packet); + } + } + avcodec_send_frame(avcodecout, NULL); + while (!avcodec_receive_packet(avcodecout, packet)) { + av_packet_rescale_ts(packet, avformatin->streams[idx]->time_base, + avformatout->streams[idx]->time_base); + packet->stream_index = idx; + av_interleaved_write_frame(avformatout, packet); + } + av_frame_free(&frame); + av_packet_free(&packet); + av_frame_free(&scaled); +} + +void tube_packet_free(void *element) +{ + av_packet_free((AVPacket **)&element); +} + +void tube_frame_free(void *element) +{ + av_frame_free((AVFrame **)&element); +} + +static void *tube_decode(void *element, void *opaque) +{ + AVPacket *packet = element; + AVCodecContext *avcodecin = opaque; + AVFrame *frame = av_frame_alloc(); + + if (decode_packet(avcodecin, packet, frame)) { + av_frame_free(&frame); + } + av_packet_free(&packet); + + return frame; +} + +struct tube_resize_opaque { + struct SwsContext *sws; + int width; + int height; + enum AVPixelFormat pix_fmt; +}; + +static void *tube_resize(void *element, void *opaque) +{ + AVFrame *frame = element; + struct tube_resize_opaque *o = opaque; + AVFrame *scaled = av_frame_alloc(); + + scale_frame(o->sws, frame, scaled, o->width, o->height, o->pix_fmt); + av_frame_free(&frame); + + return scaled; +} + +static void *tube_encode(void *element, void *opaque) +{ + AVFrame *frame = element; + AVCodecContext *avcodecout = opaque; + AVPacket *packet = av_packet_alloc(); + + if (encode_frame(avcodecout, frame, packet)) { + av_packet_free(&packet); + } + av_frame_free(&frame); + + return packet; +} + +struct tube_mux_opaque { + AVFormatContext *avformatin; + AVFormatContext *avformatout; +}; + +void tube_mux(void *element, void *opaque) +{ + AVPacket *packet = element; + struct tube_mux_opaque *o = opaque; + AVPacket packet2mux; + int idx = packet->stream_index; + + av_packet_rescale_ts(packet, o->avformatin->streams[idx]->time_base, + o->avformatout->streams[idx]->time_base); + av_packet_ref(&packet2mux, packet); + av_interleaved_write_frame(o->avformatout, &packet2mux); + av_packet_free(&packet); +} + +static void print_tube_stats(tube *ctx) +{ + int queued_read = tube_get_queued(ctx, 0); + int slots_read = tube_get_slots(ctx, 0); + int queued_decode = tube_get_queued(ctx, 1); + int slots_decode = tube_get_slots(ctx, 1); + int queued_resize = tube_get_queued(ctx, 2); + int slots_resize = tube_get_slots(ctx, 2); + int queued_encode = tube_get_queued(ctx, 3); + int slots_encode = tube_get_slots(ctx, 3); + + printf("read -> [%d/%d] -> deco -> [%d/%d] -> resize -> [%d/%d] -> encode -> [%d/%d] -> write\n", + queued_read, slots_read, queued_decode, slots_decode, + queued_resize, slots_resize, queued_encode, slots_encode); +} + +static tube *build_tuberia(AVCodecContext *avcodecin, AVCodecContext *avcodecout, + struct tube_resize_opaque *tube_resize_opaque, + struct tube_mux_opaque *tube_mux_opaque) +{ + tube *ctx = NULL; + tube_source *source = NULL; + tube_stage *decode = NULL; + tube_stage *resize = NULL; + tube_stage *encode = NULL; + + source = tube_source_alloc(10, NULL, NULL, tube_packet_free); + decode = tube_stage_alloc(10, tube_decode, avcodecin, tube_frame_free); + resize = tube_stage_alloc(10, tube_resize, tube_resize_opaque, tube_frame_free); + encode = tube_stage_alloc(10, tube_encode, avcodecout, tube_packet_free); + + if (tube_stage_append(source, decode)) { + fprintf(stderr, "Couldn't append decode stage to tuberia\n"); + return NULL; + } + if (tube_stage_append(source, resize)) { + fprintf(stderr, "Couldn't append resize stage to tuberia\n"); + return NULL; + } + if (tube_stage_append(source, encode)) { + fprintf(stderr, "Couldn't append encode stage to tuberia\n"); + return NULL; + } + ctx = tube_alloc(source, tube_mux, tube_mux_opaque); + tube_source_and_stages_free(&source); + + return ctx; +} + +static void do_tuberia(AVFormatContext *avformatin, AVCodecContext *avcodecin, + AVFormatContext *avformatout, struct SwsContext *sws, + AVCodecContext *avcodecout, int video_idx) +{ + tube *ctx = NULL; + AVPacket *packet = NULL; + int idx = 0; + struct tube_resize_opaque tube_resize_opaque = { + .sws = sws, + .width = avcodecout->width, + .height = avcodecout->height, + .pix_fmt = avcodecout->pix_fmt + }; + struct tube_mux_opaque tube_mux_opaque = { + .avformatin = avformatin, + .avformatout = avformatout, + }; + + ctx = build_tuberia(avcodecin, avcodecout, &tube_resize_opaque, + &tube_mux_opaque); + if (ctx == NULL) { + fprintf(stderr, "Couldn't create tuberia\n"); + return; + } + + printf("Starting to transcode with libtuberia\n"); + tube_start(ctx); + packet = av_packet_alloc(); + while (!av_read_frame(avformatin, packet)) { + idx = packet->stream_index; + if (idx == video_idx) { + if (tube_inject(ctx, 15000, packet)) { + fprintf(stderr, "Couldn't inject packet to tuberia after 15s\n"); + break; + } + print_tube_stats(ctx); + } else { + tube_inject_at(ctx, 3, 15000, packet); + } + packet = av_packet_alloc(); + } + tube_stop_and_wait_empty(ctx); + if (video_idx >= 0) { + flush_video(avformatin, avcodecin, sws, avcodecout, avformatout, video_idx); + } + printf("Transcode with libtuberia finished\n"); + + av_packet_free(&packet); + tube_free(&ctx); +} + +static void do_no_tuberia(AVFormatContext *avformatin, AVCodecContext *avcodecin, + AVFormatContext *avformatout, struct SwsContext *sws, + AVCodecContext *avcodecout, int video_idx) +{ + AVPacket *packet = NULL; + AVFrame *frame = NULL; + AVFrame *scaled = NULL; + int idx = 0; + + packet = av_packet_alloc(); + frame = av_frame_alloc(); + scaled = av_frame_alloc(); + + printf("Starting to transcode without libtuberia\n"); + while (!av_read_frame(avformatin, packet)) { + idx = packet->stream_index; + if (idx == video_idx) { + if (decode_packet(avcodecin, packet, frame)) { + continue; + } + scale_frame(sws, frame, scaled, avcodecout->width, + avcodecout->height, avcodecout->pix_fmt); + if (encode_frame(avcodecout, scaled, packet)) { + continue; + } + } + av_packet_rescale_ts(packet, avformatin->streams[idx]->time_base, + avformatout->streams[idx]->time_base); + av_interleaved_write_frame(avformatout, packet); + } + if (video_idx >= 0) { + flush_video(avformatin, avcodecin, sws, avcodecout, avformatout, video_idx); + } + printf("Transcode without libtuberia finished\n"); + + av_packet_free(&packet); + av_frame_free(&frame); + av_frame_free(&scaled); +} + +int main(int argc, char **argv) +{ + int c = 0; + char *input = NULL; + char *output = NULL; + int width = 720; + int height = 404; + int notuberia = 0; + AVFormatContext *avformatin = NULL; + AVFormatContext *avformatout = NULL; + AVCodecContext *avcodecin = NULL; + AVCodecContext *avcodecout = NULL; + struct SwsContext *sws = NULL; + int video_idx = -1; + + while ((c = getopt(argc, argv, optstring)) >= 0) { + switch (c) { + case 'h': + printhelp(argv[0]); + return 0; + break; + case 'i': + input = optarg; + break; + case 'o': + output = optarg; + break; + case 's': + if (sscanf(optarg, "%dx%d", &width, &height) != 2) { + fprintf(stderr, "Size parameter must be WxH, 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]); + return -1; + } + } + if (input == NULL || !input[0]) { + printhelp(argv[0]); + return -1; + } + + if (open_input(&avformatin, &avcodecin, &video_idx, input) < 0) { + fprintf(stderr, "Couldn't open input %s\n", input); + return -1; + } + if (open_output(&avformatout, &avcodecout, &sws, output, avformatin, width, + height) < 0) { + fprintf(stderr, "Couldn't initiate output %s\n", output); + return -1; + } + + if (notuberia) { + do_no_tuberia(avformatin, avcodecin, avformatout, sws, avcodecout, video_idx); + } else { + do_tuberia(avformatin, avcodecin, avformatout, sws, avcodecout, video_idx); + } + + close_input(&avformatin); + close_output(&avformatout); + close_codec(&avcodecin); + close_codec(&avcodecout); + close_sws(&sws); + + return 0; +} + -- cgit v1.2.3