Skip to content

Commit 130345b

Browse files
committed
Add ffmpeg source
1 parent 69a819c commit 130345b

6 files changed

+628
-4
lines changed

Makefile

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
CC=g++
22
LD=g++
33

4-
CFLAGS = -std=c++11 -Wno-multichar -Wall -Wno-format -g -I/opt/vc/include/IL -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -DSTANDALONE -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -DTARGET_POSIX -D_LINUX -D_REENTRANT -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -U_FORTIFY_SOURCE -DHAVE_LIBOPENMAX=2 -DOMX -DOMX_SKIP64BIT -ftree-vectorize -pipe -DUSE_EXTERNAL_OMX -DHAVE_LIBBCM_HOST -DUSE_EXTERNAL_LIBBCM_HOST -DUSE_VCHIQ_ARM -L/usr/local/lib -I/usr/local/include -O3
4+
CFLAGS = -std=c++11 -Wno-multichar -Wall -Wno-format -g -I/opt/vc/include/IL -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -Ilibyuv/include -DSTANDALONE -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -DTARGET_POSIX -D_LINUX -D_REENTRANT -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -U_FORTIFY_SOURCE -DHAVE_LIBOPENMAX=2 -DOMX -DOMX_SKIP64BIT -ftree-vectorize -pipe -DUSE_EXTERNAL_OMX -DHAVE_LIBBCM_HOST -DUSE_EXTERNAL_LIBBCM_HOST -DUSE_VCHIQ_ARM -L/usr/local/lib -I/usr/local/include -O3
55

66
LDFLAGS = -Xlinker -R/opt/vc/lib -L/opt/vc/lib/ -Xlinker -L/usr/local/lib -Xlinker -R/usr/local/lib # -Xlinker --verbose
77
#LIBS = -L/opt/vc/lib -rdynamic -lopenmaxil -lvcos -lbcm_host -lpthread -L/libmpegts/libmpegts.o -lm
8-
LIBS = -lopenmaxil -lbcm_host -lvcos -lpthread -lm -lrt -lvncclient
8+
LIBS = -lopenmaxil -lbcm_host -lvcos -lpthread -lm -lrt -lvncclient -ljpeg
99

10-
OFILES = vncclient.o grabdisplay.o avc2ts.o webcam.o ./libmpegts/libmpegts.a
10+
CFLAGS+=`pkg-config --cflags libavcodec libavformat libswscale libavutil libavdevice`
11+
LDFLAGS+=`pkg-config --libs libavcodec libavformat libswscale libavutil libavdevice`
12+
13+
OFILES = vncclient.o grabdisplay.o avc2ts.o webcam.o ffmpegsrc.o ./libmpegts/libmpegts.a libyuv/libyuv.a
1114

1215
.PHONY: all clean install dist
1316

