Skip to content

Commit 18c354f

Browse files
committed
VideoFrame: allow to adjust JPEG pixel formats to the normal one
...and vice versa
1 parent a22d7bf commit 18c354f

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

src/avcpp/frame.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,54 @@ int VideoFrame::height() const
150150
return RAW_GET(height, 0);
151151
}
152152

153+
template<typename LookupProc>
154+
static PixelFormat adjustJpegPixelFormat(AVFrame *frame, LookupProc&& proc)
155+
{
156+
return frame
157+
? static_cast<AVPixelFormat>(std::exchange(frame->format, proc(static_cast<AVPixelFormat>(frame->format))))
158+
: AV_PIX_FMT_NONE;
159+
}
160+
161+
PixelFormat VideoFrame::adjustFromJpegPixelFormat()
162+
{
163+
return adjustJpegPixelFormat(m_raw, [](AVPixelFormat pixfmt) {
164+
switch (pixfmt) {
165+
case AV_PIX_FMT_YUVJ420P:
166+
return AV_PIX_FMT_YUV420P;
167+
case AV_PIX_FMT_YUVJ411P:
168+
return AV_PIX_FMT_YUV411P;
169+
case AV_PIX_FMT_YUVJ422P:
170+
return AV_PIX_FMT_YUV422P;
171+
case AV_PIX_FMT_YUVJ444P:
172+
return AV_PIX_FMT_YUV444P;
173+
case AV_PIX_FMT_YUVJ440P:
174+
return AV_PIX_FMT_YUV440P;
175+
default:
176+
return pixfmt;
177+
}
178+
});
179+
}
180+
181+
PixelFormat VideoFrame::adjustToJpegPixelFormat()
182+
{
183+
return adjustJpegPixelFormat(m_raw, [](AVPixelFormat pixfmt) {
184+
switch (pixfmt) {
185+
case AV_PIX_FMT_YUV420P:
186+
return AV_PIX_FMT_YUVJ420P;
187+
case AV_PIX_FMT_YUV411P:
188+
return AV_PIX_FMT_YUVJ411P;
189+
case AV_PIX_FMT_YUV422P:
190+
return AV_PIX_FMT_YUVJ422P;
191+
case AV_PIX_FMT_YUV444P:
192+
return AV_PIX_FMT_YUVJ444P;
193+
case AV_PIX_FMT_YUV440P:
194+
return AV_PIX_FMT_YUVJ440P;
195+
default:
196+
return pixfmt;
197+
}
198+
});
199+
}
200+
153201
bool VideoFrame::isKeyFrame() const
154202
{
155203
#if AVCPP_API_FRAME_KEY

src/avcpp/frame.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,25 @@ class VideoFrame : public Frame<VideoFrame>
378378
int width() const;
379379
int height() const;
380380

381+
/**
382+
* Convert JPEG pixel format to the compatible non-JPEG one
383+
*
384+
* YUV Pixel formats with J in the naming (AV_PIX_FMT_YUVJxxx) mostly deprecated and some components warn about it.
385+
* Allow to adjust internal pixel format into compatible one.
386+
*
387+
* @return previous pixel format
388+
*/
389+
PixelFormat adjustFromJpegPixelFormat();
390+
/**
391+
* Convert non-JPEG pixel format to the compatible JPEG one
392+
*
393+
* YUV Pixel formats with J in the naming (AV_PIX_FMT_YUVJxxx) mostly deprecated and some components warn about it.
394+
* Allow to adjust internal pixel format into compatible one.
395+
*
396+
* @return previous pixel format
397+
*/
398+
PixelFormat adjustToJpegPixelFormat();
399+
381400
bool isKeyFrame() const;
382401
void setKeyFrame(bool isKey);
383402

0 commit comments

Comments
 (0)