Skip to content

Commit 7d038d9

Browse files
committed
fill AVCodecContext with Header Infomation from rkmpp
1 parent 31fd7c6 commit 7d038d9

File tree

5 files changed

+116
-170
lines changed

5 files changed

+116
-170
lines changed

mpp.c

Lines changed: 68 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "mpp.h"
22
static void mpp_close(MppContext* ctx);
33
static void init_mpp(MppContext *mpp_enc_data);
4-
static _Bool write_header(MppContext *mpp_enc_data);
4+
static _Bool write_header(MppContext *mpp_enc_data,SpsHeader *sps_header);
55
static _Bool process_image(uint8_t *p, int size,MppContext *mpp_enc_data);
66

77
MppContext * alloc_mpp_context()
@@ -213,7 +213,7 @@ static void init_mpp(MppContext *mpp_enc_data)
213213
printf("init mpp failed!\n");
214214
}
215215

216-
static _Bool write_header(MppContext *mpp_enc_data)
216+
static _Bool write_header(MppContext *mpp_enc_data,SpsHeader *sps_header)
217217
{
218218
int ret;
219219
if (mpp_enc_data->type == MPP_VIDEO_CodingAVC)
@@ -229,105 +229,85 @@ static _Bool write_header(MppContext *mpp_enc_data)
229229
/* get and write sps/pps for H.264 */
230230
if (packet)
231231
{
232-
233232
void *ptr = mpp_packet_get_pos(packet);
234233
size_t len = mpp_packet_get_length(packet);
235-
236-
if (mpp_enc_data->fp_output)
237-
{
238-
fwrite(ptr, 1, len, mpp_enc_data->fp_output);
239-
return 0;
240-
}
241-
// if(mpp_enc_data->write_frame)
242-
// if(!(mpp_enc_data->write_frame)(ptr,len))
243-
// printf("------------sendok!\n");
234+
sps_header->data = (uint8_t*)malloc(len);
235+
sps_header->size = len;
236+
memcpy(sps_header->data,ptr,len);
244237
packet = NULL;
245238
}
246239
}
247240
return 1;
248241
}
249242

