<div dir="ltr"><div style="font-size:14px">using VS2010 in Win7 x64, ffmpeg 2.4.5,</div><div style="font-size:14px">i download a demo to record my screen(with audio record ). </div><div style="font-size:14px">i modified the program so that it can work in 3 threads. but  every time i close the thread i found it have about 40MB memory remained. (every time i start->close thread, memory occupancy increases 40MB) </div><div style="font-size:14px">but the VS didn't detect any memory leak.</div><div style="font-size:14px">i'm a newcomer to ffmpeg, i wonder if there are any resoures i didn't free?</div><div style="font-size:14px">ps. i run the demo step by step, and i found it has the same problem, until it quit the main function, it still have  40MB memory occupancy. And i found the memory increase obviously in the screen capture thread,but when it increased to about 100MB, it won't increase, tben i quit the program, it will have 40MB memory left. i fell confused.</div><div style="font-size:14px">here is my demo code.</div><div style="font-size:14px"><br></div><div style="font-size:14px">[code]#include "stdafx.h"</div><div style="font-size:14px"><br></div><div style="font-size:14px">#ifdef<span style="white-space:pre-wrap">  </span>__cplusplus</div><div style="font-size:14px">extern "C"</div><div style="font-size:14px">{</div><div style="font-size:14px">#endif</div><div style="font-size:14px">#include "libavcodec/avcodec.h"</div><div style="font-size:14px">#include "libavformat/avformat.h"</div><div style="font-size:14px">#include "libswscale/swscale.h"</div><div style="font-size:14px">#include "libavdevice/avdevice.h"</div><div style="font-size:14px">#include "libavutil/audio_fifo.h"</div><div style="font-size:14px"><br></div><div style="font-size:14px">#pragma comment(lib, "avcodec.lib")</div><div style="font-size:14px">#pragma comment(lib, "avformat.lib")</div><div style="font-size:14px">#pragma comment(lib, "avutil.lib")</div><div style="font-size:14px">#pragma comment(lib, "avdevice.lib")</div><div style="font-size:14px">#pragma comment(lib, "avfilter.lib")</div><div style="font-size:14px"><br></div><div style="font-size:14px">//#pragma comment(lib, "avfilter.lib")</div><div style="font-size:14px">//#pragma comment(lib, "postproc.lib")</div><div style="font-size:14px">//#pragma comment(lib, "swresample.lib")</div><div style="font-size:14px">#pragma comment(lib, "swscale.lib")</div><div style="font-size:14px">#ifdef __cplusplus</div><div style="font-size:14px">};</div><div style="font-size:14px">#endif</div><div style="font-size:14px"><br></div><div style="font-size:14px">AVFormatContext<span style="white-space:pre-wrap">      </span>*pFormatCtx_Video = NULL, *pFormatCtx_Audio = NULL, *pFormatCtx_Out = NULL;</div><div style="font-size:14px">AVCodecContext<span style="white-space:pre-wrap">       </span>*pCodecCtx_Video;</div><div style="font-size:14px">AVCodec<span style="white-space:pre-wrap">                        </span>*pCodec_Video;</div><div style="font-size:14px">AVFifoBuffer<span style="white-space:pre-wrap">      </span>*fifo_video = NULL;</div><div style="font-size:14px">AVAudioFifo<span style="white-space:pre-wrap">          </span>*fifo_audio = NULL;</div><div style="font-size:14px">int VideoIndex, AudioIndex;</div><div style="font-size:14px"><br></div><div style="font-size:14px">CRITICAL_SECTION AudioSection, VideoSection;</div><div style="font-size:14px"><br></div><div style="font-size:14px"><br></div><div style="font-size:14px"><br></div><div style="font-size:14px">SwsContext *img_convert_ctx;</div><div style="font-size:14px">int frame_size = 0;</div><div style="font-size:14px"><br></div><div style="font-size:14px">uint8_t *picture_buf = NULL, *frame_buf = NULL;</div><div style="font-size:14px"><br></div><div style="font-size:14px">bool bCap = true;</div><div style="font-size:14px"><br></div><div style="font-size:14px">DWORD WINAPI ScreenCapThreadProc( LPVOID lpParam );</div><div style="font-size:14px">DWORD WINAPI AudioCapThreadProc( LPVOID lpParam );</div><div style="font-size:14px"><br></div><div style="font-size:14px">int OpenVideoCapture()</div><div style="font-size:14px">{</div><div style="font-size:14px"><span style="white-space:pre-wrap">   </span>AVInputFormat *ifmt=av_find_input_format("gdigrab");</div><div style="font-size:14px"><span style="white-space:pre-wrap">  </span>//</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>AVDictionary *options = NULL;</div><div style="font-size:14px"><span style="white-space:pre-wrap">   </span>av_dict_set(&options, "framerate", "15", NULL);</div><div style="font-size:14px"><span style="white-space:pre-wrap"> </span>//av_dict_set(&options,"offset_x","20",0);</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>//The distance from the top edge of the screen or desktop</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>//av_dict_set(&options,"offset_y","40",0);</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>//Video frame size. The default is to capture the full screen</div><div style="font-size:14px"><span style="white-space:pre-wrap">   </span>//av_dict_set(&options,"video_size","320x240",0);</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>if(avformat_open_input(&pFormatCtx_Video, "desktop", ifmt, &options)!=0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>printf("Couldn't open input stream.(无法打开视频输入流)\n");</div><div style="font-size:14px"><span style="white-space:pre-wrap">         </span>return -1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>if(avformat_find_stream_info(pFormatCtx_Video,NULL)<0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>printf("Couldn't find stream information.(无法获取视频流信息)\n");</div><div style="font-size:14px"><span style="white-space:pre-wrap">           </span>return -1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>if (pFormatCtx_Video->streams[0]->codec->codec_type != AVMEDIA_TYPE_VIDEO)</div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>printf("Couldn't find video stream information.(无法获取视频流信息)\n");</div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>return -1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>pCodecCtx_Video = pFormatCtx_Video->streams[0]->codec;</div><div style="font-size:14px"><span style="white-space:pre-wrap">    </span>pCodec_Video = avcodec_find_decoder(pCodecCtx_Video->codec_id);</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>if(pCodec_Video == NULL)</div><div style="font-size:14px"><span style="white-space:pre-wrap">        </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>printf("Codec not found.(没有找到解码器)\n");</div><div style="font-size:14px"><span style="white-space:pre-wrap">              </span>return -1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>if(avcodec_open2(pCodecCtx_Video, pCodec_Video, NULL) < 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">   </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>printf("Could not open codec.(无法打开解码器)\n");</div><div style="font-size:14px"><span style="white-space:pre-wrap">         </span>return -1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>}</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span></div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>img_convert_ctx = sws_getContext(pCodecCtx_Video->width, pCodecCtx_Video->height, pCodecCtx_Video->pix_fmt, </div><div style="font-size:14px"><span style="white-space:pre-wrap">          </span>pCodecCtx_Video->width, pCodecCtx_Video->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); </div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>frame_size = avpicture_get_size(pCodecCtx_Video->pix_fmt, pCodecCtx_Video->width, pCodecCtx_Video->height);</div><div style="font-size:14px"><span style="white-space:pre-wrap">    </span>//</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>fifo_video = av_fifo_alloc(30 * avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx_Video->width, pCodecCtx_Video->height));</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>return 0;</div><div style="font-size:14px">}</div><div style="font-size:14px"><br></div><div style="font-size:14px">static char *dup_wchar_to_utf8(wchar_t *w)</div><div style="font-size:14px">{</div><div style="font-size:14px"><span style="white-space:pre-wrap"> </span>char *s = NULL;</div><div style="font-size:14px"><span style="white-space:pre-wrap"> </span>int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);</div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>s = (char *) av_malloc(l);</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>if (s)</div><div style="font-size:14px"><span style="white-space:pre-wrap">          </span>WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);</div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>return s;</div><div style="font-size:14px">}</div><div style="font-size:14px"><br></div><div style="font-size:14px">int OpenAudioCapture()</div><div style="font-size:14px">{</div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>//</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>AVInputFormat *pAudioInputFmt = av_find_input_format("dshow");</div><div style="font-size:14px"><span style="white-space:pre-wrap">        </span></div><div style="font-size:14px"><span style="white-space:pre-wrap">        </span>//</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>char * psDevName = dup_wchar_to_utf8(L"audio=virtual-audio-capturer");</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>if (avformat_open_input(&pFormatCtx_Audio, psDevName, pAudioInputFmt,NULL) < 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">  </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>printf("Couldn't open input stream.(无法打开音频输入流)\n");</div><div style="font-size:14px"><span style="white-space:pre-wrap">         </span>return -1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>}</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>if(avformat_find_stream_info(pFormatCtx_Audio,NULL)<0)  </div><div style="font-size:14px"><span style="white-space:pre-wrap">            </span>return -1; </div><div style="font-size:14px"><span style="white-space:pre-wrap">    </span></div><div style="font-size:14px"><span style="white-space:pre-wrap">        </span>if(pFormatCtx_Audio->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO)</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>printf("Couldn't find video stream information.(无法获取音频流信息)\n");</div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>return -1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>}</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>AVCodec *tmpCodec = avcodec_find_decoder(pFormatCtx_Audio->streams[0]->codec->codec_id);</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>if(0 > avcodec_open2(pFormatCtx_Audio->streams[0]->codec, tmpCodec, NULL))</div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>printf("can not find or open audio decoder!\n");</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>}</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span></div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>return 0;</div><div style="font-size:14px">}</div><div style="font-size:14px"><br></div><div style="font-size:14px">int OpenOutPut()</div><div style="font-size:14px">{</div><div style="font-size:14px"><span style="white-space:pre-wrap">   </span>AVStream *pVideoStream = NULL, *pAudioStream = NULL;</div><div style="font-size:14px"><span style="white-space:pre-wrap">    </span>const char *outFileName = "test.mp4";</div><div style="font-size:14px"><span style="white-space:pre-wrap"> </span>avformat_alloc_output_context2(&pFormatCtx_Out, NULL, NULL, outFileName);</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap"> </span>if (pFormatCtx_Video->streams[0]->codec->codec_type == AVMEDIA_TYPE_VIDEO)</div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>VideoIndex = 0;</div><div style="font-size:14px"><span style="white-space:pre-wrap">         </span>pVideoStream = avformat_new_stream(pFormatCtx_Out, NULL);</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>if (!pVideoStream)</div><div style="font-size:14px"><span style="white-space:pre-wrap">              </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>printf("can not new stream for output!\n");</div><div style="font-size:14px"><span style="white-space:pre-wrap">                   </span>return -1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">              </span>}</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>//set codec context param</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>pVideoStream->codec->codec = avcodec_find_encoder(AV_CODEC_ID_MPEG4);</div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>pVideoStream->codec->height = pFormatCtx_Video->streams[0]->codec->height;</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>pVideoStream->codec->width = pFormatCtx_Video->streams[0]->codec->width;</div><div style="font-size:14px"><span style="white-space:pre-wrap">         </span></div><div style="font-size:14px"><span style="white-space:pre-wrap">                </span>pVideoStream->codec->time_base = pFormatCtx_Video->streams[0]->codec->time_base;</div><div style="font-size:14px"><span style="white-space:pre-wrap">         </span>pVideoStream->codec->sample_aspect_ratio = pFormatCtx_Video->streams[0]->codec->sample_aspect_ratio;</div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>// take first format from list of supported formats</div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>pVideoStream->codec->pix_fmt = pFormatCtx_Out->streams[VideoIndex]->codec->codec->pix_fmts[0];</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">              </span>//open encoder</div><div style="font-size:14px"><span style="white-space:pre-wrap">          </span>if (!pVideoStream->codec->codec)</div><div style="font-size:14px"><span style="white-space:pre-wrap">          </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>printf("can not find the encoder!\n");</div><div style="font-size:14px"><span style="white-space:pre-wrap">                        </span>return -1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">              </span>}</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>if (pFormatCtx_Out->oformat->flags & AVFMT_GLOBALHEADER)</div><div style="font-size:14px"><span style="white-space:pre-wrap">                      </span>pVideoStream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">         </span>if ((avcodec_open2(pVideoStream->codec, pVideoStream->codec->codec, NULL)) < 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>printf("can not open the encoder\n");</div><div style="font-size:14px"><span style="white-space:pre-wrap">                 </span>return -1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">              </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>}</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>if(pFormatCtx_Audio->streams[0]->codec->codec_type == AVMEDIA_TYPE_AUDIO)</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>AVCodecContext *pOutputCodecCtx;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                </span>AudioIndex = 1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">         </span>pAudioStream = avformat_new_stream(pFormatCtx_Out, NULL);</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>pAudioStream->codec->codec = avcodec_find_encoder(pFormatCtx_Out->oformat->audio_codec);</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">          </span>pOutputCodecCtx = pAudioStream->codec;</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>pOutputCodecCtx->sample_rate = pFormatCtx_Audio->streams[0]->codec->sample_rate;</div><div style="font-size:14px"><span style="white-space:pre-wrap">            </span>pOutputCodecCtx->channel_layout = pFormatCtx_Out->streams[0]->codec->channel_layout;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                </span>pOutputCodecCtx->channels = av_get_channel_layout_nb_channels(pAudioStream->codec->channel_layout);</div><div style="font-size:14px"><span style="white-space:pre-wrap">            </span>if(pOutputCodecCtx->channel_layout == 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>pOutputCodecCtx->channel_layout = AV_CH_LAYOUT_STEREO;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>pOutputCodecCtx->channels = av_get_channel_layout_nb_channels(pOutputCodecCtx->channel_layout);</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">         </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>pOutputCodecCtx->sample_fmt = pAudioStream->codec->codec->sample_fmts[0];</div><div style="font-size:14px"><span style="white-space:pre-wrap">           </span>AVRational time_base={1, pAudioStream->codec->sample_rate};</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>pAudioStream->time_base = time_base;</div><div style="font-size:14px"><span style="white-space:pre-wrap">         </span>//audioCodecCtx->time_base = time_base;</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">            </span>pOutputCodecCtx->codec_tag = 0;  </div><div style="font-size:14px"><span style="white-space:pre-wrap">           </span>if (pFormatCtx_Out->oformat->flags & AVFMT_GLOBALHEADER)  </div><div style="font-size:14px"><span style="white-space:pre-wrap">                   </span>pOutputCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">                </span>if (avcodec_open2(pOutputCodecCtx, pOutputCodecCtx->codec, 0) < 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">                </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>//</div><div style="font-size:14px"><span style="white-space:pre-wrap">                      </span>return -1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">              </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>}</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>if (!(pFormatCtx_Out->oformat->flags & AVFMT_NOFILE))</div><div style="font-size:14px"><span style="white-space:pre-wrap"> </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>if(avio_open(&pFormatCtx_Out->pb, outFileName, AVIO_FLAG_WRITE) < 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">          </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>printf("can not open output file handle!\n");</div><div style="font-size:14px"><span style="white-space:pre-wrap">                 </span>return -1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">              </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>}</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>if(avformat_write_header(pFormatCtx_Out, NULL) < 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">  </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>printf("can not write the header of the output file!\n");</div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>return -1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>}</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>return 0;</div><div style="font-size:14px">}</div><div style="font-size:14px"><br></div><div style="font-size:14px">int _tmain(int argc, _TCHAR* argv[])</div><div style="font-size:14px">{</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>av_register_all();</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>avdevice_register_all();</div><div style="font-size:14px"><span style="white-space:pre-wrap">        </span>if (OpenVideoCapture() < 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">  </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>return -1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>if (OpenAudioCapture() < 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">  </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>return -1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>if (OpenOutPut() < 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">        </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>return -1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span></div><div style="font-size:14px"><span style="white-space:pre-wrap">        </span>InitializeCriticalSection(&VideoSection);</div><div style="font-size:14px"><span style="white-space:pre-wrap">   </span>InitializeCriticalSection(&AudioSection);</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap"> </span>AVFrame *picture = av_frame_alloc();</div><div style="font-size:14px"><span style="white-space:pre-wrap">    </span>int size = avpicture_get_size(pFormatCtx_Out->streams[VideoIndex]->codec->pix_fmt, </div><div style="font-size:14px"><span style="white-space:pre-wrap">           </span>pFormatCtx_Out->streams[VideoIndex]->codec->width, pFormatCtx_Out->streams[VideoIndex]->codec->height);</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>picture_buf = new uint8_t[size];</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>avpicture_fill((AVPicture *)picture, picture_buf, </div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>pFormatCtx_Out->streams[VideoIndex]->codec->pix_fmt, </div><div style="font-size:14px"><span style="white-space:pre-wrap">         </span>pFormatCtx_Out->streams[VideoIndex]->codec->width, </div><div style="font-size:14px"><span style="white-space:pre-wrap">           </span>pFormatCtx_Out->streams[VideoIndex]->codec->height);</div><div style="font-size:14px"><br></div><div style="font-size:14px"><br></div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>//star cap screen thread</div><div style="font-size:14px"><span style="white-space:pre-wrap">        </span>CreateThread( NULL, 0, ScreenCapThreadProc, 0, 0, NULL);</div><div style="font-size:14px"><span style="white-space:pre-wrap">        </span>//star cap audio thread</div><div style="font-size:14px"><span style="white-space:pre-wrap"> </span>CreateThread( NULL, 0, AudioCapThreadProc, 0, 0, NULL);</div><div style="font-size:14px"><span style="white-space:pre-wrap"> </span>int64_t cur_pts_v=0,cur_pts_a=0;</div><div style="font-size:14px"><span style="white-space:pre-wrap">        </span>int VideoFrameIndex = 0, AudioFrameIndex = 0;</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap"> </span>while(1)</div><div style="font-size:14px"><span style="white-space:pre-wrap">        </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>if (_kbhit() != 0 && bCap)</div><div style="font-size:14px"><span style="white-space:pre-wrap">              </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>bCap = false;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                   </span>Sleep(2000);//</div><div style="font-size:14px"><span style="white-space:pre-wrap">          </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>if (fifo_audio && fifo_video)</div><div style="font-size:14px"><span style="white-space:pre-wrap">           </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>int sizeAudio = av_audio_fifo_size(fifo_audio);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                 </span>int sizeVideo = av_fifo_size(fifo_video);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>//</div><div style="font-size:14px"><span style="white-space:pre-wrap">                      </span>if (av_audio_fifo_size(fifo_audio) <= pFormatCtx_Out->streams[AudioIndex]->codec->frame_size && </div><div style="font-size:14px"><span style="white-space:pre-wrap">                           </span>av_fifo_size(fifo_video) <= frame_size && !bCap)</div><div style="font-size:14px"><span style="white-space:pre-wrap">                     </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>break;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                  </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>}</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>if(av_compare_ts(cur_pts_v, pFormatCtx_Out->streams[VideoIndex]->time_base, </div><div style="font-size:14px"><span style="white-space:pre-wrap">                     </span>cur_pts_a,pFormatCtx_Out->streams[AudioIndex]->time_base) <= 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">                </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>//read data from fifo</div><div style="font-size:14px"><span style="white-space:pre-wrap">                   </span>if (av_fifo_size(fifo_video) < frame_size && !bCap)</div><div style="font-size:14px"><span style="white-space:pre-wrap">                  </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>cur_pts_v = 0x7fffffffffffffff;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                 </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>if(av_fifo_size(fifo_video) >= size)</div><div style="font-size:14px"><span style="white-space:pre-wrap">                 </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>EnterCriticalSection(&VideoSection);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                </span>av_fifo_generic_read(fifo_video, picture_buf, size, NULL);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                              </span>LeaveCriticalSection(&VideoSection);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                </span></div><div style="font-size:14px"><span style="white-space:pre-wrap">                                </span>avpicture_fill((AVPicture *)picture, picture_buf, </div><div style="font-size:14px"><span style="white-space:pre-wrap">                                     </span>pFormatCtx_Out->streams[VideoIndex]->codec->pix_fmt, </div><div style="font-size:14px"><span style="white-space:pre-wrap">                                 </span>pFormatCtx_Out->streams[VideoIndex]->codec->width, </div><div style="font-size:14px"><span style="white-space:pre-wrap">                                   </span>pFormatCtx_Out->streams[VideoIndex]->codec->height);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                           </span></div><div style="font-size:14px"><span style="white-space:pre-wrap">                                </span>//pts = n * ((1 / timbase)/ fps);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                           </span>picture->pts = VideoFrameIndex * ((pFormatCtx_Video->streams[0]->time_base.den / pFormatCtx_Video->streams[0]->time_base.num) / 15);</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">                           </span>int got_picture = 0;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                            </span>AVPacket pkt;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                           </span>av_init_packet(&pkt);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span></div><div style="font-size:14px"><span style="white-space:pre-wrap">                                </span>pkt.data = NULL;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                </span>pkt.size = 0;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                           </span>int ret = avcodec_encode_video2(pFormatCtx_Out->streams[VideoIndex]->codec, &pkt, picture, &got_picture);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                         </span>if(ret < 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">                          </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                       </span>//</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                      </span>continue;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span></div><div style="font-size:14px"><span style="white-space:pre-wrap">                                </span>if (got_picture==1)</div><div style="font-size:14px"><span style="white-space:pre-wrap">                             </span>{</div><div style="font-size:14px"> <span style="white-space:pre-wrap">                                     </span>pkt.stream_index = VideoIndex;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                  </span>pkt.pts = av_rescale_q_rnd(pkt.pts, pFormatCtx_Video->streams[0]->time_base, </div><div style="font-size:14px"><span style="white-space:pre-wrap">                                            </span>pFormatCtx_Out->streams[VideoIndex]->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));  </div><div style="font-size:14px"><span style="white-space:pre-wrap">                                 </span>pkt.dts = av_rescale_q_rnd(pkt.dts,  pFormatCtx_Video->streams[0]->time_base, </div><div style="font-size:14px"><span style="white-space:pre-wrap">                                          </span>pFormatCtx_Out->streams[VideoIndex]->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));  </div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">                                       </span>pkt.duration = ((pFormatCtx_Out->streams[0]->time_base.den / pFormatCtx_Out->streams[0]->time_base.num) / 15);</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">                                    </span>cur_pts_v = pkt.pts;</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">                                  </span>ret = av_interleaved_write_frame(pFormatCtx_Out, &pkt);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                     </span>//delete[] pkt.data;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                    </span>av_free_packet(&pkt);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>VideoFrameIndex++;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                      </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>else</div><div style="font-size:14px"><span style="white-space:pre-wrap">            </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>if (NULL == fifo_audio)</div><div style="font-size:14px"><span style="white-space:pre-wrap">                 </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>continue;//</div><div style="font-size:14px"><span style="white-space:pre-wrap">                     </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>if (av_audio_fifo_size(fifo_audio) < pFormatCtx_Out->streams[AudioIndex]->codec->frame_size && !bCap)</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>cur_pts_a = 0x7fffffffffffffff;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                 </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>if(av_audio_fifo_size(fifo_audio) >= </div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>(pFormatCtx_Out->streams[AudioIndex]->codec->frame_size > 0 ? pFormatCtx_Out->streams[AudioIndex]->codec->frame_size : 1024))</div><div style="font-size:14px"><span style="white-space:pre-wrap">                      </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>AVFrame *frame;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                         </span>frame = av_frame_alloc();</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>frame->nb_samples = pFormatCtx_Out->streams[AudioIndex]->codec->frame_size>0 ? pFormatCtx_Out->streams[AudioIndex]->codec->frame_size: 1024;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                            </span>frame->channel_layout = pFormatCtx_Out->streams[AudioIndex]->codec->channel_layout;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                         </span>frame->format = pFormatCtx_Out->streams[AudioIndex]->codec->sample_fmt;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                             </span>frame->sample_rate = pFormatCtx_Out->streams[AudioIndex]->codec->sample_rate;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>av_frame_get_buffer(frame, 0);</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">                                </span>EnterCriticalSection(&AudioSection);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                </span>av_audio_fifo_read(fifo_audio, (void **)frame->data, </div><div style="font-size:14px"><span style="white-space:pre-wrap">                                       </span>(pFormatCtx_Out->streams[AudioIndex]->codec->frame_size > 0 ? pFormatCtx_Out->streams[AudioIndex]->codec->frame_size : 1024));</div><div style="font-size:14px"><span style="white-space:pre-wrap">                             </span>LeaveCriticalSection(&AudioSection);</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">                              </span>if (pFormatCtx_Out->streams[0]->codec->sample_fmt != pFormatCtx_Audio->streams[AudioIndex]->codec->sample_fmt </div><div style="font-size:14px"><span style="white-space:pre-wrap">                                       </span>|| pFormatCtx_Out->streams[0]->codec->channels != pFormatCtx_Audio->streams[AudioIndex]->codec->channels </div><div style="font-size:14px"><span style="white-space:pre-wrap">                                    </span>|| pFormatCtx_Out->streams[0]->codec->sample_rate != pFormatCtx_Audio->streams[AudioIndex]->codec->sample_rate)</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                       </span>//</div><div style="font-size:14px"><span style="white-space:pre-wrap">                              </span>}</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">                             </span>AVPacket pkt_out;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>av_init_packet(&pkt_out);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                           </span>int got_picture = -1;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                           </span>pkt_out.data = NULL;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                            </span>pkt_out.size = 0;</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">                             </span>frame->pts = AudioFrameIndex * pFormatCtx_Out->streams[AudioIndex]->codec->frame_size;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                              </span>if (avcodec_encode_audio2(pFormatCtx_Out->streams[AudioIndex]->codec, &pkt_out, frame, &got_picture) < 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">                              </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                       </span>printf("can not decoder a frame");</div><div style="font-size:14px"><span style="white-space:pre-wrap">                            </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>av_frame_free(&frame);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                              </span>if (got_picture) </div><div style="font-size:14px"><span style="white-space:pre-wrap">                              </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                       </span>pkt_out.stream_index = AudioIndex;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                      </span>pkt_out.pts = AudioFrameIndex * pFormatCtx_Out->streams[AudioIndex]->codec->frame_size;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                        </span>pkt_out.dts = AudioFrameIndex * pFormatCtx_Out->streams[AudioIndex]->codec->frame_size;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                        </span>pkt_out.duration = pFormatCtx_Out->streams[AudioIndex]->codec->frame_size;</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">                                   </span>cur_pts_a = pkt_out.pts;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                        </span></div><div style="font-size:14px"><span style="white-space:pre-wrap">                                        </span>int ret = av_interleaved_write_frame(pFormatCtx_Out, &pkt_out);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                     </span>av_free_packet(&pkt_out);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                           </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>AudioFrameIndex++;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                      </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>}</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>delete[] picture_buf;</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap"> </span>delete[]frame_buf;</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>av_fifo_free(fifo_video);</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>av_audio_fifo_free(fifo_audio);</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>av_write_trailer(pFormatCtx_Out);</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>avio_close(pFormatCtx_Out->pb);</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>avformat_free_context(pFormatCtx_Out);</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">        </span>if (pFormatCtx_Video != NULL)</div><div style="font-size:14px"><span style="white-space:pre-wrap">   </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>avformat_close_input(&pFormatCtx_Video);</div><div style="font-size:14px"><span style="white-space:pre-wrap">            </span>pFormatCtx_Video = NULL;</div><div style="font-size:14px"><span style="white-space:pre-wrap">        </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>if (pFormatCtx_Audio != NULL)</div><div style="font-size:14px"><span style="white-space:pre-wrap">   </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>avformat_close_input(&pFormatCtx_Audio);</div><div style="font-size:14px"><span style="white-space:pre-wrap">            </span>pFormatCtx_Audio = NULL;</div><div style="font-size:14px"><span style="white-space:pre-wrap">        </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>if (NULL != img_convert_ctx)</div><div style="font-size:14px"><span style="white-space:pre-wrap">    </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>sws_freeContext(img_convert_ctx);</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>img_convert_ctx = NULL;</div><div style="font-size:14px"><span style="white-space:pre-wrap"> </span>}</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>return 0;</div><div style="font-size:14px">}</div><div style="font-size:14px"><br></div><div style="font-size:14px">DWORD WINAPI ScreenCapThreadProc( LPVOID lpParam )</div><div style="font-size:14px">{</div><div style="font-size:14px"><span style="white-space:pre-wrap"> </span>AVPacket packet;/* = (AVPacket *)av_malloc(sizeof(AVPacket))*/;</div><div style="font-size:14px"><span style="white-space:pre-wrap"> </span>int got_picture;</div><div style="font-size:14px"><span style="white-space:pre-wrap">        </span>AVFrame<span style="white-space:pre-wrap"> </span>*pFrame;</div><div style="font-size:14px"><span style="white-space:pre-wrap">        </span>pFrame= av_frame_alloc();</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>AVFrame *picture = av_frame_alloc();</div><div style="font-size:14px"><span style="white-space:pre-wrap">    </span>int size = avpicture_get_size(pFormatCtx_Out->streams[VideoIndex]->codec->pix_fmt, </div><div style="font-size:14px"><span style="white-space:pre-wrap">           </span>pFormatCtx_Out->streams[VideoIndex]->codec->width, pFormatCtx_Out->streams[VideoIndex]->codec->height);</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>//picture_buf = new uint8_t[size];</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">    </span>avpicture_fill((AVPicture *)picture, picture_buf, </div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>pFormatCtx_Out->streams[VideoIndex]->codec->pix_fmt, </div><div style="font-size:14px"><span style="white-space:pre-wrap">         </span>pFormatCtx_Out->streams[VideoIndex]->codec->width, </div><div style="font-size:14px"><span style="white-space:pre-wrap">           </span>pFormatCtx_Out->streams[VideoIndex]->codec->height);</div><div style="font-size:14px"><br></div><div style="font-size:14px">// <span style="white-space:pre-wrap">     </span>FILE *p = NULL;</div><div style="font-size:14px">// <span style="white-space:pre-wrap">     </span>p = fopen("proc_test.yuv", "wb+");</div><div style="font-size:14px"><span style="white-space:pre-wrap">  </span>av_init_packet(&packet);</div><div style="font-size:14px"><span style="white-space:pre-wrap">    </span>int height = pFormatCtx_Out->streams[VideoIndex]->codec->height;</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>int width = pFormatCtx_Out->streams[VideoIndex]->codec->width;</div><div style="font-size:14px"><span style="white-space:pre-wrap"> </span>int y_size=height*width;</div><div style="font-size:14px"><span style="white-space:pre-wrap">        </span>while(bCap)</div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>packet.data = NULL;</div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>packet.size = 0;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                </span>if (av_read_frame(pFormatCtx_Video, &packet) < 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">                </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>av_free_packet(&packet);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                    </span>continue;</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>if(packet.stream_index == 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">            </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>if (avcodec_decode_video2(pCodecCtx_Video, pFrame, &got_picture, &packet) < 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>printf("Decode Error.(解码错误)\n");</div><div style="font-size:14px"><span style="white-space:pre-wrap">                          </span>continue;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>if (got_picture)</div><div style="font-size:14px"><span style="white-space:pre-wrap">                        </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, </div><div style="font-size:14px"><span style="white-space:pre-wrap">                                    </span>pFormatCtx_Out->streams[VideoIndex]->codec->height, picture->data, picture->linesize);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                           </span></div><div style="font-size:14px"><span style="white-space:pre-wrap">                                </span>if (av_fifo_space(fifo_video) >= size)</div><div style="font-size:14px"><span style="white-space:pre-wrap">                               </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                       </span>EnterCriticalSection(&VideoSection);<span style="white-space:pre-wrap">                                        </span></div><div style="font-size:14px"><span style="white-space:pre-wrap">                                        </span>av_fifo_generic_write(fifo_video, picture->data[0], y_size, NULL);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                   </span>av_fifo_generic_write(fifo_video, picture->data[1], y_size/4, NULL);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                 </span>av_fifo_generic_write(fifo_video, picture->data[2], y_size/4, NULL);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                 </span>LeaveCriticalSection(&VideoSection);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                                </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>av_free_packet(&packet);</div><div style="font-size:14px"><span style="white-space:pre-wrap">            </span>//Sleep(50);</div><div style="font-size:14px"><span style="white-space:pre-wrap">    </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>av_frame_free(&pFrame);</div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>av_frame_free(&picture);</div><div style="font-size:14px"><span style="white-space:pre-wrap">    </span>//delete[] picture_buf;</div><div style="font-size:14px"><span style="white-space:pre-wrap"> </span>return 0;</div><div style="font-size:14px">}</div><div style="font-size:14px"><br></div><div style="font-size:14px">DWORD WINAPI AudioCapThreadProc( LPVOID lpParam )</div><div style="font-size:14px">{</div><div style="font-size:14px"><span style="white-space:pre-wrap">  </span>AVPacket pkt;</div><div style="font-size:14px"><span style="white-space:pre-wrap">   </span>AVFrame *frame;</div><div style="font-size:14px"><span style="white-space:pre-wrap"> </span>frame = av_frame_alloc();</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>int gotframe;</div><div style="font-size:14px"><span style="white-space:pre-wrap">   </span>while(bCap)</div><div style="font-size:14px"><span style="white-space:pre-wrap">     </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>pkt.data = NULL;</div><div style="font-size:14px"><span style="white-space:pre-wrap">                </span>pkt.size = 0;</div><div style="font-size:14px"><span style="white-space:pre-wrap">           </span>if(av_read_frame(pFormatCtx_Audio,&pkt) < 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>av_free_packet(&pkt);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>continue;</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span></div><div style="font-size:14px"><span style="white-space:pre-wrap">                </span>if (avcodec_decode_audio4(pFormatCtx_Audio->streams[0]->codec, frame, &gotframe, &pkt) < 0)</div><div style="font-size:14px"><span style="white-space:pre-wrap">            </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>av_frame_free(&frame);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                      </span>printf("can not decoder a frame");</div><div style="font-size:14px"><span style="white-space:pre-wrap">                    </span>break;</div><div style="font-size:14px"><span style="white-space:pre-wrap">          </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">               </span>av_free_packet(&pkt);</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>if (!gotframe)</div><div style="font-size:14px"><span style="white-space:pre-wrap">          </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>continue;//</div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>}</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>if (NULL == fifo_audio)</div><div style="font-size:14px"><span style="white-space:pre-wrap">         </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>fifo_audio = av_audio_fifo_alloc(pFormatCtx_Audio->streams[0]->codec->sample_fmt, </div><div style="font-size:14px"><span style="white-space:pre-wrap">                            </span>pFormatCtx_Audio->streams[0]->codec->channels, 30 * frame->nb_samples);</div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>}</div><div style="font-size:14px"><br></div><div style="font-size:14px"><span style="white-space:pre-wrap">             </span>int buf_space = av_audio_fifo_space(fifo_audio);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                </span>if (av_audio_fifo_space(fifo_audio) >= frame->nb_samples)</div><div style="font-size:14px"><span style="white-space:pre-wrap">         </span>{</div><div style="font-size:14px"><span style="white-space:pre-wrap">                       </span>EnterCriticalSection(&AudioSection);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                        </span>av_audio_fifo_write(fifo_audio, (void **)frame->data, frame->nb_samples);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                 </span>LeaveCriticalSection(&AudioSection);</div><div style="font-size:14px"><span style="white-space:pre-wrap">                </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>}</div><div style="font-size:14px"><span style="white-space:pre-wrap">       </span>av_frame_free(&frame);</div><div style="font-size:14px"><span style="white-space:pre-wrap">      </span>return 0;</div><div style="font-size:14px">}[/code]</div></div><div class="gmail_extra"><br><div class="gmail_quote">2016-05-17 23:36 GMT+08:00 汤明辉 <span dir="ltr"><<a href="mailto:vbtangmh@gmail.com" target="_blank">vbtangmh@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>using VS2010 in Win7 x64, ffmpeg 2.4.5,</div><div>i download a demo to record my screen(with audio record ). </div><div>i modified the program so that it can work in 3 threads. but  every time i close the thread i found it have about 40MB memory remained. (every time i start->close thread, memory occupancy increases 40MB) </div><div>but the VS didn't detect any memory leak.</div><div>i'm a newcomer to ffmpeg, i wonder if there are any resoures i didn't free?</div><div>ps. i run the demo step by step, and i found it has the same problem, until it quit the main function, it still have  40MB memory occupancy. And i found the memory increase obviously in the screen capture thread,but when it increased to about 100MB, it won't increase, tben i quit the program, it will have 40MB memory left. i fell confused.</div><div>here is my demo code.</div><div><br></div><div>[code]#include "stdafx.h"</div><div><br></div><div>#ifdef<span style="white-space:pre-wrap">    </span>__cplusplus</div><div>extern "C"</div><div>{</div><div>#endif</div><div>#include "libavcodec/avcodec.h"</div><div>#include "libavformat/avformat.h"</div><div>#include "libswscale/swscale.h"</div><div>#include "libavdevice/avdevice.h"</div><div>#include "libavutil/audio_fifo.h"</div><div><br></div><div>#pragma comment(lib, "avcodec.lib")</div><div>#pragma comment(lib, "avformat.lib")</div><div>#pragma comment(lib, "avutil.lib")</div><div>#pragma comment(lib, "avdevice.lib")</div><div>#pragma comment(lib, "avfilter.lib")</div><div><br></div><div>//#pragma comment(lib, "avfilter.lib")</div><div>//#pragma comment(lib, "postproc.lib")</div><div>//#pragma comment(lib, "swresample.lib")</div><div>#pragma comment(lib, "swscale.lib")</div><div>#ifdef __cplusplus</div><div>};</div><div>#endif</div><div><br></div><div>AVFormatContext<span style="white-space:pre-wrap">      </span>*pFormatCtx_Video = NULL, *pFormatCtx_Audio = NULL, *pFormatCtx_Out = NULL;</div><div>AVCodecContext<span style="white-space:pre-wrap">        </span>*pCodecCtx_Video;</div><div>AVCodec<span style="white-space:pre-wrap">                 </span>*pCodec_Video;</div><div>AVFifoBuffer<span style="white-space:pre-wrap">       </span>*fifo_video = NULL;</div><div>AVAudioFifo<span style="white-space:pre-wrap">           </span>*fifo_audio = NULL;</div><div>int VideoIndex, AudioIndex;</div><div><br></div><div>CRITICAL_SECTION AudioSection, VideoSection;</div><div><br></div><div><br></div><div><br></div><div>SwsContext *img_convert_ctx;</div><div>int frame_size = 0;</div><div><br></div><div>uint8_t *picture_buf = NULL, *frame_buf = NULL;</div><div><br></div><div>bool bCap = true;</div><div><br></div><div>DWORD WINAPI ScreenCapThreadProc( LPVOID lpParam );</div><div>DWORD WINAPI AudioCapThreadProc( LPVOID lpParam );</div><div><br></div><div>int OpenVideoCapture()</div><div>{</div><div><span style="white-space:pre-wrap">      </span>AVInputFormat *ifmt=av_find_input_format("gdigrab");</div><div><span style="white-space:pre-wrap">   </span>//</div><div><span style="white-space:pre-wrap">       </span>AVDictionary *options = NULL;</div><div><span style="white-space:pre-wrap">    </span>av_dict_set(&options, "framerate", "15", NULL);</div><div><span style="white-space:pre-wrap">  </span>//av_dict_set(&options,"offset_x","20",0);</div><div><span style="white-space:pre-wrap">       </span>//The distance from the top edge of the screen or desktop</div><div><span style="white-space:pre-wrap">        </span>//av_dict_set(&options,"offset_y","40",0);</div><div><span style="white-space:pre-wrap">       </span>//Video frame size. The default is to capture the full screen</div><div><span style="white-space:pre-wrap">    </span>//av_dict_set(&options,"video_size","320x240",0);</div><div><span style="white-space:pre-wrap">        </span>if(avformat_open_input(&pFormatCtx_Video, "desktop", ifmt, &options)!=0)</div><div><span style="white-space:pre-wrap">       </span>{</div><div><span style="white-space:pre-wrap">                </span>printf("Couldn't open input stream.(无法打开视频输入流)\n");</div><div><span style="white-space:pre-wrap">          </span>return -1;</div><div><span style="white-space:pre-wrap">       </span>}</div><div><span style="white-space:pre-wrap">        </span>if(avformat_find_stream_info(pFormatCtx_Video,NULL)<0)</div><div><span style="white-space:pre-wrap">        </span>{</div><div><span style="white-space:pre-wrap">                </span>printf("Couldn't find stream information.(无法获取视频流信息)\n");</div><div><span style="white-space:pre-wrap">            </span>return -1;</div><div><span style="white-space:pre-wrap">       </span>}</div><div><span style="white-space:pre-wrap">        </span>if (pFormatCtx_Video->streams[0]->codec->codec_type != AVMEDIA_TYPE_VIDEO)</div><div><span style="white-space:pre-wrap">      </span>{</div><div><span style="white-space:pre-wrap">                </span>printf("Couldn't find video stream information.(无法获取视频流信息)\n");</div><div><span style="white-space:pre-wrap">              </span>return -1;</div><div><span style="white-space:pre-wrap">       </span>}</div><div><span style="white-space:pre-wrap">        </span>pCodecCtx_Video = pFormatCtx_Video->streams[0]->codec;</div><div><span style="white-space:pre-wrap">     </span>pCodec_Video = avcodec_find_decoder(pCodecCtx_Video->codec_id);</div><div><span style="white-space:pre-wrap">       </span>if(pCodec_Video == NULL)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap">                </span>printf("Codec not found.(没有找到解码器)\n");</div><div><span style="white-space:pre-wrap">               </span>return -1;</div><div><span style="white-space:pre-wrap">       </span>}</div><div><span style="white-space:pre-wrap">        </span>if(avcodec_open2(pCodecCtx_Video, pCodec_Video, NULL) < 0)</div><div><span style="white-space:pre-wrap">    </span>{</div><div><span style="white-space:pre-wrap">                </span>printf("Could not open codec.(无法打开解码器)\n");</div><div><span style="white-space:pre-wrap">          </span>return -1;</div><div><span style="white-space:pre-wrap">       </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span></div><div><br></div><div><span style="white-space:pre-wrap">        </span>img_convert_ctx = sws_getContext(pCodecCtx_Video->width, pCodecCtx_Video->height, pCodecCtx_Video->pix_fmt, </div><div><span style="white-space:pre-wrap">           </span>pCodecCtx_Video->width, pCodecCtx_Video->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); </div><div><br></div><div><span style="white-space:pre-wrap">       </span>frame_size = avpicture_get_size(pCodecCtx_Video->pix_fmt, pCodecCtx_Video->width, pCodecCtx_Video->height);</div><div><span style="white-space:pre-wrap">     </span>//</div><div><span style="white-space:pre-wrap">       </span>fifo_video = av_fifo_alloc(30 * avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx_Video->width, pCodecCtx_Video->height));</div><div><br></div><div><span style="white-space:pre-wrap"> </span>return 0;</div><div>}</div><div><br></div><div>static char *dup_wchar_to_utf8(wchar_t *w)</div><div>{</div><div><span style="white-space:pre-wrap">      </span>char *s = NULL;</div><div><span style="white-space:pre-wrap">  </span>int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);</div><div><span style="white-space:pre-wrap">      </span>s = (char *) av_malloc(l);</div><div><span style="white-space:pre-wrap">       </span>if (s)</div><div><span style="white-space:pre-wrap">           </span>WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);</div><div><span style="white-space:pre-wrap">      </span>return s;</div><div>}</div><div><br></div><div>int OpenAudioCapture()</div><div>{</div><div><span style="white-space:pre-wrap">  </span>//</div><div><span style="white-space:pre-wrap">       </span>AVInputFormat *pAudioInputFmt = av_find_input_format("dshow");</div><div><span style="white-space:pre-wrap"> </span></div><div><span style="white-space:pre-wrap"> </span>//</div><div><span style="white-space:pre-wrap">       </span>char * psDevName = dup_wchar_to_utf8(L"audio=virtual-audio-capturer");</div><div><br></div><div><span style="white-space:pre-wrap">        </span>if (avformat_open_input(&pFormatCtx_Audio, psDevName, pAudioInputFmt,NULL) < 0)</div><div><span style="white-space:pre-wrap">   </span>{</div><div><span style="white-space:pre-wrap">                </span>printf("Couldn't open input stream.(无法打开音频输入流)\n");</div><div><span style="white-space:pre-wrap">          </span>return -1;</div><div><span style="white-space:pre-wrap">       </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>if(avformat_find_stream_info(pFormatCtx_Audio,NULL)<0)  </div><div><span style="white-space:pre-wrap">             </span>return -1; </div><div><span style="white-space:pre-wrap">     </span></div><div><span style="white-space:pre-wrap"> </span>if(pFormatCtx_Audio->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO)</div><div><span style="white-space:pre-wrap">       </span>{</div><div><span style="white-space:pre-wrap">                </span>printf("Couldn't find video stream information.(无法获取音频流信息)\n");</div><div><span style="white-space:pre-wrap">              </span>return -1;</div><div><span style="white-space:pre-wrap">       </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>AVCodec *tmpCodec = avcodec_find_decoder(pFormatCtx_Audio->streams[0]->codec->codec_id);</div><div><span style="white-space:pre-wrap">        </span>if(0 > avcodec_open2(pFormatCtx_Audio->streams[0]->codec, tmpCodec, NULL))</div><div><span style="white-space:pre-wrap">      </span>{</div><div><span style="white-space:pre-wrap">                </span>printf("can not find or open audio decoder!\n");</div><div><span style="white-space:pre-wrap">       </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span></div><div><br></div><div><span style="white-space:pre-wrap">        </span>return 0;</div><div>}</div><div><br></div><div>int OpenOutPut()</div><div>{</div><div><span style="white-space:pre-wrap">        </span>AVStream *pVideoStream = NULL, *pAudioStream = NULL;</div><div><span style="white-space:pre-wrap">     </span>const char *outFileName = "test.mp4";</div><div><span style="white-space:pre-wrap">  </span>avformat_alloc_output_context2(&pFormatCtx_Out, NULL, NULL, outFileName);</div><div><br></div><div><span style="white-space:pre-wrap">   </span>if (pFormatCtx_Video->streams[0]->codec->codec_type == AVMEDIA_TYPE_VIDEO)</div><div><span style="white-space:pre-wrap">      </span>{</div><div><span style="white-space:pre-wrap">                </span>VideoIndex = 0;</div><div><span style="white-space:pre-wrap">          </span>pVideoStream = avformat_new_stream(pFormatCtx_Out, NULL);</div><div><br></div><div><span style="white-space:pre-wrap">               </span>if (!pVideoStream)</div><div><span style="white-space:pre-wrap">               </span>{</div><div><span style="white-space:pre-wrap">                        </span>printf("can not new stream for output!\n");</div><div><span style="white-space:pre-wrap">                    </span>return -1;</div><div><span style="white-space:pre-wrap">               </span>}</div><div><br></div><div><span style="white-space:pre-wrap">               </span>//set codec context param</div><div><span style="white-space:pre-wrap">                </span>pVideoStream->codec->codec = avcodec_find_encoder(AV_CODEC_ID_MPEG4);</div><div><span style="white-space:pre-wrap">              </span>pVideoStream->codec->height = pFormatCtx_Video->streams[0]->codec->height;</div><div><span style="white-space:pre-wrap">                </span>pVideoStream->codec->width = pFormatCtx_Video->streams[0]->codec->width;</div><div><span style="white-space:pre-wrap">          </span></div><div><span style="white-space:pre-wrap">         </span>pVideoStream->codec->time_base = pFormatCtx_Video->streams[0]->codec->time_base;</div><div><span style="white-space:pre-wrap">          </span>pVideoStream->codec->sample_aspect_ratio = pFormatCtx_Video->streams[0]->codec->sample_aspect_ratio;</div><div><span style="white-space:pre-wrap">              </span>// take first format from list of supported formats</div><div><span style="white-space:pre-wrap">              </span>pVideoStream->codec->pix_fmt = pFormatCtx_Out->streams[VideoIndex]->codec->codec->pix_fmts[0];</div><div><br></div><div><span style="white-space:pre-wrap">                </span>//open encoder</div><div><span style="white-space:pre-wrap">           </span>if (!pVideoStream->codec->codec)</div><div><span style="white-space:pre-wrap">           </span>{</div><div><span style="white-space:pre-wrap">                        </span>printf("can not find the encoder!\n");</div><div><span style="white-space:pre-wrap">                 </span>return -1;</div><div><span style="white-space:pre-wrap">               </span>}</div><div><br></div><div><span style="white-space:pre-wrap">               </span>if (pFormatCtx_Out->oformat->flags & AVFMT_GLOBALHEADER)</div><div><span style="white-space:pre-wrap">                       </span>pVideoStream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;</div><div><br></div><div><span style="white-space:pre-wrap">           </span>if ((avcodec_open2(pVideoStream->codec, pVideoStream->codec->codec, NULL)) < 0)</div><div><span style="white-space:pre-wrap">              </span>{</div><div><span style="white-space:pre-wrap">                        </span>printf("can not open the encoder\n");</div><div><span style="white-space:pre-wrap">                  </span>return -1;</div><div><span style="white-space:pre-wrap">               </span>}</div><div><span style="white-space:pre-wrap">        </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>if(pFormatCtx_Audio->streams[0]->codec->codec_type == AVMEDIA_TYPE_AUDIO)</div><div><span style="white-space:pre-wrap">       </span>{</div><div><span style="white-space:pre-wrap">                </span>AVCodecContext *pOutputCodecCtx;</div><div><span style="white-space:pre-wrap">         </span>AudioIndex = 1;</div><div><span style="white-space:pre-wrap">          </span>pAudioStream = avformat_new_stream(pFormatCtx_Out, NULL);</div><div><br></div><div><span style="white-space:pre-wrap">               </span>pAudioStream->codec->codec = avcodec_find_encoder(pFormatCtx_Out->oformat->audio_codec);</div><div><br></div><div><span style="white-space:pre-wrap">            </span>pOutputCodecCtx = pAudioStream->codec;</div><div><br></div><div><span style="white-space:pre-wrap">               </span>pOutputCodecCtx->sample_rate = pFormatCtx_Audio->streams[0]->codec->sample_rate;</div><div><span style="white-space:pre-wrap">             </span>pOutputCodecCtx->channel_layout = pFormatCtx_Out->streams[0]->codec->channel_layout;</div><div><span style="white-space:pre-wrap">         </span>pOutputCodecCtx->channels = av_get_channel_layout_nb_channels(pAudioStream->codec->channel_layout);</div><div><span style="white-space:pre-wrap">             </span>if(pOutputCodecCtx->channel_layout == 0)</div><div><span style="white-space:pre-wrap">              </span>{</div><div><span style="white-space:pre-wrap">                        </span>pOutputCodecCtx->channel_layout = AV_CH_LAYOUT_STEREO;</div><div><span style="white-space:pre-wrap">                        </span>pOutputCodecCtx->channels = av_get_channel_layout_nb_channels(pOutputCodecCtx->channel_layout);</div><div><br></div><div><span style="white-space:pre-wrap">           </span>}</div><div><span style="white-space:pre-wrap">                </span>pOutputCodecCtx->sample_fmt = pAudioStream->codec->codec->sample_fmts[0];</div><div><span style="white-space:pre-wrap">            </span>AVRational time_base={1, pAudioStream->codec->sample_rate};</div><div><span style="white-space:pre-wrap">                </span>pAudioStream->time_base = time_base;</div><div><span style="white-space:pre-wrap">          </span>//audioCodecCtx->time_base = time_base;</div><div><br></div><div><span style="white-space:pre-wrap">              </span>pOutputCodecCtx->codec_tag = 0;  </div><div><span style="white-space:pre-wrap">            </span>if (pFormatCtx_Out->oformat->flags & AVFMT_GLOBALHEADER)  </div><div><span style="white-space:pre-wrap">                    </span>pOutputCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;</div><div><br></div><div><span style="white-space:pre-wrap">          </span>if (avcodec_open2(pOutputCodecCtx, pOutputCodecCtx->codec, 0) < 0)</div><div><span style="white-space:pre-wrap">         </span>{</div><div><span style="white-space:pre-wrap">                        </span>//</div><div><span style="white-space:pre-wrap">                       </span>return -1;</div><div><span style="white-space:pre-wrap">               </span>}</div><div><span style="white-space:pre-wrap">        </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>if (!(pFormatCtx_Out->oformat->flags & AVFMT_NOFILE))</div><div><span style="white-space:pre-wrap">  </span>{</div><div><span style="white-space:pre-wrap">                </span>if(avio_open(&pFormatCtx_Out->pb, outFileName, AVIO_FLAG_WRITE) < 0)</div><div><span style="white-space:pre-wrap">           </span>{</div><div><span style="white-space:pre-wrap">                        </span>printf("can not open output file handle!\n");</div><div><span style="white-space:pre-wrap">                  </span>return -1;</div><div><span style="white-space:pre-wrap">               </span>}</div><div><span style="white-space:pre-wrap">        </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>if(avformat_write_header(pFormatCtx_Out, NULL) < 0)</div><div><span style="white-space:pre-wrap">   </span>{</div><div><span style="white-space:pre-wrap">                </span>printf("can not write the header of the output file!\n");</div><div><span style="white-space:pre-wrap">              </span>return -1;</div><div><span style="white-space:pre-wrap">       </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>return 0;</div><div>}</div><div><br></div><div>int _tmain(int argc, _TCHAR* argv[])</div><div>{</div><div><span style="white-space:pre-wrap">    </span>av_register_all();</div><div><span style="white-space:pre-wrap">       </span>avdevice_register_all();</div><div><span style="white-space:pre-wrap"> </span>if (OpenVideoCapture() < 0)</div><div><span style="white-space:pre-wrap">   </span>{</div><div><span style="white-space:pre-wrap">                </span>return -1;</div><div><span style="white-space:pre-wrap">       </span>}</div><div><span style="white-space:pre-wrap">        </span>if (OpenAudioCapture() < 0)</div><div><span style="white-space:pre-wrap">   </span>{</div><div><span style="white-space:pre-wrap">                </span>return -1;</div><div><span style="white-space:pre-wrap">       </span>}</div><div><span style="white-space:pre-wrap">        </span>if (OpenOutPut() < 0)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap">                </span>return -1;</div><div><span style="white-space:pre-wrap">       </span>}</div><div><span style="white-space:pre-wrap">        </span></div><div><span style="white-space:pre-wrap"> </span>InitializeCriticalSection(&VideoSection);</div><div><span style="white-space:pre-wrap">    </span>InitializeCriticalSection(&AudioSection);</div><div><br></div><div><span style="white-space:pre-wrap">   </span>AVFrame *picture = av_frame_alloc();</div><div><span style="white-space:pre-wrap">     </span>int size = avpicture_get_size(pFormatCtx_Out->streams[VideoIndex]->codec->pix_fmt, </div><div><span style="white-space:pre-wrap">            </span>pFormatCtx_Out->streams[VideoIndex]->codec->width, pFormatCtx_Out->streams[VideoIndex]->codec->height);</div><div><span style="white-space:pre-wrap">        </span>picture_buf = new uint8_t[size];</div><div><br></div><div><span style="white-space:pre-wrap">        </span>avpicture_fill((AVPicture *)picture, picture_buf, </div><div><span style="white-space:pre-wrap">              </span>pFormatCtx_Out->streams[VideoIndex]->codec->pix_fmt, </div><div><span style="white-space:pre-wrap">          </span>pFormatCtx_Out->streams[VideoIndex]->codec->width, </div><div><span style="white-space:pre-wrap">            </span>pFormatCtx_Out->streams[VideoIndex]->codec->height);</div><div><br></div><div><br></div><div><br></div><div><span style="white-space:pre-wrap"> </span>//star cap screen thread</div><div><span style="white-space:pre-wrap"> </span>CreateThread( NULL, 0, ScreenCapThreadProc, 0, 0, NULL);</div><div><span style="white-space:pre-wrap"> </span>//star cap audio thread</div><div><span style="white-space:pre-wrap">  </span>CreateThread( NULL, 0, AudioCapThreadProc, 0, 0, NULL);</div><div><span style="white-space:pre-wrap">  </span>int64_t cur_pts_v=0,cur_pts_a=0;</div><div><span style="white-space:pre-wrap"> </span>int VideoFrameIndex = 0, AudioFrameIndex = 0;</div><div><br></div><div><span style="white-space:pre-wrap">   </span>while(1)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap">                </span>if (_kbhit() != 0 && bCap)</div><div><span style="white-space:pre-wrap">               </span>{</div><div><span style="white-space:pre-wrap">                        </span>bCap = false;</div><div><span style="white-space:pre-wrap">                    </span>Sleep(2000);//</div><div><span style="white-space:pre-wrap">           </span>}</div><div><span style="white-space:pre-wrap">                </span>if (fifo_audio && fifo_video)</div><div><span style="white-space:pre-wrap">            </span>{</div><div><span style="white-space:pre-wrap">                        </span>int sizeAudio = av_audio_fifo_size(fifo_audio);</div><div><span style="white-space:pre-wrap">                  </span>int sizeVideo = av_fifo_size(fifo_video);</div><div><span style="white-space:pre-wrap">                        </span>//</div><div><span style="white-space:pre-wrap">                       </span>if (av_audio_fifo_size(fifo_audio) <= pFormatCtx_Out->streams[AudioIndex]->codec->frame_size && </div><div><span style="white-space:pre-wrap">                            </span>av_fifo_size(fifo_video) <= frame_size && !bCap)</div><div><span style="white-space:pre-wrap">                      </span>{</div><div><span style="white-space:pre-wrap">                                </span>break;</div><div><span style="white-space:pre-wrap">                   </span>}</div><div><span style="white-space:pre-wrap">                </span>}</div><div><br></div><div><span style="white-space:pre-wrap">               </span>if(av_compare_ts(cur_pts_v, pFormatCtx_Out->streams[VideoIndex]->time_base, </div><div><span style="white-space:pre-wrap">                      </span>cur_pts_a,pFormatCtx_Out->streams[AudioIndex]->time_base) <= 0)</div><div><span style="white-space:pre-wrap">         </span>{</div><div><span style="white-space:pre-wrap">                        </span>//read data from fifo</div><div><span style="white-space:pre-wrap">                    </span>if (av_fifo_size(fifo_video) < frame_size && !bCap)</div><div><span style="white-space:pre-wrap">                   </span>{</div><div><span style="white-space:pre-wrap">                                </span>cur_pts_v = 0x7fffffffffffffff;</div><div><span style="white-space:pre-wrap">                  </span>}</div><div><span style="white-space:pre-wrap">                        </span>if(av_fifo_size(fifo_video) >= size)</div><div><span style="white-space:pre-wrap">                  </span>{</div><div><span style="white-space:pre-wrap">                                </span>EnterCriticalSection(&VideoSection);</div><div><span style="white-space:pre-wrap">                         </span>av_fifo_generic_read(fifo_video, picture_buf, size, NULL);</div><div><span style="white-space:pre-wrap">                               </span>LeaveCriticalSection(&VideoSection);</div><div><span style="white-space:pre-wrap">                         </span></div><div><span style="white-space:pre-wrap">                         </span>avpicture_fill((AVPicture *)picture, picture_buf, </div><div><span style="white-space:pre-wrap">                                      </span>pFormatCtx_Out->streams[VideoIndex]->codec->pix_fmt, </div><div><span style="white-space:pre-wrap">                                  </span>pFormatCtx_Out->streams[VideoIndex]->codec->width, </div><div><span style="white-space:pre-wrap">                                    </span>pFormatCtx_Out->streams[VideoIndex]->codec->height);</div><div><span style="white-space:pre-wrap">                            </span></div><div><span style="white-space:pre-wrap">                         </span>//pts = n * ((1 / timbase)/ fps);</div><div><span style="white-space:pre-wrap">                            </span>picture->pts = VideoFrameIndex * ((pFormatCtx_Video->streams[0]->time_base.den / pFormatCtx_Video->streams[0]->time_base.num) / 15);</div><div><br></div><div><span style="white-space:pre-wrap">                             </span>int got_picture = 0;</div><div><span style="white-space:pre-wrap">                             </span>AVPacket pkt;</div><div><span style="white-space:pre-wrap">                            </span>av_init_packet(&pkt);</div><div><span style="white-space:pre-wrap">                                </span></div><div><span style="white-space:pre-wrap">                         </span>pkt.data = NULL;</div><div><span style="white-space:pre-wrap">                         </span>pkt.size = 0;</div><div><span style="white-space:pre-wrap">                            </span>int ret = avcodec_encode_video2(pFormatCtx_Out->streams[VideoIndex]->codec, &pkt, picture, &got_picture);</div><div><span style="white-space:pre-wrap">                          </span>if(ret < 0)</div><div><span style="white-space:pre-wrap">                           </span>{</div><div><span style="white-space:pre-wrap">                                        </span>//</div><div><span style="white-space:pre-wrap">                                       </span>continue;</div><div><span style="white-space:pre-wrap">                                </span>}</div><div><span style="white-space:pre-wrap">                                </span></div><div><span style="white-space:pre-wrap">                         </span>if (got_picture==1)</div><div><span style="white-space:pre-wrap">                              </span>{</div><div> <span style="white-space:pre-wrap">                                      </span>pkt.stream_index = VideoIndex;</div><div><span style="white-space:pre-wrap">                                   </span>pkt.pts = av_rescale_q_rnd(pkt.pts, pFormatCtx_Video->streams[0]->time_base, </div><div><span style="white-space:pre-wrap">                                             </span>pFormatCtx_Out->streams[VideoIndex]->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));  </div><div><span style="white-space:pre-wrap">                                  </span>pkt.dts = av_rescale_q_rnd(pkt.dts,  pFormatCtx_Video->streams[0]->time_base, </div><div><span style="white-space:pre-wrap">                                           </span>pFormatCtx_Out->streams[VideoIndex]->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));  </div><div><br></div><div><span style="white-space:pre-wrap">                                 </span>pkt.duration = ((pFormatCtx_Out->streams[0]->time_base.den / pFormatCtx_Out->streams[0]->time_base.num) / 15);</div><div><br></div><div><span style="white-space:pre-wrap">                                      </span>cur_pts_v = pkt.pts;</div><div><br></div><div><span style="white-space:pre-wrap">                                    </span>ret = av_interleaved_write_frame(pFormatCtx_Out, &pkt);</div><div><span style="white-space:pre-wrap">                                      </span>//delete[] pkt.data;</div><div><span style="white-space:pre-wrap">                                     </span>av_free_packet(&pkt);</div><div><span style="white-space:pre-wrap">                                </span>}</div><div><span style="white-space:pre-wrap">                                </span>VideoFrameIndex++;</div><div><span style="white-space:pre-wrap">                       </span>}</div><div><span style="white-space:pre-wrap">                </span>}</div><div><span style="white-space:pre-wrap">                </span>else</div><div><span style="white-space:pre-wrap">             </span>{</div><div><span style="white-space:pre-wrap">                        </span>if (NULL == fifo_audio)</div><div><span style="white-space:pre-wrap">                  </span>{</div><div><span style="white-space:pre-wrap">                                </span>continue;//</div><div><span style="white-space:pre-wrap">                      </span>}</div><div><span style="white-space:pre-wrap">                        </span>if (av_audio_fifo_size(fifo_audio) < pFormatCtx_Out->streams[AudioIndex]->codec->frame_size && !bCap)</div><div><span style="white-space:pre-wrap">                        </span>{</div><div><span style="white-space:pre-wrap">                                </span>cur_pts_a = 0x7fffffffffffffff;</div><div><span style="white-space:pre-wrap">                  </span>}</div><div><span style="white-space:pre-wrap">                        </span>if(av_audio_fifo_size(fifo_audio) >= </div><div><span style="white-space:pre-wrap">                                </span>(pFormatCtx_Out->streams[AudioIndex]->codec->frame_size > 0 ? pFormatCtx_Out->streams[AudioIndex]->codec->frame_size : 1024))</div><div><span style="white-space:pre-wrap">                       </span>{</div><div><span style="white-space:pre-wrap">                                </span>AVFrame *frame;</div><div><span style="white-space:pre-wrap">                          </span>frame = av_frame_alloc();</div><div><span style="white-space:pre-wrap">                                </span>frame->nb_samples = pFormatCtx_Out->streams[AudioIndex]->codec->frame_size>0 ? pFormatCtx_Out->streams[AudioIndex]->codec->frame_size: 1024;</div><div><span style="white-space:pre-wrap">                             </span>frame->channel_layout = pFormatCtx_Out->streams[AudioIndex]->codec->channel_layout;</div><div><span style="white-space:pre-wrap">                          </span>frame->format = pFormatCtx_Out->streams[AudioIndex]->codec->sample_fmt;</div><div><span style="white-space:pre-wrap">                              </span>frame->sample_rate = pFormatCtx_Out->streams[AudioIndex]->codec->sample_rate;</div><div><span style="white-space:pre-wrap">                                </span>av_frame_get_buffer(frame, 0);</div><div><br></div><div><span style="white-space:pre-wrap">                          </span>EnterCriticalSection(&AudioSection);</div><div><span style="white-space:pre-wrap">                         </span>av_audio_fifo_read(fifo_audio, (void **)frame->data, </div><div><span style="white-space:pre-wrap">                                        </span>(pFormatCtx_Out->streams[AudioIndex]->codec->frame_size > 0 ? pFormatCtx_Out->streams[AudioIndex]->codec->frame_size : 1024));</div><div><span style="white-space:pre-wrap">                              </span>LeaveCriticalSection(&AudioSection);</div><div><br></div><div><span style="white-space:pre-wrap">                                </span>if (pFormatCtx_Out->streams[0]->codec->sample_fmt != pFormatCtx_Audio->streams[AudioIndex]->codec->sample_fmt </div><div><span style="white-space:pre-wrap">                                        </span>|| pFormatCtx_Out->streams[0]->codec->channels != pFormatCtx_Audio->streams[AudioIndex]->codec->channels </div><div><span style="white-space:pre-wrap">                                     </span>|| pFormatCtx_Out->streams[0]->codec->sample_rate != pFormatCtx_Audio->streams[AudioIndex]->codec->sample_rate)</div><div><span style="white-space:pre-wrap">                                </span>{</div><div><span style="white-space:pre-wrap">                                        </span>//</div><div><span style="white-space:pre-wrap">                               </span>}</div><div><br></div><div><span style="white-space:pre-wrap">                               </span>AVPacket pkt_out;</div><div><span style="white-space:pre-wrap">                                </span>av_init_packet(&pkt_out);</div><div><span style="white-space:pre-wrap">                            </span>int got_picture = -1;</div><div><span style="white-space:pre-wrap">                            </span>pkt_out.data = NULL;</div><div><span style="white-space:pre-wrap">                             </span>pkt_out.size = 0;</div><div><br></div><div><span style="white-space:pre-wrap">                               </span>frame->pts = AudioFrameIndex * pFormatCtx_Out->streams[AudioIndex]->codec->frame_size;</div><div><span style="white-space:pre-wrap">                               </span>if (avcodec_encode_audio2(pFormatCtx_Out->streams[AudioIndex]->codec, &pkt_out, frame, &got_picture) < 0)</div><div><span style="white-space:pre-wrap">                               </span>{</div><div><span style="white-space:pre-wrap">                                        </span>printf("can not decoder a frame");</div><div><span style="white-space:pre-wrap">                             </span>}</div><div><span style="white-space:pre-wrap">                                </span>av_frame_free(&frame);</div><div><span style="white-space:pre-wrap">                               </span>if (got_picture) </div><div><span style="white-space:pre-wrap">                               </span>{</div><div><span style="white-space:pre-wrap">                                        </span>pkt_out.stream_index = AudioIndex;</div><div><span style="white-space:pre-wrap">                                       </span>pkt_out.pts = AudioFrameIndex * pFormatCtx_Out->streams[AudioIndex]->codec->frame_size;</div><div><span style="white-space:pre-wrap">                                 </span>pkt_out.dts = AudioFrameIndex * pFormatCtx_Out->streams[AudioIndex]->codec->frame_size;</div><div><span style="white-space:pre-wrap">                                 </span>pkt_out.duration = pFormatCtx_Out->streams[AudioIndex]->codec->frame_size;</div><div><br></div><div><span style="white-space:pre-wrap">                                     </span>cur_pts_a = pkt_out.pts;</div><div><span style="white-space:pre-wrap">                                 </span></div><div><span style="white-space:pre-wrap">                                 </span>int ret = av_interleaved_write_frame(pFormatCtx_Out, &pkt_out);</div><div><span style="white-space:pre-wrap">                                      </span>av_free_packet(&pkt_out);</div><div><span style="white-space:pre-wrap">                            </span>}</div><div><span style="white-space:pre-wrap">                                </span>AudioFrameIndex++;</div><div><span style="white-space:pre-wrap">                       </span>}</div><div><span style="white-space:pre-wrap">                </span>}</div><div><span style="white-space:pre-wrap">        </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>delete[] picture_buf;</div><div><br></div><div><span style="white-space:pre-wrap">   </span>delete[]frame_buf;</div><div><span style="white-space:pre-wrap">       </span>av_fifo_free(fifo_video);</div><div><span style="white-space:pre-wrap">        </span>av_audio_fifo_free(fifo_audio);</div><div><br></div><div><span style="white-space:pre-wrap"> </span>av_write_trailer(pFormatCtx_Out);</div><div><br></div><div><span style="white-space:pre-wrap">       </span>avio_close(pFormatCtx_Out->pb);</div><div><span style="white-space:pre-wrap">       </span>avformat_free_context(pFormatCtx_Out);</div><div><br></div><div><span style="white-space:pre-wrap">  </span>if (pFormatCtx_Video != NULL)</div><div><span style="white-space:pre-wrap">    </span>{</div><div><span style="white-space:pre-wrap">                </span>avformat_close_input(&pFormatCtx_Video);</div><div><span style="white-space:pre-wrap">             </span>pFormatCtx_Video = NULL;</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap">        </span>if (pFormatCtx_Audio != NULL)</div><div><span style="white-space:pre-wrap">    </span>{</div><div><span style="white-space:pre-wrap">                </span>avformat_close_input(&pFormatCtx_Audio);</div><div><span style="white-space:pre-wrap">             </span>pFormatCtx_Audio = NULL;</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap">        </span>if (NULL != img_convert_ctx)</div><div><span style="white-space:pre-wrap">     </span>{</div><div><span style="white-space:pre-wrap">                </span>sws_freeContext(img_convert_ctx);</div><div><span style="white-space:pre-wrap">                </span>img_convert_ctx = NULL;</div><div><span style="white-space:pre-wrap">  </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>return 0;</div><div>}</div><div><br></div><div>DWORD WINAPI ScreenCapThreadProc( LPVOID lpParam )</div><div>{</div><div><span style="white-space:pre-wrap">      </span>AVPacket packet;/* = (AVPacket *)av_malloc(sizeof(AVPacket))*/;</div><div><span style="white-space:pre-wrap">  </span>int got_picture;</div><div><span style="white-space:pre-wrap"> </span>AVFrame<span style="white-space:pre-wrap"> </span>*pFrame;</div><div><span style="white-space:pre-wrap"> </span>pFrame= av_frame_alloc();</div><div><br></div><div><span style="white-space:pre-wrap">       </span>AVFrame *picture = av_frame_alloc();</div><div><span style="white-space:pre-wrap">     </span>int size = avpicture_get_size(pFormatCtx_Out->streams[VideoIndex]->codec->pix_fmt, </div><div><span style="white-space:pre-wrap">            </span>pFormatCtx_Out->streams[VideoIndex]->codec->width, pFormatCtx_Out->streams[VideoIndex]->codec->height);</div><div><span style="white-space:pre-wrap">        </span>//picture_buf = new uint8_t[size];</div><div><br></div><div><span style="white-space:pre-wrap">      </span>avpicture_fill((AVPicture *)picture, picture_buf, </div><div><span style="white-space:pre-wrap">              </span>pFormatCtx_Out->streams[VideoIndex]->codec->pix_fmt, </div><div><span style="white-space:pre-wrap">          </span>pFormatCtx_Out->streams[VideoIndex]->codec->width, </div><div><span style="white-space:pre-wrap">            </span>pFormatCtx_Out->streams[VideoIndex]->codec->height);</div><div><br></div><div>// <span style="white-space:pre-wrap">        </span>FILE *p = NULL;</div><div>// <span style="white-space:pre-wrap">       </span>p = fopen("proc_test.yuv", "wb+");</div><div><span style="white-space:pre-wrap">   </span>av_init_packet(&packet);</div><div><span style="white-space:pre-wrap">     </span>int height = pFormatCtx_Out->streams[VideoIndex]->codec->height;</div><div><span style="white-space:pre-wrap">        </span>int width = pFormatCtx_Out->streams[VideoIndex]->codec->width;</div><div><span style="white-space:pre-wrap">  </span>int y_size=height*width;</div><div><span style="white-space:pre-wrap"> </span>while(bCap)</div><div><span style="white-space:pre-wrap">      </span>{</div><div><span style="white-space:pre-wrap">                </span>packet.data = NULL;</div><div><span style="white-space:pre-wrap">              </span>packet.size = 0;</div><div><span style="white-space:pre-wrap">         </span>if (av_read_frame(pFormatCtx_Video, &packet) < 0)</div><div><span style="white-space:pre-wrap">         </span>{</div><div><span style="white-space:pre-wrap">                        </span>av_free_packet(&packet);</div><div><span style="white-space:pre-wrap">                     </span>continue;</div><div><span style="white-space:pre-wrap">                </span>}</div><div><span style="white-space:pre-wrap">                </span>if(packet.stream_index == 0)</div><div><span style="white-space:pre-wrap">             </span>{</div><div><span style="white-space:pre-wrap">                        </span>if (avcodec_decode_video2(pCodecCtx_Video, pFrame, &got_picture, &packet) < 0)</div><div><span style="white-space:pre-wrap">                        </span>{</div><div><span style="white-space:pre-wrap">                                </span>printf("Decode Error.(解码错误)\n");</div><div><span style="white-space:pre-wrap">                           </span>continue;</div><div><span style="white-space:pre-wrap">                        </span>}</div><div><span style="white-space:pre-wrap">                        </span>if (got_picture)</div><div><span style="white-space:pre-wrap">                 </span>{</div><div><span style="white-space:pre-wrap">                                </span>sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, </div><div><span style="white-space:pre-wrap">                                     </span>pFormatCtx_Out->streams[VideoIndex]->codec->height, picture->data, picture->linesize);</div><div><span style="white-space:pre-wrap">                            </span></div><div><span style="white-space:pre-wrap">                         </span>if (av_fifo_space(fifo_video) >= size)</div><div><span style="white-space:pre-wrap">                                </span>{</div><div><span style="white-space:pre-wrap">                                        </span>EnterCriticalSection(&VideoSection);<span style="white-space:pre-wrap">                                        </span></div><div><span style="white-space:pre-wrap">                                 </span>av_fifo_generic_write(fifo_video, picture->data[0], y_size, NULL);</div><div><span style="white-space:pre-wrap">                                    </span>av_fifo_generic_write(fifo_video, picture->data[1], y_size/4, NULL);</div><div><span style="white-space:pre-wrap">                                  </span>av_fifo_generic_write(fifo_video, picture->data[2], y_size/4, NULL);</div><div><span style="white-space:pre-wrap">                                  </span>LeaveCriticalSection(&VideoSection);</div><div><span style="white-space:pre-wrap">                         </span>}</div><div><span style="white-space:pre-wrap">                        </span>}</div><div><span style="white-space:pre-wrap">                </span>}</div><div><span style="white-space:pre-wrap">                </span>av_free_packet(&packet);</div><div><span style="white-space:pre-wrap">             </span>//Sleep(50);</div><div><span style="white-space:pre-wrap">     </span>}</div><div><span style="white-space:pre-wrap">        </span>av_frame_free(&pFrame);</div><div><span style="white-space:pre-wrap">      </span>av_frame_free(&picture);</div><div><span style="white-space:pre-wrap">     </span>//delete[] picture_buf;</div><div><span style="white-space:pre-wrap">  </span>return 0;</div><div>}</div><div><br></div><div>DWORD WINAPI AudioCapThreadProc( LPVOID lpParam )</div><div>{</div><div><span style="white-space:pre-wrap">       </span>AVPacket pkt;</div><div><span style="white-space:pre-wrap">    </span>AVFrame *frame;</div><div><span style="white-space:pre-wrap">  </span>frame = av_frame_alloc();</div><div><span style="white-space:pre-wrap">        </span>int gotframe;</div><div><span style="white-space:pre-wrap">    </span>while(bCap)</div><div><span style="white-space:pre-wrap">      </span>{</div><div><span style="white-space:pre-wrap">                </span>pkt.data = NULL;</div><div><span style="white-space:pre-wrap">         </span>pkt.size = 0;</div><div><span style="white-space:pre-wrap">            </span>if(av_read_frame(pFormatCtx_Audio,&pkt) < 0)</div><div><span style="white-space:pre-wrap">              </span>{</div><div><span style="white-space:pre-wrap">                        </span>av_free_packet(&pkt);</div><div><span style="white-space:pre-wrap">                        </span>continue;</div><div><span style="white-space:pre-wrap">                </span>}</div><div><span style="white-space:pre-wrap">                </span></div><div><span style="white-space:pre-wrap">         </span>if (avcodec_decode_audio4(pFormatCtx_Audio->streams[0]->codec, frame, &gotframe, &pkt) < 0)</div><div><span style="white-space:pre-wrap">             </span>{</div><div><span style="white-space:pre-wrap">                        </span>av_frame_free(&frame);</div><div><span style="white-space:pre-wrap">                       </span>printf("can not decoder a frame");</div><div><span style="white-space:pre-wrap">                     </span>break;</div><div><span style="white-space:pre-wrap">           </span>}</div><div><span style="white-space:pre-wrap">                </span>av_free_packet(&pkt);</div><div><br></div><div><span style="white-space:pre-wrap">               </span>if (!gotframe)</div><div><span style="white-space:pre-wrap">           </span>{</div><div><span style="white-space:pre-wrap">                        </span>continue;//</div><div><span style="white-space:pre-wrap">              </span>}</div><div><br></div><div><span style="white-space:pre-wrap">               </span>if (NULL == fifo_audio)</div><div><span style="white-space:pre-wrap">          </span>{</div><div><span style="white-space:pre-wrap">                        </span>fifo_audio = av_audio_fifo_alloc(pFormatCtx_Audio->streams[0]->codec->sample_fmt, </div><div><span style="white-space:pre-wrap">                             </span>pFormatCtx_Audio->streams[0]->codec->channels, 30 * frame->nb_samples);</div><div><span style="white-space:pre-wrap">              </span>}</div><div><br></div><div><span style="white-space:pre-wrap">               </span>int buf_space = av_audio_fifo_space(fifo_audio);</div><div><span style="white-space:pre-wrap">         </span>if (av_audio_fifo_space(fifo_audio) >= frame->nb_samples)</div><div><span style="white-space:pre-wrap">          </span>{</div><div><span style="white-space:pre-wrap">                        </span>EnterCriticalSection(&AudioSection);</div><div><span style="white-space:pre-wrap">                 </span>av_audio_fifo_write(fifo_audio, (void **)frame->data, frame->nb_samples);</div><div><span style="white-space:pre-wrap">                  </span>LeaveCriticalSection(&AudioSection);</div><div><span style="white-space:pre-wrap">         </span>}</div><div><span style="white-space:pre-wrap">        </span>}</div><div><span style="white-space:pre-wrap">        </span>av_frame_free(&frame);</div><div><span style="white-space:pre-wrap">       </span>return 0;</div><div>}[/code]</div></div>
</blockquote></div><br></div>