#define WIDTH 320 #define HEIGHT 240 AVFrame *input_frame = avcodec_alloc_frame(); r = avpicture_alloc((AVPicture *) input_frame, PIX_FMT_GRAY8, WIDTH, HEIGHT); AVFrame *omx_input_frame = avcodec_alloc_frame(); r = avpicture_alloc((AVPicture *) omx_input_frame, PIX_FMT_YUV420P, WIDTH, HEIGHT); SwsContext *img_convert_ctx = sws_getContext(WIDTH, HEIGHT, PIX_FMT_GRAY8, WIDTH, HEIGHT, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
avpicture_fill ((AVPicture *) input_frame, (uint8_t *) frame->imageData, PIX_FMT_GRAY8, WIDTH, HEIGHT); buf->nFilledLen = avpicture_fill ((AVPicture *) omx_input_frame, buf->pBuffer, PIX_FMT_YUV420P, WIDTH, HEIGHT); sws_scale(img_convert_ctx, (const uint8_t* const*)input_frame->data, input_frame->linesize, 0, HEIGHT, omx_input_frame->data, omx_input_frame->linesize);
OMX_VIDEO_PARAM_PORTFORMATTYPE format; OMX_PARAM_PORTDEFINITIONTYPE def; COMPONENT_T *video_encode; ILCLIENT_T *client; OMX_BUFFERHEADERTYPE *buf; // OMX_BUFFERHEADERTYPE *out; // int r = 0; #define VIDEO_ENCODE_PORT_IN 200 #define VIDEO_ENCODE_PORT_OUT 201 #define BITRATE 400000 #define FPS 25 bcm_host_init(); client = ilclient_init(); OMX_Init(); ilclient_create_component(client, &video_encode, "video_encode", (ILCLIENT_CREATE_FLAGS_T)(ILCLIENT_DISABLE_ALL_PORTS | ILCLIENT_ENABLE_INPUT_BUFFERS | ILCLIENT_ENABLE_OUTPUT_BUFFERS)); memset(&def, 0, sizeof(OMX_PARAM_PORTDEFINITIONTYPE)); def.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE); def.nVersion.nVersion = OMX_VERSION; def.nPortIndex = VIDEO_ENCODE_PORT_IN; OMX_GetParameter(ILC_GET_HANDLE(video_encode), OMX_IndexParamPortDefinition, &def); def.format.video.nFrameWidth = WIDTH; def.format.video.nFrameHeight = HEIGHT; def.format.video.xFramerate = FPS << 16; def.format.video.nSliceHeight = def.format.video.nFrameHeight; def.format.video.nStride = def.format.video.nFrameWidth; def.format.video.eColorFormat = OMX_COLOR_FormatYUV420PackedPlanar; r = OMX_SetParameter(ILC_GET_HANDLE(video_encode), OMX_IndexParamPortDefinition, &def);
memset(&format, 0, sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE)); format.nSize = sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE); format.nVersion.nVersion = OMX_VERSION; format.nPortIndex = VIDEO_ENCODE_PORT_OUT; format.eCompressionFormat = OMX_VIDEO_CodingAVC; r = OMX_SetParameter(ILC_GET_HANDLE(video_encode), OMX_IndexParamVideoPortFormat, &format); OMX_VIDEO_PARAM_BITRATETYPE bitrateType; memset(&bitrateType, 0, sizeof(OMX_VIDEO_PARAM_BITRATETYPE)); bitrateType.nSize = sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE); bitrateType.nVersion.nVersion = OMX_VERSION; bitrateType.eControlRate = OMX_Video_ControlRateVariable; bitrateType.nTargetBitrate = BITRATE; bitrateType.nPortIndex = VIDEO_ENCODE_PORT_OUT; r = OMX_SetParameter(ILC_GET_HANDLE(video_encode), OMX_IndexParamVideoBitrate, &bitrateType); ilclient_change_component_state(video_encode, OMX_StateIdle);
ilclient_enable_port_buffers(video_encode, VIDEO_ENCODE_PORT_IN, NULL, NULL, NULL); ilclient_enable_port_buffers(video_encode, VIDEO_ENCODE_PORT_OUT, NULL, NULL, NULL); ilclient_change_component_state(video_encode, OMX_StateExecuting);
buf = ilclient_get_input_buffer(video_encode, VIDEO_ENCODE_PORT_IN, 1); OMX_EmptyThisBuffer(ILC_GET_HANDLE(video_encode), buf); out = ilclient_get_output_buffer(video_encode, VIDEO_ENCODE_PORT_OUT, 1); OMX_FillThisBuffer(ILC_GET_HANDLE(video_encode), out);
AVCodecContext *cc; char *out_file_name; // .avi AVOutputFormat *fmt; AVFormatContext *oc; AVStream *video_st; av_register_all(); fmt = av_guess_format(NULL, out_file_name, NULL); oc = avformat_alloc_context(); oc->debug = 1; oc->start_time_realtime = AV_NOPTS_VALUE; oc->start_time = AV_NOPTS_VALUE; oc->duration = 0; oc->bit_rate = 0; oc->oformat = fmt; snprintf(oc->filename, sizeof(out_file_name), "%s", out_file_name); video_st = avformat_new_stream(oc, NULL); cc = video_st->codec; cc->width = WIDTH; cc->height = HEIGHT; cc->codec_id = CODEC_ID_H264; cc->codec_type = AVMEDIA_TYPE_VIDEO; cc->bit_rate = BITRATE; cc->profile = FF_PROFILE_H264_HIGH; cc->level = 41; cc->time_base.den = FPS; cc->time_base.num = 1; video_st->time_base.den = FPS; video_st->time_base.num = 1; video_st->r_frame_rate.num = FPS; video_st->r_frame_rate.den = 1; video_st->start_time = AV_NOPTS_VALUE; cc->sample_aspect_ratio.num = video_st->sample_aspect_ratio.num; cc->sample_aspect_ratio.den = video_st->sample_aspect_ratio.den;
avio_open(&oc->pb, out_file_name, URL_WRONLY); avformat_write_header(oc, NULL); if (oc->oformat->flags & AVFMT_GLOBALHEADER) oc->streams[0]->codec->flags |= CODEC_FLAG_GLOBAL_HEADER; av_dump_format(oc, 0, out_file_name, 1);
AVPacket pkt; AVRational omxtimebase = { 1, FPS}; OMX_TICKS tick = out->nTimeStamp; av_init_packet(&pkt); pkt.stream_index = video_st->index; pkt.data= out->pBuffer; pkt.size= out->nFilledLen; if (out->nFlags & OMX_BUFFERFLAG_SYNCFRAME) pkt.flags |= AV_PKT_FLAG_KEY; pkt.pts = av_rescale_q(((((uint64_t)tick.nHighPart)<<32) | tick.nLowPart), omxtimebase, oc->streams[video_st->index]->time_base); pkt.dts = AV_NOPTS_VALUE; av_write_frame(oc, &pkt); out->nFilledLen = 0;
#include "opencv2/core/core_c.h" #include "opencv2/imgproc/imgproc_c.h" #include "libavcodec/avcodec.h" #include "libavformat/avformat.h" #include "libswscale/swscale.h" #include "libavutil/opt.h" #include "libavutil/avutil.h" #include "libavutil/mathematics.h" #include "libavformat/avio.h" #include "bcm_host.h" #include "ilclient.h"
-L/opt/vc/lib -lbcm_host -lopenmaxil -lbcm_host -lvcos -lvchiq_arm –lpthread
-L/usr/local/lib -lavcodec -lavformat -lavutil -lswscale \ -L/usr/local/lib -lopencv_imgproc -lopencv_core
Source: https://habr.com/ru/post/207314/
All Articles