250243
static _Bool process_image(uint8_t *p, int size,MppContext *mpp_enc_data)
251-
{
252-
// if(mpp_enc_data->fp_outputx)
253-
// {
254-
// printf("ok\n");
255-
// //fwrite(p, 1, size, mpp_enc_data.fp_outputx);
256-
// }
257-
244+
{
258245
MPP_RET ret = MPP_OK;
259-
260-
246+
MppFrame frame = NULL;
247+
MppPacket packet = NULL;
261248

262-
MppFrame frame = NULL;
263-
MppPacket packet = NULL;
264-
265-
void *buf = mpp_buffer_get_ptr(mpp_enc_data->frm_buf);
266-
//TODO: improve performance here?
267-
memcpy(buf, p, size);
268-
ret = mpp_frame_init(&frame);
269-
if (ret)
270-
{
271-
printf("mpp_frame_init failed\n");
272-
return 1;
273-
}
249+
void *buf = mpp_buffer_get_ptr(mpp_enc_data->frm_buf);
250+
//TODO: improve performance here?
251+
memcpy(buf, p, size);
252+
ret = mpp_frame_init(&frame);
253+
if (ret)
254+
{
255+
printf("mpp_frame_init failed\n");
256+
return 1;
257+
}
274258

275-
mpp_frame_set_width(frame, mpp_enc_data->width);
276-
mpp_frame_set_height(frame, mpp_enc_data->height);
277-
mpp_frame_set_hor_stride(frame, mpp_enc_data->hor_stride);
278-
mpp_frame_set_ver_stride(frame, mpp_enc_data->ver_stride);
279-
mpp_frame_set_fmt(frame, mpp_enc_data->fmt);
280-
mpp_frame_set_buffer(frame, mpp_enc_data->frm_buf);
281-
mpp_frame_set_eos(frame, mpp_enc_data->frm_eos);
259+
mpp_frame_set_width(frame, mpp_enc_data->width);
260+
mpp_frame_set_height(frame, mpp_enc_data->height);
261+
mpp_frame_set_hor_stride(frame, mpp_enc_data->hor_stride);
262+
mpp_frame_set_ver_stride(frame, mpp_enc_data->ver_stride);
263+
mpp_frame_set_fmt(frame, mpp_enc_data->fmt);
264+
mpp_frame_set_buffer(frame, mpp_enc_data->frm_buf);
265+
mpp_frame_set_eos(frame, mpp_enc_data->frm_eos);
282266

283-
ret = mpp_enc_data->mpi->encode_put_frame(mpp_enc_data->ctx, frame);
284-
if (ret)
285-
{
286-
printf("mpp encode put frame failed\n");
287-
return 1;
288-
}
267+
ret = mpp_enc_data->mpi->encode_put_frame(mpp_enc_data->ctx, frame);
268+
if (ret)
269+
{
270+
printf("mpp encode put frame failed\n");
271+
return 1;
272+
}
289273

290274
mdddd:
291-
ret = mpp_enc_data->mpi->encode_get_packet(mpp_enc_data->ctx, &packet);
292-
if (ret)
293-
{
294-
printf("mpp encode get packet failed\n");
295-
return 1;
296-
}
297-
298-
if (packet)
299-
{
300-
// write packet to file here
301-
void *ptr = mpp_packet_get_pos(packet);
302-
size_t len = mpp_packet_get_length(packet);
303-
mpp_enc_data->pkt_eos = mpp_packet_get_eos(packet);
304-
if (mpp_enc_data->fp_output)
305-
{
306-
fwrite(ptr, 1, len, mpp_enc_data->fp_output);
307-
}
308-
if(!mpp_enc_data->fp_output&&mpp_enc_data->write_frame)
309-
if(!(mpp_enc_data->write_frame)(ptr,len))
310-
printf("------------sendok!\n");
311-
312-
mpp_packet_deinit(&packet);
313-
printf("encoded frame %d size %ld\n", mpp_enc_data->frame_count, len);
314-
mpp_enc_data->stream_size += len;
315-
mpp_enc_data->frame_count++;
316-
317-
if (mpp_enc_data->pkt_eos)
318-
{
319-
printf("found last packet\n");
320-
}
321-
}
322-
else
323-
goto mdddd;
324-
if (mpp_enc_data->num_frames && mpp_enc_data->frame_count >= mpp_enc_data->num_frames)
325-
{
326-
printf("encode max %d frames", mpp_enc_data->frame_count);
327-
return 0;
328-
}
329-
if (mpp_enc_data->frm_eos && mpp_enc_data->pkt_eos)
330-
return 0;
331-
332-
return 1;
275+
ret = mpp_enc_data->mpi->encode_get_packet(mpp_enc_data->ctx, &packet);
276+
if (ret)
277+
{
278+
printf("mpp encode get packet failed\n");
279+
return 1;
280+
}
281+
282+
if (packet)
283+
{
284+
// write packet to file here
285+
void *ptr = mpp_packet_get_pos(packet);
286+
size_t len = mpp_packet_get_length(packet);
287+
mpp_enc_data->pkt_eos = mpp_packet_get_eos(packet);
288+
if(mpp_enc_data->write_frame)
289+
if(!(mpp_enc_data->write_frame)(ptr,len))
290+
printf("------------sendok!\n");
291+
292+
mpp_packet_deinit(&packet);
293+
printf("encoded frame %d size %ld\n", mpp_enc_data->frame_count, len);
294+
mpp_enc_data->stream_size += len;
295+
mpp_enc_data->frame_count++;
296+
297+
if (mpp_enc_data->pkt_eos)
298+
{
299+
printf("found last packet\n");
300+
}
301+
}
302+
else
303+
goto mdddd;
304+
if (mpp_enc_data->num_frames && mpp_enc_data->frame_count >= mpp_enc_data->num_frames)
305+
{
306+
printf("encode max %d frames", mpp_enc_data->frame_count);
307+
return 0;
308+
}
309+
if (mpp_enc_data->frm_eos && mpp_enc_data->pkt_eos)
310+
return 0;
311+
312+
return 1;
333313
}

mpp.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919

2020
#define MPP_ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
2121

22+
typedef struct {
23+
uint8_t *data;
24+
uint32_t size;
25+
} SpsHeader;
26+
2227
typedef struct {
2328
// global flow control flag
2429
RK_U32 frm_eos;
@@ -62,7 +67,7 @@ typedef struct {
6267
//function pointer
6368
void (*init_mpp)(void *mpp_enc_data);
6469
_Bool (*process_image)(uint8_t *p, int size,void *mpp_enc_data);
65-
_Bool (*write_header)(void *mpp_enc_data);
70+
_Bool (*write_header)(void *mpp_enc_data,SpsHeader *sps_header);
6671
void (*close)(void* ctx);
6772

6873
} MppContext;

rtmp.c

Lines changed: 38 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,8 @@
77
#include "rtmp.h"
88

99
static AVPacket pkt;
10-
static char *outfile_name = "rtmp://192.168.1.165:1935/live/room1";
11-
static AVOutputFormat *ofmt = NULL;
1210
static AVFormatContext *ofmt_ctx = NULL;
13-
static AVFormatContext *ifmt_ctx = NULL;
14-
static int ret;
15-
static int i;
16-
static int videoindex=0;
1711
static int frame_index=0;
18-
static int64_t start_time=0;
19-
static char *infile_name = "/tmp/output.h264";
20-
static int ceshi;
2112

2213
int write_frame(uint8_t*data,int size)
2314
{
@@ -29,49 +20,31 @@ int write_frame(uint8_t*data,int size)
2920
// pkt.dts = frame_index*pkt.duration;
3021
// pkt.pts = pkt.dts;
3122
// frame_index ++;
32-
ret = av_write_frame(ofmt_ctx, &pkt);
33-
34-
if (ret < 0) {
23+
24+
if (av_write_frame(ofmt_ctx, &pkt) < 0) {
3525
printf( "Error muxing packet\n");
3626
return -1;
3727
}
3828
return 0;
3929
}
4030

41-
42-
43-
int init_rtmp_streamer(char* stream)
31+
int init_rtmp_streamer(char* stream,uint8_t *data,uint32_t size)
4432
{
4533
int ret;
4634
av_register_all();
47-
outfile_name=stream;
4835
if((ret = avformat_network_init()) < 0)
4936
{
5037
fprintf(stderr, "avformat_network_init failed!");
5138
return -1;
5239
}
5340

54-
avformat_alloc_output_context2(&ofmt_ctx,NULL,"flv",outfile_name);
41+
avformat_alloc_output_context2(&ofmt_ctx,NULL,"flv",stream);
5542
if(!ofmt_ctx)
5643
{
5744
fprintf(stderr, "Could not create output context\n");
5845
return -1;
5946
}
60-
ofmt = ofmt_ctx->oformat;
61-
62-
if((ret = avformat_open_input(&ifmt_ctx,infile_name,0,0)) < 0)
63-
{
64-
fprintf(stderr, "Could not open input file '%s' %d", infile_name,ret);
65-
return -1;
66-
}
67-
68-
if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
69-
fprintf(stderr, "Failed to retrieve input stream information");
70-
return -1;
71-
}
72-
av_dump_format(ifmt_ctx, 0, infile_name, 0);
73-
74-
47+
7548

7649
AVStream *out_stream = avformat_new_stream(ofmt_ctx,NULL);
7750
if(! out_stream)
@@ -80,43 +53,41 @@ outfile_name=stream;
8053
goto end;
8154
}
8255

83-
ret = avcodec_copy_context(out_stream->codec,ifmt_ctx->streams[0]->codec);
84-
if(ret < 0)
85-
{
86-
printf("Fail to copy context from out stream to input stream!\n");
87-
goto end;
88-
}
89-
90-
91-
out_stream->codec->codec_tag = 0;
92-
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
93-
out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
94-
95-
96-
97-
av_dump_format(ofmt_ctx,0,outfile_name,1);
56+
AVCodecContext *o_codec_ctx;
57+
o_codec_ctx = out_stream->codec;
58+
o_codec_ctx->codec_id = AV_CODEC_ID_H264;
59+
o_codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
60+
o_codec_ctx->codec_tag = 0;
61+
o_codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
62+
o_codec_ctx->width = 640;
63+
o_codec_ctx->height = 480;
64+
o_codec_ctx->extradata = data;
65+
o_codec_ctx->extradata_size = size;
66+
67+
av_dump_format(ofmt_ctx,0,stream,1);
9868

99-
//打开输出URL(Open output URL)
100-
if (!(ofmt->flags & AVFMT_NOFILE)) {
101-
ret = avio_open(&ofmt_ctx->pb, outfile_name, AVIO_FLAG_WRITE);
102-
if (ret < 0) {
103-
printf( "Could not open output URL '%s'", outfile_name);
104-
goto end;
105-
}
106-
}
69+
// 打开输出URL(Open output URL)
70+
if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
71+
ret = avio_open(&ofmt_ctx->pb, stream, AVIO_FLAG_WRITE);
72+
if (ret < 0) {
73+
printf( "Could not open output URL '%s'", stream);
74+
goto end;
75+
}
76+
}
10777
//写文件头(Write file header)
108-
ret = avformat_write_header(ofmt_ctx, NULL);
109-
if (ret < 0) {
110-
printf( "Error occurred when opening output URL\n");
111-
goto end;
112-
}
78+
ret = avformat_write_header(ofmt_ctx, NULL);
79+
if (ret < 0) {
80+
printf( "Error occurred when opening output URL\n");
81+
goto end;
82+
}
83+
// free(data);
11384
return 0;
11485
end:
115-
if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
116-
avio_close(ofmt_ctx->pb);
117-
avformat_free_context(ofmt_ctx);
118-
if (ret < 0 && ret != AVERROR_EOF) {
119-
printf( "Error occurred.\n");
120-
return -1;
121-
}
86+
if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
87+
avio_close(ofmt_ctx->pb);
88+
avformat_free_context(ofmt_ctx);
89+
if (ret < 0 && ret != AVERROR_EOF) {
90+
printf( "Error occurred.\n");
91+
return -1;
92+
}
12293
}

rtmp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{
66
#endif
77

8-
int init_rtmp_streamer(char * stream);
8+
int init_rtmp_streamer(char* stream,uint8_t* data,uint32_t size);
99
int write_frame(uint8_t*data,int size);
1010
#ifdef __cplusplus
1111
}

0 commit comments

Comments
 (0)