OmxEncoder: use macro to check error (#19785)

old-commit-hash: 0592c326ed
commatwo_master
Dean Lee 5 years ago committed by GitHub
parent b6e19ce5ab
commit 9eb6648ec5
  1. 105
      selfdrive/loggerd/omx_encoder.cc

@ -23,6 +23,11 @@
#include "omx_encoder.h" #include "omx_encoder.h"
// Check the OMX error code and assert if an error occurred.
#define OMX_CHECK(_expr) \
do { \
assert(OMX_ErrorNone == _expr); \
} while (0)
// ***** OMX callback functions ***** // ***** OMX callback functions *****
@ -159,8 +164,6 @@ static const char* omx_color_fomat_name(uint32_t format) {
// ***** encoder functions ***** // ***** encoder functions *****
OmxEncoder::OmxEncoder(const char* filename, int width, int height, int fps, int bitrate, bool h265, bool downscale) { OmxEncoder::OmxEncoder(const char* filename, int width, int height, int fps, int bitrate, bool h265, bool downscale) {
int err;
this->filename = filename; this->filename = filename;
this->width = width; this->width = width;
this->height = height; this->height = height;
@ -182,7 +185,7 @@ OmxEncoder::OmxEncoder(const char* filename, int width, int height, int fps, int
} }
auto component = (OMX_STRING)(h265 ? "OMX.qcom.video.encoder.hevc" : "OMX.qcom.video.encoder.avc"); auto component = (OMX_STRING)(h265 ? "OMX.qcom.video.encoder.hevc" : "OMX.qcom.video.encoder.avc");
err = OMX_GetHandle(&this->handle, component, this, &omx_callbacks); int err = OMX_GetHandle(&this->handle, component, this, &omx_callbacks);
if (err != OMX_ErrorNone) { if (err != OMX_ErrorNone) {
LOGE("error getting codec: %x", err); LOGE("error getting codec: %x", err);
} }
@ -194,9 +197,7 @@ OmxEncoder::OmxEncoder(const char* filename, int width, int height, int fps, int
OMX_PARAM_PORTDEFINITIONTYPE in_port = {0}; OMX_PARAM_PORTDEFINITIONTYPE in_port = {0};
in_port.nSize = sizeof(in_port); in_port.nSize = sizeof(in_port);
in_port.nPortIndex = (OMX_U32) PORT_INDEX_IN; in_port.nPortIndex = (OMX_U32) PORT_INDEX_IN;
err = OMX_GetParameter(this->handle, OMX_IndexParamPortDefinition, OMX_CHECK(OMX_GetParameter(this->handle, OMX_IndexParamPortDefinition, (OMX_PTR) &in_port));
(OMX_PTR) &in_port);
assert(err == OMX_ErrorNone);
in_port.format.video.nFrameWidth = this->width; in_port.format.video.nFrameWidth = this->width;
in_port.format.video.nFrameHeight = this->height; in_port.format.video.nFrameHeight = this->height;
@ -209,14 +210,8 @@ OmxEncoder::OmxEncoder(const char* filename, int width, int height, int fps, int
// in_port.format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar; // in_port.format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
in_port.format.video.eColorFormat = (OMX_COLOR_FORMATTYPE)QOMX_COLOR_FORMATYUV420PackedSemiPlanar32m; in_port.format.video.eColorFormat = (OMX_COLOR_FORMATTYPE)QOMX_COLOR_FORMATYUV420PackedSemiPlanar32m;
err = OMX_SetParameter(this->handle, OMX_IndexParamPortDefinition, OMX_CHECK(OMX_SetParameter(this->handle, OMX_IndexParamPortDefinition, (OMX_PTR) &in_port));
(OMX_PTR) &in_port); OMX_CHECK(OMX_GetParameter(this->handle, OMX_IndexParamPortDefinition, (OMX_PTR) &in_port));
assert(err == OMX_ErrorNone);
err = OMX_GetParameter(this->handle, OMX_IndexParamPortDefinition,
(OMX_PTR) &in_port);
assert(err == OMX_ErrorNone);
this->num_in_bufs = in_port.nBufferCountActual; this->num_in_bufs = in_port.nBufferCountActual;
// setup output port // setup output port
@ -224,9 +219,7 @@ OmxEncoder::OmxEncoder(const char* filename, int width, int height, int fps, int
OMX_PARAM_PORTDEFINITIONTYPE out_port = {0}; OMX_PARAM_PORTDEFINITIONTYPE out_port = {0};
out_port.nSize = sizeof(out_port); out_port.nSize = sizeof(out_port);
out_port.nPortIndex = (OMX_U32) PORT_INDEX_OUT; out_port.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
err = OMX_GetParameter(this->handle, OMX_IndexParamPortDefinition, OMX_CHECK(OMX_GetParameter(this->handle, OMX_IndexParamPortDefinition, (OMX_PTR)&out_port));
(OMX_PTR)&out_port);
assert(err == OMX_ErrorNone);
out_port.format.video.nFrameWidth = this->width; out_port.format.video.nFrameWidth = this->width;
out_port.format.video.nFrameHeight = this->height; out_port.format.video.nFrameHeight = this->height;
out_port.format.video.xFramerate = 0; out_port.format.video.xFramerate = 0;
@ -238,28 +231,19 @@ OmxEncoder::OmxEncoder(const char* filename, int width, int height, int fps, int
} }
out_port.format.video.eColorFormat = OMX_COLOR_FormatUnused; out_port.format.video.eColorFormat = OMX_COLOR_FormatUnused;
err = OMX_SetParameter(this->handle, OMX_IndexParamPortDefinition, OMX_CHECK(OMX_SetParameter(this->handle, OMX_IndexParamPortDefinition, (OMX_PTR) &out_port));
(OMX_PTR) &out_port);
assert(err == OMX_ErrorNone);
err = OMX_GetParameter(this->handle, OMX_IndexParamPortDefinition, OMX_CHECK(OMX_GetParameter(this->handle, OMX_IndexParamPortDefinition, (OMX_PTR) &out_port));
(OMX_PTR) &out_port);
assert(err == OMX_ErrorNone);
this->num_out_bufs = out_port.nBufferCountActual; this->num_out_bufs = out_port.nBufferCountActual;
OMX_VIDEO_PARAM_BITRATETYPE bitrate_type = {0}; OMX_VIDEO_PARAM_BITRATETYPE bitrate_type = {0};
bitrate_type.nSize = sizeof(bitrate_type); bitrate_type.nSize = sizeof(bitrate_type);
bitrate_type.nPortIndex = (OMX_U32) PORT_INDEX_OUT; bitrate_type.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
err = OMX_GetParameter(this->handle, OMX_IndexParamVideoBitrate, OMX_CHECK(OMX_GetParameter(this->handle, OMX_IndexParamVideoBitrate, (OMX_PTR) &bitrate_type));
(OMX_PTR) &bitrate_type);
assert(err == OMX_ErrorNone);
bitrate_type.eControlRate = OMX_Video_ControlRateVariable; bitrate_type.eControlRate = OMX_Video_ControlRateVariable;
bitrate_type.nTargetBitrate = bitrate; bitrate_type.nTargetBitrate = bitrate;
err = OMX_SetParameter(this->handle, OMX_IndexParamVideoBitrate, OMX_CHECK(OMX_SetParameter(this->handle, OMX_IndexParamVideoBitrate, (OMX_PTR) &bitrate_type));
(OMX_PTR) &bitrate_type);
assert(err == OMX_ErrorNone);
if (h265) { if (h265) {
// setup HEVC // setup HEVC
@ -272,23 +256,18 @@ OmxEncoder::OmxEncoder(const char* filename, int width, int height, int fps, int
#endif #endif
hevc_type.nSize = sizeof(hevc_type); hevc_type.nSize = sizeof(hevc_type);
hevc_type.nPortIndex = (OMX_U32) PORT_INDEX_OUT; hevc_type.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
err = OMX_GetParameter(this->handle, index_type, OMX_CHECK(OMX_GetParameter(this->handle, index_type, (OMX_PTR) &hevc_type));
(OMX_PTR) &hevc_type);
assert(err == OMX_ErrorNone);
hevc_type.eProfile = OMX_VIDEO_HEVCProfileMain; hevc_type.eProfile = OMX_VIDEO_HEVCProfileMain;
hevc_type.eLevel = OMX_VIDEO_HEVCHighTierLevel5; hevc_type.eLevel = OMX_VIDEO_HEVCHighTierLevel5;
err = OMX_SetParameter(this->handle, index_type, OMX_CHECK(OMX_SetParameter(this->handle, index_type, (OMX_PTR) &hevc_type));
(OMX_PTR) &hevc_type);
assert(err == OMX_ErrorNone);
} else { } else {
// setup h264 // setup h264
OMX_VIDEO_PARAM_AVCTYPE avc = { 0 }; OMX_VIDEO_PARAM_AVCTYPE avc = { 0 };
avc.nSize = sizeof(avc); avc.nSize = sizeof(avc);
avc.nPortIndex = (OMX_U32) PORT_INDEX_OUT; avc.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
err = OMX_GetParameter(this->handle, OMX_IndexParamVideoAvc, &avc); OMX_CHECK(OMX_GetParameter(this->handle, OMX_IndexParamVideoAvc, &avc));
assert(err == OMX_ErrorNone);
avc.nBFrames = 0; avc.nBFrames = 0;
avc.nPFrames = 15; avc.nPFrames = 15;
@ -299,8 +278,7 @@ OmxEncoder::OmxEncoder(const char* filename, int width, int height, int fps, int
avc.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB; avc.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB;
avc.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable; avc.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
err = OMX_SetParameter(this->handle, OMX_IndexParamVideoAvc, &avc); OMX_CHECK(OMX_SetParameter(this->handle, OMX_IndexParamVideoAvc, &avc));
assert(err == OMX_ErrorNone);
} }
@ -326,35 +304,30 @@ OmxEncoder::OmxEncoder(const char* filename, int width, int height, int fps, int
// printf("profile %d level 0x%x\n", params.eProfile, params.eLevel); // printf("profile %d level 0x%x\n", params.eProfile, params.eLevel);
// } // }
err = OMX_SendCommand(this->handle, OMX_CommandStateSet, OMX_StateIdle, NULL); OMX_CHECK(OMX_SendCommand(this->handle, OMX_CommandStateSet, OMX_StateIdle, NULL));
assert(err == OMX_ErrorNone);
this->in_buf_headers = (OMX_BUFFERHEADERTYPE **)calloc(this->num_in_bufs, sizeof(OMX_BUFFERHEADERTYPE*)); this->in_buf_headers = (OMX_BUFFERHEADERTYPE **)calloc(this->num_in_bufs, sizeof(OMX_BUFFERHEADERTYPE*));
for (int i=0; i<this->num_in_bufs; i++) { for (int i=0; i<this->num_in_bufs; i++) {
err = OMX_AllocateBuffer(this->handle, &this->in_buf_headers[i], PORT_INDEX_IN, this, OMX_CHECK(OMX_AllocateBuffer(this->handle, &this->in_buf_headers[i], PORT_INDEX_IN, this,
in_port.nBufferSize); in_port.nBufferSize));
assert(err == OMX_ErrorNone);
} }
this->out_buf_headers = (OMX_BUFFERHEADERTYPE **)calloc(this->num_out_bufs, sizeof(OMX_BUFFERHEADERTYPE*)); this->out_buf_headers = (OMX_BUFFERHEADERTYPE **)calloc(this->num_out_bufs, sizeof(OMX_BUFFERHEADERTYPE*));
for (int i=0; i<this->num_out_bufs; i++) { for (int i=0; i<this->num_out_bufs; i++) {
err = OMX_AllocateBuffer(this->handle, &this->out_buf_headers[i], PORT_INDEX_OUT, this, OMX_CHECK(OMX_AllocateBuffer(this->handle, &this->out_buf_headers[i], PORT_INDEX_OUT, this,
out_port.nBufferSize); out_port.nBufferSize));
assert(err == OMX_ErrorNone);
} }
wait_for_state(OMX_StateIdle); wait_for_state(OMX_StateIdle);
err = OMX_SendCommand(this->handle, OMX_CommandStateSet, OMX_StateExecuting, NULL); OMX_CHECK(OMX_SendCommand(this->handle, OMX_CommandStateSet, OMX_StateExecuting, NULL));
assert(err == OMX_ErrorNone);
wait_for_state(OMX_StateExecuting); wait_for_state(OMX_StateExecuting);
// give omx all the output buffers // give omx all the output buffers
for (int i = 0; i < this->num_out_bufs; i++) { for (int i = 0; i < this->num_out_bufs; i++) {
// printf("fill %p\n", this->out_buf_headers[i]); // printf("fill %p\n", this->out_buf_headers[i]);
err = OMX_FillThisBuffer(this->handle, this->out_buf_headers[i]); OMX_CHECK(OMX_FillThisBuffer(this->handle, this->out_buf_headers[i]));
assert(err == OMX_ErrorNone);
} }
// fill the input free queue // fill the input free queue
@ -430,8 +403,7 @@ void OmxEncoder::handle_out_buf(OmxEncoder *e, OMX_BUFFERHEADERTYPE *out_buf) {
out_buf->nTimeStamp = 0; out_buf->nTimeStamp = 0;
} }
#endif #endif
err = OMX_FillThisBuffer(e->handle, out_buf); OMX_CHECK(OMX_FillThisBuffer(e->handle, out_buf));
assert(err == OMX_ErrorNone);
} }
int OmxEncoder::encode_frame(const uint8_t *y_ptr, const uint8_t *u_ptr, const uint8_t *v_ptr, int OmxEncoder::encode_frame(const uint8_t *y_ptr, const uint8_t *u_ptr, const uint8_t *v_ptr,
@ -491,8 +463,7 @@ int OmxEncoder::encode_frame(const uint8_t *y_ptr, const uint8_t *u_ptr, const u
in_buf->nTimeStamp = extra->timestamp_eof/1000LL; // OMX_TICKS, in microseconds in_buf->nTimeStamp = extra->timestamp_eof/1000LL; // OMX_TICKS, in microseconds
this->last_t = in_buf->nTimeStamp; this->last_t = in_buf->nTimeStamp;
err = OMX_EmptyThisBuffer(this->handle, in_buf); OMX_CHECK(OMX_EmptyThisBuffer(this->handle, in_buf));
assert(err == OMX_ErrorNone);
// pump output // pump output
while (true) { while (true) {
@ -572,8 +543,6 @@ void OmxEncoder::encoder_open(const char* path, int segment) {
} }
void OmxEncoder::encoder_close() { void OmxEncoder::encoder_close() {
int err;
pthread_mutex_lock(&this->lock); pthread_mutex_lock(&this->lock);
if (this->is_open) { if (this->is_open) {
@ -586,8 +555,7 @@ void OmxEncoder::encoder_close() {
in_buf->nFlags = OMX_BUFFERFLAG_EOS; in_buf->nFlags = OMX_BUFFERFLAG_EOS;
in_buf->nTimeStamp = this->last_t + 1000000LL/this->fps; in_buf->nTimeStamp = this->last_t + 1000000LL/this->fps;
err = OMX_EmptyThisBuffer(this->handle, in_buf); OMX_CHECK(OMX_EmptyThisBuffer(this->handle, in_buf));
assert(err == OMX_ErrorNone);
while (true) { while (true) {
OMX_BUFFERHEADERTYPE *out_buf = (OMX_BUFFERHEADERTYPE *)queue_pop(&this->done_out); OMX_BUFFERHEADERTYPE *out_buf = (OMX_BUFFERHEADERTYPE *)queue_pop(&this->done_out);
@ -617,34 +585,27 @@ void OmxEncoder::encoder_close() {
} }
OmxEncoder::~OmxEncoder() { OmxEncoder::~OmxEncoder() {
int err;
assert(!this->is_open); assert(!this->is_open);
err = OMX_SendCommand(this->handle, OMX_CommandStateSet, OMX_StateIdle, NULL); OMX_CHECK(OMX_SendCommand(this->handle, OMX_CommandStateSet, OMX_StateIdle, NULL));
assert(err == OMX_ErrorNone);
wait_for_state(OMX_StateIdle); wait_for_state(OMX_StateIdle);
err = OMX_SendCommand(this->handle, OMX_CommandStateSet, OMX_StateLoaded, NULL); OMX_CHECK(OMX_SendCommand(this->handle, OMX_CommandStateSet, OMX_StateLoaded, NULL));
assert(err == OMX_ErrorNone);
for (int i=0; i<this->num_in_bufs; i++) { for (int i=0; i<this->num_in_bufs; i++) {
err = OMX_FreeBuffer(this->handle, PORT_INDEX_IN, this->in_buf_headers[i]); OMX_CHECK(OMX_FreeBuffer(this->handle, PORT_INDEX_IN, this->in_buf_headers[i]));
assert(err == OMX_ErrorNone);
} }
free(this->in_buf_headers); free(this->in_buf_headers);
for (int i=0; i<this->num_out_bufs; i++) { for (int i=0; i<this->num_out_bufs; i++) {
err = OMX_FreeBuffer(this->handle, PORT_INDEX_OUT, this->out_buf_headers[i]); OMX_CHECK(OMX_FreeBuffer(this->handle, PORT_INDEX_OUT, this->out_buf_headers[i]));
assert(err == OMX_ErrorNone);
} }
free(this->out_buf_headers); free(this->out_buf_headers);
wait_for_state(OMX_StateLoaded); wait_for_state(OMX_StateLoaded);
err = OMX_FreeHandle(this->handle); OMX_CHECK(OMX_FreeHandle(this->handle));
assert(err == OMX_ErrorNone);
while (queue_try_pop(&this->free_in)); while (queue_try_pop(&this->free_in));
while (queue_try_pop(&this->done_out)); while (queue_try_pop(&this->done_out));

Loading…
Cancel
Save