ffmpeg_input_test_2.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*=============================================================================
  2. * # FileName: read_device.c
  3. * # Desc: use ffmpeg read a frame data from v4l2, and convert
  4. * # the output data format
  5. * # Author: licaibiao
  6. * # LastChange: 2017-03-28
  7. * =============================================================================*/
  8. #include <libavcodec/avcodec.h>
  9. #include <libavdevice/avdevice.h>
  10. #include <libavformat/avformat.h>
  11. #include <libavformat/avio.h>
  12. #include <libavutil/file.h>
  13. #include <libavutil/imgutils.h>
  14. #include <libswscale/swscale.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. char* input_name = "video4linux2";
  20. char* file_name = "/dev/video2";
  21. char* out_file = "yuv420.yuv";
  22. void captureOneFrame(void)
  23. {
  24. AVFormatContext* fmtCtx = NULL;
  25. AVInputFormat* inputFmt;
  26. AVPacket* packet;
  27. AVCodecContext* pCodecCtx;
  28. AVCodec* pCodec;
  29. struct SwsContext* sws_ctx;
  30. FILE* fp;
  31. int i;
  32. int ret;
  33. int videoindex;
  34. enum AVPixelFormat dst_pix_fmt = AV_PIX_FMT_NV12; // AV_PIX_FMT_YUV420P;
  35. const char* dst_size = NULL;
  36. const char* src_size = NULL;
  37. uint8_t* src_data[4];
  38. uint8_t* dst_data[4];
  39. int src_linesize[4];
  40. int dst_linesize[4];
  41. int src_bufsize;
  42. int dst_bufsize;
  43. int src_w;
  44. int src_h;
  45. int dst_w = 1920;
  46. int dst_h = 1080;
  47. av_log_set_level(AV_LOG_TRACE);
  48. fp = fopen(out_file, "wb");
  49. if (fp < 0) {
  50. printf("open frame data file failed\n");
  51. return;
  52. }
  53. inputFmt = av_find_input_format(input_name);
  54. if (inputFmt == NULL) {
  55. printf("can not find_input_format\n");
  56. return;
  57. }
  58. AVDictionary *options = NULL;
  59. av_dict_set(&options, "f", "v4l2", 0); // speed probe
  60. av_dict_set(&options, "input_format", "nv12", 0); // format
  61. av_dict_set(&options, "video_size", "1920*1080", 0); // resolution
  62. av_dict_set(&options, "framerate", "15", 0); // framerate
  63. if (avformat_open_input(&fmtCtx, file_name, inputFmt, &options) < 0) {
  64. printf("can not open_input_file\n");
  65. return;
  66. }
  67. av_dict_free(&options);
  68. av_dump_format(fmtCtx, 0, file_name, 0);
  69. printf("fmtCtx->nb_streams = %d \n", fmtCtx->nb_streams);
  70. videoindex = -1;
  71. for (i = 0; i < fmtCtx->nb_streams; i++) {
  72. if (fmtCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  73. videoindex = i;
  74. break;
  75. }
  76. }
  77. if (videoindex == -1) {
  78. printf("Didn't find a video stream.\n");
  79. return -1;
  80. }
  81. pCodecCtx = fmtCtx->streams[videoindex]->codec;
  82. pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
  83. printf("picture width = %d \n", pCodecCtx->width);
  84. printf("picture height = %d \n", pCodecCtx->height);
  85. printf("Pixel Format = %d \n", pCodecCtx->pix_fmt);
  86. sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, dst_w, dst_h, dst_pix_fmt,
  87. SWS_BILINEAR, NULL, NULL, NULL);
  88. src_bufsize = av_image_alloc(src_data, src_linesize, pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, 16);
  89. dst_bufsize = av_image_alloc(dst_data, dst_linesize, dst_w, dst_h, dst_pix_fmt, 1);
  90. packet = (AVPacket*)av_malloc(sizeof(AVPacket));
  91. int loop = 1000;
  92. // while(loop--){
  93. av_read_frame(fmtCtx, packet);
  94. memcpy(src_data[0], packet->data, packet->size);
  95. sws_scale(sws_ctx, src_data, src_linesize, 0, pCodecCtx->height, dst_data, dst_linesize);
  96. fwrite(dst_data[0], 1, dst_bufsize, fp);
  97. // }
  98. fclose(fp);
  99. av_free_packet(packet);
  100. av_freep(&dst_data[0]);
  101. sws_freeContext(sws_ctx);
  102. avformat_close_input(&fmtCtx);
  103. }
  104. int main(void)
  105. {
  106. avcodec_register_all();
  107. avdevice_register_all();
  108. captureOneFrame();
  109. return 0;
  110. }