19 #pragma clang diagnostic push
20 #pragma clang diagnostic ignored "-Wdocumentation"
21 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
22 #include <libavcodec/avcodec.h>
23 #include <libavformat/avformat.h>
24 #include <libavutil/avutil.h>
25 #include <libavutil/imgutils.h>
26 #include <libswscale/swscale.h>
28 #include <libswresample/swresample.h>
29 #include <libavutil/opt.h>
30 #pragma clang diagnostic pop
33 #pragma clang diagnostic push
34 #pragma clang diagnostic ignored "-Wunused-function"
52 return av_rescale_q(pts, stream->time_base, AV_TIME_BASE_Q) *
USEC_TO_SECOND;
58 if( pts == AV_NOPTS_VALUE )
59 VUserLog(
"Timestamp value is invalid!");
60 return av_rescale_q(pts, stream->time_base, AV_TIME_BASE_Q);
67 int64_t ms = av_rescale(second * 1000, stream->time_base.den, stream->time_base.num);
78 for(
int i = 0; i < context->nb_streams; i++)
79 if( context->streams[i]->codecpar->codec_type == type)
89 uint row_size_bytes = width *
sizeof(uint8_t);
90 uint8_t* tmp = (uint8_t*)malloc(row_size_bytes);
92 for(uint i = 0; i < height/2; i++)
94 memcpy(tmp, buffer + width * i, row_size_bytes);
95 memcpy(buffer + width * i, buffer + width * (height-i-1), row_size_bytes);
96 memcpy(buffer + width * (height-i-1), tmp, row_size_bytes);
107 static bool ConvertAVFrameToRGB(AVFrame *dst, AVPixelFormat dst_pix_fmt, AVFrame *src, AVPixelFormat pix_fmt,
int width,
int height)
109 SwsContext *img_convert_ctx = sws_getContext(width, height, pix_fmt, width, height, dst_pix_fmt, SWS_BICUBIC, NULL, NULL, NULL);
111 bool slowFlip =
false;
112 int planeCount = av_pix_fmt_count_planes((AVPixelFormat)src->format);
116 || src->format == AV_PIX_FMT_YUV422P
117 || src->format == AV_PIX_FMT_YUVJ422P
118 || src->format == AV_PIX_FMT_YUV422P10LE
119 || src->format == AV_PIX_FMT_YUVJ444P
120 || src->format == AV_PIX_FMT_YUVA444P12LE
121 || src->format == AV_PIX_FMT_YUV411P
122 || src->format == AV_PIX_FMT_YUVJ411P)
125 for (
int i = 0; i < planeCount; ++i)
127 src->data[i] += src->linesize[i] * (src->height - 1);
128 src->linesize[i] = -src->linesize[i];
131 else if (src->format == AV_PIX_FMT_YUV420P
132 || src->format == AV_PIX_FMT_YUVJ420P)
135 for (
int i = 0; i < 3; ++i)
138 src->data[i] += src->linesize[i] * (src->height - 1);
140 src->data[i] += src->linesize[i] * (src->height/2 - 1);
141 src->linesize[i] = -src->linesize[i];
148 VUserLog(
"Unknown pixelformat %s (%d planes); using slow flipper.", av_get_pix_fmt_name((AVPixelFormat)src->format), planeCount);
154 int result = sws_scale(img_convert_ctx, src->data, src->linesize, 0, height, dst->data, dst->linesize);
155 sws_freeContext(img_convert_ctx);
169 AVPixelFormat pixelFormat = AV_PIX_FMT_RGBA;
172 AVFrame *pFrameRGB = av_frame_alloc();
176 int linesize = av_image_get_linesize(pixelFormat, videoCodecCtx->width, 0);
177 int numBytes = av_image_get_buffer_size(pixelFormat, videoCodecCtx->width, videoCodecCtx->height, linesize);
178 uint8_t *buffer = (uint8_t *)av_malloc(numBytes *
sizeof(uint8_t));
180 av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, buffer, pixelFormat, videoCodecCtx->width, videoCodecCtx->height, 1);
183 ConvertAVFrameToRGB(pFrameRGB, pixelFormat, frame, videoCodecCtx->pix_fmt, videoCodecCtx->width, videoCodecCtx->height);
186 image =
VuoImage_makeFromBuffer(pFrameRGB->data[0], GL_RGBA, videoCodecCtx->width, videoCodecCtx->height, VuoImageColorDepth_8, ^(
void *){
195 #pragma clang diagnostic pop