File size: 11,115 Bytes
477da44 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
#include <stdio.h>
#include <sys/time.h>
#include "mpp_decoder.h"
#include <unistd.h>
#include <pthread.h>
#include <sys/syscall.h>
#define LOGD printf
// #define LOGD
static unsigned long GetCurrentTimeMS() {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec*1000+tv.tv_usec/1000;
}
MppDecoder::MppDecoder()
{
}
MppDecoder::~MppDecoder() {
if (loop_data.packet) {
mpp_packet_deinit(&loop_data.packet);
loop_data.packet = NULL;
}
if (frame) {
mpp_frame_deinit(&frame);
frame = NULL;
}
if (mpp_ctx) {
mpp_destroy(mpp_ctx);
mpp_ctx = NULL;
}
if (loop_data.frm_grp) {
mpp_buffer_group_put(loop_data.frm_grp);
loop_data.frm_grp = NULL;
}
}
int MppDecoder::Init(int video_type, int fps, void* userdata)
{
MPP_RET ret = MPP_OK;
this->userdata = userdata;
this->fps = fps;
this->last_frame_time_ms = 0;
if(video_type == 264) {
mpp_type = MPP_VIDEO_CodingAVC;
} else if (video_type == 265) {
mpp_type =MPP_VIDEO_CodingHEVC;
} else {
LOGD("unsupport video_type %d", video_type);
return -1;
}
LOGD("mpi_dec_test start ");
memset(&loop_data, 0, sizeof(loop_data));
LOGD("mpi_dec_test decoder test start mpp_type %d ", mpp_type);
MppDecCfg cfg = NULL;
MppCtx mpp_ctx = NULL;
ret = mpp_create(&mpp_ctx, &mpp_mpi);
if (MPP_OK != ret) {
LOGD("mpp_create failed ");
return 0;
}
ret = mpp_init(mpp_ctx, MPP_CTX_DEC, mpp_type);
if (ret) {
LOGD("%p mpp_init failed ", mpp_ctx);
return -1;
}
mpp_dec_cfg_init(&cfg);
/* get default config from decoder context */
ret = mpp_mpi->control(mpp_ctx, MPP_DEC_GET_CFG, cfg);
if (ret) {
LOGD("%p failed to get decoder cfg ret %d ", mpp_ctx, ret);
return -1;
}
/*
* split_parse is to enable mpp internal frame spliter when the input
* packet is not aplited into frames.
*/
ret = mpp_dec_cfg_set_u32(cfg, "base:split_parse", need_split);
if (ret) {
LOGD("%p failed to set split_parse ret %d ", mpp_ctx, ret);
return -1;
}
ret = mpp_mpi->control(mpp_ctx, MPP_DEC_SET_CFG, cfg);
if (ret) {
LOGD("%p failed to set cfg %p ret %d ", mpp_ctx, cfg, ret);
return -1;
}
mpp_dec_cfg_deinit(cfg);
loop_data.ctx = mpp_ctx;
loop_data.mpi = mpp_mpi;
loop_data.eos = 0;
loop_data.packet_size = packet_size;
loop_data.frame = 0;
loop_data.frame_count = 0;
return 1;
}
int MppDecoder::Reset() {
if (mpp_mpi != NULL) {
mpp_mpi->reset(mpp_ctx);
}
return 0;
}
int MppDecoder::Decode(uint8_t* pkt_data, int pkt_size, int pkt_eos)
{
MpiDecLoopData *data=&loop_data;
RK_U32 pkt_done = 0;
RK_U32 err_info = 0;
MPP_RET ret = MPP_OK;
MppCtx ctx = data->ctx;
MppApi *mpi = data->mpi;
size_t read_size = 0;
size_t packet_size = data->packet_size;
LOGD("receive packet size=%d ", pkt_size);
if (packet == NULL) {
ret = mpp_packet_init(&packet, NULL, 0);
}
///////////////////////////////////////////////
// ret = mpp_packet_init(&packet, frame_data, frame_size);
mpp_packet_set_data(packet, pkt_data);
mpp_packet_set_size(packet, pkt_size);
mpp_packet_set_pos(packet, pkt_data);
mpp_packet_set_length(packet, pkt_size);
// setup eos flag
if (pkt_eos)
mpp_packet_set_eos(packet);
do {
RK_S32 times = 5;
// send the packet first if packet is not done
if (!pkt_done) {
ret = mpi->decode_put_packet(ctx, packet);
if (MPP_OK == ret)
pkt_done = 1;
}
// then get all available frame and release
do {
RK_S32 get_frm = 0;
RK_U32 frm_eos = 0;
try_again:
ret = mpi->decode_get_frame(ctx, &frame);
if (MPP_ERR_TIMEOUT == ret) {
if (times > 0) {
times--;
usleep(2000);
goto try_again;
}
LOGD("decode_get_frame failed too much time ");
}
if (MPP_OK != ret) {
LOGD("decode_get_frame failed ret %d ", ret);
break;
}
if (frame) {
RK_U32 hor_stride = mpp_frame_get_hor_stride(frame);
RK_U32 ver_stride = mpp_frame_get_ver_stride(frame);
RK_U32 hor_width = mpp_frame_get_width(frame);
RK_U32 ver_height = mpp_frame_get_height(frame);
RK_U32 buf_size = mpp_frame_get_buf_size(frame);
RK_S64 pts = mpp_frame_get_pts(frame);
RK_S64 dts = mpp_frame_get_dts(frame);
LOGD("decoder require buffer w:h [%d:%d] stride [%d:%d] buf_size %d pts=%lld dts=%lld ",
hor_width, ver_height, hor_stride, ver_stride, buf_size, pts, dts);
if (mpp_frame_get_info_change(frame)) {
LOGD("decode_get_frame get info changed found ");
// ret = mpp_buffer_group_get_internal(&data->frm_grp, MPP_BUFFER_TYPE_DRM);
// if (ret) {
// LOGD("get mpp buffer group failed ret %d ", ret);
// break;
// }
// mpi->control(ctx, MPP_DEC_SET_EXT_BUF_GROUP, data->frm_grp);
// mpi->control(ctx, MPP_DEC_SET_INFO_CHANGE_READY, NULL);
if (NULL == data->frm_grp) {
/* If buffer group is not set create one and limit it */
ret = mpp_buffer_group_get_internal(&data->frm_grp, MPP_BUFFER_TYPE_DRM);
if (ret) {
LOGD("%p get mpp buffer group failed ret %d ", ctx, ret);
break;
}
/* Set buffer to mpp decoder */
ret = mpi->control(ctx, MPP_DEC_SET_EXT_BUF_GROUP, data->frm_grp);
if (ret) {
LOGD("%p set buffer group failed ret %d ", ctx, ret);
break;
}
} else {
/* If old buffer group exist clear it */
ret = mpp_buffer_group_clear(data->frm_grp);
if (ret) {
LOGD("%p clear buffer group failed ret %d ", ctx, ret);
break;
}
}
/* Use limit config to limit buffer count to 24 with buf_size */
ret = mpp_buffer_group_limit_config(data->frm_grp, buf_size, 24);
if (ret) {
LOGD("%p limit buffer group failed ret %d ", ctx, ret);
break;
}
/*
* All buffer group config done. Set info change ready to let
* decoder continue decoding
*/
ret = mpi->control(ctx, MPP_DEC_SET_INFO_CHANGE_READY, NULL);
if (ret) {
LOGD("%p info change ready failed ret %d ", ctx, ret);
break;
}
this->last_frame_time_ms = GetCurrentTimeMS();
} else {
err_info = mpp_frame_get_errinfo(frame) | mpp_frame_get_discard(frame);
if (err_info) {
LOGD("decoder_get_frame get err info:%d discard:%d. ",
mpp_frame_get_errinfo(frame), mpp_frame_get_discard(frame));
}
data->frame_count++;
struct timeval tv;
gettimeofday(&tv, NULL);
LOGD("get one frame %ld ", (tv.tv_sec * 1000 + tv.tv_usec/1000));
// mpp_frame_get_width(frame);
// char *input_data =(char *) mpp_buffer_get_ptr(mpp_frame_get_buffer(frame));
if (callback != nullptr) {
MppFrameFormat format = mpp_frame_get_fmt(frame);
char *data_vir =(char *) mpp_buffer_get_ptr(mpp_frame_get_buffer(frame));
int fd = mpp_buffer_get_fd(mpp_frame_get_buffer(frame));
LOGD("data_vir=%p fd=%d ", data_vir, fd);
callback(this->userdata, hor_stride, ver_stride, hor_width, ver_height, format, fd, data_vir);
}
unsigned long cur_time_ms = GetCurrentTimeMS();
long time_gap = 1000/this->fps - (cur_time_ms - this->last_frame_time_ms);
LOGD("time_gap=%ld", time_gap);
if (time_gap > 0) {
usleep(time_gap * 1000);
}
this->last_frame_time_ms = GetCurrentTimeMS();
}
frm_eos = mpp_frame_get_eos(frame);
ret = mpp_frame_deinit(&frame);
frame = NULL;
// if(frame_pre!=NULL)
// {
// mpp_frame_deinit(&frame_pre);
// }
// &frame_pre=&frame;
get_frm = 1;
}
// try get runtime frame memory usage
if (data->frm_grp) {
size_t usage = mpp_buffer_group_usage(data->frm_grp);
if (usage > data->max_usage)
data->max_usage = usage;
}
// if last packet is send but last frame is not found continue
if (pkt_eos && pkt_done && !frm_eos) {
usleep(1*1000);
continue;
}
if (frm_eos) {
LOGD("found last frame ");
break;
}
if (data->frame_num > 0 && data->frame_count >= data->frame_num) {
data->eos = 1;
break;
}
if (get_frm)
continue;
break;
} while (1);
if (data->frame_num > 0 && data->frame_count >= data->frame_num) {
data->eos = 1;
LOGD("reach max frame number %d ", data->frame_count);
break;
}
if (pkt_done)
break;
/*
* why sleep here:
* mpi->decode_put_packet will failed when packet in internal queue is
* full,waiting the package is consumed .Usually hardware decode one
* frame which resolution is 1080p needs 2 ms,so here we sleep 3ms
* * is enough.
*/
usleep(3*1000);
} while (1);
mpp_packet_deinit(&packet);
return ret;
}
int MppDecoder::SetCallback(MppDecoderFrameCallback callback) {
this->callback = callback;
return 0;
} |