print jni log to log file
[rtmpclient.git] / app / src / main / jni / ai_suanzi_rtmpclient_Ffmpeg.cpp
1 //
2 // Created by Peng Li on 30/4/2018.
3 //
4 #include "ai_suanzi_rtmpclient_Ffmpeg.h"
5 #include <android/native_window.h>
6 #include <android/native_window_jni.h>
7 #include "log.h"
8 #include <stdlib.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <limits.h>
12 #include <unistd.h>
13
14 extern "C" {
15     #include "libavformat/avformat.h"
16     #include "libavcodec/avcodec.h"
17     #include "libswscale/swscale.h"
18     #include "libavutil/imgutils.h"
19     #include "libavutil/time.h"
20     #include "libavdevice/avdevice.h"
21 }
22
23 int64_t start_time;
24 AVFormatContext *ofmt_ctx;
25 AVStream* video_st;
26 AVCodecContext* pCodecCtx;
27 AVCodec* pCodec;
28 AVPacket enc_pkt;
29 AVFrame *pFrameYUV;
30
31 void javaPrint(JNIEnv *env, jobject obj, const char* str)
32 {
33     jclass clazz = (*env).GetObjectClass(obj);
34     jobject mobj = env->NewGlobalRef(obj);
35     jmethodID mmid = env->GetMethodID(clazz, "print", "(Ljava/lang/String;)V");
36     jstring jstr = env->NewStringUTF(str);
37     env->CallVoidMethod(mobj, mmid, jstr);
38     env->DeleteLocalRef(jstr);
39 }
40
41 JNIEnv *g_env;
42 jobject g_obj;
43
44 void custom_log222 (void *ptr, int level, const char* fmt, va_list vl){
45     static int print_prefix = 1;
46     char line[1024];
47     av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix);
48     if (level <= AV_LOG_WARNING){
49         LOGE("%s", line);
50         javaPrint(g_env, g_obj, line);
51     } else {
52         //LOGE("%s", line);
53         //javaPrint(g_env, g_obj, line);
54     }
55 }
56
57 void custom_log(void *ptr, int level, const char* fmt, va_list vl){
58     //To TXT file
59     /*FILE *fp=fopen("/storage/emulated/0/av_log.txt","a+");
60     if(fp){
61     vfprintf(fp,fmt,vl);
62     fflush(fp);
63     fclose(fp);
64     }  */
65     //To Logcat
66     // LOGE(fmt, vl);
67     static int print_prefix = 1;
68     //static char prev[1024];
69     char line[1024];
70
71     av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix);
72
73     //strcpy(prev, line);
74     //sanitize((uint8_t *)line);
75
76     if (level <= AV_LOG_WARNING){
77         LOGE("%s", line);
78
79
80     } else {
81 //        LOGE("%s", line);
82     }
83 }
84
85
86 int framecnt = 0;
87 int yuv_width;
88 int yuv_height;
89 int y_length;
90 int uv_length;
91
92 JNIEXPORT void JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_init (JNIEnv *env, jobject obj ){
93     LOGE("########## Ffmpeg Init ##########");
94     unsigned int v = avutil_version();
95     LOGE("libavutil - %d.%d.%d", AV_VERSION_MAJOR(v), AV_VERSION_MINOR(v), AV_VERSION_MICRO(v));
96     v = avcodec_version();
97     LOGE("libavcodec - %d.%d.%d", AV_VERSION_MAJOR(v), AV_VERSION_MINOR(v), AV_VERSION_MICRO(v));
98     v = avformat_version();
99     LOGE("libavformat - %d.%d.%d", AV_VERSION_MAJOR(v), AV_VERSION_MINOR(v), AV_VERSION_MICRO(v));
100     v = avdevice_version();
101     LOGE("libavdevice - %d.%d.%d", AV_VERSION_MAJOR(v), AV_VERSION_MINOR(v), AV_VERSION_MICRO(v));
102
103     //system("su -c chmod 666 /dev/video0");
104     system("/system/xbin/su -c echo 'wowo' >> /data/local/test");
105     system("echo 'wowow' >> /sdcard/peng/test");
106
107     av_log_set_level(AV_LOG_TRACE);
108     av_register_all();
109     avdevice_register_all();
110     avformat_network_init();
111     av_log_set_callback(custom_log);
112 }
113
114
115 JNIEXPORT jstring JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_getVersion (JNIEnv *env, jobject obj) {
116     jint v = avformat_version();
117         LOGE("######### Ffmpeg JNI version i= %d", v);
118
119         system("su -c chmod 666 /dev/video0");
120
121     LOGE("######### Ffmpeg JNI version i= %d", v);
122
123
124     /*AVFormatContext *pFormatCtx = avformat_alloc_context();
125             avdevice_register_all();
126               av_log_set_callback(custom_log);
127         AVInputFormat *ifmt=av_find_input_format("video4linux2");
128         LOGE("===%s===", ifmt->name);
129         if(avformat_open_input(&pFormatCtx,"/dev/video0",ifmt,NULL)!=0){
130             LOGE("Couldn't open input stream.\n");
131                 return env->NewStringUTF("===== error =======");
132
133             //return -1;
134         }*/
135
136     return env->NewStringUTF("====== Ffmpeg call =======");
137 }
138
139 //const char* out_path;
140
141 JNIEXPORT void JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_setRtmpUrl (JNIEnv *env, jobject obj, jstring url){
142     LOGE("set rtmpurl");
143     //const char* out_path = "rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid";
144     //out_path  = env->GetStringUTFChars(url, 0);
145
146 }
147
148
149
150 //#defind JLOGE(s) javaPrint(env, obj, (s));
151
152
153 JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_initnew (JNIEnv *env, jobject obj, jint width, jint height, jstring url)
154 {
155     const char * out_path= env->GetStringUTFChars(url, 0);
156     LOGE("Ffmpeg init, width=%d, heigh=%d, url=%s", width, height, out_path);
157     javaPrint(env, obj, "Ffmpeg init");
158
159         yuv_width=width;
160         yuv_height=height;
161         y_length=width*height;
162         uv_length=width*height/4;
163
164
165         av_register_all();
166         avformat_network_init();
167         g_env = env;
168         g_obj = obj;
169         av_log_set_callback(custom_log222);
170
171
172         //output initialize
173         avformat_alloc_output_context2(&ofmt_ctx, NULL, "flv", out_path);
174         //output encoder initialize
175         pCodec = avcodec_find_encoder(AV_CODEC_ID_H264);
176         if (!pCodec){
177                 LOGE("Can not find encoder!\n");
178                 javaPrint(env, obj, "Can not find encoder!");
179                 return -1;
180         }
181         pCodecCtx = avcodec_alloc_context3(pCodec);
182         pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
183         pCodecCtx->width = width;
184         pCodecCtx->height = height;
185         pCodecCtx->time_base.num = 1;
186         pCodecCtx->time_base.den = 30;
187         pCodecCtx->bit_rate = 800000;
188         pCodecCtx->gop_size = 300;
189         /* Some formats want stream headers to be separate. */
190         if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
191                 pCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
192
193         //H264 codec param
194         //pCodecCtx->me_range = 16;
195         //pCodecCtx->max_qdiff = 4;
196         //pCodecCtx->qcompress = 0.6;
197         pCodecCtx->qmin = 10;
198         pCodecCtx->qmax = 51;
199         //Optional Param
200         pCodecCtx->max_b_frames = 3;
201         // Set H264 preset and tune
202         AVDictionary *param = 0;
203         av_dict_set(&param, "preset", "ultrafast", 0);
204         av_dict_set(&param, "tune", "zerolatency", 0);
205
206         if (avcodec_open2(pCodecCtx, pCodec, &param) < 0){
207                 LOGE("Failed to open encoder!\n");
208                 javaPrint(env, obj, "Failed to open encoder!");
209                 return -1;
210         }
211
212         //Add a new stream to output,should be called by the user before avformat_write_header() for muxing
213         video_st = avformat_new_stream(ofmt_ctx, pCodec);
214         if (video_st == NULL){
215                 return -1;
216         }
217         video_st->time_base.num = 1;
218         video_st->time_base.den = 30;
219         video_st->codec = pCodecCtx;
220
221         //Open output URL,set before avformat_write_header() for muxing
222         jint ret = 0;
223         if (( ret = avio_open(&ofmt_ctx->pb, out_path, AVIO_FLAG_READ_WRITE)) < 0){
224                 LOGE("Failed to open output file! return :%s(%d)\n", av_err2str(ret),ret);
225                 javaPrint(env, obj, "Failed to open output file! return!");
226                 return -1;
227         }
228
229         //Write File Header
230         avformat_write_header(ofmt_ctx, NULL);
231
232         start_time = av_gettime();
233     env->ReleaseStringUTFChars(url, out_path);
234     return 0;
235
236 }
237
238 JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_inithaha (JNIEnv *env, jobject obj, jint width, jint height) {
239
240         //const char* out_path = "/storage/emulated/0/Movies/output.flv";
241
242     //const char* out_path = "rtmp://192.168.1.35:1935/myapp/suanzi";
243     const char* out_path = "rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid";
244
245     // const char* out_path = "/storage/sdcard0/output.flv";
246
247
248
249     LOGE("Ffmpeg init, width=%d, heigh=%d", width, height);
250
251         yuv_width=width;
252         yuv_height=height;
253         y_length=width*height;
254         uv_length=width*height/4;
255
256
257         av_register_all();
258
259         //output initialize
260         avformat_alloc_output_context2(&ofmt_ctx, NULL, "flv", out_path);
261         //output encoder initialize
262         pCodec = avcodec_find_encoder(AV_CODEC_ID_H264);
263         if (!pCodec){
264                 LOGE("Can not find encoder!\n");
265                 return -1;
266         }
267         pCodecCtx = avcodec_alloc_context3(pCodec);
268         pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
269         pCodecCtx->width = width;
270         pCodecCtx->height = height;
271         pCodecCtx->time_base.num = 1;
272         pCodecCtx->time_base.den = 30;
273         pCodecCtx->bit_rate = 800000;
274         pCodecCtx->gop_size = 300;
275         /* Some formats want stream headers to be separate. */
276         if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
277                 pCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
278
279         //H264 codec param
280         //pCodecCtx->me_range = 16;
281         //pCodecCtx->max_qdiff = 4;
282         //pCodecCtx->qcompress = 0.6;
283         pCodecCtx->qmin = 10;
284         pCodecCtx->qmax = 51;
285         //Optional Param
286         pCodecCtx->max_b_frames = 3;
287         // Set H264 preset and tune
288         AVDictionary *param = 0;
289         av_dict_set(&param, "preset", "ultrafast", 0);
290         av_dict_set(&param, "tune", "zerolatency", 0);
291
292         if (avcodec_open2(pCodecCtx, pCodec, &param) < 0){
293                 LOGE("Failed to open encoder!\n");
294                 return -1;
295         }
296
297         //Add a new stream to output,should be called by the user before avformat_write_header() for muxing
298         video_st = avformat_new_stream(ofmt_ctx, pCodec);
299         if (video_st == NULL){
300                 return -1;
301         }
302         video_st->time_base.num = 1;
303         video_st->time_base.den = 30;
304         video_st->codec = pCodecCtx;
305
306         //Open output URL,set before avformat_write_header() for muxing
307         jint ret = 0;
308         if (( ret = avio_open(&ofmt_ctx->pb, out_path, AVIO_FLAG_READ_WRITE)) < 0){
309                 LOGE("Failed to open output file! return :%d\n", ret);
310                 return -1;
311         }
312
313         //Write File Header
314         avformat_write_header(ofmt_ctx, NULL);
315
316         start_time = av_gettime();
317     return 0;
318 }
319
320 JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_flush (JNIEnv *env, jobject obj){
321         int ret;
322         int got_frame;
323         AVPacket enc_pkt;
324         if (!(ofmt_ctx->streams[0]->codec->codec->capabilities & CODEC_CAP_DELAY))
325                 return 0;
326         while (1) {
327                 enc_pkt.data = NULL;
328                 enc_pkt.size = 0;
329                 av_init_packet(&enc_pkt);
330                 ret = avcodec_encode_video2(ofmt_ctx->streams[0]->codec, &enc_pkt,
331                         NULL, &got_frame);
332                 if (ret < 0)
333                         break;
334                 if (!got_frame){
335                         ret = 0;
336                         break;
337                 }
338                 LOGE("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n", enc_pkt.size);
339
340                 //Write PTS
341                 AVRational time_base = ofmt_ctx->streams[0]->time_base;//{ 1, 1000 };
342                 AVRational r_framerate1 = { 60, 2 };
343                 AVRational time_base_q = { 1, AV_TIME_BASE };
344                 //Duration between 2 frames (us)
345                 int64_t calc_duration = (double)(AV_TIME_BASE)*(1 / av_q2d(r_framerate1));      //内部时间戳
346                 //Parameters
347                 enc_pkt.pts = av_rescale_q(framecnt*calc_duration, time_base_q, time_base);
348                 enc_pkt.dts = enc_pkt.pts;
349                 enc_pkt.duration = av_rescale_q(calc_duration, time_base_q, time_base);
350
351                 //转换PTS/DTS(Convert PTS/DTS)
352                 enc_pkt.pos = -1;
353                 framecnt++;
354                 ofmt_ctx->duration = enc_pkt.duration * framecnt;
355
356                 /* mux encoded frame */
357                 ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
358                 if (ret < 0)
359                         break;
360         }
361         //Write file trailer
362         av_write_trailer(ofmt_ctx);
363     return 0;
364 }
365
366 JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_close (JNIEnv *env, jobject obj){
367         if (video_st)
368                 avcodec_close(video_st->codec);
369         avio_close(ofmt_ctx->pb);
370         avformat_free_context(ofmt_ctx);
371     return 0;
372 }
373
374
375
376 JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_processnew (JNIEnv *env, jobject obj, jbyteArray yuv){
377
378 }
379
380
381
382 JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_process (JNIEnv *env, jobject obj, jbyteArray yuv){
383         int ret;
384         int enc_got_frame=0;
385         int i=0;
386
387     //LOGE(" process data - ffmpeg");
388         pFrameYUV = av_frame_alloc();
389         uint8_t *out_buffer = (uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
390         avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
391
392         //安卓摄像头数据为NV21格式,此处将其转换为YUV420P格式
393         jbyte* in= (jbyte*)env->GetByteArrayElements(yuv,0);
394         memcpy(pFrameYUV->data[0],in,y_length);
395         for(i=0;i<uv_length;i++)
396         {
397                 *(pFrameYUV->data[2]+i)=*(in+y_length+i*2);
398                 *(pFrameYUV->data[1]+i)=*(in+y_length+i*2+1);
399         }
400
401         pFrameYUV->format = AV_PIX_FMT_YUV420P;
402         pFrameYUV->width = yuv_width;
403         pFrameYUV->height = yuv_height;
404
405         enc_pkt.data = NULL;
406         enc_pkt.size = 0;
407         av_init_packet(&enc_pkt);
408         ret = avcodec_encode_video2(pCodecCtx, &enc_pkt, pFrameYUV, &enc_got_frame);
409         av_frame_free(&pFrameYUV);
410
411         if (enc_got_frame == 1){
412         if (framecnt % (15 * 60) == 0){
413                     LOGE("Succeed to encode frame: %5d\tsize:%5d\n", framecnt, enc_pkt.size);
414                     javaPrint(env, obj, "Succeed to encode frame:");
415
416         }
417                 framecnt++;
418                 enc_pkt.stream_index = video_st->index;
419
420                 //Write PTS
421                 AVRational time_base = ofmt_ctx->streams[0]->time_base;//{ 1, 1000 };
422                 AVRational r_framerate1 = {60, 2 };//{ 50, 2 };
423                 AVRational time_base_q = { 1, AV_TIME_BASE };
424                 //Duration between 2 frames (us)
425                 int64_t calc_duration = (double)(AV_TIME_BASE)*(1 / av_q2d(r_framerate1));      //内部时间戳
426                 //Parameters
427                 //enc_pkt.pts = (double)(framecnt*calc_duration)*(double)(av_q2d(time_base_q)) / (double)(av_q2d(time_base));
428                 enc_pkt.pts = av_rescale_q(framecnt*calc_duration, time_base_q, time_base);
429                 enc_pkt.dts = enc_pkt.pts;
430                 enc_pkt.duration = av_rescale_q(calc_duration, time_base_q, time_base); //(double)(calc_duration)*(double)(av_q2d(time_base_q)) / (double)(av_q2d(time_base));
431                 enc_pkt.pos = -1;
432
433                 //Delay
434                 int64_t pts_time = av_rescale_q(enc_pkt.dts, time_base, time_base_q);
435                 int64_t now_time = av_gettime() - start_time;
436                 if (pts_time > now_time)
437                         av_usleep(pts_time - now_time);
438
439                 ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
440                 av_free_packet(&enc_pkt);
441         }
442     av_free(out_buffer);
443     return 0;
444 }
445
446 JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_play (JNIEnv *env, jobject obj, jobject surface, jstring fname){
447
448
449
450
451
452     LOGE("###### video play #####");
453     // char * file_name = "/storage/emulated/0/Movies/big_buck_bunny_720p_10mb.mp4";
454     const char * file_name = env->GetStringUTFChars(fname, 0);
455
456     av_register_all();
457       avdevice_register_all();
458
459
460     AVFormatContext * pFormatCtx = avformat_alloc_context();
461
462
463 //////////
464               av_log_set_callback(custom_log);
465
466      AVInputFormat *ifmt=av_find_input_format("video4linux2");
467      LOGE("===%s===", ifmt->name);
468      if(avformat_open_input(&pFormatCtx,"/dev/video0",ifmt,NULL)!=0){
469              LOGE("Couldn't open file:\n");
470              return -1; // Couldn't open file
471      }
472
473
474 ///////////
475
476 /*
477     // Open video file
478     if(avformat_open_input(&pFormatCtx, file_name, NULL, NULL)!=0) {
479
480         LOGE("Couldn't open file:%s\n", file_name);
481         return -1; // Couldn't open file
482     }
483 */
484     // Retrieve stream information
485     if(avformat_find_stream_info(pFormatCtx, NULL)<0) {
486         LOGE("Couldn't find stream information.");
487         return -1;
488     }
489
490     // Find the first video stream
491     int videoStream = -1, i;
492     for (i = 0; i < pFormatCtx->nb_streams; i++) {
493         if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO
494            && videoStream < 0) {
495             videoStream = i;
496         }
497     }
498     if(videoStream==-1) {
499         LOGE("Didn't find a video stream.");
500         return -1; // Didn't find a video stream
501     }
502
503     // Get a pointer to the codec context for the video stream
504     AVCodecContext  * pCodecCtx = pFormatCtx->streams[videoStream]->codec;
505     LOGE("============= %d ========",__LINE__);
506     // Find the decoder for the video stream
507     AVCodec * pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
508     if(pCodec==NULL) {
509         LOGE("Codec not found.");
510         return -1; // Codec not found
511     }
512
513     if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
514         LOGE("Could not open codec.");
515         return -1; // Could not open codec
516     }
517
518     // 获取native window
519     ANativeWindow* nativeWindow = ANativeWindow_fromSurface(env, surface);
520
521     // 获取视频宽高
522     int videoWidth = pCodecCtx->width;
523     int videoHeight = pCodecCtx->height;
524
525     // 设置native window的buffer大小,可自动拉伸
526     ANativeWindow_setBuffersGeometry(nativeWindow,  videoWidth, videoHeight, WINDOW_FORMAT_RGBA_8888);
527     ANativeWindow_Buffer windowBuffer;
528
529     if(avcodec_open2(pCodecCtx, pCodec, NULL)<0) {
530         LOGE("Could not open codec.");
531         return -1; // Could not open codec
532     }
533
534     LOGE("stream format:%s", pFormatCtx->iformat->name);
535     LOGE("duration :%lld", (pFormatCtx->duration) / 1000000);
536     LOGE("Width, Height:%d x %d", pCodecCtx->width, pCodecCtx->height);
537     LOGE("Decoder name:%s", pCodec->name);
538
539     // Allocate video frame
540     AVFrame * pFrame = av_frame_alloc();
541
542     // 用于渲染
543     AVFrame * pFrameRGBA = av_frame_alloc();
544     if(pFrameRGBA == NULL || pFrame == NULL) {
545         LOGE("Could not allocate video frame.");
546         return -1;
547     }
548
549     // Determine required buffer size and allocate buffer
550     int numBytes=av_image_get_buffer_size(AV_PIX_FMT_RGBA, pCodecCtx->width, pCodecCtx->height, 1);
551     uint8_t * buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
552     av_image_fill_arrays(pFrameRGBA->data, pFrameRGBA->linesize, buffer, AV_PIX_FMT_RGBA,
553                          pCodecCtx->width, pCodecCtx->height, 1);
554
555     // 由于解码出来的帧格式不是RGBA的,在渲染之前需要进行格式转换
556     struct SwsContext *sws_ctx = sws_getContext(pCodecCtx->width,
557                              pCodecCtx->height,
558                              pCodecCtx->pix_fmt,
559                              pCodecCtx->width,
560                              pCodecCtx->height,
561                              AV_PIX_FMT_RGBA,
562                              SWS_BILINEAR,
563                              NULL,
564                              NULL,
565                              NULL);
566
567     int frameFinished;
568     AVPacket packet;
569     while(av_read_frame(pFormatCtx, &packet)>=0) {
570         // Is this a packet from the video stream?
571         if(packet.stream_index==videoStream) {
572
573             // Decode video frame
574             avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
575
576             // 并不是decode一次就可解码出一帧
577             if (frameFinished) {
578
579                 // lock native window buffer
580                 ANativeWindow_lock(nativeWindow, &windowBuffer, 0);
581
582                 // 格式转换
583                 sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data,
584                           pFrame->linesize, 0, pCodecCtx->height,
585                           pFrameRGBA->data, pFrameRGBA->linesize);
586
587                 // 获取stride
588                 uint8_t * dst = (uint8_t*) windowBuffer.bits;
589                 int dstStride = windowBuffer.stride * 4;
590                 uint8_t * src = (uint8_t*) (pFrameRGBA->data[0]);
591                 int srcStride = pFrameRGBA->linesize[0];
592
593                 // 由于window的stride和帧的stride不同,因此需要逐行复制
594                 int h;
595                 for (h = 0; h < videoHeight; h++) {
596                     memcpy(dst + h * dstStride, src + h * srcStride, srcStride);
597                 }
598
599                 ANativeWindow_unlockAndPost(nativeWindow);
600             }
601
602         }
603         av_packet_unref(&packet);
604     }
605
606     av_free(buffer);
607     av_free(pFrameRGBA);
608
609     // Free the YUV frame
610     av_free(pFrame);
611
612     // Close the codecs
613     avcodec_close(pCodecCtx);
614
615     // Close the video file
616     avformat_close_input(&pFormatCtx);
617
618      env->ReleaseStringUTFChars(fname, file_name);
619     return 0;
620 }
621
622 JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_push (JNIEnv *env, jobject obj, jobject surface, jstring url){
623
624     /*
625     av_log_set_level(AV_LOG_TRACE);
626     av_register_all();
627     avformat_network_init();
628     avdevice_register_all();
629     */
630
631     LOGE("====push=====");
632 //    av_log_set_callback(custom_log);
633  // Open Output
634     //const char* out_path = "rtmp://192.168.1.35:1935/myapp/peng2";
635     const char* out_path =  "rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid";
636     //const char* out_path =  env->GetStringUTFChars(url, 0);
637     //const char * file_name = env->GetStringUTFChars(fname, 0);
638
639
640     int ret = 0;
641     /// Open Input
642     AVFormatContext *pFormatCtx = avformat_alloc_context();
643
644     AVInputFormat *ifmt = av_find_input_format("video4linux2");
645     if((ret = avformat_open_input(&pFormatCtx, "/dev/video0", ifmt, NULL)) != 0) {
646     //    if((ret = avformat_open_input(&pFormatCtx, "/dev/bus/usb/003/007", ifmt, NULL)) != 0) {
647
648         LOGE("could not open file11, ret=%d, error=%s,", ret, av_err2str(ret));
649         return -1;
650     }
651
652     if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
653         LOGE( "could not find stream info");
654         return -1;
655     }
656
657     av_dump_format(pFormatCtx, 0, "0", 0);
658
659     AVCodec *dec;
660     int video_index = -1;
661     if((video_index = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0)) < 0){
662         LOGE( "error");
663         return -1;
664     }
665
666     AVCodecContext *pCodecCtx = pFormatCtx->streams[video_index]->codec;
667     if(avcodec_open2(pCodecCtx, dec, NULL) <0){
668         LOGE( "eee");
669         return -1;
670     }
671
672
673     // Open Output
674     //const char* out_path = "rtmp://192.168.1.35:1935/myapp/peng2";
675     //const char* out_path =  "rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid";
676
677     AVFormatContext *ofmt_ctx;
678     avformat_alloc_output_context2(&ofmt_ctx, NULL, "flv", out_path);
679     AVCodec *oDec = avcodec_find_encoder(AV_CODEC_ID_H264);
680     if (!oDec) {
681         LOGE("Can not find endoder");
682         return -1;
683     }
684
685     AVCodecContext *oCodecCtx = avcodec_alloc_context3(oDec);
686     oCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
687     oCodecCtx->width = pCodecCtx->width;
688     oCodecCtx->height = pCodecCtx->height;
689     oCodecCtx->time_base.num = 1;
690     oCodecCtx->time_base.den = 30;
691     oCodecCtx->bit_rate = 800000;
692     oCodecCtx->gop_size = 300;
693     if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
694         oCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
695     oCodecCtx->qmin = 10;
696     oCodecCtx->qmax = 51;
697     oCodecCtx->max_b_frames = 3;
698
699     AVDictionary *params = 0;
700     av_dict_set(&params, "preset", "ultrafast", 0);
701     av_dict_set(&params, "tune", "zerolatency", 0);
702
703     if (avcodec_open2(oCodecCtx, oDec, &params) < 0){
704         LOGE("Failed to open encoder");
705         return -1;
706     }
707
708     AVStream *videoStream = avformat_new_stream(ofmt_ctx, oDec);
709     if (videoStream == NULL){
710         return -1;
711     }
712
713     videoStream->time_base.num = 1;
714     videoStream->time_base.den = 30;
715     videoStream->codec = oCodecCtx;
716
717     if((ret = avio_open(&ofmt_ctx->pb, out_path, AVIO_FLAG_READ_WRITE)) < 0){
718         LOGE("Failed open out file22 erro=%d, ==%s==", ret, av_err2str(ret) );
719         //LOGE("Failed open out file22 erro=%d", ret);
720         return -1;
721     }
722
723     avformat_write_header(ofmt_ctx, NULL);
724     /////////////
725
726
727
728
729     //
730     AVFrame *pFrame, *pFrameYUV;
731     pFrame = av_frame_alloc();
732     pFrameYUV = av_frame_alloc();
733
734     int num_bytes = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);
735     uint8_t *buffer = (uint8_t *)av_malloc(num_bytes * sizeof(uint8_t));
736     av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);
737
738     pFrameYUV->format = AV_PIX_FMT_YUV420P;
739     pFrameYUV->width = pCodecCtx->width;
740     pFrameYUV->height = pCodecCtx->height;
741
742     struct SwsContext *img_convert_ctx;
743     img_convert_ctx = sws_getContext(pCodecCtx->width,
744                               pCodecCtx->height,
745                               pCodecCtx->pix_fmt,
746                               pCodecCtx->width,
747                               pCodecCtx->height,
748                               AV_PIX_FMT_YUV420P,
749                               SWS_BICUBIC,
750                               NULL, NULL, NULL);
751
752     AVPacket *packet = (AVPacket *)av_malloc(sizeof(AVPacket));
753     int got_picture = 0;
754
755     AVPacket enc_pkt ;
756
757     int64_t framecnt = 0;
758
759     while(av_read_frame(pFormatCtx, packet) >= 0){
760         if (packet->stream_index == video_index){
761             ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
762             if (ret < 0){
763                 LOGE("Decode Error.");
764                 return -1;
765             }
766             if (got_picture){
767                 sws_scale(img_convert_ctx, (const unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
768
769                 enc_pkt.data = NULL;
770                 enc_pkt.size = 0;
771                 av_init_packet(&enc_pkt);
772                 int enc_got_frame = 0;
773                 ret = avcodec_encode_video2(oCodecCtx, &enc_pkt, pFrameYUV, &enc_got_frame);
774                 if (enc_got_frame == 1){
775
776                             framecnt++;
777                     enc_pkt.stream_index = videoStream->index;
778
779                     // write PTS
780                     AVRational time_base = ofmt_ctx->streams[0]->time_base;
781                     AVRational r_framerate1 = {60, 2};
782                     AVRational time_base_q = {1, AV_TIME_BASE};
783
784                             int64_t calc_duration = (double)(AV_TIME_BASE)*(1 / av_q2d(r_framerate1));  //内部时间戳
785                     enc_pkt.pts = av_rescale_q(framecnt*calc_duration, time_base_q, time_base);
786                     enc_pkt.dts = enc_pkt.pts;
787                     enc_pkt.duration = av_rescale_q(calc_duration, time_base_q, time_base); //(double)(calc_duration)*(double)(av_q2d(time_base_q)) / (double)(av_q2d(time_base));
788                     enc_pkt.pos = -1;
789
790                     int64_t pts_time = av_rescale_q(enc_pkt.dts, time_base, time_base_q);
791
792                 ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
793                 //av_frame_free(&pFrameYUV);
794                 //av_packet_unref(packet);
795
796                 av_free_packet(&enc_pkt);
797                 //av_packet_unref(&enc_pkt);
798                 }
799             }
800         }
801         av_packet_unref(packet);
802     }
803
804     sws_freeContext(img_convert_ctx);
805     av_free(pFrameYUV);
806     av_free(pFrame);
807     avcodec_close(pCodecCtx);
808     avformat_close_input(&pFormatCtx);
809     return 0;
810 }
811
812 JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_preview (JNIEnv *env, jobject obj, jobject surface){
813
814     LOGE("###### video preview #####");
815
816     av_register_all();
817     avdevice_register_all();
818
819
820     AVFormatContext * pFormatCtx = avformat_alloc_context();
821
822
823     av_log_set_callback(custom_log);
824
825      AVInputFormat *ifmt=av_find_input_format("video4linux2");
826      LOGE("===%s===", ifmt->name);
827      if(avformat_open_input(&pFormatCtx,"/dev/video0",ifmt,NULL)!=0){
828              LOGE("Couldn't open file:\n");
829              return -1; // Couldn't open file
830      }
831
832     // Retrieve stream information
833     if(avformat_find_stream_info(pFormatCtx, NULL)<0) {
834         LOGE("Couldn't find stream information.");
835         return -1;
836     }
837
838     // Find the first video stream
839     int videoStream = -1, i;
840     for (i = 0; i < pFormatCtx->nb_streams; i++) {
841         if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO
842            && videoStream < 0) {
843             videoStream = i;
844         }
845     }
846     if(videoStream==-1) {
847         LOGE("Didn't find a video stream.");
848         return -1; // Didn't find a video stream
849     }
850
851     // Get a pointer to the codec context for the video stream
852     AVCodecContext  * pCodecCtx = pFormatCtx->streams[videoStream]->codec;
853     LOGE("============= %d ========",__LINE__);
854     // Find the decoder for the video stream
855     AVCodec * pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
856     if(pCodec==NULL) {
857         LOGE("Codec not found.");
858         return -1; // Codec not found
859     }
860
861     if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
862         LOGE("Could not open codec.");
863         return -1; // Could not open codec
864     }
865
866     // 获取native window
867     ANativeWindow* nativeWindow = ANativeWindow_fromSurface(env, surface);
868
869     // 获取视频宽高
870     int videoWidth = pCodecCtx->width;
871     int videoHeight = pCodecCtx->height;
872
873     // 设置native window的buffer大小,可自动拉伸
874     ANativeWindow_setBuffersGeometry(nativeWindow,  videoWidth, videoHeight, WINDOW_FORMAT_RGBA_8888);
875     ANativeWindow_Buffer windowBuffer;
876
877
878     LOGE("stream format:%s", pFormatCtx->iformat->name);
879     LOGE("duration :%lld", (pFormatCtx->duration) / 1000000);
880     LOGE("Width, Height:%d x %d", pCodecCtx->width, pCodecCtx->height);
881     LOGE("Decoder name:%s", pCodec->name);
882
883     // Allocate video frame
884     AVFrame * pFrame = av_frame_alloc();
885
886     // 用于渲染
887     AVFrame * pFrameRGBA = av_frame_alloc();
888     if(pFrameRGBA == NULL || pFrame == NULL) {
889         LOGE("Could not allocate video frame.");
890         return -1;
891     }
892
893     // Determine required buffer size and allocate buffer
894     int numBytes=av_image_get_buffer_size(AV_PIX_FMT_RGBA, pCodecCtx->width, pCodecCtx->height, 1);
895     uint8_t * buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
896     av_image_fill_arrays(pFrameRGBA->data, pFrameRGBA->linesize, buffer, AV_PIX_FMT_RGBA,
897                          pCodecCtx->width, pCodecCtx->height, 1);
898
899     // 由于解码出来的帧格式不是RGBA的,在渲染之前需要进行格式转换
900     struct SwsContext *sws_ctx = sws_getContext(pCodecCtx->width,
901                              pCodecCtx->height,
902                              pCodecCtx->pix_fmt,
903                              pCodecCtx->width,
904                              pCodecCtx->height,
905                              AV_PIX_FMT_RGBA,
906                              SWS_BILINEAR,
907                              NULL,
908                              NULL,
909                              NULL);
910
911     int frameFinished;
912     AVPacket packet;
913     while(av_read_frame(pFormatCtx, &packet)>=0) {
914         // Is this a packet from the video stream?
915         if(packet.stream_index==videoStream) {
916
917             // Decode video frame
918             avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
919
920             // 并不是decode一次就可解码出一帧
921             if (frameFinished) {
922
923                 // lock native window buffer
924                 ANativeWindow_lock(nativeWindow, &windowBuffer, 0);
925
926                 // 格式转换
927                 sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data,
928                           pFrame->linesize, 0, pCodecCtx->height,
929                           pFrameRGBA->data, pFrameRGBA->linesize);
930
931                 // 获取stride
932                 uint8_t * dst = (uint8_t*) windowBuffer.bits;
933                 int dstStride = windowBuffer.stride * 4;
934                 uint8_t * src = (uint8_t*) (pFrameRGBA->data[0]);
935                 int srcStride = pFrameRGBA->linesize[0];
936
937                 // 由于window的stride和帧的stride不同,因此需要逐行复制
938                 int h;
939                 for (h = 0; h < videoHeight; h++) {
940                     memcpy(dst + h * dstStride, src + h * srcStride, srcStride);
941                 }
942
943                 ANativeWindow_unlockAndPost(nativeWindow);
944             }
945
946         }
947         av_packet_unref(&packet);
948     }
949
950     av_free(buffer);
951     av_free(pFrameRGBA);
952
953     // Free the YUV frame
954     av_free(pFrame);
955
956     // Close the codecs
957     avcodec_close(pCodecCtx);
958
959     // Close the video file
960     avformat_close_input(&pFormatCtx);
961
962      //env->ReleaseStringUTFChars(fname, file_name);
963     return 0;
964 }
965
966 JNIEXPORT jstring JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_getPerfectDevice (JNIEnv *env, jobject obj) {
967     int ret;
968     LOGE("getPerfectDevice");
969     AVFormatContext *pFormatCtx = avformat_alloc_context();
970     AVInputFormat *ifmt = av_find_input_format("video4linux2");
971     if((ret = avformat_open_input(&pFormatCtx, "/dev/video0", ifmt, NULL)) != 0) {
972         LOGE("could not open file11, ret=%d, error=%s,", ret, av_err2str(ret));
973         //return ;
974     }
975     if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
976         LOGE( "could not find stream info");
977         //return -1;
978     }
979     av_dump_format(pFormatCtx, 0, "0", 0);
980     avformat_free_context(pFormatCtx);
981     //system("su -c \"find / -perm -2000 -o -perm -4000; ps; ls\"");
982     system("touch /storage/sdcard0/aa");
983
984     return env->NewStringUTF("====== Ffmpeg call =======");
985 }
986
987
988
989
990 JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_test (JNIEnv *env, jobject obj, jint fd){
991     char path[512] = {0};
992     char* real_path = NULL;
993
994     LOGE("=================");
995     //system("su -c chmod 666 /dev/video0");
996     /*
997 #ifdef ANDROID_USB_CAMERA
998     //MY_USB_CAMER_FD = fd;
999     avdevice_set_android_usb_fd(fd);
1000
1001     //LOGE("MY camer fd is %d", MY_USB_CAMER_FD);
1002 #endif
1003
1004     sprintf(path, "/proc/%d/fd/%d", getpid(), fd);
1005     if(path[0] != '\0'){
1006         LOGE("fd path is %s.", path);
1007         real_path = realpath(path, NULL);
1008         if(real_path != NULL){
1009             LOGE("get full path from fd %s.", real_path);
1010             free(real_path);
1011         }
1012     }
1013 */
1014
1015 /*
1016
1017
1018
1019     LOGE("====push=====");
1020 //    av_log_set_callback(custom_log);
1021
1022     int ret = 0;
1023     /// Open Input
1024     AVFormatContext *pFormatCtx = avformat_alloc_context();
1025
1026     AVInputFormat *ifmt = av_find_input_format("video4linux2");
1027     //if((ret = avformat_open_input(&pFormatCtx, "/dev/video0", ifmt, NULL)) != 0) {
1028         if((ret = avformat_open_input(&pFormatCtx, real_path, ifmt, NULL)) != 0) {
1029
1030         LOGE("could not open file11, ret=%d, error=%s,", ret, av_err2str(ret));
1031         return -1;
1032     }
1033
1034     if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
1035         LOGE( "could not find stream info");
1036         return -1;
1037     }
1038
1039     av_dump_format(pFormatCtx, 0, "0", 0);
1040
1041     AVCodec *dec;
1042     int video_index = -1;
1043     if((video_index = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0)) < 0){
1044         LOGE( "error");
1045         return -1;
1046     }
1047
1048     AVCodecContext *pCodecCtx = pFormatCtx->streams[video_index]->codec;
1049     if(avcodec_open2(pCodecCtx, dec, NULL) <0){
1050         LOGE( "eee");
1051         return -1;
1052     }
1053
1054
1055     // Open Output
1056     //const char* out_path = "rtmp://192.168.1.35:1935/myapp/peng2";
1057     const char* out_path =  "rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid";
1058
1059     AVFormatContext *ofmt_ctx;
1060     avformat_alloc_output_context2(&ofmt_ctx, NULL, "flv", out_path);
1061     AVCodec *oDec = avcodec_find_encoder(AV_CODEC_ID_H264);
1062     if (!oDec) {
1063         LOGE("Can not find endoder");
1064         return -1;
1065     }
1066
1067     AVCodecContext *oCodecCtx = avcodec_alloc_context3(oDec);
1068     oCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
1069     oCodecCtx->width = pCodecCtx->width;
1070     oCodecCtx->height = pCodecCtx->height;
1071     oCodecCtx->time_base.num = 1;
1072     oCodecCtx->time_base.den = 30;
1073     oCodecCtx->bit_rate = 800000;
1074     oCodecCtx->gop_size = 300;
1075     if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
1076         oCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
1077     oCodecCtx->qmin = 10;
1078     oCodecCtx->qmax = 51;
1079     oCodecCtx->max_b_frames = 3;
1080
1081     AVDictionary *params = 0;
1082     av_dict_set(&params, "preset", "ultrafast", 0);
1083     av_dict_set(&params, "tune", "zerolatency", 0);
1084
1085     if (avcodec_open2(oCodecCtx, oDec, &params) < 0){
1086         LOGE("Failed to open encoder");
1087         return -1;
1088     }
1089
1090     AVStream *videoStream = avformat_new_stream(ofmt_ctx, oDec);
1091     if (videoStream == NULL){
1092         return -1;
1093     }
1094
1095     videoStream->time_base.num = 1;
1096     videoStream->time_base.den = 30;
1097     videoStream->codec = oCodecCtx;
1098
1099     if((ret = avio_open(&ofmt_ctx->pb, out_path, AVIO_FLAG_READ_WRITE)) < 0){
1100         LOGE("Failed open out file22 erro=%d, ==%s==", ret, av_err2str(ret) );
1101         //LOGE("Failed open out file22 erro=%d", ret);
1102         return -1;
1103     }
1104
1105     avformat_write_header(ofmt_ctx, NULL);
1106     /////////////
1107
1108
1109
1110
1111     //
1112     AVFrame *pFrame, *pFrameYUV;
1113     pFrame = av_frame_alloc();
1114     pFrameYUV = av_frame_alloc();
1115
1116     int num_bytes = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);
1117     uint8_t *buffer = (uint8_t *)av_malloc(num_bytes * sizeof(uint8_t));
1118     av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);
1119
1120     pFrameYUV->format = AV_PIX_FMT_YUV420P;
1121     pFrameYUV->width = pCodecCtx->width;
1122     pFrameYUV->height = pCodecCtx->height;
1123
1124     struct SwsContext *img_convert_ctx;
1125     img_convert_ctx = sws_getContext(pCodecCtx->width,
1126                               pCodecCtx->height,
1127                               pCodecCtx->pix_fmt,
1128                               pCodecCtx->width,
1129                               pCodecCtx->height,
1130                               AV_PIX_FMT_YUV420P,
1131                               SWS_BICUBIC,
1132                               NULL, NULL, NULL);
1133
1134     AVPacket *packet = (AVPacket *)av_malloc(sizeof(AVPacket));
1135     int got_picture = 0;
1136
1137     AVPacket enc_pkt ;
1138
1139     int64_t framecnt = 0;
1140
1141     while(av_read_frame(pFormatCtx, packet) >= 0){
1142         if (packet->stream_index == video_index){
1143             ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
1144             if (ret < 0){
1145                 LOGE("Decode Error.");
1146                 return -1;
1147             }
1148             if (got_picture){
1149                 sws_scale(img_convert_ctx, (const unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
1150
1151                 enc_pkt.data = NULL;
1152                 enc_pkt.size = 0;
1153                 av_init_packet(&enc_pkt);
1154                 int enc_got_frame = 0;
1155                 ret = avcodec_encode_video2(oCodecCtx, &enc_pkt, pFrameYUV, &enc_got_frame);
1156                 if (enc_got_frame == 1){
1157
1158                             framecnt++;
1159                     enc_pkt.stream_index = videoStream->index;
1160
1161                     // write PTS
1162                     AVRational time_base = ofmt_ctx->streams[0]->time_base;
1163                     AVRational r_framerate1 = {60, 2};
1164                     AVRational time_base_q = {1, AV_TIME_BASE};
1165
1166                             int64_t calc_duration = (double)(AV_TIME_BASE)*(1 / av_q2d(r_framerate1));  //内部时间戳
1167                     enc_pkt.pts = av_rescale_q(framecnt*calc_duration, time_base_q, time_base);
1168                     enc_pkt.dts = enc_pkt.pts;
1169                     enc_pkt.duration = av_rescale_q(calc_duration, time_base_q, time_base); //(double)(calc_duration)*(double)(av_q2d(time_base_q)) / (double)(av_q2d(time_base));
1170                     enc_pkt.pos = -1;
1171
1172                     int64_t pts_time = av_rescale_q(enc_pkt.dts, time_base, time_base_q);
1173
1174                 ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
1175                 //av_frame_free(&pFrameYUV);
1176                 //av_packet_unref(packet);
1177
1178                 av_free_packet(&enc_pkt);
1179                 //av_packet_unref(&enc_pkt);
1180                 }
1181             }
1182         }
1183         av_packet_unref(packet);
1184     }
1185
1186     sws_freeContext(img_convert_ctx);
1187     av_free(pFrameYUV);
1188     av_free(pFrame);
1189     avcodec_close(pCodecCtx);
1190     avformat_close_input(&pFormatCtx);
1191
1192
1193 */
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205 }