ffmpegsrc.cpp

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
2+
using namespace std;
3+
#include "ffmpegsrc.h"
4+
5+
#include <unistd.h>
6+
7+
8+
ffmpegsrc::ffmpegsrc(const string& source) :
9+
source(source)
10+
{
11+
open_device();
12+
13+
}
14+
15+
ffmpegsrc::~ffmpegsrc()
16+
{
17+
close_device();
18+
}
19+
void ffmpegsrc::open_device(void)
20+
{
21+
22+
AVCodec *codec = NULL;
23+
24+
25+
int i;
26+
27+
av_register_all();
28+
avdevice_register_all();
29+
avformat_network_init();
30+
31+
//AVInputFormat *inputFormat =av_find_input_format("v4l2");
32+
33+
/* Open the video */
34+
if(avformat_open_input(&format_ctx, source.c_str(), NULL/*inputFormat*/, NULL) < 0)
35+
{
36+
fprintf(stderr, "Error opening file '%s'\n", source.c_str());
37+
return ;
38+
}
39+
40+
/* Read stream info from the file */
41+
if(avformat_find_stream_info(format_ctx, NULL) < 0)
42+
{
43+
fprintf(stderr, "Error reading stream information from file\n");
44+
45+
}
46+
47+
/* Dump some useful information to stderr */
48+
fprintf(stderr, "Opening '%s'...\n", source.c_str());
49+
av_dump_format(format_ctx, 0, source.c_str(), 0);
50+
51+
/* Find the first video stream */
52+
for(i = 0; i < format_ctx->nb_streams; i++)
53+
{
54+
if(format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
55+
{
56+
break;
57+
}
58+
}
59+
60+
if(i == format_ctx->nb_streams)
61+
{
62+
fprintf(stderr, "No video streams found\n");
63+
return ;
64+
}
65+
66+
video_stream = i;
67+
68+
/* Get a pointer to the codec context for the video stream */
69+
codec_ctx = format_ctx->streams[i]->codec;
70+
71+
/* Find the decoder for the video stream */
72+
codec = avcodec_find_decoder(codec_ctx->codec_id);
73+
if(codec == NULL)
74+
{
75+
fprintf(stderr, "Unsupported video codec\n");
76+
return ;
77+
}
78+
79+
/* Open codec */
80+
if(avcodec_open2(codec_ctx, codec, NULL) < 0)
81+
{
82+
fprintf(stderr, "Error opening video codec\n");
83+
return ;
84+
}
85+
framey420 = av_frame_alloc();
86+
/* Allocate video frame */
87+
frame = av_frame_alloc();
88+
len = avpicture_get_size(codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height);
89+
fprintf(stderr, "ffmpeg YUV should %d\n",len);
90+
91+
return;
92+
}
93+
94+
void ffmpegsrc::close_device()
95+
{
96+
97+
98+
av_free(framey420);
99+
av_free(frame);
100+
avformat_close_input(&format_ctx);
101+
102+
103+
return ;
104+
}
105+
106+
void ffmpegsrc::SetOmxBuffer(unsigned char* Buffer)
107+
{
108+
OmxBuffer=Buffer;
109+
/* Assign appropriate parts of buffer to image planes in frame_rgb
110+
* Note that frame_rgb is an AVFrame, but AVFrame is a superset
111+
* of AVPicture */
112+
avpicture_fill((AVPicture *) framey420, Buffer, AV_PIX_FMT_YUV420P, codec_ctx->width, codec_ctx->height);
113+
114+
/* initialize SWS context for software scaling */
115+
sws_ctx = sws_getContext(
116+
codec_ctx->width,
117+
codec_ctx->height,
118+
codec_ctx->pix_fmt,
119+
codec_ctx->width,
120+
codec_ctx->height,
121+
AV_PIX_FMT_YUV420P,
122+
SWS_BICUBIC,
123+
NULL,
124+
NULL,
125+
NULL
126+
);
127+
}
128+
129+
void ffmpegsrc::GetVideoSize(int& Width,int& Height)
130+
{
131+
Width=codec_ctx->width;
132+
Height=codec_ctx->height;
133+
}
134+
135+
bool ffmpegsrc::read_frame(int timeout=1)
136+
{
137+
138+
AVPacket packet;
139+
//uint32_t *frame = NULL;
140+
int i=0;
141+
int ret;
142+
while(i==0)
143+
{
144+
145+
while((ret=av_read_frame(format_ctx, &packet)) >= 0)
146+
{
147+
/* Only handle frames from the video stream */
148+
if(packet.stream_index == video_stream)
149+
{
150+
/* Decode video frame */
151+
avcodec_decode_video2(codec_ctx,frame, &i, &packet);
152+
153+
if(i)
154+
{
155+
sws_scale(
156+
sws_ctx,
157+
(uint8_t const * const *) frame->data,
158+
frame->linesize,
159+
0,
160+
codec_ctx->height,
161+
framey420->data,
162+
framey420->linesize
163+
);
164+
// memcpy(OmxBuffer,frame->data[0],len);
165+
av_free_packet(&packet);
166+
//printf("Frame %d\n",i);
167+
break;
168+
}
169+
}
170+
171+
av_free_packet(&packet);
172+
173+
}
174+
if(ret<0) break;
175+
}
176+
177+
return (ret>=0);
178+
}

ffmpegsrc.h

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
#include <string>
3+
#include <memory> // unique_ptr
4+
extern "C"
5+
{
6+
#include <libavcodec/avcodec.h>
7+
#include <libavformat/avformat.h>
8+
#include <libswscale/swscale.h>
9+
#include <libavdevice/avdevice.h>
10+
11+
}
12+
13+
14+
class ffmpegsrc {
15+
16+
public:
17+
ffmpegsrc(const std::string& source = "/dev/video0");
18+
19+
~ffmpegsrc();
20+
21+
void GetVideoSize(int& Width,int& Height);
22+
23+
void SetOmxBuffer(unsigned char* Buffer);
24+
bool read_frame(int timeout);
25+
26+
27+
private:
28+
29+
void open_device();
30+
void close_device();
31+
32+
33+
34+
std::string source;
35+
36+
AVFormatContext *format_ctx;
37+
AVCodecContext *codec_ctx;
38+
struct SwsContext *sws_ctx;
39+
AVFrame *frame;
40+
AVFrame *framey420;
41+
uint8_t *OmxBuffer = NULL;
42+
int video_stream;
43+
int len;
44+
};

information.txt

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,47 @@
11
For debugging :
2-
vcgencmd set_logging level=204
2+
vcgencmd set_logging level=8
33
and
44
sudo vcdbg log msg
55
sudo vcdbg log assert
6+
7+
sudo vcdbg log msg 2>&1 | grep BO
8+
9+
/* Bitfield for indicating the category and level of a logging message. */
10+
#define LOGGING_GENERAL (1<<0) /* for logging general messages */
11+
#define LOGGING_GENERAL_VERBOSE (2<<0)
12+
#define LOGGING_CODECS (1<<2) /* for codec messages */
13+
#define LOGGING_CODECS_VERBOSE (2<<2)
14+
#define LOGGING_FILESYSTEM (1<<4) /* filesystem messages */
15+
#define LOGGING_FILESYSTEM_VERBOSE (2<<4)
16+
#define LOGGING_VMCS (1<<6) /* VMCS related messages */
17+
#define LOGGING_VMCS_VERBOSE (2<<6)
18+
#define LOGGING_DISPMAN2 (1<<8) /* Dispman2/scalar logs */
19+
#define LOGGING_DISPMAN2_VERBOSE (2<<8)
20+
#define LOGGING_GCE (1<<8) /* Re-use Dispman2 for GCE logging */
21+
#define LOGGING_GCE_VERBOSE (2<<8)
22+
#define LOGGING_CAMPLUS (1<<10) /* Camplus logs */
23+
#define LOGGING_CAMPLUS_VERBOSE (2<<10)
24+
#define LOGGING_APPS (1<<12) /* Application logs */
25+
#define LOGGING_APPS_VERBOSE (2<<12)
26+
#define LOGGING_CLOCKMAN_POWERMAN (1<<14) /* Clockman + powerman logs */
27+
#define LOGGING_CLOCKMAN_POWERMAN_VERBOSE (2<<14)
28+
#define LOGGING_VCOS (1<<16)
29+
#define LOGGING_VCOS_VERBOSE (2<<16)
30+
#define LOGGING_IMAGE_POOL (1<<18) /* Image pool messages */
31+
#define LOGGING_IMAGE_POOL_VERBOSE (2<<18)
32+
#define LOGGING_HDMI (1<<20) /* HDMI and HDCP messages */
33+
#define LOGGING_HDMI_VERBOSE (2<<20)
34+
#define LOGGING_MINIMAL (1<<22) /* minimal logging for bandwidth measurement, ie all others off. */
35+
#define LOGGING_MINIMAL_VERBOSE (2<<22)
36+
#define LOGGING_TUNER (1<<24) /* ISP Tuner logs - AGC, AWB etc */
37+
#define LOGGING_TUNER_VERBOSE (2<<24)
38+
#define LOGGING_VCHI (1<<26) /* For all VCHI based services */
39+
#define LOGGING_VCHI_VERBOSE (2<<26)
40+
#define LOGGING_FOCUS (1<<28) /* Focus messages */
41+
#define LOGGING_HANDLERS (1<<29) /* For handler messages */
42+
#define LOGGING_VOWIFI (1<<28) /* Re-use FOCUS for VOWIFI */
43+
#define LOGGING_VOWIFI_VERBOSE (2<<28) /* Re-use HANDLERS for VOWIFI */
44+
// add more here
45+
#define LOGGING_USER (1<<30) /* only for code under development - do not check in! */
46+
#define LOGGING_USER_VERBOSE (2<<30)
47+

preinstall.sh

+16
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,19 @@ cd libmpegts
33
./configure
44
make
55
cd ../
6+
7+
git clone https://chromium.googlesource.com/libyuv/libyuv
8+
cd libyuv
9+
#should patch linux.mk with -DHAVE_JPEG on CXX and CFLAGS
10+
#seems to be link with libjpeg9-dev
11+
make V=1 -f linux.mk
12+
cd ../
13+
14+
# Required for ffmpegsrc.cpp
15+
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libavdevice-dev
16+
17+
# For transcoding youtube
18+
curl -L https://yt-dl.org/downloads/latest/youtube-dl -o youtube-dl
19+
chmod +x youtube-dl
20+
# Example : ./youtube-dl -o - https://www.youtube.com/watch?v=gOlqWk3E7Sc | ./avc2ts -b 2100000 -m 2530000 -x 640 -y 480 -f 25 -n 230.0.0.1:10000 -t 5 -i 25 -d 140 -e /dev/stdin -v
21+

0 commit comments

Comments
 (0)