1 /*****************************************************************************
2 #                                                                            #
3 #    uStreamer - Lightweight and fast MJPG-HTTP streamer.                    #
4 #                                                                            #
5 #    Copyright (C) 2018-2021  Maxim Devaev <mdevaev@gmail.com>               #
6 #                                                                            #
7 #    This program is free software: you can redistribute it and/or modify    #
8 #    it under the terms of the GNU General Public License as published by    #
9 #    the Free Software Foundation, either version 3 of the License, or       #
10 #    (at your option) any later version.                                     #
11 #                                                                            #
12 #    This program is distributed in the hope that it will be useful,         #
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of          #
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
15 #    GNU General Public License for more details.                            #
16 #                                                                            #
17 #    You should have received a copy of the GNU General Public License       #
18 #    along with this program.  If not, see <https://www.gnu.org/licenses/>.  #
19 #                                                                            #
20 *****************************************************************************/
21 
22 
23 #pragma once
24 
25 #include <stdlib.h>
26 #include <stdbool.h>
27 #include <assert.h>
28 
29 #include <linux/videodev2.h>
30 
31 #include <interface/mmal/mmal.h>
32 #include <interface/mmal/mmal_format.h>
33 #include <interface/mmal/util/mmal_default_components.h>
34 #include <interface/mmal/util/mmal_component_wrapper.h>
35 #include <interface/mmal/util/mmal_util_params.h>
36 #include <interface/vcsm/user-vcsm.h>
37 
38 #include "../../libs/tools.h"
39 #include "../../libs/logging.h"
40 #include "../../libs/frame.h"
41 #include "../encoders/omx/vcos.h"
42 
43 
44 typedef struct {
45 	unsigned bitrate; // Kbit-per-sec
46 	unsigned gop; // Interval between keyframes
47 	unsigned fps;
48 
49 	MMAL_WRAPPER_T		*wrapper;
50 	MMAL_PORT_T			*input_port;
51 	MMAL_PORT_T			*output_port;
52 	VCOS_SEMAPHORE_T	handler_sem;
53 	bool				i_handler_sem;
54 
55 	int last_online;
56 
57 	unsigned	width;
58 	unsigned	height;
59 	unsigned	format;
60 	unsigned	stride;
61 	bool		zero_copy;
62 	bool		ready;
63 } h264_encoder_s;
64 
65 
66 h264_encoder_s *h264_encoder_init(unsigned bitrate, unsigned gop, unsigned fps);
67 void h264_encoder_destroy(h264_encoder_s *enc);
68 
69 bool h264_encoder_is_prepared_for(h264_encoder_s *enc, const frame_s *frame, bool zero_copy);
70 int h264_encoder_prepare(h264_encoder_s *enc, const frame_s *frame, bool zero_copy);
71 int h264_encoder_compress(h264_encoder_s *enc, const frame_s *src, int src_vcsm_handle, frame_s *dest, bool force_key);
72