11#include "mpp.h"
22static void mpp_close (MppContext * ctx );
33static 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 );
55static _Bool process_image (uint8_t * p , int size ,MppContext * mpp_enc_data );
66
77MppContext * 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
250243static _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
290274mdddd :
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}
0 commit comments