decode AVPacket into AVFrame #61

This commit is contained in:
nick black 2019-11-27 15:46:17 -05:00 committed by Nick Black
parent cf557ff500
commit 9ed5a8b6c7
3 changed files with 55 additions and 3 deletions

View File

@ -428,10 +428,14 @@ ncplane_rounded_box_cells(struct ncplane* n, cell* ul, cell* ur, cell* ll,
} }
// multimedia functionality // multimedia functionality
struct AVFrame;
API struct ncvisual* notcurses_visual_open(struct notcurses* nc, API struct ncvisual* notcurses_visual_open(struct notcurses* nc,
const char* filename); const char* filename);
API void ncvisual_destroy(struct ncvisual* ncv); API void ncvisual_destroy(struct ncvisual* ncv);
API struct AVFrame* ncvisual_decode(struct ncvisual* nc);
#undef API #undef API
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -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)), ncvisual* notcurses_visual_open(struct notcurses* nc __attribute__ ((unused)),
const char* filename){ const char* filename){
ncvisual* ncv = ncvisual_create(); ncvisual* ncv = ncvisual_create();
if(ncv == NULL){ if(ncv == NULL){
fprintf(stderr, "Couldn't create %s (%s)\n", filename, strerror(errno));
return NULL; return NULL;
} }
int ret = avformat_open_input(&ncv->fmtctx, filename, NULL, NULL); int ret = avformat_open_input(&ncv->fmtctx, filename, NULL, NULL);

View File

@ -1,18 +1,45 @@
#include <cstdlib> #include <cstdlib>
#include <libgen.h> #include <libgen.h>
#include <iostream> #include <iostream>
#include "notcurses.h"
void usage(std::ostream& o, char* argv0, int exitcode){ static void usage(std::ostream& os, const char* name, int exitcode)
o << "usage: " << basename(argv0) << " files" << '\n'; __attribute__ ((noreturn));
void usage(std::ostream& o, const char* name, int exitcode){
o << "usage: " << name << " files" << '\n';
exit(exitcode); 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){ int main(int argc, char** argv){
if(argc == 1){ if(argc == 1){
usage(std::cerr, argv[0], EXIT_FAILURE); usage(std::cerr, argv[0], EXIT_FAILURE);
} }
notcurses_options opts{};
opts.outfp = stdout;
bool success = true;
for(int i = 1 ; i < argc ; ++i){ 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; return EXIT_SUCCESS;
} }