main.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. 
  2. /**
  3. * 叶海辉
  4. * QQ群121376426
  5. * http://blog.yundiantech.com/
  6. */
  7. #include <stdio.h>
  8. extern "C"
  9. {
  10. #include "libavcodec/avcodec.h"
  11. #include "libavformat/avformat.h"
  12. #include "libswscale/swscale.h"
  13. #include "libavdevice/avdevice.h"
  14. }
  15. //'1' Use Dshow
  16. //'0' Use VFW
  17. #define USE_DSHOW 0
  18. //Show Dshow Device
  19. void show_dshow_device(){
  20. AVFormatContext *pFormatCtx = avformat_alloc_context();
  21. AVDictionary* options = NULL;
  22. av_dict_set(&options,"list_devices","true",0);
  23. AVInputFormat *iformat = av_find_input_format("dshow");
  24. printf("========Device Info=============\n");
  25. avformat_open_input(&pFormatCtx,"video=dummy",iformat,&options);
  26. printf("================================\n");
  27. }
  28. //Show Dshow Device Option
  29. void show_dshow_device_option(){
  30. AVFormatContext *pFormatCtx = avformat_alloc_context();
  31. AVDictionary* options = NULL;
  32. av_dict_set(&options,"list_options","true",0);
  33. AVInputFormat *iformat = av_find_input_format("dshow");
  34. printf("========Device Option Info======\n");
  35. avformat_open_input(&pFormatCtx,"video=Integrated Camera",iformat,&options);
  36. printf("================================\n");
  37. }
  38. //Show VFW Device
  39. void show_vfw_device(){
  40. AVFormatContext *pFormatCtx = avformat_alloc_context();
  41. AVInputFormat *iformat = av_find_input_format("vfwcap");
  42. printf("========VFW Device Info======\n");
  43. avformat_open_input(&pFormatCtx,"list",iformat,NULL);
  44. printf("=============================\n");
  45. }
  46. //Show AVFoundation Device
  47. void show_avfoundation_device(){
  48. AVFormatContext *pFormatCtx = avformat_alloc_context();
  49. AVDictionary* options = NULL;
  50. av_dict_set(&options,"list_devices","true",0);
  51. AVInputFormat *iformat = av_find_input_format("avfoundation");
  52. printf("==AVFoundation Device Info===\n");
  53. avformat_open_input(&pFormatCtx,"",iformat,&options);
  54. printf("=============================\n");
  55. }
  56. #define USE_DSHOW 1
  57. int main(int argc, char* argv[])
  58. {
  59. AVFormatContext *pFormatCtx;
  60. int i, videoindex;
  61. AVCodecContext *pCodecCtx;
  62. AVCodec *pCodec;
  63. av_register_all();
  64. avformat_network_init();
  65. avdevice_register_all();//Register Device
  66. //Show Dshow Device
  67. show_dshow_device();
  68. //Show Device Options
  69. // show_dshow_device_option();
  70. //Show VFW Options
  71. // show_vfw_device();
  72. pFormatCtx = avformat_alloc_context();
  73. #if USE_DSHOW
  74. AVInputFormat *ifmt=av_find_input_format("dshow");
  75. //Set own video device's name
  76. if(avformat_open_input(&pFormatCtx,"video=e2eSoft VCam",ifmt,NULL)!=0){
  77. printf("Couldn't open input stream.\n");
  78. return -1;
  79. }
  80. #else
  81. AVInputFormat *ifmt=av_find_input_format("vfwcap");
  82. if(avformat_open_input(&pFormatCtx,"0",ifmt,NULL)!=0){
  83. printf("Couldn't open input stream.\n");
  84. return -1;
  85. }
  86. #endif
  87. if(avformat_find_stream_info(pFormatCtx,NULL)<0)
  88. {
  89. printf("Couldn't find stream information.\n");
  90. return -1;
  91. }
  92. videoindex=-1;
  93. for(i=0; i<pFormatCtx->nb_streams; i++)
  94. {
  95. if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
  96. {
  97. videoindex=i;
  98. }
  99. }
  100. if(videoindex==-1)
  101. {
  102. printf("Couldn't find a video stream.\n");
  103. return -1;
  104. }
  105. pCodecCtx=pFormatCtx->streams[videoindex]->codec;
  106. pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
  107. if(pCodec==NULL)
  108. {
  109. printf("Codec not found.\n");
  110. return -1;
  111. }
  112. if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)
  113. {
  114. printf("Could not open codec.\n");
  115. return -1;
  116. }
  117. AVFrame *pFrame,*pFrameYUV;
  118. pFrame=av_frame_alloc();
  119. pFrameYUV=av_frame_alloc();
  120. uint8_t *out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
  121. avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
  122. int ret, got_picture;
  123. AVPacket *packet=(AVPacket *)av_malloc(sizeof(AVPacket));
  124. FILE *fp_yuv=fopen("output.yuv","wb");
  125. struct SwsContext *img_convert_ctx;
  126. img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
  127. ///这里打印出视频的宽高
  128. fprintf(stderr,"w= %d h= %d\n",pCodecCtx->width, pCodecCtx->height);
  129. ///我们就读取100张图像
  130. for(int i=0;i<100;i++)
  131. {
  132. if(av_read_frame(pFormatCtx, packet) < 0)
  133. {
  134. break;
  135. }
  136. if(packet->stream_index==videoindex)
  137. {
  138. ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
  139. if(ret < 0){
  140. printf("Decode Error.\n");
  141. return -1;
  142. }
  143. if(got_picture)
  144. {
  145. sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
  146. int y_size=pCodecCtx->width*pCodecCtx->height;
  147. fwrite(pFrameYUV->data[0],1,y_size,fp_yuv); //Y
  148. fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv); //U
  149. fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv); //V
  150. }
  151. }
  152. av_free_packet(packet);
  153. }
  154. sws_freeContext(img_convert_ctx);
  155. fclose(fp_yuv);
  156. av_free(out_buffer);
  157. av_free(pFrameYUV);
  158. avcodec_close(pCodecCtx);
  159. avformat_close_input(&pFormatCtx);
  160. return 0;
  161. }