Skip to content

Commit

Permalink
simplified VideoEncoder API
Browse files Browse the repository at this point in the history
  • Loading branch information
LiangliangNan committed Nov 29, 2024
1 parent 8091c6c commit 6cecfb1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 21 deletions.
4 changes: 2 additions & 2 deletions applications/Mapple/paint_canvas_snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,14 @@ void PaintCanvas::recordAnimation(const QString &file_name, int fps, int bit_rat
}
if (image.format() != QImage::Format_RGBA8888)
image = image.convertToFormat(QImage::Format_RGBA8888);
if (!encoder.encode(image.constBits(), image.width(), image.height(), 4, VideoEncoder::PIX_FMT_RGBA_8888)) {
if (!encoder.encode(image.constBits(), image.width(), image.height(), VideoEncoder::PIX_FMT_RGBA_8888)) {
success = false;
break;
}
#else
std::vector<unsigned char> image;
fbo->read_color(0, image, GL_RGBA);
if (!encoder.encode(image.data(), fw, fh, 4, VideoEncoder::PIX_FMT_RGBA_8888)) {
if (!encoder.encode(image.data(), fw, fh, VideoEncoder::PIX_FMT_RGBA_8888)) {
success = false;
break;
}
Expand Down
23 changes: 6 additions & 17 deletions easy3d/video/video_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ namespace easy3d {
}


bool VideoEncoder::encode(const unsigned char *image_data, int width, int height, int channels, PixelFormat pixel_format) {
bool VideoEncoder::encode(const unsigned char *image_data, int width, int height, PixelFormat pixel_format) {
if (!is_size_acceptable(width, height)) {
LOG(ERROR) << "video frame resolution (" << width << ", " << height << ") is not a multiple of 8";
return false;
Expand Down Expand Up @@ -1120,34 +1120,23 @@ namespace easy3d {
return false;
}

int channels = 3;
AVPixelFormat pix_fmt = AV_PIX_FMT_RGB24;
switch (pixel_format) {
case PIX_FMT_RGB_888:
if (channels != 3) {
LOG(ERROR) << "pixel format (PIX_FMT_RGB_888) does not match the number of channels (" << channels << ")";
return false;
}
channels = 3;
pix_fmt = AV_PIX_FMT_RGB24;
break;
case PIX_FMT_BGR_888:
if (channels != 3) {
LOG(ERROR) << "pixel format (PIX_FMT_BGR_888) does not match the number of channels (" << channels << ")";
return false;
}
channels = 3;
pix_fmt = AV_PIX_FMT_BGR24;
break;
case PIX_FMT_RGBA_8888:
if (channels != 4) {
LOG(ERROR) << "pixel format (PIX_FMT_RGBA_8888) does not match the number of channels (" << channels << ")";
return false;
}
channels = 4;
pix_fmt = AV_PIX_FMT_RGBA;
break;
case PIX_FMT_BGRA_8888:
if (channels != 4) {
LOG(ERROR) << "pixel format (PIX_FMT_BGRA_8888) does not match the number of channels (" << channels << ")";
return false;
}
channels = 4;
pix_fmt = AV_PIX_FMT_BGRA;
break;
}
Expand Down
3 changes: 1 addition & 2 deletions easy3d/video/video_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,14 @@ namespace easy3d {
* following structures.
* \param width video width (must be a multiple of 8)
* \param height video height (must be a multiple of 8)
* \param channels the number of channels of the image
* \param pixel_format pixel format. The correspondences between the image structures and pixel/OpenGL formats are:
* RGB 8:8:8, 24bpp <---> PIX_FMT_RGB_888 <---> GL_RGB
* BGR 8:8:8, 24bpp <---> PIX_FMT_BGR_888 <---> GL_BGR
* RGBA 8:8:8:8, 32bpp <---> PIX_FMT_RGBA_8888 <---> GL_RGBA
* BGRA 8:8:8:8, 32bpp <---> PIX_FMT_BGRA_8888 <---> GL_BGRA
* \return true on successful.
**/
bool encode(const unsigned char* image_data, int width, int height, int channels, PixelFormat pixel_format);
bool encode(const unsigned char* image_data, int width, int height, PixelFormat pixel_format);

/// Returns whether the image size (width, height) is acceptable.
static bool is_size_acceptable(int width, int height) { return (width % 8) == 0 && (height % 8) == 0; }
Expand Down

0 comments on commit 6cecfb1

Please sign in to comment.