From 9ed5a8b6c70bc3a00d6ac0f6a902c347d8f32007 Mon Sep 17 00:00:00 2001 From: nick black Date: Wed, 27 Nov 2019 15:46:17 -0500 Subject: [PATCH] decode AVPacket into AVFrame #61 --- include/notcurses.h | 4 ++++ src/lib/libav.c | 21 +++++++++++++++++++++ src/view/main.cpp | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/include/notcurses.h b/include/notcurses.h index 89a6742fa..c1b5b96eb 100644 --- a/include/notcurses.h +++ b/include/notcurses.h @@ -428,10 +428,14 @@ ncplane_rounded_box_cells(struct ncplane* n, cell* ul, cell* ur, cell* ll, } // multimedia functionality +struct AVFrame; API struct ncvisual* notcurses_visual_open(struct notcurses* nc, const char* filename); + API void ncvisual_destroy(struct ncvisual* ncv); +API struct AVFrame* ncvisual_decode(struct ncvisual* nc); + #undef API #ifdef __cplusplus diff --git a/src/lib/libav.c b/src/lib/libav.c index b6021e850..2f824df38 100644 --- a/src/lib/libav.c +++ b/src/lib/libav.c @@ -31,10 +31,31 @@ void ncvisual_destroy(ncvisual* ncv){ } } +AVFrame* ncvisual_decode(struct ncvisual* nc){ + int ret = avcodec_send_packet(nc->codecctx, nc->packet); + if(ret < 0){ + fprintf(stderr, "Error processing AVPacket (%s)\n", av_err2str(ret)); + return NULL; + } + do{ + ret = avcodec_receive_frame(nc->codecctx, nc->frame); + if(ret == AVERROR(EAGAIN) || ret == AVERROR_EOF){ + // FIXME can we still use nc->frame? + return NULL; + }else if(ret < 0){ + fprintf(stderr, "Error decoding AVPacket (%s)\n", av_err2str(ret)); + return NULL; + } + fprintf(stderr, "Got frame %05d\n", nc->codecctx->frame_number); + }while(ret > 0); + return NULL; // FIXME +} + ncvisual* notcurses_visual_open(struct notcurses* nc __attribute__ ((unused)), const char* filename){ ncvisual* ncv = ncvisual_create(); if(ncv == NULL){ + fprintf(stderr, "Couldn't create %s (%s)\n", filename, strerror(errno)); return NULL; } int ret = avformat_open_input(&ncv->fmtctx, filename, NULL, NULL); diff --git a/src/view/main.cpp b/src/view/main.cpp index 425199a92..03d9611b3 100644 --- a/src/view/main.cpp +++ b/src/view/main.cpp @@ -1,18 +1,45 @@ #include #include #include +#include "notcurses.h" -void usage(std::ostream& o, char* argv0, int exitcode){ - o << "usage: " << basename(argv0) << " files" << '\n'; +static void usage(std::ostream& os, const char* name, int exitcode) + __attribute__ ((noreturn)); + +void usage(std::ostream& o, const char* name, int exitcode){ + o << "usage: " << name << " files" << '\n'; exit(exitcode); } +int ncview(struct ncvisual* ncv, const notcurses_options* opts){ + ncvisual_decode(ncv); + auto nc = notcurses_init(opts); + if(nc == nullptr){ + return -1; + } + ncvisual_destroy(ncv); + return notcurses_stop(nc); +} + int main(int argc, char** argv){ if(argc == 1){ usage(std::cerr, argv[0], EXIT_FAILURE); } + notcurses_options opts{}; + opts.outfp = stdout; + bool success = true; for(int i = 1 ; i < argc ; ++i){ - std::cout << "file: " << argv[i] << std::endl; + auto ncv = notcurses_visual_open(nullptr, argv[i]); + if(ncv == nullptr){ + success = false; + continue; + } + if(ncview(ncv, &opts)){ + success = false; + } + } + if(!success){ + return EXIT_FAILURE; } return EXIT_SUCCESS; }