- <div>#include <functional></div><div>#include <iostream></div><div>#include <pthread.h></div><div>#include <sys/syscall.h></div><div>#include <getopt.h></div><div>#include <chrono></div><div>#include <asm/types.h></div><div>#include <assert.h></div><div>#include <fcntl.h></div><div>#include <dirent.h></div><div>#include <malloc.h></div><div>#include <stdio.h></div><div>#include <stdlib.h></div><div>#include <string.h></div><div>#include <sys/ioctl.h></div><div>#include <sys/mman.h></div><div>#include <sys/stat.h></div><div>#include <sys/types.h></div><div>#include <time.h></div><div>#include <unistd.h></div><div>#include <linux/stddef.h></div><div>#include <linux/videodev2.h></div><div>#include <errno.h></div><div>
- </div><div>#include "rockchip_mpp.hpp"</div><div>#include <rockchip/rockchip_rga.h></div><div>#include "opencv2/opencv.hpp"</div><div>
- </div><div>#define BUFFER_WIDTH 1280</div><div>#define BUFFER_HEIGHT 720</div><div>#define BUFFER_SIZE BUFFER_WIDTH*BUFFER_HEIGHT*3</div><div>struct decWithFrame</div><div>{</div><div> mpp_decoder dec;</div><div> char* frameptr;</div><div>};</div><div>
- </div><div>using namespace std;</div><div>
- </div><div>unsigned long long nanoTime()</div><div>{</div><div> struct timespec t;</div><div> t.tv_sec = t.tv_nsec = 0;</div><div> clock_gettime(CLOCK_MONOTONIC, &t);</div><div> return (unsigned long long)(t.tv_sec) * 1000000000 + t.tv_nsec;</div><div>}</div><div>//get rgb by rga</div><div>void rga_copy(unsigned char* srcBuffer, unsigned char* dstBuffer, RockchipRga* mRga)</div><div>{</div><div> //mRga = (RockchipRga*)calloc(1, sizeof(*mRga));</div><div> if (!mRga)</div><div> {</div><div> printf("create rga failed !\n");</div><div> abort();</div><div> }</div><div>
- </div><div> mRga->ops->initCtx(mRga);</div><div> mRga->ops->setSrcFormat(mRga, V4L2_PIX_FMT_NV12, BUFFER_WIDTH, BUFFER_HEIGHT);</div><div> mRga->ops->setDstFormat(mRga, V4L2_PIX_FMT_BGR24, BUFFER_WIDTH, BUFFER_HEIGHT);</div><div> mRga->ops->setSrcBufferPtr(mRga, srcBuffer);</div><div> mRga->ops->setDstBufferPtr(mRga, dstBuffer);</div><div> mRga->ops->go(mRga);</div><div>}</div><div>DecFrame* mpp_get_frame(mpp_decoder* dec)</div><div>{</div><div> int retry = 20;</div><div> DecFrame* frame = NULL;</div><div>
- </div><div> while (retry--)</div><div> {</div><div> /* TODO: Dequeue and get decoded frame */</div><div> frame = dec->dequeue();</div><div>
- </div><div> if (frame)</div><div> {</div><div> return frame;</div><div> }</div><div>
- </div><div> usleep(1000);</div><div> }</div><div>
- </div><div> return NULL;</div><div>}</div><div>
- </div><div>int mpp_decode(FILE* in, FILE* out)</div><div>{</div><div> int ret = 0;</div><div> int count = 0;</div><div> size_t file_size;</div><div> DecFrame* frame = NULL;</div><div> mpp_decoder dec;</div><div> void* buf = NULL;</div><div> fseek(in, 0L, SEEK_END);</div><div> file_size = ftell(in);</div><div> rewind(in);</div><div> buf = malloc(file_size);</div><div>
- </div><div> if (!buf)</div><div> {</div><div> return -ENOMEM;</div><div> }</div><div>
- </div><div> fread(buf, 1, file_size, in);</div><div> /* TODO: Init decoder context */</div><div> ret = dec.init(DECODE_TYPE_H264);</div><div>
- </div><div> if (ret < 0)</div><div> {</div><div> return ret;</div><div> }</div><div>
- </div><div> for (int i = 0; i < 4; i++)</div><div> /* TODO: Enqueue data */</div><div> {</div><div> dec.enqueue((char*)buf + i * (file_size / 4), file_size / 4);</div><div> }</div><div>
- </div><div> //libo</div><div> decWithFrame* decPtr_ = new decWithFrame;</div><div> decPtr_->frameptr = new char[BUFFER_SIZE];</div><div> printf("check1\n");</div><div> memset(decPtr_->frameptr, 0, BUFFER_SIZE);</div><div> printf("check2\n");</div><div> int framenum = 0;</div><div> RockchipRga* mRga = RgaCreate();</div><div>
- </div><div> while (1)</div><div> {</div><div> framenum++;</div><div> /* TODO: Get decoded frame */</div><div> frame = mpp_get_frame(&dec);</div><div>
- </div><div> if (frame == NULL)</div><div> {</div><div> break;</div><div> }</div><div>
- </div><div> printf("Get frame %d: width %llu, height %llu, size %llu\n", count++, frame->width, frame->height, frame->size);</div><div> //fwrite(frame->data, frame->size, 1, out);</div><div> //rga_copy((unsigned char*)frame->data, (unsigned char*)decPtr_->frameptr, mRga);</div><div> //cv::Mat m(720, 1280, CV_8UC3, decPtr_->frameptr);</div><div> // if (framenum == 355)</div><div> // {</div><div> // cv::imwrite("./355.jpg", m);</div><div> // }</div><div> // if (framenum == 200)</div><div> // {</div><div> // cv::imwrite("./200.jpg", m);</div><div> // }</div><div> // if (framenum == 1)</div><div> // {</div><div> // cv::imwrite("./1.jpg", m);</div><div> // }</div><div> /* TODO: Free frame */</div><div> dec.freeFrame(frame);</div><div> }</div><div>
- </div><div> RgaDestroy(mRga);</div><div> delete[] decPtr_->frameptr;</div><div> decPtr_->frameptr = NULL;</div><div> delete decPtr_;</div><div> decPtr_ = NULL;</div><div> return ret;</div><div>}</div><div>
- </div><div>int main(int argc, char** argv)</div><div>{</div><div> int ret;</div><div> /* Input h264 file */</div><div> FILE* in = fopen("test.h264", "r");</div><div> /* Output nv12 file */</div><div> FILE* out = fopen("test.nv12", "w+");</div><div>
- </div><div> if (in == NULL || out == NULL)</div><div> {</div><div> fprintf(stderr, "Open file failed\n");</div><div> return -EINVAL;</div><div> }</div><div>
- </div><div> ret = mpp_decode(in, out);</div><div> ret = mpp_decode(in, out);</div><div> ret = mpp_decode(in, out);</div><div> ret = mpp_decode(in, out);</div><div>exit:</div><div>
- </div><div> if (in != NULL)</div><div> {</div><div> fclose(in);</div><div> }</div><div>
- </div><div> if (out != NULL)</div><div> {</div><div> fclose(out);</div><div> }</div><div>
- </div><div> return ret;</div><div>}</div>
复制代码
欢迎光临 Toybrick (https://t.rock-chips.com/) | Powered by Discuz! X3.3 |