From 1622beeac4823110cbb1819ad06b8501097b71d9 Mon Sep 17 00:00:00 2001 From: nick black Date: Sat, 22 May 2021 17:28:15 -0400 Subject: [PATCH] ncvisual_from_rgba: align suitably for ffmpeg (64B) #1675 --- include/notcurses/notcurses.h | 4 ++-- src/lib/visual.c | 13 +++++++++++-- src/media/ffmpeg.c | 11 ++++++----- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/include/notcurses/notcurses.h b/include/notcurses/notcurses.h index 74196767d..2e402c987 100644 --- a/include/notcurses/notcurses.h +++ b/include/notcurses/notcurses.h @@ -2643,8 +2643,8 @@ API int ncblit_rgb_loose(const void* data, int linesize, // Per libav, we "store as BGRA on little-endian, and ARGB on big-endian". // This is an RGBA *byte-order* scheme. libav emits bytes, not words. Those // bytes are R-G-B-A. When read as words, on little endian this will be ABGR, -// and on big-endian this will be RGBA. force everything to LE ABGR, a no-op on -// and thus favoring little-endian. Take that, big-endian mafia! +// and on big-endian this will be RGBA. force everything to LE ABGR, a no-op +// on (and thus favoring) little-endian. Take that, big-endian mafia! // Extract the 8-bit alpha component from a pixel static inline unsigned diff --git a/src/lib/visual.c b/src/lib/visual.c index 3c6dac188..fd1732ba7 100644 --- a/src/lib/visual.c +++ b/src/lib/visual.c @@ -526,15 +526,24 @@ ncvisual* ncvisual_from_rgba(const void* rgba, int rows, int rowstride, int cols } ncvisual* ncv = ncvisual_create(); if(ncv){ + // ffmpeg needs inputs with rows aligned on 192-byte boundaries ncv->rowstride = rowstride; + #define IMGALIGN 64 + if(ncv->rowstride % IMGALIGN){ + ncv->rowstride = (ncv->rowstride + IMGALIGN) / IMGALIGN * IMGALIGN; + } + #undef IMGALIGN ncv->pixx = cols; ncv->pixy = rows; - uint32_t* data = memdup(rgba, rowstride * ncv->pixy); -//fprintf(stderr, "COPY US %d (%d)\n", rowstride * ncv->pixy, ncv->pixy); + uint32_t* data = malloc(ncv->rowstride * ncv->pixy); if(data == NULL){ ncvisual_destroy(ncv); return NULL; } + for(int y = 0 ; y < rows ; ++y){ + memcpy(data + (ncv->rowstride * y) / 4, rgba + rowstride * y, rowstride); + } +//fprintf(stderr, "COPY US %d (%d)\n", rowstride * ncv->pixy, ncv->pixy); //fprintf(stderr, "ROWS: %d STRIDE: %d (%d) COLS: %d\n", rows, rowstride, rowstride / 4, cols); ncvisual_set_data(ncv, data, true); ncvisual_details_seed(ncv); diff --git a/src/media/ffmpeg.c b/src/media/ffmpeg.c index 034509170..663bc3c5c 100644 --- a/src/media/ffmpeg.c +++ b/src/media/ffmpeg.c @@ -42,8 +42,8 @@ typedef struct ncvisual_details { print_frame_summary(const AVCodecContext* cctx, const AVFrame* f){ char pfmt[128]; av_get_pix_fmt_string(pfmt, sizeof(pfmt), f->format); - fprintf(stderr, "Frame %05d (%d? %d?) %dx%d pfmt %d (%s)\n", - cctx->frame_number, + fprintf(stderr, "Frame %05d %p (%d? %d?) %dx%d pfmt %d (%s)\n", + cctx ? cctx->frame_number : 0, f, f->coded_picture_number, f->display_picture_number, f->width, f->height, @@ -455,12 +455,12 @@ int ffmpeg_decode_loop(ncvisual* ncv){ int ffmpeg_blit(ncvisual* ncv, int rows, int cols, ncplane* n, const struct blitset* bset, const blitterargs* bargs){ const AVFrame* inframe = ncv->details->frame; -//fprintf(stderr, "inframe: %p frame: %p\n", inframe, ncv->details->frame); +//print_frame_summary(NULL, inframe); void* data = NULL; int stride = 0; AVFrame* sframe = NULL; const int targformat = AV_PIX_FMT_RGBA; -//fprintf(stderr, "got format: %d want format: %d\n", inframe->format, targformat); +//fprintf(stderr, "got format: %d (%d/%d) want format: %d\n", inframe->format, inframe->height, inframe->width, targformat); if(inframe && (cols != inframe->width || rows != inframe->height || inframe->format != targformat)){ //fprintf(stderr, "resize+render: %d/%d->%d/%d\n", inframe->height, inframe->width, rows, cols); sframe = av_frame_alloc(); @@ -471,6 +471,7 @@ int ffmpeg_blit(ncvisual* ncv, int rows, int cols, ncplane* n, //fprintf(stderr, "WHN NCV: %d/%d bargslen: %d/%d\n", inframe->width, inframe->height, bargs->leny, bargs->lenx); const int srclenx = bargs->lenx ? bargs->lenx : inframe->width; const int srcleny = bargs->leny ? bargs->leny : inframe->height; +//fprintf(stderr, "src %d/%d -> targ %d/%d ctx: %p\n", srcleny, srclenx, rows, cols, ncv->details->swsctx); ncv->details->swsctx = sws_getCachedContext(ncv->details->swsctx, srclenx, srcleny, inframe->format, @@ -492,7 +493,7 @@ int ffmpeg_blit(ncvisual* ncv, int rows, int cols, ncplane* n, //fprintf(stderr, "Error allocating visual data (%d X %d)\n", sframe->height, sframe->width); return -1; } -//fprintf(stderr, "INFRAME DAA: %p SDATA: %p FDATA: %p\n", inframe->data[0], sframe->data[0], ncv->details->frame->data[0]); +//fprintf(stderr, "INFRAME DAA: %p SDATA: %p FDATA: %p to %d/%d\n", inframe->data[0], sframe->data[0], ncv->details->frame->data[0], sframe->height, sframe->width); int height = sws_scale(ncv->details->swsctx, (const uint8_t* const*)inframe->data, inframe->linesize, 0, srcleny, sframe->data, sframe->linesize);